RangeSelect/MultiSelect: Clarify and better enforce lifetime of BeginMultiSelect() value.
diff --git a/imgui_demo.cpp b/imgui_demo.cpp
index ccbbd6c..dcfff37 100644
--- a/imgui_demo.cpp
+++ b/imgui_demo.cpp
@@ -2626,7 +2626,7 @@
{
// Data
ImGuiStorage Storage; // Selection set
- int SelectionSize; // Number of selected items (== number of 1 in the Storage, maintained by this class) // FIXME-RANGESELECT: Imply more difficult to track with intrusive selection schemes?
+ int SelectionSize; // Number of selected items (== number of 1 in the Storage, maintained by this class). // FIXME-MULTISELECT: Imply more difficult to track with intrusive selection schemes?
int RangeRef; // Reference/pivot item (generally last clicked item)
// Functions
@@ -2636,13 +2636,12 @@
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 GetSize() const { return SelectionSize; }
- // When using SelectAll() / SetRange() we assume that our objects ID are indices.
+ // When using SetRange() / SelectAll() 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).
// If your selection system is storing selection using object ID and you want to support Shift+Click range-selection,
- // you will need a way to iterate from one object to another given the ID you use.
- // You are likely to need some kind of data structure to convert 'view index' <> 'object ID'.
- // FIXME-MULTISELECT: Would be worth providing a demo of doing this.
- // FIXME-MULTISELECT: This implementation of SetRange() is inefficient because it doesn't take advantage of the fact that ImGuiStorage stores sorted key.
+ // you will need a way to iterate from one item to the other item given the ID you use.
+ // You are likely to need some kind of data structure to convert 'view index' <> 'object ID' (FIXME-MULTISELECT: Would be worth providing a demo of doing this).
+ // Note: This implementation of SetRange() is inefficient because it doesn't take advantage of the fact that ImGuiStorage stores sorted key.
void SetRange(int a, int b, bool v) { if (b < a) { int tmp = b; b = a; a = tmp; } for (int n = a; n <= b; n++) SetSelected(n, v); }
void SelectAll(int count) { Storage.Data.resize(count); for (int idx = 0; idx < count; idx++) Storage.Data[idx] = ImGuiStorage::ImGuiStoragePair((ImGuiID)idx, 1); SelectionSize = count; } // This could be using SetRange(), but it this way is faster.
diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp
index ad9267c..4ed1806 100644
--- a/imgui_widgets.cpp
+++ b/imgui_widgets.cpp
@@ -6665,6 +6665,7 @@
if (data->RequestSetRange) IMGUI_DEBUG_LOG_SELECTION("[selection] %s: RequestSetRange %p..%p = %d (dir %+d)\n", function, data->RangeSrcItem, data->RangeDstItem, data->RangeSelected, data->RangeDirection);
}
+// Return ImGuiMultiSelectIO structure. Lifetime: valid until corresponding call to EndMultiSelect().
ImGuiMultiSelectIO* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags, void* range_ref, bool range_ref_is_selected)
{
ImGuiContext& g = *GImGui;
@@ -6718,6 +6719,7 @@
return &ms->BeginIO;
}
+// Return updated ImGuiMultiSelectIO structure. Lifetime: until EndFrame() or next BeginMultiSelect() call.
ImGuiMultiSelectIO* ImGui::EndMultiSelect()
{
ImGuiContext& g = *GImGui;
@@ -6739,6 +6741,7 @@
ms->FocusScopeId = 0;
ms->Window = NULL;
ms->Flags = ImGuiMultiSelectFlags_None;
+ ms->BeginIO.Clear(); // Invalidate contents of BeginMultiSelect() to enforce scope.
PopFocusScope();
g.CurrentMultiSelect = NULL;