Skip to content

Commit b194df4

Browse files
committed
Tables: exposed status flags via TableGetColumnFlags(), removed TableGetColumnIsSorted()
Scoped width auto calc.
1 parent 984c4cb commit b194df4

File tree

4 files changed

+100
-60
lines changed

4 files changed

+100
-60
lines changed

imgui.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,9 @@ namespace ImGui
668668
// you may prefer using TableNextColumn() instead of TableNextRow() + TableSetColumnIndex().
669669
// TableNextColumn() will automatically wrap-around into the next row if needed.
670670
// - IMPORTANT: Comparatively to the old Columns() API, we need to call TableNextColumn() for the first column!
671-
// - Both TableSetColumnIndex() and TableNextColumn() return false when the column is not visible, so you can
672-
// skip submitting the contents of a cell BUT ONLY if you know it is not going to contribute to row height.
671+
// - Both TableSetColumnIndex() and TableNextColumn() return true when the column is visible or performing
672+
// width measurements. Otherwise, you may skip submitting the contents of a cell/column, BUT ONLY if you know
673+
// it is not going to contribute to row height.
673674
// In many situations, you may skip submitting contents for every columns but one (e.g. the first one).
674675
// - Summary of possible call flow:
675676
// ----------------------------------------------------------------------------------------------------------
@@ -704,12 +705,12 @@ namespace ImGui
704705
// When 'SpecsDirty == true' you should sort your data. It will be true when sorting specs have changed since last call, or the first time.
705706
// Make sure to set 'SpecsDirty = false' after sorting, else you may wastefully sort your data every frame!
706707
// Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable().
707-
IMGUI_API int TableGetColumnCount(); // return number of columns (value passed to BeginTable)
708-
IMGUI_API const char* TableGetColumnName(int column_n = -1); // return "" if column didn't have a name declared by TableSetupColumn(). Pass -1 to use current column.
709-
IMGUI_API bool TableGetColumnIsSorted(int column_n = -1); // return true if column is included in the sort specs. Rarely used, can be useful to tell if a data change should trigger resort. Equivalent to test ImGuiTableSortSpecs's ->ColumnsMask & (1 << column_n). Pass -1 to use current column.
710-
IMGUI_API int TableGetHoveredColumn(); // 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.
711-
IMGUI_API ImGuiTableSortSpecs* TableGetSortSpecs(); // get latest sort specs for the table (NULL if not sorting).
712-
IMGUI_API void TableSetBgColor(ImGuiTableBgTarget bg_target, ImU32 color, int column_n = -1); // change the color of a cell, row, or column. See ImGuiTableBgTarget_ flags for details.
708+
IMGUI_API int TableGetColumnCount(); // return number of columns (value passed to BeginTable)
709+
IMGUI_API const char* TableGetColumnName(int column_n = -1); // return "" if column didn't have a name declared by TableSetupColumn(). Pass -1 to use current column.
710+
IMGUI_API ImGuiTableColumnFlags TableGetColumnFlags(int column_n = -1); // return column flags so you can query their Enabled/Visible/Sorted/Hovered status flags.
711+
IMGUI_API int TableGetHoveredColumn(); // 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.
712+
IMGUI_API ImGuiTableSortSpecs* TableGetSortSpecs(); // get latest sort specs for the table (NULL if not sorting).
713+
IMGUI_API void TableSetBgColor(ImGuiTableBgTarget bg_target, ImU32 color, int column_n = -1); // change the color of a cell, row, or column. See ImGuiTableBgTarget_ flags for details.
713714

