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.

147 lines
5.1KB

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