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