Skip to content

Commit b7c83e4

Browse files
committed
Tables: moved TableGetSortSpecs(), TableGetColumnIsSorted(), TableFixColumnSortDirection(), TableSetColumnSortDirection() to their own section.
1 parent d6b1f0d commit b7c83e4

File tree

2 files changed

+82
-79
lines changed

2 files changed

+82
-79
lines changed

imgui_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,7 @@ namespace ImGui
22912291
IMGUI_API void TableReorderDrawChannelsForMerge(ImGuiTable* table);
22922292
IMGUI_API void TableSortSpecsSanitize(ImGuiTable* table);
22932293
IMGUI_API void TableSortSpecsBuild(ImGuiTable* table);
2294+
IMGUI_API void TableFixColumnSortDirection(ImGuiTableColumn* column);
22942295
IMGUI_API void TableBeginRow(ImGuiTable* table);
22952296
IMGUI_API void TableEndRow(ImGuiTable* table);
22962297
IMGUI_API void TableBeginCell(ImGuiTable* table, int column_n);

imgui_tables.cpp

Lines changed: 81 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Index of this file:
77
88
// [SECTION] Tables: BeginTable, EndTable, etc.
9+
// [SECTION] Tables: Sorting
910
// [SECTION] Tables: Headers
1011
// [SECTION] Tables: Context Menu
1112
// [SECTION] Tables: Settings (.ini data)
@@ -556,19 +557,6 @@ static ImGuiTableColumnFlags TableFixColumnFlags(ImGuiTable* table, ImGuiTableCo
556557
return flags;
557558
}
558559

559-
static void TableFixColumnSortDirection(ImGuiTableColumn* column)
560-
{
561-
// Initial sort state
562-
if (column->SortDirection == ImGuiSortDirection_None)
563-
column->SortDirection = (column->Flags & ImGuiTableColumnFlags_PreferSortDescending) ? (ImS8)ImGuiSortDirection_Descending : (ImS8)(ImGuiSortDirection_Ascending);
564-
565-
// Handle NoSortAscending/NoSortDescending
566-
if (column->SortDirection == ImGuiSortDirection_Ascending && (column->Flags & ImGuiTableColumnFlags_NoSortAscending))
567-
column->SortDirection = ImGuiSortDirection_Descending;
568-
else if (column->SortDirection == ImGuiSortDirection_Descending && (column->Flags & ImGuiTableColumnFlags_NoSortDescending))
569-
column->SortDirection = ImGuiSortDirection_Ascending;
570-
}
571-
572560
// Minimum column content width (without padding)
573561
static float TableGetMinColumnWidth()
574562
{
@@ -2172,72 +2160,6 @@ void ImGui::TablePopBackgroundChannel()
21722160
table->DrawSplitter.SetCurrentChannel(window->DrawList, column->DrawChannelCurrent);
21732161
}
21742162

