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