mshtml: Initialize OLEINPLACEFRAMEINFO.cb for IOleInPlaceSite::GetWindowContext.
[wine] / dlls / mshtml / htmlwindow.c
1 /*
2  * Copyright 2006-2010 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "mshtmdid.h"
28 #include "shlguid.h"
29
30 #include "wine/debug.h"
31
32 #include "mshtml_private.h"
33 #include "htmlevent.h"
34 #include "resource.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 static struct list window_list = LIST_INIT(window_list);
39
40 static void window_set_docnode(HTMLWindow *window, HTMLDocumentNode *doc_node)
41 {
42     if(window->doc) {
43         if(window->doc_obj && window == window->doc_obj->basedoc.window)
44             window->doc->basedoc.cp_container.forward_container = NULL;
45         abort_document_bindings(window->doc);
46         window->doc->basedoc.window = NULL;
47         htmldoc_release(&window->doc->basedoc);
48     }
49     window->doc = doc_node;
50     if(doc_node)
51         htmldoc_addref(&doc_node->basedoc);
52
53     if(window->doc_obj && window->doc_obj->basedoc.window == window) {
54         if(window->doc_obj->basedoc.doc_node)
55             htmldoc_release(&window->doc_obj->basedoc.doc_node->basedoc);
56         window->doc_obj->basedoc.doc_node = doc_node;
57         if(doc_node)
58             htmldoc_addref(&doc_node->basedoc);
59     }
60
61     if(doc_node && window->doc_obj && window->doc_obj->usermode == EDITMODE) {
62         nsIDOMNSHTMLDocument *nshtmldoc;
63         nsAString mode_str;
64         nsresult nsres;
65
66         static const PRUnichar onW[] = {'o','n',0};
67
68         nsres = nsIDOMHTMLDocument_QueryInterface(doc_node->nsdoc, &IID_nsIDOMNSHTMLDocument, (void**)&nshtmldoc);
69         if(NS_SUCCEEDED(nsres)) {
70             nsAString_Init(&mode_str, onW);
71             nsres = nsIDOMNSHTMLDocument_SetDesignMode(nshtmldoc, &mode_str);
72             nsAString_Finish(&mode_str);
73             nsIDOMNSHTMLDocument_Release(nshtmldoc);
74             if(NS_FAILED(nsres))
75                 ERR("SetDesignMode failed: %08x\n", nsres);
76         }else {
77             ERR("Could not get nsIDOMNSHTMLDocument interface: %08x\n", nsres);
78         }
79     }
80 }
81
82 nsIDOMWindow *get_nsdoc_window(nsIDOMDocument *nsdoc)
83 {
84     nsIDOMDocumentView *nsdocview;
85     nsIDOMAbstractView *nsview;
86     nsIDOMWindow *nswindow;
87     nsresult nsres;
88
89     nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMDocumentView, (void**)&nsdocview);
90     nsIDOMDocument_Release(nsdoc);
91     if(NS_FAILED(nsres)) {
92         ERR("Could not get nsIDOMDocumentView iface: %08x\n", nsres);
93         return NULL;
94     }
95
96     nsres = nsIDOMDocumentView_GetDefaultView(nsdocview, &nsview);
97     nsIDOMDocumentView_Release(nsdocview);
98     if(NS_FAILED(nsres)) {
99         ERR("GetDefaultView failed: %08x\n", nsres);
100         return NULL;
101     }
102
103     nsres = nsIDOMAbstractView_QueryInterface(nsview, &IID_nsIDOMWindow, (void**)&nswindow);
104     nsIDOMAbstractView_Release(nsview);
105     if(NS_FAILED(nsres)) {
106         ERR("Coult not get nsIDOMWindow iface: %08x\n", nsres);
107         return NULL;
108     }
109
110     return nswindow;
111 }
112
113 static void release_children(HTMLWindow *This)
114 {
115     HTMLWindow *child;
116
117     while(!list_empty(&This->children)) {
118         child = LIST_ENTRY(list_tail(&This->children), HTMLWindow, sibling_entry);
119
120         list_remove(&child->sibling_entry);
121         child->parent = NULL;
122         IHTMLWindow2_Release(&child->IHTMLWindow2_iface);
123     }
124 }
125
126 static HRESULT get_location(HTMLWindow *This, HTMLLocation **ret)
127 {
128     if(This->location) {
129         IHTMLLocation_AddRef(&This->location->IHTMLLocation_iface);
130     }else {
131         HRESULT hres;
132
133         hres = HTMLLocation_Create(This, &This->location);
134         if(FAILED(hres))
135             return hres;
136     }
137
138     *ret = This->location;
139     return S_OK;
140 }
141
142 static inline HRESULT set_window_event(HTMLWindow *window, eventid_t eid, VARIANT *var)
143 {
144     if(!window->doc) {
145         FIXME("No document\n");
146         return E_FAIL;
147     }
148
149     return set_event_handler(&window->doc->body_event_target, NULL, window->doc, eid, var);
150 }
151
152 static inline HRESULT get_window_event(HTMLWindow *window, eventid_t eid, VARIANT *var)
153 {
154     if(!window->doc) {
155         FIXME("No document\n");
156         return E_FAIL;
157     }
158
159     return get_event_handler(&window->doc->body_event_target, eid, var);
160 }
161
162 static inline HTMLWindow *impl_from_IHTMLWindow2(IHTMLWindow2 *iface)
163 {
164     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow2_iface);
165 }
166
167 static HRESULT WINAPI HTMLWindow2_QueryInterface(IHTMLWindow2 *iface, REFIID riid, void **ppv)
168 {
169     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
170
171     *ppv = NULL;
172
173     if(IsEqualGUID(&IID_IUnknown, riid)) {
174         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
175         *ppv = &This->IHTMLWindow2_iface;
176     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
177         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
178         *ppv = &This->IHTMLWindow2_iface;
179     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
180         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
181         *ppv = &This->IDispatchEx_iface;
182     }else if(IsEqualGUID(&IID_IHTMLFramesCollection2, riid)) {
183         TRACE("(%p)->(IID_IHTMLFramesCollection2 %p)\n", This, ppv);
184         *ppv = &This->IHTMLWindow2_iface;
185     }else if(IsEqualGUID(&IID_IHTMLWindow2, riid)) {
186         TRACE("(%p)->(IID_IHTMLWindow2 %p)\n", This, ppv);
187         *ppv = &This->IHTMLWindow2_iface;
188     }else if(IsEqualGUID(&IID_IHTMLWindow3, riid)) {
189         TRACE("(%p)->(IID_IHTMLWindow3 %p)\n", This, ppv);
190         *ppv = &This->IHTMLWindow3_iface;
191     }else if(IsEqualGUID(&IID_IHTMLWindow4, riid)) {
192         TRACE("(%p)->(IID_IHTMLWindow4 %p)\n", This, ppv);
193         *ppv = &This->IHTMLWindow4_iface;
194     }else if(IsEqualGUID(&IID_IHTMLPrivateWindow, riid)) {
195         TRACE("(%p)->(IID_IHTMLPrivateWindow %p)\n", This, ppv);
196         *ppv = &This->IHTMLPrivateWindow_iface;
197     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
198         TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
199         *ppv = &This->IServiceProvider_iface;
200     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
201         return *ppv ? S_OK : E_NOINTERFACE;
202     }
203
204     if(*ppv) {
205         IUnknown_AddRef((IUnknown*)*ppv);
206         return S_OK;
207     }
208
209     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
210     return E_NOINTERFACE;
211 }
212
213 static ULONG WINAPI HTMLWindow2_AddRef(IHTMLWindow2 *iface)
214 {
215     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
216     LONG ref = InterlockedIncrement(&This->ref);
217
218     TRACE("(%p) ref=%d\n", This, ref);
219
220     return ref;
221 }
222
223 static ULONG WINAPI HTMLWindow2_Release(IHTMLWindow2 *iface)
224 {
225     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
226     LONG ref = InterlockedDecrement(&This->ref);
227
228     TRACE("(%p) ref=%d\n", This, ref);
229
230     if(!ref) {
231         DWORD i;
232
233         remove_target_tasks(This->task_magic);
234         set_window_bscallback(This, NULL);
235         set_current_mon(This, NULL);
236         window_set_docnode(This, NULL);
237         release_children(This);
238
239         if(This->frame_element)
240             This->frame_element->content_window = NULL;
241
242         if(This->option_factory) {
243             This->option_factory->window = NULL;
244             IHTMLOptionElementFactory_Release(&This->option_factory->IHTMLOptionElementFactory_iface);
245         }
246
247         if(This->image_factory) {
248             This->image_factory->window = NULL;
249             IHTMLImageElementFactory_Release(&This->image_factory->IHTMLImageElementFactory_iface);
250         }
251
252         if(This->location) {
253             This->location->window = NULL;
254             IHTMLLocation_Release(&This->location->IHTMLLocation_iface);
255         }
256
257         if(This->screen)
258             IHTMLScreen_Release(This->screen);
259
260         for(i=0; i < This->global_prop_cnt; i++)
261             heap_free(This->global_props[i].name);
262
263         This->window_ref->window = NULL;
264         windowref_release(This->window_ref);
265
266         heap_free(This->global_props);
267         release_script_hosts(This);
268
269         if(This->nswindow)
270             nsIDOMWindow_Release(This->nswindow);
271
272         list_remove(&This->entry);
273         release_dispex(&This->dispex);
274         heap_free(This);
275     }
276
277     return ref;
278 }
279
280 static HRESULT WINAPI HTMLWindow2_GetTypeInfoCount(IHTMLWindow2 *iface, UINT *pctinfo)
281 {
282     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
283
284     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
285 }
286
287 static HRESULT WINAPI HTMLWindow2_GetTypeInfo(IHTMLWindow2 *iface, UINT iTInfo,
288                                               LCID lcid, ITypeInfo **ppTInfo)
289 {
290     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
291
292     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
293 }
294
295 static HRESULT WINAPI HTMLWindow2_GetIDsOfNames(IHTMLWindow2 *iface, REFIID riid,
296                                                 LPOLESTR *rgszNames, UINT cNames,
297                                                 LCID lcid, DISPID *rgDispId)
298 {
299     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
300
301     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
302             rgDispId);
303 }
304
305 static HRESULT WINAPI HTMLWindow2_Invoke(IHTMLWindow2 *iface, DISPID dispIdMember,
306                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
307                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
308 {
309     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
310
311     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
312             pDispParams, pVarResult, pExcepInfo, puArgErr);
313 }
314
315 static HRESULT get_frame_by_index(nsIDOMWindowCollection *nsFrames, PRUint32 index, HTMLWindow **ret)
316 {
317     PRUint32 length;
318     nsIDOMWindow *nsWindow;
319     nsresult nsres;
320
321     nsres = nsIDOMWindowCollection_GetLength(nsFrames, &length);
322     if(NS_FAILED(nsres)) {
323         FIXME("nsIDOMWindowCollection_GetLength failed: 0x%08x\n", nsres);
324         return E_FAIL;
325     }
326
327     if(index >= length)
328         return DISP_E_MEMBERNOTFOUND;
329
330     nsres = nsIDOMWindowCollection_Item(nsFrames, index, &nsWindow);
331     if(NS_FAILED(nsres)) {
332         FIXME("nsIDOMWindowCollection_Item failed: 0x%08x\n", nsres);
333         return E_FAIL;
334     }
335
336     *ret = nswindow_to_window(nsWindow);
337
338     nsIDOMWindow_Release(nsWindow);
339
340     return S_OK;
341 }
342
343 static HRESULT WINAPI HTMLWindow2_item(IHTMLWindow2 *iface, VARIANT *pvarIndex, VARIANT *pvarResult)
344 {
345     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
346     nsIDOMWindowCollection *nsFrames;
347     HTMLWindow *window;
348     HRESULT hres;
349     nsresult nsres;
350
351     TRACE("(%p)->(%p %p)\n", This, pvarIndex, pvarResult);
352
353     nsres = nsIDOMWindow_GetFrames(This->nswindow, &nsFrames);
354     if(NS_FAILED(nsres)) {
355         FIXME("nsIDOMWindow_GetFrames failed: 0x%08x\n", nsres);
356         return E_FAIL;
357     }
358
359     if(V_VT(pvarIndex) == VT_I4) {
360         int index = V_I4(pvarIndex);
361         TRACE("Getting index %d\n", index);
362         if(index < 0) {
363             hres = DISP_E_MEMBERNOTFOUND;
364             goto cleanup;
365         }
366         hres = get_frame_by_index(nsFrames, index, &window);
367         if(FAILED(hres))
368             goto cleanup;
369     }else if(V_VT(pvarIndex) == VT_UINT) {
370         unsigned int index = V_UINT(pvarIndex);
371         TRACE("Getting index %u\n", index);
372         hres = get_frame_by_index(nsFrames, index, &window);
373         if(FAILED(hres))
374             goto cleanup;
375     }else if(V_VT(pvarIndex) == VT_BSTR) {
376         BSTR str = V_BSTR(pvarIndex);
377         PRUint32 length, i;
378
379         TRACE("Getting name %s\n", wine_dbgstr_w(str));
380
381         nsres = nsIDOMWindowCollection_GetLength(nsFrames, &length);
382
383         window = NULL;
384         for(i = 0; i < length && !window; ++i) {
385             HTMLWindow *cur_window;
386             nsIDOMWindow *nsWindow;
387             BSTR id;
388
389             nsres = nsIDOMWindowCollection_Item(nsFrames, i, &nsWindow);
390             if(NS_FAILED(nsres)) {
391                 FIXME("nsIDOMWindowCollection_Item failed: 0x%08x\n", nsres);
392                 hres = E_FAIL;
393                 goto cleanup;
394             }
395
396             cur_window = nswindow_to_window(nsWindow);
397
398             nsIDOMWindow_Release(nsWindow);
399
400             hres = IHTMLElement_get_id(&cur_window->frame_element->element.IHTMLElement_iface, &id);
401             if(FAILED(hres)) {
402                 FIXME("IHTMLElement_get_id failed: 0x%08x\n", hres);
403                 goto cleanup;
404             }
405
406             if(!strcmpW(id, str))
407                 window = cur_window;
408
409             SysFreeString(id);
410         }
411
412         if(!window) {
413             hres = DISP_E_MEMBERNOTFOUND;
414             goto cleanup;
415         }
416     }else {
417         hres = E_INVALIDARG;
418         goto cleanup;
419     }
420
421     IHTMLWindow2_AddRef(&window->IHTMLWindow2_iface);
422     V_VT(pvarResult) = VT_DISPATCH;
423     V_DISPATCH(pvarResult) = (IDispatch*)window;
424
425     hres = S_OK;
426
427 cleanup:
428     nsIDOMWindowCollection_Release(nsFrames);
429
430     return hres;
431 }
432
433 static HRESULT WINAPI HTMLWindow2_get_length(IHTMLWindow2 *iface, LONG *p)
434 {
435     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
436     nsIDOMWindowCollection *nscollection;
437     PRUint32 length;
438     nsresult nsres;
439
440     TRACE("(%p)->(%p)\n", This, p);
441
442     nsres = nsIDOMWindow_GetFrames(This->nswindow, &nscollection);
443     if(NS_FAILED(nsres)) {
444         ERR("GetFrames failed: %08x\n", nsres);
445         return E_FAIL;
446     }
447
448     nsres = nsIDOMWindowCollection_GetLength(nscollection, &length);
449     nsIDOMWindowCollection_Release(nscollection);
450     if(NS_FAILED(nsres)) {
451         ERR("GetLength failed: %08x\n", nsres);
452         return E_FAIL;
453     }
454
455     *p = length;
456     return S_OK;
457 }
458
459 static HRESULT WINAPI HTMLWindow2_get_frames(IHTMLWindow2 *iface, IHTMLFramesCollection2 **p)
460 {
461     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
462     FIXME("(%p)->(%p): semi-stub\n", This, p);
463
464     /* FIXME: Should return a separate Window object */
465     *p = (IHTMLFramesCollection2*)&This->IHTMLWindow2_iface;
466     HTMLWindow2_AddRef(iface);
467     return S_OK;
468 }
469
470 static HRESULT WINAPI HTMLWindow2_put_defaultStatus(IHTMLWindow2 *iface, BSTR v)
471 {
472     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
473     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
474     return E_NOTIMPL;
475 }
476
477 static HRESULT WINAPI HTMLWindow2_get_defaultStatus(IHTMLWindow2 *iface, BSTR *p)
478 {
479     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
480     FIXME("(%p)->(%p)\n", This, p);
481     return E_NOTIMPL;
482 }
483
484 static HRESULT WINAPI HTMLWindow2_put_status(IHTMLWindow2 *iface, BSTR v)
485 {
486     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
487     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
488     return E_NOTIMPL;
489 }
490
491 static HRESULT WINAPI HTMLWindow2_get_status(IHTMLWindow2 *iface, BSTR *p)
492 {
493     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
494     FIXME("(%p)->(%p)\n", This, p);
495     return E_NOTIMPL;
496 }
497
498 static HRESULT WINAPI HTMLWindow2_setTimeout(IHTMLWindow2 *iface, BSTR expression,
499         LONG msec, VARIANT *language, LONG *timerID)
500 {
501     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
502     VARIANT expr_var;
503
504     TRACE("(%p)->(%s %d %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
505
506     V_VT(&expr_var) = VT_BSTR;
507     V_BSTR(&expr_var) = expression;
508
509     return IHTMLWindow3_setTimeout(&This->IHTMLWindow3_iface, &expr_var, msec, language, timerID);
510 }
511
512 static HRESULT WINAPI HTMLWindow2_clearTimeout(IHTMLWindow2 *iface, LONG timerID)
513 {
514     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
515
516     TRACE("(%p)->(%d)\n", This, timerID);
517
518     return clear_task_timer(&This->doc->basedoc, FALSE, timerID);
519 }
520
521 #define MAX_MESSAGE_LEN 2000
522
523 static HRESULT WINAPI HTMLWindow2_alert(IHTMLWindow2 *iface, BSTR message)
524 {
525     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
526     WCHAR title[100], *msg = message;
527     DWORD len;
528
529     TRACE("(%p)->(%s)\n", This, debugstr_w(message));
530
531     if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, title,
532                     sizeof(title)/sizeof(WCHAR))) {
533         WARN("Could not load message box title: %d\n", GetLastError());
534         return S_OK;
535     }
536
537     len = SysStringLen(message);
538     if(len > MAX_MESSAGE_LEN) {
539         msg = heap_alloc((MAX_MESSAGE_LEN+1)*sizeof(WCHAR));
540         if(!msg)
541             return E_OUTOFMEMORY;
542         memcpy(msg, message, MAX_MESSAGE_LEN*sizeof(WCHAR));
543         msg[MAX_MESSAGE_LEN] = 0;
544     }
545
546     MessageBoxW(This->doc_obj->hwnd, msg, title, MB_ICONWARNING);
547     if(msg != message)
548         heap_free(msg);
549     return S_OK;
550 }
551
552 static HRESULT WINAPI HTMLWindow2_confirm(IHTMLWindow2 *iface, BSTR message,
553         VARIANT_BOOL *confirmed)
554 {
555     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
556     WCHAR wszTitle[100];
557
558     TRACE("(%p)->(%s %p)\n", This, debugstr_w(message), confirmed);
559
560     if(!confirmed) return E_INVALIDARG;
561
562     if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, wszTitle,
563                 sizeof(wszTitle)/sizeof(WCHAR))) {
564         WARN("Could not load message box title: %d\n", GetLastError());
565         *confirmed = VARIANT_TRUE;
566         return S_OK;
567     }
568
569     if(MessageBoxW(This->doc_obj->hwnd, message, wszTitle,
570                 MB_OKCANCEL|MB_ICONQUESTION)==IDOK)
571         *confirmed = VARIANT_TRUE;
572     else *confirmed = VARIANT_FALSE;
573
574     return S_OK;
575 }
576
577 typedef struct
578 {
579     BSTR message;
580     BSTR dststr;
581     VARIANT *textdata;
582 }prompt_arg;
583
584 static INT_PTR CALLBACK prompt_dlgproc(HWND hwnd, UINT msg,
585         WPARAM wparam, LPARAM lparam)
586 {
587     switch(msg)
588     {
589         case WM_INITDIALOG:
590         {
591             prompt_arg *arg = (prompt_arg*)lparam;
592             WCHAR wszTitle[100];
593
594             if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, wszTitle,
595                         sizeof(wszTitle)/sizeof(WCHAR))) {
596                 WARN("Could not load message box title: %d\n", GetLastError());
597                 EndDialog(hwnd, wparam);
598                 return FALSE;
599             }
600
601             SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
602             SetWindowTextW(hwnd, wszTitle);
603             SetWindowTextW(GetDlgItem(hwnd, ID_PROMPT_PROMPT), arg->message);
604             SetWindowTextW(GetDlgItem(hwnd, ID_PROMPT_EDIT), arg->dststr);
605             return FALSE;
606         }
607         case WM_COMMAND:
608             switch(wparam)
609             {
610                 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
611                     EndDialog(hwnd, wparam);
612                     return TRUE;
613                 case MAKEWPARAM(IDOK, BN_CLICKED):
614                 {
615                     prompt_arg *arg =
616                         (prompt_arg*)GetWindowLongPtrW(hwnd, DWLP_USER);
617                     HWND hwndPrompt = GetDlgItem(hwnd, ID_PROMPT_EDIT);
618                     INT len = GetWindowTextLengthW(hwndPrompt);
619
620                     if(!arg->textdata)
621                     {
622                         EndDialog(hwnd, wparam);
623                         return TRUE;
624                     }
625
626                     V_VT(arg->textdata) = VT_BSTR;
627                     if(!len && !arg->dststr)
628                         V_BSTR(arg->textdata) = NULL;
629                     else
630                     {
631                         V_BSTR(arg->textdata) = SysAllocStringLen(NULL, len);
632                         GetWindowTextW(hwndPrompt, V_BSTR(arg->textdata), len+1);
633                     }
634                     EndDialog(hwnd, wparam);
635                     return TRUE;
636                 }
637             }
638             return FALSE;
639         case WM_CLOSE:
640             EndDialog(hwnd, IDCANCEL);
641             return TRUE;
642         default:
643             return FALSE;
644     }
645 }
646
647 static HRESULT WINAPI HTMLWindow2_prompt(IHTMLWindow2 *iface, BSTR message,
648         BSTR dststr, VARIANT *textdata)
649 {
650     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
651     prompt_arg arg;
652
653     TRACE("(%p)->(%s %s %p)\n", This, debugstr_w(message), debugstr_w(dststr), textdata);
654
655     if(textdata) V_VT(textdata) = VT_NULL;
656
657     arg.message = message;
658     arg.dststr = dststr;
659     arg.textdata = textdata;
660
661     DialogBoxParamW(hInst, MAKEINTRESOURCEW(ID_PROMPT_DIALOG),
662             This->doc_obj->hwnd, prompt_dlgproc, (LPARAM)&arg);
663     return S_OK;
664 }
665
666 static HRESULT WINAPI HTMLWindow2_get_Image(IHTMLWindow2 *iface, IHTMLImageElementFactory **p)
667 {
668     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
669
670     TRACE("(%p)->(%p)\n", This, p);
671
672     if(!This->image_factory)
673         This->image_factory = HTMLImageElementFactory_Create(This);
674
675     *p = &This->image_factory->IHTMLImageElementFactory_iface;
676     IHTMLImageElementFactory_AddRef(*p);
677
678     return S_OK;
679 }
680
681 static HRESULT WINAPI HTMLWindow2_get_location(IHTMLWindow2 *iface, IHTMLLocation **p)
682 {
683     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
684     HTMLLocation *location;
685     HRESULT hres;
686
687     TRACE("(%p)->(%p)\n", This, p);
688
689     hres = get_location(This, &location);
690     if(FAILED(hres))
691         return hres;
692
693     *p = &location->IHTMLLocation_iface;
694     return S_OK;
695 }
696
697 static HRESULT WINAPI HTMLWindow2_get_history(IHTMLWindow2 *iface, IOmHistory **p)
698 {
699     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
700     FIXME("(%p)->(%p)\n", This, p);
701     return E_NOTIMPL;
702 }
703
704 static HRESULT WINAPI HTMLWindow2_close(IHTMLWindow2 *iface)
705 {
706     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
707     FIXME("(%p)->()\n", This);
708     return E_NOTIMPL;
709 }
710
711 static HRESULT WINAPI HTMLWindow2_put_opener(IHTMLWindow2 *iface, VARIANT v)
712 {
713     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
714     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
715     return E_NOTIMPL;
716 }
717
718 static HRESULT WINAPI HTMLWindow2_get_opener(IHTMLWindow2 *iface, VARIANT *p)
719 {
720     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
721     FIXME("(%p)->(%p)\n", This, p);
722     return E_NOTIMPL;
723 }
724
725 static HRESULT WINAPI HTMLWindow2_get_navigator(IHTMLWindow2 *iface, IOmNavigator **p)
726 {
727     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
728
729     TRACE("(%p)->(%p)\n", This, p);
730
731     *p = OmNavigator_Create();
732     return S_OK;
733 }
734
735 static HRESULT WINAPI HTMLWindow2_put_name(IHTMLWindow2 *iface, BSTR v)
736 {
737     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
738     nsAString name_str;
739     nsresult nsres;
740
741     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
742
743     nsAString_InitDepend(&name_str, v);
744     nsres = nsIDOMWindow_SetName(This->nswindow, &name_str);
745     nsAString_Finish(&name_str);
746     if(NS_FAILED(nsres))
747         ERR("SetName failed: %08x\n", nsres);
748
749     return S_OK;
750 }
751
752 static HRESULT WINAPI HTMLWindow2_get_name(IHTMLWindow2 *iface, BSTR *p)
753 {
754     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
755     nsAString name_str;
756     nsresult nsres;
757     HRESULT hres;
758
759     TRACE("(%p)->(%p)\n", This, p);
760
761     nsAString_Init(&name_str, NULL);
762     nsres = nsIDOMWindow_GetName(This->nswindow, &name_str);
763     if(NS_SUCCEEDED(nsres)) {
764         const PRUnichar *name;
765
766         nsAString_GetData(&name_str, &name);
767         if(*name) {
768             *p = SysAllocString(name);
769             hres = *p ? S_OK : E_OUTOFMEMORY;
770         }else {
771             *p = NULL;
772             hres = S_OK;
773         }
774     }else {
775         ERR("GetName failed: %08x\n", nsres);
776         hres = E_FAIL;
777     }
778     nsAString_Finish(&name_str);
779
780     return hres;
781 }
782
783 static HRESULT WINAPI HTMLWindow2_get_parent(IHTMLWindow2 *iface, IHTMLWindow2 **p)
784 {
785     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
786     TRACE("(%p)->(%p)\n", This, p);
787
788     if(This->parent) {
789         *p = &This->parent->IHTMLWindow2_iface;
790         IHTMLWindow2_AddRef(*p);
791     }else
792         *p = NULL;
793
794     return S_OK;
795 }
796
797 static HRESULT WINAPI HTMLWindow2_open(IHTMLWindow2 *iface, BSTR url, BSTR name,
798          BSTR features, VARIANT_BOOL replace, IHTMLWindow2 **pomWindowResult)
799 {
800     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
801     FIXME("(%p)->(%s %s %s %x %p)\n", This, debugstr_w(url), debugstr_w(name),
802           debugstr_w(features), replace, pomWindowResult);
803     return E_NOTIMPL;
804 }
805
806 static HRESULT WINAPI HTMLWindow2_get_self(IHTMLWindow2 *iface, IHTMLWindow2 **p)
807 {
808     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
809
810     TRACE("(%p)->(%p)\n", This, p);
811
812     /* FIXME: We should return kind of proxy window here. */
813     IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
814     *p = &This->IHTMLWindow2_iface;
815     return S_OK;
816 }
817
818 static HRESULT WINAPI HTMLWindow2_get_top(IHTMLWindow2 *iface, IHTMLWindow2 **p)
819 {
820     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
821     HTMLWindow *curr;
822     TRACE("(%p)->(%p)\n", This, p);
823
824     curr = This;
825     while(curr->parent)
826         curr = curr->parent;
827     *p = &curr->IHTMLWindow2_iface;
828     IHTMLWindow2_AddRef(*p);
829
830     return S_OK;
831 }
832
833 static HRESULT WINAPI HTMLWindow2_get_window(IHTMLWindow2 *iface, IHTMLWindow2 **p)
834 {
835     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
836
837     TRACE("(%p)->(%p)\n", This, p);
838
839     /* FIXME: We should return kind of proxy window here. */
840     IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
841     *p = &This->IHTMLWindow2_iface;
842     return S_OK;
843 }
844
845 static HRESULT WINAPI HTMLWindow2_navigate(IHTMLWindow2 *iface, BSTR url)
846 {
847     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
848     FIXME("(%p)->(%s)\n", This, debugstr_w(url));
849     return E_NOTIMPL;
850 }
851
852 static HRESULT WINAPI HTMLWindow2_put_onfocus(IHTMLWindow2 *iface, VARIANT v)
853 {
854     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
855     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
856     return E_NOTIMPL;
857 }
858
859 static HRESULT WINAPI HTMLWindow2_get_onfocus(IHTMLWindow2 *iface, VARIANT *p)
860 {
861     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
862     FIXME("(%p)->(%p)\n", This, p);
863     return E_NOTIMPL;
864 }
865
866 static HRESULT WINAPI HTMLWindow2_put_onblur(IHTMLWindow2 *iface, VARIANT v)
867 {
868     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
869     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
870     return E_NOTIMPL;
871 }
872
873 static HRESULT WINAPI HTMLWindow2_get_onblur(IHTMLWindow2 *iface, VARIANT *p)
874 {
875     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
876     FIXME("(%p)->(%p)\n", This, p);
877     return E_NOTIMPL;
878 }
879
880 static HRESULT WINAPI HTMLWindow2_put_onload(IHTMLWindow2 *iface, VARIANT v)
881 {
882     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
883
884     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
885
886     return set_window_event(This, EVENTID_LOAD, &v);
887 }
888
889 static HRESULT WINAPI HTMLWindow2_get_onload(IHTMLWindow2 *iface, VARIANT *p)
890 {
891     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
892
893     TRACE("(%p)->(%p)\n", This, p);
894
895     return get_window_event(This, EVENTID_LOAD, p);
896 }
897
898 static HRESULT WINAPI HTMLWindow2_put_onbeforeunload(IHTMLWindow2 *iface, VARIANT v)
899 {
900     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
901
902     TRACE("(%p)->(v(%d))\n", This, V_VT(&v));
903
904     return set_window_event(This, EVENTID_BEFOREUNLOAD, &v);
905 }
906
907 static HRESULT WINAPI HTMLWindow2_get_onbeforeunload(IHTMLWindow2 *iface, VARIANT *p)
908 {
909     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
910
911     TRACE("(%p)->(%p)\n", This, p);
912
913     return get_window_event(This, EVENTID_BEFOREUNLOAD, p);
914 }
915
916 static HRESULT WINAPI HTMLWindow2_put_onunload(IHTMLWindow2 *iface, VARIANT v)
917 {
918     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
919     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
920     return E_NOTIMPL;
921 }
922
923 static HRESULT WINAPI HTMLWindow2_get_onunload(IHTMLWindow2 *iface, VARIANT *p)
924 {
925     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
926     FIXME("(%p)->(%p)\n", This, p);
927     return E_NOTIMPL;
928 }
929
930 static HRESULT WINAPI HTMLWindow2_put_onhelp(IHTMLWindow2 *iface, VARIANT v)
931 {
932     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
933     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
934     return E_NOTIMPL;
935 }
936
937 static HRESULT WINAPI HTMLWindow2_get_onhelp(IHTMLWindow2 *iface, VARIANT *p)
938 {
939     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
940     FIXME("(%p)->(%p)\n", This, p);
941     return E_NOTIMPL;
942 }
943
944 static HRESULT WINAPI HTMLWindow2_put_onerror(IHTMLWindow2 *iface, VARIANT v)
945 {
946     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
947     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
948     return E_NOTIMPL;
949 }
950
951 static HRESULT WINAPI HTMLWindow2_get_onerror(IHTMLWindow2 *iface, VARIANT *p)
952 {
953     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
954     FIXME("(%p)->(%p)\n", This, p);
955     return E_NOTIMPL;
956 }
957
958 static HRESULT WINAPI HTMLWindow2_put_onresize(IHTMLWindow2 *iface, VARIANT v)
959 {
960     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
961
962     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
963
964     return set_window_event(This, EVENTID_RESIZE, &v);
965 }
966
967 static HRESULT WINAPI HTMLWindow2_get_onresize(IHTMLWindow2 *iface, VARIANT *p)
968 {
969     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
970
971     TRACE("(%p)->(%p)\n", This, p);
972
973     return get_window_event(This, EVENTID_RESIZE, p);
974 }
975
976 static HRESULT WINAPI HTMLWindow2_put_onscroll(IHTMLWindow2 *iface, VARIANT v)
977 {
978     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
979     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
980     return E_NOTIMPL;
981 }
982
983 static HRESULT WINAPI HTMLWindow2_get_onscroll(IHTMLWindow2 *iface, VARIANT *p)
984 {
985     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
986     FIXME("(%p)->(%p)\n", This, p);
987     return E_NOTIMPL;
988 }
989
990 static HRESULT WINAPI HTMLWindow2_get_document(IHTMLWindow2 *iface, IHTMLDocument2 **p)
991 {
992     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
993
994     TRACE("(%p)->(%p)\n", This, p);
995
996     if(This->doc) {
997         /* FIXME: We should return a wrapper object here */
998         *p = &This->doc->basedoc.IHTMLDocument2_iface;
999         IHTMLDocument2_AddRef(*p);
1000     }else {
1001         *p = NULL;
1002     }
1003
1004     return S_OK;
1005 }
1006
1007 static HRESULT WINAPI HTMLWindow2_get_event(IHTMLWindow2 *iface, IHTMLEventObj **p)
1008 {
1009     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1010
1011     TRACE("(%p)->(%p)\n", This, p);
1012
1013     if(This->event)
1014         IHTMLEventObj_AddRef(This->event);
1015     *p = This->event;
1016     return S_OK;
1017 }
1018
1019 static HRESULT WINAPI HTMLWindow2_get__newEnum(IHTMLWindow2 *iface, IUnknown **p)
1020 {
1021     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1022     FIXME("(%p)->(%p)\n", This, p);
1023     return E_NOTIMPL;
1024 }
1025
1026 static HRESULT WINAPI HTMLWindow2_showModalDialog(IHTMLWindow2 *iface, BSTR dialog,
1027         VARIANT *varArgIn, VARIANT *varOptions, VARIANT *varArgOut)
1028 {
1029     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1030     FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(dialog), varArgIn, varOptions, varArgOut);
1031     return E_NOTIMPL;
1032 }
1033
1034 static HRESULT WINAPI HTMLWindow2_showHelp(IHTMLWindow2 *iface, BSTR helpURL, VARIANT helpArg,
1035         BSTR features)
1036 {
1037     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1038     FIXME("(%p)->(%s v(%d) %s)\n", This, debugstr_w(helpURL), V_VT(&helpArg), debugstr_w(features));
1039     return E_NOTIMPL;
1040 }
1041
1042 static HRESULT WINAPI HTMLWindow2_get_screen(IHTMLWindow2 *iface, IHTMLScreen **p)
1043 {
1044     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1045
1046     TRACE("(%p)->(%p)\n", This, p);
1047
1048     if(!This->screen) {
1049         HRESULT hres;
1050
1051         hres = HTMLScreen_Create(&This->screen);
1052         if(FAILED(hres))
1053             return hres;
1054     }
1055
1056     *p = This->screen;
1057     IHTMLScreen_AddRef(This->screen);
1058     return S_OK;
1059 }
1060
1061 static HRESULT WINAPI HTMLWindow2_get_Option(IHTMLWindow2 *iface, IHTMLOptionElementFactory **p)
1062 {
1063     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1064
1065     TRACE("(%p)->(%p)\n", This, p);
1066
1067     if(!This->option_factory)
1068         This->option_factory = HTMLOptionElementFactory_Create(This);
1069
1070     *p = &This->option_factory->IHTMLOptionElementFactory_iface;
1071     IHTMLOptionElementFactory_AddRef(*p);
1072
1073     return S_OK;
1074 }
1075
1076 static HRESULT WINAPI HTMLWindow2_focus(IHTMLWindow2 *iface)
1077 {
1078     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1079
1080     TRACE("(%p)->()\n", This);
1081
1082     if(This->doc_obj)
1083         SetFocus(This->doc_obj->hwnd);
1084     return S_OK;
1085 }
1086
1087 static HRESULT WINAPI HTMLWindow2_get_closed(IHTMLWindow2 *iface, VARIANT_BOOL *p)
1088 {
1089     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1090     FIXME("(%p)->(%p)\n", This, p);
1091     return E_NOTIMPL;
1092 }
1093
1094 static HRESULT WINAPI HTMLWindow2_blur(IHTMLWindow2 *iface)
1095 {
1096     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1097     FIXME("(%p)->()\n", This);
1098     return E_NOTIMPL;
1099 }
1100
1101 static HRESULT WINAPI HTMLWindow2_scroll(IHTMLWindow2 *iface, LONG x, LONG y)
1102 {
1103     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1104     FIXME("(%p)->(%d %d)\n", This, x, y);
1105     return E_NOTIMPL;
1106 }
1107
1108 static HRESULT WINAPI HTMLWindow2_get_clientInformation(IHTMLWindow2 *iface, IOmNavigator **p)
1109 {
1110     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1111     FIXME("(%p)->(%p)\n", This, p);
1112     return E_NOTIMPL;
1113 }
1114
1115 static HRESULT WINAPI HTMLWindow2_setInterval(IHTMLWindow2 *iface, BSTR expression,
1116         LONG msec, VARIANT *language, LONG *timerID)
1117 {
1118     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1119     VARIANT expr;
1120
1121     TRACE("(%p)->(%s %d %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
1122
1123     V_VT(&expr) = VT_BSTR;
1124     V_BSTR(&expr) = expression;
1125     return IHTMLWindow3_setInterval(&This->IHTMLWindow3_iface, &expr, msec, language, timerID);
1126 }
1127
1128 static HRESULT WINAPI HTMLWindow2_clearInterval(IHTMLWindow2 *iface, LONG timerID)
1129 {
1130     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1131
1132     TRACE("(%p)->(%d)\n", This, timerID);
1133
1134     return clear_task_timer(&This->doc->basedoc, TRUE, timerID);
1135 }
1136
1137 static HRESULT WINAPI HTMLWindow2_put_offscreenBuffering(IHTMLWindow2 *iface, VARIANT v)
1138 {
1139     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1140     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
1141     return E_NOTIMPL;
1142 }
1143
1144 static HRESULT WINAPI HTMLWindow2_get_offscreenBuffering(IHTMLWindow2 *iface, VARIANT *p)
1145 {
1146     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1147     FIXME("(%p)->(%p)\n", This, p);
1148     return E_NOTIMPL;
1149 }
1150
1151 static HRESULT WINAPI HTMLWindow2_execScript(IHTMLWindow2 *iface, BSTR scode, BSTR language,
1152         VARIANT *pvarRet)
1153 {
1154     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1155
1156     TRACE("(%p)->(%s %s %p)\n", This, debugstr_w(scode), debugstr_w(language), pvarRet);
1157
1158     return exec_script(This, scode, language, pvarRet);
1159 }
1160
1161 static HRESULT WINAPI HTMLWindow2_toString(IHTMLWindow2 *iface, BSTR *String)
1162 {
1163     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1164
1165     static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
1166
1167     TRACE("(%p)->(%p)\n", This, String);
1168
1169     if(!String)
1170         return E_INVALIDARG;
1171
1172     *String = SysAllocString(objectW);
1173     return *String ? S_OK : E_OUTOFMEMORY;
1174 }
1175
1176 static HRESULT WINAPI HTMLWindow2_scrollBy(IHTMLWindow2 *iface, LONG x, LONG y)
1177 {
1178     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1179     nsresult nsres;
1180
1181     TRACE("(%p)->(%d %d)\n", This, x, y);
1182
1183     nsres = nsIDOMWindow_ScrollBy(This->nswindow, x, y);
1184     if(NS_FAILED(nsres))
1185         ERR("ScrollBy failed: %08x\n", nsres);
1186
1187     return S_OK;
1188 }
1189
1190 static HRESULT WINAPI HTMLWindow2_scrollTo(IHTMLWindow2 *iface, LONG x, LONG y)
1191 {
1192     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1193     nsresult nsres;
1194
1195     TRACE("(%p)->(%d %d)\n", This, x, y);
1196
1197     nsres = nsIDOMWindow_ScrollTo(This->nswindow, x, y);
1198     if(NS_FAILED(nsres))
1199         ERR("ScrollTo failed: %08x\n", nsres);
1200
1201     return S_OK;
1202 }
1203
1204 static HRESULT WINAPI HTMLWindow2_moveTo(IHTMLWindow2 *iface, LONG x, LONG y)
1205 {
1206     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1207     FIXME("(%p)->(%d %d)\n", This, x, y);
1208     return E_NOTIMPL;
1209 }
1210
1211 static HRESULT WINAPI HTMLWindow2_moveBy(IHTMLWindow2 *iface, LONG x, LONG y)
1212 {
1213     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1214     FIXME("(%p)->(%d %d)\n", This, x, y);
1215     return E_NOTIMPL;
1216 }
1217
1218 static HRESULT WINAPI HTMLWindow2_resizeTo(IHTMLWindow2 *iface, LONG x, LONG y)
1219 {
1220     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1221     FIXME("(%p)->(%d %d)\n", This, x, y);
1222     return E_NOTIMPL;
1223 }
1224
1225 static HRESULT WINAPI HTMLWindow2_resizeBy(IHTMLWindow2 *iface, LONG x, LONG y)
1226 {
1227     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1228     FIXME("(%p)->(%d %d)\n", This, x, y);
1229     return E_NOTIMPL;
1230 }
1231
1232 static HRESULT WINAPI HTMLWindow2_get_external(IHTMLWindow2 *iface, IDispatch **p)
1233 {
1234     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1235
1236     TRACE("(%p)->(%p)\n", This, p);
1237
1238     *p = NULL;
1239
1240     if(!This->doc_obj->hostui)
1241         return S_OK;
1242
1243     return IDocHostUIHandler_GetExternal(This->doc_obj->hostui, p);
1244 }
1245
1246 static const IHTMLWindow2Vtbl HTMLWindow2Vtbl = {
1247     HTMLWindow2_QueryInterface,
1248     HTMLWindow2_AddRef,
1249     HTMLWindow2_Release,
1250     HTMLWindow2_GetTypeInfoCount,
1251     HTMLWindow2_GetTypeInfo,
1252     HTMLWindow2_GetIDsOfNames,
1253     HTMLWindow2_Invoke,
1254     HTMLWindow2_item,
1255     HTMLWindow2_get_length,
1256     HTMLWindow2_get_frames,
1257     HTMLWindow2_put_defaultStatus,
1258     HTMLWindow2_get_defaultStatus,
1259     HTMLWindow2_put_status,
1260     HTMLWindow2_get_status,
1261     HTMLWindow2_setTimeout,
1262     HTMLWindow2_clearTimeout,
1263     HTMLWindow2_alert,
1264     HTMLWindow2_confirm,
1265     HTMLWindow2_prompt,
1266     HTMLWindow2_get_Image,
1267     HTMLWindow2_get_location,
1268     HTMLWindow2_get_history,
1269     HTMLWindow2_close,
1270     HTMLWindow2_put_opener,
1271     HTMLWindow2_get_opener,
1272     HTMLWindow2_get_navigator,
1273     HTMLWindow2_put_name,
1274     HTMLWindow2_get_name,
1275     HTMLWindow2_get_parent,
1276     HTMLWindow2_open,
1277     HTMLWindow2_get_self,
1278     HTMLWindow2_get_top,
1279     HTMLWindow2_get_window,
1280     HTMLWindow2_navigate,
1281     HTMLWindow2_put_onfocus,
1282     HTMLWindow2_get_onfocus,
1283     HTMLWindow2_put_onblur,
1284     HTMLWindow2_get_onblur,
1285     HTMLWindow2_put_onload,
1286     HTMLWindow2_get_onload,
1287     HTMLWindow2_put_onbeforeunload,
1288     HTMLWindow2_get_onbeforeunload,
1289     HTMLWindow2_put_onunload,
1290     HTMLWindow2_get_onunload,
1291     HTMLWindow2_put_onhelp,
1292     HTMLWindow2_get_onhelp,
1293     HTMLWindow2_put_onerror,
1294     HTMLWindow2_get_onerror,
1295     HTMLWindow2_put_onresize,
1296     HTMLWindow2_get_onresize,
1297     HTMLWindow2_put_onscroll,
1298     HTMLWindow2_get_onscroll,
1299     HTMLWindow2_get_document,
1300     HTMLWindow2_get_event,
1301     HTMLWindow2_get__newEnum,
1302     HTMLWindow2_showModalDialog,
1303     HTMLWindow2_showHelp,
1304     HTMLWindow2_get_screen,
1305     HTMLWindow2_get_Option,
1306     HTMLWindow2_focus,
1307     HTMLWindow2_get_closed,
1308     HTMLWindow2_blur,
1309     HTMLWindow2_scroll,
1310     HTMLWindow2_get_clientInformation,
1311     HTMLWindow2_setInterval,
1312     HTMLWindow2_clearInterval,
1313     HTMLWindow2_put_offscreenBuffering,
1314     HTMLWindow2_get_offscreenBuffering,
1315     HTMLWindow2_execScript,
1316     HTMLWindow2_toString,
1317     HTMLWindow2_scrollBy,
1318     HTMLWindow2_scrollTo,
1319     HTMLWindow2_moveTo,
1320     HTMLWindow2_moveBy,
1321     HTMLWindow2_resizeTo,
1322     HTMLWindow2_resizeBy,
1323     HTMLWindow2_get_external
1324 };
1325
1326 static inline HTMLWindow *impl_from_IHTMLWindow3(IHTMLWindow3 *iface)
1327 {
1328     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow3_iface);
1329 }
1330
1331 static HRESULT WINAPI HTMLWindow3_QueryInterface(IHTMLWindow3 *iface, REFIID riid, void **ppv)
1332 {
1333     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1334
1335     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1336 }
1337
1338 static ULONG WINAPI HTMLWindow3_AddRef(IHTMLWindow3 *iface)
1339 {
1340     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1341
1342     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1343 }
1344
1345 static ULONG WINAPI HTMLWindow3_Release(IHTMLWindow3 *iface)
1346 {
1347     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1348
1349     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1350 }
1351
1352 static HRESULT WINAPI HTMLWindow3_GetTypeInfoCount(IHTMLWindow3 *iface, UINT *pctinfo)
1353 {
1354     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1355
1356     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
1357 }
1358
1359 static HRESULT WINAPI HTMLWindow3_GetTypeInfo(IHTMLWindow3 *iface, UINT iTInfo,
1360                                               LCID lcid, ITypeInfo **ppTInfo)
1361 {
1362     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1363
1364     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1365 }
1366
1367 static HRESULT WINAPI HTMLWindow3_GetIDsOfNames(IHTMLWindow3 *iface, REFIID riid,
1368                                                 LPOLESTR *rgszNames, UINT cNames,
1369                                                 LCID lcid, DISPID *rgDispId)
1370 {
1371     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1372
1373     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
1374             rgDispId);
1375 }
1376
1377 static HRESULT WINAPI HTMLWindow3_Invoke(IHTMLWindow3 *iface, DISPID dispIdMember,
1378                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1379                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1380 {
1381     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1382
1383     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
1384             pDispParams, pVarResult, pExcepInfo, puArgErr);
1385 }
1386
1387 static HRESULT WINAPI HTMLWindow3_get_screenLeft(IHTMLWindow3 *iface, LONG *p)
1388 {
1389     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1390     FIXME("(%p)->(%p)\n", This, p);
1391     return E_NOTIMPL;
1392 }
1393
1394 static HRESULT WINAPI HTMLWindow3_get_screenTop(IHTMLWindow3 *iface, LONG *p)
1395 {
1396     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1397     FIXME("(%p)->(%p)\n", This, p);
1398     return E_NOTIMPL;
1399 }
1400
1401 static HRESULT WINAPI HTMLWindow3_attachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp, VARIANT_BOOL *pfResult)
1402 {
1403     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1404
1405     TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(event), pDisp, pfResult);
1406
1407     if(!This->doc) {
1408         FIXME("No document\n");
1409         return E_FAIL;
1410     }
1411
1412     return attach_event(&This->doc->body_event_target, NULL, &This->doc->basedoc, event, pDisp, pfResult);
1413 }
1414
1415 static HRESULT WINAPI HTMLWindow3_detachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp)
1416 {
1417     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1418     FIXME("(%p)->()\n", This);
1419     return E_NOTIMPL;
1420 }
1421
1422 static HRESULT window_set_timer(HTMLWindow *This, VARIANT *expr, LONG msec, VARIANT *language,
1423         BOOL interval, LONG *timer_id)
1424 {
1425     IDispatch *disp = NULL;
1426
1427     switch(V_VT(expr)) {
1428     case VT_DISPATCH:
1429         disp = V_DISPATCH(expr);
1430         IDispatch_AddRef(disp);
1431         break;
1432
1433     case VT_BSTR:
1434         disp = script_parse_event(This, V_BSTR(expr));
1435         break;
1436
1437     default:
1438         FIXME("unimplemented vt=%d\n", V_VT(expr));
1439         return E_NOTIMPL;
1440     }
1441
1442     if(!disp)
1443         return E_FAIL;
1444
1445     *timer_id = set_task_timer(&This->doc->basedoc, msec, interval, disp);
1446     IDispatch_Release(disp);
1447
1448     return S_OK;
1449 }
1450
1451 static HRESULT WINAPI HTMLWindow3_setTimeout(IHTMLWindow3 *iface, VARIANT *expression, LONG msec,
1452         VARIANT *language, LONG *timerID)
1453 {
1454     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1455
1456     TRACE("(%p)->(%p(%d) %d %p %p)\n", This, expression, V_VT(expression), msec, language, timerID);
1457
1458     return window_set_timer(This, expression, msec, language, FALSE, timerID);
1459 }
1460
1461 static HRESULT WINAPI HTMLWindow3_setInterval(IHTMLWindow3 *iface, VARIANT *expression, LONG msec,
1462         VARIANT *language, LONG *timerID)
1463 {
1464     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1465
1466     TRACE("(%p)->(%p %d %p %p)\n", This, expression, msec, language, timerID);
1467
1468     return window_set_timer(This, expression, msec, language, TRUE, timerID);
1469 }
1470
1471 static HRESULT WINAPI HTMLWindow3_print(IHTMLWindow3 *iface)
1472 {
1473     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1474     FIXME("(%p)\n", This);
1475     return E_NOTIMPL;
1476 }
1477
1478 static HRESULT WINAPI HTMLWindow3_put_onbeforeprint(IHTMLWindow3 *iface, VARIANT v)
1479 {
1480     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1481     FIXME("(%p)->()\n", This);
1482     return E_NOTIMPL;
1483 }
1484
1485 static HRESULT WINAPI HTMLWindow3_get_onbeforeprint(IHTMLWindow3 *iface, VARIANT *p)
1486 {
1487     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1488     FIXME("(%p)->(%p)\n", This, p);
1489     return E_NOTIMPL;
1490 }
1491
1492 static HRESULT WINAPI HTMLWindow3_put_onafterprint(IHTMLWindow3 *iface, VARIANT v)
1493 {
1494     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1495     FIXME("(%p)->()\n", This);
1496     return E_NOTIMPL;
1497 }
1498
1499 static HRESULT WINAPI HTMLWindow3_get_onafterprint(IHTMLWindow3 *iface, VARIANT *p)
1500 {
1501     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1502     FIXME("(%p)->(%p)\n", This, p);
1503     return E_NOTIMPL;
1504 }
1505
1506 static HRESULT WINAPI HTMLWindow3_get_clipboardData(IHTMLWindow3 *iface, IHTMLDataTransfer **p)
1507 {
1508     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1509     FIXME("(%p)->(%p)\n", This, p);
1510     return E_NOTIMPL;
1511 }
1512
1513 static HRESULT WINAPI HTMLWindow3_showModelessDialog(IHTMLWindow3 *iface, BSTR url,
1514         VARIANT *varArgIn, VARIANT *options, IHTMLWindow2 **pDialog)
1515 {
1516     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1517     FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(url), varArgIn, options, pDialog);
1518     return E_NOTIMPL;
1519 }
1520
1521 static const IHTMLWindow3Vtbl HTMLWindow3Vtbl = {
1522     HTMLWindow3_QueryInterface,
1523     HTMLWindow3_AddRef,
1524     HTMLWindow3_Release,
1525     HTMLWindow3_GetTypeInfoCount,
1526     HTMLWindow3_GetTypeInfo,
1527     HTMLWindow3_GetIDsOfNames,
1528     HTMLWindow3_Invoke,
1529     HTMLWindow3_get_screenLeft,
1530     HTMLWindow3_get_screenTop,
1531     HTMLWindow3_attachEvent,
1532     HTMLWindow3_detachEvent,
1533     HTMLWindow3_setTimeout,
1534     HTMLWindow3_setInterval,
1535     HTMLWindow3_print,
1536     HTMLWindow3_put_onbeforeprint,
1537     HTMLWindow3_get_onbeforeprint,
1538     HTMLWindow3_put_onafterprint,
1539     HTMLWindow3_get_onafterprint,
1540     HTMLWindow3_get_clipboardData,
1541     HTMLWindow3_showModelessDialog
1542 };
1543
1544 static inline HTMLWindow *impl_from_IHTMLWindow4(IHTMLWindow4 *iface)
1545 {
1546     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow4_iface);
1547 }
1548
1549 static HRESULT WINAPI HTMLWindow4_QueryInterface(IHTMLWindow4 *iface, REFIID riid, void **ppv)
1550 {
1551     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1552
1553     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1554 }
1555
1556 static ULONG WINAPI HTMLWindow4_AddRef(IHTMLWindow4 *iface)
1557 {
1558     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1559
1560     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1561 }
1562
1563 static ULONG WINAPI HTMLWindow4_Release(IHTMLWindow4 *iface)
1564 {
1565     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1566
1567     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1568 }
1569
1570 static HRESULT WINAPI HTMLWindow4_GetTypeInfoCount(IHTMLWindow4 *iface, UINT *pctinfo)
1571 {
1572     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1573
1574     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
1575 }
1576
1577 static HRESULT WINAPI HTMLWindow4_GetTypeInfo(IHTMLWindow4 *iface, UINT iTInfo,
1578                                               LCID lcid, ITypeInfo **ppTInfo)
1579 {
1580     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1581
1582     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1583 }
1584
1585 static HRESULT WINAPI HTMLWindow4_GetIDsOfNames(IHTMLWindow4 *iface, REFIID riid,
1586                                                 LPOLESTR *rgszNames, UINT cNames,
1587                                                 LCID lcid, DISPID *rgDispId)
1588 {
1589     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1590
1591     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
1592             rgDispId);
1593 }
1594
1595 static HRESULT WINAPI HTMLWindow4_Invoke(IHTMLWindow4 *iface, DISPID dispIdMember,
1596                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1597                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1598 {
1599     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1600
1601     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
1602             pDispParams, pVarResult, pExcepInfo, puArgErr);
1603 }
1604
1605 static HRESULT WINAPI HTMLWindow4_createPopup(IHTMLWindow4 *iface, VARIANT *varArgIn,
1606                             IDispatch **ppPopup)
1607 {
1608     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1609     FIXME("(%p)->(%p %p)\n", This, varArgIn, ppPopup);
1610     return E_NOTIMPL;
1611 }
1612
1613 static HRESULT WINAPI HTMLWindow4_get_frameElement(IHTMLWindow4 *iface, IHTMLFrameBase **p)
1614 {
1615     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1616     TRACE("(%p)->(%p)\n", This, p);
1617
1618     if(This->frame_element) {
1619         *p = &This->frame_element->IHTMLFrameBase_iface;
1620         IHTMLFrameBase_AddRef(*p);
1621     }else
1622         *p = NULL;
1623
1624     return S_OK;
1625 }
1626
1627 static const IHTMLWindow4Vtbl HTMLWindow4Vtbl = {
1628     HTMLWindow4_QueryInterface,
1629     HTMLWindow4_AddRef,
1630     HTMLWindow4_Release,
1631     HTMLWindow4_GetTypeInfoCount,
1632     HTMLWindow4_GetTypeInfo,
1633     HTMLWindow4_GetIDsOfNames,
1634     HTMLWindow4_Invoke,
1635     HTMLWindow4_createPopup,
1636     HTMLWindow4_get_frameElement
1637 };
1638
1639 static inline HTMLWindow *impl_from_IHTMLPrivateWindow(IHTMLPrivateWindow *iface)
1640 {
1641     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLPrivateWindow_iface);
1642 }
1643
1644 static HRESULT WINAPI HTMLPrivateWindow_QueryInterface(IHTMLPrivateWindow *iface, REFIID riid, void **ppv)
1645 {
1646     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1647
1648     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1649 }
1650
1651 static ULONG WINAPI HTMLPrivateWindow_AddRef(IHTMLPrivateWindow *iface)
1652 {
1653     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1654
1655     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1656 }
1657
1658 static ULONG WINAPI HTMLPrivateWindow_Release(IHTMLPrivateWindow *iface)
1659 {
1660     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1661
1662     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1663 }
1664
1665 static HRESULT WINAPI HTMLPrivateWindow_SuperNavigate(IHTMLPrivateWindow *iface, BSTR url, BSTR arg2, BSTR arg3,
1666         BSTR arg4, VARIANT *post_data_var, VARIANT *headers_var, ULONG flags)
1667 {
1668     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1669     DWORD post_data_size = 0;
1670     BYTE *post_data = NULL;
1671     WCHAR *headers = NULL;
1672     nsChannelBSC *bsc;
1673     IMoniker *mon;
1674     BSTR new_url;
1675     HRESULT hres;
1676
1677     TRACE("(%p)->(%s %s %s %s %s %s %x)\n", This, debugstr_w(url), debugstr_w(arg2), debugstr_w(arg3), debugstr_w(arg4),
1678           debugstr_variant(post_data_var), debugstr_variant(headers_var), flags);
1679
1680     new_url = url;
1681     if(This->doc_obj->hostui) {
1682         OLECHAR *translated_url = NULL;
1683
1684         hres = IDocHostUIHandler_TranslateUrl(This->doc_obj->hostui, 0, url, &translated_url);
1685         if(hres == S_OK && translated_url) {
1686             new_url = SysAllocString(translated_url);
1687             CoTaskMemFree(translated_url);
1688         }
1689     }
1690
1691     if(This->doc_obj->client) {
1692         IOleCommandTarget *cmdtrg;
1693
1694         hres = IOleClientSite_QueryInterface(This->doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1695         if(SUCCEEDED(hres)) {
1696             VARIANT in, out;
1697
1698             V_VT(&in) = VT_BSTR;
1699             V_BSTR(&in) = new_url;
1700             V_VT(&out) = VT_BOOL;
1701             V_BOOL(&out) = VARIANT_TRUE;
1702             hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &in, &out);
1703             IOleCommandTarget_Release(cmdtrg);
1704             if(SUCCEEDED(hres))
1705                 VariantClear(&out);
1706         }
1707     }
1708
1709     /* FIXME: Why not set_ready_state? */
1710     This->readystate = READYSTATE_UNINITIALIZED;
1711
1712     hres = CreateURLMoniker(NULL, new_url, &mon);
1713     if(new_url != url)
1714         SysFreeString(new_url);
1715     if(FAILED(hres))
1716         return hres;
1717
1718     if(post_data_var) {
1719         if(V_VT(post_data_var) == (VT_ARRAY|VT_UI1)) {
1720             SafeArrayAccessData(V_ARRAY(post_data_var), (void**)&post_data);
1721             post_data_size = V_ARRAY(post_data_var)->rgsabound[0].cElements;
1722         }
1723     }
1724
1725     if(headers_var && V_VT(headers_var) != VT_EMPTY && V_VT(headers_var) != VT_ERROR) {
1726         if(V_VT(headers_var) != VT_BSTR)
1727             return E_INVALIDARG;
1728
1729         headers = V_BSTR(headers_var);
1730     }
1731
1732     hres = create_channelbsc(mon, headers, post_data, post_data_size, &bsc);
1733     if(post_data)
1734         SafeArrayUnaccessData(V_ARRAY(post_data_var));
1735     if(FAILED(hres)) {
1736         IMoniker_Release(mon);
1737         return hres;
1738     }
1739
1740     hres = set_moniker(&This->doc_obj->basedoc, mon, NULL, bsc, TRUE);
1741     if(SUCCEEDED(hres))
1742         hres = async_start_doc_binding(This, bsc);
1743
1744     IUnknown_Release((IUnknown*)bsc);
1745     IMoniker_Release(mon);
1746     return hres;
1747 }
1748
1749 static HRESULT WINAPI HTMLPrivateWindow_GetPendingUrl(IHTMLPrivateWindow *iface, BSTR *url)
1750 {
1751     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1752     FIXME("(%p)->(%p)\n", This, url);
1753     return E_NOTIMPL;
1754 }
1755
1756 static HRESULT WINAPI HTMLPrivateWindow_SetPICSTarget(IHTMLPrivateWindow *iface, IOleCommandTarget *cmdtrg)
1757 {
1758     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1759     FIXME("(%p)->(%p)\n", This, cmdtrg);
1760     return E_NOTIMPL;
1761 }
1762
1763 static HRESULT WINAPI HTMLPrivateWindow_PICSComplete(IHTMLPrivateWindow *iface, int arg)
1764 {
1765     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1766     FIXME("(%p)->(%x)\n", This, arg);
1767     return E_NOTIMPL;
1768 }
1769
1770 static HRESULT WINAPI HTMLPrivateWindow_FindWindowByName(IHTMLPrivateWindow *iface, LPCWSTR name, IHTMLWindow2 **ret)
1771 {
1772     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1773     FIXME("(%p)->(%s %p)\n", This, debugstr_w(name), ret);
1774     return E_NOTIMPL;
1775 }
1776
1777 static HRESULT WINAPI HTMLPrivateWindow_GetAddressBar(IHTMLPrivateWindow *iface, BSTR *url)
1778 {
1779     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1780     TRACE("(%p)->(%p)\n", This, url);
1781
1782     if(!url)
1783         return E_INVALIDARG;
1784
1785     *url = SysAllocString(This->url);
1786     return S_OK;
1787 }
1788
1789 static const IHTMLPrivateWindowVtbl HTMLPrivateWindowVtbl = {
1790     HTMLPrivateWindow_QueryInterface,
1791     HTMLPrivateWindow_AddRef,
1792     HTMLPrivateWindow_Release,
1793     HTMLPrivateWindow_SuperNavigate,
1794     HTMLPrivateWindow_GetPendingUrl,
1795     HTMLPrivateWindow_SetPICSTarget,
1796     HTMLPrivateWindow_PICSComplete,
1797     HTMLPrivateWindow_FindWindowByName,
1798     HTMLPrivateWindow_GetAddressBar
1799 };
1800
1801 static inline HTMLWindow *impl_from_IDispatchEx(IDispatchEx *iface)
1802 {
1803     return CONTAINING_RECORD(iface, HTMLWindow, IDispatchEx_iface);
1804 }
1805
1806 static HRESULT WINAPI WindowDispEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1807 {
1808     HTMLWindow *This = impl_from_IDispatchEx(iface);
1809
1810     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1811 }
1812
1813 static ULONG WINAPI WindowDispEx_AddRef(IDispatchEx *iface)
1814 {
1815     HTMLWindow *This = impl_from_IDispatchEx(iface);
1816
1817     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1818 }
1819
1820 static ULONG WINAPI WindowDispEx_Release(IDispatchEx *iface)
1821 {
1822     HTMLWindow *This = impl_from_IDispatchEx(iface);
1823
1824     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1825 }
1826
1827 static HRESULT WINAPI WindowDispEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1828 {
1829     HTMLWindow *This = impl_from_IDispatchEx(iface);
1830
1831     TRACE("(%p)->(%p)\n", This, pctinfo);
1832
1833     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
1834 }
1835
1836 static HRESULT WINAPI WindowDispEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1837                                                LCID lcid, ITypeInfo **ppTInfo)
1838 {
1839     HTMLWindow *This = impl_from_IDispatchEx(iface);
1840
1841     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1842
1843     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1844 }
1845
1846 static HRESULT WINAPI WindowDispEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1847                                                  LPOLESTR *rgszNames, UINT cNames,
1848                                                  LCID lcid, DISPID *rgDispId)
1849 {
1850     HTMLWindow *This = impl_from_IDispatchEx(iface);
1851     UINT i;
1852     HRESULT hres;
1853
1854     WARN("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1855           lcid, rgDispId);
1856
1857     for(i=0; i < cNames; i++) {
1858         /* We shouldn't use script's IDispatchEx here, so we shouldn't use GetDispID */
1859         hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1860         if(FAILED(hres))
1861             return hres;
1862     }
1863
1864     return S_OK;
1865 }
1866
1867 static HRESULT WINAPI WindowDispEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1868                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1869                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1870 {
1871     HTMLWindow *This = impl_from_IDispatchEx(iface);
1872
1873     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1874           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1875
1876     /* FIXME: Use script dispatch */
1877
1878     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
1879             pDispParams, pVarResult, pExcepInfo, puArgErr);
1880 }
1881
1882 static global_prop_t *alloc_global_prop(HTMLWindow *This, global_prop_type_t type, BSTR name)
1883 {
1884     if(This->global_prop_cnt == This->global_prop_size) {
1885         global_prop_t *new_props;
1886         DWORD new_size;
1887
1888         if(This->global_props) {
1889             new_size = This->global_prop_size*2;
1890             new_props = heap_realloc(This->global_props, new_size*sizeof(global_prop_t));
1891         }else {
1892             new_size = 16;
1893             new_props = heap_alloc(new_size*sizeof(global_prop_t));
1894         }
1895         if(!new_props)
1896             return NULL;
1897         This->global_props = new_props;
1898         This->global_prop_size = new_size;
1899     }
1900
1901     This->global_props[This->global_prop_cnt].name = heap_strdupW(name);
1902     if(!This->global_props[This->global_prop_cnt].name)
1903         return NULL;
1904
1905     This->global_props[This->global_prop_cnt].type = type;
1906     return This->global_props + This->global_prop_cnt++;
1907 }
1908
1909 static inline DWORD prop_to_dispid(HTMLWindow *This, global_prop_t *prop)
1910 {
1911     return MSHTML_DISPID_CUSTOM_MIN + (prop-This->global_props);
1912 }
1913
1914 HRESULT search_window_props(HTMLWindow *This, BSTR bstrName, DWORD grfdex, DISPID *pid)
1915 {
1916     DWORD i;
1917     ScriptHost *script_host;
1918     DISPID id;
1919
1920     for(i=0; i < This->global_prop_cnt; i++) {
1921         /* FIXME: case sensitivity */
1922         if(!strcmpW(This->global_props[i].name, bstrName)) {
1923             *pid = MSHTML_DISPID_CUSTOM_MIN+i;
1924             return S_OK;
1925         }
1926     }
1927
1928     if(find_global_prop(This, bstrName, grfdex, &script_host, &id)) {
1929         global_prop_t *prop;
1930
1931         prop = alloc_global_prop(This, GLOBAL_SCRIPTVAR, bstrName);
1932         if(!prop)
1933             return E_OUTOFMEMORY;
1934
1935         prop->script_host = script_host;
1936         prop->id = id;
1937
1938         *pid = prop_to_dispid(This, prop);
1939         return S_OK;
1940     }
1941
1942     return DISP_E_UNKNOWNNAME;
1943 }
1944
1945 static HRESULT WINAPI WindowDispEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1946 {
1947     HTMLWindow *This = impl_from_IDispatchEx(iface);
1948     HRESULT hres;
1949
1950     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1951
1952     hres = search_window_props(This, bstrName, grfdex, pid);
1953     if(hres != DISP_E_UNKNOWNNAME)
1954         return hres;
1955
1956     hres = IDispatchEx_GetDispID(&This->dispex.IDispatchEx_iface, bstrName, grfdex, pid);
1957     if(hres != DISP_E_UNKNOWNNAME)
1958         return hres;
1959
1960     if(This->doc) {
1961         global_prop_t *prop;
1962         IHTMLElement *elem;
1963
1964         hres = IHTMLDocument3_getElementById(&This->doc->basedoc.IHTMLDocument3_iface,
1965                                              bstrName, &elem);
1966         if(SUCCEEDED(hres) && elem) {
1967             IHTMLElement_Release(elem);
1968
1969             prop = alloc_global_prop(This, GLOBAL_ELEMENTVAR, bstrName);
1970             if(!prop)
1971                 return E_OUTOFMEMORY;
1972
1973             *pid = prop_to_dispid(This, prop);
1974             return S_OK;
1975         }
1976     }
1977
1978     return DISP_E_UNKNOWNNAME;
1979 }
1980
1981 static HRESULT WINAPI WindowDispEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1982         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1983 {
1984     HTMLWindow *This = impl_from_IDispatchEx(iface);
1985
1986     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1987
1988     if(id == DISPID_IHTMLWINDOW2_LOCATION && (wFlags & DISPATCH_PROPERTYPUT)) {
1989         HTMLLocation *location;
1990         HRESULT hres;
1991
1992         TRACE("forwarding to location.href\n");
1993
1994         hres = get_location(This, &location);
1995         if(FAILED(hres))
1996             return hres;
1997
1998         hres = IDispatchEx_InvokeEx(&location->dispex.IDispatchEx_iface, DISPID_VALUE, lcid,
1999                 wFlags, pdp, pvarRes, pei, pspCaller);
2000         IHTMLLocation_Release(&location->IHTMLLocation_iface);
2001         return hres;
2002     }
2003
2004     return IDispatchEx_InvokeEx(&This->dispex.IDispatchEx_iface, id, lcid, wFlags, pdp, pvarRes,
2005             pei, pspCaller);
2006 }
2007
2008 static HRESULT WINAPI WindowDispEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
2009 {
2010     HTMLWindow *This = impl_from_IDispatchEx(iface);
2011
2012     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
2013
2014     return IDispatchEx_DeleteMemberByName(&This->dispex.IDispatchEx_iface, bstrName, grfdex);
2015 }
2016
2017 static HRESULT WINAPI WindowDispEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
2018 {
2019     HTMLWindow *This = impl_from_IDispatchEx(iface);
2020
2021     TRACE("(%p)->(%x)\n", This, id);
2022
2023     return IDispatchEx_DeleteMemberByDispID(&This->dispex.IDispatchEx_iface, id);
2024 }
2025
2026 static HRESULT WINAPI WindowDispEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
2027 {
2028     HTMLWindow *This = impl_from_IDispatchEx(iface);
2029
2030     TRACE("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
2031
2032     return IDispatchEx_GetMemberProperties(&This->dispex.IDispatchEx_iface, id, grfdexFetch,
2033             pgrfdex);
2034 }
2035
2036 static HRESULT WINAPI WindowDispEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
2037 {
2038     HTMLWindow *This = impl_from_IDispatchEx(iface);
2039
2040     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
2041
2042     return IDispatchEx_GetMemberName(&This->dispex.IDispatchEx_iface, id, pbstrName);
2043 }
2044
2045 static HRESULT WINAPI WindowDispEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
2046 {
2047     HTMLWindow *This = impl_from_IDispatchEx(iface);
2048
2049     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
2050
2051     return IDispatchEx_GetNextDispID(&This->dispex.IDispatchEx_iface, grfdex, id, pid);
2052 }
2053
2054 static HRESULT WINAPI WindowDispEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
2055 {
2056     HTMLWindow *This = impl_from_IDispatchEx(iface);
2057
2058     TRACE("(%p)->(%p)\n", This, ppunk);
2059
2060     *ppunk = NULL;
2061     return S_OK;
2062 }
2063
2064 static const IDispatchExVtbl WindowDispExVtbl = {
2065     WindowDispEx_QueryInterface,
2066     WindowDispEx_AddRef,
2067     WindowDispEx_Release,
2068     WindowDispEx_GetTypeInfoCount,
2069     WindowDispEx_GetTypeInfo,
2070     WindowDispEx_GetIDsOfNames,
2071     WindowDispEx_Invoke,
2072     WindowDispEx_GetDispID,
2073     WindowDispEx_InvokeEx,
2074     WindowDispEx_DeleteMemberByName,
2075     WindowDispEx_DeleteMemberByDispID,
2076     WindowDispEx_GetMemberProperties,
2077     WindowDispEx_GetMemberName,
2078     WindowDispEx_GetNextDispID,
2079     WindowDispEx_GetNameSpaceParent
2080 };
2081
2082 static inline HTMLWindow *impl_from_IServiceProvider(IServiceProvider *iface)
2083 {
2084     return CONTAINING_RECORD(iface, HTMLWindow, IServiceProvider_iface);
2085 }
2086
2087 static HRESULT WINAPI HTMLWindowSP_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
2088 {
2089     HTMLWindow *This = impl_from_IServiceProvider(iface);
2090     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2091 }
2092
2093 static ULONG WINAPI HTMLWindowSP_AddRef(IServiceProvider *iface)
2094 {
2095     HTMLWindow *This = impl_from_IServiceProvider(iface);
2096     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
2097 }
2098
2099 static ULONG WINAPI HTMLWindowSP_Release(IServiceProvider *iface)
2100 {
2101     HTMLWindow *This = impl_from_IServiceProvider(iface);
2102     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
2103 }
2104
2105 static HRESULT WINAPI HTMLWindowSP_QueryService(IServiceProvider *iface, REFGUID guidService, REFIID riid, void **ppv)
2106 {
2107     HTMLWindow *This = impl_from_IServiceProvider(iface);
2108
2109     if(IsEqualGUID(guidService, &IID_IHTMLWindow2)) {
2110         TRACE("IID_IHTMLWindow2\n");
2111         return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2112     }
2113
2114     TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
2115
2116     if(!This->doc_obj)
2117         return E_NOINTERFACE;
2118
2119     return IServiceProvider_QueryService(&This->doc_obj->basedoc.IServiceProvider_iface,
2120             guidService, riid, ppv);
2121 }
2122
2123 static const IServiceProviderVtbl ServiceProviderVtbl = {
2124     HTMLWindowSP_QueryInterface,
2125     HTMLWindowSP_AddRef,
2126     HTMLWindowSP_Release,
2127     HTMLWindowSP_QueryService
2128 };
2129
2130 static inline HTMLWindow *impl_from_DispatchEx(DispatchEx *iface)
2131 {
2132     return CONTAINING_RECORD(iface, HTMLWindow, dispex);
2133 }
2134
2135 static HRESULT HTMLWindow_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
2136         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
2137 {
2138     HTMLWindow *This = impl_from_DispatchEx(dispex);
2139     global_prop_t *prop;
2140     DWORD idx;
2141     HRESULT hres;
2142
2143     idx = id - MSHTML_DISPID_CUSTOM_MIN;
2144     if(idx >= This->global_prop_cnt)
2145         return DISP_E_MEMBERNOTFOUND;
2146
2147     prop = This->global_props+idx;
2148
2149     switch(prop->type) {
2150     case GLOBAL_SCRIPTVAR: {
2151         IDispatchEx *dispex;
2152         IDispatch *disp;
2153
2154         disp = get_script_disp(prop->script_host);
2155         if(!disp)
2156             return E_UNEXPECTED;
2157
2158         hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
2159         if(SUCCEEDED(hres)) {
2160             TRACE("%s >>>\n", debugstr_w(prop->name));
2161             hres = IDispatchEx_InvokeEx(dispex, prop->id, lcid, flags, params, res, ei, caller);
2162             if(hres == S_OK)
2163                 TRACE("%s <<<\n", debugstr_w(prop->name));
2164             else
2165                 WARN("%s <<< %08x\n", debugstr_w(prop->name), hres);
2166             IDispatchEx_Release(dispex);
2167         }else {
2168             FIXME("No IDispatchEx\n");
2169         }
2170         IDispatch_Release(disp);
2171         break;
2172     }
2173     case GLOBAL_ELEMENTVAR: {
2174         IHTMLElement *elem;
2175
2176         hres = IHTMLDocument3_getElementById(&This->doc->basedoc.IHTMLDocument3_iface,
2177                                              prop->name, &elem);
2178         if(FAILED(hres))
2179             return hres;
2180
2181         if(!elem)
2182             return DISP_E_MEMBERNOTFOUND;
2183
2184         V_VT(res) = VT_DISPATCH;
2185         V_DISPATCH(res) = (IDispatch*)elem;
2186         break;
2187     }
2188     default:
2189         ERR("invalid type %d\n", prop->type);
2190         hres = DISP_E_MEMBERNOTFOUND;
2191     }
2192
2193     return hres;
2194 }
2195
2196
2197 static const dispex_static_data_vtbl_t HTMLWindow_dispex_vtbl = {
2198     NULL,
2199     NULL,
2200     HTMLWindow_invoke
2201 };
2202
2203 static const tid_t HTMLWindow_iface_tids[] = {
2204     IHTMLWindow2_tid,
2205     IHTMLWindow3_tid,
2206     IHTMLWindow4_tid,
2207     0
2208 };
2209
2210 static dispex_static_data_t HTMLWindow_dispex = {
2211     &HTMLWindow_dispex_vtbl,
2212     DispHTMLWindow2_tid,
2213     NULL,
2214     HTMLWindow_iface_tids
2215 };
2216
2217 HRESULT HTMLWindow_Create(HTMLDocumentObj *doc_obj, nsIDOMWindow *nswindow, HTMLWindow *parent, HTMLWindow **ret)
2218 {
2219     HTMLWindow *window;
2220
2221     window = heap_alloc_zero(sizeof(HTMLWindow));
2222     if(!window)
2223         return E_OUTOFMEMORY;
2224
2225     window->window_ref = heap_alloc(sizeof(windowref_t));
2226     if(!window->window_ref) {
2227         heap_free(window);
2228         return E_OUTOFMEMORY;
2229     }
2230
2231     window->IHTMLWindow2_iface.lpVtbl = &HTMLWindow2Vtbl;
2232     window->IHTMLWindow3_iface.lpVtbl = &HTMLWindow3Vtbl;
2233     window->IHTMLWindow4_iface.lpVtbl = &HTMLWindow4Vtbl;
2234     window->IHTMLPrivateWindow_iface.lpVtbl = &HTMLPrivateWindowVtbl;
2235     window->IDispatchEx_iface.lpVtbl = &WindowDispExVtbl;
2236     window->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
2237     window->ref = 1;
2238     window->doc_obj = doc_obj;
2239
2240     window->window_ref->window = window;
2241     window->window_ref->ref = 1;
2242
2243     init_dispex(&window->dispex, (IUnknown*)&window->IHTMLWindow2_iface, &HTMLWindow_dispex);
2244
2245     if(nswindow) {
2246         nsIDOMWindow_AddRef(nswindow);
2247         window->nswindow = nswindow;
2248     }
2249
2250     window->scriptmode = parent ? parent->scriptmode : SCRIPTMODE_GECKO;
2251     window->readystate = READYSTATE_UNINITIALIZED;
2252     list_init(&window->script_hosts);
2253
2254     window->task_magic = get_task_target_magic();
2255     update_window_doc(window);
2256
2257     list_init(&window->children);
2258     list_add_head(&window_list, &window->entry);
2259
2260     if(parent) {
2261         IHTMLWindow2_AddRef(&window->IHTMLWindow2_iface);
2262
2263         window->parent = parent;
2264         list_add_tail(&parent->children, &window->sibling_entry);
2265     }
2266
2267     *ret = window;
2268     return S_OK;
2269 }
2270
2271 void update_window_doc(HTMLWindow *window)
2272 {
2273     nsIDOMHTMLDocument *nshtmldoc;
2274     nsIDOMDocument *nsdoc;
2275     nsresult nsres;
2276
2277     nsres = nsIDOMWindow_GetDocument(window->nswindow, &nsdoc);
2278     if(NS_FAILED(nsres) || !nsdoc) {
2279         ERR("GetDocument failed: %08x\n", nsres);
2280         return;
2281     }
2282
2283     nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
2284     nsIDOMDocument_Release(nsdoc);
2285     if(NS_FAILED(nsres)) {
2286         ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
2287         return;
2288     }
2289
2290     if(!window->doc || window->doc->nsdoc != nshtmldoc) {
2291         HTMLDocumentNode *doc;
2292         HRESULT hres;
2293
2294         hres = create_doc_from_nsdoc(nshtmldoc, window->doc_obj, window, &doc);
2295         if(SUCCEEDED(hres)) {
2296             window_set_docnode(window, doc);
2297             htmldoc_release(&doc->basedoc);
2298         }else {
2299             ERR("create_doc_from_nsdoc failed: %08x\n", hres);
2300         }
2301     }
2302
2303     nsIDOMHTMLDocument_Release(nshtmldoc);
2304 }
2305
2306 HTMLWindow *nswindow_to_window(const nsIDOMWindow *nswindow)
2307 {
2308     HTMLWindow *iter;
2309
2310     LIST_FOR_EACH_ENTRY(iter, &window_list, HTMLWindow, entry) {
2311         if(iter->nswindow == nswindow)
2312             return iter;
2313     }
2314
2315     return NULL;
2316 }