| 2641 | }; |
| 2642 | |
| 2643 | static void DemoWindowWidgetsSelectionAndMultiSelect(ImGuiDemoWindowData* demo_data) |
| 2644 | { |
| 2645 | IMGUI_DEMO_MARKER("Widgets/Selection State & Multi-Select"); |
| 2646 | if (ImGui::TreeNode("Selection State & Multi-Select")) |
| 2647 | { |
| 2648 | HelpMarker("Selections can be built using Selectable(), TreeNode() or other widgets. Selection state is owned by application code/data."); |
| 2649 | |
| 2650 | // Without any fancy API: manage single-selection yourself. |
| 2651 | IMGUI_DEMO_MARKER("Widgets/Selection State/Single-Select"); |
| 2652 | if (ImGui::TreeNode("Single-Select")) |
| 2653 | { |
| 2654 | static int selected = -1; |
| 2655 | for (int n = 0; n < 5; n++) |
| 2656 | { |
| 2657 | char buf[32]; |
| 2658 | sprintf(buf, "Object %d", n); |
| 2659 | if (ImGui::Selectable(buf, selected == n)) |
| 2660 | selected = n; |
| 2661 | } |
| 2662 | ImGui::TreePop(); |
| 2663 | } |
| 2664 | |
| 2665 | // Demonstrate implementation a most-basic form of multi-selection manually |
| 2666 | // This doesn't support the SHIFT modifier which requires BeginMultiSelect()! |
| 2667 | IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select (manual/simplified, without BeginMultiSelect)"); |
| 2668 | if (ImGui::TreeNode("Multi-Select (manual/simplified, without BeginMultiSelect)")) |
| 2669 | { |
| 2670 | HelpMarker("Hold CTRL and click to select multiple items."); |
| 2671 | static bool selection[5] = { false, false, false, false, false }; |
| 2672 | for (int n = 0; n < 5; n++) |
| 2673 | { |
| 2674 | char buf[32]; |
| 2675 | sprintf(buf, "Object %d", n); |
| 2676 | if (ImGui::Selectable(buf, selection[n])) |
| 2677 | { |
| 2678 | if (!ImGui::GetIO().KeyCtrl) // Clear selection when CTRL is not held |
| 2679 | memset(selection, 0, sizeof(selection)); |
| 2680 | selection[n] ^= 1; // Toggle current item |
| 2681 | } |
| 2682 | } |
| 2683 | ImGui::TreePop(); |
| 2684 | } |
| 2685 | |
| 2686 | // Demonstrate handling proper multi-selection using the BeginMultiSelect/EndMultiSelect API. |
| 2687 | // SHIFT+Click w/ CTRL and other standard features are supported. |
| 2688 | // We use the ImGuiSelectionBasicStorage helper which you may freely reimplement. |
| 2689 | IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select"); |
| 2690 | if (ImGui::TreeNode("Multi-Select")) |
| 2691 | { |
| 2692 | ImGui::Text("Supported features:"); |
| 2693 | ImGui::BulletText("Keyboard navigation (arrows, page up/down, home/end, space)."); |
| 2694 | ImGui::BulletText("Ctrl modifier to preserve and toggle selection."); |
| 2695 | ImGui::BulletText("Shift modifier for range selection."); |
| 2696 | ImGui::BulletText("CTRL+A to select all."); |
| 2697 | ImGui::BulletText("Escape to clear selection."); |
| 2698 | ImGui::BulletText("Click and drag to box-select."); |
| 2699 | ImGui::Text("Tip: Use 'Demo->Tools->Debug Log->Selection' to see selection requests as they happen."); |
| 2700 |
no test coverage detected