ieframe: Always create doc view window when creating InternetExplore object instance.
[wine/multimedia.git] / dlls / ieframe / iexplore.c
blobcbab8f554045803c223e849887c3fea3524e4cf5
1 /*
2 * ieframe - Internet Explorer main frame window
4 * Copyright 2006 Mike McCormack (for CodeWeavers)
5 * Copyright 2006 Jacek Caban (for CodeWeavers)
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.
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.
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
22 #define COBJMACROS
24 #include <stdarg.h>
26 #include "ieframe.h"
27 #include "resource.h"
29 #include "winuser.h"
30 #include "wingdi.h"
31 #include "winnls.h"
32 #include "ole2.h"
33 #include "exdisp.h"
34 #include "oleidl.h"
36 #include "mshtmcid.h"
37 #include "shellapi.h"
38 #include "winreg.h"
39 #include "shlwapi.h"
40 #include "intshcut.h"
41 #include "ddeml.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(ieframe);
47 #define IDI_APPICON 1
49 #define WM_UPDATEADDRBAR (WM_APP+1)
51 static const WCHAR szIEWinFrame[] = { 'I','E','F','r','a','m','e',0 };
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};
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);
62 HRESULT update_ie_statustext(InternetExplorer* This, LPCWSTR text)
64 if(!SendMessageW(This->status_hwnd, SB_SETTEXTW, MAKEWORD(SB_SIMPLEID, 0), (LPARAM)text))
65 return E_FAIL;
67 return S_OK;
70 static void adjust_ie_docobj_rect(HWND frame, RECT* rc)
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);
76 rc->top += barHeight;
77 rc->bottom -= barHeight;
79 if(IsWindowVisible(hwndStatus))
81 RECT statusrc;
83 GetClientRect(hwndStatus, &statusrc);
84 rc->bottom -= statusrc.bottom - statusrc.top;
88 static HMENU get_tb_menu(HMENU menu)
90 HMENU menu_view = GetSubMenu(menu, 1);
92 return GetSubMenu(menu_view, 0);
95 static HMENU get_fav_menu(HMENU menu)
97 return GetSubMenu(menu, 2);
100 static LPWSTR get_fav_url_from_id(HMENU menu, UINT id)
102 MENUITEMINFOW item;
104 item.cbSize = sizeof(item);
105 item.fMask = MIIM_DATA;
107 if(!GetMenuItemInfoW(menu, id, FALSE, &item))
108 return NULL;
110 return (LPWSTR)item.dwItemData;
113 static void free_fav_menu_data(HMENU menu)
115 LPWSTR url;
116 int i;
118 for(i = 0; (url = get_fav_url_from_id(menu, ID_BROWSE_GOTOFAV_FIRST + i)); i++)
119 heap_free( url );
122 static int get_menu_item_count(HMENU menu)
124 MENUITEMINFOW item;
125 int count = 0;
126 int i;
128 item.cbSize = sizeof(item);
129 item.fMask = MIIM_DATA | MIIM_SUBMENU;
131 for(i = 0; GetMenuItemInfoW(menu, i, TRUE, &item); i++)
133 if(item.hSubMenu)
134 count += get_menu_item_count(item.hSubMenu);
135 else
136 count++;
139 return count;
142 static void add_fav_to_menu(HMENU favmenu, HMENU menu, LPWSTR title, LPCWSTR url)
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;
149 if(favcount > (ID_BROWSE_GOTOFAV_MAX - ID_BROWSE_GOTOFAV_FIRST))
151 FIXME("Add support for more than %d Favorites\n", favcount);
152 return;
155 urlbuf = heap_alloc((lstrlenW(url) + 1) * sizeof(WCHAR));
157 if(!urlbuf)
158 return;
160 lstrcpyW(urlbuf, url);
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);
171 static void add_favs_to_menu(HMENU favmenu, HMENU menu, LPCWSTR dir)
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;
182 lstrcpyW(path, dir);
183 PathAppendW(path, search);
185 findhandle = FindFirstFileW(path, &finddata);
187 if(findhandle == INVALID_HANDLE_VALUE)
188 return;
190 res = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, &IID_IUniformResourceLocatorW, (PVOID*)&urlobj);
192 if(SUCCEEDED(res))
193 res = IUnknown_QueryInterface(urlobj, &IID_IPersistFile, (PVOID*)&urlfile);
195 if(SUCCEEDED(res))
197 filename = path + lstrlenW(path) - lstrlenW(search);
201 lstrcpyW(filename, finddata.cFileName);
203 if(finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
205 MENUITEMINFOW item;
206 const WCHAR ignore1[] = {'.','.',0};
207 const WCHAR ignore2[] = {'.',0};
209 if(!lstrcmpW(filename, ignore1) || !lstrcmpW(filename, ignore2))
210 continue;
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
220 WCHAR* fileext;
221 WCHAR* url = NULL;
222 const WCHAR urlext[] = {'.','u','r','l',0};
224 if(lstrcmpiW(PathFindExtensionW(filename), urlext))
225 continue;
227 if(FAILED(IPersistFile_Load(urlfile, path, 0)))
228 continue;
230 urlobj->lpVtbl->GetURL(urlobj, &url);
232 if(!url)
233 continue;
235 fileext = filename + lstrlenW(filename) - lstrlenW(urlext);
236 *fileext = 0;
237 add_fav_to_menu(favmenu, menu, filename, url);
239 } while(FindNextFileW(findhandle, &finddata));
242 if(urlfile)
243 IPersistFile_Release(urlfile);
245 if(urlobj)
246 IUnknown_Release(urlobj);
248 FindClose(findhandle);
251 static void add_tbs_to_menu(HMENU menu)
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};
260 if(SHRegOpenUSKeyW(toolbar_key, KEY_READ, NULL, &toolbar_handle, TRUE) == ERROR_SUCCESS)
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;
269 if(SHRegOpenUSKeyW(classes_key, KEY_READ, NULL, &classes_handle, TRUE) != ERROR_SUCCESS)
271 SHRegCloseUSKey(toolbar_handle);
272 ERR("Failed to open key %s\n", debugstr_w(classes_key));
273 return;
276 for(i = 0; SHRegEnumUSValueW(toolbar_handle, i, guid, &value_len, NULL, NULL, NULL, SHREGENUM_HKLM) == ERROR_SUCCESS; i++)
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]);
285 if(lstrlenW(guid) != 38)
287 TRACE("Found invalid IE toolbar entry: %s\n", debugstr_w(guid));
288 continue;
291 if(SHRegOpenUSKeyW(guid, KEY_READ, classes_handle, &tb_class_handle, TRUE) != ERROR_SUCCESS)
293 ERR("Failed to get class info for %s\n", debugstr_w(guid));
294 continue;
297 ret = SHRegQueryUSValueW(tb_class_handle, NULL, NULL, tb_name, &tb_name_len, TRUE, NULL, 0);
299 SHRegCloseUSKey(tb_class_handle);
301 if(ret != ERROR_SUCCESS)
303 ERR("Failed to get toolbar name for %s\n", debugstr_w(guid));
304 continue;
307 item.cbSize = sizeof(item);
308 item.fMask = MIIM_STRING;
309 item.dwTypeData = tb_name;
310 InsertMenuItemW(menu, GetMenuItemCount(menu), TRUE, &item);
313 SHRegCloseUSKey(classes_handle);
314 SHRegCloseUSKey(toolbar_handle);
318 static HMENU create_ie_menu(void)
320 HMENU menu = LoadMenuW(ieframe_instance, MAKEINTRESOURCEW(IDR_BROWSE_MAIN_MENU));
321 HMENU favmenu = get_fav_menu(menu);
322 WCHAR path[MAX_PATH];
324 add_tbs_to_menu(get_tb_menu(menu));
326 if(SHGetFolderPathW(NULL, CSIDL_COMMON_FAVORITES, NULL, SHGFP_TYPE_CURRENT, path) == S_OK)
327 add_favs_to_menu(favmenu, favmenu, path);
329 if(SHGetFolderPathW(NULL, CSIDL_FAVORITES, NULL, SHGFP_TYPE_CURRENT, path) == S_OK)
330 add_favs_to_menu(favmenu, favmenu, path);
332 return menu;
335 static void ie_navigate(InternetExplorer* This, LPCWSTR url)
337 VARIANT variant;
339 V_VT(&variant) = VT_BSTR;
340 V_BSTR(&variant) = SysAllocString(url);
342 IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &variant, NULL, NULL, NULL, NULL);
344 SysFreeString(V_BSTR(&variant));
347 static INT_PTR CALLBACK ie_dialog_open_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
349 static InternetExplorer* This;
351 switch(msg)
353 case WM_INITDIALOG:
354 This = (InternetExplorer*)lparam;
355 EnableWindow(GetDlgItem(hwnd, IDOK), FALSE);
356 return TRUE;
358 case WM_COMMAND:
359 switch(LOWORD(wparam))
361 case IDC_BROWSE_OPEN_URL:
363 HWND hwndurl = GetDlgItem(hwnd, IDC_BROWSE_OPEN_URL);
364 int len = GetWindowTextLengthW(hwndurl);
366 EnableWindow(GetDlgItem(hwnd, IDOK), len ? TRUE : FALSE);
367 break;
369 case IDOK:
371 HWND hwndurl = GetDlgItem(hwnd, IDC_BROWSE_OPEN_URL);
372 int len = GetWindowTextLengthW(hwndurl);
374 if(len)
376 VARIANT url;
378 V_VT(&url) = VT_BSTR;
379 V_BSTR(&url) = SysAllocStringLen(NULL, len);
381 GetWindowTextW(hwndurl, V_BSTR(&url), len + 1);
382 IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &url, NULL, NULL, NULL, NULL);
384 SysFreeString(V_BSTR(&url));
387 /* fall through */
388 case IDCANCEL:
389 EndDialog(hwnd, wparam);
390 return TRUE;
393 return FALSE;
396 static void ie_dialog_about(HWND hwnd)
398 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON, 48, 48, LR_SHARED);
400 ShellAboutW(hwnd, wszWineInternetExplorer, NULL, icon);
402 DestroyIcon(icon);
405 static void add_tb_separator(HWND hwnd)
407 TBBUTTON btn;
409 ZeroMemory(&btn, sizeof(btn));
411 btn.iBitmap = 3;
412 btn.fsStyle = BTNS_SEP;
413 SendMessageW(hwnd, TB_ADDBUTTONSW, 1, (LPARAM)&btn);
416 static void add_tb_button(HWND hwnd, int bmp, int cmd, int strId)
418 TBBUTTON btn;
419 WCHAR buf[30];
421 LoadStringW(ieframe_instance, strId, buf, sizeof(buf)/sizeof(buf[0]));
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;
430 SendMessageW(hwnd, TB_ADDBUTTONSW, 1, (LPARAM)&btn);
433 static void create_rebar(HWND hwnd)
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};
444 LoadStringW(ieframe_instance, IDS_ADDRESS, addr, sizeof(addr)/sizeof(addr[0]));
446 hwndRebar = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
447 WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP|CCS_NODIVIDER, 0, 0, 0, 0,
448 hwnd, (HMENU)IDC_BROWSE_REBAR, ieframe_instance, NULL);
450 rebarinf.cbSize = sizeof(rebarinf);
451 rebarinf.fMask = 0;
452 rebarinf.himl = NULL;
453 rebarinf.cbSize = sizeof(rebarinf);
455 SendMessageW(hwndRebar, RB_SETBARINFO, 0, (LPARAM)&rebarinf);
457 hwndToolbar = CreateWindowExW(TBSTYLE_EX_MIXEDBUTTONS, TOOLBARCLASSNAMEW, NULL, TBSTYLE_FLAT | WS_CHILD | WS_VISIBLE,
458 0, 0, 0, 0, hwndRebar, (HMENU)IDC_BROWSE_TOOLBAR, ieframe_instance, NULL);
460 imagelist = ImageList_LoadImageW(ieframe_instance, idb_ietoolbar, 32, 0, CLR_NONE, IMAGE_BITMAP, LR_CREATEDIBSECTION);
462 SendMessageW(hwndToolbar, TB_SETIMAGELIST, 0, (LPARAM)imagelist);
463 SendMessageW(hwndToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
464 add_tb_button(hwndToolbar, 0, 0, IDS_TB_BACK);
465 add_tb_button(hwndToolbar, 1, 0, IDS_TB_FORWARD);
466 add_tb_button(hwndToolbar, 2, 0, IDS_TB_STOP);
467 add_tb_button(hwndToolbar, 3, 0, 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_AUTOSIZE, 0, 0);
474 bandinf.cbSize = sizeof(bandinf);
475 bandinf.fMask = RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE | RBBIM_SIZE;
476 bandinf.fStyle = RBBS_CHILDEDGE;
477 bandinf.cx = 100;
478 bandinf.cyMinChild = 52;
479 bandinf.hwndChild = hwndToolbar;
481 SendMessageW(hwndRebar, RB_INSERTBANDW, -1, (LPARAM)&bandinf);
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);
486 bandinf.fMask |= RBBIM_TEXT;
487 bandinf.fStyle = RBBS_CHILDEDGE | RBBS_BREAK;
488 bandinf.lpText = addr;
489 bandinf.cyMinChild = 20;
490 bandinf.hwndChild = hwndAddress;
492 SendMessageW(hwndRebar, RB_INSERTBANDW, -1, (LPARAM)&bandinf);
495 static LRESULT iewnd_OnCreate(HWND hwnd, LPCREATESTRUCTW lpcs)
497 InternetExplorer* This = (InternetExplorer*)lpcs->lpCreateParams;
498 SetWindowLongPtrW(hwnd, 0, (LONG_PTR) lpcs->lpCreateParams);
500 This->menu = create_ie_menu();
502 This->status_hwnd = CreateStatusWindowW(WS_VISIBLE|WS_CHILD|SBT_NOBORDERS|CCS_NODIVIDER,
503 NULL, hwnd, IDC_BROWSE_STATUSBAR);
504 SendMessageW(This->status_hwnd, SB_SIMPLE, TRUE, 0);
506 create_rebar(hwnd);
508 return 0;
511 static LRESULT iewnd_OnSize(InternetExplorer *This, INT width, INT height)
513 HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
514 INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);
515 RECT docarea = {0, 0, width, height};
517 SendMessageW(This->status_hwnd, WM_SIZE, 0, 0);
519 adjust_ie_docobj_rect(This->frame_hwnd, &docarea);
521 if(This->doc_host->doc_host.hwnd)
522 SetWindowPos(This->doc_host->doc_host.hwnd, NULL, docarea.left, docarea.top, docarea.right, docarea.bottom,
523 SWP_NOZORDER | SWP_NOACTIVATE);
525 SetWindowPos(hwndRebar, NULL, 0, 0, width, barHeight, SWP_NOZORDER | SWP_NOACTIVATE);
527 return 0;
530 static LRESULT iewnd_OnNotify(InternetExplorer *This, WPARAM wparam, LPARAM lparam)
532 NMHDR* hdr = (NMHDR*)lparam;
534 if(hdr->idFrom == IDC_BROWSE_ADDRESSBAR && hdr->code == CBEN_ENDEDITW)
536 NMCBEENDEDITW* info = (NMCBEENDEDITW*)lparam;
538 if(info->fChanged && info->iWhy == CBENF_RETURN)
540 VARIANT vt;
542 V_VT(&vt) = VT_BSTR;
543 V_BSTR(&vt) = SysAllocString(info->szText);
545 IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &vt, NULL, NULL, NULL, NULL);
547 SysFreeString(V_BSTR(&vt));
549 return 0;
553 return 0;
556 static LRESULT iewnd_OnDestroy(InternetExplorer *This)
558 HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
559 HWND hwndToolbar = GetDlgItem(hwndRebar, IDC_BROWSE_TOOLBAR);
560 HIMAGELIST list = (HIMAGELIST)SendMessageW(hwndToolbar, TB_GETIMAGELIST, 0, 0);
562 TRACE("%p\n", This);
564 free_fav_menu_data(get_fav_menu(This->menu));
565 ImageList_Destroy(list);
566 This->frame_hwnd = NULL;
568 return 0;
571 static LRESULT iewnd_OnCommand(InternetExplorer *This, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
573 switch(LOWORD(wparam))
575 case ID_BROWSE_OPEN:
576 DialogBoxParamW(ieframe_instance, MAKEINTRESOURCEW(IDD_BROWSE_OPEN), hwnd, ie_dialog_open_proc, (LPARAM)This);
577 break;
579 case ID_BROWSE_PRINT:
580 if(This->doc_host->doc_host.document)
582 IOleCommandTarget* target;
584 if(FAILED(IUnknown_QueryInterface(This->doc_host->doc_host.document, &IID_IOleCommandTarget, (LPVOID*)&target)))
585 break;
587 IOleCommandTarget_Exec(target, &CGID_MSHTML, IDM_PRINT, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
589 IOleCommandTarget_Release(target);
591 break;
593 case ID_BROWSE_HOME:
594 IWebBrowser2_GoHome(&This->IWebBrowser2_iface);
595 break;
597 case ID_BROWSE_ABOUT:
598 ie_dialog_about(hwnd);
599 break;
601 case ID_BROWSE_QUIT:
602 ShowWindow(hwnd, SW_HIDE);
603 break;
605 default:
606 if(LOWORD(wparam) >= ID_BROWSE_GOTOFAV_FIRST && LOWORD(wparam) <= ID_BROWSE_GOTOFAV_MAX)
608 LPCWSTR url = get_fav_url_from_id(get_fav_menu(This->menu), LOWORD(wparam));
610 if(url)
611 ie_navigate(This, url);
613 return DefWindowProcW(hwnd, msg, wparam, lparam);
615 return 0;
618 static LRESULT update_addrbar(InternetExplorer *This, LPARAM lparam)
620 HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
621 HWND hwndAddress = GetDlgItem(hwndRebar, IDC_BROWSE_ADDRESSBAR);
622 HWND hwndEdit = (HWND)SendMessageW(hwndAddress, CBEM_GETEDITCONTROL, 0, 0);
623 LPCWSTR url = (LPCWSTR)lparam;
625 SendMessageW(hwndEdit, WM_SETTEXT, 0, (LPARAM)url);
627 return 0;
630 static LRESULT WINAPI ie_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
632 InternetExplorer *This = (InternetExplorer*) GetWindowLongPtrW(hwnd, 0);
634 switch (msg)
636 case WM_CREATE:
637 return iewnd_OnCreate(hwnd, (LPCREATESTRUCTW)lparam);
638 case WM_CLOSE:
639 TRACE("WM_CLOSE\n");
640 ShowWindow(hwnd, SW_HIDE);
641 return 0;
642 case WM_SHOWWINDOW:
643 TRACE("WM_SHOWWINDOW %lx\n", wparam);
644 if(wparam)
645 IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
646 else
647 IWebBrowser2_Release(&This->IWebBrowser2_iface);
648 break;
649 case WM_DESTROY:
650 return iewnd_OnDestroy(This);
651 case WM_SIZE:
652 return iewnd_OnSize(This, LOWORD(lparam), HIWORD(lparam));
653 case WM_COMMAND:
654 return iewnd_OnCommand(This, hwnd, msg, wparam, lparam);
655 case WM_NOTIFY:
656 return iewnd_OnNotify(This, wparam, lparam);
657 case WM_DOCHOSTTASK:
658 return process_dochost_tasks(&This->doc_host->doc_host);
659 case WM_UPDATEADDRBAR:
660 return update_addrbar(This, lparam);
662 return DefWindowProcW(hwnd, msg, wparam, lparam);
665 void register_iewindow_class(void)
667 WNDCLASSEXW wc;
669 memset(&wc, 0, sizeof wc);
670 wc.cbSize = sizeof(wc);
671 wc.style = 0;
672 wc.lpfnWndProc = ie_window_proc;
673 wc.cbClsExtra = 0;
674 wc.cbWndExtra = sizeof(InternetExplorer*);
675 wc.hInstance = ieframe_instance;
676 wc.hIcon = LoadIconW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON));
677 wc.hIconSm = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON,
678 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
679 wc.hCursor = LoadCursorW(0, MAKEINTRESOURCEW(IDC_ARROW));
680 wc.hbrBackground = 0;
681 wc.lpszClassName = szIEWinFrame;
682 wc.lpszMenuName = NULL;
684 RegisterClassExW(&wc);
687 void unregister_iewindow_class(void)
689 UnregisterClassW(szIEWinFrame, ieframe_instance);
692 static void create_frame_hwnd(InternetExplorer *This)
694 This->frame_hwnd = CreateWindowExW(
695 WS_EX_WINDOWEDGE,
696 szIEWinFrame, wszWineInternetExplorer,
697 WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
698 | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
699 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
700 NULL, NULL /* FIXME */, ieframe_instance, This);
702 This->doc_host->doc_host.frame_hwnd = This->frame_hwnd;
703 create_doc_view_hwnd(&This->doc_host->doc_host);
706 static inline IEDocHost *impl_from_DocHost(DocHost *iface)
708 return CONTAINING_RECORD(iface, IEDocHost, doc_host);
711 static ULONG IEDocHost_addref(DocHost *iface)
713 IEDocHost *This = impl_from_DocHost(iface);
714 LONG ref = InterlockedIncrement(&This->ref);
716 TRACE("(%p) ref=%d\n", This, ref);
718 return ref;
721 static ULONG IEDocHost_release(DocHost *iface)
723 IEDocHost *This = impl_from_DocHost(iface);
724 LONG ref = InterlockedDecrement(&This->ref);
726 TRACE("(%p) ref=%d\n", This, ref);
728 if(!ref) {
729 if(This->ie)
730 ERR("This->ie is not NULL\n");
731 heap_free(This);
734 return ref;
737 static void WINAPI DocHostContainer_GetDocObjRect(DocHost* This, RECT* rc)
739 GetClientRect(This->frame_hwnd, rc);
740 adjust_ie_docobj_rect(This->frame_hwnd, rc);
743 static HRESULT WINAPI DocHostContainer_SetStatusText(DocHost *iface, LPCWSTR text)
745 IEDocHost *This = impl_from_DocHost(iface);
746 return update_ie_statustext(This->ie, text);
749 static void WINAPI DocHostContainer_SetURL(DocHost* iface, LPCWSTR url)
751 IEDocHost *This = impl_from_DocHost(iface);
753 if(!This->ie)
754 return;
756 This->ie->nohome = FALSE;
757 SendMessageW(This->ie->frame_hwnd, WM_UPDATEADDRBAR, 0, (LPARAM)url);
760 static HRESULT DocHostContainer_exec(DocHost* This, const GUID *cmd_group, DWORD cmdid, DWORD execopt, VARIANT *in,
761 VARIANT *out)
763 return S_OK;
765 static const IDocHostContainerVtbl DocHostContainerVtbl = {
766 IEDocHost_addref,
767 IEDocHost_release,
768 DocHostContainer_GetDocObjRect,
769 DocHostContainer_SetStatusText,
770 DocHostContainer_SetURL,
771 DocHostContainer_exec
774 static HRESULT create_ie(InternetExplorer **ret_obj)
776 InternetExplorer *ret;
778 ret = heap_alloc_zero(sizeof(InternetExplorer));
779 if(!ret)
780 return E_OUTOFMEMORY;
782 ret->doc_host = heap_alloc_zero(sizeof(IEDocHost));
783 if(!ret->doc_host) {
784 heap_free(ret);
785 return E_OUTOFMEMORY;
788 ret->ref = 1;
789 ret->doc_host->ref = 1;
790 ret->doc_host->ie = ret;
792 DocHost_Init(&ret->doc_host->doc_host, (IDispatch*)&ret->IWebBrowser2_iface, &DocHostContainerVtbl);
794 InternetExplorer_WebBrowser_Init(ret);
796 HlinkFrame_Init(&ret->hlink_frame, (IUnknown*)&ret->IWebBrowser2_iface, &ret->doc_host->doc_host);
798 create_frame_hwnd(ret);
800 InterlockedIncrement(&obj_cnt);
801 list_add_tail(&ie_list, &ret->entry);
802 *ret_obj = ret;
803 return S_OK;
806 HRESULT WINAPI InternetExplorer_Create(IClassFactory *iface, IUnknown *pOuter, REFIID riid, void **ppv)
808 InternetExplorer *ret;
809 HRESULT hres;
811 TRACE("(%p %s %p)\n", pOuter, debugstr_guid(riid), ppv);
813 hres = create_ie(&ret);
814 if(FAILED(hres))
815 return hres;
817 hres = IWebBrowser2_QueryInterface(&ret->IWebBrowser2_iface, riid, ppv);
818 IWebBrowser2_Release(&ret->IWebBrowser2_iface);
819 if(FAILED(hres))
820 return hres;
822 return S_OK;
825 void released_obj(void)
827 if(!InterlockedDecrement(&obj_cnt))
828 PostQuitMessage(0);
831 static BOOL create_ie_window(const WCHAR *cmdline)
833 InternetExplorer *ie;
834 HRESULT hres;
836 hres = create_ie(&ie);
837 if(FAILED(hres))
838 return FALSE;
840 IWebBrowser2_put_Visible(&ie->IWebBrowser2_iface, VARIANT_TRUE);
841 IWebBrowser2_put_MenuBar(&ie->IWebBrowser2_iface, VARIANT_TRUE);
843 if(!*cmdline) {
844 IWebBrowser2_GoHome(&ie->IWebBrowser2_iface);
845 }else {
846 VARIANT var_url;
847 int cmdlen;
849 static const WCHAR nohomeW[] = {'-','n','o','h','o','m','e'};
851 while(*cmdline == ' ' || *cmdline == '\t')
852 cmdline++;
853 cmdlen = strlenW(cmdline);
854 if(cmdlen > 2 && cmdline[0] == '"' && cmdline[cmdlen-1] == '"') {
855 cmdline++;
856 cmdlen -= 2;
859 if(cmdlen == sizeof(nohomeW)/sizeof(*nohomeW) && !memcmp(cmdline, nohomeW, sizeof(nohomeW))) {
860 ie->nohome = TRUE;
861 }else {
862 V_VT(&var_url) = VT_BSTR;
864 V_BSTR(&var_url) = SysAllocStringLen(cmdline, cmdlen);
866 /* navigate to the first page */
867 IWebBrowser2_Navigate2(&ie->IWebBrowser2_iface, &var_url, NULL, NULL, NULL, NULL);
869 SysFreeString(V_BSTR(&var_url));
873 IWebBrowser2_Release(&ie->IWebBrowser2_iface);
874 return TRUE;
877 static HDDEDATA open_dde_url(WCHAR *dde_url)
879 InternetExplorer *ie = NULL, *iter;
880 WCHAR *url, *url_end;
881 VARIANT urlv;
882 HRESULT hres;
884 TRACE("%s\n", debugstr_w(dde_url));
886 url = dde_url;
887 if(*url == '"') {
888 url++;
889 url_end = strchrW(url, '"');
890 if(!url_end) {
891 FIXME("missing string terminator\n");
892 return 0;
894 *url_end = 0;
895 }else {
896 url_end = strchrW(url, ',');
897 if(url_end)
898 *url_end = 0;
899 else
900 url_end = url + strlenW(url);
903 LIST_FOR_EACH_ENTRY(iter, &ie_list, InternetExplorer, entry) {
904 if(iter->nohome) {
905 IWebBrowser2_AddRef(&iter->IWebBrowser2_iface);
906 ie = iter;
907 break;
911 if(!ie) {
912 hres = create_ie(&ie);
913 if(FAILED(hres))
914 return 0;
917 IWebBrowser2_put_Visible(&ie->IWebBrowser2_iface, VARIANT_TRUE);
918 IWebBrowser2_put_MenuBar(&ie->IWebBrowser2_iface, VARIANT_TRUE);
920 V_VT(&urlv) = VT_BSTR;
921 V_BSTR(&urlv) = SysAllocStringLen(url, url_end-url);
922 if(!V_BSTR(&urlv)) {
923 IWebBrowser2_Release(&ie->IWebBrowser2_iface);
924 return 0;
927 hres = IWebBrowser2_Navigate2(&ie->IWebBrowser2_iface, &urlv, NULL, NULL, NULL, NULL);
928 if(FAILED(hres))
929 return 0;
931 IWebBrowser2_Release(&ie->IWebBrowser2_iface);
932 return ULongToHandle(DDE_FACK);
935 static HDDEDATA WINAPI dde_proc(UINT type, UINT uFmt, HCONV hConv, HSZ hsz1, HSZ hsz2, HDDEDATA data,
936 ULONG_PTR dwData1, ULONG_PTR dwData2)
938 switch(type) {
939 case XTYP_CONNECT:
940 TRACE("XTYP_CONNECT %p\n", hsz1);
941 return ULongToHandle(!DdeCmpStringHandles(hsz1, ddestr_openurl));
943 case XTYP_EXECUTE: {
944 WCHAR *url;
945 DWORD size;
946 HDDEDATA ret;
948 TRACE("XTYP_EXECUTE %p\n", data);
950 size = DdeGetData(data, NULL, 0, 0);
951 if(!size) {
952 WARN("size = 0\n");
953 break;
956 url = heap_alloc(size);
957 if(!url)
958 break;
960 if(DdeGetData(data, (BYTE*)url, size, 0) != size) {
961 ERR("error during read\n");
962 break;
965 ret = open_dde_url(url);
967 heap_free(url);
968 return ret;
971 case XTYP_REQUEST:
972 FIXME("XTYP_REQUEST\n");
973 break;
975 default:
976 TRACE("type %d\n", type);
979 return NULL;
982 static void init_dde(void)
984 UINT res;
986 static const WCHAR iexploreW[] = {'I','E','x','p','l','o','r','e',0};
987 static const WCHAR openurlW[] = {'W','W','W','_','O','p','e','n','U','R','L',0};
989 res = DdeInitializeW(&dde_inst, dde_proc, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
990 if(res != DMLERR_NO_ERROR) {
991 WARN("DdeInitialize failed: %u\n", res);
992 return;
995 ddestr_iexplore = DdeCreateStringHandleW(dde_inst, iexploreW, CP_WINUNICODE);
996 if(!ddestr_iexplore)
997 WARN("Failed to create string handle: %u\n", DdeGetLastError(dde_inst));
999 ddestr_openurl = DdeCreateStringHandleW(dde_inst, openurlW, CP_WINUNICODE);
1000 if(!ddestr_openurl)
1001 WARN("Failed to create string handle: %u\n", DdeGetLastError(dde_inst));
1003 res = HandleToULong(DdeNameService(dde_inst, ddestr_iexplore, 0, DNS_REGISTER));
1004 if(res != DMLERR_NO_ERROR)
1005 WARN("DdeNameService failed: %u\n", res);
1008 static void release_dde(void)
1010 if(ddestr_iexplore)
1011 DdeNameService(dde_inst, ddestr_iexplore, 0, DNS_UNREGISTER);
1012 if(ddestr_openurl)
1013 DdeFreeStringHandle(dde_inst, ddestr_openurl);
1014 if(ddestr_iexplore)
1015 DdeFreeStringHandle(dde_inst, ddestr_iexplore);
1016 DdeUninitialize(dde_inst);
1019 /******************************************************************
1020 * IEWinMain (ieframe.101)
1022 * Only returns on error.
1024 DWORD WINAPI IEWinMain(const WCHAR *cmdline, int nShowWindow)
1026 MSG msg;
1027 HRESULT hres;
1029 static const WCHAR embeddingW[] = {'-','e','m','b','e','d','d','i','n','g',0};
1031 TRACE("%s %d\n", debugstr_w(cmdline), nShowWindow);
1033 CoInitialize(NULL);
1035 hres = register_class_object(TRUE);
1036 if(FAILED(hres)) {
1037 CoUninitialize();
1038 ExitProcess(1);
1041 init_dde();
1043 if(strcmpiW(cmdline, embeddingW)) {
1044 if(!create_ie_window(cmdline)) {
1045 CoUninitialize();
1046 ExitProcess(1);
1050 /* run the message loop for this thread */
1051 while (GetMessageW(&msg, 0, 0, 0))
1053 TranslateMessage(&msg);
1054 DispatchMessageW(&msg);
1057 register_class_object(FALSE);
1058 release_dde();
1060 CoUninitialize();
1062 ExitProcess(0);
1063 return 0;