Skip to content

Commit 592fc69

Browse files
committed
Tables: moved TableDrawContextMenu(), TableOpenContextMenu() to their own section.
1 parent 7aed4b1 commit 592fc69

File tree

1 file changed

+113
-106
lines changed

1 file changed

+113
-106
lines changed

imgui_tables.cpp

Lines changed: 113 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,112 +2172,6 @@ void ImGui::TablePopBackgroundChannel()
21722172
table->DrawSplitter.SetCurrentChannel(window->DrawList, column->DrawChannelCurrent);
21732173
}
21742174

2175-
// Output context menu into current window (generally a popup)
2176-
// FIXME-TABLE: Ideally this should be writable by the user. Full programmatic access to that data?
2177-
void ImGui::TableDrawContextMenu(ImGuiTable* table)
2178-
{
2179-
ImGuiContext& g = *GImGui;
2180-
ImGuiWindow* window = g.CurrentWindow;
2181-
if (window->SkipItems)
2182-
return;
2183-
2184-
bool want_separator = false;
2185-
const int column_n = (table->ContextPopupColumn >= 0 && table->ContextPopupColumn < table->ColumnsCount) ? table->ContextPopupColumn : -1;
2186-
ImGuiTableColumn* column = (column_n != -1) ? &table->Columns[column_n] : NULL;
2187-
2188-
// Sizing
2189-
if (table->Flags & ImGuiTableFlags_Resizable)
2190-
{
2191-
if (column != NULL)
2192-
{
2193-
const bool can_resize = !(column->Flags & ImGuiTableColumnFlags_NoResize) && column->IsEnabled;
2194-
if (MenuItem("Size column to fit", NULL, false, can_resize))
2195-
TableSetColumnWidthAutoSingle(table, column_n);
2196-
}
2197-
2198-
const char* size_all_desc;
2199-
if (table->ColumnsEnabledFixedCount == table->ColumnsEnabledCount)
2200-
size_all_desc = "Size all columns to fit"; // All fixed
2201-
else if (table->ColumnsEnabledFixedCount == 0)
2202-
size_all_desc = "Size all columns to default"; // All stretch
2203-
else
2204-
size_all_desc = "Size all columns to fit/default"; // Mixed
2205-
if (MenuItem(size_all_desc, NULL))
2206-
TableSetColumnWidthAutoAll(table);
2207-
want_separator = true;
2208-
}
2209-
2210-
// Ordering
2211-
if (table->Flags & ImGuiTableFlags_Reorderable)
2212-
{
2213-
if (MenuItem("Reset order", NULL, false, !table->IsDefaultDisplayOrder))
2214-
table->IsResetDisplayOrderRequest = true;
2215-
want_separator = true;
2216-
}
2217-
2218-
// Sorting
2219-
// (modify TableOpenContextMenu() to add _Sortable flag if enabling this)
2220-
#if 0
2221-
if ((table->Flags & ImGuiTableFlags_Sortable) && column != NULL && (column->Flags & ImGuiTableColumnFlags_NoSort) == 0)
2222-
{
2223-
if (want_separator)
2224-
Separator();
2225-
want_separator = true;
2226-
2227-
bool append_to_sort_specs = g.IO.KeyShift;
2228-
if (MenuItem("Sort in Ascending Order", NULL, column->SortOrder != -1 && column->SortDirection == ImGuiSortDirection_Ascending, (column->Flags & ImGuiTableColumnFlags_NoSortAscending) == 0))
2229-
TableSetColumnSortDirection(table, column_n, ImGuiSortDirection_Ascending, append_to_sort_specs);
2230-
if (MenuItem("Sort in Descending Order", NULL, column->SortOrder != -1 && column->SortDirection == ImGuiSortDirection_Descending, (column->Flags & ImGuiTableColumnFlags_NoSortDescending) == 0))
2231-
TableSetColumnSortDirection(table, column_n, ImGuiSortDirection_Descending, append_to_sort_specs);
2232-
}
2233-
#endif
2234-
2235-
// Hiding / Visibility
2236-
if (table->Flags & ImGuiTableFlags_Hideable)
2237-
{
2238-
if (want_separator)
2239-
Separator();
2240-
want_separator = true;
2241-
2242-
PushItemFlag(ImGuiItemFlags_SelectableDontClosePopup, true);
2243-
for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++)
2244-
{
2245-
ImGuiTableColumn* other_column = &table->Columns[other_column_n];
2246-
const char* name = TableGetColumnName(table, other_column_n);
2247-
if (name == NULL || name[0] == 0)
2248-
name = "<Unknown>";
2249-
2250-
// Make sure we can't hide the last active column
2251-
bool menu_item_active = (other_column->Flags & ImGuiTableColumnFlags_NoHide) ? false : true;
2252-
if (other_column->IsEnabled && table->ColumnsEnabledCount <= 1)
2253-
menu_item_active = false;
2254-
if (MenuItem(name, NULL, other_column->IsEnabled, menu_item_active))
2255-
other_column->IsEnabledNextFrame = !other_column->IsEnabled;
2256-
}
2257-
PopItemFlag();
2258-
}
2259-
}
2260-
2261-
// Use -1 to open menu not specific to a given column.
2262-
void ImGui::TableOpenContextMenu(int column_n)
2263-
{
2264-
ImGuiContext& g = *GImGui;
2265-
ImGuiTable* table = g.CurrentTable;
2266-
if (column_n == -1 && table->CurrentColumn != -1) // When called within a column automatically use this one (for consistency)
2267-
column_n = table->CurrentColumn;
2268-
if (column_n == table->ColumnsCount) // To facilitate using with TableGetHoveredColumn()
2269-
column_n = -1;
2270-
IM_ASSERT(column_n >= -1 && column_n < table->ColumnsCount);
2271-
if (table->Flags & (ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable))
2272-
{
2273-
table->IsContextPopupOpen = true;
2274-
table->ContextPopupColumn = (ImS8)column_n;
2275-
table->InstanceInteracted = table->InstanceCurrent;
2276-
const ImGuiID context_menu_id = ImHashStr("##ContextMenu", 0, table->ID);
2277-
OpenPopupEx(context_menu_id, ImGuiPopupFlags_None);
2278-
}
2279-
}
2280-
22812175
// This is a helper to output TableHeader() calls based on the column names declared in TableSetupColumn().
22822176
// The intent is that advanced users willing to create customized headers would not need to use this helper
22832177
// and can create their own! For example: TableHeader() may be preceeded by Checkbox() or other custom widgets.
@@ -2693,6 +2587,119 @@ void ImGui::TableSortSpecsBuild(ImGuiTable* table)
26932587
table->IsSortSpecsDirty = false; // Mark as not dirty for us
26942588
}
26952589

