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.
33 #include "FrameView.h"
34 #include "MouseEvent.h"
37 using namespace WebCore;
41 class WebView::WebViewPrivate {
53 const LPCWSTR kWebViewWindowClassName = L"WebViewWindowClass";
55 LRESULT CALLBACK WebViewWndProc(HWND, UINT, WPARAM, LPARAM);
57 static ATOM registerWebViewWithInstance(HINSTANCE hInstance)
59 static bool haveRegisteredWindowClass = false;
60 if (haveRegisteredWindowClass)
65 wcex.cbSize = sizeof(WNDCLASSEX);
67 wcex.style = CS_DBLCLKS;
68 wcex.lpfnWndProc = WebViewWndProc;
70 wcex.cbWndExtra = 4; // 4 bytes for the WebView pointer
71 wcex.hInstance = hInstance;
73 wcex.hCursor = LoadCursor(0, IDC_ARROW);
74 wcex.hbrBackground = 0;
75 wcex.lpszMenuName = 0;
76 wcex.lpszClassName = kWebViewWindowClassName;
79 return RegisterClassEx(&wcex);
82 // FIXME: This should eventually just use the DLL instance, I think.
83 WebView* WebView::createWebView(HINSTANCE hInstance, HWND parent)
85 // Save away our instace handle for WebCore to use.
86 Widget::instanceHandle = hInstance;
88 registerWebViewWithInstance(hInstance);
90 HWND hWnd = CreateWindow(kWebViewWindowClassName, 0, WS_CHILD | WS_HSCROLL | WS_VSCROLL,
91 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, parent, 0, hInstance, 0);
96 WebView* newWebView = new WebView(hWnd);
97 SetWindowLongPtr(hWnd, 0, (LONG)newWebView);
101 WebView::WebView(HWND hWnd)
103 d = new WebViewPrivate();
104 d->windowHandle = hWnd;
105 d->mainFrame = new WebFrame("dummy", this);
106 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>");
114 HWND WebView::windowHandle()
116 return d->windowHandle;
119 WebFrame* WebView::mainFrame()
124 void WebView::mouseMoved(HWND hWnd, WPARAM wParam, LPARAM lParam)
126 MouseEvent mouseEvent(hWnd, wParam, lParam, 0);
127 d->mainFrame->viewImpl()->viewportMouseMoveEvent(&mouseEvent);
130 void WebView::mouseDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
132 MouseEvent mouseEvent(hWnd, wParam, lParam, 1);
133 d->mainFrame->viewImpl()->viewportMousePressEvent(&mouseEvent);
136 void WebView::mouseUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
138 MouseEvent mouseEvent(hWnd, wParam, lParam, 1);
139 d->mainFrame->viewImpl()->viewportMouseReleaseEvent(&mouseEvent);
142 void WebView::mouseDoubleClick(HWND hWnd, WPARAM wParam, LPARAM lParam)
144 MouseEvent mouseEvent(hWnd, wParam, lParam, 2);
145 d->mainFrame->viewImpl()->viewportMouseReleaseEvent(&mouseEvent);
148 #define LINE_SCROLL_SIZE 30
150 static int calculateScrollDelta(WPARAM wParam, int oldPosition, int pageSize)
152 switch (LOWORD(wParam)) {
154 return -(pageSize - LINE_SCROLL_SIZE);
156 return (pageSize - LINE_SCROLL_SIZE);
158 return -LINE_SCROLL_SIZE;
160 return LINE_SCROLL_SIZE;
161 case SB_THUMBPOSITION:
162 return HIWORD(wParam) - oldPosition;
167 static int scrollMessageForKey(WPARAM keyCode)
183 return (GetKeyState(VK_SHIFT) & 0x8000) ? SB_PAGEUP : SB_PAGEDOWN;
188 LRESULT CALLBACK WebViewWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
191 WebView* webview = (WebView*)GetWindowLongPtr(hWnd, 0);
195 webview->mainFrame()->paint();
201 webview->mouseMoved(hWnd, wParam, lParam);
204 // Make ourselves the focused window before doing anything else
205 // FIXME: I'm not sure if this is the "right" way to do this
206 // but w/o this call, we never become focused since we don't allow
207 // the default handling of mouse events.
211 webview->mouseDown(hWnd, wParam, lParam);
216 webview->mouseUp(hWnd, wParam, lParam);
218 case WM_LBUTTONDBLCLK:
219 case WM_MBUTTONDBLCLK:
220 case WM_RBUTTONDBLCLK:
221 webview->mouseDoubleClick(hWnd, wParam, lParam);
224 ScrollView* view = webview->mainFrame()->impl()->view();
225 view->scrollBy(calculateScrollDelta(wParam, view->contentsX(), view->visibleWidth()), 0);
229 ScrollView* view = webview->mainFrame()->impl()->view();
230 view->scrollBy(0, calculateScrollDelta(wParam, view->contentsY(), view->visibleHeight()));
234 // FIXME: First we should send key events up through the DOM
235 // to form controls, etc. If they are not handled, we fall
236 // through to the top level webview and do things like scrolling
238 WORD wScrollNotify = scrollMessageForKey(wParam);
239 if (wScrollNotify != -1)
240 SendMessage(hWnd, WM_VSCROLL, MAKELONG(wScrollNotify, 0), 0L);
244 return DefWindowProc(hWnd, message, wParam, lParam);