1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-09-24 03:28:33 +02:00

Tables: exposed status flags via TableGetColumnFlags(), removed TableGetColumnIsSorted()

Scoped width auto calc.
This commit is contained in:
ocornut 2020-12-03 18:45:15 +01:00
parent 984c4cb5f8
commit b194df413c
4 changed files with 98 additions and 58 deletions

15
imgui.h
View File

@ -668,8 +668,9 @@ namespace ImGui
// you may prefer using TableNextColumn() instead of TableNextRow() + TableSetColumnIndex().
// TableNextColumn() will automatically wrap-around into the next row if needed.
// - IMPORTANT: Comparatively to the old Columns() API, we need to call TableNextColumn() for the first column!
// - Both TableSetColumnIndex() and TableNextColumn() return false when the column is not visible, so you can
// skip submitting the contents of a cell BUT ONLY if you know it is not going to contribute to row height.
// - Both TableSetColumnIndex() and TableNextColumn() return true when the column is visible or performing
// width measurements. Otherwise, you may skip submitting the contents of a cell/column, BUT ONLY if you know
// it is not going to contribute to row height.
// In many situations, you may skip submitting contents for every columns but one (e.g. the first one).
// - Summary of possible call flow:
// ----------------------------------------------------------------------------------------------------------
@ -706,7 +707,7 @@ namespace ImGui
// 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 "" if column didn't have a name declared by TableSetupColumn(). 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.
IMGUI_API ImGuiTableColumnFlags TableGetColumnFlags(int column_n = -1); // return column flags so you can query their Enabled/Visible/Sorted/Hovered status flags.
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.
IMGUI_API ImGuiTableSortSpecs* TableGetSortSpecs(); // get latest sort specs for the table (NULL if not sorting).
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.
@ -1094,6 +1095,7 @@ enum ImGuiTableFlags_
// Flags for ImGui::TableSetupColumn()
enum ImGuiTableColumnFlags_
{
// Input configuration flags
ImGuiTableColumnFlags_None = 0,
ImGuiTableColumnFlags_DefaultHide = 1 << 0, // Default as a hidden/disabled column.
ImGuiTableColumnFlags_DefaultSort = 1 << 1, // Default as a sorting column.
@ -1113,9 +1115,16 @@ enum ImGuiTableColumnFlags_
ImGuiTableColumnFlags_IndentEnable = 1 << 15, // Use current Indent value when entering cell (default for column 0).
ImGuiTableColumnFlags_IndentDisable = 1 << 16, // Ignore current Indent value when entering cell (default for columns > 0). Indentation changes _within_ the cell will still be honored.
// Output status flags, read-only via TableGetColumnFlags()
ImGuiTableColumnFlags_IsEnabled = 1 << 20, // Status: is enabled == not hidden by user/api (referred to as "Hide" in _DefaultHide and _NoHide) flags.
ImGuiTableColumnFlags_IsVisible = 1 << 21, // Status: is visible == is enabled AND not clipped by scrolling.
ImGuiTableColumnFlags_IsSorted = 1 << 22, // Status: is currently part of the sort specs
ImGuiTableColumnFlags_IsHovered = 1 << 23, // Status: is hovered by mouse
// [Internal] Combinations and masks
ImGuiTableColumnFlags_WidthMask_ = ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_WidthAutoResize,
ImGuiTableColumnFlags_IndentMask_ = ImGuiTableColumnFlags_IndentEnable | ImGuiTableColumnFlags_IndentDisable,
ImGuiTableColumnFlags_StatusMask_ = ImGuiTableColumnFlags_IsEnabled | ImGuiTableColumnFlags_IsVisible | ImGuiTableColumnFlags_IsSorted | ImGuiTableColumnFlags_IsHovered,
ImGuiTableColumnFlags_NoDirectResize_ = 1 << 30 // [Internal] Disable user resizing this column directly (it may however we resized indirectly from its left edge)
};

View File

