From 62955e7737160cfb37fa3231253be317ce378e49 Mon Sep 17 00:00:00 2001 From: jules Date: Thu, 12 Oct 2017 09:16:34 +0100 Subject: [PATCH] Fixed some typos and did a bit of code cleanup --- .../juce_gui_basics/layout/juce_FlexItem.h | 2 +- .../juce_gui_basics/widgets/juce_ComboBox.cpp | 82 ++++++++----------- .../juce_gui_basics/widgets/juce_ComboBox.h | 8 +- 3 files changed, 39 insertions(+), 53 deletions(-) diff --git a/modules/juce_gui_basics/layout/juce_FlexItem.h b/modules/juce_gui_basics/layout/juce_FlexItem.h index 71c3d20d20..55b415e5c5 100644 --- a/modules/juce_gui_basics/layout/juce_FlexItem.h +++ b/modules/juce_gui_basics/layout/juce_FlexItem.h @@ -91,7 +91,7 @@ public: enum class AlignSelf { autoAlign, flexStart, flexEnd, center, stretch }; /** This is the aligh-self property of the item. - This determines the alignment of the item along the corss-axis (perpendicular to the direction + This determines the alignment of the item along the cross-axis (perpendicular to the direction of flow). */ AlignSelf alignSelf = AlignSelf::stretch; diff --git a/modules/juce_gui_basics/widgets/juce_ComboBox.cpp b/modules/juce_gui_basics/widgets/juce_ComboBox.cpp index 2100c123b8..dbdd6aeca0 100644 --- a/modules/juce_gui_basics/widgets/juce_ComboBox.cpp +++ b/modules/juce_gui_basics/widgets/juce_ComboBox.cpp @@ -29,13 +29,7 @@ namespace juce ComboBox::ComboBox (const String& name) : Component (name), - lastCurrentId (0), - isButtonDown (false), - menuActive (false), - scrollWheelEnabled (false), - mouseWheelAccumulator (0), - noChoicesMessage (TRANS("(no choices)")), - labelEditableState (editableUnknown) + noChoicesMessage (TRANS("(no choices)")) { setRepaintsOnMouseActivity (true); lookAndFeelChanged(); @@ -84,7 +78,7 @@ void ComboBox::setTooltip (const String& newTooltip) } //============================================================================== -void ComboBox::addItem (const String& newItemText, const int newItemId) +void ComboBox::addItem (const String& newItemText, int newItemId) { // you can't add empty strings to the list.. jassert (newItemText.isNotEmpty()); @@ -96,12 +90,10 @@ void ComboBox::addItem (const String& newItemText, const int newItemId) jassert (getItemForId (newItemId) == nullptr); if (newItemText.isNotEmpty() && newItemId != 0) - { currentMenu.addItem (newItemId, newItemText, true, false); - } } -void ComboBox::addItemList (const StringArray& itemsToAdd, const int firstItemIdOffset) +void ComboBox::addItemList (const StringArray& itemsToAdd, int firstItemIdOffset) { for (int i = 0; i < itemsToAdd.size(); ++i) currentMenu.addItem (i + firstItemIdOffset, itemsToAdd[i]); @@ -118,26 +110,26 @@ void ComboBox::addSectionHeading (const String& headingName) jassert (headingName.isNotEmpty()); if (headingName.isNotEmpty()) - { currentMenu.addSectionHeader (headingName); - } } -void ComboBox::setItemEnabled (const int itemId, const bool shouldBeEnabled) +void ComboBox::setItemEnabled (int itemId, bool shouldBeEnabled) { - if (PopupMenu::Item* item = getItemForId (itemId)) + if (auto* item = getItemForId (itemId)) item->isEnabled = shouldBeEnabled; } bool ComboBox::isItemEnabled (int itemId) const noexcept { - const PopupMenu::Item* item = getItemForId (itemId); - return item != nullptr && item->isEnabled; + if (auto* item = getItemForId (itemId)) + return item->isEnabled; + + return false; } -void ComboBox::changeItemText (const int itemId, const String& newText) +void ComboBox::changeItemText (int itemId, const String& newText) { - if (PopupMenu::Item* item = getItemForId (itemId)) + if (auto* item = getItemForId (itemId)) item->text = newText; else jassertfalse; @@ -152,15 +144,13 @@ void ComboBox::clear (const NotificationType notification) } //============================================================================== -PopupMenu::Item* ComboBox::getItemForId (const int itemId) const noexcept +PopupMenu::Item* ComboBox::getItemForId (int itemId) const noexcept { if (itemId != 0) { - PopupMenu::MenuItemIterator iterator (currentMenu, true); - - while (iterator.next()) + for (PopupMenu::MenuItemIterator iterator (currentMenu, true); iterator.next();) { - PopupMenu::Item &item = iterator.getItem(); + auto& item = iterator.getItem(); if (item.itemID == itemId) return &item; @@ -173,11 +163,10 @@ PopupMenu::Item* ComboBox::getItemForId (const int itemId) const noexcept PopupMenu::Item* ComboBox::getItemForIndex (const int index) const noexcept { int n = 0; - PopupMenu::MenuItemIterator iterator (currentMenu, true); - while (iterator.next()) + for (PopupMenu::MenuItemIterator iterator (currentMenu, true); iterator.next();) { - PopupMenu::Item &item = iterator.getItem(); + auto& item = iterator.getItem(); if (item.itemID != 0) if (n++ == index) @@ -190,11 +179,10 @@ PopupMenu::Item* ComboBox::getItemForIndex (const int index) const noexcept int ComboBox::getNumItems() const noexcept { int n = 0; - PopupMenu::MenuItemIterator iterator (currentMenu, true); - while (iterator.next()) + for (PopupMenu::MenuItemIterator iterator (currentMenu, true); iterator.next();) { - PopupMenu::Item &item = iterator.getItem(); + auto& item = iterator.getItem(); if (item.itemID != 0) n++; @@ -224,9 +212,8 @@ int ComboBox::indexOfItemId (const int itemId) const noexcept if (itemId != 0) { int n = 0; - PopupMenu::MenuItemIterator iterator (currentMenu, true); - while (iterator.next()) + for (PopupMenu::MenuItemIterator iterator (currentMenu, true); iterator.next();) { auto& item = iterator.getItem(); @@ -244,7 +231,7 @@ int ComboBox::indexOfItemId (const int itemId) const noexcept //============================================================================== int ComboBox::getSelectedItemIndex() const { - int index = indexOfItemId (currentId.getValue()); + auto index = indexOfItemId (currentId.getValue()); if (getText() != getItemText (index)) index = -1; @@ -259,15 +246,17 @@ void ComboBox::setSelectedItemIndex (const int index, const NotificationType not int ComboBox::getSelectedId() const noexcept { - const PopupMenu::Item* const item = getItemForId (currentId.getValue()); + if (auto* item = getItemForId (currentId.getValue())) + if (getText() == item->text) + return item->itemID; - return (item != nullptr && getText() == item->text) ? item->itemID : 0; + return 0; } void ComboBox::setSelectedId (const int newItemId, const NotificationType notification) { - const PopupMenu::Item* const item = getItemForId (newItemId); - const String newItemText (item != nullptr ? item->text : String()); + auto* item = getItemForId (newItemId); + auto newItemText = item != nullptr ? item->text : String(); if (lastCurrentId != newItemId || label->getText() != newItemText) { @@ -283,7 +272,7 @@ void ComboBox::setSelectedId (const int newItemId, const NotificationType notifi bool ComboBox::selectIfEnabled (const int index) { - if (const PopupMenu::Item* const item = getItemForIndex (index)) + if (auto* item = getItemForIndex (index)) { if (item->isEnabled) { @@ -318,11 +307,9 @@ String ComboBox::getText() const void ComboBox::setText (const String& newText, const NotificationType notification) { - PopupMenu::MenuItemIterator iterator (currentMenu, true); - - while (iterator.next()) + for (PopupMenu::MenuItemIterator iterator (currentMenu, true); iterator.next();) { - PopupMenu::Item &item = iterator.getItem(); + auto& item = iterator.getItem(); if (item.itemID != 0 && item.text == newText) @@ -547,12 +534,11 @@ void ComboBox::showPopup() if (hasItems) { - PopupMenu::MenuItemIterator iterator (currentMenu, true); - const int selectedId = getSelectedId(); + auto selectedId = getSelectedId(); - while (iterator.next()) + for (PopupMenu::MenuItemIterator iterator (currentMenu, true); iterator.next();) { - PopupMenu::Item &item = iterator.getItem(); + auto& item = iterator.getItem(); if (item.itemID != 0) item.isTicked = (item.itemID == selectedId); @@ -563,7 +549,7 @@ void ComboBox::showPopup() noChoicesMenu.addItem (1, noChoicesMessage, false, false); } - PopupMenu& menuToShow = (hasItems ? currentMenu : noChoicesMenu); + auto& menuToShow = (hasItems ? currentMenu : noChoicesMenu); menuToShow.setLookAndFeel (&getLookAndFeel()); menuToShow.showMenuAsync (PopupMenu::Options().withTargetComponent (this) .withItemThatMustBeVisible (getSelectedId()) @@ -599,7 +585,7 @@ void ComboBox::mouseUp (const MouseEvent& e2) isButtonDown = false; repaint(); - const MouseEvent e (e2.getEventRelativeTo (this)); + auto e = e2.getEventRelativeTo (this); if (reallyContains (e.getPosition(), true) && (e2.eventComponent == this || ! label->isEditable())) diff --git a/modules/juce_gui_basics/widgets/juce_ComboBox.h b/modules/juce_gui_basics/widgets/juce_ComboBox.h index 2092287629..302f6df981 100644 --- a/modules/juce_gui_basics/widgets/juce_ComboBox.h +++ b/modules/juce_gui_basics/widgets/juce_ComboBox.h @@ -428,13 +428,13 @@ private: PopupMenu currentMenu; Value currentId; - int lastCurrentId; - bool isButtonDown, menuActive, scrollWheelEnabled; - float mouseWheelAccumulator; + int lastCurrentId = 0; + bool isButtonDown = false, menuActive = false, scrollWheelEnabled = false; + float mouseWheelAccumulator = 0; ListenerList listeners; ScopedPointer