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.

159 lines
5.0KB

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