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.

179 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_COMPONENTCOLOURPROPERTY_JUCEHEADER__
  19. #define __JUCER_COMPONENTCOLOURPROPERTY_JUCEHEADER__
  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. ~ComponentColourIdProperty()
  68. {
  69. }
  70. //==============================================================================
  71. Colour getColour() const
  72. {
  73. return component->findColour (colourId);
  74. }
  75. void setColour (const Colour& newColour)
  76. {
  77. if (component->findColour (colourId) != newColour)
  78. {
  79. document.getUndoManager().undoCurrentTransactionOnly();
  80. document.perform (new ColourChangeAction (component,
  81. *document.getComponentLayout(),
  82. colourId,
  83. newColour,
  84. false),
  85. "Change colour");
  86. }
  87. }
  88. void resetToDefault()
  89. {
  90. document.getUndoManager().undoCurrentTransactionOnly();
  91. document.perform (new ColourChangeAction (component,
  92. *document.getComponentLayout(),
  93. colourId,
  94. Colours::black,
  95. true),
  96. "Reset colour");
  97. }
  98. private:
  99. const int colourId;
  100. class ColourChangeAction : public ComponentUndoableAction <Component>
  101. {
  102. public:
  103. ColourChangeAction (Component* const comp,
  104. ComponentLayout& layout,
  105. const int colourId_,
  106. const Colour& newColour_,
  107. const bool newColourIsDefault)
  108. : ComponentUndoableAction <Component> (comp, layout),
  109. colourId (colourId_),
  110. newColour (newColour_),
  111. isDefault (newColourIsDefault)
  112. {
  113. }
  114. bool perform()
  115. {
  116. showCorrectTab();
  117. wasSpecified = getComponent()->isColourSpecified (colourId);
  118. oldColour = getComponent()->findColour (colourId);
  119. if (isDefault)
  120. getComponent()->removeColour (colourId);
  121. else
  122. getComponent()->setColour (colourId, newColour);
  123. changed();
  124. return true;
  125. }
  126. bool undo()
  127. {
  128. showCorrectTab();
  129. if (wasSpecified)
  130. getComponent()->setColour (colourId, oldColour);
  131. else
  132. getComponent()->removeColour (colourId);
  133. if (TextEditor* const te = dynamic_cast <TextEditor*> (getComponent()))
  134. te->applyFontToAllText (te->getFont());
  135. changed();
  136. return true;
  137. }
  138. int colourId;
  139. Colour newColour, oldColour;
  140. bool isDefault, wasSpecified;
  141. };
  142. };
  143. #endif // __JUCER_COMPONENTCOLOURPROPERTY_JUCEHEADER__