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