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 BOOL InitInstance(HINSTANCE, int);
69 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
70 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
71 LRESULT CALLBACK MyEditProc(HWND, UINT, WPARAM, LPARAM);
73 static void loadURL(BSTR urlBStr);
75 static bool usesLayeredWebView()
77 return s_usesLayeredWebView;
80 static bool shouldUseFullDesktop()
85 class SimpleEventListener : public DOMEventListener {
87 SimpleEventListener(LPWSTR type)
89 wcsncpy_s(m_eventType, 100, type, 100);
93 virtual HRESULT STDMETHODCALLTYPE handleEvent(IDOMEvent* evt)
96 wcscpy_s(message, 255, m_eventType);
97 wcscat_s(message, 255, L" event fired!");
98 ::MessageBox(0, message, L"Event Handler", MB_OK);
103 wchar_t m_eventType[100];
106 HRESULT WinLauncherWebHost::updateAddressBar(IWebView* webView)
108 IWebFrame* mainFrame = 0;
109 IWebDataSource* dataSource = 0;
110 IWebMutableURLRequest* request = 0;
115 hr = webView->mainFrame(&mainFrame);
119 hr = mainFrame->dataSource(&dataSource);
120 if (FAILED(hr) || !dataSource)
121 hr = mainFrame->provisionalDataSource(&dataSource);
122 if (FAILED(hr) || !dataSource)
125 hr = dataSource->request(&request);
126 if (FAILED(hr) || !request)
129 hr = request->mainDocumentURL(&frameURL);
133 SendMessage(hURLBarWnd, (UINT)WM_SETTEXT, 0, (LPARAM)frameURL);
137 mainFrame->Release();
139 dataSource->Release();
142 SysFreeString(frameURL);
146 HRESULT STDMETHODCALLTYPE WinLauncherWebHost::QueryInterface(REFIID riid, void** ppvObject)
149 if (IsEqualGUID(riid, IID_IUnknown))
150 *ppvObject = static_cast<IWebFrameLoadDelegate*>(this);
151 else if (IsEqualGUID(riid, IID_IWebFrameLoadDelegate))
152 *ppvObject = static_cast<IWebFrameLoadDelegate*>(this);
154 return E_NOINTERFACE;
160 ULONG STDMETHODCALLTYPE WinLauncherWebHost::AddRef(void)
165 ULONG STDMETHODCALLTYPE WinLauncherWebHost::Release(void)
167 ULONG newRef = --m_refCount;
174 HRESULT WinLauncherWebHost::didFinishLoadForFrame(IWebView* webView, IWebFrame* frame)
176 IDOMDocument* doc = 0;
177 frame->DOMDocument(&doc);
179 IDOMElement* element = 0;
180 IDOMEventTarget* target = 0;
181 HRESULT hr = doc->getElementById(L"webkit logo", &element);
185 hr = element->QueryInterface(IID_IDOMEventTarget, reinterpret_cast<void**>(&target));
189 hr = target->addEventListener(L"click", new SimpleEventListener (L"webkit logo click"), FALSE);
204 static void resizeSubViews()
206 if (usesLayeredWebView() || !gViewWindow)
210 GetClientRect(hMainWnd, &rcClient);
211 MoveWindow(hURLBarWnd, 0, 0, rcClient.right, URLBAR_HEIGHT, TRUE);
212 MoveWindow(gViewWindow, 0, URLBAR_HEIGHT, rcClient.right, rcClient.bottom - URLBAR_HEIGHT, TRUE);
215 static void subclassForLayeredWindow()
217 hMainWnd = gViewWindow;
218 DefWebKitProc = reinterpret_cast<WNDPROC>(::GetWindowLongPtr(hMainWnd, GWL_WNDPROC));
219 ::SetWindowLongPtr(hMainWnd, GWL_WNDPROC, reinterpret_cast<LONG_PTR>(WndProc));
222 static void computeFullDesktopFrame()
225 if (!::SystemParametersInfo(SPI_GETWORKAREA, 0, static_cast<void*>(&desktop), 0))
228 s_windowPosition.x = 0;
229 s_windowPosition.y = 0;
230 s_windowSize.cx = desktop.right - desktop.left;
231 s_windowSize.cy = desktop.bottom - desktop.top;
234 int APIENTRY _tWinMain(HINSTANCE hInstance,
235 HINSTANCE hPrevInstance,
239 #ifdef _CRTDBG_MAP_ALLOC
240 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
241 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
244 UNREFERENCED_PARAMETER(hPrevInstance);
245 UNREFERENCED_PARAMETER(lpCmdLine);
247 // TODO: Place code here.
251 INITCOMMONCONTROLSEX InitCtrlEx;
253 InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
254 InitCtrlEx.dwICC = 0x00004000; //ICC_STANDARD_CLASSES;
255 InitCommonControlsEx(&InitCtrlEx);
258 WCHAR** argv = CommandLineToArgvW(GetCommandLineW(), &argc);
259 for (int i = 1; i < argc; ++i) {
260 if (!wcsicmp(argv[i], L"--transparent"))
261 s_usesLayeredWebView = true;
262 else if (!wcsicmp(argv[i], L"--desktop"))
263 s_fullDesktop = true;
266 // Initialize global strings
267 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
268 LoadString(hInstance, IDC_WINLAUNCHER, szWindowClass, MAX_LOADSTRING);
269 MyRegisterClass(hInstance);
271 if (shouldUseFullDesktop())
272 computeFullDesktopFrame();
274 // Perform application initialization:
275 if (!InitInstance (hInstance, nCmdShow))
281 if (usesLayeredWebView()) {
282 hURLBarWnd = CreateWindow(L"EDIT", L"Type URL Here",
283 WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOVSCROLL,
284 s_windowPosition.x, s_windowPosition.y + s_windowSize.cy, s_windowSize.cx, URLBAR_HEIGHT,
289 hURLBarWnd = CreateWindow(L"EDIT", 0,
290 WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOVSCROLL,
297 DefEditProc = reinterpret_cast<WNDPROC>(GetWindowLongPtr(hURLBarWnd, GWL_WNDPROC));
298 SetWindowLongPtr(hURLBarWnd, GWL_WNDPROC, reinterpret_cast<LONG_PTR>(MyEditProc));
299 SetFocus(hURLBarWnd);
301 RECT clientRect = { s_windowPosition.x, s_windowPosition.y, s_windowPosition.x + s_windowSize.cx, s_windowPosition.y + s_windowSize.cy };
303 IWebPreferences* tmpPreferences = 0;
304 IWebPreferences* standardPreferences = 0;
305 if (FAILED(WebKitCreateInstance(CLSID_WebPreferences, 0, IID_IWebPreferences, reinterpret_cast<void**>(&tmpPreferences))))
308 if (FAILED(tmpPreferences->standardPreferences(&standardPreferences)))
311 standardPreferences->setAcceleratedCompositingEnabled(TRUE);
313 HRESULT hr = WebKitCreateInstance(CLSID_WebView, 0, IID_IWebView, reinterpret_cast<void**>(&gWebView));
317 hr = gWebView->QueryInterface(IID_IWebViewPrivate, reinterpret_cast<void**>(&gWebViewPrivate));
321 gWebHost = new WinLauncherWebHost();
323 hr = gWebView->setFrameLoadDelegate(gWebHost);
327 gPrintDelegate = new PrintWebUIDelegate;
328 gPrintDelegate->AddRef();
329 hr = gWebView->setUIDelegate(gPrintDelegate);
333 hr = gWebView->setHostWindow(reinterpret_cast<OLE_HANDLE>(hMainWnd));
337 hr = gWebView->initWithFrame(clientRect, 0, 0);
342 hr = gWebView->mainFrame(&frame);
346 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>"));
347 frame->loadHTMLString(defaultHTML, 0);
350 hr = gWebViewPrivate->setTransparent(usesLayeredWebView());
354 hr = gWebViewPrivate->setUsesLayeredWindow(usesLayeredWebView());
358 hr = gWebViewPrivate->viewWindow(reinterpret_cast<OLE_HANDLE*>(&gViewWindow));
359 if (FAILED(hr) || !gViewWindow)
362 if (usesLayeredWebView())
363 subclassForLayeredWindow();
367 ShowWindow(gViewWindow, nCmdShow);
368 UpdateWindow(gViewWindow);
370 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINLAUNCHER));
372 // Main message loop:
373 while (GetMessage(&msg, NULL, 0, 0)) {
374 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
375 TranslateMessage(&msg);
376 DispatchMessage(&msg);
381 gPrintDelegate->Release();
383 gWebViewPrivate->Release();
385 if (standardPreferences)
386 standardPreferences->Release();
387 tmpPreferences->Release();
390 #ifdef _CRTDBG_MAP_ALLOC
391 _CrtDumpMemoryLeaks();
397 return static_cast<int>(msg.wParam);
400 ATOM MyRegisterClass(HINSTANCE hInstance)
404 wcex.cbSize = sizeof(WNDCLASSEX);
406 wcex.style = CS_HREDRAW | CS_VREDRAW;
407 wcex.lpfnWndProc = WndProc;
410 wcex.hInstance = hInstance;
411 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINLAUNCHER));
412 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
413 wcex.hbrBackground = 0;
414 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WINLAUNCHER);
415 wcex.lpszClassName = szWindowClass;
416 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
418 return RegisterClassEx(&wcex);
421 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
423 hInst = hInstance; // Store instance handle in our global variable
425 if (usesLayeredWebView())
428 hMainWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
429 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, hInstance, 0);
434 ShowWindow(hMainWnd, nCmdShow);
435 UpdateWindow(hMainWnd);
440 static BOOL CALLBACK AbortProc(HDC hDC, int Error)
443 while (::PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
444 ::TranslateMessage(&msg);
445 ::DispatchMessage(&msg);
451 static HDC getPrinterDC()
454 memset(&pdlg, 0, sizeof(PRINTDLG));
455 pdlg.lStructSize = sizeof(PRINTDLG);
456 pdlg.Flags = PD_PRINTSETUP | PD_RETURNDC;
463 static void initDocStruct(DOCINFO* di, TCHAR* docname)
465 memset(di, 0, sizeof(DOCINFO));
466 di->cbSize = sizeof(DOCINFO);
467 di->lpszDocName = docname;
470 void PrintView(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
472 HDC printDC = getPrinterDC();
474 ::MessageBoxW(0, L"Error creating printing DC", L"Error", MB_APPLMODAL | MB_OK);
478 if (::SetAbortProc(printDC, AbortProc) == SP_ERROR) {
479 ::MessageBoxW(0, L"Error setting up AbortProc", L"Error", MB_APPLMODAL | MB_OK);
483 IWebFrame* frame = 0;
484 IWebFramePrivate* framePrivate = 0;
485 if (FAILED(gWebView->mainFrame(&frame)))
488 if (FAILED(frame->QueryInterface(&framePrivate)))
491 framePrivate->setInPrintingMode(TRUE, printDC);
494 framePrivate->getPrintedPageCount(printDC, &pageCount);
497 initDocStruct(&di, L"WebKit Doc");
498 ::StartDoc(printDC, &di);
500 // FIXME: Need CoreGraphics implementation
501 void* graphicsContext = 0;
502 for (size_t page = 1; page <= pageCount; ++page) {
503 ::StartPage(printDC);
504 framePrivate->spoolPages(printDC, page, page, graphicsContext);
508 framePrivate->setInPrintingMode(FALSE, printDC);
517 framePrivate->Release();
520 static const int dragBarHeight = 30;
522 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
524 WNDPROC parentProc = usesLayeredWebView() ? DefWebKitProc : DefWindowProc;
528 if (usesLayeredWebView()) {
530 ::GetWindowRect(hWnd, &window);
531 // For testing our transparent window, we need a region to use as a handle for
532 // dragging. The right way to do this would be to query the web view to see what's
533 // under the mouse. However, for testing purposes we just use an arbitrary
534 // 30 pixel band at the top of the view as an arbitrary gripping location.
536 // When we are within this bad, return HT_CAPTION to tell Windows we want to
537 // treat this region as if it were the title bar on a normal window.
538 int y = HIWORD(lParam);
540 if ((y > window.top) && (y < window.top + dragBarHeight))
543 return CallWindowProc(parentProc, hWnd, message, wParam, lParam);
547 int wmId = LOWORD(wParam);
548 int wmEvent = HIWORD(wParam);
549 // Parse the menu selections:
552 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
558 PrintView(hWnd, message, wParam, lParam);
561 return CallWindowProc(parentProc, hWnd, message, wParam, lParam);
569 if (!gWebView || usesLayeredWebView())
570 return CallWindowProc(parentProc, hWnd, message, wParam, lParam);
575 return CallWindowProc(parentProc, hWnd, message, wParam, lParam);
582 #define MAX_URL_LENGTH 1024
584 LRESULT CALLBACK MyEditProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
588 if (wParam == 13) { // Enter Key
589 wchar_t strPtr[MAX_URL_LENGTH];
590 *((LPWORD)strPtr) = MAX_URL_LENGTH;
591 int strLen = SendMessage(hDlg, EM_GETLINE, 0, (LPARAM)strPtr);
593 BSTR bstr = SysAllocStringLen(strPtr, strLen);
599 return (LRESULT)CallWindowProc((WNDPROC)DefEditProc,hDlg,message,wParam,lParam);
602 return (LRESULT)CallWindowProc((WNDPROC)DefEditProc,hDlg,message,wParam,lParam);
608 // Message handler for about box.
609 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
611 UNREFERENCED_PARAMETER(lParam);
614 return (INT_PTR)TRUE;
617 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
618 EndDialog(hDlg, LOWORD(wParam));
619 return (INT_PTR)TRUE;
623 return (INT_PTR)FALSE;
626 static void loadURL(BSTR urlBStr)
628 IWebFrame* frame = 0;
629 IWebMutableURLRequest* request = 0;
631 static BSTR methodBStr = SysAllocString(TEXT("GET"));
633 if (urlBStr && urlBStr[0] && (PathFileExists(urlBStr) || PathIsUNC(urlBStr))) {
634 TCHAR fileURL[INTERNET_MAX_URL_LENGTH];
635 DWORD fileURLLength = sizeof(fileURL)/sizeof(fileURL[0]);
637 if (SUCCEEDED(UrlCreateFromPath(urlBStr, fileURL, &fileURLLength, 0)))
638 SysReAllocString(&urlBStr, fileURL);
641 HRESULT hr = gWebView->mainFrame(&frame);
645 hr = WebKitCreateInstance(CLSID_WebMutableURLRequest, 0, IID_IWebMutableURLRequest, (void**)&request);
649 hr = request->initWithURL(urlBStr, WebURLRequestUseProtocolCachePolicy, 60);
653 hr = request->setHTTPMethod(methodBStr);
657 hr = frame->loadRequest(request);
661 SetFocus(gViewWindow);