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