htmlwindow.c 70.3 KB
Newer Older
1
/*
2
 * Copyright 2006-2010 Jacek Caban for CodeWeavers
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"
27
#include "mshtmdid.h"
28
#include "shlguid.h"
29

30 31 32
#define NO_SHLWAPI_REG
#include "shlwapi.h"

33 34 35
#include "wine/debug.h"

#include "mshtml_private.h"
36
#include "htmlevent.h"
37
#include "binding.h"
38 39 40 41
#include "resource.h"

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

42 43
static struct list window_list = LIST_INIT(window_list);

44
static void window_set_docnode(HTMLWindow *window, HTMLDocumentNode *doc_node)
45 46
{
    if(window->doc) {
47 48
        if(window->doc_obj && window == window->doc_obj->basedoc.window)
            window->doc->basedoc.cp_container.forward_container = NULL;
49
        abort_document_bindings(window->doc);
50 51 52 53 54 55
        window->doc->basedoc.window = NULL;
        htmldoc_release(&window->doc->basedoc);
    }
    window->doc = doc_node;
    if(doc_node)
        htmldoc_addref(&doc_node->basedoc);
56 57 58 59 60 61 62 63

    if(window->doc_obj && window->doc_obj->basedoc.window == window) {
        if(window->doc_obj->basedoc.doc_node)
            htmldoc_release(&window->doc_obj->basedoc.doc_node->basedoc);
        window->doc_obj->basedoc.doc_node = doc_node;
        if(doc_node)
            htmldoc_addref(&doc_node->basedoc);
    }
64

65
    if(doc_node && window->doc_obj && window->doc_obj->usermode == EDITMODE) {
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        nsIDOMNSHTMLDocument *nshtmldoc;
        nsAString mode_str;
        nsresult nsres;

        static const PRUnichar onW[] = {'o','n',0};

        nsres = nsIDOMHTMLDocument_QueryInterface(doc_node->nsdoc, &IID_nsIDOMNSHTMLDocument, (void**)&nshtmldoc);
        if(NS_SUCCEEDED(nsres)) {
            nsAString_Init(&mode_str, onW);
            nsres = nsIDOMNSHTMLDocument_SetDesignMode(nshtmldoc, &mode_str);
            nsAString_Finish(&mode_str);
            nsIDOMNSHTMLDocument_Release(nshtmldoc);
            if(NS_FAILED(nsres))
                ERR("SetDesignMode failed: %08x\n", nsres);
        }else {
            ERR("Could not get nsIDOMNSHTMLDocument interface: %08x\n", nsres);
        }
    }
84 85
}

86 87 88 89 90 91 92 93 94
static void release_children(HTMLWindow *This)
{
    HTMLWindow *child;

    while(!list_empty(&This->children)) {
        child = LIST_ENTRY(list_tail(&This->children), HTMLWindow, sibling_entry);

        list_remove(&child->sibling_entry);
        child->parent = NULL;
95
        IHTMLWindow2_Release(&child->IHTMLWindow2_iface);
96 97 98
    }
}

99 100 101
static HRESULT get_location(HTMLWindow *This, HTMLLocation **ret)
{
    if(This->location) {
102
        IHTMLLocation_AddRef(&This->location->IHTMLLocation_iface);
103 104 105 106 107 108 109 110 111 112 113 114
    }else {
        HRESULT hres;

        hres = HTMLLocation_Create(This, &This->location);
        if(FAILED(hres))
            return hres;
    }

    *ret = This->location;
    return S_OK;
}

115 116 117 118 119 120 121
static inline HRESULT set_window_event(HTMLWindow *window, eventid_t eid, VARIANT *var)
{
    if(!window->doc) {
        FIXME("No document\n");
        return E_FAIL;
    }

122
    return set_event_handler(&window->doc->body_event_target, NULL, window->doc, eid, var);
123 124 125 126 127 128 129 130 131 132 133 134
}

static inline HRESULT get_window_event(HTMLWindow *window, eventid_t eid, VARIANT *var)
{
    if(!window->doc) {
        FIXME("No document\n");
        return E_FAIL;
    }

    return get_event_handler(&window->doc->body_event_target, eid, var);
}

135 136 137 138
static inline HTMLWindow *impl_from_IHTMLWindow2(IHTMLWindow2 *iface)
{
    return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow2_iface);
}
139

140 141
static HRESULT WINAPI HTMLWindow2_QueryInterface(IHTMLWindow2 *iface, REFIID riid, void **ppv)
{
142
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
143 144 145 146 147

    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
148
        *ppv = &This->IHTMLWindow2_iface;
149 150
    }else if(IsEqualGUID(&IID_IDispatch, riid)) {
        TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
151
        *ppv = &This->IHTMLWindow2_iface;
152 153
    }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
        TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
154
        *ppv = &This->IDispatchEx_iface;
155 156
    }else if(IsEqualGUID(&IID_IHTMLFramesCollection2, riid)) {
        TRACE("(%p)->(IID_IHTMLFramesCollection2 %p)\n", This, ppv);
157
        *ppv = &This->IHTMLWindow2_iface;
158 159
    }else if(IsEqualGUID(&IID_IHTMLWindow2, riid)) {
        TRACE("(%p)->(IID_IHTMLWindow2 %p)\n", This, ppv);
160
        *ppv = &This->IHTMLWindow2_iface;
161
    }else if(IsEqualGUID(&IID_IHTMLWindow3, riid)) {
162
        TRACE("(%p)->(IID_IHTMLWindow3 %p)\n", This, ppv);
163
        *ppv = &This->IHTMLWindow3_iface;
164 165
    }else if(IsEqualGUID(&IID_IHTMLWindow4, riid)) {
        TRACE("(%p)->(IID_IHTMLWindow4 %p)\n", This, ppv);
166
        *ppv = &This->IHTMLWindow4_iface;
167 168
    }else if(IsEqualGUID(&IID_IHTMLPrivateWindow, riid)) {
        TRACE("(%p)->(IID_IHTMLPrivateWindow %p)\n", This, ppv);
169
        *ppv = &This->IHTMLPrivateWindow_iface;
170 171
    }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
        TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
172
        *ppv = &This->IServiceProvider_iface;
173 174
    }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
        return *ppv ? S_OK : E_NOINTERFACE;
175 176 177 178 179 180 181 182 183 184 185 186 187
    }

    if(*ppv) {
        IUnknown_AddRef((IUnknown*)*ppv);
        return S_OK;
    }

    WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
    return E_NOINTERFACE;
}

static ULONG WINAPI HTMLWindow2_AddRef(IHTMLWindow2 *iface)
{
188
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
189 190 191 192 193 194 195 196 197
    LONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    return ref;
}

static ULONG WINAPI HTMLWindow2_Release(IHTMLWindow2 *iface)
{
198
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
199 200 201 202
    LONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

203
    if(!ref) {
204 205
        DWORD i;

206
        remove_target_tasks(This->task_magic);
207
        set_window_bscallback(This, NULL);
208
        set_current_mon(This, NULL);
209
        window_set_docnode(This, NULL);
210
        release_children(This);
211

212 213 214
        if(This->secmgr)
            IInternetSecurityManager_Release(This->secmgr);

215 216 217
        if(This->frame_element)
            This->frame_element->content_window = NULL;

218 219
        if(This->option_factory) {
            This->option_factory->window = NULL;
220
            IHTMLOptionElementFactory_Release(&This->option_factory->IHTMLOptionElementFactory_iface);
221 222
        }

223 224
        if(This->image_factory) {
            This->image_factory->window = NULL;
225
            IHTMLImageElementFactory_Release(&This->image_factory->IHTMLImageElementFactory_iface);
226 227
        }

228 229
        if(This->location) {
            This->location->window = NULL;
230
            IHTMLLocation_Release(&This->location->IHTMLLocation_iface);
231 232
        }

233 234 235
        if(This->screen)
            IHTMLScreen_Release(This->screen);

236 237
        for(i=0; i < This->global_prop_cnt; i++)
            heap_free(This->global_props[i].name);
238 239 240 241

        This->window_ref->window = NULL;
        windowref_release(This->window_ref);

242
        heap_free(This->global_props);
243
        release_script_hosts(This);
244 245 246 247

        if(This->nswindow)
            nsIDOMWindow_Release(This->nswindow);

248
        list_remove(&This->entry);
249
        release_dispex(&This->dispex);
250
        heap_free(This);
251
    }
252 253 254 255 256 257

    return ref;
}

static HRESULT WINAPI HTMLWindow2_GetTypeInfoCount(IHTMLWindow2 *iface, UINT *pctinfo)
{
258
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
259

260
    return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
261 262 263 264 265
}

static HRESULT WINAPI HTMLWindow2_GetTypeInfo(IHTMLWindow2 *iface, UINT iTInfo,
                                              LCID lcid, ITypeInfo **ppTInfo)
{
266
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
267

268
    return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
269 270 271 272 273 274
}

static HRESULT WINAPI HTMLWindow2_GetIDsOfNames(IHTMLWindow2 *iface, REFIID riid,
                                                LPOLESTR *rgszNames, UINT cNames,
                                                LCID lcid, DISPID *rgDispId)
{
275
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
276

277 278
    return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
            rgDispId);
279 280 281 282 283 284
}

static HRESULT WINAPI HTMLWindow2_Invoke(IHTMLWindow2 *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
285
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
286

287 288
    return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
            pDispParams, pVarResult, pExcepInfo, puArgErr);
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 318
static HRESULT get_frame_by_index(nsIDOMWindowCollection *nsFrames, PRUint32 index, HTMLWindow **ret)
{
    PRUint32 length;
    nsIDOMWindow *nsWindow;
    nsresult nsres;

    nsres = nsIDOMWindowCollection_GetLength(nsFrames, &length);
    if(NS_FAILED(nsres)) {
        FIXME("nsIDOMWindowCollection_GetLength failed: 0x%08x\n", nsres);
        return E_FAIL;
    }

    if(index >= length)
        return DISP_E_MEMBERNOTFOUND;

    nsres = nsIDOMWindowCollection_Item(nsFrames, index, &nsWindow);
    if(NS_FAILED(nsres)) {
        FIXME("nsIDOMWindowCollection_Item failed: 0x%08x\n", nsres);
        return E_FAIL;
    }

    *ret = nswindow_to_window(nsWindow);

    nsIDOMWindow_Release(nsWindow);

    return S_OK;
}

319 320
static HRESULT WINAPI HTMLWindow2_item(IHTMLWindow2 *iface, VARIANT *pvarIndex, VARIANT *pvarResult)
{
321
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    nsIDOMWindowCollection *nsFrames;
    HTMLWindow *window;
    HRESULT hres;
    nsresult nsres;

    TRACE("(%p)->(%p %p)\n", This, pvarIndex, pvarResult);

    nsres = nsIDOMWindow_GetFrames(This->nswindow, &nsFrames);
    if(NS_FAILED(nsres)) {
        FIXME("nsIDOMWindow_GetFrames failed: 0x%08x\n", nsres);
        return E_FAIL;
    }

    if(V_VT(pvarIndex) == VT_I4) {
        int index = V_I4(pvarIndex);
        TRACE("Getting index %d\n", index);
        if(index < 0) {
            hres = DISP_E_MEMBERNOTFOUND;
            goto cleanup;
        }
        hres = get_frame_by_index(nsFrames, index, &window);
        if(FAILED(hres))
            goto cleanup;
    }else if(V_VT(pvarIndex) == VT_UINT) {
        unsigned int index = V_UINT(pvarIndex);
        TRACE("Getting index %u\n", index);
        hres = get_frame_by_index(nsFrames, index, &window);
        if(FAILED(hres))
            goto cleanup;
    }else if(V_VT(pvarIndex) == VT_BSTR) {
        BSTR str = V_BSTR(pvarIndex);
        PRUint32 length, i;

        TRACE("Getting name %s\n", wine_dbgstr_w(str));

        nsres = nsIDOMWindowCollection_GetLength(nsFrames, &length);

        window = NULL;
        for(i = 0; i < length && !window; ++i) {
            HTMLWindow *cur_window;
            nsIDOMWindow *nsWindow;
            BSTR id;

            nsres = nsIDOMWindowCollection_Item(nsFrames, i, &nsWindow);
            if(NS_FAILED(nsres)) {
                FIXME("nsIDOMWindowCollection_Item failed: 0x%08x\n", nsres);
                hres = E_FAIL;
                goto cleanup;
            }

            cur_window = nswindow_to_window(nsWindow);

            nsIDOMWindow_Release(nsWindow);

376
            hres = IHTMLElement_get_id(&cur_window->frame_element->element.IHTMLElement_iface, &id);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
            if(FAILED(hres)) {
                FIXME("IHTMLElement_get_id failed: 0x%08x\n", hres);
                goto cleanup;
            }

            if(!strcmpW(id, str))
                window = cur_window;

            SysFreeString(id);
        }

        if(!window) {
            hres = DISP_E_MEMBERNOTFOUND;
            goto cleanup;
        }
    }else {
        hres = E_INVALIDARG;
        goto cleanup;
    }

397
    IHTMLWindow2_AddRef(&window->IHTMLWindow2_iface);
398 399 400 401 402 403 404 405 406
    V_VT(pvarResult) = VT_DISPATCH;
    V_DISPATCH(pvarResult) = (IDispatch*)window;

    hres = S_OK;

cleanup:
    nsIDOMWindowCollection_Release(nsFrames);

    return hres;
407 408
}

409
static HRESULT WINAPI HTMLWindow2_get_length(IHTMLWindow2 *iface, LONG *p)
410
{
411
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    nsIDOMWindowCollection *nscollection;
    PRUint32 length;
    nsresult nsres;

    TRACE("(%p)->(%p)\n", This, p);

    nsres = nsIDOMWindow_GetFrames(This->nswindow, &nscollection);
    if(NS_FAILED(nsres)) {
        ERR("GetFrames failed: %08x\n", nsres);
        return E_FAIL;
    }

    nsres = nsIDOMWindowCollection_GetLength(nscollection, &length);
    nsIDOMWindowCollection_Release(nscollection);
    if(NS_FAILED(nsres)) {
        ERR("GetLength failed: %08x\n", nsres);
        return E_FAIL;
    }

    *p = length;
    return S_OK;
433 434 435 436
}

static HRESULT WINAPI HTMLWindow2_get_frames(IHTMLWindow2 *iface, IHTMLFramesCollection2 **p)
{
437
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
438 439 440
    FIXME("(%p)->(%p): semi-stub\n", This, p);

    /* FIXME: Should return a separate Window object */
