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.

151 lines
5.1KB

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