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