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