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.

144 lines
4.6KB

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