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.

601 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. const 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::addSeparator()
  99. {
  100. separatorPending = (items.size() > 0);
  101. }
  102. void ComboBox::addSectionHeading (const String& headingName)
  103. {
  104. // you can't add empty strings to the list..
  105. jassert (headingName.isNotEmpty());
  106. if (headingName.isNotEmpty())
  107. {
  108. if (separatorPending)
  109. {
  110. separatorPending = false;
  111. items.add (new ItemInfo (String::empty, 0, false, false));
  112. }
  113. items.add (new ItemInfo (headingName, 0, true, true));
  114. }
  115. }
  116. void ComboBox::setItemEnabled (const int itemId, const bool shouldBeEnabled)
  117. {
  118. ItemInfo* const item = getItemForId (itemId);
  119. if (item != nullptr)
  120. item->isEnabled = shouldBeEnabled;
  121. }
  122. bool ComboBox::isItemEnabled (int itemId) const noexcept
  123. {
  124. const ItemInfo* const item = getItemForId (itemId);
  125. return item != nullptr && item->isEnabled;
  126. }
  127. void ComboBox::changeItemText (const int itemId, const String& newText)
  128. {
  129. ItemInfo* const item = getItemForId (itemId);
  130. jassert (item != nullptr);
  131. if (item != nullptr)
  132. item->name = newText;
  133. }
  134. void ComboBox::clear (const bool dontSendChangeMessage)
  135. {
  136. items.clear();
  137. separatorPending = false;
  138. if (! label->isEditable())
  139. setSelectedItemIndex (-1, dontSendChangeMessage);
  140. }
  141. //==============================================================================
  142. ComboBox::ItemInfo* ComboBox::getItemForId (const int itemId) const noexcept
  143. {
  144. if (itemId != 0)
  145. {
  146. for (int i = items.size(); --i >= 0;)
  147. if (items.getUnchecked(i)->itemId == itemId)
  148. return items.getUnchecked(i);
  149. }
  150. return nullptr;
  151. }
  152. ComboBox::ItemInfo* ComboBox::getItemForIndex (const int index) const noexcept
  153. {
  154. for (int n = 0, i = 0; i < items.size(); ++i)
  155. {
  156. ItemInfo* const item = items.getUnchecked(i);
  157. if (item->isRealItem())
  158. if (n++ == index)
  159. return item;
  160. }
  161. return nullptr;
  162. }
  163. int ComboBox::getNumItems() const noexcept
  164. {
  165. int n = 0;
  166. for (int i = items.size(); --i >= 0;)
  167. if (items.getUnchecked(i)->isRealItem())
  168. ++n;
  169. return n;
  170. }
  171. String ComboBox::getItemText (const int index) const
  172. {
  173. const ItemInfo* const item = getItemForIndex (index);
  174. return item != nullptr ? item->name : String::empty;
  175. }
  176. int ComboBox::getItemId (const int index) const noexcept
  177. {
  178. const ItemInfo* const item = getItemForIndex (index);
  179. return item != nullptr ? item->itemId : 0;
  180. }
  181. int ComboBox::indexOfItemId (const int itemId) const noexcept
  182. {
  183. for (int n = 0, i = 0; i < items.size(); ++i)
  184. {
  185. const ItemInfo* const item = items.getUnchecked(i);
  186. if (item->isRealItem())
  187. {
  188. if (item->itemId == itemId)
  189. return n;
  190. ++n;
  191. }
  192. }
  193. return -1;
  194. }
  195. //==============================================================================
  196. int ComboBox::getSelectedItemIndex() const
  197. {
  198. int index = indexOfItemId (currentId.getValue());
  199. if (getText() != getItemText (index))
  200. index = -1;
  201. return index;
  202. }
  203. void ComboBox::setSelectedItemIndex (const int index, const bool dontSendChangeMessage)
  204. {
  205. setSelectedId (getItemId (index), dontSendChangeMessage);
  206. }
  207. int ComboBox::getSelectedId() const noexcept
  208. {
  209. const ItemInfo* const item = getItemForId (currentId.getValue());
  210. return (item != nullptr && getText() == item->name) ? item->itemId : 0;
  211. }
  212. void ComboBox::setSelectedId (const int newItemId, const bool dontSendChangeMessage)
  213. {
  214. const ItemInfo* const item = getItemForId (newItemId);
  215. const String newItemText (item != nullptr ? item->name : String::empty);
  216. if (lastCurrentId != newItemId || label->getText() != newItemText)
  217. {
  218. if (! dontSendChangeMessage)
  219. triggerAsyncUpdate();
  220. label->setText (newItemText, false);
  221. lastCurrentId = newItemId;
  222. currentId = newItemId;
  223. repaint(); // for the benefit of the 'none selected' text
  224. }
  225. }
  226. bool ComboBox::selectIfEnabled (const int index)
  227. {
  228. const ItemInfo* const item = getItemForIndex (index);
  229. if (item != nullptr && item->isEnabled)
  230. {
  231. setSelectedItemIndex (index);
  232. return true;
  233. }
  234. return false;
  235. }
  236. void ComboBox::valueChanged (Value&)
  237. {
  238. if (lastCurrentId != (int) currentId.getValue())
  239. setSelectedId (currentId.getValue(), false);
  240. }
  241. //==============================================================================
  242. String ComboBox::getText() const
  243. {
  244. return label->getText();
  245. }
  246. void ComboBox::setText (const String& newText, const bool dontSendChangeMessage)
  247. {
  248. for (int i = items.size(); --i >= 0;)
  249. {
  250. const ItemInfo* const item = items.getUnchecked(i);
  251. if (item->isRealItem()
  252. && item->name == newText)
  253. {
  254. setSelectedId (item->itemId, dontSendChangeMessage);
  255. return;
  256. }
  257. }
  258. lastCurrentId = 0;
  259. currentId = 0;
  260. if (label->getText() != newText)
  261. {
  262. label->setText (newText, false);
  263. if (! dontSendChangeMessage)
  264. triggerAsyncUpdate();
  265. }
  266. repaint();
  267. }
  268. void ComboBox::showEditor()
  269. {
  270. jassert (isTextEditable()); // you probably shouldn't do this to a non-editable combo box?
  271. label->showEditor();
  272. }
  273. //==============================================================================
  274. void ComboBox::setTextWhenNothingSelected (const String& newMessage)
  275. {
  276. if (textWhenNothingSelected != newMessage)
  277. {
  278. textWhenNothingSelected = newMessage;
  279. repaint();
  280. }
  281. }
  282. String ComboBox::getTextWhenNothingSelected() const
  283. {
  284. return textWhenNothingSelected;
  285. }
  286. void ComboBox::setTextWhenNoChoicesAvailable (const String& newMessage)
  287. {
  288. noChoicesMessage = newMessage;
  289. }
  290. String ComboBox::getTextWhenNoChoicesAvailable() const
  291. {
  292. return noChoicesMessage;
  293. }
  294. //==============================================================================
  295. void ComboBox::paint (Graphics& g)
  296. {
  297. getLookAndFeel().drawComboBox (g, getWidth(), getHeight(), isButtonDown,
  298. label->getRight(), 0, getWidth() - label->getRight(), getHeight(),
  299. *this);
  300. if (textWhenNothingSelected.isNotEmpty()
  301. && label->getText().isEmpty()
  302. && ! label->isBeingEdited())
  303. {
  304. g.setColour (findColour (textColourId).withMultipliedAlpha (0.5f));
  305. g.setFont (label->getFont());
  306. g.drawFittedText (textWhenNothingSelected,
  307. label->getX() + 2, label->getY() + 1,
  308. label->getWidth() - 4, label->getHeight() - 2,
  309. label->getJustificationType(),
  310. jmax (1, (int) (label->getHeight() / label->getFont().getHeight())));
  311. }
  312. }
  313. void ComboBox::resized()
  314. {
  315. if (getHeight() > 0 && getWidth() > 0)
  316. getLookAndFeel().positionComboBoxText (*this, *label);
  317. }
  318. void ComboBox::enablementChanged()
  319. {
  320. repaint();
  321. }
  322. void ComboBox::lookAndFeelChanged()
  323. {
  324. repaint();
  325. {
  326. ScopedPointer <Label> newLabel (getLookAndFeel().createComboBoxTextBox (*this));
  327. jassert (newLabel != nullptr);
  328. if (label != nullptr)
  329. {
  330. newLabel->setEditable (label->isEditable());
  331. newLabel->setJustificationType (label->getJustificationType());
  332. newLabel->setTooltip (label->getTooltip());
  333. newLabel->setText (label->getText(), false);
  334. }
  335. label = newLabel;
  336. }
  337. addAndMakeVisible (label);
  338. setWantsKeyboardFocus (! label->isEditable());
  339. label->addListener (this);
  340. label->addMouseListener (this, false);
  341. label->setColour (Label::backgroundColourId, Colours::transparentBlack);
  342. label->setColour (Label::textColourId, findColour (ComboBox::textColourId));
  343. label->setColour (TextEditor::textColourId, findColour (ComboBox::textColourId));
  344. label->setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  345. label->setColour (TextEditor::highlightColourId, findColour (TextEditor::highlightColourId));
  346. label->setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  347. resized();
  348. }
  349. void ComboBox::colourChanged()
  350. {
  351. lookAndFeelChanged();
  352. }
  353. //==============================================================================
  354. bool ComboBox::keyPressed (const KeyPress& key)
  355. {
  356. if (key.isKeyCode (KeyPress::upKey) || key.isKeyCode (KeyPress::leftKey))
  357. {
  358. int index = getSelectedItemIndex() - 1;
  359. while (index >= 0 && ! selectIfEnabled (index))
  360. --index;
  361. return true;
  362. }
  363. else if (key.isKeyCode (KeyPress::downKey) || key.isKeyCode (KeyPress::rightKey))
  364. {
  365. int index = getSelectedItemIndex() + 1;
  366. while (index < getNumItems() && ! selectIfEnabled (index))
  367. ++index;
  368. return true;
  369. }
  370. else if (key.isKeyCode (KeyPress::returnKey))
  371. {
  372. showPopup();
  373. return true;
  374. }
  375. return false;
  376. }
  377. bool ComboBox::keyStateChanged (const bool isKeyDown)
  378. {
  379. // only forward key events that aren't used by this component
  380. return isKeyDown
  381. && (KeyPress::isKeyCurrentlyDown (KeyPress::upKey)
  382. || KeyPress::isKeyCurrentlyDown (KeyPress::leftKey)
  383. || KeyPress::isKeyCurrentlyDown (KeyPress::downKey)
  384. || KeyPress::isKeyCurrentlyDown (KeyPress::rightKey));
  385. }
  386. //==============================================================================
  387. void ComboBox::focusGained (FocusChangeType) { repaint(); }
  388. void ComboBox::focusLost (FocusChangeType) { repaint(); }
  389. void ComboBox::labelTextChanged (Label*)
  390. {
  391. triggerAsyncUpdate();
  392. }
  393. //==============================================================================
  394. void ComboBox::popupMenuFinishedCallback (int result, ComboBox* box)
  395. {
  396. if (box != nullptr)
  397. {
  398. box->menuActive = false;
  399. if (result != 0)
  400. box->setSelectedId (result);
  401. }
  402. }
  403. void ComboBox::showPopup()
  404. {
  405. if (! menuActive)
  406. {
  407. const int selectedId = getSelectedId();
  408. PopupMenu menu;
  409. menu.setLookAndFeel (&getLookAndFeel());
  410. for (int i = 0; i < items.size(); ++i)
  411. {
  412. const ItemInfo* const item = items.getUnchecked(i);
  413. if (item->isSeparator())
  414. menu.addSeparator();
  415. else if (item->isHeading)
  416. menu.addSectionHeader (item->name);
  417. else
  418. menu.addItem (item->itemId, item->name,
  419. item->isEnabled, item->itemId == selectedId);
  420. }
  421. if (items.size() == 0)
  422. menu.addItem (1, noChoicesMessage, false);
  423. menuActive = true;
  424. menu.showMenuAsync (PopupMenu::Options().withTargetComponent (this)
  425. .withItemThatMustBeVisible (selectedId)
  426. .withMinimumWidth (getWidth())
  427. .withMaximumNumColumns (1)
  428. .withStandardItemHeight (jlimit (12, 24, getHeight())),
  429. ModalCallbackFunction::forComponent (popupMenuFinishedCallback, this));
  430. }
  431. }
  432. //==============================================================================
  433. void ComboBox::mouseDown (const MouseEvent& e)
  434. {
  435. beginDragAutoRepeat (300);
  436. isButtonDown = isEnabled() && ! e.mods.isPopupMenu();
  437. if (isButtonDown && (e.eventComponent == this || ! label->isEditable()))
  438. showPopup();
  439. }
  440. void ComboBox::mouseDrag (const MouseEvent& e)
  441. {
  442. beginDragAutoRepeat (50);
  443. if (isButtonDown && ! e.mouseWasClicked())
  444. showPopup();
  445. }
  446. void ComboBox::mouseUp (const MouseEvent& e2)
  447. {
  448. if (isButtonDown)
  449. {
  450. isButtonDown = false;
  451. repaint();
  452. const MouseEvent e (e2.getEventRelativeTo (this));
  453. if (reallyContains (e.getPosition(), true)
  454. && (e2.eventComponent == this || ! label->isEditable()))
  455. {
  456. showPopup();
  457. }
  458. }
  459. }
  460. //==============================================================================
  461. void ComboBox::addListener (ComboBoxListener* const listener)
  462. {
  463. listeners.add (listener);
  464. }
  465. void ComboBox::removeListener (ComboBoxListener* const listener)
  466. {
  467. listeners.remove (listener);
  468. }
  469. void ComboBox::handleAsyncUpdate()
  470. {
  471. Component::BailOutChecker checker (this);
  472. listeners.callChecked (checker, &ComboBoxListener::comboBoxChanged, this); // (can't use ComboBox::Listener due to idiotic VC2005 bug)
  473. }
  474. END_JUCE_NAMESPACE