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.

473 lines
13KB

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