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.

155 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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, vs2022, linux_make, androidstudio, xcode_iphone
  27. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  28. type: Component
  29. mainClass: WebBrowserDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #if JUCE_WEB_BROWSER
  35. #include "../Assets/DemoUtilities.h"
  36. //==============================================================================
  37. /** We'll use a subclass of WebBrowserComponent to demonstrate how to get callbacks
  38. when the browser changes URL. You don't need to do this, you can just also
  39. just use the WebBrowserComponent class directly.
  40. */
  41. class DemoBrowserComponent final : public WebBrowserComponent
  42. {
  43. public:
  44. //==============================================================================
  45. DemoBrowserComponent (TextEditor& addressBox)
  46. : addressTextBox (addressBox)
  47. {}
  48. // This method gets called when the browser is about to go to a new URL..
  49. bool pageAboutToLoad (const String& newURL) override
  50. {
  51. // We'll just update our address box to reflect the new location..
  52. addressTextBox.setText (newURL, false);
  53. // we could return false here to tell the browser not to go ahead with
  54. // loading the page.
  55. return true;
  56. }
  57. // This method gets called when the browser is requested to launch a new window
  58. void newWindowAttemptingToLoad (const String& newURL) override
  59. {
  60. // We'll just load the URL into the main window
  61. goToURL (newURL);
  62. }
  63. private:
  64. TextEditor& addressTextBox;
  65. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DemoBrowserComponent)
  66. };
  67. //==============================================================================
  68. class WebBrowserDemo final : public Component
  69. {
  70. public:
  71. WebBrowserDemo()
  72. {
  73. setOpaque (true);
  74. // Create an address box..
  75. addAndMakeVisible (addressTextBox);
  76. addressTextBox.setTextToShowWhenEmpty ("Enter a web address, e.g. https://www.juce.com", Colours::grey);
  77. addressTextBox.onReturnKey = [this] { webView->goToURL (addressTextBox.getText()); };
  78. // create the actual browser component
  79. webView.reset (new DemoBrowserComponent (addressTextBox));
  80. addAndMakeVisible (webView.get());
  81. // add some buttons..
  82. addAndMakeVisible (goButton);
  83. goButton.onClick = [this] { webView->goToURL (addressTextBox.getText()); };
  84. addAndMakeVisible (backButton);
  85. backButton.onClick = [this] { webView->goBack(); };
  86. addAndMakeVisible (forwardButton);
  87. forwardButton.onClick = [this] { webView->goForward(); };
  88. // send the browser to a start page..
  89. webView->goToURL ("https://www.juce.com");
  90. setSize (1000, 1000);
  91. }
  92. void paint (Graphics& g) override
  93. {
  94. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  95. Colours::grey));
  96. }
  97. void resized() override
  98. {
  99. webView->setBounds (10, 45, getWidth() - 20, getHeight() - 55);
  100. goButton .setBounds (getWidth() - 45, 10, 35, 25);
  101. addressTextBox.setBounds (100, 10, getWidth() - 155, 25);
  102. backButton .setBounds (10, 10, 35, 25);
  103. forwardButton .setBounds (55, 10, 35, 25);
  104. }
  105. private:
  106. std::unique_ptr<DemoBrowserComponent> webView;
  107. TextEditor addressTextBox;
  108. TextButton goButton { "Go", "Go to URL" },
  109. backButton { "<<", "Back" },
  110. forwardButton { ">>", "Forward" };
  111. void lookAndFeelChanged() override
  112. {
  113. addressTextBox.applyFontToAllText (addressTextBox.getFont());
  114. }
  115. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WebBrowserDemo)
  116. };
  117. #endif