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.

470 lines
13KB

  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. && isEnabled()
  261. && e.mouseWasClicked()
  262. && contains (e.getPosition())
  263. && ! e.mods.isPopupMenu())
  264. {
  265. showEditor();
  266. }
  267. }
  268. void Label::mouseDoubleClick (const MouseEvent& e)
  269. {
  270. if (editDoubleClick
  271. && isEnabled()
  272. && ! e.mods.isPopupMenu())
  273. showEditor();
  274. }
  275. void Label::resized()
  276. {
  277. if (editor != nullptr)
  278. editor->setBounds (getLocalBounds());
  279. }
  280. void Label::focusGained (FocusChangeType cause)
  281. {
  282. if (editSingleClick
  283. && isEnabled()
  284. && cause == focusChangedByTabKey)
  285. showEditor();
  286. }
  287. void Label::enablementChanged()
  288. {
  289. repaint();
  290. }
  291. void Label::colourChanged()
  292. {
  293. repaint();
  294. }
  295. void Label::setMinimumHorizontalScale (const float newScale)
  296. {
  297. if (minimumHorizontalScale != newScale)
  298. {
  299. minimumHorizontalScale = newScale;
  300. repaint();
  301. }
  302. }
  303. //==============================================================================
  304. // We'll use a custom focus traverser here to make sure focus goes from the
  305. // text editor to another component rather than back to the label itself.
  306. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  307. {
  308. public:
  309. LabelKeyboardFocusTraverser() {}
  310. Component* getNextComponent (Component* c) { return KeyboardFocusTraverser::getNextComponent (getComp (c)); }
  311. Component* getPreviousComponent (Component* c) { return KeyboardFocusTraverser::getPreviousComponent (getComp (c)); }
  312. static Component* getComp (Component* current)
  313. {
  314. return dynamic_cast <TextEditor*> (current) != nullptr
  315. ? current->getParentComponent() : current;
  316. }
  317. };
  318. KeyboardFocusTraverser* Label::createFocusTraverser()
  319. {
  320. return new LabelKeyboardFocusTraverser();
  321. }
  322. //==============================================================================
  323. void Label::addListener (LabelListener* const listener)
  324. {
  325. listeners.add (listener);
  326. }
  327. void Label::removeListener (LabelListener* const listener)
  328. {
  329. listeners.remove (listener);
  330. }
  331. void Label::callChangeListeners()
  332. {
  333. Component::BailOutChecker checker (this);
  334. listeners.callChecked (checker, &LabelListener::labelTextChanged, this); // (can't use Label::Listener due to idiotic VC2005 bug)
  335. }
  336. //==============================================================================
  337. void Label::textEditorTextChanged (TextEditor& ed)
  338. {
  339. if (editor != nullptr)
  340. {
  341. jassert (&ed == editor);
  342. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  343. {
  344. if (lossOfFocusDiscardsChanges)
  345. textEditorEscapeKeyPressed (ed);
  346. else
  347. textEditorReturnKeyPressed (ed);
  348. }
  349. }
  350. }
  351. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  352. {
  353. if (editor != nullptr)
  354. {
  355. jassert (&ed == editor);
  356. const bool changed = updateFromTextEditorContents (ed);
  357. hideEditor (true);
  358. if (changed)
  359. {
  360. WeakReference<Component> deletionChecker (this);
  361. textWasEdited();
  362. if (deletionChecker != nullptr)
  363. callChangeListeners();
  364. }
  365. }
  366. }
  367. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  368. {
  369. if (editor != nullptr)
  370. {
  371. jassert (&ed == editor);
  372. (void) ed;
  373. editor->setText (textValue.toString(), false);
  374. hideEditor (true);
  375. }
  376. }
  377. void Label::textEditorFocusLost (TextEditor& ed)
  378. {
  379. textEditorTextChanged (ed);
  380. }