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.

149 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-12 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../JuceDemoHeader.h"
  19. #if JUCE_WEB_BROWSER
  20. //==============================================================================
  21. /** We'll use a subclass of WebBrowserComponent to demonstrate how to get callbacks
  22. when the browser changes URL. You don't need to do this, you can just also
  23. just use the WebBrowserComponent class directly.
  24. */
  25. class DemoBrowserComponent : public WebBrowserComponent
  26. {
  27. public:
  28. //==============================================================================
  29. DemoBrowserComponent (TextEditor& addressBox) : addressTextBox (addressBox)
  30. {
  31. }
  32. // This method gets called when the browser is about to go to a new URL..
  33. bool pageAboutToLoad (const String& newURL) override
  34. {
  35. // We'll just update our address box to reflect the new location..
  36. addressTextBox.setText (newURL, false);
  37. // we could return false here to tell the browser not to go ahead with
  38. // loading the page.
  39. return true;
  40. }
  41. // This method gets called when the browser is requested to launch a new window
  42. void newWindowAttemptingToLoad (const String& newURL) override
  43. {
  44. // We'll just load the URL into the main window
  45. goToURL (newURL);
  46. }
  47. private:
  48. TextEditor& addressTextBox;
  49. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DemoBrowserComponent)
  50. };
  51. //==============================================================================
  52. class WebBrowserDemo : public Component,
  53. private TextEditor::Listener,
  54. private ButtonListener
  55. {
  56. public:
  57. WebBrowserDemo()
  58. : goButton ("Go", "Go to URL"),
  59. backButton ("<<", "Back"),
  60. forwardButton (">>", "Forward")
  61. {
  62. setOpaque (true);
  63. // Create an address box..
  64. addAndMakeVisible (addressTextBox);
  65. addressTextBox.setTextToShowWhenEmpty ("Enter a web address, e.g. http://www.juce.com", Colours::grey);
  66. addressTextBox.addListener (this);
  67. // create the actual browser component
  68. addAndMakeVisible (webView = new DemoBrowserComponent (addressTextBox));
  69. // add some buttons..
  70. addAndMakeVisible (goButton);
  71. goButton.addListener (this);
  72. addAndMakeVisible (backButton);
  73. backButton.addListener (this);
  74. addAndMakeVisible (forwardButton);
  75. forwardButton.addListener (this);
  76. // send the browser to a start page..
  77. webView->goToURL ("http://www.juce.com");
  78. }
  79. void paint (Graphics& g) override
  80. {
  81. g.fillAll (Colours::grey);
  82. }
  83. void resized() override
  84. {
  85. webView->setBounds (10, 45, getWidth() - 20, getHeight() - 55);
  86. goButton.setBounds (getWidth() - 45, 10, 35, 25);
  87. addressTextBox.setBounds (100, 10, getWidth() - 155, 25);
  88. backButton.setBounds (10, 10, 35, 25);
  89. forwardButton.setBounds (55, 10, 35, 25);
  90. }
  91. private:
  92. ScopedPointer<DemoBrowserComponent> webView;
  93. TextEditor addressTextBox;
  94. TextButton goButton, backButton, forwardButton;
  95. void textEditorTextChanged (TextEditor&) override {}
  96. void textEditorEscapeKeyPressed (TextEditor&) override {}
  97. void textEditorFocusLost (TextEditor&) override {}
  98. void textEditorReturnKeyPressed (TextEditor&) override
  99. {
  100. webView->goToURL (addressTextBox.getText());
  101. }
  102. void buttonClicked (Button* b) override
  103. {
  104. if (b == &backButton)
  105. webView->goBack();
  106. else if (b == &forwardButton)
  107. webView->goForward();
  108. else if (b == &goButton)
  109. webView->goToURL (addressTextBox.getText());
  110. }
  111. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WebBrowserDemo);
  112. };
  113. // This static object will register this demo type in a global list of demos..
  114. static JuceDemoType<WebBrowserDemo> demo ("10 Components: Web Browser");
  115. #endif // JUCE_WEB_BROWSER