mshtml: Added support for IHTMLEventObj::cancelBubble property.
[wine/multimedia.git] / dlls / mshtml / htmlevent.c
blobb9c9aad92a4ac838229560746638bc085e104ebf
1 /*
2 * Copyright 2008-2009 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "mshtmdid.h"
29 #include "mshtml_private.h"
30 #include "htmlevent.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36 typedef struct {
37 IDispatch *handler_prop;
38 DWORD handler_cnt;
39 IDispatch *handlers[0];
40 } handler_vector_t;
42 struct event_target_t {
43 DWORD node_handlers_mask;
44 handler_vector_t *event_table[EVENTID_LAST];
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};
50 static const WCHAR blurW[] = {'b','l','u','r',0};
51 static const WCHAR onblurW[] = {'o','n','b','l','u','r',0};
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};
56 static const WCHAR clickW[] = {'c','l','i','c','k',0};
57 static const WCHAR onclickW[] = {'o','n','c','l','i','c','k',0};
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};
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};
65 static const WCHAR dragW[] = {'d','r','a','g',0};
66 static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};
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};
71 static const WCHAR errorW[] = {'e','r','r','o','r',0};
72 static const WCHAR onerrorW[] = {'o','n','e','r','r','o','r',0};
74 static const WCHAR focusW[] = {'f','o','c','u','s',0};
75 static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};
77 static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
78 static const WCHAR onkeydownW[] = {'o','n','k','e','y','d','o','w','n',0};
80 static const WCHAR keypressW[] = {'k','e','y','p','r','e','s','s',0};
81 static const WCHAR onkeypressW[] = {'o','n','k','e','y','p','r','e','s','s',0};
83 static const WCHAR keyupW[] = {'k','e','y','u','p',0};
84 static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};
86 static const WCHAR loadW[] = {'l','o','a','d',0};
87 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
89 static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
90 static const WCHAR onmousedownW[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
92 static const WCHAR mousemoveW[] = {'m','o','u','s','e','m','o','v','e',0};
93 static const WCHAR onmousemoveW[] = {'o','n','m','o','u','s','e','m','o','v','e',0};
95 static const WCHAR mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
96 static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};
98 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
99 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
101 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
102 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
104 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
105 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
107 static const WCHAR readystatechangeW[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
108 static const WCHAR onreadystatechangeW[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
110 static const WCHAR resizeW[] = {'r','e','s','i','z','e',0};
111 static const WCHAR onresizeW[] = {'o','n','r','e','s','i','z','e',0};
113 static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
114 static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
116 static const WCHAR submitW[] = {'s','u','b','m','i','t',0};
117 static const WCHAR onsubmitW[] = {'o','n','s','u','b','m','i','t',0};
119 static const WCHAR HTMLEventsW[] = {'H','T','M','L','E','v','e','n','t','s',0};
120 static const WCHAR KeyboardEventW[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
121 static const WCHAR MouseEventW[] = {'M','o','u','s','e','E','v','e','n','t',0};
123 enum {
124 EVENTT_NONE,
125 EVENTT_HTML,
126 EVENTT_KEY,
127 EVENTT_MOUSE
130 static const WCHAR *event_types[] = {
131 NULL,
132 HTMLEventsW,
133 KeyboardEventW,
134 MouseEventW
137 typedef struct {
138 LPCWSTR name;
139 LPCWSTR attr_name;
140 DWORD type;
141 DISPID dispid;
142 DWORD flags;
143 } event_info_t;
145 #define EVENT_DEFAULTLISTENER 0x0001
146 #define EVENT_BUBBLE 0x0002
147 #define EVENT_FORWARDBODY 0x0004
148 #define EVENT_NODEHANDLER 0x0008
149 #define EVENT_CANCELABLE 0x0010
150 #define EVENT_HASDEFAULTHANDLERS 0x0020
152 static const event_info_t event_info[] = {
153 {beforeunloadW, onbeforeunloadW, EVENTT_NONE, DISPID_EVMETH_ONBEFOREUNLOAD,
154 EVENT_DEFAULTLISTENER|EVENT_FORWARDBODY},
155 {blurW, onblurW, EVENTT_HTML, DISPID_EVMETH_ONBLUR,
156 EVENT_DEFAULTLISTENER},
157 {changeW, onchangeW, EVENTT_HTML, DISPID_EVMETH_ONCHANGE,
158 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
159 {clickW, onclickW, EVENTT_MOUSE, DISPID_EVMETH_ONCLICK,
160 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE|EVENT_HASDEFAULTHANDLERS},
161 {contextmenuW, oncontextmenuW, EVENTT_MOUSE, DISPID_EVMETH_ONCONTEXTMENU,
162 EVENT_BUBBLE|EVENT_CANCELABLE},
163 {dblclickW, ondblclickW, EVENTT_MOUSE, DISPID_EVMETH_ONDBLCLICK,
164 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE},
165 {dragW, ondragW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAG,
166 EVENT_CANCELABLE},
167 {dragstartW, ondragstartW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAGSTART,
168 EVENT_CANCELABLE},
169 {errorW, onerrorW, EVENTT_NONE, DISPID_EVMETH_ONERROR,
170 EVENT_NODEHANDLER},
171 {focusW, onfocusW, EVENTT_HTML, DISPID_EVMETH_ONFOCUS,
172 EVENT_DEFAULTLISTENER},
173 {keydownW, onkeydownW, EVENTT_KEY, DISPID_EVMETH_ONKEYDOWN,
174 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
175 {keypressW, onkeypressW, EVENTT_KEY, DISPID_EVMETH_ONKEYPRESS,
176 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
177 {keyupW, onkeyupW, EVENTT_KEY, DISPID_EVMETH_ONKEYUP,
178 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
179 {loadW, onloadW, EVENTT_HTML, DISPID_EVMETH_ONLOAD,
180 EVENT_NODEHANDLER},
181 {mousedownW, onmousedownW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEDOWN,
182 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
183 {mousemoveW, onmousemoveW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEMOVE,
184 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
185 {mouseoutW, onmouseoutW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEOUT,
186 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
187 {mouseoverW, onmouseoverW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEOVER,
188 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
189 {mouseupW, onmouseupW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEUP,
190 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
191 {pasteW, onpasteW, EVENTT_NONE, DISPID_EVMETH_ONPASTE,
192 EVENT_CANCELABLE},
193 {readystatechangeW, onreadystatechangeW, EVENTT_NONE, DISPID_EVMETH_ONREADYSTATECHANGE,
195 {resizeW, onresizeW, EVENTT_NONE, DISPID_EVMETH_ONRESIZE,
196 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
197 {selectstartW, onselectstartW, EVENTT_MOUSE, DISPID_EVMETH_ONSELECTSTART,
198 EVENT_CANCELABLE},
199 {submitW, onsubmitW, EVENTT_HTML, DISPID_EVMETH_ONSUBMIT,
200 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE}
203 static const eventid_t node_handled_list[] = { EVENTID_ERROR, EVENTID_LOAD };
205 eventid_t str_to_eid(LPCWSTR str)
207 int i;
209 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
210 if(!strcmpW(event_info[i].name, str))
211 return i;
214 ERR("unknown type %s\n", debugstr_w(str));
215 return EVENTID_LAST;
218 static eventid_t attr_to_eid(LPCWSTR str)
220 int i;
222 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
223 if(!strcmpW(event_info[i].attr_name, str))
224 return i;
227 return EVENTID_LAST;
230 static DWORD get_node_handler_mask(eventid_t eid)
232 DWORD i;
234 for(i=0; i<sizeof(node_handled_list)/sizeof(*node_handled_list); i++) {
235 if(node_handled_list[i] == eid)
236 return 1 << i;
239 ERR("Invalid eid %d\n", eid);
240 return ~0;
243 typedef struct {
244 DispatchEx dispex;
245 IHTMLEventObj IHTMLEventObj_iface;
247 LONG ref;
249 HTMLDOMNode *target;
250 const event_info_t *type;
251 nsIDOMEvent *nsevent;
252 BOOL prevent_default;
253 BOOL cancel_bubble;
254 } HTMLEventObj;
256 static inline HTMLEventObj *impl_from_IHTMLEventObj(IHTMLEventObj *iface)
258 return CONTAINING_RECORD(iface, HTMLEventObj, IHTMLEventObj_iface);
261 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
263 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
265 *ppv = NULL;
267 if(IsEqualGUID(&IID_IUnknown, riid)) {
268 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
269 *ppv = &This->IHTMLEventObj_iface;
270 }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
271 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
272 *ppv = &This->IHTMLEventObj_iface;
273 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
274 return *ppv ? S_OK : E_NOINTERFACE;
277 if(*ppv) {
278 IUnknown_AddRef((IUnknown*)*ppv);
279 return S_OK;
282 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
283 return E_NOINTERFACE;
286 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
288 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
289 LONG ref = InterlockedIncrement(&This->ref);
291 TRACE("(%p) ref=%d\n", This, ref);
293 return ref;
296 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
298 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
299 LONG ref = InterlockedDecrement(&This->ref);
301 TRACE("(%p) ref=%d\n", This, ref);
303 if(!ref) {
304 if(This->target)
305 IHTMLDOMNode_Release(&This->target->IHTMLDOMNode_iface);
306 if(This->nsevent)
307 nsIDOMEvent_Release(This->nsevent);
308 release_dispex(&This->dispex);
309 heap_free(This);
312 return ref;
315 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
317 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
318 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
321 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
322 LCID lcid, ITypeInfo **ppTInfo)
324 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
325 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
328 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
329 LPOLESTR *rgszNames, UINT cNames,
330 LCID lcid, DISPID *rgDispId)
332 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
333 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
334 lcid, rgDispId);
337 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
338 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
339 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
341 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
342 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
343 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
346 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
348 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
350 TRACE("(%p)->(%p)\n", This, p);
352 return IHTMLDOMNode_QueryInterface(&This->target->IHTMLDOMNode_iface, &IID_IHTMLElement,
353 (void**)p);
356 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
358 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
359 cpp_bool ret = FALSE;
361 TRACE("(%p)->(%p)\n", This, p);
363 if(This->nsevent) {
364 nsIDOMKeyEvent *key_event;
365 nsresult nsres;
367 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
368 if(NS_SUCCEEDED(nsres)) {
369 nsIDOMKeyEvent_GetAltKey(key_event, &ret);
370 nsIDOMKeyEvent_Release(key_event);
371 }else {
372 nsIDOMMouseEvent *mouse_event;
374 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
375 if(NS_SUCCEEDED(nsres)) {
376 nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
377 nsIDOMMouseEvent_Release(mouse_event);
382 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
383 return S_OK;
386 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
388 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
389 cpp_bool ret = FALSE;
391 TRACE("(%p)->(%p)\n", This, p);
393 if(This->nsevent) {
394 nsIDOMKeyEvent *key_event;
395 nsresult nsres;
397 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
398 if(NS_SUCCEEDED(nsres)) {
399 nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
400 nsIDOMKeyEvent_Release(key_event);
401 }else {
402 nsIDOMMouseEvent *mouse_event;
404 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
405 if(NS_SUCCEEDED(nsres)) {
406 nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
407 nsIDOMMouseEvent_Release(mouse_event);
412 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
413 return S_OK;
416 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
418 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
419 cpp_bool ret = FALSE;
421 TRACE("(%p)->(%p)\n", This, p);
423 if(This->nsevent) {
424 nsIDOMKeyEvent *key_event;
425 nsresult nsres;
427 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
428 if(NS_SUCCEEDED(nsres)) {
429 nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
430 nsIDOMKeyEvent_Release(key_event);
431 }else {
432 nsIDOMMouseEvent *mouse_event;
434 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
435 if(NS_SUCCEEDED(nsres)) {
436 nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
437 nsIDOMMouseEvent_Release(mouse_event);
442 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
443 return S_OK;
446 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
448 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
450 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
452 if(V_VT(&v) != VT_BOOL) {
453 FIXME("unsupported value %s\n", debugstr_variant(&v));
454 return DISP_E_BADVARTYPE;
457 if(!V_BOOL(&v))
458 This->prevent_default = TRUE;
459 return S_OK;
462 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
464 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
466 FIXME("(%p)->(%p)\n", This, p);
468 V_VT(p) = VT_EMPTY;
469 return S_OK;
472 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
474 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
476 TRACE("(%p)->(%x)\n", This, v);
478 This->cancel_bubble = !!v;
479 return S_OK;
482 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
484 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
486 TRACE("(%p)->(%p)\n", This, p);
488 *p = This->cancel_bubble ? VARIANT_TRUE : VARIANT_FALSE;
489 return S_OK;
492 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
494 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
496 FIXME("(%p)->(%p)\n", This, p);
498 *p = NULL;
499 return S_OK;
502 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
504 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
506 FIXME("(%p)->(%p)\n", This, p);
508 *p = NULL;
509 return S_OK;
512 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
514 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
515 FIXME("(%p)->(%d)\n", This, v);
516 return E_NOTIMPL;
519 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
521 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
522 PRUint32 key_code = 0;
524 TRACE("(%p)->(%p)\n", This, p);
526 if(This->nsevent) {
527 nsIDOMKeyEvent *key_event;
528 nsresult nsres;
530 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
531 if(NS_SUCCEEDED(nsres)) {
532 nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
533 nsIDOMKeyEvent_Release(key_event);
537 *p = key_code;
538 return S_OK;
541 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
543 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
544 PRUint16 button = 0;
546 TRACE("(%p)->(%p)\n", This, p);
548 if(This->nsevent) {
549 nsIDOMMouseEvent *mouse_event;
550 nsresult nsres;
552 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
553 if(NS_SUCCEEDED(nsres)) {
554 nsIDOMMouseEvent_GetButton(mouse_event, &button);
555 nsIDOMMouseEvent_Release(mouse_event);
559 *p = button;
560 return S_OK;
563 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
565 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
567 TRACE("(%p)->(%p)\n", This, p);
569 *p = SysAllocString(This->type->name);
570 return *p ? S_OK : E_OUTOFMEMORY;
573 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
575 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
577 FIXME("(%p)->(%p)\n", This, p);
579 *p = NULL;
580 return S_OK;
583 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
585 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
587 FIXME("(%p)->(%p)\n", This, p);
589 *p = 0;
590 return S_OK;
593 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
595 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
597 FIXME("(%p)->(%p)\n", This, p);
599 *p = -1;
600 return S_OK;
603 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
605 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
607 FIXME("(%p)->(%p)\n", This, p);
609 *p = -1;
610 return S_OK;
613 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
615 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
616 PRInt32 x = 0;
618 TRACE("(%p)->(%p)\n", This, p);
620 if(This->nsevent) {
621 nsIDOMMouseEvent *mouse_event;
622 nsresult nsres;
624 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
625 if(NS_SUCCEEDED(nsres)) {
626 nsIDOMMouseEvent_GetClientX(mouse_event, &x);
627 nsIDOMMouseEvent_Release(mouse_event);
631 *p = x;
632 return S_OK;
635 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
637 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
638 PRInt32 y = 0;
640 TRACE("(%p)->(%p)\n", This, p);
642 if(This->nsevent) {
643 nsIDOMMouseEvent *mouse_event;
644 nsresult nsres;
646 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
647 if(NS_SUCCEEDED(nsres)) {
648 nsIDOMMouseEvent_GetClientY(mouse_event, &y);
649 nsIDOMMouseEvent_Release(mouse_event);
653 *p = y;
654 return S_OK;
657 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
659 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
661 FIXME("(%p)->(%p)\n", This, p);
663 *p = 0;
664 return S_OK;
667 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
669 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
671 FIXME("(%p)->(%p)\n", This, p);
673 *p = 0;
674 return S_OK;
677 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
679 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
680 PRInt32 x = 0;
682 TRACE("(%p)->(%p)\n", This, p);
684 if(This->nsevent) {
685 nsIDOMMouseEvent *mouse_event;
686 nsresult nsres;
688 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
689 if(NS_SUCCEEDED(nsres)) {
690 nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
691 nsIDOMMouseEvent_Release(mouse_event);
695 *p = x;
696 return S_OK;
699 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
701 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
702 PRInt32 y = 0;
704 TRACE("(%p)->(%p)\n", This, p);
706 if(This->nsevent) {
707 nsIDOMMouseEvent *mouse_event;
708 nsresult nsres;
710 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
711 if(NS_SUCCEEDED(nsres)) {
712 nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
713 nsIDOMMouseEvent_Release(mouse_event);
717 *p = y;
718 return S_OK;
721 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
723 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
725 FIXME("(%p)->(%p)\n", This, p);
727 *p = NULL;
728 return S_OK;
731 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
732 HTMLEventObj_QueryInterface,
733 HTMLEventObj_AddRef,
734 HTMLEventObj_Release,
735 HTMLEventObj_GetTypeInfoCount,
736 HTMLEventObj_GetTypeInfo,
737 HTMLEventObj_GetIDsOfNames,
738 HTMLEventObj_Invoke,
739 HTMLEventObj_get_srcElement,
740 HTMLEventObj_get_altKey,
741 HTMLEventObj_get_ctrlKey,
742 HTMLEventObj_get_shiftKey,
743 HTMLEventObj_put_returnValue,
744 HTMLEventObj_get_returnValue,
745 HTMLEventObj_put_cancelBubble,
746 HTMLEventObj_get_cancelBubble,
747 HTMLEventObj_get_fromElement,
748 HTMLEventObj_get_toElement,
749 HTMLEventObj_put_keyCode,
750 HTMLEventObj_get_keyCode,
751 HTMLEventObj_get_button,
752 HTMLEventObj_get_type,
753 HTMLEventObj_get_qualifier,
754 HTMLEventObj_get_reason,
755 HTMLEventObj_get_x,
756 HTMLEventObj_get_y,
757 HTMLEventObj_get_clientX,
758 HTMLEventObj_get_clientY,
759 HTMLEventObj_get_offsetX,
760 HTMLEventObj_get_offsetY,
761 HTMLEventObj_get_screenX,
762 HTMLEventObj_get_screenY,
763 HTMLEventObj_get_srcFilter
766 static const tid_t HTMLEventObj_iface_tids[] = {
767 IHTMLEventObj_tid,
771 static dispex_static_data_t HTMLEventObj_dispex = {
772 NULL,
773 DispCEventObj_tid,
774 NULL,
775 HTMLEventObj_iface_tids
778 static HTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
780 HTMLEventObj *ret;
782 ret = heap_alloc_zero(sizeof(*ret));
783 if(!ret)
784 return NULL;
786 ret->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
787 ret->ref = 1;
788 ret->type = event_info+eid;
790 ret->nsevent = nsevent;
791 if(nsevent) {
792 nsIDOMEvent_AddRef(nsevent);
793 }else if(event_types[event_info[eid].type]) {
794 nsAString type_str;
795 nsresult nsres;
797 nsAString_InitDepend(&type_str, event_types[event_info[eid].type]);
798 nsres = nsIDOMHTMLDocument_CreateEvent(target->doc->nsdoc, &type_str, &ret->nsevent);
799 nsAString_Finish(&type_str);
800 if(NS_FAILED(nsres)) {
801 ERR("Could not create event: %08x\n", nsres);
802 IHTMLEventObj_Release(&ret->IHTMLEventObj_iface);
803 return NULL;
807 ret->target = target;
808 IHTMLDOMNode_AddRef(&target->IHTMLDOMNode_iface);
810 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLEventObj_iface, &HTMLEventObj_dispex);
812 return ret;
815 static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
817 IDispatchEx *dispex;
818 EXCEPINFO ei;
819 HRESULT hres;
821 memset(&ei, 0, sizeof(ei));
823 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
824 if(SUCCEEDED(hres)) {
825 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, retv, &ei, NULL);
826 IDispatchEx_Release(dispex);
827 }else {
828 TRACE("Could not get IDispatchEx interface: %08x\n", hres);
829 hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
830 dp, retv, &ei, NULL);
833 return hres;
836 static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, VARIANT *retv)
838 DISPPARAMS dp = {NULL,NULL,0,0};
839 ULONG argerr;
840 EXCEPINFO ei;
842 memset(&ei, 0, sizeof(ei));
843 return IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, retv, &ei, &argerr);
846 static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
848 int min, max, i;
849 HRESULT hres;
851 if(!data)
852 return FALSE;
854 if(!data->ids) {
855 hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
856 if(FAILED(hres))
857 return FALSE;
860 min = 0;
861 max = data->id_cnt-1;
862 while(min <= max) {
863 i = (min+max)/2;
864 if(data->ids[i] == dispid)
865 return TRUE;
867 if(data->ids[i] < dispid)
868 min = i+1;
869 else
870 max = i-1;
873 return FALSE;
876 static void call_event_handlers(HTMLDocumentNode *doc, HTMLEventObj *event_obj, event_target_t *event_target,
877 ConnectionPointContainer *cp_container, eventid_t eid, IDispatch *this_obj)
879 const BOOL cancelable = event_info[eid].flags & EVENT_CANCELABLE;
880 handler_vector_t *handler_vector = NULL;
881 VARIANT v;
882 int i;
883 HRESULT hres;
885 if(event_target)
886 handler_vector = event_target->event_table[eid];
888 if(handler_vector && handler_vector->handler_prop) {
889 DISPID named_arg = DISPID_THIS;
890 VARIANTARG arg;
891 DISPPARAMS dp = {&arg, &named_arg, 1, 1};
893 V_VT(&arg) = VT_DISPATCH;
894 V_DISPATCH(&arg) = this_obj;
895 V_VT(&v) = VT_EMPTY;
897 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
898 hres = call_disp_func(handler_vector->handler_prop, &dp, &v);
899 if(hres == S_OK) {
900 TRACE("%s <<< %s\n", debugstr_w(event_info[eid].name), debugstr_variant(&v));
902 if(cancelable) {
903 if(V_VT(&v) == VT_BOOL) {
904 if(!V_BOOL(&v))
905 event_obj->prevent_default = TRUE;
906 }else if(V_VT(&v) != VT_EMPTY) {
907 FIXME("unhandled result %s\n", debugstr_variant(&v));
910 VariantClear(&v);
911 }else {
912 WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
916 if(handler_vector && handler_vector->handler_cnt) {
917 VARIANTARG arg;
918 DISPPARAMS dp = {&arg, NULL, 1, 0};
920 V_VT(&arg) = VT_DISPATCH;
921 V_DISPATCH(&arg) = (IDispatch*)event_obj;
923 i = handler_vector->handler_cnt;
924 while(i--) {
925 if(handler_vector->handlers[i]) {
926 V_VT(&v) = VT_EMPTY;
928 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
929 hres = call_disp_func(handler_vector->handlers[i], &dp, &v);
930 if(hres == S_OK) {
931 TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
933 if(cancelable) {
934 if(V_VT(&v) == VT_BOOL) {
935 if(!V_BOOL(&v))
936 event_obj->prevent_default = TRUE;
937 }else if(V_VT(&v) != VT_EMPTY) {
938 FIXME("unhandled result %s\n", debugstr_variant(&v));
941 VariantClear(&v);
942 }else {
943 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
949 if(cp_container) {
950 ConnectionPoint *cp;
952 if(cp_container->forward_container)
953 cp_container = cp_container->forward_container;
955 for(cp = cp_container->cp_list; cp; cp = cp->next) {
956 if(cp->sinks_size && is_cp_event(cp->data, event_info[eid].dispid)) {
957 for(i=0; i < cp->sinks_size; i++) {
958 if(!cp->sinks[i].disp)
959 continue;
961 V_VT(&v) = VT_EMPTY;
963 TRACE("cp %s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
964 hres = call_cp_func(cp->sinks[i].disp, event_info[eid].dispid, &v);
965 if(hres == S_OK) {
966 TRACE("cp %s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
968 if(cancelable) {
969 if(V_VT(&v) == VT_BOOL) {
970 if(!V_BOOL(&v))
971 event_obj->prevent_default = TRUE;
972 }else if(V_VT(&v) != VT_EMPTY) {
973 FIXME("unhandled result %s\n", debugstr_variant(&v));
976 VariantClear(&v);
977 }else {
978 WARN("cp %s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
986 void fire_event(HTMLDocumentNode *doc, eventid_t eid, BOOL set_event, nsIDOMNode *target, nsIDOMEvent *nsevent)
988 HTMLEventObj *event_obj = NULL;
989 IHTMLEventObj *prev_event;
990 nsIDOMNode *parent, *nsnode;
991 BOOL prevent_default = FALSE;
992 HTMLDOMNode *node;
993 PRUint16 node_type;
994 nsresult nsres;
995 HRESULT hres;
997 TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
999 prev_event = doc->basedoc.window->event;
1000 if(set_event) {
1001 hres = get_node(doc, target, TRUE, &node);
1002 if(FAILED(hres))
1003 return;
1005 event_obj = create_event(node, eid, nsevent);
1006 doc->basedoc.window->event = &event_obj->IHTMLEventObj_iface;
1007 }else {
1008 doc->basedoc.window->event = NULL;
1011 nsIDOMNode_GetNodeType(target, &node_type);
1012 nsnode = target;
1013 nsIDOMNode_AddRef(nsnode);
1015 switch(node_type) {
1016 case ELEMENT_NODE:
1017 do {
1018 hres = get_node(doc, nsnode, FALSE, &node);
1019 if(SUCCEEDED(hres) && node)
1020 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1021 node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1023 if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1024 break;
1026 nsIDOMNode_GetParentNode(nsnode, &parent);
1027 nsIDOMNode_Release(nsnode);
1028 nsnode = parent;
1029 if(!nsnode)
1030 break;
1032 nsIDOMNode_GetNodeType(nsnode, &node_type);
1033 }while(node_type == ELEMENT_NODE);
1035 if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1036 break;
1038 case DOCUMENT_NODE:
1039 if(event_info[eid].flags & EVENT_FORWARDBODY) {
1040 nsIDOMHTMLElement *nsbody;
1041 nsresult nsres;
1043 nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
1044 if(NS_SUCCEEDED(nsres) && nsbody) {
1045 hres = get_node(doc, (nsIDOMNode*)nsbody, FALSE, &node);
1046 if(SUCCEEDED(hres) && node)
1047 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1048 node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1049 nsIDOMHTMLElement_Release(nsbody);
1050 }else {
1051 ERR("Could not get body: %08x\n", nsres);
1055 call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
1056 (IDispatch*)&doc->basedoc.IHTMLDocument2_iface);
1057 break;
1059 default:
1060 FIXME("unimplemented node type %d\n", node_type);
1063 if(nsnode)
1064 nsIDOMNode_Release(nsnode);
1066 if(event_obj && event_obj->prevent_default)
1067 prevent_default = TRUE;
1068 doc->basedoc.window->event = prev_event;
1069 if(event_obj)
1070 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1072 if(!prevent_default && (event_info[eid].flags & EVENT_HASDEFAULTHANDLERS)) {
1073 nsIDOMNode_AddRef(target);
1074 nsnode = target;
1076 do {
1077 hres = get_node(doc, nsnode, TRUE, &node);
1078 if(FAILED(hres))
1079 break;
1081 if(node && node->vtbl->handle_event) {
1082 hres = node->vtbl->handle_event(node, eid, nsevent, &prevent_default);
1083 if(FAILED(hres) || prevent_default || (event_obj && event_obj->cancel_bubble))
1084 break;
1087 nsres = nsIDOMNode_GetParentNode(nsnode, &parent);
1088 if(NS_FAILED(nsres))
1089 break;
1091 nsIDOMNode_Release(nsnode);
1092 nsnode = parent;
1093 } while(nsnode);
1095 if(nsnode)
1096 nsIDOMNode_Release(nsnode);
1099 if(prevent_default && nsevent) {
1100 TRACE("calling PreventDefault\n");
1101 nsIDOMEvent_PreventDefault(nsevent);
1105 HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_obj, VARIANT_BOOL *cancelled)
1107 eventid_t eid;
1109 eid = attr_to_eid(event_name);
1110 if(eid == EVENTID_LAST) {
1111 WARN("unknown event %s\n", debugstr_w(event_name));
1112 return E_INVALIDARG;
1115 if(event_obj && V_VT(event_obj) != VT_EMPTY && V_VT(event_obj) != VT_ERROR)
1116 FIXME("event_obj not implemented\n");
1118 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
1119 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1120 return E_NOTIMPL;
1123 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1125 *cancelled = VARIANT_TRUE; /* FIXME */
1126 return S_OK;
1129 HRESULT call_fire_event(HTMLDOMNode *node, eventid_t eid)
1131 HRESULT hres;
1133 if(node->vtbl->fire_event) {
1134 BOOL handled = FALSE;
1136 hres = node->vtbl->fire_event(node, eid, &handled);
1137 if(handled)
1138 return hres;
1141 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1142 return S_OK;
1145 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
1147 if(!*event_target_ptr)
1148 *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
1149 return *event_target_ptr;
1152 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
1154 handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
1156 if(handler_vector) {
1157 if(cnt <= handler_vector->handler_cnt)
1158 return TRUE;
1160 new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1161 }else {
1162 new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1165 if(!new_vector)
1166 return FALSE;
1168 new_vector->handler_cnt = cnt;
1169 event_target->event_table[eid] = new_vector;
1170 return TRUE;
1173 static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1175 if(!doc->nsdoc)
1176 return S_OK;
1178 if(event_info[eid].flags & EVENT_NODEHANDLER) {
1179 DWORD mask;
1181 mask = get_node_handler_mask(eid);
1182 if(event_target->node_handlers_mask & mask)
1183 return S_OK;
1185 add_nsevent_listener(doc, nsnode, event_info[eid].name);
1186 event_target->node_handlers_mask |= mask;
1187 return S_OK;
1190 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER))
1191 return S_OK;
1193 if(!doc->event_vector[eid]) {
1194 doc->event_vector[eid] = TRUE;
1195 add_nsevent_listener(doc, NULL, event_info[eid].name);
1198 return S_OK;
1201 void detach_events(HTMLDocumentNode *doc)
1203 if(doc->event_vector) {
1204 int i;
1206 for(i=0; i < EVENTID_LAST; i++) {
1207 if(doc->event_vector[i])
1208 detach_nsevent(doc, event_info[i].name);
1212 release_nsevents(doc);
1216 static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
1218 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1219 IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
1220 (*event_target)->event_table[eid]->handler_prop = NULL;
1223 return S_OK;
1226 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocumentNode *doc,
1227 eventid_t eid, IDispatch *disp)
1229 event_target_t *event_target;
1231 if(!disp)
1232 return remove_event_handler(event_target_ptr, eid);
1234 event_target = get_event_target(event_target_ptr);
1235 if(!event_target)
1236 return E_OUTOFMEMORY;
1238 if(!alloc_handler_vector(event_target, eid, 0))
1239 return E_OUTOFMEMORY;
1241 if(event_target->event_table[eid]->handler_prop)
1242 IDispatch_Release(event_target->event_table[eid]->handler_prop);
1244 event_target->event_table[eid]->handler_prop = disp;
1245 IDispatch_AddRef(disp);
1247 return ensure_nsevent_handler(doc, event_target, nsnode, eid);
1250 HRESULT set_event_handler(event_target_t **event_target, nsIDOMNode *nsnode, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1252 switch(V_VT(var)) {
1253 case VT_NULL:
1254 return remove_event_handler(event_target, eid);
1256 case VT_DISPATCH:
1257 return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1259 default:
1260 FIXME("not handler %s\n", debugstr_variant(var));
1261 /* fall through */
1262 case VT_EMPTY:
1263 return E_NOTIMPL;
1266 return S_OK;
1269 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
1271 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1272 V_VT(var) = VT_DISPATCH;
1273 V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1274 IDispatch_AddRef(V_DISPATCH(var));
1275 }else {
1276 V_VT(var) = VT_NULL;
1279 return S_OK;
1282 HRESULT attach_event(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocument *doc, BSTR name,
1283 IDispatch *disp, VARIANT_BOOL *res)
1285 event_target_t *event_target;
1286 eventid_t eid;
1287 DWORD i = 0;
1289 eid = attr_to_eid(name);
1290 if(eid == EVENTID_LAST) {
1291 WARN("Unknown event\n");
1292 *res = VARIANT_TRUE;
1293 return S_OK;
1296 event_target = get_event_target(event_target_ptr);
1297 if(!event_target)
1298 return E_OUTOFMEMORY;
1300 if(event_target->event_table[eid]) {
1301 while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
1302 i++;
1303 if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
1304 return E_OUTOFMEMORY;
1305 }else if(!alloc_handler_vector(event_target, eid, i+1)) {
1306 return E_OUTOFMEMORY;
1309 IDispatch_AddRef(disp);
1310 event_target->event_table[eid]->handlers[i] = disp;
1312 *res = VARIANT_TRUE;
1313 return ensure_nsevent_handler(doc->doc_node, event_target, nsnode, eid);
1316 HRESULT detach_event(event_target_t *event_target, HTMLDocument *doc, BSTR name, IDispatch *disp)
1318 eventid_t eid;
1319 DWORD i = 0;
1321 if(!event_target)
1322 return S_OK;
1324 eid = attr_to_eid(name);
1325 if(eid == EVENTID_LAST) {
1326 WARN("Unknown event\n");
1327 return S_OK;
1330 if(!event_target->event_table[eid])
1331 return S_OK;
1333 while(i < event_target->event_table[eid]->handler_cnt) {
1334 if(event_target->event_table[eid]->handlers[i] == disp) {
1335 IDispatch_Release(event_target->event_table[eid]->handlers[i]);
1336 event_target->event_table[eid]->handlers[i] = NULL;
1338 i++;
1341 return S_OK;
1344 void update_cp_events(HTMLWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp, nsIDOMNode *nsnode)
1346 event_target_t *event_target;
1347 int i;
1349 event_target = get_event_target(event_target_ptr);
1350 if(!event_target)
1351 return; /* FIXME */
1353 for(i=0; i < EVENTID_LAST; i++) {
1354 if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1355 ensure_nsevent_handler(window->doc, event_target, nsnode, i);
1359 void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
1361 const PRUnichar *attr_value;
1362 nsAString attr_name_str, attr_value_str;
1363 IDispatch *disp;
1364 HTMLDOMNode *node;
1365 int i;
1366 nsresult nsres;
1367 HRESULT hres;
1369 nsAString_Init(&attr_value_str, NULL);
1370 nsAString_Init(&attr_name_str, NULL);
1372 for(i=0; i < EVENTID_LAST; i++) {
1373 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
1374 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
1375 if(NS_SUCCEEDED(nsres)) {
1376 nsAString_GetData(&attr_value_str, &attr_value);
1377 if(!*attr_value)
1378 continue;
1380 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
1382 disp = script_parse_event(doc->basedoc.window, attr_value);
1383 if(disp) {
1384 hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1385 if(SUCCEEDED(hres))
1386 set_event_handler_disp(get_node_event_target(node), node->nsnode, node->doc, i, disp);
1387 IDispatch_Release(disp);
1392 nsAString_Finish(&attr_value_str);
1393 nsAString_Finish(&attr_name_str);
1396 HRESULT doc_init_events(HTMLDocumentNode *doc)
1398 unsigned i;
1399 HRESULT hres;
1401 doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
1402 if(!doc->event_vector)
1403 return E_OUTOFMEMORY;
1405 init_nsevents(doc);
1407 for(i=0; i < EVENTID_LAST; i++) {
1408 if(event_info[i].flags & EVENT_HASDEFAULTHANDLERS) {
1409 hres = ensure_nsevent_handler(doc, NULL, NULL, i);
1410 if(FAILED(hres))
1411 return hres;
1415 return S_OK;
1418 void release_event_target(event_target_t *event_target)
1420 int i, j;
1422 for(i=0; i < EVENTID_LAST; i++) {
1423 if(event_target->event_table[i]) {
1424 if(event_target->event_table[i]->handler_prop)
1425 IDispatch_Release(event_target->event_table[i]->handler_prop);
1426 for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
1427 if(event_target->event_table[i]->handlers[j])
1428 IDispatch_Release(event_target->event_table[i]->handlers[j]);
1432 heap_free(event_target);