shdocvw: Separate DocHost ref count from InternetExplorer ref count.
[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 static const WCHAR szIEWinFrame[] = { 'I','E','F','r','a','m','e',0 };
48
49 /* Windows uses "Microsoft Internet Explorer" */
50 static const WCHAR wszWineInternetExplorer[] =
51         {'W','i','n','e',' ','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',0};
52
53 static LONG obj_cnt;
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->doc_host.hwnd)
511         SetWindowPos(This->doc_host->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
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->doc_host.document)
570             {
571                 IOleCommandTarget* target;
572
573                 if(FAILED(IUnknown_QueryInterface(This->doc_host->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(&This->IWebBrowser2_iface);
584             break;
585
586         case ID_BROWSE_ABOUT:
587             ie_dialog_about(hwnd);
588             break;
589
590         case ID_BROWSE_QUIT:
591             ShowWindow(hwnd, SW_HIDE);
592             break;
593
594         default:
595             if(LOWORD(wparam) >= ID_BROWSE_GOTOFAV_FIRST && LOWORD(wparam) <= ID_BROWSE_GOTOFAV_MAX)
596             {
597                 LPCWSTR url = get_fav_url_from_id(get_fav_menu(This->menu), LOWORD(wparam));
598
599                 if(url)
600                     ie_navigate(This, url);
601             }
602             return DefWindowProcW(hwnd, msg, wparam, lparam);
603     }
604     return 0;
605 }
606
607 static LRESULT update_addrbar(InternetExplorer *This, LPARAM lparam)
608 {
609     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
610     HWND hwndAddress = GetDlgItem(hwndRebar, IDC_BROWSE_ADDRESSBAR);
611     HWND hwndEdit = (HWND)SendMessageW(hwndAddress, CBEM_GETEDITCONTROL, 0, 0);
612     LPCWSTR url = (LPCWSTR)lparam;
613
614     SendMessageW(hwndEdit, WM_SETTEXT, 0, (LPARAM)url);
615
616     return 0;
617 }
618
619 static LRESULT CALLBACK
620 ie_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
621 {
622     InternetExplorer *This = (InternetExplorer*) GetWindowLongPtrW(hwnd, 0);
623
624     switch (msg)
625     {
626     case WM_CREATE:
627         return iewnd_OnCreate(hwnd, (LPCREATESTRUCTW)lparam);
628     case WM_CLOSE:
629         TRACE("WM_CLOSE\n");
630         ShowWindow(hwnd, SW_HIDE);
631         return 0;
632     case WM_SHOWWINDOW:
633         TRACE("WM_SHOWWINDOW %lx\n", wparam);
634         if(wparam)
635             IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
636         else
637             IWebBrowser2_Release(&This->IWebBrowser2_iface);
638         break;
639     case WM_DESTROY:
640         return iewnd_OnDestroy(This);
641     case WM_SIZE:
642         return iewnd_OnSize(This, LOWORD(lparam), HIWORD(lparam));
643     case WM_COMMAND:
644         return iewnd_OnCommand(This, hwnd, msg, wparam, lparam);
645     case WM_NOTIFY:
646         return iewnd_OnNotify(This, wparam, lparam);
647     case WM_DOCHOSTTASK:
648         return process_dochost_task(&This->doc_host->doc_host, lparam);
649     case WM_UPDATEADDRBAR:
650         return update_addrbar(This, lparam);
651     }
652     return DefWindowProcW(hwnd, msg, wparam, lparam);
653 }
654
655 void register_iewindow_class(void)
656 {
657     WNDCLASSEXW wc;
658
659     memset(&wc, 0, sizeof wc);
660     wc.cbSize = sizeof(wc);
661     wc.style = 0;
662     wc.lpfnWndProc = ie_window_proc;
663     wc.cbClsExtra = 0;
664     wc.cbWndExtra = sizeof(InternetExplorer*);
665     wc.hInstance = shdocvw_hinstance;
666     wc.hIcon = LoadIconW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON));
667     wc.hIconSm = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON,
668                             GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
669     wc.hCursor = LoadCursorW(0, MAKEINTRESOURCEW(IDC_ARROW));
670     wc.hbrBackground = 0;
671     wc.lpszClassName = szIEWinFrame;
672     wc.lpszMenuName = NULL;
673
674     RegisterClassExW(&wc);
675 }
676
677 void unregister_iewindow_class(void)
678 {
679     UnregisterClassW(szIEWinFrame, shdocvw_hinstance);
680 }
681
682 static void create_frame_hwnd(InternetExplorer *This)
683 {
684     This->frame_hwnd = CreateWindowExW(
685             WS_EX_WINDOWEDGE,
686             szIEWinFrame, wszWineInternetExplorer,
687             WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
688                 | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
689             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
690             NULL, NULL /* FIXME */, shdocvw_hinstance, This);
691 }
692
693 static BOOL create_ie_window(LPCSTR cmdline)
694 {
695     IWebBrowser2 *wb = NULL;
696
697     InternetExplorer_Create(NULL, &IID_IWebBrowser2, (void**)&wb);
698     if(!wb)
699         return FALSE;
700
701     IWebBrowser2_put_Visible(wb, VARIANT_TRUE);
702     IWebBrowser2_put_MenuBar(wb, VARIANT_TRUE);
703
704     if(!*cmdline) {
705         IWebBrowser2_GoHome(wb);
706     }else {
707         VARIANT var_url;
708         DWORD len;
709         int cmdlen;
710
711         if(!strncasecmp(cmdline, "-nohome", 7))
712             cmdline += 7;
713         while(*cmdline == ' ' || *cmdline == '\t')
714             cmdline++;
715         cmdlen = lstrlenA(cmdline);
716         if(cmdlen > 2 && cmdline[0] == '"' && cmdline[cmdlen-1] == '"') {
717             cmdline++;
718             cmdlen -= 2;
719         }
720
721         V_VT(&var_url) = VT_BSTR;
722
723         len = MultiByteToWideChar(CP_ACP, 0, cmdline, cmdlen, NULL, 0);
724         V_BSTR(&var_url) = SysAllocStringLen(NULL, len);
725         MultiByteToWideChar(CP_ACP, 0, cmdline, cmdlen, V_BSTR(&var_url), len);
726
727         /* navigate to the first page */
728         IWebBrowser2_Navigate2(wb, &var_url, NULL, NULL, NULL, NULL);
729
730         SysFreeString(V_BSTR(&var_url));
731     }
732
733     IWebBrowser2_Release(wb);
734     return TRUE;
735 }
736
737 static inline IEDocHost *impl_from_DocHost(DocHost *iface)
738 {
739     return CONTAINING_RECORD(iface, IEDocHost, doc_host);
740 }
741
742 static ULONG IEDocHost_addref(DocHost *iface)
743 {
744     IEDocHost *This = impl_from_DocHost(iface);
745     LONG ref = InterlockedIncrement(&This->ref);
746
747     TRACE("(%p) ref=%d\n", This, ref);
748
749     return ref;
750 }
751
752 static ULONG IEDocHost_release(DocHost *iface)
753 {
754     IEDocHost *This = impl_from_DocHost(iface);
755     LONG ref = InterlockedDecrement(&This->ref);
756
757     TRACE("(%p) ref=%d\n", This, ref);
758
759     if(!ref) {
760         if(This->ie)
761             This->ie->doc_host = NULL;
762         heap_free(This);
763     }
764
765     return ref;
766 }
767
768 static void WINAPI DocHostContainer_GetDocObjRect(DocHost* This, RECT* rc)
769 {
770     GetClientRect(This->frame_hwnd, rc);
771     adjust_ie_docobj_rect(This->frame_hwnd, rc);
772 }
773
774 static HRESULT WINAPI DocHostContainer_SetStatusText(DocHost *iface, LPCWSTR text)
775 {
776     IEDocHost *This = impl_from_DocHost(iface);
777     return update_ie_statustext(This->ie, text);
778 }
779
780 static void WINAPI DocHostContainer_SetURL(DocHost* This, LPCWSTR url)
781 {
782     SendMessageW(This->frame_hwnd, WM_UPDATEADDRBAR, 0, (LPARAM)url);
783 }
784
785 static HRESULT DocHostContainer_exec(DocHost* This, const GUID *cmd_group, DWORD cmdid, DWORD execopt, VARIANT *in,
786         VARIANT *out)
787 {
788     return S_OK;
789 }
790 static const IDocHostContainerVtbl DocHostContainerVtbl = {
791     IEDocHost_addref,
792     IEDocHost_release,
793     DocHostContainer_GetDocObjRect,
794     DocHostContainer_SetStatusText,
795     DocHostContainer_SetURL,
796     DocHostContainer_exec
797 };
798
799 HRESULT InternetExplorer_Create(IUnknown *pOuter, REFIID riid, void **ppv)
800 {
801     InternetExplorer *ret;
802     HRESULT hres;
803
804     TRACE("(%p %s %p)\n", pOuter, debugstr_guid(riid), ppv);
805
806     ret = heap_alloc_zero(sizeof(InternetExplorer));
807     if(!ret)
808         return E_OUTOFMEMORY;
809
810     ret->doc_host = heap_alloc_zero(sizeof(IEDocHost));
811     if(!ret->doc_host) {
812         heap_free(ret);
813         return E_OUTOFMEMORY;
814     }
815
816     ret->ref = 1;
817     ret->doc_host->ie = ret;
818
819     DocHost_Init(&ret->doc_host->doc_host, (IDispatch*)&ret->IWebBrowser2_iface, &DocHostContainerVtbl);
820
821     InternetExplorer_WebBrowser_Init(ret);
822
823     HlinkFrame_Init(&ret->hlink_frame, (IUnknown*)&ret->IWebBrowser2_iface, &ret->doc_host->doc_host);
824
825     create_frame_hwnd(ret);
826     ret->doc_host->doc_host.frame_hwnd = ret->frame_hwnd;
827
828     hres = IWebBrowser2_QueryInterface(&ret->IWebBrowser2_iface, riid, ppv);
829     IWebBrowser2_Release(&ret->IWebBrowser2_iface);
830     if(FAILED(hres))
831         return hres;
832
833     InterlockedIncrement(&obj_cnt);
834     return S_OK;
835 }
836
837 void released_obj(void)
838 {
839     if(!InterlockedDecrement(&obj_cnt))
840         PostQuitMessage(0);
841 }
842
843 /******************************************************************
844  *              IEWinMain            (SHDOCVW.101)
845  *
846  * Only returns on error.
847  */
848 DWORD WINAPI IEWinMain(LPSTR szCommandLine, int nShowWindow)
849 {
850     MSG msg;
851     HRESULT hres;
852
853     TRACE("%s %d\n", debugstr_a(szCommandLine), nShowWindow);
854
855     if(*szCommandLine == '-' || *szCommandLine == '/') {
856         if(!strcasecmp(szCommandLine+1, "regserver"))
857             return register_iexplore(TRUE);
858         if(!strcasecmp(szCommandLine+1, "unregserver"))
859             return register_iexplore(FALSE);
860     }
861
862     CoInitialize(NULL);
863
864     hres = register_class_object(TRUE);
865     if(FAILED(hres)) {
866         CoUninitialize();
867         ExitProcess(1);
868     }
869
870     if(strcasecmp(szCommandLine, "-embedding")) {
871         if(!create_ie_window(szCommandLine)) {
872             CoUninitialize();
873             ExitProcess(1);
874         }
875     }
876
877     /* run the message loop for this thread */
878     while (GetMessageW(&msg, 0, 0, 0))
879     {
880         TranslateMessage(&msg);
881         DispatchMessageW(&msg);
882     }
883
884     register_class_object(FALSE);
885
886     CoUninitialize();
887
888     ExitProcess(0);
889     return 0;
890 }