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.

457 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. 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, const bool onLeft)
  120. {
  121. if (ownerComponent != nullptr)
  122. ownerComponent->removeComponentListener (this);
  123. ownerComponent = owner;
  124. leftOfOwnerComp = onLeft;
  125. if (ownerComponent != nullptr)
  126. {
  127. setVisible (owner->isVisible());
  128. ownerComponent->addComponentListener (this);
  129. componentParentHierarchyChanged (*ownerComponent);
  130. componentMovedOrResized (*ownerComponent, true, true);
  131. }
  132. }
  133. void Label::componentMovedOrResized (Component& component, bool /*wasMoved*/, bool /*wasResized*/)
  134. {
  135. if (leftOfOwnerComp)
  136. {
  137. setSize (jmin (getFont().getStringWidth (textValue.toString()) + 8, component.getX()),
  138. component.getHeight());
  139. setTopRightPosition (component.getX(), component.getY());
  140. }
  141. else
  142. {
  143. setSize (component.getWidth(),
  144. 8 + roundToInt (getFont().getHeight()));
  145. setTopLeftPosition (component.getX(), component.getY() - getHeight());
  146. }
  147. }
  148. void Label::componentParentHierarchyChanged (Component& component)
  149. {
  150. if (component.getParentComponent() != nullptr)
  151. component.getParentComponent()->addChildComponent (this);
  152. }
  153. void Label::componentVisibilityChanged (Component& component)
  154. {
  155. setVisible (component.isVisible());
  156. }
  157. //==============================================================================
  158. void Label::textWasEdited() {}
  159. void Label::textWasChanged() {}
  160. void Label::editorShown (TextEditor*) {}
  161. void Label::editorAboutToBeHidden (TextEditor*) {}
  162. void Label::showEditor()
  163. {
  164. if (editor == nullptr)
  165. {
  166. addAndMakeVisible (editor = createEditorComponent());
  167. editor->setText (getText(), false);
  168. editor->addListener (this);
  169. editor->grabKeyboardFocus();
  170. editor->setHighlightedRegion (Range<int> (0, textValue.toString().length()));
  171. resized();
  172. repaint();
  173. editorShown (editor);
  174. enterModalState (false);
  175. editor->grabKeyboardFocus();
  176. }
  177. }
  178. bool Label::updateFromTextEditorContents (TextEditor& ed)
  179. {
  180. const String newText (ed.getText());
  181. if (textValue.toString() != newText)
  182. {
  183. lastTextValue = newText;
  184. textValue = newText;
  185. repaint();
  186. textWasChanged();
  187. if (ownerComponent != nullptr)
  188. componentMovedOrResized (*ownerComponent, true, true);
  189. return true;
  190. }
  191. return false;
  192. }
  193. void Label::hideEditor (const bool discardCurrentEditorContents)
  194. {
  195. if (editor != nullptr)
  196. {
  197. WeakReference<Component> deletionChecker (this);
  198. ScopedPointer<TextEditor> outgoingEditor (editor);
  199. editorAboutToBeHidden (outgoingEditor);
  200. const bool changed = (! discardCurrentEditorContents)
  201. && updateFromTextEditorContents (*outgoingEditor);
  202. outgoingEditor = nullptr;
  203. repaint();
  204. if (changed)
  205. textWasEdited();
  206. if (deletionChecker != nullptr)
  207. exitModalState (0);
  208. if (changed && deletionChecker != nullptr)
  209. callChangeListeners();
  210. }
  211. }
  212. void Label::inputAttemptWhenModal()
  213. {
  214. if (editor != nullptr)
  215. {
  216. if (lossOfFocusDiscardsChanges)
  217. textEditorEscapeKeyPressed (*editor);
  218. else
  219. textEditorReturnKeyPressed (*editor);
  220. }
  221. }
  222. bool Label::isBeingEdited() const noexcept
  223. {
  224. return editor != nullptr;
  225. }
  226. TextEditor* Label::createEditorComponent()
  227. {
  228. TextEditor* const ed = new TextEditor (getName());
  229. ed->setFont (font);
  230. // copy these colours from our own settings..
  231. const int cols[] = { TextEditor::backgroundColourId,
  232. TextEditor::textColourId,
  233. TextEditor::highlightColourId,
  234. TextEditor::highlightedTextColourId,
  235. TextEditor::outlineColourId,
  236. TextEditor::focusedOutlineColourId,
  237. TextEditor::shadowColourId,
  238. CaretComponent::caretColourId };
  239. for (int i = 0; i < numElementsInArray (cols); ++i)
  240. ed->setColour (cols[i], findColour (cols[i]));
  241. return ed;
  242. }
  243. //==============================================================================
  244. void Label::paint (Graphics& g)
  245. {
  246. getLookAndFeel().drawLabel (g, *this);
  247. }
  248. void Label::mouseUp (const MouseEvent& e)
  249. {
  250. if (editSingleClick
  251. && e.mouseWasClicked()
  252. && contains (e.getPosition())
  253. && ! e.mods.isPopupMenu())
  254. {
  255. showEditor();
  256. }
  257. }
  258. void Label::mouseDoubleClick (const MouseEvent& e)
  259. {
  260. if (editDoubleClick && ! e.mods.isPopupMenu())
  261. showEditor();
  262. }
  263. void Label::resized()
  264. {
  265. if (editor != nullptr)
  266. editor->setBoundsInset (BorderSize<int> (0));
  267. }
  268. void Label::focusGained (FocusChangeType cause)
  269. {
  270. if (editSingleClick && cause == focusChangedByTabKey)
  271. showEditor();
  272. }
  273. void Label::enablementChanged()
  274. {
  275. repaint();
  276. }
  277. void Label::colourChanged()
  278. {
  279. repaint();
  280. }
  281. void Label::setMinimumHorizontalScale (const float newScale)
  282. {
  283. if (minimumHorizontalScale != newScale)
  284. {
  285. minimumHorizontalScale = newScale;
  286. repaint();
  287. }
  288. }
  289. //==============================================================================
  290. // We'll use a custom focus traverser here to make sure focus goes from the
  291. // text editor to another component rather than back to the label itself.
  292. class LabelKeyboardFocusTraverser : public KeyboardFocusTraverser
  293. {
  294. public:
  295. LabelKeyboardFocusTraverser() {}
  296. Component* getNextComponent (Component* current)
  297. {
  298. return KeyboardFocusTraverser::getNextComponent (dynamic_cast <TextEditor*> (current) != nullptr
  299. ? current->getParentComponent() : current);
  300. }
  301. Component* getPreviousComponent (Component* current)
  302. {
  303. return KeyboardFocusTraverser::getPreviousComponent (dynamic_cast <TextEditor*> (current) != nullptr
  304. ? current->getParentComponent() : current);
  305. }
  306. };
  307. KeyboardFocusTraverser* Label::createFocusTraverser()
  308. {
  309. return new LabelKeyboardFocusTraverser();
  310. }
  311. //==============================================================================
  312. void Label::addListener (LabelListener* const listener)
  313. {
  314. listeners.add (listener);
  315. }
  316. void Label::removeListener (LabelListener* const listener)
  317. {
  318. listeners.remove (listener);
  319. }
  320. void Label::callChangeListeners()
  321. {
  322. Component::BailOutChecker checker (this);
  323. listeners.callChecked (checker, &LabelListener::labelTextChanged, this); // (can't use Label::Listener due to idiotic VC2005 bug)
  324. }
  325. //==============================================================================
  326. void Label::textEditorTextChanged (TextEditor& ed)
  327. {
  328. if (editor != nullptr)
  329. {
  330. jassert (&ed == editor);
  331. if (! (hasKeyboardFocus (true) || isCurrentlyBlockedByAnotherModalComponent()))
  332. {
  333. if (lossOfFocusDiscardsChanges)
  334. textEditorEscapeKeyPressed (ed);
  335. else
  336. textEditorReturnKeyPressed (ed);
  337. }
  338. }
  339. }
  340. void Label::textEditorReturnKeyPressed (TextEditor& ed)
  341. {
  342. if (editor != nullptr)
  343. {
  344. jassert (&ed == editor);
  345. const bool changed = updateFromTextEditorContents (ed);
  346. hideEditor (true);
  347. if (changed)
  348. {
  349. WeakReference<Component> deletionChecker (this);
  350. textWasEdited();
  351. if (deletionChecker != nullptr)
  352. callChangeListeners();
  353. }
  354. }
  355. }
  356. void Label::textEditorEscapeKeyPressed (TextEditor& ed)
  357. {
  358. if (editor != nullptr)
  359. {
  360. jassert (&ed == editor);
  361. (void) ed;
  362. editor->setText (textValue.toString(), false);
  363. hideEditor (true);
  364. }
  365. }
  366. void Label::textEditorFocusLost (TextEditor& ed)
  367. {
  368. textEditorTextChanged (ed);
  369. }
  370. END_JUCE_NAMESPACE