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