axmol/cocos/ui/UIWebView.h

204 lines
5.9 KiB
C
Raw Normal View History

2014-08-26 12:08:26 +08:00
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2014-08-25 13:44:38 +08:00
2014-09-15 16:56:51 +08:00
#ifndef __COCOS2D_UI_WEBVIEW_H
#define __COCOS2D_UI_WEBVIEW_H
2014-08-25 13:44:38 +08:00
#include "platform/CCPlatformConfig.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
2014-08-25 13:44:38 +08:00
#include "ui/UIWidget.h"
2014-09-18 16:33:46 +08:00
#include "ui/GUIExport.h"
2014-08-25 13:44:38 +08:00
#include "base/CCData.h"
NS_CC_BEGIN
namespace experimental{
namespace ui{
2014-08-25 13:44:38 +08:00
class WebViewImpl;
2014-09-18 16:33:46 +08:00
class CC_GUI_DLL WebView : public cocos2d::ui::Widget {
2014-08-25 13:44:38 +08:00
public:
/**
* Allocates and initializes a WebView.
*/
static WebView *create();
/**
* Set javascript interface scheme.
* @see #onJsCallback
*/
void setJavascriptInterfaceScheme(const std::string &scheme);
/**
* Sets the main page contents, MIME type, content encoding, and base URL.
* @param data The content for the main page.
* @param MIMEType The MIME type of the data.
* @param encoding the encoding of the data.
* @param baseURL The base URL for the content.
*/
2014-08-26 12:08:26 +08:00
void loadData(const cocos2d::Data &data,
const std::string &MIMEType,
const std::string &encoding,
const std::string &baseURL);
2014-08-25 13:44:38 +08:00
/**
* Sets the main page content and base URL.
* @param string The content for the main page.
* @param baseURL The base URL for the content.
*/
void loadHTMLString(const std::string &string, const std::string &baseURL = "");
2014-08-25 13:44:38 +08:00
/**
* Loads the given URL.
* @param url content URL
*/
2014-09-15 18:02:11 +08:00
void loadURL(const std::string &url);
2014-08-25 13:44:38 +08:00
/**
* Loads the given fileName.
* @param fileName content fileName
*/
void loadFile(const std::string &fileName);
/**
* Stops the current load.
*/
void stopLoading();
/**
* Reloads the current URL.
*/
void reload();
/**
* Gets whether this WebView has a back history item.
* @return web view has a back history item.
*/
bool canGoBack();
/**
* Gets whether this WebView has a forward history item.
* @return web view has a forward history item.
*/
bool canGoForward();
/**
* Goes back in the history.
*/
void goBack();
/**
* Goes forward in the history.
*/
void goForward();
/**
* evaluates JavaScript in the context of the currently displayed page
*/
void evaluateJS(const std::string &js);
/**
* Set WebView should support zooming. The default value is false.
*/
void setScalesPageToFit(const bool scalesPageToFit);
virtual void draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) override;
virtual void setVisible(bool visible) override;
2014-09-15 18:02:11 +08:00
typedef std::function<void(WebView *sender, const std::string &url)> ccWebViewCallback;
2014-09-15 18:02:11 +08:00
/**
* Call before a web view begins loading.
* @param sender The web view that is about to load new content.
* @param url content URL.
* @return YES if the web view should begin loading content; otherwise, NO .
*/
void setOnShouldStartLoading(const std::function<bool(WebView *sender, const std::string &url)>& callback);
2014-09-15 18:02:11 +08:00
/**
* Call after a web view finishes loading.
* @param sender The web view that has finished loading.
* @param url content URL.
*/
void setOnDidFinishLoading(const ccWebViewCallback& callback);
2014-09-15 18:02:11 +08:00
/**
* Call if a web view failed to load content.
* @param sender The web view that has failed loading.
* @param url content URL.
*/
void setOnDidFailLoading(const ccWebViewCallback& callback);
2014-09-15 18:02:11 +08:00
/**
* This callback called when load URL that start with javascript interface scheme.
*/
void setOnJSCallback(const ccWebViewCallback& callback);
2014-09-15 18:02:11 +08:00
std::function<bool(WebView *sender, const std::string &url)> getOnShouldStartLoading()const;
ccWebViewCallback getOnDidFinishLoading()const;
ccWebViewCallback getOnDidFailLoading()const;
ccWebViewCallback getOnJSCallback()const;
2014-09-15 18:02:11 +08:00
protected:
virtual cocos2d::ui::Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
std::function<bool(WebView *sender, const std::string &url)> _onShouldStartLoading;
2014-09-15 18:02:11 +08:00
ccWebViewCallback _onDidFinishLoading;
2014-09-15 18:02:11 +08:00
ccWebViewCallback _onDidFailLoading;
2014-09-15 18:02:11 +08:00
ccWebViewCallback _onJSCallback;
2014-09-15 18:02:11 +08:00
/**
* Default constructor
*/
WebView();
/**
* Default destructor
*/
virtual ~WebView();
2014-08-25 13:44:38 +08:00
private:
WebViewImpl *_impl;
2014-09-15 18:02:11 +08:00
friend class WebViewImpl;
2014-08-25 13:44:38 +08:00
};
2014-08-26 22:03:55 +08:00
} // namespace ui
2014-08-26 12:08:26 +08:00
} // namespace experimental
2014-08-26 22:03:55 +08:00
}//namespace cocos2d
#endif
2014-08-25 13:44:38 +08:00
2014-09-15 16:56:51 +08:00
#endif //__COCOS2D_UI_WEBVIEW_H