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.

597 lines
16KB

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