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.

215 lines
6.7KB

  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. #include "../../Application/jucer_Application.h"
  20. class JucerColourPropertyComponent : public PropertyComponent
  21. {
  22. public:
  23. JucerColourPropertyComponent (const String& name,
  24. const bool canReset)
  25. : PropertyComponent (name)
  26. {
  27. colourPropEditor = new ColourPropEditorComponent (this, canReset);
  28. addAndMakeVisible (colourPropEditor);
  29. }
  30. virtual void setColour (Colour newColour) = 0;
  31. virtual Colour getColour() const = 0;
  32. virtual void resetToDefault() = 0;
  33. void refresh()
  34. {
  35. ((ColourPropEditorComponent*) getChildComponent (0))->refresh();
  36. }
  37. class ColourEditorComponent : public Component,
  38. public ChangeListener
  39. {
  40. public:
  41. ColourEditorComponent (const bool canReset)
  42. : canResetToDefault (canReset)
  43. {
  44. }
  45. void paint (Graphics& g)
  46. {
  47. g.fillAll (Colours::grey);
  48. g.fillCheckerBoard (getLocalBounds().reduced (2, 2),
  49. 10, 10,
  50. Colour (0xffdddddd).overlaidWith (colour),
  51. Colour (0xffffffff).overlaidWith (colour));
  52. g.setColour (Colours::white.overlaidWith (colour).contrasting());
  53. g.setFont (Font (getHeight() * 0.6f, Font::bold));
  54. g.drawFittedText (colour.toDisplayString (true),
  55. 2, 1, getWidth() - 4, getHeight() - 1,
  56. Justification::centred, 1);
  57. }
  58. virtual void setColour (Colour newColour) = 0;
  59. virtual void resetToDefault() = 0;
  60. virtual Colour getColour() const = 0;
  61. void refresh()
  62. {
  63. const Colour col (getColour());
  64. if (col != colour)
  65. {
  66. colour = col;
  67. repaint();
  68. }
  69. }
  70. void mouseDown (const MouseEvent&)
  71. {
  72. CallOutBox::launchAsynchronously (new ColourSelectorComp (this, canResetToDefault),
  73. getScreenBounds(), nullptr);
  74. }
  75. void changeListenerCallback (ChangeBroadcaster* source)
  76. {
  77. const ColourSelector* const cs = (const ColourSelector*) source;
  78. if (cs->getCurrentColour() != getColour())
  79. setColour (cs->getCurrentColour());
  80. }
  81. class ColourSelectorComp : public Component,
  82. public ButtonListener
  83. {
  84. public:
  85. ColourSelectorComp (ColourEditorComponent* owner_,
  86. const bool canReset)
  87. : owner (owner_),
  88. defaultButton ("Reset to Default")
  89. {
  90. addAndMakeVisible (selector);
  91. selector.setName ("Colour");
  92. selector.setCurrentColour (owner->getColour());
  93. selector.addChangeListener (owner);
  94. if (canReset)
  95. {
  96. addAndMakeVisible (defaultButton);
  97. defaultButton.addListener (this);
  98. }
  99. setSize (300, 400);
  100. }
  101. void resized()
  102. {
  103. if (defaultButton.isVisible())
  104. {
  105. selector.setBounds (0, 0, getWidth(), getHeight() - 30);
  106. defaultButton.changeWidthToFitText (22);
  107. defaultButton.setTopLeftPosition (10, getHeight() - 26);
  108. }
  109. else
  110. {
  111. selector.setBounds (getLocalBounds());
  112. }
  113. }
  114. void buttonClicked (Button*)
  115. {
  116. owner->resetToDefault();
  117. owner->refresh();
  118. selector.setCurrentColour (owner->getColour());
  119. }
  120. private:
  121. class ColourSelectorWithSwatches : public ColourSelector
  122. {
  123. public:
  124. ColourSelectorWithSwatches()
  125. {
  126. }
  127. int getNumSwatches() const override
  128. {
  129. return getAppSettings().swatchColours.size();
  130. }
  131. Colour getSwatchColour (int index) const override
  132. {
  133. return getAppSettings().swatchColours [index];
  134. }
  135. void setSwatchColour (int index, const Colour& newColour) const override
  136. {
  137. getAppSettings().swatchColours.set (index, newColour);
  138. }
  139. };
  140. ColourEditorComponent* owner;
  141. ColourSelectorWithSwatches selector;
  142. TextButton defaultButton;
  143. };
  144. private:
  145. Colour colour;
  146. bool canResetToDefault;
  147. };
  148. class ColourPropEditorComponent : public ColourEditorComponent
  149. {
  150. JucerColourPropertyComponent* const owner;
  151. public:
  152. ColourPropEditorComponent (JucerColourPropertyComponent* const owner_,
  153. const bool canReset)
  154. : ColourEditorComponent (canReset),
  155. owner (owner_)
  156. {}
  157. void setColour (Colour newColour) override
  158. {
  159. owner->setColour (newColour);
  160. }
  161. Colour getColour() const override
  162. {
  163. return owner->getColour();
  164. }
  165. void resetToDefault() override
  166. {
  167. owner->resetToDefault();
  168. }
  169. };
  170. ScopedPointer<ColourPropEditorComponent> colourPropEditor;
  171. };
  172. #endif // JUCER_COLOURPROPERTYCOMPONENT_H_INCLUDED