mshtml: Added nsINetUtil interface to nsIOService.
[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 "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "ole2.h"
31
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34
35 #include "mshtml_private.h"
36 #include "resource.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39
40 #define HTMLWINDOW2_THIS(iface) DEFINE_THIS(HTMLWindow, HTMLWindow2, iface)
41
42 static struct list window_list = LIST_INIT(window_list);
43
44 static HRESULT WINAPI HTMLWindow2_QueryInterface(IHTMLWindow2 *iface, REFIID riid, void **ppv)
45 {
46     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
47
48     *ppv = NULL;
49
50     if(IsEqualGUID(&IID_IUnknown, riid)) {
51         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
52         *ppv = HTMLWINDOW2(This);
53     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
54         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
55         *ppv = HTMLWINDOW2(This);
56     }else if(IsEqualGUID(&IID_IHTMLFramesCollection2, riid)) {
57         TRACE("(%p)->(IID_IHTMLFramesCollection2 %p)\n", This, ppv);
58         *ppv = HTMLWINDOW2(This);
59     }else if(IsEqualGUID(&IID_IHTMLWindow2, riid)) {
60         TRACE("(%p)->(IID_IHTMLWindow2 %p)\n", This, ppv);
61         *ppv = HTMLWINDOW2(This);
62     }
63
64     if(*ppv) {
65         IUnknown_AddRef((IUnknown*)*ppv);
66         return S_OK;
67     }
68
69     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
70     return E_NOINTERFACE;
71 }
72
73 static ULONG WINAPI HTMLWindow2_AddRef(IHTMLWindow2 *iface)
74 {
75     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
76     LONG ref = InterlockedIncrement(&This->ref);
77
78     TRACE("(%p) ref=%d\n", This, ref);
79
80     return ref;
81 }
82
83 static ULONG WINAPI HTMLWindow2_Release(IHTMLWindow2 *iface)
84 {
85     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
86     LONG ref = InterlockedDecrement(&This->ref);
87
88     TRACE("(%p) ref=%d\n", This, ref);
89
90     if(!ref) {
91         list_remove(&This->entry);
92         heap_free(This);
93     }
94
95     return ref;
96 }
97
98 static HRESULT WINAPI HTMLWindow2_GetTypeInfoCount(IHTMLWindow2 *iface, UINT *pctinfo)
99 {
100     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
101
102     TRACE("(%p)->(%p)\n", This, pctinfo);
103
104     *pctinfo = 1;
105     return S_OK;
106 }
107
108 static HRESULT WINAPI HTMLWindow2_GetTypeInfo(IHTMLWindow2 *iface, UINT iTInfo,
109                                               LCID lcid, ITypeInfo **ppTInfo)
110 {
111     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
112     HRESULT hres;
113
114     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
115
116     hres = get_typeinfo(IHTMLWindow2_tid, ppTInfo);
117     if(SUCCEEDED(hres))
118         ITypeInfo_AddRef(*ppTInfo);
119
120     return hres;
121 }
122
123 static HRESULT WINAPI HTMLWindow2_GetIDsOfNames(IHTMLWindow2 *iface, REFIID riid,
124                                                 LPOLESTR *rgszNames, UINT cNames,
125                                                 LCID lcid, DISPID *rgDispId)
126 {
127     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
128     ITypeInfo *typeinfo;
129     HRESULT hres;
130
131     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
132           lcid, rgDispId);
133
134     hres = get_typeinfo(IHTMLWindow2_tid, &typeinfo);
135     if(SUCCEEDED(hres))
136         hres = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
137
138     return hres;
139 }
140
141 static HRESULT WINAPI HTMLWindow2_Invoke(IHTMLWindow2 *iface, DISPID dispIdMember,
142                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
143                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
144 {
145     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
146     ITypeInfo *typeinfo;
147     HRESULT hres;
148
149     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
150           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
151
152     hres = get_typeinfo(IHTMLWindow2_tid, &typeinfo);
153     if(SUCCEEDED(hres))
154         hres = ITypeInfo_Invoke(typeinfo, HTMLWINDOW2(This), dispIdMember, wFlags, pDispParams,
155                 pVarResult, pExcepInfo, puArgErr);
156
157     return hres;
158 }
159
160 static HRESULT WINAPI HTMLWindow2_item(IHTMLWindow2 *iface, VARIANT *pvarIndex, VARIANT *pvarResult)
161 {
162     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
163     FIXME("(%p)->(%p %p)\n", This, pvarIndex, pvarResult);
164     return E_NOTIMPL;
165 }
166
167 static HRESULT WINAPI HTMLWindow2_get_length(IHTMLWindow2 *iface, long *p)
168 {
169     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
170     FIXME("(%p)->(%p)\n", This, p);
171     return E_NOTIMPL;
172 }
173
174 static HRESULT WINAPI HTMLWindow2_get_frames(IHTMLWindow2 *iface, IHTMLFramesCollection2 **p)
175 {
176     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
177     FIXME("(%p)->(%p)\n", This, p);
178     return E_NOTIMPL;
179 }
180
181 static HRESULT WINAPI HTMLWindow2_put_defaultStatus(IHTMLWindow2 *iface, BSTR v)
182 {
183     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
184     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
185     return E_NOTIMPL;
186 }
187
188 static HRESULT WINAPI HTMLWindow2_get_defaultStatus(IHTMLWindow2 *iface, BSTR *p)
189 {
190     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
191     FIXME("(%p)->(%p)\n", This, p);
192     return E_NOTIMPL;
193 }
194
195 static HRESULT WINAPI HTMLWindow2_put_status(IHTMLWindow2 *iface, BSTR v)
196 {
197     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
198     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
199     return E_NOTIMPL;
200 }
201
202 static HRESULT WINAPI HTMLWindow2_get_status(IHTMLWindow2 *iface, BSTR *p)
203 {
204     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
205     FIXME("(%p)->(%p)\n", This, p);
206     return E_NOTIMPL;
207 }
208
209 static HRESULT WINAPI HTMLWindow2_setTimeout(IHTMLWindow2 *iface, BSTR expression,
210         long msec, VARIANT *language, long *timerID)
211 {
212     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
213     FIXME("(%p)->(%s %ld %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
214     return E_NOTIMPL;
215 }
216
217 static HRESULT WINAPI HTMLWindow2_clearTimeout(IHTMLWindow2 *iface, long timerID)
218 {
219     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
220     FIXME("(%p)->(%ld)\n", This, timerID);
221     return E_NOTIMPL;
222 }
223
224 static HRESULT WINAPI HTMLWindow2_alert(IHTMLWindow2 *iface, BSTR message)
225 {
226     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
227     WCHAR wszTitle[100];
228
229     TRACE("(%p)->(%s)\n", This, debugstr_w(message));
230
231     if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, wszTitle,
232                     sizeof(wszTitle)/sizeof(WCHAR))) {
233         WARN("Could not load message box title: %d\n", GetLastError());
234         return S_OK;
235     }
236
237     MessageBoxW(This->doc->hwnd, message, wszTitle, MB_ICONWARNING);
238     return S_OK;
239 }
240
241 static HRESULT WINAPI HTMLWindow2_confirm(IHTMLWindow2 *iface, BSTR message,
242         VARIANT_BOOL *confirmed)
243 {
244     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
245     FIXME("(%p)->(%s %p)\n", This, debugstr_w(message), confirmed);
246     return E_NOTIMPL;
247 }
248
249 static HRESULT WINAPI HTMLWindow2_prompt(IHTMLWindow2 *iface, BSTR message,
250         BSTR dststr, VARIANT *textdata)
251 {
252     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
253     FIXME("(%p)->(%s %s %p)\n", This, debugstr_w(message), debugstr_w(dststr), textdata);
254     return E_NOTIMPL;
255 }
256
257 static HRESULT WINAPI HTMLWindow2_get_Image(IHTMLWindow2 *iface, IHTMLImageElementFactory **p)
258 {
259     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
260     FIXME("(%p)->(%p)\n", This, p);
261     return E_NOTIMPL;
262 }
263
264 static HRESULT WINAPI HTMLWindow2_get_location(IHTMLWindow2 *iface, IHTMLLocation **p)
265 {
266     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
267     FIXME("(%p)->(%p)\n", This, p);
268     return E_NOTIMPL;
269 }
270
271 static HRESULT WINAPI HTMLWindow2_get_history(IHTMLWindow2 *iface, IOmHistory **p)
272 {
273     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
274     FIXME("(%p)->(%p)\n", This, p);
275     return E_NOTIMPL;
276 }
277
278 static HRESULT WINAPI HTMLWindow2_close(IHTMLWindow2 *iface)
279 {
280     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
281     FIXME("(%p)->()\n", This);
282     return E_NOTIMPL;
283 }
284
285 static HRESULT WINAPI HTMLWindow2_put_opener(IHTMLWindow2 *iface, VARIANT v)
286 {
287     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
288     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
289     return E_NOTIMPL;
290 }
291
292 static HRESULT WINAPI HTMLWindow2_get_opener(IHTMLWindow2 *iface, VARIANT *p)
293 {
294     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
295     FIXME("(%p)->(%p)\n", This, p);
296     return E_NOTIMPL;
297 }
298
299 static HRESULT WINAPI HTMLWindow2_get_navigator(IHTMLWindow2 *iface, IOmNavigator **p)
300 {
301     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
302     FIXME("(%p)->(%p)\n", This, p);
303     return E_NOTIMPL;
304 }
305
306 static HRESULT WINAPI HTMLWindow2_put_name(IHTMLWindow2 *iface, BSTR v)
307 {
308     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
309     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
310     return E_NOTIMPL;
311 }
312
313 static HRESULT WINAPI HTMLWindow2_get_name(IHTMLWindow2 *iface, BSTR *p)
314 {
315     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
316     FIXME("(%p)->(%p)\n", This, p);
317     return E_NOTIMPL;
318 }
319
320 static HRESULT WINAPI HTMLWindow2_get_parent(IHTMLWindow2 *iface, IHTMLWindow2 **p)
321 {
322     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
323     FIXME("(%p)->(%p)\n", This, p);
324     return E_NOTIMPL;
325 }
326
327 static HRESULT WINAPI HTMLWindow2_open(IHTMLWindow2 *iface, BSTR url, BSTR name,
328          BSTR features, VARIANT_BOOL replace, IHTMLWindow2 **pomWindowResult)
329 {
330     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
331     FIXME("(%p)->(%s %s %s %x %p)\n", This, debugstr_w(url), debugstr_w(name),
332           debugstr_w(features), replace, pomWindowResult);
333     return E_NOTIMPL;
334 }
335
336 static HRESULT WINAPI HTMLWindow2_get_self(IHTMLWindow2 *iface, IHTMLWindow2 **p)
337 {
338     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
339     FIXME("(%p)->(%p)\n", This, p);
340     return E_NOTIMPL;
341 }
342
343 static HRESULT WINAPI HTMLWindow2_get_top(IHTMLWindow2 *iface, IHTMLWindow2 **p)
344 {
345     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
346     FIXME("(%p)->(%p)\n", This, p);
347     return E_NOTIMPL;
348 }
349
350 static HRESULT WINAPI HTMLWindow2_get_window(IHTMLWindow2 *iface, IHTMLWindow2 **p)
351 {
352     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
353     FIXME("(%p)->(%p)\n", This, p);
354     return E_NOTIMPL;
355 }
356
357 static HRESULT WINAPI HTMLWindow2_navigate(IHTMLWindow2 *iface, BSTR url)
358 {
359     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
360     FIXME("(%p)->(%s)\n", This, debugstr_w(url));
361     return E_NOTIMPL;
362 }
363
364 static HRESULT WINAPI HTMLWindow2_put_onfocus(IHTMLWindow2 *iface, VARIANT v)
365 {
366     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
367     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
368     return E_NOTIMPL;
369 }
370
371 static HRESULT WINAPI HTMLWindow2_get_onfocus(IHTMLWindow2 *iface, VARIANT *p)
372 {
373     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
374     FIXME("(%p)->(%p)\n", This, p);
375     return E_NOTIMPL;
376 }
377
378 static HRESULT WINAPI HTMLWindow2_put_onblur(IHTMLWindow2 *iface, VARIANT v)
379 {
380     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
381     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
382     return E_NOTIMPL;
383 }
384
385 static HRESULT WINAPI HTMLWindow2_get_onblur(IHTMLWindow2 *iface, VARIANT *p)
386 {
387     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
388     FIXME("(%p)->(%p)\n", This, p);
389     return E_NOTIMPL;
390 }
391
392 static HRESULT WINAPI HTMLWindow2_put_onload(IHTMLWindow2 *iface, VARIANT v)
393 {
394     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
395     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
396     return E_NOTIMPL;
397 }
398
399 static HRESULT WINAPI HTMLWindow2_get_onload(IHTMLWindow2 *iface, VARIANT *p)
400 {
401     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
402     FIXME("(%p)->(%p)\n", This, p);
403     return E_NOTIMPL;
404 }
405
406 static HRESULT WINAPI HTMLWindow2_put_onbeforeunload(IHTMLWindow2 *iface, VARIANT v)
407 {
408     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
409     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
410     return E_NOTIMPL;
411 }
412
413 static HRESULT WINAPI HTMLWindow2_get_onbeforeunload(IHTMLWindow2 *iface, VARIANT *p)
414 {
415     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
416     FIXME("(%p)->(%p)\n", This, p);
417     return E_NOTIMPL;
418 }
419
420 static HRESULT WINAPI HTMLWindow2_put_onunload(IHTMLWindow2 *iface, VARIANT v)
421 {
422     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
423     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
424     return E_NOTIMPL;
425 }
426
427 static HRESULT WINAPI HTMLWindow2_get_onunload(IHTMLWindow2 *iface, VARIANT *p)
428 {
429     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
430     FIXME("(%p)->(%p)\n", This, p);
431     return E_NOTIMPL;
432 }
433
434 static HRESULT WINAPI HTMLWindow2_put_onhelp(IHTMLWindow2 *iface, VARIANT v)
435 {
436     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
437     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
438     return E_NOTIMPL;
439 }
440
441 static HRESULT WINAPI HTMLWindow2_get_onhelp(IHTMLWindow2 *iface, VARIANT *p)
442 {
443     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
444     FIXME("(%p)->(%p)\n", This, p);
445     return E_NOTIMPL;
446 }
447
448 static HRESULT WINAPI HTMLWindow2_put_onerror(IHTMLWindow2 *iface, VARIANT v)
449 {
450     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
451     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
452     return E_NOTIMPL;
453 }
454
455 static HRESULT WINAPI HTMLWindow2_get_onerror(IHTMLWindow2 *iface, VARIANT *p)
456 {
457     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
458     FIXME("(%p)->(%p)\n", This, p);
459     return E_NOTIMPL;
460 }
461
462 static HRESULT WINAPI HTMLWindow2_put_onresize(IHTMLWindow2 *iface, VARIANT v)
463 {
464     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
465     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
466     return E_NOTIMPL;
467 }
468
469 static HRESULT WINAPI HTMLWindow2_get_onresize(IHTMLWindow2 *iface, VARIANT *p)
470 {
471     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
472     FIXME("(%p)->(%p)\n", This, p);
473     return E_NOTIMPL;
474 }
475
476 static HRESULT WINAPI HTMLWindow2_put_onscroll(IHTMLWindow2 *iface, VARIANT v)
477 {
478     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
479     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
480     return E_NOTIMPL;
481 }
482
483 static HRESULT WINAPI HTMLWindow2_get_onscroll(IHTMLWindow2 *iface, VARIANT *p)
484 {
485     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
486     FIXME("(%p)->(%p)\n", This, p);
487     return E_NOTIMPL;
488 }
489
490 static HRESULT WINAPI HTMLWindow2_get_document(IHTMLWindow2 *iface, IHTMLDocument2 **p)
491 {
492     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
493     FIXME("(%p)->(%p)\n", This, p);
494     return E_NOTIMPL;
495 }
496
497 static HRESULT WINAPI HTMLWindow2_get_event(IHTMLWindow2 *iface, IHTMLEventObj **p)
498 {
499     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
500     FIXME("(%p)->(%p)\n", This, p);
501     return E_NOTIMPL;
502 }
503
504 static HRESULT WINAPI HTMLWindow2_get__newEnum(IHTMLWindow2 *iface, IUnknown **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_showModalDialog(IHTMLWindow2 *iface, BSTR dialog,
512         VARIANT *varArgIn, VARIANT *varOptions, VARIANT *varArgOut)
513 {
514     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
515     FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(dialog), varArgIn, varOptions, varArgOut);
516     return E_NOTIMPL;
517 }
518
519 static HRESULT WINAPI HTMLWindow2_showHelp(IHTMLWindow2 *iface, BSTR helpURL, VARIANT helpArg,
520         BSTR features)
521 {
522     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
523     FIXME("(%p)->(%s v(%d) %s)\n", This, debugstr_w(helpURL), V_VT(&helpArg), debugstr_w(features));
524     return E_NOTIMPL;
525 }
526
527 static HRESULT WINAPI HTMLWindow2_get_screen(IHTMLWindow2 *iface, IHTMLScreen **p)
528 {
529     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
530     FIXME("(%p)->(%p)\n", This, p);
531     return E_NOTIMPL;
532 }
533
534 static HRESULT WINAPI HTMLWindow2_get_Option(IHTMLWindow2 *iface, IHTMLOptionElementFactory **p)
535 {
536     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
537
538     TRACE("(%p)->(%p)\n", This, p);
539
540     if(!This->doc->option_factory)
541         This->doc->option_factory = HTMLOptionElementFactory_Create(This->doc);
542
543     *p = HTMLOPTFACTORY(This->doc->option_factory);
544     IHTMLOptionElementFactory_AddRef(*p);
545
546     return S_OK;
547 }
548
549 static HRESULT WINAPI HTMLWindow2_focus(IHTMLWindow2 *iface)
550 {
551     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
552     FIXME("(%p)->()\n", This);
553     return E_NOTIMPL;
554 }
555
556 static HRESULT WINAPI HTMLWindow2_get_closed(IHTMLWindow2 *iface, VARIANT_BOOL *p)
557 {
558     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
559     FIXME("(%p)->(%p)\n", This, p);
560     return E_NOTIMPL;
561 }
562
563 static HRESULT WINAPI HTMLWindow2_blur(IHTMLWindow2 *iface)
564 {
565     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
566     FIXME("(%p)->()\n", This);
567     return E_NOTIMPL;
568 }
569
570 static HRESULT WINAPI HTMLWindow2_scroll(IHTMLWindow2 *iface, long x, long y)
571 {
572     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
573     FIXME("(%p)->(%ld %ld)\n", This, x, y);
574     return E_NOTIMPL;
575 }
576
577 static HRESULT WINAPI HTMLWindow2_get_clientInformation(IHTMLWindow2 *iface, IOmNavigator **p)
578 {
579     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
580     FIXME("(%p)->(%p)\n", This, p);
581     return E_NOTIMPL;
582 }
583
584 static HRESULT WINAPI HTMLWindow2_setInterval(IHTMLWindow2 *iface, BSTR expression,
585         long msec, VARIANT *language, long *timerID)
586 {
587     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
588     FIXME("(%p)->(%s %ld %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
589     return E_NOTIMPL;
590 }
591
592 static HRESULT WINAPI HTMLWindow2_clearInterval(IHTMLWindow2 *iface, long timerID)
593 {
594     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
595     FIXME("(%p)->(%ld)\n", This, timerID);
596     return E_NOTIMPL;
597 }
598
599 static HRESULT WINAPI HTMLWindow2_put_offscreenBuffering(IHTMLWindow2 *iface, VARIANT v)
600 {
601     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
602     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
603     return E_NOTIMPL;
604 }
605
606 static HRESULT WINAPI HTMLWindow2_get_offscreenBuffering(IHTMLWindow2 *iface, VARIANT *p)
607 {
608     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
609     FIXME("(%p)->(%p)\n", This, p);
610     return E_NOTIMPL;
611 }
612
613 static HRESULT WINAPI HTMLWindow2_execScript(IHTMLWindow2 *iface, BSTR scode, BSTR language,
614         VARIANT *pvarRet)
615 {
616     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
617     FIXME("(%p)->(%s %s %p)\n", This, debugstr_w(scode), debugstr_w(language), pvarRet);
618     return E_NOTIMPL;
619 }
620
621 static HRESULT WINAPI HTMLWindow2_toString(IHTMLWindow2 *iface, BSTR *String)
622 {
623     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
624     FIXME("(%p)->(%p)\n", This, String);
625     return E_NOTIMPL;
626 }
627
628 static HRESULT WINAPI HTMLWindow2_scrollBy(IHTMLWindow2 *iface, long x, long y)
629 {
630     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
631     FIXME("(%p)->(%ld %ld)\n", This, x, y);
632     return E_NOTIMPL;
633 }
634
635 static HRESULT WINAPI HTMLWindow2_scrollTo(IHTMLWindow2 *iface, long x, long y)
636 {
637     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
638     FIXME("(%p)->(%ld %ld)\n", This, x, y);
639     return E_NOTIMPL;
640 }
641
642 static HRESULT WINAPI HTMLWindow2_moveTo(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_moveBy(IHTMLWindow2 *iface, long x, long y)
650 {
651     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
652     FIXME("(%p)->(%ld %ld)\n", This, x, y);
653     return E_NOTIMPL;
654 }
655
656 static HRESULT WINAPI HTMLWindow2_resizeTo(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_resizeBy(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_get_external(IHTMLWindow2 *iface, IDispatch **p)
671 {
672     HTMLWindow *This = HTMLWINDOW2_THIS(iface);
673
674     TRACE("(%p)->(%p)\n", This, p);
675
676     *p = NULL;
677
678     if(!This->doc->hostui)
679         return S_OK;
680
681     return IDocHostUIHandler_GetExternal(This->doc->hostui, p);
682 }
683
684 #undef HTMLWINDOW2_THIS
685
686 static const IHTMLWindow2Vtbl HTMLWindow2Vtbl = {
687     HTMLWindow2_QueryInterface,
688     HTMLWindow2_AddRef,
689     HTMLWindow2_Release,
690     HTMLWindow2_GetTypeInfoCount,
691     HTMLWindow2_GetTypeInfo,
692     HTMLWindow2_GetIDsOfNames,
693     HTMLWindow2_Invoke,
694     HTMLWindow2_item,
695     HTMLWindow2_get_length,
696     HTMLWindow2_get_frames,
697     HTMLWindow2_put_defaultStatus,
698     HTMLWindow2_get_defaultStatus,
699     HTMLWindow2_put_status,
700     HTMLWindow2_get_status,
701     HTMLWindow2_setTimeout,
702     HTMLWindow2_clearTimeout,
703     HTMLWindow2_alert,
704     HTMLWindow2_confirm,
705     HTMLWindow2_prompt,
706     HTMLWindow2_get_Image,
707     HTMLWindow2_get_location,
708     HTMLWindow2_get_history,
709     HTMLWindow2_close,
710     HTMLWindow2_put_opener,
711     HTMLWindow2_get_opener,
712     HTMLWindow2_get_navigator,
713     HTMLWindow2_put_name,
714     HTMLWindow2_get_name,
715     HTMLWindow2_get_parent,
716     HTMLWindow2_open,
717     HTMLWindow2_get_self,
718     HTMLWindow2_get_top,
719     HTMLWindow2_get_window,
720     HTMLWindow2_navigate,
721     HTMLWindow2_put_onfocus,
722     HTMLWindow2_get_onfocus,
723     HTMLWindow2_put_onblur,
724     HTMLWindow2_get_onblur,
725     HTMLWindow2_put_onload,
726     HTMLWindow2_get_onload,
727     HTMLWindow2_put_onbeforeunload,
728     HTMLWindow2_get_onbeforeunload,
729     HTMLWindow2_put_onunload,
730     HTMLWindow2_get_onunload,
731     HTMLWindow2_put_onhelp,
732     HTMLWindow2_get_onhelp,
733     HTMLWindow2_put_onerror,
734     HTMLWindow2_get_onerror,
735     HTMLWindow2_put_onresize,
736     HTMLWindow2_get_onresize,
737     HTMLWindow2_put_onscroll,
738     HTMLWindow2_get_onscroll,
739     HTMLWindow2_get_document,
740     HTMLWindow2_get_event,
741     HTMLWindow2_get__newEnum,
742     HTMLWindow2_showModalDialog,
743     HTMLWindow2_showHelp,
744     HTMLWindow2_get_screen,
745     HTMLWindow2_get_Option,
746     HTMLWindow2_focus,
747     HTMLWindow2_get_closed,
748     HTMLWindow2_blur,
749     HTMLWindow2_scroll,
750     HTMLWindow2_get_clientInformation,
751     HTMLWindow2_setInterval,
752     HTMLWindow2_clearInterval,
753     HTMLWindow2_put_offscreenBuffering,
754     HTMLWindow2_get_offscreenBuffering,
755     HTMLWindow2_execScript,
756     HTMLWindow2_toString,
757     HTMLWindow2_scrollBy,
758     HTMLWindow2_scrollTo,
759     HTMLWindow2_moveTo,
760     HTMLWindow2_moveBy,
761     HTMLWindow2_resizeTo,
762     HTMLWindow2_resizeBy,
763     HTMLWindow2_get_external
764 };
765
766 static const char wineConfig_func[] =
767 "window.__defineGetter__(\"external\",function() {\n"
768 "    return window.__wineWindow__.external;\n"
769 "});\n"
770 "window.__wineWindow__ = wineWindow;\n";
771
772 static void astr_to_nswstr(const char *str, nsAString *nsstr)
773 {
774     LPWSTR wstr;
775     int len;
776
777     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
778     wstr = heap_alloc(len*sizeof(WCHAR));
779     MultiByteToWideChar(CP_ACP, 0, str, -1, wstr, len);
780
781     nsAString_Init(nsstr, wstr);
782
783     heap_free(wstr);
784 }
785
786 static nsresult call_js_func(nsIScriptContainer *script_container, nsISupports *target,
787                              const char *name, const char *body,
788                              PRUint32 argc, const char **arg_names, nsIArray *argv)
789 {
790     nsACString name_str;
791     nsAString body_str;
792     JSObject func_obj, jsglobal;
793     nsIVariant *jsret;
794     nsresult nsres;
795
796     nsres = nsIScriptContainer_GetGlobalObject(script_container, &jsglobal);
797     if(NS_FAILED(nsres))
798         ERR("GetGlobalObject: %08x\n", nsres);
799
800     nsACString_Init(&name_str, name);
801     astr_to_nswstr(body, &body_str);
802
803     nsres =  nsIScriptContainer_CompileFunction(script_container, jsglobal, &name_str, argc, arg_names,
804                                                 &body_str, NULL, 1, FALSE, &func_obj);
805
806     nsACString_Finish(&name_str);
807     nsAString_Finish(&body_str);
808
809     if(NS_FAILED(nsres)) {
810         ERR("CompileFunction failed: %08x\n", nsres);
811         return nsres;
812     }
813
814     nsres = nsIScriptContainer_CallFunction(script_container, target, jsglobal, func_obj, argv, &jsret);
815
816     nsIScriptContainer_DropScriptObject(script_container, func_obj);
817     nsIScriptContainer_DropScriptObject(script_container, jsglobal);
818     if(NS_FAILED(nsres)) {
819         ERR("CallFunction failed: %08x\n", nsres);
820         return nsres;
821     }
822
823     nsIVariant_Release(jsret);
824     return NS_OK;
825 }
826
827 void setup_nswindow(HTMLWindow *This)
828 {
829     nsIScriptContainer *script_container;
830     nsIDOMWindow *nswindow;
831     nsIDOMDocument *domdoc;
832     nsIWritableVariant *nsvar;
833     nsIMutableArray *argv;
834     nsresult nsres;
835
836     static const char *args[] = {"wineWindow"};
837
838     TRACE("(%p)\n", This);
839
840     nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &domdoc);
841     nsres = nsIDOMDocument_QueryInterface(domdoc, &IID_nsIScriptContainer, (void**)&script_container);
842     nsIDOMDocument_Release(domdoc);
843     if(NS_FAILED(nsres)) {
844         TRACE("Could not get nsIDOMScriptContainer: %08x\n", nsres);
845         return;
846     }
847
848     nsIWebBrowser_GetContentDOMWindow(This->doc->nscontainer->webbrowser, &nswindow);
849
850     nsvar = create_nsvariant();
851     nsres = nsIWritableVariant_SetAsInterface(nsvar, &IID_IDispatch, HTMLWINDOW2(This));
852     if(NS_FAILED(nsres))
853         ERR("SetAsInterface failed: %08x\n", nsres);
854
855     argv = create_nsarray();
856     nsres = nsIMutableArray_AppendElement(argv, (nsISupports*)nsvar, FALSE);
857     nsIWritableVariant_Release(nsvar);
858     if(NS_FAILED(nsres))
859         ERR("AppendElement failed: %08x\n", nsres);
860
861     call_js_func(script_container, (nsISupports*)nswindow/*HTMLWINDOW2(This)*/, "wineConfig",
862                  wineConfig_func, 1, args, (nsIArray*)argv);
863
864     nsIMutableArray_Release(argv);
865     nsIScriptContainer_Release(script_container);
866 }
867
868 HTMLWindow *HTMLWindow_Create(HTMLDocument *doc)
869 {
870     HTMLWindow *ret = heap_alloc(sizeof(HTMLWindow));
871
872     ret->lpHTMLWindow2Vtbl = &HTMLWindow2Vtbl;
873     ret->ref = 1;
874     ret->nswindow = NULL;
875     ret->doc = doc;
876
877     if(doc->nscontainer) {
878         nsresult nsres;
879
880         nsres = nsIWebBrowser_GetContentDOMWindow(doc->nscontainer->webbrowser, &ret->nswindow);
881         if(NS_FAILED(nsres))
882             ERR("GetContentDOMWindow failed: %08x\n", nsres);
883     }
884
885     list_add_head(&window_list, &ret->entry);
886
887     return ret;
888 }
889
890 HTMLWindow *nswindow_to_window(const nsIDOMWindow *nswindow)
891 {
892     HTMLWindow *iter;
893
894     LIST_FOR_EACH_ENTRY(iter, &window_list, HTMLWindow, entry) {
895         if(iter->nswindow == nswindow)
896             return iter;
897     }
898
899     return NULL;
900 }