iexplore.c 30.3 KB
Newer Older
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 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 23 24
 */

#define COBJMACROS

#include <stdarg.h>
25 26 27 28

#include "ieframe.h"
#include "resource.h"

29 30 31 32 33 34 35
#include "winuser.h"
#include "wingdi.h"
#include "winnls.h"
#include "ole2.h"
#include "exdisp.h"
#include "oleidl.h"

36
#include "mshtmcid.h"
37
#include "shellapi.h"
38 39 40
#include "winreg.h"
#include "shlwapi.h"
#include "intshcut.h"
41
#include "ddeml.h"
42 43 44

#include "wine/debug.h"

45
WINE_DEFAULT_DEBUG_CHANNEL(ieframe);
46

47
#define IDI_APPICON 1
48

49 50
#define WM_UPDATEADDRBAR    (WM_APP+1)

51 52
static const WCHAR szIEWinFrame[] = { 'I','E','F','r','a','m','e',0 };

53 54 55 56
/* Windows uses "Microsoft Internet Explorer" */
static const WCHAR wszWineInternetExplorer[] =
        {'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 59
static DWORD dde_inst;
static HSZ ddestr_iexplore, ddestr_openurl;
60
static struct list ie_list = LIST_INIT(ie_list);
61

62 63 64 65 66 67 68 69
HRESULT update_ie_statustext(InternetExplorer* This, LPCWSTR text)
{
    if(!SendMessageW(This->status_hwnd, SB_SETTEXTW, MAKEWORD(SB_SIMPLEID, 0), (LPARAM)text))
        return E_FAIL;

    return S_OK;
}

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 75 76 77
    INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);

    rc->top += barHeight;
    rc->bottom -= barHeight;
78 79 80 81 82 83 84 85

    if(IsWindowVisible(hwndStatus))
    {
        RECT statusrc;

        GetClientRect(hwndStatus, &statusrc);
        rc->bottom -= statusrc.bottom - statusrc.top;
    }
86 87
}

88 89 90 91 92 93 94
static HMENU get_tb_menu(HMENU menu)
{
    HMENU menu_view = GetSubMenu(menu, 1);

    return GetSubMenu(menu_view, 0);
}

95 96
static HMENU get_fav_menu(HMENU menu)
{
97
    return GetSubMenu(menu, 2);
98 99
}

100
static LPWSTR get_fav_url_from_id(HMENU menu, UINT id)
101 102 103 104
{
    MENUITEMINFOW item;

    item.cbSize = sizeof(item);
105 106 107 108 109 110 111 112 113 114
    item.fMask = MIIM_DATA;

    if(!GetMenuItemInfoW(menu, id, FALSE, &item))
        return NULL;

    return (LPWSTR)item.dwItemData;
}