2590+
//-------------------------------------------------------------------------
2591+
// [SECTION] Tables: Context Menu
2592+
//-------------------------------------------------------------------------
2593+
// - TableOpenContextMenu() [Internal]
2594+
// - TableDrawContextMenu() [Internal]
2595+
//-------------------------------------------------------------------------
2596+
2597+
// Use -1 to open menu not specific to a given column.
2598+
void ImGui::TableOpenContextMenu(int column_n)
2599+
{
2600+
ImGuiContext& g = *GImGui;
2601+
ImGuiTable* table = g.CurrentTable;
2602+
if (column_n == -1 && table->CurrentColumn != -1) // When called within a column automatically use this one (for consistency)
2603+
column_n = table->CurrentColumn;
2604+
if (column_n == table->ColumnsCount) // To facilitate using with TableGetHoveredColumn()
2605+
column_n = -1;
2606+
IM_ASSERT(column_n >= -1 && column_n < table->ColumnsCount);
2607+
if (table->Flags & (ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable))
2608+
{
2609+
table->IsContextPopupOpen = true;
2610+
table->ContextPopupColumn = (ImS8)column_n;
2611+
table->InstanceInteracted = table->InstanceCurrent;
2612+
const ImGuiID context_menu_id = ImHashStr("##ContextMenu", 0, table->ID);
2613+
OpenPopupEx(context_menu_id, ImGuiPopupFlags_None);
2614+
}
2615+
}
2616+
2617+
// Output context menu into current window (generally a popup)
2618+
// FIXME-TABLE: Ideally this should be writable by the user. Full programmatic access to that data?
2619+
void ImGui::TableDrawContextMenu(ImGuiTable* table)
2620+
{
2621+
ImGuiContext& g = *GImGui;
2622+
ImGuiWindow* window = g.CurrentWindow;
2623+
if (window->SkipItems)
2624+
return;
2625+
2626+
bool want_separator = false;
2627+
const int column_n = (table->ContextPopupColumn >= 0 && table->ContextPopupColumn < table->ColumnsCount) ? table->ContextPopupColumn : -1;
2628+
ImGuiTableColumn* column = (column_n != -1) ? &table->Columns[column_n] : NULL;
2629+
2630+
// Sizing
2631+
if (table->Flags & ImGuiTableFlags_Resizable)
2632+
{
2633+
if (column != NULL)
2634+
{
2635+
const bool can_resize = !(column->Flags & ImGuiTableColumnFlags_NoResize) && column->IsEnabled;
2636+
if (MenuItem("Size column to fit", NULL, false, can_resize))
2637+
TableSetColumnWidthAutoSingle(table, column_n);
2638+
}
2639+
2640+
const char* size_all_desc;
2641+
if (table->ColumnsEnabledFixedCount == table->ColumnsEnabledCount)
2642+
size_all_desc = "Size all columns to fit"; // All fixed
2643+
else if (table->ColumnsEnabledFixedCount == 0)
2644+
size_all_desc = "Size all columns to default"; // All stretch
2645+
else
2646+
size_all_desc = "Size all columns to fit/default"; // Mixed
2647+
if (MenuItem(size_all_desc, NULL))
2648+
TableSetColumnWidthAutoAll(table);
2649+
want_separator = true;
2650+
}
2651+
2652+
// Ordering
2653+
if (table->Flags & ImGuiTableFlags_Reorderable)
2654+
{
2655+
if (MenuItem("Reset order", NULL, false, !table->IsDefaultDisplayOrder))
2656+
table->IsResetDisplayOrderRequest = true;
2657+
want_separator = true;
2658+
}
2659+
2660+
// Sorting
2661+
// (modify TableOpenContextMenu() to add _Sortable flag if enabling this)
2662+
#if 0
2663+
if ((table->Flags & ImGuiTableFlags_Sortable) && column != NULL && (column->Flags & ImGuiTableColumnFlags_NoSort) == 0)
2664+
{
2665+
if (want_separator)
2666+
Separator();
2667+
want_separator = true;
2668+
2669+
bool append_to_sort_specs = g.IO.KeyShift;
2670+
if (MenuItem("Sort in Ascending Order", NULL, column->SortOrder != -1 && column->SortDirection == ImGuiSortDirection_Ascending, (column->Flags & ImGuiTableColumnFlags_NoSortAscending) == 0))
2671+
TableSetColumnSortDirection(table, column_n, ImGuiSortDirection_Ascending, append_to_sort_specs);
2672+
if (MenuItem("Sort in Descending Order", NULL, column->SortOrder != -1 && column->SortDirection == ImGuiSortDirection_Descending, (column->Flags & ImGuiTableColumnFlags_NoSortDescending) == 0))
2673+
TableSetColumnSortDirection(table, column_n, ImGuiSortDirection_Descending, append_to_sort_specs);
2674+
}
2675+
#endif
2676+
2677+
// Hiding / Visibility
2678+
if (table->Flags & ImGuiTableFlags_Hideable)
2679+
{
2680+
if (want_separator)
2681+
Separator();
2682+
want_separator = true;
2683+
2684+
PushItemFlag(ImGuiItemFlags_SelectableDontClosePopup, true);
2685+
for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++)
2686+
{
2687+
ImGuiTableColumn* other_column = &table->Columns[other_column_n];
2688+
const char* name = TableGetColumnName(table, other_column_n);
2689+
if (name == NULL || name[0] == 0)
2690+
name = "<Unknown>";
2691+
2692+
// Make sure we can't hide the last active column
2693+
bool menu_item_active = (other_column->Flags & ImGuiTableColumnFlags_NoHide) ? false : true;
2694+
if (other_column->IsEnabled && table->ColumnsEnabledCount <= 1)
2695+
menu_item_active = false;
2696+
if (MenuItem(name, NULL, other_column->IsEnabled, menu_item_active))
2697+
other_column->IsEnabledNextFrame = !other_column->IsEnabled;
2698+
}
2699+
PopItemFlag();
2700+
}
2701+
}
2702+
26962703
//-------------------------------------------------------------------------
26972704
// [SECTION] Tables: Settings (.ini data)
26982705
//-------------------------------------------------------------------------

0 commit comments

Comments
 (0)