Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_ComboBox.cpp 18KB

9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. ComboBox::ItemInfo::ItemInfo (const String& nm, int iid, bool enabled, bool heading)
  18. : name (nm), itemId (iid), isEnabled (enabled), isHeading (heading)
  19. {
  20. }
  21. bool ComboBox::ItemInfo::isSeparator() const noexcept
  22. {
  23. return name.isEmpty();
  24. }
  25. bool ComboBox::ItemInfo::isRealItem() const noexcept
  26. {
  27. return ! (isHeading || name.isEmpty());
  28. }
  29. //==============================================================================
  30. ComboBox::ComboBox (const String& name)
  31. : Component (name),
  32. lastCurrentId (0),
  33. isButtonDown (false),
  34. separatorPending (false),
  35. menuActive (false),
  36. scrollWheelEnabled (false),
  37. mouseWheelAccumulator (0),
  38. noChoicesMessage (TRANS("(no choices)"))
  39. {
  40. setRepaintsOnMouseActivity (true);
  41. lookAndFeelChanged();
  42. currentId.addListener (this);
  43. }
  44. ComboBox::~ComboBox()
  45. {
  46. currentId.removeListener (this);
  47. hidePopup();
  48. label = nullptr;
  49. }
  50. //==============================================================================
  51. void ComboBox::setEditableText (const bool isEditable)
  52. {
  53. if (label->isEditableOnSingleClick() != isEditable || label->isEditableOnDoubleClick() != isEditable)
  54. {
  55. label->setEditable (isEditable, isEditable, false);
  56. setWantsKeyboardFocus (! isEditable);
  57. resized();
  58. }
  59. }
  60. bool ComboBox::isTextEditable() const noexcept
  61. {
  62. return label->isEditable();
  63. }
  64. void ComboBox::setJustificationType (Justification justification)
  65. {
  66. label->setJustificationType (justification);
  67. }
  68. Justification ComboBox::getJustificationType() const noexcept
  69. {
  70. return label->getJustificationType();
  71. }
  72. void ComboBox::setTooltip (const String& newTooltip)
  73. {
  74. SettableTooltipClient::setTooltip (newTooltip);
  75. label->setTooltip (newTooltip);
  76. }
  77. //==============================================================================
  78. void ComboBox::addItem (const String& newItemText, const int newItemId)
  79. {
  80. // you can't add empty strings to the list..
  81. jassert (newItemText.isNotEmpty());
  82. // IDs must be non-zero, as zero is used to indicate a lack of selecion.
  83. jassert (newItemId != 0);
  84. // you shouldn't use duplicate item IDs!
  85. jassert (getItemForId (newItemId) == nullptr);
  86. if (newItemText.isNotEmpty() && newItemId != 0)
  87. {
  88. if (separatorPending)
  89. {
  90. separatorPending = false;
  91. items.add (new ItemInfo (String::empty, 0, false, false));
  92. }
  93. items.add (new ItemInfo (newItemText, newItemId, true, false));
  94. }
  95. }
  96. void ComboBox::addItemList (const StringArray& itemsToAdd, const int firstItemIdOffset)
  97. {
  98. for (int i = 0; i < itemsToAdd.size(); ++i)
  99. addItem (itemsToAdd[i], i + firstItemIdOffset);
  100. }
  101. void ComboBox::addSeparator()
  102. {
  103. separatorPending = (items.size() > 0);
  104. }
  105. void ComboBox::addSectionHeading (const String& headingName)
  106. {
  107. // you can't add empty strings to the list..
  108. jassert (headingName.isNotEmpty());
  109. if (headingName.isNotEmpty())
  110. {
  111. if (separatorPending)
  112. {
  113. separatorPending = false;
  114. items.add (new ItemInfo (String::empty, 0, false, false));
  115. }
  116. items.add (new ItemInfo (headingName, 0, true, true));
  117. }
  118. }
  119. void ComboBox::setItemEnabled (const int itemId, const bool shouldBeEnabled)
  120. {
  121. if (ItemInfo* const item = getItemForId (itemId))
  122. item->isEnabled = shouldBeEnabled;
  123. }
  124. bool ComboBox::isItemEnabled (int itemId) const noexcept
  125. {
  126. const ItemInfo* const item = getItemForId (itemId);
  127. return item != nullptr && item->isEnabled;
  128. }
  129. void ComboBox::changeItemText (const int itemId, const String& newText)
  130. {
  131. if (ItemInfo* const item = getItemForId (itemId))
  132. item->name = newText;
  133. else
  134. jassertfalse;
  135. }
  136. void ComboBox::clear (const NotificationType notification)
  137. {
  138. items.clear();
  139. separatorPending = false;
  140. if (! label->isEditable())
  141. setSelectedItemIndex (-1, notification);
  142. }
  143. //==============================================================================
  144. ComboBox::ItemInfo* ComboBox::getItemForId (const int itemId) const noexcept
  145. {
  146. if (itemId != 0)
  147. {
  148. for (int i = items.size(); --i >= 0;)
  149. if (items.getUnchecked(i)->itemId == itemId)
  150. return items.getUnchecked(i);
  151. }
  152. return nullptr;
  153. }
  154. ComboBox::ItemInfo* ComboBox::getItemForIndex (const int index) const noexcept
  155. {
  156. for (int n = 0, i = 0; i < items.size(); ++i)
  157. {
  158. ItemInfo* const item = items.getUnchecked(i);
  159. if (item->isRealItem())
  160. if (n++ == index)
  161. return item;
  162. }
  163. return nullptr;
  164. }
  165. int ComboBox::getNumItems() const noexcept
  166. {
  167. int n = 0;
  168. for (int i = items.size(); --i >= 0;)
  169. if (items.getUnchecked(i)->isRealItem())
  170. ++n;
  171. return n;
  172. }
  173. String ComboBox::getItemText (const int index) const
  174. {
  175. if (const ItemInfo* const item = getItemForIndex (index))
  176. return item->name;
  177. return String::empty;
  178. }
  179. int ComboBox::getItemId (const int index) const noexcept
  180. {
  181. if (const ItemInfo* const item = getItemForIndex (index))
  182. return item->itemId;
  183. return 0;
  184. }
  185. int ComboBox::indexOfItemId (const int itemId) const noexcept
  186. {
  187. for (int n = 0, i = 0; i < items.size(); ++i)
  188. {
  189. const ItemInfo* const item = items.getUnchecked(i);
  190. if (item->isRealItem())
  191. {
  192. if (item->itemId == itemId)
  193. return n;
  194. ++n;
  195. }
  196. }
  197. return -1;
  198. }
  199. //==============================================================================
  200. int ComboBox::getSelectedItemIndex() const
  201. {
  202. int index = indexOfItemId (currentId.getValue());
  203. if (getText() != getItemText (index))
  204. index = -1;
  205. return index;
  206. }
  207. void ComboBox::setSelectedItemIndex (const int index, const NotificationType notification)
  208. {
  209. setSelectedId (getItemId (index), notification);
  210. }
  211. int ComboBox::getSelectedId() const noexcept
  212. {
  213. const ItemInfo* const item = getItemForId (currentId.getValue());
  214. return (item != nullptr && getText() == item->name) ? item->itemId : 0;
  215. }
  216. void ComboBox::setSelectedId (const int newItemId, const NotificationType notification)
  217. {
  218. const ItemInfo* const item = getItemForId (newItemId);
  219. const String newItemText (item != nullptr ? item->name : String::empty);
  220. if (lastCurrentId != newItemId || label->getText() != newItemText)
  221. {
  222. label->setText (newItemText, dontSendNotification);
  223. lastCurrentId = newItemId;
  224. currentId = newItemId;
  225. repaint(); // for the benefit of the 'none selected' text
  226. sendChange (notification);
  227. }
  228. }
  229. bool ComboBox::selectIfEnabled (const int index)
  230. {
  231. if (const ItemInfo* const item = getItemForIndex (index))
  232. {
  233. if (item->isEnabled)
  234. {
  235. setSelectedItemIndex (index);
  236. return true;
  237. }
  238. }
  239. return false;
  240. }
  241. bool ComboBox::nudgeSelectedItem (int delta)
  242. {
  243. for (int i = getSelectedItemIndex() + delta; isPositiveAndBelow (i, getNumItems()); i += delta)
  244. if (selectIfEnabled (i))
  245. return true;
  246. return false;
  247. }
  248. void ComboBox::valueChanged (Value&)
  249. {
  250. if (lastCurrentId != (int) currentId.getValue())
  251. setSelectedId (currentId.getValue());
  252. }
  253. //==============================================================================
  254. String ComboBox::getText() const
  255. {
  256. return label->getText();
  257. }
  258. void ComboBox::setText (const String& newText, const NotificationType notification)
  259. {
  260. for (int i = items.size(); --i >= 0;)
  261. {
  262. const ItemInfo* const item = items.getUnchecked(i);
  263. if (item->isRealItem()
  264. && item->name == newText)
  265. {
  266. setSelectedId (item->itemId, notification);
  267. return;
  268. }
  269. }
  270. lastCurrentId = 0;
  271. currentId = 0;
  272. repaint();
  273. if (label->getText() != newText)
  274. {
  275. label->setText (newText, dontSendNotification);
  276. sendChange (notification);
  277. }
  278. }
  279. void ComboBox::showEditor()
  280. {
  281. jassert (isTextEditable()); // you probably shouldn't do this to a non-editable combo box?
  282. label->showEditor();
  283. }
  284. //==============================================================================
  285. void ComboBox::setTextWhenNothingSelected (const String& newMessage)
  286. {
  287. if (textWhenNothingSelected != newMessage)
  288. {
  289. textWhenNothingSelected = newMessage;
  290. repaint();
  291. }
  292. }
  293. String ComboBox::getTextWhenNothingSelected() const
  294. {
  295. return textWhenNothingSelected;
  296. }
  297. void ComboBox::setTextWhenNoChoicesAvailable (const String& newMessage)
  298. {
  299. noChoicesMessage = newMessage;
  300. }
  301. String ComboBox::getTextWhenNoChoicesAvailable() const
  302. {
  303. return noChoicesMessage;
  304. }
  305. //==============================================================================
  306. void ComboBox::paint (Graphics& g)
  307. {
  308. getLookAndFeel().drawComboBox (g, getWidth(), getHeight(), isButtonDown,
  309. label->getRight(), 0, getWidth() - label->getRight(), getHeight(),
  310. *this);
  311. if (textWhenNothingSelected.isNotEmpty()
  312. && label->getText().isEmpty()
  313. && ! label->isBeingEdited())
  314. {
  315. g.setColour (findColour (textColourId).withMultipliedAlpha (0.5f));
  316. g.setFont (label->getFont());
  317. g.drawFittedText (textWhenNothingSelected, label->getBounds().reduced (2, 1),
  318. label->getJustificationType(),
  319. jmax (1, (int) (label->getHeight() / label->getFont().getHeight())));
  320. }
  321. }
  322. void ComboBox::resized()
  323. {
  324. if (getHeight() > 0 && getWidth() > 0)
  325. getLookAndFeel().positionComboBoxText (*this, *label);
  326. }
  327. void ComboBox::enablementChanged()
  328. {
  329. repaint();
  330. }
  331. void ComboBox::colourChanged()
  332. {
  333. lookAndFeelChanged();
  334. }
  335. void ComboBox::parentHierarchyChanged()
  336. {
  337. lookAndFeelChanged();
  338. }
  339. void ComboBox::lookAndFeelChanged()
  340. {
  341. repaint();
  342. {
  343. ScopedPointer<Label> newLabel (getLookAndFeel().createComboBoxTextBox (*this));
  344. jassert (newLabel != nullptr);
  345. if (label != nullptr)
  346. {
  347. newLabel->setEditable (label->isEditable());
  348. newLabel->setJustificationType (label->getJustificationType());
  349. newLabel->setTooltip (label->getTooltip());
  350. newLabel->setText (label->getText(), dontSendNotification);
  351. }
  352. label = newLabel;
  353. }
  354. addAndMakeVisible (label);
  355. setWantsKeyboardFocus (! label->isEditable());
  356. label->addListener (this);
  357. label->addMouseListener (this, false);
  358. label->setColour (Label::backgroundColourId, Colours::transparentBlack);
  359. label->setColour (Label::textColourId, findColour (ComboBox::textColourId));
  360. label->setColour (TextEditor::textColourId, findColour (ComboBox::textColourId));
  361. label->setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  362. label->setColour (TextEditor::highlightColourId, findColour (TextEditor::highlightColourId));
  363. label->setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  364. resized();
  365. }
  366. //==============================================================================
  367. bool ComboBox::keyPressed (const KeyPress& key)
  368. {
  369. if (key == KeyPress::upKey || key == KeyPress::leftKey)
  370. {
  371. nudgeSelectedItem (-1);
  372. return true;
  373. }
  374. if (key == KeyPress::downKey || key == KeyPress::rightKey)
  375. {
  376. nudgeSelectedItem (1);
  377. return true;
  378. }
  379. if (key == KeyPress::returnKey)
  380. {
  381. showPopupIfNotActive();
  382. return true;
  383. }
  384. return false;
  385. }
  386. bool ComboBox::keyStateChanged (const bool isKeyDown)
  387. {
  388. // only forward key events that aren't used by this component
  389. return isKeyDown
  390. && (KeyPress::isKeyCurrentlyDown (KeyPress::upKey)
  391. || KeyPress::isKeyCurrentlyDown (KeyPress::leftKey)
  392. || KeyPress::isKeyCurrentlyDown (KeyPress::downKey)
  393. || KeyPress::isKeyCurrentlyDown (KeyPress::rightKey));
  394. }
  395. //==============================================================================
  396. void ComboBox::focusGained (FocusChangeType) { repaint(); }
  397. void ComboBox::focusLost (FocusChangeType) { repaint(); }
  398. void ComboBox::labelTextChanged (Label*)
  399. {
  400. triggerAsyncUpdate();
  401. }
  402. //==============================================================================
  403. void ComboBox::showPopupIfNotActive()
  404. {
  405. if (! menuActive)
  406. {
  407. menuActive = true;
  408. showPopup();
  409. }
  410. }
  411. void ComboBox::hidePopup()
  412. {
  413. if (menuActive)
  414. {
  415. menuActive = false;
  416. PopupMenu::dismissAllActiveMenus();
  417. repaint();
  418. }
  419. }
  420. static void comboBoxPopupMenuFinishedCallback (int result, ComboBox* combo)
  421. {
  422. if (combo != nullptr)
  423. {
  424. combo->hidePopup();
  425. if (result != 0)
  426. combo->setSelectedId (result);
  427. }
  428. }
  429. void ComboBox::showPopup()
  430. {
  431. PopupMenu menu;
  432. menu.setLookAndFeel (&getLookAndFeel());
  433. addItemsToMenu (menu);
  434. menu.showMenuAsync (PopupMenu::Options().withTargetComponent (this)
  435. .withItemThatMustBeVisible (getSelectedId())
  436. .withMinimumWidth (getWidth())
  437. .withMaximumNumColumns (1)
  438. .withStandardItemHeight (label->getHeight()),
  439. ModalCallbackFunction::forComponent (comboBoxPopupMenuFinishedCallback, this));
  440. }
  441. void ComboBox::addItemsToMenu (PopupMenu& menu) const
  442. {
  443. const int selectedId = getSelectedId();
  444. for (int i = 0; i < items.size(); ++i)
  445. {
  446. const ItemInfo* const item = items.getUnchecked(i);
  447. jassert (item != nullptr);
  448. if (item->isSeparator())
  449. menu.addSeparator();
  450. else if (item->isHeading)
  451. menu.addSectionHeader (item->name);
  452. else
  453. menu.addItem (item->itemId, item->name,
  454. item->isEnabled, item->itemId == selectedId);
  455. }
  456. if (items.size() == 0)
  457. menu.addItem (1, noChoicesMessage, false);
  458. }
  459. //==============================================================================
  460. void ComboBox::mouseDown (const MouseEvent& e)
  461. {
  462. beginDragAutoRepeat (300);
  463. isButtonDown = isEnabled() && ! e.mods.isPopupMenu();
  464. if (isButtonDown && (e.eventComponent == this || ! label->isEditable()))
  465. showPopupIfNotActive();
  466. }
  467. void ComboBox::mouseDrag (const MouseEvent& e)
  468. {
  469. beginDragAutoRepeat (50);
  470. if (isButtonDown && e.mouseWasDraggedSinceMouseDown())
  471. showPopupIfNotActive();
  472. }
  473. void ComboBox::mouseUp (const MouseEvent& e2)
  474. {
  475. if (isButtonDown)
  476. {
  477. isButtonDown = false;
  478. repaint();
  479. const MouseEvent e (e2.getEventRelativeTo (this));
  480. if (reallyContains (e.getPosition(), true)
  481. && (e2.eventComponent == this || ! label->isEditable()))
  482. {
  483. showPopupIfNotActive();
  484. }
  485. }
  486. }
  487. void ComboBox::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  488. {
  489. if (! menuActive && scrollWheelEnabled && e.eventComponent == this && wheel.deltaY != 0)
  490. {
  491. const int oldPos = (int) mouseWheelAccumulator;
  492. mouseWheelAccumulator += wheel.deltaY * 5.0f;
  493. const int delta = oldPos - (int) mouseWheelAccumulator;
  494. if (delta != 0)
  495. nudgeSelectedItem (delta);
  496. }
  497. else
  498. {
  499. Component::mouseWheelMove (e, wheel);
  500. }
  501. }
  502. void ComboBox::setScrollWheelEnabled (bool enabled) noexcept
  503. {
  504. scrollWheelEnabled = enabled;
  505. }
  506. //==============================================================================
  507. void ComboBox::addListener (ComboBoxListener* listener) { listeners.add (listener); }
  508. void ComboBox::removeListener (ComboBoxListener* listener) { listeners.remove (listener); }
  509. void ComboBox::handleAsyncUpdate()
  510. {
  511. Component::BailOutChecker checker (this);
  512. listeners.callChecked (checker, &ComboBoxListener::comboBoxChanged, this); // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  513. }
  514. void ComboBox::sendChange (const NotificationType notification)
  515. {
  516. if (notification != dontSendNotification)
  517. triggerAsyncUpdate();
  518. if (notification == sendNotificationSync)
  519. handleUpdateNowIfNeeded();
  520. }
  521. // Old deprecated methods - remove eventually...
  522. void ComboBox::clear (const bool dontSendChange) { clear (dontSendChange ? dontSendNotification : sendNotification); }
  523. void ComboBox::setSelectedItemIndex (const int index, const bool dontSendChange) { setSelectedItemIndex (index, dontSendChange ? dontSendNotification : sendNotification); }
  524. void ComboBox::setSelectedId (const int newItemId, const bool dontSendChange) { setSelectedId (newItemId, dontSendChange ? dontSendNotification : sendNotification); }
  525. void ComboBox::setText (const String& newText, const bool dontSendChange) { setText (newText, dontSendChange ? dontSendNotification : sendNotification); }