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.

465 lines
12KB

  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.7f),
  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 (f.getStringWidth (textValue.toString()) + 8, component.getX()),
  134. component.getHeight());
  135. setTopRightPosition (component.getX(), component.getY());
  136. }
  137. else
  138. {
  139. setSize (component.getWidth(),
  140. 8 + roundToInt (f.getHeight()));
  141. setTopLeftPosition (component.getX(), component.getY() - getHeight());
  142. }
  143. }
  144. void Label::componentParentHierarchyChanged (Component& component)
  145. {
  146. if (Component* parent = component.getParentComponent())
  147. parent->addChildComponent (this);
  148. }
  149. void Label::componentVisibilityChanged (Component& component)
  150. {
  151. setVisible (component.isVisible());
  152. }
  153. //==============================================================================
  154. void Label::textWasEdited() {}
  155. void Label::textWasChanged() {}
  156. void Label::editorShown (TextEditor* textEditor)
  157. {
  158. Component::BailOutChecker checker (this);
  159. listeners.callChecked (checker, &LabelListener::editorShown, this, *textEditor);
  160. }
  161. void Label::editorAboutToBeHidden (TextEditor*)
  162. {
  163. if (ComponentPeer* const peer = getPeer())
  164. peer->dismissPendingTextInput();
  165. }
  166. void Label::showEditor()
  167. {
  168. if (editor == nullptr)
  169. {
  170. addAndMakeVisible (editor = createEditorComponent());
  171. editor->setText (getText(), false);
  172. editor->setKeyboardType (keyboardType);
  173. editor->addListener (this);
  174. editor->grabKeyboardFocus();
  175. if (editor == nullptr) // may be deleted by a callback
  176. return;
  177. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  178. resized();
  179. repaint();
  180. editorShown (editor);
  181. enterModalState (false);
  182. editor->grabKeyboardFocus();
  183. }
  184. }
  185. bool Label::updateFromTextEditorContents (TextEditor& ed)
  186. {
  187. const String newText (ed.getText());
  188. if (textValue.toString() != newText)
  189. {
  190. lastTextValue = newText;
  191. textValue = newText;
  192. repaint();
  193. textWasChanged();
  194. if (ownerComponent != nullptr)
  195. componentMovedOrResized (*ownerComponent, true, true);
  196. return true;
  197. }
  198. return false;
  199. }
  200. void Label::hideEditor (const bool discardCurrentEditorContents)
  201. {
  202. if (editor != nullptr)
  203. {
  204. WeakReference<Component> deletionChecker (this);
  205. ScopedPointer<TextEditor> outgoingEditor (editor);
  206. editorAboutToBeHidden (outgoingEditor);
  207. const bool changed = (! discardCurrentEditorContents)
  208. && updateFromTextEditorContents (*outgoingEditor);
  209. outgoingEditor = nullptr;
  210. repaint();
  211. if (changed)
  212. textWasEdited();
  213. if (deletionChecker != nullptr)
  214. exitModalState (0);
  215. if (changed && deletionChecker != nullptr)
  216. callChangeListeners();
  217. }
  218. }
  219. void Label::inputAttemptWhenModal()
  220. {
  221. if (editor != nullptr)
  222. {
  223. if (lossOfFocusDiscardsChanges)
  224. textEditorEscapeKeyPressed (*editor);
  225. else
  226. textEditorReturnKeyPressed (*editor);
  227. }
  228. }
  229. bool Label::isBeingEdited() const noexcept
  230. {
  231. return editor != nullptr;
  232. }
  233. static void copyColourIfSpecified (Label& l, TextEditor& ed, int colourID, int targetColourID)
  234. {
  235. if (l.isColourSpecified (colourID) || l.getLookAndFeel().isColourSpecified (colourID))
  236. ed.setColour (targetColourID, l.findColour (colourID));
  237. }
  238. TextEditor* Label::createEditorComponent()
  239. {
  240. TextEditor* const ed = new TextEditor (getName());
  241. ed->applyFontToAllText (getLookAndFeel().getLabelFont (*this));
  242. copyAllExplicitColoursTo (*ed);
  243. copyColourIfSpecified (*this, *ed, textWhenEditingColourId, TextEditor::textColourId);
  244. copyColourIfSpecified (*this, *ed, backgroundWhenEditingColourId, TextEditor::backgroundColourId);
  245. copyColourIfSpecified (*this, *ed, outlineWhenEditingColourId, TextEditor::outlineColourId);
  246. return ed;
  247. }
  248. TextEditor* Label::getCurrentTextEditor() const noexcept
  249. {
  250. return editor;
  251. }
  252. //==============================================================================
  253. void Label::paint (Graphics& g)
  254. {
  255. getLookAndFeel().drawLabel (g, *this);
  256. }
  257. void Label::mouseUp (const MouseEvent& e)
  258. {
  259. if (editSingleClick
  260. && e.mouseWasClicked()
  261. && contains (e.getPosition())
  262. && ! e.mods.isPopupMenu())
  263. {
  264. showEditor();
  265. }
  266. }
  267. void Label::mouseDoubleClick (const MouseEvent& e)
  268. {
  269. if (editDoubleClick && ! e.mods.isPopupMenu())
  270. showEditor();
  271. }
  272. void Label::resized()
  273. {
  274. if (editor != nullptr)
  275. editor->setBounds (getLocalBounds());
  276. }
  277. void Label::focusGained (FocusChangeType cause)
  278. {
  279. if (editSingleClick && cause == focusChangedByTabKey)
  280. showEditor();
  281. }
  282. void Label::enablementChanged()
  283. {
  284. repaint();
  285. }
  286. void Label::colourChanged()
  287. {
  288. repaint();
  289. }
  290. void Label::setMinimumHorizontalScale (const float newScale)
  291. {
  292. if (minimumHorizontalScale != newScale)
  293. {
  294. minimumHorizontalScale = newScale;
  295. repaint();
  296. }
  297. }
  298. //==============================================================================
  299. // We'll use a custom focus traverser here to make sure focus goes from the
  300. // text editor to another component rather than back to the label itself.
  301. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  302. {
  303. public:
  304. LabelKeyboardFocusTraverser() {}
  305. Component* getNextComponent (Component* c) { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  306. Component* getPreviousComponent (Component* c) { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  307. static Component* getComp (Component* current)
  308. {
  309. return dynamic_cast <TextEditor*> (current) != nullptr
  310. ? current->getParentComponent() : current;
  311. }
  312. };
  313. KeyboardFocusTraverser* Label::createFocusTraverser()
  314. {
  315. return new LabelKeyboardFocusTraverser();
  316. }
  317. //==============================================================================
  318. void Label::addListener (LabelListener* const listener)
  319. {
  320. listeners.add (listener);
  321. }
  322. void Label::removeListener (LabelListener* const listener)
  323. {
  324. listeners.remove (listener);
  325. }
  326. void Label::callChangeListeners()
  327. {
  328. Component::BailOutChecker checker (this);
  329. listeners.callChecked (checker, &LabelListener::labelTextChanged, this); // (can't use Label::Listener due to idiotic VC2005 bug)
  330. }
  331. //==============================================================================
  332. void Label::textEditorTextChanged (TextEditor& ed)
  333. {
  334. if (editor != nullptr)
  335. {
  336. jassert (&ed == editor);
  337. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  338. {
  339. if (lossOfFocusDiscardsChanges)
  340. textEditorEscapeKeyPressed (ed);
  341. else
  342. textEditorReturnKeyPressed (ed);
  343. }
  344. }
  345. }
  346. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  347. {
  348. if (editor != nullptr)
  349. {
  350. jassert (&ed == editor);
  351. const bool changed = updateFromTextEditorContents (ed);
  352. hideEditor (true);
  353. if (changed)
  354. {
  355. WeakReference<Component> deletionChecker (this);
  356. textWasEdited();
  357. if (deletionChecker != nullptr)
  358. callChangeListeners();
  359. }
  360. }
  361. }
  362. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  363. {
  364. if (editor != nullptr)
  365. {
  366. jassert (&ed == editor);
  367. (void) ed;
  368. editor->setText (textValue.toString(), false);
  369. hideEditor (true);
  370. }
  371. }
  372. void Label::textEditorFocusLost (TextEditor& ed)
  373. {
  374. textEditorTextChanged (ed);
  375. }