Skip to content

Commit

Permalink
Tables: Made only first column honor Indent by default (like Columns …
Browse files Browse the repository at this point in the history
…api) and exposed flags. Added simple Tree demo. (#2957)
  • Loading branch information
ocornut committed Jan 9, 2020
1 parent c4c4652 commit 33925f5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
3 changes: 3 additions & 0 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -981,9 +981,12 @@ enum ImGuiTableColumnFlags_
ImGuiTableColumnFlags_NoHeaderWidth = 1 << 11, // Header width don't contribute to automatic column width.
ImGuiTableColumnFlags_PreferSortAscending = 1 << 12, // Make the initial sort direction Ascending when first sorting on this column (default).
ImGuiTableColumnFlags_PreferSortDescending = 1 << 13, // Make the initial sort direction Descending when first sorting on this column.
ImGuiTableColumnFlags_IndentEnable = 1 << 14, // Use current Indent value when entering cell (default for 1st column).
ImGuiTableColumnFlags_IndentDisable = 1 << 15, // Ignore current Indent value when entering cell (default for columns after the 1st one). Indentation changes _within_ the cell will still be honored.

// [Internal] Combinations and masks
ImGuiTableColumnFlags_WidthMask_ = ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_WidthAlwaysAutoResize,
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)
};

Expand Down
91 changes: 89 additions & 2 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3146,6 +3146,8 @@ static void ShowDemoWindowTables()
ImGui::CheckboxFlags("_NoSortDescending", (unsigned int*)&column_flags[column], ImGuiTableColumnFlags_NoSortDescending);
ImGui::CheckboxFlags("_PreferSortAscending", (unsigned int*)&column_flags[column], ImGuiTableColumnFlags_PreferSortAscending);
ImGui::CheckboxFlags("_PreferSortDescending", (unsigned int*)&column_flags[column], ImGuiTableColumnFlags_PreferSortDescending);
ImGui::CheckboxFlags("_IndentEnable", (unsigned int*)&column_flags[column], ImGuiTableColumnFlags_IndentEnable);
ImGui::CheckboxFlags("_IndentDisable", (unsigned int*)&column_flags[column], ImGuiTableColumnFlags_IndentDisable);
ImGui::PopID();
ImGui::PopStyleVar(2);
}
Expand All @@ -3154,20 +3156,23 @@ static void ShowDemoWindowTables()

// Create the real table we care about for the example!
const ImGuiTableFlags flags = ImGuiTableFlags_SizingPolicyFixedX | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Sortable;
if (ImGui::BeginTable("##table1", column_count, flags))
if (ImGui::BeginTable("##table", column_count, flags))
{
for (int column = 0; column < column_count; column++)
ImGui::TableSetupColumn(column_names[column], column_flags[column]);
ImGui::TableAutoHeaders();
for (int row = 0; row < 8; row++)
{
ImGui::Indent(2.0f); // Add some indentation to demonstrate usage of per-column IndentEnable/IndentDisable flags.
ImGui::TableNextRow();
for (int column = 0; column < column_count; column++)
{
ImGui::TableSetColumnIndex(column);
ImGui::Text("Hello %s", ImGui::TableGetColumnName(column));
ImGui::Text("%s %s", (column == 0) ? "Indented" : "Hello", ImGui::TableGetColumnName(column));
}
}
ImGui::Unindent(2.0f * 8.0f);

ImGui::EndTable();
}
ImGui::TreePop();
Expand Down Expand Up @@ -3330,6 +3335,88 @@ static void ShowDemoWindowTables()
ImGui::TreePop();
}

if (open_action != -1)
ImGui::SetNextItemOpen(open_action != 0);
if (ImGui::TreeNode("Tree view"))
{
static ImGuiTableFlags flags = ImGuiTableFlags_BordersV | ImGuiTableFlags_BordersHOuter | ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg;
//ImGui::CheckboxFlags("ImGuiTableFlags_Scroll", (unsigned int*)&flags, ImGuiTableFlags_Scroll);
//ImGui::CheckboxFlags("ImGuiTableFlags_ScrollFreezeLeftColumn", (unsigned int*)&flags, ImGuiTableFlags_ScrollFreezeLeftColumn);

if (ImGui::BeginTable("##3ways", 3, flags))
{
// The first column will use the default _WidthStretch when ScrollX is Off and _WidthFixed when ScrollX is On
ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_NoHide);
ImGui::TableSetupColumn("Size", ImGuiTableColumnFlags_WidthFixed, ImGui::GetFontSize() * 10);
ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed, ImGui::GetFontSize() * 20);
ImGui::TableAutoHeaders();

