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