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.

148 lines
4.9KB

  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. #include "../JuceDemoHeader.h"
  18. #if JUCE_WEB_BROWSER
  19. //==============================================================================
  20. /** We'll use a subclass of WebBrowserComponent to demonstrate how to get callbacks
  21. when the browser changes URL. You don't need to do this, you can just also
  22. just use the WebBrowserComponent class directly.
  23. */
  24. class DemoBrowserComponent : public WebBrowserComponent
  25. {
  26. public:
  27. //==============================================================================
  28. DemoBrowserComponent (TextEditor& addressBox) : addressTextBox (addressBox)
  29. {
  30. }
  31. // This method gets called when the browser is about to go to a new URL..
  32. bool pageAboutToLoad (const String& newURL) override
  33. {
  34. // We'll just update our address box to reflect the new location..
  35. addressTextBox.setText (newURL, false);
  36. // we could return false here to tell the browser not to go ahead with
  37. // loading the page.
  38. return true;
  39. }
  40. // This method gets called when the browser is requested to launch a new window
  41. void newWindowAttemptingToLoad (const String& newURL) override
  42. {
  43. // We'll just load the URL into the main window
  44. goToURL (newURL);
  45. }
  46. private:
  47. TextEditor& addressTextBox;
  48. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DemoBrowserComponent)
  49. };
  50. //==============================================================================
  51. class WebBrowserDemo : public Component,
  52. private TextEditor::Listener,
  53. private ButtonListener
  54. {
  55. public:
  56. WebBrowserDemo()
  57. : goButton ("Go", "Go to URL"),
  58. backButton ("<<", "Back"),
  59. forwardButton (">>", "Forward")
  60. {
  61. setOpaque (true);
  62. // Create an address box..
  63. addAndMakeVisible (addressTextBox);
  64. addressTextBox.setTextToShowWhenEmpty ("Enter a web address, e.g. https://www.juce.com", Colours::grey);
  65. addressTextBox.addListener (this);
  66. // create the actual browser component
  67. addAndMakeVisible (webView = new DemoBrowserComponent (addressTextBox));
  68. // add some buttons..
  69. addAndMakeVisible (goButton);
  70. goButton.addListener (this);
  71. addAndMakeVisible (backButton);
  72. backButton.addListener (this);
  73. addAndMakeVisible (forwardButton);
  74. forwardButton.addListener (this);
  75. // send the browser to a start page..
  76. webView->goToURL ("https://www.juce.com");
  77. }
  78. void paint (Graphics& g) override
  79. {
  80. g.fillAll (Colours::grey);
  81. }
  82. void resized() override
  83. {
  84. webView->setBounds (10, 45, getWidth() - 20, getHeight() - 55);
  85. goButton.setBounds (getWidth() - 45, 10, 35, 25);
  86. addressTextBox.setBounds (100, 10, getWidth() - 155, 25);
  87. backButton.setBounds (10, 10, 35, 25);
  88. forwardButton.setBounds (55, 10, 35, 25);
  89. }
  90. private:
  91. ScopedPointer<DemoBrowserComponent> webView;
  92. TextEditor addressTextBox;
  93. TextButton goButton, backButton, forwardButton;
  94. void textEditorTextChanged (TextEditor&) override {}
  95. void textEditorEscapeKeyPressed (TextEditor&) override {}
  96. void textEditorFocusLost (TextEditor&) override {}
  97. void textEditorReturnKeyPressed (TextEditor&) override
  98. {
  99. webView->goToURL (addressTextBox.getText());
  100. }
  101. void buttonClicked (Button* b) override
  102. {
  103. if (b == &backButton)
  104. webView->goBack();
  105. else if (b == &forwardButton)
  106. webView->goForward();
  107. else if (b == &goButton)
  108. webView->goToURL (addressTextBox.getText());
  109. }
  110. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WebBrowserDemo)
  111. };
  112. // This static object will register this demo type in a global list of demos..
  113. static JuceDemoType<WebBrowserDemo> demo ("10 Components: Web Browser");
  114. #endif // JUCE_WEB_BROWSER