shdocvw: Make the addressbar in IE do something useful.
[wine] / dlls / shdocvw / iexplore.c
1 /*
2  * SHDOCVW - Internet Explorer main frame window
3  *
4  * Copyright 2006 Mike McCormack (for CodeWeavers)
5  * Copyright 2006 Jacek Caban (for CodeWeavers)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #define COBJMACROS
23
24 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "wingdi.h"
29 #include "winnls.h"
30 #include "ole2.h"
31 #include "exdisp.h"
32 #include "oleidl.h"
33
34 #include "shdocvw.h"
35 #include "mshtmcid.h"
36 #include "shellapi.h"
37
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
41
42 #define IDI_APPICON 1
43
44 static const WCHAR szIEWinFrame[] = { 'I','E','F','r','a','m','e',0 };
45
46 /* Windows uses "Microsoft Internet Explorer" */
47 static const WCHAR wszWineInternetExplorer[] =
48         {'W','i','n','e',' ','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',0};
49
50 void adjust_ie_docobj_rect(HWND frame, RECT* rc)
51 {
52     HWND hwndRebar = GetDlgItem(frame, IDC_BROWSE_REBAR);
53     INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);
54
55     rc->top += barHeight;
56     rc->bottom -= barHeight;
57 }
58
59 static INT_PTR CALLBACK ie_dialog_open_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
60 {
61     static InternetExplorer* This;
62
63     switch(msg)
64     {
65         case WM_INITDIALOG:
66             This = (InternetExplorer*)lparam;
67             EnableWindow(GetDlgItem(hwnd, IDOK), FALSE);
68             return TRUE;
69
70         case WM_COMMAND:
71             switch(LOWORD(wparam))
72             {
73                 case IDC_BROWSE_OPEN_URL:
74                 {
75                     HWND hwndurl = GetDlgItem(hwnd, IDC_BROWSE_OPEN_URL);
76                     int len = GetWindowTextLengthW(hwndurl);
77
78                     EnableWindow(GetDlgItem(hwnd, IDOK), len ? TRUE : FALSE);
79                     break;
80                 }
81                 case IDOK:
82                 {
83                     HWND hwndurl = GetDlgItem(hwnd, IDC_BROWSE_OPEN_URL);
84                     int len = GetWindowTextLengthW(hwndurl);
85
86                     if(len)
87                     {
88                         VARIANT url;
89
90                         V_VT(&url) = VT_BSTR;
91                         V_BSTR(&url) = SysAllocStringLen(NULL, len);
92
93                         GetWindowTextW(hwndurl, V_BSTR(&url), len);
94                         IWebBrowser2_Navigate2(WEBBROWSER2(This), &url, NULL, NULL, NULL, NULL);
95
96                         SysFreeString(V_BSTR(&url));
97                     }
98                 }
99                 /* fall through */
100                 case IDCANCEL:
101                     EndDialog(hwnd, wparam);
102                     return TRUE;
103             }
104     }
105     return FALSE;
106 }
107
108 static void ie_dialog_about(HWND hwnd)
109 {
110     HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON, 48, 48, LR_SHARED);
111
112     ShellAboutW(hwnd, wszWineInternetExplorer, NULL, icon);
113
114     DestroyIcon(icon);
115 }
116
117 static void create_rebar(HWND hwnd)
118 {
119     HWND hwndRebar;
120     HWND hwndAddress;
121     REBARINFO rebarinf;
122     REBARBANDINFOW bandinf;
123     WCHAR addr[] = {'A','d','d','r','e','s','s',0};
124
125     hwndRebar = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL, WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP|CCS_NODIVIDER, 0, 0, 0, 0, hwnd, (HMENU)IDC_BROWSE_REBAR, shdocvw_hinstance, NULL);
126
127     rebarinf.cbSize = sizeof(rebarinf);
128     rebarinf.fMask = 0;
129     rebarinf.himl = NULL;
130     rebarinf.cbSize = sizeof(rebarinf);
131
132     SendMessageW(hwndRebar, RB_SETBARINFO, 0, (LPARAM)&rebarinf);
133
134     hwndAddress = CreateWindowExW(0, WC_COMBOBOXEXW, NULL, WS_BORDER|WS_CHILD|WS_VISIBLE|CBS_DROPDOWN, 0, 0, 100,20,hwndRebar, (HMENU)IDC_BROWSE_ADDRESSBAR, shdocvw_hinstance, NULL);
135
136     bandinf.fMask = RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE | RBBIM_SIZE | RBBIM_TEXT;
137     bandinf.fStyle = RBBS_CHILDEDGE | RBBS_GRIPPERALWAYS;
138     bandinf.lpText = addr;
139     bandinf.cx = 100;
140     bandinf.cyMinChild = 20;
141     bandinf.hwndChild = hwndAddress;
142
143     SendMessageW(hwndRebar, RB_INSERTBANDW, 0, (LPARAM)&bandinf);
144 }
145
146 static LRESULT iewnd_OnCreate(HWND hwnd, LPCREATESTRUCTW lpcs)
147 {
148     SetWindowLongPtrW(hwnd, 0, (LONG_PTR) lpcs->lpCreateParams);
149     create_rebar(hwnd);
150
151     return 0;
152 }
153
154 static LRESULT iewnd_OnSize(InternetExplorer *This, INT width, INT height)
155 {
156     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
157     INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);
158     RECT docarea = {0, 0, width, height};
159
160     adjust_ie_docobj_rect(This->frame_hwnd, &docarea);
161
162     if(This->doc_host.hwnd)
163         SetWindowPos(This->doc_host.hwnd, NULL, docarea.left, docarea.top, docarea.right, docarea.bottom,
164                      SWP_NOZORDER | SWP_NOACTIVATE);
165
166     SetWindowPos(hwndRebar, NULL, 0, 0, width, barHeight, SWP_NOZORDER | SWP_NOACTIVATE);
167
168     return 0;
169 }
170
171 static LRESULT iewnd_OnNotify(InternetExplorer *This, WPARAM wparam, LPARAM lparam)
172 {
173     NMHDR* hdr = (NMHDR*)lparam;
174
175     if(hdr->idFrom == IDC_BROWSE_ADDRESSBAR && hdr->code == CBEN_ENDEDITW)
176     {
177         NMCBEENDEDITW* info = (NMCBEENDEDITW*)lparam;
178
179         if(info->fChanged && info->iWhy == CBENF_RETURN && info->szText)
180         {
181             VARIANT vt;
182             HWND hwndEdit = (HWND)SendMessageW(hdr->hwndFrom, CBEM_GETEDITCONTROL, 0, 0);
183
184             V_VT(&vt) = VT_BSTR;
185             V_BSTR(&vt) = SysAllocString(info->szText);
186
187             IWebBrowser2_Navigate2(WEBBROWSER2(This), &vt, NULL, NULL, NULL, NULL);
188
189             SysFreeString(V_BSTR(&vt));
190
191             /* Clear the address bar, as we don't change it when
192                the user navigates to a new page */
193             SendMessageW(hwndEdit, WM_SETTEXT, 0, 0);
194             return 0;
195         }
196     }
197
198     return 0;
199 }
200
201 static LRESULT iewnd_OnDestroy(InternetExplorer *This)
202 {
203     TRACE("%p\n", This);
204
205     This->frame_hwnd = NULL;
206     PostQuitMessage(0); /* FIXME */
207
208     return 0;
209 }
210
211 static LRESULT CALLBACK iewnd_OnCommand(InternetExplorer *This, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
212 {
213     switch(LOWORD(wparam))
214     {
215         case ID_BROWSE_OPEN:
216             DialogBoxParamW(shdocvw_hinstance, MAKEINTRESOURCEW(IDD_BROWSE_OPEN), hwnd, ie_dialog_open_proc, (LPARAM)This);
217             break;
218
219         case ID_BROWSE_PRINT:
220             if(This->doc_host.document)
221             {
222                 IOleCommandTarget* target;
223
224                 if(FAILED(IUnknown_QueryInterface(This->doc_host.document, &IID_IOleCommandTarget, (LPVOID*)&target)))
225                     break;
226
227                 IOleCommandTarget_Exec(target, &CGID_MSHTML, IDM_PRINT, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
228
229                 IOleCommandTarget_Release(target);
230             }
231             break;
232
233         case ID_BROWSE_ABOUT:
234             ie_dialog_about(hwnd);
235             break;
236
237         default:
238             return DefWindowProcW(hwnd, msg, wparam, lparam);
239     }
240     return 0;
241 }
242
243 static LRESULT CALLBACK
244 ie_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
245 {
246     InternetExplorer *This = (InternetExplorer*) GetWindowLongPtrW(hwnd, 0);
247
248     switch (msg)
249     {
250     case WM_CREATE:
251         return iewnd_OnCreate(hwnd, (LPCREATESTRUCTW)lparam);
252     case WM_DESTROY:
253         return iewnd_OnDestroy(This);
254     case WM_SIZE:
255         return iewnd_OnSize(This, LOWORD(lparam), HIWORD(lparam));
256     case WM_COMMAND:
257         return iewnd_OnCommand(This, hwnd, msg, wparam, lparam);
258     case WM_NOTIFY:
259         return iewnd_OnNotify(This, wparam, lparam);
260     case WM_DOCHOSTTASK:
261         return process_dochost_task(&This->doc_host, lparam);
262     }
263     return DefWindowProcW(hwnd, msg, wparam, lparam);
264 }
265
266 void register_iewindow_class(void)
267 {
268     WNDCLASSEXW wc;
269
270     memset(&wc, 0, sizeof wc);
271     wc.cbSize = sizeof(wc);
272     wc.style = 0;
273     wc.lpfnWndProc = ie_window_proc;
274     wc.cbClsExtra = 0;
275     wc.cbWndExtra = sizeof(InternetExplorer*);
276     wc.hInstance = shdocvw_hinstance;
277     wc.hIcon = LoadIconW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON));
278     wc.hIconSm = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON,
279                             GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
280     wc.hCursor = LoadCursorW(0, MAKEINTRESOURCEW(IDC_ARROW));
281     wc.hbrBackground = 0;
282     wc.lpszClassName = szIEWinFrame;
283     wc.lpszMenuName = NULL;
284
285     RegisterClassExW(&wc);
286 }
287
288 void unregister_iewindow_class(void)
289 {
290     UnregisterClassW(szIEWinFrame, shdocvw_hinstance);
291 }
292
293 static void create_frame_hwnd(InternetExplorer *This)
294 {
295     This->frame_hwnd = CreateWindowExW(
296             WS_EX_WINDOWEDGE,
297             szIEWinFrame, wszWineInternetExplorer,
298             WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
299                 | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
300             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
301             NULL, NULL /* FIXME */, shdocvw_hinstance, This);
302 }
303
304 static IWebBrowser2 *create_ie_window(LPCSTR cmdline)
305 {
306     IWebBrowser2 *wb = NULL;
307
308     InternetExplorer_Create(NULL, &IID_IWebBrowser2, (void**)&wb);
309     if(!wb)
310         return NULL;
311
312     IWebBrowser2_put_Visible(wb, VARIANT_TRUE);
313     IWebBrowser2_put_MenuBar(wb, VARIANT_TRUE);
314
315     if(!*cmdline) {
316         IWebBrowser2_GoHome(wb);
317     }else {
318         VARIANT var_url;
319         DWORD len;
320         int cmdlen;
321
322         if(!strncasecmp(cmdline, "-nohome", 7))
323             cmdline += 7;
324         while(*cmdline == ' ' || *cmdline == '\t')
325             cmdline++;
326         cmdlen = lstrlenA(cmdline);
327         if(cmdlen > 2 && cmdline[0] == '"' && cmdline[cmdlen-1] == '"') {
328             cmdline++;
329             cmdlen -= 2;
330         }
331
332         V_VT(&var_url) = VT_BSTR;
333
334         len = MultiByteToWideChar(CP_ACP, 0, cmdline, cmdlen, NULL, 0);
335         V_BSTR(&var_url) = SysAllocStringLen(NULL, len);
336         MultiByteToWideChar(CP_ACP, 0, cmdline, cmdlen, V_BSTR(&var_url), len);
337
338         /* navigate to the first page */
339         IWebBrowser2_Navigate2(wb, &var_url, NULL, NULL, NULL, NULL);
340
341         SysFreeString(V_BSTR(&var_url));
342     }
343
344     return wb;
345 }
346
347 HRESULT InternetExplorer_Create(IUnknown *pOuter, REFIID riid, void **ppv)
348 {
349     InternetExplorer *ret;
350     HRESULT hres;
351
352     TRACE("(%p %s %p)\n", pOuter, debugstr_guid(riid), ppv);
353
354     ret = heap_alloc_zero(sizeof(InternetExplorer));
355     ret->ref = 0;
356
357     ret->doc_host.disp = (IDispatch*)WEBBROWSER2(ret);
358     DocHost_Init(&ret->doc_host, (IDispatch*)WEBBROWSER2(ret));
359
360     InternetExplorer_WebBrowser_Init(ret);
361
362     HlinkFrame_Init(&ret->hlink_frame, (IUnknown*)WEBBROWSER2(ret), &ret->doc_host);
363
364     create_frame_hwnd(ret);
365     ret->doc_host.frame_hwnd = ret->frame_hwnd;
366
367     hres = IWebBrowser2_QueryInterface(WEBBROWSER2(ret), riid, ppv);
368     if(FAILED(hres)) {
369         heap_free(ret);
370         return hres;
371     }
372
373     return hres;
374 }
375
376 /******************************************************************
377  *              IEWinMain            (SHDOCVW.101)
378  *
379  * Only returns on error.
380  */
381 DWORD WINAPI IEWinMain(LPSTR szCommandLine, int nShowWindow)
382 {
383     IWebBrowser2 *wb = NULL;
384     MSG msg;
385     HRESULT hres;
386
387     TRACE("%s %d\n", debugstr_a(szCommandLine), nShowWindow);
388
389     if(*szCommandLine == '-' || *szCommandLine == '/') {
390         if(!strcasecmp(szCommandLine+1, "regserver"))
391             return register_iexplore(TRUE);
392         if(!strcasecmp(szCommandLine+1, "unregserver"))
393             return register_iexplore(FALSE);
394     }
395
396     CoInitialize(NULL);
397
398     hres = register_class_object(TRUE);
399     if(FAILED(hres)) {
400         CoUninitialize();
401         ExitProcess(1);
402     }
403
404     if(strcasecmp(szCommandLine, "-embedding"))
405         wb = create_ie_window(szCommandLine);
406
407     /* run the message loop for this thread */
408     while (GetMessageW(&msg, 0, 0, 0))
409     {
410         TranslateMessage(&msg);
411         DispatchMessageW(&msg);
412     }
413
414     if(wb)
415         IWebBrowser2_Release(wb);
416
417     register_class_object(FALSE);
418
419     CoUninitialize();
420
421     ExitProcess(0);
422     return 0;
423 }