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.

170 lines
5.3KB

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