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
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.
27 #include "WinLauncher.h"
35 #define MAX_LOADSTRING 100
36 #define URLBAR_HEIGHT 24
39 HINSTANCE hInst; // current instance
43 IWebView* gWebView = 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;
67 hr = webView->mainFrame(&mainFrame);
71 hr = mainFrame->dataSource(&dataSource);
72 if (FAILED(hr) || !dataSource)
73 hr = mainFrame->provisionalDataSource(&dataSource);
74 if (FAILED(hr) || !dataSource)
77 hr = dataSource->request(&request);
78 if (FAILED(hr) || !request)
81 hr = request->mainDocumentURL(&frameURL);
85 SendMessage(hURLBarWnd, (UINT)WM_SETTEXT, 0, (LPARAM)frameURL);
91 dataSource->Release();
94 SysFreeString(frameURL);
98 HRESULT STDMETHODCALLTYPE WinLauncherWebHost::QueryInterface(REFIID riid, void** ppvObject)
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);
106 return E_NOINTERFACE;
112 ULONG STDMETHODCALLTYPE WinLauncherWebHost::AddRef(void)
117 ULONG STDMETHODCALLTYPE WinLauncherWebHost::Release(void)
119 ULONG newRef = --m_refCount;
126 static void resizeSubViews()
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,
139 #ifdef _CRTDBG_MAP_ALLOC
140 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
141 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
144 UNREFERENCED_PARAMETER(hPrevInstance);
145 UNREFERENCED_PARAMETER(lpCmdLine);
147 // TODO: Place code here.
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))
169 hURLBarWnd = CreateWindow(L"EDIT", 0,
170 WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOVSCROLL,
176 DefEditProc = GetWindowLong(hURLBarWnd, GWL_WNDPROC);
177 SetWindowLong(hURLBarWnd, GWL_WNDPROC,(long)MyEditProc);
178 SetFocus(hURLBarWnd);
180 HRESULT hr = CoCreateInstance(CLSID_WebView, 0, CLSCTX_ALL, IID_IWebView, (void**)&gWebView);
184 gWebHost = new WinLauncherWebHost();
186 hr = gWebView->setFrameLoadDelegate(gWebHost);
190 hr = gWebView->setHostWindow((OLE_HANDLE) hMainWnd);
195 GetClientRect(hMainWnd, &clientRect);
196 hr = gWebView->initWithFrame(clientRect, 0, 0);
201 hr = gWebView->mainFrame(&frame);
204 static BSTR defaultHTML = 0;
206 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>"));
207 frame->loadHTMLString(defaultHTML, 0);
210 IWebViewPrivate* viewExt;
211 hr = gWebView->QueryInterface(IID_IWebViewPrivate, (void**)&viewExt);
214 hr = viewExt->viewWindow((OLE_HANDLE*) &gViewWindow);
216 if (FAILED(hr) || !gViewWindow)
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);
237 #ifdef _CRTDBG_MAP_ALLOC
238 _CrtDumpMemoryLeaks();
244 return (int) msg.wParam;
247 ATOM MyRegisterClass(HINSTANCE hInstance)
251 wcex.cbSize = sizeof(WNDCLASSEX);
253 wcex.style = CS_HREDRAW | CS_VREDRAW;
254 wcex.lpfnWndProc = WndProc;
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);
278 ShowWindow(hMainWnd, nCmdShow);
279 UpdateWindow(hMainWnd);
284 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
292 wmId = LOWORD(wParam);
293 wmEvent = HIWORD(wParam);
294 // Parse the menu selections:
297 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
303 return DefWindowProc(hWnd, message, wParam, lParam);
315 return DefWindowProc(hWnd, message, wParam, lParam);
321 #define MAX_URL_LENGTH 1024
323 LRESULT CALLBACK MyEditProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
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);
338 return (LRESULT)CallWindowProc((WNDPROC)DefEditProc,hDlg,message,wParam,lParam);
341 return (LRESULT)CallWindowProc((WNDPROC)DefEditProc,hDlg,message,wParam,lParam);
348 // Message handler for about box.
349 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
351 UNREFERENCED_PARAMETER(lParam);
354 return (INT_PTR)TRUE;
357 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
358 EndDialog(hDlg, LOWORD(wParam));
359 return (INT_PTR)TRUE;
363 return (INT_PTR)FALSE;
366 static void loadURL(BSTR urlBStr)
368 IWebFrame* frame = 0;
369 IWebMutableURLRequest* request = 0;
370 static BSTR methodBStr = 0;
373 methodBStr = SysAllocString(TEXT("GET"));
375 if (urlBStr && urlBStr[0] && (PathFileExists(urlBStr) || PathIsUNC(urlBStr))) {
376 TCHAR fileURL[INTERNET_MAX_URL_LENGTH];
377 DWORD fileURLLength = sizeof(fileURL)/sizeof(fileURL[0]);
378 if (SUCCEEDED(UrlCreateFromPath(urlBStr, fileURL, &fileURLLength, 0)))
382 HRESULT hr = gWebView->mainFrame(&frame);
386 hr = CoCreateInstance(CLSID_WebMutableURLRequest, 0, CLSCTX_ALL, IID_IWebMutableURLRequest, (void**)&request);
390 hr = request->initWithURL(urlBStr, WebURLRequestUseProtocolCachePolicy, 0);
394 hr = request->setHTTPMethod(methodBStr);
398 hr = frame->loadRequest(request);
402 SetFocus(gViewWindow);