2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "PlatformWebView.h"
29 #include "WindowMessageObserver.h"
31 namespace TestWebKitAPI {
33 static const wchar_t* hostWindowClassName = L"org.WebKit.TestWebKitAPI.PlatformWebViewHostWindow";
34 static const wchar_t* webViewPointerProperty = L"org.WebKit.TestWebKitAPI.PlatformWebView.InstancePointer";
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;
43 void PlatformWebView::registerWindowClass()
45 static bool initialized;
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;
57 ::RegisterClassExW(&wndClass);
60 PlatformWebView::PlatformWebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef)
61 : m_parentWindowMessageObserver(0)
63 registerWindowClass();
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);
70 PlatformWebView::~PlatformWebView()
72 ::DestroyWindow(m_window);
76 WKPageRef PlatformWebView::page() const
78 return WKViewGetPage(m_view);
81 void PlatformWebView::resizeTo(unsigned width, unsigned height)
83 ::SetWindowPos(WKViewGetWindow(m_view), 0, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOCOPYBITS);
86 void PlatformWebView::simulateSpacebarKeyPress()
88 HWND window = WKViewGetWindow(m_view);
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));
96 void PlatformWebView::simulateAKeyDown()
98 HWND window = WKViewGetWindow(m_view);
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));
104 void PlatformWebView::simulateAltKeyPress()
106 HWND window = WKViewGetWindow(m_view);
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));
113 void PlatformWebView::simulateRightClick(unsigned x, unsigned y)
115 HWND window = WKViewGetWindow(m_view);
117 ::SendMessageW(window, WM_RBUTTONDOWN, 0, MAKELPARAM(x, y));
118 ::SendMessageW(window, WM_RBUTTONUP, 0, MAKELPARAM(x, y));
121 void PlatformWebView::simulateMouseMove(unsigned x, unsigned y)
123 HWND window = WKViewGetWindow(m_view);
125 ::SendMessageW(window, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
128 LRESULT PlatformWebView::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
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);
136 webView = reinterpret_cast<PlatformWebView*>(::GetPropW(hWnd, webViewPointerProperty));
138 if (webView && webView->m_parentWindowMessageObserver)
139 webView->m_parentWindowMessageObserver->windowReceivedMessage(hWnd, message, wParam, lParam);
141 if (message == WM_NCDESTROY)
142 ::RemovePropW(hWnd, webViewPointerProperty);
144 return ::DefWindowProcW(hWnd, message, wParam, lParam);
147 } // namespace TestWebKitAPI