示例#1
1
HRESULT WinCE_DoDragDrop(LPDATAOBJECT pDataObj, LPDROPSOURCE pDropSource, DWORD dwOKEffects, LPDWORD pdwEffect)
{
    MSG msg;
    POINT dest = {0,0};
    POINT lastDraw = {-1,-1};
    DWORD effect = DROPEFFECT_NONE;
    DWORD lastEffect = -1;
    IDropTarget * currentTarget = NULL;
    IWebViewPrivate * webView = NULL;
    IDataObject * dataObject = pDataObj;
    IDropSource * dropSource = pDropSource;
    HRESULT feedbackHR = DRAGDROP_S_USEDEFAULTCURSORS;
    HCURSOR startCursor = ::GetCursor();
    HCURSOR noCursor = LoadCursor(NULL, IDC_NO);

    // If there is a change in the keyboard or mouse button state, DoDragDrop calls
    // IDropSource::QueryContinueDrag and determines whether to continue the drag, to
    // drop the data, or to cancel the operation based on the return value.
    BOOL escapePressed = FALSE;
    DWORD keyState = MK_LBUTTON;
    if (GetKeyState(VK_CONTROL) & HIGH_BIT_MASK_SHORT)
        keyState |= MK_CONTROL;
    if (GetKeyState(VK_SHIFT) & HIGH_BIT_MASK_SHORT)
        keyState |= MK_SHIFT;

    HRESULT result = dropSource->QueryContinueDrag(escapePressed, keyState);
    bool dragActive = (result == S_OK);
    POINTL screenPoint;

    while (dragActive && GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);

        // Filter all mouse and key events during the drag and drop:
        if (msg.message == WM_LBUTTONUP ||
            msg.message == WM_LBUTTONDOWN ||
            msg.message == WM_RBUTTONUP ||
            msg.message == WM_RBUTTONDOWN ||
            msg.message == WM_MOUSEMOVE ||
            msg.message == WM_KEYDOWN ||
            msg.message == WM_KEYUP ||
            msg.message == WM_CHAR ||
            msg.message == WM_SYSKEYDOWN ||
            msg.message == WM_SYSKEYUP ||
            msg.message == WM_SYSCHAR)
        {
            if (msg.message == WM_MOUSEMOVE) {
                PERFORMANCE_START(WTF::PerformanceTrace::InputEvent,"DragDropManager: Mouse Move");
            }
            else if (msg.message == WM_LBUTTONUP) {
                PERFORMANCE_START(WTF::PerformanceTrace::InputEvent,"DragDropManager: Mouse Up");
            }
            else {
                PERFORMANCE_START(WTF::PerformanceTrace::InputEvent,"DragDropManager: Other Input");
            }
            bool stateChanged = false;
            if (msg.message == WM_MOUSEMOVE ||
                msg.message == WM_LBUTTONUP) {

                POINT pt = {GET_X_LPARAM(msg.lParam), GET_Y_LPARAM(msg.lParam)};
                ::ClientToScreen(msg.hwnd, &pt);
                screenPoint.x = pt.x;
                screenPoint.y = pt.y;

                dest.x = pt.x - currentDragImage.ptOffset.x;
                dest.y = pt.y - currentDragImage.ptOffset.y;

                if (msg.message == WM_MOUSEMOVE) {

                    // Which window is my mouse over?
                    HWND mouseWnd = WindowFromPoint(dest);

                    IDropTarget * newTarget = NULL;
                    if (mouseWnd != NULL && dragMap) {
                        DragWindowMap::iterator it = dragMap->find(mouseWnd);
                        if (it != dragMap->end())
                            newTarget = it->second;
                    }
                    if (currentTarget && currentTarget != newTarget) {
                        currentTarget->DragLeave();
                        feedbackHR = dropSource->GiveFeedback(DROPEFFECT_NONE);
                    }
                    if (newTarget) {
                        effect = DROPEFFECT_NONE; //is this the correct way to set effect?
                        if (pdwEffect)
                            effect = *pdwEffect;
                        if (currentTarget != newTarget) {
                            newTarget->DragEnter(dataObject, keyState, screenPoint, &effect);
                            feedbackHR = dropSource->GiveFeedback(effect);
                            // Find out if the target is a webview now.
                            if (webView)
                                webView->Release();
                            newTarget->QueryInterface(IID_IWebViewPrivate, (void**) &webView);
                        }
                        else if (currentTarget) {
                            currentTarget->DragOver(keyState, screenPoint, &effect);
                            feedbackHR = dropSource->GiveFeedback(effect);
                        }
                    }
                    if (webView) {
                        dragImageVisible = true;
                        dragAreaInvalid = true;
                    }
                    if (feedbackHR == DRAGDROP_S_USEDEFAULTCURSORS && effect != lastEffect) {
                        switch (effect) {
                            case DROPEFFECT_NONE:
                                ::SetCursor(noCursor);
                                break;
                            /* MS doesn't give us these cursors!!!
                            case DROPEFFECT_COPY:
                                break;
                            case DROPEFFECT_MOVE:
                                break;
                            case DROPEFFECT_LINK:
                                break;
                                */
                            default:
                                ::SetCursor(startCursor);
                                break;
                        }
                        lastEffect = effect;
                    }
                    currentTarget = newTarget;
                }
                else if (msg.message == WM_LBUTTONUP) {
                    keyState = 0;
                    stateChanged = true;
                }
            }
            else if (msg.message == WM_KEYDOWN) {
                if (msg.wParam == VK_ESCAPE) {
                    escapePressed = TRUE;
                    stateChanged = true;
                }
            }
            else if (msg.message == WM_KEYUP) {
                if (msg.wParam == VK_ESCAPE) {
                    escapePressed = FALSE;
                    stateChanged = true;
                }
            }

            if (stateChanged) {
                result = dropSource->QueryContinueDrag(escapePressed, keyState);
                if (result != S_OK) {
                    dragImageVisible = false;
                    dragAreaInvalid = true;
                    if (currentTarget && effect && result == DRAGDROP_S_DROP) {
                        effect = DROPEFFECT_NONE; //FIXME: should we reset effect or not?
                        result = currentTarget->Drop(dataObject, keyState, screenPoint, &effect);
                        if (SUCCEEDED(result)) {
                            result = DRAGDROP_S_DROP;
                            if (pdwEffect)
                                *pdwEffect = effect;
                        }
                    }
                    else {
                        result = DRAGDROP_S_CANCEL;
                    }
                    dragActive = false;
                }
            }
            PERFORMANCE_END(WTF::PerformanceTrace::InputEvent);
        }
        else {
            DispatchMessage(&msg);
        }
        if (dragAreaInvalid && webView && dragImageCairo) {
            PERFORMANCE_START(WTF::PerformanceTrace::Paint, "DragDropManager::Paint");
            HDC screenDC = ::GetDC(NULL);
            cairo_surface_t * screenSurface = cairo_win32_surface_create(screenDC);
            cairo_t *crScreen = cairo_create(screenSurface);
            HWND wnd;
            webView->viewWindow((OLE_HANDLE*) &wnd);
            RECT dirty;
            if (invalidDragRect.left != -1) {
                dirty = invalidDragRect;
            }
            else {
                POINT p = {0, 0};
                ::ClientToScreen(wnd, &p);
                ::GetClientRect(wnd, &dirty);
                dirty.left += p.x;
                dirty.top += p.y;
                dirty.right += p.x;
                dirty.bottom += p.y;
            }
            if (dragImageVisible) {
                POINT * prevDraw = NULL;
                if (lastDraw.x != -1 && (lastDraw.x != dest.x || lastDraw.y != dest.y))
                    prevDraw = &lastDraw;
                drawDragImage(crScreen, dest, prevDraw, webView, wnd, true, &dirty);
                lastDraw = dest;
                invalidDragRect.left = -1;
            }
            else {
                // erase the drag image wherever we last put it:
                drawDragImage(crScreen, lastDraw, NULL, webView, wnd, false, &dirty);
            }
            cairo_destroy(crScreen);
            cairo_surface_destroy(screenSurface);
            ::ReleaseDC(NULL, screenDC);
            dragAreaInvalid = false;
            PERFORMANCE_END(WTF::PerformanceTrace::Paint);
        }
    }

    if (currentTarget && result != DRAGDROP_S_DROP) {
        currentTarget->DragLeave();
    }
    ::SetCursor(startCursor);

    if (webView)
        webView->Release();
    if (dragImageCairo) {
        cairo_surface_destroy(dragImageCairo);
        dragImageCairo = NULL;
    }
    return result;
}
示例#2
0
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
#ifdef _CRTDBG_MAP_ALLOC
    _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
    _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
