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.

150 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. //==============================================================================
  20. class HyperlinkButtonHandler : public ButtonHandler
  21. {
  22. public:
  23. HyperlinkButtonHandler()
  24. : ButtonHandler ("Hyperlink Button", "juce::HyperlinkButton", typeid (HyperlinkButton), 150, 24)
  25. {
  26. registerColour (juce::HyperlinkButton::textColourId, "text", "textCol");
  27. }
  28. Component* createNewComponent (JucerDocument*) override
  29. {
  30. HyperlinkButton* hb = new HyperlinkButton ("new hyperlink", URL ("http://www.juce.com"));
  31. setNeedsButtonListener (hb, false);
  32. return hb;
  33. }
  34. void getEditableProperties (Component* component, JucerDocument& document,
  35. Array<PropertyComponent*>& props, bool multipleSelected) override
  36. {
  37. ButtonHandler::getEditableProperties (component, document, props, multipleSelected);
  38. if (multipleSelected)
  39. return;
  40. if (auto* hb = dynamic_cast<HyperlinkButton*> (component))
  41. props.add (new HyperlinkURLProperty (hb, document));
  42. addColourProperties (component, document, props);
  43. }
  44. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout) override
  45. {
  46. HyperlinkButton* const hb = (HyperlinkButton*) comp;
  47. XmlElement* const e = ButtonHandler::createXmlFor (comp, layout);
  48. e->setAttribute ("url", hb->getURL().toString (false));
  49. return e;
  50. }
  51. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout) override
  52. {
  53. HyperlinkButton* const hb = (HyperlinkButton*) comp;
  54. if (! ButtonHandler::restoreFromXml (xml, comp, layout))
  55. return false;
  56. hb->setURL (URL (xml.getStringAttribute ("url", hb->getURL().toString (false))));
  57. return true;
  58. }
  59. String getCreationParameters (GeneratedCode& code, Component* comp) override
  60. {
  61. HyperlinkButton* const hb = dynamic_cast<HyperlinkButton*> (comp);
  62. return quotedString (hb->getButtonText(), code.shouldUseTransMacro())
  63. + ",\nURL ("
  64. + quotedString (hb->getURL().toString (false), false)
  65. + ")";
  66. }
  67. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName) override
  68. {
  69. ButtonHandler::fillInCreationCode (code, component, memberVariableName);
  70. code.constructorCode << getColourIntialisationCode (component, memberVariableName)
  71. << '\n';
  72. }
  73. private:
  74. //==============================================================================
  75. class HyperlinkURLProperty : public ComponentTextProperty <HyperlinkButton>
  76. {
  77. public:
  78. HyperlinkURLProperty (HyperlinkButton* comp, JucerDocument& doc)
  79. : ComponentTextProperty <HyperlinkButton> ("URL", 512, false, comp, doc)
  80. {}
  81. void setText (const String& newText) override
  82. {
  83. document.perform (new HyperlinkURLChangeAction (component, *document.getComponentLayout(), URL::createWithoutParsing (newText)),
  84. "Change hyperlink URL");
  85. }
  86. String getText() const override
  87. {
  88. return component->getURL().toString (false);
  89. }
  90. private:
  91. class HyperlinkURLChangeAction : public ComponentUndoableAction <HyperlinkButton>
  92. {
  93. public:
  94. HyperlinkURLChangeAction (HyperlinkButton* const comp, ComponentLayout& l, const URL& newState_)
  95. : ComponentUndoableAction <HyperlinkButton> (comp, l),
  96. newState (newState_)
  97. {
  98. oldState = comp->getURL();
  99. }
  100. bool perform()
  101. {
  102. showCorrectTab();
  103. getComponent()->setURL (newState);
  104. changed();
  105. return true;
  106. }
  107. bool undo()
  108. {
  109. showCorrectTab();
  110. getComponent()->setURL (oldState);
  111. changed();
  112. return true;
  113. }
  114. URL newState, oldState;
  115. };
  116. };
  117. };