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.

609 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. #include "../../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_ComboBox.h"
  21. #include "../menus/juce_PopupMenu.h"
  22. #include "../lookandfeel/juce_LookAndFeel.h"
  23. #include "../../../text/juce_LocalisedStrings.h"
  24. //==============================================================================
  25. ComboBox::ItemInfo::ItemInfo (const String& name_, int itemId_, bool isEnabled_, bool isHeading_)
  26. : name (name_), itemId (itemId_), isEnabled (isEnabled_), isHeading (isHeading_)
  27. {
  28. }
  29. bool ComboBox::ItemInfo::isSeparator() const noexcept
  30. {
  31. return name.isEmpty();
  32. }
  33. bool ComboBox::ItemInfo::isRealItem() const noexcept
  34. {
  35. return ! (isHeading || name.isEmpty());
  36. }
  37. //==============================================================================
  38. ComboBox::ComboBox (const String& name)
  39. : Component (name),
  40. lastCurrentId (0),
  41. isButtonDown (false),
  42. separatorPending (false),
  43. menuActive (false),
  44. noChoicesMessage (TRANS("(no choices)"))
  45. {
  46. setRepaintsOnMouseActivity (true);
  47. ComboBox::lookAndFeelChanged();
  48. currentId.addListener (this);
  49. }
  50. ComboBox::~ComboBox()
  51. {
  52. currentId.removeListener (this);
  53. if (menuActive)
  54. PopupMenu::dismissAllActiveMenus();
  55. label = nullptr;
  56. }
  57. //==============================================================================
  58. void ComboBox::setEditableText (const bool isEditable)
  59. {
  60. if (label->isEditableOnSingleClick() != isEditable || label->isEditableOnDoubleClick() != isEditable)
  61. {
  62. label->setEditable (isEditable, isEditable, false);
  63. setWantsKeyboardFocus (! isEditable);
  64. resized();
  65. }
  66. }
  67. bool ComboBox::isTextEditable() const noexcept
  68. {
  69. return label->isEditable();
  70. }
  71. void ComboBox::setJustificationType (const Justification& justification)
  72. {
  73. label->setJustificationType (justification);
  74. }
  75. const Justification ComboBox::getJustificationType() const noexcept
  76. {
  77. return label->getJustificationType();
  78. }
  79. void ComboBox::setTooltip (const String& newTooltip)
  80. {
  81. SettableTooltipClient::setTooltip (newTooltip);
  82. label->setTooltip (newTooltip);
  83. }
  84. //==============================================================================
  85. void ComboBox::addItem (const String& newItemText, const int newItemId)
  86. {
  87. // you can't add empty strings to the list..
  88. jassert (newItemText.isNotEmpty());
  89. // IDs must be non-zero, as zero is used to indicate a lack of selecion.
  90. jassert (newItemId != 0);
  91. // you shouldn't use duplicate item IDs!
  92. jassert (getItemForId (newItemId) == nullptr);
  93. if (newItemText.isNotEmpty() && newItemId != 0)
  94. {
  95. if (separatorPending)
  96. {
  97. separatorPending = false;
  98. items.add (new ItemInfo (String::empty, 0, false, false));
  99. }
  100. items.add (new ItemInfo (newItemText, newItemId, true, false));
  101. }
  102. }
  103. void ComboBox::addSeparator()
  104. {
  105. separatorPending = (items.size() > 0);
  106. }
  107. void ComboBox::addSectionHeading (const String& headingName)
  108. {
  109. // you can't add empty strings to the list..
  110. jassert (headingName.isNotEmpty());
  111. if (headingName.isNotEmpty())
  112. {
  113. if (separatorPending)
  114. {
  115. separatorPending = false;
  116. items.add (new ItemInfo (String::empty, 0, false, false));
  117. }
  118. items.add (new ItemInfo (headingName, 0, true, true));
  119. }
  120. }
  121. void ComboBox::setItemEnabled (const int itemId, const bool shouldBeEnabled)
  122. {
  123. ItemInfo* const item = getItemForId (itemId);
  124. if (item != nullptr)
  125. item->isEnabled = shouldBeEnabled;
  126. }
  127. bool ComboBox::isItemEnabled (int itemId) const noexcept
  128. {
  129. const ItemInfo* const item = getItemForId (itemId);
  130. return item != nullptr && item->isEnabled;
  131. }
  132. void ComboBox::changeItemText (const int itemId, const String& newText)
  133. {
  134. ItemInfo* const item = getItemForId (itemId);
  135. jassert (item != nullptr);
  136. if (item != nullptr)
  137. item->name = newText;
  138. }
  139. void ComboBox::clear (const bool dontSendChangeMessage)
  140. {
  141. items.clear();
  142. separatorPending = false;
  143. if (! label->isEditable())
  144. setSelectedItemIndex (-1, dontSendChangeMessage);
  145. }
  146. //==============================================================================
  147. ComboBox::ItemInfo* ComboBox::getItemForId (const int itemId) const noexcept
  148. {
  149. if (itemId != 0)
  150. {
  151. for (int i = items.size(); --i >= 0;)
  152. if (items.getUnchecked(i)->itemId == itemId)
  153. return items.getUnchecked(i);
  154. }
  155. return nullptr;
  156. }
  157. ComboBox::ItemInfo* ComboBox::getItemForIndex (const int index) const noexcept
  158. {
  159. for (int n = 0, i = 0; i < items.size(); ++i)
  160. {
  161. ItemInfo* const item = items.getUnchecked(i);
  162. if (item->isRealItem())
  163. if (n++ == index)
  164. return item;
  165. }
  166. return nullptr;
  167. }
  168. int ComboBox::getNumItems() const noexcept
  169. {
  170. int n = 0;
  171. for (int i = items.size(); --i >= 0;)
  172. if (items.getUnchecked(i)->isRealItem())
  173. ++n;
  174. return n;
  175. }
  176. String ComboBox::getItemText (const int index) const
  177. {
  178. const ItemInfo* const item = getItemForIndex (index);
  179. return item != nullptr ? item->name : String::empty;
  180. }
  181. int ComboBox::getItemId (const int index) const noexcept
  182. {
  183. const ItemInfo* const item = getItemForIndex (index);
  184. return item != nullptr ? item->itemId : 0;
  185. }
  186. int ComboBox::indexOfItemId (const int itemId) const noexcept
  187. {
  188. for (int n = 0, i = 0; i < items.size(); ++i)
  189. {
  190. const ItemInfo* const item = items.getUnchecked(i);
  191. if (item->isRealItem())
  192. {
  193. if (item->itemId == itemId)
  194. return n;
  195. ++n;
  196. }
  197. }
  198. return -1;
  199. }
  200. //==============================================================================
  201. int ComboBox::getSelectedItemIndex() const
  202. {
  203. int index = indexOfItemId (currentId.getValue());
  204. if (getText() != getItemText (index))
  205. index = -1;
  206. return index;
  207. }
  208. void ComboBox::setSelectedItemIndex (const int index, const bool dontSendChangeMessage)
  209. {
  210. setSelectedId (getItemId (index), dontSendChangeMessage);
  211. }
  212. int ComboBox::getSelectedId() const noexcept
  213. {
  214. const ItemInfo* const item = getItemForId (currentId.getValue());
  215. return (item != nullptr && getText() == item->name) ? item->itemId : 0;
  216. }
  217. void ComboBox::setSelectedId (const int newItemId, const bool dontSendChangeMessage)
  218. {
  219. const ItemInfo* const item = getItemForId (newItemId);
  220. const String newItemText (item != nullptr ? item->name : String::empty);
  221. if (lastCurrentId != newItemId || label->getText() != newItemText)
  222. {
  223. if (! dontSendChangeMessage)
  224. triggerAsyncUpdate();
  225. label->setText (newItemText, false);
  226. lastCurrentId = newItemId;
  227. currentId = newItemId;
  228. repaint(); // for the benefit of the 'none selected' text
  229. }
  230. }
  231. bool ComboBox::selectIfEnabled (const int index)
  232. {
  233. const ItemInfo* const item = getItemForIndex (index);
  234. if (item != nullptr && item->isEnabled)
  235. {
  236. setSelectedItemIndex (index);
  237. return true;
  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, false);
  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,
  312. label->getX() + 2, label->getY() + 1,
  313. label->getWidth() - 4, label->getHeight() - 2,
  314. label->getJustificationType(),
  315. jmax (1, (int) (label->getHeight() / label->getFont().getHeight())));
  316. }
  317. }
  318. void ComboBox::resized()
  319. {
  320. if (getHeight() > 0 && getWidth() > 0)
  321. getLookAndFeel().positionComboBoxText (*this, *label);
  322. }
  323. void ComboBox::enablementChanged()
  324. {
  325. repaint();
  326. }
  327. void ComboBox::lookAndFeelChanged()
  328. {
  329. repaint();
  330. {
  331. ScopedPointer <Label> newLabel (getLookAndFeel().createComboBoxTextBox (*this));
  332. jassert (newLabel != nullptr);
  333. if (label != nullptr)
  334. {
  335. newLabel->setEditable (label->isEditable());
  336. newLabel->setJustificationType (label->getJustificationType());
  337. newLabel->setTooltip (label->getTooltip());
  338. newLabel->setText (label->getText(), false);
  339. }
  340. label = newLabel;
  341. }
  342. addAndMakeVisible (label);
  343. setWantsKeyboardFocus (! label->isEditable());
  344. label->addListener (this);
  345. label->addMouseListener (this, false);
  346. label->setColour (Label::backgroundColourId, Colours::transparentBlack);
  347. label->setColour (Label::textColourId, findColour (ComboBox::textColourId));
  348. label->setColour (TextEditor::textColourId, findColour (ComboBox::textColourId));
  349. label->setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  350. label->setColour (TextEditor::highlightColourId, findColour (TextEditor::highlightColourId));
  351. label->setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  352. resized();
  353. }
  354. void ComboBox::colourChanged()
  355. {
  356. lookAndFeelChanged();
  357. }
  358. //==============================================================================
  359. bool ComboBox::keyPressed (const KeyPress& key)
  360. {
  361. if (key.isKeyCode (KeyPress::upKey) || key.isKeyCode (KeyPress::leftKey))
  362. {
  363. int index = getSelectedItemIndex() - 1;
  364. while (index >= 0 && ! selectIfEnabled (index))
  365. --index;
  366. return true;
  367. }
  368. else if (key.isKeyCode (KeyPress::downKey) || key.isKeyCode (KeyPress::rightKey))
  369. {
  370. int index = getSelectedItemIndex() + 1;
  371. while (index < getNumItems() && ! selectIfEnabled (index))
  372. ++index;
  373. return true;
  374. }
  375. else if (key.isKeyCode (KeyPress::returnKey))
  376. {
  377. showPopup();
  378. return true;
  379. }
  380. return false;
  381. }
  382. bool ComboBox::keyStateChanged (const bool isKeyDown)
  383. {
  384. // only forward key events that aren't used by this component
  385. return isKeyDown
  386. && (KeyPress::isKeyCurrentlyDown (KeyPress::upKey)
  387. || KeyPress::isKeyCurrentlyDown (KeyPress::leftKey)
  388. || KeyPress::isKeyCurrentlyDown (KeyPress::downKey)
  389. || KeyPress::isKeyCurrentlyDown (KeyPress::rightKey));
  390. }
  391. //==============================================================================
  392. void ComboBox::focusGained (FocusChangeType) { repaint(); }
  393. void ComboBox::focusLost (FocusChangeType) { repaint(); }
  394. void ComboBox::labelTextChanged (Label*)
  395. {
  396. triggerAsyncUpdate();
  397. }
  398. //==============================================================================
  399. void ComboBox::popupMenuFinishedCallback (int result, ComboBox* box)
  400. {
  401. if (box != nullptr)
  402. {
  403. box->menuActive = false;
  404. if (result != 0)
  405. box->setSelectedId (result);
  406. }
  407. }
  408. void ComboBox::showPopup()
  409. {
  410. if (! menuActive)
  411. {
  412. const int selectedId = getSelectedId();
  413. PopupMenu menu;
  414. menu.setLookAndFeel (&getLookAndFeel());
  415. for (int i = 0; i < items.size(); ++i)
  416. {
  417. const ItemInfo* const item = items.getUnchecked(i);
  418. if (item->isSeparator())
  419. menu.addSeparator();
  420. else if (item->isHeading)
  421. menu.addSectionHeader (item->name);
  422. else
  423. menu.addItem (item->itemId, item->name,
  424. item->isEnabled, item->itemId == selectedId);
  425. }
  426. if (items.size() == 0)
  427. menu.addItem (1, noChoicesMessage, false);
  428. menuActive = true;
  429. menu.showMenuAsync (PopupMenu::Options().withTargetComponent (this)
  430. .withItemThatMustBeVisible (selectedId)
  431. .withMinimumWidth (getWidth())
  432. .withMaximumNumColumns (1)
  433. .withStandardItemHeight (jlimit (12, 24, getHeight())),
  434. ModalCallbackFunction::forComponent (popupMenuFinishedCallback, this));
  435. }
  436. }
  437. //==============================================================================
  438. void ComboBox::mouseDown (const MouseEvent& e)
  439. {
  440. beginDragAutoRepeat (300);
  441. isButtonDown = isEnabled() && ! e.mods.isPopupMenu();
  442. if (isButtonDown && (e.eventComponent == this || ! label->isEditable()))
  443. showPopup();
  444. }
  445. void ComboBox::mouseDrag (const MouseEvent& e)
  446. {
  447. beginDragAutoRepeat (50);
  448. if (isButtonDown && ! e.mouseWasClicked())
  449. showPopup();
  450. }
  451. void ComboBox::mouseUp (const MouseEvent& e2)
  452. {
  453. if (isButtonDown)
  454. {
  455. isButtonDown = false;
  456. repaint();
  457. const MouseEvent e (e2.getEventRelativeTo (this));
  458. if (reallyContains (e.getPosition(), true)
  459. && (e2.eventComponent == this || ! label->isEditable()))
  460. {
  461. showPopup();
  462. }
  463. }
  464. }
  465. //==============================================================================
  466. void ComboBox::addListener (ComboBoxListener* const listener)
  467. {
  468. listeners.add (listener);
  469. }
  470. void ComboBox::removeListener (ComboBoxListener* const listener)
  471. {
  472. listeners.remove (listener);
  473. }
  474. void ComboBox::handleAsyncUpdate()
  475. {
  476. Component::BailOutChecker checker (this);
  477. listeners.callChecked (checker, &ComboBoxListener::comboBoxChanged, this); // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  478. }
  479. END_JUCE_NAMESPACE