mshtml: Moved binding list to HTMLDocumentNode.
[wine] / dlls / mshtml / htmldoc.c
1 /*
2  * Copyright 2005-2009 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 "ole2.h"
30 #include "perhist.h"
31 #include "mshtmdid.h"
32
33 #include "wine/debug.h"
34
35 #include "mshtml_private.h"
36 #include "htmlevent.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39
40 #define HTMLDOC_THIS(iface) DEFINE_THIS(HTMLDocument, HTMLDocument2, iface)
41
42 static HRESULT WINAPI HTMLDocument_QueryInterface(IHTMLDocument2 *iface, REFIID riid, void **ppv)
43 {
44     HTMLDocument *This = HTMLDOC_THIS(iface);
45
46     return htmldoc_query_interface(This, riid, ppv);
47 }
48
49 static ULONG WINAPI HTMLDocument_AddRef(IHTMLDocument2 *iface)
50 {
51     HTMLDocument *This = HTMLDOC_THIS(iface);
52
53     return htmldoc_addref(This);
54 }
55
56 static ULONG WINAPI HTMLDocument_Release(IHTMLDocument2 *iface)
57 {
58     HTMLDocument *This = HTMLDOC_THIS(iface);
59
60     return htmldoc_release(This);
61 }
62
63 static HRESULT WINAPI HTMLDocument_GetTypeInfoCount(IHTMLDocument2 *iface, UINT *pctinfo)
64 {
65     HTMLDocument *This = HTMLDOC_THIS(iface);
66
67     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(This), pctinfo);
68 }
69
70 static HRESULT WINAPI HTMLDocument_GetTypeInfo(IHTMLDocument2 *iface, UINT iTInfo,
71                                                 LCID lcid, ITypeInfo **ppTInfo)
72 {
73     HTMLDocument *This = HTMLDOC_THIS(iface);
74
75     return IDispatchEx_GetTypeInfo(DISPATCHEX(This), iTInfo, lcid, ppTInfo);
76 }
77
78 static HRESULT WINAPI HTMLDocument_GetIDsOfNames(IHTMLDocument2 *iface, REFIID riid,
79                                                 LPOLESTR *rgszNames, UINT cNames,
80                                                 LCID lcid, DISPID *rgDispId)
81 {
82     HTMLDocument *This = HTMLDOC_THIS(iface);
83
84     return IDispatchEx_GetIDsOfNames(DISPATCHEX(This), riid, rgszNames, cNames, lcid, rgDispId);
85 }
86
87 static HRESULT WINAPI HTMLDocument_Invoke(IHTMLDocument2 *iface, DISPID dispIdMember,
88                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
89                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
90 {
91     HTMLDocument *This = HTMLDOC_THIS(iface);
92
93     return IDispatchEx_Invoke(DISPATCHEX(This), dispIdMember, riid, lcid, wFlags, pDispParams,
94             pVarResult, pExcepInfo, puArgErr);
95 }
96
97 static HRESULT WINAPI HTMLDocument_get_Script(IHTMLDocument2 *iface, IDispatch **p)
98 {
99     HTMLDocument *This = HTMLDOC_THIS(iface);
100
101     TRACE("(%p)->(%p)\n", This, p);
102
103     *p = (IDispatch*)HTMLWINDOW2(This->window);
104     IDispatch_AddRef(*p);
105     return S_OK;
106 }
107
108 static HRESULT WINAPI HTMLDocument_get_all(IHTMLDocument2 *iface, IHTMLElementCollection **p)
109 {
110     HTMLDocument *This = HTMLDOC_THIS(iface);
111     nsIDOMElement *nselem = NULL;
112     nsresult nsres;
113
114     TRACE("(%p)->(%p)\n", This, p);
115
116     if(!This->doc_node->nsdoc) {
117         WARN("NULL nsdoc\n");
118         return E_UNEXPECTED;
119     }
120
121     nsres = nsIDOMHTMLDocument_GetDocumentElement(This->doc_node->nsdoc, &nselem);
122     if(NS_FAILED(nsres)) {
123         ERR("GetDocumentElement failed: %08x\n", nsres);
124         return E_FAIL;
125     }
126
127     if(nselem) {
128         *p = create_all_collection(get_node(This->doc_node, (nsIDOMNode*)nselem, TRUE), TRUE);
129         nsIDOMElement_Release(nselem);
130     }else {
131         *p = NULL;
132     }
133
134     return S_OK;
135 }
136
137 static HRESULT WINAPI HTMLDocument_get_body(IHTMLDocument2 *iface, IHTMLElement **p)
138 {
139     HTMLDocument *This = HTMLDOC_THIS(iface);
140     nsIDOMHTMLElement *nsbody = NULL;
141     HTMLDOMNode *node;
142     nsresult nsres;
143
144     TRACE("(%p)->(%p)\n", This, p);
145
146     if(!This->doc_node->nsdoc) {
147         WARN("NULL nsdoc\n");
148         return E_UNEXPECTED;
149     }
150
151     nsres = nsIDOMHTMLDocument_GetBody(This->doc_node->nsdoc, &nsbody);
152     if(NS_FAILED(nsres)) {
153         TRACE("Could not get body: %08x\n", nsres);
154         return E_UNEXPECTED;
155     }
156
157     if(nsbody) {
158         node = get_node(This->doc_node, (nsIDOMNode*)nsbody, TRUE);
159         nsIDOMHTMLElement_Release(nsbody);
160
161         IHTMLDOMNode_QueryInterface(HTMLDOMNODE(node), &IID_IHTMLElement, (void**)p);
162     }else {
163         *p = NULL;
164     }
165
166     TRACE("*p = %p\n", *p);
167     return S_OK;
168 }
169
170 static HRESULT WINAPI HTMLDocument_get_activeElement(IHTMLDocument2 *iface, IHTMLElement **p)
171 {
172     HTMLDocument *This = HTMLDOC_THIS(iface);
173     FIXME("(%p)->(%p)\n", This, p);
174     return E_NOTIMPL;
175 }
176
177 static HRESULT WINAPI HTMLDocument_get_images(IHTMLDocument2 *iface, IHTMLElementCollection **p)
178 {
179     HTMLDocument *This = HTMLDOC_THIS(iface);
180     nsIDOMHTMLCollection *nscoll = NULL;
181     nsresult nsres;
182
183     TRACE("(%p)->(%p)\n", This, p);
184
185     if(!p)
186         return E_INVALIDARG;
187
188     *p = NULL;
189
190     if(!This->doc_node->nsdoc) {
191         WARN("NULL nsdoc\n");
192         return E_UNEXPECTED;
193     }
194
195     nsres = nsIDOMHTMLDocument_GetImages(This->doc_node->nsdoc, &nscoll);
196     if(NS_FAILED(nsres)) {
197         ERR("GetImages failed: %08x\n", nsres);
198         return E_FAIL;
199     }
200
201     if(nscoll) {
202         *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)HTMLDOC(This), nscoll);
203         nsIDOMElement_Release(nscoll);
204     }
205
206     return S_OK;
207 }
208
209 static HRESULT WINAPI HTMLDocument_get_applets(IHTMLDocument2 *iface, IHTMLElementCollection **p)
210 {
211     HTMLDocument *This = HTMLDOC_THIS(iface);
212     nsIDOMHTMLCollection *nscoll = NULL;
213     nsresult nsres;
214
215     TRACE("(%p)->(%p)\n", This, p);
216
217     if(!p)
218         return E_INVALIDARG;
219
220     *p = NULL;
221
222     if(!This->doc_node->nsdoc) {
223         WARN("NULL nsdoc\n");
224         return E_UNEXPECTED;
225     }
226
227     nsres = nsIDOMHTMLDocument_GetApplets(This->doc_node->nsdoc, &nscoll);
228     if(NS_FAILED(nsres)) {
229         ERR("GetApplets failed: %08x\n", nsres);
230         return E_FAIL;
231     }
232
233     if(nscoll) {
234         *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)HTMLDOC(This), nscoll);
235         nsIDOMElement_Release(nscoll);
236     }
237
238     return S_OK;
239 }
240
241 static HRESULT WINAPI HTMLDocument_get_links(IHTMLDocument2 *iface, IHTMLElementCollection **p)
242 {
243     HTMLDocument *This = HTMLDOC_THIS(iface);
244     nsIDOMHTMLCollection *nscoll = NULL;
245     nsresult nsres;
246
247     TRACE("(%p)->(%p)\n", This, p);
248
249     if(!p)
250         return E_INVALIDARG;
251
252     *p = NULL;
253
254     if(!This->doc_node->nsdoc) {
255         WARN("NULL nsdoc\n");
256         return E_UNEXPECTED;
257     }
258
259     nsres = nsIDOMHTMLDocument_GetLinks(This->doc_node->nsdoc, &nscoll);
260     if(NS_FAILED(nsres)) {
261         ERR("GetLinks failed: %08x\n", nsres);
262         return E_FAIL;
263     }
264
265     if(nscoll) {
266         *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)HTMLDOC(This), nscoll);
267         nsIDOMElement_Release(nscoll);
268     }
269
270     return S_OK;
271 }
272
273 static HRESULT WINAPI HTMLDocument_get_forms(IHTMLDocument2 *iface, IHTMLElementCollection **p)
274 {
275     HTMLDocument *This = HTMLDOC_THIS(iface);
276     nsIDOMHTMLCollection *nscoll = NULL;
277     nsresult nsres;
278
279     TRACE("(%p)->(%p)\n", This, p);
280
281     if(!p)
282         return E_INVALIDARG;
283
284     *p = NULL;
285
286     if(!This->doc_node->nsdoc) {
287         WARN("NULL nsdoc\n");
288         return E_UNEXPECTED;
289     }
290
291     nsres = nsIDOMHTMLDocument_GetForms(This->doc_node->nsdoc, &nscoll);
292     if(NS_FAILED(nsres)) {
293         ERR("GetForms failed: %08x\n", nsres);
294         return E_FAIL;
295     }
296
297     if(nscoll) {
298         *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)HTMLDOC(This), nscoll);
299         nsIDOMElement_Release(nscoll);
300     }
301
302     return S_OK;
303 }
304
305 static HRESULT WINAPI HTMLDocument_get_anchors(IHTMLDocument2 *iface, IHTMLElementCollection **p)
306 {
307     HTMLDocument *This = HTMLDOC_THIS(iface);
308     nsIDOMHTMLCollection *nscoll = NULL;
309     nsresult nsres;
310
311     TRACE("(%p)->(%p)\n", This, p);
312
313     if(!p)
314         return E_INVALIDARG;
315
316     *p = NULL;
317
318     if(!This->doc_node->nsdoc) {
319         WARN("NULL nsdoc\n");
320         return E_UNEXPECTED;
321     }
322
323     nsres = nsIDOMHTMLDocument_GetAnchors(This->doc_node->nsdoc, &nscoll);
324     if(NS_FAILED(nsres)) {
325         ERR("GetAnchors failed: %08x\n", nsres);
326         return E_FAIL;
327     }
328
329     if(nscoll) {
330         *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)HTMLDOC(This), nscoll);
331         nsIDOMElement_Release(nscoll);
332     }
333
334     return S_OK;
335 }
336
337 static HRESULT WINAPI HTMLDocument_put_title(IHTMLDocument2 *iface, BSTR v)
338 {
339     HTMLDocument *This = HTMLDOC_THIS(iface);
340     nsAString nsstr;
341     nsresult nsres;
342
343     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
344
345     if(!This->doc_node->nsdoc) {
346         WARN("NULL nsdoc\n");
347         return E_UNEXPECTED;
348     }
349
350     nsAString_Init(&nsstr, v);
351     nsres = nsIDOMHTMLDocument_SetTitle(This->doc_node->nsdoc, &nsstr);
352     nsAString_Finish(&nsstr);
353     if(NS_FAILED(nsres))
354         ERR("SetTitle failed: %08x\n", nsres);
355
356     return S_OK;
357 }
358
359 static HRESULT WINAPI HTMLDocument_get_title(IHTMLDocument2 *iface, BSTR *p)
360 {
361     HTMLDocument *This = HTMLDOC_THIS(iface);
362     const PRUnichar *ret;
363     nsAString nsstr;
364     nsresult nsres;
365
366     TRACE("(%p)->(%p)\n", This, p);
367
368     if(!This->doc_node->nsdoc) {
369         WARN("NULL nsdoc\n");
370         return E_UNEXPECTED;
371     }
372
373
374     nsAString_Init(&nsstr, NULL);
375     nsres = nsIDOMHTMLDocument_GetTitle(This->doc_node->nsdoc, &nsstr);
376     if (NS_SUCCEEDED(nsres)) {
377         nsAString_GetData(&nsstr, &ret);
378         *p = SysAllocString(ret);
379     }
380     nsAString_Finish(&nsstr);
381
382     if(NS_FAILED(nsres)) {
383         ERR("GetTitle failed: %08x\n", nsres);
384         return E_FAIL;
385     }
386
387     return S_OK;
388 }
389
390 static HRESULT WINAPI HTMLDocument_get_scripts(IHTMLDocument2 *iface, IHTMLElementCollection **p)
391 {
392     HTMLDocument *This = HTMLDOC_THIS(iface);
393     FIXME("(%p)->(%p)\n", This, p);
394     return E_NOTIMPL;
395 }
396
397 static HRESULT WINAPI HTMLDocument_put_designMode(IHTMLDocument2 *iface, BSTR v)
398 {
399     HTMLDocument *This = HTMLDOC_THIS(iface);
400     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
401     return E_NOTIMPL;
402 }
403
404 static HRESULT WINAPI HTMLDocument_get_designMode(IHTMLDocument2 *iface, BSTR *p)
405 {
406     HTMLDocument *This = HTMLDOC_THIS(iface);
407     static WCHAR szOff[] = {'O','f','f',0};
408     FIXME("(%p)->(%p) always returning Off\n", This, p);
409
410     if(!p)
411         return E_INVALIDARG;
412
413     *p = SysAllocString(szOff);
414
415     return S_OK;
416 }
417
418 static HRESULT WINAPI HTMLDocument_get_selection(IHTMLDocument2 *iface, IHTMLSelectionObject **p)
419 {
420     HTMLDocument *This = HTMLDOC_THIS(iface);
421     nsISelection *nsselection;
422     nsresult nsres;
423
424     TRACE("(%p)->(%p)\n", This, p);
425
426     nsres = nsIDOMWindow_GetSelection(This->window->nswindow, &nsselection);
427     if(NS_FAILED(nsres)) {
428         ERR("GetSelection failed: %08x\n", nsres);
429         return E_FAIL;
430     }
431
432     return HTMLSelectionObject_Create(This->doc_node, nsselection, p);
433 }
434
435 static HRESULT WINAPI HTMLDocument_get_readyState(IHTMLDocument2 *iface, BSTR *p)
436 {
437     HTMLDocument *This = HTMLDOC_THIS(iface);
438
439     static const WCHAR wszUninitialized[] = {'u','n','i','n','i','t','i','a','l','i','z','e','d',0};
440     static const WCHAR wszLoading[] = {'l','o','a','d','i','n','g',0};
441     static const WCHAR wszLoaded[] = {'l','o','a','d','e','d',0};
442     static const WCHAR wszInteractive[] = {'i','n','t','e','r','a','c','t','i','v','e',0};
443     static const WCHAR wszComplete[] = {'c','o','m','p','l','e','t','e',0};
444
445     static const LPCWSTR readystate_str[] = {
446         wszUninitialized,
447         wszLoading,
448         wszLoaded,
449         wszInteractive,
450         wszComplete
451     };
452
453     TRACE("(%p)->(%p)\n", iface, p);
454
455     if(!p)
456         return E_POINTER;
457
458     *p = SysAllocString(readystate_str[This->doc_obj->readystate]);
459     return S_OK;
460 }
461
462 static HRESULT WINAPI HTMLDocument_get_frames(IHTMLDocument2 *iface, IHTMLFramesCollection2 **p)
463 {
464     HTMLDocument *This = HTMLDOC_THIS(iface);
465     FIXME("(%p)->(%p)\n", This, p);
466     return E_NOTIMPL;
467 }
468
469 static HRESULT WINAPI HTMLDocument_get_embeds(IHTMLDocument2 *iface, IHTMLElementCollection **p)
470 {
471     HTMLDocument *This = HTMLDOC_THIS(iface);
472     FIXME("(%p)->(%p)\n", This, p);
473     return E_NOTIMPL;
474 }
475
476 static HRESULT WINAPI HTMLDocument_get_plugins(IHTMLDocument2 *iface, IHTMLElementCollection **p)
477 {
478     HTMLDocument *This = HTMLDOC_THIS(iface);
479     FIXME("(%p)->(%p)\n", This, p);
480     return E_NOTIMPL;
481 }
482
483 static HRESULT WINAPI HTMLDocument_put_alinkColor(IHTMLDocument2 *iface, VARIANT v)
484 {
485     HTMLDocument *This = HTMLDOC_THIS(iface);
486     FIXME("(%p)\n", This);
487     return E_NOTIMPL;
488 }
489
490 static HRESULT WINAPI HTMLDocument_get_alinkColor(IHTMLDocument2 *iface, VARIANT *p)
491 {
492     HTMLDocument *This = HTMLDOC_THIS(iface);
493     FIXME("(%p)->(%p)\n", This, p);
494     return E_NOTIMPL;
495 }
496
497 static HRESULT WINAPI HTMLDocument_put_bgColor(IHTMLDocument2 *iface, VARIANT v)
498 {
499     HTMLDocument *This = HTMLDOC_THIS(iface);
500     FIXME("(%p)\n", This);
501     return E_NOTIMPL;
502 }
503
504 static HRESULT WINAPI HTMLDocument_get_bgColor(IHTMLDocument2 *iface, VARIANT *p)
505 {
506     HTMLDocument *This = HTMLDOC_THIS(iface);
507     FIXME("(%p)->(%p)\n", This, p);
508     return E_NOTIMPL;
509 }
510
511 static HRESULT WINAPI HTMLDocument_put_fgColor(IHTMLDocument2 *iface, VARIANT v)
512 {
513     HTMLDocument *This = HTMLDOC_THIS(iface);
514     FIXME("(%p)\n", This);
515     return E_NOTIMPL;
516 }
517
518 static HRESULT WINAPI HTMLDocument_get_fgColor(IHTMLDocument2 *iface, VARIANT *p)
519 {
520     HTMLDocument *This = HTMLDOC_THIS(iface);
521     FIXME("(%p)->(%p)\n", This, p);
522     return E_NOTIMPL;
523 }
524
525 static HRESULT WINAPI HTMLDocument_put_linkColor(IHTMLDocument2 *iface, VARIANT v)
526 {
527     HTMLDocument *This = HTMLDOC_THIS(iface);
528     FIXME("(%p)->()\n", This);
529     return E_NOTIMPL;
530 }
531
532 static HRESULT WINAPI HTMLDocument_get_linkColor(IHTMLDocument2 *iface, VARIANT *p)
533 {
534     HTMLDocument *This = HTMLDOC_THIS(iface);
535     FIXME("(%p)->(%p)\n", This, p);
536     return E_NOTIMPL;
537 }
538
539 static HRESULT WINAPI HTMLDocument_put_vlinkColor(IHTMLDocument2 *iface, VARIANT v)
540 {
541     HTMLDocument *This = HTMLDOC_THIS(iface);
542     FIXME("(%p)\n", This);
543     return E_NOTIMPL;
544 }
545
546 static HRESULT WINAPI HTMLDocument_get_vlinkColor(IHTMLDocument2 *iface, VARIANT *p)
547 {
548     HTMLDocument *This = HTMLDOC_THIS(iface);
549     FIXME("(%p)->(%p)\n", This, p);
550     return E_NOTIMPL;
551 }
552
553 static HRESULT WINAPI HTMLDocument_get_referrer(IHTMLDocument2 *iface, BSTR *p)
554 {
555     HTMLDocument *This = HTMLDOC_THIS(iface);
556     FIXME("(%p)->(%p)\n", This, p);
557     return E_NOTIMPL;
558 }
559
560 static HRESULT WINAPI HTMLDocument_get_location(IHTMLDocument2 *iface, IHTMLLocation **p)
561 {
562     HTMLDocument *This = HTMLDOC_THIS(iface);
563
564     TRACE("(%p)->(%p)\n", This, p);
565
566     return IHTMLWindow2_get_location(HTMLWINDOW2(This->window), p);
567 }
568
569 static HRESULT WINAPI HTMLDocument_get_lastModified(IHTMLDocument2 *iface, BSTR *p)
570 {
571     HTMLDocument *This = HTMLDOC_THIS(iface);
572     FIXME("(%p)->(%p)\n", This, p);
573     return E_NOTIMPL;
574 }
575
576 static HRESULT WINAPI HTMLDocument_put_URL(IHTMLDocument2 *iface, BSTR v)
577 {
578     HTMLDocument *This = HTMLDOC_THIS(iface);
579     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
580     return E_NOTIMPL;
581 }
582
583 static HRESULT WINAPI HTMLDocument_get_URL(IHTMLDocument2 *iface, BSTR *p)
584 {
585     HTMLDocument *This = HTMLDOC_THIS(iface);
586
587     static const WCHAR about_blank_url[] =
588         {'a','b','o','u','t',':','b','l','a','n','k',0};
589
590     TRACE("(%p)->(%p)\n", iface, p);
591
592     *p = SysAllocString(This->window->url ? This->window->url : about_blank_url);
593     return *p ? S_OK : E_OUTOFMEMORY;
594 }
595
596 static HRESULT WINAPI HTMLDocument_put_domain(IHTMLDocument2 *iface, BSTR v)
597 {
598     HTMLDocument *This = HTMLDOC_THIS(iface);
599     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
600     return E_NOTIMPL;
601 }
602
603 static HRESULT WINAPI HTMLDocument_get_domain(IHTMLDocument2 *iface, BSTR *p)
604 {
605     HTMLDocument *This = HTMLDOC_THIS(iface);
606     FIXME("(%p)->(%p)\n", This, p);
607     return E_NOTIMPL;
608 }
609
610 static HRESULT WINAPI HTMLDocument_put_cookie(IHTMLDocument2 *iface, BSTR v)
611 {
612     HTMLDocument *This = HTMLDOC_THIS(iface);
613     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
614     return E_NOTIMPL;
615 }
616
617 static HRESULT WINAPI HTMLDocument_get_cookie(IHTMLDocument2 *iface, BSTR *p)
618 {
619     HTMLDocument *This = HTMLDOC_THIS(iface);
620     FIXME("(%p)->(%p)\n", This, p);
621     return E_NOTIMPL;
622 }
623
624 static HRESULT WINAPI HTMLDocument_put_expando(IHTMLDocument2 *iface, VARIANT_BOOL v)
625 {
626     HTMLDocument *This = HTMLDOC_THIS(iface);
627     FIXME("(%p)->(%x)\n", This, v);
628     return E_NOTIMPL;
629 }
630
631 static HRESULT WINAPI HTMLDocument_get_expando(IHTMLDocument2 *iface, VARIANT_BOOL *p)
632 {
633     HTMLDocument *This = HTMLDOC_THIS(iface);
634     FIXME("(%p)->(%p)\n", This, p);
635     return E_NOTIMPL;
636 }
637
638 static HRESULT WINAPI HTMLDocument_put_charset(IHTMLDocument2 *iface, BSTR v)
639 {
640     HTMLDocument *This = HTMLDOC_THIS(iface);
641     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
642     return E_NOTIMPL;
643 }
644
645 static HRESULT WINAPI HTMLDocument_get_charset(IHTMLDocument2 *iface, BSTR *p)
646 {
647     HTMLDocument *This = HTMLDOC_THIS(iface);
648     FIXME("(%p)->(%p)\n", This, p);
649     return E_NOTIMPL;
650 }
651
652 static HRESULT WINAPI HTMLDocument_put_defaultCharset(IHTMLDocument2 *iface, BSTR v)
653 {
654     HTMLDocument *This = HTMLDOC_THIS(iface);
655     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
656     return E_NOTIMPL;
657 }
658
659 static HRESULT WINAPI HTMLDocument_get_defaultCharset(IHTMLDocument2 *iface, BSTR *p)
660 {
661     HTMLDocument *This = HTMLDOC_THIS(iface);
662     FIXME("(%p)->(%p)\n", This, p);
663     return E_NOTIMPL;
664 }
665
666 static HRESULT WINAPI HTMLDocument_get_mimeType(IHTMLDocument2 *iface, BSTR *p)
667 {
668     HTMLDocument *This = HTMLDOC_THIS(iface);
669     FIXME("(%p)->(%p)\n", This, p);
670     return E_NOTIMPL;
671 }
672
673 static HRESULT WINAPI HTMLDocument_get_fileSize(IHTMLDocument2 *iface, BSTR *p)
674 {
675     HTMLDocument *This = HTMLDOC_THIS(iface);
676     FIXME("(%p)->(%p)\n", This, p);
677     return E_NOTIMPL;
678 }
679
680 static HRESULT WINAPI HTMLDocument_get_fileCreatedDate(IHTMLDocument2 *iface, BSTR *p)
681 {
682     HTMLDocument *This = HTMLDOC_THIS(iface);
683     FIXME("(%p)->(%p)\n", This, p);
684     return E_NOTIMPL;
685 }
686
687 static HRESULT WINAPI HTMLDocument_get_fileModifiedDate(IHTMLDocument2 *iface, BSTR *p)
688 {
689     HTMLDocument *This = HTMLDOC_THIS(iface);
690     FIXME("(%p)->(%p)\n", This, p);
691     return E_NOTIMPL;
692 }
693
694 static HRESULT WINAPI HTMLDocument_get_fileUpdatedDate(IHTMLDocument2 *iface, BSTR *p)
695 {
696     HTMLDocument *This = HTMLDOC_THIS(iface);
697     FIXME("(%p)->(%p)\n", This, p);
698     return E_NOTIMPL;
699 }
700
701 static HRESULT WINAPI HTMLDocument_get_security(IHTMLDocument2 *iface, BSTR *p)
702 {
703     HTMLDocument *This = HTMLDOC_THIS(iface);
704     FIXME("(%p)->(%p)\n", This, p);
705     return E_NOTIMPL;
706 }
707
708 static HRESULT WINAPI HTMLDocument_get_protocol(IHTMLDocument2 *iface, BSTR *p)
709 {
710     HTMLDocument *This = HTMLDOC_THIS(iface);
711     FIXME("(%p)->(%p)\n", This, p);
712     return E_NOTIMPL;
713 }
714
715 static HRESULT WINAPI HTMLDocument_get_nameProp(IHTMLDocument2 *iface, BSTR *p)
716 {
717     HTMLDocument *This = HTMLDOC_THIS(iface);
718     FIXME("(%p)->(%p)\n", This, p);
719     return E_NOTIMPL;
720 }
721
722 static HRESULT document_write(HTMLDocument *This, SAFEARRAY *psarray, BOOL ln)
723 {
724     nsAString nsstr;
725     VARIANT *var;
726     ULONG i, argc;
727     nsresult nsres;
728     HRESULT hres;
729
730     if(!This->doc_node->nsdoc) {
731         WARN("NULL nsdoc\n");
732         return E_UNEXPECTED;
733     }
734
735     if(psarray->cDims != 1) {
736         FIXME("cDims=%d\n", psarray->cDims);
737         return E_INVALIDARG;
738     }
739
740     hres = SafeArrayAccessData(psarray, (void**)&var);
741     if(FAILED(hres)) {
742         WARN("SafeArrayAccessData failed: %08x\n", hres);
743         return hres;
744     }
745
746     nsAString_Init(&nsstr, NULL);
747
748     argc = psarray->rgsabound[0].cElements;
749     for(i=0; i < argc; i++) {
750         if(V_VT(var+i) == VT_BSTR) {
751             nsAString_SetData(&nsstr, V_BSTR(var+i));
752             if(!ln || i != argc-1)
753                 nsres = nsIDOMHTMLDocument_Write(This->doc_node->nsdoc, &nsstr);
754             else
755                 nsres = nsIDOMHTMLDocument_Writeln(This->doc_node->nsdoc, &nsstr);
756             if(NS_FAILED(nsres))
757                 ERR("Write failed: %08x\n", nsres);
758         }else {
759             FIXME("vt=%d\n", V_VT(var+i));
760         }
761     }
762
763     nsAString_Finish(&nsstr);
764     SafeArrayUnaccessData(psarray);
765
766     return S_OK;
767 }
768
769 static HRESULT WINAPI HTMLDocument_write(IHTMLDocument2 *iface, SAFEARRAY *psarray)
770 {
771     HTMLDocument *This = HTMLDOC_THIS(iface);
772
773     TRACE("(%p)->(%p)\n", iface, psarray);
774
775     return document_write(This, psarray, FALSE);
776 }
777
778 static HRESULT WINAPI HTMLDocument_writeln(IHTMLDocument2 *iface, SAFEARRAY *psarray)
779 {
780     HTMLDocument *This = HTMLDOC_THIS(iface);
781
782     TRACE("(%p)->(%p)\n", This, psarray);
783
784     return document_write(This, psarray, TRUE);
785 }
786
787 static HRESULT WINAPI HTMLDocument_open(IHTMLDocument2 *iface, BSTR url, VARIANT name,
788                         VARIANT features, VARIANT replace, IDispatch **pomWindowResult)
789 {
790     HTMLDocument *This = HTMLDOC_THIS(iface);
791     nsresult nsres;
792
793     static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};
794
795     TRACE("(%p)->(%s %s %s %s %p)\n", This, debugstr_w(url), debugstr_variant(&name),
796           debugstr_variant(&features), debugstr_variant(&replace), pomWindowResult);
797
798     if(!This->doc_node->nsdoc) {
799         ERR("!nsdoc\n");
800         return E_NOTIMPL;
801     }
802
803     if(!url || strcmpW(url, text_htmlW) || V_VT(&name) != VT_ERROR
804        || V_VT(&features) != VT_ERROR || V_VT(&replace) != VT_ERROR)
805         FIXME("unsupported args\n");
806
807     nsres = nsIDOMHTMLDocument_Open(This->doc_node->nsdoc);
808     if(NS_FAILED(nsres)) {
809         ERR("Open failed: %08x\n", nsres);
810         return E_FAIL;
811     }
812
813     *pomWindowResult = (IDispatch*)HTMLWINDOW2(This->window);
814     IHTMLWindow2_AddRef(HTMLWINDOW2(This->window));
815     return S_OK;
816 }
817
818 static HRESULT WINAPI HTMLDocument_close(IHTMLDocument2 *iface)
819 {
820     HTMLDocument *This = HTMLDOC_THIS(iface);
821     nsresult nsres;
822
823     TRACE("(%p)\n", This);
824
825     if(!This->doc_node->nsdoc) {
826         ERR("!nsdoc\n");
827         return E_NOTIMPL;
828     }
829
830     nsres = nsIDOMHTMLDocument_Close(This->doc_node->nsdoc);
831     if(NS_FAILED(nsres)) {
832         ERR("Close failed: %08x\n", nsres);
833         return E_FAIL;
834     }
835
836     return S_OK;
837 }
838
839 static HRESULT WINAPI HTMLDocument_clear(IHTMLDocument2 *iface)
840 {
841     HTMLDocument *This = HTMLDOC_THIS(iface);
842     FIXME("(%p)\n", This);
843     return E_NOTIMPL;
844 }
845
846 static HRESULT WINAPI HTMLDocument_queryCommandSupported(IHTMLDocument2 *iface, BSTR cmdID,
847                                                         VARIANT_BOOL *pfRet)
848 {
849     HTMLDocument *This = HTMLDOC_THIS(iface);
850     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
851     return E_NOTIMPL;
852 }
853
854 static HRESULT WINAPI HTMLDocument_queryCommandEnabled(IHTMLDocument2 *iface, BSTR cmdID,
855                                                         VARIANT_BOOL *pfRet)
856 {
857     HTMLDocument *This = HTMLDOC_THIS(iface);
858     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
859     return E_NOTIMPL;
860 }
861
862 static HRESULT WINAPI HTMLDocument_queryCommandState(IHTMLDocument2 *iface, BSTR cmdID,
863                                                         VARIANT_BOOL *pfRet)
864 {
865     HTMLDocument *This = HTMLDOC_THIS(iface);
866     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
867     return E_NOTIMPL;
868 }
869
870 static HRESULT WINAPI HTMLDocument_queryCommandIndeterm(IHTMLDocument2 *iface, BSTR cmdID,
871                                                         VARIANT_BOOL *pfRet)
872 {
873     HTMLDocument *This = HTMLDOC_THIS(iface);
874     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
875     return E_NOTIMPL;
876 }
877
878 static HRESULT WINAPI HTMLDocument_queryCommandText(IHTMLDocument2 *iface, BSTR cmdID,
879                                                         BSTR *pfRet)
880 {
881     HTMLDocument *This = HTMLDOC_THIS(iface);
882     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
883     return E_NOTIMPL;
884 }
885
886 static HRESULT WINAPI HTMLDocument_queryCommandValue(IHTMLDocument2 *iface, BSTR cmdID,
887                                                         VARIANT *pfRet)
888 {
889     HTMLDocument *This = HTMLDOC_THIS(iface);
890     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
891     return E_NOTIMPL;
892 }
893
894 static HRESULT WINAPI HTMLDocument_execCommand(IHTMLDocument2 *iface, BSTR cmdID,
895                                 VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
896 {
897     HTMLDocument *This = HTMLDOC_THIS(iface);
898     FIXME("(%p)->(%s %x %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
899     return E_NOTIMPL;
900 }
901
902 static HRESULT WINAPI HTMLDocument_execCommandShowHelp(IHTMLDocument2 *iface, BSTR cmdID,
903                                                         VARIANT_BOOL *pfRet)
904 {
905     HTMLDocument *This = HTMLDOC_THIS(iface);
906     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
907     return E_NOTIMPL;
908 }
909
910 static HRESULT WINAPI HTMLDocument_createElement(IHTMLDocument2 *iface, BSTR eTag,
911                                                  IHTMLElement **newElem)
912 {
913     HTMLDocument *This = HTMLDOC_THIS(iface);
914     nsIDOMHTMLElement *nselem;
915     HTMLElement *elem;
916     HRESULT hres;
917
918     TRACE("(%p)->(%s %p)\n", This, debugstr_w(eTag), newElem);
919
920     hres = create_nselem(This->doc_node, eTag, &nselem);
921     if(FAILED(hres))
922         return hres;
923
924     elem = HTMLElement_Create(This->doc_node, (nsIDOMNode*)nselem, TRUE);
925     nsIDOMHTMLElement_Release(nselem);
926
927     *newElem = HTMLELEM(elem);
928     IHTMLElement_AddRef(HTMLELEM(elem));
929     return S_OK;
930 }
931
932 static HRESULT WINAPI HTMLDocument_put_onhelp(IHTMLDocument2 *iface, VARIANT v)
933 {
934     HTMLDocument *This = HTMLDOC_THIS(iface);
935     FIXME("(%p)\n", This);
936     return E_NOTIMPL;
937 }
938
939 static HRESULT WINAPI HTMLDocument_get_onhelp(IHTMLDocument2 *iface, VARIANT *p)
940 {
941     HTMLDocument *This = HTMLDOC_THIS(iface);
942     FIXME("(%p)->(%p)\n", This, p);
943     return E_NOTIMPL;
944 }
945
946 static HRESULT WINAPI HTMLDocument_put_onclick(IHTMLDocument2 *iface, VARIANT v)
947 {
948     HTMLDocument *This = HTMLDOC_THIS(iface);
949
950     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
951
952     return set_doc_event(This, EVENTID_CLICK, &v);
953 }
954
955 static HRESULT WINAPI HTMLDocument_get_onclick(IHTMLDocument2 *iface, VARIANT *p)
956 {
957     HTMLDocument *This = HTMLDOC_THIS(iface);
958
959     TRACE("(%p)->(%p)\n", This, p);
960
961     return get_doc_event(This, EVENTID_CLICK, p);
962 }
963
964 static HRESULT WINAPI HTMLDocument_put_ondblclick(IHTMLDocument2 *iface, VARIANT v)
965 {
966     HTMLDocument *This = HTMLDOC_THIS(iface);
967     FIXME("(%p)\n", This);
968     return E_NOTIMPL;
969 }
970
971 static HRESULT WINAPI HTMLDocument_get_ondblclick(IHTMLDocument2 *iface, VARIANT *p)
972 {
973     HTMLDocument *This = HTMLDOC_THIS(iface);
974     FIXME("(%p)->(%p)\n", This, p);
975     return E_NOTIMPL;
976 }
977
978 static HRESULT WINAPI HTMLDocument_put_onkeyup(IHTMLDocument2 *iface, VARIANT v)
979 {
980     HTMLDocument *This = HTMLDOC_THIS(iface);
981
982     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
983
984     return set_doc_event(This, EVENTID_KEYUP, &v);
985 }
986
987 static HRESULT WINAPI HTMLDocument_get_onkeyup(IHTMLDocument2 *iface, VARIANT *p)
988 {
989     HTMLDocument *This = HTMLDOC_THIS(iface);
990
991     TRACE("(%p)->(%p)\n", This, p);
992
993     return get_doc_event(This, EVENTID_KEYUP, p);
994 }
995
996 static HRESULT WINAPI HTMLDocument_put_onkeydown(IHTMLDocument2 *iface, VARIANT v)
997 {
998     HTMLDocument *This = HTMLDOC_THIS(iface);
999
1000     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1001
1002     return set_doc_event(This, EVENTID_KEYDOWN, &v);
1003 }
1004
1005 static HRESULT WINAPI HTMLDocument_get_onkeydown(IHTMLDocument2 *iface, VARIANT *p)
1006 {
1007     HTMLDocument *This = HTMLDOC_THIS(iface);
1008
1009     TRACE("(%p)->(%p)\n", This, p);
1010
1011     return get_doc_event(This, EVENTID_KEYDOWN, p);
1012 }
1013
1014 static HRESULT WINAPI HTMLDocument_put_onkeypress(IHTMLDocument2 *iface, VARIANT v)
1015 {
1016     HTMLDocument *This = HTMLDOC_THIS(iface);
1017     FIXME("(%p)\n", This);
1018     return E_NOTIMPL;
1019 }
1020
1021 static HRESULT WINAPI HTMLDocument_get_onkeypress(IHTMLDocument2 *iface, VARIANT *p)
1022 {
1023     HTMLDocument *This = HTMLDOC_THIS(iface);
1024     FIXME("(%p)->(%p)\n", This, p);
1025     return E_NOTIMPL;
1026 }
1027
1028 static HRESULT WINAPI HTMLDocument_put_onmouseup(IHTMLDocument2 *iface, VARIANT v)
1029 {
1030     HTMLDocument *This = HTMLDOC_THIS(iface);
1031     FIXME("(%p)\n", This);
1032     return E_NOTIMPL;
1033 }
1034
1035 static HRESULT WINAPI HTMLDocument_get_onmouseup(IHTMLDocument2 *iface, VARIANT *p)
1036 {
1037     HTMLDocument *This = HTMLDOC_THIS(iface);
1038     FIXME("(%p)->(%p)\n", This, p);
1039     return E_NOTIMPL;
1040 }
1041
1042 static HRESULT WINAPI HTMLDocument_put_onmousedown(IHTMLDocument2 *iface, VARIANT v)
1043 {
1044     HTMLDocument *This = HTMLDOC_THIS(iface);
1045     FIXME("(%p)\n", This);
1046     return E_NOTIMPL;
1047 }
1048
1049 static HRESULT WINAPI HTMLDocument_get_onmousedown(IHTMLDocument2 *iface, VARIANT *p)
1050 {
1051     HTMLDocument *This = HTMLDOC_THIS(iface);
1052     FIXME("(%p)->(%p)\n", This, p);
1053     return E_NOTIMPL;
1054 }
1055
1056 static HRESULT WINAPI HTMLDocument_put_onmousemove(IHTMLDocument2 *iface, VARIANT v)
1057 {
1058     HTMLDocument *This = HTMLDOC_THIS(iface);
1059     FIXME("(%p)\n", This);
1060     return E_NOTIMPL;
1061 }
1062
1063 static HRESULT WINAPI HTMLDocument_get_onmousemove(IHTMLDocument2 *iface, VARIANT *p)
1064 {
1065     HTMLDocument *This = HTMLDOC_THIS(iface);
1066     FIXME("(%p)->(%p)\n", This, p);
1067     return E_NOTIMPL;
1068 }
1069
1070 static HRESULT WINAPI HTMLDocument_put_onmouseout(IHTMLDocument2 *iface, VARIANT v)
1071 {
1072     HTMLDocument *This = HTMLDOC_THIS(iface);
1073     FIXME("(%p)\n", This);
1074     return E_NOTIMPL;
1075 }
1076
1077 static HRESULT WINAPI HTMLDocument_get_onmouseout(IHTMLDocument2 *iface, VARIANT *p)
1078 {
1079     HTMLDocument *This = HTMLDOC_THIS(iface);
1080     FIXME("(%p)->(%p)\n", This, p);
1081     return E_NOTIMPL;
1082 }
1083
1084 static HRESULT WINAPI HTMLDocument_put_onmouseover(IHTMLDocument2 *iface, VARIANT v)
1085 {
1086     HTMLDocument *This = HTMLDOC_THIS(iface);
1087
1088     TRACE("(%p)\n", This);
1089
1090     return set_doc_event(This, EVENTID_MOUSEOVER, &v);
1091 }
1092
1093 static HRESULT WINAPI HTMLDocument_get_onmouseover(IHTMLDocument2 *iface, VARIANT *p)
1094 {
1095     HTMLDocument *This = HTMLDOC_THIS(iface);
1096
1097     TRACE("(%p)->(%p)\n", This, p);
1098
1099     return get_doc_event(This, EVENTID_MOUSEOVER, p);
1100 }
1101
1102 static HRESULT WINAPI HTMLDocument_put_onreadystatechange(IHTMLDocument2 *iface, VARIANT v)
1103 {
1104     HTMLDocument *This = HTMLDOC_THIS(iface);
1105     FIXME("(%p)\n", This);
1106     return E_NOTIMPL;
1107 }
1108
1109 static HRESULT WINAPI HTMLDocument_get_onreadystatechange(IHTMLDocument2 *iface, VARIANT *p)
1110 {
1111     HTMLDocument *This = HTMLDOC_THIS(iface);
1112     FIXME("(%p)->(%p)\n", This, p);
1113     return E_NOTIMPL;
1114 }
1115
1116 static HRESULT WINAPI HTMLDocument_put_onafterupdate(IHTMLDocument2 *iface, VARIANT v)
1117 {
1118     HTMLDocument *This = HTMLDOC_THIS(iface);
1119     FIXME("(%p)\n", This);
1120     return E_NOTIMPL;
1121 }
1122
1123 static HRESULT WINAPI HTMLDocument_get_onafterupdate(IHTMLDocument2 *iface, VARIANT *p)
1124 {
1125     HTMLDocument *This = HTMLDOC_THIS(iface);
1126     FIXME("(%p)->(%p)\n", This, p);
1127     return E_NOTIMPL;
1128 }
1129
1130 static HRESULT WINAPI HTMLDocument_put_onrowexit(IHTMLDocument2 *iface, VARIANT v)
1131 {
1132     HTMLDocument *This = HTMLDOC_THIS(iface);
1133     FIXME("(%p)\n", This);
1134     return E_NOTIMPL;
1135 }
1136
1137 static HRESULT WINAPI HTMLDocument_get_onrowexit(IHTMLDocument2 *iface, VARIANT *p)
1138 {
1139     HTMLDocument *This = HTMLDOC_THIS(iface);
1140     FIXME("(%p)->(%p)\n", This, p);
1141     return E_NOTIMPL;
1142 }
1143
1144 static HRESULT WINAPI HTMLDocument_put_onrowenter(IHTMLDocument2 *iface, VARIANT v)
1145 {
1146     HTMLDocument *This = HTMLDOC_THIS(iface);
1147     FIXME("(%p)\n", This);
1148     return E_NOTIMPL;
1149 }
1150
1151 static HRESULT WINAPI HTMLDocument_get_onrowenter(IHTMLDocument2 *iface, VARIANT *p)
1152 {
1153     HTMLDocument *This = HTMLDOC_THIS(iface);
1154     FIXME("(%p)->(%p)\n", This, p);
1155     return E_NOTIMPL;
1156 }
1157
1158 static HRESULT WINAPI HTMLDocument_put_ondragstart(IHTMLDocument2 *iface, VARIANT v)
1159 {
1160     HTMLDocument *This = HTMLDOC_THIS(iface);
1161
1162     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1163
1164     return set_doc_event(This, EVENTID_DRAGSTART, &v);
1165 }
1166
1167 static HRESULT WINAPI HTMLDocument_get_ondragstart(IHTMLDocument2 *iface, VARIANT *p)
1168 {
1169     HTMLDocument *This = HTMLDOC_THIS(iface);
1170
1171     TRACE("(%p)->(%p)\n", This, p);
1172
1173     return get_doc_event(This, EVENTID_DRAGSTART, p);
1174 }
1175
1176 static HRESULT WINAPI HTMLDocument_put_onselectstart(IHTMLDocument2 *iface, VARIANT v)
1177 {
1178     HTMLDocument *This = HTMLDOC_THIS(iface);
1179
1180     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1181
1182     return set_doc_event(This, EVENTID_SELECTSTART, &v);
1183 }
1184
1185 static HRESULT WINAPI HTMLDocument_get_onselectstart(IHTMLDocument2 *iface, VARIANT *p)
1186 {
1187     HTMLDocument *This = HTMLDOC_THIS(iface);
1188
1189     TRACE("(%p)->(%p)\n", This, p);
1190
1191     return get_doc_event(This, EVENTID_SELECTSTART, p);
1192 }
1193
1194 static HRESULT WINAPI HTMLDocument_elementFromPoint(IHTMLDocument2 *iface, LONG x, LONG y,
1195                                                         IHTMLElement **elementHit)
1196 {
1197     HTMLDocument *This = HTMLDOC_THIS(iface);
1198     FIXME("(%p)->(%d %d %p)\n", This, x, y, elementHit);
1199     return E_NOTIMPL;
1200 }
1201
1202 static HRESULT WINAPI HTMLDocument_get_parentWindow(IHTMLDocument2 *iface, IHTMLWindow2 **p)
1203 {
1204     HTMLDocument *This = HTMLDOC_THIS(iface);
1205
1206     TRACE("(%p)->(%p)\n", This, p);
1207
1208     *p = HTMLWINDOW2(This->window);
1209     IHTMLWindow2_AddRef(*p);
1210     return S_OK;
1211 }
1212
1213 static HRESULT WINAPI HTMLDocument_get_styleSheets(IHTMLDocument2 *iface,
1214                                                    IHTMLStyleSheetsCollection **p)
1215 {
1216     HTMLDocument *This = HTMLDOC_THIS(iface);
1217     nsIDOMStyleSheetList *nsstylelist;
1218     nsIDOMDocumentStyle *nsdocstyle;
1219     nsresult nsres;
1220
1221     TRACE("(%p)->(%p)\n", This, p);
1222
1223     *p = NULL;
1224
1225     if(!This->doc_node->nsdoc) {
1226         WARN("NULL nsdoc\n");
1227         return E_UNEXPECTED;
1228     }
1229
1230     nsIDOMHTMLDocument_QueryInterface(This->doc_node->nsdoc, &IID_nsIDOMDocumentStyle, (void**)&nsdocstyle);
1231     nsres = nsIDOMDocumentStyle_GetStyleSheets(nsdocstyle, &nsstylelist);
1232     nsIDOMDocumentStyle_Release(nsdocstyle);
1233     if(NS_FAILED(nsres)) {
1234         ERR("GetStyleSheets failed: %08x\n", nsres);
1235         return E_FAIL;
1236     }
1237
1238     *p = HTMLStyleSheetsCollection_Create(nsstylelist);
1239     nsIDOMDocumentStyle_Release(nsstylelist);
1240
1241     return S_OK;
1242 }
1243
1244 static HRESULT WINAPI HTMLDocument_put_onbeforeupdate(IHTMLDocument2 *iface, VARIANT v)
1245 {
1246     HTMLDocument *This = HTMLDOC_THIS(iface);
1247     FIXME("(%p)\n", This);
1248     return E_NOTIMPL;
1249 }
1250
1251 static HRESULT WINAPI HTMLDocument_get_onbeforeupdate(IHTMLDocument2 *iface, VARIANT *p)
1252 {
1253     HTMLDocument *This = HTMLDOC_THIS(iface);
1254     FIXME("(%p)->(%p)\n", This, p);
1255     return E_NOTIMPL;
1256 }
1257
1258 static HRESULT WINAPI HTMLDocument_put_onerrorupdate(IHTMLDocument2 *iface, VARIANT v)
1259 {
1260     HTMLDocument *This = HTMLDOC_THIS(iface);
1261     FIXME("(%p)\n", This);
1262     return E_NOTIMPL;
1263 }
1264
1265 static HRESULT WINAPI HTMLDocument_get_onerrorupdate(IHTMLDocument2 *iface, VARIANT *p)
1266 {
1267     HTMLDocument *This = HTMLDOC_THIS(iface);
1268     FIXME("(%p)->(%p)\n", This, p);
1269     return E_NOTIMPL;
1270 }
1271
1272 static HRESULT WINAPI HTMLDocument_toString(IHTMLDocument2 *iface, BSTR *String)
1273 {
1274     HTMLDocument *This = HTMLDOC_THIS(iface);
1275     FIXME("(%p)->(%p)\n", This, String);
1276     return E_NOTIMPL;
1277 }
1278
1279 static HRESULT WINAPI HTMLDocument_createStyleSheet(IHTMLDocument2 *iface, BSTR bstrHref,
1280                                             LONG lIndex, IHTMLStyleSheet **ppnewStyleSheet)
1281 {
1282     HTMLDocument *This = HTMLDOC_THIS(iface);
1283
1284     FIXME("(%p)->(%s %d %p) semi-stub\n", This, debugstr_w(bstrHref), lIndex, ppnewStyleSheet);
1285
1286     *ppnewStyleSheet = HTMLStyleSheet_Create(NULL);
1287     return S_OK;
1288 }
1289
1290 static const IHTMLDocument2Vtbl HTMLDocumentVtbl = {
1291     HTMLDocument_QueryInterface,
1292     HTMLDocument_AddRef,
1293     HTMLDocument_Release,
1294     HTMLDocument_GetTypeInfoCount,
1295     HTMLDocument_GetTypeInfo,
1296     HTMLDocument_GetIDsOfNames,
1297     HTMLDocument_Invoke,
1298     HTMLDocument_get_Script,
1299     HTMLDocument_get_all,
1300     HTMLDocument_get_body,
1301     HTMLDocument_get_activeElement,
1302     HTMLDocument_get_images,
1303     HTMLDocument_get_applets,
1304     HTMLDocument_get_links,
1305     HTMLDocument_get_forms,
1306     HTMLDocument_get_anchors,
1307     HTMLDocument_put_title,
1308     HTMLDocument_get_title,
1309     HTMLDocument_get_scripts,
1310     HTMLDocument_put_designMode,
1311     HTMLDocument_get_designMode,
1312     HTMLDocument_get_selection,
1313     HTMLDocument_get_readyState,
1314     HTMLDocument_get_frames,
1315     HTMLDocument_get_embeds,
1316     HTMLDocument_get_plugins,
1317     HTMLDocument_put_alinkColor,
1318     HTMLDocument_get_alinkColor,
1319     HTMLDocument_put_bgColor,
1320     HTMLDocument_get_bgColor,
1321     HTMLDocument_put_fgColor,
1322     HTMLDocument_get_fgColor,
1323     HTMLDocument_put_linkColor,
1324     HTMLDocument_get_linkColor,
1325     HTMLDocument_put_vlinkColor,
1326     HTMLDocument_get_vlinkColor,
1327     HTMLDocument_get_referrer,
1328     HTMLDocument_get_location,
1329     HTMLDocument_get_lastModified,
1330     HTMLDocument_put_URL,
1331     HTMLDocument_get_URL,
1332     HTMLDocument_put_domain,
1333     HTMLDocument_get_domain,
1334     HTMLDocument_put_cookie,
1335     HTMLDocument_get_cookie,
1336     HTMLDocument_put_expando,
1337     HTMLDocument_get_expando,
1338     HTMLDocument_put_charset,
1339     HTMLDocument_get_charset,
1340     HTMLDocument_put_defaultCharset,
1341     HTMLDocument_get_defaultCharset,
1342     HTMLDocument_get_mimeType,
1343     HTMLDocument_get_fileSize,
1344     HTMLDocument_get_fileCreatedDate,
1345     HTMLDocument_get_fileModifiedDate,
1346     HTMLDocument_get_fileUpdatedDate,
1347     HTMLDocument_get_security,
1348     HTMLDocument_get_protocol,
1349     HTMLDocument_get_nameProp,
1350     HTMLDocument_write,
1351     HTMLDocument_writeln,
1352     HTMLDocument_open,
1353     HTMLDocument_close,
1354     HTMLDocument_clear,
1355     HTMLDocument_queryCommandSupported,
1356     HTMLDocument_queryCommandEnabled,
1357     HTMLDocument_queryCommandState,
1358     HTMLDocument_queryCommandIndeterm,
1359     HTMLDocument_queryCommandText,
1360     HTMLDocument_queryCommandValue,
1361     HTMLDocument_execCommand,
1362     HTMLDocument_execCommandShowHelp,
1363     HTMLDocument_createElement,
1364     HTMLDocument_put_onhelp,
1365     HTMLDocument_get_onhelp,
1366     HTMLDocument_put_onclick,
1367     HTMLDocument_get_onclick,
1368     HTMLDocument_put_ondblclick,
1369     HTMLDocument_get_ondblclick,
1370     HTMLDocument_put_onkeyup,
1371     HTMLDocument_get_onkeyup,
1372     HTMLDocument_put_onkeydown,
1373     HTMLDocument_get_onkeydown,
1374     HTMLDocument_put_onkeypress,
1375     HTMLDocument_get_onkeypress,
1376     HTMLDocument_put_onmouseup,
1377     HTMLDocument_get_onmouseup,
1378     HTMLDocument_put_onmousedown,
1379     HTMLDocument_get_onmousedown,
1380     HTMLDocument_put_onmousemove,
1381     HTMLDocument_get_onmousemove,
1382     HTMLDocument_put_onmouseout,
1383     HTMLDocument_get_onmouseout,
1384     HTMLDocument_put_onmouseover,
1385     HTMLDocument_get_onmouseover,
1386     HTMLDocument_put_onreadystatechange,
1387     HTMLDocument_get_onreadystatechange,
1388     HTMLDocument_put_onafterupdate,
1389     HTMLDocument_get_onafterupdate,
1390     HTMLDocument_put_onrowexit,
1391     HTMLDocument_get_onrowexit,
1392     HTMLDocument_put_onrowenter,
1393     HTMLDocument_get_onrowenter,
1394     HTMLDocument_put_ondragstart,
1395     HTMLDocument_get_ondragstart,
1396     HTMLDocument_put_onselectstart,
1397     HTMLDocument_get_onselectstart,
1398     HTMLDocument_elementFromPoint,
1399     HTMLDocument_get_parentWindow,
1400     HTMLDocument_get_styleSheets,
1401     HTMLDocument_put_onbeforeupdate,
1402     HTMLDocument_get_onbeforeupdate,
1403     HTMLDocument_put_onerrorupdate,
1404     HTMLDocument_get_onerrorupdate,
1405     HTMLDocument_toString,
1406     HTMLDocument_createStyleSheet
1407 };
1408
1409 #define SUPPINFO_THIS(iface) DEFINE_THIS(HTMLDocument, SupportErrorInfo, iface)
1410
1411 static HRESULT WINAPI SupportErrorInfo_QueryInterface(ISupportErrorInfo *iface, REFIID riid, void **ppv)
1412 {
1413     HTMLDocument *This = SUPPINFO_THIS(iface);
1414     return IHTMLDocument_QueryInterface(HTMLDOC(This), riid, ppv);
1415 }
1416
1417 static ULONG WINAPI SupportErrorInfo_AddRef(ISupportErrorInfo *iface)
1418 {
1419     HTMLDocument *This = SUPPINFO_THIS(iface);
1420     return IHTMLDocument_AddRef(HTMLDOC(This));
1421 }
1422
1423 static ULONG WINAPI SupportErrorInfo_Release(ISupportErrorInfo *iface)
1424 {
1425     HTMLDocument *This = SUPPINFO_THIS(iface);
1426     return IHTMLDocument_Release(HTMLDOC(This));
1427 }
1428
1429 static HRESULT WINAPI SupportErrorInfo_InterfaceSupportsErrorInfo(ISupportErrorInfo *iface, REFIID riid)
1430 {
1431     FIXME("(%p)->(%s)\n", iface, debugstr_guid(riid));
1432     return S_FALSE;
1433 }
1434
1435 static const ISupportErrorInfoVtbl SupportErrorInfoVtbl = {
1436     SupportErrorInfo_QueryInterface,
1437     SupportErrorInfo_AddRef,
1438     SupportErrorInfo_Release,
1439     SupportErrorInfo_InterfaceSupportsErrorInfo
1440 };
1441
1442 #define DISPEX_THIS(iface) DEFINE_THIS(HTMLDocument, IDispatchEx, iface)
1443
1444 static HRESULT WINAPI DocDispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1445 {
1446     HTMLDocument *This = DISPEX_THIS(iface);
1447
1448     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppv);
1449 }
1450
1451 static ULONG WINAPI DocDispatchEx_AddRef(IDispatchEx *iface)
1452 {
1453     HTMLDocument *This = DISPEX_THIS(iface);
1454
1455     return IHTMLDocument2_AddRef(HTMLDOC(This));
1456 }
1457
1458 static ULONG WINAPI DocDispatchEx_Release(IDispatchEx *iface)
1459 {
1460     HTMLDocument *This = DISPEX_THIS(iface);
1461
1462     return IHTMLDocument2_Release(HTMLDOC(This));
1463 }
1464
1465 static HRESULT WINAPI DocDispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1466 {
1467     HTMLDocument *This = DISPEX_THIS(iface);
1468
1469     return IDispatchEx_GetTypeInfoCount(This->dispex, pctinfo);
1470 }
1471
1472 static HRESULT WINAPI DocDispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1473                                                LCID lcid, ITypeInfo **ppTInfo)
1474 {
1475     HTMLDocument *This = DISPEX_THIS(iface);
1476
1477     return IDispatchEx_GetTypeInfo(This->dispex, iTInfo, lcid, ppTInfo);
1478 }
1479
1480 static HRESULT WINAPI DocDispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1481                                                  LPOLESTR *rgszNames, UINT cNames,
1482                                                  LCID lcid, DISPID *rgDispId)
1483 {
1484     HTMLDocument *This = DISPEX_THIS(iface);
1485
1486     return IDispatchEx_GetIDsOfNames(This->dispex, riid, rgszNames, cNames, lcid, rgDispId);
1487 }
1488
1489 static HRESULT WINAPI DocDispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1490                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1491                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1492 {
1493     HTMLDocument *This = DISPEX_THIS(iface);
1494
1495     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1496           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1497
1498     switch(dispIdMember) {
1499     case DISPID_READYSTATE:
1500         TRACE("DISPID_READYSTATE\n");
1501
1502         if(!(wFlags & DISPATCH_PROPERTYGET))
1503             return E_INVALIDARG;
1504
1505         V_VT(pVarResult) = VT_I4;
1506         V_I4(pVarResult) = This->doc_obj->readystate;
1507         return S_OK;
1508     }
1509
1510     return IDispatchEx_Invoke(This->dispex, dispIdMember, riid, lcid, wFlags, pDispParams,
1511                               pVarResult, pExcepInfo, puArgErr);
1512 }
1513
1514 static HRESULT WINAPI DocDispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1515 {
1516     HTMLDocument *This = DISPEX_THIS(iface);
1517
1518     return IDispatchEx_GetDispID(This->dispex, bstrName, grfdex, pid);
1519 }
1520
1521 static HRESULT WINAPI DocDispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1522         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1523 {
1524     HTMLDocument *This = DISPEX_THIS(iface);
1525
1526     return IDispatchEx_InvokeEx(This->dispex, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1527 }
1528
1529 static HRESULT WINAPI DocDispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1530 {
1531     HTMLDocument *This = DISPEX_THIS(iface);
1532
1533     return IDispatchEx_DeleteMemberByName(This->dispex, bstrName, grfdex);
1534 }
1535
1536 static HRESULT WINAPI DocDispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1537 {
1538     HTMLDocument *This = DISPEX_THIS(iface);
1539
1540     return IDispatchEx_DeleteMemberByDispID(This->dispex, id);
1541 }
1542
1543 static HRESULT WINAPI DocDispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1544 {
1545     HTMLDocument *This = DISPEX_THIS(iface);
1546
1547     return IDispatchEx_GetMemberProperties(This->dispex, id, grfdexFetch, pgrfdex);
1548 }
1549
1550 static HRESULT WINAPI DocDispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1551 {
1552     HTMLDocument *This = DISPEX_THIS(iface);
1553
1554     return IDispatchEx_GetMemberName(This->dispex, id, pbstrName);
1555 }
1556
1557 static HRESULT WINAPI DocDispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1558 {
1559     HTMLDocument *This = DISPEX_THIS(iface);
1560
1561     return IDispatchEx_GetNextDispID(This->dispex, grfdex, id, pid);
1562 }
1563
1564 static HRESULT WINAPI DocDispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1565 {
1566     HTMLDocument *This = DISPEX_THIS(iface);
1567
1568     return IDispatchEx_GetNameSpaceParent(This->dispex, ppunk);
1569 }
1570
1571 #undef DISPEX_THIS
1572
1573 static const IDispatchExVtbl DocDispatchExVtbl = {
1574     DocDispatchEx_QueryInterface,
1575     DocDispatchEx_AddRef,
1576     DocDispatchEx_Release,
1577     DocDispatchEx_GetTypeInfoCount,
1578     DocDispatchEx_GetTypeInfo,
1579     DocDispatchEx_GetIDsOfNames,
1580     DocDispatchEx_Invoke,
1581     DocDispatchEx_GetDispID,
1582     DocDispatchEx_InvokeEx,
1583     DocDispatchEx_DeleteMemberByName,
1584     DocDispatchEx_DeleteMemberByDispID,
1585     DocDispatchEx_GetMemberProperties,
1586     DocDispatchEx_GetMemberName,
1587     DocDispatchEx_GetNextDispID,
1588     DocDispatchEx_GetNameSpaceParent
1589 };
1590
1591 static BOOL htmldoc_qi(HTMLDocument *This, REFIID riid, void **ppv)
1592 {
1593     *ppv = NULL;
1594
1595     if(IsEqualGUID(&IID_IUnknown, riid)) {
1596         TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
1597         *ppv = HTMLDOC(This);
1598     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
1599         TRACE("(%p)->(IID_IDispatch, %p)\n", This, ppv);
1600         *ppv = DISPATCHEX(This);
1601     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1602         TRACE("(%p)->(IID_IDispatchEx, %p)\n", This, ppv);
1603         *ppv = DISPATCHEX(This);
1604     }else if(IsEqualGUID(&IID_IHTMLDocument, riid)) {
1605         TRACE("(%p)->(IID_IHTMLDocument, %p)\n", This, ppv);
1606         *ppv = HTMLDOC(This);
1607     }else if(IsEqualGUID(&IID_IHTMLDocument2, riid)) {
1608         TRACE("(%p)->(IID_IHTMLDocument2, %p)\n", This, ppv);
1609         *ppv = HTMLDOC(This);
1610     }else if(IsEqualGUID(&IID_IHTMLDocument3, riid)) {
1611         TRACE("(%p)->(IID_IHTMLDocument3, %p)\n", This, ppv);
1612         *ppv = HTMLDOC3(This);
1613     }else if(IsEqualGUID(&IID_IHTMLDocument4, riid)) {
1614         TRACE("(%p)->(IID_IHTMLDocument4, %p)\n", This, ppv);
1615         *ppv = HTMLDOC4(This);
1616     }else if(IsEqualGUID(&IID_IHTMLDocument5, riid)) {
1617         TRACE("(%p)->(IID_IHTMLDocument5, %p)\n", This, ppv);
1618         *ppv = HTMLDOC5(This);
1619     }else if(IsEqualGUID(&IID_IHTMLDocument6, riid)) {
1620         TRACE("(%p)->(IID_IHTMLDocument6, %p)\n", This, ppv);
1621         *ppv = HTMLDOC6(This);
1622     }else if(IsEqualGUID(&IID_IPersist, riid)) {
1623         TRACE("(%p)->(IID_IPersist, %p)\n", This, ppv);
1624         *ppv = PERSIST(This);
1625     }else if(IsEqualGUID(&IID_IPersistMoniker, riid)) {
1626         TRACE("(%p)->(IID_IPersistMoniker, %p)\n", This, ppv);
1627         *ppv = PERSISTMON(This);
1628     }else if(IsEqualGUID(&IID_IPersistFile, riid)) {
1629         TRACE("(%p)->(IID_IPersistFile, %p)\n", This, ppv);
1630         *ppv = PERSISTFILE(This);
1631     }else if(IsEqualGUID(&IID_IMonikerProp, riid)) {
1632         TRACE("(%p)->(IID_IMonikerProp, %p)\n", This, ppv);
1633         *ppv = MONPROP(This);
1634     }else if(IsEqualGUID(&IID_IOleObject, riid)) {
1635         TRACE("(%p)->(IID_IOleObject, %p)\n", This, ppv);
1636         *ppv = OLEOBJ(This);
1637     }else if(IsEqualGUID(&IID_IOleDocument, riid)) {
1638         TRACE("(%p)->(IID_IOleDocument, %p)\n", This, ppv);
1639         *ppv = OLEDOC(This);
1640     }else if(IsEqualGUID(&IID_IOleDocumentView, riid)) {
1641         TRACE("(%p)->(IID_IOleDocumentView, %p)\n", This, ppv);
1642         *ppv = DOCVIEW(This);
1643     }else if(IsEqualGUID(&IID_IOleInPlaceActiveObject, riid)) {
1644         TRACE("(%p)->(IID_IOleInPlaceActiveObject, %p)\n", This, ppv);
1645         *ppv = ACTOBJ(This);
1646     }else if(IsEqualGUID(&IID_IViewObject, riid)) {
1647         TRACE("(%p)->(IID_IViewObject, %p)\n", This, ppv);
1648         *ppv = VIEWOBJ(This);
1649     }else if(IsEqualGUID(&IID_IViewObject2, riid)) {
1650         TRACE("(%p)->(IID_IViewObject2, %p)\n", This, ppv);
1651         *ppv = VIEWOBJ2(This);
1652     }else if(IsEqualGUID(&IID_IOleWindow, riid)) {
1653         TRACE("(%p)->(IID_IOleWindow, %p)\n", This, ppv);
1654         *ppv = OLEWIN(This);
1655     }else if(IsEqualGUID(&IID_IOleInPlaceObject, riid)) {
1656         TRACE("(%p)->(IID_IOleInPlaceObject, %p)\n", This, ppv);
1657         *ppv = INPLACEOBJ(This);
1658     }else if(IsEqualGUID(&IID_IOleInPlaceObjectWindowless, riid)) {
1659         TRACE("(%p)->(IID_IOleInPlaceObjectWindowless, %p)\n", This, ppv);
1660         *ppv = INPLACEWIN(This);
1661     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
1662         TRACE("(%p)->(IID_IServiceProvider, %p)\n", This, ppv);
1663         *ppv = SERVPROV(This);
1664     }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
1665         TRACE("(%p)->(IID_IOleCommandTarget, %p)\n", This, ppv);
1666         *ppv = CMDTARGET(This);
1667     }else if(IsEqualGUID(&IID_IOleControl, riid)) {
1668         TRACE("(%p)->(IID_IOleControl, %p)\n", This, ppv);
1669         *ppv = CONTROL(This);
1670     }else if(IsEqualGUID(&IID_IHlinkTarget, riid)) {
1671         TRACE("(%p)->(IID_IHlinkTarget, %p)\n", This, ppv);
1672         *ppv = HLNKTARGET(This);
1673     }else if(IsEqualGUID(&IID_IConnectionPointContainer, riid)) {
1674         TRACE("(%p)->(IID_IConnectionPointContainer %p)\n", This, ppv);
1675         *ppv = CONPTCONT(&This->cp_container);
1676     }else if(IsEqualGUID(&IID_IPersistStreamInit, riid)) {
1677         TRACE("(%p)->(IID_IPersistStreamInit %p)\n", This, ppv);
1678         *ppv = PERSTRINIT(This);
1679     }else if(IsEqualGUID(&DIID_DispHTMLDocument, riid)) {
1680         TRACE("(%p)->(DIID_DispHTMLDocument %p)\n", This, ppv);
1681         *ppv = HTMLDOC(This);
1682     }else if(IsEqualGUID(&IID_ISupportErrorInfo, riid)) {
1683         TRACE("(%p)->(IID_ISupportErrorInfo %p)\n", This, ppv);
1684         *ppv = SUPPERRINFO(This);
1685     }else if(IsEqualGUID(&IID_IPersistHistory, riid)) {
1686         TRACE("(%p)->(IID_IPersistHistory %p)\n", This, ppv);
1687         *ppv = PERSISTHIST(This);
1688     }else if(IsEqualGUID(&CLSID_CMarkup, riid)) {
1689         FIXME("(%p)->(CLSID_CMarkup %p)\n", This, ppv);
1690         *ppv = NULL;
1691     }else if(IsEqualGUID(&IID_IRunnableObject, riid)) {
1692         TRACE("(%p)->(IID_IRunnableObject %p) returning NULL\n", This, ppv);
1693         *ppv = NULL;
1694     }else if(IsEqualGUID(&IID_IPersistPropertyBag, riid)) {
1695         TRACE("(%p)->(IID_IPersistPropertyBag %p) returning NULL\n", This, ppv);
1696         *ppv = NULL;
1697     }else if(IsEqualGUID(&IID_IMarshal, riid)) {
1698         TRACE("(%p)->(IID_IMarshal %p) returning NULL\n", This, ppv);
1699         *ppv = NULL;
1700     }else {
1701         return FALSE;
1702     }
1703
1704     if(*ppv)
1705         IUnknown_AddRef((IUnknown*)*ppv);
1706     return TRUE;
1707 }
1708
1709 static void init_doc(HTMLDocument *doc, IUnknown *unk_impl, IDispatchEx *dispex)
1710 {
1711     doc->lpHTMLDocument2Vtbl = &HTMLDocumentVtbl;
1712     doc->lpIDispatchExVtbl = &DocDispatchExVtbl;
1713     doc->lpSupportErrorInfoVtbl = &SupportErrorInfoVtbl;
1714
1715     doc->unk_impl = unk_impl;
1716     doc->dispex = dispex;
1717     doc->task_magic = get_task_target_magic();
1718
1719     HTMLDocument_HTMLDocument3_Init(doc);
1720     HTMLDocument_HTMLDocument5_Init(doc);
1721     HTMLDocument_Persist_Init(doc);
1722     HTMLDocument_OleCmd_Init(doc);
1723     HTMLDocument_OleObj_Init(doc);
1724     HTMLDocument_View_Init(doc);
1725     HTMLDocument_Window_Init(doc);
1726     HTMLDocument_Service_Init(doc);
1727     HTMLDocument_Hlink_Init(doc);
1728
1729     ConnectionPointContainer_Init(&doc->cp_container, (IUnknown*)HTMLDOC(doc));
1730     ConnectionPoint_Init(&doc->cp_propnotif, &doc->cp_container, &IID_IPropertyNotifySink);
1731     ConnectionPoint_Init(&doc->cp_htmldocevents, &doc->cp_container, &DIID_HTMLDocumentEvents);
1732     ConnectionPoint_Init(&doc->cp_htmldocevents2, &doc->cp_container, &DIID_HTMLDocumentEvents2);
1733 }
1734
1735 static void destroy_htmldoc(HTMLDocument *This)
1736 {
1737     remove_target_tasks(This->task_magic);
1738
1739     ConnectionPointContainer_Destroy(&This->cp_container);
1740 }
1741
1742 #define HTMLDOCNODE_NODE_THIS(iface) DEFINE_THIS2(HTMLDocumentNode, node, iface)
1743
1744 static HRESULT HTMLDocumentNode_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
1745 {
1746     HTMLDocumentNode *This = HTMLDOCNODE_NODE_THIS(iface);
1747
1748     if(htmldoc_qi(&This->basedoc, riid, ppv))
1749         return *ppv ? S_OK : E_NOINTERFACE;
1750
1751     if(IsEqualGUID(&IID_IInternetHostSecurityManager, riid)) {
1752         TRACE("(%p)->(IID_IInternetHostSecurityManager %p)\n", This, ppv);
1753         *ppv = HOSTSECMGR(This);
1754     }else {
1755         return HTMLDOMNode_QI(&This->node, riid, ppv);
1756     }
1757
1758     IUnknown_AddRef((IUnknown*)*ppv);
1759     return S_OK;
1760 }
1761
1762 static void HTMLDocumentNode_destructor(HTMLDOMNode *iface)
1763 {
1764     HTMLDocumentNode *This = HTMLDOCNODE_NODE_THIS(iface);
1765
1766     if(This->secmgr)
1767         IInternetSecurityManager_Release(This->secmgr);
1768
1769     detach_selection(This);
1770     detach_ranges(This);
1771     release_nodes(This);
1772
1773     if(This->nsdoc) {
1774         release_mutation(This);
1775         nsIDOMHTMLDocument_Release(This->nsdoc);
1776     }
1777
1778     destroy_htmldoc(&This->basedoc);
1779 }
1780
1781 #undef HTMLDOCNODE_NODE_THIS
1782
1783 static const NodeImplVtbl HTMLDocumentNodeImplVtbl = {
1784     HTMLDocumentNode_QI,
1785     HTMLDocumentNode_destructor
1786 };
1787
1788 static const tid_t HTMLDocumentNode_iface_tids[] = {
1789     IHTMLDOMNode_tid,
1790     IHTMLDOMNode2_tid,
1791     IHTMLDocument2_tid,
1792     IHTMLDocument3_tid,
1793     IHTMLDocument4_tid,
1794     IHTMLDocument5_tid,
1795     0
1796 };
1797
1798 static dispex_static_data_t HTMLDocumentNode_dispex = {
1799     NULL,
1800     DispHTMLDocument_tid,
1801     NULL,
1802     HTMLDocumentNode_iface_tids
1803 };
1804
1805 HRESULT create_doc_from_nsdoc(nsIDOMHTMLDocument *nsdoc, HTMLDocumentObj *doc_obj, HTMLWindow *window, HTMLDocumentNode **ret)
1806 {
1807     HTMLDocumentNode *doc;
1808     HRESULT hres;
1809
1810     doc = heap_alloc_zero(sizeof(HTMLDocumentNode));
1811     if(!doc)
1812         return E_OUTOFMEMORY;
1813
1814     doc->basedoc.doc_node = doc;
1815     doc->basedoc.doc_obj = doc_obj;
1816
1817     init_dispex(&doc->node.dispex, (IUnknown*)HTMLDOMNODE(&doc->node), &HTMLDocumentNode_dispex);
1818     init_doc(&doc->basedoc, (IUnknown*)HTMLDOMNODE(&doc->node), DISPATCHEX(&doc->node.dispex));
1819     HTMLDocumentNode_SecMgr_Init(doc);
1820     doc->ref = 1;
1821
1822     nsIDOMHTMLDocument_AddRef(nsdoc);
1823     doc->nsdoc = nsdoc;
1824     init_mutation(doc);
1825
1826     doc->basedoc.window = window;
1827
1828     list_init(&doc->bindings);
1829     list_init(&doc->selection_list);
1830     list_init(&doc->range_list);
1831
1832     HTMLDOMNode_Init(doc, &doc->node, (nsIDOMNode*)nsdoc);
1833     doc->node.vtbl = &HTMLDocumentNodeImplVtbl;
1834
1835     hres = CoInternetCreateSecurityManager(NULL, &doc->secmgr, 0);
1836     if(FAILED(hres)) {
1837         htmldoc_release(&doc->basedoc);
1838         return hres;
1839     }
1840
1841     *ret = doc;
1842     return S_OK;
1843 }
1844
1845 /**********************************************************
1846  * ICustomDoc implementation
1847  */
1848
1849 #define CUSTOMDOC_THIS(iface) DEFINE_THIS(HTMLDocumentObj, CustomDoc, iface)
1850
1851 static HRESULT WINAPI CustomDoc_QueryInterface(ICustomDoc *iface, REFIID riid, void **ppv)
1852 {
1853     HTMLDocumentObj *This = CUSTOMDOC_THIS(iface);
1854
1855     if(htmldoc_qi(&This->basedoc, riid, ppv))
1856         return *ppv ? S_OK : E_NOINTERFACE;
1857
1858     if(IsEqualGUID(&IID_ICustomDoc, riid)) {
1859         TRACE("(%p)->(IID_ICustomDoc %p)\n", This, ppv);
1860         *ppv = CUSTOMDOC(This);
1861     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
1862         return *ppv ? S_OK : E_NOINTERFACE;
1863     }else {
1864         FIXME("Unimplemented interface %s\n", debugstr_guid(riid));
1865         *ppv = NULL;
1866         return E_NOINTERFACE;
1867     }
1868
1869     IUnknown_AddRef((IUnknown*)*ppv);
1870     return S_OK;
1871 }
1872
1873 static ULONG WINAPI CustomDoc_AddRef(ICustomDoc *iface)
1874 {
1875     HTMLDocumentObj *This = CUSTOMDOC_THIS(iface);
1876     ULONG ref = InterlockedIncrement(&This->ref);
1877
1878     TRACE("(%p) ref = %u\n", This, ref);
1879
1880     return ref;
1881 }
1882
1883 static ULONG WINAPI CustomDoc_Release(ICustomDoc *iface)
1884 {
1885     HTMLDocumentObj *This = CUSTOMDOC_THIS(iface);
1886     ULONG ref = InterlockedDecrement(&This->ref);
1887
1888     TRACE("(%p) ref = %u\n", This, ref);
1889
1890     if(!ref) {
1891         if(This->basedoc.doc_node) {
1892             This->basedoc.doc_node->basedoc.doc_obj = NULL;
1893             IHTMLDocument2_Release(HTMLDOC(&This->basedoc.doc_node->basedoc));
1894         }
1895         if(This->basedoc.window) {
1896             This->basedoc.window->doc_obj = NULL;
1897             IHTMLWindow2_Release(HTMLWINDOW2(This->basedoc.window));
1898         }
1899
1900         if(This->client)
1901             IOleObject_SetClientSite(OLEOBJ(&This->basedoc), NULL);
1902         if(This->in_place_active)
1903             IOleInPlaceObjectWindowless_InPlaceDeactivate(INPLACEWIN(&This->basedoc));
1904         if(This->ipsite)
1905             IOleDocumentView_SetInPlaceSite(DOCVIEW(&This->basedoc), NULL);
1906         if(This->undomgr)
1907             IOleUndoManager_Release(This->undomgr);
1908         if(This->tooltips_hwnd)
1909             DestroyWindow(This->tooltips_hwnd);
1910
1911         if(This->hwnd)
1912             DestroyWindow(This->hwnd);
1913         heap_free(This->mime);
1914
1915         destroy_htmldoc(&This->basedoc);
1916         release_dispex(&This->dispex);
1917
1918         if(This->nscontainer)
1919             NSContainer_Release(This->nscontainer);
1920         heap_free(This);
1921     }
1922
1923     return ref;
1924 }
1925
1926 static HRESULT WINAPI CustomDoc_SetUIHandler(ICustomDoc *iface, IDocHostUIHandler *pUIHandler)
1927 {
1928     HTMLDocumentObj *This = CUSTOMDOC_THIS(iface);
1929     FIXME("(%p)->(%p)\n", This, pUIHandler);
1930     return E_NOTIMPL;
1931 }
1932
1933 #undef CUSTOMDOC_THIS
1934
1935 static const ICustomDocVtbl CustomDocVtbl = {
1936     CustomDoc_QueryInterface,
1937     CustomDoc_AddRef,
1938     CustomDoc_Release,
1939     CustomDoc_SetUIHandler
1940 };
1941
1942 static const tid_t HTMLDocumentObj_iface_tids[] = {
1943     IHTMLDocument2_tid,
1944     IHTMLDocument3_tid,
1945     IHTMLDocument4_tid,
1946     IHTMLDocument5_tid,
1947     0
1948 };
1949 static dispex_static_data_t HTMLDocumentObj_dispex = {
1950     NULL,
1951     DispHTMLDocument_tid,
1952     NULL,
1953     HTMLDocumentObj_iface_tids
1954 };
1955
1956 HRESULT HTMLDocument_Create(IUnknown *pUnkOuter, REFIID riid, void** ppvObject)
1957 {
1958     HTMLDocumentObj *doc;
1959     nsIDOMWindow *nswindow = NULL;
1960     nsresult nsres;
1961     HRESULT hres;
1962
1963     TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppvObject);
1964
1965     doc = heap_alloc_zero(sizeof(HTMLDocumentObj));
1966     if(!doc)
1967         return E_OUTOFMEMORY;
1968
1969     init_dispex(&doc->dispex, (IUnknown*)CUSTOMDOC(doc), &HTMLDocumentObj_dispex);
1970     init_doc(&doc->basedoc, (IUnknown*)CUSTOMDOC(doc), DISPATCHEX(&doc->dispex));
1971
1972     doc->lpCustomDocVtbl = &CustomDocVtbl;
1973     doc->ref = 1;
1974     doc->basedoc.doc_obj = doc;
1975
1976     doc->usermode = UNKNOWN_USERMODE;
1977     doc->readystate = READYSTATE_UNINITIALIZED;
1978
1979     doc->nscontainer = NSContainer_Create(doc, NULL);
1980     if(!doc->nscontainer) {
1981         ERR("Failed to init Gecko, returning CLASS_E_CLASSNOTAVAILABLE\n");
1982         htmldoc_release(&doc->basedoc);
1983         return CLASS_E_CLASSNOTAVAILABLE;
1984     }
1985
1986     hres = htmldoc_query_interface(&doc->basedoc, riid, ppvObject);
1987     htmldoc_release(&doc->basedoc);
1988     if(FAILED(hres))
1989         return hres;
1990
1991
1992     nsres = nsIWebBrowser_GetContentDOMWindow(doc->nscontainer->webbrowser, &nswindow);
1993     if(NS_FAILED(nsres))
1994         ERR("GetContentDOMWindow failed: %08x\n", nsres);
1995
1996     hres = HTMLWindow_Create(doc, nswindow, NULL /* FIXME */, &doc->basedoc.window);
1997     if(nswindow)
1998         nsIDOMWindow_Release(nswindow);
1999     if(FAILED(hres)) {
2000         IHTMLDocument_Release(HTMLDOC(&doc->basedoc));
2001         return hres;
2002     }
2003
2004     if(!doc->basedoc.doc_node && doc->basedoc.window->doc) {
2005         doc->basedoc.doc_node = doc->basedoc.window->doc;
2006         htmldoc_addref(&doc->basedoc.doc_node->basedoc);
2007     }
2008
2009     get_thread_hwnd();
2010
2011     return S_OK;
2012 }