UIWebView.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __COCOS2D_UI_WEBVIEW_H
  22. #define __COCOS2D_UI_WEBVIEW_H
  23. #include "platform/CCPlatformConfig.h"
  24. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) && !defined(CC_PLATFORM_OS_TVOS)
  25. #include "ui/UIWidget.h"
  26. #include "ui/GUIExport.h"
  27. #include "base/CCData.h"
  28. /**
  29. * @addtogroup ui
  30. * @{
  31. */
  32. NS_CC_BEGIN
  33. namespace experimental{
  34. namespace ui{
  35. class WebViewImpl;
  36. /**
  37. * @brief A View that displays web pages.
  38. *
  39. * @note WebView displays web pages base on system widget.
  40. * It's mean WebView displays web pages above all graphical elements of cocos2d-x.
  41. * @js NA
  42. */
  43. class CC_GUI_DLL WebView : public cocos2d::ui::Widget {
  44. public:
  45. /**
  46. * Allocates and initializes a WebView.
  47. */
  48. static WebView *create();
  49. /**
  50. * Set javascript interface scheme.
  51. *
  52. * @see WebView::setOnJSCallback()
  53. */
  54. void setJavascriptInterfaceScheme(const std::string &scheme);
  55. /**
  56. * Sets the main page contents, MIME type, content encoding, and base URL.
  57. *
  58. * @param data The content for the main page.
  59. * @param MIMEType The MIME type of the data.
  60. * @param encoding The encoding of the data.
  61. * @param baseURL The base URL for the content.
  62. */
  63. void loadData(const cocos2d::Data &data,
  64. const std::string &MIMEType,
  65. const std::string &encoding,
  66. const std::string &baseURL);
  67. /**
  68. * Sets the main page content and base URL.
  69. *
  70. * @param string The content for the main page.
  71. * @param baseURL The base URL for the content.
  72. */
  73. void loadHTMLString(const std::string &string, const std::string &baseURL = "");
  74. /**
  75. * Loads the given URL. It doesn't clean cached data.
  76. *
  77. * @param url Content URL.
  78. */
  79. void loadURL(const std::string &url);
  80. /**
  81. * Loads the given URL with cleaning cached data or not.
  82. * @param url Content URL.
  83. * @cleanCachedData Whether to clean cached data.
  84. */
  85. void loadURL(const std::string &url, bool cleanCachedData);
  86. /**
  87. * Loads the given fileName.
  88. *
  89. * @param fileName Content fileName.
  90. */
  91. void loadFile(const std::string &fileName);
  92. void PostURL(const std::string &url,const std::string& data);
  93. /**
  94. * Stops the current load.
  95. */
  96. void stopLoading();
  97. /**
  98. * Reloads the current URL.
  99. */
  100. void reload();
  101. /**
  102. * Gets whether this WebView has a back history item.
  103. *
  104. * @return WebView has a back history item.
  105. */
  106. bool canGoBack();
  107. /**
  108. * Gets whether this WebView has a forward history item.
  109. *
  110. * @return WebView has a forward history item.
  111. */
  112. bool canGoForward();
  113. /**
  114. * Goes back in the history.
  115. */
  116. void goBack();
  117. /**
  118. * Goes forward in the history.
  119. */
  120. void goForward();
  121. /**
  122. * Evaluates JavaScript in the context of the currently displayed page.
  123. */
  124. void evaluateJS(const std::string &js);
  125. /**
  126. * Set WebView should support zooming. The default value is false.
  127. */
  128. void setScalesPageToFit(const bool scalesPageToFit);
  129. /**
  130. * Call before a web view begins loading.
  131. *
  132. * @param callback The web view that is about to load new content.
  133. * @return YES if the web view should begin loading content; otherwise, NO.
  134. */
  135. void setOnShouldStartLoading(const std::function<bool(WebView *sender, const std::string &url)>& callback);
  136. /**
  137. * A callback which will be called when a WebView event happens.
  138. */
  139. typedef std::function<void(WebView *sender, const std::string &url)> ccWebViewCallback;
  140. /**
  141. * Call after a web view finishes loading.
  142. *
  143. * @param callback The web view that has finished loading.
  144. */
  145. void setOnDidFinishLoading(const ccWebViewCallback& callback);
  146. /**
  147. * Call if a web view failed to load content.
  148. *
  149. * @param callback The web view that has failed loading.
  150. */
  151. void setOnDidFailLoading(const ccWebViewCallback& callback);
  152. /**
  153. * This callback called when load URL that start with javascript interface scheme.
  154. */
  155. void setOnJSCallback(const ccWebViewCallback& callback);
  156. /**
  157. * Get the callback when WebView is about to start.
  158. */
  159. std::function<bool(WebView *sender, const std::string &url)> getOnShouldStartLoading()const;
  160. /**
  161. * Get the callback when WebView has finished loading.
  162. */
  163. ccWebViewCallback getOnDidFinishLoading()const;
  164. /**
  165. * Get the callback when WebView has failed loading.
  166. */
  167. ccWebViewCallback getOnDidFailLoading()const;
  168. /**
  169. *Get the Javascript callback.
  170. */
  171. ccWebViewCallback getOnJSCallback()const;
  172. /**
  173. * Set whether the webview bounces at end of scroll of WebView.
  174. */
  175. void setBounces(bool bounce);
  176. virtual void draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) override;
  177. /**
  178. * Toggle visibility of WebView.
  179. */
  180. virtual void setVisible(bool visible) override;
  181. /**
  182. * SetOpacity of webview.
  183. */
  184. virtual void setOpacityWebView(float opacity);
  185. /**
  186. * getOpacity of webview.
  187. */
  188. virtual float getOpacityWebView() const;
  189. /**
  190. * set the background transparent
  191. */
  192. virtual void setBackgroundTransparent();
  193. virtual void onEnter() override;
  194. virtual void onExit() override;
  195. protected:
  196. virtual cocos2d::ui::Widget* createCloneInstance() override;
  197. virtual void copySpecialProperties(Widget* model) override;
  198. std::function<bool(WebView *sender, const std::string &url)> _onShouldStartLoading;
  199. ccWebViewCallback _onDidFinishLoading;
  200. ccWebViewCallback _onDidFailLoading;
  201. ccWebViewCallback _onJSCallback;
  202. CC_CONSTRUCTOR_ACCESS:
  203. /**
  204. * Default constructor.
  205. */
  206. WebView();
  207. /**
  208. * Default destructor.
  209. */
  210. virtual ~WebView();
  211. private:
  212. WebViewImpl *_impl;
  213. friend class WebViewImpl;
  214. };
  215. } // namespace ui
  216. } // namespace experimental
  217. }//namespace cocos2d
  218. #endif
  219. // end group
  220. /// @}
  221. #endif //__COCOS2D_UI_WEBVIEW_H