441
    *p = (IHTMLFramesCollection2*)&This->IHTMLWindow2_iface;
442 443
    HTMLWindow2_AddRef(iface);
    return S_OK;
444 445 446 447
}

static HRESULT WINAPI HTMLWindow2_put_defaultStatus(IHTMLWindow2 *iface, BSTR v)
{
448
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
449 450 451 452 453 454
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_defaultStatus(IHTMLWindow2 *iface, BSTR *p)
{
455
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
456 457 458 459 460 461
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_put_status(IHTMLWindow2 *iface, BSTR v)
{
462
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
463 464 465 466 467 468 469 470

    WARN("(%p)->(%s)\n", This, debugstr_w(v));

    /*
     * FIXME: Since IE7, setting status is blocked, but still possible in certain circumstances.
     * Ignoring the call should be enough for us.
     */
    return S_OK;
471 472 473 474
}

static HRESULT WINAPI HTMLWindow2_get_status(IHTMLWindow2 *iface, BSTR *p)
{
475
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
476 477 478 479 480 481

    TRACE("(%p)->(%p)\n", This, p);

    /* See put_status */
    *p = NULL;
    return S_OK;
482 483 484
}

static HRESULT WINAPI HTMLWindow2_setTimeout(IHTMLWindow2 *iface, BSTR expression,
485
        LONG msec, VARIANT *language, LONG *timerID)
486
{
487
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
488 489
    VARIANT expr_var;

490
    TRACE("(%p)->(%s %d %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
491 492 493 494

    V_VT(&expr_var) = VT_BSTR;
    V_BSTR(&expr_var) = expression;

495
    return IHTMLWindow3_setTimeout(&This->IHTMLWindow3_iface, &expr_var, msec, language, timerID);
496 497
}

498
static HRESULT WINAPI HTMLWindow2_clearTimeout(IHTMLWindow2 *iface, LONG timerID)
499
{
500
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
501

502
    TRACE("(%p)->(%d)\n", This, timerID);
503

504
    return clear_task_timer(&This->doc->basedoc, FALSE, timerID);
505 506
}

507 508
#define MAX_MESSAGE_LEN 2000

509 510
static HRESULT WINAPI HTMLWindow2_alert(IHTMLWindow2 *iface, BSTR message)
{
511
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
512 513
    WCHAR title[100], *msg = message;
    DWORD len;
514 515 516

    TRACE("(%p)->(%s)\n", This, debugstr_w(message));

517 518
    if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, title,
                    sizeof(title)/sizeof(WCHAR))) {
519 520 521 522
        WARN("Could not load message box title: %d\n", GetLastError());
        return S_OK;
    }

523 524 525 526 527 528 529 530 531 532 533 534
    len = SysStringLen(message);
    if(len > MAX_MESSAGE_LEN) {
        msg = heap_alloc((MAX_MESSAGE_LEN+1)*sizeof(WCHAR));
        if(!msg)
            return E_OUTOFMEMORY;
        memcpy(msg, message, MAX_MESSAGE_LEN*sizeof(WCHAR));
        msg[MAX_MESSAGE_LEN] = 0;
    }

    MessageBoxW(This->doc_obj->hwnd, msg, title, MB_ICONWARNING);
    if(msg != message)
        heap_free(msg);
535
    return S_OK;
536 537 538 539 540
}

static HRESULT WINAPI HTMLWindow2_confirm(IHTMLWindow2 *iface, BSTR message,
        VARIANT_BOOL *confirmed)
{
541
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
542 543 544 545 546 547 548 549 550 551 552 553 554
    WCHAR wszTitle[100];

    TRACE("(%p)->(%s %p)\n", This, debugstr_w(message), confirmed);

    if(!confirmed) return E_INVALIDARG;

    if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, wszTitle,
                sizeof(wszTitle)/sizeof(WCHAR))) {
        WARN("Could not load message box title: %d\n", GetLastError());
        *confirmed = VARIANT_TRUE;
        return S_OK;
    }

555
    if(MessageBoxW(This->doc_obj->hwnd, message, wszTitle,
556 557 558 559 560
                MB_OKCANCEL|MB_ICONQUESTION)==IDOK)
        *confirmed = VARIANT_TRUE;
    else *confirmed = VARIANT_FALSE;

    return S_OK;
561 562
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
typedef struct
{
    BSTR message;
    BSTR dststr;
    VARIANT *textdata;
}prompt_arg;

static INT_PTR CALLBACK prompt_dlgproc(HWND hwnd, UINT msg,
        WPARAM wparam, LPARAM lparam)
{
    switch(msg)
    {
        case WM_INITDIALOG:
        {
            prompt_arg *arg = (prompt_arg*)lparam;
            WCHAR wszTitle[100];

            if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, wszTitle,
                        sizeof(wszTitle)/sizeof(WCHAR))) {
                WARN("Could not load message box title: %d\n", GetLastError());
                EndDialog(hwnd, wparam);
                return FALSE;
            }

            SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
            SetWindowTextW(hwnd, wszTitle);
            SetWindowTextW(GetDlgItem(hwnd, ID_PROMPT_PROMPT), arg->message);
            SetWindowTextW(GetDlgItem(hwnd, ID_PROMPT_EDIT), arg->dststr);
            return FALSE;
        }
        case WM_COMMAND:
            switch(wparam)
            {
                case MAKEWPARAM(IDCANCEL, BN_CLICKED):
                    EndDialog(hwnd, wparam);
                    return TRUE;
                case MAKEWPARAM(IDOK, BN_CLICKED):
                {
                    prompt_arg *arg =
                        (prompt_arg*)GetWindowLongPtrW(hwnd, DWLP_USER);
                    HWND hwndPrompt = GetDlgItem(hwnd, ID_PROMPT_EDIT);
                    INT len = GetWindowTextLengthW(hwndPrompt);

                    if(!arg->textdata)
                    {
                        EndDialog(hwnd, wparam);
                        return TRUE;
                    }

                    V_VT(arg->textdata) = VT_BSTR;
                    if(!len && !arg->dststr)
                        V_BSTR(arg->textdata) = NULL;
                    else
                    {
                        V_BSTR(arg->textdata) = SysAllocStringLen(NULL, len);
                        GetWindowTextW(hwndPrompt, V_BSTR(arg->textdata), len+1);
                    }
                    EndDialog(hwnd, wparam);
                    return TRUE;
                }
            }
            return FALSE;
        case WM_CLOSE:
            EndDialog(hwnd, IDCANCEL);
            return TRUE;
        default:
            return FALSE;
    }
}

