mshtml: Use heap_alloc_zero for HTMLWindow allocation.
[wine] / dlls / mshtml / htmlwindow.c
1 /*
2  * Copyright 2006 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
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
30
31 #include "mshtml_private.h"
32 #include "resource.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35
36 static struct list window_list = LIST_INIT(window_list);
37
38 #define HTMLWINDOW2_THIS(iface) DEFINE_THIS(HTMLWindow, HTMLWindow2, iface)
39
40 static HRESULT WINAPI HTMLWindow2_QueryInterface(IHTMLWindow2 *iface, REFIID riid, void **ppv)
41 {
42     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
43
44     *ppv = NULL;
45
46     if(IsEqualGUID(&IID_IUnknown, riid)) {
47         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
48         *ppv = HTMLWINDOW2(This);
49     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
50         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
51         *ppv = HTMLWINDOW2(This);
52     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
53         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
54         *ppv = DISPATCHEX(This);
55     }else if(IsEqualGUID(&IID_IHTMLFramesCollection2, riid)) {
56         TRACE("(%p)->(IID_IHTMLFramesCollection2 %p)\n", This, ppv);
57         *ppv = HTMLWINDOW2(This);
58     }else if(IsEqualGUID(&IID_IHTMLWindow2, riid)) {
59         TRACE("(%p)->(IID_IHTMLWindow2 %p)\n", This, ppv);
60         *ppv = HTMLWINDOW2(This);
61     }else if(IsEqualGUID(&IID_IHTMLWindow3, riid)) {
62         TRACE("(%p)->(IID_IHTMLWindow2 %p)\n", This, ppv);
63         *ppv = HTMLWINDOW3(This);
64     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
65         return *ppv ? S_OK : E_NOINTERFACE;
66     }
67
68     if(*ppv) {
69         IUnknown_AddRef((IUnknown*)*ppv);
70         return S_OK;
71     }
72
73     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
74     return E_NOINTERFACE;
75 }
76
77 static ULONG WINAPI HTMLWindow2_AddRef(IHTMLWindow2 *iface)
78 {
79     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
80     LONG ref = InterlockedIncrement(&This->ref);
81
82     TRACE("(%p) ref=%d\n", This, ref);
83
84     return ref;
85 }
86
87 static ULONG WINAPI HTMLWindow2_Release(IHTMLWindow2 *iface)
88 {
89     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
90     LONG ref = InterlockedDecrement(&This->ref);
91
92     TRACE("(%p) ref=%d\n", This, ref);
93
94     if(!ref) {
95         list_remove(&This->entry);
96         heap_free(This);
97     }
98
99     return ref;
100 }
101
102 static HRESULT WINAPI HTMLWindow2_GetTypeInfoCount(IHTMLWindow2 *iface, UINT *pctinfo)
103 {
104     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
105
106     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(This), pctinfo);
107 }
108
109 static HRESULT WINAPI HTMLWindow2_GetTypeInfo(IHTMLWindow2 *iface, UINT iTInfo,
110                                               LCID lcid, ITypeInfo **ppTInfo)
111 {
112     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
113
114     return IDispatchEx_GetTypeInfo(DISPATCHEX(This), iTInfo, lcid, ppTInfo);
115 }
116
117 static HRESULT WINAPI HTMLWindow2_GetIDsOfNames(IHTMLWindow2 *iface, REFIID riid,
118                                                 LPOLESTR *rgszNames, UINT cNames,
119                                                 LCID lcid, DISPID *rgDispId)
120 {
121     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
122
123     return IDispatchEx_GetIDsOfNames(DISPATCHEX(This), riid, rgszNames, cNames, lcid, rgDispId);
124 }
125
126 static HRESULT WINAPI HTMLWindow2_Invoke(IHTMLWindow2 *iface, DISPID dispIdMember,
127                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
128                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
129 {
130     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
131
132     return IDispatchEx_Invoke(DISPATCHEX(This), dispIdMember, riid, lcid, wFlags, pDispParams,
133             pVarResult, pExcepInfo, puArgErr);
134 }
135
136 static HRESULT WINAPI HTMLWindow2_item(IHTMLWindow2 *iface, VARIANT *pvarIndex, VARIANT *pvarResult)
137 {
138     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
139     FIXME("(%p)->(%p %p)\n", This, pvarIndex, pvarResult);
140     return E_NOTIMPL;
141 }
142
143 static HRESULT WINAPI HTMLWindow2_get_length(IHTMLWindow2 *iface, long *p)
144 {
145     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
146     FIXME("(%p)->(%p)\n", This, p);
147     return E_NOTIMPL;
148 }
149
150 static HRESULT WINAPI HTMLWindow2_get_frames(IHTMLWindow2 *iface, IHTMLFramesCollection2 **p)
151 {
152     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
153     FIXME("(%p)->(%p)\n", This, p);
154     return E_NOTIMPL;
155 }
156
157 static HRESULT WINAPI HTMLWindow2_put_defaultStatus(IHTMLWindow2 *iface, BSTR v)
158 {
159     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
160     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
161     return E_NOTIMPL;
162 }
163
164 static HRESULT WINAPI HTMLWindow2_get_defaultStatus(IHTMLWindow2 *iface, BSTR *p)
165 {
166     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
167     FIXME("(%p)->(%p)\n", This, p);
168     return E_NOTIMPL;
169 }
170
171 static HRESULT WINAPI HTMLWindow2_put_status(IHTMLWindow2 *iface, BSTR v)
172 {
173     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
174     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
175     return E_NOTIMPL;
176 }
177
178 static HRESULT WINAPI HTMLWindow2_get_status(IHTMLWindow2 *iface, BSTR *p)
179 {
180     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
181     FIXME("(%p)->(%p)\n", This, p);
182     return E_NOTIMPL;
183 }
184
185 static HRESULT WINAPI HTMLWindow2_setTimeout(IHTMLWindow2 *iface, BSTR expression,
186         long msec, VARIANT *language, long *timerID)
187 {
188     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
189     VARIANT expr_var;
190
191     TRACE("(%p)->(%s %ld %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
192
193     V_VT(&expr_var) = VT_BSTR;
194     V_BSTR(&expr_var) = expression;
195
196     return IHTMLWindow3_setTimeout(HTMLWINDOW3(This), &expr_var, msec, language, timerID);
197 }
198
199 static HRESULT WINAPI HTMLWindow2_clearTimeout(IHTMLWindow2 *iface, long timerID)
200 {
201     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
202
203     TRACE("(%p)->(%ld)\n", This, timerID);
204
205     return clear_task_timer(This->doc, FALSE, timerID);
206 }
207
208 static HRESULT WINAPI HTMLWindow2_alert(IHTMLWindow2 *iface, BSTR message)
209 {
210     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
211     WCHAR wszTitle[100];
212
213     TRACE("(%p)->(%s)\n", This, debugstr_w(message));
214
215     if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, wszTitle,
216                     sizeof(wszTitle)/sizeof(WCHAR))) {
217         WARN("Could not load message box title: %d\n", GetLastError());
218         return S_OK;
219     }
220
221     MessageBoxW(This->doc->hwnd, message, wszTitle, MB_ICONWARNING);
222     return S_OK;
223 }
224
225 static HRESULT WINAPI HTMLWindow2_confirm(IHTMLWindow2 *iface, BSTR message,
226         VARIANT_BOOL *confirmed)
227 {
228     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
229     FIXME("(%p)->(%s %p)\n", This, debugstr_w(message), confirmed);
230     return E_NOTIMPL;
231 }
232
233 static HRESULT WINAPI HTMLWindow2_prompt(IHTMLWindow2 *iface, BSTR message,
234         BSTR dststr, VARIANT *textdata)
235 {
236     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
237     FIXME("(%p)->(%s %s %p)\n", This, debugstr_w(message), debugstr_w(dststr), textdata);
238     return E_NOTIMPL;
239 }
240
241 static HRESULT WINAPI HTMLWindow2_get_Image(IHTMLWindow2 *iface, IHTMLImageElementFactory **p)
242 {
243     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
244     FIXME("(%p)->(%p)\n", This, p);
245     return E_NOTIMPL;
246 }
247
248 static HRESULT WINAPI HTMLWindow2_get_location(IHTMLWindow2 *iface, IHTMLLocation **p)
249 {
250     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
251     FIXME("(%p)->(%p)\n", This, p);
252     return E_NOTIMPL;
253 }
254
255 static HRESULT WINAPI HTMLWindow2_get_history(IHTMLWindow2 *iface, IOmHistory **p)
256 {
257     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
258     FIXME("(%p)->(%p)\n", This, p);
259     return E_NOTIMPL;
260 }
261
262 static HRESULT WINAPI HTMLWindow2_close(IHTMLWindow2 *iface)
263 {
264     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
265     FIXME("(%p)->()\n", This);
266     return E_NOTIMPL;
267 }
268
269 static HRESULT WINAPI HTMLWindow2_put_opener(IHTMLWindow2 *iface, VARIANT v)
270 {
271     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
272     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
273     return E_NOTIMPL;
274 }
275
276 static HRESULT WINAPI HTMLWindow2_get_opener(IHTMLWindow2 *iface, VARIANT *p)
277 {
278     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
279     FIXME("(%p)->(%p)\n", This, p);
280     return E_NOTIMPL;
281 }
282
283 static HRESULT WINAPI HTMLWindow2_get_navigator(IHTMLWindow2 *iface, IOmNavigator **p)
284 {
285     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
286
287     TRACE("(%p)->(%p)\n", This, p);
288
289     *p = OmNavigator_Create();
290     return S_OK;
291 }
292
293 static HRESULT WINAPI HTMLWindow2_put_name(IHTMLWindow2 *iface, BSTR v)
294 {
295     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
296     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
297     return E_NOTIMPL;
298 }
299
300 static HRESULT WINAPI HTMLWindow2_get_name(IHTMLWindow2 *iface, BSTR *p)
301 {
302     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
303     FIXME("(%p)->(%p)\n", This, p);
304     return E_NOTIMPL;
305 }
306
307 static HRESULT WINAPI HTMLWindow2_get_parent(IHTMLWindow2 *iface, IHTMLWindow2 **p)
308 {
309     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
310     FIXME("(%p)->(%p)\n", This, p);
311     return E_NOTIMPL;
312 }
313
314 static HRESULT WINAPI HTMLWindow2_open(IHTMLWindow2 *iface, BSTR url, BSTR name,
315          BSTR features, VARIANT_BOOL replace, IHTMLWindow2 **pomWindowResult)
316 {
317     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
318     FIXME("(%p)->(%s %s %s %x %p)\n", This, debugstr_w(url), debugstr_w(name),
319           debugstr_w(features), replace, pomWindowResult);
320     return E_NOTIMPL;
321 }
322
323 static HRESULT WINAPI HTMLWindow2_get_self(IHTMLWindow2 *iface, IHTMLWindow2 **p)
324 {
325     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
326
327     TRACE("(%p)->(%p)\n", This, p);
328
329     /* FIXME: We should return kind of proxy window here. */
330     IHTMLWindow2_AddRef(HTMLWINDOW2(This));
331     *p = HTMLWINDOW2(This);
332     return S_OK;
333 }
334
335 static HRESULT WINAPI HTMLWindow2_get_top(IHTMLWindow2 *iface, IHTMLWindow2 **p)
336 {
337     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
338     FIXME("(%p)->(%p)\n", This, p);
339     return E_NOTIMPL;
340 }
341
342 static HRESULT WINAPI HTMLWindow2_get_window(IHTMLWindow2 *iface, IHTMLWindow2 **p)
343 {
344     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
345
346     TRACE("(%p)->(%p)\n", This, p);
347
348     /* FIXME: We should return kind of proxy window here. */
349     IHTMLWindow2_AddRef(HTMLWINDOW2(This));
350     *p = HTMLWINDOW2(This);
351     return S_OK;
352 }
353
354 static HRESULT WINAPI HTMLWindow2_navigate(IHTMLWindow2 *iface, BSTR url)
355 {
356     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
357     FIXME("(%p)->(%s)\n", This, debugstr_w(url));
358     return E_NOTIMPL;
359 }
360
361 static HRESULT WINAPI HTMLWindow2_put_onfocus(IHTMLWindow2 *iface, VARIANT v)
362 {
363     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
364     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
365     return E_NOTIMPL;
366 }
367
368 static HRESULT WINAPI HTMLWindow2_get_onfocus(IHTMLWindow2 *iface, VARIANT *p)
369 {
370     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
371     FIXME("(%p)->(%p)\n", This, p);
372     return E_NOTIMPL;
373 }
374
375 static HRESULT WINAPI HTMLWindow2_put_onblur(IHTMLWindow2 *iface, VARIANT v)
376 {
377     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
378     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
379     return E_NOTIMPL;
380 }
381
382 static HRESULT WINAPI HTMLWindow2_get_onblur(IHTMLWindow2 *iface, VARIANT *p)
383 {
384     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
385     FIXME("(%p)->(%p)\n", This, p);
386     return E_NOTIMPL;
387 }
388
389 static HRESULT WINAPI HTMLWindow2_put_onload(IHTMLWindow2 *iface, VARIANT v)
390 {
391     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
392     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
393     return E_NOTIMPL;
394 }
395
396 static HRESULT WINAPI HTMLWindow2_get_onload(IHTMLWindow2 *iface, VARIANT *p)
397 {
398     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
399     FIXME("(%p)->(%p)\n", This, p);
400     return E_NOTIMPL;
401 }
402
403 static HRESULT WINAPI HTMLWindow2_put_onbeforeunload(IHTMLWindow2 *iface, VARIANT v)
404 {
405     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
406     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
407     return E_NOTIMPL;
408 }
409
410 static HRESULT WINAPI HTMLWindow2_get_onbeforeunload(IHTMLWindow2 *iface, VARIANT *p)
411 {
412     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
413     FIXME("(%p)->(%p)\n", This, p);
414     return E_NOTIMPL;
415 }
416
417 static HRESULT WINAPI HTMLWindow2_put_onunload(IHTMLWindow2 *iface, VARIANT v)
418 {
419     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
420     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
421     return E_NOTIMPL;
422 }
423
424 static HRESULT WINAPI HTMLWindow2_get_onunload(IHTMLWindow2 *iface, VARIANT *p)
425 {
426     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
427     FIXME("(%p)->(%p)\n", This, p);
428     return E_NOTIMPL;
429 }
430
431 static HRESULT WINAPI HTMLWindow2_put_onhelp(IHTMLWindow2 *iface, VARIANT v)
432 {
433     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
434     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
435     return E_NOTIMPL;
436 }
437
438 static HRESULT WINAPI HTMLWindow2_get_onhelp(IHTMLWindow2 *iface, VARIANT *p)
439 {
440     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
441     FIXME("(%p)->(%p)\n", This, p);
442     return E_NOTIMPL;
443 }
444
445 static HRESULT WINAPI HTMLWindow2_put_onerror(IHTMLWindow2 *iface, VARIANT v)
446 {
447     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
448     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
449     return E_NOTIMPL;
450 }
451
452 static HRESULT WINAPI HTMLWindow2_get_onerror(IHTMLWindow2 *iface, VARIANT *p)
453 {
454     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
455     FIXME("(%p)->(%p)\n", This, p);
456     return E_NOTIMPL;
457 }
458
459 static HRESULT WINAPI HTMLWindow2_put_onresize(IHTMLWindow2 *iface, VARIANT v)
460 {
461     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
462     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
463     return E_NOTIMPL;
464 }
465
466 static HRESULT WINAPI HTMLWindow2_get_onresize(IHTMLWindow2 *iface, VARIANT *p)
467 {
468     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
469     FIXME("(%p)->(%p)\n", This, p);
470     return E_NOTIMPL;
471 }
472
473 static HRESULT WINAPI HTMLWindow2_put_onscroll(IHTMLWindow2 *iface, VARIANT v)
474 {
475     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
476     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
477     return E_NOTIMPL;
478 }
479
480 static HRESULT WINAPI HTMLWindow2_get_onscroll(IHTMLWindow2 *iface, VARIANT *p)
481 {
482     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
483     FIXME("(%p)->(%p)\n", This, p);
484     return E_NOTIMPL;
485 }
486
487 static HRESULT WINAPI HTMLWindow2_get_document(IHTMLWindow2 *iface, IHTMLDocument2 **p)
488 {
489     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
490
491     TRACE("(%p)->(%p)\n", This, p);
492
493     if(This->doc) {
494         /* FIXME: We should return a wrapper object here */
495         *p = HTMLDOC(This->doc);
496         IHTMLDocument2_AddRef(*p);
497     }else {
498         *p = NULL;
499     }
500
501     return S_OK;
502 }
503
504 static HRESULT WINAPI HTMLWindow2_get_event(IHTMLWindow2 *iface, IHTMLEventObj **p)
505 {
506     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
507     FIXME("(%p)->(%p)\n", This, p);
508     return E_NOTIMPL;
509 }
510
511 static HRESULT WINAPI HTMLWindow2_get__newEnum(IHTMLWindow2 *iface, IUnknown **p)
512 {
513     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
514     FIXME("(%p)->(%p)\n", This, p);
515     return E_NOTIMPL;
516 }
517
518 static HRESULT WINAPI HTMLWindow2_showModalDialog(IHTMLWindow2 *iface, BSTR dialog,
519         VARIANT *varArgIn, VARIANT *varOptions, VARIANT *varArgOut)
520 {
521     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
522     FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(dialog), varArgIn, varOptions, varArgOut);
523     return E_NOTIMPL;
524 }
525
526 static HRESULT WINAPI HTMLWindow2_showHelp(IHTMLWindow2 *iface, BSTR helpURL, VARIANT helpArg,
527         BSTR features)
528 {
529     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
530     FIXME("(%p)->(%s v(%d) %s)\n", This, debugstr_w(helpURL), V_VT(&helpArg), debugstr_w(features));
531     return E_NOTIMPL;
532 }
533
534 static HRESULT WINAPI HTMLWindow2_get_screen(IHTMLWindow2 *iface, IHTMLScreen **p)
535 {
536     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
537     FIXME("(%p)->(%p)\n", This, p);
538     return E_NOTIMPL;
539 }
540
541 static HRESULT WINAPI HTMLWindow2_get_Option(IHTMLWindow2 *iface, IHTMLOptionElementFactory **p)
542 {
543     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
544
545     TRACE("(%p)->(%p)\n", This, p);
546
547     if(!This->doc->option_factory)
548         This->doc->option_factory = HTMLOptionElementFactory_Create(This->doc);
549
550     *p = HTMLOPTFACTORY(This->doc->option_factory);
551     IHTMLOptionElementFactory_AddRef(*p);
552
553     return S_OK;
554 }
555
556 static HRESULT WINAPI HTMLWindow2_focus(IHTMLWindow2 *iface)
557 {
558     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
559     FIXME("(%p)->()\n", This);
560     return E_NOTIMPL;
561 }
562
563 static HRESULT WINAPI HTMLWindow2_get_closed(IHTMLWindow2 *iface, VARIANT_BOOL *p)
564 {
565     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
566     FIXME("(%p)->(%p)\n", This, p);
567     return E_NOTIMPL;
568 }
569
570 static HRESULT WINAPI HTMLWindow2_blur(IHTMLWindow2 *iface)
571 {
572     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
573     FIXME("(%p)->()\n", This);
574     return E_NOTIMPL;
575 }
576
577 static HRESULT WINAPI HTMLWindow2_scroll(IHTMLWindow2 *iface, long x, long y)
578 {
579     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
580     FIXME("(%p)->(%ld %ld)\n", This, x, y);
581     return E_NOTIMPL;
582 }
583
584 static HRESULT WINAPI HTMLWindow2_get_clientInformation(IHTMLWindow2 *iface, IOmNavigator **p)
585 {
586     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
587     FIXME("(%p)->(%p)\n", This, p);
588     return E_NOTIMPL;
589 }
590
591 static HRESULT WINAPI HTMLWindow2_setInterval(IHTMLWindow2 *iface, BSTR expression,
592         long msec, VARIANT *language, long *timerID)
593 {
594     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
595     VARIANT expr;
596
597     TRACE("(%p)->(%s %ld %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
598
599     V_VT(&expr) = VT_BSTR;
600     V_BSTR(&expr) = expression;
601     return IHTMLWindow3_setInterval(HTMLWINDOW3(This), &expr, msec, language, timerID);
602 }
603
604 static HRESULT WINAPI HTMLWindow2_clearInterval(IHTMLWindow2 *iface, long timerID)
605 {
606     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
607
608     TRACE("(%p)->(%ld)\n", This, timerID);
609
610     return clear_task_timer(This->doc, TRUE, timerID);
611 }
612
613 static HRESULT WINAPI HTMLWindow2_put_offscreenBuffering(IHTMLWindow2 *iface, VARIANT v)
614 {
615     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
616     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
617     return E_NOTIMPL;
618 }
619
620 static HRESULT WINAPI HTMLWindow2_get_offscreenBuffering(IHTMLWindow2 *iface, VARIANT *p)
621 {
622     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
623     FIXME("(%p)->(%p)\n", This, p);
624     return E_NOTIMPL;
625 }
626
627 static HRESULT WINAPI HTMLWindow2_execScript(IHTMLWindow2 *iface, BSTR scode, BSTR language,
628         VARIANT *pvarRet)
629 {
630     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
631     FIXME("(%p)->(%s %s %p)\n", This, debugstr_w(scode), debugstr_w(language), pvarRet);
632     return E_NOTIMPL;
633 }
634
635 static HRESULT WINAPI HTMLWindow2_toString(IHTMLWindow2 *iface, BSTR *String)
636 {
637     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
638     FIXME("(%p)->(%p)\n", This, String);
639     return E_NOTIMPL;
640 }
641
642 static HRESULT WINAPI HTMLWindow2_scrollBy(IHTMLWindow2 *iface, long x, long y)
643 {
644     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
645     nsresult nsres;
646
647     TRACE("(%p)->(%ld %ld)\n", This, x, y);
648
649     nsres = nsIDOMWindow_ScrollBy(This->nswindow, x, y);
650     if(NS_FAILED(nsres))
651         ERR("ScrollBy failed: %08x\n", nsres);
652
653     return S_OK;
654 }
655
656 static HRESULT WINAPI HTMLWindow2_scrollTo(IHTMLWindow2 *iface, long x, long y)
657 {
658     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
659     nsresult nsres;
660
661     TRACE("(%p)->(%ld %ld)\n", This, x, y);
662
663     nsres = nsIDOMWindow_ScrollTo(This->nswindow, x, y);
664     if(NS_FAILED(nsres))
665         ERR("ScrollTo failed: %08x\n", nsres);
666
667     return S_OK;
668 }
669
670 static HRESULT WINAPI HTMLWindow2_moveTo(IHTMLWindow2 *iface, long x, long y)
671 {
672     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
673     FIXME("(%p)->(%ld %ld)\n", This, x, y);
674     return E_NOTIMPL;
675 }
676
677 static HRESULT WINAPI HTMLWindow2_moveBy(IHTMLWindow2 *iface, long x, long y)
678 {
679     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
680     FIXME("(%p)->(%ld %ld)\n", This, x, y);
681     return E_NOTIMPL;
682 }
683
684 static HRESULT WINAPI HTMLWindow2_resizeTo(IHTMLWindow2 *iface, long x, long y)
685 {
686     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
687     FIXME("(%p)->(%ld %ld)\n", This, x, y);
688     return E_NOTIMPL;
689 }
690
691 static HRESULT WINAPI HTMLWindow2_resizeBy(IHTMLWindow2 *iface, long x, long y)
692 {
693     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
694     FIXME("(%p)->(%ld %ld)\n", This, x, y);
695     return E_NOTIMPL;
696 }
697
698 static HRESULT WINAPI HTMLWindow2_get_external(IHTMLWindow2 *iface, IDispatch **p)
699 {
700     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
701
702     TRACE("(%p)->(%p)\n", This, p);
703
704     *p = NULL;
705
706     if(!This->doc->hostui)
707         return S_OK;
708
709     return IDocHostUIHandler_GetExternal(This->doc->hostui, p);
710 }
711
712 #undef HTMLWINDOW2_THIS
713
714 static const IHTMLWindow2Vtbl HTMLWindow2Vtbl = {
715     HTMLWindow2_QueryInterface,
716     HTMLWindow2_AddRef,
717     HTMLWindow2_Release,
718     HTMLWindow2_GetTypeInfoCount,
719     HTMLWindow2_GetTypeInfo,
720     HTMLWindow2_GetIDsOfNames,
721     HTMLWindow2_Invoke,
722     HTMLWindow2_item,
723     HTMLWindow2_get_length,
724     HTMLWindow2_get_frames,
725     HTMLWindow2_put_defaultStatus,
726     HTMLWindow2_get_defaultStatus,
727     HTMLWindow2_put_status,
728     HTMLWindow2_get_status,
729     HTMLWindow2_setTimeout,
730     HTMLWindow2_clearTimeout,
731     HTMLWindow2_alert,
732     HTMLWindow2_confirm,
733     HTMLWindow2_prompt,
734     HTMLWindow2_get_Image,
735     HTMLWindow2_get_location,
736     HTMLWindow2_get_history,
737     HTMLWindow2_close,
738     HTMLWindow2_put_opener,
739     HTMLWindow2_get_opener,
740     HTMLWindow2_get_navigator,
741     HTMLWindow2_put_name,
742     HTMLWindow2_get_name,
743     HTMLWindow2_get_parent,
744     HTMLWindow2_open,
745     HTMLWindow2_get_self,
746     HTMLWindow2_get_top,
747     HTMLWindow2_get_window,
748     HTMLWindow2_navigate,
749     HTMLWindow2_put_onfocus,
750     HTMLWindow2_get_onfocus,
751     HTMLWindow2_put_onblur,
752     HTMLWindow2_get_onblur,
753     HTMLWindow2_put_onload,
754     HTMLWindow2_get_onload,
755     HTMLWindow2_put_onbeforeunload,
756     HTMLWindow2_get_onbeforeunload,
757     HTMLWindow2_put_onunload,
758     HTMLWindow2_get_onunload,
759     HTMLWindow2_put_onhelp,
760     HTMLWindow2_get_onhelp,
761     HTMLWindow2_put_onerror,
762     HTMLWindow2_get_onerror,
763     HTMLWindow2_put_onresize,
764     HTMLWindow2_get_onresize,
765     HTMLWindow2_put_onscroll,
766     HTMLWindow2_get_onscroll,
767     HTMLWindow2_get_document,
768     HTMLWindow2_get_event,
769     HTMLWindow2_get__newEnum,
770     HTMLWindow2_showModalDialog,
771     HTMLWindow2_showHelp,
772     HTMLWindow2_get_screen,
773     HTMLWindow2_get_Option,
774     HTMLWindow2_focus,
775     HTMLWindow2_get_closed,
776     HTMLWindow2_blur,
777     HTMLWindow2_scroll,
778     HTMLWindow2_get_clientInformation,
779     HTMLWindow2_setInterval,
780     HTMLWindow2_clearInterval,
781     HTMLWindow2_put_offscreenBuffering,
782     HTMLWindow2_get_offscreenBuffering,
783     HTMLWindow2_execScript,
784     HTMLWindow2_toString,
785     HTMLWindow2_scrollBy,
786     HTMLWindow2_scrollTo,
787     HTMLWindow2_moveTo,
788     HTMLWindow2_moveBy,
789     HTMLWindow2_resizeTo,
790     HTMLWindow2_resizeBy,
791     HTMLWindow2_get_external
792 };
793
794 #define HTMLWINDOW3_THIS(iface) DEFINE_THIS(HTMLWindow, HTMLWindow3, iface)
795
796 static HRESULT WINAPI HTMLWindow3_QueryInterface(IHTMLWindow3 *iface, REFIID riid, void **ppv)
797 {
798     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
799
800     return IHTMLWindow2_QueryInterface(HTMLWINDOW2(This), riid, ppv);
801 }
802
803 static ULONG WINAPI HTMLWindow3_AddRef(IHTMLWindow3 *iface)
804 {
805     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
806
807     return IHTMLWindow2_AddRef(HTMLWINDOW2(This));
808 }
809
810 static ULONG WINAPI HTMLWindow3_Release(IHTMLWindow3 *iface)
811 {
812     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
813
814     return IHTMLWindow2_Release(HTMLWINDOW2(This));
815 }
816
817 static HRESULT WINAPI HTMLWindow3_GetTypeInfoCount(IHTMLWindow3 *iface, UINT *pctinfo)
818 {
819     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
820
821     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(This), pctinfo);
822 }
823
824 static HRESULT WINAPI HTMLWindow3_GetTypeInfo(IHTMLWindow3 *iface, UINT iTInfo,
825                                               LCID lcid, ITypeInfo **ppTInfo)
826 {
827     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
828
829     return IDispatchEx_GetTypeInfo(DISPATCHEX(This), iTInfo, lcid, ppTInfo);
830 }
831
832 static HRESULT WINAPI HTMLWindow3_GetIDsOfNames(IHTMLWindow3 *iface, REFIID riid,
833                                                 LPOLESTR *rgszNames, UINT cNames,
834                                                 LCID lcid, DISPID *rgDispId)
835 {
836     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
837
838     return IDispatchEx_GetIDsOfNames(DISPATCHEX(This), riid, rgszNames, cNames, lcid, rgDispId);
839 }
840
841 static HRESULT WINAPI HTMLWindow3_Invoke(IHTMLWindow3 *iface, DISPID dispIdMember,
842                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
843                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
844 {
845     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
846
847     return IDispatchEx_Invoke(DISPATCHEX(This), dispIdMember, riid, lcid, wFlags, pDispParams,
848             pVarResult, pExcepInfo, puArgErr);
849 }
850
851 static HRESULT WINAPI HTMLWindow3_get_screenLeft(IHTMLWindow3 *iface, long *p)
852 {
853     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
854     FIXME("(%p)->(%p)\n", This, p);
855     return E_NOTIMPL;
856 }
857
858 static HRESULT WINAPI HTMLWindow3_get_screenTop(IHTMLWindow3 *iface, long *p)
859 {
860     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
861     FIXME("(%p)->(%p)\n", This, p);
862     return E_NOTIMPL;
863 }
864
865 static HRESULT WINAPI HTMLWindow3_attachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp, VARIANT_BOOL *pfResult)
866 {
867     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
868     FIXME("(%p)->(%s %p %p)\n", This, debugstr_w(event), pDisp, pfResult);
869     return E_NOTIMPL;
870 }
871
872 static HRESULT WINAPI HTMLWindow3_detachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp)
873 {
874     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
875     FIXME("(%p)->()\n", This);
876     return E_NOTIMPL;
877 }
878
879 static HRESULT window_set_timer(HTMLWindow *This, VARIANT *expr, long msec, VARIANT *language,
880         BOOL interval, long *timer_id)
881 {
882     IDispatch *disp = NULL;
883
884     switch(V_VT(expr)) {
885     case VT_DISPATCH:
886         disp = V_DISPATCH(expr);
887         IDispatch_AddRef(disp);
888         break;
889
890     case VT_BSTR:
891         disp = script_parse_event(This->doc, V_BSTR(expr));
892         break;
893
894     default:
895         FIXME("unimplemented vt=%d\n", V_VT(expr));
896         return E_NOTIMPL;
897     }
898
899     if(!disp)
900         return E_FAIL;
901
902     *timer_id = set_task_timer(This->doc, msec, interval, disp);
903     IDispatch_Release(disp);
904
905     return S_OK;
906 }
907
908 static HRESULT WINAPI HTMLWindow3_setTimeout(IHTMLWindow3 *iface, VARIANT *expression, long msec,
909         VARIANT *language, long *timerID)
910 {
911     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
912
913     TRACE("(%p)->(%p(%d) %ld %p %p)\n", This, expression, V_VT(expression), msec, language, timerID);
914
915     return window_set_timer(This, expression, msec, language, FALSE, timerID);
916 }
917
918 static HRESULT WINAPI HTMLWindow3_setInterval(IHTMLWindow3 *iface, VARIANT *expression, long msec,
919         VARIANT *language, long *timerID)
920 {
921     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
922
923     TRACE("(%p)->(%p %ld %p %p)\n", This, expression, msec, language, timerID);
924
925     return window_set_timer(This, expression, msec, language, TRUE, timerID);
926 }
927
928 static HRESULT WINAPI HTMLWindow3_print(IHTMLWindow3 *iface)
929 {
930     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
931     FIXME("(%p)\n", This);
932     return E_NOTIMPL;
933 }
934
935 static HRESULT WINAPI HTMLWindow3_put_onbeforeprint(IHTMLWindow3 *iface, VARIANT v)
936 {
937     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
938     FIXME("(%p)->()\n", This);
939     return E_NOTIMPL;
940 }
941
942 static HRESULT WINAPI HTMLWindow3_get_onbeforeprint(IHTMLWindow3 *iface, VARIANT *p)
943 {
944     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
945     FIXME("(%p)->(%p)\n", This, p);
946     return E_NOTIMPL;
947 }
948
949 static HRESULT WINAPI HTMLWindow3_put_onafterprint(IHTMLWindow3 *iface, VARIANT v)
950 {
951     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
952     FIXME("(%p)->()\n", This);
953     return E_NOTIMPL;
954 }
955
956 static HRESULT WINAPI HTMLWindow3_get_onafterprint(IHTMLWindow3 *iface, VARIANT *p)
957 {
958     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
959     FIXME("(%p)->(%p)\n", This, p);
960     return E_NOTIMPL;
961 }
962
963 static HRESULT WINAPI HTMLWindow3_get_clipboardData(IHTMLWindow3 *iface, IHTMLDataTransfer **p)
964 {
965     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
966     FIXME("(%p)->(%p)\n", This, p);
967     return E_NOTIMPL;
968 }
969
970 static HRESULT WINAPI HTMLWindow3_showModelessDialog(IHTMLWindow3 *iface, BSTR url,
971         VARIANT *varArgIn, VARIANT *options, IHTMLWindow2 **pDialog)
972 {
973     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
974     FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(url), varArgIn, options, pDialog);
975     return E_NOTIMPL;
976 }
977
978 #undef HTMLWINDOW3_THIS
979
980 static const IHTMLWindow3Vtbl HTMLWindow3Vtbl = {
981     HTMLWindow3_QueryInterface,
982     HTMLWindow3_AddRef,
983     HTMLWindow3_Release,
984     HTMLWindow3_GetTypeInfoCount,
985     HTMLWindow3_GetTypeInfo,
986     HTMLWindow3_GetIDsOfNames,
987     HTMLWindow3_Invoke,
988     HTMLWindow3_get_screenLeft,
989     HTMLWindow3_get_screenTop,
990     HTMLWindow3_attachEvent,
991     HTMLWindow3_detachEvent,
992     HTMLWindow3_setTimeout,
993     HTMLWindow3_setInterval,
994     HTMLWindow3_print,
995     HTMLWindow3_put_onbeforeprint,
996     HTMLWindow3_get_onbeforeprint,
997     HTMLWindow3_put_onafterprint,
998     HTMLWindow3_get_onafterprint,
999     HTMLWindow3_get_clipboardData,
1000     HTMLWindow3_showModelessDialog
1001 };
1002
1003 #define DISPEX_THIS(iface) DEFINE_THIS(HTMLWindow, IDispatchEx, iface)
1004
1005 static HRESULT WINAPI WindowDispEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1006 {
1007     HTMLWindow *This = DISPEX_THIS(iface);
1008
1009     return IHTMLWindow2_QueryInterface(HTMLWINDOW2(This), riid, ppv);
1010 }
1011
1012 static ULONG WINAPI WindowDispEx_AddRef(IDispatchEx *iface)
1013 {
1014     HTMLWindow *This = DISPEX_THIS(iface);
1015
1016     return IHTMLWindow2_AddRef(HTMLWINDOW2(This));
1017 }
1018
1019 static ULONG WINAPI WindowDispEx_Release(IDispatchEx *iface)
1020 {
1021     HTMLWindow *This = DISPEX_THIS(iface);
1022
1023     return IHTMLWindow2_Release(HTMLWINDOW2(This));
1024 }
1025
1026 static HRESULT WINAPI WindowDispEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1027 {
1028     HTMLWindow *This = DISPEX_THIS(iface);
1029
1030     TRACE("(%p)->(%p)\n", This, pctinfo);
1031
1032     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
1033 }
1034
1035 static HRESULT WINAPI WindowDispEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1036                                                LCID lcid, ITypeInfo **ppTInfo)
1037 {
1038     HTMLWindow *This = DISPEX_THIS(iface);
1039
1040     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1041
1042     return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
1043 }
1044
1045 static HRESULT WINAPI WindowDispEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1046                                                  LPOLESTR *rgszNames, UINT cNames,
1047                                                  LCID lcid, DISPID *rgDispId)
1048 {
1049     HTMLWindow *This = DISPEX_THIS(iface);
1050
1051     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1052           lcid, rgDispId);
1053
1054     /* FIXME: Use script dispatch */
1055
1056     return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
1057 }
1058
1059 static HRESULT WINAPI WindowDispEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1060                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1061                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1062 {
1063     HTMLWindow *This = DISPEX_THIS(iface);
1064
1065     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1066           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1067
1068     /* FIXME: Use script dispatch */
1069
1070     return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid, wFlags, pDispParams,
1071                               pVarResult, pExcepInfo, puArgErr);
1072 }
1073
1074 static HRESULT WINAPI WindowDispEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1075 {
1076     HTMLWindow *This = DISPEX_THIS(iface);
1077
1078     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1079
1080     return IDispatchEx_GetDispID(DISPATCHEX(&This->dispex), bstrName, grfdex, pid);
1081 }
1082
1083 static HRESULT WINAPI WindowDispEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1084         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1085 {
1086     HTMLWindow *This = DISPEX_THIS(iface);
1087
1088     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1089
1090     return IDispatchEx_InvokeEx(DISPATCHEX(&This->dispex), id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1091 }
1092
1093 static HRESULT WINAPI WindowDispEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1094 {
1095     HTMLWindow *This = DISPEX_THIS(iface);
1096
1097     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1098
1099     return IDispatchEx_DeleteMemberByName(DISPATCHEX(&This->dispex), bstrName, grfdex);
1100 }
1101
1102 static HRESULT WINAPI WindowDispEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1103 {
1104     HTMLWindow *This = DISPEX_THIS(iface);
1105
1106     TRACE("(%p)->(%x)\n", This, id);
1107
1108     return IDispatchEx_DeleteMemberByDispID(DISPATCHEX(&This->dispex), id);
1109 }
1110
1111 static HRESULT WINAPI WindowDispEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1112 {
1113     HTMLWindow *This = DISPEX_THIS(iface);
1114
1115     TRACE("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1116
1117     return IDispatchEx_GetMemberProperties(DISPATCHEX(&This->dispex), id, grfdexFetch, pgrfdex);
1118 }
1119
1120 static HRESULT WINAPI WindowDispEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1121 {
1122     HTMLWindow *This = DISPEX_THIS(iface);
1123
1124     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1125
1126     return IDispatchEx_GetMemberName(DISPATCHEX(&This->dispex), id, pbstrName);
1127 }
1128
1129 static HRESULT WINAPI WindowDispEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1130 {
1131     HTMLWindow *This = DISPEX_THIS(iface);
1132
1133     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1134
1135     return IDispatchEx_GetNextDispID(DISPATCHEX(&This->dispex), grfdex, id, pid);
1136 }
1137
1138 static HRESULT WINAPI WindowDispEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1139 {
1140     HTMLWindow *This = DISPEX_THIS(iface);
1141
1142     TRACE("(%p)->(%p)\n", This, ppunk);
1143
1144     *ppunk = NULL;
1145     return S_OK;
1146 }
1147
1148 #undef DISPEX_THIS
1149
1150 static const IDispatchExVtbl WindowDispExVtbl = {
1151     WindowDispEx_QueryInterface,
1152     WindowDispEx_AddRef,
1153     WindowDispEx_Release,
1154     WindowDispEx_GetTypeInfoCount,
1155     WindowDispEx_GetTypeInfo,
1156     WindowDispEx_GetIDsOfNames,
1157     WindowDispEx_Invoke,
1158     WindowDispEx_GetDispID,
1159     WindowDispEx_InvokeEx,
1160     WindowDispEx_DeleteMemberByName,
1161     WindowDispEx_DeleteMemberByDispID,
1162     WindowDispEx_GetMemberProperties,
1163     WindowDispEx_GetMemberName,
1164     WindowDispEx_GetNextDispID,
1165     WindowDispEx_GetNameSpaceParent
1166 };
1167
1168 static const tid_t HTMLWindow_iface_tids[] = {
1169     IHTMLWindow2_tid,
1170     IHTMLWindow3_tid,
1171     0
1172 };
1173 static dispex_static_data_t HTMLWindow_dispex = {
1174     NULL,
1175     DispHTMLWindow2_tid,
1176     NULL,
1177     HTMLWindow_iface_tids
1178 };
1179
1180 static const char wineConfig_func[] =
1181 "window.__defineGetter__(\"external\",function() {\n"
1182 "    return window.__wineWindow__.external;\n"
1183 "});\n"
1184 "window.__wineWindow__ = wineWindow;\n";
1185
1186 static void astr_to_nswstr(const char *str, nsAString *nsstr)
1187 {
1188     LPWSTR wstr;
1189     int len;
1190
1191     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
1192     wstr = heap_alloc(len*sizeof(WCHAR));
1193     MultiByteToWideChar(CP_ACP, 0, str, -1, wstr, len);
1194
1195     nsAString_Init(nsstr, wstr);
1196
1197     heap_free(wstr);
1198 }
1199
1200 static nsresult call_js_func(nsIScriptContainer *script_container, nsISupports *target,
1201                              const char *name, const char *body,
1202                              PRUint32 argc, const char **arg_names, nsIArray *argv)
1203 {
1204     nsACString name_str;
1205     nsAString body_str;
1206     JSObject func_obj, jsglobal;
1207     nsIVariant *jsret;
1208     nsresult nsres;
1209
1210     nsres = nsIScriptContainer_GetGlobalObject(script_container, &jsglobal);
1211     if(NS_FAILED(nsres))
1212         ERR("GetGlobalObject: %08x\n", nsres);
1213
1214     nsACString_Init(&name_str, name);
1215     astr_to_nswstr(body, &body_str);
1216
1217     nsres =  nsIScriptContainer_CompileFunction(script_container, jsglobal, &name_str, argc, arg_names,
1218                                                 &body_str, NULL, 1, FALSE, &func_obj);
1219
1220     nsACString_Finish(&name_str);
1221     nsAString_Finish(&body_str);
1222
1223     if(NS_FAILED(nsres)) {
1224         ERR("CompileFunction failed: %08x\n", nsres);
1225         return nsres;
1226     }
1227
1228     nsres = nsIScriptContainer_CallFunction(script_container, target, jsglobal, func_obj, argv, &jsret);
1229
1230     nsIScriptContainer_DropScriptObject(script_container, func_obj);
1231     nsIScriptContainer_DropScriptObject(script_container, jsglobal);
1232     if(NS_FAILED(nsres)) {
1233         ERR("CallFunction failed: %08x\n", nsres);
1234         return nsres;
1235     }
1236
1237     nsIVariant_Release(jsret);
1238     return NS_OK;
1239 }
1240
1241 void setup_nswindow(HTMLWindow *This)
1242 {
1243     nsIScriptContainer *script_container;
1244     nsIDOMWindow *nswindow;
1245     nsIDOMDocument *domdoc;
1246     nsIWritableVariant *nsvar;
1247     nsIMutableArray *argv;
1248     nsresult nsres;
1249
1250     static const char *args[] = {"wineWindow"};
1251
1252     TRACE("(%p)\n", This);
1253
1254     nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &domdoc);
1255     nsres = nsIDOMDocument_QueryInterface(domdoc, &IID_nsIScriptContainer, (void**)&script_container);
1256     nsIDOMDocument_Release(domdoc);
1257     if(NS_FAILED(nsres)) {
1258         TRACE("Could not get nsIDOMScriptContainer: %08x\n", nsres);
1259         return;
1260     }
1261
1262     nsIWebBrowser_GetContentDOMWindow(This->doc->nscontainer->webbrowser, &nswindow);
1263
1264     nsvar = create_nsvariant();
1265     nsres = nsIWritableVariant_SetAsInterface(nsvar, &IID_IDispatch, HTMLWINDOW2(This));
1266     if(NS_FAILED(nsres))
1267         ERR("SetAsInterface failed: %08x\n", nsres);
1268
1269     argv = create_nsarray();
1270     nsres = nsIMutableArray_AppendElement(argv, (nsISupports*)nsvar, FALSE);
1271     nsIWritableVariant_Release(nsvar);
1272     if(NS_FAILED(nsres))
1273         ERR("AppendElement failed: %08x\n", nsres);
1274
1275     call_js_func(script_container, (nsISupports*)nswindow/*HTMLWINDOW2(This)*/, "wineConfig",
1276                  wineConfig_func, 1, args, (nsIArray*)argv);
1277
1278     nsIMutableArray_Release(argv);
1279     nsIScriptContainer_Release(script_container);
1280 }
1281
1282 HTMLWindow *HTMLWindow_Create(HTMLDocument *doc)
1283 {
1284     HTMLWindow *ret = heap_alloc_zero(sizeof(HTMLWindow));
1285
1286     ret->lpHTMLWindow2Vtbl = &HTMLWindow2Vtbl;
1287     ret->lpHTMLWindow3Vtbl = &HTMLWindow3Vtbl;
1288     ret->lpIDispatchExVtbl = &WindowDispExVtbl;
1289     ret->ref = 1;
1290     ret->doc = doc;
1291
1292     init_dispex(&ret->dispex, (IUnknown*)HTMLWINDOW2(ret), &HTMLWindow_dispex);
1293
1294     if(doc->nscontainer) {
1295         nsresult nsres;
1296
1297         nsres = nsIWebBrowser_GetContentDOMWindow(doc->nscontainer->webbrowser, &ret->nswindow);
1298         if(NS_FAILED(nsres))
1299             ERR("GetContentDOMWindow failed: %08x\n", nsres);
1300     }
1301
1302     list_add_head(&window_list, &ret->entry);
1303
1304     return ret;
1305 }
1306
1307 HTMLWindow *nswindow_to_window(const nsIDOMWindow *nswindow)
1308 {
1309     HTMLWindow *iter;
1310
1311     LIST_FOR_EACH_ENTRY(iter, &window_list, HTMLWindow, entry) {
1312         if(iter->nswindow == nswindow)
1313             return iter;
1314     }
1315
1316     return NULL;
1317 }