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.

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