mshtml: Fixed nsAString handling in navigate_anchor.
[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("Could 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 typedef struct {
1683     task_t header;
1684     HTMLWindow *window;
1685     IUri *uri;
1686 } navigate_javascript_task_t;
1687
1688 static void navigate_javascript_proc(task_t *_task)
1689 {
1690     navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1691     HTMLWindow *window = task->window;
1692     VARIANT v;
1693     BSTR code;
1694     HRESULT hres;
1695
1696     static const WCHAR jscriptW[] = {'j','s','c','r','i','p','t',0};
1697
1698     task->window->readystate = READYSTATE_COMPLETE;
1699
1700     hres = IUri_GetPath(task->uri, &code);
1701     if(FAILED(hres))
1702         return;
1703
1704     set_download_state(window->doc_obj, 1);
1705
1706     V_VT(&v) = VT_EMPTY;
1707     hres = exec_script(window, code, jscriptW, &v);
1708     SysFreeString(code);
1709     if(SUCCEEDED(hres) && V_VT(&v) != VT_EMPTY) {
1710         FIXME("javascirpt URL returned %s\n", debugstr_variant(&v));
1711         VariantClear(&v);
1712     }
1713
1714     if(window->doc_obj->view_sink)
1715         IAdviseSink_OnViewChange(window->doc_obj->view_sink, DVASPECT_CONTENT, -1);
1716
1717     set_download_state(window->doc_obj, 0);
1718 }
1719
1720 static void navigate_javascript_task_destr(task_t *_task)
1721 {
1722     navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1723
1724     IUri_Release(task->uri);
1725     heap_free(task);
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 }
1745
1746 static void navigate_task_destr(task_t *_task)
1747 {
1748     navigate_task_t *task = (navigate_task_t*)_task;
1749
1750     IUnknown_Release((IUnknown*)task->bscallback);
1751     IMoniker_Release(task->mon);
1752     heap_free(task);
1753 }
1754
1755 static HRESULT WINAPI HTMLPrivateWindow_SuperNavigate(IHTMLPrivateWindow *iface, BSTR url, BSTR arg2, BSTR arg3,
1756         BSTR arg4, VARIANT *post_data_var, VARIANT *headers_var, ULONG flags)
1757 {
1758     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1759     DWORD post_data_size = 0;
1760     BYTE *post_data = NULL;
1761     WCHAR *headers = NULL;
1762     nsChannelBSC *bsc;
1763     IMoniker *mon;
1764     DWORD scheme;
1765     BSTR new_url;
1766     IUri *uri;
1767     HRESULT hres;
1768
1769     TRACE("(%p)->(%s %s %s %s %s %s %x)\n", This, debugstr_w(url), debugstr_w(arg2), debugstr_w(arg3), debugstr_w(arg4),
1770           debugstr_variant(post_data_var), debugstr_variant(headers_var), flags);
1771
1772     new_url = url;
1773     if(This->doc_obj->hostui) {
1774         OLECHAR *translated_url = NULL;
1775
1776         hres = IDocHostUIHandler_TranslateUrl(This->doc_obj->hostui, 0, url, &translated_url);
1777         if(hres == S_OK && translated_url) {
1778             new_url = SysAllocString(translated_url);
1779             CoTaskMemFree(translated_url);
1780         }
1781     }
1782
1783     if(This->doc_obj->client) {
1784         IOleCommandTarget *cmdtrg;
1785
1786         hres = IOleClientSite_QueryInterface(This->doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1787         if(SUCCEEDED(hres)) {
1788             VARIANT in, out;
1789
1790             V_VT(&in) = VT_BSTR;
1791             V_BSTR(&in) = new_url;
1792             V_VT(&out) = VT_BOOL;
1793             V_BOOL(&out) = VARIANT_TRUE;
1794             hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &in, &out);
1795             IOleCommandTarget_Release(cmdtrg);
1796             if(SUCCEEDED(hres))
1797                 VariantClear(&out);
1798         }
1799     }
1800
1801     hres = CreateUri(new_url, 0, 0, &uri);
1802     if(new_url != url)
1803         SysFreeString(new_url);
1804     if(FAILED(hres))
1805         return hres;
1806
1807     hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
1808     if(FAILED(hres))
1809         return hres;
1810
1811     /* FIXME: Why not set_ready_state? */
1812     This->readystate = READYSTATE_UNINITIALIZED;
1813
1814     if(post_data_var) {
1815         if(V_VT(post_data_var) == (VT_ARRAY|VT_UI1)) {
1816             SafeArrayAccessData(V_ARRAY(post_data_var), (void**)&post_data);
1817             post_data_size = V_ARRAY(post_data_var)->rgsabound[0].cElements;
1818         }
1819     }
1820
1821     if(headers_var && V_VT(headers_var) != VT_EMPTY && V_VT(headers_var) != VT_ERROR) {
1822         if(V_VT(headers_var) != VT_BSTR)
1823             return E_INVALIDARG;
1824
1825         headers = V_BSTR(headers_var);
1826     }
1827
1828     hres = create_channelbsc(mon, headers, post_data, post_data_size, &bsc);
1829     if(post_data)
1830         SafeArrayUnaccessData(V_ARRAY(post_data_var));
1831     if(FAILED(hres)) {
1832         IMoniker_Release(mon);
1833         return hres;
1834     }
1835
1836     prepare_for_binding(&This->doc_obj->basedoc, mon, NULL, TRUE);
1837
1838     hres = IUri_GetScheme(uri, &scheme);
1839
1840     if(scheme != URL_SCHEME_JAVASCRIPT) {
1841         navigate_task_t *task;
1842
1843         IUri_Release(uri);
1844
1845         task = heap_alloc(sizeof(*task));
1846         if(!task) {
1847             IUnknown_Release((IUnknown*)bsc);
1848             IMoniker_Release(mon);
1849             return E_OUTOFMEMORY;
1850         }
1851
1852         task->window = This;
1853         task->bscallback = bsc;
1854         task->mon = mon;
1855         push_task(&task->header, navigate_proc, navigate_task_destr, This->task_magic);
1856
1857         /* Silently and repeated when real loading starts? */
1858         This->readystate = READYSTATE_LOADING;
1859     }else {
1860         navigate_javascript_task_t *task;
1861
1862         IUnknown_Release((IUnknown*)bsc);
1863         IMoniker_Release(mon);
1864
1865         task = heap_alloc(sizeof(*task));
1866         if(!task) {
1867             IUri_Release(uri);
1868             return E_OUTOFMEMORY;
1869         }
1870
1871         task->window = This;
1872         task->uri = uri;
1873         push_task(&task->header, navigate_javascript_proc, navigate_javascript_task_destr, This->task_magic);
1874
1875         /* Why silently? */
1876         This->readystate = READYSTATE_COMPLETE;
1877     }
1878
1879     return S_OK;
1880 }
1881
1882 static HRESULT WINAPI HTMLPrivateWindow_GetPendingUrl(IHTMLPrivateWindow *iface, BSTR *url)
1883 {
1884     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1885     FIXME("(%p)->(%p)\n", This, url);
1886     return E_NOTIMPL;
1887 }
1888
1889 static HRESULT WINAPI HTMLPrivateWindow_SetPICSTarget(IHTMLPrivateWindow *iface, IOleCommandTarget *cmdtrg)
1890 {
1891     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1892     FIXME("(%p)->(%p)\n", This, cmdtrg);
1893     return E_NOTIMPL;
1894 }
1895
1896 static HRESULT WINAPI HTMLPrivateWindow_PICSComplete(IHTMLPrivateWindow *iface, int arg)
1897 {
1898     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1899     FIXME("(%p)->(%x)\n", This, arg);
1900     return E_NOTIMPL;
1901 }
1902
1903 static HRESULT WINAPI HTMLPrivateWindow_FindWindowByName(IHTMLPrivateWindow *iface, LPCWSTR name, IHTMLWindow2 **ret)
1904 {
1905     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1906     FIXME("(%p)->(%s %p)\n", This, debugstr_w(name), ret);
1907     return E_NOTIMPL;
1908 }
1909
1910 static HRESULT WINAPI HTMLPrivateWindow_GetAddressBar(IHTMLPrivateWindow *iface, BSTR *url)
1911 {
1912     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1913     TRACE("(%p)->(%p)\n", This, url);
1914
1915     if(!url)
1916         return E_INVALIDARG;
1917
1918     *url = SysAllocString(This->url);
1919     return S_OK;
1920 }
1921
1922 static const IHTMLPrivateWindowVtbl HTMLPrivateWindowVtbl = {
1923     HTMLPrivateWindow_QueryInterface,
1924     HTMLPrivateWindow_AddRef,
1925     HTMLPrivateWindow_Release,
1926     HTMLPrivateWindow_SuperNavigate,
1927     HTMLPrivateWindow_GetPendingUrl,
1928     HTMLPrivateWindow_SetPICSTarget,
1929     HTMLPrivateWindow_PICSComplete,
1930     HTMLPrivateWindow_FindWindowByName,
1931     HTMLPrivateWindow_GetAddressBar
1932 };
1933
1934 static inline HTMLWindow *impl_from_IDispatchEx(IDispatchEx *iface)
1935 {
1936     return CONTAINING_RECORD(iface, HTMLWindow, IDispatchEx_iface);
1937 }
1938
1939 static HRESULT WINAPI WindowDispEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1940 {
1941     HTMLWindow *This = impl_from_IDispatchEx(iface);
1942
1943     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1944 }
1945
1946 static ULONG WINAPI WindowDispEx_AddRef(IDispatchEx *iface)
1947 {
1948     HTMLWindow *This = impl_from_IDispatchEx(iface);
1949
1950     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1951 }
1952
1953 static ULONG WINAPI WindowDispEx_Release(IDispatchEx *iface)
1954 {
1955     HTMLWindow *This = impl_from_IDispatchEx(iface);
1956
1957     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1958 }
1959
1960 static HRESULT WINAPI WindowDispEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1961 {
1962     HTMLWindow *This = impl_from_IDispatchEx(iface);
1963
1964     TRACE("(%p)->(%p)\n", This, pctinfo);
1965
1966     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
1967 }
1968
1969 static HRESULT WINAPI WindowDispEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1970                                                LCID lcid, ITypeInfo **ppTInfo)
1971 {
1972     HTMLWindow *This = impl_from_IDispatchEx(iface);
1973
1974     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1975
1976     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1977 }
1978
1979 static HRESULT WINAPI WindowDispEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1980                                                  LPOLESTR *rgszNames, UINT cNames,
1981                                                  LCID lcid, DISPID *rgDispId)
1982 {
1983     HTMLWindow *This = impl_from_IDispatchEx(iface);
1984     UINT i;
1985     HRESULT hres;
1986
1987     WARN("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1988           lcid, rgDispId);
1989
1990     for(i=0; i < cNames; i++) {
1991         /* We shouldn't use script's IDispatchEx here, so we shouldn't use GetDispID */
1992         hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1993         if(FAILED(hres))
1994             return hres;
1995     }
1996
1997     return S_OK;
1998 }
1999
2000 static HRESULT WINAPI WindowDispEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
2001                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
2002                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
2003 {
2004     HTMLWindow *This = impl_from_IDispatchEx(iface);
2005
2006     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
2007           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
2008
2009     /* FIXME: Use script dispatch */
2010
2011     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
2012             pDispParams, pVarResult, pExcepInfo, puArgErr);
2013 }
2014
2015 static global_prop_t *alloc_global_prop(HTMLWindow *This, global_prop_type_t type, BSTR name)
2016 {
2017     if(This->global_prop_cnt == This->global_prop_size) {
2018         global_prop_t *new_props;
2019         DWORD new_size;
2020
2021         if(This->global_props) {
2022             new_size = This->global_prop_size*2;
2023             new_props = heap_realloc(This->global_props, new_size*sizeof(global_prop_t));
2024         }else {
2025             new_size = 16;
2026             new_props = heap_alloc(new_size*sizeof(global_prop_t));
2027         }
2028         if(!new_props)
2029             return NULL;
2030         This->global_props = new_props;
2031         This->global_prop_size = new_size;
2032     }
2033
2034     This->global_props[This->global_prop_cnt].name = heap_strdupW(name);
2035     if(!This->global_props[This->global_prop_cnt].name)
2036         return NULL;
2037
2038     This->global_props[This->global_prop_cnt].type = type;
2039     return This->global_props + This->global_prop_cnt++;
2040 }
2041
2042 static inline DWORD prop_to_dispid(HTMLWindow *This, global_prop_t *prop)
2043 {
2044     return MSHTML_DISPID_CUSTOM_MIN + (prop-This->global_props);
2045 }
2046
2047 HRESULT search_window_props(HTMLWindow *This, BSTR bstrName, DWORD grfdex, DISPID *pid)
2048 {
2049     DWORD i;
2050     ScriptHost *script_host;
2051     DISPID id;
2052
2053     for(i=0; i < This->global_prop_cnt; i++) {
2054         /* FIXME: case sensitivity */
2055         if(!strcmpW(This->global_props[i].name, bstrName)) {
2056             *pid = MSHTML_DISPID_CUSTOM_MIN+i;
2057             return S_OK;
2058         }
2059     }
2060
2061     if(find_global_prop(This, bstrName, grfdex, &script_host, &id)) {
2062         global_prop_t *prop;
2063
2064         prop = alloc_global_prop(This, GLOBAL_SCRIPTVAR, bstrName);
2065         if(!prop)
2066             return E_OUTOFMEMORY;
2067
2068         prop->script_host = script_host;
2069         prop->id = id;
2070
2071         *pid = prop_to_dispid(This, prop);
2072         return S_OK;
2073     }
2074
2075     return DISP_E_UNKNOWNNAME;
2076 }
2077
2078 static HRESULT WINAPI WindowDispEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
2079 {
2080     HTMLWindow *This = impl_from_IDispatchEx(iface);
2081     HRESULT hres;
2082
2083     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
2084
2085     hres = search_window_props(This, bstrName, grfdex, pid);
2086     if(hres != DISP_E_UNKNOWNNAME)
2087         return hres;
2088
2089     hres = IDispatchEx_GetDispID(&This->dispex.IDispatchEx_iface, bstrName, grfdex, pid);
2090     if(hres != DISP_E_UNKNOWNNAME)
2091         return hres;
2092
2093     if(This->doc) {
2094         global_prop_t *prop;
2095         IHTMLElement *elem;
2096
2097         hres = IHTMLDocument3_getElementById(&This->doc->basedoc.IHTMLDocument3_iface,
2098                                              bstrName, &elem);
2099         if(SUCCEEDED(hres) && elem) {
2100             IHTMLElement_Release(elem);
2101
2102             prop = alloc_global_prop(This, GLOBAL_ELEMENTVAR, bstrName);
2103             if(!prop)
2104                 return E_OUTOFMEMORY;
2105
2106             *pid = prop_to_dispid(This, prop);
2107             return S_OK;
2108         }
2109     }
2110
2111     return DISP_E_UNKNOWNNAME;
2112 }
2113
2114 static HRESULT WINAPI WindowDispEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
2115         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
2116 {
2117     HTMLWindow *This = impl_from_IDispatchEx(iface);
2118
2119     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
2120
2121     if(id == DISPID_IHTMLWINDOW2_LOCATION && (wFlags & DISPATCH_PROPERTYPUT)) {
2122         HTMLLocation *location;
2123         HRESULT hres;
2124
2125         TRACE("forwarding to location.href\n");
2126
2127         hres = get_location(This, &location);
2128         if(FAILED(hres))
2129             return hres;
2130
2131         hres = IDispatchEx_InvokeEx(&location->dispex.IDispatchEx_iface, DISPID_VALUE, lcid,
2132                 wFlags, pdp, pvarRes, pei, pspCaller);
2133         IHTMLLocation_Release(&location->IHTMLLocation_iface);
2134         return hres;
2135     }
2136
2137     return IDispatchEx_InvokeEx(&This->dispex.IDispatchEx_iface, id, lcid, wFlags, pdp, pvarRes,
2138             pei, pspCaller);
2139 }
2140
2141 static HRESULT WINAPI WindowDispEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
2142 {
2143     HTMLWindow *This = impl_from_IDispatchEx(iface);
2144
2145     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
2146
2147     return IDispatchEx_DeleteMemberByName(&This->dispex.IDispatchEx_iface, bstrName, grfdex);
2148 }
2149
2150 static HRESULT WINAPI WindowDispEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
2151 {
2152     HTMLWindow *This = impl_from_IDispatchEx(iface);
2153
2154     TRACE("(%p)->(%x)\n", This, id);
2155
2156     return IDispatchEx_DeleteMemberByDispID(&This->dispex.IDispatchEx_iface, id);
2157 }
2158
2159 static HRESULT WINAPI WindowDispEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
2160 {
2161     HTMLWindow *This = impl_from_IDispatchEx(iface);
2162
2163     TRACE("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
2164
2165     return IDispatchEx_GetMemberProperties(&This->dispex.IDispatchEx_iface, id, grfdexFetch,
2166             pgrfdex);
2167 }
2168
2169 static HRESULT WINAPI WindowDispEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
2170 {
2171     HTMLWindow *This = impl_from_IDispatchEx(iface);
2172
2173     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
2174
2175     return IDispatchEx_GetMemberName(&This->dispex.IDispatchEx_iface, id, pbstrName);
2176 }
2177
2178 static HRESULT WINAPI WindowDispEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
2179 {
2180     HTMLWindow *This = impl_from_IDispatchEx(iface);
2181
2182     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
2183
2184     return IDispatchEx_GetNextDispID(&This->dispex.IDispatchEx_iface, grfdex, id, pid);
2185 }
2186
2187 static HRESULT WINAPI WindowDispEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
2188 {
2189     HTMLWindow *This = impl_from_IDispatchEx(iface);
2190
2191     TRACE("(%p)->(%p)\n", This, ppunk);
2192
2193     *ppunk = NULL;
2194     return S_OK;
2195 }
2196
2197 static const IDispatchExVtbl WindowDispExVtbl = {
2198     WindowDispEx_QueryInterface,
2199     WindowDispEx_AddRef,
2200     WindowDispEx_Release,
2201     WindowDispEx_GetTypeInfoCount,
2202     WindowDispEx_GetTypeInfo,
2203     WindowDispEx_GetIDsOfNames,
2204     WindowDispEx_Invoke,
2205     WindowDispEx_GetDispID,
2206     WindowDispEx_InvokeEx,
2207     WindowDispEx_DeleteMemberByName,
2208     WindowDispEx_DeleteMemberByDispID,
2209     WindowDispEx_GetMemberProperties,
2210     WindowDispEx_GetMemberName,
2211     WindowDispEx_GetNextDispID,
2212     WindowDispEx_GetNameSpaceParent
2213 };
2214
2215 static inline HTMLWindow *impl_from_IServiceProvider(IServiceProvider *iface)
2216 {
2217     return CONTAINING_RECORD(iface, HTMLWindow, IServiceProvider_iface);
2218 }
2219
2220 static HRESULT WINAPI HTMLWindowSP_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
2221 {
2222     HTMLWindow *This = impl_from_IServiceProvider(iface);
2223     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2224 }
2225
2226 static ULONG WINAPI HTMLWindowSP_AddRef(IServiceProvider *iface)
2227 {
2228     HTMLWindow *This = impl_from_IServiceProvider(iface);
2229     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
2230 }
2231
2232 static ULONG WINAPI HTMLWindowSP_Release(IServiceProvider *iface)
2233 {
2234     HTMLWindow *This = impl_from_IServiceProvider(iface);
2235     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
2236 }
2237
2238 static HRESULT WINAPI HTMLWindowSP_QueryService(IServiceProvider *iface, REFGUID guidService, REFIID riid, void **ppv)
2239 {
2240     HTMLWindow *This = impl_from_IServiceProvider(iface);
2241
2242     if(IsEqualGUID(guidService, &IID_IHTMLWindow2)) {
2243         TRACE("IID_IHTMLWindow2\n");
2244         return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2245     }
2246
2247     TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
2248
2249     if(!This->doc_obj)
2250         return E_NOINTERFACE;
2251
2252     return IServiceProvider_QueryService(&This->doc_obj->basedoc.IServiceProvider_iface,
2253             guidService, riid, ppv);
2254 }
2255
2256 static const IServiceProviderVtbl ServiceProviderVtbl = {
2257     HTMLWindowSP_QueryInterface,
2258     HTMLWindowSP_AddRef,
2259     HTMLWindowSP_Release,
2260     HTMLWindowSP_QueryService
2261 };
2262
2263 static inline HTMLWindow *impl_from_DispatchEx(DispatchEx *iface)
2264 {
2265     return CONTAINING_RECORD(iface, HTMLWindow, dispex);
2266 }
2267
2268 static HRESULT HTMLWindow_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
2269         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
2270 {
2271     HTMLWindow *This = impl_from_DispatchEx(dispex);
2272     global_prop_t *prop;
2273     DWORD idx;
2274     HRESULT hres;
2275
2276     idx = id - MSHTML_DISPID_CUSTOM_MIN;
2277     if(idx >= This->global_prop_cnt)
2278         return DISP_E_MEMBERNOTFOUND;
2279
2280     prop = This->global_props+idx;
2281
2282     switch(prop->type) {
2283     case GLOBAL_SCRIPTVAR: {
2284         IDispatchEx *iface;
2285         IDispatch *disp;
2286
2287         disp = get_script_disp(prop->script_host);
2288         if(!disp)
2289             return E_UNEXPECTED;
2290
2291         hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&iface);
2292         if(SUCCEEDED(hres)) {
2293             TRACE("%s >>>\n", debugstr_w(prop->name));
2294             hres = IDispatchEx_InvokeEx(iface, prop->id, lcid, flags, params, res, ei, caller);
2295             if(hres == S_OK)
2296                 TRACE("%s <<<\n", debugstr_w(prop->name));
2297             else
2298                 WARN("%s <<< %08x\n", debugstr_w(prop->name), hres);
2299             IDispatchEx_Release(iface);
2300         }else {
2301             FIXME("No IDispatchEx\n");
2302         }
2303         IDispatch_Release(disp);
2304         break;
2305     }
2306     case GLOBAL_ELEMENTVAR: {
2307         IHTMLElement *elem;
2308
2309         hres = IHTMLDocument3_getElementById(&This->doc->basedoc.IHTMLDocument3_iface,
2310                                              prop->name, &elem);
2311         if(FAILED(hres))
2312             return hres;
2313
2314         if(!elem)
2315             return DISP_E_MEMBERNOTFOUND;
2316
2317         V_VT(res) = VT_DISPATCH;
2318         V_DISPATCH(res) = (IDispatch*)elem;
2319         break;
2320     }
2321     default:
2322         ERR("invalid type %d\n", prop->type);
2323         hres = DISP_E_MEMBERNOTFOUND;
2324     }
2325
2326     return hres;
2327 }
2328
2329
2330 static const dispex_static_data_vtbl_t HTMLWindow_dispex_vtbl = {
2331     NULL,
2332     NULL,
2333     HTMLWindow_invoke
2334 };
2335
2336 static const tid_t HTMLWindow_iface_tids[] = {
2337     IHTMLWindow2_tid,
2338     IHTMLWindow3_tid,
2339     IHTMLWindow4_tid,
2340     0
2341 };
2342
2343 static dispex_static_data_t HTMLWindow_dispex = {
2344     &HTMLWindow_dispex_vtbl,
2345     DispHTMLWindow2_tid,
2346     NULL,
2347     HTMLWindow_iface_tids
2348 };
2349
2350 HRESULT HTMLWindow_Create(HTMLDocumentObj *doc_obj, nsIDOMWindow *nswindow, HTMLWindow *parent, HTMLWindow **ret)
2351 {
2352     HTMLWindow *window;
2353     HRESULT hres;
2354
2355     window = heap_alloc_zero(sizeof(HTMLWindow));
2356     if(!window)
2357         return E_OUTOFMEMORY;
2358
2359     window->window_ref = heap_alloc(sizeof(windowref_t));
2360     if(!window->window_ref) {
2361         heap_free(window);
2362         return E_OUTOFMEMORY;
2363     }
2364
2365     window->IHTMLWindow2_iface.lpVtbl = &HTMLWindow2Vtbl;
2366     window->IHTMLWindow3_iface.lpVtbl = &HTMLWindow3Vtbl;
2367     window->IHTMLWindow4_iface.lpVtbl = &HTMLWindow4Vtbl;
2368     window->IHTMLPrivateWindow_iface.lpVtbl = &HTMLPrivateWindowVtbl;
2369     window->IDispatchEx_iface.lpVtbl = &WindowDispExVtbl;
2370     window->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
2371     window->ref = 1;
2372     window->doc_obj = doc_obj;
2373
2374     window->window_ref->window = window;
2375     window->window_ref->ref = 1;
2376
2377     init_dispex(&window->dispex, (IUnknown*)&window->IHTMLWindow2_iface, &HTMLWindow_dispex);
2378
2379     if(nswindow) {
2380         nsIDOMWindow_AddRef(nswindow);
2381         window->nswindow = nswindow;
2382     }
2383
2384     window->scriptmode = parent ? parent->scriptmode : SCRIPTMODE_GECKO;
2385     window->readystate = READYSTATE_UNINITIALIZED;
2386     list_init(&window->script_hosts);
2387
2388     hres = CoInternetCreateSecurityManager(NULL, &window->secmgr, 0);
2389     if(FAILED(hres)) {
2390         IHTMLWindow2_Release(&window->IHTMLWindow2_iface);
2391         return hres;
2392     }
2393
2394     window->task_magic = get_task_target_magic();
2395     update_window_doc(window);
2396
2397     list_init(&window->children);
2398     list_add_head(&window_list, &window->entry);
2399
2400     if(parent) {
2401         IHTMLWindow2_AddRef(&window->IHTMLWindow2_iface);
2402
2403         window->parent = parent;
2404         list_add_tail(&parent->children, &window->sibling_entry);
2405     }
2406
2407     *ret = window;
2408     return S_OK;
2409 }
2410
2411 void update_window_doc(HTMLWindow *window)
2412 {
2413     nsIDOMHTMLDocument *nshtmldoc;
2414     nsIDOMDocument *nsdoc;
2415     nsresult nsres;
2416
2417     nsres = nsIDOMWindow_GetDocument(window->nswindow, &nsdoc);
2418     if(NS_FAILED(nsres) || !nsdoc) {
2419         ERR("GetDocument failed: %08x\n", nsres);
2420         return;
2421     }
2422
2423     nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
2424     nsIDOMDocument_Release(nsdoc);
2425     if(NS_FAILED(nsres)) {
2426         ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
2427         return;
2428     }
2429
2430     if(!window->doc || window->doc->nsdoc != nshtmldoc) {
2431         HTMLDocumentNode *doc;
2432         HRESULT hres;
2433
2434         hres = create_doc_from_nsdoc(nshtmldoc, window->doc_obj, window, &doc);
2435         if(SUCCEEDED(hres)) {
2436             window_set_docnode(window, doc);
2437             htmldoc_release(&doc->basedoc);
2438         }else {
2439             ERR("create_doc_from_nsdoc failed: %08x\n", hres);
2440         }
2441     }
2442
2443     nsIDOMHTMLDocument_Release(nshtmldoc);
2444 }
2445
2446 HTMLWindow *nswindow_to_window(const nsIDOMWindow *nswindow)
2447 {
2448     HTMLWindow *iter;
2449
2450     LIST_FOR_EACH_ENTRY(iter, &window_list, HTMLWindow, entry) {
2451         if(iter->nswindow == nswindow)
2452             return iter;
2453     }
2454
2455     return NULL;
2456 }