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.

628 lines
18KB

  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. ComboBox::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::lookAndFeelChanged()
  333. {
  334. repaint();
  335. {
  336. ScopedPointer <Label> newLabel (getLookAndFeel().createComboBoxTextBox (*this));
  337. jassert (newLabel != nullptr);
  338. if (label != nullptr)
  339. {
  340. newLabel->setEditable (label->isEditable());
  341. newLabel->setJustificationType (label->getJustificationType());
  342. newLabel->setTooltip (label->getTooltip());
  343. newLabel->setText (label->getText(), dontSendNotification);
  344. }
  345. label = newLabel;
  346. }
  347. addAndMakeVisible (label);
  348. setWantsKeyboardFocus (! label->isEditable());
  349. label->addListener (this);
  350. label->addMouseListener (this, false);
  351. label->setColour (Label::backgroundColourId, Colours::transparentBlack);
  352. label->setColour (Label::textColourId, findColour (ComboBox::textColourId));
  353. label->setColour (TextEditor::textColourId, findColour (ComboBox::textColourId));
  354. label->setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  355. label->setColour (TextEditor::highlightColourId, findColour (TextEditor::highlightColourId));
  356. label->setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  357. resized();
  358. }
  359. void ComboBox::colourChanged()
  360. {
  361. lookAndFeelChanged();
  362. }
  363. //==============================================================================
  364. bool ComboBox::keyPressed (const KeyPress& key)
  365. {
  366. if (key == KeyPress::upKey || key == KeyPress::leftKey)
  367. {
  368. nudgeSelectedItem (-1);
  369. return true;
  370. }
  371. if (key == KeyPress::downKey || key == KeyPress::rightKey)
  372. {
  373. nudgeSelectedItem (1);
  374. return true;
  375. }
  376. if (key == KeyPress::returnKey)
  377. {
  378. showPopup();
  379. return true;
  380. }
  381. return false;
  382. }
  383. bool ComboBox::keyStateChanged (const bool isKeyDown)
  384. {
  385. // only forward key events that aren't used by this component
  386. return isKeyDown
  387. && (KeyPress::isKeyCurrentlyDown (KeyPress::upKey)
  388. || KeyPress::isKeyCurrentlyDown (KeyPress::leftKey)
  389. || KeyPress::isKeyCurrentlyDown (KeyPress::downKey)
  390. || KeyPress::isKeyCurrentlyDown (KeyPress::rightKey));
  391. }
  392. //==============================================================================
  393. void ComboBox::focusGained (FocusChangeType) { repaint(); }
  394. void ComboBox::focusLost (FocusChangeType) { repaint(); }
  395. void ComboBox::labelTextChanged (Label*)
  396. {
  397. triggerAsyncUpdate();
  398. }
  399. //==============================================================================
  400. void ComboBox::popupMenuFinishedCallback (int result, ComboBox* box)
  401. {
  402. if (box != nullptr)
  403. {
  404. box->menuActive = false;
  405. if (result != 0)
  406. box->setSelectedId (result);
  407. }
  408. }
  409. void ComboBox::showPopup()
  410. {
  411. if (! menuActive)
  412. {
  413. const int selectedId = getSelectedId();
  414. PopupMenu menu;
  415. menu.setLookAndFeel (&getLookAndFeel());
  416. for (int i = 0; i < items.size(); ++i)
  417. {
  418. const ItemInfo* const item = items.getUnchecked(i);
  419. if (item->isSeparator())
  420. menu.addSeparator();
  421. else if (item->isHeading)
  422. menu.addSectionHeader (item->name);
  423. else
  424. menu.addItem (item->itemId, item->name,
  425. item->isEnabled, item->itemId == selectedId);
  426. }
  427. if (items.size() == 0)
  428. menu.addItem (1, noChoicesMessage, false);
  429. menuActive = true;
  430. menu.showMenuAsync (PopupMenu::Options().withTargetComponent (this)
  431. .withItemThatMustBeVisible (selectedId)
  432. .withMinimumWidth (getWidth())
  433. .withMaximumNumColumns (1)
  434. .withStandardItemHeight (jlimit (12, 24, getHeight())),
  435. ModalCallbackFunction::forComponent (popupMenuFinishedCallback, this));
  436. }
  437. }
  438. //==============================================================================
  439. void ComboBox::mouseDown (const MouseEvent& e)
  440. {
  441. beginDragAutoRepeat (300);
  442. isButtonDown = isEnabled() && ! e.mods.isPopupMenu();
  443. if (isButtonDown && (e.eventComponent == this || ! label->isEditable()))
  444. showPopup();
  445. }
  446. void ComboBox::mouseDrag (const MouseEvent& e)
  447. {
  448. beginDragAutoRepeat (50);
  449. if (isButtonDown && ! e.mouseWasClicked())
  450. showPopup();
  451. }
  452. void ComboBox::mouseUp (const MouseEvent& e2)
  453. {
  454. if (isButtonDown)
  455. {
  456. isButtonDown = false;
  457. repaint();
  458. const MouseEvent e (e2.getEventRelativeTo (this));
  459. if (reallyContains (e.getPosition(), true)
  460. && (e2.eventComponent == this || ! label->isEditable()))
  461. {
  462. showPopup();
  463. }
  464. }
  465. }
  466. void ComboBox::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  467. {
  468. if (! menuActive && scrollWheelEnabled && e.eventComponent == this && wheel.deltaY != 0)
  469. {
  470. const int oldPos = (int) mouseWheelAccumulator;
  471. mouseWheelAccumulator += wheel.deltaY * 5.0f;
  472. const int delta = oldPos - (int) mouseWheelAccumulator;
  473. if (delta != 0)
  474. nudgeSelectedItem (delta);
  475. }
  476. else
  477. {
  478. Component::mouseWheelMove (e, wheel);
  479. }
  480. }
  481. void ComboBox::setScrollWheelEnabled (bool enabled) noexcept
  482. {
  483. scrollWheelEnabled = enabled;
  484. }
  485. //==============================================================================
  486. void ComboBox::addListener (ComboBoxListener* listener) { listeners.add (listener); }
  487. void ComboBox::removeListener (ComboBoxListener* listener) { listeners.remove (listener); }
  488. void ComboBox::handleAsyncUpdate()
  489. {
  490. Component::BailOutChecker checker (this);
  491. listeners.callChecked (checker, &ComboBoxListener::comboBoxChanged, this); // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  492. }
  493. void ComboBox::sendChange (const NotificationType notification)
  494. {
  495. if (notification != dontSendNotification)
  496. triggerAsyncUpdate();
  497. if (notification == sendNotificationSync)
  498. handleUpdateNowIfNeeded();
  499. }
  500. // Old deprecated methods - remove eventually...
  501. void ComboBox::clear (const bool dontSendChange) { clear (dontSendChange ? dontSendNotification : sendNotification); }
  502. void ComboBox::setSelectedItemIndex (const int index, const bool dontSendChange) { setSelectedItemIndex (index, dontSendChange ? dontSendNotification : sendNotification); }
  503. void ComboBox::setSelectedId (const int newItemId, const bool dontSendChange) { setSelectedId (newItemId, dontSendChange ? dontSendNotification : sendNotification); }
  504. void ComboBox::setText (const String& newText, const bool dontSendChange) { setText (newText, dontSendChange ? dontSendNotification : sendNotification); }