From e690350df3ddb76290ccbdbae28f1483e4d7722d Mon Sep 17 00:00:00 2001 From: Noah Dayan Date: Thu, 18 Jan 2018 15:23:23 +0000 Subject: [PATCH] Use lambda callbacks instead of listeners with Slider, Button, Label, ComboBox and TextEditor classes --- .../jucer_GlobalSearchPathsWindowComponent.h | 10 +---- .../jucer_SVGPathDataWindowComponent.h | 16 ++----- .../Windows/jucer_UTF8WindowComponent.h | 18 ++------ .../CodeEditor/jucer_LiveBuildCodeEditor.h | 11 ++--- .../CodeEditor/jucer_SourceCodeEditor.cpp | 26 ++++-------- .../Project/UI/Sidebar/jucer_TabComponents.h | 19 +++------ .../Source/Project/UI/jucer_HeaderComponent.h | 9 +--- .../jucer_DependencyPathPropertyComponent.cpp | 24 ++++------- .../jucer_DependencyPathPropertyComponent.h | 15 ++----- .../jucer_FilePathPropertyComponent.h | 9 ++-- .../Utility/UI/jucer_JucerTreeViewBase.cpp | 12 ++---- .../Wizards/jucer_NewProjectWizardComponent.h | 32 ++++---------- .../jucer_TemplateThumbnailsComponent.h | 13 ++---- .../filebrowser/juce_FileBrowserComponent.cpp | 26 ++++-------- .../filebrowser/juce_FileBrowserComponent.h | 10 +---- .../juce_ChoicePropertyComponent.cpp | 4 +- .../properties/juce_ChoicePropertyComponent.h | 5 +-- .../juce_SliderPropertyComponent.cpp | 12 +++--- .../properties/juce_SliderPropertyComponent.h | 5 +-- .../juce_gui_basics/widgets/juce_ComboBox.cpp | 8 +--- .../juce_gui_basics/widgets/juce_ComboBox.h | 3 -- .../juce_gui_basics/widgets/juce_Slider.cpp | 7 ++-- .../windows/juce_AlertWindow.cpp | 4 +- .../windows/juce_AlertWindow.h | 6 +-- .../misc/juce_ColourSelector.cpp | 4 +- .../juce_gui_extra/misc/juce_ColourSelector.h | 5 +-- .../misc/juce_LiveConstantEditor.cpp | 42 +++++++------------ .../misc/juce_LiveConstantEditor.h | 6 +-- .../misc/juce_PreferencesPanel.cpp | 4 +- .../misc/juce_PreferencesPanel.h | 7 ++-- 30 files changed, 114 insertions(+), 258 deletions(-) diff --git a/extras/Projucer/Source/Application/Windows/jucer_GlobalSearchPathsWindowComponent.h b/extras/Projucer/Source/Application/Windows/jucer_GlobalSearchPathsWindowComponent.h index 2ae37a5666..d87719702e 100644 --- a/extras/Projucer/Source/Application/Windows/jucer_GlobalSearchPathsWindowComponent.h +++ b/extras/Projucer/Source/Application/Windows/jucer_GlobalSearchPathsWindowComponent.h @@ -28,8 +28,7 @@ //============================================================================== -class GlobalSearchPathsWindowComponent : public Component, - private ComboBox::Listener +class GlobalSearchPathsWindowComponent : public Component { public: GlobalSearchPathsWindowComponent() @@ -60,7 +59,7 @@ public: osSelector.addItem ("Windows", 2); osSelector.addItem ("Linux", 3); - osSelector.addListener (this); + osSelector.onChange = [this] { updateFilePathPropertyComponents(); }; auto os = TargetOS::getThisOS(); @@ -120,11 +119,6 @@ private: ComboBox osSelector; InfoButton info; - void comboBoxChanged (ComboBox*) override - { - updateFilePathPropertyComponents(); - } - TargetOS::OS getSelectedOS() const { auto selectedOS = TargetOS::unknown; diff --git a/extras/Projucer/Source/Application/Windows/jucer_SVGPathDataWindowComponent.h b/extras/Projucer/Source/Application/Windows/jucer_SVGPathDataWindowComponent.h index e8e914d43f..7c48ecb769 100644 --- a/extras/Projucer/Source/Application/Windows/jucer_SVGPathDataWindowComponent.h +++ b/extras/Projucer/Source/Application/Windows/jucer_SVGPathDataWindowComponent.h @@ -29,8 +29,7 @@ //============================================================================== class SVGPathDataComponent : public Component, - public FileDragAndDropTarget, - private TextEditor::Listener + public FileDragAndDropTarget { public: @@ -43,7 +42,8 @@ public: userText.setMultiLine (true, true); userText.setReturnKeyStartsNewLine (true); addAndMakeVisible (userText); - userText.addListener (this); + userText.onTextChange = [this] { update(); }; + userText.onEscapeKey = [this] { getTopLevelComponent()->exitModalState (0); }; resultText.setFont (getAppSettings().appearance.getCodeFont().withHeight (13.0f)); resultText.setMultiLine (true, true); @@ -65,16 +65,6 @@ public: fillPathButton.setToggleState (true, NotificationType::dontSendNotification); } - void textEditorTextChanged (TextEditor&) override - { - update(); - } - - void textEditorEscapeKeyPressed (TextEditor&) override - { - getTopLevelComponent()->exitModalState (0); - } - void update() { getLastText() = userText.getText(); diff --git a/extras/Projucer/Source/Application/Windows/jucer_UTF8WindowComponent.h b/extras/Projucer/Source/Application/Windows/jucer_UTF8WindowComponent.h index 556bd49fc3..b208b1a4a6 100644 --- a/extras/Projucer/Source/Application/Windows/jucer_UTF8WindowComponent.h +++ b/extras/Projucer/Source/Application/Windows/jucer_UTF8WindowComponent.h @@ -28,8 +28,7 @@ //============================================================================== -class UTF8Component : public Component, - private TextEditor::Listener +class UTF8Component : public Component { public: UTF8Component() @@ -43,7 +42,8 @@ public: userText.setMultiLine (true, true); userText.setReturnKeyStartsNewLine (true); addAndMakeVisible (userText); - userText.addListener (this); + userText.onTextChange = [this] { update(); }; + userText.onEscapeKey = [this] { getTopLevelComponent()->exitModalState (0); }; resultText.setFont (getAppSettings().appearance.getCodeFont().withHeight (13.0f)); resultText.setMultiLine (true, true); @@ -54,16 +54,6 @@ public: userText.setText (getLastText()); } - void textEditorTextChanged (TextEditor&) override - { - update(); - } - - void textEditorEscapeKeyPressed (TextEditor&) override - { - getTopLevelComponent()->exitModalState (0); - } - void update() { getLastText() = userText.getText(); @@ -72,7 +62,7 @@ public: void resized() override { - Rectangle r (getLocalBounds().reduced (8)); + auto r = getLocalBounds().reduced (8); desc.setBounds (r.removeFromTop (44)); r.removeFromTop (8); userText.setBounds (r.removeFromTop (r.getHeight() / 2)); diff --git a/extras/Projucer/Source/CodeEditor/jucer_LiveBuildCodeEditor.h b/extras/Projucer/Source/CodeEditor/jucer_LiveBuildCodeEditor.h index 9ed2c8e843..8f37ed8a9c 100644 --- a/extras/Projucer/Source/CodeEditor/jucer_LiveBuildCodeEditor.h +++ b/extras/Projucer/Source/CodeEditor/jucer_LiveBuildCodeEditor.h @@ -446,7 +446,6 @@ private: //============================================================================== class ControlsComponent : public Component, - private Slider::Listener, private ChangeListener { public: @@ -464,7 +463,8 @@ private: setMouseClickGrabsKeyboardFocus (false); addAndMakeVisible (&slider); updateRange(); - slider.addListener (this); + slider.onValueChange = [this] { updateSliderValue(); }; + slider.onDragEnd = [this] { updateRange(); }; if (showColourSelector) { @@ -511,10 +511,10 @@ private: g.fillRoundedRectangle (getLocalBounds().toFloat(), 8.0f); } - void sliderValueChanged (Slider* s) override + void updateSliderValue() { const String oldText (document.getTextBetween (start, end)); - const String newText (CppParserHelpers::getReplacementStringInSameFormat (oldText, s->getValue())); + const String newText (CppParserHelpers::getReplacementStringInSameFormat (oldText, slider.getValue())); if (oldText != newText) document.replaceSection (start.getPosition(), end.getPosition(), newText); @@ -525,9 +525,6 @@ private: updateColourSelector(); } - void sliderDragStarted (Slider*) override {} - void sliderDragEnded (Slider*) override { updateRange(); } - void changeListenerCallback (ChangeBroadcaster*) override { setNewColour (selector.getCurrentColour()); diff --git a/extras/Projucer/Source/CodeEditor/jucer_SourceCodeEditor.cpp b/extras/Projucer/Source/CodeEditor/jucer_SourceCodeEditor.cpp index 10bd1d93de..66e0e87bd1 100644 --- a/extras/Projucer/Source/CodeEditor/jucer_SourceCodeEditor.cpp +++ b/extras/Projucer/Source/CodeEditor/jucer_SourceCodeEditor.cpp @@ -344,8 +344,7 @@ void GenericCodeEditorComponent::removeListener (GenericCodeEditorComponent::Lis } //============================================================================== -class GenericCodeEditorComponent::FindPanel : public Component, - private TextEditor::Listener +class GenericCodeEditorComponent::FindPanel : public Component { public: FindPanel() @@ -376,7 +375,13 @@ public: findNext.setWantsKeyboardFocus (false); editor.setText (getSearchString()); - editor.addListener (this); + editor.onTextChange = [this] { changeSearchString(); }; + editor.onReturnKey = [this] { ProjucerApplication::getCommandManager().invokeDirectly (CommandIDs::findNext, true); }; + editor.onEscapeKey = [this] + { + if (GenericCodeEditorComponent* ed = getOwner()) + ed->hideFindPanel(); + }; } void setCommandManager (ApplicationCommandManager* cm) @@ -406,7 +411,7 @@ public: findPrev.setBounds (getWidth() - 70, y, 30, 22); } - void textEditorTextChanged (TextEditor&) override + void changeSearchString() { setSearchString (editor.getText()); @@ -414,19 +419,6 @@ public: ed->findNext (true, false); } - void textEditorFocusLost (TextEditor&) override {} - - void textEditorReturnKeyPressed (TextEditor&) override - { - ProjucerApplication::getCommandManager().invokeDirectly (CommandIDs::findNext, true); - } - - void textEditorEscapeKeyPressed (TextEditor&) override - { - if (GenericCodeEditorComponent* ed = getOwner()) - ed->hideFindPanel(); - } - GenericCodeEditorComponent* getOwner() const { return findParentComponentOfClass (); diff --git a/extras/Projucer/Source/Project/UI/Sidebar/jucer_TabComponents.h b/extras/Projucer/Source/Project/UI/Sidebar/jucer_TabComponents.h index 1f46d67fd8..c8562efee1 100644 --- a/extras/Projucer/Source/Project/UI/Sidebar/jucer_TabComponents.h +++ b/extras/Projucer/Source/Project/UI/Sidebar/jucer_TabComponents.h @@ -91,7 +91,6 @@ private: //============================================================================== class FindPanel : public Component, - private TextEditor::Listener, private Timer, private FocusChangeListener { @@ -100,7 +99,12 @@ public: : callback (cb) { addAndMakeVisible (editor); - editor.addListener (this); + editor.onTextChange = [this] { startTimer (250); }; + editor.onFocusLost = [this] + { + isFocused = false; + repaint(); + }; Desktop::getInstance().addFocusChangeListener (this); @@ -141,17 +145,6 @@ private: editor.setTextToShowWhenEmpty ("Filter...", findColour (widgetTextColourId).withAlpha (0.3f)); } - void textEditorTextChanged (TextEditor&) override - { - startTimer (250); - } - - void textEditorFocusLost (TextEditor&) override - { - isFocused = false; - repaint(); - } - void globalFocusChanged (Component* focusedComponent) override { if (focusedComponent == &editor) diff --git a/extras/Projucer/Source/Project/UI/jucer_HeaderComponent.h b/extras/Projucer/Source/Project/UI/jucer_HeaderComponent.h index c32fb4770a..50a2a62c80 100644 --- a/extras/Projucer/Source/Project/UI/jucer_HeaderComponent.h +++ b/extras/Projucer/Source/Project/UI/jucer_HeaderComponent.h @@ -31,7 +31,6 @@ //============================================================================== class HeaderComponent : public Component, - private ComboBox::Listener, private ValueTree::Listener, private ChangeListener { @@ -42,7 +41,7 @@ public: addAndMakeVisible (configLabel); addAndMakeVisible (exporterBox); - exporterBox.addListener (this); + exporterBox.onChange = [this] { updateExporterButton(); }; addAndMakeVisible (juceIcon = new ImageComponent ("icon")); juceIcon->setImage (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize), @@ -217,12 +216,6 @@ private: int tabsWidth = 200; //========================================================================== - void comboBoxChanged (ComboBox* c) override - { - if (c == &exporterBox) - updateExporterButton(); - } - void changeListenerCallback (ChangeBroadcaster* source) override { if (source == project) diff --git a/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_DependencyPathPropertyComponent.cpp b/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_DependencyPathPropertyComponent.cpp index 781195c63c..a72d53f362 100644 --- a/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_DependencyPathPropertyComponent.cpp +++ b/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_DependencyPathPropertyComponent.cpp @@ -77,8 +77,8 @@ try : TextPropertyComponent (propertyName, 1024, false), getValue().addListener (this); - if (Label* label = dynamic_cast (getChildComponent (0))) - label->addListener (this); + if (auto* label = dynamic_cast (getChildComponent (0))) + label->onEditorShow = [this, label] { setEditorText (label); }; else jassertfalse; @@ -116,18 +116,11 @@ Colour DependencyPathPropertyComponent::getTextColourToDisplay() const : Colours::red; } -void DependencyPathPropertyComponent::labelTextChanged (Label*) -{ -} - -void DependencyPathPropertyComponent::editorShown (Label* /*label*/, TextEditor& editor) +void DependencyPathPropertyComponent::setEditorText (Label* label) { if (! pathValueSource.isUsingProjectSettings()) - editor.setText (String(), dontSendNotification); -} - -void DependencyPathPropertyComponent::editorHidden (Label*, TextEditor&) -{ + if (auto editor = label->getCurrentTextEditor()) + editor->setText (String(), dontSendNotification); } void DependencyPathPropertyComponent::lookAndFeelChanged() @@ -159,7 +152,7 @@ try : TextPropertyComponent (propertyDescription, 1024, false), getValue().addListener (this); if (auto* label = dynamic_cast (getChildComponent (0))) - label->addListener (this); + label->onEditorShow = [this, label] { setEditorText (label); }; else jassertfalse; @@ -238,10 +231,11 @@ void DependencyFilePathPropertyComponent::valueChanged (Value& value) textWasEdited(); } -void DependencyFilePathPropertyComponent::editorShown (Label*, TextEditor& editor) +void DependencyFilePathPropertyComponent::setEditorText (Label* label) { if (! pathValueSource.isUsingProjectSettings()) - editor.setText (String(), dontSendNotification); + if (auto editor = label->getCurrentTextEditor()) + editor->setText (String(), dontSendNotification); } void DependencyFilePathPropertyComponent::browse() diff --git a/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_DependencyPathPropertyComponent.h b/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_DependencyPathPropertyComponent.h index 5bdf6767b2..9bcc340306 100644 --- a/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_DependencyPathPropertyComponent.h +++ b/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_DependencyPathPropertyComponent.h @@ -148,8 +148,7 @@ private: //============================================================================== class DependencyPathPropertyComponent : public TextPropertyComponent, - private Value::Listener, - private Label::Listener + private Value::Listener { public: DependencyPathPropertyComponent (const File& pathRelativeToUse, @@ -178,10 +177,7 @@ private: /** a reference to the value source that this value refers to. */ DependencyPathValueSource& pathValueSource; - // Label::Listener overrides: - void labelTextChanged (Label* labelThatHasChanged) override; - void editorShown (Label*, TextEditor&) override; - void editorHidden (Label*, TextEditor&) override; + void setEditorText (Label* label); void lookAndFeelChanged() override; @@ -191,8 +187,7 @@ private: //============================================================================== class DependencyFilePathPropertyComponent : public TextPropertyComponent, public FileDragAndDropTarget, - private Value::Listener, - private Label::Listener + private Value::Listener { public: DependencyFilePathPropertyComponent (Value& value, @@ -218,9 +213,7 @@ private: void valueChanged (Value&) override; - void labelTextChanged (Label*) override {} - void editorHidden (Label*, TextEditor&) override {} - void editorShown (Label*, TextEditor&) override; + void setEditorText (Label* label); void lookAndFeelChanged() override { diff --git a/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_FilePathPropertyComponent.h b/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_FilePathPropertyComponent.h index f254df264a..bcf5a35580 100644 --- a/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_FilePathPropertyComponent.h +++ b/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_FilePathPropertyComponent.h @@ -53,8 +53,7 @@ public: private: struct InnerComponent : public Component, - public FileDragAndDropTarget, - private TextEditor::Listener + public FileDragAndDropTarget { InnerComponent (Value v, bool isDir, const String& wc, const File& rt, const bool multiplePaths) : value (v), @@ -67,7 +66,8 @@ private: { addAndMakeVisible (textbox); textbox.getTextValue().referTo (value); - textbox.addListener (this); + textbox.onReturnKey = [this] { updateEditorColour (textbox); }; + textbox.onFocusLost = [this] { updateEditorColour (textbox); }; addAndMakeVisible (button); button.onClick = [this] { browse(); }; @@ -130,9 +130,6 @@ private: } } - void textEditorReturnKeyPressed (TextEditor& editor) override { updateEditorColour (editor); } - void textEditorFocusLost (TextEditor& editor) override { updateEditorColour (editor); } - void updateEditorColour (TextEditor& editor) { if (supportsMultiplePaths) diff --git a/extras/Projucer/Source/Utility/UI/jucer_JucerTreeViewBase.cpp b/extras/Projucer/Source/Utility/UI/jucer_JucerTreeViewBase.cpp index e59f0b9ec1..8e7061ad39 100644 --- a/extras/Projucer/Source/Utility/UI/jucer_JucerTreeViewBase.cpp +++ b/extras/Projucer/Source/Utility/UI/jucer_JucerTreeViewBase.cpp @@ -132,8 +132,7 @@ Component* JucerTreeViewBase::createItemComponent() } //============================================================================== -class RenameTreeItemCallback : public ModalComponentManager::Callback, - public TextEditor::Listener +class RenameTreeItemCallback : public ModalComponentManager::Callback { public: RenameTreeItemCallback (JucerTreeViewBase& ti, Component& parent, const Rectangle& bounds) @@ -143,7 +142,9 @@ public: ed.setPopupMenuEnabled (false); ed.setSelectAllWhenFocused (true); ed.setFont (item.getFont()); - ed.addListener (this); + ed.onReturnKey = [this] { ed.exitModalState (1); }; + ed.onEscapeKey = [this] { ed.exitModalState (0); }; + ed.onFocusLost = [this] { ed.exitModalState (0); }; ed.setText (item.getRenamingName()); ed.setBounds (bounds); @@ -157,11 +158,6 @@ public: item.setName (ed.getText()); } - void textEditorTextChanged (TextEditor&) override {} - void textEditorReturnKeyPressed (TextEditor& editor) override { editor.exitModalState (1); } - void textEditorEscapeKeyPressed (TextEditor& editor) override { editor.exitModalState (0); } - void textEditorFocusLost (TextEditor& editor) override { editor.exitModalState (0); } - private: struct RenameEditor : public TextEditor { diff --git a/extras/Projucer/Source/Wizards/jucer_NewProjectWizardComponent.h b/extras/Projucer/Source/Wizards/jucer_NewProjectWizardComponent.h index cf5273ad7e..98ef3295a7 100644 --- a/extras/Projucer/Source/Wizards/jucer_NewProjectWizardComponent.h +++ b/extras/Projucer/Source/Wizards/jucer_NewProjectWizardComponent.h @@ -28,8 +28,7 @@ //============================================================================== -class ModulesFolderPathBox : public Component, - private ComboBox::Listener +class ModulesFolderPathBox : public Component { public: ModulesFolderPathBox (File initialFileOrDirectory) @@ -45,7 +44,8 @@ public: addAndMakeVisible (currentPathBox); currentPathBox.setEditableText (true); - currentPathBox.addListener (this); + currentPathBox.onChange = [this] { setModulesFolder (File::getCurrentWorkingDirectory() + .getChildFile (currentPathBox.getText())); }; addAndMakeVisible (openFolderButton); openFolderButton.setTooltip (TRANS ("Select JUCE modules folder")); @@ -121,11 +121,6 @@ public: } } - void comboBoxChanged (ComboBox*) override - { - setModulesFolder (File::getCurrentWorkingDirectory().getChildFile (currentPathBox.getText())); - } - File modulesFolder; bool isUsingGlobalPaths; @@ -285,8 +280,6 @@ private: a list box of platform targets to generate. */ class WizardComp : public Component, - private ComboBox::Listener, - private TextEditor::Listener, private FileBrowserListener { public: @@ -300,13 +293,17 @@ public: addChildAndSetID (&projectName, "projectName"); projectName.setText ("NewProject"); nameLabel.attachToComponent (&projectName, true); - projectName.addListener (this); + projectName.onTextChange = [this] + { + updateCreateButton(); + fileBrowser.setFileName (File::createLegalFileName (projectName.getText())); + }; addChildAndSetID (&projectType, "projectType"); projectType.addItemList (getWizardNames(), 1); projectType.setSelectedId (1, dontSendNotification); typeLabel.attachToComponent (&projectType, true); - projectType.addListener (this); + projectType.onChange = [this] { updateFileCreationTypes(); }; addChildAndSetID (&fileOutline, "fileOutline"); fileOutline.setColour (GroupComponent::outlineColourId, Colours::black.withAlpha (0.2f)); @@ -447,17 +444,6 @@ public: filesToCreate.setSelectedId (1, dontSendNotification); } - void comboBoxChanged (ComboBox*) override - { - updateFileCreationTypes(); - } - - void textEditorTextChanged (TextEditor&) override - { - updateCreateButton(); - fileBrowser.setFileName (File::createLegalFileName (projectName.getText())); - } - void selectionChanged() override {} void fileClicked (const File&, const MouseEvent&) override {} diff --git a/extras/Projucer/Source/Wizards/jucer_TemplateThumbnailsComponent.h b/extras/Projucer/Source/Wizards/jucer_TemplateThumbnailsComponent.h index 6dedc835f6..5e154bef84 100644 --- a/extras/Projucer/Source/Wizards/jucer_TemplateThumbnailsComponent.h +++ b/extras/Projucer/Source/Wizards/jucer_TemplateThumbnailsComponent.h @@ -134,8 +134,7 @@ private: Project Template Component for front page. Features multiple icon buttons to select the type of project template */ -class TemplateTileBrowser : public Component, - private Button::Listener +class TemplateTileBrowser : public Component { public: TemplateTileBrowser (WizardComp* projectWizard) @@ -152,7 +151,8 @@ public: optionButtons.add (b); addAndMakeVisible (b); b->setDescription (wizard->getDescription()); - b->addListener (this); + b->onClick = [this, b] { showWizardButton (b); }; + b->onStateChange = [this] { repaint(); }; } // Handle Open Project button functionality @@ -262,16 +262,11 @@ private: NewProjectWizardClasses::WizardComp* newProjectWizard; ScopedPointer blankProjectButton, openProjectButton, exampleProjectButton; - void buttonClicked (Button* b) override + void showWizardButton (Button* b) { if (dynamic_cast (b) != nullptr) showWizard (b->getButtonText()); } - void buttonStateChanged (Button*) override - { - repaint(); - } - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TemplateTileBrowser) }; diff --git a/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.cpp b/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.cpp index 91dfe853b7..ca59e419a5 100644 --- a/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.cpp +++ b/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.cpp @@ -99,7 +99,14 @@ FileBrowserComponent::FileBrowserComponent (int flags_, filenameBox.setMultiLine (false); filenameBox.setSelectAllWhenFocused (true); filenameBox.setText (filename, false); - filenameBox.addListener (this); + filenameBox.onTextChange = [this] { sendListenerChangeMessage(); }; + filenameBox.onReturnKey = [this] { changeFilename(); }; + filenameBox.onFocusLost = [this] + { + if (! isSaveMode()) + selectionChanged(); + }; + filenameBox.setReadOnly ((flags & (filenameBoxIsReadOnly | canSelectMultipleItems)) != 0); addAndMakeVisible (fileLabel); @@ -439,12 +446,7 @@ bool FileBrowserComponent::keyPressed (const KeyPress& key) } //============================================================================== -void FileBrowserComponent::textEditorTextChanged (TextEditor&) -{ - sendListenerChangeMessage(); -} - -void FileBrowserComponent::textEditorReturnKeyPressed (TextEditor&) +void FileBrowserComponent::changeFilename() { if (filenameBox.getText().containsChar (File::getSeparatorChar())) { @@ -472,16 +474,6 @@ void FileBrowserComponent::textEditorReturnKeyPressed (TextEditor&) } } -void FileBrowserComponent::textEditorEscapeKeyPressed (TextEditor&) -{ -} - -void FileBrowserComponent::textEditorFocusLost (TextEditor&) -{ - if (! isSaveMode()) - selectionChanged(); -} - //============================================================================== void FileBrowserComponent::updateSelectedPath() { diff --git a/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.h b/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.h index 83f64c574b..5c440eaca0 100644 --- a/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.h +++ b/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.h @@ -39,7 +39,6 @@ namespace juce */ class JUCE_API FileBrowserComponent : public Component, private FileBrowserListener, - private TextEditor::Listener, private FileFilter, private Timer { @@ -235,14 +234,6 @@ public: /** @internal */ void lookAndFeelChanged() override; /** @internal */ - void textEditorTextChanged (TextEditor&) override; - /** @internal */ - void textEditorReturnKeyPressed (TextEditor&) override; - /** @internal */ - void textEditorEscapeKeyPressed (TextEditor&) override; - /** @internal */ - void textEditorFocusLost (TextEditor&) override; - /** @internal */ bool keyPressed (const KeyPress&) override; /** @internal */ void selectionChanged() override; @@ -295,6 +286,7 @@ private: void sendListenerChangeMessage(); bool isFileOrDirSuitable (const File&) const; void updateSelectedPath(); + void changeFilename(); JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileBrowserComponent) }; diff --git a/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.cpp b/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.cpp index 18e15bb8ef..22fa24c94f 100644 --- a/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.cpp +++ b/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.cpp @@ -239,14 +239,14 @@ void ChoicePropertyComponent::refresh() if (! comboBox.isVisible()) { createComboBox(); - comboBox.addListener (this); + comboBox.onChange = [this] { changeIndex(); }; } comboBox.setSelectedId (getIndex() + 1, dontSendNotification); } } -void ChoicePropertyComponent::comboBoxChanged (ComboBox*) +void ChoicePropertyComponent::changeIndex() { if (isCustomClass) { diff --git a/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.h b/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.h index 02611ba912..40379aafe8 100644 --- a/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.h +++ b/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.h @@ -46,8 +46,7 @@ namespace juce @see PropertyComponent, PropertyPanel */ -class JUCE_API ChoicePropertyComponent : public PropertyComponent, - private ComboBox::Listener +class JUCE_API ChoicePropertyComponent : public PropertyComponent { protected: /** Creates the component. @@ -146,7 +145,7 @@ private: void createComboBox(); void createComboBoxWithDefault (const String&); - void comboBoxChanged (ComboBox*) override; + void changeIndex(); JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChoicePropertyComponent) }; diff --git a/modules/juce_gui_basics/properties/juce_SliderPropertyComponent.cpp b/modules/juce_gui_basics/properties/juce_SliderPropertyComponent.cpp index 439ef36cf5..a623cd56a2 100644 --- a/modules/juce_gui_basics/properties/juce_SliderPropertyComponent.cpp +++ b/modules/juce_gui_basics/properties/juce_SliderPropertyComponent.cpp @@ -41,7 +41,11 @@ SliderPropertyComponent::SliderPropertyComponent (const String& name, slider.setSkewFactor (skewFactor, symmetricSkew); slider.setSliderStyle (Slider::LinearBar); - slider.addListener (this); + slider.onValueChange = [this] + { + if (getValue() != slider.getValue()) + setValue (slider.getValue()); + }; } SliderPropertyComponent::SliderPropertyComponent (const Value& valueToControl, @@ -80,10 +84,4 @@ void SliderPropertyComponent::refresh() slider.setValue (getValue(), dontSendNotification); } -void SliderPropertyComponent::sliderValueChanged (Slider*) -{ - if (getValue() != slider.getValue()) - setValue (slider.getValue()); -} - } // namespace juce diff --git a/modules/juce_gui_basics/properties/juce_SliderPropertyComponent.h b/modules/juce_gui_basics/properties/juce_SliderPropertyComponent.h index 944d6c1b9c..408c8c43e6 100644 --- a/modules/juce_gui_basics/properties/juce_SliderPropertyComponent.h +++ b/modules/juce_gui_basics/properties/juce_SliderPropertyComponent.h @@ -33,8 +33,7 @@ namespace juce @see PropertyComponent, Slider */ -class JUCE_API SliderPropertyComponent : public PropertyComponent, - private Slider::Listener +class JUCE_API SliderPropertyComponent : public PropertyComponent { protected: //============================================================================== @@ -92,8 +91,6 @@ public: //============================================================================== /** @internal */ void refresh(); - /** @internal */ - void sliderValueChanged (Slider*); protected: /** The slider component being used in this component. diff --git a/modules/juce_gui_basics/widgets/juce_ComboBox.cpp b/modules/juce_gui_basics/widgets/juce_ComboBox.cpp index f88bcd48df..b83023d871 100644 --- a/modules/juce_gui_basics/widgets/juce_ComboBox.cpp +++ b/modules/juce_gui_basics/widgets/juce_ComboBox.cpp @@ -431,7 +431,7 @@ void ComboBox::lookAndFeelChanged() setWantsKeyboardFocus (labelEditableState == labelIsNotEditable); } - label->addListener (this); + label->onTextChange = [this] { triggerAsyncUpdate(); }; label->addMouseListener (this, false); label->setColour (Label::backgroundColourId, Colours::transparentBlack); @@ -483,12 +483,6 @@ bool ComboBox::keyStateChanged (const bool isKeyDown) void ComboBox::focusGained (FocusChangeType) { repaint(); } void ComboBox::focusLost (FocusChangeType) { repaint(); } -void ComboBox::labelTextChanged (Label*) -{ - triggerAsyncUpdate(); -} - - //============================================================================== void ComboBox::showPopupIfNotActive() { diff --git a/modules/juce_gui_basics/widgets/juce_ComboBox.h b/modules/juce_gui_basics/widgets/juce_ComboBox.h index 4b2643c82a..a3d35735b8 100644 --- a/modules/juce_gui_basics/widgets/juce_ComboBox.h +++ b/modules/juce_gui_basics/widgets/juce_ComboBox.h @@ -43,7 +43,6 @@ namespace juce */ class JUCE_API ComboBox : public Component, public SettableTooltipClient, - public Label::Listener, public Value::Listener, private AsyncUpdater { @@ -379,8 +378,6 @@ public: //============================================================================== /** @internal */ - void labelTextChanged (Label*) override; - /** @internal */ void enablementChanged() override; /** @internal */ void colourChanged() override; diff --git a/modules/juce_gui_basics/widgets/juce_Slider.cpp b/modules/juce_gui_basics/widgets/juce_Slider.cpp index ca3884a50d..50e0972274 100644 --- a/modules/juce_gui_basics/widgets/juce_Slider.cpp +++ b/modules/juce_gui_basics/widgets/juce_Slider.cpp @@ -29,7 +29,6 @@ namespace juce class Slider::Pimpl : public AsyncUpdater, // this needs to be public otherwise it will cause an // error when JUCE_DLL_BUILD=1 - private Label::Listener, private Value::Listener { public: @@ -398,9 +397,9 @@ public: setMaxValue (valueMax.getValue(), dontSendNotification, true); } - void labelTextChanged (Label* label) override + void textChanged() { - auto newValue = owner.snapValue (owner.getValueFromText (label->getText()), notDragging); + auto newValue = owner.snapValue (owner.getValueFromText (valueBox->getText()), notDragging); if (newValue != static_cast (currentValue.getValue())) { @@ -572,7 +571,7 @@ public: valueBox->setText (previousTextBoxContent, dontSendNotification); valueBox->setTooltip (owner.getTooltip()); updateTextBoxEnablement(); - valueBox->addListener (this); + valueBox->onTextChange = [this] { textChanged(); }; if (style == LinearBar || style == LinearBarVertical) { diff --git a/modules/juce_gui_basics/windows/juce_AlertWindow.cpp b/modules/juce_gui_basics/windows/juce_AlertWindow.cpp index f35ba3c49c..3426cb6625 100644 --- a/modules/juce_gui_basics/windows/juce_AlertWindow.cpp +++ b/modules/juce_gui_basics/windows/juce_AlertWindow.cpp @@ -81,7 +81,7 @@ void AlertWindow::setMessage (const String& message) } //============================================================================== -void AlertWindow::buttonClicked (Button* button) +void AlertWindow::exitAlert (Button* button) { if (auto* parent = button->getParentComponent()) parent->exitModalState (button->getCommandID()); @@ -101,7 +101,7 @@ void AlertWindow::addButton (const String& name, b->setCommandToTrigger (0, returnValue, false); b->addShortcut (shortcutKey1); b->addShortcut (shortcutKey2); - b->addListener (this); + b->onClick = [this, b] { exitAlert (b); }; Array buttonsArray (buttons.begin(), buttons.size()); auto& lf = getLookAndFeel(); diff --git a/modules/juce_gui_basics/windows/juce_AlertWindow.h b/modules/juce_gui_basics/windows/juce_AlertWindow.h index 13dc9154f6..c28ee6bd0e 100644 --- a/modules/juce_gui_basics/windows/juce_AlertWindow.h +++ b/modules/juce_gui_basics/windows/juce_AlertWindow.h @@ -40,8 +40,7 @@ namespace juce @see ThreadWithProgressWindow */ -class JUCE_API AlertWindow : public TopLevelWindow, - private Button::Listener +class JUCE_API AlertWindow : public TopLevelWindow { public: //============================================================================== @@ -455,8 +454,6 @@ protected: /** @internal */ bool keyPressed (const KeyPress&) override; /** @internal */ - void buttonClicked (Button*) override; - /** @internal */ void lookAndFeelChanged() override; /** @internal */ void userTriedToCloseWindow() override; @@ -482,6 +479,7 @@ private: Component* const associatedComponent; bool escapeKeyCancels = true; + void exitAlert (Button* button); void updateLayout (bool onlyIncreaseSize); JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AlertWindow) diff --git a/modules/juce_gui_extra/misc/juce_ColourSelector.cpp b/modules/juce_gui_extra/misc/juce_ColourSelector.cpp index cd7e7950a6..0a1f728bb5 100644 --- a/modules/juce_gui_extra/misc/juce_ColourSelector.cpp +++ b/modules/juce_gui_extra/misc/juce_ColourSelector.cpp @@ -335,7 +335,7 @@ ColourSelector::ColourSelector (int sectionsToShow, int edge, int gapAroundColou sliders[3]->setVisible ((flags & showAlphaChannel) != 0); for (int i = 4; --i >= 0;) - sliders[i]->addListener (this); + sliders[i]->onValueChange = [this] { changeColour(); }; } if ((flags & showColourspace) != 0) @@ -554,7 +554,7 @@ void ColourSelector::resized() } } -void ColourSelector::sliderValueChanged (Slider*) +void ColourSelector::changeColour() { if (sliders[0] != nullptr) setCurrentColour (Colour ((uint8) sliders[0]->getValue(), diff --git a/modules/juce_gui_extra/misc/juce_ColourSelector.h b/modules/juce_gui_extra/misc/juce_ColourSelector.h index c3b6747c6a..a3cd3faaee 100644 --- a/modules/juce_gui_extra/misc/juce_ColourSelector.h +++ b/modules/juce_gui_extra/misc/juce_ColourSelector.h @@ -37,8 +37,7 @@ namespace juce when the colour changes. */ class JUCE_API ColourSelector : public Component, - public ChangeBroadcaster, - protected Slider::Listener + public ChangeBroadcaster { public: //============================================================================== @@ -160,7 +159,7 @@ private: void setSV (float newS, float newV); void updateHSV(); void update (NotificationType); - void sliderValueChanged (Slider*) override; + void changeColour(); void paint (Graphics&) override; void resized() override; diff --git a/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp b/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp index 7a3121039d..0be9335678 100644 --- a/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp +++ b/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp @@ -144,10 +144,10 @@ LivePropertyEditorBase::LivePropertyEditorBase (LiveValueBase& v, CodeDocument& valueEditor.setMultiLine (v.isString()); valueEditor.setReturnKeyStartsNewLine (v.isString()); valueEditor.setText (v.getStringValue (wasHex), dontSendNotification); - valueEditor.addListener (this); + valueEditor.onTextChange = [this] { applyNewValue (valueEditor.getText()); }; sourceEditor.setReadOnly (true); sourceEditor.setFont (sourceEditor.getFont().withHeight (13.0f)); - resetButton.addListener (this); + resetButton.onClick = [this] { applyNewValue (value.getOriginalStringValue (wasHex)); }; } void LivePropertyEditorBase::paint (Graphics& g) @@ -181,16 +181,6 @@ void LivePropertyEditorBase::resized() sourceEditor.setBounds (r); } -void LivePropertyEditorBase::textEditorTextChanged (TextEditor&) -{ - applyNewValue (valueEditor.getText()); -} - -void LivePropertyEditorBase::buttonClicked (Button*) -{ - applyNewValue (value.getOriginalStringValue (wasHex)); -} - void LivePropertyEditorBase::applyNewValue (const String& s) { value.setStringValue (s); @@ -444,8 +434,7 @@ Component* createColourEditor (LivePropertyEditorBase& editor) } //============================================================================== -struct SliderComp : public Component, - private Slider::Listener +struct SliderComp : public Component { SliderComp (LivePropertyEditorBase& e, bool useFloat) : editor (e), isFloat (useFloat) @@ -453,7 +442,12 @@ struct SliderComp : public Component, slider.setTextBoxStyle (Slider::NoTextBox, true, 0, 0); addAndMakeVisible (slider); updateRange(); - slider.addListener (this); + slider.onDragEnd = [this] { updateRange(); }; + slider.onValueChange = [this] + { + editor.applyNewValue (isFloat ? getAsString ((double) slider.getValue(), editor.wasHex) + : getAsString ((int64) slider.getValue(), editor.wasHex)); + }; } virtual void updateRange() @@ -467,16 +461,6 @@ struct SliderComp : public Component, slider.setValue (v, dontSendNotification); } - void sliderValueChanged (Slider*) override - { - editor.applyNewValue (isFloat ? getAsString ((double) slider.getValue(), editor.wasHex) - : getAsString ((int64) slider.getValue(), editor.wasHex)); - - } - - void sliderDragStarted (Slider*) override {} - void sliderDragEnded (Slider*) override { updateRange(); } - void resized() override { slider.setBounds (getLocalBounds().removeFromTop (25)); @@ -490,15 +474,17 @@ struct SliderComp : public Component, //============================================================================== struct BoolSliderComp : public SliderComp { - BoolSliderComp (LivePropertyEditorBase& e) : SliderComp (e, false) {} + BoolSliderComp (LivePropertyEditorBase& e) + : SliderComp (e, false) + { + slider.onValueChange = [this] { editor.applyNewValue (slider.getValue() > 0.5 ? "true" : "false"); }; + } void updateRange() override { slider.setRange (0.0, 1.0, dontSendNotification); slider.setValue (editor.value.getStringValue (false) == "true", dontSendNotification); } - - void sliderValueChanged (Slider*) override { editor.applyNewValue (slider.getValue() > 0.5 ? "true" : "false"); } }; Component* createIntegerSlider (LivePropertyEditorBase& editor) { return new SliderComp (editor, false); } diff --git a/modules/juce_gui_extra/misc/juce_LiveConstantEditor.h b/modules/juce_gui_extra/misc/juce_LiveConstantEditor.h index dacd885b28..712ca84542 100644 --- a/modules/juce_gui_extra/misc/juce_LiveConstantEditor.h +++ b/modules/juce_gui_extra/misc/juce_LiveConstantEditor.h @@ -106,16 +106,12 @@ namespace LiveConstantEditor }; //============================================================================== - struct JUCE_API LivePropertyEditorBase : public Component, - private TextEditor::Listener, - private Button::Listener + struct JUCE_API LivePropertyEditorBase : public Component { LivePropertyEditorBase (LiveValueBase&, CodeDocument&); void paint (Graphics&) override; void resized() override; - void textEditorTextChanged (TextEditor&) override; - void buttonClicked (Button*) override; void applyNewValue (const String&); void selectOriginalValue(); diff --git a/modules/juce_gui_extra/misc/juce_PreferencesPanel.cpp b/modules/juce_gui_extra/misc/juce_PreferencesPanel.cpp index b373f4d756..27f35c7f52 100644 --- a/modules/juce_gui_extra/misc/juce_PreferencesPanel.cpp +++ b/modules/juce_gui_extra/misc/juce_PreferencesPanel.cpp @@ -58,7 +58,7 @@ void PreferencesPanel::addSettingsPage (const String& title, button->setImages (icon, overIcon, downIcon); button->setRadioGroupId (1); - button->addListener (this); + button->onClick = [this] { clickedPage(); }; button->setClickingTogglesState (true); button->setWantsKeyboardFocus (false); addAndMakeVisible (button); @@ -142,7 +142,7 @@ void PreferencesPanel::setCurrentPage (const String& pageName) } } -void PreferencesPanel::buttonClicked (Button*) +void PreferencesPanel::clickedPage() { for (auto* b : buttons) { diff --git a/modules/juce_gui_extra/misc/juce_PreferencesPanel.h b/modules/juce_gui_extra/misc/juce_PreferencesPanel.h index 99de995008..5c74d4a8c1 100644 --- a/modules/juce_gui_extra/misc/juce_PreferencesPanel.h +++ b/modules/juce_gui_extra/misc/juce_PreferencesPanel.h @@ -44,8 +44,7 @@ namespace juce and implement the createComponentForPage() method to create suitable components for each of these pages. */ -class JUCE_API PreferencesPanel : public Component, - private Button::Listener +class JUCE_API PreferencesPanel : public Component { public: //============================================================================== @@ -131,8 +130,6 @@ public: void resized() override; /** @internal */ void paint (Graphics&) override; - /** @internal */ - void buttonClicked (Button*) override; private: //============================================================================== @@ -141,6 +138,8 @@ private: OwnedArray buttons; int buttonSize; + void clickedPage(); + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PreferencesPanel) };