コード例 #1
0
ファイル: win32_user_window.cpp プロジェクト: rlwesq/titanium
Win32UserWindow::Win32UserWindow(SharedUIBinding binding, WindowConfig* config, SharedUserWindow& parent) :
	UserWindow(binding, config, parent),
	script_evaluator(binding->GetHost()),
	menuBarHandle(NULL),
	menuInUse(NULL),
	menu(NULL),
	contextMenuHandle(NULL),
	initial_icon(NULL),
	web_inspector(NULL)
{
	static bool initialized = false;
	win32_host = static_cast<kroll::Win32Host*>(binding->GetHost());
	if (!initialized) {
		INITCOMMONCONTROLSEX InitCtrlEx;

		InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
		InitCtrlEx.dwICC = 0x00004000; //ICC_STANDARD_CLASSES;
		InitCommonControlsEx(&InitCtrlEx);

		curl_register_local_handler(&Titanium_app_url_handler);
		curl_register_local_handler(&Titanium_ti_url_handler);
		addScriptEvaluator(&script_evaluator);
	}

	Win32UserWindow::RegisterWindowClass(win32_host->GetInstanceHandle());
	window_handle
			= CreateWindowEx(WS_EX_LAYERED, windowClassName,
					config->GetTitle().c_str(), WS_CLIPCHILDREN, CW_USEDEFAULT,
					0, CW_USEDEFAULT, 0, NULL, NULL,
					win32_host->GetInstanceHandle(), NULL);

	if (window_handle == NULL) {
		std::cout << "Error Creating Window: " << GetLastError() << std::endl;
	}
	std::cout << "window_handle = " << (int) window_handle << std::endl;

	// make our HWND available to 3rd party devs without needing our headers
	SharedValue windowHandle = Value::NewVoidPtr((void*) window_handle);
	this->Set("windowHandle", windowHandle);
	this->SetMethod("addMessageHandler", &Win32UserWindow::AddMessageHandler);

	SetWindowUserData(window_handle, this);

	this->ReloadTiWindowConfig();
	this->SetupDecorations(false);

	Bounds b;
	b.x = config->GetX();
	b.y = config->GetY();
	b.width = config->GetWidth();
	b.height = config->GetHeight();
	SetBounds(b);

	//web_view = WebView::createInstance();
	HRESULT hr = CoCreateInstance(CLSID_WebView, 0, CLSCTX_ALL, IID_IWebView,
			(void**) &web_view);
	if (FAILED(hr)) {
		std::cerr << "Error Creating WebView: ";
		if (hr == REGDB_E_CLASSNOTREG)
			std::cerr << "REGDB_E_CLASSNOTREG" << std::endl;
		else if (hr == CLASS_E_NOAGGREGATION)
			std::cerr << "CLASS_E_NOAGGREGATION" << std::endl;
		else if (hr == E_NOINTERFACE)
			std::cerr << "E_NOINTERFACE" << std::endl;
		else if (hr == E_UNEXPECTED)
			std::cerr << "E_UNEXPECTED" << std::endl;
		else if (hr == E_OUTOFMEMORY)
			std::cerr << "E_OUTOFMEMORY" << std::endl;
		else if (hr == E_INVALIDARG)
			std::cerr << "E_INVALIDARG" << std::endl;
		else
			fprintf(stderr, "Unknown Error? %x\n", hr);
	}

	// set the custom user agent for Titanium
	double version = host->GetGlobalObject()->Get("version")->ToDouble();
	char userAgent[128];
	sprintf(userAgent, "%s/%0.2f", PRODUCT_NAME, version);
	_bstr_t ua(userAgent);
	web_view->setApplicationNameForUserAgent(ua.copy());

	// place our user agent string in the global so we can later use it
	SharedBoundObject global = host->GetGlobalObject();
	_bstr_t uaurl("http://titaniumapp.com");
	BSTR uaresp;
	web_view->userAgentForURL(uaurl.copy(), &uaresp);
	std::string ua_str = _bstr_t(uaresp);
	global->Set("userAgent", Value::NewString(ua_str.c_str()));

	std::cout << "create frame load delegate " << std::endl;
	frameLoadDelegate = new Win32WebKitFrameLoadDelegate(this);
	uiDelegate = new Win32WebKitUIDelegate(this);
	policyDelegate = new Win32WebKitPolicyDelegate(this);

	std::cout << "set delegates, set host window, webview=" << (int) web_view
			<< std::endl;
	hr = web_view->setFrameLoadDelegate(frameLoadDelegate);
	hr = web_view->setUIDelegate(uiDelegate);
	hr = web_view->setPolicyDelegate(policyDelegate);
	hr = web_view->setHostWindow((OLE_HANDLE) window_handle);

	std::cout << "init with frame" << std::endl;
	RECT client_rect;
	GetClientRect(window_handle, &client_rect);
	hr = web_view->initWithFrame(client_rect, 0, 0);

	AppConfig *appConfig = AppConfig::Instance();
	std::string appid = appConfig->GetAppID();

	IWebPreferences *prefs = NULL;
	hr = CoCreateInstance(CLSID_WebPreferences, 0, CLSCTX_ALL,
			IID_IWebPreferences, (void**) &prefs);
	if (FAILED(hr) || prefs == NULL) {
		std::cerr << "Couldn't create the preferences object" << std::endl;
	} else {
		_bstr_t pi(appid.c_str());
		prefs->initWithIdentifier(pi.copy(), &prefs);

		prefs->setCacheModel(WebCacheModelDocumentBrowser);
		prefs->setPlugInsEnabled(true);
		prefs->setJavaEnabled(true);
		prefs->setJavaScriptEnabled(true);
		prefs->setDOMPasteAllowed(true);

		IWebPreferencesPrivate* privatePrefs = NULL;
		hr = prefs->QueryInterface(IID_IWebPreferencesPrivate,
				(void**) &privatePrefs);
		if (FAILED(hr)) {
			std::cerr << "Failed to get private preferences" << std::endl;
		} else {
			privatePrefs->setDeveloperExtrasEnabled(host->IsDebugMode());
			//privatePrefs->setDeveloperExtrasEnabled(host->IsDebugMode());
			privatePrefs->setDatabasesEnabled(true);
			privatePrefs->setLocalStorageEnabled(true);
			privatePrefs->setOfflineWebApplicationCacheEnabled(true);

			_bstr_t db_path(
					FileUtils::GetApplicationDataDirectory(appid).c_str());
			privatePrefs->setLocalStorageDatabasePath(db_path.copy());
			privatePrefs->Release();
		}

		web_view->setPreferences(prefs);
		prefs->Release();
	}

	// allow app:// and ti:// to run with local permissions (cross-domain ajax,etc)
	_bstr_t app_proto("app");
	web_view->registerURLSchemeAsLocal(app_proto.copy());

	_bstr_t ti_proto("ti");
	web_view->registerURLSchemeAsLocal(ti_proto.copy());

	IWebViewPrivate *web_view_private;
	hr = web_view->QueryInterface(IID_IWebViewPrivate,
			(void**) &web_view_private);
	hr = web_view_private->viewWindow((OLE_HANDLE*) &view_window_handle);

	hr = web_view_private->inspector(&web_inspector);
	if (FAILED(hr) || web_inspector == NULL) {
		std::cerr << "Couldn't retrieve the web inspector object" << std::endl;
	}

	web_view_private->Release();

	_bstr_t inspector_url("ti://com.titaniumapp/runtime/inspector/inspector.html");
	_bstr_t localized_strings_url("ti://com.titaniumapp/runtime/inspector/localizedStrings.js");
	web_inspector->setInspectorURL(inspector_url.copy());
	web_inspector->setLocalizedStringsURL(localized_strings_url.copy());

	hr = web_view->mainFrame(&web_frame);
	//web_view->setShouldCloseWithWindow(TRUE);

	std::cout << "resize subviews" << std::endl;
	ResizeSubViews();

	// ensure we have valid restore values
	restore_bounds = GetBounds();
	restore_styles = GetWindowLong(window_handle, GWL_STYLE);

	if (this->config->IsFullScreen()) {
		this->SetFullScreen(true);
	}

	if (this->config->IsTopMost() && this->config->IsVisible()) {
		this->SetTopMost(true);
	}

	// set this flag to indicate that when the frame is loaded
	// we want to show the window - we do this to prevent white screen
	// while the URL is being fetched
	this->requires_display = true;

	// set initial window icon to icon associated with exe file
	char exePath[MAX_PATH];
	GetModuleFileNameA(GetModuleHandle(NULL), exePath, MAX_PATH);
	initial_icon = ExtractIcon(win32_host->GetInstanceHandle(), exePath, 0);
	if (initial_icon) {
		SendMessageA(window_handle, (UINT) WM_SETICON, ICON_BIG,
				(LPARAM) initial_icon);
	}
}
コード例 #2
0
void Win32UserWindow::InitWebKit()
{
	HRESULT hr = WebKitCreateInstance(CLSID_WebView, 0, IID_IWebView,
		(void**) &(this->webView));
	if (FAILED(hr))
		HandleHResultError("Error creating WebKitWebView", hr, true);

	// Set the custom user agent for Titanium
	const char *version = host->GetGlobalObject()->Get("version")->ToString();
	char userAgent[128];
	//TI-303 we need to add safari UA to our UA to resolve broken
	//sites that look at Safari and not WebKit for UA
	sprintf(userAgent, "Version/4.0 Safari/528.16 %s/%s", PRODUCT_NAME, version);
	_bstr_t ua(userAgent);
	webView->setApplicationNameForUserAgent(ua.copy());

	// place our user agent string in the global so we can later use it
	KObjectRef global = host->GetGlobalObject();
	if (global->Get("userAgent")->IsUndefined())
	{
		_bstr_t uaURL("http://titaniumapp.com");
		BSTR uaResp;
		webView->userAgentForURL(uaURL.copy(), &uaResp);
		std::string uaStr = _bstr_t(uaResp);
		global->Set("userAgent", Value::NewString(uaStr.c_str()));
	}

	frameLoadDelegate = new Win32WebKitFrameLoadDelegate(this);
	hr = webView->setFrameLoadDelegate(frameLoadDelegate);
	if (FAILED(hr))
		HandleHResultError("Error setting FrameLoadDelegate", hr, true);

	uiDelegate = new Win32WebKitUIDelegate(this);
	hr = webView->setUIDelegate(uiDelegate);
	if (FAILED(hr))
		HandleHResultError("Error setting UIDelegate", hr, true);

	policyDelegate = new Win32WebKitPolicyDelegate(this);
	hr = webView->setPolicyDelegate(policyDelegate);
	if (FAILED(hr))
		HandleHResultError("Error setting PolicyDelegate", hr, true);

	hr = webView->setHostWindow((OLE_HANDLE) windowHandle);
	if (FAILED(hr))
		HandleHResultError("Error setting host window", hr, true);

	RECT clientRect;
	GetClientRect(windowHandle, &clientRect);
	hr = webView->initWithFrame(clientRect, 0, 0);
	if (FAILED(hr))
		HandleHResultError("Could not intialize WebView with frame", hr, true);

	IWebPreferences *prefs = NULL;
	hr = WebKitCreateInstance(CLSID_WebPreferences, 0, IID_IWebPreferences,
		(void**) &prefs);
	if (FAILED(hr) || !prefs)
		HandleHResultError("Error getting IWebPreferences", hr, true);

	std::string appid(AppConfig::Instance()->GetAppID());
	_bstr_t pi(appid.c_str());
	prefs->initWithIdentifier(pi.copy(), &prefs);
	prefs->setCacheModel(WebCacheModelDocumentBrowser);
	prefs->setPlugInsEnabled(true);
	prefs->setJavaEnabled(true);
	prefs->setJavaScriptEnabled(true);
	prefs->setJavaScriptCanOpenWindowsAutomatically(true);
	prefs->setDOMPasteAllowed(true);

	IWebPreferencesPrivate* privatePrefs = NULL;
	hr = prefs->QueryInterface(IID_IWebPreferencesPrivate, (void**) &privatePrefs);
	if (FAILED(hr) || !privatePrefs)
		HandleHResultError("Error getting IWebPreferencesPrivate", hr, true);

	privatePrefs->setDeveloperExtrasEnabled(host->IsDebugMode());
	privatePrefs->setDatabasesEnabled(true);
	privatePrefs->setLocalStorageEnabled(true);
	privatePrefs->setOfflineWebApplicationCacheEnabled(true);
	privatePrefs->setAllowUniversalAccessFromFileURLs(true);
	_bstr_t dbPath(FileUtils::GetApplicationDataDirectory(appid).c_str());
	privatePrefs->setLocalStorageDatabasePath(dbPath.copy());
	privatePrefs->Release();

	webView->setPreferences(prefs);
	prefs->Release();

	// allow app:// and ti:// to run with local permissions (cross-domain ajax,etc)
	_bstr_t appProto("app");
	webView->registerURLSchemeAsLocal(appProto.copy());

	_bstr_t tiProto("ti");
	webView->registerURLSchemeAsLocal(tiProto.copy());

	IWebViewPrivate *webViewPrivate;
	hr = webView->QueryInterface(IID_IWebViewPrivate, (void**) &webViewPrivate);
	if (FAILED(hr))
		HandleHResultError("Error getting IWebViewPrivate", hr);

	// Get the WebView's HWND
	hr = webViewPrivate->viewWindow((OLE_HANDLE*) &viewWindowHandle);
	if (FAILED(hr))
		HandleHResultError("Error getting WebView HWND", hr);

	// Get the WebView's WebInspector
	hr = webViewPrivate->inspector(&webInspector);
	if (FAILED(hr) || !webInspector)
		HandleHResultError("Error getting WebInspector HWND", hr);

	webViewPrivate->Release();

	_bstr_t inspector_url("ti://runtime/WebKit.resources/inspector/inspector.html");
	webInspector->setInspectorURL(inspector_url.copy());

	hr = webView->mainFrame(&mainFrame);
	if (FAILED(hr) || !webInspector)
		HandleHResultError("Error getting WebView main frame", hr);
}