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.

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