static void free_fav_menu_data(HMENU menu)
{
115
    LPWSTR url;
116 117 118
    int i;

    for(i = 0; (url = get_fav_url_from_id(menu, ID_BROWSE_GOTOFAV_FIRST + i)); i++)
119
        heap_free( url );
120 121
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
static int get_menu_item_count(HMENU menu)
{
    MENUITEMINFOW item;
    int count = 0;
    int i;

    item.cbSize = sizeof(item);
    item.fMask = MIIM_DATA | MIIM_SUBMENU;

    for(i = 0; GetMenuItemInfoW(menu, i, TRUE, &item); i++)
    {
        if(item.hSubMenu)
            count += get_menu_item_count(item.hSubMenu);
        else
            count++;
    }

    return count;
}

static void add_fav_to_menu(HMENU favmenu, HMENU menu, LPWSTR title, LPCWSTR url)
143 144 145
{
    MENUITEMINFOW item;
    /* Subtract the number of standard elements in the Favorites menu */
146
    int favcount = get_menu_item_count(favmenu) - 2;
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    LPWSTR urlbuf;

    if(favcount > (ID_BROWSE_GOTOFAV_MAX - ID_BROWSE_GOTOFAV_FIRST))
    {
        FIXME("Add support for more than %d Favorites\n", favcount);
        return;
    }

    urlbuf = heap_alloc((lstrlenW(url) + 1) * sizeof(WCHAR));

    if(!urlbuf)
        return;

    lstrcpyW(urlbuf, url);

    item.cbSize = sizeof(item);
    item.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_DATA | MIIM_ID;
164 165
    item.fType = MFT_STRING;
    item.dwTypeData = title;
166 167
    item.wID = ID_BROWSE_GOTOFAV_FIRST + favcount;
    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 176 177 178
    WCHAR* filename;
    HANDLE findhandle;
    WIN32_FIND_DATAW finddata;
    IUniformResourceLocatorW* urlobj;
179
    IPersistFile* urlfile = NULL;
180 181 182
    HRESULT res;

    lstrcpyW(path, dir);
183
    PathAppendW(path, search);
184 185 186 187 188 189 190 191 192

    findhandle = FindFirstFileW(path, &finddata);

    if(findhandle == INVALID_HANDLE_VALUE)
        return;

    res = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, &IID_IUniformResourceLocatorW, (PVOID*)&urlobj);

    if(SUCCEEDED(res))
193
        res = IUnknown_QueryInterface((IUnknown*)urlobj, &IID_IPersistFile, (PVOID*)&urlfile);
194 195 196

    if(SUCCEEDED(res))
    {
197
        filename = path + lstrlenW(path) - lstrlenW(search);
198 199 200 201 202

        do
        {
            lstrcpyW(filename, finddata.cFileName);

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
            if(finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                MENUITEMINFOW item;
                const WCHAR ignore1[] = {'.','.',0};
                const WCHAR ignore2[] = {'.',0};

                if(!lstrcmpW(filename, ignore1) || !lstrcmpW(filename, ignore2))
                    continue;

                item.cbSize = sizeof(item);
                item.fMask = MIIM_STRING | MIIM_SUBMENU;
                item.dwTypeData = filename;
                item.hSubMenu = CreatePopupMenu();
                InsertMenuItemW(menu, -1, TRUE, &item);
                add_favs_to_menu(favmenu, item.hSubMenu, path);
            } else
            {
                WCHAR* fileext;
                WCHAR* url = NULL;
                const WCHAR urlext[] = {'.','u','r','l',0};

                if(lstrcmpiW(PathFindExtensionW(filename), urlext))
                    continue;

                if(FAILED(IPersistFile_Load(urlfile, path, 0)))
                    continue;

                urlobj->lpVtbl->GetURL(urlobj, &url);

                if(!url)
                    continue;

                fileext = filename + lstrlenW(filename) - lstrlenW(urlext);
                *fileext = 0;
                add_fav_to_menu(favmenu, menu, filename, url);
            }
239 240 241 242 243 244 245
        } while(FindNextFileW(findhandle, &finddata));
    }

    if(urlfile)
        IPersistFile_Release(urlfile);

    if(urlobj)
246
        IUnknown_Release((IUnknown*)urlobj);
247 248 249 250

    FindClose(findhandle);
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
static void add_tbs_to_menu(HMENU menu)
{
    HUSKEY toolbar_handle;
    WCHAR toolbar_key[] = {'S','o','f','t','w','a','r','e','\\',
                           'M','i','c','r','o','s','o','f','t','\\',
                           'I','n','t','e','r','n','e','t',' ',
                           'E','x','p','l','o','r','e','r','\\',
                           'T','o','o','l','b','a','r',0};

    if(SHRegOpenUSKeyW(toolbar_key, KEY_READ, NULL, &toolbar_handle, TRUE) == ERROR_SUCCESS)
    {
        HUSKEY classes_handle;
        WCHAR classes_key[] = {'S','o','f','t','w','a','r','e','\\',
                               'C','l','a','s','s','e','s','\\','C','L','S','I','D',0};
        WCHAR guid[39];
        DWORD value_len = sizeof(guid)/sizeof(guid[0]);
        int i;

        if(SHRegOpenUSKeyW(classes_key, KEY_READ, NULL, &classes_handle, TRUE) != ERROR_SUCCESS)
        {
            SHRegCloseUSKey(toolbar_handle);
            ERR("Failed to open key %s\n", debugstr_w(classes_key));
            return;
        }

        for(i = 0; SHRegEnumUSValueW(toolbar_handle, i, guid, &value_len, NULL, NULL, NULL, SHREGENUM_HKLM) == ERROR_SUCCESS; i++)
        {
            WCHAR tb_name[100];
            DWORD tb_name_len = sizeof(tb_name)/sizeof(tb_name[0]);
            HUSKEY tb_class_handle;
            MENUITEMINFOW item;
            LSTATUS ret;
            value_len = sizeof(guid)/sizeof(guid[0]);

            if(lstrlenW(guid) != 38)
            {
                TRACE("Found invalid IE toolbar entry: %s\n", debugstr_w(guid));
                continue;
            }

            if(SHRegOpenUSKeyW(guid, KEY_READ, classes_handle, &tb_class_handle, TRUE) != ERROR_SUCCESS)
            {
                ERR("Failed to get class info for %s\n", debugstr_w(guid));
                continue;
            }

            ret = SHRegQueryUSValueW(tb_class_handle, NULL, NULL, tb_name, &tb_name_len, TRUE, NULL, 0);

            SHRegCloseUSKey(tb_class_handle);

            if(ret != ERROR_SUCCESS)
            {
                ERR("Failed to get toolbar name for %s\n", debugstr_w(guid));
                continue;
            }

            item.cbSize = sizeof(item);
            item.fMask = MIIM_STRING;
            item.dwTypeData = tb_name;
            InsertMenuItemW(menu, GetMenuItemCount(menu), TRUE, &item);
        }

        SHRegCloseUSKey(classes_handle);
        SHRegCloseUSKey(toolbar_handle);
    }
}

318 319
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 323
    WCHAR path[MAX_PATH];

324 325
    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);