@ -3344,9 +3344,12 @@ static void EditTableColumnsFlags(ImGuiTableColumnFlags* p_flags)
{
ImGui::CheckboxFlags("_DefaultHide", p_flags, ImGuiTableColumnFlags_DefaultHide);
ImGui::CheckboxFlags("_DefaultSort", p_flags, ImGuiTableColumnFlags_DefaultSort);
ImGui::CheckboxFlags("_WidthStretch", p_flags, ImGuiTableColumnFlags_WidthStretch);
ImGui::CheckboxFlags("_WidthFixed", p_flags, ImGuiTableColumnFlags_WidthFixed);
ImGui::CheckboxFlags("_WidthAutoResize", p_flags, ImGuiTableColumnFlags_WidthAutoResize);
if (ImGui::CheckboxFlags("_WidthStretch", p_flags, ImGuiTableColumnFlags_WidthStretch))
*p_flags &= ~(ImGuiTableColumnFlags_WidthMask_ ^ ImGuiTableColumnFlags_WidthStretch);
if (ImGui::CheckboxFlags("_WidthFixed", p_flags, ImGuiTableColumnFlags_WidthFixed))
*p_flags &= ~(ImGuiTableColumnFlags_WidthMask_ ^ ImGuiTableColumnFlags_WidthFixed);
if (ImGui::CheckboxFlags("_WidthAutoResize", p_flags, ImGuiTableColumnFlags_WidthAutoResize))
*p_flags &= ~(ImGuiTableColumnFlags_WidthMask_ ^ ImGuiTableColumnFlags_WidthAutoResize);
ImGui::CheckboxFlags("_NoResize", p_flags, ImGuiTableColumnFlags_NoResize);
ImGui::CheckboxFlags("_NoReorder", p_flags, ImGuiTableColumnFlags_NoReorder);
ImGui::CheckboxFlags("_NoHide", p_flags, ImGuiTableColumnFlags_NoHide);
@ -3354,13 +3357,21 @@ static void EditTableColumnsFlags(ImGuiTableColumnFlags* p_flags)
ImGui::CheckboxFlags("_NoSort", p_flags, ImGuiTableColumnFlags_NoSort);
ImGui::CheckboxFlags("_NoSortAscending", p_flags, ImGuiTableColumnFlags_NoSortAscending);
ImGui::CheckboxFlags("_NoSortDescending", p_flags, ImGuiTableColumnFlags_NoSortDescending);
ImGui::CheckboxFlags("_NoSHeaderWidth", p_flags, ImGuiTableColumnFlags_NoHeaderWidth);
ImGui::CheckboxFlags("_NoHeaderWidth", p_flags, ImGuiTableColumnFlags_NoHeaderWidth);
ImGui::CheckboxFlags("_PreferSortAscending", p_flags, ImGuiTableColumnFlags_PreferSortAscending);
ImGui::CheckboxFlags("_PreferSortDescending", p_flags, ImGuiTableColumnFlags_PreferSortDescending);
ImGui::CheckboxFlags("_IndentEnable", p_flags, ImGuiTableColumnFlags_IndentEnable); ImGui::SameLine(); HelpMarker("Default for column 0");
ImGui::CheckboxFlags("_IndentDisable", p_flags, ImGuiTableColumnFlags_IndentDisable); ImGui::SameLine(); HelpMarker("Default for column >0");
}
static void ShowTableColumnsStatusFlags(ImGuiTableColumnFlags flags)
{
ImGui::CheckboxFlags("_IsEnabled", &flags, ImGuiTableColumnFlags_IsEnabled);
ImGui::CheckboxFlags("_IsVisible", &flags, ImGuiTableColumnFlags_IsVisible);
ImGui::CheckboxFlags("_IsSorted", &flags, ImGuiTableColumnFlags_IsSorted);
ImGui::CheckboxFlags("_IsHovered", &flags, ImGuiTableColumnFlags_IsHovered);
}
static void ShowDemoWindowTables()
{
//ImGui::SetNextItemOpen(true, ImGuiCond_Once);
@ -3871,9 +3882,12 @@ static void ShowDemoWindowTables()
ImGui::TableNextRow();
for (int column = 0; column < 7; column++)
{
// Both TableNextColumn() and TableSetColumnIndex() return false when a column is not visible.
// Because here we know that A) all our columns are contributing the same to row height and B) column 0 is always visible,
// we only always submit this one column.
// Both TableNextColumn() and TableSetColumnIndex() return true when a column is visible or performing width measurement.
// Because here we know that:
// - A) all our columns are contributing the same to row height
// - B) column 0 is always visible,
// We only always submit this one column and can skip others.
// More advanced per-column clipping behaviors may benefit from polling the status flags via TableGetColumnFlags().
if (!ImGui::TableSetColumnIndex(column) && column > 0)
continue;
if (column == 0)
@ -3895,6 +3909,7 @@ static void ShowDemoWindowTables()
const int column_count = 3;
const char* column_names[column_count] = { "One", "Two", "Three" };
static ImGuiTableColumnFlags column_flags[column_count] = { ImGuiTableColumnFlags_DefaultSort, ImGuiTableColumnFlags_None, ImGuiTableColumnFlags_DefaultHide };
static ImGuiTableColumnFlags column_flags_out[column_count] = { 0, 0, 0 }; // Output from TableGetColumnFlags()
if (ImGui::BeginTable("##flags", column_count, ImGuiTableFlags_None))
{
@ -3904,8 +3919,13 @@ static void ShowDemoWindowTables()
ImGui::TableNextColumn();
ImGui::PushID(column);
ImGui::AlignTextToFramePadding(); // FIXME-TABLE: Workaround for wrong text baseline propagation
ImGui::Text("Flags for '%s'", column_names[column]);
ImGui::Text("'%s'", column_names[column]);
ImGui::Spacing();
ImGui::Text("Input flags:");
EditTableColumnsFlags(&column_flags[column]);
ImGui::Spacing();
ImGui::Text("Output flags:");
ShowTableColumnsStatusFlags(column_flags_out[column]);
ImGui::PopID();
}
PopStyleCompact();
@ -3913,12 +3933,20 @@ static void ShowDemoWindowTables()
}
// Create the real table we care about for the example!
const ImGuiTableFlags flags = ImGuiTableFlags_ColumnsWidthFixed | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Sortable;
if (ImGui::BeginTable("##table", column_count, flags))
// We use a scrolling table to be able to showcase the difference between the _IsEnabled and _IsVisible flags above, otherwise in
// a non-scrolling table columns are always visible (unless using ImGuiTableFlags_NoKeepColumnsVisible + resizing the parent window down)
const ImGuiTableFlags flags
= ImGuiTableFlags_ColumnsWidthFixed | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY
| ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV
| ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Sortable;
ImVec2 size = ImVec2(0, TEXT_BASE_HEIGHT * 9);
if (ImGui::BeginTable("##table", column_count, flags, size))
{
for (int column = 0; column < column_count; column++)
ImGui::TableSetupColumn(column_names[column], column_flags[column]);
ImGui::TableHeadersRow();
for (int column = 0; column < column_count; column++)
column_flags_out[column] = ImGui::TableGetColumnFlags(column);
float indent_step = (float)((int)TEXT_BASE_WIDTH / 2);
for (int row = 0; row < 8; row++)
{
@ -4747,7 +4775,7 @@ static void ShowDemoWindowTables()
// Take note of whether we are currently sorting based on the Quantity field,
// we will use this to trigger sorting when we know the data of this column has been modified.
const bool sorts_specs_using_quantity = ImGui::TableGetColumnIsSorted(3);
const bool sorts_specs_using_quantity = (ImGui::TableGetColumnFlags(3) & ImGuiTableColumnFlags_IsSorted) != 0;
// Show headers
if (show_headers)

View File

@ -2280,10 +2280,9 @@ namespace ImGui
IMGUI_API float GetColumnOffsetFromNorm(const ImGuiOldColumns* columns, float offset_norm);
IMGUI_API float GetColumnNormFromOffset(const ImGuiOldColumns* columns, float offset);
// Tables: Candidates for public api
// Tables: Candidates for public API
IMGUI_API void TableOpenContextMenu(int column_n = -1);
IMGUI_API void TableSetColumnWidth(int column_n, float width);
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)
IMGUI_API void TableSetColumnIsEnabled(int column_n, bool enabled);
IMGUI_API void TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs);
IMGUI_API float TableGetHeaderRowHeight();

View File

@ -685,7 +685,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
// Adjust flags: default width mode + weighted columns are not allowed when auto extending
// FIXME-TABLE: Clarify why we need to do this again here and not just in TableSetupColumn()
column->Flags = TableFixColumnFlags(table, column->FlagsIn);
column->Flags = TableFixColumnFlags(table, column->FlagsIn) | (column->Flags & ImGuiTableColumnFlags_StatusMask_);
if ((column->Flags & ImGuiTableColumnFlags_IndentMask_) == 0)
column->Flags |= (column_n == 0) ? ImGuiTableColumnFlags_IndentEnable : ImGuiTableColumnFlags_IndentDisable;
if ((column->Flags & ImGuiTableColumnFlags_NoResize) == 0)
@ -696,8 +696,9 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
if (table->Flags & ImGuiTableFlags_Sortable)
TableFixColumnSortDirection(column);
// Calculate "ideal" column width for nothing to be clipped.
// Calculate ideal/auto column width (that's the width required for all contents to be visible without clipping)
// Combine width from regular rows + width from headers unless requested not to.
{
const float content_width_body = (float)ImMax(column->ContentMaxXFrozen, column->ContentMaxXUnfrozen) - column->WorkMinX;
const float content_width_headers = (float)column->ContentMaxXHeadersIdeal - column->WorkMinX;
float width_auto = content_width_body;
@ -711,12 +712,14 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
width_auto = ImMax(width_auto, column->InitStretchWeightOrWidth);
column->WidthAuto = width_auto;
}
if (column->Flags & (ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_WidthAutoResize))
{
// Process auto-fit for non-stretched columns
// Latch initial size for fixed columns and update it constantly for auto-resizing column (unless clipped!)
if ((column->AutoFitQueue != 0x00) || ((column->Flags & ImGuiTableColumnFlags_WidthAutoResize) && column->IsVisibleX))
column->WidthRequest = width_auto;
column->WidthRequest = column->WidthAuto;
// FIXME-TABLE: Increase minimum size during init frame to avoid biasing auto-fitting widgets
// (e.g. TextWrapped) too much. Otherwise what tends to happen is that TextWrapped would output a very
@ -738,7 +741,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
if (table->LeftMostStretchedColumnDisplayOrder == -1 || table->LeftMostStretchedColumnDisplayOrder > column->DisplayOrder)
table->LeftMostStretchedColumnDisplayOrder = column->DisplayOrder;
}
max_width_auto = ImMax(max_width_auto, width_auto);
max_width_auto = ImMax(max_width_auto, column->WidthAuto);
sum_width_fixed_requests += table->CellPaddingX * 2.0f;
}
table->ColumnsEnabledFixedCount = (ImGuiTableColumnIdx)count_fixed;
@ -851,6 +854,9 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
if (table->FreezeColumnsCount > 0 && table->FreezeColumnsCount == visible_n)
offset_x += work_rect.Min.x - table->OuterRect.Min.x;
// Clear status flags
column->Flags &= ~ImGuiTableColumnFlags_StatusMask_;
if ((table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n)) == 0)
{
// Hidden column: clear a few fields and we are done with it for the remainder of the function.
@ -866,6 +872,10 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
continue;
}
// Detect hovered column
if (is_hovering_table && g.IO.MousePos.x >= column->ClipRect.Min.x && g.IO.MousePos.x < column->ClipRect.Max.x)
table->HoveredColumnBody = (ImGuiTableColumnIdx)column_n;
// Maximum width
float max_width = FLT_MAX;
if (table->Flags & ImGuiTableFlags_ScrollX)
@ -936,9 +946,14 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
if (column->IsSkipItems)
IM_ASSERT(!is_visible);
// Detect hovered column
if (is_hovering_table && g.IO.MousePos.x >= column->ClipRect.Min.x && g.IO.MousePos.x < column->ClipRect.Max.x)
table->HoveredColumnBody = (ImGuiTableColumnIdx)column_n;
// Update status flags
column->Flags |= ImGuiTableColumnFlags_IsEnabled;
if (is_visible)
column->Flags |= ImGuiTableColumnFlags_IsVisible;
if (column->SortOrder != -1)
column->Flags |= ImGuiTableColumnFlags_IsSorted;
if (table->HoveredColumnBody == column_n)
column->Flags |= ImGuiTableColumnFlags_IsHovered;
// Alignment
// FIXME-TABLE: This align based on the whole column width, not per-cell, and therefore isn't useful in
@ -1237,6 +1252,7 @@ static void TableUpdateColumnsWeightFromWidth(ImGuiTable* table)
ImGuiTableColumn* column = &table->Columns[column_n];
if (!column->IsEnabled || !(column->Flags & ImGuiTableColumnFlags_WidthStretch))
continue;
IM_ASSERT(column->StretchWeight > 0.0f);
visible_weight += column->StretchWeight;
visible_width += column->WidthRequest;
}
@ -1355,6 +1371,7 @@ void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, flo
IM_ASSERT(table != NULL && "Need to call TableSetupColumn() after BeginTable()!");
IM_ASSERT(table->IsLayoutLocked == false && "Need to call call TableSetupColumn() before first row!");
IM_ASSERT(table->DeclColumnsCount >= 0 && table->DeclColumnsCount < table->ColumnsCount && "Called TableSetupColumn() too many times!");
IM_ASSERT((flags & ImGuiTableColumnFlags_StatusMask_) == 0 && "Illegal to pass StatusMask values to TableSetupColumn()");
ImGuiTableColumn* column = &table->Columns[table->DeclColumnsCount];
table->DeclColumnsCount++;
@ -1368,7 +1385,7 @@ void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, flo
column->UserID = user_id;
column->FlagsIn = flags;
column->Flags = TableFixColumnFlags(table, column->FlagsIn);
column->Flags = TableFixColumnFlags(table, column->FlagsIn) | (column->Flags & ImGuiTableColumnFlags_StatusMask_);
flags = column->Flags;
// Initialize defaults
@ -1383,7 +1400,7 @@ void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, flo
if (flags & ImGuiTableColumnFlags_WidthStretch)
column->StretchWeight = (init_width_or_weight > 0.0f) ? init_width_or_weight : 1.0f;
// Disable auto-fit if an explicit fixed width has been specified
// Disable auto-fit if an explicit width/weight has been specified
if (init_width_or_weight > 0.0f)
column->AutoFitQueue = 0x00;
}
@ -1747,15 +1764,15 @@ const char* ImGui::TableGetColumnName(int column_n)
return TableGetColumnName(table, column_n);
}
bool ImGui::TableGetColumnIsEnabled(int column_n)
ImGuiTableColumnFlags ImGui::TableGetColumnFlags(int column_n)
{
ImGuiContext& g = *GImGui;
ImGuiTable* table = g.CurrentTable;
if (!table)
return false;
return ImGuiTableColumnFlags_None;
if (column_n < 0)
column_n = table->CurrentColumn;
return (table->EnabledMaskByIndex & ((ImU64)1 << column_n)) != 0;
return table->Columns[column_n].Flags;
}
void ImGui::TableSetColumnIsEnabled(int column_n, bool hidden)
@ -2272,7 +2289,6 @@ void ImGui::TableDrawBorders(ImGuiTable* table)
// [SECTION] Tables: Sorting
//-------------------------------------------------------------------------
// - TableGetSortSpecs()
// - TableGetColumnIsSorted()
// - TableFixColumnSortDirection() [Internal]
// - TableSetColumnSortDirection() [Internal]
// - TableSortSpecsSanitize() [Internal]
@ -2302,18 +2318,6 @@ ImGuiTableSortSpecs* ImGui::TableGetSortSpecs()
return table->SortSpecs.SpecsCount ? &table->SortSpecs : NULL;
}
bool ImGui::TableGetColumnIsSorted(int column_n)
{
ImGuiContext& g = *GImGui;
ImGuiTable* table = g.CurrentTable;
if (!table)
return false;
if (column_n < 0)
column_n = table->CurrentColumn;
ImGuiTableColumn* column = &table->Columns[column_n];
return (column->SortOrder != -1);
}
void ImGui::TableFixColumnSortDirection(ImGuiTableColumn* column)
{
// Initial sort state
@ -2469,7 +2473,7 @@ float ImGui::TableGetHeaderRowHeight()
float row_height = GetTextLineHeight();
int columns_count = TableGetColumnCount();
for (int column_n = 0; column_n < columns_count; column_n++)
if (TableGetColumnIsEnabled(column_n))
if (TableGetColumnFlags(column_n) & ImGuiTableColumnFlags_IsEnabled)
row_height = ImMax(row_height, CalcTextSize(TableGetColumnName(column_n)).y);
row_height += GetStyle().CellPadding.y * 2.0f;
return row_height;