diff --git a/examples/Demo/Source/Demos/CameraDemo.cpp b/examples/Demo/Source/Demos/CameraDemo.cpp index 08f1387577..f638140b60 100644 --- a/examples/Demo/Source/Demos/CameraDemo.cpp +++ b/examples/Demo/Source/Demos/CameraDemo.cpp @@ -30,7 +30,6 @@ //============================================================================== class CameraDemo : public Component, - private ComboBox::Listener, private CameraDevice::Listener, private AsyncUpdater { @@ -46,7 +45,7 @@ public: addAndMakeVisible (cameraSelectorComboBox); updateCameraList(); cameraSelectorComboBox.setSelectedId (1); - cameraSelectorComboBox.addListener (this); + cameraSelectorComboBox.onChange = [this] { cameraChanged(); }; addAndMakeVisible (snapshotButton); snapshotButton.onClick = [this]() { takeSnapshot(); }; @@ -117,7 +116,7 @@ private: cameraSelectorComboBox.addItem (cameras[i], i + 2); } - void comboBoxChanged (ComboBox*) override + void cameraChanged() { // This is called when the user chooses a camera from the drop-down list. cameraDevice = nullptr; diff --git a/examples/Demo/Source/Demos/FlexBoxDemo.cpp b/examples/Demo/Source/Demos/FlexBoxDemo.cpp index b93be90a9a..4ac6a76741 100644 --- a/examples/Demo/Source/Demos/FlexBoxDemo.cpp +++ b/examples/Demo/Source/Demos/FlexBoxDemo.cpp @@ -30,8 +30,7 @@ #if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS struct DemoFlexPanel : public juce::Component, - private juce::TextEditor::Listener, - private juce::ComboBox::Listener + private juce::TextEditor::Listener { DemoFlexPanel (juce::Colour col, FlexItem& item) : flexItem (item), colour (col) { @@ -61,7 +60,7 @@ struct DemoFlexPanel : public juce::Component, alignSelfCombo.addItem ("stretch", 5); alignSelfCombo.setBounds (x, y, 90, 18); - alignSelfCombo.addListener (this); + alignSelfCombo.onChange = [this] { updateAssignSelf(); }; alignSelfCombo.setSelectedId (5); alignSelfCombo.setColour (ComboBox::outlineColourId, Colours::transparentBlack); addAndMakeVisible (alignSelfCombo); @@ -85,15 +84,16 @@ struct DemoFlexPanel : public juce::Component, addAndMakeVisible (label); } - void comboBoxChanged (ComboBox* cb) override + void updateAssignSelf() { - auto selectedID = cb->getSelectedId(); - - if (selectedID == 1) flexItem.alignSelf = FlexItem::AlignSelf::autoAlign; - if (selectedID == 2) flexItem.alignSelf = FlexItem::AlignSelf::flexStart; - if (selectedID == 3) flexItem.alignSelf = FlexItem::AlignSelf::flexEnd; - if (selectedID == 4) flexItem.alignSelf = FlexItem::AlignSelf::center; - if (selectedID == 5) flexItem.alignSelf = FlexItem::AlignSelf::stretch; + switch (alignSelfCombo.getSelectedId()) + { + case 1: flexItem.alignSelf = FlexItem::AlignSelf::autoAlign; break; + case 2: flexItem.alignSelf = FlexItem::AlignSelf::flexStart; break; + case 3: flexItem.alignSelf = FlexItem::AlignSelf::flexEnd; break; + case 4: flexItem.alignSelf = FlexItem::AlignSelf::center; break; + case 5: flexItem.alignSelf = FlexItem::AlignSelf::stretch; break; + } if (auto parent = getParentComponent()) parent->resized(); diff --git a/examples/Demo/Source/Demos/FontsDemo.cpp b/examples/Demo/Source/Demos/FontsDemo.cpp index 63ff650dd8..bfe4a1cbbd 100644 --- a/examples/Demo/Source/Demos/FontsDemo.cpp +++ b/examples/Demo/Source/Demos/FontsDemo.cpp @@ -30,8 +30,7 @@ //============================================================================== class FontsDemo : public Component, private ListBoxModel, - private Slider::Listener, - private ComboBox::Listener + private Slider::Listener { public: FontsDemo() @@ -58,9 +57,10 @@ public: heightSlider.addListener (this); kerningSlider.addListener (this); scaleSlider.addListener (this); + boldToggle.onClick = [this]() { refreshPreviewBoxFont(); }; italicToggle.onClick = [this]() { refreshPreviewBoxFont(); }; - styleBox.addListener (this); + styleBox.onChange = [this]() { refreshPreviewBoxFont(); }; Font::findFonts (fonts); // Generate the list of fonts @@ -244,12 +244,6 @@ private: } } - void comboBoxChanged (ComboBox* box) override - { - if (box == &styleBox) - refreshPreviewBoxFont(); - } - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FontsDemo) }; diff --git a/examples/Demo/Source/Demos/LookAndFeelDemo.cpp b/examples/Demo/Source/Demos/LookAndFeelDemo.cpp index 4648a92f97..9ab7a842c5 100644 --- a/examples/Demo/Source/Demos/LookAndFeelDemo.cpp +++ b/examples/Demo/Source/Demos/LookAndFeelDemo.cpp @@ -504,8 +504,7 @@ struct LookAndFeelDemoComponent : public Component }; //============================================================================== -class LookAndFeelDemo : public Component, - private ComboBox::Listener +class LookAndFeelDemo : public Component { public: LookAndFeelDemo() @@ -536,7 +535,7 @@ public: addLookAndFeel (slaf, "Square Look And Feel"); setupSquareLookAndFeelColours (*slaf); - lafBox.addListener (this); + lafBox.onChange = [this]() { setAllLookAndFeels (lookAndFeels[lafBox.getSelectedItemIndex()]); }; lafBox.setSelectedItemIndex (3); addAndMakeVisible (randomButton); @@ -612,12 +611,6 @@ private: child->setLookAndFeel (laf); } - void comboBoxChanged (ComboBox* comboBoxThatHasChanged) override - { - if (comboBoxThatHasChanged == &lafBox) - setAllLookAndFeels (lookAndFeels[lafBox.getSelectedItemIndex()]); - } - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LookAndFeelDemo) }; diff --git a/examples/Demo/Source/Demos/MidiDemo.cpp b/examples/Demo/Source/Demos/MidiDemo.cpp index 9a8047e0e4..ddc0e8bb18 100644 --- a/examples/Demo/Source/Demos/MidiDemo.cpp +++ b/examples/Demo/Source/Demos/MidiDemo.cpp @@ -68,7 +68,6 @@ private: //============================================================================== class MidiDemo : public Component, - private ComboBox::Listener, private MidiInputCallback, private MidiKeyboardStateListener, private AsyncUpdater @@ -76,7 +75,6 @@ class MidiDemo : public Component, public: MidiDemo() : deviceManager (MainAppWindow::getSharedAudioDeviceManager()), - lastInputIndex (0), isAddingFromMidiInput (false), keyboardComponent (keyboardState, MidiKeyboardComponent::horizontalKeyboard), midiLogListBoxModel (midiMessageList) { @@ -89,9 +87,10 @@ public: addAndMakeVisible (midiInputList); midiInputList.setTextWhenNoChoicesAvailable ("No MIDI Inputs Enabled"); - const StringArray midiInputs (MidiInput::getDevices()); + + auto midiInputs = MidiInput::getDevices(); midiInputList.addItemList (midiInputs, 1); - midiInputList.addListener (this); + midiInputList.onChange = [this]() { setMidiInput (midiInputList.getSelectedItemIndex()); }; // find the first enabled device and use that by default for (int i = 0; i < midiInputs.size(); ++i) @@ -116,7 +115,7 @@ public: addAndMakeVisible (midiOutputList); midiOutputList.setTextWhenNoChoicesAvailable ("No MIDI Output Enabled"); midiOutputList.addItemList (MidiOutput::getDevices(), 1); - midiOutputList.addListener (this); + midiOutputList.onChange = [this]() { setMidiOutput (midiOutputList.getSelectedItemIndex()); }; addAndMakeVisible (keyboardComponent); keyboardState.addListener (this); @@ -129,7 +128,6 @@ public: { keyboardState.removeListener (this); deviceManager.removeMidiInputCallback (MidiInput::getDevices()[midiInputList.getSelectedItemIndex()], this); - midiInputList.removeListener (this); } void paint (Graphics& g) override @@ -139,7 +137,7 @@ public: void resized() override { - Rectangle area (getLocalBounds()); + auto area = getLocalBounds(); midiInputList.setBounds (area.removeFromTop (36).removeFromRight (getWidth() - 150).reduced (8)); midiOutputList.setBounds (area.removeFromTop (36).removeFromRight (getWidth() - 150).reduced (8)); keyboardComponent.setBounds (area.removeFromTop (80).reduced(8)); @@ -150,8 +148,8 @@ private: AudioDeviceManager& deviceManager; ComboBox midiInputList, midiOutputList; Label midiInputListLabel, midiOutputListLabel; - int lastInputIndex; - bool isAddingFromMidiInput; + int lastInputIndex = 0; + bool isAddingFromMidiInput = false; MidiKeyboardState keyboardState; MidiKeyboardComponent keyboardComponent; @@ -165,11 +163,11 @@ private: /** Starts listening to a MIDI input device, enabling it if necessary. */ void setMidiInput (int index) { - const StringArray list (MidiInput::getDevices()); + auto list = MidiInput::getDevices(); deviceManager.removeMidiInputCallback (list[lastInputIndex], this); - const String newInput (list[index]); + auto newInput = list[index]; if (! deviceManager.isMidiInputEnabled (newInput)) deviceManager.setMidiInputEnabled (newInput, true); @@ -192,12 +190,6 @@ private: } } - void comboBoxChanged (ComboBox* box) override - { - if (box == &midiInputList) setMidiInput (midiInputList.getSelectedItemIndex()); - if (box == &midiOutputList) setMidiOutput (midiOutputList.getSelectedItemIndex()); - } - // These methods handle callbacks from the midi device + on-screen keyboard.. void handleIncomingMidiMessage (MidiInput*, const MidiMessage& message) override { @@ -210,7 +202,7 @@ private: { if (! isAddingFromMidiInput) { - MidiMessage m (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity)); + auto m = MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity); m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001); postMessageToList (m); } @@ -220,7 +212,7 @@ private: { if (! isAddingFromMidiInput) { - MidiMessage m (MidiMessage::noteOff (midiChannel, midiNoteNumber, velocity)); + auto m = MidiMessage::noteOff (midiChannel, midiNoteNumber, velocity); m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001); postMessageToList (m); } diff --git a/examples/Demo/Source/Demos/OpenGLDemo.cpp b/examples/Demo/Source/Demos/OpenGLDemo.cpp index 570cf960c9..eed43520c7 100644 --- a/examples/Demo/Source/Demos/OpenGLDemo.cpp +++ b/examples/Demo/Source/Demos/OpenGLDemo.cpp @@ -329,7 +329,6 @@ struct OpenGLDemoClasses */ class DemoControlsOverlay : public Component, private CodeDocument::Listener, - private ComboBox::Listener, private Slider::Listener, private Timer { @@ -378,11 +377,11 @@ struct OpenGLDemoClasses textures.add (new DynamicTexture()); addAndMakeVisible (textureBox); - textureBox.addListener (this); + textureBox.onChange = [this]() { selectTexture (textureBox.getSelectedId()); }; updateTexturesList(); addAndMakeVisible (presetBox); - presetBox.addListener (this); + presetBox.onChange = [this]() { selectPreset (presetBox.getSelectedItemIndex()); }; auto presets = getPresets(); StringArray presetNames; @@ -539,14 +538,6 @@ struct OpenGLDemoClasses fragmentDocument.getAllContent()); } - void comboBoxChanged (ComboBox* box) override - { - if (box == &presetBox) - selectPreset (presetBox.getSelectedItemIndex()); - else if (box == &textureBox) - selectTexture (textureBox.getSelectedId()); - } - void lookAndFeelChanged() override { auto editorBackground = getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground, diff --git a/examples/Demo/Source/Demos/OpenGLDemo2D.cpp b/examples/Demo/Source/Demos/OpenGLDemo2D.cpp index 201cf78ea5..d41d0181d8 100644 --- a/examples/Demo/Source/Demos/OpenGLDemo2D.cpp +++ b/examples/Demo/Source/Demos/OpenGLDemo2D.cpp @@ -31,7 +31,6 @@ //============================================================================== class OpenGL2DShaderDemo : public Component, private CodeDocument::Listener, - private ComboBox::Listener, private Timer { public: @@ -40,14 +39,14 @@ public: { setOpaque (true); - if (MainAppWindow* mw = MainAppWindow::getMainAppWindow()) + if (auto* mw = MainAppWindow::getMainAppWindow()) mw->setOpenGLRenderingEngine(); addAndMakeVisible (statusLabel); statusLabel.setJustificationType (Justification::topLeft); statusLabel.setFont (Font (14.0f)); - Array presets (getPresets()); + auto presets = getPresets(); StringArray presetNames; for (int i = 0; i < presets.size(); ++i) @@ -58,7 +57,7 @@ public: presetLabel.attachToComponent (&presetBox, true); addAndMakeVisible (presetBox); - presetBox.addListener (this); + presetBox.onChange = [this]() { selectPreset (presetBox.getSelectedItemIndex()); }; fragmentEditorComp.setOpaque (false); fragmentDocument.addListener (this); @@ -152,11 +151,6 @@ private: repaint(); } - void comboBoxChanged (ComboBox*) override - { - selectPreset (presetBox.getSelectedItemIndex()); - } - struct ShaderPreset { const char* name; diff --git a/examples/Demo/Source/Demos/WidgetsDemo.cpp b/examples/Demo/Source/Demos/WidgetsDemo.cpp index 944ad26e00..2c6018b30c 100644 --- a/examples/Demo/Source/Demos/WidgetsDemo.cpp +++ b/examples/Demo/Source/Demos/WidgetsDemo.cpp @@ -950,8 +950,7 @@ private: //============================================================================== // This is a custom component containing a combo box, which we're going to put inside // our table's "rating" column. - class RatingColumnCustomComponent : public Component, - private ComboBox::Listener + class RatingColumnCustomComponent : public Component { public: RatingColumnCustomComponent (TableDemoComponent& td) : owner (td) @@ -966,8 +965,7 @@ private: comboBox.addItem ("swingin", 6); comboBox.addItem ("wild", 7); - // when the combo is changed, we'll get a callback. - comboBox.addListener (this); + comboBox.onChange = [this]() { owner.setRating (row, comboBox.getSelectedId()); }; comboBox.setWantsKeyboardFocus (false); } @@ -984,11 +982,6 @@ private: comboBox.setSelectedId (owner.getRating (row), dontSendNotification); } - void comboBoxChanged (ComboBox*) override - { - owner.setRating (row, comboBox.getSelectedId()); - } - private: TableDemoComponent& owner; ComboBox comboBox; diff --git a/examples/Demo/Source/Demos/XMLandJSONDemo.cpp b/examples/Demo/Source/Demos/XMLandJSONDemo.cpp index 6eae2522cc..a6a0273fc0 100644 --- a/examples/Demo/Source/Demos/XMLandJSONDemo.cpp +++ b/examples/Demo/Source/Demos/XMLandJSONDemo.cpp @@ -208,7 +208,6 @@ private: //============================================================================== class StringsDemo : public Component, - private ComboBox::Listener, private CodeDocument::Listener { public: @@ -225,10 +224,17 @@ public: setOpaque (true); addAndMakeVisible (typeBox); - typeBox.addListener (this); typeBox.addItem ("XML", 1); typeBox.addItem ("JSON", 2); + typeBox.onChange = [this]() + { + if (typeBox.getSelectedId() == 1) + reset (xml); + else + reset (json); + }; + comboBoxLabel.setText ("Database Type:", dontSendNotification); comboBoxLabel.attachToComponent (&typeBox, true); @@ -364,17 +370,6 @@ private: } } - void comboBoxChanged (ComboBox* box) override - { - if (box == &typeBox) - { - if (typeBox.getSelectedId() == 1) - reset (xml); - else - reset (json); - } - } - void codeDocumentTextInserted (const String&, int) override { rebuildTree(); } void codeDocumentTextDeleted (int, int) override { rebuildTree(); } diff --git a/modules/juce_audio_utils/gui/juce_AudioDeviceSelectorComponent.cpp b/modules/juce_audio_utils/gui/juce_AudioDeviceSelectorComponent.cpp index 5433da31e0..56bebcf21d 100644 --- a/modules/juce_audio_utils/gui/juce_AudioDeviceSelectorComponent.cpp +++ b/modules/juce_audio_utils/gui/juce_AudioDeviceSelectorComponent.cpp @@ -985,7 +985,7 @@ AudioDeviceSelectorComponent::AudioDeviceSelectorComponent (AudioDeviceManager& deviceTypeDropDown->addItem (types.getUnchecked(i)->getTypeName(), i + 1); addAndMakeVisible (deviceTypeDropDown); - deviceTypeDropDown->addListener (this); + deviceTypeDropDown->onChange = [this]() { updateDeviceType(); }; deviceTypeDropDownLabel = new Label ({}, TRANS("Audio device type:")); deviceTypeDropDownLabel->setJustificationType (Justification::centredRight); @@ -1019,7 +1019,7 @@ AudioDeviceSelectorComponent::AudioDeviceSelectorComponent (AudioDeviceManager& if (showMidiOutputSelector) { addAndMakeVisible (midiOutputSelector = new ComboBox()); - midiOutputSelector->addListener (this); + midiOutputSelector->onChange = [this]() { updateMidiOutput(); }; midiOutputLabel = new Label ("lm", TRANS("MIDI Output:")); midiOutputLabel->attachToComponent (midiOutputSelector, true); @@ -1095,26 +1095,24 @@ void AudioDeviceSelectorComponent::timerCallback() updateAllControls(); } -void AudioDeviceSelectorComponent::comboBoxChanged (ComboBox* comboBoxThatHasChanged) +void AudioDeviceSelectorComponent::updateDeviceType() { - if (comboBoxThatHasChanged == deviceTypeDropDown) + if (auto* type = deviceManager.getAvailableDeviceTypes() [deviceTypeDropDown->getSelectedId() - 1]) { - if (auto* type = deviceManager.getAvailableDeviceTypes() [deviceTypeDropDown->getSelectedId() - 1]) - { - audioDeviceSettingsComp.reset(); - deviceManager.setCurrentAudioDeviceType (type->getTypeName(), true); - updateAllControls(); // needed in case the type hasn't actually changed - } + audioDeviceSettingsComp.reset(); + deviceManager.setCurrentAudioDeviceType (type->getTypeName(), true); + updateAllControls(); // needed in case the type hasn't actually changed } - else if (comboBoxThatHasChanged == midiOutputSelector) - { - auto midiDeviceName = midiOutputSelector->getText(); +} + +void AudioDeviceSelectorComponent::updateMidiOutput() +{ + auto midiDeviceName = midiOutputSelector->getText(); - if (midiDeviceName == getNoDeviceString()) - midiDeviceName = {}; + if (midiDeviceName == getNoDeviceString()) + midiDeviceName = {}; - deviceManager.setDefaultMidiOutput (midiDeviceName); - } + deviceManager.setDefaultMidiOutput (midiDeviceName); } void AudioDeviceSelectorComponent::changeListenerCallback (ChangeBroadcaster*) diff --git a/modules/juce_audio_utils/gui/juce_AudioDeviceSelectorComponent.h b/modules/juce_audio_utils/gui/juce_AudioDeviceSelectorComponent.h index 9498e5d153..2172605da0 100644 --- a/modules/juce_audio_utils/gui/juce_AudioDeviceSelectorComponent.h +++ b/modules/juce_audio_utils/gui/juce_AudioDeviceSelectorComponent.h @@ -38,7 +38,6 @@ namespace juce */ class JUCE_API AudioDeviceSelectorComponent : public Component, private ChangeListener, - private ComboBox::Listener, private Timer { public: @@ -111,7 +110,8 @@ private: ScopedPointer bluetoothButton; void handleBluetoothButton(); - void comboBoxChanged (ComboBox*) override; + void updateDeviceType(); + void updateMidiOutput(); void changeListenerCallback (ChangeBroadcaster*) override; void updateAllControls(); diff --git a/modules/juce_events/broadcasters/juce_EventHandler.h b/modules/juce_events/broadcasters/juce_EventHandler.h deleted file mode 100644 index 9fe20e5fa0..0000000000 --- a/modules/juce_events/broadcasters/juce_EventHandler.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - ============================================================================== - - This file is part of the JUCE library. - Copyright (c) 2017 - ROLI Ltd. - - JUCE is an open source library subject to commercial or open-source - licensing. - - The code included in this file is provided under the terms of the ISC license - http://www.isc.org/downloads/software-support-policy/isc-license. Permission - To use, copy, modify, and/or distribute this software for any purpose with or - without fee is hereby granted provided that the above copyright notice and - this permission notice appear in all copies. - - JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER - EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE - DISCLAIMED. - - ============================================================================== -*/ - -namespace juce -{ - -//============================================================================== -/** - Helper class for dispatching callbacks to a lambda function. - - This class probably isn't something many users will use in their own code, but - some juce classes use it as a helper to allow lambdas to be assigned to callback - hooks - e.g. see its use in Button::onClick -*/ -template -struct EventHandler -{ - EventHandler() {} - ~EventHandler() {} - - /** Assigns a lambda to this callback. - Note that this will replace any existing function that was previously assigned. - */ - void operator= (const std::function& callbackToAttach) - { - callback = callbackToAttach; - } - - /** Assigns a lambda to this callback. - Note that this will replace any existing function that was previously assigned. - */ - void operator= (std::function&& callbackToAttach) - { - callback = static_cast&&> (callbackToAttach); - } - - /** Removes any existing function that was previously assigned to the callback. */ - void reset() noexcept - { - callback = {}; - } - - /** @internal */ - void invoke() - { - if (callback != nullptr) - callback(); - } - -private: - std::function callback; - - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EventHandler) -}; - -} // namespace juce diff --git a/modules/juce_events/juce_events.h b/modules/juce_events/juce_events.h index d24689313d..dfa512a5c9 100644 --- a/modules/juce_events/juce_events.h +++ b/modules/juce_events/juce_events.h @@ -76,7 +76,6 @@ #include "broadcasters/juce_AsyncUpdater.h" #include "broadcasters/juce_ChangeListener.h" #include "broadcasters/juce_ChangeBroadcaster.h" -#include "broadcasters/juce_EventHandler.h" #include "timers/juce_Timer.h" #include "timers/juce_MultiTimer.h" #include "interprocess/juce_InterprocessConnection.h" diff --git a/modules/juce_gui_basics/buttons/juce_Button.cpp b/modules/juce_gui_basics/buttons/juce_Button.cpp index 88a7d93b67..b5b2399243 100644 --- a/modules/juce_gui_basics/buttons/juce_Button.cpp +++ b/modules/juce_gui_basics/buttons/juce_Button.cpp @@ -399,7 +399,8 @@ void Button::sendClickMessage (const ModifierKeys& modifiers) if (checker.shouldBailOut()) return; - onClick.invoke(); + if (onClick != nullptr) + onClick(); } void Button::sendStateMessage() @@ -416,7 +417,8 @@ void Button::sendStateMessage() if (checker.shouldBailOut()) return; - onStateChange.invoke(); + if (onStateChange != nullptr) + onStateChange(); } //============================================================================== diff --git a/modules/juce_gui_basics/buttons/juce_Button.h b/modules/juce_gui_basics/buttons/juce_Button.h index 1f8f106118..1c42af4402 100644 --- a/modules/juce_gui_basics/buttons/juce_Button.h +++ b/modules/juce_gui_basics/buttons/juce_Button.h @@ -186,10 +186,10 @@ public: //============================================================================== /** You can assign a lambda to this callback object to have it called when the button is clicked. */ - EventHandler