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