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.

209 lines
6.6KB

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