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