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.

638 lines
17KB

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