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.4KB

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