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