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.

224 lines
7.2KB

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