2 * Copyright (C) 2006, 2008 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2009, 2011 Brent Fulgham. All rights reserved.
4 * Copyright (C) 2009, 2010, 2011 Appcelerator, Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "WinLauncher.h"
31 #include "DOMDefaultImpl.h"
32 #include "PrintWebUIDelegate.h"
33 #include <WebKit/WebKitCOMAPI.h>
43 #define MAX_LOADSTRING 100
44 #define URLBAR_HEIGHT 24
47 HINSTANCE hInst; // current instance
50 WNDPROC DefEditProc = 0;
51 WNDPROC DefWebKitProc = 0;
52 IWebView* gWebView = 0;
53 IWebViewPrivate* gWebViewPrivate = 0;
55 WinLauncherWebHost* gWebHost = 0;
56 PrintWebUIDelegate* gPrintDelegate = 0;
57 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
58 TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
60 // Support moving the transparent window
61 POINT s_windowPosition = { 100, 100 };
62 SIZE s_windowSize = { 800, 400 };
63 bool s_usesLayeredWebView = false;
64 bool s_fullDesktop = false;
66 // Forward declarations of functions included in this code module:
67 ATOM MyRegisterClass(HINSTANCE hInstance);
68 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
69 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
70 LRESULT CALLBACK MyEditProc(HWND, UINT, WPARAM, LPARAM);
72 static void loadURL(BSTR urlBStr);
74 static bool usesLayeredWebView()
76 return s_usesLayeredWebView;
79 static bool shouldUseFullDesktop()
84 class SimpleEventListener : public DOMEventListener {
86 SimpleEventListener(LPWSTR type)
88 wcsncpy_s(m_eventType, 100, type, 100);
92 virtual HRESULT STDMETHODCALLTYPE handleEvent(IDOMEvent* evt)
95 wcscpy_s(message, 255, m_eventType);
96 wcscat_s(message, 255, L" event fired!");
97 ::MessageBox(0, message, L"Event Handler", MB_OK);
102 wchar_t m_eventType[100];
105 HRESULT WinLauncherWebHost::updateAddressBar(IWebView* webView)
107 IWebFrame* mainFrame = 0;
108 IWebDataSource* dataSource = 0;
109 IWebMutableURLRequest* request = 0;
114 hr = webView->mainFrame(&mainFrame);
118 hr = mainFrame->dataSource(&dataSource);
119 if (FAILED(hr) || !dataSource)
120 hr = mainFrame->provisionalDataSource(&dataSource);
121 if (FAILED(hr) || !dataSource)
124 hr = dataSource->request(&request);
125 if (FAILED(hr) || !request)
128 hr = request->mainDocumentURL(&frameURL);
132 SendMessage(hURLBarWnd, (UINT)WM_SETTEXT, 0, (LPARAM)frameURL);
136 mainFrame->Release();
138 dataSource->Release();
141 SysFreeString(frameURL);
145 HRESULT STDMETHODCALLTYPE WinLauncherWebHost::QueryInterface(REFIID riid, void** ppvObject)
148 if (IsEqualGUID(riid, IID_IUnknown))
149 *ppvObject = static_cast<IWebFrameLoadDelegate*>(this);
150 else if (IsEqualGUID(riid, IID_IWebFrameLoadDelegate))
151 *ppvObject = static_cast<IWebFrameLoadDelegate*>(this);
153 return E_NOINTERFACE;
159 ULONG STDMETHODCALLTYPE WinLauncherWebHost::AddRef(void)
164 ULONG STDMETHODCALLTYPE WinLauncherWebHost::Release(void)
166 ULONG newRef = --m_refCount;
173 HRESULT WinLauncherWebHost::didFinishLoadForFrame(IWebView* webView, IWebFrame* frame)
175 IDOMDocument* doc = 0;
176 frame->DOMDocument(&doc);
178 IDOMElement* element = 0;
179 IDOMEventTarget* target = 0;
180 HRESULT hr = doc->getElementById(L"webkit logo", &element);
184 hr = element->QueryInterface(IID_IDOMEventTarget, reinterpret_cast<void**>(&target));
188 hr = target->addEventListener(L"click", new SimpleEventListener (L"webkit logo click"), FALSE);
203 static void resizeSubViews()
205 if (usesLayeredWebView() || !gViewWindow)
209 GetClientRect(hMainWnd, &rcClient);
210 MoveWindow(hURLBarWnd, 0, 0, rcClient.right, URLBAR_HEIGHT, TRUE);
211 MoveWindow(gViewWindow, 0, URLBAR_HEIGHT, rcClient.right, rcClient.bottom - URLBAR_HEIGHT, TRUE);
214 static void subclassForLayeredWindow()
216 hMainWnd = gViewWindow;
217 DefWebKitProc = reinterpret_cast<WNDPROC>(::GetWindowLongPtr(hMainWnd, GWL_WNDPROC));
218 ::SetWindowLongPtr(hMainWnd, GWL_WNDPROC, reinterpret_cast<LONG_PTR>(WndProc));
221 static void computeFullDesktopFrame()
224 if (!::SystemParametersInfo(SPI_GETWORKAREA, 0, static_cast<void*>(&desktop), 0))
227 s_windowPosition.x = 0;
228 s_windowPosition.y = 0;
229 s_windowSize.cx = desktop.right - desktop.left;
230 s_windowSize.cy = desktop.bottom - desktop.top;
233 BOOL WINAPI DllMain(HINSTANCE dllInstance, DWORD reason, LPVOID)
235 if (reason == DLL_PROCESS_ATTACH)
241 extern "C" __declspec(dllexport) int WINAPI dllLauncherEntryPoint(HINSTANCE, HINSTANCE, LPTSTR, int nCmdShow)
243 #ifdef _CRTDBG_MAP_ALLOC
244 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
245 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
248 // TODO: Place code here.
252 INITCOMMONCONTROLSEX InitCtrlEx;
254 InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
255 InitCtrlEx.dwICC = 0x00004000; //ICC_STANDARD_CLASSES;
256 InitCommonControlsEx(&InitCtrlEx);
259 WCHAR** argv = CommandLineToArgvW(GetCommandLineW(), &argc);
260 for (int i = 1; i < argc; ++i) {
261 if (!wcsicmp(argv[i], L"--transparent"))
262 s_usesLayeredWebView = true;
263 else if (!wcsicmp(argv[i], L"--desktop"))
264 s_fullDesktop = true;
267 // Initialize global strings
268 LoadString(hInst, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
269 LoadString(hInst, IDC_WINLAUNCHER, szWindowClass, MAX_LOADSTRING);
270 MyRegisterClass(hInst);
272 if (shouldUseFullDesktop())
273 computeFullDesktopFrame();
278 if (usesLayeredWebView()) {
279 hURLBarWnd = CreateWindow(L"EDIT", L"Type URL Here",
280 WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOVSCROLL,
281 s_windowPosition.x, s_windowPosition.y + s_windowSize.cy, s_windowSize.cx, URLBAR_HEIGHT,
286 hMainWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
287 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, hInst, 0);
292 hURLBarWnd = CreateWindow(L"EDIT", 0,
293 WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOVSCROLL,
299 ShowWindow(hMainWnd, nCmdShow);
300 UpdateWindow(hMainWnd);
303 DefEditProc = reinterpret_cast<WNDPROC>(GetWindowLongPtr(hURLBarWnd, GWL_WNDPROC));
304 SetWindowLongPtr(hURLBarWnd, GWL_WNDPROC, reinterpret_cast<LONG_PTR>(MyEditProc));
305 SetFocus(hURLBarWnd);
307 RECT clientRect = { s_windowPosition.x, s_windowPosition.y, s_windowPosition.x + s_windowSize.cx, s_windowPosition.y + s_windowSize.cy };
309 IWebPreferences* tmpPreferences = 0;
310 IWebPreferences* standardPreferences = 0;
311 if (FAILED(WebKitCreateInstance(CLSID_WebPreferences, 0, IID_IWebPreferences, reinterpret_cast<void**>(&tmpPreferences))))
314 if (FAILED(tmpPreferences->standardPreferences(&standardPreferences)))
317 standardPreferences->setAcceleratedCompositingEnabled(TRUE);
319 HRESULT hr = WebKitCreateInstance(CLSID_WebView, 0, IID_IWebView, reinterpret_cast<void**>(&gWebView));
323 hr = gWebView->QueryInterface(IID_IWebViewPrivate, reinterpret_cast<void**>(&gWebViewPrivate));
327 gWebHost = new WinLauncherWebHost();
329 hr = gWebView->setFrameLoadDelegate(gWebHost);
333 gPrintDelegate = new PrintWebUIDelegate;
334 gPrintDelegate->AddRef();
335 hr = gWebView->setUIDelegate(gPrintDelegate);
339 hr = gWebView->setHostWindow(reinterpret_cast<OLE_HANDLE>(hMainWnd));
343 hr = gWebView->initWithFrame(clientRect, 0, 0);
348 hr = gWebView->mainFrame(&frame);
352 static BSTR defaultHTML = SysAllocString(TEXT("<p style=\"background-color: #00FF00\">Testing</p><img id=\"webkit logo\" src=\"http://webkit.org/images/icon-gold.png\" alt=\"Face\"><div style=\"border: solid blue; background: white;\" contenteditable=\"true\">div with blue border</div><ul><li>foo<li>bar<li>baz</ul>"));
353 frame->loadHTMLString(defaultHTML, 0);
356 hr = gWebViewPrivate->setTransparent(usesLayeredWebView());
360 hr = gWebViewPrivate->setUsesLayeredWindow(usesLayeredWebView());
364 hr = gWebViewPrivate->viewWindow(reinterpret_cast<OLE_HANDLE*>(&gViewWindow));
365 if (FAILED(hr) || !gViewWindow)
368 if (usesLayeredWebView())
369 subclassForLayeredWindow();
373 ShowWindow(gViewWindow, nCmdShow);
374 UpdateWindow(gViewWindow);
376 hAccelTable = LoadAccelerators(hInst, MAKEINTRESOURCE(IDC_WINLAUNCHER));
378 // Main message loop:
379 while (GetMessage(&msg, NULL, 0, 0)) {
380 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
381 TranslateMessage(&msg);
382 DispatchMessage(&msg);
387 gPrintDelegate->Release();
389 gWebViewPrivate->Release();
391 if (standardPreferences)
392 standardPreferences->Release();
393 tmpPreferences->Release();
396 #ifdef _CRTDBG_MAP_ALLOC
397 _CrtDumpMemoryLeaks();
403 return static_cast<int>(msg.wParam);
406 ATOM MyRegisterClass(HINSTANCE hInstance)
410 wcex.cbSize = sizeof(WNDCLASSEX);
412 wcex.style = CS_HREDRAW | CS_VREDRAW;
413 wcex.lpfnWndProc = WndProc;
416 wcex.hInstance = hInstance;
417 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINLAUNCHER));
418 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
419 wcex.hbrBackground = 0;
420 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WINLAUNCHER);
421 wcex.lpszClassName = szWindowClass;
422 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
424 return RegisterClassEx(&wcex);
427 static BOOL CALLBACK AbortProc(HDC hDC, int Error)
430 while (::PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
431 ::TranslateMessage(&msg);
432 ::DispatchMessage(&msg);
438 static HDC getPrinterDC()
441 memset(&pdlg, 0, sizeof(PRINTDLG));
442 pdlg.lStructSize = sizeof(PRINTDLG);
443 pdlg.Flags = PD_PRINTSETUP | PD_RETURNDC;
450 static void initDocStruct(DOCINFO* di, TCHAR* docname)
452 memset(di, 0, sizeof(DOCINFO));
453 di->cbSize = sizeof(DOCINFO);
454 di->lpszDocName = docname;
457 void PrintView(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
459 HDC printDC = getPrinterDC();
461 ::MessageBoxW(0, L"Error creating printing DC", L"Error", MB_APPLMODAL | MB_OK);
465 if (::SetAbortProc(printDC, AbortProc) == SP_ERROR) {
466 ::MessageBoxW(0, L"Error setting up AbortProc", L"Error", MB_APPLMODAL | MB_OK);
470 IWebFrame* frame = 0;
471 IWebFramePrivate* framePrivate = 0;
472 if (FAILED(gWebView->mainFrame(&frame)))
475 if (FAILED(frame->QueryInterface(&framePrivate)))
478 framePrivate->setInPrintingMode(TRUE, printDC);
481 framePrivate->getPrintedPageCount(printDC, &pageCount);
484 initDocStruct(&di, L"WebKit Doc");
485 ::StartDoc(printDC, &di);
487 // FIXME: Need CoreGraphics implementation
488 void* graphicsContext = 0;
489 for (size_t page = 1; page <= pageCount; ++page) {
490 ::StartPage(printDC);
491 framePrivate->spoolPages(printDC, page, page, graphicsContext);
495 framePrivate->setInPrintingMode(FALSE, printDC);
504 framePrivate->Release();
507 static const int dragBarHeight = 30;
509 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
511 WNDPROC parentProc = usesLayeredWebView() ? DefWebKitProc : DefWindowProc;
515 if (usesLayeredWebView()) {
517 ::GetWindowRect(hWnd, &window);
518 // For testing our transparent window, we need a region to use as a handle for
519 // dragging. The right way to do this would be to query the web view to see what's
520 // under the mouse. However, for testing purposes we just use an arbitrary
521 // 30 pixel band at the top of the view as an arbitrary gripping location.
523 // When we are within this bad, return HT_CAPTION to tell Windows we want to
524 // treat this region as if it were the title bar on a normal window.
525 int y = HIWORD(lParam);
527 if ((y > window.top) && (y < window.top + dragBarHeight))
530 return CallWindowProc(parentProc, hWnd, message, wParam, lParam);
532 int wmId = LOWORD(wParam);
533 int wmEvent = HIWORD(wParam);
534 // Parse the menu selections:
537 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
543 PrintView(hWnd, message, wParam, lParam);
546 return CallWindowProc(parentProc, hWnd, message, wParam, lParam);
554 if (!gWebView || usesLayeredWebView())
555 return CallWindowProc(parentProc, hWnd, message, wParam, lParam);
560 return CallWindowProc(parentProc, hWnd, message, wParam, lParam);
566 #define MAX_URL_LENGTH 1024
568 LRESULT CALLBACK MyEditProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
572 if (wParam == 13) { // Enter Key
573 wchar_t strPtr[MAX_URL_LENGTH];
574 *((LPWORD)strPtr) = MAX_URL_LENGTH;
575 int strLen = SendMessage(hDlg, EM_GETLINE, 0, (LPARAM)strPtr);
577 BSTR bstr = SysAllocStringLen(strPtr, strLen);
583 return (LRESULT)CallWindowProc((WNDPROC)DefEditProc,hDlg,message,wParam,lParam);
586 return (LRESULT)CallWindowProc((WNDPROC)DefEditProc,hDlg,message,wParam,lParam);
592 // Message handler for about box.
593 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
595 UNREFERENCED_PARAMETER(lParam);
598 return (INT_PTR)TRUE;
601 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
602 EndDialog(hDlg, LOWORD(wParam));
603 return (INT_PTR)TRUE;
607 return (INT_PTR)FALSE;
610 static void loadURL(BSTR urlBStr)
612 IWebFrame* frame = 0;
613 IWebMutableURLRequest* request = 0;
615 static BSTR methodBStr = SysAllocString(TEXT("GET"));
617 if (urlBStr && urlBStr[0] && (PathFileExists(urlBStr) || PathIsUNC(urlBStr))) {
618 TCHAR fileURL[INTERNET_MAX_URL_LENGTH];
619 DWORD fileURLLength = sizeof(fileURL)/sizeof(fileURL[0]);
621 if (SUCCEEDED(UrlCreateFromPath(urlBStr, fileURL, &fileURLLength, 0)))
622 SysReAllocString(&urlBStr, fileURL);
625 HRESULT hr = gWebView->mainFrame(&frame);
629 hr = WebKitCreateInstance(CLSID_WebMutableURLRequest, 0, IID_IWebMutableURLRequest, (void**)&request);
633 hr = request->initWithURL(urlBStr, WebURLRequestUseProtocolCachePolicy, 60);
637 hr = request->setHTTPMethod(methodBStr);
641 hr = frame->loadRequest(request);
645 SetFocus(gViewWindow);