shdocvw: Add Resizer control to the iexplore window.
[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 #include "winreg.h"
38 #include "shlwapi.h"
39 #include "intshcut.h"
40
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
44
45 #define IDI_APPICON 1
46
47 #define DOCHOST_THIS(iface) DEFINE_THIS2(InternetExplorer,doc_host,iface)
48
49 static const WCHAR szIEWinFrame[] = { 'I','E','F','r','a','m','e',0 };
50
51 /* Windows uses "Microsoft Internet Explorer" */
52 static const WCHAR wszWineInternetExplorer[] =
53         {'W','i','n','e',' ','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',0};
54
55 HRESULT update_ie_statustext(InternetExplorer* This, LPCWSTR text)
56 {
57     if(!SendMessageW(This->status_hwnd, SB_SETTEXTW, MAKEWORD(SB_SIMPLEID, 0), (LPARAM)text))
58         return E_FAIL;
59
60     return S_OK;
61 }
62
63 static void adjust_ie_docobj_rect(HWND frame, RECT* rc)
64 {
65     HWND hwndRebar = GetDlgItem(frame, IDC_BROWSE_REBAR);
66     HWND hwndStatus = GetDlgItem(frame, IDC_BROWSE_STATUSBAR);
67     INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);
68
69     rc->top += barHeight;
70     rc->bottom -= barHeight;
71
72     if(IsWindowVisible(hwndStatus))
73     {
74         RECT statusrc;
75
76         GetClientRect(hwndStatus, &statusrc);
77         rc->bottom -= statusrc.bottom - statusrc.top;
78     }
79 }
80
81 static HMENU get_tb_menu(HMENU menu)
82 {
83     HMENU menu_view = GetSubMenu(menu, 1);
84
85     return GetSubMenu(menu_view, 0);
86 }
87
88 static HMENU get_fav_menu(HMENU menu)
89 {
90     return GetSubMenu(menu, 2);
91 }
92
93 static LPWSTR get_fav_url_from_id(HMENU menu, UINT id)
94 {
95     MENUITEMINFOW item;
96
97     item.cbSize = sizeof(item);
98     item.fMask = MIIM_DATA;
99
100     if(!GetMenuItemInfoW(menu, id, FALSE, &item))
101         return NULL;
102
103     return (LPWSTR)item.dwItemData;
104 }
105
106 static void free_fav_menu_data(HMENU menu)
107 {
108     LPWSTR url;
109     int i;
110
111     for(i = 0; (url = get_fav_url_from_id(menu, ID_BROWSE_GOTOFAV_FIRST + i)); i++)
112         heap_free( url );
113 }
114
115 static int get_menu_item_count(HMENU menu)
116 {
117     MENUITEMINFOW item;
118     int count = 0;
119     int i;
120
121     item.cbSize = sizeof(item);
122     item.fMask = MIIM_DATA | MIIM_SUBMENU;
123
124     for(i = 0; GetMenuItemInfoW(menu, i, TRUE, &item); i++)
125     {
126         if(item.hSubMenu)
127             count += get_menu_item_count(item.hSubMenu);
128         else
129             count++;
130     }
131
132     return count;
133 }
134
135 static void add_fav_to_menu(HMENU favmenu, HMENU menu, LPWSTR title, LPCWSTR url)
136 {
137     MENUITEMINFOW item;
138     /* Subtract the number of standard elements in the Favorites menu */
139     int favcount = get_menu_item_count(favmenu) - 2;
140     LPWSTR urlbuf;
141
142     if(favcount > (ID_BROWSE_GOTOFAV_MAX - ID_BROWSE_GOTOFAV_FIRST))
143     {
144         FIXME("Add support for more than %d Favorites\n", favcount);
145         return;
146     }
147
148     urlbuf = heap_alloc((lstrlenW(url) + 1) * sizeof(WCHAR));
149
150     if(!urlbuf)
151         return;
152
153     lstrcpyW(urlbuf, url);
154
155     item.cbSize = sizeof(item);
156     item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA | MIIM_ID;
157     item.fType = MFT_STRING;
158     item.dwTypeData = title;
159     item.wID = ID_BROWSE_GOTOFAV_FIRST + favcount;
160     item.dwItemData = (ULONG_PTR)urlbuf;
161     InsertMenuItemW(menu, -1, TRUE, &item);
162 }
163
164 static void add_favs_to_menu(HMENU favmenu, HMENU menu, LPCWSTR dir)
165 {
166     WCHAR path[MAX_PATH*2];
167     const WCHAR search[] = {'*',0};
168     WCHAR* filename;
169     HANDLE findhandle;
170     WIN32_FIND_DATAW finddata;
171     IUniformResourceLocatorW* urlobj;
172     IPersistFile* urlfile;
173     HRESULT res;
174
175     lstrcpyW(path, dir);
176     PathAppendW(path, search);
177
178     findhandle = FindFirstFileW(path, &finddata);
179
180     if(findhandle == INVALID_HANDLE_VALUE)
181         return;
182
183     res = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, &IID_IUniformResourceLocatorW, (PVOID*)&urlobj);
184
185     if(SUCCEEDED(res))
186         res = IUnknown_QueryInterface(urlobj, &IID_IPersistFile, (PVOID*)&urlfile);
187
188     if(SUCCEEDED(res))
189     {
190         filename = path + lstrlenW(path) - lstrlenW(search);
191
192         do
193         {
194             lstrcpyW(filename, finddata.cFileName);
195
196             if(finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
197             {
198                 MENUITEMINFOW item;
199                 const WCHAR ignore1[] = {'.','.',0};
200                 const WCHAR ignore2[] = {'.',0};
201
202                 if(!lstrcmpW(filename, ignore1) || !lstrcmpW(filename, ignore2))
203                     continue;
204
205                 item.cbSize = sizeof(item);
206                 item.fMask = MIIM_STRING | MIIM_SUBMENU;
207                 item.dwTypeData = filename;
208                 item.hSubMenu = CreatePopupMenu();
209                 InsertMenuItemW(menu, -1, TRUE, &item);
210                 add_favs_to_menu(favmenu, item.hSubMenu, path);
211             } else
212             {
213                 WCHAR* fileext;
214                 WCHAR* url = NULL;
215                 const WCHAR urlext[] = {'.','u','r','l',0};
216
217                 if(lstrcmpiW(PathFindExtensionW(filename), urlext))
218                     continue;
219
220                 if(FAILED(IPersistFile_Load(urlfile, path, 0)))
221                     continue;
222
223                 urlobj->lpVtbl->GetURL(urlobj, &url);
224
225                 if(!url)
226                     continue;
227
228                 fileext = filename + lstrlenW(filename) - lstrlenW(urlext);
229                 *fileext = 0;
230                 add_fav_to_menu(favmenu, menu, filename, url);
231             }
232         } while(FindNextFileW(findhandle, &finddata));
233     }
234
235     if(urlfile)
236         IPersistFile_Release(urlfile);
237
238     if(urlobj)
239         IUnknown_Release(urlobj);
240
241     FindClose(findhandle);
242 }
243
244 static void add_tbs_to_menu(HMENU menu)
245 {
246     HUSKEY toolbar_handle;
247     WCHAR toolbar_key[] = {'S','o','f','t','w','a','r','e','\\',
248                            'M','i','c','r','o','s','o','f','t','\\',
249                            'I','n','t','e','r','n','e','t',' ',
250                            'E','x','p','l','o','r','e','r','\\',
251                            'T','o','o','l','b','a','r',0};
252
253     if(SHRegOpenUSKeyW(toolbar_key, KEY_READ, NULL, &toolbar_handle, TRUE) == ERROR_SUCCESS)
254     {
255         HUSKEY classes_handle;
256         WCHAR classes_key[] = {'S','o','f','t','w','a','r','e','\\',
257                                'C','l','a','s','s','e','s','\\','C','L','S','I','D',0};
258         WCHAR guid[39];
259         DWORD value_len = sizeof(guid)/sizeof(guid[0]);
260         int i;
261
262         if(SHRegOpenUSKeyW(classes_key, KEY_READ, NULL, &classes_handle, TRUE) != ERROR_SUCCESS)
263         {
264             SHRegCloseUSKey(toolbar_handle);
265             ERR("Failed to open key %s\n", debugstr_w(classes_key));
266             return;
267         }
268
269         for(i = 0; SHRegEnumUSValueW(toolbar_handle, i, guid, &value_len, NULL, NULL, NULL, SHREGENUM_HKLM) == ERROR_SUCCESS; i++)
270         {
271             WCHAR tb_name[100];
272             DWORD tb_name_len = sizeof(tb_name)/sizeof(tb_name[0]);
273             HUSKEY tb_class_handle;
274             MENUITEMINFOW item;
275             LSTATUS ret;
276             value_len = sizeof(guid)/sizeof(guid[0]);
277
278             if(lstrlenW(guid) != 38)
279             {
280                 TRACE("Found invalid IE toolbar entry: %s\n", debugstr_w(guid));
281                 continue;
282             }
283
284             if(SHRegOpenUSKeyW(guid, KEY_READ, classes_handle, &tb_class_handle, TRUE) != ERROR_SUCCESS)
285             {
286                 ERR("Failed to get class info for %s\n", debugstr_w(guid));
287                 continue;
288             }
289
290             ret = SHRegQueryUSValueW(tb_class_handle, NULL, NULL, tb_name, &tb_name_len, TRUE, NULL, 0);
291
292             SHRegCloseUSKey(tb_class_handle);
293
294             if(ret != ERROR_SUCCESS)
295             {
296                 ERR("Failed to get toolbar name for %s\n", debugstr_w(guid));
297                 continue;
298             }
299
300             item.cbSize = sizeof(item);
301             item.fMask = MIIM_STRING;
302             item.dwTypeData = tb_name;
303             InsertMenuItemW(menu, GetMenuItemCount(menu), TRUE, &item);
304         }
305
306         SHRegCloseUSKey(classes_handle);
307         SHRegCloseUSKey(toolbar_handle);
308     }
309 }
310
311 static HMENU create_ie_menu(void)
312 {
313     HMENU menu = LoadMenuW(shdocvw_hinstance, MAKEINTRESOURCEW(IDR_BROWSE_MAIN_MENU));
314     HMENU favmenu = get_fav_menu(menu);
315     WCHAR path[MAX_PATH];
316
317     add_tbs_to_menu(get_tb_menu(menu));
318
319     if(SHGetFolderPathW(NULL, CSIDL_COMMON_FAVORITES, NULL, SHGFP_TYPE_CURRENT, path) == S_OK)
320         add_favs_to_menu(favmenu, favmenu, path);
321
322     if(SHGetFolderPathW(NULL, CSIDL_FAVORITES, NULL, SHGFP_TYPE_CURRENT, path) == S_OK)
323         add_favs_to_menu(favmenu, favmenu, path);
324
325     return menu;
326 }
327
328 static void ie_navigate(InternetExplorer* This, LPCWSTR url)
329 {
330     VARIANT variant;
331
332     V_VT(&variant) = VT_BSTR;
333     V_BSTR(&variant) = SysAllocString(url);
334
335     IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &variant, NULL, NULL, NULL, NULL);
336
337     SysFreeString(V_BSTR(&variant));
338 }
339
340 static INT_PTR CALLBACK ie_dialog_open_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
341 {
342     static InternetExplorer* This;
343
344     switch(msg)
345     {
346         case WM_INITDIALOG:
347             This = (InternetExplorer*)lparam;
348             EnableWindow(GetDlgItem(hwnd, IDOK), FALSE);
349             return TRUE;
350
351         case WM_COMMAND:
352             switch(LOWORD(wparam))
353             {
354                 case IDC_BROWSE_OPEN_URL:
355                 {
356                     HWND hwndurl = GetDlgItem(hwnd, IDC_BROWSE_OPEN_URL);
357                     int len = GetWindowTextLengthW(hwndurl);
358
359                     EnableWindow(GetDlgItem(hwnd, IDOK), len ? TRUE : FALSE);
360                     break;
361                 }
362                 case IDOK:
363                 {
364                     HWND hwndurl = GetDlgItem(hwnd, IDC_BROWSE_OPEN_URL);
365                     int len = GetWindowTextLengthW(hwndurl);
366
367                     if(len)
368                     {
369                         VARIANT url;
370
371                         V_VT(&url) = VT_BSTR;
372                         V_BSTR(&url) = SysAllocStringLen(NULL, len);
373
374                         GetWindowTextW(hwndurl, V_BSTR(&url), len + 1);
375                         IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &url, NULL, NULL, NULL, NULL);
376
377                         SysFreeString(V_BSTR(&url));
378                     }
379                 }
380                 /* fall through */
381                 case IDCANCEL:
382                     EndDialog(hwnd, wparam);
383                     return TRUE;
384             }
385     }
386     return FALSE;
387 }
388
389 static void ie_dialog_about(HWND hwnd)
390 {
391     HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON, 48, 48, LR_SHARED);
392
393     ShellAboutW(hwnd, wszWineInternetExplorer, NULL, icon);
394
395     DestroyIcon(icon);
396 }
397
398 static void add_tb_separator(HWND hwnd)
399 {
400     TBBUTTON btn;
401
402     ZeroMemory(&btn, sizeof(btn));
403
404     btn.iBitmap = 3;
405     btn.fsStyle = BTNS_SEP;
406     SendMessageW(hwnd, TB_ADDBUTTONSW, 1, (LPARAM)&btn);
407 }
408
409 static void add_tb_button(HWND hwnd, int bmp, int cmd, int strId)
410 {
411     TBBUTTON btn;
412     WCHAR buf[30];
413
414     LoadStringW(shdocvw_hinstance, strId, buf, sizeof(buf)/sizeof(buf[0]));
415
416     btn.iBitmap = bmp;
417     btn.idCommand = cmd;
418     btn.fsState = TBSTATE_ENABLED;
419     btn.fsStyle = BTNS_SHOWTEXT;
420     btn.dwData = 0;
421     btn.iString = (INT_PTR)buf;
422
423     SendMessageW(hwnd, TB_ADDBUTTONSW, 1, (LPARAM)&btn);
424 }
425
426 static void create_rebar(HWND hwnd)
427 {
428     HWND hwndRebar;
429     HWND hwndAddress;
430     HWND hwndToolbar;
431     REBARINFO rebarinf;
432     REBARBANDINFOW bandinf;
433     WCHAR addr[40];
434     HIMAGELIST imagelist;
435     WCHAR idb_ietoolbar[] = {'I','D','B','_','I','E','T','O','O','L','B','A','R',0};
436
437     LoadStringW(shdocvw_hinstance, IDS_ADDRESS, addr, sizeof(addr)/sizeof(addr[0]));
438
439     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);
440
441     rebarinf.cbSize = sizeof(rebarinf);
442     rebarinf.fMask = 0;
443     rebarinf.himl = NULL;
444     rebarinf.cbSize = sizeof(rebarinf);
445
446     SendMessageW(hwndRebar, RB_SETBARINFO, 0, (LPARAM)&rebarinf);
447
448     hwndToolbar = CreateWindowExW(TBSTYLE_EX_MIXEDBUTTONS, TOOLBARCLASSNAMEW, NULL, TBSTYLE_FLAT | WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwndRebar, (HMENU)IDC_BROWSE_TOOLBAR, shdocvw_hinstance, NULL);
449
450     imagelist = ImageList_LoadImageW(shdocvw_hinstance, idb_ietoolbar, 32, 0, CLR_NONE, IMAGE_BITMAP, LR_CREATEDIBSECTION);
451
452     SendMessageW(hwndToolbar, TB_SETIMAGELIST, 0, (LPARAM)imagelist);
453     SendMessageW(hwndToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
454     add_tb_button(hwndToolbar, 0, 0, IDS_TB_BACK);
455     add_tb_button(hwndToolbar, 1, 0, IDS_TB_FORWARD);
456     add_tb_button(hwndToolbar, 2, 0, IDS_TB_STOP);
457     add_tb_button(hwndToolbar, 3, 0, IDS_TB_REFRESH);
458     add_tb_button(hwndToolbar, 4, ID_BROWSE_HOME, IDS_TB_HOME);
459     add_tb_separator(hwndToolbar);
460     add_tb_button(hwndToolbar, 5, ID_BROWSE_PRINT, IDS_TB_PRINT);
461     SendMessageW(hwndToolbar, TB_SETBUTTONSIZE, 0, MAKELPARAM(55,50));
462     SendMessageW(hwndToolbar, TB_AUTOSIZE, 0, 0);
463
464     bandinf.cbSize = sizeof(bandinf);
465     bandinf.fMask = RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE | RBBIM_SIZE;
466     bandinf.fStyle = RBBS_CHILDEDGE;
467     bandinf.cx = 100;
468     bandinf.cyMinChild = 52;
469     bandinf.hwndChild = hwndToolbar;
470
471     SendMessageW(hwndRebar, RB_INSERTBANDW, -1, (LPARAM)&bandinf);
472
473     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);
474
475     bandinf.fMask |= RBBIM_TEXT;
476     bandinf.fStyle = RBBS_CHILDEDGE | RBBS_BREAK;
477     bandinf.lpText = addr;
478     bandinf.cyMinChild = 20;
479     bandinf.hwndChild = hwndAddress;
480
481     SendMessageW(hwndRebar, RB_INSERTBANDW, -1, (LPARAM)&bandinf);
482 }
483
484 static LRESULT iewnd_OnCreate(HWND hwnd, LPCREATESTRUCTW lpcs)
485 {
486     InternetExplorer* This = (InternetExplorer*)lpcs->lpCreateParams;
487     SetWindowLongPtrW(hwnd, 0, (LONG_PTR) lpcs->lpCreateParams);
488
489     This->menu = create_ie_menu();
490
491     This->status_hwnd = CreateStatusWindowW(WS_VISIBLE|WS_CHILD|SBT_NOBORDERS|CCS_NODIVIDER,
492                                             NULL, hwnd, IDC_BROWSE_STATUSBAR);
493     SendMessageW(This->status_hwnd, SB_SIMPLE, TRUE, 0);
494
495     create_rebar(hwnd);
496
497     return 0;
498 }
499
500 static LRESULT iewnd_OnSize(InternetExplorer *This, INT width, INT height)
501 {
502     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
503     INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);
504     RECT docarea = {0, 0, width, height};
505
506     SendMessageW(This->status_hwnd, WM_SIZE, 0, 0);
507
508     adjust_ie_docobj_rect(This->frame_hwnd, &docarea);
509
510     if(This->doc_host.hwnd)
511         SetWindowPos(This->doc_host.hwnd, NULL, docarea.left, docarea.top, docarea.right, docarea.bottom,
512                      SWP_NOZORDER | SWP_NOACTIVATE);
513
514     SetWindowPos(hwndRebar, NULL, 0, 0, width, barHeight, SWP_NOZORDER | SWP_NOACTIVATE);
515
516     return 0;
517 }
518
519 static LRESULT iewnd_OnNotify(InternetExplorer *This, WPARAM wparam, LPARAM lparam)
520 {
521     NMHDR* hdr = (NMHDR*)lparam;
522
523     if(hdr->idFrom == IDC_BROWSE_ADDRESSBAR && hdr->code == CBEN_ENDEDITW)
524     {
525         NMCBEENDEDITW* info = (NMCBEENDEDITW*)lparam;
526
527         if(info->fChanged && info->iWhy == CBENF_RETURN && info->szText)
528         {
529             VARIANT vt;
530
531             V_VT(&vt) = VT_BSTR;
532             V_BSTR(&vt) = SysAllocString(info->szText);
533
534             IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &vt, NULL, NULL, NULL, NULL);
535
536             SysFreeString(V_BSTR(&vt));
537
538             return 0;
539         }
540     }
541
542     return 0;
543 }
544
545 static LRESULT iewnd_OnDestroy(InternetExplorer *This)
546 {
547     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
548     HWND hwndToolbar = GetDlgItem(hwndRebar, IDC_BROWSE_TOOLBAR);
549     HIMAGELIST list = (HIMAGELIST)SendMessageW(hwndToolbar, TB_GETIMAGELIST, 0, 0);
550
551     TRACE("%p\n", This);
552
553     free_fav_menu_data(get_fav_menu(This->menu));
554     ImageList_Destroy(list);
555     This->frame_hwnd = NULL;
556     PostQuitMessage(0); /* FIXME */
557
558     return 0;
559 }
560
561 static LRESULT iewnd_OnCommand(InternetExplorer *This, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
562 {
563     switch(LOWORD(wparam))
564     {
565         case ID_BROWSE_OPEN:
566             DialogBoxParamW(shdocvw_hinstance, MAKEINTRESOURCEW(IDD_BROWSE_OPEN), hwnd, ie_dialog_open_proc, (LPARAM)This);
567             break;
568
569         case ID_BROWSE_PRINT:
570             if(This->doc_host.document)
571             {
572                 IOleCommandTarget* target;
573
574                 if(FAILED(IUnknown_QueryInterface(This->doc_host.document, &IID_IOleCommandTarget, (LPVOID*)&target)))
575                     break;
576
577                 IOleCommandTarget_Exec(target, &CGID_MSHTML, IDM_PRINT, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
578
579                 IOleCommandTarget_Release(target);
580             }
581             break;
582
583         case ID_BROWSE_HOME:
584             IWebBrowser2_GoHome(&This->IWebBrowser2_iface);
585             break;
586
587         case ID_BROWSE_ABOUT:
588             ie_dialog_about(hwnd);
589             break;
590
591         case ID_BROWSE_QUIT:
592             iewnd_OnDestroy(This);
593             break;
594
595         default:
596             if(LOWORD(wparam) >= ID_BROWSE_GOTOFAV_FIRST && LOWORD(wparam) <= ID_BROWSE_GOTOFAV_MAX)
597             {
598                 LPCWSTR url = get_fav_url_from_id(get_fav_menu(This->menu), LOWORD(wparam));
599
600                 if(url)
601                     ie_navigate(This, url);
602             }
603             return DefWindowProcW(hwnd, msg, wparam, lparam);
604     }
605     return 0;
606 }
607
608 static LRESULT update_addrbar(InternetExplorer *This, LPARAM lparam)
609 {
610     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
611     HWND hwndAddress = GetDlgItem(hwndRebar, IDC_BROWSE_ADDRESSBAR);
612     HWND hwndEdit = (HWND)SendMessageW(hwndAddress, CBEM_GETEDITCONTROL, 0, 0);
613     LPCWSTR url = (LPCWSTR)lparam;
614
615     SendMessageW(hwndEdit, WM_SETTEXT, 0, (LPARAM)url);
616
617     return 0;
618 }
619
620 static LRESULT CALLBACK
621 ie_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
622 {
623     InternetExplorer *This = (InternetExplorer*) GetWindowLongPtrW(hwnd, 0);
624
625     switch (msg)
626     {
627     case WM_CREATE:
628         return iewnd_OnCreate(hwnd, (LPCREATESTRUCTW)lparam);
629     case WM_DESTROY:
630         return iewnd_OnDestroy(This);
631     case WM_SIZE:
632         return iewnd_OnSize(This, LOWORD(lparam), HIWORD(lparam));
633     case WM_COMMAND:
634         return iewnd_OnCommand(This, hwnd, msg, wparam, lparam);
635     case WM_NOTIFY:
636         return iewnd_OnNotify(This, wparam, lparam);
637     case WM_DOCHOSTTASK:
638         return process_dochost_task(&This->doc_host, lparam);
639     case WM_UPDATEADDRBAR:
640         return update_addrbar(This, lparam);
641     }
642     return DefWindowProcW(hwnd, msg, wparam, lparam);
643 }
644
645 void register_iewindow_class(void)
646 {
647     WNDCLASSEXW wc;
648
649     memset(&wc, 0, sizeof wc);
650     wc.cbSize = sizeof(wc);
651     wc.style = 0;
652     wc.lpfnWndProc = ie_window_proc;
653     wc.cbClsExtra = 0;
654     wc.cbWndExtra = sizeof(InternetExplorer*);
655     wc.hInstance = shdocvw_hinstance;
656     wc.hIcon = LoadIconW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON));
657     wc.hIconSm = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON,
658                             GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
659     wc.hCursor = LoadCursorW(0, MAKEINTRESOURCEW(IDC_ARROW));
660     wc.hbrBackground = 0;
661     wc.lpszClassName = szIEWinFrame;
662     wc.lpszMenuName = NULL;
663
664     RegisterClassExW(&wc);
665 }
666
667 void unregister_iewindow_class(void)
668 {
669     UnregisterClassW(szIEWinFrame, shdocvw_hinstance);
670 }
671
672 static void create_frame_hwnd(InternetExplorer *This)
673 {
674     This->frame_hwnd = CreateWindowExW(
675             WS_EX_WINDOWEDGE,
676             szIEWinFrame, wszWineInternetExplorer,
677             WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
678                 | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
679             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
680             NULL, NULL /* FIXME */, shdocvw_hinstance, This);
681 }
682
683 static IWebBrowser2 *create_ie_window(LPCSTR cmdline)
684 {
685     IWebBrowser2 *wb = NULL;
686
687     InternetExplorer_Create(NULL, &IID_IWebBrowser2, (void**)&wb);
688     if(!wb)
689         return NULL;
690
691     IWebBrowser2_put_Visible(wb, VARIANT_TRUE);
692     IWebBrowser2_put_MenuBar(wb, VARIANT_TRUE);
693
694     if(!*cmdline) {
695         IWebBrowser2_GoHome(wb);
696     }else {
697         VARIANT var_url;
698         DWORD len;
699         int cmdlen;
700
701         if(!strncasecmp(cmdline, "-nohome", 7))
702             cmdline += 7;
703         while(*cmdline == ' ' || *cmdline == '\t')
704             cmdline++;
705         cmdlen = lstrlenA(cmdline);
706         if(cmdlen > 2 && cmdline[0] == '"' && cmdline[cmdlen-1] == '"') {
707             cmdline++;
708             cmdlen -= 2;
709         }
710
711         V_VT(&var_url) = VT_BSTR;
712
713         len = MultiByteToWideChar(CP_ACP, 0, cmdline, cmdlen, NULL, 0);
714         V_BSTR(&var_url) = SysAllocStringLen(NULL, len);
715         MultiByteToWideChar(CP_ACP, 0, cmdline, cmdlen, V_BSTR(&var_url), len);
716
717         /* navigate to the first page */
718         IWebBrowser2_Navigate2(wb, &var_url, NULL, NULL, NULL, NULL);
719
720         SysFreeString(V_BSTR(&var_url));
721     }
722
723     return wb;
724 }
725
726 static inline InternetExplorer *impl_from_DocHost(DocHost *iface)
727 {
728     return CONTAINING_RECORD(iface, InternetExplorer, doc_host);
729 }
730
731 static void WINAPI DocHostContainer_GetDocObjRect(DocHost* This, RECT* rc)
732 {
733     GetClientRect(This->frame_hwnd, rc);
734     adjust_ie_docobj_rect(This->frame_hwnd, rc);
735 }
736
737 static HRESULT WINAPI DocHostContainer_SetStatusText(DocHost* This, LPCWSTR text)
738 {
739     InternetExplorer* ie = impl_from_DocHost(This);
740     return update_ie_statustext(ie, text);
741 }
742
743 static void WINAPI DocHostContainer_SetURL(DocHost* This, LPCWSTR url)
744 {
745     SendMessageW(This->frame_hwnd, WM_UPDATEADDRBAR, 0, (LPARAM)url);
746 }
747
748 static HRESULT DocHostContainer_exec(DocHost* This, const GUID *cmd_group, DWORD cmdid, DWORD execopt, VARIANT *in,
749         VARIANT *out)
750 {
751     return S_OK;
752 }
753 static const IDocHostContainerVtbl DocHostContainerVtbl = {
754     DocHostContainer_GetDocObjRect,
755     DocHostContainer_SetStatusText,
756     DocHostContainer_SetURL,
757     DocHostContainer_exec
758 };
759
760 HRESULT InternetExplorer_Create(IUnknown *pOuter, REFIID riid, void **ppv)
761 {
762     InternetExplorer *ret;
763     HRESULT hres;
764
765     TRACE("(%p %s %p)\n", pOuter, debugstr_guid(riid), ppv);
766
767     ret = heap_alloc_zero(sizeof(InternetExplorer));
768     ret->ref = 0;
769
770     ret->doc_host.disp = (IDispatch*)&ret->IWebBrowser2_iface;
771     DocHost_Init(&ret->doc_host, (IDispatch*)&ret->IWebBrowser2_iface, &DocHostContainerVtbl);
772
773     InternetExplorer_WebBrowser_Init(ret);
774
775     HlinkFrame_Init(&ret->hlink_frame, (IUnknown*)&ret->IWebBrowser2_iface, &ret->doc_host);
776
777     create_frame_hwnd(ret);
778     ret->doc_host.frame_hwnd = ret->frame_hwnd;
779
780     hres = IWebBrowser2_QueryInterface(&ret->IWebBrowser2_iface, riid, ppv);
781     if(FAILED(hres)) {
782         heap_free(ret);
783         return hres;
784     }
785
786     return hres;
787 }
788
789 /******************************************************************
790  *              IEWinMain            (SHDOCVW.101)
791  *
792  * Only returns on error.
793  */
794 DWORD WINAPI IEWinMain(LPSTR szCommandLine, int nShowWindow)
795 {
796     IWebBrowser2 *wb = NULL;
797     MSG msg;
798     HRESULT hres;
799
800     TRACE("%s %d\n", debugstr_a(szCommandLine), nShowWindow);
801
802     if(*szCommandLine == '-' || *szCommandLine == '/') {
803         if(!strcasecmp(szCommandLine+1, "regserver"))
804             return register_iexplore(TRUE);
805         if(!strcasecmp(szCommandLine+1, "unregserver"))
806             return register_iexplore(FALSE);
807     }
808
809     CoInitialize(NULL);
810
811     hres = register_class_object(TRUE);
812     if(FAILED(hres)) {
813         CoUninitialize();
814         ExitProcess(1);
815     }
816
817     if(strcasecmp(szCommandLine, "-embedding"))
818         wb = create_ie_window(szCommandLine);
819
820     /* run the message loop for this thread */
821     while (GetMessageW(&msg, 0, 0, 0))
822     {
823         TranslateMessage(&msg);
824         DispatchMessageW(&msg);
825     }
826
827     if(wb)
828         IWebBrowser2_Release(wb);
829
830     register_class_object(FALSE);
831
832     CoUninitialize();
833
834     ExitProcess(0);
835     return 0;
836 }