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.

216 lines
6.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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_JUCEHEADER__
  18. #define __JUCER_COLOURPROPERTYCOMPONENT_JUCEHEADER__
  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 (const 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 (const 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. ColourSelectorComp colourSelector (this, canResetToDefault);
  73. PopupMenu m;
  74. m.addCustomItem (1234, &colourSelector, 300, 400, false);
  75. m.showAt (this);
  76. }
  77. void changeListenerCallback (ChangeBroadcaster* source)
  78. {
  79. const ColourSelector* const cs = (const ColourSelector*) source;
  80. if (cs->getCurrentColour() != getColour())
  81. setColour (cs->getCurrentColour());
  82. }
  83. class ColourSelectorComp : public Component,
  84. public ButtonListener
  85. {
  86. public:
  87. ColourSelectorComp (ColourEditorComponent* owner_,
  88. const bool canReset)
  89. : owner (owner_),
  90. defaultButton ("Reset to Default")
  91. {
  92. addAndMakeVisible (&selector);
  93. selector.setName ("Colour");
  94. selector.setCurrentColour (owner->getColour());
  95. selector.addChangeListener (owner);
  96. if (canReset)
  97. {
  98. addAndMakeVisible (&defaultButton);
  99. defaultButton.addListener (this);
  100. }
  101. }
  102. void resized()
  103. {
  104. if (defaultButton.isVisible())
  105. {
  106. selector.setBounds (0, 0, getWidth(), getHeight() - 30);
  107. defaultButton.changeWidthToFitText (22);
  108. defaultButton.setTopLeftPosition (10, getHeight() - 26);
  109. }
  110. else
  111. {
  112. selector.setBounds (getLocalBounds());
  113. }
  114. }
  115. void buttonClicked (Button*)
  116. {
  117. owner->resetToDefault();
  118. owner->refresh();
  119. selector.setCurrentColour (owner->getColour());
  120. }
  121. private:
  122. class ColourSelectorWithSwatches : public ColourSelector
  123. {
  124. public:
  125. ColourSelectorWithSwatches()
  126. {
  127. }
  128. int getNumSwatches() const
  129. {
  130. return getAppSettings().swatchColours.size();
  131. }
  132. Colour getSwatchColour (int index) const
  133. {
  134. return getAppSettings().swatchColours [index];
  135. }
  136. void setSwatchColour (int index, const Colour& newColour) const
  137. {
  138. getAppSettings().swatchColours.set (index, newColour);
  139. }
  140. };
  141. ColourEditorComponent* owner;
  142. ColourSelectorWithSwatches selector;
  143. TextButton defaultButton;
  144. };
  145. private:
  146. Colour colour;
  147. bool canResetToDefault;
  148. };
  149. class ColourPropEditorComponent : public ColourEditorComponent
  150. {
  151. JucerColourPropertyComponent* const owner;
  152. public:
  153. ColourPropEditorComponent (JucerColourPropertyComponent* const owner_,
  154. const bool canReset)
  155. : ColourEditorComponent (canReset),
  156. owner (owner_)
  157. {}
  158. void setColour (const Colour& newColour)
  159. {
  160. owner->setColour (newColour);
  161. }
  162. Colour getColour() const
  163. {
  164. return owner->getColour();
  165. }
  166. void resetToDefault()
  167. {
  168. owner->resetToDefault();
  169. }
  170. };
  171. ScopedPointer<ColourPropEditorComponent> colourPropEditor;
  172. };
  173. #endif // __JUCER_COLOURPROPERTYCOMPONENT_JUCEHEADER__