RangeSelect/MultiSelect: removed DragDropActive/preserve_existing_selection logic which seems unused + comments.
Can't find trace of early prototype for range-select but I couldn't find way to trigger this anymore. May be wrong. Will find out.
diff --git a/imgui.h b/imgui.h
index 252ec95..5ff8892 100644
--- a/imgui.h
+++ b/imgui.h
@@ -1782,7 +1782,7 @@
enum ImGuiMultiSelectFlags_
{
ImGuiMultiSelectFlags_None = 0,
- ImGuiMultiSelectFlags_NoMultiSelect = 1 << 0,
+ ImGuiMultiSelectFlags_NoMultiSelect = 1 << 0, // Disable selecting more than one item. This is not very useful at this kind of selection can be implemented without BeginMultiSelect(), but this is available for consistency.
ImGuiMultiSelectFlags_NoUnselect = 1 << 1, // Disable unselecting items with CTRL+Click, CTRL+Space etc.
ImGuiMultiSelectFlags_NoSelectAll = 1 << 2, // Disable CTRL+A shortcut to set RequestSelectAll
ImGuiMultiSelectFlags_ClearOnClickWindowVoid= 1 << 3, // Clear selection when clicking on empty location within host window (use if BeginMultiSelect() covers a whole window)
@@ -2509,7 +2509,7 @@
bool RangeValue; // End // End: parameter from RequestSetRange request. true = Select Range, false = Unselect Range.
void* RangeSrc; // Begin, End // End: parameter from RequestSetRange request + you need to save this value so you can pass it again next frame. / Begin: this is the value you passed to BeginMultiSelect()
void* RangeDst; // End // End: parameter from RequestSetRange request.
- int RangeDirection; // End // End: parameter from RequestSetRange request. +1 if RangeSrc came before RangeDst, -1 otherwise. Available as an indicator in case you cannot infer order from the void* values.
+ int RangeDirection; // End // End: parameter from RequestSetRange request. +1 if RangeSrc came before RangeDst, -1 otherwise. Available as an indicator in case you cannot infer order from the void* values. If your void* values are storing indices you will never need this.
ImGuiMultiSelectData() { Clear(); }
void Clear()
diff --git a/imgui_demo.cpp b/imgui_demo.cpp
index c55d080..7455666 100644
--- a/imgui_demo.cpp
+++ b/imgui_demo.cpp
@@ -2633,7 +2633,7 @@
void Clear() { Storage.Clear(); SelectionSize = 0; }
bool GetSelected(int n) const { return Storage.GetInt((ImGuiID)n, 0) != 0; }
void SetSelected(int n, bool v) { int* p_int = Storage.GetIntRef((ImGuiID)n, 0); if (*p_int == (int)v) return; if (v) SelectionSize++; else SelectionSize--; *p_int = (bool)v; }
- int GetSelectionSize() const { return SelectionSize; }
+ int GetSize() const { return SelectionSize; }
// When using SelectAll() / SetRange() we assume that our objects ID are indices.
// In this demo we always store selection using indices and never in another manner (e.g. object ID or pointers).
@@ -2654,7 +2654,6 @@
}
};
-
static void ShowDemoWindowMultiSelect()
{
IMGUI_DEMO_MARKER("Widgets/Selection State");
@@ -2719,7 +2718,7 @@
// The BeginListBox() has no actual purpose for selection logic (other that offering a scrolling regions).
const int ITEMS_COUNT = 50;
- ImGui::Text("Selection size: %d", selection.GetSelectionSize());
+ ImGui::Text("Selection size: %d", selection.GetSize());
if (ImGui::BeginListBox("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20)))
{
ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape;
@@ -2764,7 +2763,8 @@
static bool use_drag_drop = true;
static bool use_multiple_scopes = false;
static ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_None;
- static WidgetType widget_type = WidgetType_TreeNode;
+ static WidgetType widget_type = WidgetType_Selectable;
+
if (ImGui::RadioButton("Selectables", widget_type == WidgetType_Selectable)) { widget_type = WidgetType_Selectable; }
ImGui::SameLine();
if (ImGui::RadioButton("Tree nodes", widget_type == WidgetType_TreeNode)) { widget_type = WidgetType_TreeNode; }
@@ -2797,7 +2797,7 @@
}
else
{
- ImGui::Text("Selection size: %d", selection->GetSelectionSize());
+ ImGui::Text("Selection size: %d", selection->GetSize());
draw_selection = ImGui::BeginListBox("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20));
}
if (draw_selection)
@@ -2813,7 +2813,7 @@
selection->ApplyRequests(multi_select_data, ITEMS_COUNT);
if (use_multiple_scopes)
- ImGui::Text("Selection size: %d", selection->GetSelectionSize()); // Draw counter below Separator and after BeginMultiSelect()
+ ImGui::Text("Selection size: %d", selection->GetSize()); // Draw counter below Separator and after BeginMultiSelect()
if (use_table)
{
@@ -2840,7 +2840,7 @@
ImGui::PushID(n);
const char* category = random_names[n % IM_ARRAYSIZE(random_names)];
char label[64];
- sprintf(label, "Object %05d (category: %s)", n, category);
+ sprintf(label, "Object %05d: category: %s", n, category);
bool item_is_selected = selection->GetSelected(n);
// Emit a color button, to test that Shift+LeftArrow landing on an item that is not part
@@ -2857,7 +2857,7 @@
selection->SetSelected(n, !item_is_selected);
if (use_drag_drop && ImGui::BeginDragDropSource())
{
- ImGui::Text("(Dragging %d items)", selection->GetSelectionSize());
+ ImGui::Text("(Dragging %d items)", selection->GetSize());
ImGui::EndDragDropSource();
}
}
@@ -2872,7 +2872,7 @@
selection->SetSelected(n, !item_is_selected);
if (use_drag_drop && ImGui::BeginDragDropSource())
{
- ImGui::Text("(Dragging %d items)", selection->GetSelectionSize());
+ ImGui::Text("(Dragging %d items)", selection->GetSize());
ImGui::EndDragDropSource();
}
if (open)
@@ -2887,6 +2887,7 @@
ImGui::EndPopup();
}
+ // Demo content within a table
if (use_table)
{
ImGui::TableNextColumn();
diff --git a/imgui_internal.h b/imgui_internal.h
index 47b3b9a..0017a3d 100644
--- a/imgui_internal.h
+++ b/imgui_internal.h
@@ -1948,9 +1948,6 @@
ImVec2 NavWindowingAccumDeltaPos;
ImVec2 NavWindowingAccumDeltaSize;
- // Range-Select/Multi-Select
- ImGuiMultiSelectState MultiSelectState; // FIXME-MULTISELECT: We currently don't support recursing/stacking multi-select
-
// Render
float DimBgRatio; // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list)
@@ -1991,6 +1988,9 @@
ImVector<ImGuiPtrOrIndex> CurrentTabBarStack;
ImVector<ImGuiShrinkWidthItem> ShrinkWidthBuffer;
+ // Multi-Select state
+ ImGuiMultiSelectState MultiSelectState; // FIXME-MULTISELECT: We currently don't support recursing/stacking multi-select
+
// Hover Delay system
ImGuiID HoverItemDelayId;
ImGuiID HoverItemDelayIdPreviousFrame;
@@ -3065,7 +3065,7 @@
IMGUI_API bool IsDragDropPayloadBeingAccepted();
IMGUI_API void RenderDragDropTargetRect(const ImRect& bb);
- // New Multi-Selection/Range-Selection API (FIXME-WIP)
+ // Multi-Select API
IMGUI_API void MultiSelectItemHeader(ImGuiID id, bool* p_selected);
IMGUI_API void MultiSelectItemFooter(ImGuiID id, bool* p_selected, bool* p_pressed);
diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp
index 94a7c81..6912d90 100644
--- a/imgui_widgets.cpp
+++ b/imgui_widgets.cpp
@@ -6691,22 +6691,20 @@
ms->In.RequestClear = true;
}
- // Shortcuts
- if (ms->IsFocused)
- {
- // Select All helper shortcut (CTRL+A)
- // Note: we are comparing FocusScope so we don't need to be testing for IsWindowFocused()
- if (!(flags & ImGuiMultiSelectFlags_NoMultiSelect) && !(flags & ImGuiMultiSelectFlags_NoSelectAll))
- if (Shortcut(ImGuiMod_Ctrl | ImGuiKey_A))
- ms->In.RequestSelectAll = true;
+ // Shortcut: Select all (CTRL+A)
+ if (ms->IsFocused && !(flags & ImGuiMultiSelectFlags_NoMultiSelect) && !(flags & ImGuiMultiSelectFlags_NoSelectAll))
+ if (Shortcut(ImGuiMod_Ctrl | ImGuiKey_A))
+ ms->In.RequestSelectAll = true;
- if (flags & ImGuiMultiSelectFlags_ClearOnEscape)
- if (Shortcut(ImGuiKey_Escape)) // FIXME-MULTISELECT: Only hog shortcut if selection is not null, meaning we need "has selection or "selection size" data here.
- ms->In.RequestClear = true;
- }
+ // Shortcut: Clear selection (Escape)
+ // FIXME-MULTISELECT: Only hog shortcut if selection is not null, meaning we need "has selection or "selection size" data here.
+ // Otherwise may be done by caller but it means Shortcut() needs to be exposed.
+ if (ms->IsFocused && (flags & ImGuiMultiSelectFlags_ClearOnEscape))
+ if (Shortcut(ImGuiKey_Escape))
+ ms->In.RequestClear = true;
- //if (ms->In.RequestClear) IMGUI_DEBUG_LOG("BeginMultiSelect: RequestClear\n");
- //if (ms->In.RequestSelectAll) IMGUI_DEBUG_LOG("BeginMultiSelect: RequestSelectAll\n");
+ //if (ms->In.RequestClear) IMGUI_DEBUG_LOG_SELECTION("BeginMultiSelect: RequestClear\n");
+ //if (ms->In.RequestSelectAll) IMGUI_DEBUG_LOG_SELECTION("BeginMultiSelect: RequestSelectAll\n");
return &ms->In;
}
@@ -6735,9 +6733,9 @@
ms->Flags = ImGuiMultiSelectFlags_None;
PopFocusScope();
- //if (ms->Out.RequestClear) IMGUI_DEBUG_LOG("EndMultiSelect: RequestClear\n");
- //if (ms->Out.RequestSelectAll) IMGUI_DEBUG_LOG("EndMultiSelect: RequestSelectAll\n");
- //if (ms->Out.RequestSetRange) IMGUI_DEBUG_LOG("EndMultiSelect: RequestSetRange %p..%p = %d\n", ms->Out.RangeSrc, ms->Out.RangeDst, ms->Out.RangeValue);
+ //if (ms->Out.RequestClear) IMGUI_DEBUG_LOG_SELECTION("EndMultiSelect: RequestClear\n");
+ //if (ms->Out.RequestSelectAll) IMGUI_DEBUG_LOG_SELECTION("EndMultiSelect: RequestSelectAll\n");
+ //if (ms->Out.RequestSetRange) IMGUI_DEBUG_LOG_SELECTION("EndMultiSelect: RequestSetRange %p..%p = %d (dir %+d)\n", ms->Out.RangeSrc, ms->Out.RangeDst, ms->Out.RangeValue, ms->Out.RangeDirection);
return &ms->Out;
}
@@ -6752,7 +6750,7 @@
// This designed so widgets can also cheaply set this before calling ItemAdd(), so we are not tied to MultiSelect api.
g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasSelectionData;
- // Auto updating RangeSrcPassedBy for cases were clipped is not used.
+ // Auto updating RangeSrcPassedBy for cases were clipper is not used.
if (g.MultiSelectState.In.RangeSrc == item_data)
g.MultiSelectState.In.RangeSrcPassedBy = true;
}
@@ -6854,6 +6852,7 @@
ImGuiInputSource input_source = (g.NavJustMovedToId == id || g.NavActivateId == id) ? g.NavInputSource : ImGuiInputSource_Mouse;
if (is_shift && is_multiselect)
{
+ // Shift+Arrow always select, Ctrl+Shift+Arrow copy source selection state.
ms->Out.RequestSetRange = true;
ms->Out.RangeDst = item_data;
if (!is_ctrl)
@@ -6862,7 +6861,8 @@
}
else
{
- selected = (!is_ctrl || (ms->Flags & ImGuiMultiSelectFlags_NoUnselect)) ? true : !selected;
+ // Ctrl inverts selection, otherwise always select
+ selected = (is_ctrl && (ms->Flags & ImGuiMultiSelectFlags_NoUnselect) == 0) ? !selected : true;
ms->Out.RangeSrc = ms->Out.RangeDst = item_data;
ms->Out.RangeValue = selected;
}
@@ -6870,13 +6870,12 @@
if (input_source == ImGuiInputSource_Mouse || g.NavActivateId == id)
{
// Mouse click without CTRL clears the selection, unless the clicked item is already selected
- bool preserve_existing_selection = g.DragDropActive;
- if (is_multiselect && !is_ctrl && !preserve_existing_selection)
+ if (is_multiselect && !is_ctrl)
ms->Out.RequestClear = true;
- if (is_multiselect && !is_shift && !preserve_existing_selection && ms->Out.RequestClear)
+ if (is_multiselect && !is_shift && ms->Out.RequestClear)
{
// For toggle selection unless there is a Clear request, we can handle it completely locally without sending a RangeSet request.
- IM_ASSERT(ms->Out.RangeSrc == ms->Out.RangeDst); // Setup by block above
+ IM_ASSERT(ms->Out.RangeSrc == ms->Out.RangeDst); // Setup by else block above
ms->Out.RequestSetRange = true;
ms->Out.RangeValue = selected;
ms->Out.RangeDirection = +1;