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.

608 lines
17KB

  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. void ComboBox::valueChanged (Value&)
  241. {
  242. if (lastCurrentId != (int) currentId.getValue())
  243. setSelectedId (currentId.getValue());
  244. }
  245. //==============================================================================
  246. String ComboBox::getText() const
  247. {
  248. return label->getText();
  249. }
  250. void ComboBox::setText (const String& newText, const NotificationType notification)
  251. {
  252. for (int i = items.size(); --i >= 0;)
  253. {
  254. const ItemInfo* const item = items.getUnchecked(i);
  255. if (item->isRealItem()
  256. && item->name == newText)
  257. {
  258. setSelectedId (item->itemId, notification);
  259. return;
  260. }
  261. }
  262. lastCurrentId = 0;
  263. currentId = 0;
  264. repaint();
  265. if (label->getText() != newText)
  266. {
  267. label->setText (newText, dontSendNotification);
  268. sendChange (notification);
  269. }
  270. }
  271. void ComboBox::showEditor()
  272. {
  273. jassert (isTextEditable()); // you probably shouldn't do this to a non-editable combo box?
  274. label->showEditor();
  275. }
  276. //==============================================================================
  277. void ComboBox::setTextWhenNothingSelected (const String& newMessage)
  278. {
  279. if (textWhenNothingSelected != newMessage)
  280. {
  281. textWhenNothingSelected = newMessage;
  282. repaint();
  283. }
  284. }
  285. String ComboBox::getTextWhenNothingSelected() const
  286. {
  287. return textWhenNothingSelected;
  288. }
  289. void ComboBox::setTextWhenNoChoicesAvailable (const String& newMessage)
  290. {
  291. noChoicesMessage = newMessage;
  292. }
  293. String ComboBox::getTextWhenNoChoicesAvailable() const
  294. {
  295. return noChoicesMessage;
  296. }
  297. //==============================================================================
  298. void ComboBox::paint (Graphics& g)
  299. {
  300. getLookAndFeel().drawComboBox (g, getWidth(), getHeight(), isButtonDown,
  301. label->getRight(), 0, getWidth() - label->getRight(), getHeight(),
  302. *this);
  303. if (textWhenNothingSelected.isNotEmpty()
  304. && label->getText().isEmpty()
  305. && ! label->isBeingEdited())
  306. {
  307. g.setColour (findColour (textColourId).withMultipliedAlpha (0.5f));
  308. g.setFont (label->getFont());
  309. g.drawFittedText (textWhenNothingSelected, label->getBounds().reduced (2, 1),
  310. label->getJustificationType(),
  311. jmax (1, (int) (label->getHeight() / label->getFont().getHeight())));
  312. }
  313. }
  314. void ComboBox::resized()
  315. {
  316. if (getHeight() > 0 && getWidth() > 0)
  317. getLookAndFeel().positionComboBoxText (*this, *label);
  318. }
  319. void ComboBox::enablementChanged()
  320. {
  321. repaint();
  322. }
  323. void ComboBox::lookAndFeelChanged()
  324. {
  325. repaint();
  326. {
  327. ScopedPointer <Label> newLabel (getLookAndFeel().createComboBoxTextBox (*this));
  328. jassert (newLabel != nullptr);
  329. if (label != nullptr)
  330. {
  331. newLabel->setEditable (label->isEditable());
  332. newLabel->setJustificationType (label->getJustificationType());
  333. newLabel->setTooltip (label->getTooltip());
  334. newLabel->setText (label->getText(), dontSendNotification);
  335. }
  336. label = newLabel;
  337. }
  338. addAndMakeVisible (label);
  339. setWantsKeyboardFocus (! label->isEditable());
  340. label->addListener (this);
  341. label->addMouseListener (this, false);
  342. label->setColour (Label::backgroundColourId, Colours::transparentBlack);
  343. label->setColour (Label::textColourId, findColour (ComboBox::textColourId));
  344. label->setColour (TextEditor::textColourId, findColour (ComboBox::textColourId));
  345. label->setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  346. label->setColour (TextEditor::highlightColourId, findColour (TextEditor::highlightColourId));
  347. label->setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  348. resized();
  349. }
  350. void ComboBox::colourChanged()
  351. {
  352. lookAndFeelChanged();
  353. }
  354. //==============================================================================
  355. bool ComboBox::keyPressed (const KeyPress& key)
  356. {
  357. if (key == KeyPress::upKey || key == KeyPress::leftKey)
  358. {
  359. int index = getSelectedItemIndex() - 1;
  360. while (index >= 0 && ! selectIfEnabled (index))
  361. --index;
  362. return true;
  363. }
  364. else if (key == KeyPress::downKey || key == KeyPress::rightKey)
  365. {
  366. int index = getSelectedItemIndex() + 1;
  367. while (index < getNumItems() && ! selectIfEnabled (index))
  368. ++index;
  369. return true;
  370. }
  371. else if (key == KeyPress::returnKey)
  372. {
  373. showPopup();
  374. return true;
  375. }
  376. return false;
  377. }
  378. bool ComboBox::keyStateChanged (const bool isKeyDown)
  379. {
  380. // only forward key events that aren't used by this component
  381. return isKeyDown
  382. && (KeyPress::isKeyCurrentlyDown (KeyPress::upKey)
  383. || KeyPress::isKeyCurrentlyDown (KeyPress::leftKey)
  384. || KeyPress::isKeyCurrentlyDown (KeyPress::downKey)
  385. || KeyPress::isKeyCurrentlyDown (KeyPress::rightKey));
  386. }
  387. //==============================================================================
  388. void ComboBox::focusGained (FocusChangeType) { repaint(); }
  389. void ComboBox::focusLost (FocusChangeType) { repaint(); }
  390. void ComboBox::labelTextChanged (Label*)
  391. {
  392. triggerAsyncUpdate();
  393. }
  394. //==============================================================================
  395. void ComboBox::popupMenuFinishedCallback (int result, ComboBox* box)
  396. {
  397. if (box != nullptr)
  398. {
  399. box->menuActive = false;
  400. if (result != 0)
  401. box->setSelectedId (result);
  402. }
  403. }
  404. void ComboBox::showPopup()
  405. {
  406. if (! menuActive)
  407. {
  408. const int selectedId = getSelectedId();
  409. PopupMenu menu;
  410. menu.setLookAndFeel (&getLookAndFeel());
  411. for (int i = 0; i < items.size(); ++i)
  412. {
  413. const ItemInfo* const item = items.getUnchecked(i);
  414. if (item->isSeparator())
  415. menu.addSeparator();
  416. else if (item->isHeading)
  417. menu.addSectionHeader (item->name);
  418. else
  419. menu.addItem (item->itemId, item->name,
  420. item->isEnabled, item->itemId == selectedId);
  421. }
  422. if (items.size() == 0)
  423. menu.addItem (1, noChoicesMessage, false);
  424. menuActive = true;
  425. menu.showMenuAsync (PopupMenu::Options().withTargetComponent (this)
  426. .withItemThatMustBeVisible (selectedId)
  427. .withMinimumWidth (getWidth())
  428. .withMaximumNumColumns (1)
  429. .withStandardItemHeight (jlimit (12, 24, getHeight())),
  430. ModalCallbackFunction::forComponent (popupMenuFinishedCallback, this));
  431. }
  432. }
  433. //==============================================================================
  434. void ComboBox::mouseDown (const MouseEvent& e)
  435. {
  436. beginDragAutoRepeat (300);
  437. isButtonDown = isEnabled() && ! e.mods.isPopupMenu();
  438. if (isButtonDown && (e.eventComponent == this || ! label->isEditable()))
  439. showPopup();
  440. }
  441. void ComboBox::mouseDrag (const MouseEvent& e)
  442. {
  443. beginDragAutoRepeat (50);
  444. if (isButtonDown && ! e.mouseWasClicked())
  445. showPopup();
  446. }
  447. void ComboBox::mouseUp (const MouseEvent& e2)
  448. {
  449. if (isButtonDown)
  450. {
  451. isButtonDown = false;
  452. repaint();
  453. const MouseEvent e (e2.getEventRelativeTo (this));
  454. if (reallyContains (e.getPosition(), true)
  455. && (e2.eventComponent == this || ! label->isEditable()))
  456. {
  457. showPopup();
  458. }
  459. }
  460. }
  461. //==============================================================================
  462. void ComboBox::addListener (ComboBoxListener* const listener)
  463. {
  464. listeners.add (listener);
  465. }
  466. void ComboBox::removeListener (ComboBoxListener* const listener)
  467. {
  468. listeners.remove (listener);
  469. }
  470. void ComboBox::handleAsyncUpdate()
  471. {
  472. Component::BailOutChecker checker (this);
  473. listeners.callChecked (checker, &ComboBoxListener::comboBoxChanged, this); // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  474. }
  475. void ComboBox::sendChange (const NotificationType notification)
  476. {
  477. if (notification != dontSendNotification)
  478. triggerAsyncUpdate();
  479. if (notification == sendNotificationSync)
  480. handleUpdateNowIfNeeded();
  481. }
  482. // Old deprecated methods - remove eventually...
  483. void ComboBox::clear (const bool dontSendChange) { clear (dontSendChange ? dontSendNotification : sendNotification); }
  484. void ComboBox::setSelectedItemIndex (const int index, const bool dontSendChange) { setSelectedItemIndex (index, dontSendChange ? dontSendNotification : sendNotification); }
  485. void ComboBox::setSelectedId (const int newItemId, const bool dontSendChange) { setSelectedId (newItemId, dontSendChange ? dontSendNotification : sendNotification); }
  486. void ComboBox::setText (const String& newText, const bool dontSendChange) { setText (newText, dontSendChange ? dontSendNotification : sendNotification); }