328 329

    if(SHGetFolderPathW(NULL, CSIDL_FAVORITES, NULL, SHGFP_TYPE_CURRENT, path) == S_OK)
330
        add_favs_to_menu(favmenu, favmenu, path);
331 332 333 334

    return menu;
}

335 336 337 338 339 340 341
static void ie_navigate(InternetExplorer* This, LPCWSTR url)
{
    VARIANT variant;

    V_VT(&variant) = VT_BSTR;
    V_BSTR(&variant) = SysAllocString(url);

342
    IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &variant, NULL, NULL, NULL, NULL);
343 344 345 346

    SysFreeString(V_BSTR(&variant));
}

347 348
static INT_PTR CALLBACK ie_dialog_open_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
349 350
    static InternetExplorer* This;

351 352 353
    switch(msg)
    {
        case WM_INITDIALOG:
354 355
            This = (InternetExplorer*)lparam;
            EnableWindow(GetDlgItem(hwnd, IDOK), FALSE);
356 357 358 359 360
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wparam))
            {
361 362 363 364 365
                case IDC_BROWSE_OPEN_URL:
                {
                    HWND hwndurl = GetDlgItem(hwnd, IDC_BROWSE_OPEN_URL);
                    int len = GetWindowTextLengthW(hwndurl);

366
                    EnableWindow(GetDlgItem(hwnd, IDOK), len != 0);
367 368
                    break;
                }
369
                case IDOK:
370 371 372 373 374 375 376 377 378 379 380
                {
                    HWND hwndurl = GetDlgItem(hwnd, IDC_BROWSE_OPEN_URL);
                    int len = GetWindowTextLengthW(hwndurl);

                    if(len)
                    {
                        VARIANT url;

                        V_VT(&url) = VT_BSTR;
                        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);
383 384 385 386 387

                        SysFreeString(V_BSTR(&url));
                    }
                }
                /* fall through */
388 389 390 391 392 393 394 395
                case IDCANCEL:
                    EndDialog(hwnd, wparam);
                    return TRUE;
            }
    }
    return FALSE;
}

396 397 398 399 400 401 402 403 404
static void ie_dialog_about(HWND hwnd)
{
    HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON, 48, 48, LR_SHARED);

    ShellAboutW(hwnd, wszWineInternetExplorer, NULL, icon);

    DestroyIcon(icon);
}

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
static void add_tb_separator(HWND hwnd)
{
    TBBUTTON btn;

    ZeroMemory(&btn, sizeof(btn));

    btn.iBitmap = 3;
    btn.fsStyle = BTNS_SEP;
    SendMessageW(hwnd, TB_ADDBUTTONSW, 1, (LPARAM)&btn);
}

static void add_tb_button(HWND hwnd, int bmp, int cmd, int strId)
{
    TBBUTTON btn;
    WCHAR buf[30];

421
    LoadStringW(ieframe_instance, strId, buf, sizeof(buf)/sizeof(buf[0]));
422 423 424 425 426 427 428 429 430 431 432

    btn.iBitmap = bmp;
    btn.idCommand = cmd;
    btn.fsState = TBSTATE_ENABLED;
    btn.fsStyle = BTNS_SHOWTEXT;
    btn.dwData = 0;
    btn.iString = (INT_PTR)buf;

    SendMessageW(hwnd, TB_ADDBUTTONSW, 1, (LPARAM)&btn);
}

433 434 435 436
static void create_rebar(HWND hwnd)
{
    HWND hwndRebar;
    HWND hwndAddress;
437
    HWND hwndToolbar;
438 439
    REBARINFO rebarinf;
    REBARBANDINFOW bandinf;
440
    WCHAR addr[40];
441
    HIMAGELIST imagelist;
442
    SIZE toolbar_size;
443

444
    LoadStringW(ieframe_instance, IDS_ADDRESS, addr, sizeof(addr)/sizeof(addr[0]));
445

446 447 448
    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, ieframe_instance, NULL);
449 450 451 452 453 454 455

    rebarinf.cbSize = sizeof(rebarinf);
    rebarinf.fMask = 0;
    rebarinf.himl = NULL;

    SendMessageW(hwndRebar, RB_SETBARINFO, 0, (LPARAM)&rebarinf);

456
    hwndToolbar = CreateWindowExW(TBSTYLE_EX_MIXEDBUTTONS, TOOLBARCLASSNAMEW, NULL, TBSTYLE_FLAT | WS_CHILD | WS_VISIBLE | CCS_NORESIZE,
457
            0, 0, 0, 0, hwndRebar, (HMENU)IDC_BROWSE_TOOLBAR, ieframe_instance, NULL);
458

