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 examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: WebBrowserDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Displays a web browser.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics, juce_gui_extra
  26. exporters: xcode_mac, vs2017, linux_make, androidstudio, xcode_iphone
  27. type: Component
  28. mainClass: WebBrowserDemo
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. #include "../Assets/DemoUtilities.h"
  34. //==============================================================================
  35. /** We'll use a subclass of WebBrowserComponent to demonstrate how to get callbacks
  36. when the browser changes URL. You don't need to do this, you can just also
  37. just use the WebBrowserComponent class directly.
  38. */
  39. class DemoBrowserComponent : public WebBrowserComponent
  40. {
  41. public:
  42. //==============================================================================
  43. DemoBrowserComponent (TextEditor& addressBox)
  44. : addressTextBox (addressBox)
  45. {}
  46. // This method gets called when the browser is about to go to a new URL..
  47. bool pageAboutToLoad (const String& newURL) override
  48. {
  49. // We'll just update our address box to reflect the new location..
  50. addressTextBox.setText (newURL, false);
  51. // we could return false here to tell the browser not to go ahead with
  52. // loading the page.
  53. return true;
  54. }
  55. // This method gets called when the browser is requested to launch a new window
  56. void newWindowAttemptingToLoad (const String& newURL) override
  57. {
  58. // We'll just load the URL into the main window
  59. goToURL (newURL);
  60. }
  61. private:
  62. TextEditor& addressTextBox;
  63. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DemoBrowserComponent)
  64. };
  65. //==============================================================================
  66. class WebBrowserDemo : public Component
  67. {
  68. public:
  69. WebBrowserDemo()
  70. {
  71. setOpaque (true);
  72. // Create an address box..
  73. addAndMakeVisible (addressTextBox);
  74. addressTextBox.setTextToShowWhenEmpty ("Enter a web address, e.g. https://www.juce.com", Colours::grey);
  75. addressTextBox.onReturnKey = [this] { webView->goToURL (addressTextBox.getText()); };
  76. // create the actual browser component
  77. webView.reset (new DemoBrowserComponent (addressTextBox));
  78. addAndMakeVisible (webView.get());
  79. // add some buttons..
  80. addAndMakeVisible (goButton);
  81. goButton.onClick = [this] { webView->goToURL (addressTextBox.getText()); };
  82. addAndMakeVisible (backButton);
  83. backButton.onClick = [this] { webView->goBack(); };
  84. addAndMakeVisible (forwardButton);
  85. forwardButton.onClick = [this] { webView->goForward(); };
  86. // send the browser to a start page..
  87. webView->goToURL ("https://www.juce.com");
  88. setSize (1000, 1000);
  89. }
  90. void paint (Graphics& g) override
  91. {
  92. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  93. Colours::grey));
  94. }
  95. void resized() override
  96. {
  97. webView->setBounds (10, 45, getWidth() - 20, getHeight() - 55);
  98. goButton .setBounds (getWidth() - 45, 10, 35, 25);
  99. addressTextBox.setBounds (100, 10, getWidth() - 155, 25);
  100. backButton .setBounds (10, 10, 35, 25);
  101. forwardButton .setBounds (55, 10, 35, 25);
  102. }
  103. private:
  104. std::unique_ptr<DemoBrowserComponent> webView;
  105. TextEditor addressTextBox;
  106. TextButton goButton { "Go", "Go to URL" },
  107. backButton { "<<", "Back" },
  108. forwardButton { ">>", "Forward" };
  109. void lookAndFeelChanged() override
  110. {
  111. addressTextBox.applyFontToAllText (addressTextBox.getFont());
  112. }
  113. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WebBrowserDemo)
  114. };