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.

139 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class HyperlinkButtonHandler : public ButtonHandler
  18. {
  19. public:
  20. HyperlinkButtonHandler()
  21. : ButtonHandler ("Hyperlink Button", "HyperlinkButton", typeid (HyperlinkButton), 150, 24)
  22. {
  23. registerColour (HyperlinkButton::textColourId, "text", "textCol");
  24. }
  25. Component* createNewComponent (JucerDocument*)
  26. {
  27. HyperlinkButton* hb = new HyperlinkButton ("new hyperlink", URL ("http://www.juce.com"));
  28. setNeedsButtonListener (hb, false);
  29. return hb;
  30. }
  31. void getEditableProperties (Component* component, JucerDocument& document, Array<PropertyComponent*>& props)
  32. {
  33. HyperlinkButton* const hb = (HyperlinkButton*) component;
  34. ButtonHandler::getEditableProperties (component, document, props);
  35. props.add (new HyperlinkURLProperty (hb, document));
  36. addColourProperties (component, document, props);
  37. }
  38. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
  39. {
  40. HyperlinkButton* const hb = (HyperlinkButton*) comp;
  41. XmlElement* const e = ButtonHandler::createXmlFor (comp, layout);
  42. e->setAttribute ("url", hb->getURL().toString (false));
  43. return e;
  44. }
  45. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
  46. {
  47. HyperlinkButton* const hb = (HyperlinkButton*) comp;
  48. if (! ButtonHandler::restoreFromXml (xml, comp, layout))
  49. return false;
  50. hb->setURL (URL (xml.getStringAttribute ("url", hb->getURL().toString (false))));
  51. return true;
  52. }
  53. String getCreationParameters (GeneratedCode& code, Component* comp)
  54. {
  55. HyperlinkButton* const hb = dynamic_cast<HyperlinkButton*> (comp);
  56. return quotedString (hb->getButtonText(), code.shouldUseTransMacro())
  57. + ",\nURL ("
  58. + quotedString (hb->getURL().toString (false), false)
  59. + ")";
  60. }
  61. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
  62. {
  63. ButtonHandler::fillInCreationCode (code, component, memberVariableName);
  64. code.constructorCode << getColourIntialisationCode (component, memberVariableName)
  65. << '\n';
  66. }
  67. private:
  68. //==============================================================================
  69. class HyperlinkURLProperty : public ComponentTextProperty <HyperlinkButton>
  70. {
  71. public:
  72. HyperlinkURLProperty (HyperlinkButton* comp, JucerDocument& doc)
  73. : ComponentTextProperty <HyperlinkButton> ("URL", 512, false, comp, doc)
  74. {}
  75. void setText (const String& newText) override
  76. {
  77. document.perform (new HyperlinkURLChangeAction (component, *document.getComponentLayout(), URL (newText)),
  78. "Change hyperlink URL");
  79. }
  80. String getText() const override
  81. {
  82. return component->getURL().toString (false);
  83. }
  84. private:
  85. class HyperlinkURLChangeAction : public ComponentUndoableAction <HyperlinkButton>
  86. {
  87. public:
  88. HyperlinkURLChangeAction (HyperlinkButton* const comp, ComponentLayout& l, const URL& newState_)
  89. : ComponentUndoableAction <HyperlinkButton> (comp, l),
  90. newState (newState_)
  91. {
  92. oldState = comp->getURL();
  93. }
  94. bool perform()
  95. {
  96. showCorrectTab();
  97. getComponent()->setURL (newState);
  98. changed();
  99. return true;
  100. }
  101. bool undo()
  102. {
  103. showCorrectTab();
  104. getComponent()->setURL (oldState);
  105. changed();
  106. return true;
  107. }
  108. URL newState, oldState;
  109. };
  110. };
  111. };