459
    imagelist = ImageList_LoadImageW(ieframe_instance, MAKEINTRESOURCEW(IDB_IETOOLBAR), 32, 0, CLR_NONE, IMAGE_BITMAP, LR_CREATEDIBSECTION);
460 461

    SendMessageW(hwndToolbar, TB_SETIMAGELIST, 0, (LPARAM)imagelist);
462
    SendMessageW(hwndToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
463 464 465 466
    add_tb_button(hwndToolbar, 0, ID_BROWSE_BACK, IDS_TB_BACK);
    add_tb_button(hwndToolbar, 1, ID_BROWSE_FORWARD, IDS_TB_FORWARD);
    add_tb_button(hwndToolbar, 2, ID_BROWSE_STOP, IDS_TB_STOP);
    add_tb_button(hwndToolbar, 3, ID_BROWSE_REFRESH, IDS_TB_REFRESH);
467
    add_tb_button(hwndToolbar, 4, ID_BROWSE_HOME, IDS_TB_HOME);
468
    add_tb_separator(hwndToolbar);
469 470
    add_tb_button(hwndToolbar, 5, ID_BROWSE_PRINT, IDS_TB_PRINT);
    SendMessageW(hwndToolbar, TB_SETBUTTONSIZE, 0, MAKELPARAM(55,50));
471
    SendMessageW(hwndToolbar, TB_GETMAXSIZE, 0, (LPARAM)&toolbar_size);
472 473

    bandinf.cbSize = sizeof(bandinf);
474
    bandinf.fMask = RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE;
475
    bandinf.fStyle = RBBS_CHILDEDGE;
476 477
    bandinf.cxMinChild = toolbar_size.cx;
    bandinf.cyMinChild = toolbar_size.cy+2;
478 479 480 481
    bandinf.hwndChild = hwndToolbar;

    SendMessageW(hwndRebar, RB_INSERTBANDW, -1, (LPARAM)&bandinf);

482 483
    hwndAddress = CreateWindowExW(0, WC_COMBOBOXEXW, NULL, WS_BORDER|WS_CHILD|WS_VISIBLE|CBS_DROPDOWN,
            0, 0, 100,20,hwndRebar, (HMENU)IDC_BROWSE_ADDRESSBAR, ieframe_instance, NULL);
484

485 486
    bandinf.fMask |= RBBIM_TEXT;
    bandinf.fStyle = RBBS_CHILDEDGE | RBBS_BREAK;
487
    bandinf.lpText = addr;
488
    bandinf.cxMinChild = 100;
489 490 491
    bandinf.cyMinChild = 20;
    bandinf.hwndChild = hwndAddress;

492
    SendMessageW(hwndRebar, RB_INSERTBANDW, -1, (LPARAM)&bandinf);
493 494
}

495 496
static LRESULT iewnd_OnCreate(HWND hwnd, LPCREATESTRUCTW lpcs)
{
497
    InternetExplorer* This = (InternetExplorer*)lpcs->lpCreateParams;
498
    SetWindowLongPtrW(hwnd, 0, (LONG_PTR) lpcs->lpCreateParams);
499

500 501
    This->menu = create_ie_menu();

502 503
    This->status_hwnd = CreateStatusWindowW(WS_VISIBLE|WS_CHILD|SBT_NOBORDERS|CCS_NODIVIDER,
                                            NULL, hwnd, IDC_BROWSE_STATUSBAR);
504 505
    SendMessageW(This->status_hwnd, SB_SIMPLE, TRUE, 0);

506 507
    create_rebar(hwnd);

508 509 510
    return 0;
}

511
static LRESULT iewnd_OnSize(InternetExplorer *This, INT width, INT height)
512
{
513 514 515 516
    HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
    INT barHeight = SendMessageW(hwndRebar, RB_GETBARHEIGHT, 0, 0);
    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.hwnd)
        SetWindowPos(This->doc_host.hwnd, NULL, docarea.left, docarea.top, docarea.right, docarea.bottom,
523
                     SWP_NOZORDER | SWP_NOACTIVATE);
524 525

    SetWindowPos(hwndRebar, NULL, 0, 0, width, barHeight, SWP_NOZORDER | SWP_NOACTIVATE);
526 527 528 529

    return 0;
}

