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.

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