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.

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