mshtml: Add HTMLWindow_scrollTo implementation.
[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     FIXME("(%p)->(%ld %ld)\n", This, x, y);
646     return E_NOTIMPL;
647 }
648
649 static HRESULT WINAPI HTMLWindow2_scrollTo(IHTMLWindow2 *iface, long x, long y)
650 {
651     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
652     TRACE("(%p)->(%ld %ld)\n", This, x, y);
653     return nsIDOMWindow_ScrollTo(This->nswindow, x, y);
654 }
655
656 static HRESULT WINAPI HTMLWindow2_moveTo(IHTMLWindow2 *iface, long x, long y)
657 {
658     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
659     FIXME("(%p)->(%ld %ld)\n", This, x, y);
660     return E_NOTIMPL;
661 }
662
663 static HRESULT WINAPI HTMLWindow2_moveBy(IHTMLWindow2 *iface, long x, long y)
664 {
665     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
666     FIXME("(%p)->(%ld %ld)\n", This, x, y);
667     return E_NOTIMPL;
668 }
669
670 static HRESULT WINAPI HTMLWindow2_resizeTo(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_resizeBy(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_get_external(IHTMLWindow2 *iface, IDispatch **p)
685 {
686     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
687
688     TRACE("(%p)->(%p)\n", This, p);
689
690     *p = NULL;
691
692     if(!This->doc->hostui)
693         return S_OK;
694
695     return IDocHostUIHandler_GetExternal(This->doc->hostui, p);
696 }
697
698 #undef HTMLWINDOW2_THIS
699
700 static const IHTMLWindow2Vtbl HTMLWindow2Vtbl = {
701     HTMLWindow2_QueryInterface,
702     HTMLWindow2_AddRef,
703     HTMLWindow2_Release,
704     HTMLWindow2_GetTypeInfoCount,
705     HTMLWindow2_GetTypeInfo,
706     HTMLWindow2_GetIDsOfNames,
707     HTMLWindow2_Invoke,
708     HTMLWindow2_item,
709     HTMLWindow2_get_length,
710     HTMLWindow2_get_frames,
711     HTMLWindow2_put_defaultStatus,
712     HTMLWindow2_get_defaultStatus,
713     HTMLWindow2_put_status,
714     HTMLWindow2_get_status,
715     HTMLWindow2_setTimeout,
716     HTMLWindow2_clearTimeout,
717     HTMLWindow2_alert,
718     HTMLWindow2_confirm,
719     HTMLWindow2_prompt,
720     HTMLWindow2_get_Image,
721     HTMLWindow2_get_location,
722     HTMLWindow2_get_history,
723     HTMLWindow2_close,
724     HTMLWindow2_put_opener,
725     HTMLWindow2_get_opener,
726     HTMLWindow2_get_navigator,
727     HTMLWindow2_put_name,
728     HTMLWindow2_get_name,
729     HTMLWindow2_get_parent,
730     HTMLWindow2_open,
731     HTMLWindow2_get_self,
732     HTMLWindow2_get_top,
733     HTMLWindow2_get_window,
734     HTMLWindow2_navigate,
735     HTMLWindow2_put_onfocus,
736     HTMLWindow2_get_onfocus,
737     HTMLWindow2_put_onblur,
738     HTMLWindow2_get_onblur,
739     HTMLWindow2_put_onload,
740     HTMLWindow2_get_onload,
741     HTMLWindow2_put_onbeforeunload,
742     HTMLWindow2_get_onbeforeunload,
743     HTMLWindow2_put_onunload,
744     HTMLWindow2_get_onunload,
745     HTMLWindow2_put_onhelp,
746     HTMLWindow2_get_onhelp,
747     HTMLWindow2_put_onerror,
748     HTMLWindow2_get_onerror,
749     HTMLWindow2_put_onresize,
750     HTMLWindow2_get_onresize,
751     HTMLWindow2_put_onscroll,
752     HTMLWindow2_get_onscroll,
753     HTMLWindow2_get_document,
754     HTMLWindow2_get_event,
755     HTMLWindow2_get__newEnum,
756     HTMLWindow2_showModalDialog,
757     HTMLWindow2_showHelp,
758     HTMLWindow2_get_screen,
759     HTMLWindow2_get_Option,
760     HTMLWindow2_focus,
761     HTMLWindow2_get_closed,
762     HTMLWindow2_blur,
763     HTMLWindow2_scroll,
764     HTMLWindow2_get_clientInformation,
765     HTMLWindow2_setInterval,
766     HTMLWindow2_clearInterval,
767     HTMLWindow2_put_offscreenBuffering,
768     HTMLWindow2_get_offscreenBuffering,
769     HTMLWindow2_execScript,
770     HTMLWindow2_toString,
771     HTMLWindow2_scrollBy,
772     HTMLWindow2_scrollTo,
773     HTMLWindow2_moveTo,
774     HTMLWindow2_moveBy,
775     HTMLWindow2_resizeTo,
776     HTMLWindow2_resizeBy,
777     HTMLWindow2_get_external
778 };
779
780 #define HTMLWINDOW3_THIS(iface) DEFINE_THIS(HTMLWindow, HTMLWindow3, iface)
781
782 static HRESULT WINAPI HTMLWindow3_QueryInterface(IHTMLWindow3 *iface, REFIID riid, void **ppv)
783 {
784     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
785
786     return IHTMLWindow2_QueryInterface(HTMLWINDOW2(This), riid, ppv);
787 }
788
789 static ULONG WINAPI HTMLWindow3_AddRef(IHTMLWindow3 *iface)
790 {
791     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
792
793     return IHTMLWindow2_AddRef(HTMLWINDOW2(This));
794 }
795
796 static ULONG WINAPI HTMLWindow3_Release(IHTMLWindow3 *iface)
797 {
798     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
799
800     return IHTMLWindow2_Release(HTMLWINDOW2(This));
801 }
802
803 static HRESULT WINAPI HTMLWindow3_GetTypeInfoCount(IHTMLWindow3 *iface, UINT *pctinfo)
804 {
805     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
806
807     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(This), pctinfo);
808 }
809
810 static HRESULT WINAPI HTMLWindow3_GetTypeInfo(IHTMLWindow3 *iface, UINT iTInfo,
811                                               LCID lcid, ITypeInfo **ppTInfo)
812 {
813     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
814
815     return IDispatchEx_GetTypeInfo(DISPATCHEX(This), iTInfo, lcid, ppTInfo);
816 }
817
818 static HRESULT WINAPI HTMLWindow3_GetIDsOfNames(IHTMLWindow3 *iface, REFIID riid,
819                                                 LPOLESTR *rgszNames, UINT cNames,
820                                                 LCID lcid, DISPID *rgDispId)
821 {
822     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
823
824     return IDispatchEx_GetIDsOfNames(DISPATCHEX(This), riid, rgszNames, cNames, lcid, rgDispId);
825 }
826
827 static HRESULT WINAPI HTMLWindow3_Invoke(IHTMLWindow3 *iface, DISPID dispIdMember,
828                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
829                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
830 {
831     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
832
833     return IDispatchEx_Invoke(DISPATCHEX(This), dispIdMember, riid, lcid, wFlags, pDispParams,
834             pVarResult, pExcepInfo, puArgErr);
835 }
836
837 static HRESULT WINAPI HTMLWindow3_get_screenLeft(IHTMLWindow3 *iface, long *p)
838 {
839     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
840     FIXME("(%p)->(%p)\n", This, p);
841     return E_NOTIMPL;
842 }
843
844 static HRESULT WINAPI HTMLWindow3_get_screenTop(IHTMLWindow3 *iface, long *p)
845 {
846     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
847     FIXME("(%p)->(%p)\n", This, p);
848     return E_NOTIMPL;
849 }
850
851 static HRESULT WINAPI HTMLWindow3_attachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp, VARIANT_BOOL *pfResult)
852 {
853     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
854     FIXME("(%p)->(%s %p %p)\n", This, debugstr_w(event), pDisp, pfResult);
855     return E_NOTIMPL;
856 }
857
858 static HRESULT WINAPI HTMLWindow3_detachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp)
859 {
860     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
861     FIXME("(%p)->()\n", This);
862     return E_NOTIMPL;
863 }
864
865 static HRESULT window_set_timer(HTMLWindow *This, VARIANT *expr, long msec, VARIANT *language,
866         BOOL interval, long *timer_id)
867 {
868     IDispatch *disp = NULL;
869
870     switch(V_VT(expr)) {
871     case VT_DISPATCH:
872         disp = V_DISPATCH(expr);
873         IDispatch_AddRef(disp);
874         break;
875
876     case VT_BSTR:
877         disp = script_parse_event(This->doc, V_BSTR(expr));
878         break;
879
880     default:
881         FIXME("unimplemented vt=%d\n", V_VT(expr));
882         return E_NOTIMPL;
883     }
884
885     if(!disp)
886         return E_FAIL;
887
888     *timer_id = set_task_timer(This->doc, msec, interval, disp);
889     IDispatch_Release(disp);
890
891     return S_OK;
892 }
893
894 static HRESULT WINAPI HTMLWindow3_setTimeout(IHTMLWindow3 *iface, VARIANT *expression, long msec,
895         VARIANT *language, long *timerID)
896 {
897     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
898
899     TRACE("(%p)->(%p(%d) %ld %p %p)\n", This, expression, V_VT(expression), msec, language, timerID);
900
901     return window_set_timer(This, expression, msec, language, FALSE, timerID);
902 }
903
904 static HRESULT WINAPI HTMLWindow3_setInterval(IHTMLWindow3 *iface, VARIANT *expression, long msec,
905         VARIANT *language, long *timerID)
906 {
907     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
908
909     TRACE("(%p)->(%p %ld %p %p)\n", This, expression, msec, language, timerID);
910
911     return window_set_timer(This, expression, msec, language, TRUE, timerID);
912 }
913
914 static HRESULT WINAPI HTMLWindow3_print(IHTMLWindow3 *iface)
915 {
916     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
917     FIXME("(%p)\n", This);
918     return E_NOTIMPL;
919 }
920
921 static HRESULT WINAPI HTMLWindow3_put_onbeforeprint(IHTMLWindow3 *iface, VARIANT v)
922 {
923     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
924     FIXME("(%p)->()\n", This);
925     return E_NOTIMPL;
926 }
927
928 static HRESULT WINAPI HTMLWindow3_get_onbeforeprint(IHTMLWindow3 *iface, VARIANT *p)
929 {
930     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
931     FIXME("(%p)->(%p)\n", This, p);
932     return E_NOTIMPL;
933 }
934
935 static HRESULT WINAPI HTMLWindow3_put_onafterprint(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_onafterprint(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_get_clipboardData(IHTMLWindow3 *iface, IHTMLDataTransfer **p)
950 {
951     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
952     FIXME("(%p)->(%p)\n", This, p);
953     return E_NOTIMPL;
954 }
955
956 static HRESULT WINAPI HTMLWindow3_showModelessDialog(IHTMLWindow3 *iface, BSTR url,
957         VARIANT *varArgIn, VARIANT *options, IHTMLWindow2 **pDialog)
958 {
959     HTMLWindow *This = HTMLWINDOW3_THIS(iface);
960     FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(url), varArgIn, options, pDialog);
961     return E_NOTIMPL;
962 }
963
964 #undef HTMLWINDOW3_THIS
965
966 static const IHTMLWindow3Vtbl HTMLWindow3Vtbl = {
967     HTMLWindow3_QueryInterface,
968     HTMLWindow3_AddRef,
969     HTMLWindow3_Release,
970     HTMLWindow3_GetTypeInfoCount,
971     HTMLWindow3_GetTypeInfo,
972     HTMLWindow3_GetIDsOfNames,
973     HTMLWindow3_Invoke,
974     HTMLWindow3_get_screenLeft,
975     HTMLWindow3_get_screenTop,
976     HTMLWindow3_attachEvent,
977     HTMLWindow3_detachEvent,
978     HTMLWindow3_setTimeout,
979     HTMLWindow3_setInterval,
980     HTMLWindow3_print,
981     HTMLWindow3_put_onbeforeprint,
982     HTMLWindow3_get_onbeforeprint,
983     HTMLWindow3_put_onafterprint,
984     HTMLWindow3_get_onafterprint,
985     HTMLWindow3_get_clipboardData,
986     HTMLWindow3_showModelessDialog
987 };
988
989 #define DISPEX_THIS(iface) DEFINE_THIS(HTMLWindow, IDispatchEx, iface)
990
991 static HRESULT WINAPI WindowDispEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
992 {
993     HTMLWindow *This = DISPEX_THIS(iface);
994
995     return IHTMLWindow2_QueryInterface(HTMLWINDOW2(This), riid, ppv);
996 }
997
998 static ULONG WINAPI WindowDispEx_AddRef(IDispatchEx *iface)
999 {
1000     HTMLWindow *This = DISPEX_THIS(iface);
1001
1002     return IHTMLWindow2_AddRef(HTMLWINDOW2(This));
1003 }
1004
1005 static ULONG WINAPI WindowDispEx_Release(IDispatchEx *iface)
1006 {
1007     HTMLWindow *This = DISPEX_THIS(iface);
1008
1009     return IHTMLWindow2_Release(HTMLWINDOW2(This));
1010 }
1011
1012 static HRESULT WINAPI WindowDispEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1013 {
1014     HTMLWindow *This = DISPEX_THIS(iface);
1015
1016     TRACE("(%p)->(%p)\n", This, pctinfo);
1017
1018     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
1019 }
1020
1021 static HRESULT WINAPI WindowDispEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1022                                                LCID lcid, ITypeInfo **ppTInfo)
1023 {
1024     HTMLWindow *This = DISPEX_THIS(iface);
1025
1026     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1027
1028     return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
1029 }
1030
1031 static HRESULT WINAPI WindowDispEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1032                                                  LPOLESTR *rgszNames, UINT cNames,
1033                                                  LCID lcid, DISPID *rgDispId)
1034 {
1035     HTMLWindow *This = DISPEX_THIS(iface);
1036
1037     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1038           lcid, rgDispId);
1039
1040     /* FIXME: Use script dispatch */
1041
1042     return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
1043 }
1044
1045 static HRESULT WINAPI WindowDispEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1046                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1047                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1048 {
1049     HTMLWindow *This = DISPEX_THIS(iface);
1050
1051     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1052           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1053
1054     /* FIXME: Use script dispatch */
1055
1056     return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid, wFlags, pDispParams,
1057                               pVarResult, pExcepInfo, puArgErr);
1058 }
1059
1060 static HRESULT WINAPI WindowDispEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1061 {
1062     HTMLWindow *This = DISPEX_THIS(iface);
1063
1064     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1065
1066     return IDispatchEx_GetDispID(DISPATCHEX(&This->dispex), bstrName, grfdex, pid);
1067 }
1068
1069 static HRESULT WINAPI WindowDispEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1070         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1071 {
1072     HTMLWindow *This = DISPEX_THIS(iface);
1073
1074     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1075
1076     return IDispatchEx_InvokeEx(DISPATCHEX(&This->dispex), id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1077 }
1078
1079 static HRESULT WINAPI WindowDispEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1080 {
1081     HTMLWindow *This = DISPEX_THIS(iface);
1082
1083     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1084
1085     return IDispatchEx_DeleteMemberByName(DISPATCHEX(&This->dispex), bstrName, grfdex);
1086 }
1087
1088 static HRESULT WINAPI WindowDispEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1089 {
1090     HTMLWindow *This = DISPEX_THIS(iface);
1091
1092     TRACE("(%p)->(%x)\n", This, id);
1093
1094     return IDispatchEx_DeleteMemberByDispID(DISPATCHEX(&This->dispex), id);
1095 }
1096
1097 static HRESULT WINAPI WindowDispEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1098 {
1099     HTMLWindow *This = DISPEX_THIS(iface);
1100
1101     TRACE("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1102
1103     return IDispatchEx_GetMemberProperties(DISPATCHEX(&This->dispex), id, grfdexFetch, pgrfdex);
1104 }
1105
1106 static HRESULT WINAPI WindowDispEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1107 {
1108     HTMLWindow *This = DISPEX_THIS(iface);
1109
1110     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1111
1112     return IDispatchEx_GetMemberName(DISPATCHEX(&This->dispex), id, pbstrName);
1113 }
1114
1115 static HRESULT WINAPI WindowDispEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1116 {
1117     HTMLWindow *This = DISPEX_THIS(iface);
1118
1119     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1120
1121     return IDispatchEx_GetNextDispID(DISPATCHEX(&This->dispex), grfdex, id, pid);
1122 }
1123
1124 static HRESULT WINAPI WindowDispEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1125 {
1126     HTMLWindow *This = DISPEX_THIS(iface);
1127
1128     TRACE("(%p)->(%p)\n", This, ppunk);
1129
1130     *ppunk = NULL;
1131     return S_OK;
1132 }
1133
1134 #undef DISPEX_THIS
1135
1136 static const IDispatchExVtbl WindowDispExVtbl = {
1137     WindowDispEx_QueryInterface,
1138     WindowDispEx_AddRef,
1139     WindowDispEx_Release,
1140     WindowDispEx_GetTypeInfoCount,
1141     WindowDispEx_GetTypeInfo,
1142     WindowDispEx_GetIDsOfNames,
1143     WindowDispEx_Invoke,
1144     WindowDispEx_GetDispID,
1145     WindowDispEx_InvokeEx,
1146     WindowDispEx_DeleteMemberByName,
1147     WindowDispEx_DeleteMemberByDispID,
1148     WindowDispEx_GetMemberProperties,
1149     WindowDispEx_GetMemberName,
1150     WindowDispEx_GetNextDispID,
1151     WindowDispEx_GetNameSpaceParent
1152 };
1153
1154 static const tid_t HTMLWindow_iface_tids[] = {
1155     IHTMLWindow2_tid,
1156     IHTMLWindow3_tid,
1157     0
1158 };
1159 static dispex_static_data_t HTMLWindow_dispex = {
1160     NULL,
1161     DispHTMLWindow2_tid,
1162     NULL,
1163     HTMLWindow_iface_tids
1164 };
1165
1166 static const char wineConfig_func[] =
1167 "window.__defineGetter__(\"external\",function() {\n"
1168 "    return window.__wineWindow__.external;\n"
1169 "});\n"
1170 "window.__wineWindow__ = wineWindow;\n";
1171
1172 static void astr_to_nswstr(const char *str, nsAString *nsstr)
1173 {
1174     LPWSTR wstr;
1175     int len;
1176
1177     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
1178     wstr = heap_alloc(len*sizeof(WCHAR));
1179     MultiByteToWideChar(CP_ACP, 0, str, -1, wstr, len);
1180
1181     nsAString_Init(nsstr, wstr);
1182
1183     heap_free(wstr);
1184 }
1185
1186 static nsresult call_js_func(nsIScriptContainer *script_container, nsISupports *target,
1187                              const char *name, const char *body,
1188                              PRUint32 argc, const char **arg_names, nsIArray *argv)
1189 {
1190     nsACString name_str;
1191     nsAString body_str;
1192     JSObject func_obj, jsglobal;
1193     nsIVariant *jsret;
1194     nsresult nsres;
1195
1196     nsres = nsIScriptContainer_GetGlobalObject(script_container, &jsglobal);
1197     if(NS_FAILED(nsres))
1198         ERR("GetGlobalObject: %08x\n", nsres);
1199
1200     nsACString_Init(&name_str, name);
1201     astr_to_nswstr(body, &body_str);
1202
1203     nsres =  nsIScriptContainer_CompileFunction(script_container, jsglobal, &name_str, argc, arg_names,
1204                                                 &body_str, NULL, 1, FALSE, &func_obj);
1205
1206     nsACString_Finish(&name_str);
1207     nsAString_Finish(&body_str);
1208
1209     if(NS_FAILED(nsres)) {
1210         ERR("CompileFunction failed: %08x\n", nsres);
1211         return nsres;
1212     }
1213
1214     nsres = nsIScriptContainer_CallFunction(script_container, target, jsglobal, func_obj, argv, &jsret);
1215
1216     nsIScriptContainer_DropScriptObject(script_container, func_obj);
1217     nsIScriptContainer_DropScriptObject(script_container, jsglobal);
1218     if(NS_FAILED(nsres)) {
1219         ERR("CallFunction failed: %08x\n", nsres);
1220         return nsres;
1221     }
1222
1223     nsIVariant_Release(jsret);
1224     return NS_OK;
1225 }
1226
1227 void setup_nswindow(HTMLWindow *This)
1228 {
1229     nsIScriptContainer *script_container;
1230     nsIDOMWindow *nswindow;
1231     nsIDOMDocument *domdoc;
1232     nsIWritableVariant *nsvar;
1233     nsIMutableArray *argv;
1234     nsresult nsres;
1235
1236     static const char *args[] = {"wineWindow"};
1237
1238     TRACE("(%p)\n", This);
1239
1240     nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &domdoc);
1241     nsres = nsIDOMDocument_QueryInterface(domdoc, &IID_nsIScriptContainer, (void**)&script_container);
1242     nsIDOMDocument_Release(domdoc);
1243     if(NS_FAILED(nsres)) {
1244         TRACE("Could not get nsIDOMScriptContainer: %08x\n", nsres);
1245         return;
1246     }
1247
1248     nsIWebBrowser_GetContentDOMWindow(This->doc->nscontainer->webbrowser, &nswindow);
1249
1250     nsvar = create_nsvariant();
1251     nsres = nsIWritableVariant_SetAsInterface(nsvar, &IID_IDispatch, HTMLWINDOW2(This));
1252     if(NS_FAILED(nsres))
1253         ERR("SetAsInterface failed: %08x\n", nsres);
1254
1255     argv = create_nsarray();
1256     nsres = nsIMutableArray_AppendElement(argv, (nsISupports*)nsvar, FALSE);
1257     nsIWritableVariant_Release(nsvar);
1258     if(NS_FAILED(nsres))
1259         ERR("AppendElement failed: %08x\n", nsres);
1260
1261     call_js_func(script_container, (nsISupports*)nswindow/*HTMLWINDOW2(This)*/, "wineConfig",
1262                  wineConfig_func, 1, args, (nsIArray*)argv);
1263
1264     nsIMutableArray_Release(argv);
1265     nsIScriptContainer_Release(script_container);
1266 }
1267
1268 HTMLWindow *HTMLWindow_Create(HTMLDocument *doc)
1269 {
1270     HTMLWindow *ret = heap_alloc(sizeof(HTMLWindow));
1271
1272     ret->lpHTMLWindow2Vtbl = &HTMLWindow2Vtbl;
1273     ret->lpHTMLWindow3Vtbl = &HTMLWindow3Vtbl;
1274     ret->lpIDispatchExVtbl = &WindowDispExVtbl;
1275     ret->ref = 1;
1276     ret->doc = doc;
1277
1278     init_dispex(&ret->dispex, (IUnknown*)HTMLWINDOW2(ret), &HTMLWindow_dispex);
1279
1280     if(doc->nscontainer) {
1281         nsresult nsres;
1282
1283         nsres = nsIWebBrowser_GetContentDOMWindow(doc->nscontainer->webbrowser, &ret->nswindow);
1284         if(NS_FAILED(nsres))
1285             ERR("GetContentDOMWindow failed: %08x\n", nsres);
1286     }
1287
1288     list_add_head(&window_list, &ret->entry);
1289
1290     return ret;
1291 }
1292
1293 HTMLWindow *nswindow_to_window(const nsIDOMWindow *nswindow)
1294 {
1295     HTMLWindow *iter;
1296
1297     LIST_FOR_EACH_ENTRY(iter, &window_list, HTMLWindow, entry) {
1298         if(iter->nswindow == nswindow)
1299             return iter;
1300     }
1301
1302     return NULL;
1303 }