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.

139 lines
4.6KB

  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. class ToggleButtonHandler : public ButtonHandler
  19. {
  20. public:
  21. ToggleButtonHandler()
  22. : ButtonHandler ("Toggle Button", "ToggleButton", typeid (ToggleButton), 150, 24)
  23. {
  24. registerColour (ToggleButton::textColourId, "text colour", "txtcol");
  25. }
  26. Component* createNewComponent (JucerDocument*)
  27. {
  28. return new ToggleButton ("new toggle button");
  29. }
  30. void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
  31. {
  32. ButtonHandler::getEditableProperties (component, document, properties);
  33. properties.add (new ToggleButtonStateProperty ((ToggleButton*) component, document));
  34. addColourProperties (component, document, properties);
  35. }
  36. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  37. {
  38. ToggleButton* tb = (ToggleButton*) comp;
  39. XmlElement* e = ButtonHandler::createXmlFor (comp, layout);
  40. e->setAttribute ("state", tb->getToggleState());
  41. return e;
  42. }
  43. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
  44. {
  45. ToggleButton* const tb = (ToggleButton*) comp;
  46. if (! ButtonHandler::restoreFromXml (xml, comp, layout))
  47. return false;
  48. tb->setToggleState (xml.getBoolAttribute ("state", false), false);
  49. return true;
  50. }
  51. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  52. {
  53. ButtonHandler::fillInCreationCode (code, component, memberVariableName);
  54. ToggleButton* const tb = dynamic_cast <ToggleButton*> (component);
  55. String s;
  56. if (tb->getToggleState())
  57. s << memberVariableName << "->setToggleState (true, false);\n";
  58. s << getColourIntialisationCode (component, memberVariableName)
  59. << '\n';
  60. code.constructorCode += s;
  61. }
  62. private:
  63. class ToggleButtonStateProperty : public ComponentBooleanProperty <ToggleButton>
  64. {
  65. public:
  66. ToggleButtonStateProperty (ToggleButton* button_, JucerDocument& doc)
  67. : ComponentBooleanProperty <ToggleButton> ("initial state", "on", "off", button_, doc)
  68. {
  69. }
  70. void setState (bool newState)
  71. {
  72. document.perform (new ToggleStateChangeAction (component, *document.getComponentLayout(), newState),
  73. "Change ToggleButton state");
  74. }
  75. bool getState() const
  76. {
  77. return component->getToggleState();
  78. }
  79. private:
  80. class ToggleStateChangeAction : public ComponentUndoableAction <ToggleButton>
  81. {
  82. public:
  83. ToggleStateChangeAction (ToggleButton* const comp, ComponentLayout& layout, const bool newState_)
  84. : ComponentUndoableAction <ToggleButton> (comp, layout),
  85. newState (newState_)
  86. {
  87. oldState = comp->getToggleState();
  88. }
  89. bool perform()
  90. {
  91. showCorrectTab();
  92. getComponent()->setToggleState (newState, false);
  93. changed();
  94. return true;
  95. }
  96. bool undo()
  97. {
  98. showCorrectTab();
  99. getComponent()->setToggleState (oldState, false);
  100. changed();
  101. return true;
  102. }
  103. bool newState, oldState;
  104. };
  105. };
  106. };