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