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