530 531 532 533 534 535 536 537
static LRESULT iewnd_OnNotify(InternetExplorer *This, WPARAM wparam, LPARAM lparam)
{
    NMHDR* hdr = (NMHDR*)lparam;

    if(hdr->idFrom == IDC_BROWSE_ADDRESSBAR && hdr->code == CBEN_ENDEDITW)
    {
        NMCBEENDEDITW* info = (NMCBEENDEDITW*)lparam;

538
        if(info->fChanged && info->iWhy == CBENF_RETURN)
539 540 541 542 543 544
        {
            VARIANT vt;

            V_VT(&vt) = VT_BSTR;
            V_BSTR(&vt) = SysAllocString(info->szText);

545
            IWebBrowser2_Navigate2(&This->IWebBrowser2_iface, &vt, NULL, NULL, NULL, NULL);
546 547 548 549 550 551 552

            SysFreeString(V_BSTR(&vt));

            return 0;
        }
    }

553 554 555 556 557 558 559
    if(hdr->idFrom == IDC_BROWSE_REBAR && hdr->code == RBN_HEIGHTCHANGE)
    {
        RECT docarea;

        GetClientRect(This->frame_hwnd, &docarea);
        adjust_ie_docobj_rect(This->frame_hwnd, &docarea);

560 561
        if(This->doc_host.hwnd)
            SetWindowPos(This->doc_host.hwnd, NULL, docarea.left, docarea.top, docarea.right, docarea.bottom,
562 563 564
                    SWP_NOZORDER | SWP_NOACTIVATE);
    }

565 566 567
    return 0;
}

568
static LRESULT iewnd_OnDestroy(InternetExplorer *This)
569
{
570 571 572 573
    HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
    HWND hwndToolbar = GetDlgItem(hwndRebar, IDC_BROWSE_TOOLBAR);
    HIMAGELIST list = (HIMAGELIST)SendMessageW(hwndToolbar, TB_GETIMAGELIST, 0, 0);

574 575
    TRACE("%p\n", This);

576
    free_fav_menu_data(get_fav_menu(This->menu));
577
    ImageList_Destroy(list);
578
    This->frame_hwnd = NULL;
579 580 581 582

    return 0;
}

583
static LRESULT iewnd_OnCommand(InternetExplorer *This, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
584 585 586
{
    switch(LOWORD(wparam))
    {
587
        case ID_BROWSE_OPEN:
588
            DialogBoxParamW(ieframe_instance, MAKEINTRESOURCEW(IDD_BROWSE_OPEN), hwnd, ie_dialog_open_proc, (LPARAM)This);
589 590
            break;

591
        case ID_BROWSE_PRINT:
592
            if(This->doc_host.document)
593 594 595
            {
                IOleCommandTarget* target;

596
                if(FAILED(IUnknown_QueryInterface(This->doc_host.document, &IID_IOleCommandTarget, (LPVOID*)&target)))
597 598 599 600 601 602 603 604
                    break;

                IOleCommandTarget_Exec(target, &CGID_MSHTML, IDM_PRINT, OLECMDEXECOPT_DODEFAULT, NULL, NULL);

                IOleCommandTarget_Release(target);
            }
            break;

605
        case ID_BROWSE_HOME:
606
            IWebBrowser2_GoHome(&This->IWebBrowser2_iface);
607 608
            break;

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
        case ID_BROWSE_BACK:
            IWebBrowser2_GoBack(&This->IWebBrowser2_iface);
            break;

        case ID_BROWSE_FORWARD:
            IWebBrowser2_GoForward(&This->IWebBrowser2_iface);
            break;

        case ID_BROWSE_STOP:
            IWebBrowser2_Stop(&This->IWebBrowser2_iface);
            break;

        case ID_BROWSE_REFRESH:
            IWebBrowser2_Refresh(&This->IWebBrowser2_iface);
            break;

625 626 627 628
        case ID_BROWSE_ABOUT:
            ie_dialog_about(hwnd);
            break;

629
        case ID_BROWSE_QUIT:
630
            ShowWindow(hwnd, SW_HIDE);
631 632
            break;

633
        default:
634 635 636 637 638 639 640
            if(LOWORD(wparam) >= ID_BROWSE_GOTOFAV_FIRST && LOWORD(wparam) <= ID_BROWSE_GOTOFAV_MAX)
            {
                LPCWSTR url = get_fav_url_from_id(get_fav_menu(This->menu), LOWORD(wparam));

                if(url)
                    ie_navigate(This, url);
            }
641 642 643 644 645
            return DefWindowProcW(hwnd, msg, wparam, lparam);
    }
    return 0;
}

646
static LRESULT update_addrbar(InternetExplorer *This, LPARAM lparam)
647 648 649 650 651 652 653 654 655 656 657
{
    HWND hwndRebar = GetDlgItem(This->frame_hwnd, IDC_BROWSE_REBAR);
    HWND hwndAddress = GetDlgItem(hwndRebar, IDC_BROWSE_ADDRESSBAR);
    HWND hwndEdit = (HWND)SendMessageW(hwndAddress, CBEM_GETEDITCONTROL, 0, 0);
    LPCWSTR url = (LPCWSTR)lparam;

    SendMessageW(hwndEdit, WM_SETTEXT, 0, (LPARAM)url);

    return 0;
}

658
static LRESULT WINAPI ie_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
659
{
660
    InternetExplorer *This = (InternetExplorer*) GetWindowLongPtrW(hwnd, 0);
661 662 663 664 665

    switch (msg)
    {
    case WM_CREATE:
        return iewnd_OnCreate(hwnd, (LPCREATESTRUCTW)lparam);
666 667 668 669 670 671
    case WM_CLOSE:
        TRACE("WM_CLOSE\n");
        ShowWindow(hwnd, SW_HIDE);
        return 0;
    case WM_SHOWWINDOW:
        TRACE("WM_SHOWWINDOW %lx\n", wparam);
672
        if(wparam) {
673
            IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
674 675 676
            InterlockedIncrement(&This->extern_ref);
        }else {
            release_extern_ref(This, TRUE);
677
            IWebBrowser2_Release(&This->IWebBrowser2_iface);
678
        }
679
        break;
680 681 682 683
    case WM_DESTROY:
        return iewnd_OnDestroy(This);
    case WM_SIZE:
        return iewnd_OnSize(This, LOWORD(lparam), HIWORD(lparam));
684 685
    case WM_COMMAND:
        return iewnd_OnCommand(This, hwnd, msg, wparam, lparam);
686 687
    case WM_NOTIFY:
        return iewnd_OnNotify(This, wparam, lparam);
688
    case WM_DOCHOSTTASK:
689
        return process_dochost_tasks(&This->doc_host);
690 691
    case WM_UPDATEADDRBAR:
        return update_addrbar(This, lparam);
692 693 694 695 696 697
    }
    return DefWindowProcW(hwnd, msg, wparam, lparam);
}

void register_iewindow_class(void)
{
698
    WNDCLASSEXW wc;
699 700

    memset(&wc, 0, sizeof wc);
701
    wc.cbSize = sizeof(wc);
702 703 704
    wc.style = 0;
    wc.lpfnWndProc = ie_window_proc;
    wc.cbClsExtra = 0;
705
    wc.cbWndExtra = sizeof(InternetExplorer*);
706
    wc.hInstance = ieframe_instance;
707 708 709
    wc.hIcon = LoadIconW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON));
    wc.hIconSm = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_APPICON), IMAGE_ICON,
                            GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