#endif

    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

     // TODO: Place code here.
    MSG msg;
    HACCEL hAccelTable;

    INITCOMMONCONTROLSEX InitCtrlEx;

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

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_WINLAUNCHER, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
        return FALSE;

    // Init COM
    OleInitialize(NULL);

    hURLBarWnd = CreateWindow(L"EDIT", 0,
                        WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOVSCROLL, 
                        0, 0, 0, 0,
                        hMainWnd,
                        0,
                        hInstance, 0);

    DefEditProc = GetWindowLong(hURLBarWnd, GWL_WNDPROC);
    SetWindowLong(hURLBarWnd, GWL_WNDPROC,(long)MyEditProc);
    SetFocus(hURLBarWnd);

    HRESULT hr = CoCreateInstance(CLSID_WebView, 0, CLSCTX_ALL, IID_IWebView, (void**)&gWebView);
    if (FAILED(hr))
        goto exit;

    gWebHost = new WinLauncherWebHost();
    gWebHost->AddRef();
    hr = gWebView->setFrameLoadDelegate(gWebHost);
    if (FAILED(hr))
        goto exit;

    hr = gWebView->setHostWindow((OLE_HANDLE) hMainWnd);
    if (FAILED(hr))
        goto exit;

    RECT clientRect;
    GetClientRect(hMainWnd, &clientRect);
    hr = gWebView->initWithFrame(clientRect, 0, 0);
    if (FAILED(hr))
        goto exit;

    IWebFrame* frame;
    hr = gWebView->mainFrame(&frame);
    if (FAILED(hr))
        goto exit;
    static BSTR defaultHTML = 0;
    if (!defaultHTML)
        defaultHTML = SysAllocString(TEXT("<p style=\"background-color: #00FF00\">Testing</p><img src=\"http://webkit.org/images/icon-gold.png\" alt=\"Face\"><div style=\"border: solid blue\" contenteditable=\"true\">div with blue border</div><ul><li>foo<li>bar<li>baz</ul>"));
    frame->loadHTMLString(defaultHTML, 0);
    frame->Release();

    IWebViewPrivate* viewExt;
    hr = gWebView->QueryInterface(IID_IWebViewPrivate, (void**)&viewExt);
    if (FAILED(hr))
        goto exit;
    hr = viewExt->viewWindow((OLE_HANDLE*) &gViewWindow);
    viewExt->Release();
    if (FAILED(hr) || !gViewWindow)
        goto exit;

    resizeSubViews();

    ShowWindow(gViewWindow, nCmdShow);
    UpdateWindow(gViewWindow);

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINLAUNCHER));

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0)) {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

exit:
    delete gWebView;
#ifdef _CRTDBG_MAP_ALLOC
    _CrtDumpMemoryLeaks();
#endif

    // Shut down COM.
    OleUninitialize();
    
    return (int) msg.wParam;
}
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);
}