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 "PlatformWebView.h" 27 28 #include "WindowMessageObserver.h" 29 30 namespace TestWebKitAPI { 31 32 static const wchar_t* hostWindowClassName = L"org.WebKit.TestWebKitAPI.PlatformWebViewHostWindow"; 33 static const wchar_t* webViewPointerProperty = L"org.WebKit.TestWebKitAPI.PlatformWebView.InstancePointer"; 34 35 // These offsets come from rom <http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx>. 36 static const size_t repeatCountBitOffset = 0; 37 static const size_t scanCodeBitOffset = 16; 38 static const size_t contextCodeBitOffset = 29; 39 static const size_t previousStateBitOffset = 30; 40 static const size_t transitionStateBitOffset = 31; 41 42 void PlatformWebView::registerWindowClass() 43 { 44 static bool initialized; 45 if (initialized) 46 return; 47 initialized = true; 48 49 WNDCLASSEXW wndClass = {0}; 50 wndClass.cbSize = sizeof(wndClass); 51 wndClass.style = CS_HREDRAW | CS_VREDRAW; 52 wndClass.lpfnWndProc = wndProc; 53 wndClass.hCursor = LoadCursor(0, IDC_ARROW); 54 wndClass.lpszClassName = hostWindowClassName; 55 56 ::RegisterClassExW(&wndClass); 57 } 58 59 PlatformWebView::PlatformWebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef) 60 : m_parentWindowMessageObserver(0) 61 { 62 registerWindowClass(); 63 64 RECT viewRect = {0, 0, 800, 600}; 65 m_window = CreateWindowExW(0, hostWindowClassName, L"TestWebKitAPI", WS_OVERLAPPEDWINDOW, viewRect.left, viewRect.top, viewRect.right, viewRect.bottom, 0, 0, 0, this); 66 m_view = WKViewCreate(viewRect, contextRef, pageGroupRef, m_window); 67 } 68 69 PlatformWebView::~PlatformWebView() 70 { 71 ::DestroyWindow(m_window); 72 WKRelease(m_view); 73 } 74 75 WKPageRef PlatformWebView::page() const 76 { 77 return WKViewGetPage(m_view); 78 } 79 80 void PlatformWebView::resizeTo(unsigned width, unsigned height) 81 { 82 ::SetWindowPos(WKViewGetWindow(m_view), 0, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOCOPYBITS); 83 } 84 85 void PlatformWebView::simulateSpacebarKeyPress() 86 { 87 HWND window = WKViewGetWindow(m_view); 88 89 // These values match what happens when you press the spacebar in Notepad, as observed by Spy++. 90 ::SendMessageW(window, WM_KEYDOWN, VK_SPACE, (1 << repeatCountBitOffset) | (39 << scanCodeBitOffset)); 91 ::SendMessageW(window, WM_CHAR, ' ', (1 << repeatCountBitOffset) | (39 << scanCodeBitOffset)); 92 ::SendMessageW(window, WM_KEYUP, VK_SPACE, (1 << repeatCountBitOffset) | (39 << scanCodeBitOffset) | (1 << previousStateBitOffset) | (1 << transitionStateBitOffset)); 93 } 94 95 void PlatformWebView::simulateAKeyDown() 96 { 97 HWND window = WKViewGetWindow(m_view); 98 99 // These values match what happens when you press the 'A' key in Notepad, as observed by Spy++. 100 ::SendMessageW(window, WM_KEYDOWN, 'A', (1 << repeatCountBitOffset) | (30 << scanCodeBitOffset)); 101 } 102 103 void PlatformWebView::simulateAltKeyPress() 104 { 105 HWND window = WKViewGetWindow(m_view); 106 107 // These values match what happens when you press the Alt key in Notepad, as observed by Spy++. 108 ::SendMessageW(window, WM_SYSKEYDOWN, VK_MENU, (1 << repeatCountBitOffset) | (38 << scanCodeBitOffset) | (1 << contextCodeBitOffset)); 109 ::SendMessageW(window, WM_SYSKEYUP, VK_MENU, (1 << repeatCountBitOffset) | (38 << scanCodeBitOffset) | (1 << previousStateBitOffset) | (1 << transitionStateBitOffset)); 110 } 111 112 void PlatformWebView::simulateRightClick(unsigned x, unsigned y) 113 { 114 HWND window = WKViewGetWindow(m_view); 115 116 ::SendMessageW(window, WM_RBUTTONDOWN, 0, MAKELPARAM(x, y)); 117 ::SendMessageW(window, WM_RBUTTONUP, 0, MAKELPARAM(x, y)); 118 } 119 120 LRESULT PlatformWebView::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 121 { 122 PlatformWebView* webView; 123 if (message == WM_CREATE) { 124 CREATESTRUCT* createStruct = reinterpret_cast<CREATESTRUCT*>(lParam); 125 webView = static_cast<PlatformWebView*>(createStruct->lpCreateParams); 126 ::SetPropW(hWnd, webViewPointerProperty, webView); 127 } else 128 webView = reinterpret_cast<PlatformWebView*>(::GetPropW(hWnd, webViewPointerProperty)); 129 130 if (webView && webView->m_parentWindowMessageObserver) 131 webView->m_parentWindowMessageObserver->windowReceivedMessage(hWnd, message, wParam, lParam); 132 133 if (message == WM_NCDESTROY) 134 ::RemovePropW(hWnd, webViewPointerProperty); 135 136 return ::DefWindowProcW(hWnd, message, wParam, lParam); 137 } 138 139 } // namespace TestWebKitAPI 140