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