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.

140 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 "JuceHeader.h"
  18. //==============================================================================
  19. /**
  20. This is our top-level component for our plugin..
  21. */
  22. class JuceDemoBrowserPlugin : public BrowserPluginComponent,
  23. public Button::Listener
  24. {
  25. public:
  26. JuceDemoBrowserPlugin()
  27. : textBox (String::empty),
  28. button ("Send a message to the webpage")
  29. {
  30. addAndMakeVisible (&textBox);
  31. textBox.setMultiLine (true);
  32. textBox.setBounds (8, 8, 300, 300);
  33. addAndMakeVisible (&button);
  34. button.setBounds (320, 8, 180, 22);
  35. button.addListener (this);
  36. button.setEnabled (false);
  37. ourJavascriptObject = new DemoBrowserObject (this);
  38. textBox.setText (SystemStats::getJUCEVersion() + "\n\n"
  39. + "Browser: " + getBrowserVersion());
  40. }
  41. var getJavascriptObject()
  42. {
  43. // The browser calls this to get the javascript object that represents our plugin..
  44. return ourJavascriptObject;
  45. }
  46. void paint (Graphics& g)
  47. {
  48. g.fillAll (Colours::lightblue);
  49. }
  50. void setJavascriptObjectFromBrowser (var callbackObject)
  51. {
  52. javascriptObjectFromBrowser = callbackObject;
  53. button.setEnabled (javascriptObjectFromBrowser.isObject());
  54. }
  55. void buttonClicked (Button*)
  56. {
  57. javascriptObjectFromBrowser.call ("printmessage", "This is a message sent from the plugin...");
  58. }
  59. var ourJavascriptObject;
  60. var javascriptObjectFromBrowser;
  61. TextEditor textBox;
  62. TextButton button;
  63. //==============================================================================
  64. /** This is the javascript object that the browser uses when the webpage accesses
  65. methods or properties on our plugin object.
  66. */
  67. class DemoBrowserObject : public DynamicObject
  68. {
  69. public:
  70. DemoBrowserObject (JuceDemoBrowserPlugin* owner_)
  71. : owner (owner_)
  72. {
  73. // Add a couple of methods to our object..
  74. setMethod ("printText", (var::MethodFunction) &DemoBrowserObject::printText);
  75. setMethod ("popUpMessageBox", (var::MethodFunction) &DemoBrowserObject::popUpMessageBox);
  76. setMethod ("registerCallbackObject", (var::MethodFunction) &DemoBrowserObject::registerCallbackObject);
  77. // Add some value properties that the webpage can access
  78. setProperty ("property1", "testing testing...");
  79. setProperty ("property2", 12345678.0);
  80. }
  81. //==============================================================================
  82. // These methods are called by javascript in the webpage...
  83. const var printText (const var* params, int numParams)
  84. {
  85. if (numParams > 0)
  86. owner->textBox.setText (owner->textBox.getText() + "\n" + params[0].toString());
  87. return "text was printed ok!";
  88. }
  89. const var popUpMessageBox (const var* params, int numParams)
  90. {
  91. if (numParams > 0)
  92. AlertWindow::showMessageBox (AlertWindow::InfoIcon,
  93. "A message from the webpage",
  94. params[0].toString(),
  95. String::empty, owner);
  96. return var::null;
  97. }
  98. const var registerCallbackObject (const var* params, int numParams)
  99. {
  100. if (numParams > 0)
  101. owner->setJavascriptObjectFromBrowser (params[0]);
  102. return var::null;
  103. }
  104. //==============================================================================
  105. JuceDemoBrowserPlugin* owner;
  106. };
  107. };
  108. BrowserPluginComponent* JUCE_CALLTYPE createBrowserPlugin()
  109. {
  110. return new JuceDemoBrowserPlugin();
  111. }