710
    wc.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
711 712 713 714
    wc.hbrBackground = 0;
    wc.lpszClassName = szIEWinFrame;
    wc.lpszMenuName = NULL;

715
    RegisterClassExW(&wc);
716 717 718 719
}

void unregister_iewindow_class(void)
{
720
    UnregisterClassW(szIEWinFrame, ieframe_instance);
721 722
}

723 724 725 726 727 728 729 730
static void create_frame_hwnd(InternetExplorer *This)
{
    This->frame_hwnd = CreateWindowExW(
            WS_EX_WINDOWEDGE,
            szIEWinFrame, wszWineInternetExplorer,
            WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
                | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
731
            NULL, NULL /* FIXME */, ieframe_instance, This);
732

733 734
    This->doc_host.frame_hwnd = This->frame_hwnd;
    create_doc_view_hwnd(&This->doc_host);
735 736
}

737
static inline InternetExplorer *impl_from_DocHost(DocHost *iface)
738
{
739
    return CONTAINING_RECORD(iface, InternetExplorer, doc_host);
740
}
741

742
static ULONG IEDocHost_addref(DocHost *iface)
743
{
744 745
    InternetExplorer *This = impl_from_DocHost(iface);
    return IWebBrowser2_AddRef(&This->IWebBrowser2_iface);
746 747 748 749
}

static ULONG IEDocHost_release(DocHost *iface)
{
750 751
    InternetExplorer *This = impl_from_DocHost(iface);
    return IWebBrowser2_Release(&This->IWebBrowser2_iface);
752 753
}

754 755 756 757 758 759
static void WINAPI DocHostContainer_GetDocObjRect(DocHost* This, RECT* rc)
{
    GetClientRect(This->frame_hwnd, rc);
    adjust_ie_docobj_rect(This->frame_hwnd, rc);
}

760
static HRESULT WINAPI DocHostContainer_SetStatusText(DocHost *iface, LPCWSTR text)
761
{
762 763
    InternetExplorer *This = impl_from_DocHost(iface);
    return update_ie_statustext(This, text);
764 765
}

766
static void WINAPI DocHostContainer_SetURL(DocHost* iface, LPCWSTR url)
767
{
768
    InternetExplorer *This = impl_from_DocHost(iface);
769

770 771
    This->nohome = FALSE;
    SendMessageW(This->frame_hwnd, WM_UPDATEADDRBAR, 0, (LPARAM)url);
772 773
}

774 775 776
static HRESULT DocHostContainer_exec(DocHost* This, const GUID *cmd_group, DWORD cmdid, DWORD execopt, VARIANT *in,
        VARIANT *out)
{
777
    return E_NOTIMPL;
778
}
779

780
static const IDocHostContainerVtbl DocHostContainerVtbl = {
781 782
    IEDocHost_addref,
    IEDocHost_release,
783 784
    DocHostContainer_GetDocObjRect,
    DocHostContainer_SetStatusText,
785 786
    DocHostContainer_SetURL,
    DocHostContainer_exec
787 788
};

