Browse Source

Tables: demo inner_size + scrollx + stretch, added TableGetRowIndex(), renamed _WidthAlwaysAutoResize to _WidthAutoResize.

pull/3657/head
ocornut 4 years ago
parent
commit
3a2f0bfc04
  1. 21
      imgui.h
  2. 72
      imgui_demo.cpp
  3. 33
      imgui_tables.cpp

21
imgui.h

@ -685,6 +685,7 @@ namespace ImGui
IMGUI_API bool TableNextColumn(); // append into the next column (or first column of next row if currently in last column). Return false when column is not visible. IMGUI_API bool TableNextColumn(); // append into the next column (or first column of next row if currently in last column). Return false when column is not visible.
IMGUI_API bool TableSetColumnIndex(int column_n); // append into the specified column. Return false when column is not visible. IMGUI_API bool TableSetColumnIndex(int column_n); // append into the specified column. Return false when column is not visible.
IMGUI_API int TableGetColumnIndex(); // return current column index. IMGUI_API int TableGetColumnIndex(); // return current column index.
IMGUI_API int TableGetRowIndex(); // return current row index.
// Tables: Headers & Columns declaration // Tables: Headers & Columns declaration
// - Use TableSetupColumn() to specify label, resizing policy, default width/weight, id, various other flags etc. // - Use TableSetupColumn() to specify label, resizing policy, default width/weight, id, various other flags etc.
// Important: this will not display anything! The name passed to TableSetupColumn() is used by TableHeadersRow() and context-menus. // Important: this will not display anything! The name passed to TableSetupColumn() is used by TableHeadersRow() and context-menus.
@ -1037,14 +1038,14 @@ enum ImGuiTabItemFlags_
// - The default sizing policy for columns depends on whether the ScrollX flag is set on the table: // - The default sizing policy for columns depends on whether the ScrollX flag is set on the table:
// When ScrollX is off: // When ScrollX is off:
// - Table defaults to ImGuiTableFlags_SizingPolicyStretchX -> all Columns defaults to ImGuiTableColumnFlags_WidthStretch. // - Table defaults to ImGuiTableFlags_SizingPolicyStretchX -> all Columns defaults to ImGuiTableColumnFlags_WidthStretch.
// - Columns sizing policy allowed: Fixed/Auto or Stretch. // - Columns sizing policy allowed: Stretch (default) or Fixed/Auto.
// - Stretch Columns will share the width available in table. // - Stretch Columns will share the width available in table.
// - Fixed Columns will generally obtain their requested width unless the Table cannot fit them all. // - Fixed Columns will generally obtain their requested width unless the Table cannot fit them all.
// When ScrollX is on: // When ScrollX is on:
// - Table defaults to ImGuiTableFlags_SizingPolicyFixedX -> all Columns defaults to ImGuiTableColumnFlags_WidthFixed. // - Table defaults to ImGuiTableFlags_SizingPolicyFixedX -> all Columns defaults to ImGuiTableColumnFlags_WidthFixed.
// - Columns sizing policy allowed: Fixed/Auto mostly! Using Stretch columns OFTEN DOES NOT MAKE SENSE if ScrollX is on, UNLESS you have specified a value for 'inner_width' in BeginTable(). // - Columns sizing policy allowed: Fixed/Auto mostly!
// - Fixed Columns can be enlarged as needed. Table will show an horizontal scrollbar if needed. // - Fixed Columns can be enlarged as needed. Table will show an horizontal scrollbar if needed.
// - Stretch Columns, if any, will calculate their width using inner_width, assuming no scrolling (it really doesn't make sense to do otherwise). // - Using Stretch columns OFTEN DOES NOT MAKE SENSE if ScrollX is on, UNLESS you have specified a value for 'inner_width' in BeginTable().
// - Mixing up columns with different sizing policy is possible BUT can be tricky and has some side-effects and restrictions. // - Mixing up columns with different sizing policy is possible BUT can be tricky and has some side-effects and restrictions.
// (their visible order and the scrolling state have subtle but necessary effects on how they can be manually resized). // (their visible order and the scrolling state have subtle but necessary effects on how they can be manually resized).
// The typical use of mixing sizing policies is to have ScrollX disabled, one or two Stretch Column and many Fixed Columns. // The typical use of mixing sizing policies is to have ScrollX disabled, one or two Stretch Column and many Fixed Columns.
@ -1059,7 +1060,7 @@ enum ImGuiTableFlags_
ImGuiTableFlags_MultiSortable = 1 << 4, // Allow sorting on multiple columns by holding Shift (sort_specs_count may 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.
ImGuiTableFlags_NoSavedSettings = 1 << 5, // Disable persisting columns order, width and sort settings in the .ini file. ImGuiTableFlags_NoSavedSettings = 1 << 5, // Disable persisting columns order, width and sort settings in the .ini file.
ImGuiTableFlags_ContextMenuInBody = 1 << 6, // Right-click on columns body/contents will display table context menu. By default it is available in TableHeadersRow(). ImGuiTableFlags_ContextMenuInBody = 1 << 6, // Right-click on columns body/contents will display table context menu. By default it is available in TableHeadersRow().
// Decoration // Decorations
ImGuiTableFlags_RowBg = 1 << 7, // Set each RowBg color with ImGuiCol_TableRowBg or ImGuiCol_TableRowBgAlt (equivalent to calling TableSetBgColor with ImGuiTableBgFlags_RowBg0 on each row manually) ImGuiTableFlags_RowBg = 1 << 7, // Set each RowBg color with ImGuiCol_TableRowBg or ImGuiCol_TableRowBgAlt (equivalent to calling TableSetBgColor with ImGuiTableBgFlags_RowBg0 on each row manually)
ImGuiTableFlags_BordersInnerH = 1 << 8, // Draw horizontal borders between rows. ImGuiTableFlags_BordersInnerH = 1 << 8, // Draw horizontal borders between rows.
ImGuiTableFlags_BordersOuterH = 1 << 9, // Draw horizontal borders at the top and bottom. ImGuiTableFlags_BordersOuterH = 1 << 9, // Draw horizontal borders at the top and bottom.
@ -1073,11 +1074,11 @@ enum ImGuiTableFlags_
ImGuiTableFlags_NoBordersInBody = 1 << 12, // Disable vertical borders in columns Body (borders will always appears in Headers). ImGuiTableFlags_NoBordersInBody = 1 << 12, // Disable vertical borders in columns Body (borders will always appears in Headers).
ImGuiTableFlags_NoBordersInBodyUntilResize = 1 << 13, // Disable vertical borders in columns Body until hovered for resize (borders will always appears in Headers). ImGuiTableFlags_NoBordersInBodyUntilResize = 1 << 13, // Disable vertical borders in columns Body until hovered for resize (borders will always appears in Headers).
// Sizing // Sizing
ImGuiTableFlags_SizingPolicyFixedX = 1 << 14, // Default if ScrollX is on. Columns will default to use _WidthFixed or _WidthAlwaysAutoResize policy. Read description above for more details. ImGuiTableFlags_SizingPolicyFixedX = 1 << 14, // Default if ScrollX is on. Columns will default to use _WidthFixed or _WidthAutoResize policy. Read description above for more details.
ImGuiTableFlags_SizingPolicyStretchX = 1 << 15, // Default if ScrollX is off. Columns will default to use _WidthStretch policy. Read description above for more details. ImGuiTableFlags_SizingPolicyStretchX = 1 << 15, // Default if ScrollX is off. Columns will default to use _WidthStretch policy. Read description above for more details.
ImGuiTableFlags_NoHeadersWidth = 1 << 16, // Disable header width contribution to automatic width calculation. ImGuiTableFlags_NoHeadersWidth = 1 << 16, // Disable header width contribution to automatic width calculation.
ImGuiTableFlags_NoHostExtendY = 1 << 17, // (FIXME-TABLE: Reword as SizingPolicy?) Disable extending past the limit set by outer_size.y, only meaningful when neither of ScrollX|ScrollY are set (data below the limit will be clipped and not visible) ImGuiTableFlags_NoHostExtendY = 1 << 17, // (FIXME-TABLE: Reword as SizingPolicy?) Disable extending past the limit set by outer_size.y, only meaningful when neither of ScrollX|ScrollY are set (data below the limit will be clipped and not visible)
ImGuiTableFlags_NoKeepColumnsVisible = 1 << 18, // Disable keeping column always minimally visible when table width gets too small and ScrllX is off. ImGuiTableFlags_NoKeepColumnsVisible = 1 << 18, // Disable keeping column always minimally visible when table width gets too small and ScrollX is off.
ImGuiTableFlags_PreciseStretchWidths = 1 << 19, // Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth. ImGuiTableFlags_PreciseStretchWidths = 1 << 19, // Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth.
ImGuiTableFlags_NoClip = 1 << 20, // Disable clipping rectangle for every individual columns (reduce draw command count, items will be able to overflow into other columns). Generally incompatible with TableSetupScrollFreeze(). ImGuiTableFlags_NoClip = 1 << 20, // Disable clipping rectangle for every individual columns (reduce draw command count, items will be able to overflow into other columns). Generally incompatible with TableSetupScrollFreeze().
// Padding // Padding
@ -1099,9 +1100,9 @@ enum ImGuiTableColumnFlags_
ImGuiTableColumnFlags_None = 0, ImGuiTableColumnFlags_None = 0,
ImGuiTableColumnFlags_DefaultHide = 1 << 0, // Default as a hidden column. ImGuiTableColumnFlags_DefaultHide = 1 << 0, // Default as a hidden column.
ImGuiTableColumnFlags_DefaultSort = 1 << 1, // Default as a sorting column. ImGuiTableColumnFlags_DefaultSort = 1 << 1, // Default as a sorting column.
ImGuiTableColumnFlags_WidthFixed = 1 << 2, // Column will keep a fixed size, preferable with horizontal scrolling enabled (default if table sizing policy is SizingPolicyFixedX and table is resizable). ImGuiTableColumnFlags_WidthStretch = 1 << 2, // Column will stretch, preferable with horizontal scrolling disabled (default if table sizing policy is SizingPolicyStretchX).
ImGuiTableColumnFlags_WidthStretch = 1 << 3, // Column will stretch, preferable with horizontal scrolling disabled (default if table sizing policy is SizingPolicyStretchX). ImGuiTableColumnFlags_WidthFixed = 1 << 3, // Column will not stretch, preferable with horizontal scrolling enabled (default if table sizing policy is SizingPolicyFixedX and table is resizable).
ImGuiTableColumnFlags_WidthAlwaysAutoResize = 1 << 4, // Column will keep resizing based on submitted contents (with a one frame delay) == Fixed with auto resize (default if table sizing policy is SizingPolicyFixedX and table is not resizable). ImGuiTableColumnFlags_WidthAutoResize = 1 << 4, // Column will not stretch and keep resizing based on submitted contents (default if table sizing policy is SizingPolicyFixedX and table is not resizable).
ImGuiTableColumnFlags_NoResize = 1 << 5, // Disable manual resizing. ImGuiTableColumnFlags_NoResize = 1 << 5, // Disable manual resizing.
ImGuiTableColumnFlags_NoClipX = 1 << 6, // Disable clipping for this column (all NoClipX columns will render in a same draw command). ImGuiTableColumnFlags_NoClipX = 1 << 6, // Disable clipping for this column (all NoClipX columns will render in a same draw command).
ImGuiTableColumnFlags_NoSort = 1 << 7, // Disable ability to sort on this field (even if ImGuiTableFlags_Sortable is set on the table). ImGuiTableColumnFlags_NoSort = 1 << 7, // Disable ability to sort on this field (even if ImGuiTableFlags_Sortable is set on the table).
@ -1116,7 +1117,7 @@ enum ImGuiTableColumnFlags_
ImGuiTableColumnFlags_NoReorder = 1 << 16, // Disable reordering this column, this will also prevent other columns from crossing over this column. ImGuiTableColumnFlags_NoReorder = 1 << 16, // Disable reordering this column, this will also prevent other columns from crossing over this column.
// [Internal] Combinations and masks // [Internal] Combinations and masks
ImGuiTableColumnFlags_WidthMask_ = ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_WidthAlwaysAutoResize, ImGuiTableColumnFlags_WidthMask_ = ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_WidthAutoResize,
ImGuiTableColumnFlags_IndentMask_ = ImGuiTableColumnFlags_IndentEnable | ImGuiTableColumnFlags_IndentDisable, ImGuiTableColumnFlags_IndentMask_ = ImGuiTableColumnFlags_IndentEnable | ImGuiTableColumnFlags_IndentDisable,
ImGuiTableColumnFlags_NoDirectResize_ = 1 << 20 // [Internal] Disable user resizing this column directly (it may however we resized indirectly from its left edge) ImGuiTableColumnFlags_NoDirectResize_ = 1 << 20 // [Internal] Disable user resizing this column directly (it may however we resized indirectly from its left edge)
}; };

