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

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