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.

174 lines
5.5KB

  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_COMPONENTCOLOURPROPERTY_JUCEHEADER__
  18. #define __JUCER_COMPONENTCOLOURPROPERTY_JUCEHEADER__
  19. #include "jucer_ColourPropertyComponent.h"
  20. //==============================================================================
  21. /**
  22. */
  23. template <class ComponentType>
  24. class ComponentColourProperty : public JucerColourPropertyComponent,
  25. private ChangeListener
  26. {
  27. public:
  28. ComponentColourProperty (const String& name,
  29. ComponentType* comp,
  30. JucerDocument& doc,
  31. const bool canResetToDefault)
  32. : JucerColourPropertyComponent (name, canResetToDefault),
  33. component (comp),
  34. document (doc)
  35. {
  36. document.addChangeListener (this);
  37. }
  38. ~ComponentColourProperty()
  39. {
  40. document.removeChangeListener (this);
  41. }
  42. void changeListenerCallback (ChangeBroadcaster*)
  43. {
  44. refresh();
  45. }
  46. protected:
  47. ComponentType* component;
  48. JucerDocument& document;
  49. };
  50. //==============================================================================
  51. /**
  52. */
  53. class ComponentColourIdProperty : public ComponentColourProperty <Component>
  54. {
  55. public:
  56. //==============================================================================
  57. ComponentColourIdProperty (Component* const comp,
  58. JucerDocument& doc,
  59. const int colourId_,
  60. const String& name,
  61. const bool canResetToDefault)
  62. : ComponentColourProperty <Component> (name, comp, doc, canResetToDefault),
  63. colourId (colourId_)
  64. {
  65. }
  66. //==============================================================================
  67. Colour getColour() const
  68. {
  69. return component->findColour (colourId);
  70. }
  71. void setColour (const Colour& newColour)
  72. {
  73. if (component->findColour (colourId) != newColour)
  74. {
  75. document.getUndoManager().undoCurrentTransactionOnly();
  76. document.perform (new ColourChangeAction (component,
  77. *document.getComponentLayout(),
  78. colourId,
  79. newColour,
  80. false),
  81. "Change colour");
  82. }
  83. }
  84. void resetToDefault()
  85. {
  86. document.getUndoManager().undoCurrentTransactionOnly();
  87. document.perform (new ColourChangeAction (component,
  88. *document.getComponentLayout(),
  89. colourId,
  90. Colours::black,
  91. true),
  92. "Reset colour");
  93. }
  94. private:
  95. const int colourId;
  96. class ColourChangeAction : public ComponentUndoableAction <Component>
  97. {
  98. public:
  99. ColourChangeAction (Component* const comp,
  100. ComponentLayout& layout,
  101. const int colourId_,
  102. const Colour& newColour_,
  103. const bool newColourIsDefault)
  104. : ComponentUndoableAction <Component> (comp, layout),
  105. colourId (colourId_),
  106. newColour (newColour_),
  107. isDefault (newColourIsDefault)
  108. {
  109. }
  110. bool perform()
  111. {
  112. showCorrectTab();
  113. wasSpecified = getComponent()->isColourSpecified (colourId);
  114. oldColour = getComponent()->findColour (colourId);
  115. if (isDefault)
  116. getComponent()->removeColour (colourId);
  117. else
  118. getComponent()->setColour (colourId, newColour);
  119. changed();
  120. return true;
  121. }
  122. bool undo()
  123. {
  124. showCorrectTab();
  125. if (wasSpecified)
  126. getComponent()->setColour (colourId, oldColour);
  127. else
  128. getComponent()->removeColour (colourId);
  129. if (TextEditor* const te = dynamic_cast <TextEditor*> (getComponent()))
  130. te->applyFontToAllText (te->getFont());
  131. changed();
  132. return true;
  133. }
  134. int colourId;
  135. Colour newColour, oldColour;
  136. bool isDefault, wasSpecified;
  137. };
  138. };
  139. #endif // __JUCER_COMPONENTCOLOURPROPERTY_JUCEHEADER__