/* ============================================================================== This file is part of the JUCE library. Copyright (c) 2015 - ROLI Ltd. Permission is granted to use this software under the terms of either: a) the GPL v2 (or any later version) b) the Affero GPL v3 Details of these licenses can be found at: www.gnu.org/licenses JUCE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ------------------------------------------------------------------------------ To release a closed-source product which uses JUCE, commercial licenses are available: visit www.juce.com for more information. ============================================================================== */ class ButtonHandler : public ComponentTypeHandler { public: ButtonHandler (const String& typeDescription_, const String& className_, const std::type_info& componentClass, const int defaultWidth_, const int defaultHeight_) : ComponentTypeHandler (typeDescription_, className_, componentClass, defaultWidth_, defaultHeight_) {} void getEditableProperties (Component* component, JucerDocument& document, Array& props) { ComponentTypeHandler::getEditableProperties (component, document, props); Button* const b = dynamic_cast (component); props.add (new ButtonTextProperty (b, document)); props.add (new ButtonCallbackProperty (b, document)); props.add (new ButtonRadioGroupProperty (b, document)); props.add (new ButtonConnectedEdgeProperty ("connected left", Button::ConnectedOnLeft, b, document)); props.add (new ButtonConnectedEdgeProperty ("connected right", Button::ConnectedOnRight, b, document)); props.add (new ButtonConnectedEdgeProperty ("connected top", Button::ConnectedOnTop, b, document)); props.add (new ButtonConnectedEdgeProperty ("connected bottom", Button::ConnectedOnBottom, b, document)); } XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout) { Button* const b = dynamic_cast (comp); XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout); e->setAttribute ("buttonText", b->getButtonText()); e->setAttribute ("connectedEdges", b->getConnectedEdgeFlags()); e->setAttribute ("needsCallback", needsButtonListener (b)); e->setAttribute ("radioGroupId", b->getRadioGroupId()); return e; } bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout) { Button* const b = dynamic_cast (comp); if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout)) return false; b->setButtonText (xml.getStringAttribute ("buttonText", b->getButtonText())); b->setConnectedEdges (xml.getIntAttribute ("connectedEdges", 0)); setNeedsButtonListener (b, xml.getBoolAttribute ("needsCallback", true)); b->setRadioGroupId (xml.getIntAttribute ("radioGroupId", 0)); return true; } String getCreationParameters (GeneratedCode&, Component* component) { return quotedString (component->getName(), false); } void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName) { ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName); Button* const b = dynamic_cast (component); if (b->getButtonText() != b->getName()) { code.constructorCode << memberVariableName << "->setButtonText (" << quotedString (b->getButtonText(), code.shouldUseTransMacro()) << ");\n"; } if (b->getConnectedEdgeFlags() != 0) { StringArray flags; if (b->isConnectedOnLeft()) flags.add ("Button::ConnectedOnLeft"); if (b->isConnectedOnRight()) flags.add ("Button::ConnectedOnRight"); if (b->isConnectedOnTop()) flags.add ("Button::ConnectedOnTop"); if (b->isConnectedOnBottom()) flags.add ("Button::ConnectedOnBottom"); String s; s << memberVariableName << "->setConnectedEdges (" << flags.joinIntoString (" | ") << ");\n"; code.constructorCode += s; } if (b->getRadioGroupId() != 0) code.constructorCode << memberVariableName << "->setRadioGroupId (" << b->getRadioGroupId() << ");\n"; if (needsButtonListener (component)) code.constructorCode << memberVariableName << "->addListener (this);\n"; } void fillInGeneratedCode (Component* component, GeneratedCode& code) { ComponentTypeHandler::fillInGeneratedCode (component, code); if (needsButtonListener (component)) { String& callback = code.getCallbackCode ("public ButtonListener", "void", "buttonClicked (Button* buttonThatWasClicked)", true); if (callback.isNotEmpty()) callback << "else "; const String memberVariableName (code.document->getComponentLayout()->getComponentMemberVariableName (component)); const String userCodeComment ("UserButtonCode_" + memberVariableName); callback << "if (buttonThatWasClicked == " << memberVariableName << ")\n{\n //[" << userCodeComment << "] -- add your button handler code here..\n //[/" << userCodeComment << "]\n}\n"; } } static bool needsButtonListener (Component* button) { return button->getProperties().getWithDefault ("generateListenerCallback", true); } static void setNeedsButtonListener (Component* button, const bool shouldDoCallback) { button->getProperties().set ("generateListenerCallback", shouldDoCallback); } private: //============================================================================== class ButtonTextProperty : public ComponentTextProperty