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.

479 lines
13KB

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