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.

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