| 2772 | }; |
| 2773 | |
| 2774 | static void DemoWindowWidgetsSelectionAndMultiSelect(ImGuiDemoWindowData* demo_data) |
| 2775 | { |
| 2776 | if (ImGui::TreeNode("Selection State & Multi-Select")) |
| 2777 | { |
| 2778 | IMGUI_DEMO_MARKER("Widgets/Selection State & Multi-Select"); |
| 2779 | HelpMarker("Selections can be built using Selectable(), TreeNode() or other widgets. Selection state is owned by application code/data."); |
| 2780 | |
| 2781 | ImGui::BulletText("Wiki page:"); |
| 2782 | ImGui::SameLine(); |
| 2783 | ImGui::TextLinkOpenURL("imgui/wiki/Multi-Select", "https://github.com/ocornut/imgui/wiki/Multi-Select"); |
| 2784 | |
| 2785 | // Without any fancy API: manage single-selection yourself. |
| 2786 | if (ImGui::TreeNode("Single-Select")) |
| 2787 | { |
| 2788 | IMGUI_DEMO_MARKER("Widgets/Selection State/Single-Select"); |
| 2789 | static int selected = -1; |
| 2790 | for (int n = 0; n < 5; n++) |
| 2791 | { |
| 2792 | char buf[32]; |
| 2793 | sprintf(buf, "Object %d", n); |
| 2794 | if (ImGui::Selectable(buf, selected == n)) |
| 2795 | selected = n; |
| 2796 | } |
| 2797 | ImGui::TreePop(); |
| 2798 | } |
| 2799 | |
| 2800 | // Demonstrate implementation a most-basic form of multi-selection manually |
| 2801 | // This doesn't support the Shift modifier which requires BeginMultiSelect()! |
| 2802 | if (ImGui::TreeNode("Multi-Select (manual/simplified, without BeginMultiSelect)")) |
| 2803 | { |
| 2804 | IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select (manual/simplified, without BeginMultiSelect)"); |
| 2805 | HelpMarker("Hold Ctrl and Click to select multiple items."); |
| 2806 | static bool selection[5] = { false, false, false, false, false }; |
| 2807 | for (int n = 0; n < 5; n++) |
| 2808 | { |
| 2809 | char buf[32]; |
| 2810 | sprintf(buf, "Object %d", n); |
| 2811 | if (ImGui::Selectable(buf, selection[n])) |
| 2812 | { |
| 2813 | if (!ImGui::GetIO().KeyCtrl) // Clear selection when Ctrl is not held |
| 2814 | memset(selection, 0, sizeof(selection)); |
| 2815 | selection[n] ^= 1; // Toggle current item |
| 2816 | } |
| 2817 | } |
| 2818 | ImGui::TreePop(); |
| 2819 | } |
| 2820 | |
| 2821 | // Demonstrate handling proper multi-selection using the BeginMultiSelect/EndMultiSelect API. |
| 2822 | // Shift+Click w/ Ctrl and other standard features are supported. |
| 2823 | // We use the ImGuiSelectionBasicStorage helper which you may freely reimplement. |
| 2824 | if (ImGui::TreeNode("Multi-Select")) |
| 2825 | { |
| 2826 | IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select"); |
| 2827 | ImGui::Text("Supported features:"); |
| 2828 | ImGui::BulletText("Keyboard navigation (arrows, page up/down, home/end, space)."); |
| 2829 | ImGui::BulletText("Ctrl modifier to preserve and toggle selection."); |
| 2830 | ImGui::BulletText("Shift modifier for range selection."); |
| 2831 | ImGui::BulletText("Ctrl+A to select all."); |
no test coverage detected