shdocvw: Make adjust_ie_docobj_rect() static.
[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(WEBBROWSER2(This), &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(WEBBROWSER2(This), &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, RGB(255,0,255), IMAGE_BITMAP, LR_DEFAULTCOLOR);
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(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, NULL, hwnd, IDC_BROWSE_STATUSBAR);
492     SendMessageW(This->status_hwnd, SB_SIMPLE, TRUE, 0);
493
494     create_rebar(hwnd);
495
496     return 0;
497 }
498
499 static LRESULT iewnd_OnSize(InternetExplorer *This, INT width, INT height)
500 {
501     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
502     INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);
503     RECT docarea = {0, 0, width, height};
504
505     SendMessageW(This->status_hwnd, WM_SIZE, 0, 0);
506
507     adjust_ie_docobj_rect(This->frame_hwnd, &docarea);
508
509     if(This->doc_host.hwnd)
510         SetWindowPos(This->doc_host.hwnd, NULL, docarea.left, docarea.top, docarea.right, docarea.bottom,
511                      SWP_NOZORDER | SWP_NOACTIVATE);
512
513     SetWindowPos(hwndRebar, NULL, 0, 0, width, barHeight, SWP_NOZORDER | SWP_NOACTIVATE);
514
515     return 0;
516 }
517
518 static LRESULT iewnd_OnNotify(InternetExplorer *This, WPARAM wparam, LPARAM lparam)
519 {
520     NMHDR* hdr = (NMHDR*)lparam;
521
522     if(hdr->idFrom == IDC_BROWSE_ADDRESSBAR && hdr->code == CBEN_ENDEDITW)
523     {
524         NMCBEENDEDITW* info = (NMCBEENDEDITW*)lparam;
525
526         if(info->fChanged && info->iWhy == CBENF_RETURN && info->szText)
527         {
528             VARIANT vt;
529
530             V_VT(&vt) = VT_BSTR;
531             V_BSTR(&vt) = SysAllocString(info->szText);
532
533             IWebBrowser2_Navigate2(WEBBROWSER2(This), &vt, NULL, NULL, NULL, NULL);
534
535             SysFreeString(V_BSTR(&vt));
536
537             return 0;
538         }
539     }
540
541     return 0;
542 }
543
544 static LRESULT iewnd_OnDestroy(InternetExplorer *This)
545 {
546     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
547     HWND hwndToolbar = GetDlgItem(hwndRebar, IDC_BROWSE_TOOLBAR);
548     HIMAGELIST list = (HIMAGELIST)SendMessageW(hwndToolbar, TB_GETIMAGELIST, 0, 0);
549
550     TRACE("%p\n", This);
551
552     free_fav_menu_data(get_fav_menu(This->menu));
553     ImageList_Destroy(list);
554     This->frame_hwnd = NULL;
555     PostQuitMessage(0); /* FIXME */
556
557     return 0;
558 }
559
560 static LRESULT iewnd_OnCommand(InternetExplorer *This, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
561 {
562     switch(LOWORD(wparam))
563     {
564         case ID_BROWSE_OPEN:
565             DialogBoxParamW(shdocvw_hinstance, MAKEINTRESOURCEW(IDD_BROWSE_OPEN), hwnd, ie_dialog_open_proc, (LPARAM)This);
566             break;
567
568         case ID_BROWSE_PRINT:
569             if(This->doc_host.document)
570             {
571                 IOleCommandTarget* target;
572
573                 if(FAILED(IUnknown_QueryInterface(This->doc_host.document, &IID_IOleCommandTarget, (LPVOID*)&target)))
574                     break;
575
576                 IOleCommandTarget_Exec(target, &CGID_MSHTML, IDM_PRINT, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
577
578                 IOleCommandTarget_Release(target);
579             }
580             break;
581
582         case ID_BROWSE_HOME:
583             IWebBrowser2_GoHome(WEBBROWSER2(This));
584             break;
585
586         case ID_BROWSE_ABOUT:
587             ie_dialog_about(hwnd);
588             break;
589
590         default:
591             if(LOWORD(wparam) >= ID_BROWSE_GOTOFAV_FIRST && LOWORD(wparam) <= ID_BROWSE_GOTOFAV_MAX)
592             {
593                 LPCWSTR url = get_fav_url_from_id(get_fav_menu(This->menu), LOWORD(wparam));
594
595                 if(url)
596                     ie_navigate(This, url);
597             }
598             return DefWindowProcW(hwnd, msg, wparam, lparam);
599     }
600     return 0;
601 }
602
603 static LRESULT update_addrbar(InternetExplorer *This, LPARAM lparam)
604 {
605     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
606     HWND hwndAddress = GetDlgItem(hwndRebar, IDC_BROWSE_ADDRESSBAR);
607     HWND hwndEdit = (HWND)SendMessageW(hwndAddress, CBEM_GETEDITCONTROL, 0, 0);
608     LPCWSTR url = (LPCWSTR)lparam;
609
610     SendMessageW(hwndEdit, WM_SETTEXT, 0, (LPARAM)url);
611
612     return 0;
613 }
614
615 static LRESULT CALLBACK
616 ie_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
617 {
618     InternetExplorer *This = (InternetExplorer*) GetWindowLongPtrW(hwnd, 0);
619
620     switch (msg)
621     {
622     case WM_CREATE:
623         return iewnd_OnCreate(hwnd, (LPCREATESTRUCTW)lparam);
624     case WM_DESTROY:
625         return iewnd_OnDestroy(This);
626     case WM_SIZE:
627         return iewnd_OnSize(This, LOWORD(lparam), HIWORD(lparam));
628     case WM_COMMAND:
629         return iewnd_OnCommand(This, hwnd, msg, wparam, lparam);
630     case WM_NOTIFY:
631         return iewnd_OnNotify(This, wparam, lparam);
632     case WM_DOCHOSTTASK:
633         return process_dochost_task(&This->doc_host, lparam);
634     case WM_UPDATEADDRBAR:
635         return update_addrbar(This, lparam);
636     }
637     return DefWindowProcW(hwnd, msg, wparam, lparam);
638 }
639
640 void register_iewindow_class(void)
641 {
642     WNDCLASSEXW wc;
643
644     memset(&wc, 0, sizeof wc);
645     wc.cbSize = sizeof(wc);
646     wc.style = 0;
647     wc.lpfnWndProc = ie_window_proc;
648     wc.cbClsExtra = 0;
649     wc.cbWndExtra = sizeof(InternetExplorer*);
650     wc.hInstance = shdocvw_hinstance;
651     wc.hIcon = LoadIconW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON));
652     wc.hIconSm = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON,
653                             GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
654     wc.hCursor = LoadCursorW(0, MAKEINTRESOURCEW(IDC_ARROW));
655     wc.hbrBackground = 0;
656     wc.lpszClassName = szIEWinFrame;
657     wc.lpszMenuName = NULL;
658
659     RegisterClassExW(&wc);
660 }
661
662 void unregister_iewindow_class(void)
663 {
664     UnregisterClassW(szIEWinFrame, shdocvw_hinstance);
665 }
666
667 static void create_frame_hwnd(InternetExplorer *This)
668 {
669     This->frame_hwnd = CreateWindowExW(
670             WS_EX_WINDOWEDGE,
671             szIEWinFrame, wszWineInternetExplorer,
672             WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
673                 | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
674             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
675             NULL, NULL /* FIXME */, shdocvw_hinstance, This);
676 }
677
678 static IWebBrowser2 *create_ie_window(LPCSTR cmdline)
679 {
680     IWebBrowser2 *wb = NULL;
681
682     InternetExplorer_Create(NULL, &IID_IWebBrowser2, (void**)&wb);
683     if(!wb)
684         return NULL;
685
686     IWebBrowser2_put_Visible(wb, VARIANT_TRUE);
687     IWebBrowser2_put_MenuBar(wb, VARIANT_TRUE);
688
689     if(!*cmdline) {
690         IWebBrowser2_GoHome(wb);
691     }else {
692         VARIANT var_url;
693         DWORD len;
694         int cmdlen;
695
696         if(!strncasecmp(cmdline, "-nohome", 7))
697             cmdline += 7;
698         while(*cmdline == ' ' || *cmdline == '\t')
699             cmdline++;
700         cmdlen = lstrlenA(cmdline);
701         if(cmdlen > 2 && cmdline[0] == '"' && cmdline[cmdlen-1] == '"') {
702             cmdline++;
703             cmdlen -= 2;
704         }
705
706         V_VT(&var_url) = VT_BSTR;
707
708         len = MultiByteToWideChar(CP_ACP, 0, cmdline, cmdlen, NULL, 0);
709         V_BSTR(&var_url) = SysAllocStringLen(NULL, len);
710         MultiByteToWideChar(CP_ACP, 0, cmdline, cmdlen, V_BSTR(&var_url), len);
711
712         /* navigate to the first page */
713         IWebBrowser2_Navigate2(wb, &var_url, NULL, NULL, NULL, NULL);
714
715         SysFreeString(V_BSTR(&var_url));
716     }
717
718     return wb;
719 }
720
721 static void WINAPI DocHostContainer_GetDocObjRect(DocHost* This, RECT* rc)
722 {
723     GetClientRect(This->frame_hwnd, rc);
724     adjust_ie_docobj_rect(This->frame_hwnd, rc);
725 }
726
727 static HRESULT WINAPI DocHostContainer_SetStatusText(DocHost* This, LPCWSTR text)
728 {
729     InternetExplorer* ie = DOCHOST_THIS(This);
730     return update_ie_statustext(ie, text);
731 }
732
733 static void WINAPI DocHostContainer_SetURL(DocHost* This, LPCWSTR url)
734 {
735     SendMessageW(This->frame_hwnd, WM_UPDATEADDRBAR, 0, (LPARAM)url);
736 }
737
738 static const IDocHostContainerVtbl DocHostContainerVtbl = {
739     DocHostContainer_GetDocObjRect,
740     DocHostContainer_SetStatusText,
741     DocHostContainer_SetURL
742 };
743
744 HRESULT InternetExplorer_Create(IUnknown *pOuter, REFIID riid, void **ppv)
745 {
746     InternetExplorer *ret;
747     HRESULT hres;
748
749     TRACE("(%p %s %p)\n", pOuter, debugstr_guid(riid), ppv);
750
751     ret = heap_alloc_zero(sizeof(InternetExplorer));
752     ret->ref = 0;
753
754     ret->doc_host.disp = (IDispatch*)WEBBROWSER2(ret);
755     DocHost_Init(&ret->doc_host, (IDispatch*)WEBBROWSER2(ret), &DocHostContainerVtbl);
756
757     InternetExplorer_WebBrowser_Init(ret);
758
759     HlinkFrame_Init(&ret->hlink_frame, (IUnknown*)WEBBROWSER2(ret), &ret->doc_host);
760
761     create_frame_hwnd(ret);
762     ret->doc_host.frame_hwnd = ret->frame_hwnd;
763
764     hres = IWebBrowser2_QueryInterface(WEBBROWSER2(ret), riid, ppv);
765     if(FAILED(hres)) {
766         heap_free(ret);
767         return hres;
768     }
769
770     return hres;
771 }
772
773 /******************************************************************
774  *              IEWinMain            (SHDOCVW.101)
775  *
776  * Only returns on error.
777  */
778 DWORD WINAPI IEWinMain(LPSTR szCommandLine, int nShowWindow)
779 {
780     IWebBrowser2 *wb = NULL;
781     MSG msg;
782     HRESULT hres;
783
784     TRACE("%s %d\n", debugstr_a(szCommandLine), nShowWindow);
785
786     if(*szCommandLine == '-' || *szCommandLine == '/') {
787         if(!strcasecmp(szCommandLine+1, "regserver"))
788             return register_iexplore(TRUE);
789         if(!strcasecmp(szCommandLine+1, "unregserver"))
790             return register_iexplore(FALSE);
791     }
792
793     CoInitialize(NULL);
794
795     hres = register_class_object(TRUE);
796     if(FAILED(hres)) {
797         CoUninitialize();
798         ExitProcess(1);
799     }
800
801     if(strcasecmp(szCommandLine, "-embedding"))
802         wb = create_ie_window(szCommandLine);
803
804     /* run the message loop for this thread */
805     while (GetMessageW(&msg, 0, 0, 0))
806     {
807         TranslateMessage(&msg);
808         DispatchMessageW(&msg);
809     }
810
811     if(wb)
812         IWebBrowser2_Release(wb);
813
814     register_class_object(FALSE);
815
816     CoUninitialize();
817
818     ExitProcess(0);
819     return 0;
820 }