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.

142 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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* bp) : owner (bp)
  71. {
  72. // Add a couple of methods to our object..
  73. setMethod ("printText", printText);
  74. setMethod ("popUpMessageBox", popUpMessageBox);
  75. setMethod ("registerCallbackObject", registerCallbackObject);
  76. // Add some value properties that the webpage can access
  77. setProperty ("property1", "testing testing...");
  78. setProperty ("property2", 12345678.0);
  79. }
  80. //==============================================================================
  81. // These methods are called by javascript in the webpage...
  82. static var printText (const var::NativeFunctionArgs& args)
  83. {
  84. if (DemoBrowserObject* b = dynamic_cast<DemoBrowserObject*> (args.thisObject.getObject()))
  85. if (args.numArguments > 0)
  86. b->owner->textBox.setText (b->owner->textBox.getText() + "\n" + args.arguments[0].toString());
  87. return "text was printed ok!";
  88. }
  89. static var popUpMessageBox (const var::NativeFunctionArgs& args)
  90. {
  91. if (DemoBrowserObject* b = dynamic_cast<DemoBrowserObject*> (args.thisObject.getObject()))
  92. if (args.numArguments > 0)
  93. AlertWindow::showMessageBox (AlertWindow::InfoIcon,
  94. "A message from the webpage",
  95. args.arguments[0].toString(),
  96. String::empty, b->owner);
  97. return var();
  98. }
  99. static var registerCallbackObject (const var::NativeFunctionArgs& args)
  100. {
  101. if (DemoBrowserObject* b = dynamic_cast<DemoBrowserObject*> (args.thisObject.getObject()))
  102. if (args.numArguments > 0)
  103. b->owner->setJavascriptObjectFromBrowser (args.arguments[0]);
  104. return var();
  105. }
  106. //==============================================================================
  107. JuceDemoBrowserPlugin* owner;
  108. };
  109. };
  110. BrowserPluginComponent* JUCE_CALLTYPE createBrowserPlugin()
  111. {
  112. return new JuceDemoBrowserPlugin();
  113. }