Skip to content

Commit 52f24df

Browse files
committed
Tables: comments, tweaks, added internal TableSetColumnVisible(), merged the two TableSetColumnWidth().
1 parent bf197c7 commit 52f24df

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

imgui_demo.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4456,9 +4456,9 @@ static void ShowDemoWindowTables()
44564456
| ImGuiTableFlags_SizingPolicyFixedX
44574457
;
44584458

4459-
enum ContentsType { CT_Text, CT_Button, CT_SmallButton, CT_FillButton, CT_Selectable };
4459+
enum ContentsType { CT_Text, CT_Button, CT_SmallButton, CT_FillButton, CT_Selectable, CT_SelectableSpanRow };
44604460
static int contents_type = CT_Button;
4461-
const char* contents_type_names[] = { "Text", "Button", "SmallButton", "FillButton", "Selectable" };
4461+
const char* contents_type_names[] = { "Text", "Button", "SmallButton", "FillButton", "Selectable", "Selectable (span row)" };
44624462
static int freeze_cols = 1;
44634463
static int freeze_rows = 1;
44644464
static int items_count = IM_ARRAYSIZE(template_items_names);
@@ -4662,9 +4662,10 @@ static void ShowDemoWindowTables()
46624662
ImGui::SmallButton(label);
46634663
else if (contents_type == CT_FillButton)
46644664
ImGui::Button(label, ImVec2(-FLT_MIN, 0.0f));
4665-
else if (contents_type == CT_Selectable)
4665+
else if (contents_type == CT_Selectable || contents_type == CT_SelectableSpanRow)
46664666
{
4667-
if (ImGui::Selectable(label, item_is_selected, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap, ImVec2(0, row_min_height)))
4667+
ImGuiSelectableFlags selectable_flags = (contents_type == CT_SelectableSpanRow) ? ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap : ImGuiSelectableFlags_None;
4668+
if (ImGui::Selectable(label, item_is_selected, selectable_flags, ImVec2(0, row_min_height)))
46684669
{
46694670
if (ImGui::GetIO().KeyCtrl)
46704671
{

imgui_internal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ struct ImGuiTableCellData
19571957
struct ImGuiTable
19581958
{
19591959
ImGuiID ID;
1960-
ImGuiTableFlags Flags;
1960+
ImGuiTableFlags Flags;
19611961
void* RawData; // Single allocation to hold Columns[], DisplayOrderToIndex[] and RowCellData[]
19621962
ImSpan<ImGuiTableColumn> Columns; // Point within RawData[]
19631963
ImSpan<ImS8> DisplayOrderToIndex; // Point within RawData[]. Store display order of columns (when not reordered, the values are 0...Count-1)
@@ -2271,12 +2271,12 @@ namespace ImGui
22712271
IMGUI_API void TableUpdateLayout(ImGuiTable* table);
22722272
IMGUI_API void TableUpdateBorders(ImGuiTable* table);
22732273
IMGUI_API void TableSetColumnWidth(int column_n, float width);
2274-
IMGUI_API void TableSetColumnWidth(ImGuiTable* table, ImGuiTableColumn* column, float width);
2274+
IMGUI_API void TableSetColumnVisible(int column_n, bool visible);
22752275
IMGUI_API void TableDrawBorders(ImGuiTable* table);
22762276
IMGUI_API void TableDrawContextMenu(ImGuiTable* table);
22772277
IMGUI_API void TableOpenContextMenu(int column_n = -1);
22782278
IMGUI_API void TableReorderDrawChannelsForMerge(ImGuiTable* table);
2279-
IMGUI_API void TableSetColumnSortDirection(ImGuiTable* table, int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs);
2279+
IMGUI_API void TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs);
22802280
IMGUI_API void TableSortSpecsSanitize(ImGuiTable* table);
22812281
IMGUI_API void TableSortSpecsBuild(ImGuiTable* table);
22822282
IMGUI_API void TableBeginRow(ImGuiTable* table);

imgui_tables.cpp

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
// | - TableUpdateColumnsWeightFromWidth()- recompute columns weights (of stretch columns) from their respective width
7070
// - TableSetupColumn() user submit columns details (optional)
7171
// - TableSetupScrollFreeze() user submit scroll freeze information (optional)
72-
// - TableUpdateLayout() [Internal] automatically called by the FIRST call to TableNextRow() or Table*Header(): lock all widths, columns positions, clipping rectangles
72+
// - TableUpdateLayout() [Internal] automatically called by the FIRST call to TableNextRow() or TableHeadersRow(): lock all widths, columns positions, clipping rectangles
7373
// | TableUpdateDrawChannels() - setup ImDrawList channels
7474
// | TableUpdateBorders() - detect hovering columns for resize, ahead of contents submission
7575
// | TableDrawContextMenu() - draw right-click context menu
@@ -78,8 +78,11 @@
7878
// | TableSortSpecsClickColumn() - when left-clicked: alter sort order and sort direction
7979
// | TableOpenContextMenu() - when right-clicked: trigger opening of the default context menu
8080
// - TableGetSortSpecs() user queries updated sort specs (optional, generally after submitting headers)
81-
// - TableNextRow() / TableNextColumn() user begin into the first row, also automatically called by TableHeadersRow()
82-
// | TableEndCell() - close existing cell if not the first time
81+
// - TableNextRow() user begin into a new row (also automatically called by TableHeadersRow())
82+
// | TableEndRow() - finish existing row
83+
// | TableBeginRow() - add a new row
84+
// - TableSetColumnIndex() / TableNextColumn() user begin into a cell
85+
// | TableEndCell() - close existing cell
8386
// | TableBeginCell() - enter into current cell
8487
// - [...] user emit contents
8588
//-----------------------------------------------------------------------------
@@ -430,7 +433,7 @@ void ImGui::TableBeginUpdateColumns(ImGuiTable* table)
430433
if (table->InstanceCurrent == 0)
431434
{
432435
if (table->ResizedColumn != -1 && table->ResizedColumnNextWidth != FLT_MAX)
433-
TableSetColumnWidth(table, &table->Columns[table->ResizedColumn], table->ResizedColumnNextWidth);
436+
TableSetColumnWidth(table->ResizedColumn, table->ResizedColumnNextWidth);
434437
table->LastResizedColumn = table->ResizedColumn;
435438
table->ResizedColumnNextWidth = FLT_MAX;
436439
table->ResizedColumn = -1;
@@ -1215,21 +1218,16 @@ static void TableUpdateColumnsWeightFromWidth(ImGuiTable* table)
12151218
}
12161219
}
12171220

1218-
// Public wrapper
12191221
// 'width' = inner column width, without padding
12201222
void ImGui::TableSetColumnWidth(int column_n, float width)
12211223
{
12221224
ImGuiContext& g = *GImGui;
12231225
ImGuiTable* table = g.CurrentTable;
1224-
IM_ASSERT(table != NULL);
1225-
IM_ASSERT(table->IsLayoutLocked == false);
1226+
IM_ASSERT(table != NULL && table->IsLayoutLocked == false);
12261227
IM_ASSERT(column_n >= 0 && column_n < table->ColumnsCount);
1227-
TableSetColumnWidth(table, &table->Columns[column_n], width);
1228-
}
1228+
ImGuiTableColumn* column_0 = &table->Columns[column_n];
1229+
float column_0_width = width;
12291230

1230-
// [Internal]
1231-
void ImGui::TableSetColumnWidth(ImGuiTable* table, ImGuiTableColumn* column_0, float column_0_width)
1232-
{
12331231
// Constraints
12341232
const float min_width = TableGetMinColumnWidth();
12351233
float max_width_0 = FLT_MAX;
@@ -1303,6 +1301,16 @@ void ImGui::TableSetColumnWidth(ImGuiTable* table, ImGuiTableColumn* column_0, f
13031301
}
13041302
}
13051303

1304+
// Public wrapper
1305+
void ImGui::TableSetColumnVisible(int column_n, bool visible)
1306+
{
1307+
ImGuiContext& g = *GImGui;
1308+
ImGuiTable* table = g.CurrentTable;
1309+
IM_ASSERT(table != NULL && table->IsLayoutLocked == false);
1310+
IM_ASSERT(column_n >= 0 && column_n < table->ColumnsCount);
1311+
table->Columns[column_n].IsVisibleNextFrame = visible;
1312+
}
1313+
13061314
// Allocate draw channels. Called by TableUpdateLayout()
13071315
// - We allocate them following storage order instead of display order so reordering columns won't needlessly
13081316
// increase overall dormant memory cost.
@@ -1314,10 +1322,11 @@ void ImGui::TableSetColumnWidth(ImGuiTable* table, ImGuiTableColumn* column_0, f
13141322
// - We allocate 1 or 2 background draw channels. This is because we know PushTableBackground() is only used for
13151323
// horizontal spanning. If we allowed vertical spanning we'd need one background draw channel per merge group (1-4).
13161324
// Draw channel allocation (before merging):
1317-
// - NoClip --> 1+1 channels: background + foreground (same clip rect == 1 draw call)
1318-
// - Clip --> 1+N channels
1319-
// - FreezeRows --> 1+N*2 (unless scrolling value is zero)
1320-
// - FreezeRows || FreezeColunns --> 2+N*2 (unless scrolling value is zero)
1325+
// - NoClip --> 1+D+1 channels: background + foreground (same clip rect == 1 draw call)
1326+
// - Clip --> 1+D+N channels
1327+
// - FreezeRows --> 1+D+N*2 (unless scrolling value is zero)
1328+
// - FreezeRows || FreezeColunns --> 2+D+N*2 (unless scrolling value is zero)
1329+
// Where D is 1 if any column is clipped or hidden (dummy channel) otherwise 0.
13211330
void ImGui::TableUpdateDrawChannels(ImGuiTable* table)
13221331
{
13231332
const int freeze_row_multiplier = (table->FreezeRowsCount > 0) ? 2 : 1;
@@ -1466,12 +1475,13 @@ void ImGui::TableReorderDrawChannelsForMerge(ImGuiTable* table)
14661475
// 2. Rewrite channel list in our preferred order
14671476
if (merge_group_mask != 0)
14681477
{
1469-
// Use shared temporary storage so the allocation gets amortized
1470-
g.DrawChannelsTempMergeBuffer.resize(splitter->_Count - 1);
1478+
// We skip background channel 0 from the shuffling since it won't move (see channels allocation in TableUpdateDrawChannels()).
1479+
const int LEADING_DRAW_CHANNELS = 1;
1480+
g.DrawChannelsTempMergeBuffer.resize(splitter->_Count - LEADING_DRAW_CHANNELS); // Use shared temporary storage so the allocation gets amortized
14711481
ImDrawChannel* dst_tmp = g.DrawChannelsTempMergeBuffer.Data;
1472-
ImBitArray<IMGUI_TABLE_MAX_DRAW_CHANNELS> remaining_mask; // We need 130-bit of storage
1482+
ImBitArray<IMGUI_TABLE_MAX_DRAW_CHANNELS> remaining_mask; // We need 131-bit of storage
14731483
remaining_mask.ClearBits();
1474-
remaining_mask.SetBitRange(1, splitter->_Count - 1); // Background channel 0 == table->BgDrawChannlFrozen, not part of the merge (see channel allocation in TableUpdateDrawChannels)
1484+
remaining_mask.SetBitRange(LEADING_DRAW_CHANNELS, splitter->_Count - 1);
14751485
remaining_mask.ClearBit(table->BgDrawChannelUnfrozen);
14761486
int remaining_count = splitter->_Count - ((table->BgDrawChannelUnfrozen == 0) ? 1 : 2);
14771487
//ImRect host_rect = (table->InnerWindow == table->OuterWindow) ? table->InnerClipRect : table->HostClipRect;
@@ -1536,7 +1546,7 @@ void ImGui::TableReorderDrawChannelsForMerge(ImGuiTable* table)
15361546
remaining_count--;
15371547
}
15381548
IM_ASSERT(dst_tmp == g.DrawChannelsTempMergeBuffer.Data + g.DrawChannelsTempMergeBuffer.Size);
1539-
memcpy(splitter->_Channels.Data + 1, g.DrawChannelsTempMergeBuffer.Data, (splitter->_Count - 1) * sizeof(ImDrawChannel));
1549+
memcpy(splitter->_Channels.Data + LEADING_DRAW_CHANNELS, g.DrawChannelsTempMergeBuffer.Data, (splitter->_Count - LEADING_DRAW_CHANNELS) * sizeof(ImDrawChannel));
15401550
}
15411551
}
15421552

@@ -1726,10 +1736,9 @@ void ImGui::TableEndRow(ImGuiTable* table)
17261736
// Decide of top border color
17271737
ImU32 border_col = 0;
17281738
const float border_size = TABLE_BORDER_SIZE;
1729-
if (table->CurrentRow != 0 || table->InnerWindow == table->OuterWindow)
1739+
if (table->CurrentRow > 0 || table->InnerWindow == table->OuterWindow)
17301740
if (table->Flags & ImGuiTableFlags_BordersInnerH)
1731-
if (table->CurrentRow > 0)
1732-
border_col = (table->LastRowFlags & ImGuiTableRowFlags_Headers) ? table->BorderColorStrong : table->BorderColorLight;
1741+
border_col = (table->LastRowFlags & ImGuiTableRowFlags_Headers) ? table->BorderColorStrong : table->BorderColorLight;
17331742

17341743
const bool draw_cell_bg_color = table->RowCellDataCurrent >= 0;
17351744
const bool draw_strong_bottom_border = unfreeze_rows_actual;
@@ -1791,7 +1800,6 @@ void ImGui::TableEndRow(ImGuiTable* table)
17911800
{
17921801
IM_ASSERT(table->IsUnfrozen == false);
17931802
table->IsUnfrozen = true;
1794-
table->DrawSplitter.SetCurrentChannel(window->DrawList, table->BgDrawChannelUnfrozen);
17951803

17961804
// BgClipRect starts as table->InnerClipRect, reduce it now and make BgClipRectForDrawCmd == BgClipRect
17971805
float y0 = ImMax(table->RowPosY2 + 1, window->InnerClipRect.Min.y);
@@ -2322,7 +2330,7 @@ void ImGui::TableHeader(const char* label)
23222330
sort_direction = (column->Flags & ImGuiTableColumnFlags_PreferSortDescending) ? ImGuiSortDirection_Descending : ImGuiSortDirection_Ascending;
23232331
else
23242332
sort_direction = (column->SortDirection == ImGuiSortDirection_Ascending) ? ImGuiSortDirection_Descending : ImGuiSortDirection_Ascending;
2325-
TableSetColumnSortDirection(table, column_n, sort_direction, g.IO.KeyShift);
2333+
TableSetColumnSortDirection(column_n, sort_direction, g.IO.KeyShift);
23262334
}
23272335
}
23282336

@@ -2347,8 +2355,11 @@ void ImGui::TableHeader(const char* label)
23472355

23482356
// Note that the NoSortAscending/NoSortDescending flags are processed in TableSortSpecsSanitize(), and they may change/revert
23492357
// the value of SortDirection. We could technically also do it here but it would be unnecessary and duplicate code.
2350-
void ImGui::TableSetColumnSortDirection(ImGuiTable* table, int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs)
2358+
void ImGui::TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs)
23512359
{
2360+
ImGuiContext& g = *GImGui;
2361+
ImGuiTable* table = g.CurrentTable;
2362+
23522363
if (!(table->Flags & ImGuiTableFlags_MultiSortable))
23532364
append_to_sort_specs = false;
23542365

0 commit comments

Comments
 (0)