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.

607 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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. ComboBox::ItemInfo::ItemInfo (const String& name_, int itemId_, bool isEnabled_, bool isHeading_)
  21. : name (name_), itemId (itemId_), isEnabled (isEnabled_), isHeading (isHeading_)
  22. {
  23. }
  24. bool ComboBox::ItemInfo::isSeparator() const noexcept
  25. {
  26. return name.isEmpty();
  27. }
  28. bool ComboBox::ItemInfo::isRealItem() const noexcept
  29. {
  30. return ! (isHeading || name.isEmpty());
  31. }
  32. //==============================================================================
  33. ComboBox::ComboBox (const String& name)
  34. : Component (name),
  35. lastCurrentId (0),
  36. isButtonDown (false),
  37. separatorPending (false),
  38. menuActive (false),
  39. noChoicesMessage (TRANS("(no choices)"))
  40. {
  41. setRepaintsOnMouseActivity (true);
  42. ComboBox::lookAndFeelChanged();
  43. currentId.addListener (this);
  44. }
  45. ComboBox::~ComboBox()
  46. {
  47. currentId.removeListener (this);
  48. if (menuActive)
  49. PopupMenu::dismissAllActiveMenus();
  50. label = nullptr;
  51. }
  52. //==============================================================================
  53. void ComboBox::setEditableText (const bool isEditable)
  54. {
  55. if (label->isEditableOnSingleClick() != isEditable || label->isEditableOnDoubleClick() != isEditable)
  56. {
  57. label->setEditable (isEditable, isEditable, false);
  58. setWantsKeyboardFocus (! isEditable);
  59. resized();
  60. }
  61. }
  62. bool ComboBox::isTextEditable() const noexcept
  63. {
  64. return label->isEditable();
  65. }
  66. void ComboBox::setJustificationType (const Justification& justification)
  67. {
  68. label->setJustificationType (justification);
  69. }
  70. Justification ComboBox::getJustificationType() const noexcept
  71. {
  72. return label->getJustificationType();
  73. }
  74. void ComboBox::setTooltip (const String& newTooltip)
  75. {
  76. SettableTooltipClient::setTooltip (newTooltip);
  77. label->setTooltip (newTooltip);
  78. }
  79. //==============================================================================
  80. void ComboBox::addItem (const String& newItemText, const int newItemId)
  81. {
  82. // you can't add empty strings to the list..
  83. jassert (newItemText.isNotEmpty());
  84. // IDs must be non-zero, as zero is used to indicate a lack of selecion.
  85. jassert (newItemId != 0);
  86. // you shouldn't use duplicate item IDs!
  87. jassert (getItemForId (newItemId) == nullptr);
  88. if (newItemText.isNotEmpty() && newItemId != 0)
  89. {
  90. if (separatorPending)
  91. {
  92. separatorPending = false;
  93. items.add (new ItemInfo (String::empty, 0, false, false));
  94. }
  95. items.add (new ItemInfo (newItemText, newItemId, true, false));
  96. }
  97. }
  98. void ComboBox::addItemList (const StringArray& items, const int firstItemIdOffset)
  99. {
  100. for (int i = 0; i < items.size(); ++i)
  101. addItem (items[i], i + firstItemIdOffset);
  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