// Simple storage to output a dummy file-system.
struct MyTreeNode
{
const char* Name;
const char* Type;
int Size;
int ChildIdx;
int ChildCount;
static void DisplayNode(const MyTreeNode* node, const MyTreeNode* all_nodes)
{
ImGui::TableNextRow();
if (ImGui::GetIO().KeyCtrl)
ImGui::SmallButton("before");
const bool is_folder = (node->ChildCount > 0);
if (is_folder)
{
bool open = ImGui::TreeNodeEx(node->Name, ImGuiTreeNodeFlags_SpanFullWidth);
if (ImGui::GetIO().KeyCtrl)
ImGui::SmallButton("after");
ImGui::TableNextCell();
ImGui::TextDisabled("--");
if (ImGui::GetIO().KeyCtrl)
ImGui::SmallButton("after");
ImGui::TableNextCell();
ImGui::TextUnformatted(node->Type);
if (open)
{
for (int child_n = 0; child_n < node->ChildCount; child_n++)
DisplayNode(&all_nodes[node->ChildIdx + child_n], all_nodes);
ImGui::TreePop();
}
}
else
{
ImGui::TreeNodeEx(node->Name, ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth);
if (ImGui::GetIO().KeyCtrl)
ImGui::SmallButton("after");
ImGui::TableNextCell();
ImGui::Text("%d", node->Size);
if (ImGui::GetIO().KeyCtrl)
ImGui::SmallButton("after");
ImGui::TableNextCell();
ImGui::TextUnformatted(node->Type);
}
}
};
static const MyTreeNode nodes[] =
{
{ "Root", "Folder", -1, 1, 3 }, // 0
{ "Music", "Folder", -1, 4, 2 }, // 1
{ "Textures", "Folder", -1, 6, 3 }, // 2
{ "desktop.ini", "System file", 1024, -1,-1 }, // 3
{ "File1_a.wav", "Audio file", 123000, -1,-1 }, // 4
{ "File1_b.wav", "Audio file", 456000, -1,-1 }, // 5
{ "Image001.png", "Image file", 203128, -1,-1 }, // 6
{ "Copy of Image001.png", "Image file", 203256, -1,-1 }, // 7
{ "Copy of Image001 (Final2).png","Image file", 203512, -1,-1 }, // 8
};

MyTreeNode::DisplayNode(&nodes[0], nodes);

ImGui::EndTable();
}
ImGui::TreePop();
}

static const char* template_items_names[] =
{
"Banana", "Apple", "Cherry", "Watermelon", "Grapefruit", "Strawberry", "Mango",
Expand Down
1 change: 1 addition & 0 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,7 @@ struct ImGuiTable
ImU32 BorderColorLight;
float BorderX1;
float BorderX2;
float HostIndentX;
float CellPaddingX1; // Padding from each borders
float CellPaddingX2;
float CellPaddingY;
Expand Down
7 changes: 6 additions & 1 deletion imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7811,6 +7811,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG

// Backup a copy of host window members we will modify
ImGuiWindow* inner_window = table->InnerWindow;
table->HostIndentX = inner_window->DC.Indent.x;
table->HostClipRect = inner_window->ClipRect;
table->HostSkipItems = inner_window->SkipItems;
table->HostWorkRect = inner_window->WorkRect;
Expand Down Expand Up @@ -8127,6 +8128,8 @@ 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);
if ((column->Flags & ImGuiTableColumnFlags_IndentMask_) == 0)
column->Flags |= (column_n == 0) ? ImGuiTableColumnFlags_IndentEnable : ImGuiTableColumnFlags_IndentDisable;

// We have a unusual edge case where if the user doesn't call TableGetSortSpecs() but has sorting enabled
// or varying sorting flags, we still want the sorting arrows to honor those flags.
Expand Down Expand Up @@ -9155,7 +9158,9 @@ void ImGui::TableBeginCell(ImGuiTable* table, int column_no)
ImGuiTableColumn* column = &table->Columns[column_no];
ImGuiWindow* window = table->InnerWindow;

const float start_x = (table->RowFlags & ImGuiTableRowFlags_Headers) ? column->StartXHeaders : column->StartXRows;
float start_x = (table->RowFlags & ImGuiTableRowFlags_Headers) ? column->StartXHeaders : column->StartXRows;
if (column->Flags & ImGuiTableColumnFlags_IndentEnable)
start_x += window->DC.Indent.x - table->HostIndentX;

window->DC.LastItemId = 0;
window->DC.CursorPos.x = start_x;
Expand Down

0 comments on commit 33925f5

Please sign in to comment.