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.

659 lines
19KB

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