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.

599 lines
17KB

  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, 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(), false);
  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. }