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.

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