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.

143 lines
4.8KB

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