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.

172 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. #include "jucer_ColourPropertyComponent.h"
  21. //==============================================================================
  22. /**
  23. */
  24. template <class ComponentType>
  25. class ComponentColourProperty : public JucerColourPropertyComponent,
  26. private ChangeListener
  27. {
  28. public:
  29. ComponentColourProperty (const String& name,
  30. ComponentType* comp,
  31. JucerDocument& doc,
  32. const bool canResetToDefault)
  33. : JucerColourPropertyComponent (name, canResetToDefault),
  34. component (comp),
  35. document (doc)
  36. {
  37. document.addChangeListener (this);
  38. }
  39. ~ComponentColourProperty()
  40. {
  41. document.removeChangeListener (this);
  42. }
  43. void changeListenerCallback (ChangeBroadcaster*)
  44. {
  45. refresh();
  46. }
  47. protected:
  48. ComponentType* component;
  49. JucerDocument& document;
  50. };
  51. //==============================================================================
  52. /**
  53. */
  54. class ComponentColourIdProperty : public ComponentColourProperty <Component>
  55. {
  56. public:
  57. //==============================================================================
  58. ComponentColourIdProperty (Component* const comp,
  59. JucerDocument& doc,
  60. const int colourId_,
  61. const String& name,
  62. const bool canResetToDefault)
  63. : ComponentColourProperty <Component> (name, comp, doc, canResetToDefault),
  64. colourId (colourId_)
  65. {
  66. }
  67. //==============================================================================
  68. Colour getColour() const
  69. {
  70. return component->findColour (colourId);
  71. }
  72. void setColour (Colour newColour)
  73. {
  74. if (component->findColour (colourId) != newColour)
  75. {
  76. document.getUndoManager().undoCurrentTransactionOnly();
  77. document.perform (new ColourChangeAction (component,
  78. *document.getComponentLayout(),
  79. colourId,
  80. newColour,
  81. false),
  82. "Change colour");
  83. }
  84. }
  85. void resetToDefault()
  86. {
  87. document.getUndoManager().undoCurrentTransactionOnly();
  88. document.perform (new ColourChangeAction (component,
  89. *document.getComponentLayout(),
  90. colourId,
  91. Colours::black,
  92. true),
  93. "Reset colour");
  94. }
  95. private:
  96. const int colourId;
  97. class ColourChangeAction : public ComponentUndoableAction <Component>
  98. {
  99. public:
  100. ColourChangeAction (Component* const comp,
  101. ComponentLayout& l,
  102. const int colourId_,
  103. Colour newColour_,
  104. const bool newColourIsDefault)
  105. : ComponentUndoableAction<Component> (comp, l),
  106. colourId (colourId_),
  107. newColour (newColour_),
  108. isDefault (newColourIsDefault)
  109. {
  110. }
  111. bool perform()
  112. {
  113. showCorrectTab();
  114. wasSpecified = getComponent()->isColourSpecified (colourId);
  115. oldColour = getComponent()->findColour (colourId);
  116. if (isDefault)
  117. getComponent()->removeColour (colourId);
  118. else
  119. getComponent()->setColour (colourId, newColour);
  120. changed();
  121. return true;
  122. }
  123. bool undo()
  124. {
  125. showCorrectTab();
  126. if (wasSpecified)
  127. getComponent()->setColour (colourId, oldColour);
  128. else
  129. getComponent()->removeColour (colourId);
  130. if (TextEditor* const te = dynamic_cast<TextEditor*> (getComponent()))
  131. te->applyFontToAllText (te->getFont());
  132. changed();
  133. return true;
  134. }
  135. int colourId;
  136. Colour newColour, oldColour;
  137. bool isDefault, wasSpecified;
  138. };
  139. };