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.

228 lines
7.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCER_COLOURPROPERTYCOMPONENT_H_INCLUDED
  18. #define JUCER_COLOURPROPERTYCOMPONENT_H_INCLUDED
  19. struct ColourPropertyComponent : public PropertyComponent
  20. {
  21. ColourPropertyComponent (UndoManager* undoManager, const String& name, const Value& colour,
  22. Colour defaultColour, bool canResetToDefault)
  23. : PropertyComponent (name),
  24. colourEditor (undoManager, colour, defaultColour, canResetToDefault)
  25. {
  26. addAndMakeVisible (colourEditor);
  27. }
  28. void resized() override
  29. {
  30. colourEditor.setBounds (getLookAndFeel().getPropertyComponentContentPosition (*this));
  31. }
  32. void refresh() override {}
  33. private:
  34. /**
  35. A component that shows a colour swatch with hex ARGB value, and which pops up
  36. a colour selector when you click it.
  37. */
  38. struct ColourEditorComponent : public Component,
  39. public Value::Listener
  40. {
  41. ColourEditorComponent (UndoManager* um, const Value& colour,
  42. Colour defaultCol, const bool canReset)
  43. : undoManager (um), colourValue (colour), defaultColour (defaultCol),
  44. canResetToDefault (canReset)
  45. {
  46. colourValue.addListener (this);
  47. }
  48. void paint (Graphics& g) override
  49. {
  50. const Colour colour (getColour());
  51. g.fillAll (Colours::grey);
  52. g.fillCheckerBoard (getLocalBounds().reduced (2),
  53. 10, 10,
  54. Colour (0xffdddddd).overlaidWith (colour),
  55. Colour (0xffffffff).overlaidWith (colour));
  56. g.setColour (Colours::white.overlaidWith (colour).contrasting());
  57. g.setFont (Font (getHeight() * 0.6f, Font::bold));
  58. g.drawFittedText (colour.toDisplayString (true), getLocalBounds().reduced (2, 1),
  59. Justification::centred, 1);
  60. }
  61. Colour getColour() const
  62. {
  63. if (colourValue.toString().isEmpty())
  64. return defaultColour;
  65. return Colour::fromString (colourValue.toString());
  66. }
  67. void setColour (Colour newColour)
  68. {
  69. if (getColour() != newColour)
  70. {
  71. if (newColour == defaultColour && canResetToDefault)
  72. colourValue = var();
  73. else
  74. colourValue = newColour.toDisplayString (true);
  75. }
  76. }
  77. void resetToDefault()
  78. {
  79. setColour (defaultColour);
  80. }
  81. void refresh()
  82. {
  83. const Colour col (getColour());
  84. if (col != lastColour)
  85. {
  86. lastColour = col;
  87. repaint();
  88. }
  89. }
  90. void mouseDown (const MouseEvent&) override
  91. {
  92. if (undoManager != nullptr)
  93. undoManager->beginNewTransaction();
  94. CallOutBox::launchAsynchronously (new PopupColourSelector (colourValue,
  95. defaultColour,
  96. canResetToDefault),
  97. getScreenBounds(), nullptr);
  98. }
  99. void valueChanged (Value&) override
  100. {
  101. refresh();
  102. }
  103. UndoManager* undoManager;
  104. Value colourValue;
  105. Colour lastColour;
  106. const Colour defaultColour;
  107. const bool canResetToDefault;
  108. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourEditorComponent)
  109. };
  110. //==============================================================================
  111. struct PopupColourSelector : public Component,
  112. public ChangeListener,
  113. public Value::Listener,
  114. public ButtonListener
  115. {
  116. PopupColourSelector (const Value& colour,
  117. Colour defaultCol,
  118. const bool canResetToDefault)
  119. : defaultButton ("Reset to Default"),
  120. colourValue (colour),
  121. defaultColour (defaultCol)
  122. {
  123. addAndMakeVisible (selector);
  124. selector.setName ("Colour");
  125. selector.setCurrentColour (getColour());
  126. selector.addChangeListener (this);
  127. if (canResetToDefault)
  128. {
  129. addAndMakeVisible (defaultButton);
  130. defaultButton.addListener (this);
  131. }
  132. colourValue.addListener (this);
  133. setSize (300, 400);
  134. }
  135. void resized() override
  136. {
  137. if (defaultButton.isVisible())
  138. {
  139. selector.setBounds (0, 0, getWidth(), getHeight() - 30);
  140. defaultButton.changeWidthToFitText (22);
  141. defaultButton.setTopLeftPosition (10, getHeight() - 26);
  142. }
  143. else
  144. {
  145. selector.setBounds (getLocalBounds());
  146. }
  147. }
  148. Colour getColour() const
  149. {
  150. if (colourValue.toString().isEmpty())
  151. return defaultColour;
  152. return Colour::fromString (colourValue.toString());
  153. }
  154. void setColour (Colour newColour)
  155. {
  156. if (getColour() != newColour)
  157. {
  158. if (newColour == defaultColour && defaultButton.isVisible())
  159. colourValue = var();
  160. else
  161. colourValue = newColour.toDisplayString (true);
  162. }
  163. }
  164. void buttonClicked (Button*) override
  165. {
  166. setColour (defaultColour);
  167. selector.setCurrentColour (defaultColour);
  168. }
  169. void changeListenerCallback (ChangeBroadcaster*) override
  170. {
  171. if (selector.getCurrentColour() != getColour())
  172. setColour (selector.getCurrentColour());
  173. }
  174. void valueChanged (Value&) override
  175. {
  176. selector.setCurrentColour (getColour());
  177. }
  178. private:
  179. StoredSettings::ColourSelectorWithSwatches selector;
  180. TextButton defaultButton;
  181. Value colourValue;
  182. Colour defaultColour;
  183. };
  184. ColourEditorComponent colourEditor;
  185. };
  186. #endif // JUCER_COLOURPROPERTYCOMPONENT_H_INCLUDED