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.

141 lines
4.8KB

  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 HyperlinkButtonHandler : public ButtonHandler
  20. {
  21. public:
  22. HyperlinkButtonHandler()
  23. : ButtonHandler ("Hyperlink Button", "HyperlinkButton", typeid (HyperlinkButton), 150, 24)
  24. {
  25. registerColour (HyperlinkButton::textColourId, "text", "textCol");
  26. }
  27. Component* createNewComponent (JucerDocument*)
  28. {
  29. HyperlinkButton* hb = new HyperlinkButton ("new hyperlink", URL ("http://www.juce.com"));
  30. setNeedsButtonListener (hb, false);
  31. return hb;
  32. }
  33. void getEditableProperties (Component* component, JucerDocument& document, Array<PropertyComponent*>& props)
  34. {
  35. HyperlinkButton* const hb = (HyperlinkButton*) component;
  36. ButtonHandler::getEditableProperties (component, document, props);
  37. props.add (new HyperlinkURLProperty (hb, document));
  38. addColourProperties (component, document, props);
  39. }
  40. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  41. {
  42. HyperlinkButton* const hb = (HyperlinkButton*) comp;
  43. XmlElement* const e = ButtonHandler::createXmlFor (comp, layout);
  44. e->setAttribute ("url", hb->getURL().toString (false));
  45. return e;
  46. }
  47. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
  48. {
  49. HyperlinkButton* const hb = (HyperlinkButton*) comp;
  50. if (! ButtonHandler::restoreFromXml (xml, comp, layout))
  51. return false;
  52. hb->setURL (URL (xml.getStringAttribute ("url", hb->getURL().toString (false))));
  53. return true;
  54. }
  55. String getCreationParameters (GeneratedCode& code, Component* comp)
  56. {
  57. HyperlinkButton* const hb = dynamic_cast<HyperlinkButton*> (comp);
  58. return quotedString (hb->getButtonText(), code.shouldUseTransMacro())
  59. + ",\nURL ("
  60. + quotedString (hb->getURL().toString (false), false)
  61. + ")";
  62. }
  63. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  64. {
  65. ButtonHandler::fillInCreationCode (code, component, memberVariableName);
  66. code.constructorCode << getColourIntialisationCode (component, memberVariableName)
  67. << '\n';
  68. }
  69. private:
  70. //==============================================================================
  71. class HyperlinkURLProperty : public ComponentTextProperty <HyperlinkButton>
  72. {
  73. public:
  74. HyperlinkURLProperty (HyperlinkButton* comp, JucerDocument& doc)
  75. : ComponentTextProperty <HyperlinkButton> ("URL", 512, false, comp, doc)
  76. {}
  77. void setText (const String& newText) override
  78. {
  79. document.perform (new HyperlinkURLChangeAction (component, *document.getComponentLayout(), URL::createWithoutParsing (newText)),
  80. "Change hyperlink URL");
  81. }
  82. String getText() const override
  83. {
  84. return component->getURL().toString (false);
  85. }
  86. private:
  87. class HyperlinkURLChangeAction : public ComponentUndoableAction <HyperlinkButton>
  88. {
  89. public:
  90. HyperlinkURLChangeAction (HyperlinkButton* const comp, ComponentLayout& l, const URL& newState_)
  91. : ComponentUndoableAction <HyperlinkButton> (comp, l),
  92. newState (newState_)
  93. {
  94. oldState = comp->getURL();
  95. }
  96. bool perform()
  97. {
  98. showCorrectTab();
  99. getComponent()->setURL (newState);
  100. changed();
  101. return true;
  102. }
  103. bool undo()
  104. {
  105. showCorrectTab();
  106. getComponent()->setURL (oldState);
  107. changed();
  108. return true;
  109. }
  110. URL newState, oldState;
  111. };
  112. };
  113. };