714715
// Legacy Columns API (2020: prefer using Tables!)
715716
// - You can also use SameLine(pos_x) to mimic simplified columns.
@@ -1094,6 +1095,7 @@ enum ImGuiTableFlags_
10941095
// Flags for ImGui::TableSetupColumn()
10951096
enum ImGuiTableColumnFlags_
10961097
{
1098+
// Input configuration flags
10971099
ImGuiTableColumnFlags_None = 0,
10981100
ImGuiTableColumnFlags_DefaultHide = 1 << 0, // Default as a hidden/disabled column.
10991101
ImGuiTableColumnFlags_DefaultSort = 1 << 1, // Default as a sorting column.
@@ -1113,9 +1115,16 @@ enum ImGuiTableColumnFlags_
11131115
ImGuiTableColumnFlags_IndentEnable = 1 << 15, // Use current Indent value when entering cell (default for column 0).
11141116
ImGuiTableColumnFlags_IndentDisable = 1 << 16, // Ignore current Indent value when entering cell (default for columns > 0). Indentation changes _within_ the cell will still be honored.
11151117

1118+
// Output status flags, read-only via TableGetColumnFlags()
1119+
ImGuiTableColumnFlags_IsEnabled = 1 << 20, // Status: is enabled == not hidden by user/api (referred to as "Hide" in _DefaultHide and _NoHide) flags.
1120+
ImGuiTableColumnFlags_IsVisible = 1 << 21, // Status: is visible == is enabled AND not clipped by scrolling.
1121+
ImGuiTableColumnFlags_IsSorted = 1 << 22, // Status: is currently part of the sort specs
1122+
ImGuiTableColumnFlags_IsHovered = 1 << 23, // Status: is hovered by mouse
1123+
11161124
// [Internal] Combinations and masks
11171125
ImGuiTableColumnFlags_WidthMask_ = ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_WidthAutoResize,
11181126
ImGuiTableColumnFlags_IndentMask_ = ImGuiTableColumnFlags_IndentEnable | ImGuiTableColumnFlags_IndentDisable,
1127+
ImGuiTableColumnFlags_StatusMask_ = ImGuiTableColumnFlags_IsEnabled | ImGuiTableColumnFlags_IsVisible | ImGuiTableColumnFlags_IsSorted | ImGuiTableColumnFlags_IsHovered,
11191128
ImGuiTableColumnFlags_NoDirectResize_ = 1 << 30 // [Internal] Disable user resizing this column directly (it may however we resized indirectly from its left edge)
11201129
};
11211130

imgui_demo.cpp

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,23 +3344,34 @@ static void EditTableColumnsFlags(ImGuiTableColumnFlags* p_flags)
33443344
{
33453345
ImGui::CheckboxFlags("_DefaultHide", p_flags, ImGuiTableColumnFlags_DefaultHide);
33463346
ImGui::CheckboxFlags("_DefaultSort", p_flags, ImGuiTableColumnFlags_DefaultSort);
3347-
ImGui::CheckboxFlags("_WidthStretch", p_flags, ImGuiTableColumnFlags_WidthStretch);
3348-
ImGui::CheckboxFlags("_WidthFixed", p_flags, ImGuiTableColumnFlags_WidthFixed);
3349-
ImGui::CheckboxFlags("_WidthAutoResize", p_flags, ImGuiTableColumnFlags_WidthAutoResize);
3347+
if (ImGui::CheckboxFlags("_WidthStretch", p_flags, ImGuiTableColumnFlags_WidthStretch))
3348+
*p_flags &= ~(ImGuiTableColumnFlags_WidthMask_ ^ ImGuiTableColumnFlags_WidthStretch);
3349+
if (ImGui::CheckboxFlags("_WidthFixed", p_flags, ImGuiTableColumnFlags_WidthFixed))
3350+
*p_flags &= ~(ImGuiTableColumnFlags_WidthMask_ ^ ImGuiTableColumnFlags_WidthFixed);
3351+
if (ImGui::CheckboxFlags("_WidthAutoResize", p_flags, ImGuiTableColumnFlags_WidthAutoResize))
3352+
*p_flags &= ~(ImGuiTableColumnFlags_WidthMask_ ^ ImGuiTableColumnFlags_WidthAutoResize);
33503353
ImGui::CheckboxFlags("_NoResize", p_flags, ImGuiTableColumnFlags_NoResize);
33513354
ImGui::CheckboxFlags("_NoReorder", p_flags, ImGuiTableColumnFlags_NoReorder);
33523355
ImGui::CheckboxFlags("_NoHide", p_flags, ImGuiTableColumnFlags_NoHide);
33533356
ImGui::CheckboxFlags("_NoClip", p_flags, ImGuiTableColumnFlags_NoClip);
33543357
ImGui::CheckboxFlags("_NoSort", p_flags, ImGuiTableColumnFlags_NoSort);
33553358
ImGui::CheckboxFlags("_NoSortAscending", p_flags, ImGuiTableColumnFlags_NoSortAscending);
33563359
ImGui::CheckboxFlags("_NoSortDescending", p_flags, ImGuiTableColumnFlags_NoSortDescending);
3357-
ImGui::CheckboxFlags("_NoSHeaderWidth", p_flags, ImGuiTableColumnFlags_NoHeaderWidth);
3360+
ImGui::CheckboxFlags("_NoHeaderWidth", p_flags, ImGuiTableColumnFlags_NoHeaderWidth);
33583361
ImGui::CheckboxFlags("_PreferSortAscending", p_flags, ImGuiTableColumnFlags_PreferSortAscending);
33593362
ImGui::CheckboxFlags("_PreferSortDescending", p_flags, ImGuiTableColumnFlags_PreferSortDescending);
33603363
ImGui::CheckboxFlags("_IndentEnable", p_flags, ImGuiTableColumnFlags_IndentEnable); ImGui::SameLine(); HelpMarker("Default for column 0");
33613364
ImGui::CheckboxFlags("_IndentDisable", p_flags, ImGuiTableColumnFlags_IndentDisable); ImGui::SameLine(); HelpMarker("Default for column >0");
33623365
}
33633366

