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