72
imgui_demo.cpp

@ -3496,7 +3496,7 @@ static void ShowDemoWindowTables()
continue; continue;
char buf[32]; char buf[32];
sprintf(buf, "Hello %d,%d", row, column); sprintf(buf, "Hello %d,%d", column, row);
if (contents_type == CT_Text) if (contents_type == CT_Text)
ImGui::TextUnformatted(buf); ImGui::TextUnformatted(buf);
else if (contents_type) else if (contents_type)
@ -3529,7 +3529,7 @@ static void ShowDemoWindowTables()
for (int column = 0; column < 3; column++) for (int column = 0; column < 3; column++)
{ {
ImGui::TableSetColumnIndex(column); ImGui::TableSetColumnIndex(column);
ImGui::Text("Hello %d,%d", row, column); ImGui::Text("Hello %d,%d", column, row);
} }
} }
ImGui::EndTable(); ImGui::EndTable();
@ -3559,7 +3559,7 @@ static void ShowDemoWindowTables()
for (int column = 0; column < 3; column++) for (int column = 0; column < 3; column++)
{ {
ImGui::TableSetColumnIndex(column); ImGui::TableSetColumnIndex(column);
ImGui::Text("Hello %d,%d", row, column); ImGui::Text("Hello %d,%d", column, row);
} }
} }
ImGui::EndTable(); ImGui::EndTable();
@ -3586,7 +3586,7 @@ static void ShowDemoWindowTables()
for (int column = 0; column < 3; column++) for (int column = 0; column < 3; column++)
{ {
ImGui::TableSetColumnIndex(column); ImGui::TableSetColumnIndex(column);
ImGui::Text("%s %d,%d", (column == 2) ? "Stretch" : "Fixed", row, column); ImGui::Text("%s %d,%d", (column == 2) ? "Stretch" : "Fixed", column, row);
} }
} }
ImGui::EndTable(); ImGui::EndTable();
@ -3606,7 +3606,7 @@ static void ShowDemoWindowTables()
for (int column = 0; column < 6; column++) for (int column = 0; column < 6; column++)
{ {
ImGui::TableSetColumnIndex(column); ImGui::TableSetColumnIndex(column);
ImGui::Text("%s %d,%d", (column >= 3) ? "Stretch" : "Fixed", row, column); ImGui::Text("%s %d,%d", (column >= 3) ? "Stretch" : "Fixed", column, row);
} }
} }
ImGui::EndTable(); ImGui::EndTable();
@ -3642,7 +3642,7 @@ static void ShowDemoWindowTables()
for (int column = 0; column < 3; column++) for (int column = 0; column < 3; column++)
{ {
ImGui::TableSetColumnIndex(column); ImGui::TableSetColumnIndex(column);
ImGui::Text("Hello %d,%d", row, column); ImGui::Text("Hello %d,%d", column, row);
} }
} }
ImGui::EndTable(); ImGui::EndTable();
@ -3660,7 +3660,7 @@ static void ShowDemoWindowTables()
for (int column = 0; column < 3; column++) for (int column = 0; column < 3; column++)
{ {
ImGui::TableSetColumnIndex(column); ImGui::TableSetColumnIndex(column);
ImGui::Text("Fixed %d,%d", row, column); ImGui::Text("Fixed %d,%d", column, row);
} }
} }
ImGui::EndTable(); ImGui::EndTable();
@ -3717,7 +3717,7 @@ static void ShowDemoWindowTables()
else else
{ {
char buf[32]; char buf[32];
sprintf(buf, "Hello %d,%d", row, column); sprintf(buf, "Hello %d,%d", column, row);
ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f)); ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f));
} }
//if (ImGui::TableGetHoveredColumn() == column) //if (ImGui::TableGetHoveredColumn() == column)
@ -3756,7 +3756,7 @@ static void ShowDemoWindowTables()
ImGui::TableSetColumnIndex(column); ImGui::TableSetColumnIndex(column);
if (row == 0) if (row == 0)
ImGui::Text("(%.2f)", ImGui::GetContentRegionAvail().x); ImGui::Text("(%.2f)", ImGui::GetContentRegionAvail().x);
ImGui::Text("Hello %d,%d", row, column); ImGui::Text("Hello %d,%d", column, row);
} }
} }
ImGui::EndTable(); ImGui::EndTable();
@ -3797,7 +3797,7 @@ static void ShowDemoWindowTables()
for (int column = 0; column < 3; column++) for (int column = 0; column < 3; column++)
{ {
ImGui::TableSetColumnIndex(column); ImGui::TableSetColumnIndex(column);
ImGui::Text("Hello %d,%d", row, column); ImGui::Text("Hello %d,%d", column, row);
} }
} }
} }
@ -3826,8 +3826,8 @@ static void ShowDemoWindowTables()
// When using ScrollX or ScrollY we need to specify a size for our table container! // When using ScrollX or ScrollY we need to specify a size for our table container!
// Otherwise by default the table will fit all available space, like a BeginChild() call. // Otherwise by default the table will fit all available space, like a BeginChild() call.
ImVec2 size = ImVec2(0, TEXT_BASE_HEIGHT * 8); ImVec2 outer_size = ImVec2(0, TEXT_BASE_HEIGHT * 8);
if (ImGui::BeginTable("##table1", 7, flags, size)) if (ImGui::BeginTable("##table1", 7, flags, outer_size))
{ {
ImGui::TableSetupScrollFreeze(freeze_cols, freeze_rows); ImGui::TableSetupScrollFreeze(freeze_cols, freeze_rows);
ImGui::TableSetupColumn("Line #", ImGuiTableColumnFlags_NoHide); // Make the first column not hideable to match our use of TableSetupScrollFreeze() ImGui::TableSetupColumn("Line #", ImGuiTableColumnFlags_NoHide); // Make the first column not hideable to match our use of TableSetupScrollFreeze()
@ -3849,7 +3849,7 @@ static void ShowDemoWindowTables()
if (column == 0) if (column == 0)
ImGui::Text("Line %d", row); ImGui::Text("Line %d", row);
else else
ImGui::Text("Hello world %d,%d", row, column); ImGui::Text("Hello world %d,%d", column, row);
} }
} }
ImGui::EndTable(); ImGui::EndTable();
@ -3994,14 +3994,15 @@ static void ShowDemoWindowTables()
ImGui::SameLine(); HelpMarker("Default if _ScrollX if disabled. Makes columns use _WidthStretch policy by default."); ImGui::SameLine(); HelpMarker("Default if _ScrollX if disabled. Makes columns use _WidthStretch policy by default.");
if (ImGui::CheckboxFlags("ImGuiTableFlags_SizingPolicyFixedX", &flags, ImGuiTableFlags_SizingPolicyFixedX)) if (ImGui::CheckboxFlags("ImGuiTableFlags_SizingPolicyFixedX", &flags, ImGuiTableFlags_SizingPolicyFixedX))
flags &= ~(ImGuiTableFlags_SizingPolicyMaskX_ ^ ImGuiTableFlags_SizingPolicyFixedX); // Can't specify both sizing polices so we clear the other flags &= ~(ImGuiTableFlags_SizingPolicyMaskX_ ^ ImGuiTableFlags_SizingPolicyFixedX); // Can't specify both sizing polices so we clear the other
ImGui::SameLine(); HelpMarker("Default if _ScrollX if enabled. Makes columns use _WidthFixed by default, or _WidthAlwaysAutoResize if _Resizable is not set."); ImGui::SameLine(); HelpMarker("Default if _ScrollX if enabled. Makes columns use _WidthFixed by default, or _WidthFixedResize if _Resizable is not set.");
ImGui::CheckboxFlags("ImGuiTableFlags_PreciseStretchWidths", &flags, ImGuiTableFlags_PreciseStretchWidths); ImGui::CheckboxFlags("ImGuiTableFlags_PreciseStretchWidths", &flags, ImGuiTableFlags_PreciseStretchWidths);
ImGui::SameLine(); HelpMarker("Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth."); ImGui::SameLine(); HelpMarker("Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth.");
ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable); ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable);
ImGui::CheckboxFlags("ImGuiTableFlags_NoClip", &flags, ImGuiTableFlags_NoClip); ImGui::CheckboxFlags("ImGuiTableFlags_NoClip", &flags, ImGuiTableFlags_NoClip);
PopStyleCompact(); PopStyleCompact();
if (ImGui::BeginTable("##nways", column_count, flags, ImVec2(0, 100))) ImVec2 outer_size(0, TEXT_BASE_HEIGHT * 7);
if (ImGui::BeginTable("##nways", column_count, flags, outer_size))
{ {
for (int row = 0; row < 10; row++) for (int row = 0; row < 10; row++)
{ {
@ -4011,11 +4012,11 @@ static void ShowDemoWindowTables()
ImGui::TableSetColumnIndex(column); ImGui::TableSetColumnIndex(column);
char label[32]; char label[32];
static char text_buf[32] = ""; static char text_buf[32] = "";
sprintf(label, "Hello %d,%d", row, column); sprintf(label, "Hello %d,%d", column, row);
switch (contents_type) switch (contents_type)
{ {
case CT_ShortText: ImGui::TextUnformatted(label); break; case CT_ShortText: ImGui::TextUnformatted(label); break;
case CT_LongText: ImGui::Text("Some longer text %d,%d\nOver two lines..", row, column); break; case CT_LongText: ImGui::Text("Some longer text %d,%d\nOver two lines..", column, row); break;
case CT_Button: ImGui::Button(label); break; case CT_Button: ImGui::Button(label); break;
case CT_FillButton: ImGui::Button(label, ImVec2(-FLT_MIN, 0.0f)); break; case CT_FillButton: ImGui::Button(label, ImVec2(-FLT_MIN, 0.0f)); break;
case CT_InputText: ImGui::SetNextItemWidth(-FLT_MIN); ImGui::InputText("##", text_buf, IM_ARRAYSIZE(text_buf)); break; case CT_InputText: ImGui::SetNextItemWidth(-FLT_MIN); ImGui::InputText("##", text_buf, IM_ARRAYSIZE(text_buf)); break;
@ -4024,6 +4025,35 @@ static void ShowDemoWindowTables()
} }
ImGui::EndTable(); ImGui::EndTable();
} }
HelpMarker(
"Showcase using Stretch columns + ScrollX together: "
"this is rather unusual and only makes sense when specifying an 'inner_width' for the table!\n"
"Without an explicit value, inner_width is == outer_size.x and therefore using Stretch columns + ScrollX together doesn't make sense.");
static ImGuiTableFlags flags2 = ImGuiTableFlags_SizingPolicyStretchX | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_RowBg;
static float inner_width = 1000.0f;
PushStyleCompact();
ImGui::PushID("flags2");
ImGui::CheckboxFlags("ImGuiTableFlags_ScrollX", &flags2, ImGuiTableFlags_ScrollX);
if (ImGui::CheckboxFlags("ImGuiTableFlags_SizingPolicyStretchX", &flags2, ImGuiTableFlags_SizingPolicyStretchX))
flags2 &= ~(ImGuiTableFlags_SizingPolicyMaskX_ ^ ImGuiTableFlags_SizingPolicyStretchX);
if (ImGui::CheckboxFlags("ImGuiTableFlags_SizingPolicyFixedX", &flags2, ImGuiTableFlags_SizingPolicyFixedX))
flags2 &= ~(ImGuiTableFlags_SizingPolicyMaskX_ ^ ImGuiTableFlags_SizingPolicyFixedX);
ImGui::SetNextItemWidth(TEXT_BASE_WIDTH * 10.0f);
ImGui::DragFloat("inner_width", &inner_width, 1.0f, 0.0f, FLT_MAX, "%.1f");
ImGui::PopID();
PopStyleCompact();
if (ImGui::BeginTable("##table2", 7, flags2 | ImGuiTableFlags_SizingPolicyStretchX | ImGuiTableFlags_ContextMenuInBody, outer_size, inner_width))
{
for (int cell = 0; cell < 20 * 7; cell++)
{
ImGui::TableNextColumn();
ImGui::Text("Hello world %d,%d", ImGui::TableGetColumnIndex(), ImGui::TableGetRowIndex());
}
ImGui::EndTable();
}
ImGui::TreePop(); ImGui::TreePop();
} }
@ -4255,7 +4285,7 @@ static void ShowDemoWindowTables()
for (int column = 0; column < 3; column++) for (int column = 0; column < 3; column++)
{ {
char buf[32]; char buf[32];
sprintf(buf, "Cell %d,%d", row, column); sprintf(buf, "Cell %d,%d", column, row);
ImGui::TableSetColumnIndex(column); ImGui::TableSetColumnIndex(column);
ImGui::Selectable(buf, column_selected[column]); ImGui::Selectable(buf, column_selected[column]);
} }
@ -4498,7 +4528,7 @@ static void ShowDemoWindowTables()
ImGui::TreePop(); ImGui::TreePop();
} }
if (ImGui::TreeNodeEx("Decoration:", ImGuiTreeNodeFlags_DefaultOpen)) if (ImGui::TreeNodeEx("Decorations:", ImGuiTreeNodeFlags_DefaultOpen))
{ {
ImGui::CheckboxFlags("ImGuiTableFlags_RowBg", &flags, ImGuiTableFlags_RowBg); ImGui::CheckboxFlags("ImGuiTableFlags_RowBg", &flags, ImGuiTableFlags_RowBg);
ImGui::CheckboxFlags("ImGuiTableFlags_BordersV", &flags, ImGuiTableFlags_BordersV); ImGui::CheckboxFlags("ImGuiTableFlags_BordersV", &flags, ImGuiTableFlags_BordersV);
@ -4611,8 +4641,8 @@ static void ShowDemoWindowTables()
ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_DefaultSort | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoHide, -1.0f, MyItemColumnID_ID); ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_DefaultSort | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoHide, -1.0f, MyItemColumnID_ID);
ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, -1.0f, MyItemColumnID_Name); ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, -1.0f, MyItemColumnID_Name);
ImGui::TableSetupColumn("Action", ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed, -1.0f, MyItemColumnID_Action); ImGui::TableSetupColumn("Action", ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed, -1.0f, MyItemColumnID_Action);
ImGui::TableSetupColumn("Quantity Long Label", ImGuiTableColumnFlags_PreferSortDescending | ImGuiTableColumnFlags_WidthStretch, 1.0f, MyItemColumnID_Quantity);// , ImGuiTableColumnFlags_None | ImGuiTableColumnFlags_WidthAlwaysAutoResize); ImGui::TableSetupColumn("Quantity (Long Label)", ImGuiTableColumnFlags_PreferSortDescending | ImGuiTableColumnFlags_WidthStretch, 1.0f, MyItemColumnID_Quantity);// , ImGuiTableColumnFlags_WidthAutoResize);
ImGui::TableSetupColumn("Description", ImGuiTableColumnFlags_WidthStretch, 1.0f, MyItemColumnID_Description);// , ImGuiTableColumnFlags_WidthAlwaysAutoResize); ImGui::TableSetupColumn("Description", ImGuiTableColumnFlags_WidthStretch, 1.0f, MyItemColumnID_Description);
ImGui::TableSetupColumn("Hidden", ImGuiTableColumnFlags_DefaultHide | ImGuiTableColumnFlags_NoSort); ImGui::TableSetupColumn("Hidden", ImGuiTableColumnFlags_DefaultHide | ImGuiTableColumnFlags_NoSort);
// Sort our data if sort specs have been changed! // Sort our data if sort specs have been changed!

