xref: /reactos/dll/win32/ieframe/iexplore.c (revision d5399189)
1 /*
2  * ieframe - 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 
26 #include "ieframe.h"
27 #include "resource.h"
28 
29 #include "winuser.h"
30 #include "wingdi.h"
31 #include "winnls.h"
32 #include "ole2.h"
33 #include "exdisp.h"
34 #include "oleidl.h"
35 
36 #include "mshtmcid.h"
37 #include "shellapi.h"
38 #include "winreg.h"
39 #include "shlwapi.h"
40 #include "intshcut.h"
41 #include "ddeml.h"
42 #include "ieautomation.h"
43 
44 #include "wine/debug.h"
45 
46 WINE_DEFAULT_DEBUG_CHANNEL(ieframe);
47 
48 #define IDI_APPICON 1
49 
50 #define WM_UPDATEADDRBAR    (WM_APP+1)
51 
52 static const WCHAR szIEWinFrame[] = { 'I','E','F','r','a','m','e',0 };
53 
54 /* Windows uses "Microsoft Internet Explorer" */
55 static const WCHAR wszWineInternetExplorer[] =
56         {'W','i','n','e',' ','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',0};
57 
58 static LONG obj_cnt;
59 static DWORD dde_inst;
60 static HSZ ddestr_iexplore, ddestr_openurl;
61 static struct list ie_list = LIST_INIT(ie_list);
62 
63 HRESULT update_ie_statustext(InternetExplorer* This, LPCWSTR text)
64 {
65     if(!SendMessageW(This->status_hwnd, SB_SETTEXTW, MAKEWORD(SB_SIMPLEID, 0), (LPARAM)text))
66         return E_FAIL;
67 
68     return S_OK;
69 }
70 
71 static void adjust_ie_docobj_rect(HWND frame, RECT* rc)
72 {
73     HWND hwndRebar = GetDlgItem(frame, IDC_BROWSE_REBAR);
74     HWND hwndStatus = GetDlgItem(frame, IDC_BROWSE_STATUSBAR);
75     INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);
76 
77     InflateRect(rc, 0, -barHeight);
78 
79     if(IsWindowVisible(hwndStatus))
80     {
81         RECT statusrc;
82 
83         GetClientRect(hwndStatus, &statusrc);
84         rc->bottom -= statusrc.bottom - statusrc.top;
85     }
86 }
87 
88 static HMENU get_tb_menu(HMENU menu)
89 {
90     HMENU menu_view = GetSubMenu(menu, 1);
91 
92     return GetSubMenu(menu_view, 0);
93 }
94 
95 static HMENU get_fav_menu(HMENU menu)
96 {
97     return GetSubMenu(menu, 2);
98 }
99 
100 static LPWSTR get_fav_url_from_id(HMENU menu, UINT id)
101 {
102     MENUITEMINFOW item;
103 
104     item.cbSize = sizeof(item);
105     item.fMask = MIIM_DATA;
106 
107     if(!GetMenuItemInfoW(menu, id, FALSE, &item))
108         return NULL;
109 
110     return (LPWSTR)item.dwItemData;
111 }
112 
113 static void free_fav_menu_data(HMENU menu)
114 {
115     LPWSTR url;
116     int i;
117 
118     for(i = 0; (url = get_fav_url_from_id(menu, ID_BROWSE_GOTOFAV_FIRST + i)); i++)
119         heap_free( url );
120 }
121 
122 static int get_menu_item_count(HMENU menu)
123 {
124     MENUITEMINFOW item;
125     int count = 0;
126     int i;
127 
128     item.cbSize = sizeof(item);
129     item.fMask = MIIM_DATA | MIIM_SUBMENU;
130 
131     for(i = 0; GetMenuItemInfoW(menu, i, TRUE, &item); i++)
132     {
133         if(item.hSubMenu)
134             count += get_menu_item_count(item.hSubMenu);
135         else
136             count++;
137     }
138 
139     return count;
140 }
141 
142 static void add_fav_to_menu(HMENU favmenu, HMENU menu, LPWSTR title, LPCWSTR url)
143 {
144     MENUITEMINFOW item;
145     /* Subtract the number of standard elements in the Favorites menu */
146     int favcount = get_menu_item_count(favmenu) - 2;
147     LPWSTR urlbuf;
148 
149     if(favcount > (ID_BROWSE_GOTOFAV_MAX - ID_BROWSE_GOTOFAV_FIRST))
150     {
151         FIXME("Add support for more than %d Favorites\n", favcount);
152         return;
153     }
154 
155     urlbuf = heap_alloc((lstrlenW(url) + 1) * sizeof(WCHAR));
156 
157     if(!urlbuf)
158         return;
159 
160     lstrcpyW(urlbuf, url);
161 
162     item.cbSize = sizeof(item);
163     item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA | MIIM_ID;
164     item.fType = MFT_STRING;
165     item.dwTypeData = title;
166     item.wID = ID_BROWSE_GOTOFAV_FIRST + favcount;
167     item.dwItemData = (ULONG_PTR)urlbuf;
168     InsertMenuItemW(menu, -1, TRUE, &item);
169 }
170 
171 static void add_favs_to_menu(HMENU favmenu, HMENU menu, LPCWSTR dir)
172 {
173     WCHAR path[MAX_PATH*2];
174     const WCHAR search[] = {'*',0};
175     WCHAR* filename;
176     HANDLE findhandle;
177     WIN32_FIND_DATAW finddata;
178     IUniformResourceLocatorW* urlobj;
179     IPersistFile* urlfile = NULL;
180     HRESULT res;
181 
182     lstrcpyW(path, dir);
183     PathAppendW(path, search);
184 
185     findhandle = FindFirstFileW(path, &finddata);
186 
187     if(findhandle == INVALID_HANDLE_VALUE)
188         return;
189 
190     res = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, &IID_IUniformResourceLocatorW, (PVOID*)&urlobj);
191 
192     if(SUCCEEDED(res))
193         res = IUnknown_QueryInterface((IUnknown*)urlobj, &IID_IPersistFile, (PVOID*)&urlfile);
194 
195     if(SUCCEEDED(res))
196     {
197         filename = path + lstrlenW(path) - lstrlenW(search);
198 
199         do
200         {
201             lstrcpyW(filename, finddata.cFileName);
202 
203             if(finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
204             {
205                 MENUITEMINFOW item;
206                 const WCHAR ignore1[] = {'.','.',0};
207                 const WCHAR ignore2[] = {'.',0};
208 
209                 if(!lstrcmpW(filename, ignore1) || !lstrcmpW(filename, ignore2))
210                     continue;
211 
212                 item.cbSize = sizeof(item);
213                 item.fMask = MIIM_STRING | MIIM_SUBMENU;
214                 item.dwTypeData = filename;
215                 item.hSubMenu = CreatePopupMenu();
216                 InsertMenuItemW(menu, -1, TRUE, &item);
217                 add_favs_to_menu(favmenu, item.hSubMenu, path);
218             } else
219             {
220                 WCHAR* fileext;
221                 WCHAR* url = NULL;
222                 const WCHAR urlext[] = {'.','u','r','l',0};
223 
224                 if(lstrcmpiW(PathFindExtensionW(filename), urlext))
225                     continue;
226 
227                 if(FAILED(IPersistFile_Load(urlfile, path, 0)))
228                     continue;
229 
230                 urlobj->lpVtbl->GetURL(urlobj, &url);
231 
232                 if(!url)
233                     continue;
234 
235                 fileext = filename + lstrlenW(filename) - lstrlenW(urlext);
236                 *fileext = 0;
237                 add_fav_to_menu(favmenu, menu, filename, url);
238             }
239         } while(FindNextFileW(findhandle, &finddata));
240     }
241 
242     if(urlfile)
243         IPersistFile_Release(urlfile);
244 
245     if(urlobj)
246         IUnknown_Release((IUnknown*)urlobj);
247 
248     FindClose(findhandle);
249 }
250 
251 static void add_tbs_to_menu(HMENU menu)
252 {
253     HUSKEY toolbar_handle;
254     WCHAR toolbar_key[] = {'S','o','f','t','w','a','r','e','\\',
255                            'M','i','c','r','o','s','o','f','t','\\',
256                            'I','n','t','e','r','n','e','t',' ',
257                            'E','x','p','l','o','r','e','r','\\',
258                            'T','o','o','l','b','a','r',0};
259 
260     if(SHRegOpenUSKeyW(toolbar_key, KEY_READ, NULL, &toolbar_handle, TRUE) == ERROR_SUCCESS)
261     {
262         HUSKEY classes_handle;
263         WCHAR classes_key[] = {'S','o','f','t','w','a','r','e','\\',
264                                'C','l','a','s','s','e','s','\\','C','L','S','I','D',0};
265         WCHAR guid[39];
266         DWORD value_len = sizeof(guid)/sizeof(guid[0]);
267         int i;
268 
269         if(SHRegOpenUSKeyW(classes_key, KEY_READ, NULL, &classes_handle, TRUE) != ERROR_SUCCESS)
270         {
271             SHRegCloseUSKey(toolbar_handle);
272             ERR("Failed to open key %s\n", debugstr_w(classes_key));
273             return;
274         }
275 
276         for(i = 0; SHRegEnumUSValueW(toolbar_handle, i, guid, &value_len, NULL, NULL, NULL, SHREGENUM_HKLM) == ERROR_SUCCESS; i++)
277         {
278             WCHAR tb_name[100];
279             DWORD tb_name_len = sizeof(tb_name)/sizeof(tb_name[0]);
280             HUSKEY tb_class_handle;
281             MENUITEMINFOW item;
282             LSTATUS ret;
283             value_len = sizeof(guid)/sizeof(guid[0]);
284 
285             if(lstrlenW(guid) != 38)
286             {
287                 TRACE("Found invalid IE toolbar entry: %s\n", debugstr_w(guid));
288                 continue;
289             }
290 
291             if(SHRegOpenUSKeyW(guid, KEY_READ, classes_handle, &tb_class_handle, TRUE) != ERROR_SUCCESS)
292             {
293                 ERR("Failed to get class info for %s\n", debugstr_w(guid));
294                 continue;
295             }
296 
297             ret = SHRegQueryUSValueW(tb_class_handle, NULL, NULL, tb_name, &tb_name_len, TRUE, NULL, 0);
298 
299             SHRegCloseUSKey(tb_class_handle);
300 
301             if(ret != ERROR_SUCCESS)
302             {
303                 ERR("Failed to get toolbar name for %s\n", debugstr_w(guid));
304                 continue;
305             }
306 
307             item.cbSize = sizeof(item);
308             item.fMask = MIIM_STRING;
309             item.dwTypeData = tb_name;
310             InsertMenuItemW(menu, GetMenuItemCount(menu), TRUE, &item);
311         }
312 
313         SHRegCloseUSKey(classes_handle);
314         SHRegCloseUSKey(toolbar_handle);
315     }
316 }
317 
318 static HMENU create_ie_menu(void)
319 {
320     HMENU menu = LoadMenuW(ieframe_instance, MAKEINTRESOURCEW(IDR_BROWSE_MAIN_MENU));
321     HMENU favmenu = get_fav_menu(menu);
322     WCHAR path[MAX_PATH];
323 
324     add_tbs_to_menu(get_tb_menu(menu));
325 
326     if(SHGetFolderPathW(NULL, CSIDL_COMMON_FAVORITES, NULL, SHGFP_TYPE_CURRENT, path) == S_OK)
327         add_favs_to_menu(favmenu, favmenu, path);
328 
329     if(SHGetFolderPathW(NULL, CSIDL_FAVORITES, NULL, SHGFP_TYPE_CURRENT, path) == S_OK)
330         add_favs_to_menu(favmenu, favmenu, path);
331 
332     return menu;
333 }
334 
335 static void ie_navigate(InternetExplorer* This, LPCWSTR url)
336 {
337     VARIANT variant;
338 
339     V_VT(&variant) = VT_BSTR;
340     V_BSTR(&variant) = SysAllocString(url);
341 
342     IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &variant, NULL, NULL, NULL, NULL);
343 
344     SysFreeString(V_BSTR(&variant));
345 }
346 
347 static INT_PTR CALLBACK ie_dialog_open_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
348 {
349     static InternetExplorer* This;
350 
351     switch(msg)
352     {
353         case WM_INITDIALOG:
354             This = (InternetExplorer*)lparam;
355             EnableWindow(GetDlgItem(hwnd, IDOK), FALSE);
356             return TRUE;
357 
358         case WM_COMMAND:
359             switch(LOWORD(wparam))
360             {
361                 case IDC_BROWSE_OPEN_URL:
362                 {
363                     HWND hwndurl = GetDlgItem(hwnd, IDC_BROWSE_OPEN_URL);
364                     int len = GetWindowTextLengthW(hwndurl);
365 
366                     EnableWindow(GetDlgItem(hwnd, IDOK), len != 0);
367                     break;
368                 }
369                 case IDOK:
370                 {
371                     HWND hwndurl = GetDlgItem(hwnd, IDC_BROWSE_OPEN_URL);
372                     int len = GetWindowTextLengthW(hwndurl);
373 
374                     if(len)
375                     {
376                         VARIANT url;
377 
378                         V_VT(&url) = VT_BSTR;
379                         V_BSTR(&url) = SysAllocStringLen(NULL, len);
380 
381                         GetWindowTextW(hwndurl, V_BSTR(&url), len + 1);
382                         IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &url, NULL, NULL, NULL, NULL);
383 
384                         SysFreeString(V_BSTR(&url));
385                     }
386                 }
387                 /* fall through */
388                 case IDCANCEL:
389                     EndDialog(hwnd, wparam);
390                     return TRUE;
391             }
392     }
393     return FALSE;
394 }
395 
396 static void ie_dialog_about(HWND hwnd)
397 {
398     HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON, 48, 48, LR_SHARED);
399 
400     ShellAboutW(hwnd, wszWineInternetExplorer, NULL, icon);
401 
402     DestroyIcon(icon);
403 }
404 
405 #ifdef __REACTOS__
406 static void ie_dialog_properties(HWND hwnd)
407 {
408     ShellExecuteW(hwnd, NULL, L"inetcpl.cpl", NULL, NULL, 0);
409 }
410 #endif
411 
412 static void add_tb_separator(InternetExplorer *ie)
413 {
414     TBBUTTON btn;
415 
416     ZeroMemory(&btn, sizeof(btn));
417 
418     btn.iBitmap = 3;
419     btn.fsStyle = BTNS_SEP;
420     SendMessageW(ie->toolbar_hwnd, TB_ADDBUTTONSW, 1, (LPARAM)&btn);
421 }
422 
423 static void add_tb_button(InternetExplorer *ie, int bmp, int cmd, int strId)
424 {
425     TBBUTTON btn;
426     WCHAR buf[30];
427 
428     LoadStringW(ieframe_instance, strId, buf, sizeof(buf)/sizeof(buf[0]));
429 
430     btn.iBitmap = bmp;
431     btn.idCommand = cmd;
432     btn.fsState = TBSTATE_ENABLED;
433     btn.fsStyle = BTNS_SHOWTEXT;
434     btn.dwData = 0;
435     btn.iString = (INT_PTR)buf;
436 
437     SendMessageW(ie->toolbar_hwnd, TB_ADDBUTTONSW, 1, (LPARAM)&btn);
438 }
439 
440 static void enable_toolbar_button(InternetExplorer *ie, int command, BOOL enable)
441 {
442     SendMessageW(ie->toolbar_hwnd, TB_ENABLEBUTTON, command, enable);
443 }
444 
445 static void create_rebar(InternetExplorer *ie)
446 {
447     HWND hwndRebar;
448     HWND hwndAddress;
449     REBARINFO rebarinf;
450     REBARBANDINFOW bandinf;
451     WCHAR addr[40];
452     HIMAGELIST imagelist;
453     SIZE toolbar_size;
454 
455     LoadStringW(ieframe_instance, IDS_ADDRESS, addr, sizeof(addr)/sizeof(addr[0]));
456 
457     hwndRebar = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
458             WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP|CCS_NODIVIDER, 0, 0, 0, 0,
459             ie->frame_hwnd, (HMENU)IDC_BROWSE_REBAR, ieframe_instance, NULL);
460 
461     rebarinf.cbSize = sizeof(rebarinf);
462     rebarinf.fMask = 0;
463     rebarinf.himl = NULL;
464 
465     SendMessageW(hwndRebar, RB_SETBARINFO, 0, (LPARAM)&rebarinf);
466 
467     ie->toolbar_hwnd = CreateWindowExW(TBSTYLE_EX_MIXEDBUTTONS, TOOLBARCLASSNAMEW, NULL, TBSTYLE_FLAT | WS_CHILD | WS_VISIBLE | CCS_NORESIZE,
468             0, 0, 0, 0, hwndRebar, (HMENU)IDC_BROWSE_TOOLBAR, ieframe_instance, NULL);
469 
470     imagelist = ImageList_LoadImageW(ieframe_instance, MAKEINTRESOURCEW(IDB_IETOOLBAR), 32, 0, CLR_NONE, IMAGE_BITMAP, LR_CREATEDIBSECTION);
471 
472     SendMessageW(ie->toolbar_hwnd, TB_SETIMAGELIST, 0, (LPARAM)imagelist);
473     SendMessageW(ie->toolbar_hwnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
474     add_tb_button(ie, 0, ID_BROWSE_BACK, IDS_TB_BACK);
475     add_tb_button(ie, 1, ID_BROWSE_FORWARD, IDS_TB_FORWARD);
476     add_tb_button(ie, 2, ID_BROWSE_STOP, IDS_TB_STOP);
477     add_tb_button(ie, 3, ID_BROWSE_REFRESH, IDS_TB_REFRESH);
478     add_tb_button(ie, 4, ID_BROWSE_HOME, IDS_TB_HOME);
479     add_tb_separator(ie);
480     add_tb_button(ie, 5, ID_BROWSE_PRINT, IDS_TB_PRINT);
481     SendMessageW(ie->toolbar_hwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(65,50));
482     SendMessageW(ie->toolbar_hwnd, TB_GETMAXSIZE, 0, (LPARAM)&toolbar_size);
483 
484     bandinf.cbSize = sizeof(bandinf);
485     bandinf.fMask = RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE;
486     bandinf.fStyle = RBBS_CHILDEDGE;
487     bandinf.cxMinChild = toolbar_size.cx;
488     bandinf.cyMinChild = toolbar_size.cy+2;
489     bandinf.hwndChild = ie->toolbar_hwnd;
490 
491     SendMessageW(hwndRebar, RB_INSERTBANDW, -1, (LPARAM)&bandinf);
492 
493     hwndAddress = CreateWindowExW(0, WC_COMBOBOXEXW, NULL, WS_BORDER|WS_CHILD|WS_VISIBLE|CBS_DROPDOWN,
494             0, 0, 100,20,hwndRebar, (HMENU)IDC_BROWSE_ADDRESSBAR, ieframe_instance, NULL);
495 
496     bandinf.fMask |= RBBIM_TEXT;
497     bandinf.fStyle = RBBS_CHILDEDGE | RBBS_BREAK;
498     bandinf.lpText = addr;
499     bandinf.cxMinChild = 100;
500     bandinf.cyMinChild = 20;
501     bandinf.hwndChild = hwndAddress;
502 
503     SendMessageW(hwndRebar, RB_INSERTBANDW, -1, (LPARAM)&bandinf);
504 }
505 
506 static LRESULT iewnd_OnCreate(HWND hwnd, LPCREATESTRUCTW lpcs)
507 {
508     InternetExplorer* This = (InternetExplorer*)lpcs->lpCreateParams;
509     SetWindowLongPtrW(hwnd, 0, (LONG_PTR) lpcs->lpCreateParams);
510 
511     This->doc_host.frame_hwnd = This->frame_hwnd = hwnd;
512 
513     This->menu = create_ie_menu();
514 
515     This->status_hwnd = CreateStatusWindowW(WS_VISIBLE|WS_CHILD|SBT_NOBORDERS|CCS_NODIVIDER,
516                                             NULL, hwnd, IDC_BROWSE_STATUSBAR);
517     SendMessageW(This->status_hwnd, SB_SIMPLE, TRUE, 0);
518 
519     create_rebar(This);
520 
521     return 0;
522 }
523 
524 static LRESULT iewnd_OnSize(InternetExplorer *This, INT width, INT height)
525 {
526     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
527     INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);
528     RECT docarea = {0, 0, width, height};
529 
530     SendMessageW(This->status_hwnd, WM_SIZE, 0, 0);
531 
532     adjust_ie_docobj_rect(This->frame_hwnd, &docarea);
533 
534     if(This->doc_host.hwnd)
535         SetWindowPos(This->doc_host.hwnd, NULL, docarea.left, docarea.top, docarea.right, docarea.bottom,
536                      SWP_NOZORDER | SWP_NOACTIVATE);
537 
538     SetWindowPos(hwndRebar, NULL, 0, 0, width, barHeight, SWP_NOZORDER | SWP_NOACTIVATE);
539 
540     return 0;
541 }
542 
543 static LRESULT iewnd_OnNotify(InternetExplorer *This, WPARAM wparam, LPARAM lparam)
544 {
545     NMHDR* hdr = (NMHDR*)lparam;
546 
547     if(hdr->idFrom == IDC_BROWSE_ADDRESSBAR && hdr->code == CBEN_ENDEDITW)
548     {
549         NMCBEENDEDITW* info = (NMCBEENDEDITW*)lparam;
550 
551         if(info->fChanged && info->iWhy == CBENF_RETURN)
552         {
553             VARIANT vt;
554 
555             V_VT(&vt) = VT_BSTR;
556             V_BSTR(&vt) = SysAllocString(info->szText);
557 
558             IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &vt, NULL, NULL, NULL, NULL);
559 
560             SysFreeString(V_BSTR(&vt));
561 
562             return 0;
563         }
564     }
565 
566     if(hdr->idFrom == IDC_BROWSE_REBAR && hdr->code == RBN_HEIGHTCHANGE)
567     {
568         RECT docarea;
569 
570         GetClientRect(This->frame_hwnd, &docarea);
571         adjust_ie_docobj_rect(This->frame_hwnd, &docarea);
572 
573         if(This->doc_host.hwnd)
574             SetWindowPos(This->doc_host.hwnd, NULL, docarea.left, docarea.top, docarea.right, docarea.bottom,
575                     SWP_NOZORDER | SWP_NOACTIVATE);
576     }
577 
578     return 0;
579 }
580 
581 static LRESULT iewnd_OnDestroy(InternetExplorer *This)
582 {
583     HIMAGELIST list = (HIMAGELIST)SendMessageW(This->toolbar_hwnd, TB_GETIMAGELIST, 0, 0);
584 
585     TRACE("%p\n", This);
586 
587     free_fav_menu_data(get_fav_menu(This->menu));
588     ImageList_Destroy(list);
589     This->frame_hwnd = NULL;
590 
591     return 0;
592 }
593 
594 static LRESULT iewnd_OnCommand(InternetExplorer *This, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
595 {
596     switch(LOWORD(wparam))
597     {
598         case ID_BROWSE_OPEN:
599             DialogBoxParamW(ieframe_instance, MAKEINTRESOURCEW(IDD_BROWSE_OPEN), hwnd, ie_dialog_open_proc, (LPARAM)This);
600             break;
601 
602         case ID_BROWSE_PRINT:
603             if(This->doc_host.document)
604             {
605                 IOleCommandTarget* target;
606 
607                 if(FAILED(IUnknown_QueryInterface(This->doc_host.document, &IID_IOleCommandTarget, (LPVOID*)&target)))
608                     break;
609 
610                 IOleCommandTarget_Exec(target, &CGID_MSHTML, IDM_PRINT, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
611 
612                 IOleCommandTarget_Release(target);
613             }
614             break;
615 
616         case ID_BROWSE_HOME:
617             IWebBrowser2_GoHome(&This->IWebBrowser2_iface);
618             break;
619 
620         case ID_BROWSE_BACK:
621             IWebBrowser2_GoBack(&This->IWebBrowser2_iface);
622             break;
623 
624         case ID_BROWSE_FORWARD:
625             IWebBrowser2_GoForward(&This->IWebBrowser2_iface);
626             break;
627 
628         case ID_BROWSE_STOP:
629             IWebBrowser2_Stop(&This->IWebBrowser2_iface);
630             break;
631 
632         case ID_BROWSE_REFRESH:
633             IWebBrowser2_Refresh(&This->IWebBrowser2_iface);
634             break;
635 
636         case ID_BROWSE_ABOUT:
637             ie_dialog_about(hwnd);
638             break;
639 
640 #ifdef __REACTOS__
641         case ID_BROWSE_PROPERTIES:
642             ie_dialog_properties(hwnd);
643             break;
644 #endif
645 
646         case ID_BROWSE_QUIT:
647             ShowWindow(hwnd, SW_HIDE);
648             break;
649 
650         default:
651             if(LOWORD(wparam) >= ID_BROWSE_GOTOFAV_FIRST && LOWORD(wparam) <= ID_BROWSE_GOTOFAV_MAX)
652             {
653                 LPCWSTR url = get_fav_url_from_id(get_fav_menu(This->menu), LOWORD(wparam));
654 
655                 if(url)
656                     ie_navigate(This, url);
657             }
658             return DefWindowProcW(hwnd, msg, wparam, lparam);
659     }
660     return 0;
661 }
662 
663 static LRESULT update_addrbar(InternetExplorer *This, LPARAM lparam)
664 {
665     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
666     HWND hwndAddress = GetDlgItem(hwndRebar, IDC_BROWSE_ADDRESSBAR);
667     HWND hwndEdit = (HWND)SendMessageW(hwndAddress, CBEM_GETEDITCONTROL, 0, 0);
668     LPCWSTR url = (LPCWSTR)lparam;
669 
670     SendMessageW(hwndEdit, WM_SETTEXT, 0, (LPARAM)url);
671 
672     return 0;
673 }
674 
675 static LRESULT WINAPI ie_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
676 {
677     InternetExplorer *This = (InternetExplorer*) GetWindowLongPtrW(hwnd, 0);
678 
679     switch (msg)
680     {
681     case WM_CREATE:
682         return iewnd_OnCreate(hwnd, (LPCREATESTRUCTW)lparam);
683     case WM_CLOSE:
684         TRACE("WM_CLOSE\n");
685         ShowWindow(hwnd, SW_HIDE);
686         return 0;
687     case WM_SHOWWINDOW:
688         TRACE("WM_SHOWWINDOW %lx\n", wparam);
689         if(wparam) {
690             IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
691             InterlockedIncrement(&This->extern_ref);
692         }else {
693             release_extern_ref(This, TRUE);
694             IWebBrowser2_Release(&This->IWebBrowser2_iface);
695         }
696         break;
697     case WM_DESTROY:
698         return iewnd_OnDestroy(This);
699     case WM_SIZE:
700         return iewnd_OnSize(This, LOWORD(lparam), HIWORD(lparam));
701     case WM_COMMAND:
702         return iewnd_OnCommand(This, hwnd, msg, wparam, lparam);
703     case WM_NOTIFY:
704         return iewnd_OnNotify(This, wparam, lparam);
705     case WM_DOCHOSTTASK:
706         return process_dochost_tasks(&This->doc_host);
707     case WM_UPDATEADDRBAR:
708         return update_addrbar(This, lparam);
709     }
710     return DefWindowProcW(hwnd, msg, wparam, lparam);
711 }
712 
713 void register_iewindow_class(void)
714 {
715     WNDCLASSEXW wc;
716 
717     memset(&wc, 0, sizeof wc);
718     wc.cbSize = sizeof(wc);
719     wc.style = 0;
720     wc.lpfnWndProc = ie_window_proc;
721     wc.cbClsExtra = 0;
722     wc.cbWndExtra = sizeof(InternetExplorer*);
723     wc.hInstance = ieframe_instance;
724     wc.hIcon = LoadIconW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON));
725     wc.hIconSm = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON,
726                             GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
727     wc.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
728     wc.hbrBackground = 0;
729     wc.lpszClassName = szIEWinFrame;
730     wc.lpszMenuName = NULL;
731 
732     RegisterClassExW(&wc);
733 }
734 
735 void unregister_iewindow_class(void)
736 {
737     UnregisterClassW(szIEWinFrame, ieframe_instance);
738 }
739 
740 static void create_frame_hwnd(InternetExplorer *This)
741 {
742     CreateWindowExW(
743             WS_EX_WINDOWEDGE,
744             szIEWinFrame, wszWineInternetExplorer,
745             WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
746                 | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
747             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
748             NULL, NULL /* FIXME */, ieframe_instance, This);
749 
750     create_doc_view_hwnd(&This->doc_host);
751 }
752 
753 static inline InternetExplorer *impl_from_DocHost(DocHost *iface)
754 {
755     return CONTAINING_RECORD(iface, InternetExplorer, doc_host);
756 }
757 
758 static ULONG IEDocHost_addref(DocHost *iface)
759 {
760     InternetExplorer *This = impl_from_DocHost(iface);
761     return IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
762 }
763 
764 static ULONG IEDocHost_release(DocHost *iface)
765 {
766     InternetExplorer *This = impl_from_DocHost(iface);
767     return IWebBrowser2_Release(&This->IWebBrowser2_iface);
768 }
769 
770 static void DocHostContainer_get_docobj_rect(DocHost *This, RECT *rc)
771 {
772     GetClientRect(This->frame_hwnd, rc);
773     adjust_ie_docobj_rect(This->frame_hwnd, rc);
774 }
775 
776 static HRESULT DocHostContainer_set_status_text(DocHost *iface, const WCHAR *text)
777 {
778     InternetExplorer *This = impl_from_DocHost(iface);
779     return update_ie_statustext(This, text);
780 }
781 
782 static void DocHostContainer_on_command_state_change(DocHost *iface, LONG command, BOOL enable)
783 {
784     InternetExplorer *This = impl_from_DocHost(iface);
785 
786     switch(command) {
787     case CSC_NAVIGATEBACK:
788         enable_toolbar_button(This, ID_BROWSE_BACK, enable);
789         break;
790     case CSC_NAVIGATEFORWARD:
791         enable_toolbar_button(This, ID_BROWSE_FORWARD, enable);
792         break;
793     }
794 }
795 
796 static void DocHostContainer_set_url(DocHost* iface, const WCHAR *url)
797 {
798     InternetExplorer *This = impl_from_DocHost(iface);
799 
800     This->nohome = FALSE;
801     SendMessageW(This->frame_hwnd, WM_UPDATEADDRBAR, 0, (LPARAM)url);
802 }
803 
804 static const IDocHostContainerVtbl DocHostContainerVtbl = {
805     IEDocHost_addref,
806     IEDocHost_release,
807     DocHostContainer_get_docobj_rect,
808     DocHostContainer_set_status_text,
809     DocHostContainer_on_command_state_change,
810     DocHostContainer_set_url
811 };
812 
813 static HRESULT create_ie(InternetExplorer **ret_obj)
814 {
815     InternetExplorer *ret;
816 
817     ret = heap_alloc_zero(sizeof(InternetExplorer));
818     if(!ret)
819         return E_OUTOFMEMORY;
820 
821     ret->ref = 1;
822 
823     DocHost_Init(&ret->doc_host, &ret->IWebBrowser2_iface, &DocHostContainerVtbl);
824 
825     InternetExplorer_WebBrowser_Init(ret);
826 
827     HlinkFrame_Init(&ret->hlink_frame, (IUnknown*)&ret->IWebBrowser2_iface, &ret->doc_host);
828 
829     create_frame_hwnd(ret);
830 
831     InterlockedIncrement(&obj_cnt);
832     list_add_tail(&ie_list, &ret->entry);
833     *ret_obj = ret;
834     return S_OK;
835 }
836 
837 HRESULT WINAPI InternetExplorer_Create(IClassFactory *iface, IUnknown *pOuter, REFIID riid, void **ppv)
838 {
839     InternetExplorer *ret;
840     HRESULT hres;
841 
842     TRACE("(%p %s %p)\n", pOuter, debugstr_guid(riid), ppv);
843 
844     hres = create_ie(&ret);
845     if(FAILED(hres))
846         return hres;
847 
848     hres = IWebBrowser2_QueryInterface(&ret->IWebBrowser2_iface, riid, ppv);
849     IWebBrowser2_Release(&ret->IWebBrowser2_iface);
850     if(FAILED(hres))
851         return hres;
852 
853     return S_OK;
854 }
855 
856 /******************************************************************
857  *      IInternetExplorerManager implementation
858  */
859 struct InternetExplorerManager {
860     IInternetExplorerManager IInternetExplorerManager_iface;
861     LONG ref;
862 };
863 
864 static inline InternetExplorerManager *impl_from_IInternetExplorerManager(IInternetExplorerManager *iface)
865 {
866     return CONTAINING_RECORD(iface, InternetExplorerManager, IInternetExplorerManager_iface);
867 }
868 
869 static HRESULT WINAPI InternetExplorerManager_QueryInterface(IInternetExplorerManager *iface, REFIID riid, void **out)
870 {
871     TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), out);
872 
873     if (IsEqualGUID(riid, &IID_IInternetExplorerManager) || IsEqualGUID(riid, &IID_IUnknown))
874     {
875         IInternetExplorerManager_AddRef(iface);
876         *out = iface;
877         return S_OK;
878     }
879 
880     FIXME("interface %s not implemented\n", debugstr_guid(riid));
881     *out = NULL;
882     return E_NOINTERFACE;
883 }
884 
885 static ULONG WINAPI InternetExplorerManager_AddRef(IInternetExplorerManager *iface)
886 {
887     InternetExplorerManager *This = impl_from_IInternetExplorerManager(iface);
888     ULONG ref = InterlockedIncrement(&This->ref);
889 
890     TRACE("(%p) increasing refcount to %u\n", iface, ref);
891 
892     return ref;
893 }
894 
895 static ULONG WINAPI InternetExplorerManager_Release(IInternetExplorerManager *iface)
896 {
897     InternetExplorerManager *This = impl_from_IInternetExplorerManager(iface);
898     ULONG ref = InterlockedDecrement(&This->ref);
899 
900     TRACE("(%p) decreasing refcount to %u\n", iface, ref);
901 
902     if (ref == 0)
903     {
904         HeapFree(GetProcessHeap(), 0, This);
905         released_obj();
906     }
907 
908     return ref;
909 }
910 
911 static HRESULT WINAPI InternetExplorerManager_CreateObject(IInternetExplorerManager *iface, DWORD config, LPCWSTR url, REFIID riid, void **ppv)
912 {
913     FIXME("(%p)->(0x%x, %s, %s, %p) stub!\n", iface, config, debugstr_w(url), debugstr_guid(riid), ppv);
914 
915     return E_NOTIMPL;
916 }
917 
918 static const IInternetExplorerManagerVtbl InternetExplorerManager_vtbl =
919 {
920     InternetExplorerManager_QueryInterface,
921     InternetExplorerManager_AddRef,
922     InternetExplorerManager_Release,
923     InternetExplorerManager_CreateObject,
924 };
925 
926 HRESULT WINAPI InternetExplorerManager_Create(IClassFactory *iface, IUnknown *pOuter, REFIID riid, void **ppv)
927 {
928     InternetExplorerManager *ret;
929     HRESULT hr;
930 
931     TRACE("(%p %s %p)\n", pOuter, debugstr_guid(riid), ppv);
932 
933     if (!(ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret))))
934         return E_OUTOFMEMORY;
935 
936     ret->IInternetExplorerManager_iface.lpVtbl = &InternetExplorerManager_vtbl;
937     ret->ref = 1;
938 
939     hr = IInternetExplorerManager_QueryInterface(&ret->IInternetExplorerManager_iface, riid, ppv);
940     IInternetExplorerManager_Release(&ret->IInternetExplorerManager_iface);
941 
942     InterlockedIncrement(&obj_cnt);
943     return hr;
944 }
945 
946 void released_obj(void)
947 {
948     if(!InterlockedDecrement(&obj_cnt))
949         PostQuitMessage(0);
950 }
951 
952 static BOOL create_ie_window(BOOL nohome, const WCHAR *cmdline)
953 {
954     InternetExplorer *ie;
955     HRESULT hres;
956 
957     hres = create_ie(&ie);
958     if(FAILED(hres))
959         return FALSE;
960 
961     IWebBrowser2_put_Visible(&ie->IWebBrowser2_iface, VARIANT_TRUE);
962     IWebBrowser2_put_MenuBar(&ie->IWebBrowser2_iface, VARIANT_TRUE);
963 
964     if(!*cmdline) {
965         if (nohome)
966             ie->nohome = TRUE;
967         else
968             IWebBrowser2_GoHome(&ie->IWebBrowser2_iface);
969     }else {
970         VARIANT var_url;
971         int cmdlen;
972 
973         cmdlen = strlenW(cmdline);
974         if(cmdlen > 2 && cmdline[0] == '"' && cmdline[cmdlen-1] == '"') {
975             cmdline++;
976             cmdlen -= 2;
977         }
978 
979         V_VT(&var_url) = VT_BSTR;
980         V_BSTR(&var_url) = SysAllocStringLen(cmdline, cmdlen);
981 
982         /* navigate to the first page */
983         IWebBrowser2_Navigate2(&ie->IWebBrowser2_iface, &var_url, NULL, NULL, NULL, NULL);
984 
985         SysFreeString(V_BSTR(&var_url));
986     }
987 
988     IWebBrowser2_Release(&ie->IWebBrowser2_iface);
989     return TRUE;
990 }
991 
992 static HDDEDATA open_dde_url(WCHAR *dde_url)
993 {
994     InternetExplorer *ie = NULL, *iter;
995     WCHAR *url, *url_end;
996     VARIANT urlv;
997     HRESULT hres;
998 
999     TRACE("%s\n", debugstr_w(dde_url));
1000 
1001     url = dde_url;
1002     if(*url == '"') {
1003         url++;
1004         url_end = strchrW(url, '"');
1005         if(!url_end) {
1006             FIXME("missing string terminator\n");
1007             return 0;
1008         }
1009         *url_end = 0;
1010     }else {
1011         url_end = strchrW(url, ',');
1012         if(url_end)
1013             *url_end = 0;
1014         else
1015             url_end = url + strlenW(url);
1016     }
1017 
1018     LIST_FOR_EACH_ENTRY(iter, &ie_list, InternetExplorer, entry) {
1019         if(iter->nohome) {
1020             IWebBrowser2_AddRef(&iter->IWebBrowser2_iface);
1021             ie = iter;
1022             break;
1023         }
1024     }
1025 
1026     if(!ie) {
1027         hres = create_ie(&ie);
1028         if(FAILED(hres))
1029             return 0;
1030     }
1031 
1032     IWebBrowser2_put_Visible(&ie->IWebBrowser2_iface, VARIANT_TRUE);
1033     IWebBrowser2_put_MenuBar(&ie->IWebBrowser2_iface, VARIANT_TRUE);
1034 
1035     V_VT(&urlv) = VT_BSTR;
1036     V_BSTR(&urlv) = SysAllocStringLen(url, url_end-url);
1037     if(!V_BSTR(&urlv)) {
1038         IWebBrowser2_Release(&ie->IWebBrowser2_iface);
1039         return 0;
1040     }
1041 
1042     hres = IWebBrowser2_Navigate2(&ie->IWebBrowser2_iface, &urlv, NULL, NULL, NULL, NULL);
1043     if(FAILED(hres))
1044         return 0;
1045 
1046     IWebBrowser2_Release(&ie->IWebBrowser2_iface);
1047     return ULongToHandle(DDE_FACK);
1048 }
1049 
1050 static HDDEDATA WINAPI dde_proc(UINT type, UINT uFmt, HCONV hConv, HSZ hsz1, HSZ hsz2, HDDEDATA data,
1051         ULONG_PTR dwData1, ULONG_PTR dwData2)
1052 {
1053     switch(type) {
1054     case XTYP_CONNECT:
1055         TRACE("XTYP_CONNECT %p\n", hsz1);
1056         return ULongToHandle(!DdeCmpStringHandles(hsz1, ddestr_openurl));
1057 
1058     case XTYP_EXECUTE: {
1059         WCHAR *url;
1060         DWORD size;
1061         HDDEDATA ret;
1062 
1063         TRACE("XTYP_EXECUTE %p\n", data);
1064 
1065         size = DdeGetData(data, NULL, 0, 0);
1066         if(!size) {
1067             WARN("size = 0\n");
1068             break;
1069         }
1070 
1071         url = heap_alloc(size);
1072         if(!url)
1073             break;
1074 
1075         if(DdeGetData(data, (BYTE*)url, size, 0) != size) {
1076             ERR("error during read\n");
1077             heap_free(url);
1078             break;
1079         }
1080 
1081         ret = open_dde_url(url);
1082 
1083         heap_free(url);
1084         return ret;
1085     }
1086 
1087     case XTYP_REQUEST:
1088         FIXME("XTYP_REQUEST\n");
1089         break;
1090 
1091     default:
1092         TRACE("type %d\n", type);
1093     }
1094 
1095     return NULL;
1096 }
1097 
1098 static void init_dde(void)
1099 {
1100     UINT res;
1101 
1102     static const WCHAR iexploreW[] = {'I','E','x','p','l','o','r','e',0};
1103     static const WCHAR openurlW[] = {'W','W','W','_','O','p','e','n','U','R','L',0};
1104 
1105     res = DdeInitializeW(&dde_inst, dde_proc, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
1106     if(res != DMLERR_NO_ERROR) {
1107         WARN("DdeInitialize failed: %u\n", res);
1108         return;
1109     }
1110 
1111     ddestr_iexplore = DdeCreateStringHandleW(dde_inst, iexploreW, CP_WINUNICODE);
1112     if(!ddestr_iexplore)
1113         WARN("Failed to create string handle: %u\n", DdeGetLastError(dde_inst));
1114 
1115     ddestr_openurl = DdeCreateStringHandleW(dde_inst, openurlW, CP_WINUNICODE);
1116     if(!ddestr_openurl)
1117         WARN("Failed to create string handle: %u\n", DdeGetLastError(dde_inst));
1118 
1119     if(!DdeNameService(dde_inst, ddestr_iexplore, 0, DNS_REGISTER))
1120         WARN("DdeNameService failed\n");
1121 }
1122 
1123 static void release_dde(void)
1124 {
1125     if(ddestr_iexplore)
1126         DdeNameService(dde_inst, ddestr_iexplore, 0, DNS_UNREGISTER);
1127     if(ddestr_openurl)
1128         DdeFreeStringHandle(dde_inst, ddestr_openurl);
1129     if(ddestr_iexplore)
1130         DdeFreeStringHandle(dde_inst, ddestr_iexplore);
1131     DdeUninitialize(dde_inst);
1132 }
1133 
1134 /******************************************************************
1135  *		IEWinMain            (ieframe.101)
1136  *
1137  * Only returns on error.
1138  */
1139 DWORD WINAPI IEWinMain(const WCHAR *cmdline, int nShowWindow)
1140 {
1141     MSG msg;
1142     HRESULT hres;
1143     BOOL embedding = FALSE, nohome = FALSE, manager = FALSE;
1144     DWORD reg_cookie;
1145 
1146     static const WCHAR embeddingW[] = {'-','e','m','b','e','d','d','i','n','g',0};
1147     static const WCHAR nohomeW[] = {'-','n','o','h','o','m','e',0};
1148     static const WCHAR startmanagerW[] = {'-','s','t','a','r','t','m','a','n','a','g','e','r',0};
1149 
1150     TRACE("%s %d\n", debugstr_w(cmdline), nShowWindow);
1151 
1152     CoInitialize(NULL);
1153 
1154     init_dde();
1155 
1156     while (*cmdline)
1157     {
1158         int length = 0;
1159 
1160         while (*cmdline == ' ' || *cmdline == '\t') cmdline++;
1161         if (!*cmdline) break;
1162 
1163         while (cmdline[length] && cmdline[length] != ' ' && cmdline[length] != '\t') length++;
1164 
1165         if (!strncmpiW(cmdline, embeddingW, length))
1166             embedding = TRUE;
1167         else if (!strncmpiW(cmdline, nohomeW, length))
1168             nohome = TRUE;
1169         else if (!strncmpiW(cmdline, startmanagerW, length))
1170             manager = TRUE;
1171         else
1172             break;
1173 
1174         cmdline += length;
1175     }
1176 
1177     if (manager)
1178         hres = CoRegisterClassObject(&CLSID_InternetExplorerManager,
1179             (IUnknown*)&InternetExplorerManagerFactory, CLSCTX_SERVER,
1180             REGCLS_SINGLEUSE, &reg_cookie);
1181     else
1182         hres = CoRegisterClassObject(&CLSID_InternetExplorer,
1183             (IUnknown*)&InternetExplorerFactory, CLSCTX_SERVER,
1184             REGCLS_MULTIPLEUSE, &reg_cookie);
1185 
1186     if (FAILED(hres))
1187     {
1188         ERR("failed to register CLSID_InternetExplorer%s: %08x\n", manager ? "Manager" : "", hres);
1189         CoUninitialize();
1190         ExitProcess(1);
1191     }
1192 
1193     if (!embedding)
1194     {
1195         if(!create_ie_window(nohome, cmdline))
1196         {
1197             CoUninitialize();
1198             ExitProcess(1);
1199         }
1200     }
1201 
1202     /* run the message loop for this thread */
1203     while (GetMessageW(&msg, 0, 0, 0))
1204     {
1205         TranslateMessage(&msg);
1206         DispatchMessageW(&msg);
1207     }
1208 
1209     CoRevokeClassObject(reg_cookie);
1210     release_dde();
1211 
1212     CoUninitialize();
1213 
1214     ExitProcess(0);
1215     return 0;
1216 }
1217