633 634 635
static HRESULT WINAPI HTMLWindow2_prompt(IHTMLWindow2 *iface, BSTR message,
        BSTR dststr, VARIANT *textdata)
{
636
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
637 638 639 640 641 642 643 644 645 646 647
    prompt_arg arg;

    TRACE("(%p)->(%s %s %p)\n", This, debugstr_w(message), debugstr_w(dststr), textdata);

    if(textdata) V_VT(textdata) = VT_NULL;

    arg.message = message;
    arg.dststr = dststr;
    arg.textdata = textdata;

    DialogBoxParamW(hInst, MAKEINTRESOURCEW(ID_PROMPT_DIALOG),
648
            This->doc_obj->hwnd, prompt_dlgproc, (LPARAM)&arg);
649
    return S_OK;
650 651 652 653
}

static HRESULT WINAPI HTMLWindow2_get_Image(IHTMLWindow2 *iface, IHTMLImageElementFactory **p)
{
654
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
655 656 657 658 659 660

    TRACE("(%p)->(%p)\n", This, p);

    if(!This->image_factory)
        This->image_factory = HTMLImageElementFactory_Create(This);

661
    *p = &This->image_factory->IHTMLImageElementFactory_iface;
662 663 664
    IHTMLImageElementFactory_AddRef(*p);

    return S_OK;
665 666 667 668
}

static HRESULT WINAPI HTMLWindow2_get_location(IHTMLWindow2 *iface, IHTMLLocation **p)
{
669
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
670 671
    HTMLLocation *location;
    HRESULT hres;
672 673 674

    TRACE("(%p)->(%p)\n", This, p);

675 676 677
    hres = get_location(This, &location);
    if(FAILED(hres))
        return hres;
678

679
    *p = &location->IHTMLLocation_iface;
680
    return S_OK;
681 682 683 684
}

