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