initial import
[vuplus_webkit] / Tools / TestWebKitAPI / win / PlatformWebViewWin.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "PlatformWebView.h"
28
29 #include "WindowMessageObserver.h"
30
31 namespace TestWebKitAPI {
32
33 static const wchar_t* hostWindowClassName = L"org.WebKit.TestWebKitAPI.PlatformWebViewHostWindow";
34 static const wchar_t* webViewPointerProperty = L"org.WebKit.TestWebKitAPI.PlatformWebView.InstancePointer";
35
36 // These offsets come from rom <http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx>.
37 static const size_t repeatCountBitOffset = 0;
38 static const size_t scanCodeBitOffset = 16;
39 static const size_t contextCodeBitOffset = 29;
40 static const size_t previousStateBitOffset = 30;
41 static const size_t transitionStateBitOffset = 31;
42
43 void PlatformWebView::registerWindowClass()
44 {
45     static bool initialized;
46     if (initialized)
47         return;
48     initialized = true;
49
50     WNDCLASSEXW wndClass = {0};
51     wndClass.cbSize = sizeof(wndClass);
52     wndClass.style = CS_HREDRAW | CS_VREDRAW;
53     wndClass.lpfnWndProc = wndProc;
54     wndClass.hCursor = LoadCursor(0, IDC_ARROW);
55     wndClass.lpszClassName = hostWindowClassName;
56
57     ::RegisterClassExW(&wndClass);
58 }
59
60 PlatformWebView::PlatformWebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef)
61     : m_parentWindowMessageObserver(0)
62 {
63     registerWindowClass();
64
65     RECT viewRect = {0, 0, 800, 600};
66     m_window = CreateWindowExW(0, hostWindowClassName, L"TestWebKitAPI", WS_OVERLAPPEDWINDOW, viewRect.left, viewRect.top, viewRect.right, viewRect.bottom, 0, 0, 0, this);
67     m_view = WKViewCreate(viewRect, contextRef, pageGroupRef, m_window);
68 }
69
70 PlatformWebView::~PlatformWebView()
71 {
72     ::DestroyWindow(m_window);
73     WKRelease(m_view);
74 }
75
76 WKPageRef PlatformWebView::page() const
77 {
78     return WKViewGetPage(m_view);
79 }
80
81 void PlatformWebView::resizeTo(unsigned width, unsigned height)
82 {
83     ::SetWindowPos(WKViewGetWindow(m_view), 0, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOCOPYBITS);
84 }
85
86 void PlatformWebView::simulateSpacebarKeyPress()
87 {
88     HWND window = WKViewGetWindow(m_view);
89
90     // These values match what happens when you press the spacebar in Notepad, as observed by Spy++.
91     ::SendMessageW(window, WM_KEYDOWN, VK_SPACE, (1 << repeatCountBitOffset) | (39 << scanCodeBitOffset));
92     ::SendMessageW(window, WM_CHAR, ' ', (1 << repeatCountBitOffset) | (39 << scanCodeBitOffset));
93     ::SendMessageW(window, WM_KEYUP, VK_SPACE, (1 << repeatCountBitOffset) | (39 << scanCodeBitOffset) | (1 << previousStateBitOffset) | (1 << transitionStateBitOffset));
94 }
95
96 void PlatformWebView::simulateAKeyDown()
97 {
98     HWND window = WKViewGetWindow(m_view);
99     
100     // These values match what happens when you press the 'A' key in Notepad, as observed by Spy++.
101     ::SendMessageW(window, WM_KEYDOWN, 'A', (1 << repeatCountBitOffset) | (30 << scanCodeBitOffset));
102 }
103
104 void PlatformWebView::simulateAltKeyPress()
105 {
106     HWND window = WKViewGetWindow(m_view);
107
108     // These values match what happens when you press the Alt key in Notepad, as observed by Spy++.
109     ::SendMessageW(window, WM_SYSKEYDOWN, VK_MENU, (1 << repeatCountBitOffset) | (38 << scanCodeBitOffset) | (1 << contextCodeBitOffset));
110     ::SendMessageW(window, WM_SYSKEYUP, VK_MENU, (1 << repeatCountBitOffset) | (38 << scanCodeBitOffset) | (1 << previousStateBitOffset) | (1 << transitionStateBitOffset));
111 }
112
113 void PlatformWebView::simulateRightClick(unsigned x, unsigned y)
114 {
115     HWND window = WKViewGetWindow(m_view);
116
117     ::SendMessageW(window, WM_RBUTTONDOWN, 0, MAKELPARAM(x, y));
118     ::SendMessageW(window, WM_RBUTTONUP, 0, MAKELPARAM(x, y));
119 }
120
121 void PlatformWebView::simulateMouseMove(unsigned x, unsigned y)
122 {
123     HWND window = WKViewGetWindow(m_view);
124
125     ::SendMessageW(window, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
126 }
127
128 LRESULT PlatformWebView::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
129 {
130     PlatformWebView* webView;
131     if (message == WM_CREATE) {
132         CREATESTRUCT* createStruct = reinterpret_cast<CREATESTRUCT*>(lParam);
133         webView = static_cast<PlatformWebView*>(createStruct->lpCreateParams);
134         ::SetPropW(hWnd, webViewPointerProperty, webView);
135     } else
136         webView = reinterpret_cast<PlatformWebView*>(::GetPropW(hWnd, webViewPointerProperty));
137
138     if (webView && webView->m_parentWindowMessageObserver)
139         webView->m_parentWindowMessageObserver->windowReceivedMessage(hWnd, message, wParam, lParam);
140
141     if (message == WM_NCDESTROY)
142         ::RemovePropW(hWnd, webViewPointerProperty);
143
144     return ::DefWindowProcW(hWnd, message, wParam, lParam);
145 }
146
147 } // namespace TestWebKitAPI