Skip to content

Commit 1ad5502

Browse files
committed
Tables: Closer to zero-clear constructor. Lazily clearing name offsets to avoid an iteration in BeginTable(). Removed unused TableSetColumnIsEnabled(), signature went faulty anyway.
1 parent 76964a2 commit 1ad5502

File tree

2 files changed

+35
-64
lines changed

2 files changed

+35
-64
lines changed

imgui_internal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,8 +2070,8 @@ struct ImGuiTable
20702070
bool MemoryCompacted;
20712071
bool HostSkipItems; // Backup of InnerWindow->SkipItem at the end of BeginTable(), because we will overwrite InnerWindow->SkipItem on a per-column basis
20722072

2073-
IMGUI_API ImGuiTable();
2074-
IMGUI_API ~ImGuiTable();
2073+
IMGUI_API ImGuiTable() { memset(this, 0, sizeof(*this)); LastFrameActive = -1; }
2074+
IMGUI_API ~ImGuiTable() { IM_FREE(RawData); }
20752075
};
20762076

20772077
// sizeof() ~ 12
@@ -2286,7 +2286,6 @@ namespace ImGui
22862286
// Tables: Candidates for public API
22872287
IMGUI_API void TableOpenContextMenu(int column_n = -1);
22882288
IMGUI_API void TableSetColumnWidth(int column_n, float width);
2289-
IMGUI_API void TableSetColumnIsEnabled(int column_n, bool enabled);
22902289
IMGUI_API void TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs);
22912290
IMGUI_API int TableGetHoveredColumn(); // May use (TableGetColumnFlags() & ImGuiTableColumnFlags_IsHovered) instead. Return hovered column. return -1 when table is not hovered. return columns_count if the unused space at the right of visible columns is hovered.
22922291
IMGUI_API float TableGetHeaderRowHeight();
@@ -2315,6 +2314,7 @@ namespace ImGui
23152314
IMGUI_API ImRect TableGetCellBgRect(const ImGuiTable* table, int column_n);
23162315
IMGUI_API const char* TableGetColumnName(const ImGuiTable* table, int column_n);
23172316
IMGUI_API ImGuiID TableGetColumnResizeID(const ImGuiTable* table, int column_n, int instance_no = 0);
2317+
IMGUI_API float TableGetMinColumnWidth();
23182318
IMGUI_API void TableSetColumnWidthAutoSingle(ImGuiTable* table, int column_n);
23192319
IMGUI_API void TableSetColumnWidthAutoAll(ImGuiTable* table);
23202320
IMGUI_API void TableRemove(ImGuiTable* table);

imgui_tables.cpp

Lines changed: 32 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ Index of this file:
178178
// Configuration
179179
static const int TABLE_DRAW_CHANNEL_BG0 = 0;
180180
static const int TABLE_DRAW_CHANNEL_BG1_FROZEN = 1;
181-
static const int TABLE_DRAW_CHANNEL_UNCLIPPED = 2; // When using ImGuiTableFlags_NoClip
181+
static const int TABLE_DRAW_CHANNEL_NOCLIP = 2; // When using ImGuiTableFlags_NoClip
182182
static const float TABLE_BORDER_SIZE = 1.0f; // FIXME-TABLE: Currently hard-coded because of clipping assumptions with outer borders rendering.
183183
static const float TABLE_RESIZE_SEPARATOR_HALF_THICKNESS = 4.0f; // Extend outside inner borders.
184184
static const float TABLE_RESIZE_SEPARATOR_FEEDBACK_TIMER = 0.06f; // Delay/timer before making the hover feedback (color+cursor) visible because tables/columns tends to be more cramped.
@@ -226,25 +226,6 @@ inline ImGuiTableFlags TableFixFlags(ImGuiTableFlags flags, ImGuiWindow* outer_w
226226
return flags;
227227
}
228228

