The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

451 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. Label::Label (const String& name,
  19. const String& labelText)
  20. : Component (name),
  21. textValue (labelText),
  22. lastTextValue (labelText),
  23. font (15.0f),
  24. justification (Justification::centredLeft),
  25. horizontalBorderSize (5),
  26. verticalBorderSize (1),
  27. minimumHorizontalScale (0.7f),
  28. editSingleClick (false),
  29. editDoubleClick (false),
  30. lossOfFocusDiscardsChanges (false)
  31. {
  32. setColour (TextEditor::textColourId, Colours::black);
  33. setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  34. setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  35. textValue.addListener (this);
  36. }
  37. Label::~Label()
  38. {
  39. textValue.removeListener (this);
  40. if (ownerComponent != nullptr)
  41. ownerComponent->removeComponentListener (this);
  42. editor = nullptr;
  43. }
  44. //==============================================================================
  45. void Label::setText (const String& newText,
  46. const bool broadcastChangeMessage)
  47. {
  48. hideEditor (true);
  49. if (lastTextValue != newText)
  50. {
  51. lastTextValue = newText;
  52. textValue = newText;
  53. repaint();
  54. textWasChanged();
  55. if (ownerComponent != nullptr)
  56. componentMovedOrResized (*ownerComponent, true, true);
  57. if (broadcastChangeMessage)
  58. callChangeListeners();
  59. }
  60. }
  61. String Label::getText (const bool returnActiveEditorContents) const
  62. {
  63. return (returnActiveEditorContents && isBeingEdited())
  64. ? editor->getText()
  65. : textValue.toString();
  66. }
  67. void Label::valueChanged (Value&)
  68. {
  69. if (lastTextValue != textValue.toString())
  70. setText (textValue.toString(), true);
  71. }
  72. //==============================================================================
  73. void Label::setFont (const Font& newFont)
  74. {
  75. if (font != newFont)
  76. {
  77. font = newFont;
  78. repaint();
  79. }
  80. }
  81. const Font& Label::getFont() const noexcept
  82. {
  83. return font;
  84. }
  85. void Label::setEditable (const bool editOnSingleClick,
  86. const bool editOnDoubleClick,
  87. const bool lossOfFocusDiscardsChanges_)
  88. {
  89. editSingleClick = editOnSingleClick;
  90. editDoubleClick = editOnDoubleClick;
  91. lossOfFocusDiscardsChanges = lossOfFocusDiscardsChanges_;
  92. setWantsKeyboardFocus (editOnSingleClick || editOnDoubleClick);
  93. setFocusContainer (editOnSingleClick || editOnDoubleClick);
  94. }
  95. void Label::setJustificationType (const Justification& newJustification)
  96. {
  97. if (justification != newJustification)
  98. {
  99. justification = newJustification;
  100. repaint();
  101. }
  102. }
  103. void Label::setBorderSize (int h, int v)
  104. {
  105. if (horizontalBorderSize != h || verticalBorderSize != v)
  106. {
  107. horizontalBorderSize = h;
  108. verticalBorderSize = v;
  109. repaint();
  110. }
  111. }
  112. //==============================================================================
  113. Component* Label::getAttachedComponent() const
  114. {
  115. return static_cast<Component*> (ownerComponent);
  116. }
  117. void Label::attachToComponent (Component* owner, const bool onLeft)
  118. {
  119. if (ownerComponent != nullptr)
  120. ownerComponent->removeComponentListener (this);
  121. ownerComponent = owner;
  122. leftOfOwnerComp = onLeft;
  123. if (ownerComponent != nullptr)
  124. {
  125. setVisible (owner->isVisible());
  126. ownerComponent->addComponentListener (this);
  127. componentParentHierarchyChanged (*ownerComponent);
  128. componentMovedOrResized (*ownerComponent, true, true);
  129. }
  130. }
  131. void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
  132. {
  133. if (leftOfOwnerComp)
  134. {
  135. setSize (jmin (getFont().getStringWidth (textValue.toString()) + 8, component.getX()),
  136. component.getHeight());
  137. setTopRightPosition (component.getX(), component.getY());
  138. }
  139. else
  140. {
  141. setSize (component.getWidth(),
  142. 8 + roundToInt (getFont().getHeight()));
  143. setTopLeftPosition (component.getX(), component.getY() - getHeight());
  144. }
  145. }
  146. void Label::componentParentHierarchyChanged (Component& component)
  147. {
  148. if (component.getParentComponent() != nullptr)
  149. component.getParentComponent()->addChildComponent (this);
  150. }
  151. void Label::componentVisibilityChanged (Component& component)
  152. {
  153. setVisible (component.isVisible());
  154. }
  155. //==============================================================================
  156. void Label::textWasEdited() {}
  157. void Label::textWasChanged() {}
  158. void Label::editorShown (TextEditor*) {}
  159. void Label::editorAboutToBeHidden (TextEditor*) {}
  160. void Label::showEditor()
  161. {
  162. if (editor == nullptr)
  163. {
  164. addAndMakeVisible (editor = createEditorComponent());
  165. editor->setText (getText(), false);
  166. editor->addListener (this);
  167. editor->grabKeyboardFocus();
  168. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  169. resized();
  170. repaint();
  171. editorShown (editor);
  172. enterModalState (false);
  173. editor->grabKeyboardFocus();
  174. }
  175. }
  176. bool Label::updateFromTextEditorContents (TextEditor& ed)
  177. {
  178. const String newText (ed.getText());
  179. if (textValue.toString() != newText)
  180. {
  181. lastTextValue = newText;
  182. textValue = newText;
  183. repaint();
  184. textWasChanged();
  185. if (ownerComponent != nullptr)
  186. componentMovedOrResized (*ownerComponent, true, true);
  187. return true;
  188. }
  189. return false;
  190. }
  191. void Label::hideEditor (const bool discardCurrentEditorContents)
  192. {
  193. if (editor != nullptr)
  194. {
  195. WeakReference<Component> deletionChecker (this);
  196. ScopedPointer<TextEditor> outgoingEditor (editor);
  197. editorAboutToBeHidden (outgoingEditor);
  198. const bool changed = (! discardCurrentEditorContents)
  199. && updateFromTextEditorContents (*outgoingEditor);
  200. outgoingEditor = nullptr;
  201. repaint();
  202. if (changed)
  203. textWasEdited();
  204. if (deletionChecker != nullptr)
  205. exitModalState (0);
  206. if (changed && deletionChecker != nullptr)
  207. callChangeListeners();
  208. }
  209. }
  210. void Label::inputAttemptWhenModal()
  211. {
  212. if (editor != nullptr)
  213. {
  214. if (lossOfFocusDiscardsChanges)
  215. textEditorEscapeKeyPressed (*editor);
  216. else
  217. textEditorReturnKeyPressed (*editor);
  218. }
  219. }
  220. bool Label::isBeingEdited() const noexcept
  221. {
  222. return editor != nullptr;
  223. }
  224. TextEditor* Label::createEditorComponent()
  225. {
  226. TextEditor* const ed = new TextEditor (getName());
  227. ed->setFont (font);
  228. // copy these colours from our own settings..
  229. const int cols[] = { TextEditor::backgroundColourId,
  230. TextEditor::textColourId,
  231. TextEditor::highlightColourId,
  232. TextEditor::highlightedTextColourId,
  233. TextEditor::outlineColourId,
  234. TextEditor::focusedOutlineColourId,
  235. TextEditor::shadowColourId,
  236. CaretComponent::caretColourId };
  237. for (int i = 0; i < numElementsInArray (cols); ++i)
  238. ed->setColour (cols[i], findColour (cols[i]));
  239. return ed;
  240. }
  241. //==============================================================================
  242. void Label::paint (Graphics& g)
  243. {
  244. getLookAndFeel().drawLabel (g, *this);
  245. }
  246. void Label::mouseUp (const MouseEvent& e)
  247. {
  248. if (editSingleClick
  249. && e.mouseWasClicked()
  250. && contains (e.getPosition())
  251. && ! e.mods.isPopupMenu())
  252. {
  253. showEditor();
  254. }
  255. }
  256. void Label::mouseDoubleClick (const MouseEvent& e)
  257. {
  258. if (editDoubleClick && ! e.mods.isPopupMenu())
  259. showEditor();
  260. }
  261. void Label::resized()
  262. {
  263. if (editor != nullptr)
  264. editor->setBoundsInset (BorderSize<int> (0));
  265. }
  266. void Label::focusGained (FocusChangeType cause)
  267. {
  268. if (editSingleClick && cause == focusChangedByTabKey)
  269. showEditor();
  270. }
  271. void Label::enablementChanged()
  272. {
  273. repaint();
  274. }
  275. void Label::colourChanged()
  276. {
  277. repaint();
  278. }
  279. void Label::setMinimumHorizontalScale (const float newScale)
  280. {
  281. if (minimumHorizontalScale != newScale)
  282. {
  283. minimumHorizontalScale = newScale;
  284. repaint();
  285. }
  286. }
  287. //==============================================================================
  288. // We'll use a custom focus traverser here to make sure focus goes from the
  289. // text editor to another component rather than back to the label itself.
  290. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  291. {
  292. public:
  293. LabelKeyboardFocusTraverser() {}
  294. Component* getNextComponent (Component* current)
  295. {
  296. return KeyboardFocusTraverser::getNextComponent (dynamic_cast <TextEditor*> (current) != nullptr
  297. ? current->getParentComponent() : current);
  298. }
  299. Component* getPreviousComponent (Component* current)
  300. {
  301. return KeyboardFocusTraverser::getPreviousComponent (dynamic_cast <TextEditor*> (current) != nullptr
  302. ? current->getParentComponent() : current);
  303. }
  304. };
  305. KeyboardFocusTraverser* Label::createFocusTraverser()
  306. {
  307. return new LabelKeyboardFocusTraverser();
  308. }
  309. //==============================================================================
  310. void Label::addListener (LabelListener* const listener)
  311. {
  312. listeners.add (listener);
  313. }
  314. void Label::removeListener (LabelListener* const listener)
  315. {
  316. listeners.remove (listener);
  317. }
  318. void Label::callChangeListeners()
  319. {
  320. Component::BailOutChecker checker (this);
  321. listeners.callChecked (checker, &LabelListener::labelTextChanged, this); // (can't use Label::Listener due to idiotic VC2005 bug)
  322. }
  323. //==============================================================================
  324. void Label::textEditorTextChanged (TextEditor& ed)
  325. {
  326. if (editor != nullptr)
  327. {
  328. jassert (&ed == editor);
  329. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  330. {
  331. if (lossOfFocusDiscardsChanges)
  332. textEditorEscapeKeyPressed (ed);
  333. else
  334. textEditorReturnKeyPressed (ed);
  335. }
  336. }
  337. }
  338. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  339. {
  340. if (editor != nullptr)
  341. {
  342. jassert (&ed == editor);
  343. const bool changed = updateFromTextEditorContents (ed);
  344. hideEditor (true);
  345. if (changed)
  346. {
  347. WeakReference<Component> deletionChecker (this);
  348. textWasEdited();
  349. if (deletionChecker != nullptr)
  350. callChangeListeners();
  351. }
  352. }
  353. }
  354. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  355. {
  356. if (editor != nullptr)
  357. {
  358. jassert (&ed == editor);
  359. (void) ed;
  360. editor->setText (textValue.toString(), false);
  361. hideEditor (true);
  362. }
  363. }
  364. void Label::textEditorFocusLost (TextEditor& ed)
  365. {
  366. textEditorTextChanged (ed);
  367. }