xref: /reactos/dll/win32/mshtml/htmlwindow.c (revision 8a978a17)
1 /*
2  * Copyright 2006-2012 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 "mshtml_private.h"
20 
21 #include <exdispid.h>
22 
23 static struct list window_list = LIST_INIT(window_list);
24 
25 static inline BOOL is_outer_window(HTMLWindow *window)
26 {
27     return &window->outer_window->base == window;
28 }
29 
30 static void release_children(HTMLOuterWindow *This)
31 {
32     HTMLOuterWindow *child;
33 
34     while(!list_empty(&This->children)) {
35         child = LIST_ENTRY(list_tail(&This->children), HTMLOuterWindow, sibling_entry);
36 
37         list_remove(&child->sibling_entry);
38         child->parent = NULL;
39         IHTMLWindow2_Release(&child->base.IHTMLWindow2_iface);
40     }
41 }
42 
43 static HRESULT get_location(HTMLInnerWindow *This, HTMLLocation **ret)
44 {
45     if(This->location) {
46         IHTMLLocation_AddRef(&This->location->IHTMLLocation_iface);
47     }else {
48         HRESULT hres;
49 
50         hres = HTMLLocation_Create(This, &This->location);
51         if(FAILED(hres))
52             return hres;
53     }
54 
55     *ret = This->location;
56     return S_OK;
57 }
58 
59 void get_top_window(HTMLOuterWindow *window, HTMLOuterWindow **ret)
60 {
61     HTMLOuterWindow *iter;
62 
63     for(iter = window; iter->parent; iter = iter->parent);
64     *ret = iter;
65 }
66 
67 static inline HRESULT set_window_event(HTMLWindow *window, eventid_t eid, VARIANT *var)
68 {
69     if(!window->inner_window->doc) {
70         FIXME("No document\n");
71         return E_FAIL;
72     }
73 
74     return set_event_handler(&window->inner_window->event_target, eid, var);
75 }
76 
77 static inline HRESULT get_window_event(HTMLWindow *window, eventid_t eid, VARIANT *var)
78 {
79     if(!window->inner_window->doc) {
80         FIXME("No document\n");
81         return E_FAIL;
82     }
83 
84     return get_event_handler(&window->inner_window->event_target, eid, var);
85 }
86 
87 static void detach_inner_window(HTMLInnerWindow *window)
88 {
89     HTMLOuterWindow *outer_window = window->base.outer_window;
90 
91     if(outer_window && outer_window->doc_obj && outer_window == outer_window->doc_obj->basedoc.window)
92         window->doc->basedoc.cp_container.forward_container = NULL;
93 
94     if(window->doc) {
95         detach_events(window->doc);
96         while(!list_empty(&window->doc->plugin_hosts))
97             detach_plugin_host(LIST_ENTRY(list_head(&window->doc->plugin_hosts), PluginHost, entry));
98     }
99 
100     abort_window_bindings(window);
101     remove_target_tasks(window->task_magic);
102     release_script_hosts(window);
103     window->base.outer_window = NULL;
104 
105     if(outer_window && outer_window->base.inner_window == window) {
106         outer_window->base.inner_window = NULL;
107         IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
108     }
109 }
110 
111 static inline HTMLWindow *impl_from_IHTMLWindow2(IHTMLWindow2 *iface)
112 {
113     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow2_iface);
114 }
115 
116 static HRESULT WINAPI HTMLWindow2_QueryInterface(IHTMLWindow2 *iface, REFIID riid, void **ppv)
117 {
118     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
119 
120     TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
121 
122     if(IsEqualGUID(&IID_IUnknown, riid)) {
123         *ppv = &This->IHTMLWindow2_iface;
124     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
125         *ppv = &This->IHTMLWindow2_iface;
126     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
127         *ppv = &This->IDispatchEx_iface;
128     }else if(IsEqualGUID(&IID_IHTMLFramesCollection2, riid)) {
129         *ppv = &This->IHTMLWindow2_iface;
130     }else if(IsEqualGUID(&IID_IHTMLWindow2, riid)) {
131         *ppv = &This->IHTMLWindow2_iface;
132     }else if(IsEqualGUID(&IID_IHTMLWindow3, riid)) {
133         *ppv = &This->IHTMLWindow3_iface;
134     }else if(IsEqualGUID(&IID_IHTMLWindow4, riid)) {
135         *ppv = &This->IHTMLWindow4_iface;
136     }else if(IsEqualGUID(&IID_IHTMLWindow5, riid)) {
137         *ppv = &This->IHTMLWindow5_iface;
138     }else if(IsEqualGUID(&IID_IHTMLWindow6, riid)) {
139         *ppv = &This->IHTMLWindow6_iface;
140     }else if(IsEqualGUID(&IID_IHTMLPrivateWindow, riid)) {
141         *ppv = &This->IHTMLPrivateWindow_iface;
142     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
143         *ppv = &This->IServiceProvider_iface;
144     }else if(IsEqualGUID(&IID_ITravelLogClient, riid)) {
145         *ppv = &This->ITravelLogClient_iface;
146     }else if(IsEqualGUID(&IID_IObjectIdentity, riid)) {
147         *ppv = &This->IObjectIdentity_iface;
148     }else if(dispex_query_interface(&This->inner_window->event_target.dispex, riid, ppv)) {
149         assert(!*ppv);
150         return E_NOINTERFACE;
151     }else {
152         *ppv = NULL;
153         WARN("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
154         return E_NOINTERFACE;
155     }
156 
157     IUnknown_AddRef((IUnknown*)*ppv);
158     return S_OK;
159 }
160 
161 static ULONG WINAPI HTMLWindow2_AddRef(IHTMLWindow2 *iface)
162 {
163     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
164     LONG ref = InterlockedIncrement(&This->ref);
165 
166     TRACE("(%p) ref=%d\n", This, ref);
167 
168     return ref;
169 }
170 
171 static void release_outer_window(HTMLOuterWindow *This)
172 {
173     if(This->pending_window) {
174         abort_window_bindings(This->pending_window);
175         This->pending_window->base.outer_window = NULL;
176         IHTMLWindow2_Release(&This->pending_window->base.IHTMLWindow2_iface);
177     }
178 
179     remove_target_tasks(This->task_magic);
180     set_current_mon(This, NULL, 0);
181     if(This->base.inner_window)
182         detach_inner_window(This->base.inner_window);
183     release_children(This);
184 
185     if(This->secmgr)
186         IInternetSecurityManager_Release(This->secmgr);
187 
188     if(This->frame_element)
189         This->frame_element->content_window = NULL;
190 
191     This->window_ref->window = NULL;
192     windowref_release(This->window_ref);
193 
194     if(This->nswindow)
195         nsIDOMWindow_Release(This->nswindow);
196 
197     list_remove(&This->entry);
198     heap_free(This);
199 }
200 
201 static void release_inner_window(HTMLInnerWindow *This)
202 {
203     unsigned i;
204 
205     TRACE("%p\n", This);
206 
207     detach_inner_window(This);
208 
209     if(This->doc) {
210         This->doc->window = NULL;
211         htmldoc_release(&This->doc->basedoc);
212     }
213 
214     release_dispex(&This->event_target.dispex);
215 
216     for(i=0; i < This->global_prop_cnt; i++)
217         heap_free(This->global_props[i].name);
218     heap_free(This->global_props);
219 
220     if(This->location) {
221         This->location->window = NULL;
222         IHTMLLocation_Release(&This->location->IHTMLLocation_iface);
223     }
224 
225     if(This->image_factory) {
226         This->image_factory->window = NULL;
227         IHTMLImageElementFactory_Release(&This->image_factory->IHTMLImageElementFactory_iface);
228     }
229 
230     if(This->option_factory) {
231         This->option_factory->window = NULL;
232         IHTMLOptionElementFactory_Release(&This->option_factory->IHTMLOptionElementFactory_iface);
233     }
234 
235     if(This->xhr_factory) {
236         This->xhr_factory->window = NULL;
237         IHTMLXMLHttpRequestFactory_Release(&This->xhr_factory->IHTMLXMLHttpRequestFactory_iface);
238     }
239 
240     if(This->screen)
241         IHTMLScreen_Release(This->screen);
242 
243     if(This->history) {
244         This->history->window = NULL;
245         IOmHistory_Release(&This->history->IOmHistory_iface);
246     }
247 
248     if(This->session_storage)
249         IHTMLStorage_Release(This->session_storage);
250 
251     if(This->mon)
252         IMoniker_Release(This->mon);
253 
254     heap_free(This);
255 }
256 
257 static ULONG WINAPI HTMLWindow2_Release(IHTMLWindow2 *iface)
258 {
259     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
260     LONG ref = InterlockedDecrement(&This->ref);
261 
262     TRACE("(%p) ref=%d\n", This, ref);
263 
264     if(!ref) {
265         if(is_outer_window(This))
266             release_outer_window(This->outer_window);
267         else
268             release_inner_window(This->inner_window);
269     }
270 
271     return ref;
272 }
273 
274 static HRESULT WINAPI HTMLWindow2_GetTypeInfoCount(IHTMLWindow2 *iface, UINT *pctinfo)
275 {
276     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
277 
278     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
279 }
280 
281 static HRESULT WINAPI HTMLWindow2_GetTypeInfo(IHTMLWindow2 *iface, UINT iTInfo,
282                                               LCID lcid, ITypeInfo **ppTInfo)
283 {
284     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
285 
286     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
287 }
288 
289 static HRESULT WINAPI HTMLWindow2_GetIDsOfNames(IHTMLWindow2 *iface, REFIID riid,
290                                                 LPOLESTR *rgszNames, UINT cNames,
291                                                 LCID lcid, DISPID *rgDispId)
292 {
293     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
294 
295     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
296             rgDispId);
297 }
298 
299 static HRESULT WINAPI HTMLWindow2_Invoke(IHTMLWindow2 *iface, DISPID dispIdMember,
300                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
301                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
302 {
303     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
304 
305     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
306             pDispParams, pVarResult, pExcepInfo, puArgErr);
307 }
308 
309 static HRESULT get_frame_by_index(HTMLOuterWindow *This, UINT32 index, HTMLOuterWindow **ret)
310 {
311     nsIDOMWindowCollection *nsframes;
312     nsIDOMWindow *nswindow;
313     UINT32 length;
314     nsresult nsres;
315 
316     nsres = nsIDOMWindow_GetFrames(This->nswindow, &nsframes);
317     if(NS_FAILED(nsres)) {
318         FIXME("nsIDOMWindow_GetFrames failed: 0x%08x\n", nsres);
319         return E_FAIL;
320     }
321 
322     nsres = nsIDOMWindowCollection_GetLength(nsframes, &length);
323     assert(nsres == NS_OK);
324 
325     if(index >= length) {
326         nsIDOMWindowCollection_Release(nsframes);
327         return DISP_E_MEMBERNOTFOUND;
328     }
329 
330     nsres = nsIDOMWindowCollection_Item(nsframes, index, &nswindow);
331     nsIDOMWindowCollection_Release(nsframes);
332     if(NS_FAILED(nsres)) {
333         FIXME("nsIDOMWindowCollection_Item failed: 0x%08x\n", nsres);
334         return E_FAIL;
335     }
336 
337     *ret = nswindow_to_window(nswindow);
338 
339     nsIDOMWindow_Release(nswindow);
340     return S_OK;
341 }
342 
343 HRESULT get_frame_by_name(HTMLOuterWindow *This, const WCHAR *name, BOOL deep, HTMLOuterWindow **ret)
344 {
345     nsIDOMWindowCollection *nsframes;
346     HTMLOuterWindow *window = NULL;
347     nsIDOMWindow *nswindow;
348     nsAString name_str;
349     UINT32 length, i;
350     nsresult nsres;
351     HRESULT hres = S_OK;
352 
353     nsres = nsIDOMWindow_GetFrames(This->nswindow, &nsframes);
354     if(NS_FAILED(nsres)) {
355         FIXME("nsIDOMWindow_GetFrames failed: 0x%08x\n", nsres);
356         return E_FAIL;
357     }
358 
359     if(!nsframes) {
360         WARN("nsIDOMWindow_GetFrames returned NULL nsframes: %p\n", This->nswindow);
361         return DISP_E_MEMBERNOTFOUND;
362     }
363 
364     nsAString_InitDepend(&name_str, name);
365     nsres = nsIDOMWindowCollection_NamedItem(nsframes, &name_str, &nswindow);
366     nsAString_Finish(&name_str);
367     if(NS_FAILED(nsres)) {
368         nsIDOMWindowCollection_Release(nsframes);
369         return E_FAIL;
370     }
371 
372     if(nswindow) {
373         *ret = nswindow_to_window(nswindow);
374         return S_OK;
375     }
376 
377     nsres = nsIDOMWindowCollection_GetLength(nsframes, &length);
378     assert(nsres == NS_OK);
379 
380     for(i = 0; i < length && !window; ++i) {
381         HTMLOuterWindow *window_iter;
382         BSTR id;
383 
384         nsres = nsIDOMWindowCollection_Item(nsframes, i, &nswindow);
385         if(NS_FAILED(nsres)) {
386             FIXME("nsIDOMWindowCollection_Item failed: 0x%08x\n", nsres);
387             hres = E_FAIL;
388             break;
389         }
390 
391         window_iter = nswindow_to_window(nswindow);
392 
393         nsIDOMWindow_Release(nswindow);
394 
395         if(!window_iter) {
396             WARN("nsIDOMWindow without HTMLOuterWindow: %p\n", nswindow);
397             continue;
398         }
399 
400         hres = IHTMLElement_get_id(&window_iter->frame_element->element.IHTMLElement_iface, &id);
401         if(FAILED(hres)) {
402             FIXME("IHTMLElement_get_id failed: 0x%08x\n", hres);
403             break;
404         }
405 
406         if(id && !strcmpiW(id, name))
407             window = window_iter;
408 
409         SysFreeString(id);
410 
411         if(!window && deep)
412             get_frame_by_name(window_iter, name, TRUE, &window);
413     }
414 
415     nsIDOMWindowCollection_Release(nsframes);
416     if(FAILED(hres))
417         return hres;
418 
419     *ret = window;
420     return NS_OK;
421 }
422 
423 static HRESULT WINAPI HTMLWindow2_item(IHTMLWindow2 *iface, VARIANT *pvarIndex, VARIANT *pvarResult)
424 {
425     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
426     HTMLOuterWindow *window = NULL;
427     HRESULT hres;
428 
429     TRACE("(%p)->(%p %p)\n", This, pvarIndex, pvarResult);
430 
431     switch(V_VT(pvarIndex)) {
432     case VT_I4: {
433         int index = V_I4(pvarIndex);
434         TRACE("Getting index %d\n", index);
435         if(index < 0)
436             return DISP_E_MEMBERNOTFOUND;
437         hres = get_frame_by_index(This->outer_window, index, &window);
438         break;
439     }
440     case VT_UINT: {
441         unsigned int index = V_UINT(pvarIndex);
442         TRACE("Getting index %u\n", index);
443         hres = get_frame_by_index(This->outer_window, index, &window);
444         break;
445     }
446     case VT_BSTR: {
447         BSTR str = V_BSTR(pvarIndex);
448         TRACE("Getting name %s\n", wine_dbgstr_w(str));
449         hres = get_frame_by_name(This->outer_window, str, FALSE, &window);
450         break;
451     }
452     default:
453         WARN("Invalid index %s\n", debugstr_variant(pvarIndex));
454         return E_INVALIDARG;
455     }
456 
457     if(FAILED(hres))
458         return hres;
459     if(!window)
460         return DISP_E_MEMBERNOTFOUND;
461 
462     IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
463     V_VT(pvarResult) = VT_DISPATCH;
464     V_DISPATCH(pvarResult) = (IDispatch*)window;
465     return S_OK;
466 }
467 
468 static HRESULT WINAPI HTMLWindow2_get_length(IHTMLWindow2 *iface, LONG *p)
469 {
470     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
471     nsIDOMWindowCollection *nscollection;
472     UINT32 length;
473     nsresult nsres;
474 
475     TRACE("(%p)->(%p)\n", This, p);
476 
477     nsres = nsIDOMWindow_GetFrames(This->outer_window->nswindow, &nscollection);
478     if(NS_FAILED(nsres)) {
479         ERR("GetFrames failed: %08x\n", nsres);
480         return E_FAIL;
481     }
482 
483     nsres = nsIDOMWindowCollection_GetLength(nscollection, &length);
484     nsIDOMWindowCollection_Release(nscollection);
485     if(NS_FAILED(nsres)) {
486         ERR("GetLength failed: %08x\n", nsres);
487         return E_FAIL;
488     }
489 
490     *p = length;
491     return S_OK;
492 }
493 
494 static HRESULT WINAPI HTMLWindow2_get_frames(IHTMLWindow2 *iface, IHTMLFramesCollection2 **p)
495 {
496     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
497     FIXME("(%p)->(%p): semi-stub\n", This, p);
498 
499     /* FIXME: Should return a separate Window object */
500     *p = (IHTMLFramesCollection2*)&This->IHTMLWindow2_iface;
501     HTMLWindow2_AddRef(iface);
502     return S_OK;
503 }
504 
505 static HRESULT WINAPI HTMLWindow2_put_defaultStatus(IHTMLWindow2 *iface, BSTR v)
506 {
507     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
508     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
509     return E_NOTIMPL;
510 }
511 
512 static HRESULT WINAPI HTMLWindow2_get_defaultStatus(IHTMLWindow2 *iface, BSTR *p)
513 {
514     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
515     FIXME("(%p)->(%p)\n", This, p);
516     return E_NOTIMPL;
517 }
518 
519 static HRESULT WINAPI HTMLWindow2_put_status(IHTMLWindow2 *iface, BSTR v)
520 {
521     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
522 
523     WARN("(%p)->(%s)\n", This, debugstr_w(v));
524 
525     /*
526      * FIXME: Since IE7, setting status is blocked, but still possible in certain circumstances.
527      * Ignoring the call should be enough for us.
528      */
529     return S_OK;
530 }
531 
532 static HRESULT WINAPI HTMLWindow2_get_status(IHTMLWindow2 *iface, BSTR *p)
533 {
534     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
535 
536     TRACE("(%p)->(%p)\n", This, p);
537 
538     /* See put_status */
539     *p = NULL;
540     return S_OK;
541 }
542 
543 static HRESULT WINAPI HTMLWindow2_setTimeout(IHTMLWindow2 *iface, BSTR expression,
544         LONG msec, VARIANT *language, LONG *timerID)
545 {
546     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
547     VARIANT expr_var;
548 
549     TRACE("(%p)->(%s %d %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
550 
551     V_VT(&expr_var) = VT_BSTR;
552     V_BSTR(&expr_var) = expression;
553 
554     return IHTMLWindow3_setTimeout(&This->IHTMLWindow3_iface, &expr_var, msec, language, timerID);
555 }
556 
557 static HRESULT WINAPI HTMLWindow2_clearTimeout(IHTMLWindow2 *iface, LONG timerID)
558 {
559     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
560 
561     TRACE("(%p)->(%d)\n", This, timerID);
562 
563     return clear_task_timer(This->inner_window, FALSE, timerID);
564 }
565 
566 #define MAX_MESSAGE_LEN 2000
567 
568 static HRESULT WINAPI HTMLWindow2_alert(IHTMLWindow2 *iface, BSTR message)
569 {
570     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
571     WCHAR title[100], *msg = message;
572     DWORD len;
573 
574     TRACE("(%p)->(%s)\n", This, debugstr_w(message));
575 
576     if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, title,
577                     sizeof(title)/sizeof(WCHAR))) {
578         WARN("Could not load message box title: %d\n", GetLastError());
579         return S_OK;
580     }
581 
582     len = SysStringLen(message);
583     if(len > MAX_MESSAGE_LEN) {
584         msg = heap_alloc((MAX_MESSAGE_LEN+1)*sizeof(WCHAR));
585         if(!msg)
586             return E_OUTOFMEMORY;
587         memcpy(msg, message, MAX_MESSAGE_LEN*sizeof(WCHAR));
588         msg[MAX_MESSAGE_LEN] = 0;
589     }
590 
591     MessageBoxW(This->outer_window->doc_obj->hwnd, msg, title, MB_ICONWARNING);
592     if(msg != message)
593         heap_free(msg);
594     return S_OK;
595 }
596 
597 static HRESULT WINAPI HTMLWindow2_confirm(IHTMLWindow2 *iface, BSTR message,
598         VARIANT_BOOL *confirmed)
599 {
600     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
601     WCHAR wszTitle[100];
602 
603     TRACE("(%p)->(%s %p)\n", This, debugstr_w(message), confirmed);
604 
605     if(!confirmed) return E_INVALIDARG;
606 
607     if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, wszTitle,
608                 sizeof(wszTitle)/sizeof(WCHAR))) {
609         WARN("Could not load message box title: %d\n", GetLastError());
610         *confirmed = VARIANT_TRUE;
611         return S_OK;
612     }
613 
614     if(MessageBoxW(This->outer_window->doc_obj->hwnd, message, wszTitle,
615                 MB_OKCANCEL|MB_ICONQUESTION)==IDOK)
616         *confirmed = VARIANT_TRUE;
617     else *confirmed = VARIANT_FALSE;
618 
619     return S_OK;
620 }
621 
622 typedef struct
623 {
624     BSTR message;
625     BSTR dststr;
626     VARIANT *textdata;
627 }prompt_arg;
628 
629 static INT_PTR CALLBACK prompt_dlgproc(HWND hwnd, UINT msg,
630         WPARAM wparam, LPARAM lparam)
631 {
632     switch(msg)
633     {
634         case WM_INITDIALOG:
635         {
636             prompt_arg *arg = (prompt_arg*)lparam;
637             WCHAR wszTitle[100];
638 
639             if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, wszTitle,
640                         sizeof(wszTitle)/sizeof(WCHAR))) {
641                 WARN("Could not load message box title: %d\n", GetLastError());
642                 EndDialog(hwnd, wparam);
643                 return FALSE;
644             }
645 
646             SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
647             SetWindowTextW(hwnd, wszTitle);
648             SetWindowTextW(GetDlgItem(hwnd, ID_PROMPT_PROMPT), arg->message);
649             SetWindowTextW(GetDlgItem(hwnd, ID_PROMPT_EDIT), arg->dststr);
650             return FALSE;
651         }
652         case WM_COMMAND:
653             switch(wparam)
654             {
655                 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
656                     EndDialog(hwnd, wparam);
657                     return TRUE;
658                 case MAKEWPARAM(IDOK, BN_CLICKED):
659                 {
660                     prompt_arg *arg =
661                         (prompt_arg*)GetWindowLongPtrW(hwnd, DWLP_USER);
662                     HWND hwndPrompt = GetDlgItem(hwnd, ID_PROMPT_EDIT);
663                     INT len = GetWindowTextLengthW(hwndPrompt);
664 
665                     if(!arg->textdata)
666                     {
667                         EndDialog(hwnd, wparam);
668                         return TRUE;
669                     }
670 
671                     V_VT(arg->textdata) = VT_BSTR;
672                     if(!len && !arg->dststr)
673                         V_BSTR(arg->textdata) = NULL;
674                     else
675                     {
676                         V_BSTR(arg->textdata) = SysAllocStringLen(NULL, len);
677                         GetWindowTextW(hwndPrompt, V_BSTR(arg->textdata), len+1);
678                     }
679                     EndDialog(hwnd, wparam);
680                     return TRUE;
681                 }
682             }
683             return FALSE;
684         case WM_CLOSE:
685             EndDialog(hwnd, IDCANCEL);
686             return TRUE;
687         default:
688             return FALSE;
689     }
690 }
691 
692 static HRESULT WINAPI HTMLWindow2_prompt(IHTMLWindow2 *iface, BSTR message,
693         BSTR dststr, VARIANT *textdata)
694 {
695     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
696     prompt_arg arg;
697 
698     TRACE("(%p)->(%s %s %p)\n", This, debugstr_w(message), debugstr_w(dststr), textdata);
699 
700     if(textdata) V_VT(textdata) = VT_NULL;
701 
702     arg.message = message;
703     arg.dststr = dststr;
704     arg.textdata = textdata;
705 
706     DialogBoxParamW(hInst, MAKEINTRESOURCEW(ID_PROMPT_DIALOG),
707             This->outer_window->doc_obj->hwnd, prompt_dlgproc, (LPARAM)&arg);
708     return S_OK;
709 }
710 
711 static HRESULT WINAPI HTMLWindow2_get_Image(IHTMLWindow2 *iface, IHTMLImageElementFactory **p)
712 {
713     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
714     HTMLInnerWindow *window = This->inner_window;
715 
716     TRACE("(%p)->(%p)\n", This, p);
717 
718     if(!window->image_factory) {
719         HRESULT hres;
720 
721         hres = HTMLImageElementFactory_Create(window, &window->image_factory);
722         if(FAILED(hres))
723             return hres;
724     }
725 
726     *p = &window->image_factory->IHTMLImageElementFactory_iface;
727     IHTMLImageElementFactory_AddRef(*p);
728 
729     return S_OK;
730 }
731 
732 static HRESULT WINAPI HTMLWindow2_get_location(IHTMLWindow2 *iface, IHTMLLocation **p)
733 {
734     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
735     HTMLLocation *location;
736     HRESULT hres;
737 
738     TRACE("(%p)->(%p)\n", This, p);
739 
740     hres = get_location(This->inner_window, &location);
741     if(FAILED(hres))
742         return hres;
743 
744     *p = &location->IHTMLLocation_iface;
745     return S_OK;
746 }
747 
748 static HRESULT WINAPI HTMLWindow2_get_history(IHTMLWindow2 *iface, IOmHistory **p)
749 {
750     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
751     HTMLInnerWindow *window = This->inner_window;
752 
753     TRACE("(%p)->(%p)\n", This, p);
754 
755     if(!window->history) {
756         HRESULT hres;
757 
758         hres = create_history(window, &window->history);
759         if(FAILED(hres))
760             return hres;
761     }
762 
763     IOmHistory_AddRef(&window->history->IOmHistory_iface);
764     *p = &window->history->IOmHistory_iface;
765     return S_OK;
766 }
767 
768 static BOOL notify_webbrowser_close(HTMLOuterWindow *window, HTMLDocumentObj *doc)
769 {
770     IConnectionPointContainer *cp_container;
771     VARIANT_BOOL cancel = VARIANT_FALSE;
772     IEnumConnections *enum_conn;
773     VARIANT args[2];
774     DISPPARAMS dp = {args, NULL, 2, 0};
775     CONNECTDATA conn_data;
776     IConnectionPoint *cp;
777     IDispatch *disp;
778     ULONG fetched;
779     HRESULT hres;
780 
781     if(!doc->webbrowser)
782         return TRUE;
783 
784     hres = IUnknown_QueryInterface(doc->webbrowser, &IID_IConnectionPointContainer, (void**)&cp_container);
785     if(FAILED(hres))
786         return TRUE;
787 
788     hres = IConnectionPointContainer_FindConnectionPoint(cp_container, &DIID_DWebBrowserEvents2, &cp);
789     IConnectionPointContainer_Release(cp_container);
790     if(FAILED(hres))
791         return TRUE;
792 
793     hres = IConnectionPoint_EnumConnections(cp, &enum_conn);
794     IConnectionPoint_Release(cp);
795     if(FAILED(hres))
796         return TRUE;
797 
798     while(!cancel) {
799         conn_data.pUnk = NULL;
800         conn_data.dwCookie = 0;
801         fetched = 0;
802         hres = IEnumConnections_Next(enum_conn, 1, &conn_data, &fetched);
803         if(hres != S_OK)
804             break;
805 
806         hres = IUnknown_QueryInterface(conn_data.pUnk, &IID_IDispatch, (void**)&disp);
807         IUnknown_Release(conn_data.pUnk);
808         if(FAILED(hres))
809             continue;
810 
811         V_VT(args) = VT_BYREF|VT_BOOL;
812         V_BOOLREF(args) = &cancel;
813         V_VT(args+1) = VT_BOOL;
814         V_BOOL(args+1) = window->parent ? VARIANT_TRUE : VARIANT_FALSE;
815         hres = IDispatch_Invoke(disp, DISPID_WINDOWCLOSING, &IID_NULL, 0, DISPATCH_METHOD, &dp, NULL, NULL, NULL);
816         IDispatch_Release(disp);
817         if(FAILED(hres))
818             cancel = VARIANT_FALSE;
819     }
820 
821     IEnumConnections_Release(enum_conn);
822     return !cancel;
823 }
824 
825 static HRESULT WINAPI HTMLWindow2_close(IHTMLWindow2 *iface)
826 {
827     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
828     HTMLOuterWindow *window = This->outer_window;
829 
830     TRACE("(%p)\n", This);
831 
832     if(!window->doc_obj) {
833         FIXME("No document object\n");
834         return E_FAIL;
835     }
836 
837     if(!notify_webbrowser_close(window, window->doc_obj))
838         return S_OK;
839 
840     FIXME("default action not implemented\n");
841     return E_NOTIMPL;
842 }
843 
844 static HRESULT WINAPI HTMLWindow2_put_opener(IHTMLWindow2 *iface, VARIANT v)
845 {
846     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
847     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
848     return E_NOTIMPL;
849 }
850 
851 static HRESULT WINAPI HTMLWindow2_get_opener(IHTMLWindow2 *iface, VARIANT *p)
852 {
853     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
854 
855     FIXME("(%p)->(%p) returning empty\n", This, p);
856 
857     V_VT(p) = VT_EMPTY;
858     return S_OK;
859 }
860 
861 static HRESULT WINAPI HTMLWindow2_get_navigator(IHTMLWindow2 *iface, IOmNavigator **p)
862 {
863     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
864 
865     TRACE("(%p)->(%p)\n", This, p);
866 
867     *p = OmNavigator_Create();
868     return *p ? S_OK : E_OUTOFMEMORY;
869 }
870 
871 static HRESULT WINAPI HTMLWindow2_put_name(IHTMLWindow2 *iface, BSTR v)
872 {
873     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
874     nsAString name_str;
875     nsresult nsres;
876 
877     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
878 
879     nsAString_InitDepend(&name_str, v);
880     nsres = nsIDOMWindow_SetName(This->outer_window->nswindow, &name_str);
881     nsAString_Finish(&name_str);
882     if(NS_FAILED(nsres))
883         ERR("SetName failed: %08x\n", nsres);
884 
885     return S_OK;
886 }
887 
888 static HRESULT WINAPI HTMLWindow2_get_name(IHTMLWindow2 *iface, BSTR *p)
889 {
890     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
891     nsAString name_str;
892     nsresult nsres;
893 
894     TRACE("(%p)->(%p)\n", This, p);
895 
896     nsAString_Init(&name_str, NULL);
897     nsres = nsIDOMWindow_GetName(This->outer_window->nswindow, &name_str);
898     return return_nsstr(nsres, &name_str, p);
899 }
900 
901 static HRESULT WINAPI HTMLWindow2_get_parent(IHTMLWindow2 *iface, IHTMLWindow2 **p)
902 {
903     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
904     HTMLOuterWindow *window = This->outer_window;
905 
906     TRACE("(%p)->(%p)\n", This, p);
907 
908     if(!window->parent)
909         return IHTMLWindow2_get_self(&This->IHTMLWindow2_iface, p);
910 
911     *p = &window->parent->base.IHTMLWindow2_iface;
912     IHTMLWindow2_AddRef(*p);
913     return S_OK;
914 }
915 
916 static HRESULT WINAPI HTMLWindow2_open(IHTMLWindow2 *iface, BSTR url, BSTR name,
917          BSTR features, VARIANT_BOOL replace, IHTMLWindow2 **pomWindowResult)
918 {
919     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
920     HTMLOuterWindow *window = This->outer_window;
921     INewWindowManager *new_window_mgr;
922     BSTR uri_str;
923     IUri *uri;
924     HRESULT hres;
925 
926     static const WCHAR _selfW[] = {'_','s','e','l','f',0};
927 
928     TRACE("(%p)->(%s %s %s %x %p)\n", This, debugstr_w(url), debugstr_w(name),
929           debugstr_w(features), replace, pomWindowResult);
930 
931     if(!window->doc_obj || !window->uri_nofrag)
932         return E_UNEXPECTED;
933 
934     if(name && *name == '_') {
935         if(!strcmpW(name, _selfW)) {
936             if((features && *features) || replace)
937                 FIXME("Unsupported arguments for _self target\n");
938 
939             hres = IHTMLWindow2_navigate(&This->IHTMLWindow2_iface, url);
940             if(FAILED(hres))
941                 return hres;
942 
943             if(pomWindowResult) {
944                 FIXME("Returning this window for _self target\n");
945                 *pomWindowResult = &This->IHTMLWindow2_iface;
946                 IHTMLWindow2_AddRef(*pomWindowResult);
947             }
948 
949             return S_OK;
950         }
951 
952         FIXME("Unsupported name %s\n", debugstr_w(name));
953         return E_NOTIMPL;
954     }
955 
956     hres = do_query_service((IUnknown*)window->doc_obj->client, &SID_SNewWindowManager, &IID_INewWindowManager,
957             (void**)&new_window_mgr);
958     if(FAILED(hres)) {
959         FIXME("No INewWindowManager\n");
960         return E_NOTIMPL;
961     }
962 
963     hres = IUri_GetDisplayUri(window->uri_nofrag, &uri_str);
964     if(SUCCEEDED(hres)) {
965         hres = INewWindowManager_EvaluateNewWindow(new_window_mgr, url, name, uri_str,
966                 features, !!replace, window->doc_obj->has_popup ? 0 : NWMF_FIRST, 0);
967         window->doc_obj->has_popup = TRUE;
968         SysFreeString(uri_str);
969     }
970     INewWindowManager_Release(new_window_mgr);
971     if(FAILED(hres)) {
972         *pomWindowResult = NULL;
973         return S_OK;
974     }
975 
976     hres = create_relative_uri(window, url, &uri);
977     if(FAILED(hres))
978         return hres;
979 
980     hres = navigate_new_window(window, uri, name, NULL, pomWindowResult);
981     IUri_Release(uri);
982     return hres;
983 }
984 
985 static HRESULT WINAPI HTMLWindow2_get_self(IHTMLWindow2 *iface, IHTMLWindow2 **p)
986 {
987     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
988 
989     TRACE("(%p)->(%p)\n", This, p);
990 
991     /* FIXME: We should return kind of proxy window here. */
992     IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
993     *p = &This->IHTMLWindow2_iface;
994     return S_OK;
995 }
996 
997 static HRESULT WINAPI HTMLWindow2_get_top(IHTMLWindow2 *iface, IHTMLWindow2 **p)
998 {
999     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1000     HTMLOuterWindow *top;
1001 
1002     TRACE("(%p)->(%p)\n", This, p);
1003 
1004     get_top_window(This->outer_window, &top);
1005     *p = &top->base.IHTMLWindow2_iface;
1006     IHTMLWindow2_AddRef(*p);
1007 
1008     return S_OK;
1009 }
1010 
1011 static HRESULT WINAPI HTMLWindow2_get_window(IHTMLWindow2 *iface, IHTMLWindow2 **p)
1012 {
1013     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1014 
1015     TRACE("(%p)->(%p)\n", This, p);
1016 
1017     /* FIXME: We should return kind of proxy window here. */
1018     IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1019     *p = &This->IHTMLWindow2_iface;
1020     return S_OK;
1021 }
1022 
1023 static HRESULT WINAPI HTMLWindow2_navigate(IHTMLWindow2 *iface, BSTR url)
1024 {
1025     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1026 
1027     TRACE("(%p)->(%s)\n", This, debugstr_w(url));
1028 
1029     return navigate_url(This->outer_window, url, This->outer_window->uri, BINDING_NAVIGATED);
1030 }
1031 
1032 static HRESULT WINAPI HTMLWindow2_put_onfocus(IHTMLWindow2 *iface, VARIANT v)
1033 {
1034     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1035 
1036     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1037 
1038     return set_window_event(This, EVENTID_FOCUS, &v);
1039 }
1040 
1041 static HRESULT WINAPI HTMLWindow2_get_onfocus(IHTMLWindow2 *iface, VARIANT *p)
1042 {
1043     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1044 
1045     TRACE("(%p)->(%p)\n", This, p);
1046 
1047     return get_window_event(This, EVENTID_FOCUS, p);
1048 }
1049 
1050 static HRESULT WINAPI HTMLWindow2_put_onblur(IHTMLWindow2 *iface, VARIANT v)
1051 {
1052     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1053 
1054     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1055 
1056     return set_window_event(This, EVENTID_BLUR, &v);
1057 }
1058 
1059 static HRESULT WINAPI HTMLWindow2_get_onblur(IHTMLWindow2 *iface, VARIANT *p)
1060 {
1061     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1062 
1063     TRACE("(%p)->(%p)\n", This, p);
1064 
1065     return get_window_event(This, EVENTID_BLUR, p);
1066 }
1067 
1068 static HRESULT WINAPI HTMLWindow2_put_onload(IHTMLWindow2 *iface, VARIANT v)
1069 {
1070     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1071 
1072     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1073 
1074     return set_window_event(This, EVENTID_LOAD, &v);
1075 }
1076 
1077 static HRESULT WINAPI HTMLWindow2_get_onload(IHTMLWindow2 *iface, VARIANT *p)
1078 {
1079     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1080 
1081     TRACE("(%p)->(%p)\n", This, p);
1082 
1083     return get_window_event(This, EVENTID_LOAD, p);
1084 }
1085 
1086 static HRESULT WINAPI HTMLWindow2_put_onbeforeunload(IHTMLWindow2 *iface, VARIANT v)
1087 {
1088     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1089 
1090     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1091 
1092     return set_window_event(This, EVENTID_BEFOREUNLOAD, &v);
1093 }
1094 
1095 static HRESULT WINAPI HTMLWindow2_get_onbeforeunload(IHTMLWindow2 *iface, VARIANT *p)
1096 {
1097     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1098 
1099     TRACE("(%p)->(%p)\n", This, p);
1100 
1101     return get_window_event(This, EVENTID_BEFOREUNLOAD, p);
1102 }
1103 
1104 static HRESULT WINAPI HTMLWindow2_put_onunload(IHTMLWindow2 *iface, VARIANT v)
1105 {
1106     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1107     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1108     return E_NOTIMPL;
1109 }
1110 
1111 static HRESULT WINAPI HTMLWindow2_get_onunload(IHTMLWindow2 *iface, VARIANT *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_put_onhelp(IHTMLWindow2 *iface, VARIANT v)
1119 {
1120     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1121 
1122     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1123 
1124     return set_window_event(This, EVENTID_HELP, &v);
1125 }
1126 
1127 static HRESULT WINAPI HTMLWindow2_get_onhelp(IHTMLWindow2 *iface, VARIANT *p)
1128 {
1129     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1130 
1131     TRACE("(%p)->(%p)\n", This, p);
1132 
1133     return get_window_event(This, EVENTID_HELP, p);
1134 }
1135 
1136 static HRESULT WINAPI HTMLWindow2_put_onerror(IHTMLWindow2 *iface, VARIANT v)
1137 {
1138     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1139 
1140     FIXME("(%p)->(%s) semi-stub\n", This, debugstr_variant(&v));
1141 
1142     return set_window_event(This, EVENTID_ERROR, &v);
1143 }
1144 
1145 static HRESULT WINAPI HTMLWindow2_get_onerror(IHTMLWindow2 *iface, VARIANT *p)
1146 {
1147     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1148 
1149     TRACE("(%p)->(%p)\n", This, p);
1150 
1151     return get_window_event(This, EVENTID_ERROR, p);
1152 }
1153 
1154 static HRESULT WINAPI HTMLWindow2_put_onresize(IHTMLWindow2 *iface, VARIANT v)
1155 {
1156     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1157 
1158     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1159 
1160     return set_window_event(This, EVENTID_RESIZE, &v);
1161 }
1162 
1163 static HRESULT WINAPI HTMLWindow2_get_onresize(IHTMLWindow2 *iface, VARIANT *p)
1164 {
1165     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1166 
1167     TRACE("(%p)->(%p)\n", This, p);
1168 
1169     return get_window_event(This, EVENTID_RESIZE, p);
1170 }
1171 
1172 static HRESULT WINAPI HTMLWindow2_put_onscroll(IHTMLWindow2 *iface, VARIANT v)
1173 {
1174     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1175 
1176     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1177 
1178     return set_window_event(This, EVENTID_SCROLL, &v);
1179 }
1180 
1181 static HRESULT WINAPI HTMLWindow2_get_onscroll(IHTMLWindow2 *iface, VARIANT *p)
1182 {
1183     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1184 
1185     TRACE("(%p)->(%p)\n", This, p);
1186 
1187     return get_window_event(This, EVENTID_SCROLL, p);
1188 }
1189 
1190 static HRESULT WINAPI HTMLWindow2_get_document(IHTMLWindow2 *iface, IHTMLDocument2 **p)
1191 {
1192     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1193 
1194     TRACE("(%p)->(%p)\n", This, p);
1195 
1196     if(This->inner_window->doc) {
1197         /* FIXME: We should return a wrapper object here */
1198         *p = &This->inner_window->doc->basedoc.IHTMLDocument2_iface;
1199         IHTMLDocument2_AddRef(*p);
1200     }else {
1201         *p = NULL;
1202     }
1203 
1204     return S_OK;
1205 }
1206 
1207 static HRESULT WINAPI HTMLWindow2_get_event(IHTMLWindow2 *iface, IHTMLEventObj **p)
1208 {
1209     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1210     HTMLInnerWindow *window = This->inner_window;
1211 
1212     TRACE("(%p)->(%p)\n", This, p);
1213 
1214     if(window->event)
1215         IHTMLEventObj_AddRef(window->event);
1216     *p = window->event;
1217     return S_OK;
1218 }
1219 
1220 static HRESULT WINAPI HTMLWindow2_get__newEnum(IHTMLWindow2 *iface, IUnknown **p)
1221 {
1222     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1223     FIXME("(%p)->(%p)\n", This, p);
1224     return E_NOTIMPL;
1225 }
1226 
1227 static HRESULT WINAPI HTMLWindow2_showModalDialog(IHTMLWindow2 *iface, BSTR dialog,
1228         VARIANT *varArgIn, VARIANT *varOptions, VARIANT *varArgOut)
1229 {
1230     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1231     FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(dialog), varArgIn, varOptions, varArgOut);
1232     return E_NOTIMPL;
1233 }
1234 
1235 static HRESULT WINAPI HTMLWindow2_showHelp(IHTMLWindow2 *iface, BSTR helpURL, VARIANT helpArg,
1236         BSTR features)
1237 {
1238     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1239     FIXME("(%p)->(%s %s %s)\n", This, debugstr_w(helpURL), debugstr_variant(&helpArg), debugstr_w(features));
1240     return E_NOTIMPL;
1241 }
1242 
1243 static HRESULT WINAPI HTMLWindow2_get_screen(IHTMLWindow2 *iface, IHTMLScreen **p)
1244 {
1245     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1246     HTMLInnerWindow *window = This->inner_window;
1247 
1248     TRACE("(%p)->(%p)\n", This, p);
1249 
1250     if(!window->screen) {
1251         HRESULT hres;
1252 
1253         hres = HTMLScreen_Create(&window->screen);
1254         if(FAILED(hres))
1255             return hres;
1256     }
1257 
1258     *p = window->screen;
1259     IHTMLScreen_AddRef(window->screen);
1260     return S_OK;
1261 }
1262 
1263 static HRESULT WINAPI HTMLWindow2_get_Option(IHTMLWindow2 *iface, IHTMLOptionElementFactory **p)
1264 {
1265     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1266     HTMLInnerWindow *window = This->inner_window;
1267 
1268     TRACE("(%p)->(%p)\n", This, p);
1269 
1270     if(!window->option_factory) {
1271         HRESULT hres;
1272 
1273         hres = HTMLOptionElementFactory_Create(window, &window->option_factory);
1274         if(FAILED(hres))
1275             return hres;
1276     }
1277 
1278     *p = &window->option_factory->IHTMLOptionElementFactory_iface;
1279     IHTMLOptionElementFactory_AddRef(*p);
1280 
1281     return S_OK;
1282 }
1283 
1284 static HRESULT WINAPI HTMLWindow2_focus(IHTMLWindow2 *iface)
1285 {
1286     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1287 
1288     TRACE("(%p)->()\n", This);
1289 
1290     if(This->outer_window->doc_obj)
1291         SetFocus(This->outer_window->doc_obj->hwnd);
1292     return S_OK;
1293 }
1294 
1295 static HRESULT WINAPI HTMLWindow2_get_closed(IHTMLWindow2 *iface, VARIANT_BOOL *p)
1296 {
1297     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1298     FIXME("(%p)->(%p)\n", This, p);
1299     return E_NOTIMPL;
1300 }
1301 
1302 static HRESULT WINAPI HTMLWindow2_blur(IHTMLWindow2 *iface)
1303 {
1304     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1305     FIXME("(%p)->()\n", This);
1306     return E_NOTIMPL;
1307 }
1308 
1309 static HRESULT WINAPI HTMLWindow2_scroll(IHTMLWindow2 *iface, LONG x, LONG y)
1310 {
1311     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1312     nsresult nsres;
1313 
1314     TRACE("(%p)->(%d %d)\n", This, x, y);
1315 
1316     nsres = nsIDOMWindow_Scroll(This->outer_window->nswindow, x, y);
1317     if(NS_FAILED(nsres)) {
1318         ERR("ScrollBy failed: %08x\n", nsres);
1319         return E_FAIL;
1320     }
1321 
1322     return S_OK;
1323 }
1324 
1325 static HRESULT WINAPI HTMLWindow2_get_clientInformation(IHTMLWindow2 *iface, IOmNavigator **p)
1326 {
1327     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1328     FIXME("(%p)->(%p)\n", This, p);
1329     return E_NOTIMPL;
1330 }
1331 
1332 static HRESULT WINAPI HTMLWindow2_setInterval(IHTMLWindow2 *iface, BSTR expression,
1333         LONG msec, VARIANT *language, LONG *timerID)
1334 {
1335     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1336     VARIANT expr;
1337 
1338     TRACE("(%p)->(%s %d %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
1339 
1340     V_VT(&expr) = VT_BSTR;
1341     V_BSTR(&expr) = expression;
1342     return IHTMLWindow3_setInterval(&This->IHTMLWindow3_iface, &expr, msec, language, timerID);
1343 }
1344 
1345 static HRESULT WINAPI HTMLWindow2_clearInterval(IHTMLWindow2 *iface, LONG timerID)
1346 {
1347     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1348 
1349     TRACE("(%p)->(%d)\n", This, timerID);
1350 
1351     return clear_task_timer(This->inner_window, TRUE, timerID);
1352 }
1353 
1354 static HRESULT WINAPI HTMLWindow2_put_offscreenBuffering(IHTMLWindow2 *iface, VARIANT v)
1355 {
1356     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1357     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1358     return E_NOTIMPL;
1359 }
1360 
1361 static HRESULT WINAPI HTMLWindow2_get_offscreenBuffering(IHTMLWindow2 *iface, VARIANT *p)
1362 {
1363     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1364     FIXME("(%p)->(%p)\n", This, p);
1365     return E_NOTIMPL;
1366 }
1367 
1368 static HRESULT WINAPI HTMLWindow2_execScript(IHTMLWindow2 *iface, BSTR scode, BSTR language,
1369         VARIANT *pvarRet)
1370 {
1371     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1372 
1373     TRACE("(%p)->(%s %s %p)\n", This, debugstr_w(scode), debugstr_w(language), pvarRet);
1374 
1375     return exec_script(This->inner_window, scode, language, pvarRet);
1376 }
1377 
1378 static HRESULT WINAPI HTMLWindow2_toString(IHTMLWindow2 *iface, BSTR *String)
1379 {
1380     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1381 
1382     static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
1383 
1384     TRACE("(%p)->(%p)\n", This, String);
1385 
1386     if(!String)
1387         return E_INVALIDARG;
1388 
1389     *String = SysAllocString(objectW);
1390     return *String ? S_OK : E_OUTOFMEMORY;
1391 }
1392 
1393 static HRESULT WINAPI HTMLWindow2_scrollBy(IHTMLWindow2 *iface, LONG x, LONG y)
1394 {
1395     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1396     nsresult nsres;
1397 
1398     TRACE("(%p)->(%d %d)\n", This, x, y);
1399 
1400     nsres = nsIDOMWindow_ScrollBy(This->outer_window->nswindow, x, y);
1401     if(NS_FAILED(nsres))
1402         ERR("ScrollBy failed: %08x\n", nsres);
1403 
1404     return S_OK;
1405 }
1406 
1407 static HRESULT WINAPI HTMLWindow2_scrollTo(IHTMLWindow2 *iface, LONG x, LONG y)
1408 {
1409     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1410     nsresult nsres;
1411 
1412     TRACE("(%p)->(%d %d)\n", This, x, y);
1413 
1414     nsres = nsIDOMWindow_ScrollTo(This->outer_window->nswindow, x, y);
1415     if(NS_FAILED(nsres))
1416         ERR("ScrollTo failed: %08x\n", nsres);
1417 
1418     return S_OK;
1419 }
1420 
1421 static HRESULT WINAPI HTMLWindow2_moveTo(IHTMLWindow2 *iface, LONG x, LONG y)
1422 {
1423     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1424     FIXME("(%p)->(%d %d)\n", This, x, y);
1425     return E_NOTIMPL;
1426 }
1427 
1428 static HRESULT WINAPI HTMLWindow2_moveBy(IHTMLWindow2 *iface, LONG x, LONG y)
1429 {
1430     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1431     FIXME("(%p)->(%d %d)\n", This, x, y);
1432     return E_NOTIMPL;
1433 }
1434 
1435 static HRESULT WINAPI HTMLWindow2_resizeTo(IHTMLWindow2 *iface, LONG x, LONG y)
1436 {
1437     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1438     FIXME("(%p)->(%d %d)\n", This, x, y);
1439     return E_NOTIMPL;
1440 }
1441 
1442 static HRESULT WINAPI HTMLWindow2_resizeBy(IHTMLWindow2 *iface, LONG x, LONG y)
1443 {
1444     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1445     FIXME("(%p)->(%d %d)\n", This, x, y);
1446     return E_NOTIMPL;
1447 }
1448 
1449 static HRESULT WINAPI HTMLWindow2_get_external(IHTMLWindow2 *iface, IDispatch **p)
1450 {
1451     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1452 
1453     TRACE("(%p)->(%p)\n", This, p);
1454 
1455     *p = NULL;
1456 
1457     if(!This->outer_window->doc_obj->hostui)
1458         return S_OK;
1459 
1460     return IDocHostUIHandler_GetExternal(This->outer_window->doc_obj->hostui, p);
1461 }
1462 
1463 static const IHTMLWindow2Vtbl HTMLWindow2Vtbl = {
1464     HTMLWindow2_QueryInterface,
1465     HTMLWindow2_AddRef,
1466     HTMLWindow2_Release,
1467     HTMLWindow2_GetTypeInfoCount,
1468     HTMLWindow2_GetTypeInfo,
1469     HTMLWindow2_GetIDsOfNames,
1470     HTMLWindow2_Invoke,
1471     HTMLWindow2_item,
1472     HTMLWindow2_get_length,
1473     HTMLWindow2_get_frames,
1474     HTMLWindow2_put_defaultStatus,
1475     HTMLWindow2_get_defaultStatus,
1476     HTMLWindow2_put_status,
1477     HTMLWindow2_get_status,
1478     HTMLWindow2_setTimeout,
1479     HTMLWindow2_clearTimeout,
1480     HTMLWindow2_alert,
1481     HTMLWindow2_confirm,
1482     HTMLWindow2_prompt,
1483     HTMLWindow2_get_Image,
1484     HTMLWindow2_get_location,
1485     HTMLWindow2_get_history,
1486     HTMLWindow2_close,
1487     HTMLWindow2_put_opener,
1488     HTMLWindow2_get_opener,
1489     HTMLWindow2_get_navigator,
1490     HTMLWindow2_put_name,
1491     HTMLWindow2_get_name,
1492     HTMLWindow2_get_parent,
1493     HTMLWindow2_open,
1494     HTMLWindow2_get_self,
1495     HTMLWindow2_get_top,
1496     HTMLWindow2_get_window,
1497     HTMLWindow2_navigate,
1498     HTMLWindow2_put_onfocus,
1499     HTMLWindow2_get_onfocus,
1500     HTMLWindow2_put_onblur,
1501     HTMLWindow2_get_onblur,
1502     HTMLWindow2_put_onload,
1503     HTMLWindow2_get_onload,
1504     HTMLWindow2_put_onbeforeunload,
1505     HTMLWindow2_get_onbeforeunload,
1506     HTMLWindow2_put_onunload,
1507     HTMLWindow2_get_onunload,
1508     HTMLWindow2_put_onhelp,
1509     HTMLWindow2_get_onhelp,
1510     HTMLWindow2_put_onerror,
1511     HTMLWindow2_get_onerror,
1512     HTMLWindow2_put_onresize,
1513     HTMLWindow2_get_onresize,
1514     HTMLWindow2_put_onscroll,
1515     HTMLWindow2_get_onscroll,
1516     HTMLWindow2_get_document,
1517     HTMLWindow2_get_event,
1518     HTMLWindow2_get__newEnum,
1519     HTMLWindow2_showModalDialog,
1520     HTMLWindow2_showHelp,
1521     HTMLWindow2_get_screen,
1522     HTMLWindow2_get_Option,
1523     HTMLWindow2_focus,
1524     HTMLWindow2_get_closed,
1525     HTMLWindow2_blur,
1526     HTMLWindow2_scroll,
1527     HTMLWindow2_get_clientInformation,
1528     HTMLWindow2_setInterval,
1529     HTMLWindow2_clearInterval,
1530     HTMLWindow2_put_offscreenBuffering,
1531     HTMLWindow2_get_offscreenBuffering,
1532     HTMLWindow2_execScript,
1533     HTMLWindow2_toString,
1534     HTMLWindow2_scrollBy,
1535     HTMLWindow2_scrollTo,
1536     HTMLWindow2_moveTo,
1537     HTMLWindow2_moveBy,
1538     HTMLWindow2_resizeTo,
1539     HTMLWindow2_resizeBy,
1540     HTMLWindow2_get_external
1541 };
1542 
1543 static inline HTMLWindow *impl_from_IHTMLWindow3(IHTMLWindow3 *iface)
1544 {
1545     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow3_iface);
1546 }
1547 
1548 static HRESULT WINAPI HTMLWindow3_QueryInterface(IHTMLWindow3 *iface, REFIID riid, void **ppv)
1549 {
1550     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1551 
1552     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1553 }
1554 
1555 static ULONG WINAPI HTMLWindow3_AddRef(IHTMLWindow3 *iface)
1556 {
1557     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1558 
1559     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1560 }
1561 
1562 static ULONG WINAPI HTMLWindow3_Release(IHTMLWindow3 *iface)
1563 {
1564     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1565 
1566     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1567 }
1568 
1569 static HRESULT WINAPI HTMLWindow3_GetTypeInfoCount(IHTMLWindow3 *iface, UINT *pctinfo)
1570 {
1571     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1572 
1573     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
1574 }
1575 
1576 static HRESULT WINAPI HTMLWindow3_GetTypeInfo(IHTMLWindow3 *iface, UINT iTInfo,
1577                                               LCID lcid, ITypeInfo **ppTInfo)
1578 {
1579     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1580 
1581     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1582 }
1583 
1584 static HRESULT WINAPI HTMLWindow3_GetIDsOfNames(IHTMLWindow3 *iface, REFIID riid,
1585                                                 LPOLESTR *rgszNames, UINT cNames,
1586                                                 LCID lcid, DISPID *rgDispId)
1587 {
1588     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1589 
1590     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
1591             rgDispId);
1592 }
1593 
1594 static HRESULT WINAPI HTMLWindow3_Invoke(IHTMLWindow3 *iface, DISPID dispIdMember,
1595                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1596                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1597 {
1598     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1599 
1600     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
1601             pDispParams, pVarResult, pExcepInfo, puArgErr);
1602 }
1603 
1604 static HRESULT WINAPI HTMLWindow3_get_screenLeft(IHTMLWindow3 *iface, LONG *p)
1605 {
1606     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1607     nsresult nsres;
1608 
1609     TRACE("(%p)->(%p)\n", This, p);
1610 
1611     nsres = nsIDOMWindow_GetScreenX(This->outer_window->nswindow, p);
1612     if(NS_FAILED(nsres)) {
1613         ERR("GetScreenX failed: %08x\n", nsres);
1614         return E_FAIL;
1615     }
1616 
1617     return S_OK;
1618 }
1619 
1620 static HRESULT WINAPI HTMLWindow3_get_screenTop(IHTMLWindow3 *iface, LONG *p)
1621 {
1622     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1623     nsresult nsres;
1624 
1625     TRACE("(%p)->(%p)\n", This, p);
1626 
1627     nsres = nsIDOMWindow_GetScreenY(This->outer_window->nswindow, p);
1628     if(NS_FAILED(nsres)) {
1629         ERR("GetScreenY failed: %08x\n", nsres);
1630         return E_FAIL;
1631     }
1632 
1633     return S_OK;
1634 }
1635 
1636 static HRESULT WINAPI HTMLWindow3_attachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp, VARIANT_BOOL *pfResult)
1637 {
1638     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1639     HTMLInnerWindow *window = This->inner_window;
1640 
1641     TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(event), pDisp, pfResult);
1642 
1643     if(!window->doc) {
1644         FIXME("No document\n");
1645         return E_FAIL;
1646     }
1647 
1648     return attach_event(&window->event_target, event, pDisp, pfResult);
1649 }
1650 
1651 static HRESULT WINAPI HTMLWindow3_detachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp)
1652 {
1653     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1654     HTMLInnerWindow *window = This->inner_window;
1655 
1656     TRACE("(%p)->()\n", This);
1657 
1658     if(!window->doc) {
1659         FIXME("No document\n");
1660         return E_FAIL;
1661     }
1662 
1663     return detach_event(&window->event_target, event, pDisp);
1664 }
1665 
1666 static HRESULT window_set_timer(HTMLInnerWindow *This, VARIANT *expr, LONG msec, VARIANT *language,
1667         BOOL interval, LONG *timer_id)
1668 {
1669     IDispatch *disp = NULL;
1670     HRESULT hres;
1671 
1672     switch(V_VT(expr)) {
1673     case VT_DISPATCH:
1674         disp = V_DISPATCH(expr);
1675         IDispatch_AddRef(disp);
1676         break;
1677 
1678     case VT_BSTR:
1679         disp = script_parse_event(This->base.inner_window, V_BSTR(expr));
1680         break;
1681 
1682     default:
1683         FIXME("unimplemented expr %s\n", debugstr_variant(expr));
1684         return E_NOTIMPL;
1685     }
1686 
1687     if(!disp)
1688         return E_FAIL;
1689 
1690     hres = set_task_timer(This, msec, interval, disp, timer_id);
1691     IDispatch_Release(disp);
1692 
1693     return hres;
1694 }
1695 
1696 static HRESULT WINAPI HTMLWindow3_setTimeout(IHTMLWindow3 *iface, VARIANT *expression, LONG msec,
1697         VARIANT *language, LONG *timerID)
1698 {
1699     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1700 
1701     TRACE("(%p)->(%s %d %s %p)\n", This, debugstr_variant(expression), msec, debugstr_variant(language), timerID);
1702 
1703     return window_set_timer(This->inner_window, expression, msec, language, FALSE, timerID);
1704 }
1705 
1706 static HRESULT WINAPI HTMLWindow3_setInterval(IHTMLWindow3 *iface, VARIANT *expression, LONG msec,
1707         VARIANT *language, LONG *timerID)
1708 {
1709     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1710 
1711     TRACE("(%p)->(%p %d %p %p)\n", This, expression, msec, language, timerID);
1712 
1713     return window_set_timer(This->inner_window, expression, msec, language, TRUE, timerID);
1714 }
1715 
1716 static HRESULT WINAPI HTMLWindow3_print(IHTMLWindow3 *iface)
1717 {
1718     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1719     FIXME("(%p)\n", This);
1720     return E_NOTIMPL;
1721 }
1722 
1723 static HRESULT WINAPI HTMLWindow3_put_onbeforeprint(IHTMLWindow3 *iface, VARIANT v)
1724 {
1725     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1726     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1727     return E_NOTIMPL;
1728 }
1729 
1730 static HRESULT WINAPI HTMLWindow3_get_onbeforeprint(IHTMLWindow3 *iface, VARIANT *p)
1731 {
1732     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1733     FIXME("(%p)->(%p)\n", This, p);
1734     return E_NOTIMPL;
1735 }
1736 
1737 static HRESULT WINAPI HTMLWindow3_put_onafterprint(IHTMLWindow3 *iface, VARIANT v)
1738 {
1739     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1740     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1741     return E_NOTIMPL;
1742 }
1743 
1744 static HRESULT WINAPI HTMLWindow3_get_onafterprint(IHTMLWindow3 *iface, VARIANT *p)
1745 {
1746     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1747     FIXME("(%p)->(%p)\n", This, p);
1748     return E_NOTIMPL;
1749 }
1750 
1751 static HRESULT WINAPI HTMLWindow3_get_clipboardData(IHTMLWindow3 *iface, IHTMLDataTransfer **p)
1752 {
1753     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1754     FIXME("(%p)->(%p)\n", This, p);
1755     return E_NOTIMPL;
1756 }
1757 
1758 static HRESULT WINAPI HTMLWindow3_showModelessDialog(IHTMLWindow3 *iface, BSTR url,
1759         VARIANT *varArgIn, VARIANT *options, IHTMLWindow2 **pDialog)
1760 {
1761     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1762     FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(url), varArgIn, options, pDialog);
1763     return E_NOTIMPL;
1764 }
1765 
1766 static const IHTMLWindow3Vtbl HTMLWindow3Vtbl = {
1767     HTMLWindow3_QueryInterface,
1768     HTMLWindow3_AddRef,
1769     HTMLWindow3_Release,
1770     HTMLWindow3_GetTypeInfoCount,
1771     HTMLWindow3_GetTypeInfo,
1772     HTMLWindow3_GetIDsOfNames,
1773     HTMLWindow3_Invoke,
1774     HTMLWindow3_get_screenLeft,
1775     HTMLWindow3_get_screenTop,
1776     HTMLWindow3_attachEvent,
1777     HTMLWindow3_detachEvent,
1778     HTMLWindow3_setTimeout,
1779     HTMLWindow3_setInterval,
1780     HTMLWindow3_print,
1781     HTMLWindow3_put_onbeforeprint,
1782     HTMLWindow3_get_onbeforeprint,
1783     HTMLWindow3_put_onafterprint,
1784     HTMLWindow3_get_onafterprint,
1785     HTMLWindow3_get_clipboardData,
1786     HTMLWindow3_showModelessDialog
1787 };
1788 
1789 static inline HTMLWindow *impl_from_IHTMLWindow4(IHTMLWindow4 *iface)
1790 {
1791     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow4_iface);
1792 }
1793 
1794 static HRESULT WINAPI HTMLWindow4_QueryInterface(IHTMLWindow4 *iface, REFIID riid, void **ppv)
1795 {
1796     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1797 
1798     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1799 }
1800 
1801 static ULONG WINAPI HTMLWindow4_AddRef(IHTMLWindow4 *iface)
1802 {
1803     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1804 
1805     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1806 }
1807 
1808 static ULONG WINAPI HTMLWindow4_Release(IHTMLWindow4 *iface)
1809 {
1810     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1811 
1812     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1813 }
1814 
1815 static HRESULT WINAPI HTMLWindow4_GetTypeInfoCount(IHTMLWindow4 *iface, UINT *pctinfo)
1816 {
1817     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1818 
1819     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
1820 }
1821 
1822 static HRESULT WINAPI HTMLWindow4_GetTypeInfo(IHTMLWindow4 *iface, UINT iTInfo,
1823                                               LCID lcid, ITypeInfo **ppTInfo)
1824 {
1825     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1826 
1827     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1828 }
1829 
1830 static HRESULT WINAPI HTMLWindow4_GetIDsOfNames(IHTMLWindow4 *iface, REFIID riid,
1831                                                 LPOLESTR *rgszNames, UINT cNames,
1832                                                 LCID lcid, DISPID *rgDispId)
1833 {
1834     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1835 
1836     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
1837             rgDispId);
1838 }
1839 
1840 static HRESULT WINAPI HTMLWindow4_Invoke(IHTMLWindow4 *iface, DISPID dispIdMember,
1841                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1842                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1843 {
1844     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1845 
1846     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
1847             pDispParams, pVarResult, pExcepInfo, puArgErr);
1848 }
1849 
1850 static HRESULT WINAPI HTMLWindow4_createPopup(IHTMLWindow4 *iface, VARIANT *varArgIn,
1851                             IDispatch **ppPopup)
1852 {
1853     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1854     FIXME("(%p)->(%p %p)\n", This, varArgIn, ppPopup);
1855     return E_NOTIMPL;
1856 }
1857 
1858 static HRESULT WINAPI HTMLWindow4_get_frameElement(IHTMLWindow4 *iface, IHTMLFrameBase **p)
1859 {
1860     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1861     TRACE("(%p)->(%p)\n", This, p);
1862 
1863     if(This->outer_window->frame_element) {
1864         *p = &This->outer_window->frame_element->IHTMLFrameBase_iface;
1865         IHTMLFrameBase_AddRef(*p);
1866     }else
1867         *p = NULL;
1868 
1869     return S_OK;
1870 }
1871 
1872 static const IHTMLWindow4Vtbl HTMLWindow4Vtbl = {
1873     HTMLWindow4_QueryInterface,
1874     HTMLWindow4_AddRef,
1875     HTMLWindow4_Release,
1876     HTMLWindow4_GetTypeInfoCount,
1877     HTMLWindow4_GetTypeInfo,
1878     HTMLWindow4_GetIDsOfNames,
1879     HTMLWindow4_Invoke,
1880     HTMLWindow4_createPopup,
1881     HTMLWindow4_get_frameElement
1882 };
1883 
1884 static inline HTMLWindow *impl_from_IHTMLWindow5(IHTMLWindow5 *iface)
1885 {
1886     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow5_iface);
1887 }
1888 
1889 static HRESULT WINAPI HTMLWindow5_QueryInterface(IHTMLWindow5 *iface, REFIID riid, void **ppv)
1890 {
1891     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1892 
1893     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1894 }
1895 
1896 static ULONG WINAPI HTMLWindow5_AddRef(IHTMLWindow5 *iface)
1897 {
1898     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1899 
1900     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1901 }
1902 
1903 static ULONG WINAPI HTMLWindow5_Release(IHTMLWindow5 *iface)
1904 {
1905     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1906 
1907     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1908 }
1909 
1910 static HRESULT WINAPI HTMLWindow5_GetTypeInfoCount(IHTMLWindow5 *iface, UINT *pctinfo)
1911 {
1912     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1913 
1914     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
1915 }
1916 
1917 static HRESULT WINAPI HTMLWindow5_GetTypeInfo(IHTMLWindow5 *iface, UINT iTInfo,
1918         LCID lcid, ITypeInfo **ppTInfo)
1919 {
1920     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1921 
1922     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1923 }
1924 
1925 static HRESULT WINAPI HTMLWindow5_GetIDsOfNames(IHTMLWindow5 *iface, REFIID riid,
1926         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1927 {
1928     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1929 
1930     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
1931             rgDispId);
1932 }
1933 
1934 static HRESULT WINAPI HTMLWindow5_Invoke(IHTMLWindow5 *iface, DISPID dispIdMember,
1935         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1936         VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1937 {
1938     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1939 
1940     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
1941             pDispParams, pVarResult, pExcepInfo, puArgErr);
1942 }
1943 
1944 static HRESULT WINAPI HTMLWindow5_put_XMLHttpRequest(IHTMLWindow5 *iface, VARIANT v)
1945 {
1946     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1947     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1948     return E_NOTIMPL;
1949 }
1950 
1951 static HRESULT WINAPI HTMLWindow5_get_XMLHttpRequest(IHTMLWindow5 *iface, VARIANT *p)
1952 {
1953     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1954     HTMLInnerWindow *window = This->inner_window;
1955 
1956     TRACE("(%p)->(%p)\n", This, p);
1957 
1958     if(!window->xhr_factory) {
1959         HRESULT hres;
1960 
1961         hres = HTMLXMLHttpRequestFactory_Create(window, &window->xhr_factory);
1962         if(FAILED(hres)) {
1963             return hres;
1964         }
1965     }
1966 
1967     V_VT(p) = VT_DISPATCH;
1968     V_DISPATCH(p) = (IDispatch*)&window->xhr_factory->IHTMLXMLHttpRequestFactory_iface;
1969     IDispatch_AddRef(V_DISPATCH(p));
1970 
1971     return S_OK;
1972 }
1973 
1974 static const IHTMLWindow5Vtbl HTMLWindow5Vtbl = {
1975     HTMLWindow5_QueryInterface,
1976     HTMLWindow5_AddRef,
1977     HTMLWindow5_Release,
1978     HTMLWindow5_GetTypeInfoCount,
1979     HTMLWindow5_GetTypeInfo,
1980     HTMLWindow5_GetIDsOfNames,
1981     HTMLWindow5_Invoke,
1982     HTMLWindow5_put_XMLHttpRequest,
1983     HTMLWindow5_get_XMLHttpRequest
1984 };
1985 
1986 static inline HTMLWindow *impl_from_IHTMLWindow6(IHTMLWindow6 *iface)
1987 {
1988     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow6_iface);
1989 }
1990 
1991 static HRESULT WINAPI HTMLWindow6_QueryInterface(IHTMLWindow6 *iface, REFIID riid, void **ppv)
1992 {
1993     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1994 
1995     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1996 }
1997 
1998 static ULONG WINAPI HTMLWindow6_AddRef(IHTMLWindow6 *iface)
1999 {
2000     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2001 
2002     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
2003 }
2004 
2005 static ULONG WINAPI HTMLWindow6_Release(IHTMLWindow6 *iface)
2006 {
2007     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2008 
2009     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
2010 }
2011 
2012 static HRESULT WINAPI HTMLWindow6_GetTypeInfoCount(IHTMLWindow6 *iface, UINT *pctinfo)
2013 {
2014     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2015 
2016     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
2017 }
2018 
2019 static HRESULT WINAPI HTMLWindow6_GetTypeInfo(IHTMLWindow6 *iface, UINT iTInfo,
2020                                               LCID lcid, ITypeInfo **ppTInfo)
2021 {
2022     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2023 
2024     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
2025 }
2026 
2027 static HRESULT WINAPI HTMLWindow6_GetIDsOfNames(IHTMLWindow6 *iface, REFIID riid,
2028                                                 LPOLESTR *rgszNames, UINT cNames,
2029                                                 LCID lcid, DISPID *rgDispId)
2030 {
2031     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2032 
2033     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
2034             rgDispId);
2035 }
2036 
2037 static HRESULT WINAPI HTMLWindow6_Invoke(IHTMLWindow6 *iface, DISPID dispIdMember,
2038                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
2039                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
2040 {
2041     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2042 
2043     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
2044             pDispParams, pVarResult, pExcepInfo, puArgErr);
2045 }
2046 
2047 static HRESULT WINAPI HTMLWindow6_put_XDomainRequest(IHTMLWindow6 *iface, VARIANT v)
2048 {
2049     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2050     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
2051     return E_NOTIMPL;
2052 }
2053 
2054 static HRESULT WINAPI HTMLWindow6_get_XDomainRequest(IHTMLWindow6 *iface, VARIANT *p)
2055 {
2056     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2057     FIXME("(%p)->(%p)\n", This, p);
2058     return E_NOTIMPL;
2059 }
2060 
2061 static HRESULT WINAPI HTMLWindow6_get_sessionStorage(IHTMLWindow6 *iface, IHTMLStorage **p)
2062 {
2063     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2064 
2065     FIXME("(%p)->(%p)\n", This, p);
2066 
2067     if(!This->inner_window->session_storage) {
2068         HRESULT hres;
2069 
2070         hres = create_storage(&This->inner_window->session_storage);
2071         if(FAILED(hres))
2072             return hres;
2073     }
2074 
2075     IHTMLStorage_AddRef(This->inner_window->session_storage);
2076     *p = This->inner_window->session_storage;
2077     return S_OK;
2078 }
2079 
2080 static HRESULT WINAPI HTMLWindow6_get_localStorage(IHTMLWindow6 *iface, IHTMLStorage **p)
2081 {
2082     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2083     FIXME("(%p)->(%p)\n", This, p);
2084     return E_NOTIMPL;
2085 }
2086 
2087 static HRESULT WINAPI HTMLWindow6_put_onhashchange(IHTMLWindow6 *iface, VARIANT v)
2088 {
2089     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2090     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
2091     return E_NOTIMPL;
2092 }
2093 
2094 static HRESULT WINAPI HTMLWindow6_get_onhashchange(IHTMLWindow6 *iface, VARIANT *p)
2095 {
2096     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2097     FIXME("(%p)->(%p)\n", This, p);
2098     return E_NOTIMPL;
2099 }
2100 
2101 static HRESULT WINAPI HTMLWindow6_get_maxConnectionsPerServer(IHTMLWindow6 *iface, LONG *p)
2102 {
2103     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2104     FIXME("(%p)->(%p)\n", This, p);
2105     return E_NOTIMPL;
2106 }
2107 
2108 static HRESULT WINAPI HTMLWindow6_postMessage(IHTMLWindow6 *iface, BSTR msg, VARIANT targetOrigin)
2109 {
2110     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2111     FIXME("(%p)->(%s %s)\n", This, debugstr_w(msg), debugstr_variant(&targetOrigin));
2112     return E_NOTIMPL;
2113 }
2114 
2115 static HRESULT WINAPI HTMLWindow6_toStaticHTML(IHTMLWindow6 *iface, BSTR bstrHTML, BSTR *pbstrStaticHTML)
2116 {
2117     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2118     FIXME("(%p)->(%s %p)\n", This, debugstr_w(bstrHTML), pbstrStaticHTML);
2119     return E_NOTIMPL;
2120 }
2121 
2122 static HRESULT WINAPI HTMLWindow6_put_onmessage(IHTMLWindow6 *iface, VARIANT v)
2123 {
2124     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2125     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
2126     return E_NOTIMPL;
2127 }
2128 
2129 static HRESULT WINAPI HTMLWindow6_get_onmessage(IHTMLWindow6 *iface, VARIANT *p)
2130 {
2131     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2132     FIXME("(%p)->(%p)\n", This, p);
2133     return E_NOTIMPL;
2134 }
2135 
2136 static HRESULT WINAPI HTMLWindow6_msWriteProfilerMark(IHTMLWindow6 *iface, BSTR bstrProfilerMark)
2137 {
2138     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
2139     FIXME("(%p)->(%s)\n", This, debugstr_w(bstrProfilerMark));
2140     return E_NOTIMPL;
2141 }
2142 
2143 static const IHTMLWindow6Vtbl HTMLWindow6Vtbl = {
2144     HTMLWindow6_QueryInterface,
2145     HTMLWindow6_AddRef,
2146     HTMLWindow6_Release,
2147     HTMLWindow6_GetTypeInfoCount,
2148     HTMLWindow6_GetTypeInfo,
2149     HTMLWindow6_GetIDsOfNames,
2150     HTMLWindow6_Invoke,
2151     HTMLWindow6_put_XDomainRequest,
2152     HTMLWindow6_get_XDomainRequest,
2153     HTMLWindow6_get_sessionStorage,
2154     HTMLWindow6_get_localStorage,
2155     HTMLWindow6_put_onhashchange,
2156     HTMLWindow6_get_onhashchange,
2157     HTMLWindow6_get_maxConnectionsPerServer,
2158     HTMLWindow6_postMessage,
2159     HTMLWindow6_toStaticHTML,
2160     HTMLWindow6_put_onmessage,
2161     HTMLWindow6_get_onmessage,
2162     HTMLWindow6_msWriteProfilerMark
2163 };
2164 
2165 static inline HTMLWindow *impl_from_IHTMLPrivateWindow(IHTMLPrivateWindow *iface)
2166 {
2167     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLPrivateWindow_iface);
2168 }
2169 
2170 static HRESULT WINAPI HTMLPrivateWindow_QueryInterface(IHTMLPrivateWindow *iface, REFIID riid, void **ppv)
2171 {
2172     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2173 
2174     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2175 }
2176 
2177 static ULONG WINAPI HTMLPrivateWindow_AddRef(IHTMLPrivateWindow *iface)
2178 {
2179     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2180 
2181     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
2182 }
2183 
2184 static ULONG WINAPI HTMLPrivateWindow_Release(IHTMLPrivateWindow *iface)
2185 {
2186     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2187 
2188     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
2189 }
2190 
2191 static HRESULT WINAPI HTMLPrivateWindow_SuperNavigate(IHTMLPrivateWindow *iface, BSTR url, BSTR arg2, BSTR arg3,
2192         BSTR arg4, VARIANT *post_data_var, VARIANT *headers_var, ULONG flags)
2193 {
2194     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2195     HTMLOuterWindow *window = This->outer_window;
2196     OLECHAR *translated_url = NULL;
2197     DWORD post_data_size = 0;
2198     BYTE *post_data = NULL;
2199     WCHAR *headers = NULL;
2200     IUri *uri;
2201     HRESULT hres;
2202 
2203     TRACE("(%p)->(%s %s %s %s %s %s %x)\n", This, debugstr_w(url), debugstr_w(arg2), debugstr_w(arg3), debugstr_w(arg4),
2204           debugstr_variant(post_data_var), debugstr_variant(headers_var), flags);
2205 
2206     if(window->doc_obj->hostui) {
2207         hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url, &translated_url);
2208         if(hres != S_OK)
2209             translated_url = NULL;
2210     }
2211 
2212     hres = create_uri(translated_url ? translated_url : url, 0, &uri);
2213     CoTaskMemFree(translated_url);
2214     if(FAILED(hres))
2215         return hres;
2216 
2217     if(post_data_var) {
2218         if(V_VT(post_data_var) == (VT_ARRAY|VT_UI1)) {
2219             SafeArrayAccessData(V_ARRAY(post_data_var), (void**)&post_data);
2220             post_data_size = V_ARRAY(post_data_var)->rgsabound[0].cElements;
2221         }
2222     }
2223 
2224     if(headers_var && V_VT(headers_var) != VT_EMPTY && V_VT(headers_var) != VT_ERROR) {
2225         if(V_VT(headers_var) != VT_BSTR)
2226             return E_INVALIDARG;
2227 
2228         headers = V_BSTR(headers_var);
2229     }
2230 
2231     hres = super_navigate(window, uri, BINDING_NAVIGATED|BINDING_NOFRAG, headers, post_data, post_data_size);
2232     IUri_Release(uri);
2233     if(post_data)
2234         SafeArrayUnaccessData(V_ARRAY(post_data_var));
2235 
2236     return hres;
2237 }
2238 
2239 static HRESULT WINAPI HTMLPrivateWindow_GetPendingUrl(IHTMLPrivateWindow *iface, BSTR *url)
2240 {
2241     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2242     FIXME("(%p)->(%p)\n", This, url);
2243     return E_NOTIMPL;
2244 }
2245 
2246 static HRESULT WINAPI HTMLPrivateWindow_SetPICSTarget(IHTMLPrivateWindow *iface, IOleCommandTarget *cmdtrg)
2247 {
2248     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2249     FIXME("(%p)->(%p)\n", This, cmdtrg);
2250     return E_NOTIMPL;
2251 }
2252 
2253 static HRESULT WINAPI HTMLPrivateWindow_PICSComplete(IHTMLPrivateWindow *iface, int arg)
2254 {
2255     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2256     FIXME("(%p)->(%x)\n", This, arg);
2257     return E_NOTIMPL;
2258 }
2259 
2260 static HRESULT WINAPI HTMLPrivateWindow_FindWindowByName(IHTMLPrivateWindow *iface, LPCWSTR name, IHTMLWindow2 **ret)
2261 {
2262     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2263     FIXME("(%p)->(%s %p)\n", This, debugstr_w(name), ret);
2264     return E_NOTIMPL;
2265 }
2266 
2267 static HRESULT WINAPI HTMLPrivateWindow_GetAddressBarUrl(IHTMLPrivateWindow *iface, BSTR *url)
2268 {
2269     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2270     TRACE("(%p)->(%p)\n", This, url);
2271 
2272     if(!url)
2273         return E_INVALIDARG;
2274 
2275     *url = SysAllocString(This->outer_window->url);
2276     return S_OK;
2277 }
2278 
2279 static const IHTMLPrivateWindowVtbl HTMLPrivateWindowVtbl = {
2280     HTMLPrivateWindow_QueryInterface,
2281     HTMLPrivateWindow_AddRef,
2282     HTMLPrivateWindow_Release,
2283     HTMLPrivateWindow_SuperNavigate,
2284     HTMLPrivateWindow_GetPendingUrl,
2285     HTMLPrivateWindow_SetPICSTarget,
2286     HTMLPrivateWindow_PICSComplete,
2287     HTMLPrivateWindow_FindWindowByName,
2288     HTMLPrivateWindow_GetAddressBarUrl
2289 };
2290 
2291 static inline HTMLWindow *impl_from_ITravelLogClient(ITravelLogClient *iface)
2292 {
2293     return CONTAINING_RECORD(iface, HTMLWindow, ITravelLogClient_iface);
2294 }
2295 
2296 static HRESULT WINAPI TravelLogClient_QueryInterface(ITravelLogClient *iface, REFIID riid, void **ppv)
2297 {
2298     HTMLWindow *This = impl_from_ITravelLogClient(iface);
2299 
2300     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2301 }
2302 
2303 static ULONG WINAPI TravelLogClient_AddRef(ITravelLogClient *iface)
2304 {
2305     HTMLWindow *This = impl_from_ITravelLogClient(iface);
2306 
2307     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
2308 }
2309 
2310 static ULONG WINAPI TravelLogClient_Release(ITravelLogClient *iface)
2311 {
2312     HTMLWindow *This = impl_from_ITravelLogClient(iface);
2313 
2314     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
2315 }
2316 
2317 static HRESULT WINAPI TravelLogClient_FindWindowByIndex(ITravelLogClient *iface, DWORD dwID, IUnknown **ppunk)
2318 {
2319     HTMLWindow *This = impl_from_ITravelLogClient(iface);
2320 
2321     FIXME("(%p)->(%d %p) semi-stub\n", This, dwID, ppunk);
2322 
2323     *ppunk = NULL;
2324     return E_FAIL;
2325 }
2326 
2327 static HRESULT WINAPI TravelLogClient_GetWindowData(ITravelLogClient *iface, IStream *pStream, LPWINDOWDATA pWinData)
2328 {
2329     HTMLWindow *This = impl_from_ITravelLogClient(iface);
2330     FIXME("(%p)->(%p %p)\n", This, pStream, pWinData);
2331     return E_NOTIMPL;
2332 }
2333 
2334 static HRESULT WINAPI TravelLogClient_LoadHistoryPosition(ITravelLogClient *iface, LPWSTR pszUrlLocation, DWORD dwPosition)
2335 {
2336     HTMLWindow *This = impl_from_ITravelLogClient(iface);
2337     FIXME("(%p)->(%s %d)\n", This, debugstr_w(pszUrlLocation), dwPosition);
2338     return E_NOTIMPL;
2339 }
2340 
2341 static const ITravelLogClientVtbl TravelLogClientVtbl = {
2342     TravelLogClient_QueryInterface,
2343     TravelLogClient_AddRef,
2344     TravelLogClient_Release,
2345     TravelLogClient_FindWindowByIndex,
2346     TravelLogClient_GetWindowData,
2347     TravelLogClient_LoadHistoryPosition
2348 };
2349 
2350 static inline HTMLWindow *impl_from_IObjectIdentity(IObjectIdentity *iface)
2351 {
2352     return CONTAINING_RECORD(iface, HTMLWindow, IObjectIdentity_iface);
2353 }
2354 
2355 static HRESULT WINAPI ObjectIdentity_QueryInterface(IObjectIdentity *iface, REFIID riid, void **ppv)
2356 {
2357     HTMLWindow *This = impl_from_IObjectIdentity(iface);
2358 
2359     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2360 }
2361 
2362 static ULONG WINAPI ObjectIdentity_AddRef(IObjectIdentity *iface)
2363 {
2364     HTMLWindow *This = impl_from_IObjectIdentity(iface);
2365 
2366     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
2367 }
2368 
2369 static ULONG WINAPI ObjectIdentity_Release(IObjectIdentity *iface)
2370 {
2371     HTMLWindow *This = impl_from_IObjectIdentity(iface);
2372 
2373     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
2374 }
2375 
2376 static HRESULT WINAPI ObjectIdentity_IsEqualObject(IObjectIdentity *iface, IUnknown *unk)
2377 {
2378     HTMLWindow *This = impl_from_IObjectIdentity(iface);
2379     IServiceProvider *sp;
2380     HRESULT hres;
2381 
2382     TRACE("(%p)->(%p)\n", This, unk);
2383 
2384     hres = IUnknown_QueryInterface(unk, &IID_IServiceProvider, (void**)&sp);
2385     if(hres != S_OK)
2386         return hres;
2387 
2388     hres = &This->inner_window->base.IServiceProvider_iface==sp ||
2389         &This->outer_window->base.IServiceProvider_iface==sp ? S_OK : S_FALSE;
2390     IServiceProvider_Release(sp);
2391     return hres;
2392 }
2393 
2394 static const IObjectIdentityVtbl ObjectIdentityVtbl = {
2395     ObjectIdentity_QueryInterface,
2396     ObjectIdentity_AddRef,
2397     ObjectIdentity_Release,
2398     ObjectIdentity_IsEqualObject
2399 };
2400 
2401 static inline HTMLWindow *impl_from_IDispatchEx(IDispatchEx *iface)
2402 {
2403     return CONTAINING_RECORD(iface, HTMLWindow, IDispatchEx_iface);
2404 }
2405 
2406 static HRESULT WINAPI WindowDispEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
2407 {
2408     HTMLWindow *This = impl_from_IDispatchEx(iface);
2409 
2410     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2411 }
2412 
2413 static ULONG WINAPI WindowDispEx_AddRef(IDispatchEx *iface)
2414 {
2415     HTMLWindow *This = impl_from_IDispatchEx(iface);
2416 
2417     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
2418 }
2419 
2420 static ULONG WINAPI WindowDispEx_Release(IDispatchEx *iface)
2421 {
2422     HTMLWindow *This = impl_from_IDispatchEx(iface);
2423 
2424     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
2425 }
2426 
2427 static HRESULT WINAPI WindowDispEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
2428 {
2429     HTMLWindow *This = impl_from_IDispatchEx(iface);
2430 
2431     TRACE("(%p)->(%p)\n", This, pctinfo);
2432 
2433     return IDispatchEx_GetTypeInfoCount(&This->inner_window->event_target.dispex.IDispatchEx_iface, pctinfo);
2434 }
2435 
2436 static HRESULT WINAPI WindowDispEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
2437                                                LCID lcid, ITypeInfo **ppTInfo)
2438 {
2439     HTMLWindow *This = impl_from_IDispatchEx(iface);
2440 
2441     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
2442 
2443     return IDispatchEx_GetTypeInfo(&This->inner_window->event_target.dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
2444 }
2445 
2446 static HRESULT WINAPI WindowDispEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
2447                                                  LPOLESTR *rgszNames, UINT cNames,
2448                                                  LCID lcid, DISPID *rgDispId)
2449 {
2450     HTMLWindow *This = impl_from_IDispatchEx(iface);
2451     UINT i;
2452     HRESULT hres;
2453 
2454     WARN("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
2455 
2456     for(i=0; i < cNames; i++) {
2457         /* We shouldn't use script's IDispatchEx here, so we shouldn't use GetDispID */
2458         hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
2459         if(FAILED(hres))
2460             return hres;
2461     }
2462 
2463     return S_OK;
2464 }
2465 
2466 static HRESULT WINAPI WindowDispEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
2467                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
2468                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
2469 {
2470     HTMLWindow *This = impl_from_IDispatchEx(iface);
2471 
2472     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
2473           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
2474 
2475     /* FIXME: Use script dispatch */
2476 
2477     return IDispatchEx_Invoke(&This->inner_window->event_target.dispex.IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
2478             pDispParams, pVarResult, pExcepInfo, puArgErr);
2479 }
2480 
2481 static global_prop_t *alloc_global_prop(HTMLInnerWindow *This, global_prop_type_t type, BSTR name)
2482 {
2483     if(This->global_prop_cnt == This->global_prop_size) {
2484         global_prop_t *new_props;
2485         DWORD new_size;
2486 
2487         if(This->global_props) {
2488             new_size = This->global_prop_size*2;
2489             new_props = heap_realloc(This->global_props, new_size*sizeof(global_prop_t));
2490         }else {
2491             new_size = 16;
2492             new_props = heap_alloc(new_size*sizeof(global_prop_t));
2493         }
2494         if(!new_props)
2495             return NULL;
2496         This->global_props = new_props;
2497         This->global_prop_size = new_size;
2498     }
2499 
2500     This->global_props[This->global_prop_cnt].name = heap_strdupW(name);
2501     if(!This->global_props[This->global_prop_cnt].name)
2502         return NULL;
2503 
2504     This->global_props[This->global_prop_cnt].type = type;
2505     return This->global_props + This->global_prop_cnt++;
2506 }
2507 
2508 static inline DWORD prop_to_dispid(HTMLInnerWindow *This, global_prop_t *prop)
2509 {
2510     return MSHTML_DISPID_CUSTOM_MIN + (prop-This->global_props);
2511 }
2512 
2513 HRESULT search_window_props(HTMLInnerWindow *This, BSTR bstrName, DWORD grfdex, DISPID *pid)
2514 {
2515     DWORD i;
2516     ScriptHost *script_host;
2517     DISPID id;
2518 
2519     for(i=0; i < This->global_prop_cnt; i++) {
2520         /* FIXME: case sensitivity */
2521         if(!strcmpW(This->global_props[i].name, bstrName)) {
2522             *pid = MSHTML_DISPID_CUSTOM_MIN+i;
2523             return S_OK;
2524         }
2525     }
2526 
2527     if(find_global_prop(This->base.inner_window, bstrName, grfdex, &script_host, &id)) {
2528         global_prop_t *prop;
2529 
2530         prop = alloc_global_prop(This, GLOBAL_SCRIPTVAR, bstrName);
2531         if(!prop)
2532             return E_OUTOFMEMORY;
2533 
2534         prop->script_host = script_host;
2535         prop->id = id;
2536 
2537         *pid = prop_to_dispid(This, prop);
2538         return S_OK;
2539     }
2540 
2541     return DISP_E_UNKNOWNNAME;
2542 }
2543 
2544 static HRESULT WINAPI WindowDispEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
2545 {
2546     HTMLWindow *This = impl_from_IDispatchEx(iface);
2547     HTMLInnerWindow *window = This->inner_window;
2548     HRESULT hres;
2549 
2550     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
2551 
2552     hres = search_window_props(window, bstrName, grfdex, pid);
2553     if(hres != DISP_E_UNKNOWNNAME)
2554         return hres;
2555 
2556     hres = IDispatchEx_GetDispID(&window->base.inner_window->event_target.dispex.IDispatchEx_iface, bstrName, grfdex, pid);
2557     if(hres != DISP_E_UNKNOWNNAME)
2558         return hres;
2559 
2560     if(This->outer_window) {
2561         HTMLOuterWindow *frame;
2562 
2563         hres = get_frame_by_name(This->outer_window, bstrName, FALSE, &frame);
2564         if(SUCCEEDED(hres) && frame) {
2565             global_prop_t *prop;
2566 
2567             IHTMLWindow2_Release(&frame->base.IHTMLWindow2_iface);
2568 
2569             prop = alloc_global_prop(window, GLOBAL_FRAMEVAR, bstrName);
2570             if(!prop)
2571                 return E_OUTOFMEMORY;
2572 
2573             *pid = prop_to_dispid(window, prop);
2574             return S_OK;
2575         }
2576     }
2577 
2578     if(window->doc) {
2579         global_prop_t *prop;
2580         IHTMLElement *elem;
2581 
2582         hres = IHTMLDocument3_getElementById(&window->base.inner_window->doc->basedoc.IHTMLDocument3_iface,
2583                                              bstrName, &elem);
2584         if(SUCCEEDED(hres) && elem) {
2585             IHTMLElement_Release(elem);
2586 
2587             prop = alloc_global_prop(window, GLOBAL_ELEMENTVAR, bstrName);
2588             if(!prop)
2589                 return E_OUTOFMEMORY;
2590 
2591             *pid = prop_to_dispid(window, prop);
2592             return S_OK;
2593         }
2594     }
2595 
2596     return DISP_E_UNKNOWNNAME;
2597 }
2598 
2599 static HRESULT WINAPI WindowDispEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
2600         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
2601 {
2602     HTMLWindow *This = impl_from_IDispatchEx(iface);
2603     HTMLInnerWindow *window = This->inner_window;
2604 
2605     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
2606 
2607     if(id == DISPID_IHTMLWINDOW2_LOCATION && (wFlags & DISPATCH_PROPERTYPUT)) {
2608         HTMLLocation *location;
2609         HRESULT hres;
2610 
2611         TRACE("forwarding to location.href\n");
2612 
2613         hres = get_location(window, &location);
2614         if(FAILED(hres))
2615             return hres;
2616 
2617         hres = IDispatchEx_InvokeEx(&location->dispex.IDispatchEx_iface, DISPID_VALUE, lcid,
2618                 wFlags, pdp, pvarRes, pei, pspCaller);
2619         IHTMLLocation_Release(&location->IHTMLLocation_iface);
2620         return hres;
2621     }
2622 
2623     return IDispatchEx_InvokeEx(&window->event_target.dispex.IDispatchEx_iface, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
2624 }
2625 
2626 static HRESULT WINAPI WindowDispEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
2627 {
2628     HTMLWindow *This = impl_from_IDispatchEx(iface);
2629 
2630     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
2631 
2632     return IDispatchEx_DeleteMemberByName(&This->inner_window->event_target.dispex.IDispatchEx_iface, bstrName, grfdex);
2633 }
2634 
2635 static HRESULT WINAPI WindowDispEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
2636 {
2637     HTMLWindow *This = impl_from_IDispatchEx(iface);
2638 
2639     TRACE("(%p)->(%x)\n", This, id);
2640 
2641     return IDispatchEx_DeleteMemberByDispID(&This->inner_window->event_target.dispex.IDispatchEx_iface, id);
2642 }
2643 
2644 static HRESULT WINAPI WindowDispEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
2645 {
2646     HTMLWindow *This = impl_from_IDispatchEx(iface);
2647 
2648     TRACE("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
2649 
2650     return IDispatchEx_GetMemberProperties(&This->inner_window->event_target.dispex.IDispatchEx_iface, id, grfdexFetch,
2651             pgrfdex);
2652 }
2653 
2654 static HRESULT WINAPI WindowDispEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
2655 {
2656     HTMLWindow *This = impl_from_IDispatchEx(iface);
2657 
2658     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
2659 
2660     return IDispatchEx_GetMemberName(&This->inner_window->event_target.dispex.IDispatchEx_iface, id, pbstrName);
2661 }
2662 
2663 static HRESULT WINAPI WindowDispEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
2664 {
2665     HTMLWindow *This = impl_from_IDispatchEx(iface);
2666 
2667     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
2668 
2669     return IDispatchEx_GetNextDispID(&This->inner_window->event_target.dispex.IDispatchEx_iface, grfdex, id, pid);
2670 }
2671 
2672 static HRESULT WINAPI WindowDispEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
2673 {
2674     HTMLWindow *This = impl_from_IDispatchEx(iface);
2675 
2676     TRACE("(%p)->(%p)\n", This, ppunk);
2677 
2678     *ppunk = NULL;
2679     return S_OK;
2680 }
2681 
2682 static const IDispatchExVtbl WindowDispExVtbl = {
2683     WindowDispEx_QueryInterface,
2684     WindowDispEx_AddRef,
2685     WindowDispEx_Release,
2686     WindowDispEx_GetTypeInfoCount,
2687     WindowDispEx_GetTypeInfo,
2688     WindowDispEx_GetIDsOfNames,
2689     WindowDispEx_Invoke,
2690     WindowDispEx_GetDispID,
2691     WindowDispEx_InvokeEx,
2692     WindowDispEx_DeleteMemberByName,
2693     WindowDispEx_DeleteMemberByDispID,
2694     WindowDispEx_GetMemberProperties,
2695     WindowDispEx_GetMemberName,
2696     WindowDispEx_GetNextDispID,
2697     WindowDispEx_GetNameSpaceParent
2698 };
2699 
2700 static inline HTMLWindow *impl_from_IServiceProvider(IServiceProvider *iface)
2701 {
2702     return CONTAINING_RECORD(iface, HTMLWindow, IServiceProvider_iface);
2703 }
2704 
2705 static HRESULT WINAPI HTMLWindowSP_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
2706 {
2707     HTMLWindow *This = impl_from_IServiceProvider(iface);
2708     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2709 }
2710 
2711 static ULONG WINAPI HTMLWindowSP_AddRef(IServiceProvider *iface)
2712 {
2713     HTMLWindow *This = impl_from_IServiceProvider(iface);
2714     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
2715 }
2716 
2717 static ULONG WINAPI HTMLWindowSP_Release(IServiceProvider *iface)
2718 {
2719     HTMLWindow *This = impl_from_IServiceProvider(iface);
2720     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
2721 }
2722 
2723 static HRESULT WINAPI HTMLWindowSP_QueryService(IServiceProvider *iface, REFGUID guidService, REFIID riid, void **ppv)
2724 {
2725     HTMLWindow *This = impl_from_IServiceProvider(iface);
2726 
2727     if(IsEqualGUID(guidService, &IID_IHTMLWindow2)) {
2728         TRACE("IID_IHTMLWindow2\n");
2729         return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2730     }
2731 
2732     TRACE("(%p)->(%s %s %p)\n", This, debugstr_mshtml_guid(guidService), debugstr_mshtml_guid(riid), ppv);
2733 
2734     if(!This->outer_window->doc_obj)
2735         return E_NOINTERFACE;
2736 
2737     return IServiceProvider_QueryService(&This->outer_window->doc_obj->basedoc.IServiceProvider_iface,
2738             guidService, riid, ppv);
2739 }
2740 
2741 static const IServiceProviderVtbl ServiceProviderVtbl = {
2742     HTMLWindowSP_QueryInterface,
2743     HTMLWindowSP_AddRef,
2744     HTMLWindowSP_Release,
2745     HTMLWindowSP_QueryService
2746 };
2747 
2748 static inline HTMLInnerWindow *impl_from_DispatchEx(DispatchEx *iface)
2749 {
2750     return CONTAINING_RECORD(iface, HTMLInnerWindow, event_target.dispex);
2751 }
2752 
2753 static HRESULT HTMLWindow_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
2754         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
2755 {
2756     HTMLInnerWindow *This = impl_from_DispatchEx(dispex);
2757     global_prop_t *prop;
2758     DWORD idx;
2759     HRESULT hres;
2760 
2761     idx = id - MSHTML_DISPID_CUSTOM_MIN;
2762     if(idx >= This->global_prop_cnt)
2763         return DISP_E_MEMBERNOTFOUND;
2764 
2765     prop = This->global_props+idx;
2766 
2767     switch(prop->type) {
2768     case GLOBAL_SCRIPTVAR: {
2769         IDispatchEx *iface;
2770         IDispatch *disp;
2771 
2772         disp = get_script_disp(prop->script_host);
2773         if(!disp)
2774             return E_UNEXPECTED;
2775 
2776         hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&iface);
2777         if(SUCCEEDED(hres)) {
2778             TRACE("%s >>>\n", debugstr_w(prop->name));
2779             hres = IDispatchEx_InvokeEx(iface, prop->id, lcid, flags, params, res, ei, caller);
2780             if(hres == S_OK)
2781                 TRACE("%s <<<\n", debugstr_w(prop->name));
2782             else
2783                 WARN("%s <<< %08x\n", debugstr_w(prop->name), hres);
2784             IDispatchEx_Release(iface);
2785         }else {
2786             FIXME("No IDispatchEx\n");
2787         }
2788         IDispatch_Release(disp);
2789         break;
2790     }
2791     case GLOBAL_ELEMENTVAR:
2792         switch(flags) {
2793         case DISPATCH_PROPERTYGET|DISPATCH_METHOD:
2794         case DISPATCH_PROPERTYGET: {
2795             IHTMLElement *elem;
2796 
2797             hres = IHTMLDocument3_getElementById(&This->base.inner_window->doc->basedoc.IHTMLDocument3_iface,
2798                     prop->name, &elem);
2799             if(FAILED(hres))
2800                 return hres;
2801 
2802             if(!elem)
2803                 return DISP_E_MEMBERNOTFOUND;
2804 
2805             V_VT(res) = VT_DISPATCH;
2806             V_DISPATCH(res) = (IDispatch*)elem;
2807             return S_OK;
2808         }
2809         case DISPATCH_PROPERTYPUT: {
2810             DISPID dispex_id;
2811 
2812             hres = dispex_get_dynid(&This->event_target.dispex, prop->name, &dispex_id);
2813             if(FAILED(hres))
2814                 return hres;
2815 
2816             prop->type = GLOBAL_DISPEXVAR;
2817             prop->id = dispex_id;
2818             return IDispatchEx_InvokeEx(&This->event_target.dispex.IDispatchEx_iface, dispex_id, 0, flags, params, res, ei, caller);
2819         }
2820         default:
2821             FIXME("Not supported flags: %x\n", flags);
2822             return E_NOTIMPL;
2823         }
2824     case GLOBAL_FRAMEVAR:
2825         if(!This->base.outer_window)
2826             return E_UNEXPECTED;
2827 
2828         switch(flags) {
2829         case DISPATCH_PROPERTYGET: {
2830             HTMLOuterWindow *frame;
2831 
2832             hres = get_frame_by_name(This->base.outer_window, prop->name, FALSE, &frame);
2833             if(FAILED(hres))
2834                 return hres;
2835 
2836             if(!frame)
2837                 return DISP_E_MEMBERNOTFOUND;
2838 
2839             V_VT(res) = VT_DISPATCH;
2840             V_DISPATCH(res) = (IDispatch*)&frame->base.inner_window->base.IHTMLWindow2_iface;
2841             IDispatch_AddRef(V_DISPATCH(res));
2842             return S_OK;
2843         }
2844         default:
2845             FIXME("Not supported flags: %x\n", flags);
2846             return E_NOTIMPL;
2847         }
2848     case GLOBAL_DISPEXVAR:
2849         return IDispatchEx_InvokeEx(&This->event_target.dispex.IDispatchEx_iface, prop->id, 0, flags, params, res, ei, caller);
2850     default:
2851         ERR("invalid type %d\n", prop->type);
2852         hres = DISP_E_MEMBERNOTFOUND;
2853     }
2854 
2855     return hres;
2856 }
2857 
2858 static event_target_t **HTMLWindow_get_event_target_ptr(DispatchEx *dispex)
2859 {
2860     HTMLInnerWindow *This = impl_from_DispatchEx(dispex);
2861     return &This->doc->body_event_target;
2862 }
2863 
2864 static void HTMLWindow_bind_event(DispatchEx *dispex, int eid)
2865 {
2866     HTMLInnerWindow *This = impl_from_DispatchEx(dispex);
2867     This->doc->node.event_target.dispex.data->vtbl->bind_event(&This->doc->node.event_target.dispex, eid);
2868 }
2869 
2870 static const dispex_static_data_vtbl_t HTMLWindow_dispex_vtbl = {
2871     NULL,
2872     NULL,
2873     HTMLWindow_invoke,
2874     NULL,
2875     HTMLWindow_get_event_target_ptr,
2876     HTMLWindow_bind_event
2877 };
2878 
2879 static const tid_t HTMLWindow_iface_tids[] = {
2880     IHTMLWindow2_tid,
2881     IHTMLWindow3_tid,
2882     IHTMLWindow4_tid,
2883     IHTMLWindow6_tid,
2884     0
2885 };
2886 
2887 static dispex_static_data_t HTMLWindow_dispex = {
2888     &HTMLWindow_dispex_vtbl,
2889     DispHTMLWindow2_tid,
2890     NULL,
2891     HTMLWindow_iface_tids
2892 };
2893 
2894 static void *alloc_window(size_t size)
2895 {
2896     HTMLWindow *window;
2897 
2898     window = heap_alloc_zero(size);
2899     if(!window)
2900         return NULL;
2901 
2902     window->IHTMLWindow2_iface.lpVtbl = &HTMLWindow2Vtbl;
2903     window->IHTMLWindow3_iface.lpVtbl = &HTMLWindow3Vtbl;
2904     window->IHTMLWindow4_iface.lpVtbl = &HTMLWindow4Vtbl;
2905     window->IHTMLWindow5_iface.lpVtbl = &HTMLWindow5Vtbl;
2906     window->IHTMLWindow6_iface.lpVtbl = &HTMLWindow6Vtbl;
2907     window->IHTMLPrivateWindow_iface.lpVtbl = &HTMLPrivateWindowVtbl;
2908     window->IDispatchEx_iface.lpVtbl = &WindowDispExVtbl;
2909     window->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
2910     window->ITravelLogClient_iface.lpVtbl = &TravelLogClientVtbl;
2911     window->IObjectIdentity_iface.lpVtbl = &ObjectIdentityVtbl;
2912     window->ref = 1;
2913 
2914     return window;
2915 }
2916 
2917 static HRESULT create_inner_window(HTMLOuterWindow *outer_window, IMoniker *mon, HTMLInnerWindow **ret)
2918 {
2919     HTMLInnerWindow *window;
2920 
2921     window = alloc_window(sizeof(HTMLInnerWindow));
2922     if(!window)
2923         return E_OUTOFMEMORY;
2924 
2925     list_init(&window->script_hosts);
2926     list_init(&window->bindings);
2927     list_init(&window->script_queue);
2928 
2929     window->base.outer_window = outer_window;
2930     window->base.inner_window = window;
2931 
2932     init_dispex(&window->event_target.dispex, (IUnknown*)&window->base.IHTMLWindow2_iface, &HTMLWindow_dispex);
2933 
2934     window->task_magic = get_task_target_magic();
2935 
2936     if(mon) {
2937         IMoniker_AddRef(mon);
2938         window->mon = mon;
2939     }
2940 
2941     *ret = window;
2942     return S_OK;
2943 }
2944 
2945 HRESULT HTMLOuterWindow_Create(HTMLDocumentObj *doc_obj, nsIDOMWindow *nswindow,
2946         HTMLOuterWindow *parent, HTMLOuterWindow **ret)
2947 {
2948     HTMLOuterWindow *window;
2949     HRESULT hres;
2950 
2951     window = alloc_window(sizeof(HTMLOuterWindow));
2952     if(!window)
2953         return E_OUTOFMEMORY;
2954 
2955     window->base.outer_window = window;
2956     window->base.inner_window = NULL;
2957 
2958     window->window_ref = heap_alloc(sizeof(windowref_t));
2959     if(!window->window_ref) {
2960         heap_free(window);
2961         return E_OUTOFMEMORY;
2962     }
2963 
2964     window->doc_obj = doc_obj;
2965 
2966     window->window_ref->window = window;
2967     window->window_ref->ref = 1;
2968 
2969     if(nswindow) {
2970         nsIDOMWindow_AddRef(nswindow);
2971         window->nswindow = nswindow;
2972     }
2973 
2974     window->scriptmode = parent ? parent->scriptmode : SCRIPTMODE_GECKO;
2975     window->readystate = READYSTATE_UNINITIALIZED;
2976 
2977     hres = create_pending_window(window, NULL);
2978     if(SUCCEEDED(hres))
2979         hres = update_window_doc(window->pending_window);
2980     if(FAILED(hres)) {
2981         IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
2982         return hres;
2983     }
2984 
2985     hres = CoInternetCreateSecurityManager(NULL, &window->secmgr, 0);
2986     if(FAILED(hres)) {
2987         IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
2988         return hres;
2989     }
2990 
2991     window->task_magic = get_task_target_magic();
2992 
2993     list_init(&window->children);
2994     list_add_head(&window_list, &window->entry);
2995 
2996     if(parent) {
2997         IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
2998 
2999         window->parent = parent;
3000         list_add_tail(&parent->children, &window->sibling_entry);
3001     }
3002 
3003     TRACE("%p inner_window %p\n", window, window->base.inner_window);
3004 
3005     *ret = window;
3006     return S_OK;
3007 }
3008 
3009 HRESULT create_pending_window(HTMLOuterWindow *outer_window, nsChannelBSC *channelbsc)
3010 {
3011     HTMLInnerWindow *pending_window;
3012     HRESULT hres;
3013 
3014     hres = create_inner_window(outer_window, outer_window->mon /* FIXME */, &pending_window);
3015     if(FAILED(hres))
3016         return hres;
3017 
3018     if(channelbsc) {
3019         IBindStatusCallback_AddRef(&channelbsc->bsc.IBindStatusCallback_iface);
3020         pending_window->bscallback = channelbsc;
3021     }
3022 
3023     if(outer_window->pending_window) {
3024         abort_window_bindings(outer_window->pending_window);
3025         outer_window->pending_window->base.outer_window = NULL;
3026         IHTMLWindow2_Release(&outer_window->pending_window->base.IHTMLWindow2_iface);
3027     }
3028 
3029     outer_window->pending_window = pending_window;
3030     return S_OK;
3031 }
3032 
3033 HRESULT update_window_doc(HTMLInnerWindow *window)
3034 {
3035     HTMLOuterWindow *outer_window = window->base.outer_window;
3036     nsIDOMHTMLDocument *nshtmldoc;
3037     nsIDOMDocument *nsdoc;
3038     nsresult nsres;
3039     HRESULT hres;
3040 
3041     assert(!window->doc);
3042 
3043     if(!outer_window) {
3044         ERR("NULL outer window\n");
3045         return E_UNEXPECTED;
3046     }
3047 
3048     nsres = nsIDOMWindow_GetDocument(outer_window->nswindow, &nsdoc);
3049     if(NS_FAILED(nsres) || !nsdoc) {
3050         ERR("GetDocument failed: %08x\n", nsres);
3051         return E_FAIL;
3052     }
3053 
3054     nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
3055     nsIDOMDocument_Release(nsdoc);
3056     if(NS_FAILED(nsres)) {
3057         ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
3058         return E_FAIL;
3059     }
3060 
3061     hres = create_doc_from_nsdoc(nshtmldoc, outer_window->doc_obj, window, &window->doc);
3062     nsIDOMHTMLDocument_Release(nshtmldoc);
3063     if(FAILED(hres))
3064         return hres;
3065 
3066     if(outer_window->doc_obj->usermode == EDITMODE) {
3067         nsAString mode_str;
3068         nsresult nsres;
3069 
3070         static const PRUnichar onW[] = {'o','n',0};
3071 
3072         nsAString_InitDepend(&mode_str, onW);
3073         nsres = nsIDOMHTMLDocument_SetDesignMode(window->doc->nsdoc, &mode_str);
3074         nsAString_Finish(&mode_str);
3075         if(NS_FAILED(nsres))
3076             ERR("SetDesignMode failed: %08x\n", nsres);
3077     }
3078 
3079     if(window != outer_window->pending_window) {
3080         ERR("not current pending window\n");
3081         return S_OK;
3082     }
3083 
3084     if(outer_window->base.inner_window)
3085         detach_inner_window(outer_window->base.inner_window);
3086     outer_window->base.inner_window = window;
3087     outer_window->pending_window = NULL;
3088 
3089     if(outer_window->doc_obj->basedoc.window == outer_window || !outer_window->doc_obj->basedoc.window) {
3090         if(outer_window->doc_obj->basedoc.doc_node)
3091             htmldoc_release(&outer_window->doc_obj->basedoc.doc_node->basedoc);
3092         outer_window->doc_obj->basedoc.doc_node = window->doc;
3093         htmldoc_addref(&window->doc->basedoc);
3094     }
3095 
3096     return hres;
3097 }
3098 
3099 HTMLOuterWindow *nswindow_to_window(const nsIDOMWindow *nswindow)
3100 {
3101     HTMLOuterWindow *iter;
3102 
3103     LIST_FOR_EACH_ENTRY(iter, &window_list, HTMLOuterWindow, entry) {
3104         if(iter->nswindow == nswindow)
3105             return iter;
3106     }
3107 
3108     return NULL;
3109 }
3110