JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_SelectedItemSet.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29//==============================================================================
44template <class SelectableItemType>
46{
47public:
48 //==============================================================================
49 using ItemType = SelectableItemType;
51 using ParameterType = typename TypeHelpers::ParameterType<SelectableItemType>::type;
52
53 //==============================================================================
55 SelectedItemSet() = default;
56
58 explicit SelectedItemSet (const ItemArray& items)
59 : selectedItems (items)
60 {
61 }
62
65 : ChangeBroadcaster(), selectedItems (other.selectedItems)
66 {
67 }
68
71 {
72 if (selectedItems != other.selectedItems)
73 {
74 changed();
75
76 for (int i = selectedItems.size(); --i >= 0;)
77 if (! other.isSelected (selectedItems.getReference (i)))
78 itemDeselected (selectedItems.removeAndReturn (i));
79
80 for (auto& i : other.selectedItems)
81 {
82 if (! isSelected (i))
83 {
84 selectedItems.add (i);
85 itemSelected (i);
86 }
87 }
88 }
89
90 return *this;
91 }
92
93 //==============================================================================
101 void selectOnly (ParameterType item)
102 {
103 if (isSelected (item))
104 {
105 for (int i = selectedItems.size(); --i >= 0;)
106 {
107 if (selectedItems.getUnchecked (i) != item)
108 {
109 deselect (selectedItems.getUnchecked (i));
110 i = jmin (i, selectedItems.size());
111 }
112 }
113 }
114 else
115 {
116 changed();
117 deselectAll();
118
119 selectedItems.add (item);
120 itemSelected (item);
121 }
122 }
123
128 void addToSelection (ParameterType item)
129 {
130 if (! isSelected (item))
131 {
132 changed();
133
134 selectedItems.add (item);
135 itemSelected (item);
136 }
137 }
138
160 void addToSelectionBasedOnModifiers (ParameterType item,
162 {
163 if (modifiers.isShiftDown())
164 {
165 addToSelection (item);
166 }
167 else if (modifiers.isCommandDown())
168 {
169 if (isSelected (item))
170 deselect (item);
171 else
172 addToSelection (item);
173 }
174 else
175 {
176 selectOnly (item);
177 }
178 }
179
197 bool addToSelectionOnMouseDown (ParameterType item,
199 {
200 if (isSelected (item))
201 return ! modifiers.isPopupMenu();
202
204 return false;
205 }
206
229
231 void deselect (ParameterType item)
232 {
233 const int i = selectedItems.indexOf (item);
234
235 if (i >= 0)
236 {
237 changed();
238 itemDeselected (selectedItems.removeAndReturn (i));
239 }
240 }
241
244 {
245 if (selectedItems.size() > 0)
246 {
247 changed();
248
249 for (int i = selectedItems.size(); --i >= 0;)
250 {
251 itemDeselected (selectedItems.removeAndReturn (i));
252 i = jmin (i, selectedItems.size());
253 }
254 }
255 }
256
257 //==============================================================================
261 int getNumSelected() const noexcept { return selectedItems.size(); }
262
267 SelectableItemType getSelectedItem (const int index) const { return selectedItems [index]; }
268
270 bool isSelected (ParameterType item) const noexcept { return selectedItems.contains (item); }
271
273 const ItemArray& getItemArray() const noexcept { return selectedItems; }
274
276 SelectableItemType* begin() noexcept { return selectedItems.begin(); }
277
278 const SelectableItemType* begin() const noexcept { return selectedItems.begin(); }
279
281 SelectableItemType* end() noexcept { return selectedItems.end(); }
282
284 const SelectableItemType* end() const noexcept { return selectedItems.end(); }
285
286 //==============================================================================
293
300
304 void changed()
305 {
307 }
308
312 void changed (const bool synchronous)
313 {
314 if (synchronous)
316 else
318 }
319
320private:
321 //==============================================================================
322 ItemArray selectedItems;
323
325};
326
327} // namespace juce
ElementType getUnchecked(int index) const
Returns one of the elements in the array, without checking the index passed in.
Definition juce_Array.h:252
int size() const noexcept
Returns the current number of elements in the array.
Definition juce_Array.h:215
ElementType * begin() noexcept
Returns a pointer to the first element in the array.
Definition juce_Array.h:328
ElementType * end() noexcept
Returns a pointer to the element which follows the last element in the array.
Definition juce_Array.h:344
int indexOf(ParameterType elementToLookFor) const
Finds the index of the first element which matches the value passed in.
Definition juce_Array.h:382
void add(const ElementType &newElement)
Appends a new element at the end of the array.
Definition juce_Array.h:418
ElementType removeAndReturn(int indexToRemove)
Removes an element from the array.
Definition juce_Array.h:760
bool contains(ParameterType elementToLookFor) const
Returns true if the array contains at least one occurrence of an object.
Definition juce_Array.h:400
ElementType & getReference(int index) noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in.
Definition juce_Array.h:267
Holds a list of ChangeListeners, and sends messages to them when instructed.
void sendChangeMessage()
Causes an asynchronous change message to be sent to all the registered listeners.
void sendSynchronousChangeMessage()
Sends a synchronous change message to all the registered listeners.
Represents the state of the mouse buttons and modifier keys.
Manages a list of selectable items.
void addToSelectionOnMouseUp(ParameterType item, ModifierKeys modifiers, const bool wasItemDragged, const bool resultOfMouseDownSelectMethod)
Selects or deselects items that can also be dragged, based on a mouse-up event.
void addToSelection(ParameterType item)
Selects an item.
bool addToSelectionOnMouseDown(ParameterType item, ModifierKeys modifiers)
Selects or deselects items that can also be dragged, based on a mouse-down event.
int getNumSelected() const noexcept
Returns the number of currently selected items.
bool isSelected(ParameterType item) const noexcept
True if this item is currently selected.
SelectedItemSet(const ItemArray &items)
Creates a set based on an array of items.
const SelectableItemType * end() const noexcept
Provides iterator access to the array of items.
SelectedItemSet()=default
Creates an empty set.
void deselectAll()
Deselects all items.
void selectOnly(ParameterType item)
Clears any other currently selected items, and selects this item.
void changed()
Used internally, but can be called to force a change message to be sent to the ChangeListeners.
SelectableItemType getSelectedItem(const int index) const
Returns one of the currently selected items.
SelectableItemType * begin() noexcept
Provides iterator access to the array of items.
void changed(const bool synchronous)
Used internally, but can be called to force a change message to be sent to the ChangeListeners.
SelectedItemSet(const SelectedItemSet &other)
Creates a copy of another set.
SelectedItemSet & operator=(const SelectedItemSet &other)
Creates a copy of another set.
virtual void itemSelected(SelectableItemType)
Can be overridden to do special handling when an item is selected.
virtual void itemDeselected(SelectableItemType)
Can be overridden to do special handling when an item is deselected.
const ItemArray & getItemArray() const noexcept
Provides access to the array of items.
void addToSelectionBasedOnModifiers(ParameterType item, ModifierKeys modifiers)
Selects or deselects an item.
void deselect(ParameterType item)
Deselects an item.
SelectableItemType * end() noexcept
Provides iterator access to the array of items.
#define JUCE_LEAK_DETECTOR(OwnerClass)
This macro lets you embed a leak-detecting object inside a class.
JUCE Namespace.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88