789
static HRESULT create_ie(InternetExplorer **ret_obj)
790 791 792
{
    InternetExplorer *ret;

793
    ret = heap_alloc_zero(sizeof(InternetExplorer));
794 795
    if(!ret)
        return E_OUTOFMEMORY;
796

797 798
    ret->ref = 1;

799
    DocHost_Init(&ret->doc_host, &ret->IWebBrowser2_iface, &DocHostContainerVtbl);
800 801 802

    InternetExplorer_WebBrowser_Init(ret);

803
    HlinkFrame_Init(&ret->hlink_frame, (IUnknown*)&ret->IWebBrowser2_iface, &ret->doc_host);
804

805
    create_frame_hwnd(ret);
806

807
    InterlockedIncrement(&obj_cnt);
808
    list_add_tail(&ie_list, &ret->entry);
809 810 811 812
    *ret_obj = ret;
    return S_OK;
}

813
HRESULT WINAPI InternetExplorer_Create(IClassFactory *iface, IUnknown *pOuter, REFIID riid, void **ppv)
814 815 816 817 818 819 820 821 822 823
{
    InternetExplorer *ret;
    HRESULT hres;

    TRACE("(%p %s %p)\n", pOuter, debugstr_guid(riid), ppv);

    hres = create_ie(&ret);
    if(FAILED(hres))
        return hres;

824
    hres = IWebBrowser2_QueryInterface(&ret->IWebBrowser2_iface, riid, ppv);
825 826
    IWebBrowser2_Release(&ret->IWebBrowser2_iface);
    if(FAILED(hres))
827 828
        return hres;

829 830 831 832 833 834 835
    return S_OK;
}

void released_obj(void)
{
    if(!InterlockedDecrement(&obj_cnt))
        PostQuitMessage(0);
836
}
837

838
static BOOL create_ie_window(const WCHAR *cmdline)
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
{
    InternetExplorer *ie;
    HRESULT hres;

    hres = create_ie(&ie);
    if(FAILED(hres))
        return FALSE;

    IWebBrowser2_put_Visible(&ie->IWebBrowser2_iface, VARIANT_TRUE);
    IWebBrowser2_put_MenuBar(&ie->IWebBrowser2_iface, VARIANT_TRUE);

    if(!*cmdline) {
        IWebBrowser2_GoHome(&ie->IWebBrowser2_iface);
    }else {
        VARIANT var_url;
        int cmdlen;

856 857
        static const WCHAR nohomeW[] = {'-','n','o','h','o','m','e'};

858 859
        while(*cmdline == ' ' || *cmdline == '\t')
            cmdline++;
860
        cmdlen = strlenW(cmdline);
861 862 863 864 865
        if(cmdlen > 2 && cmdline[0] == '"' && cmdline[cmdlen-1] == '"') {
            cmdline++;
            cmdlen -= 2;
        }

866
        if(cmdlen == sizeof(nohomeW)/sizeof(*nohomeW) && !memcmp(cmdline, nohomeW, sizeof(nohomeW))) {
867 868 869
            ie->nohome = TRUE;
        }else {
            V_VT(&var_url) = VT_BSTR;
870

871
            V_BSTR(&var_url) = SysAllocStringLen(cmdline, cmdlen);
872

873 874
            /* navigate to the first page */
            IWebBrowser2_Navigate2(&ie->IWebBrowser2_iface, &var_url, NULL, NULL, NULL, NULL);
875

876 877
            SysFreeString(V_BSTR(&var_url));
        }
878 879 880 881 882 883
    }

    IWebBrowser2_Release(&ie->IWebBrowser2_iface);
    return TRUE;
}

884
static HDDEDATA open_dde_url(WCHAR *dde_url)
885
{
886
    InternetExplorer *ie = NULL, *iter;
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
    WCHAR *url, *url_end;
    VARIANT urlv;
    HRESULT hres;

    TRACE("%s\n", debugstr_w(dde_url));

    url = dde_url;
    if(*url == '"') {
        url++;
        url_end = strchrW(url, '"');
        if(!url_end) {
            FIXME("missing string terminator\n");
            return 0;
        }
        *url_end = 0;
    }else {
        url_end = strchrW(url, ',');
        if(url_end)
            *url_end = 0;
        else
            url_end = url + strlenW(url);
    }

910 911 912 913 914 915 916 917 918 919 920 921 922
    LIST_FOR_EACH_ENTRY(iter, &ie_list, InternetExplorer, entry) {
        if(iter->nohome) {
            IWebBrowser2_AddRef(&iter->IWebBrowser2_iface);
            ie = iter;
            break;
        }
    }

    if(!ie) {
        hres = create_ie(&ie);
        if(FAILED(hres))
            return 0;
    }
923

924 925
    IWebBrowser2_put_Visible(&ie->IWebBrowser2_iface, VARIANT_TRUE);
    IWebBrowser2_put_MenuBar(&ie->IWebBrowser2_iface, VARIANT_TRUE);
926 927 928 929

    V_VT(&urlv) = VT_BSTR;
    V_BSTR(&urlv) = SysAllocStringLen(url, url_end-url);
    if(!V_BSTR(&urlv)) {
930
        IWebBrowser2_Release(&ie->IWebBrowser2_iface);
931 932 933
        return 0;
    }

934
    hres = IWebBrowser2_Navigate2(&ie->IWebBrowser2_iface, &urlv, NULL, NULL, NULL, NULL);
935 936 937
    if(FAILED(hres))
        return 0;

938
    IWebBrowser2_Release(&ie->IWebBrowser2_iface);
939
    return ULongToHandle(DDE_FACK);
940 941 942 943 944 945 946 947
}

