Rubber-stamped by Brady Eidson.
[webbrowser.git] / WebKitTools / WinLauncher / WinLauncher.cpp
blob2dda619ae13d36ba77e1dc0d7026a6a1aef363e8
1 /*
2 * Copyright (C) 2006, 2008 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
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.
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.
26 #include "stdafx.h"
27 #include "WinLauncher.h"
28 #include <WebKit/WebKitCOMAPI.h>
30 #include <commctrl.h>
31 #include <objbase.h>
32 #include <shlwapi.h>
33 #include <wininet.h>
35 #define MAX_LOADSTRING 100
36 #define URLBAR_HEIGHT 24
38 // Global Variables:
39 HINSTANCE hInst; // current instance
40 HWND hMainWnd;
41 HWND hURLBarWnd;
42 long DefEditProc;
43 IWebView* gWebView = 0;
44 HWND gViewWindow = 0;
45 WinLauncherWebHost* gWebHost = 0;
46 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
47 TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
49 // Forward declarations of functions included in this code module:
50 ATOM MyRegisterClass(HINSTANCE hInstance);
51 BOOL InitInstance(HINSTANCE, int);
52 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
53 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
54 LRESULT CALLBACK MyEditProc(HWND, UINT, WPARAM, LPARAM);
56 static void loadURL(BSTR urlBStr);
58 HRESULT WinLauncherWebHost::updateAddressBar(IWebView* webView)
60 IWebFrame* mainFrame = 0;
61 IWebDataSource* dataSource = 0;
62 IWebMutableURLRequest* request = 0;
63 BSTR frameURL = 0;
65 HRESULT hr = S_OK;
67 hr = webView->mainFrame(&mainFrame);
68 if (FAILED(hr))
69 goto exit;
71 hr = mainFrame->dataSource(&dataSource);
72 if (FAILED(hr) || !dataSource)
73 hr = mainFrame->provisionalDataSource(&dataSource);
74 if (FAILED(hr) || !dataSource)
75 goto exit;
77 hr = dataSource->request(&request);
78 if (FAILED(hr) || !request)
79 goto exit;
81 hr = request->mainDocumentURL(&frameURL);
82 if (FAILED(hr))
83 goto exit;
85 SendMessage(hURLBarWnd, (UINT)WM_SETTEXT, 0, (LPARAM)frameURL);
87 exit:
88 if (mainFrame)
89 mainFrame->Release();
90 if (dataSource)
91 dataSource->Release();
92 if (request)
93 request->Release();
94 SysFreeString(frameURL);
95 return 0;
98 HRESULT STDMETHODCALLTYPE WinLauncherWebHost::QueryInterface(REFIID riid, void** ppvObject)
100 *ppvObject = 0;
101 if (IsEqualGUID(riid, IID_IUnknown))
102 *ppvObject = static_cast<IWebFrameLoadDelegate*>(this);
103 else if (IsEqualGUID(riid, IID_IWebFrameLoadDelegate))
104 *ppvObject = static_cast<IWebFrameLoadDelegate*>(this);
105 else
106 return E_NOINTERFACE;
108 AddRef();
109 return S_OK;
112 ULONG STDMETHODCALLTYPE WinLauncherWebHost::AddRef(void)
114 return ++m_refCount;
117 ULONG STDMETHODCALLTYPE WinLauncherWebHost::Release(void)
119 ULONG newRef = --m_refCount;
120 if (!newRef)
121 delete(this);
123 return newRef;
126 static void resizeSubViews()
128 RECT rcClient;
129 GetClientRect(hMainWnd, &rcClient);
130 MoveWindow(hURLBarWnd, 0, 0, rcClient.right, URLBAR_HEIGHT, TRUE);
131 MoveWindow(gViewWindow, 0, URLBAR_HEIGHT, rcClient.right, rcClient.bottom - URLBAR_HEIGHT, TRUE);
134 int APIENTRY _tWinMain(HINSTANCE hInstance,
135 HINSTANCE hPrevInstance,
136 LPTSTR lpCmdLine,
137 int nCmdShow)
139 #ifdef _CRTDBG_MAP_ALLOC
140 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
141 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
142 #endif
144 UNREFERENCED_PARAMETER(hPrevInstance);
145 UNREFERENCED_PARAMETER(lpCmdLine);
147 // TODO: Place code here.
148 MSG msg;
149 HACCEL hAccelTable;
151 INITCOMMONCONTROLSEX InitCtrlEx;
153 InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
154 InitCtrlEx.dwICC = 0x00004000; //ICC_STANDARD_CLASSES;
155 InitCommonControlsEx(&InitCtrlEx);
157 // Initialize global strings
158 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
159 LoadString(hInstance, IDC_WINLAUNCHER, szWindowClass, MAX_LOADSTRING);
160 MyRegisterClass(hInstance);
162 // Perform application initialization:
163 if (!InitInstance (hInstance, nCmdShow))
164 return FALSE;
166 // Init COM
167 OleInitialize(NULL);
169 hURLBarWnd = CreateWindow(L"EDIT", 0,
170 WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOVSCROLL,
171 0, 0, 0, 0,
172 hMainWnd,
174 hInstance, 0);
176 DefEditProc = GetWindowLong(hURLBarWnd, GWL_WNDPROC);
177 SetWindowLong(hURLBarWnd, GWL_WNDPROC,(long)MyEditProc);
178 SetFocus(hURLBarWnd);
180 HRESULT hr = WebKitCreateInstance(CLSID_WebView, 0, IID_IWebView, (void**)&gWebView);
181 if (FAILED(hr))
182 goto exit;
184 gWebHost = new WinLauncherWebHost();
185 gWebHost->AddRef();
186 hr = gWebView->setFrameLoadDelegate(gWebHost);
187 if (FAILED(hr))
188 goto exit;
190 hr = gWebView->setHostWindow((OLE_HANDLE) hMainWnd);
191 if (FAILED(hr))
192 goto exit;
194 RECT clientRect;
195 GetClientRect(hMainWnd, &clientRect);
196 hr = gWebView->initWithFrame(clientRect, 0, 0);
197 if (FAILED(hr))
198 goto exit;
200 IWebFrame* frame;
201 hr = gWebView->mainFrame(&frame);
202 if (FAILED(hr))
203 goto exit;
205 static BSTR defaultHTML = SysAllocString(TEXT("<p style=\"background-color: #00FF00\">Testing</p><img src=\"http://webkit.org/images/icon-gold.png\" alt=\"Face\"><div style=\"border: solid blue\" contenteditable=\"true\">div with blue border</div><ul><li>foo<li>bar<li>baz</ul>"));
206 frame->loadHTMLString(defaultHTML, 0);
207 frame->Release();
209 IWebViewPrivate* viewExt;
210 hr = gWebView->QueryInterface(IID_IWebViewPrivate, (void**)&viewExt);
211 if (FAILED(hr))
212 goto exit;
214 hr = viewExt->viewWindow((OLE_HANDLE*) &gViewWindow);
215 viewExt->Release();
216 if (FAILED(hr) || !gViewWindow)
217 goto exit;
219 resizeSubViews();
221 ShowWindow(gViewWindow, nCmdShow);
222 UpdateWindow(gViewWindow);
224 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINLAUNCHER));
226 // Main message loop:
227 while (GetMessage(&msg, NULL, 0, 0)) {
228 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
229 TranslateMessage(&msg);
230 DispatchMessage(&msg);
234 exit:
235 gWebView->Release();
236 shutDownWebKit();
237 #ifdef _CRTDBG_MAP_ALLOC
238 _CrtDumpMemoryLeaks();
239 #endif
241 // Shut down COM.
242 OleUninitialize();
244 return static_cast<int>(msg.wParam);
247 ATOM MyRegisterClass(HINSTANCE hInstance)
249 WNDCLASSEX wcex;
251 wcex.cbSize = sizeof(WNDCLASSEX);
253 wcex.style = CS_HREDRAW | CS_VREDRAW;
254 wcex.lpfnWndProc = WndProc;
255 wcex.cbClsExtra = 0;
256 wcex.cbWndExtra = 0;
257 wcex.hInstance = hInstance;
258 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINLAUNCHER));
259 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
260 wcex.hbrBackground = 0;
261 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WINLAUNCHER);
262 wcex.lpszClassName = szWindowClass;
263 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
265 return RegisterClassEx(&wcex);
268 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
270 hInst = hInstance; // Store instance handle in our global variable
272 hMainWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
273 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
275 if (!hMainWnd)
276 return FALSE;
278 ShowWindow(hMainWnd, nCmdShow);
279 UpdateWindow(hMainWnd);
281 return TRUE;
284 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
286 int wmId, wmEvent;
287 PAINTSTRUCT ps;
288 HDC hdc;
290 switch (message) {
291 case WM_COMMAND:
292 wmId = LOWORD(wParam);
293 wmEvent = HIWORD(wParam);
294 // Parse the menu selections:
295 switch (wmId) {
296 case IDM_ABOUT:
297 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
298 break;
299 case IDM_EXIT:
300 DestroyWindow(hWnd);
301 break;
302 default:
303 return DefWindowProc(hWnd, message, wParam, lParam);
305 break;
306 case WM_DESTROY:
307 PostQuitMessage(0);
308 break;
309 case WM_SIZE:
310 if (!gWebView)
311 break;
312 resizeSubViews();
313 break;
314 default:
315 return DefWindowProc(hWnd, message, wParam, lParam);
317 return 0;
321 #define MAX_URL_LENGTH 1024
323 LRESULT CALLBACK MyEditProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
325 switch (message) {
326 case WM_CHAR:
327 if (wParam == 13) { // Enter Key
328 wchar_t strPtr[MAX_URL_LENGTH];
329 *((LPWORD)strPtr) = MAX_URL_LENGTH;
330 int strLen = SendMessage(hDlg, EM_GETLINE, 0, (LPARAM)strPtr);
332 BSTR bstr = SysAllocStringLen(strPtr, strLen);
333 loadURL(bstr);
334 SysFreeString(bstr);
336 return 0;
337 } else
338 return (LRESULT)CallWindowProc((WNDPROC)DefEditProc,hDlg,message,wParam,lParam);
339 break;
340 default:
341 return (LRESULT)CallWindowProc((WNDPROC)DefEditProc,hDlg,message,wParam,lParam);
342 break;
344 return 0;
348 // Message handler for about box.
349 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
351 UNREFERENCED_PARAMETER(lParam);
352 switch (message) {
353 case WM_INITDIALOG:
354 return (INT_PTR)TRUE;
356 case WM_COMMAND:
357 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
358 EndDialog(hDlg, LOWORD(wParam));
359 return (INT_PTR)TRUE;
361 break;
363 return (INT_PTR)FALSE;
366 static void loadURL(BSTR urlBStr)
368 IWebFrame* frame = 0;
369 IWebMutableURLRequest* request = 0;
371 static BSTR methodBStr = SysAllocString(TEXT("GET"));
373 if (urlBStr && urlBStr[0] && (PathFileExists(urlBStr) || PathIsUNC(urlBStr))) {
374 TCHAR fileURL[INTERNET_MAX_URL_LENGTH];
375 DWORD fileURLLength = sizeof(fileURL)/sizeof(fileURL[0]);
377 if (SUCCEEDED(UrlCreateFromPath(urlBStr, fileURL, &fileURLLength, 0)))
378 SysReAllocString(&urlBStr, fileURL);
381 HRESULT hr = gWebView->mainFrame(&frame);
382 if (FAILED(hr))
383 goto exit;
385 hr = WebKitCreateInstance(CLSID_WebMutableURLRequest, 0, IID_IWebMutableURLRequest, (void**)&request);
386 if (FAILED(hr))
387 goto exit;
389 hr = request->initWithURL(urlBStr, WebURLRequestUseProtocolCachePolicy, 60);
390 if (FAILED(hr))
391 goto exit;
393 hr = request->setHTTPMethod(methodBStr);
394 if (FAILED(hr))
395 goto exit;
397 hr = frame->loadRequest(request);
398 if (FAILED(hr))
399 goto exit;
401 SetFocus(gViewWindow);
403 exit:
404 if (frame)
405 frame->Release();
406 if (request)
407 request->Release();