The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

635 lines
18KB

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