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

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