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