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