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