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