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.

471 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. Label::Label (const String& name, const String& labelText)
  18. : Component (name),
  19. textValue (labelText),
  20. lastTextValue (labelText),
  21. font (15.0f),
  22. justification (Justification::centredLeft),
  23. border (1, 5, 1, 5),
  24. minimumHorizontalScale (0.0f),
  25. keyboardType (TextEditor::textKeyboard),
  26. editSingleClick (false),
  27. editDoubleClick (false),
  28. lossOfFocusDiscardsChanges (false)
  29. {
  30. setColour (TextEditor::textColourId, Colours::black);
  31. setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  32. setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  33. textValue.addListener (this);
  34. }
  35. Label::~Label()
  36. {
  37. textValue.removeListener (this);
  38. if (ownerComponent != nullptr)
  39. ownerComponent->removeComponentListener (this);
  40. editor = nullptr;
  41. }
  42. //==============================================================================
  43. void Label::setText (const String& newText,
  44. const NotificationType notification)
  45. {
  46. hideEditor (true);
  47. if (lastTextValue != newText)
  48. {
  49. lastTextValue = newText;
  50. textValue = newText;
  51. repaint();
  52. textWasChanged();
  53. if (ownerComponent != nullptr)
  54. componentMovedOrResized (*ownerComponent, true, true);
  55. if (notification != dontSendNotification)
  56. callChangeListeners();
  57. }
  58. }
  59. String Label::getText (const bool returnActiveEditorContents) const
  60. {
  61. return (returnActiveEditorContents && isBeingEdited())
  62. ? editor->getText()
  63. : textValue.toString();
  64. }
  65. void Label::valueChanged (Value&)
  66. {
  67. if (lastTextValue != textValue.toString())
  68. setText (textValue.toString(), sendNotification);
  69. }
  70. //==============================================================================
  71. void Label::setFont (const Font& newFont)
  72. {
  73. if (font != newFont)
  74. {
  75. font = newFont;
  76. repaint();
  77. }
  78. }
  79. Font Label::getFont() const noexcept
  80. {
  81. return font;
  82. }
  83. void Label::setEditable (const bool editOnSingleClick,
  84. const bool editOnDoubleClick,
  85. const bool lossOfFocusDiscardsChanges_)
  86. {
  87. editSingleClick = editOnSingleClick;
  88. editDoubleClick = editOnDoubleClick;
  89. lossOfFocusDiscardsChanges = lossOfFocusDiscardsChanges_;
  90. setWantsKeyboardFocus (editOnSingleClick || editOnDoubleClick);
  91. setFocusContainer (editOnSingleClick || editOnDoubleClick);
  92. }
  93. void Label::setJustificationType (Justification newJustification)
  94. {
  95. if (justification != newJustification)
  96. {
  97. justification = newJustification;
  98. repaint();
  99. }
  100. }
  101. void Label::setBorderSize (BorderSize<int> newBorder)
  102. {
  103. if (border != newBorder)
  104. {
  105. border = newBorder;
  106. repaint();
  107. }
  108. }
  109. //==============================================================================
  110. Component* Label::getAttachedComponent() const
  111. {
  112. return static_cast<Component*> (ownerComponent);
  113. }
  114. void Label::attachToComponent (Component* owner, const bool onLeft)
  115. {
  116. if (ownerComponent != nullptr)
  117. ownerComponent->removeComponentListener (this);
  118. ownerComponent = owner;
  119. leftOfOwnerComp = onLeft;
  120. if (ownerComponent != nullptr)
  121. {
  122. setVisible (owner->isVisible());
  123. ownerComponent->addComponentListener (this);
  124. componentParentHierarchyChanged (*ownerComponent);
  125. componentMovedOrResized (*ownerComponent, true, true);
  126. }
  127. }
  128. void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
  129. {
  130. const Font f (getLookAndFeel().getLabelFont (*this));
  131. if (leftOfOwnerComp)
  132. {
  133. setSize (jmin (roundToInt (f.getStringWidthFloat (textValue.toString()) + 0.5f) + getBorderSize().getLeftAndRight(),
  134. component.getX()),
  135. component.getHeight());
  136. setTopRightPosition (component.getX(), component.getY());
  137. }
  138. else
  139. {
  140. setSize (component.getWidth(),
  141. getBorderSize().getTopAndBottom() + 6 + roundToInt (f.getHeight() + 0.5f));
  142. setTopLeftPosition (component.getX(), component.getY() - getHeight());
  143. }
  144. }
  145. void Label::componentParentHierarchyChanged (Component& component)
  146. {
  147. if (Component* parent = component.getParentComponent())
  148. parent->addChildComponent (this);
  149. }
  150. void Label::componentVisibilityChanged (Component& component)
  151. {
  152. setVisible (component.isVisible());
  153. }
  154. //==============================================================================
  155. void Label::textWasEdited() {}
  156. void Label::textWasChanged() {}
  157. void Label::editorShown (TextEditor* textEditor)
  158. {
  159. Component::BailOutChecker checker (this);
  160. listeners.callChecked (checker, &LabelListener::editorShown, this, *textEditor);
  161. }
  162. void Label::editorAboutToBeHidden (TextEditor*)
  163. {
  164. if (ComponentPeer* const peer = getPeer())
  165. peer->dismissPendingTextInput();
  166. }
  167. void Label::showEditor()
  168. {
  169. if (editor == nullptr)
  170. {
  171. addAndMakeVisible (editor = createEditorComponent());
  172. editor->setText (getText(), false);
  173. editor->setKeyboardType (keyboardType);
  174. editor->addListener (this);
  175. editor->grabKeyboardFocus();
  176. if (editor == nullptr) // may be deleted by a callback
  177. return;
  178. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  179. resized();
  180. repaint();
  181. editorShown (editor);
  182. enterModalState (false);
  183. editor->grabKeyboardFocus();
  184. }
  185. }
  186. bool Label::updateFromTextEditorContents (TextEditor& ed)
  187. {
  188. const String newText (ed.getText());
  189. if (textValue.toString() != newText)
  190. {
  191. lastTextValue = newText;
  192. textValue = newText;
  193. repaint();
  194. textWasChanged();
  195. if (ownerComponent != nullptr)
  196. componentMovedOrResized (*ownerComponent, true, true);
  197. return true;
  198. }
  199. return false;
  200. }
  201. void Label::hideEditor (const bool discardCurrentEditorContents)
  202. {
  203. if (editor != nullptr)
  204. {
  205. WeakReference<Component> deletionChecker (this);
  206. ScopedPointer<TextEditor> outgoingEditor (editor);
  207. editorAboutToBeHidden (outgoingEditor);
  208. const bool changed = (! discardCurrentEditorContents)
  209. && updateFromTextEditorContents (*outgoingEditor);
  210. outgoingEditor = nullptr;
  211. repaint();
  212. if (changed)
  213. textWasEdited();
  214. if (deletionChecker != nullptr)
  215. exitModalState (0);
  216. if (changed && deletionChecker != nullptr)
  217. callChangeListeners();
  218. }
  219. }
  220. void Label::inputAttemptWhenModal()
  221. {
  222. if (editor != nullptr)
  223. {
  224. if (lossOfFocusDiscardsChanges)
  225. textEditorEscapeKeyPressed (*editor);
  226. else
  227. textEditorReturnKeyPressed (*editor);
  228. }
  229. }
  230. bool Label::isBeingEdited() const noexcept
  231. {
  232. return editor != nullptr;
  233. }
  234. static void copyColourIfSpecified (Label& l, TextEditor& ed, int colourID, int targetColourID)
  235. {
  236. if (l.isColourSpecified (colourID) || l.getLookAndFeel().isColourSpecified (colourID))
  237. ed.setColour (targetColourID, l.findColour (colourID));
  238. }
  239. TextEditor* Label::createEditorComponent()
  240. {
  241. TextEditor* const ed = new TextEditor (getName());
  242. ed->applyFontToAllText (getLookAndFeel().getLabelFont (*this));
  243. copyAllExplicitColoursTo (*ed);
  244. copyColourIfSpecified (*this, *ed, textWhenEditingColourId, TextEditor::textColourId);
  245. copyColourIfSpecified (*this, *ed, backgroundWhenEditingColourId, TextEditor::backgroundColourId);
  246. copyColourIfSpecified (*this, *ed, outlineWhenEditingColourId, TextEditor::outlineColourId);
  247. return ed;
  248. }
  249. TextEditor* Label::getCurrentTextEditor() const noexcept
  250. {
  251. return editor;
  252. }
  253. //==============================================================================
  254. void Label::paint (Graphics& g)
  255. {
  256. getLookAndFeel().drawLabel (g, *this);
  257. }
  258. void Label::mouseUp (const MouseEvent& e)
  259. {
  260. if (editSingleClick
  261. && isEnabled()
  262. && e.mouseWasClicked()
  263. && contains (e.getPosition())
  264. && ! e.mods.isPopupMenu())
  265. {
  266. showEditor();
  267. }
  268. }
  269. void Label::mouseDoubleClick (const MouseEvent& e)
  270. {
  271. if (editDoubleClick
  272. && isEnabled()
  273. && ! e.mods.isPopupMenu())
  274. showEditor();
  275. }
  276. void Label::resized()
  277. {
  278. if (editor != nullptr)
  279. editor->setBounds (getLocalBounds());
  280. }
  281. void Label::focusGained (FocusChangeType cause)
  282. {
  283. if (editSingleClick
  284. && isEnabled()
  285. && cause == focusChangedByTabKey)
  286. showEditor();
  287. }
  288. void Label::enablementChanged()
  289. {
  290. repaint();
  291. }
  292. void Label::colourChanged()
  293. {
  294. repaint();
  295. }
  296. void Label::setMinimumHorizontalScale (const float newScale)
  297. {
  298. if (minimumHorizontalScale != newScale)
  299. {
  300. minimumHorizontalScale = newScale;
  301. repaint();
  302. }
  303. }
  304. //==============================================================================
  305. // We'll use a custom focus traverser here to make sure focus goes from the
  306. // text editor to another component rather than back to the label itself.
  307. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  308. {
  309. public:
  310. LabelKeyboardFocusTraverser() {}
  311. Component* getNextComponent (Component* c) { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  312. Component* getPreviousComponent (Component* c) { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  313. static Component* getComp (Component* current)
  314. {
  315. return dynamic_cast <TextEditor*> (current) != nullptr
  316. ? current->getParentComponent() : current;
  317. }
  318. };
  319. KeyboardFocusTraverser* Label::createFocusTraverser()
  320. {
  321. return new LabelKeyboardFocusTraverser();
  322. }
  323. //==============================================================================
  324. void Label::addListener (LabelListener* const listener)
  325. {
  326. listeners.add (listener);
  327. }
  328. void Label::removeListener (LabelListener* const listener)
  329. {
  330. listeners.remove (listener);
  331. }
  332. void Label::callChangeListeners()
  333. {
  334. Component::BailOutChecker checker (this);
  335. listeners.callChecked (checker, &LabelListener::labelTextChanged, this); // (can't use Label::Listener due to idiotic VC2005 bug)
  336. }
  337. //==============================================================================
  338. void Label::textEditorTextChanged (TextEditor& ed)
  339. {
  340. if (editor != nullptr)
  341. {
  342. jassert (&ed == editor);
  343. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  344. {
  345. if (lossOfFocusDiscardsChanges)
  346. textEditorEscapeKeyPressed (ed);
  347. else
  348. textEditorReturnKeyPressed (ed);
  349. }
  350. }
  351. }
  352. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  353. {
  354. if (editor != nullptr)
  355. {
  356. jassert (&ed == editor);
  357. const bool changed = updateFromTextEditorContents (ed);
  358. hideEditor (true);
  359. if (changed)
  360. {
  361. WeakReference<Component> deletionChecker (this);
  362. textWasEdited();
  363. if (deletionChecker != nullptr)
  364. callChangeListeners();
  365. }
  366. }
  367. }
  368. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  369. {
  370. if (editor != nullptr)
  371. {
  372. jassert (&ed == editor);
  373. (void) ed;
  374. editor->setText (textValue.toString(), false);
  375. hideEditor (true);
  376. }
  377. }
  378. void Label::textEditorFocusLost (TextEditor& ed)
  379. {
  380. textEditorTextChanged (ed);
  381. }