Skip to content

Commit 3fbb928

Browse files
committed
Tables: explicit/custom width in TableSetupColumn() is reapplied when table or column becomes not resizable. Comments.
1 parent 9564357 commit 3fbb928

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

imgui.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,11 +1049,12 @@ enum ImGuiTabItemFlags_
10491049
// The typical use of mixing sizing policies is: any number of LEADING Fixed columns, followed by one or two TRAILING Stretch columns.
10501050
// (this is because the visible order of columns have subtle but necessary effects on how they react to manual resizing).
10511051
// - When ScrollX is on:
1052-
// - Table defaults to ImGuiTableFlags_ColumnsWidthFixed -> all Columns defaults to ImGuiTableColumnFlags_WidthFixed or ImGuiTableColumnFlags_WidthAuto
1052+
// - Table defaults to ImGuiTableFlags_ColumnsWidthFixed -> all Columns defaults to ImGuiTableColumnFlags_WidthFixed or ImGuiTableColumnFlags_WidthAuto.
10531053
// - Columns sizing policy allowed: Fixed/Auto mostly.
10541054
// - Fixed Columns can be enlarged as needed. Table will show an horizontal scrollbar if needed.
10551055
// - Using Stretch columns OFTEN DOES NOT MAKE SENSE if ScrollX is on, UNLESS you have specified a value for 'inner_width' in BeginTable().
10561056
// If you specify a value for 'inner_width' then effectively the scrolling space is known and Stretch or mixed Fixed/Stretch columns become meaningful again.
1057+
// - Read on documentation at the top of imgui_tables.cpp for details.
10571058
enum ImGuiTableFlags_
10581059
{
10591060
// Features

imgui_demo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3912,6 +3912,7 @@ static void ShowDemoWindowTables()
39123912
static int freeze_rows = 1;
39133913

39143914
PushStyleCompact();
3915+
ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable);
39153916
ImGui::CheckboxFlags("ImGuiTableFlags_ScrollX", &flags, ImGuiTableFlags_ScrollX);
39163917
ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY);
39173918
ImGui::SetNextItemWidth(ImGui::GetFrameHeight());

imgui_tables.cpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,25 @@ Index of this file:
9797
// of what the value does.
9898
//-----------------------------------------------------------------------------
9999

100+
//-----------------------------------------------------------------------------
101+
// COLUMNS SIZING POLICIES
102+
//-----------------------------------------------------------------------------
103+
// About overriding column width/weight with TableSetupColumn():
104+
// We use a default parameter of 'init_width_or_weight == -1'.
105+
// - With ImGuiTableColumnFlags_WidthAuto, init_width (ignored) --> width is automatic
106+
// - With ImGuiTableColumnFlags_WidthFixed, init_width <= 0 (default) --> width is automatic
107+
// - With ImGuiTableColumnFlags_WidthFixed, init_width > 0 (explicit) --> width is custom
108+
// - With ImGuiTableColumnFlags_WidthStretch, init_weight <= 0 (default) --> weight is 1.0f
109+
// - With ImGuiTableColumnFlags_WidthStretch, init_weight > 0 (explicit) --> weight is custom
110+
// Widths are specified _without_ CellPadding. If you specify a width of 100.0f, the column will be cover (100.0f + Padding * 2.0f)
111+
// and you can fit a 100.0f wide item in it without clipping and with full padding.
112+
//-----------------------------------------------------------------------------
113+
// About default width policy (if you don't specify a ImGuiTableColumnFlags_WidthXXXX flag)
114+
// - When Table policy ImGuiTableFlags_ColumnsWidthStretch --> default Column policy is ImGuiTableColumnFlags_WidthStretch
115+
// - When Table policy ImGuiTableFlags_ColumnsWidthFixed and (Table is Resizable or init_width > 0) --> default Column policy is ImGuiTableColumnFlags_WidthFixed
116+
// - When Table policy ImGuiTableFlags_ColumnsWidthFixed and (Table is not Resizable and init_width <= 0) --> default Column policy is ImGuiTableColumnFlags_WidthAuto
117+
//-----------------------------------------------------------------------------
118+
100119
//-----------------------------------------------------------------------------
101120
// TABLES CLIPPING/CULLING
102121
//-----------------------------------------------------------------------------
@@ -730,19 +749,17 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
730749
float width_auto = content_width_body;
731750
if (!(column->Flags & ImGuiTableColumnFlags_NoHeaderWidth))
732751
width_auto = ImMax(width_auto, content_width_headers);
733-
width_auto = ImMax(width_auto, min_column_width);
734-
735-
// Non-resizable columns also submit their requested width
736-
if ((column->Flags & ImGuiTableColumnFlags_WidthFixed) && column->InitStretchWeightOrWidth > 0.0f)
737-
if (!(table->Flags & ImGuiTableFlags_Resizable) || (column->Flags & ImGuiTableColumnFlags_NoResize))
738-
width_auto = ImMax(width_auto, column->InitStretchWeightOrWidth);
739-
740-
column->WidthAuto = width_auto;
752+
column->WidthAuto = ImMax(width_auto, min_column_width);
741753
}
742754
column->IsPreserveWidthAuto = false;
743755

