From 9b37087fbee0d8b9a33bb4ec456a5a349ba1a18b Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 24 Sep 2020 14:33:40 +0200 Subject: [PATCH] Tables: (Breaking) Rename TableAutoHeaders() to TableHeadersRow() + added TableGetColumnCount(). --- imgui.h | 11 ++++++----- imgui_demo.cpp | 36 ++++++++++++++++++------------------ imgui_tables.cpp | 37 ++++++++++++++++++++----------------- 3 files changed, 44 insertions(+), 40 deletions(-) diff --git a/imgui.h b/imgui.h index b9515138..9f0e9c8b 100644 --- a/imgui.h +++ b/imgui.h @@ -661,7 +661,7 @@ namespace ImGui // - 1. Call BeginTable() // - 2. Optionally call TableSetupColumn() to submit column name/flags/defaults // - 3. Optionally call TableSetupScrollFreeze() to request scroll freezing of columns/rows - // - 4. Optionally call TableAutoHeaders() to submit a header row (names will be pulled from data submitted to TableSetupColumns) + // - 4. Optionally call TableHeadersRow() to submit a header row (names will be pulled from data submitted to TableSetupColumns) // - 4. Populate contents // - In most situations you can use TableNextRow() + TableSetColumnIndex() to start appending into a column. // - If you are using tables as a sort of grid, where every columns is holding the same type of contents, @@ -678,13 +678,13 @@ namespace ImGui // Tables: Headers & Columns declaration // - Use TableSetupScrollFreeze() to lock columns (from the right) or rows (from the top) so they stay visible when scrolled. // - Use TableSetupColumn() to specify label, resizing policy, default width, id, various other flags etc. - // Important: this will not display anything! The name passed to TableSetupColumn() is used by TableAutoHeaders() and context-menus. - // - Use TableAutoHeaders() to create a row and automatically submit a TableHeader() for each column. + // Important: this will not display anything! The name passed to TableSetupColumn() is used by TableHeadersRow() and context-menus. + // - Use TableHeadersRow() to create a row and automatically submit a TableHeader() for each column. // Headers are required to perform some interactions: reordering, sorting, context menu (FIXME-TABLE: context menu should work without!) // - You may manually submit headers using TableNextRow() + TableHeader() calls, but this is only useful in some advanced cases (e.g. adding custom widgets in header row). IMGUI_API void TableSetupScrollFreeze(int columns, int rows); IMGUI_API void TableSetupColumn(const char* label, ImGuiTableColumnFlags flags = 0, float init_width_or_weight = -1.0f, ImU32 user_id = 0); - IMGUI_API void TableAutoHeaders(); // submit all headers cells based on data provided to TableSetupColumn() + submit context menu + IMGUI_API void TableHeadersRow(); // submit all headers cells based on data provided to TableSetupColumn() + submit context menu IMGUI_API void TableHeader(const char* label); // submit one header cell manually (rarely used) // Tables: Miscellaneous functions // - Most functions taking 'int column_n' treat the default value of -1 as the same as passing the current column index @@ -692,6 +692,7 @@ namespace ImGui // When 'SpecsDirty == true' you should sort your data. It will be true when sorting specs have changed since last call, or the first time. // Make sure to set 'SpecsDirty = false' after sorting, else you may wastefully sort your data every frame! // Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable(). + IMGUI_API int TableGetColumnCount(); // return number of columns (value passed to BeginTable) IMGUI_API const char* TableGetColumnName(int column_n = -1); // return NULL if column didn't have a name declared by TableSetupColumn(). Pass -1 to use current column. IMGUI_API bool TableGetColumnIsVisible(int column_n = -1); // return true if column is visible. Same value is also returned by TableNextCell() and TableSetColumnIndex(). Pass -1 to use current column. 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. @@ -1042,7 +1043,7 @@ enum ImGuiTableFlags_ // Features ImGuiTableFlags_None = 0, ImGuiTableFlags_Resizable = 1 << 0, // Allow resizing columns. - ImGuiTableFlags_Reorderable = 1 << 1, // Allow reordering columns (need calling TableSetupColumn() + TableAutoHeaders() or TableHeaders() to display headers) + ImGuiTableFlags_Reorderable = 1 << 1, // Allow reordering columns (need calling TableSetupColumn() + TableHeadersRow() to display headers) ImGuiTableFlags_Hideable = 1 << 2, // Allow hiding columns (with right-click on header) (FIXME-TABLE: allow without headers). ImGuiTableFlags_Sortable = 1 << 3, // Allow sorting on one column (sort_specs_count will always be == 1). Call TableGetSortSpecs() to obtain sort specs. ImGuiTableFlags_MultiSortable = 1 << 4, // Allow sorting on multiple columns by holding Shift (sort_specs_count may be > 1). Call TableGetSortSpecs() to obtain sort specs. diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 78c4ecb0..f95bbffe 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -3513,7 +3513,7 @@ static void ShowDemoWindowTables() ImGui::TableSetupColumn("AAA", ImGuiTableColumnFlags_WidthFixed);// | ImGuiTableColumnFlags_NoResize); ImGui::TableSetupColumn("BBB", ImGuiTableColumnFlags_WidthFixed); ImGui::TableSetupColumn("CCC", ImGuiTableColumnFlags_WidthStretch); - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); for (int row = 0; row < 5; row++) { ImGui::TableNextRow(); @@ -3533,7 +3533,7 @@ static void ShowDemoWindowTables() ImGui::TableSetupColumn("DDD", ImGuiTableColumnFlags_WidthStretch); ImGui::TableSetupColumn("EEE", ImGuiTableColumnFlags_WidthStretch); ImGui::TableSetupColumn("FFF", ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_DefaultHide); - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); for (int row = 0; row < 5; row++) { ImGui::TableNextRow(); @@ -3560,12 +3560,12 @@ static void ShowDemoWindowTables() if (ImGui::BeginTable("##table1", 3, flags)) { - // Submit columns name with TableSetupColumn() and call TableAutoHeaders() to create a row with a header in each column. + // Submit columns name with TableSetupColumn() and call TableHeadersRow() to create a row with a header in each column. // (Later we will show how TableSetupColumn() has other uses, optional flags, sizing weight etc.) ImGui::TableSetupColumn("One"); ImGui::TableSetupColumn("Two"); ImGui::TableSetupColumn("Three"); - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); for (int row = 0; row < 6; row++) { ImGui::TableNextRow(); @@ -3583,7 +3583,7 @@ static void ShowDemoWindowTables() ImGui::TableSetupColumn("One"); ImGui::TableSetupColumn("Two"); ImGui::TableSetupColumn("Three"); - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); for (int row = 0; row < 6; row++) { ImGui::TableNextRow(); @@ -3639,7 +3639,7 @@ static void ShowDemoWindowTables() ImGui::TableSetupColumn("One", ImGuiTableColumnFlags_None); ImGui::TableSetupColumn("Two", ImGuiTableColumnFlags_None); ImGui::TableSetupColumn("Three", ImGuiTableColumnFlags_None); - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); ImGuiListClipper clipper; clipper.Begin(1000); while (clipper.Step()) @@ -3684,7 +3684,7 @@ static void ShowDemoWindowTables() ImGui::TableSetupColumn("Four", ImGuiTableColumnFlags_None); ImGui::TableSetupColumn("Five", ImGuiTableColumnFlags_None); ImGui::TableSetupColumn("Six", ImGuiTableColumnFlags_None); - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); for (int row = 0; row < 20; row++) { ImGui::TableNextRow(); @@ -3750,7 +3750,7 @@ static void ShowDemoWindowTables() { for (int column = 0; column < column_count; column++) ImGui::TableSetupColumn(column_names[column], column_flags[column]); - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); for (int row = 0; row < 8; row++) { ImGui::Indent(2.0f); // Add some indentation to demonstrate usage of per-column IndentEnable/IndentDisable flags. @@ -3778,7 +3778,7 @@ static void ShowDemoWindowTables() { ImGui::TableSetupColumn("A0"); ImGui::TableSetupColumn("A1"); - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); ImGui::TableNextRow(); ImGui::Text("A0 Cell 0"); { @@ -3787,7 +3787,7 @@ static void ShowDemoWindowTables() { ImGui::TableSetupColumn("B0"); ImGui::TableSetupColumn("B1"); - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); ImGui::TableNextRow(ImGuiTableRowFlags_None, rows_height); ImGui::Text("B0 Cell 0"); @@ -3990,7 +3990,7 @@ static void ShowDemoWindowTables() ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_NoHide); ImGui::TableSetupColumn("Size", ImGuiTableColumnFlags_WidthFixed, ImGui::GetFontSize() * 6); ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed, ImGui::GetFontSize() * 10); - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); // Simple storage to output a dummy file-system. struct MyTreeNode @@ -4048,7 +4048,7 @@ static void ShowDemoWindowTables() ImGui::TreePop(); } - // Demonstrate using TableHeader() calls instead of TableAutoHeaders() + // Demonstrate using TableHeader() calls instead of TableHeadersRow() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); if (ImGui::TreeNode("Custom headers")) @@ -4064,7 +4064,7 @@ static void ShowDemoWindowTables() // FIXME: It would be nice to actually demonstrate full-featured selection using those checkbox. static bool column_selected[3] = {}; - // Instead of calling TableAutoHeaders() we'll submit custom headers ourselves + // Instead of calling TableHeadersRow() we'll submit custom headers ourselves ImGui::TableNextRow(ImGuiTableRowFlags_Headers); for (int column = 0; column < COLUMNS_COUNT; column++) { @@ -4095,12 +4095,12 @@ static void ShowDemoWindowTables() ImGui::TreePop(); } - // Demonstrate creating custom context menus inside columns, while playing it nice with context menus provided by TableHeader()/TableAutoHeaders() + // Demonstrate creating custom context menus inside columns, while playing it nice with context menus provided by TableHeadersRow()/TableHeader() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); if (ImGui::TreeNode("Context menus")) { - HelpMarker("By default, TableHeader()/TableAutoHeaders() will open a context-menu on right-click."); + HelpMarker("By default, TableHeadersRow()/TableHeader() will open a context-menu on right-click."); ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_SizingPolicyFixedX | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders; const int COLUMNS_COUNT = 3; if (ImGui::BeginTable("##table1", COLUMNS_COUNT, flags)) @@ -4110,7 +4110,7 @@ static void ShowDemoWindowTables() ImGui::TableSetupColumn("Three"); // Context Menu 1: right-click on header (including empty section after the third column!) should open Default Table Popup - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); for (int row = 0; row < 4; row++) { ImGui::TableNextRow(); @@ -4219,7 +4219,7 @@ static void ShowDemoWindowTables() } // Display data - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); ImGuiListClipper clipper; clipper.Begin(items.Size); while (clipper.Step()) @@ -4408,7 +4408,7 @@ static void ShowDemoWindowTables() // Show headers if (show_headers) - ImGui::TableAutoHeaders(); + ImGui::TableHeadersRow(); // Show data // FIXME-TABLE FIXME-NAV: How we can get decent up/down even though we have the buttons here? diff --git a/imgui_tables.cpp b/imgui_tables.cpp index b67f1107..9a28a00d 100644 --- a/imgui_tables.cpp +++ b/imgui_tables.cpp @@ -75,11 +75,11 @@ // | TableUpdateBorders() - detect hovering columns for resize, ahead of contents submission // | TableDrawContextMenu() - draw right-click context menu //----------------------------------------------------------------------------- -// - TableAutoHeaders() or TableHeader() user submit a headers row (optional) +// - TableHeadersRow() or TableHeader() user submit a headers row (optional) // | TableSortSpecsClickColumn() - when left-clicked: alter sort order and sort direction // | TableOpenContextMenu() - when right-clicked: trigger opening of the default context menu // - TableGetSortSpecs() user queries updated sort specs (optional, generally after submitting headers) -// - TableNextRow() / TableNextCell() user begin into the first row, also automatically called by TableAutoHeaders() +// - TableNextRow() / TableNextCell() user begin into the first row, also automatically called by TableHeadersRow() // | TableEndCell() - close existing cell if not the first time // | TableBeginCell() - enter into current cell // - [...] user emit contents @@ -1867,6 +1867,13 @@ bool ImGui::TableNextCell() return (table->VisibleUnclippedMaskByIndex & ((ImU64)1 << column_n)) != 0; } +int ImGui::TableGetColumnCount() +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + return table ? table->ColumnsCount : 0; +} + const char* ImGui::TableGetColumnName(int column_n) { ImGuiContext& g = *GImGui; @@ -2066,18 +2073,20 @@ void ImGui::TableOpenContextMenu(ImGuiTable* table, int column_n) // This is a helper to output TableHeader() calls based on the column names declared in TableSetupColumn(). // The intent is that advanced users willing to create customized headers would not need to use this helper // and can create their own! For example: TableHeader() may be preceeded by Checkbox() or other custom widgets. -void ImGui::TableAutoHeaders() +// See 'Demo->Tables->Custom headers' for a demonstration of implementing a custom version of this. +void ImGui::TableHeadersRow() { ImGuiStyle& style = ImGui::GetStyle(); ImGuiContext& g = *GImGui; ImGuiTable* table = g.CurrentTable; - IM_ASSERT(table != NULL && "Need to call TableAutoHeaders() after BeginTable()!"); - const int columns_count = table->ColumnsCount; + IM_ASSERT(table != NULL && "Need to call TableHeadersRow() after BeginTable()!"); // Calculate row height (for the unlikely case that labels may be are multi-line) - float row_y1 = GetCursorScreenPos().y; + // If we didn't do that, uneven header height would work but their highlight won't cover the full row height. float row_height = GetTextLineHeight(); + const float row_y1 = GetCursorScreenPos().y; + const int columns_count = TableGetColumnCount(); for (int column_n = 0; column_n < columns_count; column_n++) if (TableGetColumnIsVisible(column_n)) row_height = ImMax(row_height, CalcTextSize(TableGetColumnName(column_n)).y); @@ -2095,8 +2104,6 @@ void ImGui::TableAutoHeaders() if (!TableSetColumnIndex(column_n)) continue; - const char* name = TableGetColumnName(column_n); - // [DEBUG] Test custom user elements #if 0 if (column_n < 2) @@ -2112,23 +2119,19 @@ void ImGui::TableAutoHeaders() #endif // Push an id to allow unnamed labels (generally accidental, but let's behave nicely with them) - // - in your own code you may omit the PushID/PopID all-together, provided you know you know they won't collide + // - in your own code you may omit the PushID/PopID all-together, provided you know they won't collide // - table->InstanceCurrent is only >0 when we use multiple BeginTable/EndTable calls with same identifier. + const char* name = TableGetColumnName(column_n); PushID(table->InstanceCurrent * table->ColumnsCount + column_n); TableHeader(name); PopID(); } - // FIXME-TABLE: This is not user-land code any more + need to explain WHY this is here! (added in fa88f023) - //window->SkipItems = table->HostSkipItems; - // Allow opening popup from the right-most section after the last column. - // (We don't actually need to use ImGuiHoveredFlags_AllowWhenBlockedByPopup because in reality this is generally - // not required anymore.. because popup opening code tends to be reacting on IsMouseReleased() and the click would - // already have closed any other popups!) // FIXME-TABLE: TableOpenContextMenu() is not public yet. + ImVec2 mouse_pos = ImGui::GetMousePos(); if (IsMouseReleased(1) && TableGetHoveredColumn() == columns_count) - if (g.IO.MousePos.y >= row_y1 && g.IO.MousePos.y < row_y1 + row_height) + if (mouse_pos.y >= row_y1 && mouse_pos.y < row_y1 + row_height) TableOpenContextMenu(table, -1); // Will open a non-column-specific popup. } @@ -2145,7 +2148,7 @@ void ImGui::TableHeader(const char* label) return; ImGuiTable* table = g.CurrentTable; - IM_ASSERT(table != NULL && "Need to call TableAutoHeaders() after BeginTable()!"); + IM_ASSERT(table != NULL && "Need to call TableHeader() after BeginTable()!"); IM_ASSERT(table->CurrentColumn != -1); const int column_n = table->CurrentColumn; ImGuiTableColumn* column = &table->Columns[column_n];