dmsynth: Dump data passed to Download method.
[wine] / dlls / ieframe / iexplore.c
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
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(ieframe);
46
47 #define IDI_APPICON 1
48
49 #define WM_UPDATEADDRBAR    (WM_APP+1)
50
51 static const WCHAR szIEWinFrame[] = { 'I','E','F','r','a','m','e',0 };
52
53 /* Windows uses "Microsoft Internet Explorer" */
54 static const WCHAR wszWineInternetExplorer[] =
55         {'W','i','n','e',' ','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',0};
56
57 static LONG obj_cnt;
58 static DWORD dde_inst;
59 static HSZ ddestr_iexplore, ddestr_openurl;
60 static struct list ie_list = LIST_INIT(ie_list);
61
62 HRESULT update_ie_statustext(InternetExplorer* This, LPCWSTR text)
63 {
64     if(!SendMessageW(This->status_hwnd, SB_SETTEXTW, MAKEWORD(SB_SIMPLEID, 0), (LPARAM)text))
65         return E_FAIL;
66
67     return S_OK;
68 }
69
70 static void adjust_ie_docobj_rect(HWND frame, RECT* rc)
71 {
72     HWND hwndRebar = GetDlgItem(frame, IDC_BROWSE_REBAR);
73     HWND hwndStatus = GetDlgItem(frame, IDC_BROWSE_STATUSBAR);
74     INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);
75
76     rc->top += barHeight;
77     rc->bottom -= 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 static void add_tb_separator(HWND hwnd)
406 {
407     TBBUTTON btn;
408
409     ZeroMemory(&btn, sizeof(btn));
410
411     btn.iBitmap = 3;
412     btn.fsStyle = BTNS_SEP;
413     SendMessageW(hwnd, TB_ADDBUTTONSW, 1, (LPARAM)&btn);
414 }
415
416 static void add_tb_button(HWND hwnd, int bmp, int cmd, int strId)
417 {
418     TBBUTTON btn;
419     WCHAR buf[30];
420
421     LoadStringW(ieframe_instance, strId, buf, sizeof(buf)/sizeof(buf[0]));
422
423     btn.iBitmap = bmp;
424     btn.idCommand = cmd;
425     btn.fsState = TBSTATE_ENABLED;
426     btn.fsStyle = BTNS_SHOWTEXT;
427     btn.dwData = 0;
428     btn.iString = (INT_PTR)buf;
429
430     SendMessageW(hwnd, TB_ADDBUTTONSW, 1, (LPARAM)&btn);
431 }
432
433 static void create_rebar(HWND hwnd)
434 {
435     HWND hwndRebar;
436     HWND hwndAddress;
437     HWND hwndToolbar;
438     REBARINFO rebarinf;
439     REBARBANDINFOW bandinf;
440     WCHAR addr[40];
441     HIMAGELIST imagelist;
442     WCHAR idb_ietoolbar[] = {'I','D','B','_','I','E','T','O','O','L','B','A','R',0};
443     SIZE toolbar_size;
444
445     LoadStringW(ieframe_instance, IDS_ADDRESS, addr, sizeof(addr)/sizeof(addr[0]));
446
447     hwndRebar = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
448             WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP|CCS_NODIVIDER, 0, 0, 0, 0,
449             hwnd, (HMENU)IDC_BROWSE_REBAR, ieframe_instance, NULL);
450
451     rebarinf.cbSize = sizeof(rebarinf);
452     rebarinf.fMask = 0;
453     rebarinf.himl = NULL;
454
455     SendMessageW(hwndRebar, RB_SETBARINFO, 0, (LPARAM)&rebarinf);
456
457     hwndToolbar = CreateWindowExW(TBSTYLE_EX_MIXEDBUTTONS, TOOLBARCLASSNAMEW, NULL, TBSTYLE_FLAT | WS_CHILD | WS_VISIBLE | CCS_NORESIZE,
458             0, 0, 0, 0, hwndRebar, (HMENU)IDC_BROWSE_TOOLBAR, ieframe_instance, NULL);
459
460     imagelist = ImageList_LoadImageW(ieframe_instance, idb_ietoolbar, 32, 0, CLR_NONE, IMAGE_BITMAP, LR_CREATEDIBSECTION);
461
462     SendMessageW(hwndToolbar, TB_SETIMAGELIST, 0, (LPARAM)imagelist);
463     SendMessageW(hwndToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
464     add_tb_button(hwndToolbar, 0, ID_BROWSE_BACK, IDS_TB_BACK);
465     add_tb_button(hwndToolbar, 1, ID_BROWSE_FORWARD, IDS_TB_FORWARD);
466     add_tb_button(hwndToolbar, 2, ID_BROWSE_STOP, IDS_TB_STOP);
467     add_tb_button(hwndToolbar, 3, ID_BROWSE_REFRESH, IDS_TB_REFRESH);
468     add_tb_button(hwndToolbar, 4, ID_BROWSE_HOME, IDS_TB_HOME);
469     add_tb_separator(hwndToolbar);
470     add_tb_button(hwndToolbar, 5, ID_BROWSE_PRINT, IDS_TB_PRINT);
471     SendMessageW(hwndToolbar, TB_SETBUTTONSIZE, 0, MAKELPARAM(55,50));
472     SendMessageW(hwndToolbar, TB_GETMAXSIZE, 0, (LPARAM)&toolbar_size);
473
474     bandinf.cbSize = sizeof(bandinf);
475     bandinf.fMask = RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE;
476     bandinf.fStyle = RBBS_CHILDEDGE;
477     bandinf.cxMinChild = toolbar_size.cx;
478     bandinf.cyMinChild = toolbar_size.cy+2;
479     bandinf.hwndChild = hwndToolbar;
480
481     SendMessageW(hwndRebar, RB_INSERTBANDW, -1, (LPARAM)&bandinf);
482
483     hwndAddress = CreateWindowExW(0, WC_COMBOBOXEXW, NULL, WS_BORDER|WS_CHILD|WS_VISIBLE|CBS_DROPDOWN,
484             0, 0, 100,20,hwndRebar, (HMENU)IDC_BROWSE_ADDRESSBAR, ieframe_instance, NULL);
485
486     bandinf.fMask |= RBBIM_TEXT;
487     bandinf.fStyle = RBBS_CHILDEDGE | RBBS_BREAK;
488     bandinf.lpText = addr;
489     bandinf.cxMinChild = 100;
490     bandinf.cyMinChild = 20;
491     bandinf.hwndChild = hwndAddress;
492
493     SendMessageW(hwndRebar, RB_INSERTBANDW, -1, (LPARAM)&bandinf);
494 }
495
496 static LRESULT iewnd_OnCreate(HWND hwnd, LPCREATESTRUCTW lpcs)
497 {
498     InternetExplorer* This = (InternetExplorer*)lpcs->lpCreateParams;
499     SetWindowLongPtrW(hwnd, 0, (LONG_PTR) lpcs->lpCreateParams);
500
501     This->menu = create_ie_menu();
502
503     This->status_hwnd = CreateStatusWindowW(WS_VISIBLE|WS_CHILD|SBT_NOBORDERS|CCS_NODIVIDER,
504                                             NULL, hwnd, IDC_BROWSE_STATUSBAR);
505     SendMessageW(This->status_hwnd, SB_SIMPLE, TRUE, 0);
506
507     create_rebar(hwnd);
508
509     return 0;
510 }
511
512 static LRESULT iewnd_OnSize(InternetExplorer *This, INT width, INT height)
513 {
514     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
515     INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);
516     RECT docarea = {0, 0, width, height};
517
518     SendMessageW(This->status_hwnd, WM_SIZE, 0, 0);
519
520     adjust_ie_docobj_rect(This->frame_hwnd, &docarea);
521
522     if(This->doc_host->doc_host.hwnd)
523         SetWindowPos(This->doc_host->doc_host.hwnd, NULL, docarea.left, docarea.top, docarea.right, docarea.bottom,
524                      SWP_NOZORDER | SWP_NOACTIVATE);
525
526     SetWindowPos(hwndRebar, NULL, 0, 0, width, barHeight, SWP_NOZORDER | SWP_NOACTIVATE);
527
528     return 0;
529 }
530
531 static LRESULT iewnd_OnNotify(InternetExplorer *This, WPARAM wparam, LPARAM lparam)
532 {
533     NMHDR* hdr = (NMHDR*)lparam;
534
535     if(hdr->idFrom == IDC_BROWSE_ADDRESSBAR && hdr->code == CBEN_ENDEDITW)
536     {
537         NMCBEENDEDITW* info = (NMCBEENDEDITW*)lparam;
538
539         if(info->fChanged && info->iWhy == CBENF_RETURN)
540         {
541             VARIANT vt;
542
543             V_VT(&vt) = VT_BSTR;
544             V_BSTR(&vt) = SysAllocString(info->szText);
545
546             IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &vt, NULL, NULL, NULL, NULL);
547
548             SysFreeString(V_BSTR(&vt));
549
550             return 0;
551         }
552     }
553
554     if(hdr->idFrom == IDC_BROWSE_REBAR && hdr->code == RBN_HEIGHTCHANGE)
555     {
556         RECT docarea;
557
558         GetClientRect(This->frame_hwnd, &docarea);
559         adjust_ie_docobj_rect(This->frame_hwnd, &docarea);
560
561         if(This->doc_host->doc_host.hwnd)
562             SetWindowPos(This->doc_host->doc_host.hwnd, NULL, docarea.left, docarea.top, docarea.right, docarea.bottom,
563                     SWP_NOZORDER | SWP_NOACTIVATE);
564     }
565
566     return 0;
567 }
568
569 static LRESULT iewnd_OnDestroy(InternetExplorer *This)
570 {
571     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
572     HWND hwndToolbar = GetDlgItem(hwndRebar, IDC_BROWSE_TOOLBAR);
573     HIMAGELIST list = (HIMAGELIST)SendMessageW(hwndToolbar, TB_GETIMAGELIST, 0, 0);
574
575     TRACE("%p\n", This);
576
577     free_fav_menu_data(get_fav_menu(This->menu));
578     ImageList_Destroy(list);
579     This->frame_hwnd = NULL;
580
581     return 0;
582 }
583
584 static LRESULT iewnd_OnCommand(InternetExplorer *This, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
585 {
586     switch(LOWORD(wparam))
587     {
588         case ID_BROWSE_OPEN:
589             DialogBoxParamW(ieframe_instance, MAKEINTRESOURCEW(IDD_BROWSE_OPEN), hwnd, ie_dialog_open_proc, (LPARAM)This);
590             break;
591
592         case ID_BROWSE_PRINT:
593             if(This->doc_host->doc_host.document)
594             {
595                 IOleCommandTarget* target;
596
597                 if(FAILED(IUnknown_QueryInterface(This->doc_host->doc_host.document, &IID_IOleCommandTarget, (LPVOID*)&target)))
598                     break;
599
600                 IOleCommandTarget_Exec(target, &CGID_MSHTML, IDM_PRINT, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
601
602                 IOleCommandTarget_Release(target);
603             }
604             break;
605
606         case ID_BROWSE_HOME:
607             IWebBrowser2_GoHome(&This->IWebBrowser2_iface);
608             break;
609
610         case ID_BROWSE_BACK:
611             IWebBrowser2_GoBack(&This->IWebBrowser2_iface);
612             break;
613
614         case ID_BROWSE_FORWARD:
615             IWebBrowser2_GoForward(&This->IWebBrowser2_iface);
616             break;
617
618         case ID_BROWSE_STOP:
619             IWebBrowser2_Stop(&This->IWebBrowser2_iface);
620             break;
621
622         case ID_BROWSE_REFRESH:
623             IWebBrowser2_Refresh(&This->IWebBrowser2_iface);
624             break;
625
626         case ID_BROWSE_ABOUT:
627             ie_dialog_about(hwnd);
628             break;
629
630         case ID_BROWSE_QUIT:
631             ShowWindow(hwnd, SW_HIDE);
632             break;
633
634         default:
635             if(LOWORD(wparam) >= ID_BROWSE_GOTOFAV_FIRST && LOWORD(wparam) <= ID_BROWSE_GOTOFAV_MAX)
636             {
637                 LPCWSTR url = get_fav_url_from_id(get_fav_menu(This->menu), LOWORD(wparam));
638
639                 if(url)
640                     ie_navigate(This, url);
641             }
642             return DefWindowProcW(hwnd, msg, wparam, lparam);
643     }
644     return 0;
645 }
646
647 static LRESULT update_addrbar(InternetExplorer *This, LPARAM lparam)
648 {
649     HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
650     HWND hwndAddress = GetDlgItem(hwndRebar, IDC_BROWSE_ADDRESSBAR);
651     HWND hwndEdit = (HWND)SendMessageW(hwndAddress, CBEM_GETEDITCONTROL, 0, 0);
652     LPCWSTR url = (LPCWSTR)lparam;
653
654     SendMessageW(hwndEdit, WM_SETTEXT, 0, (LPARAM)url);
655
656     return 0;
657 }
658
659 static LRESULT WINAPI ie_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
660 {
661     InternetExplorer *This = (InternetExplorer*) GetWindowLongPtrW(hwnd, 0);
662
663     switch (msg)
664     {
665     case WM_CREATE:
666         return iewnd_OnCreate(hwnd, (LPCREATESTRUCTW)lparam);
667     case WM_CLOSE:
668         TRACE("WM_CLOSE\n");
669         ShowWindow(hwnd, SW_HIDE);
670         return 0;
671     case WM_SHOWWINDOW:
672         TRACE("WM_SHOWWINDOW %lx\n", wparam);
673         if(wparam)
674             IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
675         else
676             IWebBrowser2_Release(&This->IWebBrowser2_iface);
677         break;
678     case WM_DESTROY:
679         return iewnd_OnDestroy(This);
680     case WM_SIZE:
681         return iewnd_OnSize(This, LOWORD(lparam), HIWORD(lparam));
682     case WM_COMMAND:
683         return iewnd_OnCommand(This, hwnd, msg, wparam, lparam);
684     case WM_NOTIFY:
685         return iewnd_OnNotify(This, wparam, lparam);
686     case WM_DOCHOSTTASK:
687         return process_dochost_tasks(&This->doc_host->doc_host);
688     case WM_UPDATEADDRBAR:
689         return update_addrbar(This, lparam);
690     }
691     return DefWindowProcW(hwnd, msg, wparam, lparam);
692 }
693
694 void register_iewindow_class(void)
695 {
696     WNDCLASSEXW wc;
697
698     memset(&wc, 0, sizeof wc);
699     wc.cbSize = sizeof(wc);
700     wc.style = 0;
701     wc.lpfnWndProc = ie_window_proc;
702     wc.cbClsExtra = 0;
703     wc.cbWndExtra = sizeof(InternetExplorer*);
704     wc.hInstance = ieframe_instance;
705     wc.hIcon = LoadIconW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON));
706     wc.hIconSm = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON,
707                             GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
708     wc.hCursor = LoadCursorW(0, MAKEINTRESOURCEW(IDC_ARROW));
709     wc.hbrBackground = 0;
710     wc.lpszClassName = szIEWinFrame;
711     wc.lpszMenuName = NULL;
712
713     RegisterClassExW(&wc);
714 }
715
716 void unregister_iewindow_class(void)
717 {
718     UnregisterClassW(szIEWinFrame, ieframe_instance);
719 }
720
721 static void create_frame_hwnd(InternetExplorer *This)
722 {
723     This->frame_hwnd = CreateWindowExW(
724             WS_EX_WINDOWEDGE,
725             szIEWinFrame, wszWineInternetExplorer,
726             WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
727                 | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
728             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
729             NULL, NULL /* FIXME */, ieframe_instance, This);
730
731     This->doc_host->doc_host.frame_hwnd = This->frame_hwnd;
732     create_doc_view_hwnd(&This->doc_host->doc_host);
733 }
734
735 static inline IEDocHost *impl_from_DocHost(DocHost *iface)
736 {
737     return CONTAINING_RECORD(iface, IEDocHost, doc_host);
738 }
739
740 static ULONG IEDocHost_addref(DocHost *iface)
741 {
742     IEDocHost *This = impl_from_DocHost(iface);
743     LONG ref = InterlockedIncrement(&This->ref);
744
745     TRACE("(%p) ref=%d\n", This, ref);
746
747     return ref;
748 }
749
750 static ULONG IEDocHost_release(DocHost *iface)
751 {
752     IEDocHost *This = impl_from_DocHost(iface);
753     LONG ref = InterlockedDecrement(&This->ref);
754
755     TRACE("(%p) ref=%d\n", This, ref);
756
757     if(!ref) {
758         if(This->ie)
759             ERR("This->ie is not NULL\n");
760         heap_free(This);
761     }
762
763     return ref;
764 }
765
766 static void WINAPI DocHostContainer_GetDocObjRect(DocHost* This, RECT* rc)
767 {
768     GetClientRect(This->frame_hwnd, rc);
769     adjust_ie_docobj_rect(This->frame_hwnd, rc);
770 }
771
772 static HRESULT WINAPI DocHostContainer_SetStatusText(DocHost *iface, LPCWSTR text)
773 {
774     IEDocHost *This = impl_from_DocHost(iface);
775     return update_ie_statustext(This->ie, text);
776 }
777
778 static void WINAPI DocHostContainer_SetURL(DocHost* iface, LPCWSTR url)
779 {
780     IEDocHost *This = impl_from_DocHost(iface);
781
782     if(!This->ie)
783         return;
784
785     This->ie->nohome = FALSE;
786     SendMessageW(This->ie->frame_hwnd, WM_UPDATEADDRBAR, 0, (LPARAM)url);
787 }
788
789 static HRESULT DocHostContainer_exec(DocHost* This, const GUID *cmd_group, DWORD cmdid, DWORD execopt, VARIANT *in,
790         VARIANT *out)
791 {
792     return E_NOTIMPL;
793 }
794
795 static const IDocHostContainerVtbl DocHostContainerVtbl = {
796     IEDocHost_addref,
797     IEDocHost_release,
798     DocHostContainer_GetDocObjRect,
799     DocHostContainer_SetStatusText,
800     DocHostContainer_SetURL,
801     DocHostContainer_exec
802 };
803
804 static HRESULT create_ie(InternetExplorer **ret_obj)
805 {
806     InternetExplorer *ret;
807
808     ret = heap_alloc_zero(sizeof(InternetExplorer));
809     if(!ret)
810         return E_OUTOFMEMORY;
811
812     ret->doc_host = heap_alloc_zero(sizeof(IEDocHost));
813     if(!ret->doc_host) {
814         heap_free(ret);
815         return E_OUTOFMEMORY;
816     }
817
818     ret->ref = 1;
819     ret->doc_host->ref = 1;
820     ret->doc_host->ie = ret;
821
822     DocHost_Init(&ret->doc_host->doc_host, &ret->IWebBrowser2_iface, &DocHostContainerVtbl);
823
824     InternetExplorer_WebBrowser_Init(ret);
825
826     HlinkFrame_Init(&ret->hlink_frame, (IUnknown*)&ret->IWebBrowser2_iface, &ret->doc_host->doc_host);
827
828     create_frame_hwnd(ret);
829
830     InterlockedIncrement(&obj_cnt);
831     list_add_tail(&ie_list, &ret->entry);
832     *ret_obj = ret;
833     return S_OK;
834 }
835
836 HRESULT WINAPI InternetExplorer_Create(IClassFactory *iface, IUnknown *pOuter, REFIID riid, void **ppv)
837 {
838     InternetExplorer *ret;
839     HRESULT hres;
840
841     TRACE("(%p %s %p)\n", pOuter, debugstr_guid(riid), ppv);
842
843     hres = create_ie(&ret);
844     if(FAILED(hres))
845         return hres;
846
847     hres = IWebBrowser2_QueryInterface(&ret->IWebBrowser2_iface, riid, ppv);
848     IWebBrowser2_Release(&ret->IWebBrowser2_iface);
849     if(FAILED(hres))
850         return hres;
851
852     return S_OK;
853 }
854
855 void released_obj(void)
856 {
857     if(!InterlockedDecrement(&obj_cnt))
858         PostQuitMessage(0);
859 }
860
861 static BOOL create_ie_window(const WCHAR *cmdline)
862 {
863     InternetExplorer *ie;
864     HRESULT hres;
865
866     hres = create_ie(&ie);
867     if(FAILED(hres))
868         return FALSE;
869
870     IWebBrowser2_put_Visible(&ie->IWebBrowser2_iface, VARIANT_TRUE);
871     IWebBrowser2_put_MenuBar(&ie->IWebBrowser2_iface, VARIANT_TRUE);
872
873     if(!*cmdline) {
874         IWebBrowser2_GoHome(&ie->IWebBrowser2_iface);
875     }else {
876         VARIANT var_url;
877         int cmdlen;
878
879         static const WCHAR nohomeW[] = {'-','n','o','h','o','m','e'};
880
881         while(*cmdline == ' ' || *cmdline == '\t')
882             cmdline++;
883         cmdlen = strlenW(cmdline);
884         if(cmdlen > 2 && cmdline[0] == '"' && cmdline[cmdlen-1] == '"') {
885             cmdline++;
886             cmdlen -= 2;
887         }
888
889         if(cmdlen == sizeof(nohomeW)/sizeof(*nohomeW) && !memcmp(cmdline, nohomeW, sizeof(nohomeW))) {
890             ie->nohome = TRUE;
891         }else {
892             V_VT(&var_url) = VT_BSTR;
893
894             V_BSTR(&var_url) = SysAllocStringLen(cmdline, cmdlen);
895
896             /* navigate to the first page */
897             IWebBrowser2_Navigate2(&ie->IWebBrowser2_iface, &var_url, NULL, NULL, NULL, NULL);
898
899             SysFreeString(V_BSTR(&var_url));
900         }
901     }
902
903     IWebBrowser2_Release(&ie->IWebBrowser2_iface);
904     return TRUE;
905 }
906
907 static HDDEDATA open_dde_url(WCHAR *dde_url)
908 {
909     InternetExplorer *ie = NULL, *iter;
910     WCHAR *url, *url_end;
911     VARIANT urlv;
912     HRESULT hres;
913
914     TRACE("%s\n", debugstr_w(dde_url));
915
916     url = dde_url;
917     if(*url == '"') {
918         url++;
919         url_end = strchrW(url, '"');
920         if(!url_end) {
921             FIXME("missing string terminator\n");
922             return 0;
923         }
924         *url_end = 0;
925     }else {
926         url_end = strchrW(url, ',');
927         if(url_end)
928             *url_end = 0;
929         else
930             url_end = url + strlenW(url);
931     }
932
933     LIST_FOR_EACH_ENTRY(iter, &ie_list, InternetExplorer, entry) {
934         if(iter->nohome) {
935             IWebBrowser2_AddRef(&iter->IWebBrowser2_iface);
936             ie = iter;
937             break;
938         }
939     }
940
941     if(!ie) {
942         hres = create_ie(&ie);
943         if(FAILED(hres))
944             return 0;
945     }
946
947     IWebBrowser2_put_Visible(&ie->IWebBrowser2_iface, VARIANT_TRUE);
948     IWebBrowser2_put_MenuBar(&ie->IWebBrowser2_iface, VARIANT_TRUE);
949
950     V_VT(&urlv) = VT_BSTR;
951     V_BSTR(&urlv) = SysAllocStringLen(url, url_end-url);
952     if(!V_BSTR(&urlv)) {
953         IWebBrowser2_Release(&ie->IWebBrowser2_iface);
954         return 0;
955     }
956
957     hres = IWebBrowser2_Navigate2(&ie->IWebBrowser2_iface, &urlv, NULL, NULL, NULL, NULL);
958     if(FAILED(hres))
959         return 0;
960
961     IWebBrowser2_Release(&ie->IWebBrowser2_iface);
962     return ULongToHandle(DDE_FACK);
963 }
964
965 static HDDEDATA WINAPI dde_proc(UINT type, UINT uFmt, HCONV hConv, HSZ hsz1, HSZ hsz2, HDDEDATA data,
966         ULONG_PTR dwData1, ULONG_PTR dwData2)
967 {
968     switch(type) {
969     case XTYP_CONNECT:
970         TRACE("XTYP_CONNECT %p\n", hsz1);
971         return ULongToHandle(!DdeCmpStringHandles(hsz1, ddestr_openurl));
972
973     case XTYP_EXECUTE: {
974         WCHAR *url;
975         DWORD size;
976         HDDEDATA ret;
977
978         TRACE("XTYP_EXECUTE %p\n", data);
979
980         size = DdeGetData(data, NULL, 0, 0);
981         if(!size) {
982             WARN("size = 0\n");
983             break;
984         }
985
986         url = heap_alloc(size);
987         if(!url)
988             break;
989
990         if(DdeGetData(data, (BYTE*)url, size, 0) != size) {
991             ERR("error during read\n");
992             heap_free(url);
993             break;
994         }
995
996         ret = open_dde_url(url);
997
998         heap_free(url);
999         return ret;
1000     }
1001
1002     case XTYP_REQUEST:
1003         FIXME("XTYP_REQUEST\n");
1004         break;
1005
1006     default:
1007         TRACE("type %d\n", type);
1008     }
1009
1010     return NULL;
1011 }
1012
1013 static void init_dde(void)
1014 {
1015     UINT res;
1016
1017     static const WCHAR iexploreW[] = {'I','E','x','p','l','o','r','e',0};
1018     static const WCHAR openurlW[] = {'W','W','W','_','O','p','e','n','U','R','L',0};
1019
1020     res = DdeInitializeW(&dde_inst, dde_proc, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
1021     if(res != DMLERR_NO_ERROR) {
1022         WARN("DdeInitialize failed: %u\n", res);
1023         return;
1024     }
1025
1026     ddestr_iexplore = DdeCreateStringHandleW(dde_inst, iexploreW, CP_WINUNICODE);
1027     if(!ddestr_iexplore)
1028         WARN("Failed to create string handle: %u\n", DdeGetLastError(dde_inst));
1029
1030     ddestr_openurl = DdeCreateStringHandleW(dde_inst, openurlW, CP_WINUNICODE);
1031     if(!ddestr_openurl)
1032         WARN("Failed to create string handle: %u\n", DdeGetLastError(dde_inst));
1033
1034     res = HandleToULong(DdeNameService(dde_inst, ddestr_iexplore, 0, DNS_REGISTER));
1035     if(res != DMLERR_NO_ERROR)
1036         WARN("DdeNameService failed: %u\n", res);
1037 }
1038
1039 static void release_dde(void)
1040 {
1041     if(ddestr_iexplore)
1042         DdeNameService(dde_inst, ddestr_iexplore, 0, DNS_UNREGISTER);
1043     if(ddestr_openurl)
1044         DdeFreeStringHandle(dde_inst, ddestr_openurl);
1045     if(ddestr_iexplore)
1046         DdeFreeStringHandle(dde_inst, ddestr_iexplore);
1047     DdeUninitialize(dde_inst);
1048 }
1049
1050 /******************************************************************
1051  *              IEWinMain            (ieframe.101)
1052  *
1053  * Only returns on error.
1054  */
1055 DWORD WINAPI IEWinMain(const WCHAR *cmdline, int nShowWindow)
1056 {
1057     MSG msg;
1058     HRESULT hres;
1059
1060     static const WCHAR embeddingW[] = {'-','e','m','b','e','d','d','i','n','g',0};
1061
1062     TRACE("%s %d\n", debugstr_w(cmdline), nShowWindow);
1063
1064     CoInitialize(NULL);
1065
1066     hres = register_class_object(TRUE);
1067     if(FAILED(hres)) {
1068         CoUninitialize();
1069         ExitProcess(1);
1070     }
1071
1072     init_dde();
1073
1074     if(strcmpiW(cmdline, embeddingW)) {
1075         if(!create_ie_window(cmdline)) {
1076             CoUninitialize();
1077             ExitProcess(1);
1078         }
1079     }
1080
1081     /* run the message loop for this thread */
1082     while (GetMessageW(&msg, 0, 0, 0))
1083     {
1084         TranslateMessage(&msg);
1085         DispatchMessageW(&msg);
1086     }
1087
1088     register_class_object(FALSE);
1089     release_dde();
1090
1091     CoUninitialize();
1092
1093     ExitProcess(0);
1094     return 0;
1095 }