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.

147 lines
5.0KB

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