2 * Copyright (C) 2006 Apple Computer, 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "FrameView.h"
35 #include "MouseEvent.h"
38 using namespace WebCore;
42 class WebView::WebViewPrivate {
54 const LPCWSTR kWebViewWindowClassName = L"WebViewWindowClass";
56 LRESULT CALLBACK WebViewWndProc(HWND, UINT, WPARAM, LPARAM);
58 static ATOM registerWebViewWithInstance(HINSTANCE hInstance)
60 static bool haveRegisteredWindowClass = false;
61 if (haveRegisteredWindowClass)
66 wcex.cbSize = sizeof(WNDCLASSEX);
68 wcex.style = CS_DBLCLKS;
69 wcex.lpfnWndProc = WebViewWndProc;
71 wcex.cbWndExtra = 4; // 4 bytes for the WebView pointer
72 wcex.hInstance = hInstance;
74 wcex.hCursor = LoadCursor(0, IDC_ARROW);
75 wcex.hbrBackground = 0;
76 wcex.lpszMenuName = 0;
77 wcex.lpszClassName = kWebViewWindowClassName;
80 return RegisterClassEx(&wcex);
83 // FIXME: This should eventually just use the DLL instance, I think.
84 WebView* WebView::createWebView(HINSTANCE hInstance, HWND parent)
86 // Save away our instace handle for WebCore to use.
87 Widget::instanceHandle = hInstance;
89 registerWebViewWithInstance(hInstance);
91 HWND hWnd = CreateWindow(kWebViewWindowClassName, 0, WS_CHILD | WS_HSCROLL | WS_VSCROLL,
92 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, parent, 0, hInstance, 0);
97 WebView* newWebView = new WebView(hWnd);
98 SetWindowLongPtr(hWnd, 0, (LONG)newWebView);
102 WebView::WebView(HWND hWnd)
104 d = new WebViewPrivate();
105 d->windowHandle = hWnd;
106 d->mainFrame = new WebFrame("dummy", this);
107 d->mainFrame->loadHTMLString("<p style=\"background-color: #00FF00\">Testing</p><img src=\"http://webkit.opendarwin.org/images/icon-gold.png\" alt=\"Face\"><div style=\"border: solid blue\">div with blue border</div><ul><li>foo<li>bar<li>baz</ul>");
115 HWND WebView::windowHandle()
117 return d->windowHandle;
120 WebFrame* WebView::mainFrame()
125 void WebView::mouseMoved(WPARAM wParam, LPARAM lParam)
127 MouseEvent mouseEvent(windowHandle(), wParam, lParam, 0);
128 d->mainFrame->viewImpl()->viewportMouseMoveEvent(&mouseEvent);
131 void WebView::mouseDown(WPARAM wParam, LPARAM lParam)
133 MouseEvent mouseEvent(windowHandle(), wParam, lParam, 1);
134 d->mainFrame->viewImpl()->viewportMousePressEvent(&mouseEvent);
137 void WebView::mouseUp(WPARAM wParam, LPARAM lParam)
139 MouseEvent mouseEvent(windowHandle(), wParam, lParam, 1);
140 d->mainFrame->viewImpl()->viewportMouseReleaseEvent(&mouseEvent);
143 void WebView::mouseDoubleClick(WPARAM wParam, LPARAM lParam)
145 MouseEvent mouseEvent(windowHandle(), wParam, lParam, 2);
146 d->mainFrame->viewImpl()->viewportMouseReleaseEvent(&mouseEvent);
149 bool WebView::keyPress(WPARAM wParam, LPARAM lParam)
151 KeyEvent keyEvent(windowHandle(), wParam, lParam);
152 return static_cast<FrameWin*>(d->mainFrame->impl())->keyPress(&keyEvent);
155 #define LINE_SCROLL_SIZE 30
157 static int calculateScrollDelta(WPARAM wParam, int oldPosition, int pageSize)
159 switch (LOWORD(wParam)) {
161 return -(pageSize - LINE_SCROLL_SIZE);
163 return (pageSize - LINE_SCROLL_SIZE);
165 return -LINE_SCROLL_SIZE;
167 return LINE_SCROLL_SIZE;
168 case SB_THUMBPOSITION:
169 return HIWORD(wParam) - oldPosition;
174 static int scrollMessageForKey(WPARAM keyCode)
190 return (GetKeyState(VK_SHIFT) & 0x8000) ? SB_PAGEUP : SB_PAGEDOWN;
195 LRESULT CALLBACK WebViewWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
198 WebView* webview = (WebView*)GetWindowLongPtr(hWnd, 0);
202 webview->mainFrame()->paint();
208 webview->mouseMoved(wParam, lParam);
211 // Make ourselves the focused window before doing anything else
212 // FIXME: I'm not sure if this is the "right" way to do this
213 // but w/o this call, we never become focused since we don't allow
214 // the default handling of mouse events.
218 webview->mouseDown(wParam, lParam);
223 webview->mouseUp(wParam, lParam);
225 case WM_LBUTTONDBLCLK:
226 case WM_MBUTTONDBLCLK:
227 case WM_RBUTTONDBLCLK:
228 webview->mouseDoubleClick(wParam, lParam);
231 ScrollView* view = webview->mainFrame()->impl()->view();
232 view->scrollBy(calculateScrollDelta(wParam, view->contentsX(), view->visibleWidth()), 0);
233 webview->mainFrame()->impl()->sendScrollEvent();
237 ScrollView* view = webview->mainFrame()->impl()->view();
238 view->scrollBy(0, calculateScrollDelta(wParam, view->contentsY(), view->visibleHeight()));
239 webview->mainFrame()->impl()->sendScrollEvent();
243 // FIXME: First we should send key events up through the DOM
244 // to form controls, etc. If they are not handled, we fall
245 // through to the top level webview and do things like scrolling
246 if (webview->keyPress(wParam, lParam))
249 WORD wScrollNotify = scrollMessageForKey(wParam);
250 if (wScrollNotify != -1)
251 SendMessage(hWnd, WM_VSCROLL, MAKELONG(wScrollNotify, 0), 0L);
254 webview->keyPress(wParam, lParam);
260 webview->mainFrame()->impl()->sendResizeEvent();
263 webview->mainFrame()->impl()->setWindowHasFocus(true);
264 webview->mainFrame()->impl()->setDisplaysWithFocusAttributes(true);
267 webview->mainFrame()->impl()->setWindowHasFocus(false);
268 webview->mainFrame()->impl()->setDisplaysWithFocusAttributes(false);
271 return DefWindowProc(hWnd, message, wParam, lParam);