229-
ImGuiTable::ImGuiTable()
230-
{
231-
memset(this, 0, sizeof(*this));
232-
SettingsOffset = -1;
233-
InstanceInteracted = -1;
234-
LastFrameActive = -1;
235-
LastResizedColumn = -1;
236-
ContextPopupColumn = -1;
237-
ReorderColumn = -1;
238-
ResizedColumn = -1;
239-
AutoFitSingleStretchColumn = -1;
240-
HoveredColumnBody = HoveredColumnBorder = -1;
241-
}
242-
243-
ImGuiTable::~ImGuiTable()
244-
{
245-
IM_FREE(RawData);
246-
}
247-
248229
ImGuiTable* ImGui::TableFindByID(ImGuiID id)
249230
{
250231
ImGuiContext& g = *GImGui;
@@ -433,9 +414,14 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG
433414
TableResetSettings(table);
434415
if (table->IsInitializing)
435416
{
436-
// Initialize for new settings
417+
// Initialize
437418
table->SettingsOffset = -1;
438419
table->IsSortSpecsDirty = true;
420+
table->InstanceInteracted = -1;
421+
table->ContextPopupColumn = -1;
422+
table->ReorderColumn = table->ResizedColumn = table->LastResizedColumn = -1;
423+
table->AutoFitSingleStretchColumn = -1;
424+
table->HoveredColumnBody = table->HoveredColumnBorder = -1;
439425
for (int n = 0; n < columns_count; n++)
440426
{
441427
ImGuiTableColumn* column = &table->Columns[n];
@@ -473,16 +459,9 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG
473459
inner_window->SkipItems = true;
474460

475461
// Clear names
476-
// FIXME-TABLE: probably could be done differently...
462+
// At this point the ->NameOffset field of each column will be invalid until TableUpdateLayout() or the first call to TableSetupColumn()
477463
if (table->ColumnsNames.Buf.Size > 0)
478-
{
479464
table->ColumnsNames.Buf.resize(0);
480-
for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
481-
{
482-
ImGuiTableColumn* column = &table->Columns[column_n];
483-
column->NameOffset = -1;
484-
}
485-
}
486465

487466
// Apply queued resizing/reordering/hiding requests
488467
TableBeginApplyRequests(table);
@@ -619,14 +598,6 @@ static void TableSetupColumnFlags(ImGuiTable* table, ImGuiTableColumn* column, I
619598
column->Flags = flags | (column->Flags & ImGuiTableColumnFlags_StatusMask_);
620599
}
621600

622-
// Minimum column content width (without padding)
623-
static float TableGetMinColumnWidth()
624-
{
625-
ImGuiContext& g = *GImGui;
626-
//return g.Style.ColumnsMinSpacing; // FIXME-TABLE
627-
return g.Style.FramePadding.x * 1.0f;
628-
}
629-
630601
// Layout columns for the frame. This is in essence the followup to BeginTable().
631602
// Runs on the first call to TableNextRow(), to give a chance for TableSetupColumn() to be called first.
632603
// FIXME-TABLE: Our width (and therefore our WorkRect) will be minimal in the first frame for WidthAutoResize
@@ -659,6 +630,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
659630
if (column_n >= table->DeclColumnsCount)
660631
{
661632
TableSetupColumnFlags(table, column, ImGuiTableColumnFlags_None);
633+
column->NameOffset = -1;
662634
column->UserID = 0;
663635
column->InitStretchWeightOrWidth = -1.0f;
664636
}
@@ -835,7 +807,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
835807
if (column->Flags & ImGuiTableColumnFlags_WidthStretch)
836808
{
837809
// StretchWeight gets converted into WidthRequest
838-
if (!mixed_same_widths)
810+
if (!mixed_same_widths)
839811
{
840812
float weight_ratio = column->StretchWeight / sum_weights_stretched;
841813
column->WidthRequest = IM_FLOOR(ImMax(width_avail_for_stretched_columns * weight_ratio, min_column_width) + 0.01f);
@@ -1074,7 +1046,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
10741046
// Initial state
10751047
ImGuiWindow* inner_window = table->InnerWindow;
10761048
if (table->Flags & ImGuiTableFlags_NoClip)
1077-
table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, TABLE_DRAW_CHANNEL_UNCLIPPED);
1049+
table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, TABLE_DRAW_CHANNEL_NOCLIP);
10781050
else
10791051
inner_window->DrawList->PushClipRect(inner_window->ClipRect.Min, inner_window->ClipRect.Max, false);
10801052
}
@@ -1346,7 +1318,7 @@ void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, flo
13461318
}
13471319

13481320
// Store name (append with zero-terminator in contiguous buffer)
1349-
IM_ASSERT(column->NameOffset == -1);
1321+
column->NameOffset = -1;
13501322
if (label != NULL && label[0] != 0)
13511323
{
13521324
column->NameOffset = (ImS16)table->ColumnsNames.size();
@@ -1389,6 +1361,15 @@ const char* ImGui::TableGetColumnName(int column_n)
13891361
return TableGetColumnName(table, column_n);
13901362
}
13911363

1364+
const char* ImGui::TableGetColumnName(const ImGuiTable* table, int column_n)
1365+
{
1366+
IM_ASSERT(table->IsLayoutLocked == true || column_n <= table->DeclColumnsCount); // NameOffset is invalid otherwise
1367+
const ImGuiTableColumn* column = &table->Columns[column_n];
1368+
if (column->NameOffset == -1)
1369+
return "";
1370+
return &table->ColumnsNames.Buf[column->NameOffset];
1371+
}
1372+
13921373
// We allow querying for an extra column in order to poll the IsHovered state of the right-most section
13931374
ImGuiTableColumnFlags ImGui::TableGetColumnFlags(int column_n)
13941375
{
@@ -1403,17 +1384,6 @@ ImGuiTableColumnFlags ImGui::TableGetColumnFlags(int column_n)
14031384
return table->Columns[column_n].Flags;
14041385
}
14051386

1406-
void ImGui::TableSetColumnIsEnabled(int column_n, bool hidden)
1407-
{
1408-
ImGuiContext& g = *GImGui;
1409-
ImGuiTable* table = g.CurrentTable;
1410-
IM_ASSERT(table != NULL && table->IsLayoutLocked == false);
1411-
if (column_n < 0)
1412-
column_n = table->CurrentColumn;
1413-
IM_ASSERT(column_n >= 0 && column_n < table->ColumnsCount);
1414-
table->Columns[column_n].IsEnabledNextFrame = !hidden;
1415-
}
1416-
14171387
// Return the cell rectangle based on currently known height.
14181388
// - Important: we generally don't know our row height until the end of the row, so Max.y will be incorrect in many situations.
14191389
// The only case where this is correct is if we provided a min_row_height to TableNextRow() and don't go below it.
@@ -1431,14 +1401,6 @@ ImRect ImGui::TableGetCellBgRect(const ImGuiTable* table, int column_n)
14311401
return ImRect(x1, table->RowPosY1, x2, table->RowPosY2);
14321402
}
14331403

