user32: Add a helper function for copying bits from a window surface.
[wine.git] / dlls / ieframe / iexplore.c
blob11b79adcc4ce0d2c7718d8f17e13171f2d58ada8
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 InflateRect(rc, 0, -barHeight);
78 if(IsWindowVisible(hwndStatus))
80 RECT statusrc;
82 GetClientRect(hwndStatus, &statusrc);
83 rc->bottom -= statusrc.bottom - statusrc.top;
87 static HMENU get_tb_menu(HMENU menu)
89 HMENU menu_view = GetSubMenu(menu, 1);
91 return GetSubMenu(menu_view, 0);
94 static HMENU get_fav_menu(HMENU menu)
96 return GetSubMenu(menu, 2);
99 static LPWSTR get_fav_url_from_id(HMENU menu, UINT id)
101 MENUITEMINFOW item;
103 item.cbSize = sizeof(item);
104 item.fMask = MIIM_DATA;
106 if(!GetMenuItemInfoW(menu, id, FALSE, &item))
107 return NULL;
109 return (LPWSTR)item.dwItemData;
112 static void free_fav_menu_data(HMENU menu)
114 LPWSTR url;
115 int i;
117 for(i = 0; (url = get_fav_url_from_id(menu, ID_BROWSE_GOTOFAV_FIRST + i)); i++)
118 heap_free( url );
121 static int get_menu_item_count(HMENU menu)
123 MENUITEMINFOW item;
124 int count = 0;
125 int i;
127 item.cbSize = sizeof(item);
128 item.fMask = MIIM_DATA | MIIM_SUBMENU;
130 for(i = 0; GetMenuItemInfoW(menu, i, TRUE, &item); i++)
132 if(item.hSubMenu)
133 count += get_menu_item_count(item.hSubMenu);
134 else
135 count++;
138 return count;
141 static void add_fav_to_menu(HMENU favmenu, HMENU menu, LPWSTR title, LPCWSTR url)
143 MENUITEMINFOW item;
144 /* Subtract the number of standard elements in the Favorites menu */
145 int favcount = get_menu_item_count(favmenu) - 2;
146 LPWSTR urlbuf;
148 if(favcount > (ID_BROWSE_GOTOFAV_MAX - ID_BROWSE_GOTOFAV_FIRST))
150 FIXME("Add support for more than %d Favorites\n", favcount);
151 return;
154 urlbuf = heap_alloc((lstrlenW(url) + 1) * sizeof(WCHAR));
156 if(!urlbuf)
157 return;
159 lstrcpyW(urlbuf, url);
161 item.cbSize = sizeof(item);
162 item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA | MIIM_ID;
163 item.fType = MFT_STRING;
164 item.dwTypeData = title;
165 item.wID = ID_BROWSE_GOTOFAV_FIRST + favcount;
166 item.dwItemData = (ULONG_PTR)urlbuf;
167 InsertMenuItemW(menu, -1, TRUE, &item);
170 static void add_favs_to_menu(HMENU favmenu, HMENU menu, LPCWSTR dir)
172 WCHAR path[MAX_PATH*2];
173 const WCHAR search[] = {'*',0};
174 WCHAR* filename;
175 HANDLE findhandle;
176 WIN32_FIND_DATAW finddata;
177 IUniformResourceLocatorW* urlobj;
178 IPersistFile* urlfile = NULL;
179 HRESULT res;
181 lstrcpyW(path, dir);
182 PathAppendW(path, search);
184 findhandle = FindFirstFileW(path, &finddata);
186 if(findhandle == INVALID_HANDLE_VALUE)
187 return;
189 res = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, &IID_IUniformResourceLocatorW, (PVOID*)&urlobj);
191 if(SUCCEEDED(res))
192 res = IUnknown_QueryInterface((IUnknown*)urlobj, &IID_IPersistFile, (PVOID*)&urlfile);
194 if(SUCCEEDED(res))
196 filename = path + lstrlenW(path) - lstrlenW(search);
200 lstrcpyW(filename, finddata.cFileName);
202 if(finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
204 MENUITEMINFOW item;
205 const WCHAR ignore1[] = {'.','.',0};
206 const WCHAR ignore2[] = {'.',0};
208 if(!lstrcmpW(filename, ignore1) || !lstrcmpW(filename, ignore2))
209 continue;
211 item.cbSize = sizeof(item);
212 item.fMask = MIIM_STRING | MIIM_SUBMENU;
213 item.dwTypeData = filename;
214 item.hSubMenu = CreatePopupMenu();
215 InsertMenuItemW(menu, -1, TRUE, &item);
216 add_favs_to_menu(favmenu, item.hSubMenu, path);
217 } else
219 WCHAR* fileext;
220 WCHAR* url = NULL;
221 const WCHAR urlext[] = {'.','u','r','l',0};
223 if(lstrcmpiW(PathFindExtensionW(filename), urlext))
224 continue;
226 if(FAILED(IPersistFile_Load(urlfile, path, 0)))
227 continue;
229 urlobj->lpVtbl->GetURL(urlobj, &url);
231 if(!url)
232 continue;
234 fileext = filename + lstrlenW(filename) - lstrlenW(urlext);
235 *fileext = 0;
236 add_fav_to_menu(favmenu, menu, filename, url);
238 } while(FindNextFileW(findhandle, &finddata));
241 if(urlfile)
242 IPersistFile_Release(urlfile);
244 if(urlobj)
245 IUnknown_Release((IUnknown*)urlobj);
247 FindClose(findhandle);
250 static void add_tbs_to_menu(HMENU menu)
252 HUSKEY toolbar_handle;
253 WCHAR toolbar_key[] = {'S','o','f','t','w','a','r','e','\\',
254 'M','i','c','r','o','s','o','f','t','\\',
255 'I','n','t','e','r','n','e','t',' ',
256 'E','x','p','l','o','r','e','r','\\',
257 'T','o','o','l','b','a','r',0};
259 if(SHRegOpenUSKeyW(toolbar_key, KEY_READ, NULL, &toolbar_handle, TRUE) == ERROR_SUCCESS)
261 HUSKEY classes_handle;
262 WCHAR classes_key[] = {'S','o','f','t','w','a','r','e','\\',
263 'C','l','a','s','s','e','s','\\','C','L','S','I','D',0};
264 WCHAR guid[39];
265 DWORD value_len = sizeof(guid)/sizeof(guid[0]);
266 int i;
268 if(SHRegOpenUSKeyW(classes_key, KEY_READ, NULL, &classes_handle, TRUE) != ERROR_SUCCESS)
270 SHRegCloseUSKey(toolbar_handle);
271 ERR("Failed to open key %s\n", debugstr_w(classes_key));
272 return;
275 for(i = 0; SHRegEnumUSValueW(toolbar_handle, i, guid, &value_len, NULL, NULL, NULL, SHREGENUM_HKLM) == ERROR_SUCCESS; i++)
277 WCHAR tb_name[100];
278 DWORD tb_name_len = sizeof(tb_name)/sizeof(tb_name[0]);
279 HUSKEY tb_class_handle;
280 MENUITEMINFOW item;
281 LSTATUS ret;
282 value_len = sizeof(guid)/sizeof(guid[0]);
284 if(lstrlenW(guid) != 38)
286 TRACE("Found invalid IE toolbar entry: %s\n", debugstr_w(guid));
287 continue;
290 if(SHRegOpenUSKeyW(guid, KEY_READ, classes_handle, &tb_class_handle, TRUE) != ERROR_SUCCESS)
292 ERR("Failed to get class info for %s\n", debugstr_w(guid));
293 continue;
296 ret = SHRegQueryUSValueW(tb_class_handle, NULL, NULL, tb_name, &tb_name_len, TRUE, NULL, 0);
298 SHRegCloseUSKey(tb_class_handle);
300 if(ret != ERROR_SUCCESS)
302 ERR("Failed to get toolbar name for %s\n", debugstr_w(guid));
303 continue;
306 item.cbSize = sizeof(item);
307 item.fMask = MIIM_STRING;
308 item.dwTypeData = tb_name;
309 InsertMenuItemW(menu, GetMenuItemCount(menu), TRUE, &item);
312 SHRegCloseUSKey(classes_handle);
313 SHRegCloseUSKey(toolbar_handle);
317 static HMENU create_ie_menu(void)
319 HMENU menu = LoadMenuW(ieframe_instance, MAKEINTRESOURCEW(IDR_BROWSE_MAIN_MENU));
320 HMENU favmenu = get_fav_menu(menu);
321 WCHAR path[MAX_PATH];
323 add_tbs_to_menu(get_tb_menu(menu));
325 if(SHGetFolderPathW(NULL, CSIDL_COMMON_FAVORITES, NULL, SHGFP_TYPE_CURRENT, path) == S_OK)
326 add_favs_to_menu(favmenu, favmenu, path);
328 if(SHGetFolderPathW(NULL, CSIDL_FAVORITES, NULL, SHGFP_TYPE_CURRENT, path) == S_OK)
329 add_favs_to_menu(favmenu, favmenu, path);
331 return menu;
334 static void ie_navigate(InternetExplorer* This, LPCWSTR url)
336 VARIANT variant;
338 V_VT(&variant) = VT_BSTR;
339 V_BSTR(&variant) = SysAllocString(url);
341 IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &variant, NULL, NULL, NULL, NULL);
343 SysFreeString(V_BSTR(&variant));
346 static INT_PTR CALLBACK ie_dialog_open_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
348 static InternetExplorer* This;
350 switch(msg)
352 case WM_INITDIALOG:
353 This = (InternetExplorer*)lparam;
354 EnableWindow(GetDlgItem(hwnd, IDOK), FALSE);
355 return TRUE;
357 case WM_COMMAND:
358 switch(LOWORD(wparam))
360 case IDC_BROWSE_OPEN_URL:
362 HWND hwndurl = GetDlgItem(hwnd, IDC_BROWSE_OPEN_URL);
363 int len = GetWindowTextLengthW(hwndurl);
365 EnableWindow(GetDlgItem(hwnd, IDOK), len != 0);
366 break;
368 case IDOK:
370 HWND hwndurl = GetDlgItem(hwnd, IDC_BROWSE_OPEN_URL);
371 int len = GetWindowTextLengthW(hwndurl);
373 if(len)
375 VARIANT url;
377 V_VT(&url) = VT_BSTR;
378 V_BSTR(&url) = SysAllocStringLen(NULL, len);
380 GetWindowTextW(hwndurl, V_BSTR(&url), len + 1);
381 IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &url, NULL, NULL, NULL, NULL);
383 SysFreeString(V_BSTR(&url));
386 /* fall through */
387 case IDCANCEL:
388 EndDialog(hwnd, wparam);
389 return TRUE;
392 return FALSE;
395 static void ie_dialog_about(HWND hwnd)
397 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON, 48, 48, LR_SHARED);
399 ShellAboutW(hwnd, wszWineInternetExplorer, NULL, icon);
401 DestroyIcon(icon);
404 static void add_tb_separator(InternetExplorer *ie)
406 TBBUTTON btn;
408 ZeroMemory(&btn, sizeof(btn));
410 btn.iBitmap = 3;
411 btn.fsStyle = BTNS_SEP;
412 SendMessageW(ie->toolbar_hwnd, TB_ADDBUTTONSW, 1, (LPARAM)&btn);
415 static void add_tb_button(InternetExplorer *ie, int bmp, int cmd, int strId)
417 TBBUTTON btn;
418 WCHAR buf[30];
420 LoadStringW(ieframe_instance, strId, buf, sizeof(buf)/sizeof(buf[0]));
422 btn.iBitmap = bmp;
423 btn.idCommand = cmd;
424 btn.fsState = TBSTATE_ENABLED;
425 btn.fsStyle = BTNS_SHOWTEXT;
426 btn.dwData = 0;
427 btn.iString = (INT_PTR)buf;
429 SendMessageW(ie->toolbar_hwnd, TB_ADDBUTTONSW, 1, (LPARAM)&btn);
432 static void enable_toolbar_button(InternetExplorer *ie, int command, BOOL enable)
434 SendMessageW(ie->toolbar_hwnd, TB_ENABLEBUTTON, command, enable);
437 static void create_rebar(InternetExplorer *ie)
439 HWND hwndRebar;
440 HWND hwndAddress;
441 REBARINFO rebarinf;
442 REBARBANDINFOW bandinf;
443 WCHAR addr[40];
444 HIMAGELIST imagelist;
445 SIZE toolbar_size;
447 LoadStringW(ieframe_instance, IDS_ADDRESS, addr, sizeof(addr)/sizeof(addr[0]));
449 hwndRebar = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
450 WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP|CCS_NODIVIDER, 0, 0, 0, 0,
451 ie->frame_hwnd, (HMENU)IDC_BROWSE_REBAR, ieframe_instance, NULL);
453 rebarinf.cbSize = sizeof(rebarinf);
454 rebarinf.fMask = 0;
455 rebarinf.himl = NULL;
457 SendMessageW(hwndRebar, RB_SETBARINFO, 0, (LPARAM)&rebarinf);
459 ie->toolbar_hwnd = CreateWindowExW(TBSTYLE_EX_MIXEDBUTTONS, TOOLBARCLASSNAMEW, NULL, TBSTYLE_FLAT | WS_CHILD | WS_VISIBLE | CCS_NORESIZE,
460 0, 0, 0, 0, hwndRebar, (HMENU)IDC_BROWSE_TOOLBAR, ieframe_instance, NULL);
462 imagelist = ImageList_LoadImageW(ieframe_instance, MAKEINTRESOURCEW(IDB_IETOOLBAR), 32, 0, CLR_NONE, IMAGE_BITMAP, LR_CREATEDIBSECTION);
464 SendMessageW(ie->toolbar_hwnd, TB_SETIMAGELIST, 0, (LPARAM)imagelist);
465 SendMessageW(ie->toolbar_hwnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
466 add_tb_button(ie, 0, ID_BROWSE_BACK, IDS_TB_BACK);
467 add_tb_button(ie, 1, ID_BROWSE_FORWARD, IDS_TB_FORWARD);
468 add_tb_button(ie, 2, ID_BROWSE_STOP, IDS_TB_STOP);
469 add_tb_button(ie, 3, ID_BROWSE_REFRESH, IDS_TB_REFRESH);
470 add_tb_button(ie, 4, ID_BROWSE_HOME, IDS_TB_HOME);
471 add_tb_separator(ie);
472 add_tb_button(ie, 5, ID_BROWSE_PRINT, IDS_TB_PRINT);
473 SendMessageW(ie->toolbar_hwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(65,50));
474 SendMessageW(ie->toolbar_hwnd, TB_GETMAXSIZE, 0, (LPARAM)&toolbar_size);
476 bandinf.cbSize = sizeof(bandinf);
477 bandinf.fMask = RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE;
478 bandinf.fStyle = RBBS_CHILDEDGE;
479 bandinf.cxMinChild = toolbar_size.cx;
480 bandinf.cyMinChild = toolbar_size.cy+2;
481 bandinf.hwndChild = ie->toolbar_hwnd;
483 SendMessageW(hwndRebar, RB_INSERTBANDW, -1, (LPARAM)&bandinf);
485 hwndAddress = CreateWindowExW(0, WC_COMBOBOXEXW, NULL, WS_BORDER|WS_CHILD|WS_VISIBLE|CBS_DROPDOWN,
486 0, 0, 100,20,hwndRebar, (HMENU)IDC_BROWSE_ADDRESSBAR, ieframe_instance, NULL);
488 bandinf.fMask |= RBBIM_TEXT;
489 bandinf.fStyle = RBBS_CHILDEDGE | RBBS_BREAK;
490 bandinf.lpText = addr;
491 bandinf.cxMinChild = 100;
492 bandinf.cyMinChild = 20;
493 bandinf.hwndChild = hwndAddress;
495 SendMessageW(hwndRebar, RB_INSERTBANDW, -1, (LPARAM)&bandinf);
498 static LRESULT iewnd_OnCreate(HWND hwnd, LPCREATESTRUCTW lpcs)
500 InternetExplorer* This = (InternetExplorer*)lpcs->lpCreateParams;
501 SetWindowLongPtrW(hwnd, 0, (LONG_PTR) lpcs->lpCreateParams);
503 This->doc_host.frame_hwnd = This->frame_hwnd = hwnd;
505 This->menu = create_ie_menu();
507 This->status_hwnd = CreateStatusWindowW(WS_VISIBLE|WS_CHILD|SBT_NOBORDERS|CCS_NODIVIDER,
508 NULL, hwnd, IDC_BROWSE_STATUSBAR);
509 SendMessageW(This->status_hwnd, SB_SIMPLE, TRUE, 0);
511 create_rebar(This);
513 return 0;
516 static LRESULT iewnd_OnSize(InternetExplorer *This, INT width, INT height)
518 HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
519 INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);
520 RECT docarea = {0, 0, width, height};
522 SendMessageW(This->status_hwnd, WM_SIZE, 0, 0);
524 adjust_ie_docobj_rect(This->frame_hwnd, &docarea);
526 if(This->doc_host.hwnd)
527 SetWindowPos(This->doc_host.hwnd, NULL, docarea.left, docarea.top, docarea.right, docarea.bottom,
528 SWP_NOZORDER | SWP_NOACTIVATE);
530 SetWindowPos(hwndRebar, NULL, 0, 0, width, barHeight, SWP_NOZORDER | SWP_NOACTIVATE);
532 return 0;
535 static LRESULT iewnd_OnNotify(InternetExplorer *This, WPARAM wparam, LPARAM lparam)
537 NMHDR* hdr = (NMHDR*)lparam;
539 if(hdr->idFrom == IDC_BROWSE_ADDRESSBAR && hdr->code == CBEN_ENDEDITW)
541 NMCBEENDEDITW* info = (NMCBEENDEDITW*)lparam;
543 if(info->fChanged && info->iWhy == CBENF_RETURN)
545 VARIANT vt;
547 V_VT(&vt) = VT_BSTR;
548 V_BSTR(&vt) = SysAllocString(info->szText);
550 IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &vt, NULL, NULL, NULL, NULL);
552 SysFreeString(V_BSTR(&vt));
554 return 0;
558 if(hdr->idFrom == IDC_BROWSE_REBAR && hdr->code == RBN_HEIGHTCHANGE)
560 RECT docarea;
562 GetClientRect(This->frame_hwnd, &docarea);
563 adjust_ie_docobj_rect(This->frame_hwnd, &docarea);
565 if(This->doc_host.hwnd)
566 SetWindowPos(This->doc_host.hwnd, NULL, docarea.left, docarea.top, docarea.right, docarea.bottom,
567 SWP_NOZORDER | SWP_NOACTIVATE);
570 return 0;
573 static LRESULT iewnd_OnDestroy(InternetExplorer *This)
575 HIMAGELIST list = (HIMAGELIST)SendMessageW(This->toolbar_hwnd, TB_GETIMAGELIST, 0, 0);
577 TRACE("%p\n", This);
579 free_fav_menu_data(get_fav_menu(This->menu));
580 ImageList_Destroy(list);
581 This->frame_hwnd = NULL;
583 return 0;
586 static LRESULT iewnd_OnCommand(InternetExplorer *This, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
588 switch(LOWORD(wparam))
590 case ID_BROWSE_OPEN:
591 DialogBoxParamW(ieframe_instance, MAKEINTRESOURCEW(IDD_BROWSE_OPEN), hwnd, ie_dialog_open_proc, (LPARAM)This);
592 break;
594 case ID_BROWSE_PRINT:
595 if(This->doc_host.document)
597 IOleCommandTarget* target;
599 if(FAILED(IUnknown_QueryInterface(This->doc_host.document, &IID_IOleCommandTarget, (LPVOID*)&target)))
600 break;
602 IOleCommandTarget_Exec(target, &CGID_MSHTML, IDM_PRINT, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
604 IOleCommandTarget_Release(target);
606 break;
608 case ID_BROWSE_HOME:
609 IWebBrowser2_GoHome(&This->IWebBrowser2_iface);
610 break;
612 case ID_BROWSE_BACK:
613 IWebBrowser2_GoBack(&This->IWebBrowser2_iface);
614 break;
616 case ID_BROWSE_FORWARD:
617 IWebBrowser2_GoForward(&This->IWebBrowser2_iface);
618 break;
620 case ID_BROWSE_STOP:
621 IWebBrowser2_Stop(&This->IWebBrowser2_iface);
622 break;
624 case ID_BROWSE_REFRESH:
625 IWebBrowser2_Refresh(&This->IWebBrowser2_iface);
626 break;
628 case ID_BROWSE_ABOUT:
629 ie_dialog_about(hwnd);
630 break;
632 case ID_BROWSE_QUIT:
633 ShowWindow(hwnd, SW_HIDE);
634 break;
636 default:
637 if(LOWORD(wparam) >= ID_BROWSE_GOTOFAV_FIRST && LOWORD(wparam) <= ID_BROWSE_GOTOFAV_MAX)
639 LPCWSTR url = get_fav_url_from_id(get_fav_menu(This->menu), LOWORD(wparam));
641 if(url)
642 ie_navigate(This, url);
644 return DefWindowProcW(hwnd, msg, wparam, lparam);
646 return 0;
649 static LRESULT update_addrbar(InternetExplorer *This, LPARAM lparam)
651 HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
652 HWND hwndAddress = GetDlgItem(hwndRebar, IDC_BROWSE_ADDRESSBAR);
653 HWND hwndEdit = (HWND)SendMessageW(hwndAddress, CBEM_GETEDITCONTROL, 0, 0);
654 LPCWSTR url = (LPCWSTR)lparam;
656 SendMessageW(hwndEdit, WM_SETTEXT, 0, (LPARAM)url);
658 return 0;
661 static LRESULT WINAPI ie_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
663 InternetExplorer *This = (InternetExplorer*) GetWindowLongPtrW(hwnd, 0);
665 switch (msg)
667 case WM_CREATE:
668 return iewnd_OnCreate(hwnd, (LPCREATESTRUCTW)lparam);
669 case WM_CLOSE:
670 TRACE("WM_CLOSE\n");
671 ShowWindow(hwnd, SW_HIDE);
672 return 0;
673 case WM_SHOWWINDOW:
674 TRACE("WM_SHOWWINDOW %lx\n", wparam);
675 if(wparam) {
676 IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
677 InterlockedIncrement(&This->extern_ref);
678 }else {
679 release_extern_ref(This, TRUE);
680 IWebBrowser2_Release(&This->IWebBrowser2_iface);
682 break;
683 case WM_DESTROY:
684 return iewnd_OnDestroy(This);
685 case WM_SIZE:
686 return iewnd_OnSize(This, LOWORD(lparam), HIWORD(lparam));
687 case WM_COMMAND:
688 return iewnd_OnCommand(This, hwnd, msg, wparam, lparam);
689 case WM_NOTIFY:
690 return iewnd_OnNotify(This, wparam, lparam);
691 case WM_DOCHOSTTASK:
692 return process_dochost_tasks(&This->doc_host);
693 case WM_UPDATEADDRBAR:
694 return update_addrbar(This, lparam);
696 return DefWindowProcW(hwnd, msg, wparam, lparam);
699 void register_iewindow_class(void)
701 WNDCLASSEXW wc;
703 memset(&wc, 0, sizeof wc);
704 wc.cbSize = sizeof(wc);
705 wc.style = 0;
706 wc.lpfnWndProc = ie_window_proc;
707 wc.cbClsExtra = 0;
708 wc.cbWndExtra = sizeof(InternetExplorer*);
709 wc.hInstance = ieframe_instance;
710 wc.hIcon = LoadIconW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON));
711 wc.hIconSm = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON,
712 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
713 wc.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
714 wc.hbrBackground = 0;
715 wc.lpszClassName = szIEWinFrame;
716 wc.lpszMenuName = NULL;
718 RegisterClassExW(&wc);
721 void unregister_iewindow_class(void)
723 UnregisterClassW(szIEWinFrame, ieframe_instance);
726 static void create_frame_hwnd(InternetExplorer *This)
728 CreateWindowExW(
729 WS_EX_WINDOWEDGE,
730 szIEWinFrame, wszWineInternetExplorer,
731 WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
732 | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
733 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
734 NULL, NULL /* FIXME */, ieframe_instance, This);
736 create_doc_view_hwnd(&This->doc_host);
739 static inline InternetExplorer *impl_from_DocHost(DocHost *iface)
741 return CONTAINING_RECORD(iface, InternetExplorer, doc_host);
744 static ULONG IEDocHost_addref(DocHost *iface)
746 InternetExplorer *This = impl_from_DocHost(iface);
747 return IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
750 static ULONG IEDocHost_release(DocHost *iface)
752 InternetExplorer *This = impl_from_DocHost(iface);
753 return IWebBrowser2_Release(&This->IWebBrowser2_iface);
756 static void DocHostContainer_get_docobj_rect(DocHost *This, RECT *rc)
758 GetClientRect(This->frame_hwnd, rc);
759 adjust_ie_docobj_rect(This->frame_hwnd, rc);
762 static HRESULT DocHostContainer_set_status_text(DocHost *iface, const WCHAR *text)
764 InternetExplorer *This = impl_from_DocHost(iface);
765 return update_ie_statustext(This, text);
768 static void DocHostContainer_on_command_state_change(DocHost *iface, LONG command, BOOL enable)
770 InternetExplorer *This = impl_from_DocHost(iface);
772 switch(command) {
773 case CSC_NAVIGATEBACK:
774 enable_toolbar_button(This, ID_BROWSE_BACK, enable);
775 break;
776 case CSC_NAVIGATEFORWARD:
777 enable_toolbar_button(This, ID_BROWSE_FORWARD, enable);
778 break;
782 static void DocHostContainer_set_url(DocHost* iface, const WCHAR *url)
784 InternetExplorer *This = impl_from_DocHost(iface);
786 This->nohome = FALSE;
787 SendMessageW(This->frame_hwnd, WM_UPDATEADDRBAR, 0, (LPARAM)url);
790 static const IDocHostContainerVtbl DocHostContainerVtbl = {
791 IEDocHost_addref,
792 IEDocHost_release,
793 DocHostContainer_get_docobj_rect,
794 DocHostContainer_set_status_text,
795 DocHostContainer_on_command_state_change,
796 DocHostContainer_set_url
799 static HRESULT create_ie(InternetExplorer **ret_obj)
801 InternetExplorer *ret;
803 ret = heap_alloc_zero(sizeof(InternetExplorer));
804 if(!ret)
805 return E_OUTOFMEMORY;
807 ret->ref = 1;
809 DocHost_Init(&ret->doc_host, &ret->IWebBrowser2_iface, &DocHostContainerVtbl);
811 InternetExplorer_WebBrowser_Init(ret);
813 HlinkFrame_Init(&ret->hlink_frame, (IUnknown*)&ret->IWebBrowser2_iface, &ret->doc_host);
815 create_frame_hwnd(ret);
817 InterlockedIncrement(&obj_cnt);
818 list_add_tail(&ie_list, &ret->entry);
819 *ret_obj = ret;
820 return S_OK;
823 HRESULT WINAPI InternetExplorer_Create(IClassFactory *iface, IUnknown *pOuter, REFIID riid, void **ppv)
825 InternetExplorer *ret;
826 HRESULT hres;
828 TRACE("(%p %s %p)\n", pOuter, debugstr_guid(riid), ppv);
830 hres = create_ie(&ret);
831 if(FAILED(hres))
832 return hres;
834 hres = IWebBrowser2_QueryInterface(&ret->IWebBrowser2_iface, riid, ppv);
835 IWebBrowser2_Release(&ret->IWebBrowser2_iface);
836 if(FAILED(hres))
837 return hres;
839 return S_OK;
842 void released_obj(void)
844 if(!InterlockedDecrement(&obj_cnt))
845 PostQuitMessage(0);
848 static BOOL create_ie_window(const WCHAR *cmdline)
850 InternetExplorer *ie;
851 HRESULT hres;
853 hres = create_ie(&ie);
854 if(FAILED(hres))
855 return FALSE;
857 IWebBrowser2_put_Visible(&ie->IWebBrowser2_iface, VARIANT_TRUE);
858 IWebBrowser2_put_MenuBar(&ie->IWebBrowser2_iface, VARIANT_TRUE);
860 if(!*cmdline) {
861 IWebBrowser2_GoHome(&ie->IWebBrowser2_iface);
862 }else {
863 VARIANT var_url;
864 int cmdlen;
866 static const WCHAR nohomeW[] = {'-','n','o','h','o','m','e'};
868 while(*cmdline == ' ' || *cmdline == '\t')
869 cmdline++;
870 cmdlen = strlenW(cmdline);
871 if(cmdlen > 2 && cmdline[0] == '"' && cmdline[cmdlen-1] == '"') {
872 cmdline++;
873 cmdlen -= 2;
876 if(cmdlen == sizeof(nohomeW)/sizeof(*nohomeW) && !memcmp(cmdline, nohomeW, sizeof(nohomeW))) {
877 ie->nohome = TRUE;
878 }else {
879 V_VT(&var_url) = VT_BSTR;
881 V_BSTR(&var_url) = SysAllocStringLen(cmdline, cmdlen);
883 /* navigate to the first page */
884 IWebBrowser2_Navigate2(&ie->IWebBrowser2_iface, &var_url, NULL, NULL, NULL, NULL);
886 SysFreeString(V_BSTR(&var_url));
890 IWebBrowser2_Release(&ie->IWebBrowser2_iface);
891 return TRUE;
894 static HDDEDATA open_dde_url(WCHAR *dde_url)
896 InternetExplorer *ie = NULL, *iter;
897 WCHAR *url, *url_end;
898 VARIANT urlv;
899 HRESULT hres;
901 TRACE("%s\n", debugstr_w(dde_url));
903 url = dde_url;
904 if(*url == '"') {
905 url++;
906 url_end = strchrW(url, '"');
907 if(!url_end) {
908 FIXME("missing string terminator\n");
909 return 0;
911 *url_end = 0;
912 }else {
913 url_end = strchrW(url, ',');
914 if(url_end)
915 *url_end = 0;
916 else
917 url_end = url + strlenW(url);
920 LIST_FOR_EACH_ENTRY(iter, &ie_list, InternetExplorer, entry) {
921 if(iter->nohome) {
922 IWebBrowser2_AddRef(&iter->IWebBrowser2_iface);
923 ie = iter;
924 break;
928 if(!ie) {
929 hres = create_ie(&ie);
930 if(FAILED(hres))
931 return 0;
934 IWebBrowser2_put_Visible(&ie->IWebBrowser2_iface, VARIANT_TRUE);
935 IWebBrowser2_put_MenuBar(&ie->IWebBrowser2_iface, VARIANT_TRUE);
937 V_VT(&urlv) = VT_BSTR;
938 V_BSTR(&urlv) = SysAllocStringLen(url, url_end-url);
939 if(!V_BSTR(&urlv)) {
940 IWebBrowser2_Release(&ie->IWebBrowser2_iface);
941 return 0;
944 hres = IWebBrowser2_Navigate2(&ie->IWebBrowser2_iface, &urlv, NULL, NULL, NULL, NULL);
945 if(FAILED(hres))
946 return 0;
948 IWebBrowser2_Release(&ie->IWebBrowser2_iface);
949 return ULongToHandle(DDE_FACK);
952 static HDDEDATA WINAPI dde_proc(UINT type, UINT uFmt, HCONV hConv, HSZ hsz1, HSZ hsz2, HDDEDATA data,
953 ULONG_PTR dwData1, ULONG_PTR dwData2)
955 switch(type) {
956 case XTYP_CONNECT:
957 TRACE("XTYP_CONNECT %p\n", hsz1);
958 return ULongToHandle(!DdeCmpStringHandles(hsz1, ddestr_openurl));
960 case XTYP_EXECUTE: {
961 WCHAR *url;
962 DWORD size;
963 HDDEDATA ret;
965 TRACE("XTYP_EXECUTE %p\n", data);
967 size = DdeGetData(data, NULL, 0, 0);
968 if(!size) {
969 WARN("size = 0\n");
970 break;
973 url = heap_alloc(size);
974 if(!url)
975 break;
977 if(DdeGetData(data, (BYTE*)url, size, 0) != size) {
978 ERR("error during read\n");
979 heap_free(url);
980 break;
983 ret = open_dde_url(url);
985 heap_free(url);
986 return ret;
989 case XTYP_REQUEST:
990 FIXME("XTYP_REQUEST\n");
991 break;
993 default:
994 TRACE("type %d\n", type);
997 return NULL;
1000 static void init_dde(void)
1002 UINT res;
1004 static const WCHAR iexploreW[] = {'I','E','x','p','l','o','r','e',0};
1005 static const WCHAR openurlW[] = {'W','W','W','_','O','p','e','n','U','R','L',0};
1007 res = DdeInitializeW(&dde_inst, dde_proc, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
1008 if(res != DMLERR_NO_ERROR) {
1009 WARN("DdeInitialize failed: %u\n", res);
1010 return;
1013 ddestr_iexplore = DdeCreateStringHandleW(dde_inst, iexploreW, CP_WINUNICODE);
1014 if(!ddestr_iexplore)
1015 WARN("Failed to create string handle: %u\n", DdeGetLastError(dde_inst));
1017 ddestr_openurl = DdeCreateStringHandleW(dde_inst, openurlW, CP_WINUNICODE);
1018 if(!ddestr_openurl)
1019 WARN("Failed to create string handle: %u\n", DdeGetLastError(dde_inst));
1021 if(!DdeNameService(dde_inst, ddestr_iexplore, 0, DNS_REGISTER))
1022 WARN("DdeNameService failed\n");
1025 static void release_dde(void)
1027 if(ddestr_iexplore)
1028 DdeNameService(dde_inst, ddestr_iexplore, 0, DNS_UNREGISTER);
1029 if(ddestr_openurl)
1030 DdeFreeStringHandle(dde_inst, ddestr_openurl);
1031 if(ddestr_iexplore)
1032 DdeFreeStringHandle(dde_inst, ddestr_iexplore);
1033 DdeUninitialize(dde_inst);
1036 /******************************************************************
1037 * IEWinMain (ieframe.101)
1039 * Only returns on error.
1041 DWORD WINAPI IEWinMain(const WCHAR *cmdline, int nShowWindow)
1043 MSG msg;
1044 HRESULT hres;
1046 static const WCHAR embeddingW[] = {'-','e','m','b','e','d','d','i','n','g',0};
1048 TRACE("%s %d\n", debugstr_w(cmdline), nShowWindow);
1050 CoInitialize(NULL);
1052 hres = register_class_object(TRUE);
1053 if(FAILED(hres)) {
1054 CoUninitialize();
1055 ExitProcess(1);
1058 init_dde();
1060 if(strcmpiW(cmdline, embeddingW)) {
1061 if(!create_ie_window(cmdline)) {
1062 CoUninitialize();
1063 ExitProcess(1);
1067 /* run the message loop for this thread */
1068 while (GetMessageW(&msg, 0, 0, 0))
1070 TranslateMessage(&msg);
1071 DispatchMessageW(&msg);
1074 register_class_object(FALSE);
1075 release_dde();
1077 CoUninitialize();
1079 ExitProcess(0);
1080 return 0;