2175-
// Note that the NoSortAscending/NoSortDescending flags are processed in TableSortSpecsSanitize(), and they may change/revert
2176-
// the value of SortDirection. We could technically also do it here but it would be unnecessary and duplicate code.
2177-
void ImGui::TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs)
2178-
{
2179-
ImGuiContext& g = *GImGui;
2180-
ImGuiTable* table = g.CurrentTable;
2181-
2182-
if (!(table->Flags & ImGuiTableFlags_MultiSortable))
2183-
append_to_sort_specs = false;
2184-
2185-
ImS8 sort_order_max = 0;
2186-
if (append_to_sort_specs)
2187-
for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++)
2188-
sort_order_max = ImMax(sort_order_max, table->Columns[other_column_n].SortOrder);
2189-
2190-
ImGuiTableColumn* column = &table->Columns[column_n];
2191-
column->SortDirection = (ImS8)sort_direction;
2192-
if (column->SortOrder == -1 || !append_to_sort_specs)
2193-
column->SortOrder = append_to_sort_specs ? sort_order_max + 1 : 0;
2194-
2195-
for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++)
2196-
{
2197-
ImGuiTableColumn* other_column = &table->Columns[other_column_n];
2198-
if (other_column != column && !append_to_sort_specs)
2199-
other_column->SortOrder = -1;
2200-
TableFixColumnSortDirection(other_column);
2201-
}
2202-
table->IsSettingsDirty = true;
2203-
table->IsSortSpecsDirty = true;
2204-
}
2205-
2206-
// Return NULL if no sort specs (most often when ImGuiTableFlags_Sortable is not set)
2207-
// You can sort your data again when 'SpecsChanged == true'. It will be true with sorting specs have changed since
2208-
// last call, or the first time.
2209-
// Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable()!
2210-
ImGuiTableSortSpecs* ImGui::TableGetSortSpecs()
2211-
{
2212-
ImGuiContext& g = *GImGui;
2213-
ImGuiTable* table = g.CurrentTable;
2214-
IM_ASSERT(table != NULL);
2215-
2216-
if (!(table->Flags & ImGuiTableFlags_Sortable))
2217-
return NULL;
2218-
2219-
// Require layout (in case TableHeadersRow() hasn't been called) as it may alter IsSortSpecsDirty in some paths.
2220-
if (!table->IsLayoutLocked)
2221-
TableUpdateLayout(table);
2222-
2223-
if (table->IsSortSpecsDirty)
2224-
TableSortSpecsBuild(table);
2225-
2226-
return table->SortSpecs.SpecsCount ? &table->SortSpecs : NULL;
2227-
}
2228-
2229-
bool ImGui::TableGetColumnIsSorted(int column_n)
2230-
{
2231-
ImGuiContext& g = *GImGui;
2232-
ImGuiTable* table = g.CurrentTable;
2233-
if (!table)
2234-
return false;
2235-
if (column_n < 0)
2236-
column_n = table->CurrentColumn;
2237-
ImGuiTableColumn* column = &table->Columns[column_n];
2238-
return (column->SortOrder != -1);
2239-
}
2240-
22412163
// Return -1 when table is not hovered. return columns_count if the unused space at the right of visible columns is hovered.
22422164
int ImGui::TableGetHoveredColumn()
22432165
{
@@ -2301,6 +2223,86 @@ void ImGui::TableSetBgColor(ImGuiTableBgTarget bg_target, ImU32 color, int colum
23012223
// - TableSortSpecsBuild() [Internal]
23022224
//-------------------------------------------------------------------------
23032225

2226+
// Return NULL if no sort specs (most often when ImGuiTableFlags_Sortable is not set)
2227+
// You can sort your data again when 'SpecsChanged == true'. It will be true with sorting specs have changed since
2228+
// last call, or the first time.
2229+
// Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable()!
2230+
ImGuiTableSortSpecs* ImGui::TableGetSortSpecs()
2231+
{
2232+
ImGuiContext& g = *GImGui;
2233+
ImGuiTable* table = g.CurrentTable;
2234+
IM_ASSERT(table != NULL);
2235+
2236+
if (!(table->Flags & ImGuiTableFlags_Sortable))
2237+
return NULL;
2238+
2239+
// Require layout (in case TableHeadersRow() hasn't been called) as it may alter IsSortSpecsDirty in some paths.
2240+
if (!table->IsLayoutLocked)
2241+
TableUpdateLayout(table);
2242+
2243+
if (table->IsSortSpecsDirty)
2244+
TableSortSpecsBuild(table);
2245+
2246+
return table->SortSpecs.SpecsCount ? &table->SortSpecs : NULL;
2247+
}
2248+
2249+
bool ImGui::TableGetColumnIsSorted(int column_n)
2250+
{
2251+
ImGuiContext& g = *GImGui;
2252+
ImGuiTable* table = g.CurrentTable;
2253+
if (!table)
2254+
return false;
2255+
if (column_n < 0)
2256+
column_n = table->CurrentColumn;
2257+
ImGuiTableColumn* column = &table->Columns[column_n];
2258+
return (column->SortOrder != -1);
2259+
}
2260+
2261+
void ImGui::TableFixColumnSortDirection(ImGuiTableColumn* column)
2262+
{
2263+
// Initial sort state
2264+
if (column->SortDirection == ImGuiSortDirection_None)
2265+
column->SortDirection = (column->Flags & ImGuiTableColumnFlags_PreferSortDescending) ? (ImS8)ImGuiSortDirection_Descending : (ImS8)(ImGuiSortDirection_Ascending);
2266+
2267+
// Handle NoSortAscending/NoSortDescending
2268+
if (column->SortDirection == ImGuiSortDirection_Ascending && (column->Flags & ImGuiTableColumnFlags_NoSortAscending))
2269+
column->SortDirection = ImGuiSortDirection_Descending;
2270+
else if (column->SortDirection == ImGuiSortDirection_Descending && (column->Flags & ImGuiTableColumnFlags_NoSortDescending))
2271+
column->SortDirection = ImGuiSortDirection_Ascending;
2272+
}
2273+
2274+
2275+
// Note that the NoSortAscending/NoSortDescending flags are processed in TableSortSpecsSanitize(), and they may change/revert
2276+
// the value of SortDirection. We could technically also do it here but it would be unnecessary and duplicate code.
2277+
void ImGui::TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs)
2278+
{
2279+
ImGuiContext& g = *GImGui;
2280+
ImGuiTable* table = g.CurrentTable;
2281+
2282+
if (!(table->Flags & ImGuiTableFlags_MultiSortable))
2283+
append_to_sort_specs = false;
2284+
2285+
ImS8 sort_order_max = 0;
2286+
if (append_to_sort_specs)
2287+
for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++)
2288+
sort_order_max = ImMax(sort_order_max, table->Columns[other_column_n].SortOrder);
2289+
2290+
ImGuiTableColumn* column = &table->Columns[column_n];
2291+
column->SortDirection = (ImS8)sort_direction;
2292+
if (column->SortOrder == -1 || !append_to_sort_specs)
2293+
column->SortOrder = append_to_sort_specs ? sort_order_max + 1 : 0;
2294+
2295+
for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++)
2296+
{
2297+
ImGuiTableColumn* other_column = &table->Columns[other_column_n];
2298+
if (other_column != column && !append_to_sort_specs)
2299+
other_column->SortOrder = -1;
2300+
TableFixColumnSortDirection(other_column);
2301+
}
2302+
table->IsSettingsDirty = true;
2303+
table->IsSortSpecsDirty = true;
2304+
}
2305+
23042306
void ImGui::TableSortSpecsSanitize(ImGuiTable* table)
23052307
{
23062308
IM_ASSERT(table->Flags & ImGuiTableFlags_Sortable);

0 commit comments

Comments
 (0)