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);
236 #ifdef _CRTDBG_MAP_ALLOC
237 _CrtDumpMemoryLeaks();
243 return (int) msg.wParam;
246 ATOM MyRegisterClass(HINSTANCE hInstance)
250 wcex.cbSize = sizeof(WNDCLASSEX);
252 wcex.style = CS_HREDRAW | CS_VREDRAW;
253 wcex.lpfnWndProc = WndProc;
256 wcex.hInstance = hInstance;
257 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINLAUNCHER));
258 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
259 wcex.hbrBackground = 0;
260 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WINLAUNCHER);
261 wcex.lpszClassName = szWindowClass;
262 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
264 return RegisterClassEx(&wcex);
267 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
269 hInst = hInstance; // Store instance handle in our global variable
271 hMainWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
272 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
277 ShowWindow(hMainWnd, nCmdShow);
278 UpdateWindow(hMainWnd);
283 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
291 wmId = LOWORD(wParam);
292 wmEvent = HIWORD(wParam);
293 // Parse the menu selections:
296 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
302 return DefWindowProc(hWnd, message, wParam, lParam);
314 return DefWindowProc(hWnd, message, wParam, lParam);
320 #define MAX_URL_LENGTH 1024
322 LRESULT CALLBACK MyEditProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
326 if( wParam == 13 ) { // Enter Key
327 wchar_t strPtr[MAX_URL_LENGTH];
328 *((LPWORD)strPtr) = MAX_URL_LENGTH;
329 int strLen = SendMessage(hDlg, EM_GETLINE, 0, (LPARAM)strPtr);
331 BSTR bstr = SysAllocStringLen(strPtr, strLen);
337 return (LRESULT)CallWindowProc((WNDPROC)DefEditProc,hDlg,message,wParam,lParam);
340 return (LRESULT)CallWindowProc((WNDPROC)DefEditProc,hDlg,message,wParam,lParam);
347 // Message handler for about box.
348 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
350 UNREFERENCED_PARAMETER(lParam);
353 return (INT_PTR)TRUE;
356 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
357 EndDialog(hDlg, LOWORD(wParam));
358 return (INT_PTR)TRUE;
362 return (INT_PTR)FALSE;
365 static void loadURL(BSTR urlBStr)
367 IWebFrame* frame = 0;
368 IWebMutableURLRequest* request = 0;
369 static BSTR methodBStr = 0;
372 methodBStr = SysAllocString(TEXT("GET"));
374 if (urlBStr && urlBStr[0] && (PathFileExists(urlBStr) || PathIsUNC(urlBStr))) {
375 TCHAR fileURL[INTERNET_MAX_URL_LENGTH];
376 DWORD fileURLLength = sizeof(fileURL)/sizeof(fileURL[0]);
377 if (SUCCEEDED(UrlCreateFromPath(urlBStr, fileURL, &fileURLLength, 0)))
381 HRESULT hr = gWebView->mainFrame(&frame);
385 hr = CoCreateInstance(CLSID_WebMutableURLRequest, 0, CLSCTX_ALL, IID_IWebMutableURLRequest, (void**)&request);
389 hr = request->initWithURL(urlBStr, WebURLRequestUseProtocolCachePolicy, 0);
393 hr = request->setHTTPMethod(methodBStr);
397 hr = frame->loadRequest(request);
401 SetFocus(gViewWindow);