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.

178 lines
5.6KB

  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. ~ComponentColourIdProperty()
  67. {
  68. }
  69. //==============================================================================
  70. Colour getColour() const
  71. {
  72. return component->findColour (colourId);
  73. }
  74. void setColour (const Colour& newColour)
  75. {
  76. if (component->findColour (colourId) != newColour)
  77. {
  78. document.getUndoManager().undoCurrentTransactionOnly();
  79. document.perform (new ColourChangeAction (component,
  80. *document.getComponentLayout(),
  81. colourId,
  82. newColour,
  83. false),
  84. "Change colour");
  85. }
  86. }
  87. void resetToDefault()
  88. {
  89. document.getUndoManager().undoCurrentTransactionOnly();
  90. document.perform (new ColourChangeAction (component,
  91. *document.getComponentLayout(),
  92. colourId,
  93. Colours::black,
  94. true),
  95. "Reset colour");
  96. }
  97. private:
  98. const int colourId;
  99. class ColourChangeAction : public ComponentUndoableAction <Component>
  100. {
  101. public:
  102. ColourChangeAction (Component* const comp,
  103. ComponentLayout& layout,
  104. const int colourId_,
  105. const Colour& newColour_,
  106. const bool newColourIsDefault)
  107. : ComponentUndoableAction <Component> (comp, layout),
  108. colourId (colourId_),
  109. newColour (newColour_),
  110. isDefault (newColourIsDefault)
  111. {
  112. }
  113. bool perform()
  114. {
  115. showCorrectTab();
  116. wasSpecified = getComponent()->isColourSpecified (colourId);
  117. oldColour = getComponent()->findColour (colourId);
  118. if (isDefault)
  119. getComponent()->removeColour (colourId);
  120. else
  121. getComponent()->setColour (colourId, newColour);
  122. changed();
  123. return true;
  124. }
  125. bool undo()
  126. {
  127. showCorrectTab();
  128. if (wasSpecified)
  129. getComponent()->setColour (colourId, oldColour);
  130. else
  131. getComponent()->removeColour (colourId);
  132. if (TextEditor* const te = dynamic_cast <TextEditor*> (getComponent()))
  133. te->applyFontToAllText (te->getFont());
  134. changed();
  135. return true;
  136. }
  137. int colourId;
  138. Colour newColour, oldColour;
  139. bool isDefault, wasSpecified;
  140. };
  141. };
  142. #endif // __JUCER_COMPONENTCOLOURPROPERTY_JUCEHEADER__