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.

166 lines
5.3KB

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