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