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.

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