static HRESULT WINAPI HTMLWindow2_get_history(IHTMLWindow2 *iface, IOmHistory **p)
{
685
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
686 687 688 689 690 691
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_close(IHTMLWindow2 *iface)
{
692
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
693 694 695 696 697 698
    FIXME("(%p)->()\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_put_opener(IHTMLWindow2 *iface, VARIANT v)
{
699
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
700 701 702 703 704 705
    FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_opener(IHTMLWindow2 *iface, VARIANT *p)
{
706
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
707 708 709 710 711 712
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_navigator(IHTMLWindow2 *iface, IOmNavigator **p)
{
713
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
714 715 716 717 718

    TRACE("(%p)->(%p)\n", This, p);

    *p = OmNavigator_Create();
    return S_OK;
719 720 721 722
}

static HRESULT WINAPI HTMLWindow2_put_name(IHTMLWindow2 *iface, BSTR v)
{
723
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
724 725 726 727 728
    nsAString name_str;
    nsresult nsres;

    TRACE("(%p)->(%s)\n", This, debugstr_w(v));

729
    nsAString_InitDepend(&name_str, v);
730 731 732 733 734 735
    nsres = nsIDOMWindow_SetName(This->nswindow, &name_str);
    nsAString_Finish(&name_str);
    if(NS_FAILED(nsres))
        ERR("SetName failed: %08x\n", nsres);

    return S_OK;
736 737 738 739
}

static HRESULT WINAPI HTMLWindow2_get_name(IHTMLWindow2 *iface, BSTR *p)
{
740
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
    nsAString name_str;
    nsresult nsres;
    HRESULT hres;

    TRACE("(%p)->(%p)\n", This, p);

    nsAString_Init(&name_str, NULL);
    nsres = nsIDOMWindow_GetName(This->nswindow, &name_str);
    if(NS_SUCCEEDED(nsres)) {
        const PRUnichar *name;

        nsAString_GetData(&name_str, &name);
        if(*name) {
            *p = SysAllocString(name);
            hres = *p ? S_OK : E_OUTOFMEMORY;
        }else {
            *p = NULL;
            hres = S_OK;
        }
    }else {
        ERR("GetName failed: %08x\n", nsres);
        hres = E_FAIL;
    }
    nsAString_Finish(&name_str);

    return hres;
767 768 769 770
}

static HRESULT WINAPI HTMLWindow2_get_parent(IHTMLWindow2 *iface, IHTMLWindow2 **p)
{
771
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
772 773 774
    TRACE("(%p)->(%p)\n", This, p);

    if(This->parent) {
775
        *p = &This->parent->IHTMLWindow2_iface;
776 777 778 779 780
        IHTMLWindow2_AddRef(*p);
    }else
        *p = NULL;

    return S_OK;
781 782 783 784 785
}

static HRESULT WINAPI HTMLWindow2_open(IHTMLWindow2 *iface, BSTR url, BSTR name,
         BSTR features, VARIANT_BOOL replace, IHTMLWindow2 **pomWindowResult)
{
786
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
787 788 789 790 791 792 793
    FIXME("(%p)->(%s %s %s %x %p)\n", This, debugstr_w(url), debugstr_w(name),
          debugstr_w(features), replace, pomWindowResult);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_self(IHTMLWindow2 *iface, IHTMLWindow2 **p)
{
794
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
795 796 797 798

    TRACE("(%p)->(%p)\n", This, p);

    /* FIXME: We should return kind of proxy window here. */
799 800
    IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
    *p = &This->IHTMLWindow2_iface;
801
    return S_OK;
802 803 804 805
}

static HRESULT WINAPI HTMLWindow2_get_top(IHTMLWindow2 *iface, IHTMLWindow2 **p)
{
806 807
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
    HTMLWindow *curr;
808 809 810 811 812
    TRACE("(%p)->(%p)\n", This, p);

    curr = This;
    while(curr->parent)
        curr = curr->parent;
813
    *p = &curr->IHTMLWindow2_iface;
814 815 816
    IHTMLWindow2_AddRef(*p);

    return S_OK;
817 818 819 820
}

static HRESULT WINAPI HTMLWindow2_get_window(IHTMLWindow2 *iface, IHTMLWindow2 **p)
{
821
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
822 823 824 825

    TRACE("(%p)->(%p)\n", This, p);

    /* FIXME: We should return kind of proxy window here. */
826 827
    IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
    *p = &This->IHTMLWindow2_iface;
828
    return S_OK;
829 830 831 832
}

static HRESULT WINAPI HTMLWindow2_navigate(IHTMLWindow2 *iface, BSTR url)
{
833
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
834 835 836 837 838 839
    FIXME("(%p)->(%s)\n", This, debugstr_w(url));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_put_onfocus(IHTMLWindow2 *iface, VARIANT v)
{
840
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
841 842 843 844 845 846
    FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_onfocus(IHTMLWindow2 *iface, VARIANT *p)
{
847
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
848 849 850 851 852 853
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_put_onblur(IHTMLWindow2 *iface, VARIANT v)
{
854
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
855 856 857 858 859 860
    FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_onblur(IHTMLWindow2 *iface, VARIANT *p)
{
861
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
862 863 864 865 866 867
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_put_onload(IHTMLWindow2 *iface, VARIANT v)
{
868
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
869 870 871 872

    TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));

    return set_window_event(This, EVENTID_LOAD, &v);
873 874 875 876
}

static HRESULT WINAPI HTMLWindow2_get_onload(IHTMLWindow2 *iface, VARIANT *p)
{
877
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
878 879 880 881

    TRACE("(%p)->(%p)\n", This, p);

    return get_window_event(This, EVENTID_LOAD, p);
882 883 884 885
}

static HRESULT WINAPI HTMLWindow2_put_onbeforeunload(IHTMLWindow2 *iface, VARIANT v)
{
886
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
887 888 889 890

    TRACE("(%p)->(v(%d))\n", This, V_VT(&v));

    return set_window_event(This, EVENTID_BEFOREUNLOAD, &v);
891 892 893 894
}

static HRESULT WINAPI HTMLWindow2_get_onbeforeunload(IHTMLWindow2 *iface, VARIANT *p)
{
895
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
896 897 898 899

    TRACE("(%p)->(%p)\n", This, p);

    return get_window_event(This, EVENTID_BEFOREUNLOAD, p);
900 901 902 903
}

static HRESULT WINAPI HTMLWindow2_put_onunload(IHTMLWindow2 *iface, VARIANT v)
{
904
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
905 906 907 908 909 910
    FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_onunload(IHTMLWindow2 *iface, VARIANT *p)
{
911
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
912 913 914 915 916 917
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_put_onhelp(IHTMLWindow2 *iface, VARIANT v)
{
918
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
919 920 921 922 923 924
    FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_onhelp(IHTMLWindow2 *iface, VARIANT *p)
{
925
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
926 927 928 929 930 931
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_put_onerror(IHTMLWindow2 *iface, VARIANT v)
{
932
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
933 934 935 936 937 938
    FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_onerror(IHTMLWindow2 *iface, VARIANT *p)
{
939
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
940 941 942 943 944 945
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_put_onresize(IHTMLWindow2 *iface, VARIANT v)
{
946
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
947 948 949 950

    TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));

    return set_window_event(This, EVENTID_RESIZE, &v);
951 952 953 954
}

static HRESULT WINAPI HTMLWindow2_get_onresize(IHTMLWindow2 *iface, VARIANT *p)
{
955
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
956 957 958 959

    TRACE("(%p)->(%p)\n", This, p);

    return get_window_event(This, EVENTID_RESIZE, p);
960 961 962 963
}

static HRESULT WINAPI HTMLWindow2_put_onscroll(IHTMLWindow2 *iface, VARIANT v)
{
964
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
965 966 967 968 969 970
    FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_onscroll(IHTMLWindow2 *iface, VARIANT *p)
{
971
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
972 973 974 975 976 977
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_document(IHTMLWindow2 *iface, IHTMLDocument2 **p)
{
978
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
979 980 981 982 983

    TRACE("(%p)->(%p)\n", This, p);

    if(This->doc) {
        /* FIXME: We should return a wrapper object here */
984
        *p = &This->doc->basedoc.IHTMLDocument2_iface;
985 986 987 988 989 990
        IHTMLDocument2_AddRef(*p);
    }else {
        *p = NULL;
    }

    return S_OK;
991 992 993 994
}

static HRESULT WINAPI HTMLWindow2_get_event(IHTMLWindow2 *iface, IHTMLEventObj **p)
{
995
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
996 997 998 999 1000 1001 1002

    TRACE("(%p)->(%p)\n", This, p);

    if(This->event)
        IHTMLEventObj_AddRef(This->event);
    *p = This->event;
    return S_OK;
1003 1004 1005 1006
}

static HRESULT WINAPI HTMLWindow2_get__newEnum(IHTMLWindow2 *iface, IUnknown **p)
{
1007
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1008 1009 1010 1011 1012 1013 1014
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_showModalDialog(IHTMLWindow2 *iface, BSTR dialog,
        VARIANT *varArgIn, VARIANT *varOptions, VARIANT *varArgOut)
{
1015
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1016 1017 1018 1019 1020 1021 1022
    FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(dialog), varArgIn, varOptions, varArgOut);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_showHelp(IHTMLWindow2 *iface, BSTR helpURL, VARIANT helpArg,
        BSTR features)
{
1023
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1024 1025 1026 1027 1028 1029
    FIXME("(%p)->(%s v(%d) %s)\n", This, debugstr_w(helpURL), V_VT(&helpArg), debugstr_w(features));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_screen(IHTMLWindow2 *iface, IHTMLScreen **p)
{
1030
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044

    TRACE("(%p)->(%p)\n", This, p);

    if(!This->screen) {
        HRESULT hres;

        hres = HTMLScreen_Create(&This->screen);
        if(FAILED(hres))
            return hres;
    }

    *p = This->screen;
    IHTMLScreen_AddRef(This->screen);
    return S_OK;
1045 1046 1047 1048
}

static HRESULT WINAPI HTMLWindow2_get_Option(IHTMLWindow2 *iface, IHTMLOptionElementFactory **p)
{
1049
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1050 1051 1052

    TRACE("(%p)->(%p)\n", This, p);

1053 1054
    if(!This->option_factory)
        This->option_factory = HTMLOptionElementFactory_Create(This);
1055

1056
    *p = &This->option_factory->IHTMLOptionElementFactory_iface;
1057 1058 1059
    IHTMLOptionElementFactory_AddRef(*p);

    return S_OK;
1060 1061 1062 1063
}

static HRESULT WINAPI HTMLWindow2_focus(IHTMLWindow2 *iface)
{
1064
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1065 1066 1067 1068 1069 1070

    TRACE("(%p)->()\n", This);

    if(This->doc_obj)
        SetFocus(This->doc_obj->hwnd);
    return S_OK;
1071 1072 1073 1074
}

static HRESULT WINAPI HTMLWindow2_get_closed(IHTMLWindow2 *iface, VARIANT_BOOL *p)
{
1075
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1076 1077 1078 1079 1080 1081
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_blur(IHTMLWindow2 *iface)
{
1082
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1083 1084 1085 1086
    FIXME("(%p)->()\n", This);
    return E_NOTIMPL;
}

1087
static HRESULT WINAPI HTMLWindow2_scroll(IHTMLWindow2 *iface, LONG x, LONG y)
1088
{
1089
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1090
    FIXME("(%p)->(%d %d)\n", This, x, y);
1091 1092 1093 1094 1095
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_clientInformation(IHTMLWindow2 *iface, IOmNavigator **p)
{
1096
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1097 1098 1099 1100 1101
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_setInterval(IHTMLWindow2 *iface, BSTR expression,
1102
        LONG msec, VARIANT *language, LONG *timerID)
1103
{
1104
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1105 1106
    VARIANT expr;

1107
    TRACE("(%p)->(%s %d %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
1108 1109 1110

    V_VT(&expr) = VT_BSTR;
    V_BSTR(&expr) = expression;
1111
    return IHTMLWindow3_setInterval(&This->IHTMLWindow3_iface, &expr, msec, language, timerID);
1112 1113
}

1114
static HRESULT WINAPI HTMLWindow2_clearInterval(IHTMLWindow2 *iface, LONG timerID)
1115
{
1116
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1117

1118
    TRACE("(%p)->(%d)\n", This, timerID);
1119

1120
    return clear_task_timer(&This->doc->basedoc, TRUE, timerID);
1121 1122 1123 1124
}

static HRESULT WINAPI HTMLWindow2_put_offscreenBuffering(IHTMLWindow2 *iface, VARIANT v)
{
1125
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1126 1127 1128 1129 1130 1131
    FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_offscreenBuffering(IHTMLWindow2 *iface, VARIANT *p)
{
1132
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1133 1134 1135 1136 1137 1138 1139
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_execScript(IHTMLWindow2 *iface, BSTR scode, BSTR language,
        VARIANT *pvarRet)
{
1140
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1141 1142 1143 1144

    TRACE("(%p)->(%s %s %p)\n", This, debugstr_w(scode), debugstr_w(language), pvarRet);

    return exec_script(This, scode, language, pvarRet);
1145 1146 1147 1148
}

static HRESULT WINAPI HTMLWindow2_toString(IHTMLWindow2 *iface, BSTR *String)
{
1149
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159

    static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};

    TRACE("(%p)->(%p)\n", This, String);

    if(!String)
        return E_INVALIDARG;

    *String = SysAllocString(objectW);
    return *String ? S_OK : E_OUTOFMEMORY;
1160 1161
}

1162
static HRESULT WINAPI HTMLWindow2_scrollBy(IHTMLWindow2 *iface, LONG x, LONG y)
1163
{
1164
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1165 1166
    nsresult nsres;

1167
    TRACE("(%p)->(%d %d)\n", This, x, y);
1168 1169 1170 1171 1172 1173

    nsres = nsIDOMWindow_ScrollBy(This->nswindow, x, y);
    if(NS_FAILED(nsres))
        ERR("ScrollBy failed: %08x\n", nsres);

    return S_OK;
1174 1175
}

1176
static HRESULT WINAPI HTMLWindow2_scrollTo(IHTMLWindow2 *iface, LONG x, LONG y)
1177
{
1178
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1179 1180
    nsresult nsres;

1181
    TRACE("(%p)->(%d %d)\n", This, x, y);
1182 1183 1184 1185 1186 1187

    nsres = nsIDOMWindow_ScrollTo(This->nswindow, x, y);
    if(NS_FAILED(nsres))
        ERR("ScrollTo failed: %08x\n", nsres);

    return S_OK;
1188 1189
}

1190
static HRESULT WINAPI HTMLWindow2_moveTo(IHTMLWindow2 *iface, LONG x, LONG y)
1191
{
1192
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1193
    FIXME("(%p)->(%d %d)\n", This, x, y);
1194 1195 1196
    return E_NOTIMPL;
}

1197
static HRESULT WINAPI HTMLWindow2_moveBy(IHTMLWindow2 *iface, LONG x, LONG y)
1198
{
1199
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1200
    FIXME("(%p)->(%d %d)\n", This, x, y);
1201 1202 1203
    return E_NOTIMPL;
}

1204
static HRESULT WINAPI HTMLWindow2_resizeTo(IHTMLWindow2 *iface, LONG x, LONG y)
1205
{
1206
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1207
    FIXME("(%p)->(%d %d)\n", This, x, y);
1208 1209 1210
    return E_NOTIMPL;
}

1211
static HRESULT WINAPI HTMLWindow2_resizeBy(IHTMLWindow2 *iface, LONG x, LONG y)
1212
{
1213
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1214
    FIXME("(%p)->(%d %d)\n", This, x, y);
1215 1216 1217 1218 1219
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow2_get_external(IHTMLWindow2 *iface, IDispatch **p)
{
1220
    HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1221 1222 1223 1224 1225

    TRACE("(%p)->(%p)\n", This, p);

    *p = NULL;

1226
    if(!This->doc_obj->hostui)
1227 1228
        return S_OK;

1229
    return IDocHostUIHandler_GetExternal(This->doc_obj->hostui, p);
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
}

static const IHTMLWindow2Vtbl HTMLWindow2Vtbl = {
    HTMLWindow2_QueryInterface,
    HTMLWindow2_AddRef,
    HTMLWindow2_Release,
    HTMLWindow2_GetTypeInfoCount,
    HTMLWindow2_GetTypeInfo,
    HTMLWindow2_GetIDsOfNames,
    HTMLWindow2_Invoke,
    HTMLWindow2_item,
    HTMLWindow2_get_length,
    HTMLWindow2_get_frames,
    HTMLWindow2_put_defaultStatus,
    HTMLWindow2_get_defaultStatus,
    HTMLWindow2_put_status,
    HTMLWindow2_get_status,
    HTMLWindow2_setTimeout,
    HTMLWindow2_clearTimeout,
    HTMLWindow2_alert,
    HTMLWindow2_confirm,
    HTMLWindow2_prompt,
    HTMLWindow2_get_Image,
    HTMLWindow2_get_location,
    HTMLWindow2_get_history,
    HTMLWindow2_close,
    HTMLWindow2_put_opener,
    HTMLWindow2_get_opener,
    HTMLWindow2_get_navigator,
    HTMLWindow2_put_name,
    HTMLWindow2_get_name,
    HTMLWindow2_get_parent,
    HTMLWindow2_open,
    HTMLWindow2_get_self,
    HTMLWindow2_get_top,
    HTMLWindow2_get_window,
    HTMLWindow2_navigate,
    HTMLWindow2_put_onfocus,
    HTMLWindow2_get_onfocus,
    HTMLWindow2_put_onblur,
    HTMLWindow2_get_onblur,
    HTMLWindow2_put_onload,
    HTMLWindow2_get_onload,
    HTMLWindow2_put_onbeforeunload,
    HTMLWindow2_get_onbeforeunload,
    HTMLWindow2_put_onunload,
    HTMLWindow2_get_onunload,
    HTMLWindow2_put_onhelp,
    HTMLWindow2_get_onhelp,
    HTMLWindow2_put_onerror,
    HTMLWindow2_get_onerror,
    HTMLWindow2_put_onresize,
    HTMLWindow2_get_onresize,
    HTMLWindow2_put_onscroll,
    HTMLWindow2_get_onscroll,
    HTMLWindow2_get_document,
    HTMLWindow2_get_event,
    HTMLWindow2_get__newEnum,
    HTMLWindow2_showModalDialog,
    HTMLWindow2_showHelp,
    HTMLWindow2_get_screen,
    HTMLWindow2_get_Option,
    HTMLWindow2_focus,
    HTMLWindow2_get_closed,
    HTMLWindow2_blur,
    HTMLWindow2_scroll,
    HTMLWindow2_get_clientInformation,
    HTMLWindow2_setInterval,
    HTMLWindow2_clearInterval,
    HTMLWindow2_put_offscreenBuffering,
    HTMLWindow2_get_offscreenBuffering,
    HTMLWindow2_execScript,
    HTMLWindow2_toString,
    HTMLWindow2_scrollBy,
    HTMLWindow2_scrollTo,
    HTMLWindow2_moveTo,
    HTMLWindow2_moveBy,
    HTMLWindow2_resizeTo,
    HTMLWindow2_resizeBy,
    HTMLWindow2_get_external
};

1312 1313 1314 1315
static inline HTMLWindow *impl_from_IHTMLWindow3(IHTMLWindow3 *iface)
{
    return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow3_iface);
}
1316 1317 1318

static HRESULT WINAPI HTMLWindow3_QueryInterface(IHTMLWindow3 *iface, REFIID riid, void **ppv)
{
1319
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1320

1321
    return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1322 1323 1324 1325
}

static ULONG WINAPI HTMLWindow3_AddRef(IHTMLWindow3 *iface)
{
1326
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1327

1328
    return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1329 1330 1331 1332
}

static ULONG WINAPI HTMLWindow3_Release(IHTMLWindow3 *iface)
{
1333
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1334

1335
    return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1336 1337 1338 1339
}

static HRESULT WINAPI HTMLWindow3_GetTypeInfoCount(IHTMLWindow3 *iface, UINT *pctinfo)
{
1340
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1341

1342
    return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
1343 1344 1345 1346 1347
}

static HRESULT WINAPI HTMLWindow3_GetTypeInfo(IHTMLWindow3 *iface, UINT iTInfo,
                                              LCID lcid, ITypeInfo **ppTInfo)
{
1348
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1349

1350
    return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1351 1352 1353 1354 1355 1356
}

static HRESULT WINAPI HTMLWindow3_GetIDsOfNames(IHTMLWindow3 *iface, REFIID riid,
                                                LPOLESTR *rgszNames, UINT cNames,
                                                LCID lcid, DISPID *rgDispId)
{
1357
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1358

1359 1360
    return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
            rgDispId);
1361 1362 1363 1364 1365 1366
}

static HRESULT WINAPI HTMLWindow3_Invoke(IHTMLWindow3 *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
1367
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1368

1369 1370
    return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
            pDispParams, pVarResult, pExcepInfo, puArgErr);
1371 1372
}

1373
static HRESULT WINAPI HTMLWindow3_get_screenLeft(IHTMLWindow3 *iface, LONG *p)
1374
{
1375
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1376 1377 1378 1379
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

1380
static HRESULT WINAPI HTMLWindow3_get_screenTop(IHTMLWindow3 *iface, LONG *p)
1381
{
1382
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1383 1384 1385 1386 1387 1388
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow3_attachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp, VARIANT_BOOL *pfResult)
{
1389
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1390 1391 1392

    TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(event), pDisp, pfResult);

1393 1394 1395 1396 1397
    if(!This->doc) {
        FIXME("No document\n");
        return E_FAIL;
    }

1398
    return attach_event(&This->doc->body_event_target, NULL, &This->doc->basedoc, event, pDisp, pfResult);
1399 1400 1401 1402
}

static HRESULT WINAPI HTMLWindow3_detachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp)
{
1403
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1404 1405 1406 1407
    FIXME("(%p)->()\n", This);
    return E_NOTIMPL;
}

1408 1409
static HRESULT window_set_timer(HTMLWindow *This, VARIANT *expr, LONG msec, VARIANT *language,
        BOOL interval, LONG *timer_id)
1410
{
1411
    IDispatch *disp = NULL;
1412

1413
    switch(V_VT(expr)) {
1414
    case VT_DISPATCH:
1415 1416 1417 1418 1419
        disp = V_DISPATCH(expr);
        IDispatch_AddRef(disp);
        break;

    case VT_BSTR:
1420
        disp = script_parse_event(This, V_BSTR(expr));
1421 1422 1423
        break;

    default:
1424 1425
        FIXME("unimplemented vt=%d\n", V_VT(expr));
        return E_NOTIMPL;
1426 1427
    }

1428 1429 1430
    if(!disp)
        return E_FAIL;

1431
    *timer_id = set_task_timer(&This->doc->basedoc, msec, interval, disp);
1432 1433
    IDispatch_Release(disp);

1434
    return S_OK;
1435 1436
}

1437 1438
static HRESULT WINAPI HTMLWindow3_setTimeout(IHTMLWindow3 *iface, VARIANT *expression, LONG msec,
        VARIANT *language, LONG *timerID)
1439
{
1440
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1441

1442
    TRACE("(%p)->(%p(%d) %d %p %p)\n", This, expression, V_VT(expression), msec, language, timerID);
1443

1444
    return window_set_timer(This, expression, msec, language, FALSE, timerID);
1445 1446
}

1447 1448
static HRESULT WINAPI HTMLWindow3_setInterval(IHTMLWindow3 *iface, VARIANT *expression, LONG msec,
        VARIANT *language, LONG *timerID)
1449
{
1450
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1451

1452
    TRACE("(%p)->(%p %d %p %p)\n", This, expression, msec, language, timerID);
1453 1454

    return window_set_timer(This, expression, msec, language, TRUE, timerID);
1455 1456 1457 1458
}

static HRESULT WINAPI HTMLWindow3_print(IHTMLWindow3 *iface)
{
1459
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1460 1461 1462 1463 1464 1465
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow3_put_onbeforeprint(IHTMLWindow3 *iface, VARIANT v)
{
1466
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1467 1468 1469 1470 1471 1472
    FIXME("(%p)->()\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow3_get_onbeforeprint(IHTMLWindow3 *iface, VARIANT *p)
{
1473
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1474 1475 1476 1477 1478 1479
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow3_put_onafterprint(IHTMLWindow3 *iface, VARIANT v)
{
1480
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1481 1482 1483 1484 1485 1486
    FIXME("(%p)->()\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow3_get_onafterprint(IHTMLWindow3 *iface, VARIANT *p)
{
1487
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1488 1489 1490 1491 1492 1493
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow3_get_clipboardData(IHTMLWindow3 *iface, IHTMLDataTransfer **p)
{
1494
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1495 1496 1497 1498 1499 1500 1501
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow3_showModelessDialog(IHTMLWindow3 *iface, BSTR url,
        VARIANT *varArgIn, VARIANT *options, IHTMLWindow2 **pDialog)
{
1502
    HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
    FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(url), varArgIn, options, pDialog);
    return E_NOTIMPL;
}

static const IHTMLWindow3Vtbl HTMLWindow3Vtbl = {
    HTMLWindow3_QueryInterface,
    HTMLWindow3_AddRef,
    HTMLWindow3_Release,
    HTMLWindow3_GetTypeInfoCount,
    HTMLWindow3_GetTypeInfo,
    HTMLWindow3_GetIDsOfNames,
    HTMLWindow3_Invoke,
    HTMLWindow3_get_screenLeft,
    HTMLWindow3_get_screenTop,
    HTMLWindow3_attachEvent,
    HTMLWindow3_detachEvent,
    HTMLWindow3_setTimeout,
    HTMLWindow3_setInterval,
    HTMLWindow3_print,
    HTMLWindow3_put_onbeforeprint,
    HTMLWindow3_get_onbeforeprint,
    HTMLWindow3_put_onafterprint,
    HTMLWindow3_get_onafterprint,
    HTMLWindow3_get_clipboardData,
    HTMLWindow3_showModelessDialog
};

1530 1531 1532 1533
static inline HTMLWindow *impl_from_IHTMLWindow4(IHTMLWindow4 *iface)
{
    return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow4_iface);
}
1534 1535 1536

static HRESULT WINAPI HTMLWindow4_QueryInterface(IHTMLWindow4 *iface, REFIID riid, void **ppv)
{
1537
    HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1538

1539
    return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1540 1541 1542 1543
}

static ULONG WINAPI HTMLWindow4_AddRef(IHTMLWindow4 *iface)
{
1544
    HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1545

1546
    return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1547 1548 1549 1550
}

static ULONG WINAPI HTMLWindow4_Release(IHTMLWindow4 *iface)
{
1551
    HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1552

1553
    return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1554 1555 1556 1557
}

static HRESULT WINAPI HTMLWindow4_GetTypeInfoCount(IHTMLWindow4 *iface, UINT *pctinfo)
{
1558
    HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1559

1560
    return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
1561 1562 1563 1564 1565
}

static HRESULT WINAPI HTMLWindow4_GetTypeInfo(IHTMLWindow4 *iface, UINT iTInfo,
                                              LCID lcid, ITypeInfo **ppTInfo)
{
1566
    HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1567

1568
    return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1569 1570 1571 1572 1573 1574
}

static HRESULT WINAPI HTMLWindow4_GetIDsOfNames(IHTMLWindow4 *iface, REFIID riid,
                                                LPOLESTR *rgszNames, UINT cNames,
                                                LCID lcid, DISPID *rgDispId)
{
1575
    HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1576

1577 1578
    return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
            rgDispId);
1579 1580 1581 1582 1583 1584
}

static HRESULT WINAPI HTMLWindow4_Invoke(IHTMLWindow4 *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
1585
    HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1586

1587 1588
    return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
            pDispParams, pVarResult, pExcepInfo, puArgErr);
1589 1590 1591 1592 1593
}

static HRESULT WINAPI HTMLWindow4_createPopup(IHTMLWindow4 *iface, VARIANT *varArgIn,
                            IDispatch **ppPopup)
{
1594
    HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1595 1596 1597 1598 1599 1600
    FIXME("(%p)->(%p %p)\n", This, varArgIn, ppPopup);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLWindow4_get_frameElement(IHTMLWindow4 *iface, IHTMLFrameBase **p)
{
1601
    HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1602 1603 1604
    TRACE("(%p)->(%p)\n", This, p);

    if(This->frame_element) {
1605
        *p = &This->frame_element->IHTMLFrameBase_iface;
1606 1607 1608 1609 1610
        IHTMLFrameBase_AddRef(*p);
    }else
        *p = NULL;

    return S_OK;
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
}

static const IHTMLWindow4Vtbl HTMLWindow4Vtbl = {
    HTMLWindow4_QueryInterface,
    HTMLWindow4_AddRef,
    HTMLWindow4_Release,
    HTMLWindow4_GetTypeInfoCount,
    HTMLWindow4_GetTypeInfo,
    HTMLWindow4_GetIDsOfNames,
    HTMLWindow4_Invoke,
    HTMLWindow4_createPopup,
    HTMLWindow4_get_frameElement
};

1625 1626 1627 1628
static inline HTMLWindow *impl_from_IHTMLPrivateWindow(IHTMLPrivateWindow *iface)
{
    return CONTAINING_RECORD(iface, HTMLWindow, IHTMLPrivateWindow_iface);
}
1629 1630 1631

static HRESULT WINAPI HTMLPrivateWindow_QueryInterface(IHTMLPrivateWindow *iface, REFIID riid, void **ppv)
{
1632
    HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1633

1634
    return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1635 1636 1637 1638
}

static ULONG WINAPI HTMLPrivateWindow_AddRef(IHTMLPrivateWindow *iface)
{
1639
    HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1640

1641
    return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1642 1643 1644 1645
}

static ULONG WINAPI HTMLPrivateWindow_Release(IHTMLPrivateWindow *iface)
{
1646
    HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1647

1648
    return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1649 1650
}

1651 1652 1653 1654 1655 1656 1657
typedef struct {
    task_t header;
    HTMLWindow *window;
    IUri *uri;
} navigate_javascript_task_t;

static void navigate_javascript_proc(task_t *_task)
1658
{
1659 1660
    navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
    HTMLWindow *window = task->window;
1661
    VARIANT v;
1662
    BSTR code;
1663 1664 1665 1666
    HRESULT hres;

    static const WCHAR jscriptW[] = {'j','s','c','r','i','p','t',0};

1667 1668 1669 1670 1671 1672
    task->window->readystate = READYSTATE_COMPLETE;

    hres = IUri_GetPath(task->uri, &code);
    if(FAILED(hres))
        return;

1673 1674 1675 1676
    set_download_state(window->doc_obj, 1);

    V_VT(&v) = VT_EMPTY;
    hres = exec_script(window, code, jscriptW, &v);
1677
    SysFreeString(code);
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
    if(SUCCEEDED(hres) && V_VT(&v) != VT_EMPTY) {
        FIXME("javascirpt URL returned %s\n", debugstr_variant(&v));
        VariantClear(&v);
    }

    if(window->doc_obj->view_sink)
        IAdviseSink_OnViewChange(window->doc_obj->view_sink, DVASPECT_CONTENT, -1);

    set_download_state(window->doc_obj, 0);
}

1689
static void navigate_javascript_task_destr(task_t *_task)
1690 1691 1692 1693
{
    navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;

    IUri_Release(task->uri);
1694
    heap_free(task);
1695 1696
}

1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
typedef struct {
    task_t header;
    HTMLWindow *window;
    nsChannelBSC *bscallback;
    IMoniker *mon;
} navigate_task_t;

static void navigate_proc(task_t *_task)
{
    navigate_task_t *task = (navigate_task_t*)_task;
    HRESULT hres;

    hres = set_moniker(&task->window->doc_obj->basedoc, task->mon, NULL, task->bscallback, TRUE);
    if(SUCCEEDED(hres))
        hres = start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);

1713 1714 1715 1716 1717 1718
}

static void navigate_task_destr(task_t *_task)
{
    navigate_task_t *task = (navigate_task_t*)_task;

1719 1720
    IUnknown_Release((IUnknown*)task->bscallback);
    IMoniker_Release(task->mon);
1721
    heap_free(task);
1722 1723
}

1724 1725 1726
static HRESULT WINAPI HTMLPrivateWindow_SuperNavigate(IHTMLPrivateWindow *iface, BSTR url, BSTR arg2, BSTR arg3,
        BSTR arg4, VARIANT *post_data_var, VARIANT *headers_var, ULONG flags)
{
1727
    HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1728 1729 1730 1731 1732
    DWORD post_data_size = 0;
    BYTE *post_data = NULL;
    WCHAR *headers = NULL;
    nsChannelBSC *bsc;
    IMoniker *mon;
1733
    DWORD scheme;
1734
    BSTR new_url;
1735
    IUri *uri;
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
    HRESULT hres;

    TRACE("(%p)->(%s %s %s %s %s %s %x)\n", This, debugstr_w(url), debugstr_w(arg2), debugstr_w(arg3), debugstr_w(arg4),
          debugstr_variant(post_data_var), debugstr_variant(headers_var), flags);

    new_url = url;
    if(This->doc_obj->hostui) {
        OLECHAR *translated_url = NULL;

        hres = IDocHostUIHandler_TranslateUrl(This->doc_obj->hostui, 0, url, &translated_url);
        if(hres == S_OK && translated_url) {
            new_url = SysAllocString(translated_url);
            CoTaskMemFree(translated_url);
        }
    }

    if(This->doc_obj->client) {
        IOleCommandTarget *cmdtrg;

        hres = IOleClientSite_QueryInterface(This->doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
        if(SUCCEEDED(hres)) {
            VARIANT in, out;

            V_VT(&in) = VT_BSTR;
            V_BSTR(&in) = new_url;
            V_VT(&out) = VT_BOOL;
            V_BOOL(&out) = VARIANT_TRUE;
            hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &in, &out);
            IOleCommandTarget_Release(cmdtrg);
            if(SUCCEEDED(hres))
                VariantClear(&out);
        }
    }

1770
    hres = CreateUri(new_url, 0, 0, &uri);
1771 1772 1773 1774 1775
    if(new_url != url)
        SysFreeString(new_url);
    if(FAILED(hres))
        return hres;

1776 1777 1778 1779 1780 1781 1782
    hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
    if(FAILED(hres))
        return hres;

    /* FIXME: Why not set_ready_state? */
    This->readystate = READYSTATE_UNINITIALIZED;

1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
    if(post_data_var) {
        if(V_VT(post_data_var) == (VT_ARRAY|VT_UI1)) {
            SafeArrayAccessData(V_ARRAY(post_data_var), (void**)&post_data);
            post_data_size = V_ARRAY(post_data_var)->rgsabound[0].cElements;
        }
    }

    if(headers_var && V_VT(headers_var) != VT_EMPTY && V_VT(headers_var) != VT_ERROR) {
        if(V_VT(headers_var) != VT_BSTR)
            return E_INVALIDARG;

        headers = V_BSTR(headers_var);
    }

    hres = create_channelbsc(mon, headers, post_data, post_data_size, &bsc);
    if(post_data)
        SafeArrayUnaccessData(V_ARRAY(post_data_var));
    if(FAILED(hres)) {
        IMoniker_Release(mon);
        return hres;
    }

1805
    prepare_for_binding(&This->doc_obj->basedoc, mon, NULL, TRUE);
1806

1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
    hres = IUri_GetScheme(uri, &scheme);

    if(scheme != URL_SCHEME_JAVASCRIPT) {
        navigate_task_t *task;

        IUri_Release(uri);

        task = heap_alloc(sizeof(*task));
        if(!task) {
            IUnknown_Release((IUnknown*)bsc);
            IMoniker_Release(mon);
            return E_OUTOFMEMORY;
        }

        task->window = This;
        task->bscallback = bsc;
        task->mon = mon;
1824
        push_task(&task->header, navigate_proc, navigate_task_destr, This->task_magic);
1825 1826 1827 1828 1829

        /* Silently and repeated when real loading starts? */
        This->readystate = READYSTATE_LOADING;
    }else {
        navigate_javascript_task_t *task;
1830 1831 1832 1833

        IUnknown_Release((IUnknown*)bsc);
        IMoniker_Release(mon);

1834 1835 1836 1837 1838
        task = heap_alloc(sizeof(*task));
        if(!task) {
            IUri_Release(uri);
            return E_OUTOFMEMORY;
        }
1839

1840 1841
        task->window = This;
        task->uri = uri;
1842
        push_task(&task->header, navigate_javascript_proc, navigate_javascript_task_destr, This->task_magic);
1843 1844 1845 1846

        /* Why silently? */
        This->readystate = READYSTATE_COMPLETE;
    }
1847 1848

    return S_OK;
1849 1850 1851 1852
}

static HRESULT WINAPI HTMLPrivateWindow_GetPendingUrl(IHTMLPrivateWindow *iface, BSTR *url)
{
1853
    HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1854 1855 1856 1857 1858 1859
    FIXME("(%p)->(%p)\n", This, url);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLPrivateWindow_SetPICSTarget(IHTMLPrivateWindow *iface, IOleCommandTarget *cmdtrg)
{
1860
    HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1861 1862 1863 1864 1865 1866
    FIXME("(%p)->(%p)\n", This, cmdtrg);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLPrivateWindow_PICSComplete(IHTMLPrivateWindow *iface, int arg)
{
1867
    HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1868 1869 1870 1871 1872 1873
    FIXME("(%p)->(%x)\n", This, arg);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLPrivateWindow_FindWindowByName(IHTMLPrivateWindow *iface, LPCWSTR name, IHTMLWindow2 **ret)
{
1874
    HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1875 1876 1877 1878 1879 1880
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(name), ret);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLPrivateWindow_GetAddressBar(IHTMLPrivateWindow *iface, BSTR *url)
{
1881
    HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1882 1883 1884 1885 1886 1887 1888
    TRACE("(%p)->(%p)\n", This, url);

    if(!url)
        return E_INVALIDARG;

    *url = SysAllocString(This->url);
    return S_OK;
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
}

static const IHTMLPrivateWindowVtbl HTMLPrivateWindowVtbl = {
    HTMLPrivateWindow_QueryInterface,
    HTMLPrivateWindow_AddRef,
    HTMLPrivateWindow_Release,
    HTMLPrivateWindow_SuperNavigate,
    HTMLPrivateWindow_GetPendingUrl,
    HTMLPrivateWindow_SetPICSTarget,
    HTMLPrivateWindow_PICSComplete,
    HTMLPrivateWindow_FindWindowByName,
    HTMLPrivateWindow_GetAddressBar
};

1903 1904 1905 1906
static inline HTMLWindow *impl_from_IDispatchEx(IDispatchEx *iface)
{
    return CONTAINING_RECORD(iface, HTMLWindow, IDispatchEx_iface);
}
1907 1908 1909

static HRESULT WINAPI WindowDispEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
{
1910
    HTMLWindow *This = impl_from_IDispatchEx(iface);
1911

1912
    return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1913 1914 1915 1916
}

static ULONG WINAPI WindowDispEx_AddRef(IDispatchEx *iface)
{
1917
    HTMLWindow *This = impl_from_IDispatchEx(iface);
1918

1919
    return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1920 1921 1922 1923
}

static ULONG WINAPI WindowDispEx_Release(IDispatchEx *iface)
{
1924
    HTMLWindow *This = impl_from_IDispatchEx(iface);
1925

1926
    return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1927 1928 1929 1930
}

static HRESULT WINAPI WindowDispEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
{
1931
    HTMLWindow *This = impl_from_IDispatchEx(iface);
1932 1933 1934

    TRACE("(%p)->(%p)\n", This, pctinfo);

1935
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
1936 1937 1938 1939 1940
}

static HRESULT WINAPI WindowDispEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
                                               LCID lcid, ITypeInfo **ppTInfo)
{
1941
    HTMLWindow *This = impl_from_IDispatchEx(iface);
1942 1943 1944

    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);

1945
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1946 1947 1948 1949 1950 1951
}

static HRESULT WINAPI WindowDispEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
                                                 LPOLESTR *rgszNames, UINT cNames,
                                                 LCID lcid, DISPID *rgDispId)
{
1952
    HTMLWindow *This = impl_from_IDispatchEx(iface);
1953 1954
    UINT i;
    HRESULT hres;
1955

1956
    WARN("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1957 1958
          lcid, rgDispId);

1959 1960
    for(i=0; i < cNames; i++) {
        /* We shouldn't use script's IDispatchEx here, so we shouldn't use GetDispID */
1961
        hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1962 1963 1964
        if(FAILED(hres))
            return hres;
    }
1965

1966
    return S_OK;
1967 1968 1969 1970 1971 1972
}

static HRESULT WINAPI WindowDispEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
1973
    HTMLWindow *This = impl_from_IDispatchEx(iface);
1974 1975 1976 1977 1978 1979

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
          lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    /* FIXME: Use script dispatch */

1980 1981
    return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
            pDispParams, pVarResult, pExcepInfo, puArgErr);
1982 1983
}

1984
static global_prop_t *alloc_global_prop(HTMLWindow *This, global_prop_type_t type, BSTR name)
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
{
    if(This->global_prop_cnt == This->global_prop_size) {
        global_prop_t *new_props;
        DWORD new_size;

        if(This->global_props) {
            new_size = This->global_prop_size*2;
            new_props = heap_realloc(This->global_props, new_size*sizeof(global_prop_t));
        }else {
            new_size = 16;
            new_props = heap_alloc(new_size*sizeof(global_prop_t));
        }
        if(!new_props)
            return NULL;
        This->global_props = new_props;
        This->global_prop_size = new_size;
    }

    This->global_props[This->global_prop_cnt].name = heap_strdupW(name);
    if(!This->global_props[This->global_prop_cnt].name)
        return NULL;

2007
    This->global_props[This->global_prop_cnt].type = type;
2008 2009 2010 2011 2012 2013 2014 2015
    return This->global_props + This->global_prop_cnt++;
}

static inline DWORD prop_to_dispid(HTMLWindow *This, global_prop_t *prop)
{
    return MSHTML_DISPID_CUSTOM_MIN + (prop-This->global_props);
}

2016
HRESULT search_window_props(HTMLWindow *This, BSTR bstrName, DWORD grfdex, DISPID *pid)
2017
{
2018
    DWORD i;
2019 2020
    ScriptHost *script_host;
    DISPID id;
2021

2022 2023 2024 2025 2026 2027 2028 2029 2030
    for(i=0; i < This->global_prop_cnt; i++) {
        /* FIXME: case sensitivity */
        if(!strcmpW(This->global_props[i].name, bstrName)) {
            *pid = MSHTML_DISPID_CUSTOM_MIN+i;
            return S_OK;
        }
    }

    if(find_global_prop(This, bstrName, grfdex, &script_host, &id)) {
2031
        global_prop_t *prop;
2032

2033
        prop = alloc_global_prop(This, GLOBAL_SCRIPTVAR, bstrName);
2034
        if(!prop)
2035 2036
            return E_OUTOFMEMORY;

2037 2038
        prop->script_host = script_host;
        prop->id = id;
2039

2040
        *pid = prop_to_dispid(This, prop);
2041 2042 2043
        return S_OK;
    }

2044 2045 2046 2047 2048
    return DISP_E_UNKNOWNNAME;
}

static HRESULT WINAPI WindowDispEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
{
2049
    HTMLWindow *This = impl_from_IDispatchEx(iface);
2050 2051 2052 2053 2054 2055 2056 2057
    HRESULT hres;

    TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);

    hres = search_window_props(This, bstrName, grfdex, pid);
    if(hres != DISP_E_UNKNOWNNAME)
        return hres;

2058
    hres = IDispatchEx_GetDispID(&This->dispex.IDispatchEx_iface, bstrName, grfdex, pid);
2059 2060 2061 2062 2063 2064 2065
    if(hres != DISP_E_UNKNOWNNAME)
        return hres;

    if(This->doc) {
        global_prop_t *prop;
        IHTMLElement *elem;

2066 2067
        hres = IHTMLDocument3_getElementById(&This->doc->basedoc.IHTMLDocument3_iface,
                                             bstrName, &elem);
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
        if(SUCCEEDED(hres) && elem) {
            IHTMLElement_Release(elem);

            prop = alloc_global_prop(This, GLOBAL_ELEMENTVAR, bstrName);
            if(!prop)
                return E_OUTOFMEMORY;

            *pid = prop_to_dispid(This, prop);
            return S_OK;
        }
    }

    return DISP_E_UNKNOWNNAME;
2081 2082 2083 2084 2085
}

static HRESULT WINAPI WindowDispEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
        VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
{
2086
    HTMLWindow *This = impl_from_IDispatchEx(iface);
2087 2088 2089

    TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);

2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
    if(id == DISPID_IHTMLWINDOW2_LOCATION && (wFlags & DISPATCH_PROPERTYPUT)) {
        HTMLLocation *location;
        HRESULT hres;

        TRACE("forwarding to location.href\n");

        hres = get_location(This, &location);
        if(FAILED(hres))
            return hres;

2100 2101
        hres = IDispatchEx_InvokeEx(&location->dispex.IDispatchEx_iface, DISPID_VALUE, lcid,
                wFlags, pdp, pvarRes, pei, pspCaller);
2102
        IHTMLLocation_Release(&location->IHTMLLocation_iface);
2103 2104 2105
        return hres;
    }

2106 2107
    return IDispatchEx_InvokeEx(&This->dispex.IDispatchEx_iface, id, lcid, wFlags, pdp, pvarRes,
            pei, pspCaller);
2108 2109 2110 2111
}

static HRESULT WINAPI WindowDispEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
{
2112
    HTMLWindow *This = impl_from_IDispatchEx(iface);
2113 2114 2115

    TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);

2116
    return IDispatchEx_DeleteMemberByName(&This->dispex.IDispatchEx_iface, bstrName, grfdex);
2117 2118 2119 2120
}

static HRESULT WINAPI WindowDispEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
{
2121
    HTMLWindow *This = impl_from_IDispatchEx(iface);
2122 2123 2124

    TRACE("(%p)->(%x)\n", This, id);

2125
    return IDispatchEx_DeleteMemberByDispID(&This->dispex.IDispatchEx_iface, id);
2126 2127 2128 2129
}

static HRESULT WINAPI WindowDispEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
{
2130
    HTMLWindow *This = impl_from_IDispatchEx(iface);
2131 2132 2133

    TRACE("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);

2134 2135
    return IDispatchEx_GetMemberProperties(&This->dispex.IDispatchEx_iface, id, grfdexFetch,
            pgrfdex);
2136 2137 2138 2139
}

static HRESULT WINAPI WindowDispEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
{
2140
    HTMLWindow *This = impl_from_IDispatchEx(iface);
2141 2142 2143

    TRACE("(%p)->(%x %p)\n", This, id, pbstrName);

2144
    return IDispatchEx_GetMemberName(&This->dispex.IDispatchEx_iface, id, pbstrName);
2145 2146 2147 2148
}

static HRESULT WINAPI WindowDispEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
{
2149
    HTMLWindow *This = impl_from_IDispatchEx(iface);
2150 2151 2152

    TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);

2153
    return IDispatchEx_GetNextDispID(&This->dispex.IDispatchEx_iface, grfdex, id, pid);
2154 2155 2156 2157
}

static HRESULT WINAPI WindowDispEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
{
2158
    HTMLWindow *This = impl_from_IDispatchEx(iface);
2159 2160 2161

    TRACE("(%p)->(%p)\n", This, ppunk);

2162 2163
    *ppunk = NULL;
    return S_OK;
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
}

static const IDispatchExVtbl WindowDispExVtbl = {
    WindowDispEx_QueryInterface,
    WindowDispEx_AddRef,
    WindowDispEx_Release,
    WindowDispEx_GetTypeInfoCount,
    WindowDispEx_GetTypeInfo,
    WindowDispEx_GetIDsOfNames,
    WindowDispEx_Invoke,
    WindowDispEx_GetDispID,
    WindowDispEx_InvokeEx,
    WindowDispEx_DeleteMemberByName,
    WindowDispEx_DeleteMemberByDispID,
    WindowDispEx_GetMemberProperties,
    WindowDispEx_GetMemberName,
    WindowDispEx_GetNextDispID,
    WindowDispEx_GetNameSpaceParent
};

2184 2185 2186 2187
static inline HTMLWindow *impl_from_IServiceProvider(IServiceProvider *iface)
{
    return CONTAINING_RECORD(iface, HTMLWindow, IServiceProvider_iface);
}
2188 2189 2190

static HRESULT WINAPI HTMLWindowSP_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
{
2191
    HTMLWindow *This = impl_from_IServiceProvider(iface);
2192
    return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2193 2194 2195 2196
}

static ULONG WINAPI HTMLWindowSP_AddRef(IServiceProvider *iface)
{
2197
    HTMLWindow *This = impl_from_IServiceProvider(iface);
2198
    return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
2199 2200 2201 2202
}

static ULONG WINAPI HTMLWindowSP_Release(IServiceProvider *iface)
{
2203
    HTMLWindow *This = impl_from_IServiceProvider(iface);
2204
    return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
2205 2206 2207 2208
}

static HRESULT WINAPI HTMLWindowSP_QueryService(IServiceProvider *iface, REFGUID guidService, REFIID riid, void **ppv)
{
2209
    HTMLWindow *This = impl_from_IServiceProvider(iface);
2210 2211 2212

    if(IsEqualGUID(guidService, &IID_IHTMLWindow2)) {
        TRACE("IID_IHTMLWindow2\n");
2213
        return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2214 2215 2216 2217 2218 2219 2220
    }

    TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);

    if(!This->doc_obj)
        return E_NOINTERFACE;

2221 2222
    return IServiceProvider_QueryService(&This->doc_obj->basedoc.IServiceProvider_iface,
            guidService, riid, ppv);
2223 2224 2225 2226 2227 2228 2229 2230 2231
}

static const IServiceProviderVtbl ServiceProviderVtbl = {
    HTMLWindowSP_QueryInterface,
    HTMLWindowSP_AddRef,
    HTMLWindowSP_Release,
    HTMLWindowSP_QueryService
};

2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
static inline HTMLWindow *impl_from_DispatchEx(DispatchEx *iface)
{
    return CONTAINING_RECORD(iface, HTMLWindow, dispex);
}

static HRESULT HTMLWindow_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
        VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
{
    HTMLWindow *This = impl_from_DispatchEx(dispex);
    global_prop_t *prop;
    DWORD idx;
    HRESULT hres;

    idx = id - MSHTML_DISPID_CUSTOM_MIN;
    if(idx >= This->global_prop_cnt)
        return DISP_E_MEMBERNOTFOUND;

    prop = This->global_props+idx;

    switch(prop->type) {
    case GLOBAL_SCRIPTVAR: {
2253
        IDispatchEx *iface;
2254 2255 2256 2257 2258 2259
        IDispatch *disp;

        disp = get_script_disp(prop->script_host);
        if(!disp)
            return E_UNEXPECTED;

2260
        hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&iface);
2261 2262
        if(SUCCEEDED(hres)) {
            TRACE("%s >>>\n", debugstr_w(prop->name));
2263
            hres = IDispatchEx_InvokeEx(iface, prop->id, lcid, flags, params, res, ei, caller);
2264 2265 2266 2267
            if(hres == S_OK)
                TRACE("%s <<<\n", debugstr_w(prop->name));
            else
                WARN("%s <<< %08x\n", debugstr_w(prop->name), hres);
2268
            IDispatchEx_Release(iface);
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
        }else {
            FIXME("No IDispatchEx\n");
        }
        IDispatch_Release(disp);
        break;
    }
    case GLOBAL_ELEMENTVAR: {
        IHTMLElement *elem;

        hres = IHTMLDocument3_getElementById(&This->doc->basedoc.IHTMLDocument3_iface,
                                             prop->name, &elem);
        if(FAILED(hres))
            return hres;

        if(!elem)
            return DISP_E_MEMBERNOTFOUND;

        V_VT(res) = VT_DISPATCH;
        V_DISPATCH(res) = (IDispatch*)elem;
        break;
    }
    default:
        ERR("invalid type %d\n", prop->type);
        hres = DISP_E_MEMBERNOTFOUND;
    }

    return hres;
}

2298 2299 2300

static const dispex_static_data_vtbl_t HTMLWindow_dispex_vtbl = {
    NULL,
2301
    NULL,
2302 2303
    HTMLWindow_invoke,
    NULL
2304 2305
};

2306 2307 2308 2309 2310 2311 2312
static const tid_t HTMLWindow_iface_tids[] = {
    IHTMLWindow2_tid,
    IHTMLWindow3_tid,
    IHTMLWindow4_tid,
    0
};

2313 2314
static dispex_static_data_t HTMLWindow_dispex = {
    &HTMLWindow_dispex_vtbl,
2315 2316
    DispHTMLWindow2_tid,
    NULL,
2317
    HTMLWindow_iface_tids
2318 2319
};

2320
HRESULT HTMLWindow_Create(HTMLDocumentObj *doc_obj, nsIDOMWindow *nswindow, HTMLWindow *parent, HTMLWindow **ret)
2321
{
2322
    HTMLWindow *window;
2323
    HRESULT hres;
2324

2325 2326 2327
    window = heap_alloc_zero(sizeof(HTMLWindow));
    if(!window)
        return E_OUTOFMEMORY;
2328

2329 2330
    window->window_ref = heap_alloc(sizeof(windowref_t));
    if(!window->window_ref) {
2331
        heap_free(window);
2332 2333 2334
        return E_OUTOFMEMORY;
    }

2335
    window->IHTMLWindow2_iface.lpVtbl = &HTMLWindow2Vtbl;
2336 2337 2338
    window->IHTMLWindow3_iface.lpVtbl = &HTMLWindow3Vtbl;
    window->IHTMLWindow4_iface.lpVtbl = &HTMLWindow4Vtbl;
    window->IHTMLPrivateWindow_iface.lpVtbl = &HTMLPrivateWindowVtbl;
2339
    window->IDispatchEx_iface.lpVtbl = &WindowDispExVtbl;
2340
    window->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
2341
    window->ref = 1;
2342
    window->doc_obj = doc_obj;
2343

2344 2345 2346
    window->window_ref->window = window;
    window->window_ref->ref = 1;

2347
    init_dispex(&window->dispex, (IUnknown*)&window->IHTMLWindow2_iface, &HTMLWindow_dispex);
2348

2349 2350 2351
    if(nswindow) {
        nsIDOMWindow_AddRef(nswindow);
        window->nswindow = nswindow;
2352 2353
    }

2354
    window->scriptmode = parent ? parent->scriptmode : SCRIPTMODE_GECKO;
2355
    window->readystate = READYSTATE_UNINITIALIZED;
2356 2357
    list_init(&window->script_hosts);

2358 2359 2360 2361 2362 2363
    hres = CoInternetCreateSecurityManager(NULL, &window->secmgr, 0);
    if(FAILED(hres)) {
        IHTMLWindow2_Release(&window->IHTMLWindow2_iface);
        return hres;
    }

2364
    window->task_magic = get_task_target_magic();
2365 2366
    update_window_doc(window);

2367
    list_init(&window->children);
2368
    list_add_head(&window_list, &window->entry);
2369

2370
    if(parent) {
2371
        IHTMLWindow2_AddRef(&window->IHTMLWindow2_iface);
2372 2373 2374 2375 2376

        window->parent = parent;
        list_add_tail(&parent->children, &window->sibling_entry);
    }

2377
    *ret = window;
2378
    return S_OK;
2379 2380
}

2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415
void update_window_doc(HTMLWindow *window)
{
    nsIDOMHTMLDocument *nshtmldoc;
    nsIDOMDocument *nsdoc;
    nsresult nsres;

    nsres = nsIDOMWindow_GetDocument(window->nswindow, &nsdoc);
    if(NS_FAILED(nsres) || !nsdoc) {
        ERR("GetDocument failed: %08x\n", nsres);
        return;
    }

    nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
    nsIDOMDocument_Release(nsdoc);
    if(NS_FAILED(nsres)) {
        ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
        return;
    }

    if(!window->doc || window->doc->nsdoc != nshtmldoc) {
        HTMLDocumentNode *doc;
        HRESULT hres;

        hres = create_doc_from_nsdoc(nshtmldoc, window->doc_obj, window, &doc);
        if(SUCCEEDED(hres)) {
            window_set_docnode(window, doc);
            htmldoc_release(&doc->basedoc);
        }else {
            ERR("create_doc_from_nsdoc failed: %08x\n", hres);
        }
    }

    nsIDOMHTMLDocument_Release(nshtmldoc);
}

2416
HTMLWindow *nswindow_to_window(const nsIDOMWindow *nswindow)
2417
{
2418 2419 2420 2421 2422 2423 2424
    HTMLWindow *iter;

    LIST_FOR_EACH_ENTRY(iter, &window_list, HTMLWindow, entry) {
        if(iter->nswindow == nswindow)
            return iter;
    }

2425 2426
    return NULL;
}