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.

447 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. editSingleClick (false),
  26. editDoubleClick (false),
  27. lossOfFocusDiscardsChanges (false)
  28. {
  29. setColour (TextEditor::textColourId, Colours::black);
  30. setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  31. setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  32. textValue.addListener (this);
  33. }
  34. Label::~Label()
  35. {
  36. textValue.removeListener (this);
  37. if (ownerComponent != nullptr)
  38. ownerComponent->removeComponentListener (this);
  39. editor = nullptr;
  40. }
  41. //==============================================================================
  42. void Label::setText (const String& newText,
  43. const NotificationType notification)
  44. {
  45. hideEditor (true);
  46. if (lastTextValue != newText)
  47. {
  48. lastTextValue = newText;
  49. textValue = newText;
  50. repaint();
  51. textWasChanged();
  52. if (ownerComponent != nullptr)
  53. componentMovedOrResized (*ownerComponent, true, true);
  54. if (notification != dontSendNotification)
  55. callChangeListeners();
  56. }
  57. }
  58. String Label::getText (const bool returnActiveEditorContents) const
  59. {
  60. return (returnActiveEditorContents && isBeingEdited())
  61. ? editor->getText()
  62. : textValue.toString();
  63. }
  64. void Label::valueChanged (Value&)
  65. {
  66. if (lastTextValue != textValue.toString())
  67. setText (textValue.toString(), sendNotification);
  68. }
  69. //==============================================================================
  70. void Label::setFont (const Font& newFont)
  71. {
  72. if (font != newFont)
  73. {
  74. font = newFont;
  75. repaint();
  76. }
  77. }
  78. Font Label::getFont() const noexcept
  79. {
  80. return font;
  81. }
  82. void Label::setEditable (const bool editOnSingleClick,
  83. const bool editOnDoubleClick,
  84. const bool lossOfFocusDiscardsChanges_)
  85. {
  86. editSingleClick = editOnSingleClick;
  87. editDoubleClick = editOnDoubleClick;
  88. lossOfFocusDiscardsChanges = lossOfFocusDiscardsChanges_;
  89. setWantsKeyboardFocus (editOnSingleClick || editOnDoubleClick);
  90. setFocusContainer (editOnSingleClick || editOnDoubleClick);
  91. }
  92. void Label::setJustificationType (Justification newJustification)
  93. {
  94. if (justification != newJustification)
  95. {
  96. justification = newJustification;
  97. repaint();
  98. }
  99. }
  100. void Label::setBorderSize (BorderSize<int> newBorder)
  101. {
  102. if (border != newBorder)
  103. {
  104. border = newBorder;
  105. repaint();
  106. }
  107. }
  108. //==============================================================================
  109. Component* Label::getAttachedComponent() const
  110. {
  111. return static_cast<Component*> (ownerComponent);
  112. }
  113. void Label::attachToComponent (Component* owner, const bool onLeft)
  114. {
  115. if (ownerComponent != nullptr)
  116. ownerComponent->removeComponentListener (this);
  117. ownerComponent = owner;
  118. leftOfOwnerComp = onLeft;
  119. if (ownerComponent != nullptr)
  120. {
  121. setVisible (owner->isVisible());
  122. ownerComponent->addComponentListener (this);
  123. componentParentHierarchyChanged (*ownerComponent);
  124. componentMovedOrResized (*ownerComponent, true, true);
  125. }
  126. }
  127. void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
  128. {
  129. const Font f (getLookAndFeel().getLabelFont (*this));
  130. if (leftOfOwnerComp)
  131. {
  132. setSize (jmin (f.getStringWidth (textValue.toString()) + 8, component.getX()),
  133. component.getHeight());
  134. setTopRightPosition (component.getX(), component.getY());
  135. }
  136. else
  137. {
  138. setSize (component.getWidth(),
  139. 8 + roundToInt (f.getHeight()));
  140. setTopLeftPosition (component.getX(), component.getY() - getHeight());
  141. }
  142. }
  143. void Label::componentParentHierarchyChanged (Component& component)
  144. {
  145. if (Component* parent = component.getParentComponent())
  146. parent->addChildComponent (this);
  147. }
  148. void Label::componentVisibilityChanged (Component& component)
  149. {
  150. setVisible (component.isVisible());
  151. }
  152. //==============================================================================
  153. void Label::textWasEdited() {}
  154. void Label::textWasChanged() {}
  155. void Label::editorShown (TextEditor*) {}
  156. void Label::editorAboutToBeHidden (TextEditor*)
  157. {
  158. if (ComponentPeer* const peer = getPeer())
  159. peer->dismissPendingTextInput();
  160. }
  161. void Label::showEditor()
  162. {
  163. if (editor == nullptr)
  164. {
  165. addAndMakeVisible (editor = createEditorComponent());
  166. editor->setText (getText(), false);
  167. editor->addListener (this);
  168. editor->grabKeyboardFocus();
  169. if (editor == nullptr) // may be deleted by a callback
  170. return;
  171. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  172. resized();
  173. repaint();
  174. editorShown (editor);
  175. enterModalState (false);
  176. editor->grabKeyboardFocus();
  177. }
  178. }
  179. bool Label::updateFromTextEditorContents (TextEditor& ed)
  180. {
  181. const String newText (ed.getText());
  182. if (textValue.toString() != newText)
  183. {
  184. lastTextValue = newText;
  185. textValue = newText;
  186. repaint();
  187. textWasChanged();
  188. if (ownerComponent != nullptr)
  189. componentMovedOrResized (*ownerComponent, true, true);
  190. return true;
  191. }
  192. return false;
  193. }
  194. void Label::hideEditor (const bool discardCurrentEditorContents)
  195. {
  196. if (editor != nullptr)
  197. {
  198. WeakReference<Component> deletionChecker (this);
  199. ScopedPointer<TextEditor> outgoingEditor (editor);
  200. editorAboutToBeHidden (outgoingEditor);
  201. const bool changed = (! discardCurrentEditorContents)
  202. && updateFromTextEditorContents (*outgoingEditor);
  203. outgoingEditor = nullptr;
  204. repaint();
  205. if (changed)
  206. textWasEdited();
  207. if (deletionChecker != nullptr)
  208. exitModalState (0);
  209. if (changed && deletionChecker != nullptr)
  210. callChangeListeners();
  211. }
  212. }
  213. void Label::inputAttemptWhenModal()
  214. {
  215. if (editor != nullptr)
  216. {
  217. if (lossOfFocusDiscardsChanges)
  218. textEditorEscapeKeyPressed (*editor);
  219. else
  220. textEditorReturnKeyPressed (*editor);
  221. }
  222. }
  223. bool Label::isBeingEdited() const noexcept
  224. {
  225. return editor != nullptr;
  226. }
  227. TextEditor* Label::createEditorComponent()
  228. {
  229. TextEditor* const ed = new TextEditor (getName());
  230. ed->applyFontToAllText (getLookAndFeel().getLabelFont (*this));
  231. copyAllExplicitColoursTo (*ed);
  232. return ed;
  233. }
  234. TextEditor* Label::getCurrentTextEditor() const noexcept
  235. {
  236. return editor;
  237. }
  238. //==============================================================================
  239. void Label::paint (Graphics& g)
  240. {
  241. getLookAndFeel().drawLabel (g, *this);
  242. }
  243. void Label::mouseUp (const MouseEvent& e)
  244. {
  245. if (editSingleClick
  246. && e.mouseWasClicked()
  247. && contains (e.getPosition())
  248. && ! e.mods.isPopupMenu())
  249. {
  250. showEditor();
  251. }
  252. }
  253. void Label::mouseDoubleClick (const MouseEvent& e)
  254. {
  255. if (editDoubleClick && ! e.mods.isPopupMenu())
  256. showEditor();
  257. }
  258. void Label::resized()
  259. {
  260. if (editor != nullptr)
  261. editor->setBounds (getLocalBounds());
  262. }
  263. void Label::focusGained (FocusChangeType cause)
  264. {
  265. if (editSingleClick && cause == focusChangedByTabKey)
  266. showEditor();
  267. }
  268. void Label::enablementChanged()
  269. {
  270. repaint();
  271. }
  272. void Label::colourChanged()
  273. {
  274. repaint();
  275. }
  276. void Label::setMinimumHorizontalScale (const float newScale)
  277. {
  278. if (minimumHorizontalScale != newScale)
  279. {
  280. minimumHorizontalScale = newScale;
  281. repaint();
  282. }
  283. }
  284. //==============================================================================
  285. // We'll use a custom focus traverser here to make sure focus goes from the
  286. // text editor to another component rather than back to the label itself.
  287. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  288. {
  289. public:
  290. LabelKeyboardFocusTraverser() {}
  291. Component* getNextComponent (Component* c) { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  292. Component* getPreviousComponent (Component* c) { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  293. static Component* getComp (Component* current)
  294. {
  295. return dynamic_cast <TextEditor*> (current) != nullptr
  296. ? current->getParentComponent() : current;
  297. }
  298. };
  299. KeyboardFocusTraverser* Label::createFocusTraverser()
  300. {
  301. return new LabelKeyboardFocusTraverser();
  302. }
  303. //==============================================================================
  304. void Label::addListener (LabelListener* const listener)
  305. {
  306. listeners.add (listener);
  307. }
  308. void Label::removeListener (LabelListener* const listener)
  309. {
  310. listeners.remove (listener);
  311. }
  312. void Label::callChangeListeners()
  313. {
  314. Component::BailOutChecker checker (this);
  315. listeners.callChecked (checker, &LabelListener::labelTextChanged, this); // (can't use Label::Listener due to idiotic VC2005 bug)
  316. }
  317. //==============================================================================
  318. void Label::textEditorTextChanged (TextEditor& ed)
  319. {
  320. if (editor != nullptr)
  321. {
  322. jassert (&ed == editor);
  323. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  324. {
  325. if (lossOfFocusDiscardsChanges)
  326. textEditorEscapeKeyPressed (ed);
  327. else
  328. textEditorReturnKeyPressed (ed);
  329. }
  330. }
  331. }
  332. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  333. {
  334. if (editor != nullptr)
  335. {
  336. jassert (&ed == editor);
  337. const bool changed = updateFromTextEditorContents (ed);
  338. hideEditor (true);
  339. if (changed)
  340. {
  341. WeakReference<Component> deletionChecker (this);
  342. textWasEdited();
  343. if (deletionChecker != nullptr)
  344. callChangeListeners();
  345. }
  346. }
  347. }
  348. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  349. {
  350. if (editor != nullptr)
  351. {
  352. jassert (&ed == editor);
  353. (void) ed;
  354. editor->setText (textValue.toString(), false);
  355. hideEditor (true);
  356. }
  357. }
  358. void Label::textEditorFocusLost (TextEditor& ed)
  359. {
  360. textEditorTextChanged (ed);
  361. }