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.0KB

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