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