Audio plugin host https://kx.studio/carla
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.

juce_Label.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. horizontalBorderSize (5),
  24. verticalBorderSize (1),
  25. minimumHorizontalScale (0.7f),
  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 (int h, int v)
  102. {
  103. if (horizontalBorderSize != h || verticalBorderSize != v)
  104. {
  105. horizontalBorderSize = h;
  106. verticalBorderSize = v;
  107. repaint();
  108. }
  109. }
  110. //==============================================================================
  111. Component* Label::getAttachedComponent() const
  112. {
  113. return static_cast<Component*> (ownerComponent);
  114. }
  115. void Label::attachToComponent (Component* owner, const bool onLeft)
  116. {
  117. if (ownerComponent != nullptr)
  118. ownerComponent->removeComponentListener (this);
  119. ownerComponent = owner;
  120. leftOfOwnerComp = onLeft;
  121. if (ownerComponent != nullptr)
  122. {
  123. setVisible (owner->isVisible());
  124. ownerComponent->addComponentListener (this);
  125. componentParentHierarchyChanged (*ownerComponent);
  126. componentMovedOrResized (*ownerComponent, true, true);
  127. }
  128. }
  129. void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
  130. {
  131. const Font f (getLookAndFeel().getLabelFont (*this));
  132. if (leftOfOwnerComp)
  133. {
  134. setSize (jmin (f.getStringWidth (textValue.toString()) + 8, component.getX()),
  135. component.getHeight());
  136. setTopRightPosition (component.getX(), component.getY());
  137. }
  138. else
  139. {
  140. setSize (component.getWidth(),
  141. 8 + roundToInt (f.getHeight()));
  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*) {}
  158. void Label::editorAboutToBeHidden (TextEditor*)
  159. {
  160. if (ComponentPeer* const peer = getPeer())
  161. peer->dismissPendingTextInput();
  162. }
  163. void Label::showEditor()
  164. {
  165. if (editor == nullptr)
  166. {
  167. addAndMakeVisible (editor = createEditorComponent());
  168. editor->setText (getText(), false);
  169. editor->addListener (this);
  170. editor->grabKeyboardFocus();
  171. if (editor == nullptr) // may be deleted by a callback
  172. return;
  173. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  174. resized();
  175. repaint();
  176. editorShown (editor);
  177. enterModalState (false);
  178. editor->grabKeyboardFocus();
  179. }
  180. }
  181. bool Label::updateFromTextEditorContents (TextEditor& ed)
  182. {
  183. const String newText (ed.getText());
  184. if (textValue.toString() != newText)
  185. {
  186. lastTextValue = newText;
  187. textValue = newText;
  188. repaint();
  189. textWasChanged();
  190. if (ownerComponent != nullptr)
  191. componentMovedOrResized (*ownerComponent, true, true);
  192. return true;
  193. }
  194. return false;
  195. }
  196. void Label::hideEditor (const bool discardCurrentEditorContents)
  197. {
  198. if (editor != nullptr)
  199. {
  200. WeakReference<Component> deletionChecker (this);
  201. ScopedPointer<TextEditor> outgoingEditor (editor);
  202. editorAboutToBeHidden (outgoingEditor);
  203. const bool changed = (! discardCurrentEditorContents)
  204. && updateFromTextEditorContents (*outgoingEditor);
  205. outgoingEditor = nullptr;
  206. repaint();
  207. if (changed)
  208. textWasEdited();
  209. if (deletionChecker != nullptr)
  210. exitModalState (0);
  211. if (changed && deletionChecker != nullptr)
  212. callChangeListeners();
  213. }
  214. }
  215. void Label::inputAttemptWhenModal()
  216. {
  217. if (editor != nullptr)
  218. {
  219. if (lossOfFocusDiscardsChanges)
  220. textEditorEscapeKeyPressed (*editor);
  221. else
  222. textEditorReturnKeyPressed (*editor);
  223. }
  224. }
  225. bool Label::isBeingEdited() const noexcept
  226. {
  227. return editor != nullptr;
  228. }
  229. TextEditor* Label::createEditorComponent()
  230. {
  231. TextEditor* const ed = new TextEditor (getName());
  232. ed->applyFontToAllText (getLookAndFeel().getLabelFont (*this));
  233. copyAllExplicitColoursTo (*ed);
  234. return ed;
  235. }
  236. TextEditor* Label::getCurrentTextEditor() const noexcept
  237. {
  238. return editor;
  239. }
  240. //==============================================================================
  241. void Label::paint (Graphics& g)
  242. {
  243. getLookAndFeel().drawLabel (g, *this);
  244. }
  245. void Label::mouseUp (const MouseEvent& e)
  246. {
  247. if (editSingleClick
  248. && e.mouseWasClicked()
  249. && contains (e.getPosition())
  250. && ! e.mods.isPopupMenu())
  251. {
  252. showEditor();
  253. }
  254. }
  255. void Label::mouseDoubleClick (const MouseEvent& e)
  256. {
  257. if (editDoubleClick && ! e.mods.isPopupMenu())
  258. showEditor();
  259. }
  260. void Label::resized()
  261. {
  262. if (editor != nullptr)
  263. editor->setBoundsInset (BorderSize<int> (0));
  264. }
  265. void Label::focusGained (FocusChangeType cause)
  266. {
  267. if (editSingleClick && cause == focusChangedByTabKey)
  268. showEditor();
  269. }
  270. void Label::enablementChanged()
  271. {
  272. repaint();
  273. }
  274. void Label::colourChanged()
  275. {
  276. repaint();
  277. }
  278. void Label::setMinimumHorizontalScale (const float newScale)
  279. {
  280. if (minimumHorizontalScale != newScale)
  281. {
  282. minimumHorizontalScale = newScale;
  283. repaint();
  284. }
  285. }
  286. //==============================================================================
  287. // We'll use a custom focus traverser here to make sure focus goes from the
  288. // text editor to another component rather than back to the label itself.
  289. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  290. {
  291. public:
  292. LabelKeyboardFocusTraverser() {}
  293. Component* getNextComponent (Component* c) { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  294. Component* getPreviousComponent (Component* c) { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  295. static Component* getComp (Component* current)
  296. {
  297. return dynamic_cast <TextEditor*> (current) != nullptr
  298. ? current->getParentComponent() : current;
  299. }
  300. };
  301. KeyboardFocusTraverser* Label::createFocusTraverser()
  302. {
  303. return new LabelKeyboardFocusTraverser();
  304. }
  305. //==============================================================================
  306. void Label::addListener (LabelListener* const listener)
  307. {
  308. listeners.add (listener);
  309. }
  310. void Label::removeListener (LabelListener* const listener)
  311. {
  312. listeners.remove (listener);
  313. }
  314. void Label::callChangeListeners()
  315. {
  316. Component::BailOutChecker checker (this);
  317. listeners.callChecked (checker, &LabelListener::labelTextChanged, this); // (can't use Label::Listener due to idiotic VC2005 bug)
  318. }
  319. //==============================================================================
  320. void Label::textEditorTextChanged (TextEditor& ed)
  321. {
  322. if (editor != nullptr)
  323. {
  324. jassert (&ed == editor);
  325. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  326. {
  327. if (lossOfFocusDiscardsChanges)
  328. textEditorEscapeKeyPressed (ed);
  329. else
  330. textEditorReturnKeyPressed (ed);
  331. }
  332. }
  333. }
  334. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  335. {
  336. if (editor != nullptr)
  337. {
  338. jassert (&ed == editor);
  339. const bool changed = updateFromTextEditorContents (ed);
  340. hideEditor (true);
  341. if (changed)
  342. {
  343. WeakReference<Component> deletionChecker (this);
  344. textWasEdited();
  345. if (deletionChecker != nullptr)
  346. callChangeListeners();
  347. }
  348. }
  349. }
  350. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  351. {
  352. if (editor != nullptr)
  353. {
  354. jassert (&ed == editor);
  355. (void) ed;
  356. editor->setText (textValue.toString(), false);
  357. hideEditor (true);
  358. }
  359. }
  360. void Label::textEditorFocusLost (TextEditor& ed)
  361. {
  362. textEditorTextChanged (ed);
  363. }