31 noChoicesMessage (
TRANS (
"(no choices)"))
48 if (label->isEditableOnSingleClick() != isEditable || label->isEditableOnDoubleClick() != isEditable)
50 label->setEditable (isEditable, isEditable,
false);
51 labelEditableState = (isEditable ? labelIsEditable : labelIsNotEditable);
64 return label->isEditable();
69 label->setJustificationType (justification);
74 return label->getJustificationType();
121 if (
auto* item = getItemForId (itemId))
127 if (
auto* item = getItemForId (itemId))
128 return item->isEnabled;
135 if (
auto* item = getItemForId (itemId))
145 if (! label->isEditable())
156 auto& item = iterator.getItem();
158 if (item.itemID == itemId)
166PopupMenu::Item* ComboBox::getItemForIndex (
const int index)
const noexcept
170 for (PopupMenu::MenuItemIterator iterator (currentMenu,
true); iterator.next();)
172 auto& item = iterator.getItem();
174 if (item.itemID != 0)
188 auto& item = iterator.getItem();
190 if (item.itemID != 0)
199 if (
auto* item = getItemForIndex (index))
207 if (
auto* item = getItemForIndex (index))
221 auto& item = iterator.getItem();
223 if (item.itemID == itemId)
226 else if (item.itemID != 0)
252 if (
auto* item = getItemForId (currentId.
getValue()))
276bool ComboBox::selectIfEnabled (
const int index)
278 if (
auto* item = getItemForIndex (index))
290bool ComboBox::nudgeSelectedItem (
int delta)
293 if (selectIfEnabled (i))
301 if (lastCurrentId != (
int) currentId.
getValue())
308 return label->getText();
315 auto& item = iterator.getItem();
329 if (label->getText() !=
newText)
355 return textWhenNothingSelected;
365 return noChoicesMessage;
375 if (textWhenNothingSelected.
isNotEmpty() && label->getText().isEmpty() && ! label->isBeingEdited())
376 getLookAndFeel().drawComboBoxTextWhenNothingSelected (g, *
this, *label);
416 if (label !=
nullptr)
418 newLabel->setEditable (label->isEditable());
419 newLabel->setJustificationType (label->getJustificationType());
420 newLabel->setTooltip (label->getTooltip());
429 EditableState
newEditableState = (label->isEditable() ? labelIsEditable : labelIsNotEditable);
438 label->addMouseListener (
this,
false);
439 label->setAccessible (labelEditableState == labelIsEditable);
450 nudgeSelectedItem (-1);
456 nudgeSelectedItem (1);
462 showPopupIfNotActive();
484void ComboBox::showPopupIfNotActive()
509static void comboBoxPopupMenuFinishedCallback (
int result,
ComboBox*
combo)
511 if (
combo !=
nullptr)
516 combo->setSelectedId (result);
525 auto menu = currentMenu;
527 if (
menu.getNumItems() > 0)
533 auto& item = iterator.getItem();
535 if (item.itemID != 0)
541 menu.addItem (1, noChoicesMessage,
false,
false);
546 menu.setLookAndFeel (&
lf);
547 menu.showMenuAsync (
lf.getOptionsForComboBoxPopupMenu (*
this, *label),
558 if (isButtonDown && (e.
eventComponent ==
this || ! label->isEditable()))
559 showPopupIfNotActive();
567 showPopupIfNotActive();
574 isButtonDown =
false;
577 auto e =
e2.getEventRelativeTo (
this);
580 && (
e2.eventComponent ==
this || ! label->isEditable()))
582 showPopupIfNotActive();
591 mouseWheelAccumulator +=
wheel.deltaY * 5.0f;
593 while (mouseWheelAccumulator > 1.0f)
595 mouseWheelAccumulator -= 1.0f;
596 nudgeSelectedItem (-1);
599 while (mouseWheelAccumulator < -1.0f)
601 mouseWheelAccumulator += 1.0f;
602 nudgeSelectedItem (1);
613 scrollWheelEnabled = enabled;
623 listeners.callChecked (
checker, [
this] (
Listener&
l) {
l.comboBoxChanged (
this); });
628 NullCheckedInvocation::invoke (
onChange);
658 AccessibilityRole::comboBox,
669 return comboBox.
isPopupActive() ? state.withExpanded() : state.withCollapsed();
684 bool isReadOnly()
const override {
return true; }
685 String getCurrentValueAsString()
const override {
return comboBox.getText(); }
686 void setValueAsString (
const String&)
override {}
695 static AccessibilityActions getAccessibilityActions (ComboBox& comboBox)
709 return std::make_unique<ComboBoxAccessibilityHandler> (*
this);
Base class for accessible Components.
virtual AccessibleState getCurrentState() const
Returns the current state of the UI element.
A value interface that represents a text value.
Represents the state of an accessible UI element.
AccessibleState withExpandable() const noexcept
Sets the expandable flag and returns the new state.
void handleUpdateNowIfNeeded()
If an update has been triggered and is pending, this will invoke it synchronously.
void triggerAsyncUpdate()
Causes the callback to be triggered at a later time.
String getHelp() const override
Some help text for the UI element (if required).
String getTitle() const override
The title of the UI element.
AccessibleState getCurrentState() const override
Returns the current state of the UI element.
A class for receiving events from a ComboBox.
A component that lets the user choose from a drop-down list of choices.
void handleAsyncUpdate() override
Called back to do whatever your class needs to do.
@ textColourId
The colour for the text in the box.
void showEditor()
Programmatically opens the text editor to allow the user to edit the current item.
bool isPopupActive() const noexcept
Returns true if the popup menu is currently being shown.
void valueChanged(Value &) override
Called when a Value object is changed.
void colourChanged() override
This method is called when a colour is changed by the setColour() method, or when the look-and-feel i...
void setText(const String &newText, NotificationType notification=sendNotificationAsync)
Sets the contents of the combo-box's text field.
void addItemList(const StringArray &items, int firstItemIdOffset)
Adds an array of items to the drop-down list.
void setJustificationType(Justification justification)
Sets the style of justification to be used for positioning the text.
void clear(NotificationType notification=sendNotificationAsync)
Removes all the items from the drop-down list.
void resized() override
Called when this component's size has been changed.
void lookAndFeelChanged() override
Called to let the component react to a change in the look-and-feel setting.
bool isItemEnabled(int itemId) const noexcept
Returns true if the given item is enabled.
void addSectionHeading(const String &headingName)
Adds a heading to the drop-down list, so that you can group the items into different sections.
void mouseDrag(const MouseEvent &) override
Called when the mouse is moved while a button is held down.
void focusGained(Component::FocusChangeType) override
Called to indicate that this component has just acquired the keyboard focus.
void hidePopup()
Hides the combo box's popup list, if it's currently visible.
virtual void showPopup()
Pops up the combo box's list.
void setItemEnabled(int itemId, bool shouldBeEnabled)
This allows items in the drop-down list to be selectively disabled.
void setSelectedItemIndex(int newItemIndex, NotificationType notification=sendNotificationAsync)
Sets one of the items to be the current selection.
void mouseUp(const MouseEvent &) override
Called when a mouse button is released.
void addListener(Listener *listener)
Registers a listener that will be called when the box's content changes.
void paint(Graphics &) override
Components can override this method to draw their content.
void setSelectedId(int newItemId, NotificationType notification=sendNotificationAsync)
Sets one of the items to be the current selection.
void parentHierarchyChanged() override
Called to indicate that the component's parents have changed.
bool keyStateChanged(bool) override
Called when a key is pressed or released.
void setScrollWheelEnabled(bool enabled) noexcept
This can be used to allow the scroll-wheel to nudge the chosen item.
void mouseDown(const MouseEvent &) override
Called when a mouse button is pressed.
Justification getJustificationType() const noexcept
Returns the current justification for the text box.
void mouseWheelMove(const MouseEvent &, const MouseWheelDetails &) override
Called when the mouse-wheel is moved.
std::function< void()> onChange
You can assign a lambda to this callback object to have it called when the selected ID is changed.
std::unique_ptr< AccessibilityHandler > createAccessibilityHandler() override
Override this method to return a custom AccessibilityHandler for this component.
void setTextWhenNothingSelected(const String &newMessage)
Sets a message to display when there is no item currently selected.
String getItemText(int index) const
Returns the text for one of the items in the list.
void setTooltip(const String &newTooltip) override
Gives the ComboBox a tooltip.
String getTooltip() override
Returns the string that this object wants to show as its tooltip.
bool isTextEditable() const noexcept
Returns true if the text is directly editable.
void setEditableText(bool isEditable)
Sets whether the text in the combo-box is editable.
int indexOfItemId(int itemId) const noexcept
Returns the index in the list of a particular item ID.
int getItemId(int index) const noexcept
Returns the ID for one of the items in the list.
void addSeparator()
Adds a separator line to the drop-down list.
ComboBox(const String &componentName={})
Creates a combo-box.
String getTextWhenNoChoicesAvailable() const
Returns the text shown when no items have been added to the list.
void changeItemText(int itemId, const String &newText)
Changes the text for an existing item.
void focusLost(Component::FocusChangeType) override
Called to indicate that this component has just lost the keyboard focus.
int getSelectedId() const noexcept
Returns the ID of the item that's currently shown in the box.
String getText() const
Returns the text that is currently shown in the combo-box's text field.
void addItem(const String &newItemText, int newItemId)
Adds an item to be shown in the drop-down list.
int getNumItems() const noexcept
Returns the number of items that have been added to the list.
String getTextWhenNothingSelected() const
Returns the text that is shown when no item is selected.
bool keyPressed(const KeyPress &) override
Called when a key is pressed.
~ComboBox() override
Destructor.
void enablementChanged() override
Callback to indicate that this component has been enabled or disabled.
int getSelectedItemIndex() const
Returns the index of the item that's currently shown in the box.
void removeListener(Listener *listener)
Deregisters a previously-registered listener.
void setTextWhenNoChoicesAvailable(const String &newMessage)
Sets the message to show when there are no items in the list, and the user clicks on the drop-down bo...
A class to keep an eye on a component and check for it being deleted.
The base class for all JUCE user-interface objects.
void setRepaintsOnMouseActivity(bool shouldRepaint) noexcept
Causes automatic repaints when the mouse enters or exits this component.
bool reallyContains(Point< int > localPoint, bool returnTrueIfWithinAChild)
Returns true if a given point lies in this component, taking any overlapping siblings into account.
int getHeight() const noexcept
Returns the component's height in pixels.
void addAndMakeVisible(Component *child, int zOrder=-1)
Adds a child component to this one, and also makes the child visible if it isn't already.
FocusChangeType
Enumeration used by the focusGained() and focusLost() methods.
AccessibilityHandler * getAccessibilityHandler()
Returns the accessibility handler for this component, or nullptr if this component is not accessible.
void repaint()
Marks the whole component as needing to be redrawn.
void setWantsKeyboardFocus(bool wantsFocus) noexcept
Sets a flag to indicate whether this component wants keyboard focus or not.
Colour findColour(int colourID, bool inheritFromParent=false) const
Looks for a colour that has been registered with the given colour ID number.
int getWidth() const noexcept
Returns the component's width in pixels.
void mouseWheelMove(const MouseEvent &event, const MouseWheelDetails &wheel) override
Called when the mouse-wheel is moved.
static void JUCE_CALLTYPE beginDragAutoRepeat(int millisecondsBetweenCallbacks)
Ensures that a non-stop stream of mouse-drag events will be sent during the current mouse-drag operat...
bool isEnabled() const noexcept
Returns true if the component (and all its parents) are enabled.
LookAndFeel & getLookAndFeel() const noexcept
Finds the appropriate look-and-feel to use for this component.
A graphics context, used for drawing a component or image.
Represents a type of justification to be used when positioning graphical items.
Represents a key press, including any modifier keys that are needed.
static const int upKey
key-code for the cursor-up key
static bool isKeyCurrentlyDown(int keyCode)
Checks whether a particular key is held down, irrespective of modifiers.
static const int rightKey
key-code for the cursor-right key
static const int downKey
key-code for the cursor-down key
static const int returnKey
key-code for the return key
static const int leftKey
key-code for the cursor-left key
@ backgroundColourId
The background colour to fill the label with.
@ textColourId
The colour for the text.
static bool callAsync(std::function< void()> functionToCall)
Asynchronously invokes a function or C++11 lambda on the message thread.
static ModalComponentManager::Callback * forComponent(void(*functionToCall)(int, ComponentType *), ComponentType *component)
This is a utility function to create a ModalComponentManager::Callback that will call a static functi...
bool isPopupMenu() const noexcept
Checks whether the user is trying to launch a pop-up menu.
Contains position and status information about a mouse event.
const ModifierKeys mods
The key modifiers associated with the event.
bool mouseWasDraggedSinceMouseDown() const noexcept
Returns true if the user seems to be performing a drag gesture.
Component *const eventComponent
The component that this event applies to.
A special array for holding a list of strings.
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
@ backgroundColourId
The colour to use for the text component's background - this can be transparent if necessary.
@ highlightColourId
The colour with which to fill the background of highlighted sections of the text - this can be transp...
@ textColourId
The colour that will be used when text is added to the editor.
@ outlineColourId
If this is non-transparent, it will be used to draw a box around the edge of the component.
Represents a shared variant value.
void addListener(Listener *listener)
Adds a listener to receive callbacks when the value changes.
void removeListener(Listener *listener)
Removes a listener that was previously added with addListener().
var getValue() const
Returns the current value.
#define TRANS(stringLiteral)
Uses the LocalisedStrings class to translate the given string literal.
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
@ valueChanged
Indicates that the UI element's value has changed.
NotificationType
These enums are used in various classes to indicate whether a notification event should be sent out.
@ sendNotification
Requests a notification message, either synchronous or not.
@ sendNotificationSync
Requests a synchronous notification.
@ dontSendNotification
No notification message should be sent.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
@ showMenu
Represents the user showing a contextual menu for a UI element.
@ press
Represents a "press" action.
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Returns true if a value is at least zero, and also below a specified upper limit.
Contains status information about a mouse wheel event.