static HDDEDATA WINAPI dde_proc(UINT type, UINT uFmt, HCONV hConv, HSZ hsz1, HSZ hsz2, HDDEDATA data,
        ULONG_PTR dwData1, ULONG_PTR dwData2)
{
    switch(type) {
    case XTYP_CONNECT:
        TRACE("XTYP_CONNECT %p\n", hsz1);
948
        return ULongToHandle(!DdeCmpStringHandles(hsz1, ddestr_openurl));
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968

    case XTYP_EXECUTE: {
        WCHAR *url;
        DWORD size;
        HDDEDATA ret;

        TRACE("XTYP_EXECUTE %p\n", data);

        size = DdeGetData(data, NULL, 0, 0);
        if(!size) {
            WARN("size = 0\n");
            break;
        }

        url = heap_alloc(size);
        if(!url)
            break;

        if(DdeGetData(data, (BYTE*)url, size, 0) != size) {
            ERR("error during read\n");
969
            heap_free(url);
970 971 972
            break;
        }

973
        ret = open_dde_url(url);
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

        heap_free(url);
        return ret;
    }

    case XTYP_REQUEST:
        FIXME("XTYP_REQUEST\n");
        break;

    default:
        TRACE("type %d\n", type);
    }

    return NULL;
}

static void init_dde(void)
{
    UINT res;

    static const WCHAR iexploreW[] = {'I','E','x','p','l','o','r','e',0};
    static const WCHAR openurlW[] = {'W','W','W','_','O','p','e','n','U','R','L',0};

    res = DdeInitializeW(&dde_inst, dde_proc, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
    if(res != DMLERR_NO_ERROR) {
        WARN("DdeInitialize failed: %u\n", res);
        return;
    }

    ddestr_iexplore = DdeCreateStringHandleW(dde_inst, iexploreW, CP_WINUNICODE);
    if(!ddestr_iexplore)
        WARN("Failed to create string handle: %u\n", DdeGetLastError(dde_inst));

    ddestr_openurl = DdeCreateStringHandleW(dde_inst, openurlW, CP_WINUNICODE);
    if(!ddestr_openurl)
        WARN("Failed to create string handle: %u\n", DdeGetLastError(dde_inst));

1011 1012
    if(!DdeNameService(dde_inst, ddestr_iexplore, 0, DNS_REGISTER))
        WARN("DdeNameService failed\n");
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
}

static void release_dde(void)
{
    if(ddestr_iexplore)
        DdeNameService(dde_inst, ddestr_iexplore, 0, DNS_UNREGISTER);
    if(ddestr_openurl)
        DdeFreeStringHandle(dde_inst, ddestr_openurl);
    if(ddestr_iexplore)
        DdeFreeStringHandle(dde_inst, ddestr_iexplore);
    DdeUninitialize(dde_inst);
}

1026
/******************************************************************
1027
 *		IEWinMain            (ieframe.101)
1028 1029 1030
 *
 * Only returns on error.
 */
1031
DWORD WINAPI IEWinMain(const WCHAR *cmdline, int nShowWindow)
1032
{
1033
    MSG msg;
1034
    HRESULT hres;
1035

1036 1037 1038
    static const WCHAR embeddingW[] = {'-','e','m','b','e','d','d','i','n','g',0};

    TRACE("%s %d\n", debugstr_w(cmdline), nShowWindow);
1039

1040 1041
    CoInitialize(NULL);

1042 1043 1044 1045 1046 1047
    hres = register_class_object(TRUE);
    if(FAILED(hres)) {
        CoUninitialize();
        ExitProcess(1);
    }

1048 1049
    init_dde();

1050 1051
    if(strcmpiW(cmdline, embeddingW)) {
        if(!create_ie_window(cmdline)) {
1052 1053 1054 1055
            CoUninitialize();
            ExitProcess(1);
        }
    }
1056 1057 1058 1059 1060 1061 1062

    /* run the message loop for this thread */
    while (GetMessageW(&msg, 0, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
1063

1064
    register_class_object(FALSE);
1065
    release_dde();
1066 1067

    CoUninitialize();
1068 1069 1070 1071

    ExitProcess(0);
    return 0;
}