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.

253 lines
7.6KB

  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. #ifndef __JUCER_COLOUREDITORCOMPONENT_JUCEHEADER__
  19. #define __JUCER_COLOUREDITORCOMPONENT_JUCEHEADER__
  20. //==============================================================================
  21. class PopupColourSelector : public Component,
  22. public ChangeListener,
  23. public Value::Listener,
  24. public ButtonListener
  25. {
  26. public:
  27. PopupColourSelector (const Value& colourValue_,
  28. const Colour& defaultColour_,
  29. const bool canResetToDefault)
  30. : defaultButton ("Reset to Default"),
  31. colourValue (colourValue_),
  32. defaultColour (defaultColour_)
  33. {
  34. addAndMakeVisible (&selector);
  35. selector.setName ("Colour");
  36. selector.setCurrentColour (getColour());
  37. selector.addChangeListener (this);
  38. if (canResetToDefault)
  39. {
  40. addAndMakeVisible (&defaultButton);
  41. defaultButton.addButtonListener (this);
  42. }
  43. colourValue.addListener (this);
  44. }
  45. ~PopupColourSelector()
  46. {
  47. }
  48. static void showAt (Component* targetComp, const Value& colourValue,
  49. const Colour& defaultColour, const bool canResetToDefault)
  50. {
  51. PopupColourSelector colourSelector (colourValue, defaultColour, canResetToDefault);
  52. colourSelector.setSize (300, 400);
  53. CallOutBox c (colourSelector, *targetComp, 0 /*targetComp->getTopLevelComponent()*/);
  54. c.runModalLoop();
  55. }
  56. void resized()
  57. {
  58. if (defaultButton.isVisible())
  59. {
  60. selector.setBounds (0, 0, getWidth(), getHeight() - 30);
  61. defaultButton.changeWidthToFitText (22);
  62. defaultButton.setTopLeftPosition (10, getHeight() - 26);
  63. }
  64. else
  65. {
  66. selector.setBounds (0, 0, getWidth(), getHeight());
  67. }
  68. }
  69. const Colour getColour() const
  70. {
  71. if (colourValue.toString().isEmpty())
  72. return defaultColour;
  73. return Colour::fromString (colourValue.toString());
  74. }
  75. void setColour (const Colour& newColour)
  76. {
  77. if (getColour() != newColour)
  78. {
  79. if (newColour == defaultColour && defaultButton.isVisible())
  80. colourValue = var::null;
  81. else
  82. colourValue = newColour.toDisplayString (true);
  83. }
  84. }
  85. void buttonClicked (Button*)
  86. {
  87. setColour (defaultColour);
  88. selector.setCurrentColour (defaultColour);
  89. }
  90. void changeListenerCallback (void* source)
  91. {
  92. if (selector.getCurrentColour() != getColour())
  93. setColour (selector.getCurrentColour());
  94. }
  95. void valueChanged (Value&)
  96. {
  97. selector.setCurrentColour (getColour());
  98. }
  99. private:
  100. StoredSettings::ColourSelectorWithSwatches selector;
  101. TextButton defaultButton;
  102. Value colourValue;
  103. Colour defaultColour;
  104. };
  105. //==============================================================================
  106. /**
  107. A component that shows a colour swatch with hex ARGB value, and which pops up
  108. a colour selector when you click it.
  109. */
  110. class ColourEditorComponent : public Component,
  111. public Value::Listener
  112. {
  113. public:
  114. ColourEditorComponent (UndoManager* undoManager_, const Value& colourValue_,
  115. const Colour& defaultColour_, const bool canResetToDefault_)
  116. : undoManager (undoManager_), colourValue (colourValue_), defaultColour (defaultColour_),
  117. canResetToDefault (canResetToDefault_)
  118. {
  119. colourValue.addListener (this);
  120. }
  121. ~ColourEditorComponent()
  122. {
  123. }
  124. void paint (Graphics& g)
  125. {
  126. const Colour colour (getColour());
  127. g.fillAll (Colours::grey);
  128. g.fillCheckerBoard (2, 2, getWidth() - 4, getHeight() - 4,
  129. 10, 10,
  130. Colour (0xffdddddd).overlaidWith (colour),
  131. Colour (0xffffffff).overlaidWith (colour));
  132. g.setColour (Colours::white.overlaidWith (colour).contrasting());
  133. g.setFont (getHeight() * 0.6f, Font::bold);
  134. g.drawFittedText (colour.toDisplayString (true),
  135. 2, 1, getWidth() - 4, getHeight() - 1,
  136. Justification::centred, 1);
  137. }
  138. const Colour getColour() const
  139. {
  140. if (colourValue.toString().isEmpty())
  141. return defaultColour;
  142. return Colour::fromString (colourValue.toString());
  143. }
  144. void setColour (const Colour& newColour)
  145. {
  146. if (getColour() != newColour)
  147. {
  148. if (newColour == defaultColour && canResetToDefault)
  149. colourValue = var::null;
  150. else
  151. colourValue = newColour.toDisplayString (true);
  152. }
  153. }
  154. void resetToDefault()
  155. {
  156. setColour (defaultColour);
  157. }
  158. void refresh()
  159. {
  160. const Colour col (getColour());
  161. if (col != lastColour)
  162. {
  163. lastColour = col;
  164. repaint();
  165. }
  166. }
  167. void mouseDown (const MouseEvent& e)
  168. {
  169. undoManager->beginNewTransaction();
  170. PopupColourSelector::showAt (this, colourValue, defaultColour, canResetToDefault);
  171. }
  172. void valueChanged (Value&)
  173. {
  174. refresh();
  175. }
  176. juce_UseDebuggingNewOperator
  177. private:
  178. UndoManager* undoManager;
  179. Value colourValue;
  180. Colour lastColour;
  181. const Colour defaultColour;
  182. const bool canResetToDefault;
  183. };
  184. //==============================================================================
  185. class ColourPropertyComponent : public PropertyComponent
  186. {
  187. public:
  188. //==============================================================================
  189. ColourPropertyComponent (UndoManager* undoManager, const String& name, const Value& colour,
  190. const Colour& defaultColour, bool canResetToDefault)
  191. : PropertyComponent (name),
  192. colourEditor (undoManager, colour, defaultColour, canResetToDefault)
  193. {
  194. addAndMakeVisible (&colourEditor);
  195. }
  196. ~ColourPropertyComponent()
  197. {
  198. }
  199. void resized()
  200. {
  201. colourEditor.setBounds (getLookAndFeel().getPropertyComponentContentPosition (*this));
  202. }
  203. void refresh() {}
  204. protected:
  205. ColourEditorComponent colourEditor;
  206. };
  207. #endif // __JUCER_COLOUREDITORCOMPONENT_JUCEHEADER__