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.

217 lines
6.9KB

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