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.

598 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. ComboBox::ItemInfo::ItemInfo (const String& nm, int iid, bool enabled, bool heading)
  19. : name (nm), itemId (iid), isEnabled (enabled), isHeading (heading)
  20. {
  21. }
  22. bool ComboBox::ItemInfo::isSeparator() const noexcept
  23. {
  24. return name.isEmpty();
  25. }
  26. bool ComboBox::ItemInfo::isRealItem() const noexcept
  27. {
  28. return ! (isHeading || name.isEmpty());
  29. }
  30. //==============================================================================
  31. ComboBox::ComboBox (const String& name)
  32. : Component (name),
  33. lastCurrentId (0),
  34. isButtonDown (false),
  35. separatorPending (false),
  36. menuActive (false),
  37. noChoicesMessage (TRANS("(no choices)"))
  38. {
  39. setRepaintsOnMouseActivity (true);
  40. ComboBox::lookAndFeelChanged();
  41. currentId.addListener (this);
  42. }
  43. ComboBox::~ComboBox()
  44. {
  45. currentId.removeListener (this);
  46. if (menuActive)
  47. PopupMenu::dismissAllActiveMenus();
  48. label = nullptr;
  49. }
  50. //==============================================================================
  51. void ComboBox::setEditableText (const bool isEditable)
  52. {
  53. if (label->isEditableOnSingleClick() != isEditable || label->isEditableOnDoubleClick() != isEditable)
  54. {
  55. label->setEditable (isEditable, isEditable, false);
  56. setWantsKeyboardFocus (! isEditable);
  57. resized();
  58. }
  59. }
  60. bool ComboBox::isTextEditable() const noexcept
  61. {
  62. return label->isEditable();
  63. }
  64. void ComboBox::setJustificationType (const Justification& justification)
  65. {
  66. label->setJustificationType (justification);
  67. }
  68. Justification ComboBox::getJustificationType() const noexcept
  69. {
  70. return label->getJustificationType();
  71. }
  72. void ComboBox::setTooltip (const String& newTooltip)
  73. {
  74. SettableTooltipClient::setTooltip (newTooltip);
  75. label->setTooltip (newTooltip);
  76. }
  77. //==============================================================================
  78. void ComboBox::addItem (const String& newItemText, const int newItemId)
  79. {
  80. // you can't add empty strings to the list..
  81. jassert (newItemText.isNotEmpty());
  82. // IDs must be non-zero, as zero is used to indicate a lack of selecion.
  83. jassert (newItemId != 0);
  84. // you shouldn't use duplicate item IDs!
  85. jassert (getItemForId (newItemId) == nullptr);
  86. if (newItemText.isNotEmpty() && newItemId != 0)
  87. {
  88. if (separatorPending)
  89. {
  90. separatorPending = false;
  91. items.add (new ItemInfo (String::empty, 0, false, false));
  92. }
  93. items.add (new ItemInfo (newItemText, newItemId, true, false));
  94. }
  95. }
  96. void ComboBox::addItemList (const StringArray& itemsToAdd, const int firstItemIdOffset)
  97. {
  98. for (int i = 0; i < itemsToAdd.size(); ++i)
  99. addItem (itemsToAdd[i], i + firstItemIdOffset);
  100. }
  101. void ComboBox::addSeparator()
  102. {
  103. separatorPending = (items.size() > 0);
  104. }
  105. void ComboBox::addSectionHeading (const String& headingName)
  106. {
  107. // you can't add empty strings to the list..
  108. jassert (headingName.isNotEmpty());
  109. if (headingName.isNotEmpty())
  110. {
  111. if (separatorPending)
  112. {
  113. separatorPending = false;
  114. items.add (new ItemInfo (String::empty, 0, false, false));
  115. }
  116. items.add (new ItemInfo (headingName, 0, true, true));
  117. }
  118. }
  119. void ComboBox::setItemEnabled (const int itemId, const bool shouldBeEnabled)
  120. {
  121. if (ItemInfo* const item = getItemForId (itemId))
  122. item->isEnabled = shouldBeEnabled;
  123. }
  124. bool ComboBox::isItemEnabled (int itemId) const noexcept
  125. {
  126. const ItemInfo* const item = getItemForId (itemId);
  127. return item != nullptr && item->isEnabled;
  128. }
  129. void ComboBox::changeItemText (const int itemId, const String& newText)
  130. {
  131. if (ItemInfo* const item = getItemForId (itemId))
  132. item->name = newText;
  133. else
  134. jassertfalse;
  135. }
  136. void ComboBox::clear (const bool dontSendChangeMessage)
  137. {
  138. items.clear();
  139. separatorPending = false;
  140. if (! label->isEditable())
  141. setSelectedItemIndex (-1, dontSendChangeMessage);
  142. }
  143. //==============================================================================
  144. ComboBox::ItemInfo* ComboBox::getItemForId (const int itemId) const noexcept
  145. {
  146. if (itemId != 0)
  147. {
  148. for (int i = items.size(); --i >= 0;)
  149. if (items.getUnchecked(i)->itemId == itemId)
  150. return items.getUnchecked(i);
  151. }
  152. return nullptr;
  153. }
  154. ComboBox::ItemInfo* ComboBox::getItemForIndex (const int index) const noexcept
  155. {
  156. for (int n = 0, i = 0; i < items.size(); ++i)
  157. {
  158. ItemInfo* const item = items.getUnchecked(i);
  159. if (item->isRealItem())
  160. if (n++ == index)
  161. return item;
  162. }
  163. return nullptr;
  164. }
  165. int ComboBox::getNumItems() const noexcept
  166. {
  167. int n = 0;
  168. for (int i = items.size(); --i >= 0;)
  169. if (items.getUnchecked(i)->isRealItem())
  170. ++n;
  171. return n;
  172. }
  173. String ComboBox::getItemText (const int index) const
  174. {
  175. if (const ItemInfo* const item = getItemForIndex (index))
  176. return item->name;
  177. return String::empty;
  178. }
  179. int ComboBox::getItemId (const int index) const noexcept
  180. {
  181. if (const ItemInfo* const item = getItemForIndex (index))
  182. return item->itemId;
  183. return 0;
  184. }
  185. int ComboBox::indexOfItemId (const int itemId) const noexcept
  186. {
  187. for (int n = 0, i = 0; i < items.size(); ++i)
  188. {
  189. const ItemInfo* const item = items.getUnchecked(i);
  190. if (item->isRealItem())
  191. {
  192. if (item->itemId == itemId)
  193. return n;
  194. ++n;
  195. }
  196. }
  197. return -1;
  198. }
  199. //==============================================================================
  200. int ComboBox::getSelectedItemIndex() const
  201. {
  202. int index = indexOfItemId (currentId.getValue());
  203. if (getText() != getItemText (index))
  204. index = -1;
  205. return index;
  206. }
  207. void ComboBox::setSelectedItemIndex (const int index, const bool dontSendChangeMessage)
  208. {
  209. setSelectedId (getItemId (index), dontSendChangeMessage);
  210. }
  211. int ComboBox::getSelectedId() const noexcept
  212. {
  213. const ItemInfo* const item = getItemForId (currentId.getValue());
  214. return (item != nullptr && getText() == item->name) ? item->itemId : 0;
  215. }
  216. void ComboBox::setSelectedId (const int newItemId, const bool dontSendChangeMessage)
  217. {
  218. const ItemInfo* const item = getItemForId (newItemId);
  219. const String newItemText (item != nullptr ? item->name : String::empty);
  220. if (lastCurrentId != newItemId || label->getText() != newItemText)
  221. {
  222. if (! dontSendChangeMessage)
  223. triggerAsyncUpdate();
  224. label->setText (newItemText, false);
  225. lastCurrentId = newItemId;
  226. currentId = newItemId;
  227. repaint(); // for the benefit of the 'none selected' text
  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. void ComboBox::valueChanged (Value&)
  243. {
  244. if (lastCurrentId != (int) currentId.getValue())
  245. setSelectedId (currentId.getValue(), false);
  246. }
  247. //==============================================================================
  248. String ComboBox::getText() const
  249. {
  250. return label->getText();
  251. }
  252. void ComboBox::setText (const String& newText, const bool dontSendChangeMessage)
  253. {
  254. for (int i = items.size(); --i >= 0;)
  255. {
  256. const ItemInfo* const item = items.getUnchecked(i);
  257. if (item->isRealItem()
  258. && item->name == newText)
  259. {
  260. setSelectedId (item->itemId, dontSendChangeMessage);
  261. return;
  262. }
  263. }
  264. lastCurrentId = 0;
  265. currentId = 0;
  266. if (label->getText() != newText)
  267. {
  268. label->setText (newText, false);
  269. if (! dontSendChangeMessage)
  270. triggerAsyncUpdate();
  271. }
  272. repaint();
  273. }
  274. void ComboBox::showEditor()
  275. {
  276. jassert (isTextEditable()); // you probably shouldn't do this to a non-editable combo box?
  277. label->showEditor();
  278. }
  279. //==============================================================================
  280. void ComboBox::setTextWhenNothingSelected (const String& newMessage)
  281. {
  282. if (textWhenNothingSelected != newMessage)
  283. {
  284. textWhenNothingSelected = newMessage;
  285. repaint();
  286. }
  287. }
  288. String ComboBox::getTextWhenNothingSelected() const
  289. {
  290. return textWhenNothingSelected;
  291. }
  292. void ComboBox::setTextWhenNoChoicesAvailable (const String& newMessage)
  293. {
  294. noChoicesMessage = newMessage;
  295. }
  296. String ComboBox::getTextWhenNoChoicesAvailable() const
  297. {
  298. return noChoicesMessage;
  299. }
  300. //==============================================================================
  301. void ComboBox::paint (Graphics& g)
  302. {
  303. getLookAndFeel().drawComboBox (g, getWidth(), getHeight(), isButtonDown,
  304. label->getRight(), 0, getWidth() - label->getRight(), getHeight(),
  305. *this);
  306. if (textWhenNothingSelected.isNotEmpty()
  307. && label->getText().isEmpty()
  308. && ! label->isBeingEdited())
  309. {
  310. g.setColour (findColour (textColourId).withMultipliedAlpha (0.5f));
  311. g.setFont (label->getFont());
  312. g.drawFittedText (textWhenNothingSelected, label->getBounds().reduced (2, 1),
  313. label->getJustificationType(),
  314. jmax (1, (int) (label->getHeight() / label->getFont().getHeight())));
  315. }
  316. }
  317. void ComboBox::resized()
  318. {
  319. if (getHeight() > 0 && getWidth() > 0)
  320. getLookAndFeel().positionComboBoxText (*this, *label);
  321. }
  322. void ComboBox::enablementChanged()
  323. {
  324. repaint();
  325. }
  326. void ComboBox::lookAndFeelChanged()
  327. {
  328. repaint();
  329. {
  330. ScopedPointer <Label> newLabel (getLookAndFeel().createComboBoxTextBox (*this));
  331. jassert (newLabel != nullptr);
  332. if (label != nullptr)
  333. {
  334. newLabel->setEditable (label->isEditable());
  335. newLabel->setJustificationType (label->getJustificationType());
  336. newLabel->setTooltip (label->getTooltip());
  337. newLabel->setText (label->getText(), false);
  338. }
  339. label = newLabel;
  340. }
  341. addAndMakeVisible (label);
  342. setWantsKeyboardFocus (! label->isEditable());
  343. label->addListener (this);
  344. label->addMouseListener (this, false);
  345. label->setColour (Label::backgroundColourId, Colours::transparentBlack);
  346. label->setColour (Label::textColourId, findColour (ComboBox::textColourId));
  347. label->setColour (TextEditor::textColourId, findColour (ComboBox::textColourId));
  348. label->setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  349. label->setColour (TextEditor::highlightColourId, findColour (TextEditor::highlightColourId));
  350. label->setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  351. resized();
  352. }
  353. void ComboBox::colourChanged()
  354. {
  355. lookAndFeelChanged();
  356. }
  357. //==============================================================================
  358. bool ComboBox::keyPressed (const KeyPress& key)
  359. {
  360. if (key == KeyPress::upKey || key == KeyPress::leftKey)
  361. {
  362. int index = getSelectedItemIndex() - 1;
  363. while (index >= 0 && ! selectIfEnabled (index))
  364. --index;
  365. return true;
  366. }
  367. else if (key == KeyPress::downKey || key == KeyPress::rightKey)
  368. {
  369. int index = getSelectedItemIndex() + 1;
  370. while (index < getNumItems() && ! selectIfEnabled (index))
  371. ++index;
  372. return true;
  373. }
  374. else 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. //==============================================================================
  465. void ComboBox::addListener (ComboBoxListener* const listener)
  466. {
  467. listeners.add (listener);
  468. }
  469. void ComboBox::removeListener (ComboBoxListener* const listener)
  470. {
  471. listeners.remove (listener);
  472. }
  473. void ComboBox::handleAsyncUpdate()
  474. {
  475. Component::BailOutChecker checker (this);
  476. listeners.callChecked (checker, &ComboBoxListener::comboBoxChanged, this); // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  477. }