744756
if (column->Flags & (ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_WidthAuto))
745757
{
758+
// Non-resizable columns keep their requested width
759+
if ((column->Flags & ImGuiTableColumnFlags_WidthFixed) && column->InitStretchWeightOrWidth > 0.0f)
760+
if (!(table->Flags & ImGuiTableFlags_Resizable) || (column->Flags & ImGuiTableColumnFlags_NoResize))
761+
column->WidthRequest = column->WidthAuto = ImMax(column->WidthAuto, column->InitStretchWeightOrWidth);
762+
746763
// Process auto-fit for non-stretched columns
747764
// Latch initial size for fixed columns and update it constantly for auto-resizing column (unless clipped!)
748765
if ((column->AutoFitQueue != 0x00) || ((column->Flags & ImGuiTableColumnFlags_WidthAuto) && column->IsVisibleX))
@@ -1258,12 +1275,7 @@ void ImGui::EndTable()
12581275
outer_window->DC.CurrentTableIdx = g.CurrentTable ? g.Tables.GetIndex(g.CurrentTable) : -1;
12591276
}
12601277

1261-
// We use a default parameter of 'init_width_or_weight == -1',
1262-
// - with ImGuiTableColumnFlags_WidthFixed, width <= 0 --> init width == auto
1263-
// - with ImGuiTableColumnFlags_WidthFixed, width > 0 --> init width == manual
1264-
// - with ImGuiTableColumnFlags_WidthStretch, weight < 0 --> init weight == 1.0f
1265-
// - with ImGuiTableColumnFlags_WidthStretch, weight >= 0 --> init weight == custom
1266-
// Widths are specified _without_ CellPadding. So if you specify a width of 100.0f the column will be 100.0f+Padding*2.0f and you can fit a 100.0-wide item in it.
1278+
// See "COLUMN SIZING POLICIES" comments at the top of this file
12671279
void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, float init_width_or_weight, ImGuiID user_id)
12681280
{
12691281
ImGuiContext& g = *GImGui;
@@ -1281,7 +1293,7 @@ void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, flo
12811293
table->DeclColumnsCount++;
12821294

12831295
// When passing a width automatically enforce WidthFixed policy
1284-
// (whereas TableSetupColumnFlags would default to WidthAuto)
1296+
// (whereas TableSetupColumnFlags would default to WidthAuto if table is not Resizable)
12851297
if ((flags & ImGuiTableColumnFlags_WidthMask_) == 0)
12861298
if ((table->Flags & ImGuiTableFlags_ColumnsWidthFixed) && (init_width_or_weight > 0.0f))
12871299
flags |= ImGuiTableColumnFlags_WidthFixed;

0 commit comments

Comments
 (0)