33
imgui_tables.cpp

@ -567,14 +567,14 @@ static ImGuiTableColumnFlags TableFixColumnFlags(ImGuiTable* table, ImGuiTableCo
// Sizing Policy // Sizing Policy
if ((flags & ImGuiTableColumnFlags_WidthMask_) == 0) if ((flags & ImGuiTableColumnFlags_WidthMask_) == 0)
{ {
// FIXME-TABLE: Inconsistent to promote columns to WidthAlwaysAutoResize // FIXME-TABLE: Inconsistent to promote columns to WidthAutoResize
if (table->Flags & ImGuiTableFlags_SizingPolicyFixedX) if (table->Flags & ImGuiTableFlags_SizingPolicyFixedX)
flags |= ((table->Flags & ImGuiTableFlags_Resizable) && !(flags & ImGuiTableColumnFlags_NoResize)) ? ImGuiTableColumnFlags_WidthFixed : ImGuiTableColumnFlags_WidthAlwaysAutoResize; flags |= ((table->Flags & ImGuiTableFlags_Resizable) && !(flags & ImGuiTableColumnFlags_NoResize)) ? ImGuiTableColumnFlags_WidthFixed : ImGuiTableColumnFlags_WidthAutoResize;
else else
flags |= ImGuiTableColumnFlags_WidthStretch; flags |= ImGuiTableColumnFlags_WidthStretch;
} }
IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiTableColumnFlags_WidthMask_)); // Check that only 1 of each set is used. IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiTableColumnFlags_WidthMask_)); // Check that only 1 of each set is used.
if ((flags & ImGuiTableColumnFlags_WidthAlwaysAutoResize))// || ((flags & ImGuiTableColumnFlags_WidthStretch) && (table->Flags & ImGuiTableFlags_SizingPolicyStretchX))) if (flags & ImGuiTableColumnFlags_WidthAutoResize)
flags |= ImGuiTableColumnFlags_NoResize; flags |= ImGuiTableColumnFlags_NoResize;
//if ((flags & ImGuiTableColumnFlags_WidthStretch) && (table->Flags & ImGuiTableFlags_SizingPolicyFixedX)) //if ((flags & ImGuiTableColumnFlags_WidthStretch) && (table->Flags & ImGuiTableFlags_SizingPolicyFixedX))
// flags = (flags & ~ImGuiTableColumnFlags_WidthMask_) | ImGuiTableColumnFlags_WidthFixed; // flags = (flags & ~ImGuiTableColumnFlags_WidthMask_) | ImGuiTableColumnFlags_WidthFixed;
@ -614,9 +614,9 @@ static float TableGetMinColumnWidth()
// Layout columns for the frame // Layout columns for the frame
// Runs on the first call to TableNextRow(), to give a chance for TableSetupColumn() to be called first. // Runs on the first call to TableNextRow(), to give a chance for TableSetupColumn() to be called first.
// FIXME-TABLE: Our width (and therefore our WorkRect) will be minimal in the first frame for WidthAlwaysAutoResize // FIXME-TABLE: Our width (and therefore our WorkRect) will be minimal in the first frame for WidthAutoResize
// columns, increase feedback side-effect with widgets relying on WorkRect.Max.x. Maybe provide a default distribution // columns, increase feedback side-effect with widgets relying on WorkRect.Max.x. Maybe provide a default distribution
// for WidthAlwaysAutoResize columns? // for WidthAutoResize columns?
void ImGui::TableUpdateLayout(ImGuiTable* table) void ImGui::TableUpdateLayout(ImGuiTable* table)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
@ -669,11 +669,11 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
column->WidthAuto = width_auto; column->WidthAuto = width_auto;
if (column->Flags & (ImGuiTableColumnFlags_WidthAlwaysAutoResize | ImGuiTableColumnFlags_WidthFixed)) if (column->Flags & (ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_WidthAutoResize))
{ {
// Process auto-fit for non-stretched columns // Process auto-fit for non-stretched columns
// Latch initial size for fixed columns and update it constantly for auto-resizing column (unless clipped!) // Latch initial size for fixed columns and update it constantly for auto-resizing column (unless clipped!)
if ((column->AutoFitQueue != 0x00) || ((column->Flags & ImGuiTableColumnFlags_WidthAlwaysAutoResize) && !column->IsClipped)) if ((column->AutoFitQueue != 0x00) || ((column->Flags & ImGuiTableColumnFlags_WidthAutoResize) && !column->IsClipped))
column->WidthRequest = width_auto; column->WidthRequest = width_auto;
// FIXME-TABLE: Increase minimum size during init frame to avoid biasing auto-fitting widgets // FIXME-TABLE: Increase minimum size during init frame to avoid biasing auto-fitting widgets
@ -847,7 +847,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
column->IsClipped = (column->ClipRect.Max.x <= column->ClipRect.Min.x) && (column->AutoFitQueue & 1) == 0 && (column->CannotSkipItemsQueue & 1) == 0; column->IsClipped = (column->ClipRect.Max.x <= column->ClipRect.Min.x) && (column->AutoFitQueue & 1) == 0 && (column->CannotSkipItemsQueue & 1) == 0;
if (column->IsClipped) if (column->IsClipped)
table->VisibleUnclippedMaskByIndex &= ~((ImU64)1 << column_n); // Columns with the _WidthAlwaysAutoResize sizing policy will never be updated then. table->VisibleUnclippedMaskByIndex &= ~((ImU64)1 << column_n); // Columns with the _WidthAutoResize sizing policy will never be updated then.
column->IsSkipItems = !column->IsVisible || table->HostSkipItems; column->IsSkipItems = !column->IsVisible || table->HostSkipItems;
@ -893,7 +893,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
} }
// Clear Resizable flag if none of our column are actually resizable (either via an explicit _NoResize flag, // Clear Resizable flag if none of our column are actually resizable (either via an explicit _NoResize flag,
// either because of using _WidthAlwaysAutoResize/_WidthStretch). // either because of using _WidthAutoResize/_WidthStretch).
// This will hide the resizing option from the context menu. // This will hide the resizing option from the context menu.
if (count_resizable == 0 && (table->Flags & ImGuiTableFlags_Resizable)) if (count_resizable == 0 && (table->Flags & ImGuiTableFlags_Resizable))
table->Flags &= ~ImGuiTableFlags_Resizable; table->Flags &= ~ImGuiTableFlags_Resizable;
@ -1621,7 +1621,7 @@ void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags,
table->DeclColumnsCount++; table->DeclColumnsCount++;
// When passing a width automatically enforce WidthFixed policy // When passing a width automatically enforce WidthFixed policy
// (vs TableFixColumnFlags would default to WidthAlwaysAutoResize) // (vs TableFixColumnFlags would default to WidthAutoResize)
// (we write to FlagsIn which is a little misleading, another solution would be to pass init_width_or_weight to TableFixColumnFlags) // (we write to FlagsIn which is a little misleading, another solution would be to pass init_width_or_weight to TableFixColumnFlags)
if ((flags & ImGuiTableColumnFlags_WidthMask_) == 0) if ((flags & ImGuiTableColumnFlags_WidthMask_) == 0)
if ((table->Flags & ImGuiTableFlags_SizingPolicyFixedX) && (init_width_or_weight > 0.0f)) if ((table->Flags & ImGuiTableFlags_SizingPolicyFixedX) && (init_width_or_weight > 0.0f))
@ -2023,6 +2023,15 @@ int ImGui::TableGetColumnIndex()
return table->CurrentColumn; return table->CurrentColumn;
} }
int ImGui::TableGetRowIndex()
{
ImGuiContext& g = *GImGui;
ImGuiTable* table = g.CurrentTable;
if (!table)
return 0;
return table->CurrentRow;
}
// Return the cell rectangle based on currently known height. // Return the cell rectangle based on currently known height.
// - Important: we generally don't know our row height until the end of the row, so Max.y will be incorrect in many situations. // - Important: we generally don't know our row height until the end of the row, so Max.y will be incorrect in many situations.
// The only case where this is correct is if we provided a min_row_height to TableNextRow() and don't go below it. // The only case where this is correct is if we provided a min_row_height to TableNextRow() and don't go below it.
@ -3019,9 +3028,9 @@ void ImGui::DebugNodeTable(ImGuiTable* table)
column->ContentMaxXFrozen - column->WorkMinX, column->ContentMaxXUnfrozen - column->WorkMinX, column->ContentMaxXHeadersUsed - column->WorkMinX, column->ContentMaxXHeadersIdeal - column->WorkMinX, column->ContentMaxXFrozen - column->WorkMinX, column->ContentMaxXUnfrozen - column->WorkMinX, column->ContentMaxXHeadersUsed - column->WorkMinX, column->ContentMaxXHeadersIdeal - column->WorkMinX,
column->SortOrder, (column->SortDirection == ImGuiSortDirection_Ascending) ? "Ascending" : (column->SortDirection == ImGuiSortDirection_Descending) ? "Descending" : "None", column->SortOrder, (column->SortDirection == ImGuiSortDirection_Ascending) ? "Ascending" : (column->SortDirection == ImGuiSortDirection_Descending) ? "Descending" : "None",
column->UserID, column->Flags, column->UserID, column->Flags,
(column->Flags & ImGuiTableColumnFlags_WidthFixed) ? "WidthFixed " : "",
(column->Flags & ImGuiTableColumnFlags_WidthStretch) ? "WidthStretch " : "", (column->Flags & ImGuiTableColumnFlags_WidthStretch) ? "WidthStretch " : "",
(column->Flags & ImGuiTableColumnFlags_WidthAlwaysAutoResize) ? "WidthAlwaysAutoResize " : "", (column->Flags & ImGuiTableColumnFlags_WidthFixed) ? "WidthFixed " : "",
(column->Flags & ImGuiTableColumnFlags_WidthAutoResize) ? "WidthAutoResize " : "",
(column->Flags & ImGuiTableColumnFlags_NoResize) ? "NoResize " : ""); (column->Flags & ImGuiTableColumnFlags_NoResize) ? "NoResize " : "");
Bullet(); Bullet();
Selectable(buf); Selectable(buf);

Loading…
Cancel
Save