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