1434-
const char* ImGui::TableGetColumnName(const ImGuiTable* table, int column_n)
1435-
{
1436-
const ImGuiTableColumn* column = &table->Columns[column_n];
1437-
if (column->NameOffset == -1)
1438-
return "";
1439-
return &table->ColumnsNames.Buf[column->NameOffset];
1440-
}
1441-
14421404
// Return the resizing ID for the right-side of the given column.
14431405
ImGuiID ImGui::TableGetColumnResizeID(const ImGuiTable* table, int column_n, int instance_no)
14441406
{
@@ -1810,8 +1772,8 @@ void ImGui::TableBeginCell(ImGuiTable* table, int column_n)
18101772
if (table->Flags & ImGuiTableFlags_NoClip)
18111773
{
18121774
// FIXME: if we end up drawing all borders/bg in EndTable, could remove this and just assert that channel hasn't changed.
1813-
table->DrawSplitter.SetCurrentChannel(window->DrawList, TABLE_DRAW_CHANNEL_UNCLIPPED);
1814-
//IM_ASSERT(table->DrawSplitter._Current == TABLE_DRAW_CHANNEL_UNCLIPPED);
1775+
table->DrawSplitter.SetCurrentChannel(window->DrawList, TABLE_DRAW_CHANNEL_NOCLIP);
1776+
//IM_ASSERT(table->DrawSplitter._Current == TABLE_DRAW_CHANNEL_NOCLIP);
18151777
}
18161778
else
18171779
{
@@ -1845,12 +1807,21 @@ void ImGui::TableEndCell(ImGuiTable* table)
18451807
//-------------------------------------------------------------------------
18461808
// [SECTION] Tables: Columns width management
18471809
//-------------------------------------------------------------------------
1810+
// - TableGetMinColumnWidth() [Internal]
18481811
// - TableSetColumnWidth()
18491812
// - TableSetColumnWidthAutoSingle() [Internal]
18501813
// - TableSetColumnWidthAutoAll() [Internal]
18511814
// - TableUpdateColumnsWeightFromWidth() [Internal]
18521815
//-------------------------------------------------------------------------
18531816

1817+
// Minimum column content width (without padding)
1818+
float ImGui::TableGetMinColumnWidth()
1819+
{
1820+
ImGuiContext& g = *GImGui;
1821+
//return g.Style.ColumnsMinSpacing; // FIXME-TABLE
1822+
return g.Style.FramePadding.x * 1.0f;
1823+
}
1824+
18541825
// 'width' = inner column width, without padding
18551826
void ImGui::TableSetColumnWidth(int column_n, float width)
18561827
{

0 commit comments

Comments
 (0)