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.

637 lines
18KB

  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& name_, int itemId_, bool isEnabled_, bool isHeading_)
  19. : name (name_), itemId (itemId_), isEnabled (isEnabled_), isHeading (isHeading_)
  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& items, const int firstItemIdOffset)
  97. {
  98. for (int i = 0; i < items.size(); ++i)
  99. addItem (items[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. ItemInfo* const item = getItemForId (itemId);
  122. if (item != nullptr)
  123. item->isEnabled = shouldBeEnabled;
  124. }
  125. bool ComboBox::isItemEnabled (int itemId) const noexcept
  126. {
  127. const ItemInfo* const item = getItemForId (itemId);
  128. return item != nullptr && item->isEnabled;
  129. }
  130. void ComboBox::changeItemText (const int itemId, const String& newText)
  131. {
  132. ItemInfo* const item = getItemForId (itemId);
  133. jassert (item != nullptr);
  134. if (item != nullptr)
  135. item->name = newText;
  136. }
  137. void ComboBox::clear (const bool dontSendChangeMessage)
  138. {
  139. items.clear();
  140. separatorPending = false;
  141. if (! label->isEditable())
  142. setSelectedItemIndex (-1, dontSendChangeMessage);
  143. }
  144. //==============================================================================
  145. ComboBox::ItemInfo* ComboBox::getItemForId (const int itemId) const noexcept
  146. {
  147. if (itemId != 0)
  148. {
  149. for (int i = items.size(); --i >= 0;)
  150. if (items.getUnchecked(i)->itemId == itemId)
  151. return items.getUnchecked(i);
  152. }
  153. return nullptr;
  154. }
  155. ComboBox::ItemInfo* ComboBox::getItemForIndex (const int index) const noexcept
  156. {
  157. for (int n = 0, i = 0; i < items.size(); ++i)
  158. {
  159. ItemInfo* const item = items.getUnchecked(i);
  160. if (item->isRealItem())
  161. if (n++ == index)
  162. return item;
  163. }
  164. return nullptr;
  165. }
  166. int ComboBox::getNumItems() const noexcept
  167. {
  168. int n = 0;
  169. for (int i = items.size(); --i >= 0;)
  170. if (items.getUnchecked(i)->isRealItem())
  171. ++n;
  172. return n;
  173. }
  174. String ComboBox::getItemText (const int index) const
  175. {
  176. const ItemInfo* const item = getItemForIndex (index);
  177. return item != nullptr ? item->name : String::empty;
  178. }
  179. int ComboBox::getItemId (const int index) const noexcept
  180. {
  181. const ItemInfo* const item = getItemForIndex (index);
  182. return item != nullptr ? item->itemId : 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, false);
  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. const ItemInfo* const item = getItemForIndex (index);
  232. if (item != nullptr && item->isEnabled)
  233. {
  234. setSelectedItemIndex (index);
  235. return true;
  236. }
  237. return false;
  238. }
  239. void ComboBox::valueChanged (Value&)
  240. {
  241. if (lastCurrentId != (int) currentId.getValue())
  242. setSelectedId (currentId.getValue(), false);
  243. }
  244. //==============================================================================
  245. String ComboBox::getText() const
  246. {
  247. return label->getText();
  248. }
  249. void ComboBox::setText (const String& newText, const bool dontSendChangeMessage)
  250. {
  251. for (int i = items.size(); --i >= 0;)
  252. {
  253. const ItemInfo* const item = items.getUnchecked(i);
  254. if (item->isRealItem()
  255. && item->name == newText)
  256. {
  257. setSelectedId (item->itemId, dontSendChangeMessage);
  258. return;
  259. }
  260. }
  261. lastCurrentId = 0;
  262. currentId = 0;
  263. if (label->getText() != newText)
  264. {
  265. label->setText (newText, false);
  266. if (! dontSendChangeMessage)
  267. triggerAsyncUpdate();
  268. }
  269. repaint();
  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,
  310. label->getX() + 2, label->getY() + 1,
  311. label->getWidth() - 4, label->getHeight() - 2,
  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(), false);
  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.isKeyCode (KeyPress::upKey) || key.isKeyCode (KeyPress::leftKey))
  360. {
  361. int index = getSelectedItemIndex() - 1;
  362. while (index >= 0 && ! selectIfEnabled (index))
  363. --index;
  364. return true;
  365. }
  366. else if (key.isKeyCode (KeyPress::downKey) || key.isKeyCode (KeyPress::rightKey))
  367. {
  368. int index = getSelectedItemIndex() + 1;
  369. while (index < getNumItems() && ! selectIfEnabled (index))
  370. ++index;
  371. return true;
  372. }
  373. else if (key.isKeyCode (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. }
  477. const Identifier ComboBox::Ids::tagType ("COMBOBOX");
  478. const Identifier ComboBox::Ids::items ("items");
  479. const Identifier ComboBox::Ids::editable ("editable");
  480. const Identifier ComboBox::Ids::textJustification ("textJustification");
  481. const Identifier ComboBox::Ids::unselectedText ("unselectedText");
  482. const Identifier ComboBox::Ids::noItemsText ("noItemsText");
  483. void ComboBox::refreshFromValueTree (const ValueTree& state, ComponentBuilder&)
  484. {
  485. ComponentBuilder::refreshBasicComponentProperties (*this, state);
  486. {
  487. StringArray items;
  488. items.addLines (state [Ids::items].toString());
  489. items.removeEmptyStrings (true);
  490. StringArray existingItems;
  491. for (int i = 0; i < getNumItems(); ++i)
  492. existingItems.add (getItemText (i));
  493. if (existingItems != items)
  494. {
  495. clear();
  496. for (int i = 0; i < items.size(); ++i)
  497. addItem (items[i], i + 1);
  498. }
  499. }
  500. setEditableText (state [Ids::editable]);
  501. setJustificationType ((int) state [Ids::textJustification]);
  502. setTextWhenNothingSelected (state [Ids::unselectedText].toString());
  503. setTextWhenNoChoicesAvailable (state [Ids::noItemsText].toString());
  504. }