3367+
static void ShowTableColumnsStatusFlags(ImGuiTableColumnFlags flags)
3368+
{
3369+
ImGui::CheckboxFlags("_IsEnabled", &flags, ImGuiTableColumnFlags_IsEnabled);
3370+
ImGui::CheckboxFlags("_IsVisible", &flags, ImGuiTableColumnFlags_IsVisible);
3371+
ImGui::CheckboxFlags("_IsSorted", &flags, ImGuiTableColumnFlags_IsSorted);
3372+
ImGui::CheckboxFlags("_IsHovered", &flags, ImGuiTableColumnFlags_IsHovered);
3373+
}
3374+
33643375
static void ShowDemoWindowTables()
33653376
{
33663377
//ImGui::SetNextItemOpen(true, ImGuiCond_Once);
@@ -3871,9 +3882,12 @@ static void ShowDemoWindowTables()
38713882
ImGui::TableNextRow();
38723883
for (int column = 0; column < 7; column++)
38733884
{
3874-
// Both TableNextColumn() and TableSetColumnIndex() return false when a column is not visible.
3875-
// Because here we know that A) all our columns are contributing the same to row height and B) column 0 is always visible,
3876-
// we only always submit this one column.
3885+
// Both TableNextColumn() and TableSetColumnIndex() return true when a column is visible or performing width measurement.
3886+
// Because here we know that:
3887+
// - A) all our columns are contributing the same to row height
3888+
// - B) column 0 is always visible,
3889+
// We only always submit this one column and can skip others.
3890+
// More advanced per-column clipping behaviors may benefit from polling the status flags via TableGetColumnFlags().
38773891
if (!ImGui::TableSetColumnIndex(column) && column > 0)
38783892
continue;
38793893
if (column == 0)
@@ -3895,6 +3909,7 @@ static void ShowDemoWindowTables()
38953909
const int column_count = 3;
38963910
const char* column_names[column_count] = { "One", "Two", "Three" };
38973911
static ImGuiTableColumnFlags column_flags[column_count] = { ImGuiTableColumnFlags_DefaultSort, ImGuiTableColumnFlags_None, ImGuiTableColumnFlags_DefaultHide };
3912+
static ImGuiTableColumnFlags column_flags_out[column_count] = { 0, 0, 0 }; // Output from TableGetColumnFlags()
38983913

38993914
if (ImGui::BeginTable("##flags", column_count, ImGuiTableFlags_None))
39003915
{
@@ -3904,21 +3919,34 @@ static void ShowDemoWindowTables()
39043919
ImGui::TableNextColumn();
39053920
ImGui::PushID(column);
39063921
ImGui::AlignTextToFramePadding(); // FIXME-TABLE: Workaround for wrong text baseline propagation
3907-
ImGui::Text("Flags for '%s'", column_names[column]);
3922+
ImGui::Text("'%s'", column_names[column]);
3923+
ImGui::Spacing();
3924+
ImGui::Text("Input flags:");
39083925
EditTableColumnsFlags(&column_flags[column]);
3926+
ImGui::Spacing();
3927+
ImGui::Text("Output flags:");
3928+
ShowTableColumnsStatusFlags(column_flags_out[column]);
39093929
ImGui::PopID();
39103930
}
39113931
PopStyleCompact();
39123932
ImGui::EndTable();
39133933
}
39143934

39153935
// Create the real table we care about for the example!
3916-
const ImGuiTableFlags flags = ImGuiTableFlags_ColumnsWidthFixed | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Sortable;
3917-
if (ImGui::BeginTable("##table", column_count, flags))
3936+
// We use a scrolling table to be able to showcase the difference between the _IsEnabled and _IsVisible flags above, otherwise in
3937+
// a non-scrolling table columns are always visible (unless using ImGuiTableFlags_NoKeepColumnsVisible + resizing the parent window down)
3938+
const ImGuiTableFlags flags
3939+
= ImGuiTableFlags_ColumnsWidthFixed | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY
3940+
| ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV
3941+
| ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Sortable;
3942+
ImVec2 size = ImVec2(0, TEXT_BASE_HEIGHT * 9);
3943+
if (ImGui::BeginTable("##table", column_count, flags, size))
39183944
{
39193945
for (int column = 0; column < column_count; column++)
39203946
ImGui::TableSetupColumn(column_names[column], column_flags[column]);
39213947
ImGui::TableHeadersRow();
3948+
for (int column = 0; column < column_count; column++)
3949+
column_flags_out[column] = ImGui::TableGetColumnFlags(column);
39223950
float indent_step = (float)((int)TEXT_BASE_WIDTH / 2);
39233951
for (int row = 0; row < 8; row++)
39243952
{
@@ -4747,7 +4775,7 @@ static void ShowDemoWindowTables()
47474775

47484776
// Take note of whether we are currently sorting based on the Quantity field,
47494777
// we will use this to trigger sorting when we know the data of this column has been modified.
4750-
const bool sorts_specs_using_quantity = ImGui::TableGetColumnIsSorted(3);
4778+
const bool sorts_specs_using_quantity = (ImGui::TableGetColumnFlags(3) & ImGuiTableColumnFlags_IsSorted) != 0;
47514779

47524780
// Show headers
47534781
if (show_headers)

imgui_internal.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,10 +2280,9 @@ namespace ImGui
22802280
IMGUI_API float GetColumnOffsetFromNorm(const ImGuiOldColumns* columns, float offset_norm);
22812281
IMGUI_API float GetColumnNormFromOffset(const ImGuiOldColumns* columns, float offset);
22822282

2283-
// Tables: Candidates for public api
2283+
// Tables: Candidates for public API
22842284
IMGUI_API void TableOpenContextMenu(int column_n = -1);
22852285
IMGUI_API void TableSetColumnWidth(int column_n, float width);
2286-
IMGUI_API bool TableGetColumnIsEnabled(int column_n = -1); // Return false when column is disabled (hidden by user/api, e.g. via context menu, or _DefaultHide flag)
22872286
IMGUI_API void TableSetColumnIsEnabled(int column_n, bool enabled);
22882287
IMGUI_API void TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs);
22892288
IMGUI_API float TableGetHeaderRowHeight();

0 commit comments

Comments
 (0)