quartz/tests: Remove misplaced ok() call.
[wine] / dlls / mshtml / htmlevent.c
1 /*
2  * Copyright 2008-2009 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "mshtmdid.h"
28
29 #include "mshtml_private.h"
30 #include "htmlevent.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35
36 typedef struct {
37     IDispatch *handler_prop;
38     DWORD handler_cnt;
39     IDispatch *handlers[0];
40 } handler_vector_t;
41
42 struct event_target_t {
43     DWORD node_handlers_mask;
44     handler_vector_t *event_table[EVENTID_LAST];
45 };
46
47 static const WCHAR beforeunloadW[] = {'b','e','f','o','r','e','u','n','l','o','a','d',0};
48 static const WCHAR onbeforeunloadW[] = {'o','n','b','e','f','o','r','e','u','n','l','o','a','d',0};
49
50 static const WCHAR blurW[] = {'b','l','u','r',0};
51 static const WCHAR onblurW[] = {'o','n','b','l','u','r',0};
52
53 static const WCHAR changeW[] = {'c','h','a','n','g','e',0};
54 static const WCHAR onchangeW[] = {'o','n','c','h','a','n','g','e',0};
55
56 static const WCHAR clickW[] = {'c','l','i','c','k',0};
57 static const WCHAR onclickW[] = {'o','n','c','l','i','c','k',0};
58
59 static const WCHAR contextmenuW[] = {'c','o','n','t','e','x','t','m','e','n','u',0};
60 static const WCHAR oncontextmenuW[] = {'o','n','c','o','n','t','e','x','t','m','e','n','u',0};
61
62 static const WCHAR dblclickW[] = {'d','b','l','c','l','i','c','k',0};
63 static const WCHAR ondblclickW[] = {'o','n','d','b','l','c','l','i','c','k',0};
64
65 static const WCHAR dragW[] = {'d','r','a','g',0};
66 static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};
67
68 static const WCHAR dragstartW[] = {'d','r','a','g','s','t','a','r','t',0};
69 static const WCHAR ondragstartW[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
70
71 static const WCHAR errorW[] = {'e','r','r','o','r',0};
72 static const WCHAR onerrorW[] = {'o','n','e','r','r','o','r',0};
73
74 static const WCHAR focusW[] = {'f','o','c','u','s',0};
75 static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};
76
77 static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
78 static const WCHAR onkeydownW[] = {'o','n','k','e','y','d','o','w','n',0};
79
80 static const WCHAR keypressW[] = {'k','e','y','p','r','e','s','s',0};
81 static const WCHAR onkeypressW[] = {'o','n','k','e','y','p','r','e','s','s',0};
82
83 static const WCHAR keyupW[] = {'k','e','y','u','p',0};
84 static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};
85
86 static const WCHAR loadW[] = {'l','o','a','d',0};
87 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
88
89 static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
90 static const WCHAR onmousedownW[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
91
92 static const WCHAR mousemoveW[] = {'m','o','u','s','e','m','o','v','e',0};
93 static const WCHAR onmousemoveW[] = {'o','n','m','o','u','s','e','m','o','v','e',0};
94
95 static const WCHAR mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
96 static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};
97
98 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
99 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
100
101 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
102 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
103
104 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
105 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
106
107 static const WCHAR readystatechangeW[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
108 static const WCHAR onreadystatechangeW[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
109
110 static const WCHAR resizeW[] = {'r','e','s','i','z','e',0};
111 static const WCHAR onresizeW[] = {'o','n','r','e','s','i','z','e',0};
112
113 static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
114 static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
115
116 static const WCHAR submitW[] = {'s','u','b','m','i','t',0};
117 static const WCHAR onsubmitW[] = {'o','n','s','u','b','m','i','t',0};
118
119 static const WCHAR HTMLEventsW[] = {'H','T','M','L','E','v','e','n','t','s',0};
120 static const WCHAR KeyboardEventW[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
121 static const WCHAR MouseEventW[] = {'M','o','u','s','e','E','v','e','n','t',0};
122
123 enum {
124     EVENTT_NONE,
125     EVENTT_HTML,
126     EVENTT_KEY,
127     EVENTT_MOUSE
128 };
129
130 static const WCHAR *event_types[] = {
131     NULL,
132     HTMLEventsW,
133     KeyboardEventW,
134     MouseEventW
135 };
136
137 typedef struct {
138     LPCWSTR name;
139     LPCWSTR attr_name;
140     DWORD type;
141     DISPID dispid;
142     DWORD flags;
143 } event_info_t;
144
145 #define EVENT_DEFAULTLISTENER    0x0001
146 #define EVENT_BUBBLE             0x0002
147 #define EVENT_FORWARDBODY        0x0004
148 #define EVENT_NODEHANDLER        0x0008
149 #define EVENT_CANCELABLE         0x0010
150 #define EVENT_HASDEFAULTHANDLERS 0x0020
151
152 static const event_info_t event_info[] = {
153     {beforeunloadW,      onbeforeunloadW,      EVENTT_NONE,   DISPID_EVMETH_ONBEFOREUNLOAD,
154         EVENT_DEFAULTLISTENER|EVENT_FORWARDBODY},
155     {blurW,              onblurW,              EVENTT_HTML,   DISPID_EVMETH_ONBLUR,
156         EVENT_DEFAULTLISTENER},
157     {changeW,            onchangeW,            EVENTT_HTML,   DISPID_EVMETH_ONCHANGE,
158         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
159     {clickW,             onclickW,             EVENTT_MOUSE,  DISPID_EVMETH_ONCLICK,
160         EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE|EVENT_HASDEFAULTHANDLERS},
161     {contextmenuW,       oncontextmenuW,       EVENTT_MOUSE,  DISPID_EVMETH_ONCONTEXTMENU,
162         EVENT_BUBBLE|EVENT_CANCELABLE},
163     {dblclickW,          ondblclickW,          EVENTT_MOUSE,  DISPID_EVMETH_ONDBLCLICK,
164         EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE},
165     {dragW,              ondragW,              EVENTT_MOUSE,  DISPID_EVMETH_ONDRAG,
166         EVENT_CANCELABLE},
167     {dragstartW,         ondragstartW,         EVENTT_MOUSE,  DISPID_EVMETH_ONDRAGSTART,
168         EVENT_CANCELABLE},
169     {errorW,             onerrorW,             EVENTT_NONE,   DISPID_EVMETH_ONERROR,
170         EVENT_NODEHANDLER},
171     {focusW,             onfocusW,             EVENTT_HTML,   DISPID_EVMETH_ONFOCUS,
172         EVENT_DEFAULTLISTENER},
173     {keydownW,           onkeydownW,           EVENTT_KEY,    DISPID_EVMETH_ONKEYDOWN,
174         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
175     {keypressW,          onkeypressW,          EVENTT_KEY,    DISPID_EVMETH_ONKEYPRESS,
176         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
177     {keyupW,             onkeyupW,             EVENTT_KEY,    DISPID_EVMETH_ONKEYUP,
178         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
179     {loadW,              onloadW,              EVENTT_HTML,   DISPID_EVMETH_ONLOAD,
180         EVENT_NODEHANDLER},
181     {mousedownW,         onmousedownW,         EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEDOWN,
182         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
183     {mousemoveW,         onmousemoveW,         EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEMOVE,
184         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
185     {mouseoutW,          onmouseoutW,          EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEOUT,
186         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
187     {mouseoverW,         onmouseoverW,         EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEOVER,
188         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
189     {mouseupW,           onmouseupW,           EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEUP,
190         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
191     {pasteW,             onpasteW,             EVENTT_NONE,   DISPID_EVMETH_ONPASTE,
192         EVENT_CANCELABLE},
193     {readystatechangeW,  onreadystatechangeW,  EVENTT_NONE,   DISPID_EVMETH_ONREADYSTATECHANGE,
194         0},
195     {resizeW,            onresizeW,            EVENTT_NONE,   DISPID_EVMETH_ONRESIZE,
196         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
197     {selectstartW,       onselectstartW,       EVENTT_MOUSE,  DISPID_EVMETH_ONSELECTSTART,
198         EVENT_CANCELABLE},
199     {submitW,            onsubmitW,            EVENTT_HTML,   DISPID_EVMETH_ONSUBMIT,
200         EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE}
201 };
202
203 static const eventid_t node_handled_list[] = { EVENTID_ERROR, EVENTID_LOAD };
204
205 eventid_t str_to_eid(LPCWSTR str)
206 {
207     int i;
208
209     for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
210         if(!strcmpW(event_info[i].name, str))
211             return i;
212     }
213
214     ERR("unknown type %s\n", debugstr_w(str));
215     return EVENTID_LAST;
216 }
217
218 static eventid_t attr_to_eid(LPCWSTR str)
219 {
220     int i;
221
222     for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
223         if(!strcmpW(event_info[i].attr_name, str))
224             return i;
225     }
226
227     return EVENTID_LAST;
228 }
229
230 static DWORD get_node_handler_mask(eventid_t eid)
231 {
232     DWORD i;
233
234     for(i=0; i<sizeof(node_handled_list)/sizeof(*node_handled_list); i++) {
235         if(node_handled_list[i] == eid)
236             return 1 << i;
237     }
238
239     ERR("Invalid eid %d\n", eid);
240     return ~0;
241 }
242
243 typedef struct {
244     DispatchEx dispex;
245     IHTMLEventObj IHTMLEventObj_iface;
246
247     LONG ref;
248
249     HTMLDOMNode *target;
250     const event_info_t *type;
251     nsIDOMEvent *nsevent;
252     BOOL prevent_default;
253     BOOL cancel_bubble;
254 } HTMLEventObj;
255
256 static inline HTMLEventObj *impl_from_IHTMLEventObj(IHTMLEventObj *iface)
257 {
258     return CONTAINING_RECORD(iface, HTMLEventObj, IHTMLEventObj_iface);
259 }
260
261 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
262 {
263     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
264
265     *ppv = NULL;
266
267     if(IsEqualGUID(&IID_IUnknown, riid)) {
268         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
269         *ppv = &This->IHTMLEventObj_iface;
270     }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
271         TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
272         *ppv = &This->IHTMLEventObj_iface;
273     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
274         return *ppv ? S_OK : E_NOINTERFACE;
275     }
276
277     if(*ppv) {
278         IUnknown_AddRef((IUnknown*)*ppv);
279         return S_OK;
280     }
281
282     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
283     return E_NOINTERFACE;
284 }
285
286 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
287 {
288     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
289     LONG ref = InterlockedIncrement(&This->ref);
290
291     TRACE("(%p) ref=%d\n", This, ref);
292
293     return ref;
294 }
295
296 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
297 {
298     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
299     LONG ref = InterlockedDecrement(&This->ref);
300
301     TRACE("(%p) ref=%d\n", This, ref);
302
303     if(!ref) {
304         if(This->target)
305             IHTMLDOMNode_Release(&This->target->IHTMLDOMNode_iface);
306         if(This->nsevent)
307             nsIDOMEvent_Release(This->nsevent);
308         release_dispex(&This->dispex);
309         heap_free(This);
310     }
311
312     return ref;
313 }
314
315 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
316 {
317     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
318     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
319 }
320
321 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
322                                               LCID lcid, ITypeInfo **ppTInfo)
323 {
324     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
325     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
326 }
327
328 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
329                                                 LPOLESTR *rgszNames, UINT cNames,
330                                                 LCID lcid, DISPID *rgDispId)
331 {
332     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
333     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
334             lcid, rgDispId);
335 }
336
337 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
338                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
339                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
340 {
341     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
342     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
343             wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
344 }
345
346 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
347 {
348     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
349
350     TRACE("(%p)->(%p)\n", This, p);
351
352     return IHTMLDOMNode_QueryInterface(&This->target->IHTMLDOMNode_iface, &IID_IHTMLElement,
353             (void**)p);
354 }
355
356 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
357 {
358     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
359     cpp_bool ret = FALSE;
360
361     TRACE("(%p)->(%p)\n", This, p);
362
363     if(This->nsevent) {
364         nsIDOMKeyEvent *key_event;
365         nsresult nsres;
366
367         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
368         if(NS_SUCCEEDED(nsres)) {
369             nsIDOMKeyEvent_GetAltKey(key_event, &ret);
370             nsIDOMKeyEvent_Release(key_event);
371         }else {
372             nsIDOMMouseEvent *mouse_event;
373
374             nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
375             if(NS_SUCCEEDED(nsres)) {
376                 nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
377                 nsIDOMMouseEvent_Release(mouse_event);
378             }
379         }
380     }
381
382     *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
383     return S_OK;
384 }
385
386 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
387 {
388     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
389     cpp_bool ret = FALSE;
390
391     TRACE("(%p)->(%p)\n", This, p);
392
393     if(This->nsevent) {
394         nsIDOMKeyEvent *key_event;
395         nsresult nsres;
396
397         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
398         if(NS_SUCCEEDED(nsres)) {
399             nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
400             nsIDOMKeyEvent_Release(key_event);
401         }else {
402             nsIDOMMouseEvent *mouse_event;
403
404             nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
405             if(NS_SUCCEEDED(nsres)) {
406                 nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
407                 nsIDOMMouseEvent_Release(mouse_event);
408             }
409         }
410     }
411
412     *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
413     return S_OK;
414 }
415
416 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
417 {
418     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
419     cpp_bool ret = FALSE;
420
421     TRACE("(%p)->(%p)\n", This, p);
422
423     if(This->nsevent) {
424         nsIDOMKeyEvent *key_event;
425         nsresult nsres;
426
427         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
428         if(NS_SUCCEEDED(nsres)) {
429             nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
430             nsIDOMKeyEvent_Release(key_event);
431         }else {
432             nsIDOMMouseEvent *mouse_event;
433
434             nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
435             if(NS_SUCCEEDED(nsres)) {
436                 nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
437                 nsIDOMMouseEvent_Release(mouse_event);
438             }
439         }
440     }
441
442     *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
443     return S_OK;
444 }
445
446 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
447 {
448     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
449
450     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
451
452     if(V_VT(&v) != VT_BOOL) {
453         FIXME("unsupported value %s\n", debugstr_variant(&v));
454         return DISP_E_BADVARTYPE;
455     }
456
457     if(!V_BOOL(&v))
458         This->prevent_default = TRUE;
459     return S_OK;
460 }
461
462 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
463 {
464     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
465
466     FIXME("(%p)->(%p)\n", This, p);
467
468     V_VT(p) = VT_EMPTY;
469     return S_OK;
470 }
471
472 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
473 {
474     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
475
476     TRACE("(%p)->(%x)\n", This, v);
477
478     This->cancel_bubble = !!v;
479     return S_OK;
480 }
481
482 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
483 {
484     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
485
486     TRACE("(%p)->(%p)\n", This, p);
487
488     *p = This->cancel_bubble ? VARIANT_TRUE : VARIANT_FALSE;
489     return S_OK;
490 }
491
492 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
493 {
494     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
495
496     FIXME("(%p)->(%p)\n", This, p);
497
498     *p = NULL;
499     return S_OK;
500 }
501
502 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
503 {
504     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
505
506     FIXME("(%p)->(%p)\n", This, p);
507
508     *p = NULL;
509     return S_OK;
510 }
511
512 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
513 {
514     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
515     FIXME("(%p)->(%d)\n", This, v);
516     return E_NOTIMPL;
517 }
518
519 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
520 {
521     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
522     PRUint32 key_code = 0;
523
524     TRACE("(%p)->(%p)\n", This, p);
525
526     if(This->nsevent) {
527         nsIDOMKeyEvent *key_event;
528         nsresult nsres;
529
530         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
531         if(NS_SUCCEEDED(nsres)) {
532             nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
533             nsIDOMKeyEvent_Release(key_event);
534         }
535     }
536
537     *p = key_code;
538     return S_OK;
539 }
540
541 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
542 {
543     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
544     PRUint16 button = 0;
545
546     TRACE("(%p)->(%p)\n", This, p);
547
548     if(This->nsevent) {
549         nsIDOMMouseEvent *mouse_event;
550         nsresult nsres;
551
552         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
553         if(NS_SUCCEEDED(nsres)) {
554             nsIDOMMouseEvent_GetButton(mouse_event, &button);
555             nsIDOMMouseEvent_Release(mouse_event);
556         }
557     }
558
559     *p = button;
560     return S_OK;
561 }
562
563 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
564 {
565     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
566
567     TRACE("(%p)->(%p)\n", This, p);
568
569     *p = SysAllocString(This->type->name);
570     return *p ? S_OK : E_OUTOFMEMORY;
571 }
572
573 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
574 {
575     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
576
577     FIXME("(%p)->(%p)\n", This, p);
578
579     *p = NULL;
580     return S_OK;
581 }
582
583 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
584 {
585     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
586
587     FIXME("(%p)->(%p)\n", This, p);
588
589     *p = 0;
590     return S_OK;
591 }
592
593 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
594 {
595     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
596
597     FIXME("(%p)->(%p)\n", This, p);
598
599     *p = -1;
600     return S_OK;
601 }
602
603 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
604 {
605     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
606
607     FIXME("(%p)->(%p)\n", This, p);
608
609     *p = -1;
610     return S_OK;
611 }
612
613 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
614 {
615     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
616     PRInt32 x = 0;
617
618     TRACE("(%p)->(%p)\n", This, p);
619
620     if(This->nsevent) {
621         nsIDOMMouseEvent *mouse_event;
622         nsresult nsres;
623
624         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
625         if(NS_SUCCEEDED(nsres)) {
626             nsIDOMMouseEvent_GetClientX(mouse_event, &x);
627             nsIDOMMouseEvent_Release(mouse_event);
628         }
629     }
630
631     *p = x;
632     return S_OK;
633 }
634
635 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
636 {
637     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
638     PRInt32 y = 0;
639
640     TRACE("(%p)->(%p)\n", This, p);
641
642     if(This->nsevent) {
643         nsIDOMMouseEvent *mouse_event;
644         nsresult nsres;
645
646         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
647         if(NS_SUCCEEDED(nsres)) {
648             nsIDOMMouseEvent_GetClientY(mouse_event, &y);
649             nsIDOMMouseEvent_Release(mouse_event);
650         }
651     }
652
653     *p = y;
654     return S_OK;
655 }
656
657 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
658 {
659     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
660
661     FIXME("(%p)->(%p)\n", This, p);
662
663     *p = 0;
664     return S_OK;
665 }
666
667 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
668 {
669     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
670
671     FIXME("(%p)->(%p)\n", This, p);
672
673     *p = 0;
674     return S_OK;
675 }
676
677 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
678 {
679     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
680     PRInt32 x = 0;
681
682     TRACE("(%p)->(%p)\n", This, p);
683
684     if(This->nsevent) {
685         nsIDOMMouseEvent *mouse_event;
686         nsresult nsres;
687
688         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
689         if(NS_SUCCEEDED(nsres)) {
690             nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
691             nsIDOMMouseEvent_Release(mouse_event);
692         }
693     }
694
695     *p = x;
696     return S_OK;
697 }
698
699 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
700 {
701     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
702     PRInt32 y = 0;
703
704     TRACE("(%p)->(%p)\n", This, p);
705
706     if(This->nsevent) {
707         nsIDOMMouseEvent *mouse_event;
708         nsresult nsres;
709
710         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
711         if(NS_SUCCEEDED(nsres)) {
712             nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
713             nsIDOMMouseEvent_Release(mouse_event);
714         }
715     }
716
717     *p = y;
718     return S_OK;
719 }
720
721 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
722 {
723     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
724
725     FIXME("(%p)->(%p)\n", This, p);
726
727     *p = NULL;
728     return S_OK;
729 }
730
731 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
732     HTMLEventObj_QueryInterface,
733     HTMLEventObj_AddRef,
734     HTMLEventObj_Release,
735     HTMLEventObj_GetTypeInfoCount,
736     HTMLEventObj_GetTypeInfo,
737     HTMLEventObj_GetIDsOfNames,
738     HTMLEventObj_Invoke,
739     HTMLEventObj_get_srcElement,
740     HTMLEventObj_get_altKey,
741     HTMLEventObj_get_ctrlKey,
742     HTMLEventObj_get_shiftKey,
743     HTMLEventObj_put_returnValue,
744     HTMLEventObj_get_returnValue,
745     HTMLEventObj_put_cancelBubble,
746     HTMLEventObj_get_cancelBubble,
747     HTMLEventObj_get_fromElement,
748     HTMLEventObj_get_toElement,
749     HTMLEventObj_put_keyCode,
750     HTMLEventObj_get_keyCode,
751     HTMLEventObj_get_button,
752     HTMLEventObj_get_type,
753     HTMLEventObj_get_qualifier,
754     HTMLEventObj_get_reason,
755     HTMLEventObj_get_x,
756     HTMLEventObj_get_y,
757     HTMLEventObj_get_clientX,
758     HTMLEventObj_get_clientY,
759     HTMLEventObj_get_offsetX,
760     HTMLEventObj_get_offsetY,
761     HTMLEventObj_get_screenX,
762     HTMLEventObj_get_screenY,
763     HTMLEventObj_get_srcFilter
764 };
765
766 static const tid_t HTMLEventObj_iface_tids[] = {
767     IHTMLEventObj_tid,
768     0
769 };
770
771 static dispex_static_data_t HTMLEventObj_dispex = {
772     NULL,
773     DispCEventObj_tid,
774     NULL,
775     HTMLEventObj_iface_tids
776 };
777
778 static HTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
779 {
780     HTMLEventObj *ret;
781
782     ret = heap_alloc_zero(sizeof(*ret));
783     if(!ret)
784         return NULL;
785
786     ret->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
787     ret->ref = 1;
788     ret->type = event_info+eid;
789
790     ret->nsevent = nsevent;
791     if(nsevent) {
792         nsIDOMEvent_AddRef(nsevent);
793     }else if(event_types[event_info[eid].type]) {
794         nsAString type_str;
795         nsresult nsres;
796
797         nsAString_InitDepend(&type_str, event_types[event_info[eid].type]);
798         nsres = nsIDOMHTMLDocument_CreateEvent(target->doc->nsdoc, &type_str, &ret->nsevent);
799         nsAString_Finish(&type_str);
800         if(NS_FAILED(nsres)) {
801             ERR("Could not create event: %08x\n", nsres);
802             IHTMLEventObj_Release(&ret->IHTMLEventObj_iface);
803             return NULL;
804         }
805     }
806
807     ret->target = target;
808     IHTMLDOMNode_AddRef(&target->IHTMLDOMNode_iface);
809
810     init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLEventObj_iface, &HTMLEventObj_dispex);
811
812     return ret;
813 }
814
815 static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
816 {
817     IDispatchEx *dispex;
818     EXCEPINFO ei;
819     HRESULT hres;
820
821     memset(&ei, 0, sizeof(ei));
822
823     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
824     if(SUCCEEDED(hres)) {
825         hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, retv, &ei, NULL);
826         IDispatchEx_Release(dispex);
827     }else {
828         TRACE("Could not get IDispatchEx interface: %08x\n", hres);
829         hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
830                 dp, retv, &ei, NULL);
831     }
832
833     return hres;
834 }
835
836 static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, VARIANT *retv)
837 {
838     DISPPARAMS dp = {NULL,NULL,0,0};
839     ULONG argerr;
840     EXCEPINFO ei;
841
842     memset(&ei, 0, sizeof(ei));
843     return IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, retv, &ei, &argerr);
844 }
845
846 static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
847 {
848     int min, max, i;
849     HRESULT hres;
850
851     if(!data)
852         return FALSE;
853
854     if(!data->ids) {
855         hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
856         if(FAILED(hres))
857             return FALSE;
858     }
859
860     min = 0;
861     max = data->id_cnt-1;
862     while(min <= max) {
863         i = (min+max)/2;
864         if(data->ids[i] == dispid)
865             return TRUE;
866
867         if(data->ids[i] < dispid)
868             min = i+1;
869         else
870             max = i-1;
871     }
872
873     return FALSE;
874 }
875
876 static void call_event_handlers(HTMLDocumentNode *doc, HTMLEventObj *event_obj, event_target_t *event_target,
877         ConnectionPointContainer *cp_container, eventid_t eid, IDispatch *this_obj)
878 {
879     const BOOL cancelable = event_info[eid].flags & EVENT_CANCELABLE;
880     handler_vector_t *handler_vector = NULL;
881     VARIANT v;
882     int i;
883     HRESULT hres;
884
885     if(event_target)
886         handler_vector = event_target->event_table[eid];
887
888     if(handler_vector && handler_vector->handler_prop) {
889         DISPID named_arg = DISPID_THIS;
890         VARIANTARG arg;
891         DISPPARAMS dp = {&arg, &named_arg, 1, 1};
892
893         V_VT(&arg) = VT_DISPATCH;
894         V_DISPATCH(&arg) = this_obj;
895         V_VT(&v) = VT_EMPTY;
896
897         TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
898         hres = call_disp_func(handler_vector->handler_prop, &dp, &v);
899         if(hres == S_OK) {
900             TRACE("%s <<< %s\n", debugstr_w(event_info[eid].name), debugstr_variant(&v));
901
902             if(cancelable) {
903                 if(V_VT(&v) == VT_BOOL) {
904                     if(!V_BOOL(&v))
905                         event_obj->prevent_default = TRUE;
906                 }else if(V_VT(&v) != VT_EMPTY) {
907                     FIXME("unhandled result %s\n", debugstr_variant(&v));
908                 }
909             }
910             VariantClear(&v);
911         }else {
912             WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
913         }
914     }
915
916     if(handler_vector && handler_vector->handler_cnt) {
917         VARIANTARG arg;
918         DISPPARAMS dp = {&arg, NULL, 1, 0};
919
920         V_VT(&arg) = VT_DISPATCH;
921         V_DISPATCH(&arg) = (IDispatch*)event_obj;
922
923         i = handler_vector->handler_cnt;
924         while(i--) {
925             if(handler_vector->handlers[i]) {
926                 V_VT(&v) = VT_EMPTY;
927
928                 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
929                 hres = call_disp_func(handler_vector->handlers[i], &dp, &v);
930                 if(hres == S_OK) {
931                     TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
932
933                     if(cancelable) {
934                         if(V_VT(&v) == VT_BOOL) {
935                             if(!V_BOOL(&v))
936                                 event_obj->prevent_default = TRUE;
937                         }else if(V_VT(&v) != VT_EMPTY) {
938                             FIXME("unhandled result %s\n", debugstr_variant(&v));
939                         }
940                     }
941                     VariantClear(&v);
942                 }else {
943                     WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
944                 }
945             }
946         }
947     }
948
949     if(cp_container) {
950         ConnectionPoint *cp;
951
952         if(cp_container->forward_container)
953             cp_container = cp_container->forward_container;
954
955         for(cp = cp_container->cp_list; cp; cp = cp->next) {
956             if(cp->sinks_size && is_cp_event(cp->data, event_info[eid].dispid)) {
957                 for(i=0; i < cp->sinks_size; i++) {
958                     if(!cp->sinks[i].disp)
959                         continue;
960
961                     V_VT(&v) = VT_EMPTY;
962
963                     TRACE("cp %s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
964                     hres = call_cp_func(cp->sinks[i].disp, event_info[eid].dispid, &v);
965                     if(hres == S_OK) {
966                         TRACE("cp %s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
967
968                         if(cancelable) {
969                             if(V_VT(&v) == VT_BOOL) {
970                                 if(!V_BOOL(&v))
971                                     event_obj->prevent_default = TRUE;
972                             }else if(V_VT(&v) != VT_EMPTY) {
973                                 FIXME("unhandled result %s\n", debugstr_variant(&v));
974                             }
975                         }
976                         VariantClear(&v);
977                     }else {
978                         WARN("cp %s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
979                     }
980                 }
981             }
982         }
983     }
984 }
985
986 void fire_event(HTMLDocumentNode *doc, eventid_t eid, BOOL set_event, nsIDOMNode *target, nsIDOMEvent *nsevent)
987 {
988     HTMLEventObj *event_obj = NULL;
989     IHTMLEventObj *prev_event;
990     nsIDOMNode *parent, *nsnode;
991     BOOL prevent_default = FALSE;
992     HTMLDOMNode *node;
993     PRUint16 node_type;
994     nsresult nsres;
995     HRESULT hres;
996
997     TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
998
999     prev_event = doc->basedoc.window->event;
1000     if(set_event) {
1001         hres = get_node(doc, target, TRUE, &node);
1002         if(FAILED(hres))
1003             return;
1004
1005         event_obj = create_event(node, eid, nsevent);
1006         doc->basedoc.window->event = &event_obj->IHTMLEventObj_iface;
1007     }else {
1008         doc->basedoc.window->event = NULL;
1009     }
1010
1011     nsIDOMNode_GetNodeType(target, &node_type);
1012     nsnode = target;
1013     nsIDOMNode_AddRef(nsnode);
1014
1015     switch(node_type) {
1016     case ELEMENT_NODE:
1017         do {
1018             hres = get_node(doc, nsnode, FALSE, &node);
1019             if(SUCCEEDED(hres) && node)
1020                 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1021                         node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1022
1023             if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1024                 break;
1025
1026             nsIDOMNode_GetParentNode(nsnode, &parent);
1027             nsIDOMNode_Release(nsnode);
1028             nsnode = parent;
1029             if(!nsnode)
1030                 break;
1031
1032             nsIDOMNode_GetNodeType(nsnode, &node_type);
1033         }while(node_type == ELEMENT_NODE);
1034
1035         if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1036             break;
1037
1038     case DOCUMENT_NODE:
1039         if(event_info[eid].flags & EVENT_FORWARDBODY) {
1040             nsIDOMHTMLElement *nsbody;
1041             nsresult nsres;
1042
1043             nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
1044             if(NS_SUCCEEDED(nsres) && nsbody) {
1045                 hres = get_node(doc, (nsIDOMNode*)nsbody, FALSE, &node);
1046                 if(SUCCEEDED(hres) && node)
1047                     call_event_handlers(doc, event_obj, *get_node_event_target(node),
1048                             node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1049                 nsIDOMHTMLElement_Release(nsbody);
1050             }else {
1051                 ERR("Could not get body: %08x\n", nsres);
1052             }
1053         }
1054
1055         call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
1056                 (IDispatch*)&doc->basedoc.IHTMLDocument2_iface);
1057         break;
1058
1059     default:
1060         FIXME("unimplemented node type %d\n", node_type);
1061     }
1062
1063     if(nsnode)
1064         nsIDOMNode_Release(nsnode);
1065
1066     if(event_obj && event_obj->prevent_default)
1067         prevent_default = TRUE;
1068     doc->basedoc.window->event = prev_event;
1069     if(event_obj)
1070         IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1071
1072     if(!prevent_default && (event_info[eid].flags & EVENT_HASDEFAULTHANDLERS)) {
1073         nsIDOMNode_AddRef(target);
1074         nsnode = target;
1075
1076         do {
1077             hres = get_node(doc, nsnode, TRUE, &node);
1078             if(FAILED(hres))
1079                 break;
1080
1081             if(node && node->vtbl->handle_event) {
1082                 hres = node->vtbl->handle_event(node, eid, nsevent, &prevent_default);
1083                 if(FAILED(hres) || prevent_default || (event_obj && event_obj->cancel_bubble))
1084                     break;
1085             }
1086
1087             nsres = nsIDOMNode_GetParentNode(nsnode, &parent);
1088             if(NS_FAILED(nsres))
1089                 break;
1090
1091             nsIDOMNode_Release(nsnode);
1092             nsnode = parent;
1093         } while(nsnode);
1094
1095         if(nsnode)
1096             nsIDOMNode_Release(nsnode);
1097     }
1098
1099     if(prevent_default && nsevent) {
1100         TRACE("calling PreventDefault\n");
1101         nsIDOMEvent_PreventDefault(nsevent);
1102     }
1103 }
1104
1105 HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_obj, VARIANT_BOOL *cancelled)
1106 {
1107     eventid_t eid;
1108
1109     eid = attr_to_eid(event_name);
1110     if(eid == EVENTID_LAST) {
1111         WARN("unknown event %s\n", debugstr_w(event_name));
1112         return E_INVALIDARG;
1113     }
1114
1115     if(event_obj && V_VT(event_obj) != VT_EMPTY && V_VT(event_obj) != VT_ERROR)
1116         FIXME("event_obj not implemented\n");
1117
1118     if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
1119         FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1120         return E_NOTIMPL;
1121     }
1122
1123     fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1124
1125     *cancelled = VARIANT_TRUE; /* FIXME */
1126     return S_OK;
1127 }
1128
1129 HRESULT call_fire_event(HTMLDOMNode *node, eventid_t eid)
1130 {
1131     HRESULT hres;
1132
1133     if(node->vtbl->fire_event) {
1134         BOOL handled = FALSE;
1135
1136         hres = node->vtbl->fire_event(node, eid, &handled);
1137         if(handled)
1138             return hres;
1139     }
1140
1141     fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1142     return S_OK;
1143 }
1144
1145 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
1146 {
1147     if(!*event_target_ptr)
1148         *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
1149     return *event_target_ptr;
1150 }
1151
1152 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
1153 {
1154     handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
1155
1156     if(handler_vector) {
1157         if(cnt <= handler_vector->handler_cnt)
1158             return TRUE;
1159
1160         new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1161     }else {
1162         new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1163     }
1164
1165     if(!new_vector)
1166         return FALSE;
1167
1168     new_vector->handler_cnt = cnt;
1169     event_target->event_table[eid] = new_vector;
1170     return TRUE;
1171 }
1172
1173 static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1174 {
1175     if(!doc->nsdoc)
1176         return S_OK;
1177
1178     if(event_info[eid].flags & EVENT_NODEHANDLER) {
1179         DWORD mask;
1180
1181         mask = get_node_handler_mask(eid);
1182         if(event_target->node_handlers_mask & mask)
1183             return S_OK;
1184
1185         add_nsevent_listener(doc, nsnode, event_info[eid].name);
1186         event_target->node_handlers_mask |= mask;
1187         return S_OK;
1188     }
1189
1190     if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER))
1191         return S_OK;
1192
1193     if(!doc->event_vector[eid]) {
1194         doc->event_vector[eid] = TRUE;
1195         add_nsevent_listener(doc, NULL, event_info[eid].name);
1196     }
1197
1198     return S_OK;
1199 }
1200
1201 void detach_events(HTMLDocumentNode *doc)
1202 {
1203     if(doc->event_vector) {
1204         int i;
1205
1206         for(i=0; i < EVENTID_LAST; i++) {
1207             if(doc->event_vector[i])
1208                 detach_nsevent(doc, event_info[i].name);
1209         }
1210     }
1211
1212     release_nsevents(doc);
1213 }
1214
1215
1216 static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
1217 {
1218     if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1219         IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
1220         (*event_target)->event_table[eid]->handler_prop = NULL;
1221     }
1222
1223     return S_OK;
1224 }
1225
1226 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocumentNode *doc,
1227         eventid_t eid, IDispatch *disp)
1228 {
1229     event_target_t *event_target;
1230
1231     if(!disp)
1232         return remove_event_handler(event_target_ptr, eid);
1233
1234     event_target = get_event_target(event_target_ptr);
1235     if(!event_target)
1236         return E_OUTOFMEMORY;
1237
1238     if(!alloc_handler_vector(event_target, eid, 0))
1239         return E_OUTOFMEMORY;
1240
1241     if(event_target->event_table[eid]->handler_prop)
1242         IDispatch_Release(event_target->event_table[eid]->handler_prop);
1243
1244     event_target->event_table[eid]->handler_prop = disp;
1245     IDispatch_AddRef(disp);
1246
1247     return ensure_nsevent_handler(doc, event_target, nsnode, eid);
1248 }
1249
1250 HRESULT set_event_handler(event_target_t **event_target, nsIDOMNode *nsnode, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1251 {
1252     switch(V_VT(var)) {
1253     case VT_NULL:
1254         return remove_event_handler(event_target, eid);
1255
1256     case VT_DISPATCH:
1257         return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1258
1259     default:
1260         FIXME("not handler %s\n", debugstr_variant(var));
1261         /* fall through */
1262     case VT_EMPTY:
1263         return E_NOTIMPL;
1264     }
1265
1266     return S_OK;
1267 }
1268
1269 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
1270 {
1271     if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1272         V_VT(var) = VT_DISPATCH;
1273         V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1274         IDispatch_AddRef(V_DISPATCH(var));
1275     }else {
1276         V_VT(var) = VT_NULL;
1277     }
1278
1279     return S_OK;
1280 }
1281
1282 HRESULT attach_event(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocument *doc, BSTR name,
1283         IDispatch *disp, VARIANT_BOOL *res)
1284 {
1285     event_target_t *event_target;
1286     eventid_t eid;
1287     DWORD i = 0;
1288
1289     eid = attr_to_eid(name);
1290     if(eid == EVENTID_LAST) {
1291         WARN("Unknown event\n");
1292         *res = VARIANT_TRUE;
1293         return S_OK;
1294     }
1295
1296     event_target = get_event_target(event_target_ptr);
1297     if(!event_target)
1298         return E_OUTOFMEMORY;
1299
1300     if(event_target->event_table[eid]) {
1301         while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
1302             i++;
1303         if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
1304             return E_OUTOFMEMORY;
1305     }else if(!alloc_handler_vector(event_target, eid, i+1)) {
1306         return E_OUTOFMEMORY;
1307     }
1308
1309     IDispatch_AddRef(disp);
1310     event_target->event_table[eid]->handlers[i] = disp;
1311
1312     *res = VARIANT_TRUE;
1313     return ensure_nsevent_handler(doc->doc_node, event_target, nsnode, eid);
1314 }
1315
1316 HRESULT detach_event(event_target_t *event_target, HTMLDocument *doc, BSTR name, IDispatch *disp)
1317 {
1318     eventid_t eid;
1319     DWORD i = 0;
1320
1321     if(!event_target)
1322         return S_OK;
1323
1324     eid = attr_to_eid(name);
1325     if(eid == EVENTID_LAST) {
1326         WARN("Unknown event\n");
1327         return S_OK;
1328     }
1329
1330     if(!event_target->event_table[eid])
1331         return S_OK;
1332
1333     while(i < event_target->event_table[eid]->handler_cnt) {
1334         if(event_target->event_table[eid]->handlers[i] == disp) {
1335             IDispatch_Release(event_target->event_table[eid]->handlers[i]);
1336             event_target->event_table[eid]->handlers[i] = NULL;
1337         }
1338         i++;
1339     }
1340
1341     return S_OK;
1342 }
1343
1344 void update_cp_events(HTMLWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp, nsIDOMNode *nsnode)
1345 {
1346     event_target_t *event_target;
1347     int i;
1348
1349     event_target = get_event_target(event_target_ptr);
1350     if(!event_target)
1351         return; /* FIXME */
1352
1353     for(i=0; i < EVENTID_LAST; i++) {
1354         if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1355             ensure_nsevent_handler(window->doc, event_target, nsnode, i);
1356     }
1357 }
1358
1359 void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
1360 {
1361     const PRUnichar *attr_value;
1362     nsAString attr_name_str, attr_value_str;
1363     IDispatch *disp;
1364     HTMLDOMNode *node;
1365     int i;
1366     nsresult nsres;
1367     HRESULT hres;
1368
1369     nsAString_Init(&attr_value_str, NULL);
1370     nsAString_Init(&attr_name_str, NULL);
1371
1372     for(i=0; i < EVENTID_LAST; i++) {
1373         nsAString_SetData(&attr_name_str, event_info[i].attr_name);
1374         nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
1375         if(NS_SUCCEEDED(nsres)) {
1376             nsAString_GetData(&attr_value_str, &attr_value);
1377             if(!*attr_value)
1378                 continue;
1379
1380             TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
1381
1382             disp = script_parse_event(doc->basedoc.window, attr_value);
1383             if(disp) {
1384                 hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1385                 if(SUCCEEDED(hres))
1386                     set_event_handler_disp(get_node_event_target(node), node->nsnode, node->doc, i, disp);
1387                 IDispatch_Release(disp);
1388             }
1389         }
1390     }
1391
1392     nsAString_Finish(&attr_value_str);
1393     nsAString_Finish(&attr_name_str);
1394 }
1395
1396 HRESULT doc_init_events(HTMLDocumentNode *doc)
1397 {
1398     unsigned i;
1399     HRESULT hres;
1400
1401     doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
1402     if(!doc->event_vector)
1403         return E_OUTOFMEMORY;
1404
1405     init_nsevents(doc);
1406
1407     for(i=0; i < EVENTID_LAST; i++) {
1408         if(event_info[i].flags & EVENT_HASDEFAULTHANDLERS) {
1409             hres = ensure_nsevent_handler(doc, NULL, NULL, i);
1410             if(FAILED(hres))
1411                 return hres;
1412         }
1413     }
1414
1415     return S_OK;
1416 }
1417
1418 void release_event_target(event_target_t *event_target)
1419 {
1420     int i, j;
1421
1422     for(i=0; i < EVENTID_LAST; i++) {
1423         if(event_target->event_table[i]) {
1424             if(event_target->event_table[i]->handler_prop)
1425                 IDispatch_Release(event_target->event_table[i]->handler_prop);
1426             for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
1427                 if(event_target->event_table[i]->handlers[j])
1428                     IDispatch_Release(event_target->event_table[i]->handlers[j]);
1429         }
1430     }
1431
1432     heap_free(event_target);
1433 }