mshtml: Moved headers parsing to separated function.
[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 && 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 #define MAX_MESSAGE_LEN 2000
515
516 static HRESULT WINAPI HTMLWindow2_alert(IHTMLWindow2 *iface, BSTR message)
517 {
518     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
519     WCHAR title[100], *msg = message;
520     DWORD len;
521
522     TRACE("(%p)->(%s)\n", This, debugstr_w(message));
523
524     if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, title,
525                     sizeof(title)/sizeof(WCHAR))) {
526         WARN("Could not load message box title: %d\n", GetLastError());
527         return S_OK;
528     }
529
530     len = SysStringLen(message);
531     if(len > MAX_MESSAGE_LEN) {
532         msg = heap_alloc((MAX_MESSAGE_LEN+1)*sizeof(WCHAR));
533         if(!msg)
534             return E_OUTOFMEMORY;
535         memcpy(msg, message, MAX_MESSAGE_LEN*sizeof(WCHAR));
536         msg[MAX_MESSAGE_LEN] = 0;
537     }
538
539     MessageBoxW(This->doc_obj->hwnd, msg, title, MB_ICONWARNING);
540     if(msg != message)
541         heap_free(msg);
542     return S_OK;
543 }
544
545 static HRESULT WINAPI HTMLWindow2_confirm(IHTMLWindow2 *iface, BSTR message,
546         VARIANT_BOOL *confirmed)
547 {
548     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
549     WCHAR wszTitle[100];
550
551     TRACE("(%p)->(%s %p)\n", This, debugstr_w(message), confirmed);
552
553     if(!confirmed) return E_INVALIDARG;
554
555     if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, wszTitle,
556                 sizeof(wszTitle)/sizeof(WCHAR))) {
557         WARN("Could not load message box title: %d\n", GetLastError());
558         *confirmed = VARIANT_TRUE;
559         return S_OK;
560     }
561
562     if(MessageBoxW(This->doc_obj->hwnd, message, wszTitle,
563                 MB_OKCANCEL|MB_ICONQUESTION)==IDOK)
564         *confirmed = VARIANT_TRUE;
565     else *confirmed = VARIANT_FALSE;
566
567     return S_OK;
568 }
569
570 typedef struct
571 {
572     BSTR message;
573     BSTR dststr;
574     VARIANT *textdata;
575 }prompt_arg;
576
577 static INT_PTR CALLBACK prompt_dlgproc(HWND hwnd, UINT msg,
578         WPARAM wparam, LPARAM lparam)
579 {
580     switch(msg)
581     {
582         case WM_INITDIALOG:
583         {
584             prompt_arg *arg = (prompt_arg*)lparam;
585             WCHAR wszTitle[100];
586
587             if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, wszTitle,
588                         sizeof(wszTitle)/sizeof(WCHAR))) {
589                 WARN("Could not load message box title: %d\n", GetLastError());
590                 EndDialog(hwnd, wparam);
591                 return FALSE;
592             }
593
594             SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
595             SetWindowTextW(hwnd, wszTitle);
596             SetWindowTextW(GetDlgItem(hwnd, ID_PROMPT_PROMPT), arg->message);
597             SetWindowTextW(GetDlgItem(hwnd, ID_PROMPT_EDIT), arg->dststr);
598             return FALSE;
599         }
600         case WM_COMMAND:
601             switch(wparam)
602             {
603                 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
604                     EndDialog(hwnd, wparam);
605                     return TRUE;
606                 case MAKEWPARAM(IDOK, BN_CLICKED):
607                 {
608                     prompt_arg *arg =
609                         (prompt_arg*)GetWindowLongPtrW(hwnd, DWLP_USER);
610                     HWND hwndPrompt = GetDlgItem(hwnd, ID_PROMPT_EDIT);
611                     INT len = GetWindowTextLengthW(hwndPrompt);
612
613                     if(!arg->textdata)
614                     {
615                         EndDialog(hwnd, wparam);
616                         return TRUE;
617                     }
618
619                     V_VT(arg->textdata) = VT_BSTR;
620                     if(!len && !arg->dststr)
621                         V_BSTR(arg->textdata) = NULL;
622                     else
623                     {
624                         V_BSTR(arg->textdata) = SysAllocStringLen(NULL, len);
625                         GetWindowTextW(hwndPrompt, V_BSTR(arg->textdata), len+1);
626                     }
627                     EndDialog(hwnd, wparam);
628                     return TRUE;
629                 }
630             }
631             return FALSE;
632         case WM_CLOSE:
633             EndDialog(hwnd, IDCANCEL);
634             return TRUE;
635         default:
636             return FALSE;
637     }
638 }
639
640 static HRESULT WINAPI HTMLWindow2_prompt(IHTMLWindow2 *iface, BSTR message,
641         BSTR dststr, VARIANT *textdata)
642 {
643     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
644     prompt_arg arg;
645
646     TRACE("(%p)->(%s %s %p)\n", This, debugstr_w(message), debugstr_w(dststr), textdata);
647
648     if(textdata) V_VT(textdata) = VT_NULL;
649
650     arg.message = message;
651     arg.dststr = dststr;
652     arg.textdata = textdata;
653
654     DialogBoxParamW(hInst, MAKEINTRESOURCEW(ID_PROMPT_DIALOG),
655             This->doc_obj->hwnd, prompt_dlgproc, (LPARAM)&arg);
656     return S_OK;
657 }
658
659 static HRESULT WINAPI HTMLWindow2_get_Image(IHTMLWindow2 *iface, IHTMLImageElementFactory **p)
660 {
661     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
662
663     TRACE("(%p)->(%p)\n", This, p);
664
665     if(!This->image_factory)
666         This->image_factory = HTMLImageElementFactory_Create(This);
667
668     *p = HTMLIMGFACTORY(This->image_factory);
669     IHTMLImageElementFactory_AddRef(*p);
670
671     return S_OK;
672 }
673
674 static HRESULT WINAPI HTMLWindow2_get_location(IHTMLWindow2 *iface, IHTMLLocation **p)
675 {
676     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
677     HTMLLocation *location;
678     HRESULT hres;
679
680     TRACE("(%p)->(%p)\n", This, p);
681
682     hres = get_location(This, &location);
683     if(FAILED(hres))
684         return hres;
685
686     *p = HTMLLOCATION(location);
687     return S_OK;
688 }
689
690 static HRESULT WINAPI HTMLWindow2_get_history(IHTMLWindow2 *iface, IOmHistory **p)
691 {
692     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
693     FIXME("(%p)->(%p)\n", This, p);
694     return E_NOTIMPL;
695 }
696
697 static HRESULT WINAPI HTMLWindow2_close(IHTMLWindow2 *iface)
698 {
699     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
700     FIXME("(%p)->()\n", This);
701     return E_NOTIMPL;
702 }
703
704 static HRESULT WINAPI HTMLWindow2_put_opener(IHTMLWindow2 *iface, VARIANT v)
705 {
706     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
707     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
708     return E_NOTIMPL;
709 }
710
711 static HRESULT WINAPI HTMLWindow2_get_opener(IHTMLWindow2 *iface, VARIANT *p)
712 {
713     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
714     FIXME("(%p)->(%p)\n", This, p);
715     return E_NOTIMPL;
716 }
717
718 static HRESULT WINAPI HTMLWindow2_get_navigator(IHTMLWindow2 *iface, IOmNavigator **p)
719 {
720     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
721
722     TRACE("(%p)->(%p)\n", This, p);
723
724     *p = OmNavigator_Create();
725     return S_OK;
726 }
727
728 static HRESULT WINAPI HTMLWindow2_put_name(IHTMLWindow2 *iface, BSTR v)
729 {
730     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
731     nsAString name_str;
732     nsresult nsres;
733
734     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
735
736     nsAString_InitDepend(&name_str, v);
737     nsres = nsIDOMWindow_SetName(This->nswindow, &name_str);
738     nsAString_Finish(&name_str);
739     if(NS_FAILED(nsres))
740         ERR("SetName failed: %08x\n", nsres);
741
742     return S_OK;
743 }
744
745 static HRESULT WINAPI HTMLWindow2_get_name(IHTMLWindow2 *iface, BSTR *p)
746 {
747     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
748     nsAString name_str;
749     nsresult nsres;
750     HRESULT hres;
751
752     TRACE("(%p)->(%p)\n", This, p);
753
754     nsAString_Init(&name_str, NULL);
755     nsres = nsIDOMWindow_GetName(This->nswindow, &name_str);
756     if(NS_SUCCEEDED(nsres)) {
757         const PRUnichar *name;
758
759         nsAString_GetData(&name_str, &name);
760         if(*name) {
761             *p = SysAllocString(name);
762             hres = *p ? S_OK : E_OUTOFMEMORY;
763         }else {
764             *p = NULL;
765             hres = S_OK;
766         }
767     }else {
768         ERR("GetName failed: %08x\n", nsres);
769         hres = E_FAIL;
770     }
771     nsAString_Finish(&name_str);
772
773     return hres;
774 }
775
776 static HRESULT WINAPI HTMLWindow2_get_parent(IHTMLWindow2 *iface, IHTMLWindow2 **p)
777 {
778     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
779     TRACE("(%p)->(%p)\n", This, p);
780
781     if(This->parent) {
782         *p = HTMLWINDOW2(This->parent);
783         IHTMLWindow2_AddRef(*p);
784     }else
785         *p = NULL;
786
787     return S_OK;
788 }
789
790 static HRESULT WINAPI HTMLWindow2_open(IHTMLWindow2 *iface, BSTR url, BSTR name,
791          BSTR features, VARIANT_BOOL replace, IHTMLWindow2 **pomWindowResult)
792 {
793     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
794     FIXME("(%p)->(%s %s %s %x %p)\n", This, debugstr_w(url), debugstr_w(name),
795           debugstr_w(features), replace, pomWindowResult);
796     return E_NOTIMPL;
797 }
798
799 static HRESULT WINAPI HTMLWindow2_get_self(IHTMLWindow2 *iface, IHTMLWindow2 **p)
800 {
801     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
802
803     TRACE("(%p)->(%p)\n", This, p);
804
805     /* FIXME: We should return kind of proxy window here. */
806     IHTMLWindow2_AddRef(HTMLWINDOW2(This));
807     *p = HTMLWINDOW2(This);
808     return S_OK;
809 }
810
811 static HRESULT WINAPI HTMLWindow2_get_top(IHTMLWindow2 *iface, IHTMLWindow2 **p)
812 {
813     HTMLWindow *This = HTMLWINDOW2_THIS(iface), *curr;
814     TRACE("(%p)->(%p)\n", This, p);
815
816     curr = This;
817     while(curr->parent)
818         curr = curr->parent;
819     *p = HTMLWINDOW2(curr);
820     IHTMLWindow2_AddRef(*p);
821
822     return S_OK;
823 }
824
825 static HRESULT WINAPI HTMLWindow2_get_window(IHTMLWindow2 *iface, IHTMLWindow2 **p)
826 {
827     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
828
829     TRACE("(%p)->(%p)\n", This, p);
830
831     /* FIXME: We should return kind of proxy window here. */
832     IHTMLWindow2_AddRef(HTMLWINDOW2(This));
833     *p = HTMLWINDOW2(This);
834     return S_OK;
835 }
836
837 static HRESULT WINAPI HTMLWindow2_navigate(IHTMLWindow2 *iface, BSTR url)
838 {
839     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
840     FIXME("(%p)->(%s)\n", This, debugstr_w(url));
841     return E_NOTIMPL;
842 }
843
844 static HRESULT WINAPI HTMLWindow2_put_onfocus(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_onfocus(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_onblur(IHTMLWindow2 *iface, VARIANT v)
859 {
860     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
861     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
862     return E_NOTIMPL;
863 }
864
865 static HRESULT WINAPI HTMLWindow2_get_onblur(IHTMLWindow2 *iface, VARIANT *p)
866 {
867     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
868     FIXME("(%p)->(%p)\n", This, p);
869     return E_NOTIMPL;
870 }
871
872 static HRESULT WINAPI HTMLWindow2_put_onload(IHTMLWindow2 *iface, VARIANT v)
873 {
874     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
875
876     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
877
878     return set_window_event(This, EVENTID_LOAD, &v);
879 }
880
881 static HRESULT WINAPI HTMLWindow2_get_onload(IHTMLWindow2 *iface, VARIANT *p)
882 {
883     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
884
885     TRACE("(%p)->(%p)\n", This, p);
886
887     return get_window_event(This, EVENTID_LOAD, p);
888 }
889
890 static HRESULT WINAPI HTMLWindow2_put_onbeforeunload(IHTMLWindow2 *iface, VARIANT v)
891 {
892     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
893
894     TRACE("(%p)->(v(%d))\n", This, V_VT(&v));
895
896     return set_window_event(This, EVENTID_BEFOREUNLOAD, &v);
897 }
898
899 static HRESULT WINAPI HTMLWindow2_get_onbeforeunload(IHTMLWindow2 *iface, VARIANT *p)
900 {
901     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
902
903     TRACE("(%p)->(%p)\n", This, p);
904
905     return get_window_event(This, EVENTID_BEFOREUNLOAD, p);
906 }
907
908 static HRESULT WINAPI HTMLWindow2_put_onunload(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_onunload(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_onhelp(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_onhelp(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_onerror(IHTMLWindow2 *iface, VARIANT v)
937 {
938     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
939     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
940     return E_NOTIMPL;
941 }
942
943 static HRESULT WINAPI HTMLWindow2_get_onerror(IHTMLWindow2 *iface, VARIANT *p)
944 {
945     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
946     FIXME("(%p)->(%p)\n", This, p);
947     return E_NOTIMPL;
948 }
949
950 static HRESULT WINAPI HTMLWindow2_put_onresize(IHTMLWindow2 *iface, VARIANT v)
951 {
952     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
953
954     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
955
956     return set_window_event(This, EVENTID_RESIZE, &v);
957 }
958
959 static HRESULT WINAPI HTMLWindow2_get_onresize(IHTMLWindow2 *iface, VARIANT *p)
960 {
961     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
962
963     TRACE("(%p)->(%p)\n", This, p);
964
965     return get_window_event(This, EVENTID_RESIZE, p);
966 }
967
968 static HRESULT WINAPI HTMLWindow2_put_onscroll(IHTMLWindow2 *iface, VARIANT v)
969 {
970     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
971     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
972     return E_NOTIMPL;
973 }
974
975 static HRESULT WINAPI HTMLWindow2_get_onscroll(IHTMLWindow2 *iface, VARIANT *p)
976 {
977     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
978     FIXME("(%p)->(%p)\n", This, p);
979     return E_NOTIMPL;
980 }
981
982 static HRESULT WINAPI HTMLWindow2_get_document(IHTMLWindow2 *iface, IHTMLDocument2 **p)
983 {
984     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
985
986     TRACE("(%p)->(%p)\n", This, p);
987
988     if(This->doc) {
989         /* FIXME: We should return a wrapper object here */
990         *p = HTMLDOC(&This->doc->basedoc);
991         IHTMLDocument2_AddRef(*p);
992     }else {
993         *p = NULL;
994     }
995
996     return S_OK;
997 }
998
999 static HRESULT WINAPI HTMLWindow2_get_event(IHTMLWindow2 *iface, IHTMLEventObj **p)
1000 {
1001     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1002
1003     TRACE("(%p)->(%p)\n", This, p);
1004
1005     if(This->event)
1006         IHTMLEventObj_AddRef(This->event);
1007     *p = This->event;
1008     return S_OK;
1009 }
1010
1011 static HRESULT WINAPI HTMLWindow2_get__newEnum(IHTMLWindow2 *iface, IUnknown **p)
1012 {
1013     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1014     FIXME("(%p)->(%p)\n", This, p);
1015     return E_NOTIMPL;
1016 }
1017
1018 static HRESULT WINAPI HTMLWindow2_showModalDialog(IHTMLWindow2 *iface, BSTR dialog,
1019         VARIANT *varArgIn, VARIANT *varOptions, VARIANT *varArgOut)
1020 {
1021     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1022     FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(dialog), varArgIn, varOptions, varArgOut);
1023     return E_NOTIMPL;
1024 }
1025
1026 static HRESULT WINAPI HTMLWindow2_showHelp(IHTMLWindow2 *iface, BSTR helpURL, VARIANT helpArg,
1027         BSTR features)
1028 {
1029     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1030     FIXME("(%p)->(%s v(%d) %s)\n", This, debugstr_w(helpURL), V_VT(&helpArg), debugstr_w(features));
1031     return E_NOTIMPL;
1032 }
1033
1034 static HRESULT WINAPI HTMLWindow2_get_screen(IHTMLWindow2 *iface, IHTMLScreen **p)
1035 {
1036     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1037
1038     TRACE("(%p)->(%p)\n", This, p);
1039
1040     if(!This->screen) {
1041         HRESULT hres;
1042
1043         hres = HTMLScreen_Create(&This->screen);
1044         if(FAILED(hres))
1045             return hres;
1046     }
1047
1048     *p = This->screen;
1049     IHTMLScreen_AddRef(This->screen);
1050     return S_OK;
1051 }
1052
1053 static HRESULT WINAPI HTMLWindow2_get_Option(IHTMLWindow2 *iface, IHTMLOptionElementFactory **p)
1054 {
1055     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1056
1057     TRACE("(%p)->(%p)\n", This, p);
1058
1059     if(!This->option_factory)
1060         This->option_factory = HTMLOptionElementFactory_Create(This);
1061
1062     *p = HTMLOPTFACTORY(This->option_factory);
1063     IHTMLOptionElementFactory_AddRef(*p);
1064
1065     return S_OK;
1066 }
1067
1068 static HRESULT WINAPI HTMLWindow2_focus(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_get_closed(IHTMLWindow2 *iface, VARIANT_BOOL *p)
1076 {
1077     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1078     FIXME("(%p)->(%p)\n", This, p);
1079     return E_NOTIMPL;
1080 }
1081
1082 static HRESULT WINAPI HTMLWindow2_blur(IHTMLWindow2 *iface)
1083 {
1084     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1085     FIXME("(%p)->()\n", This);
1086     return E_NOTIMPL;
1087 }
1088
1089 static HRESULT WINAPI HTMLWindow2_scroll(IHTMLWindow2 *iface, LONG x, LONG y)
1090 {
1091     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1092     FIXME("(%p)->(%d %d)\n", This, x, y);
1093     return E_NOTIMPL;
1094 }
1095
1096 static HRESULT WINAPI HTMLWindow2_get_clientInformation(IHTMLWindow2 *iface, IOmNavigator **p)
1097 {
1098     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1099     FIXME("(%p)->(%p)\n", This, p);
1100     return E_NOTIMPL;
1101 }
1102
1103 static HRESULT WINAPI HTMLWindow2_setInterval(IHTMLWindow2 *iface, BSTR expression,
1104         LONG msec, VARIANT *language, LONG *timerID)
1105 {
1106     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1107     VARIANT expr;
1108
1109     TRACE("(%p)->(%s %d %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
1110
1111     V_VT(&expr) = VT_BSTR;
1112     V_BSTR(&expr) = expression;
1113     return IHTMLWindow3_setInterval(HTMLWINDOW3(This), &expr, msec, language, timerID);
1114 }
1115
1116 static HRESULT WINAPI HTMLWindow2_clearInterval(IHTMLWindow2 *iface, LONG timerID)
1117 {
1118     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1119
1120     TRACE("(%p)->(%d)\n", This, timerID);
1121
1122     return clear_task_timer(&This->doc->basedoc, TRUE, timerID);
1123 }
1124
1125 static HRESULT WINAPI HTMLWindow2_put_offscreenBuffering(IHTMLWindow2 *iface, VARIANT v)
1126 {
1127     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1128     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
1129     return E_NOTIMPL;
1130 }
1131
1132 static HRESULT WINAPI HTMLWindow2_get_offscreenBuffering(IHTMLWindow2 *iface, VARIANT *p)
1133 {
1134     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1135     FIXME("(%p)->(%p)\n", This, p);
1136     return E_NOTIMPL;
1137 }
1138
1139 static HRESULT WINAPI HTMLWindow2_execScript(IHTMLWindow2 *iface, BSTR scode, BSTR language,
1140         VARIANT *pvarRet)
1141 {
1142     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1143     FIXME("(%p)->(%s %s %p)\n", This, debugstr_w(scode), debugstr_w(language), pvarRet);
1144     return E_NOTIMPL;
1145 }
1146
1147 static HRESULT WINAPI HTMLWindow2_toString(IHTMLWindow2 *iface, BSTR *String)
1148 {
1149     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1150
1151     static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
1152
1153     TRACE("(%p)->(%p)\n", This, String);
1154
1155     if(!String)
1156         return E_INVALIDARG;
1157
1158     *String = SysAllocString(objectW);
1159     return *String ? S_OK : E_OUTOFMEMORY;
1160 }
1161
1162 static HRESULT WINAPI HTMLWindow2_scrollBy(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_ScrollBy(This->nswindow, x, y);
1170     if(NS_FAILED(nsres))
1171         ERR("ScrollBy failed: %08x\n", nsres);
1172
1173     return S_OK;
1174 }
1175
1176 static HRESULT WINAPI HTMLWindow2_scrollTo(IHTMLWindow2 *iface, LONG x, LONG y)
1177 {
1178     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1179     nsresult nsres;
1180
1181     TRACE("(%p)->(%d %d)\n", This, x, y);
1182
1183     nsres = nsIDOMWindow_ScrollTo(This->nswindow, x, y);
1184     if(NS_FAILED(nsres))
1185         ERR("ScrollTo failed: %08x\n", nsres);
1186
1187     return S_OK;
1188 }
1189
1190 static HRESULT WINAPI HTMLWindow2_moveTo(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_moveBy(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_resizeTo(IHTMLWindow2 *iface, LONG x, LONG y)
1205 {
1206     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1207     FIXME("(%p)->(%d %d)\n", This, x, y);
1208     return E_NOTIMPL;
1209 }
1210
1211 static HRESULT WINAPI HTMLWindow2_resizeBy(IHTMLWindow2 *iface, LONG x, LONG y)
1212 {
1213     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1214     FIXME("(%p)->(%d %d)\n", This, x, y);
1215     return E_NOTIMPL;
1216 }
1217
1218 static HRESULT WINAPI HTMLWindow2_get_external(IHTMLWindow2 *iface, IDispatch **p)
1219 {
1220     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1221
1222     TRACE("(%p)->(%p)\n", This, p);
1223
1224     *p = NULL;
1225
1226     if(!This->doc_obj->hostui)
1227         return S_OK;
1228
1229     return IDocHostUIHandler_GetExternal(This->doc_obj->hostui, p);
1230 }
1231
1232 static HRESULT HTMLWindow_invoke(IUnknown *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
1233         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
1234 {
1235     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
1236     global_prop_t *prop;
1237     DWORD idx;
1238     HRESULT hres;
1239
1240     idx = id - MSHTML_DISPID_CUSTOM_MIN;
1241     if(idx >= This->global_prop_cnt)
1242         return DISP_E_MEMBERNOTFOUND;
1243
1244     prop = This->global_props+idx;
1245
1246     switch(prop->type) {
1247     case GLOBAL_SCRIPTVAR: {
1248         IDispatchEx *dispex;
1249         IDispatch *disp;
1250
1251         disp = get_script_disp(prop->script_host);
1252         if(!disp)
1253             return E_UNEXPECTED;
1254
1255         hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1256         if(SUCCEEDED(hres)) {
1257             TRACE("%s >>>\n", debugstr_w(prop->name));
1258             hres = IDispatchEx_InvokeEx(dispex, prop->id, lcid, flags, params, res, ei, caller);
1259             if(hres == S_OK)
1260                 TRACE("%s <<<\n", debugstr_w(prop->name));
1261             else
1262                 WARN("%s <<< %08x\n", debugstr_w(prop->name), hres);
1263             IDispatchEx_Release(dispex);
1264         }else {
1265             FIXME("No IDispatchEx\n");
1266         }
1267         IDispatch_Release(disp);
1268         break;
1269     }
1270     case GLOBAL_ELEMENTVAR: {
1271         IHTMLElement *elem;
1272
1273         hres = IHTMLDocument3_getElementById(HTMLDOC3(&This->doc->basedoc), prop->name, &elem);
1274         if(FAILED(hres))
1275             return hres;
1276
1277         if(!elem)
1278             return DISP_E_MEMBERNOTFOUND;
1279
1280         V_VT(res) = VT_DISPATCH;
1281         V_DISPATCH(res) = (IDispatch*)elem;
1282         break;
1283     }
1284     default:
1285         ERR("invalid type %d\n", prop->type);
1286         hres = DISP_E_MEMBERNOTFOUND;
1287     }
1288
1289     return hres;
1290 }
1291
1292 #undef HTMLWINDOW2_THIS
1293
1294 static const IHTMLWindow2Vtbl HTMLWindow2Vtbl = {
1295     HTMLWindow2_QueryInterface,
1296     HTMLWindow2_AddRef,
1297     HTMLWindow2_Release,
1298     HTMLWindow2_GetTypeInfoCount,
1299     HTMLWindow2_GetTypeInfo,
1300     HTMLWindow2_GetIDsOfNames,
1301     HTMLWindow2_Invoke,
1302     HTMLWindow2_item,
1303     HTMLWindow2_get_length,
1304     HTMLWindow2_get_frames,
1305     HTMLWindow2_put_defaultStatus,
1306     HTMLWindow2_get_defaultStatus,
1307     HTMLWindow2_put_status,
1308     HTMLWindow2_get_status,
1309     HTMLWindow2_setTimeout,
1310     HTMLWindow2_clearTimeout,
1311     HTMLWindow2_alert,
1312     HTMLWindow2_confirm,
1313     HTMLWindow2_prompt,
1314     HTMLWindow2_get_Image,
1315     HTMLWindow2_get_location,
1316     HTMLWindow2_get_history,
1317     HTMLWindow2_close,
1318     HTMLWindow2_put_opener,
1319     HTMLWindow2_get_opener,
1320     HTMLWindow2_get_navigator,
1321     HTMLWindow2_put_name,
1322     HTMLWindow2_get_name,
1323     HTMLWindow2_get_parent,
1324     HTMLWindow2_open,
1325     HTMLWindow2_get_self,
1326     HTMLWindow2_get_top,
1327     HTMLWindow2_get_window,
1328     HTMLWindow2_navigate,
1329     HTMLWindow2_put_onfocus,
1330     HTMLWindow2_get_onfocus,
1331     HTMLWindow2_put_onblur,
1332     HTMLWindow2_get_onblur,
1333     HTMLWindow2_put_onload,
1334     HTMLWindow2_get_onload,
1335     HTMLWindow2_put_onbeforeunload,
1336     HTMLWindow2_get_onbeforeunload,
1337     HTMLWindow2_put_onunload,
1338     HTMLWindow2_get_onunload,
1339     HTMLWindow2_put_onhelp,
1340     HTMLWindow2_get_onhelp,
1341     HTMLWindow2_put_onerror,
1342     HTMLWindow2_get_onerror,
1343     HTMLWindow2_put_onresize,
1344     HTMLWindow2_get_onresize,
1345     HTMLWindow2_put_onscroll,
1346     HTMLWindow2_get_onscroll,
1347     HTMLWindow2_get_document,
1348     HTMLWindow2_get_event,
1349     HTMLWindow2_get__newEnum,
1350     HTMLWindow2_showModalDialog,
1351     HTMLWindow2_showHelp,
1352     HTMLWindow2_get_screen,
1353     HTMLWindow2_get_Option,
1354     HTMLWindow2_focus,
1355     HTMLWindow2_get_closed,
1356     HTMLWindow2_blur,
1357     HTMLWindow2_scroll,
1358     HTMLWindow2_get_clientInformation,
1359     HTMLWindow2_setInterval,
1360     HTMLWindow2_clearInterval,
1361     HTMLWindow2_put_offscreenBuffering,
1362     HTMLWindow2_get_offscreenBuffering,
1363     HTMLWindow2_execScript,
1364     HTMLWindow2_toString,
1365     HTMLWindow2_scrollBy,
1366     HTMLWindow2_scrollTo,
1367     HTMLWindow2_moveTo,
1368     HTMLWindow2_moveBy,
1369     HTMLWindow2_resizeTo,
1370     HTMLWindow2_resizeBy,
1371     HTMLWindow2_get_external
1372 };
1373
1374 #define HTMLWINDOW3_THIS(iface) DEFINE_THIS(HTMLWindow, HTMLWindow3, iface)
1375
1376 static HRESULT WINAPI HTMLWindow3_QueryInterface(IHTMLWindow3 *iface, REFIID riid, void **ppv)
1377 {
1378     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1379
1380     return IHTMLWindow2_QueryInterface(HTMLWINDOW2(This), riid, ppv);
1381 }
1382
1383 static ULONG WINAPI HTMLWindow3_AddRef(IHTMLWindow3 *iface)
1384 {
1385     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1386
1387     return IHTMLWindow2_AddRef(HTMLWINDOW2(This));
1388 }
1389
1390 static ULONG WINAPI HTMLWindow3_Release(IHTMLWindow3 *iface)
1391 {
1392     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1393
1394     return IHTMLWindow2_Release(HTMLWINDOW2(This));
1395 }
1396
1397 static HRESULT WINAPI HTMLWindow3_GetTypeInfoCount(IHTMLWindow3 *iface, UINT *pctinfo)
1398 {
1399     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1400
1401     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(This), pctinfo);
1402 }
1403
1404 static HRESULT WINAPI HTMLWindow3_GetTypeInfo(IHTMLWindow3 *iface, UINT iTInfo,
1405                                               LCID lcid, ITypeInfo **ppTInfo)
1406 {
1407     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1408
1409     return IDispatchEx_GetTypeInfo(DISPATCHEX(This), iTInfo, lcid, ppTInfo);
1410 }
1411
1412 static HRESULT WINAPI HTMLWindow3_GetIDsOfNames(IHTMLWindow3 *iface, REFIID riid,
1413                                                 LPOLESTR *rgszNames, UINT cNames,
1414                                                 LCID lcid, DISPID *rgDispId)
1415 {
1416     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1417
1418     return IDispatchEx_GetIDsOfNames(DISPATCHEX(This), riid, rgszNames, cNames, lcid, rgDispId);
1419 }
1420
1421 static HRESULT WINAPI HTMLWindow3_Invoke(IHTMLWindow3 *iface, DISPID dispIdMember,
1422                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1423                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1424 {
1425     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1426
1427     return IDispatchEx_Invoke(DISPATCHEX(This), dispIdMember, riid, lcid, wFlags, pDispParams,
1428             pVarResult, pExcepInfo, puArgErr);
1429 }
1430
1431 static HRESULT WINAPI HTMLWindow3_get_screenLeft(IHTMLWindow3 *iface, LONG *p)
1432 {
1433     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1434     FIXME("(%p)->(%p)\n", This, p);
1435     return E_NOTIMPL;
1436 }
1437
1438 static HRESULT WINAPI HTMLWindow3_get_screenTop(IHTMLWindow3 *iface, LONG *p)
1439 {
1440     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1441     FIXME("(%p)->(%p)\n", This, p);
1442     return E_NOTIMPL;
1443 }
1444
1445 static HRESULT WINAPI HTMLWindow3_attachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp, VARIANT_BOOL *pfResult)
1446 {
1447     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1448
1449     TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(event), pDisp, pfResult);
1450
1451     if(!This->doc) {
1452         FIXME("No document\n");
1453         return E_FAIL;
1454     }
1455
1456     return attach_event(&This->doc->body_event_target, NULL, &This->doc->basedoc, event, pDisp, pfResult);
1457 }
1458
1459 static HRESULT WINAPI HTMLWindow3_detachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp)
1460 {
1461     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1462     FIXME("(%p)->()\n", This);
1463     return E_NOTIMPL;
1464 }
1465
1466 static HRESULT window_set_timer(HTMLWindow *This, VARIANT *expr, LONG msec, VARIANT *language,
1467         BOOL interval, LONG *timer_id)
1468 {
1469     IDispatch *disp = NULL;
1470
1471     switch(V_VT(expr)) {
1472     case VT_DISPATCH:
1473         disp = V_DISPATCH(expr);
1474         IDispatch_AddRef(disp);
1475         break;
1476
1477     case VT_BSTR:
1478         disp = script_parse_event(This, V_BSTR(expr));
1479         break;
1480
1481     default:
1482         FIXME("unimplemented vt=%d\n", V_VT(expr));
1483         return E_NOTIMPL;
1484     }
1485
1486     if(!disp)
1487         return E_FAIL;
1488
1489     *timer_id = set_task_timer(&This->doc->basedoc, msec, interval, disp);
1490     IDispatch_Release(disp);
1491
1492     return S_OK;
1493 }
1494
1495 static HRESULT WINAPI HTMLWindow3_setTimeout(IHTMLWindow3 *iface, VARIANT *expression, LONG msec,
1496         VARIANT *language, LONG *timerID)
1497 {
1498     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1499
1500     TRACE("(%p)->(%p(%d) %d %p %p)\n", This, expression, V_VT(expression), msec, language, timerID);
1501
1502     return window_set_timer(This, expression, msec, language, FALSE, timerID);
1503 }
1504
1505 static HRESULT WINAPI HTMLWindow3_setInterval(IHTMLWindow3 *iface, VARIANT *expression, LONG msec,
1506         VARIANT *language, LONG *timerID)
1507 {
1508     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1509
1510     TRACE("(%p)->(%p %d %p %p)\n", This, expression, msec, language, timerID);
1511
1512     return window_set_timer(This, expression, msec, language, TRUE, timerID);
1513 }
1514
1515 static HRESULT WINAPI HTMLWindow3_print(IHTMLWindow3 *iface)
1516 {
1517     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1518     FIXME("(%p)\n", This);
1519     return E_NOTIMPL;
1520 }
1521
1522 static HRESULT WINAPI HTMLWindow3_put_onbeforeprint(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_onbeforeprint(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_put_onafterprint(IHTMLWindow3 *iface, VARIANT v)
1537 {
1538     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1539     FIXME("(%p)->()\n", This);
1540     return E_NOTIMPL;
1541 }
1542
1543 static HRESULT WINAPI HTMLWindow3_get_onafterprint(IHTMLWindow3 *iface, VARIANT *p)
1544 {
1545     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1546     FIXME("(%p)->(%p)\n", This, p);
1547     return E_NOTIMPL;
1548 }
1549
1550 static HRESULT WINAPI HTMLWindow3_get_clipboardData(IHTMLWindow3 *iface, IHTMLDataTransfer **p)
1551 {
1552     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1553     FIXME("(%p)->(%p)\n", This, p);
1554     return E_NOTIMPL;
1555 }
1556
1557 static HRESULT WINAPI HTMLWindow3_showModelessDialog(IHTMLWindow3 *iface, BSTR url,
1558         VARIANT *varArgIn, VARIANT *options, IHTMLWindow2 **pDialog)
1559 {
1560     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
1561     FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(url), varArgIn, options, pDialog);
1562     return E_NOTIMPL;
1563 }
1564
1565 #undef HTMLWINDOW3_THIS
1566
1567 static const IHTMLWindow3Vtbl HTMLWindow3Vtbl = {
1568     HTMLWindow3_QueryInterface,
1569     HTMLWindow3_AddRef,
1570     HTMLWindow3_Release,
1571     HTMLWindow3_GetTypeInfoCount,
1572     HTMLWindow3_GetTypeInfo,
1573     HTMLWindow3_GetIDsOfNames,
1574     HTMLWindow3_Invoke,
1575     HTMLWindow3_get_screenLeft,
1576     HTMLWindow3_get_screenTop,
1577     HTMLWindow3_attachEvent,
1578     HTMLWindow3_detachEvent,
1579     HTMLWindow3_setTimeout,
1580     HTMLWindow3_setInterval,
1581     HTMLWindow3_print,
1582     HTMLWindow3_put_onbeforeprint,
1583     HTMLWindow3_get_onbeforeprint,
1584     HTMLWindow3_put_onafterprint,
1585     HTMLWindow3_get_onafterprint,
1586     HTMLWindow3_get_clipboardData,
1587     HTMLWindow3_showModelessDialog
1588 };
1589
1590 #define HTMLWINDOW4_THIS(iface) DEFINE_THIS(HTMLWindow, HTMLWindow4, iface)
1591
1592 static HRESULT WINAPI HTMLWindow4_QueryInterface(IHTMLWindow4 *iface, REFIID riid, void **ppv)
1593 {
1594     HTMLWindow *This = HTMLWINDOW4_THIS(iface);
1595
1596     return IHTMLWindow2_QueryInterface(HTMLWINDOW2(This), riid, ppv);
1597 }
1598
1599 static ULONG WINAPI HTMLWindow4_AddRef(IHTMLWindow4 *iface)
1600 {
1601     HTMLWindow *This = HTMLWINDOW4_THIS(iface);
1602
1603     return IHTMLWindow2_AddRef(HTMLWINDOW2(This));
1604 }
1605
1606 static ULONG WINAPI HTMLWindow4_Release(IHTMLWindow4 *iface)
1607 {
1608     HTMLWindow *This = HTMLWINDOW4_THIS(iface);
1609
1610     return IHTMLWindow2_Release(HTMLWINDOW2(This));
1611 }
1612
1613 static HRESULT WINAPI HTMLWindow4_GetTypeInfoCount(IHTMLWindow4 *iface, UINT *pctinfo)
1614 {
1615     HTMLWindow *This = HTMLWINDOW4_THIS(iface);
1616
1617     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(This), pctinfo);
1618 }
1619
1620 static HRESULT WINAPI HTMLWindow4_GetTypeInfo(IHTMLWindow4 *iface, UINT iTInfo,
1621                                               LCID lcid, ITypeInfo **ppTInfo)
1622 {
1623     HTMLWindow *This = HTMLWINDOW4_THIS(iface);
1624
1625     return IDispatchEx_GetTypeInfo(DISPATCHEX(This), iTInfo, lcid, ppTInfo);
1626 }
1627
1628 static HRESULT WINAPI HTMLWindow4_GetIDsOfNames(IHTMLWindow4 *iface, REFIID riid,
1629                                                 LPOLESTR *rgszNames, UINT cNames,
1630                                                 LCID lcid, DISPID *rgDispId)
1631 {
1632     HTMLWindow *This = HTMLWINDOW4_THIS(iface);
1633
1634     return IDispatchEx_GetIDsOfNames(DISPATCHEX(This), riid, rgszNames, cNames, lcid, rgDispId);
1635 }
1636
1637 static HRESULT WINAPI HTMLWindow4_Invoke(IHTMLWindow4 *iface, DISPID dispIdMember,
1638                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1639                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1640 {
1641     HTMLWindow *This = HTMLWINDOW4_THIS(iface);
1642
1643     return IDispatchEx_Invoke(DISPATCHEX(This), dispIdMember, riid, lcid, wFlags, pDispParams,
1644             pVarResult, pExcepInfo, puArgErr);
1645 }
1646
1647 static HRESULT WINAPI HTMLWindow4_createPopup(IHTMLWindow4 *iface, VARIANT *varArgIn,
1648                             IDispatch **ppPopup)
1649 {
1650     HTMLWindow *This = HTMLWINDOW4_THIS(iface);
1651     FIXME("(%p)->(%p %p)\n", This, varArgIn, ppPopup);
1652     return E_NOTIMPL;
1653 }
1654
1655 static HRESULT WINAPI HTMLWindow4_get_frameElement(IHTMLWindow4 *iface, IHTMLFrameBase **p)
1656 {
1657     HTMLWindow *This = HTMLWINDOW4_THIS(iface);
1658     TRACE("(%p)->(%p)\n", This, p);
1659
1660     if(This->frame_element) {
1661         *p = HTMLFRAMEBASE(This->frame_element);
1662         IHTMLFrameBase_AddRef(*p);
1663     }else
1664         *p = NULL;
1665
1666     return S_OK;
1667 }
1668
1669 #undef HTMLWINDOW4_THIS
1670
1671 static const IHTMLWindow4Vtbl HTMLWindow4Vtbl = {
1672     HTMLWindow4_QueryInterface,
1673     HTMLWindow4_AddRef,
1674     HTMLWindow4_Release,
1675     HTMLWindow4_GetTypeInfoCount,
1676     HTMLWindow4_GetTypeInfo,
1677     HTMLWindow4_GetIDsOfNames,
1678     HTMLWindow4_Invoke,
1679     HTMLWindow4_createPopup,
1680     HTMLWindow4_get_frameElement
1681 };
1682
1683 #define HTMLPRIVWINDOW_THIS(iface) DEFINE_THIS(HTMLWindow, IHTMLPrivateWindow, iface)
1684
1685 static HRESULT WINAPI HTMLPrivateWindow_QueryInterface(IHTMLPrivateWindow *iface, REFIID riid, void **ppv)
1686 {
1687     HTMLWindow *This = HTMLPRIVWINDOW_THIS(iface);
1688
1689     return IHTMLWindow2_QueryInterface(HTMLWINDOW2(This), riid, ppv);
1690 }
1691
1692 static ULONG WINAPI HTMLPrivateWindow_AddRef(IHTMLPrivateWindow *iface)
1693 {
1694     HTMLWindow *This = HTMLPRIVWINDOW_THIS(iface);
1695
1696     return IHTMLWindow2_AddRef(HTMLWINDOW2(This));
1697 }
1698
1699 static ULONG WINAPI HTMLPrivateWindow_Release(IHTMLPrivateWindow *iface)
1700 {
1701     HTMLWindow *This = HTMLPRIVWINDOW_THIS(iface);
1702
1703     return IHTMLWindow2_Release(HTMLWINDOW2(This));
1704 }
1705
1706 static HRESULT WINAPI HTMLPrivateWindow_SuperNavigate(IHTMLPrivateWindow *iface, BSTR url, BSTR arg2, BSTR arg3,
1707         BSTR arg4, VARIANT *post_data_var, VARIANT *headers_var, ULONG flags)
1708 {
1709     HTMLWindow *This = HTMLPRIVWINDOW_THIS(iface);
1710     DWORD post_data_size = 0;
1711     BYTE *post_data = NULL;
1712     WCHAR *headers = NULL;
1713     nsChannelBSC *bsc;
1714     IMoniker *mon;
1715     BSTR new_url;
1716     HRESULT hres;
1717
1718     TRACE("(%p)->(%s %s %s %s %s %s %x)\n", This, debugstr_w(url), debugstr_w(arg2), debugstr_w(arg3), debugstr_w(arg4),
1719           debugstr_variant(post_data_var), debugstr_variant(headers_var), flags);
1720
1721     new_url = url;
1722     if(This->doc_obj->hostui) {
1723         OLECHAR *translated_url = NULL;
1724
1725         hres = IDocHostUIHandler_TranslateUrl(This->doc_obj->hostui, 0, url, &translated_url);
1726         if(hres == S_OK && translated_url) {
1727             new_url = SysAllocString(translated_url);
1728             CoTaskMemFree(translated_url);
1729         }
1730     }
1731
1732     if(This->doc_obj->client) {
1733         IOleCommandTarget *cmdtrg;
1734
1735         hres = IOleClientSite_QueryInterface(This->doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1736         if(SUCCEEDED(hres)) {
1737             VARIANT in, out;
1738
1739             V_VT(&in) = VT_BSTR;
1740             V_BSTR(&in) = new_url;
1741             V_VT(&out) = VT_BOOL;
1742             V_BOOL(&out) = VARIANT_TRUE;
1743             hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &in, &out);
1744             IOleCommandTarget_Release(cmdtrg);
1745             if(SUCCEEDED(hres))
1746                 VariantClear(&out);
1747         }
1748     }
1749
1750     /* FIXME: Why not set_ready_state? */
1751     This->readystate = READYSTATE_UNINITIALIZED;
1752
1753     hres = CreateURLMoniker(NULL, new_url, &mon);
1754     if(new_url != url)
1755         SysFreeString(new_url);
1756     if(FAILED(hres))
1757         return hres;
1758
1759     if(post_data_var) {
1760         if(V_VT(post_data_var) == (VT_ARRAY|VT_UI1)) {
1761             SafeArrayAccessData(V_ARRAY(post_data_var), (void**)&post_data);
1762             post_data_size = V_ARRAY(post_data_var)->rgsabound[0].cElements;
1763         }
1764     }
1765
1766     if(headers_var && V_VT(headers_var) != VT_EMPTY && V_VT(headers_var) != VT_ERROR) {
1767         if(V_VT(headers_var) != VT_BSTR)
1768             return E_INVALIDARG;
1769
1770         headers = V_BSTR(headers_var);
1771     }
1772
1773     hres = create_channelbsc(mon, headers, post_data, post_data_size, &bsc);
1774     if(post_data)
1775         SafeArrayUnaccessData(V_ARRAY(post_data_var));
1776     if(FAILED(hres)) {
1777         IMoniker_Release(mon);
1778         return hres;
1779     }
1780
1781     hres = set_moniker(&This->doc_obj->basedoc, mon, NULL, bsc, TRUE);
1782     if(SUCCEEDED(hres))
1783         hres = async_start_doc_binding(This, bsc);
1784
1785     IUnknown_Release((IUnknown*)bsc);
1786     IMoniker_Release(mon);
1787     return hres;
1788 }
1789
1790 static HRESULT WINAPI HTMLPrivateWindow_GetPendingUrl(IHTMLPrivateWindow *iface, BSTR *url)
1791 {
1792     HTMLWindow *This = HTMLPRIVWINDOW_THIS(iface);
1793     FIXME("(%p)->(%p)\n", This, url);
1794     return E_NOTIMPL;
1795 }
1796
1797 static HRESULT WINAPI HTMLPrivateWindow_SetPICSTarget(IHTMLPrivateWindow *iface, IOleCommandTarget *cmdtrg)
1798 {
1799     HTMLWindow *This = HTMLPRIVWINDOW_THIS(iface);
1800     FIXME("(%p)->(%p)\n", This, cmdtrg);
1801     return E_NOTIMPL;
1802 }
1803
1804 static HRESULT WINAPI HTMLPrivateWindow_PICSComplete(IHTMLPrivateWindow *iface, int arg)
1805 {
1806     HTMLWindow *This = HTMLPRIVWINDOW_THIS(iface);
1807     FIXME("(%p)->(%x)\n", This, arg);
1808     return E_NOTIMPL;
1809 }
1810
1811 static HRESULT WINAPI HTMLPrivateWindow_FindWindowByName(IHTMLPrivateWindow *iface, LPCWSTR name, IHTMLWindow2 **ret)
1812 {
1813     HTMLWindow *This = HTMLPRIVWINDOW_THIS(iface);
1814     FIXME("(%p)->(%s %p)\n", This, debugstr_w(name), ret);
1815     return E_NOTIMPL;
1816 }
1817
1818 static HRESULT WINAPI HTMLPrivateWindow_GetAddressBar(IHTMLPrivateWindow *iface, BSTR *url)
1819 {
1820     HTMLWindow *This = HTMLPRIVWINDOW_THIS(iface);
1821     FIXME("(%p)->(%p)\n", This, url);
1822     return E_NOTIMPL;
1823 }
1824
1825 #undef HTMLPRIVWINDOW_THIS
1826
1827 static const IHTMLPrivateWindowVtbl HTMLPrivateWindowVtbl = {
1828     HTMLPrivateWindow_QueryInterface,
1829     HTMLPrivateWindow_AddRef,
1830     HTMLPrivateWindow_Release,
1831     HTMLPrivateWindow_SuperNavigate,
1832     HTMLPrivateWindow_GetPendingUrl,
1833     HTMLPrivateWindow_SetPICSTarget,
1834     HTMLPrivateWindow_PICSComplete,
1835     HTMLPrivateWindow_FindWindowByName,
1836     HTMLPrivateWindow_GetAddressBar
1837 };
1838
1839 #define DISPEX_THIS(iface) DEFINE_THIS(HTMLWindow, IDispatchEx, iface)
1840
1841 static HRESULT WINAPI WindowDispEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1842 {
1843     HTMLWindow *This = DISPEX_THIS(iface);
1844
1845     return IHTMLWindow2_QueryInterface(HTMLWINDOW2(This), riid, ppv);
1846 }
1847
1848 static ULONG WINAPI WindowDispEx_AddRef(IDispatchEx *iface)
1849 {
1850     HTMLWindow *This = DISPEX_THIS(iface);
1851
1852     return IHTMLWindow2_AddRef(HTMLWINDOW2(This));
1853 }
1854
1855 static ULONG WINAPI WindowDispEx_Release(IDispatchEx *iface)
1856 {
1857     HTMLWindow *This = DISPEX_THIS(iface);
1858
1859     return IHTMLWindow2_Release(HTMLWINDOW2(This));
1860 }
1861
1862 static HRESULT WINAPI WindowDispEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1863 {
1864     HTMLWindow *This = DISPEX_THIS(iface);
1865
1866     TRACE("(%p)->(%p)\n", This, pctinfo);
1867
1868     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
1869 }
1870
1871 static HRESULT WINAPI WindowDispEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1872                                                LCID lcid, ITypeInfo **ppTInfo)
1873 {
1874     HTMLWindow *This = DISPEX_THIS(iface);
1875
1876     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1877
1878     return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
1879 }
1880
1881 static HRESULT WINAPI WindowDispEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1882                                                  LPOLESTR *rgszNames, UINT cNames,
1883                                                  LCID lcid, DISPID *rgDispId)
1884 {
1885     HTMLWindow *This = DISPEX_THIS(iface);
1886     UINT i;
1887     HRESULT hres;
1888
1889     WARN("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1890           lcid, rgDispId);
1891
1892     for(i=0; i < cNames; i++) {
1893         /* We shouldn't use script's IDispatchEx here, so we shouldn't use GetDispID */
1894         hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
1895         if(FAILED(hres))
1896             return hres;
1897     }
1898
1899     return S_OK;
1900 }
1901
1902 static HRESULT WINAPI WindowDispEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1903                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1904                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1905 {
1906     HTMLWindow *This = DISPEX_THIS(iface);
1907
1908     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1909           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1910
1911     /* FIXME: Use script dispatch */
1912
1913     return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid, wFlags, pDispParams,
1914                               pVarResult, pExcepInfo, puArgErr);
1915 }
1916
1917 static global_prop_t *alloc_global_prop(HTMLWindow *This, global_prop_type_t type, BSTR name)
1918 {
1919     if(This->global_prop_cnt == This->global_prop_size) {
1920         global_prop_t *new_props;
1921         DWORD new_size;
1922
1923         if(This->global_props) {
1924             new_size = This->global_prop_size*2;
1925             new_props = heap_realloc(This->global_props, new_size*sizeof(global_prop_t));
1926         }else {
1927             new_size = 16;
1928             new_props = heap_alloc(new_size*sizeof(global_prop_t));
1929         }
1930         if(!new_props)
1931             return NULL;
1932         This->global_props = new_props;
1933         This->global_prop_size = new_size;
1934     }
1935
1936     This->global_props[This->global_prop_cnt].name = heap_strdupW(name);
1937     if(!This->global_props[This->global_prop_cnt].name)
1938         return NULL;
1939
1940     This->global_props[This->global_prop_cnt].type = type;
1941     return This->global_props + This->global_prop_cnt++;
1942 }
1943
1944 static inline DWORD prop_to_dispid(HTMLWindow *This, global_prop_t *prop)
1945 {
1946     return MSHTML_DISPID_CUSTOM_MIN + (prop-This->global_props);
1947 }
1948
1949 HRESULT search_window_props(HTMLWindow *This, BSTR bstrName, DWORD grfdex, DISPID *pid)
1950 {
1951     DWORD i;
1952     ScriptHost *script_host;
1953     DISPID id;
1954
1955     for(i=0; i < This->global_prop_cnt; i++) {
1956         /* FIXME: case sensitivity */
1957         if(!strcmpW(This->global_props[i].name, bstrName)) {
1958             *pid = MSHTML_DISPID_CUSTOM_MIN+i;
1959             return S_OK;
1960         }
1961     }
1962
1963     if(find_global_prop(This, bstrName, grfdex, &script_host, &id)) {
1964         global_prop_t *prop;
1965
1966         prop = alloc_global_prop(This, GLOBAL_SCRIPTVAR, bstrName);
1967         if(!prop)
1968             return E_OUTOFMEMORY;
1969
1970         prop->script_host = script_host;
1971         prop->id = id;
1972
1973         *pid = prop_to_dispid(This, prop);
1974         return S_OK;
1975     }
1976
1977     return DISP_E_UNKNOWNNAME;
1978 }
1979
1980 static HRESULT WINAPI WindowDispEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1981 {
1982     HTMLWindow *This = DISPEX_THIS(iface);
1983     HRESULT hres;
1984
1985     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1986
1987     hres = search_window_props(This, bstrName, grfdex, pid);
1988     if(hres != DISP_E_UNKNOWNNAME)
1989         return hres;
1990
1991     hres = IDispatchEx_GetDispID(DISPATCHEX(&This->dispex), bstrName, grfdex, pid);
1992     if(hres != DISP_E_UNKNOWNNAME)
1993         return hres;
1994
1995     if(This->doc) {
1996         global_prop_t *prop;
1997         IHTMLElement *elem;
1998
1999         hres = IHTMLDocument3_getElementById(HTMLDOC3(&This->doc->basedoc), bstrName, &elem);
2000         if(SUCCEEDED(hres) && elem) {
2001             IHTMLElement_Release(elem);
2002
2003             prop = alloc_global_prop(This, GLOBAL_ELEMENTVAR, bstrName);
2004             if(!prop)
2005                 return E_OUTOFMEMORY;
2006
2007             *pid = prop_to_dispid(This, prop);
2008             return S_OK;
2009         }
2010     }
2011
2012     return DISP_E_UNKNOWNNAME;
2013 }
2014
2015 static HRESULT WINAPI WindowDispEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
2016         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
2017 {
2018     HTMLWindow *This = DISPEX_THIS(iface);
2019
2020     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
2021
2022     if(id == DISPID_IHTMLWINDOW2_LOCATION && (wFlags & DISPATCH_PROPERTYPUT)) {
2023         HTMLLocation *location;
2024         HRESULT hres;
2025
2026         TRACE("forwarding to location.href\n");
2027
2028         hres = get_location(This, &location);
2029         if(FAILED(hres))
2030             return hres;
2031
2032         hres = IDispatchEx_InvokeEx(DISPATCHEX(&location->dispex), DISPID_VALUE, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
2033         IHTMLLocation_Release(HTMLLOCATION(location));
2034         return hres;
2035     }
2036
2037     return IDispatchEx_InvokeEx(DISPATCHEX(&This->dispex), id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
2038 }
2039
2040 static HRESULT WINAPI WindowDispEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
2041 {
2042     HTMLWindow *This = DISPEX_THIS(iface);
2043
2044     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
2045
2046     return IDispatchEx_DeleteMemberByName(DISPATCHEX(&This->dispex), bstrName, grfdex);
2047 }
2048
2049 static HRESULT WINAPI WindowDispEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
2050 {
2051     HTMLWindow *This = DISPEX_THIS(iface);
2052
2053     TRACE("(%p)->(%x)\n", This, id);
2054
2055     return IDispatchEx_DeleteMemberByDispID(DISPATCHEX(&This->dispex), id);
2056 }
2057
2058 static HRESULT WINAPI WindowDispEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
2059 {
2060     HTMLWindow *This = DISPEX_THIS(iface);
2061
2062     TRACE("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
2063
2064     return IDispatchEx_GetMemberProperties(DISPATCHEX(&This->dispex), id, grfdexFetch, pgrfdex);
2065 }
2066
2067 static HRESULT WINAPI WindowDispEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
2068 {
2069     HTMLWindow *This = DISPEX_THIS(iface);
2070
2071     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
2072
2073     return IDispatchEx_GetMemberName(DISPATCHEX(&This->dispex), id, pbstrName);
2074 }
2075
2076 static HRESULT WINAPI WindowDispEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
2077 {
2078     HTMLWindow *This = DISPEX_THIS(iface);
2079
2080     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
2081
2082     return IDispatchEx_GetNextDispID(DISPATCHEX(&This->dispex), grfdex, id, pid);
2083 }
2084
2085 static HRESULT WINAPI WindowDispEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
2086 {
2087     HTMLWindow *This = DISPEX_THIS(iface);
2088
2089     TRACE("(%p)->(%p)\n", This, ppunk);
2090
2091     *ppunk = NULL;
2092     return S_OK;
2093 }
2094
2095 #undef DISPEX_THIS
2096
2097 static const IDispatchExVtbl WindowDispExVtbl = {
2098     WindowDispEx_QueryInterface,
2099     WindowDispEx_AddRef,
2100     WindowDispEx_Release,
2101     WindowDispEx_GetTypeInfoCount,
2102     WindowDispEx_GetTypeInfo,
2103     WindowDispEx_GetIDsOfNames,
2104     WindowDispEx_Invoke,
2105     WindowDispEx_GetDispID,
2106     WindowDispEx_InvokeEx,
2107     WindowDispEx_DeleteMemberByName,
2108     WindowDispEx_DeleteMemberByDispID,
2109     WindowDispEx_GetMemberProperties,
2110     WindowDispEx_GetMemberName,
2111     WindowDispEx_GetNextDispID,
2112     WindowDispEx_GetNameSpaceParent
2113 };
2114
2115 static const tid_t HTMLWindow_iface_tids[] = {
2116     IHTMLWindow2_tid,
2117     IHTMLWindow3_tid,
2118     IHTMLWindow4_tid,
2119     0
2120 };
2121
2122 static const dispex_static_data_vtbl_t HTMLWindow_dispex_vtbl = {
2123     NULL,
2124     NULL,
2125     HTMLWindow_invoke
2126 };
2127
2128 static dispex_static_data_t HTMLWindow_dispex = {
2129     &HTMLWindow_dispex_vtbl,
2130     DispHTMLWindow2_tid,
2131     NULL,
2132     HTMLWindow_iface_tids
2133 };
2134
2135 HRESULT HTMLWindow_Create(HTMLDocumentObj *doc_obj, nsIDOMWindow *nswindow, HTMLWindow *parent, HTMLWindow **ret)
2136 {
2137     HTMLWindow *window;
2138
2139     window = heap_alloc_zero(sizeof(HTMLWindow));
2140     if(!window)
2141         return E_OUTOFMEMORY;
2142
2143     window->window_ref = heap_alloc(sizeof(windowref_t));
2144     if(!window->window_ref) {
2145         heap_free(window);
2146         return E_OUTOFMEMORY;
2147     }
2148
2149     window->lpHTMLWindow2Vtbl = &HTMLWindow2Vtbl;
2150     window->lpHTMLWindow3Vtbl = &HTMLWindow3Vtbl;
2151     window->lpHTMLWindow4Vtbl = &HTMLWindow4Vtbl;
2152     window->lpIHTMLPrivateWindowVtbl = &HTMLPrivateWindowVtbl;
2153     window->lpIDispatchExVtbl = &WindowDispExVtbl;
2154     window->ref = 1;
2155     window->doc_obj = doc_obj;
2156
2157     window->window_ref->window = window;
2158     window->window_ref->ref = 1;
2159
2160     init_dispex(&window->dispex, (IUnknown*)HTMLWINDOW2(window), &HTMLWindow_dispex);
2161
2162     if(nswindow) {
2163         nsIDOMWindow_AddRef(nswindow);
2164         window->nswindow = nswindow;
2165     }
2166
2167     window->scriptmode = parent ? parent->scriptmode : SCRIPTMODE_GECKO;
2168     window->readystate = READYSTATE_UNINITIALIZED;
2169     list_init(&window->script_hosts);
2170
2171     window->task_magic = get_task_target_magic();
2172     update_window_doc(window);
2173
2174     list_init(&window->children);
2175     list_add_head(&window_list, &window->entry);
2176
2177     if(parent) {
2178         IHTMLWindow2_AddRef(HTMLWINDOW2(window));
2179
2180         window->parent = parent;
2181         list_add_tail(&parent->children, &window->sibling_entry);
2182     }
2183
2184     *ret = window;
2185     return S_OK;
2186 }
2187
2188 void update_window_doc(HTMLWindow *window)
2189 {
2190     nsIDOMHTMLDocument *nshtmldoc;
2191     nsIDOMDocument *nsdoc;
2192     nsresult nsres;
2193
2194     nsres = nsIDOMWindow_GetDocument(window->nswindow, &nsdoc);
2195     if(NS_FAILED(nsres) || !nsdoc) {
2196         ERR("GetDocument failed: %08x\n", nsres);
2197         return;
2198     }
2199
2200     nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
2201     nsIDOMDocument_Release(nsdoc);
2202     if(NS_FAILED(nsres)) {
2203         ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
2204         return;
2205     }
2206
2207     if(!window->doc || window->doc->nsdoc != nshtmldoc) {
2208         HTMLDocumentNode *doc;
2209         HRESULT hres;
2210
2211         hres = create_doc_from_nsdoc(nshtmldoc, window->doc_obj, window, &doc);
2212         if(SUCCEEDED(hres)) {
2213             window_set_docnode(window, doc);
2214             htmldoc_release(&doc->basedoc);
2215         }else {
2216             ERR("create_doc_from_nsdoc failed: %08x\n", hres);
2217         }
2218     }
2219
2220     nsIDOMHTMLDocument_Release(nshtmldoc);
2221 }
2222
2223 HTMLWindow *nswindow_to_window(const nsIDOMWindow *nswindow)
2224 {
2225     HTMLWindow *iter;
2226
2227     LIST_FOR_EACH_ENTRY(iter, &window_list, HTMLWindow, entry) {
2228         if(iter->nswindow == nswindow)
2229             return iter;
2230     }
2231
2232     return NULL;
2233 }