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