UIWebViewImpl-tizen.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #if (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN)
  22. #include "ui/UIWebViewImpl-tizen.h"
  23. #include <unordered_map>
  24. #include <stdlib.h>
  25. #include <string>
  26. #include "platform/tizen/CCApplication-tizen.h"
  27. #include "ui/UIWebView.h"
  28. #include "platform/CCGLView.h"
  29. #include "base/CCDirector.h"
  30. #include "platform/CCFileUtils.h"
  31. #include "EWebKit.h"
  32. void JScallback(Evas_Object* o, const char* result_value, void* user_data)
  33. {
  34. }
  35. namespace cocos2d {
  36. namespace experimental {
  37. namespace ui{
  38. WebViewImpl::WebViewImpl(WebView *webView) : _webView(webView) {
  39. Application* app = Application::getInstance();
  40. Evas *evas = evas_object_evas_get(app->_win);
  41. _ewkView = ewk_view_add(evas);
  42. }
  43. WebViewImpl::~WebViewImpl() {
  44. evas_object_del(_ewkView);
  45. }
  46. void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) {
  47. const char* contents = nullptr;
  48. size_t contents_size = 0;
  49. char* mime_type = (char*)MIMEType.c_str();
  50. char* encode = (char*)encoding.c_str();
  51. char* base_uri = (char*)baseURL.c_str();
  52. ewk_view_contents_set(_ewkView, contents, contents_size, mime_type, encode, base_uri);
  53. }
  54. void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL) {
  55. const char* contents = string.c_str();
  56. size_t contents_size = string.length();
  57. char* mime_type = (char*)baseURL.c_str();
  58. char* encoding = nullptr;
  59. char* base_uri = nullptr;//(char*)baseURL.c_str();
  60. ewk_view_contents_set(_ewkView, contents, contents_size, mime_type, encoding, base_uri);
  61. }
  62. void WebViewImpl::loadURL(const std::string &url) {
  63. const char* urlchar = url.c_str();
  64. ewk_view_url_set(_ewkView, urlchar);
  65. }
  66. void WebViewImpl::loadURL(const std::string &url, bool cleanCachedData) {
  67. this->loadURL(url);
  68. }
  69. void WebViewImpl::loadFile(const std::string &fileName) {
  70. auto fileUtiles = FileUtils::getInstance();
  71. auto fileFullPath = fileUtiles->fullPathForFilename(fileName);
  72. auto contentsString = fileUtiles->getStringFromFile(fileFullPath);
  73. loadHTMLString(contentsString, "text/html");
  74. }
  75. void WebViewImpl::stopLoading() {
  76. ewk_view_stop(_ewkView);
  77. }
  78. void WebViewImpl::reload() {
  79. ewk_view_reload(_ewkView);
  80. }
  81. bool WebViewImpl::canGoBack() {
  82. return ewk_view_back_possible(_ewkView);
  83. }
  84. bool WebViewImpl::canGoForward() {
  85. return ewk_view_forward_possible(_ewkView);
  86. }
  87. void WebViewImpl::goBack() {
  88. ewk_view_back(_ewkView);
  89. }
  90. void WebViewImpl::goForward() {
  91. ewk_view_forward(_ewkView);
  92. }
  93. void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme) {
  94. }
  95. void WebViewImpl::evaluateJS(const std::string &js) {
  96. const char* script = js.c_str();
  97. Eina_Bool ret = ewk_view_script_execute(_ewkView, script, JScallback, this);
  98. }
  99. void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit) {
  100. }
  101. void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, int flags) {
  102. if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) {
  103. auto directorInstance = cocos2d::Director::getInstance();
  104. auto glView = directorInstance->getOpenGLView();
  105. auto frameSize = glView->getFrameSize();
  106. auto winSize = directorInstance->getWinSize();
  107. auto leftBottom = this->_webView->convertToWorldSpace(cocos2d::Point::ZERO);
  108. auto rightTop = this->_webView->convertToWorldSpace(cocos2d::Point(this->_webView->getContentSize().width,this->_webView->getContentSize().height));
  109. auto uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX();
  110. auto uiTop = frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY();
  111. Evas_Coord width = (rightTop.x - leftBottom.x) * glView->getScaleX();
  112. Evas_Coord height = (rightTop.y - leftBottom.y) * glView->getScaleY();
  113. evas_object_resize(_ewkView, width, height);
  114. evas_object_move(_ewkView, uiLeft, uiTop);
  115. evas_object_show(_ewkView);
  116. }
  117. }
  118. void WebViewImpl::setVisible(bool visible) {
  119. if (visible)
  120. {
  121. evas_object_show(_ewkView);
  122. }
  123. else
  124. {
  125. evas_object_hide(_ewkView);
  126. }
  127. }
  128. void WebViewImpl::setBounces(bool bounces) {
  129. // empty function as this was mainly a fix for iOS
  130. }
  131. void WebViewImpl::setOpacityWebView(float opacity){
  132. //TODO
  133. }
  134. float WebViewImpl::getOpacityWebView() const{
  135. //TODO
  136. }
  137. void WebViewImpl::setBackgroundTransparent(){
  138. //TODO
  139. }
  140. } // namespace ui
  141. } // namespace experimental
  142. } //namespace cocos2d
  143. #endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN)