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