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.

470 lines
13KB

  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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. Label::Label (const String& name,
  21. const String& labelText)
  22. : Component (name),
  23. textValue (labelText),
  24. lastTextValue (labelText),
  25. font (15.0f),
  26. justification (Justification::centredLeft),
  27. horizontalBorderSize (5),
  28. verticalBorderSize (1),
  29. minimumHorizontalScale (0.7f),
  30. editSingleClick (false),
  31. editDoubleClick (false),
  32. lossOfFocusDiscardsChanges (false)
  33. {
  34. setColour (TextEditor::textColourId, Colours::black);
  35. setColour (TextEditor::backgroundColourId, Colours::transparentBlack);
  36. setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  37. textValue.addListener (this);
  38. }
  39. Label::~Label()
  40. {
  41. textValue.removeListener (this);
  42. if (ownerComponent != nullptr)
  43. ownerComponent->removeComponentListener (this);
  44. editor = nullptr;
  45. }
  46. //==============================================================================
  47. void Label::setText (const String& newText,
  48. const bool broadcastChangeMessage)
  49. {
  50. hideEditor (true);
  51. if (lastTextValue != newText)
  52. {
  53. lastTextValue = newText;
  54. textValue = newText;
  55. repaint();
  56. textWasChanged();
  57. if (ownerComponent != nullptr)
  58. componentMovedOrResized (*ownerComponent, true, true);
  59. if (broadcastChangeMessage)
  60. callChangeListeners();
  61. }
  62. }
  63. String Label::getText (const bool returnActiveEditorContents) const
  64. {
  65. return (returnActiveEditorContents && isBeingEdited())
  66. ? editor->getText()
  67. : textValue.toString();
  68. }
  69. void Label::valueChanged (Value&)
  70. {
  71. if (lastTextValue != textValue.toString())
  72. setText (textValue.toString(), true);
  73. }
  74. //==============================================================================
  75. void Label::setFont (const Font& newFont)
  76. {
  77. if (font != newFont)
  78. {
  79. font = newFont;
  80. repaint();
  81. }
  82. }
  83. const Font& Label::getFont() const noexcept
  84. {
  85. return font;
  86. }
  87. void Label::setEditable (const bool editOnSingleClick,
  88. const bool editOnDoubleClick,
  89. const bool lossOfFocusDiscardsChanges_)
  90. {
  91. editSingleClick = editOnSingleClick;
  92. editDoubleClick = editOnDoubleClick;
  93. lossOfFocusDiscardsChanges = lossOfFocusDiscardsChanges_;
  94. setWantsKeyboardFocus (editOnSingleClick || editOnDoubleClick);
  95. setFocusContainer (editOnSingleClick || editOnDoubleClick);
  96. }
  97. void Label::setJustificationType (const Justification& newJustification)
  98. {
  99. if (justification != newJustification)
  100. {
  101. justification = newJustification;
  102. repaint();
  103. }
  104. }
  105. void Label::setBorderSize (int h, int v)
  106. {
  107. if (horizontalBorderSize != h || verticalBorderSize != v)
  108. {
  109. horizontalBorderSize = h;
  110. verticalBorderSize = v;
  111. repaint();
  112. }
  113. }
  114. //==============================================================================
  115. Component* Label::getAttachedComponent() const
  116. {
  117. return static_cast<Component*> (ownerComponent);
  118. }
  119. void Label::attachToComponent (Component* owner,
  120. const bool onLeft)
  121. {
  122. if (ownerComponent != nullptr)
  123. ownerComponent->removeComponentListener (this);
  124. ownerComponent = owner;
  125. leftOfOwnerComp = onLeft;
  126. if (ownerComponent != nullptr)
  127. {
  128. setVisible (owner->isVisible());
  129. ownerComponent->addComponentListener (this);
  130. componentParentHierarchyChanged (*ownerComponent);
  131. componentMovedOrResized (*ownerComponent, true, true);
  132. }
  133. }
  134. void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
  135. {
  136. if (leftOfOwnerComp)
  137. {
  138. setSize (jmin (getFont().getStringWidth (textValue.toString()) + 8, component.getX()),
  139. component.getHeight());
  140. setTopRightPosition (component.getX(), component.getY());
  141. }
  142. else
  143. {
  144. setSize (component.getWidth(),
  145. 8 + roundToInt (getFont().getHeight()));
  146. setTopLeftPosition (component.getX(), component.getY() - getHeight());
  147. }
  148. }
  149. void Label::componentParentHierarchyChanged (Component& component)
  150. {
  151. if (component.getParentComponent() != nullptr)
  152. component.getParentComponent()->addChildComponent (this);
  153. }
  154. void Label::componentVisibilityChanged (Component& component)
  155. {
  156. setVisible (component.isVisible());
  157. }
  158. //==============================================================================
  159. void Label::textWasEdited()
  160. {
  161. }
  162. void Label::textWasChanged()
  163. {
  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. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  174. editor->addListener (this);
  175. resized();
  176. repaint();
  177. editorShown (editor);
  178. enterModalState (false);
  179. editor->grabKeyboardFocus();
  180. }
  181. }
  182. void Label::editorShown (TextEditor*)
  183. {
  184. }
  185. void Label::editorAboutToBeHidden (TextEditor*)
  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. TextEditor* Label::createEditorComponent()
  237. {
  238. TextEditor* const ed = new TextEditor (getName());
  239. ed->setFont (font);
  240. // copy these colours from our own settings..
  241. const int cols[] = { TextEditor::backgroundColourId,
  242. TextEditor::textColourId,
  243. TextEditor::highlightColourId,
  244. TextEditor::highlightedTextColourId,
  245. TextEditor::outlineColourId,
  246. TextEditor::focusedOutlineColourId,
  247. TextEditor::shadowColourId,
  248. CaretComponent::caretColourId };
  249. for (int i = 0; i < numElementsInArray (cols); ++i)
  250. ed->setColour (cols[i], findColour (cols[i]));
  251. return ed;
  252. }
  253. //==============================================================================
  254. void Label::paint (Graphics& g)
  255. {
  256. getLookAndFeel().drawLabel (g, *this);
  257. }
  258. void Label::mouseUp (const MouseEvent& e)
  259. {
  260. if (editSingleClick
  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 && ! e.mods.isPopupMenu())
  271. showEditor();
  272. }
  273. void Label::resized()
  274. {
  275. if (editor != nullptr)
  276. editor->setBoundsInset (BorderSize<int> (0));
  277. }
  278. void Label::focusGained (FocusChangeType cause)
  279. {
  280. if (editSingleClick && cause == focusChangedByTabKey)
  281. showEditor();
  282. }
  283. void Label::enablementChanged()
  284. {
  285. repaint();
  286. }
  287. void Label::colourChanged()
  288. {
  289. repaint();
  290. }
  291. void Label::setMinimumHorizontalScale (const float newScale)
  292. {
  293. if (minimumHorizontalScale != newScale)
  294. {
  295. minimumHorizontalScale = newScale;
  296. repaint();
  297. }
  298. }
  299. //==============================================================================
  300. // We'll use a custom focus traverser here to make sure focus goes from the
  301. // text editor to another component rather than back to the label itself.
  302. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  303. {
  304. public:
  305. LabelKeyboardFocusTraverser() {}
  306. Component* getNextComponent (Component* current)
  307. {
  308. return KeyboardFocusTraverser::getNextComponent (dynamic_cast <TextEditor*> (current) != nullptr
  309. ? current->getParentComponent() : current);
  310. }
  311. Component* getPreviousComponent (Component* current)
  312. {
  313. return KeyboardFocusTraverser::getPreviousComponent (dynamic_cast <TextEditor*> (current) != nullptr
  314. ? current->getParentComponent() : current);
  315. }
  316. };
  317. KeyboardFocusTraverser* Label::createFocusTraverser()
  318. {
  319. return new LabelKeyboardFocusTraverser();
  320. }
  321. //==============================================================================
  322. void Label::addListener (LabelListener* const listener)
  323. {
  324. listeners.add (listener);
  325. }
  326. void Label::removeListener (LabelListener* const listener)
  327. {
  328. listeners.remove (listener);
  329. }
  330. void Label::callChangeListeners()
  331. {
  332. Component::BailOutChecker checker (this);
  333. listeners.callChecked (checker, &LabelListener::labelTextChanged, this); // (can't use Label::Listener due to idiotic VC2005 bug)
  334. }
  335. //==============================================================================
  336. void Label::textEditorTextChanged (TextEditor& ed)
  337. {
  338. if (editor != nullptr)
  339. {
  340. jassert (&ed == editor);
  341. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  342. {
  343. if (lossOfFocusDiscardsChanges)
  344. textEditorEscapeKeyPressed (ed);
  345. else
  346. textEditorReturnKeyPressed (ed);
  347. }
  348. }
  349. }
  350. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  351. {
  352. if (editor != nullptr)
  353. {
  354. jassert (&ed == editor);
  355. const bool changed = updateFromTextEditorContents (ed);
  356. hideEditor (true);
  357. if (changed)
  358. {
  359. WeakReference<Component> deletionChecker (this);
  360. textWasEdited();
  361. if (deletionChecker != nullptr)
  362. callChangeListeners();
  363. }
  364. }
  365. }
  366. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  367. {
  368. if (editor != nullptr)
  369. {
  370. jassert (&ed == editor);
  371. (void) ed;
  372. editor->setText (textValue.toString(), false);
  373. hideEditor (true);
  374. }
  375. }
  376. void Label::textEditorFocusLost (TextEditor& ed)
  377. {
  378. textEditorTextChanged (ed);
  379. }
  380. END_JUCE_NAMESPACE