mshtml: Added IHTMLButtonElement::name tests.
[wine.git] / dlls / mshtml / htmlevent.c
blob9ea17082062195393af573845dcab78a06d1985e
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"
31 #include "htmlscript.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37 typedef struct {
38 IDispatch *handler_prop;
39 DWORD handler_cnt;
40 IDispatch *handlers[0];
41 } handler_vector_t;
43 struct event_target_t {
44 DWORD node_handlers_mask;
45 handler_vector_t *event_table[EVENTID_LAST];
48 static const WCHAR abortW[] = {'a','b','o','r','t',0};
49 static const WCHAR onabortW[] = {'o','n','a','b','o','r','t',0};
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};
54 static const WCHAR blurW[] = {'b','l','u','r',0};
55 static const WCHAR onblurW[] = {'o','n','b','l','u','r',0};
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};
60 static const WCHAR clickW[] = {'c','l','i','c','k',0};
61 static const WCHAR onclickW[] = {'o','n','c','l','i','c','k',0};
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};
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};
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};
72 static const WCHAR dragW[] = {'d','r','a','g',0};
73 static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};
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};
78 static const WCHAR errorW[] = {'e','r','r','o','r',0};
79 static const WCHAR onerrorW[] = {'o','n','e','r','r','o','r',0};
81 static const WCHAR focusW[] = {'f','o','c','u','s',0};
82 static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};
84 static const WCHAR helpW[] = {'h','e','l','p',0};
85 static const WCHAR onhelpW[] = {'o','n','h','e','l','p',0};
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};
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};
93 static const WCHAR keyupW[] = {'k','e','y','u','p',0};
94 static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};
96 static const WCHAR loadW[] = {'l','o','a','d',0};
97 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
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};
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};
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};
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};
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};
114 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
115 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
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};
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};
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};
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};
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};
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};
136 enum {
137 EVENTT_NONE,
138 EVENTT_HTML,
139 EVENTT_KEY,
140 EVENTT_MOUSE
143 static const WCHAR *event_types[] = {
144 NULL,
145 HTMLEventsW,
146 KeyboardEventW,
147 MouseEventW
150 typedef struct {
151 LPCWSTR name;
152 LPCWSTR attr_name;
153 DWORD type;
154 DISPID dispid;
155 DWORD flags;
156 } event_info_t;
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
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,
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}
224 static const eventid_t node_handled_list[] = { EVENTID_ABORT, EVENTID_ERROR, EVENTID_LOAD };
226 eventid_t str_to_eid(LPCWSTR str)
228 int i;
230 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
231 if(!strcmpW(event_info[i].name, str))
232 return i;
235 ERR("unknown type %s\n", debugstr_w(str));
236 return EVENTID_LAST;
239 static eventid_t attr_to_eid(LPCWSTR str)
241 int i;
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;
248 return EVENTID_LAST;
251 static DWORD get_node_handler_mask(eventid_t eid)
253 DWORD i;
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;
260 ERR("Invalid eid %d\n", eid);
261 return ~0;
264 typedef struct {
265 DispatchEx dispex;
266 IHTMLEventObj IHTMLEventObj_iface;
268 LONG ref;
270 HTMLDOMNode *target;
271 const event_info_t *type;
272 nsIDOMEvent *nsevent;
273 BOOL prevent_default;
274 BOOL cancel_bubble;
275 } HTMLEventObj;
277 static inline HTMLEventObj *impl_from_IHTMLEventObj(IHTMLEventObj *iface)
279 return CONTAINING_RECORD(iface, HTMLEventObj, IHTMLEventObj_iface);
282 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
284 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
286 *ppv = NULL;
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;
298 if(*ppv) {
299 IUnknown_AddRef((IUnknown*)*ppv);
300 return S_OK;
303 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
304 return E_NOINTERFACE;
307 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
309 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
310 LONG ref = InterlockedIncrement(&This->ref);
312 TRACE("(%p) ref=%d\n", This, ref);
314 return ref;
317 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
319 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
320 LONG ref = InterlockedDecrement(&This->ref);
322 TRACE("(%p) ref=%d\n", This, ref);
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);
333 return ref;
336 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
338 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
339 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
342 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
343 LCID lcid, ITypeInfo **ppTInfo)
345 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
346 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
349 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
350 LPOLESTR *rgszNames, UINT cNames,
351 LCID lcid, DISPID *rgDispId)
353 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
354 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
355 lcid, rgDispId);
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)
362 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
363 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
364 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
367 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
369 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
371 TRACE("(%p)->(%p)\n", This, p);
373 *p = NULL;
374 if(This->target)
375 IHTMLDOMNode_QueryInterface(&This->target->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)p);
376 return S_OK;
379 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
381 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
382 cpp_bool ret = FALSE;
384 TRACE("(%p)->(%p)\n", This, p);
386 if(This->nsevent) {
387 nsIDOMKeyEvent *key_event;
388 nsresult nsres;
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;
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);
405 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
406 return S_OK;
409 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
411 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
412 cpp_bool ret = FALSE;
414 TRACE("(%p)->(%p)\n", This, p);
416 if(This->nsevent) {
417 nsIDOMKeyEvent *key_event;
418 nsresult nsres;
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;
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);
435 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
436 return S_OK;
439 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
441 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
442 cpp_bool ret = FALSE;
444 TRACE("(%p)->(%p)\n", This, p);
446 if(This->nsevent) {
447 nsIDOMKeyEvent *key_event;
448 nsresult nsres;
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;
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);
465 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
466 return S_OK;
469 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
471 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
473 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
475 if(V_VT(&v) != VT_BOOL) {
476 FIXME("unsupported value %s\n", debugstr_variant(&v));
477 return DISP_E_BADVARTYPE;
480 if(!V_BOOL(&v))
481 This->prevent_default = TRUE;
482 return S_OK;
485 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
487 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
489 FIXME("(%p)->(%p)\n", This, p);
491 V_VT(p) = VT_EMPTY;
492 return S_OK;
495 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
497 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
499 TRACE("(%p)->(%x)\n", This, v);
501 This->cancel_bubble = !!v;
502 return S_OK;
505 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
507 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
509 TRACE("(%p)->(%p)\n", This, p);
511 *p = This->cancel_bubble ? VARIANT_TRUE : VARIANT_FALSE;
512 return S_OK;
515 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
517 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
519 FIXME("(%p)->(%p)\n", This, p);
521 *p = NULL;
522 return S_OK;
525 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
527 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
529 FIXME("(%p)->(%p)\n", This, p);
531 *p = NULL;
532 return S_OK;
535 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
537 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
538 FIXME("(%p)->(%d)\n", This, v);
539 return E_NOTIMPL;
542 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
544 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
545 UINT32 key_code = 0;
547 TRACE("(%p)->(%p)\n", This, p);
549 if(This->nsevent) {
550 nsIDOMKeyEvent *key_event;
551 nsresult nsres;
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);
560 *p = key_code;
561 return S_OK;
564 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
566 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
567 UINT16 button = 0;
569 TRACE("(%p)->(%p)\n", This, p);
571 if(This->nsevent) {
572 nsIDOMMouseEvent *mouse_event;
573 nsresult nsres;
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);
582 *p = button;
583 return S_OK;
586 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
588 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
590 TRACE("(%p)->(%p)\n", This, p);
592 if(!This->type) {
593 *p = NULL;
594 return S_OK;
597 *p = SysAllocString(This->type->name);
598 return *p ? S_OK : E_OUTOFMEMORY;
601 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
603 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
605 FIXME("(%p)->(%p)\n", This, p);
607 *p = NULL;
608 return S_OK;
611 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
613 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
615 FIXME("(%p)->(%p)\n", This, p);
617 *p = 0;
618 return S_OK;
621 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
623 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
625 FIXME("(%p)->(%p)\n", This, p);
627 *p = -1;
628 return S_OK;
631 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
633 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
635 FIXME("(%p)->(%p)\n", This, p);
637 *p = -1;
638 return S_OK;
641 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
643 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
644 LONG x = 0;
646 TRACE("(%p)->(%p)\n", This, p);
648 if(This->nsevent) {
649 nsIDOMMouseEvent *mouse_event;
650 nsresult nsres;
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);
659 *p = x;
660 return S_OK;
663 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
665 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
666 LONG y = 0;
668 TRACE("(%p)->(%p)\n", This, p);
670 if(This->nsevent) {
671 nsIDOMMouseEvent *mouse_event;
672 nsresult nsres;
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);
681 *p = y;
682 return S_OK;
685 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
687 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
689 FIXME("(%p)->(%p)\n", This, p);
691 *p = 0;
692 return S_OK;
695 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
697 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
699 FIXME("(%p)->(%p)\n", This, p);
701 *p = 0;
702 return S_OK;
705 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
707 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
708 LONG x = 0;
710 TRACE("(%p)->(%p)\n", This, p);
712 if(This->nsevent) {
713 nsIDOMMouseEvent *mouse_event;
714 nsresult nsres;
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);
723 *p = x;
724 return S_OK;
727 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
729 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
730 LONG y = 0;
732 TRACE("(%p)->(%p)\n", This, p);
734 if(This->nsevent) {
735 nsIDOMMouseEvent *mouse_event;
736 nsresult nsres;
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);
745 *p = y;
746 return S_OK;
749 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
751 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
753 FIXME("(%p)->(%p)\n", This, p);
755 *p = NULL;
756 return S_OK;
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
794 static inline HTMLEventObj *unsafe_impl_from_IHTMLEventObj(IHTMLEventObj *iface)
796 return iface->lpVtbl == &HTMLEventObjVtbl ? impl_from_IHTMLEventObj(iface) : NULL;
799 static const tid_t HTMLEventObj_iface_tids[] = {
800 IHTMLEventObj_tid,
804 static dispex_static_data_t HTMLEventObj_dispex = {
805 NULL,
806 DispCEventObj_tid,
807 NULL,
808 HTMLEventObj_iface_tids
811 static HTMLEventObj *create_event(void)
813 HTMLEventObj *ret;
815 ret = heap_alloc_zero(sizeof(*ret));
816 if(!ret)
817 return NULL;
819 ret->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
820 ret->ref = 1;
822 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLEventObj_iface, &HTMLEventObj_dispex);
824 return ret;
827 static HRESULT set_event_info(HTMLEventObj *event, HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
829 event->type = event_info+eid;
830 event->nsevent = nsevent;
832 if(nsevent) {
833 nsIDOMEvent_AddRef(nsevent);
834 }else if(event_types[event_info[eid].type]) {
835 nsAString type_str;
836 nsresult nsres;
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;
847 event->target = target;
848 if(target)
849 IHTMLDOMNode_AddRef(&target->IHTMLDOMNode_iface);
850 return S_OK;
853 HRESULT create_event_obj(IHTMLEventObj **ret)
855 HTMLEventObj *event;
857 event = create_event();
858 if(!event)
859 return E_OUTOFMEMORY;
861 *ret = &event->IHTMLEventObj_iface;
862 return S_OK;
865 static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
867 IDispatchEx *dispex;
868 EXCEPINFO ei;
869 HRESULT hres;
871 memset(&ei, 0, sizeof(ei));
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);
883 return hres;
886 static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, HTMLEventObj *event_obj, VARIANT *retv)
888 DISPPARAMS dp = {NULL,NULL,0,0};
889 VARIANT event_arg;
890 ULONG argerr;
891 EXCEPINFO ei;
893 if(event_obj) {
894 V_VT(&event_arg) = VT_DISPATCH;
895 V_DISPATCH(&event_arg) = (IDispatch*)&event_obj->IHTMLEventObj_iface;
896 dp.rgvarg = &event_arg;
897 dp.cArgs = 1;
900 memset(&ei, 0, sizeof(ei));
901 return IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, retv, &ei, &argerr);
904 static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
906 int min, max, i;
907 HRESULT hres;
909 if(!data)
910 return FALSE;
912 if(!data->ids) {
913 hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
914 if(FAILED(hres))
915 return FALSE;
918 min = 0;
919 max = data->id_cnt-1;
920 while(min <= max) {
921 i = (min+max)/2;
922 if(data->ids[i] == dispid)
923 return TRUE;
925 if(data->ids[i] < dispid)
926 min = i+1;
927 else
928 max = i-1;
931 return FALSE;
934 static void call_event_handlers(HTMLDocumentNode *doc, HTMLEventObj *event_obj, event_target_t *event_target,
935 ConnectionPointContainer *cp_container, eventid_t eid, IDispatch *this_obj)
937 const BOOL cancelable = event_info[eid].flags & EVENT_CANCELABLE;
938 handler_vector_t *handler_vector = NULL;
939 VARIANT v;
940 HRESULT hres;
942 if(event_target)
943 handler_vector = event_target->event_table[eid];
945 if(handler_vector && handler_vector->handler_prop) {
946 DISPID named_arg = DISPID_THIS;
947 VARIANTARG arg;
948 DISPPARAMS dp = {&arg, &named_arg, 1, 1};
950 V_VT(&arg) = VT_DISPATCH;
951 V_DISPATCH(&arg) = this_obj;
952 V_VT(&v) = VT_EMPTY;
954 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
955 hres = call_disp_func(handler_vector->handler_prop, &dp, &v);
956 if(hres == S_OK) {
957 TRACE("%s <<< %s\n", debugstr_w(event_info[eid].name), debugstr_variant(&v));
959 if(cancelable) {
960 if(V_VT(&v) == VT_BOOL) {
961 if(!V_BOOL(&v))
962 event_obj->prevent_default = TRUE;
963 }else if(V_VT(&v) != VT_EMPTY) {
964 FIXME("unhandled result %s\n", debugstr_variant(&v));
967 VariantClear(&v);
968 }else {
969 WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
973 if(handler_vector && handler_vector->handler_cnt) {
974 VARIANTARG arg;
975 DISPPARAMS dp = {&arg, NULL, 1, 0};
976 int i;
978 V_VT(&arg) = VT_DISPATCH;
979 V_DISPATCH(&arg) = (IDispatch*)event_obj;
981 i = handler_vector->handler_cnt;
982 while(i--) {
983 if(handler_vector->handlers[i]) {
984 V_VT(&v) = VT_EMPTY;
986 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
987 hres = call_disp_func(handler_vector->handlers[i], &dp, &v);
988 if(hres == S_OK) {
989 TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
991 if(cancelable) {
992 if(V_VT(&v) == VT_BOOL) {
993 if(!V_BOOL(&v))
994 event_obj->prevent_default = TRUE;
995 }else if(V_VT(&v) != VT_EMPTY) {
996 FIXME("unhandled result %s\n", debugstr_variant(&v));
999 VariantClear(&v);
1000 }else {
1001 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
1008 * NOTE: CP events may require doc_obj reference, which we don't own. We make sure that
1009 * it's safe to call event handler by checking nsevent_listener, which is NULL for
1010 * detached documents.
1012 if(cp_container && cp_container->forward_container)
1013 cp_container = cp_container->forward_container;
1014 if(cp_container && cp_container->cps && doc->nsevent_listener) {
1015 ConnectionPoint *cp;
1016 unsigned i, j;
1018 for(j=0; cp_container->cp_entries[j].riid; j++) {
1019 cp = cp_container->cps + j;
1020 if(!cp->sinks_size || !is_cp_event(cp->data, event_info[eid].dispid))
1021 continue;
1023 for(i=0; doc->nsevent_listener && i < cp->sinks_size; i++) {
1024 if(!cp->sinks[i].disp)
1025 continue;
1027 V_VT(&v) = VT_EMPTY;
1029 TRACE("cp %s [%u] >>>\n", debugstr_w(event_info[eid].name), i);
1030 hres = call_cp_func(cp->sinks[i].disp, event_info[eid].dispid,
1031 cp->data->pass_event_arg ? event_obj : NULL, &v);
1032 if(hres == S_OK) {
1033 TRACE("cp %s [%u] <<<\n", debugstr_w(event_info[eid].name), i);
1035 if(cancelable) {
1036 if(V_VT(&v) == VT_BOOL) {
1037 if(!V_BOOL(&v))
1038 event_obj->prevent_default = TRUE;
1039 }else if(V_VT(&v) != VT_EMPTY) {
1040 FIXME("unhandled result %s\n", debugstr_variant(&v));
1043 VariantClear(&v);
1044 }else {
1045 WARN("cp %s [%u] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
1049 if(!doc->nsevent_listener)
1050 break;
1055 static void fire_event_obj(HTMLDocumentNode *doc, eventid_t eid, HTMLEventObj *event_obj,
1056 nsIDOMNode *target, IDispatch *script_this)
1058 IHTMLEventObj *prev_event;
1059 nsIDOMNode *parent, *nsnode;
1060 BOOL prevent_default = FALSE;
1061 HTMLInnerWindow *window;
1062 HTMLDOMNode *node;
1063 UINT16 node_type;
1064 nsresult nsres;
1065 HRESULT hres;
1067 TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
1069 window = doc->window;
1070 if(!window) {
1071 WARN("NULL window\n");
1072 return;
1075 htmldoc_addref(&doc->basedoc);
1077 prev_event = window->event;
1078 window->event = event_obj ? &event_obj->IHTMLEventObj_iface : NULL;
1080 nsIDOMNode_GetNodeType(target, &node_type);
1081 nsnode = target;
1082 nsIDOMNode_AddRef(nsnode);
1084 switch(node_type) {
1085 case ELEMENT_NODE:
1086 do {
1087 hres = get_node(doc, nsnode, FALSE, &node);
1088 if(SUCCEEDED(hres) && node) {
1089 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1090 node->cp_container, eid, script_this ? script_this : (IDispatch*)&node->IHTMLDOMNode_iface);
1091 node_release(node);
1094 if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1095 break;
1097 nsIDOMNode_GetParentNode(nsnode, &parent);
1098 nsIDOMNode_Release(nsnode);
1099 nsnode = parent;
1100 if(!nsnode)
1101 break;
1103 nsIDOMNode_GetNodeType(nsnode, &node_type);
1104 }while(node_type == ELEMENT_NODE);
1106 if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1107 break;
1109 case DOCUMENT_NODE:
1110 if(event_info[eid].flags & EVENT_FORWARDBODY) {
1111 nsIDOMHTMLElement *nsbody;
1112 nsresult nsres;
1114 nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
1115 if(NS_SUCCEEDED(nsres) && nsbody) {
1116 hres = get_node(doc, (nsIDOMNode*)nsbody, FALSE, &node);
1117 if(SUCCEEDED(hres) && node) {
1118 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1119 node->cp_container, eid, script_this ? script_this : (IDispatch*)&node->IHTMLDOMNode_iface);
1120 node_release(node);
1122 nsIDOMHTMLElement_Release(nsbody);
1123 }else {
1124 ERR("Could not get body: %08x\n", nsres);
1128 call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
1129 script_this ? script_this : (IDispatch*)&doc->basedoc.IHTMLDocument2_iface);
1130 break;
1132 default:
1133 FIXME("unimplemented node type %d\n", node_type);
1136 if(nsnode)
1137 nsIDOMNode_Release(nsnode);
1139 if(event_obj && event_obj->prevent_default)
1140 prevent_default = TRUE;
1141 window->event = prev_event;
1143 if(!prevent_default && (event_info[eid].flags & EVENT_HASDEFAULTHANDLERS)) {
1144 nsIDOMNode_AddRef(target);
1145 nsnode = target;
1147 do {
1148 hres = get_node(doc, nsnode, TRUE, &node);
1149 if(FAILED(hres))
1150 break;
1152 if(node) {
1153 if(node->vtbl->handle_event)
1154 hres = node->vtbl->handle_event(node, eid, event_obj ? event_obj->nsevent : NULL, &prevent_default);
1155 node_release(node);
1156 if(FAILED(hres) || prevent_default || (event_obj && event_obj->cancel_bubble))
1157 break;
1160 nsres = nsIDOMNode_GetParentNode(nsnode, &parent);
1161 if(NS_FAILED(nsres))
1162 break;
1164 nsIDOMNode_Release(nsnode);
1165 nsnode = parent;
1166 } while(nsnode);
1168 if(nsnode)
1169 nsIDOMNode_Release(nsnode);
1172 if(prevent_default && event_obj && event_obj->nsevent) {
1173 TRACE("calling PreventDefault\n");
1174 nsIDOMEvent_PreventDefault(event_obj->nsevent);
1177 htmldoc_release(&doc->basedoc);
1180 void fire_event(HTMLDocumentNode *doc, eventid_t eid, BOOL set_event, nsIDOMNode *target, nsIDOMEvent *nsevent,
1181 IDispatch *script_this)
1183 HTMLEventObj *event_obj = NULL;
1184 HTMLDOMNode *node;
1185 HRESULT hres;
1187 if(set_event) {
1188 hres = get_node(doc, target, TRUE, &node);
1189 if(FAILED(hres))
1190 return;
1192 event_obj = create_event();
1193 node_release(node);
1194 if(!event_obj)
1195 return;
1197 hres = set_event_info(event_obj, node, eid, nsevent);
1198 if(FAILED(hres)) {
1199 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1200 return;
1204 fire_event_obj(doc, eid, event_obj, target, script_this);
1206 if(event_obj)
1207 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1210 HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_var, VARIANT_BOOL *cancelled)
1212 HTMLEventObj *event_obj = NULL;
1213 eventid_t eid;
1214 HRESULT hres;
1216 eid = attr_to_eid(event_name);
1217 if(eid == EVENTID_LAST) {
1218 WARN("unknown event %s\n", debugstr_w(event_name));
1219 return E_INVALIDARG;
1222 if(event_var && V_VT(event_var) != VT_EMPTY && V_VT(event_var) != VT_ERROR) {
1223 if(V_VT(event_var) != VT_DISPATCH) {
1224 FIXME("event_var %s not supported\n", debugstr_variant(event_var));
1225 return E_NOTIMPL;
1228 if(V_DISPATCH(event_var)) {
1229 IHTMLEventObj *event_iface;
1231 hres = IDispatch_QueryInterface(V_DISPATCH(event_var), &IID_IHTMLEventObj, (void**)&event_iface);
1232 if(FAILED(hres)) {
1233 FIXME("No IHTMLEventObj iface\n");
1234 return hres;
1237 event_obj = unsafe_impl_from_IHTMLEventObj(event_iface);
1238 if(!event_obj) {
1239 ERR("Not our IHTMLEventObj?\n");
1240 IHTMLEventObj_Release(event_iface);
1241 return E_FAIL;
1246 if(event_obj) {
1247 hres = set_event_info(event_obj, node, eid, NULL);
1248 if(SUCCEEDED(hres))
1249 fire_event_obj(node->doc, eid, event_obj, node->nsnode, NULL);
1251 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1252 if(FAILED(hres))
1253 return hres;
1254 }else {
1255 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
1256 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1257 return E_NOTIMPL;
1260 fire_event(node->doc, eid, TRUE, node->nsnode, NULL, NULL);
1263 *cancelled = VARIANT_TRUE; /* FIXME */
1264 return S_OK;
1267 HRESULT call_fire_event(HTMLDOMNode *node, eventid_t eid)
1269 HRESULT hres;
1271 if(node->vtbl->fire_event) {
1272 BOOL handled = FALSE;
1274 hres = node->vtbl->fire_event(node, eid, &handled);
1275 if(handled)
1276 return hres;
1279 fire_event(node->doc, eid, TRUE, node->nsnode, NULL, NULL);
1280 return S_OK;
1283 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
1285 if(!*event_target_ptr)
1286 *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
1287 return *event_target_ptr;
1290 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
1292 handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
1294 if(handler_vector) {
1295 if(cnt <= handler_vector->handler_cnt)
1296 return TRUE;
1298 new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1299 }else {
1300 new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1303 if(!new_vector)
1304 return FALSE;
1306 new_vector->handler_cnt = cnt;
1307 event_target->event_table[eid] = new_vector;
1308 return TRUE;
1311 static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1313 if(!doc->nsdoc)
1314 return S_OK;
1316 if(event_info[eid].flags & EVENT_NODEHANDLER) {
1317 DWORD mask;
1319 mask = get_node_handler_mask(eid);
1320 if(event_target->node_handlers_mask & mask)
1321 return S_OK;
1323 add_nsevent_listener(doc, nsnode, event_info[eid].name);
1324 event_target->node_handlers_mask |= mask;
1325 return S_OK;
1328 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER))
1329 return S_OK;
1331 if(!doc->event_vector[eid]) {
1332 doc->event_vector[eid] = TRUE;
1333 add_nsevent_listener(doc, NULL, event_info[eid].name);
1336 return S_OK;
1339 void detach_events(HTMLDocumentNode *doc)
1341 if(doc->event_vector) {
1342 int i;
1344 for(i=0; i < EVENTID_LAST; i++) {
1345 if(doc->event_vector[i]) {
1346 detach_nsevent(doc, event_info[i].name);
1347 doc->event_vector[i] = FALSE;
1352 release_nsevents(doc);
1356 static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
1358 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1359 IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
1360 (*event_target)->event_table[eid]->handler_prop = NULL;
1363 return S_OK;
1366 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocumentNode *doc,
1367 eventid_t eid, IDispatch *disp)
1369 event_target_t *event_target;
1371 if(!disp)
1372 return remove_event_handler(event_target_ptr, eid);
1374 event_target = get_event_target(event_target_ptr);
1375 if(!event_target)
1376 return E_OUTOFMEMORY;
1378 if(!alloc_handler_vector(event_target, eid, 0))
1379 return E_OUTOFMEMORY;
1381 if(event_target->event_table[eid]->handler_prop)
1382 IDispatch_Release(event_target->event_table[eid]->handler_prop);
1384 event_target->event_table[eid]->handler_prop = disp;
1385 IDispatch_AddRef(disp);
1387 return ensure_nsevent_handler(doc, event_target, nsnode, eid);
1390 HRESULT set_event_handler(event_target_t **event_target, nsIDOMNode *nsnode, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1392 switch(V_VT(var)) {
1393 case VT_NULL:
1394 return remove_event_handler(event_target, eid);
1396 case VT_DISPATCH:
1397 return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1399 default:
1400 FIXME("not handler %s\n", debugstr_variant(var));
1401 /* fall through */
1402 case VT_EMPTY:
1403 return E_NOTIMPL;
1406 return S_OK;
1409 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
1411 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1412 V_VT(var) = VT_DISPATCH;
1413 V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1414 IDispatch_AddRef(V_DISPATCH(var));
1415 }else {
1416 V_VT(var) = VT_NULL;
1419 return S_OK;
1422 HRESULT attach_event(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocument *doc, BSTR name,
1423 IDispatch *disp, VARIANT_BOOL *res)
1425 event_target_t *event_target;
1426 eventid_t eid;
1427 DWORD i = 0;
1429 eid = attr_to_eid(name);
1430 if(eid == EVENTID_LAST) {
1431 WARN("Unknown event\n");
1432 *res = VARIANT_TRUE;
1433 return S_OK;
1436 event_target = get_event_target(event_target_ptr);
1437 if(!event_target)
1438 return E_OUTOFMEMORY;
1440 if(event_target->event_table[eid]) {
1441 while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
1442 i++;
1443 if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
1444 return E_OUTOFMEMORY;
1445 }else if(!alloc_handler_vector(event_target, eid, i+1)) {
1446 return E_OUTOFMEMORY;
1449 IDispatch_AddRef(disp);
1450 event_target->event_table[eid]->handlers[i] = disp;
1452 *res = VARIANT_TRUE;
1453 return ensure_nsevent_handler(doc->doc_node, event_target, nsnode, eid);
1456 HRESULT detach_event(event_target_t *event_target, HTMLDocument *doc, BSTR name, IDispatch *disp)
1458 eventid_t eid;
1459 DWORD i = 0;
1461 if(!event_target)
1462 return S_OK;
1464 eid = attr_to_eid(name);
1465 if(eid == EVENTID_LAST) {
1466 WARN("Unknown event\n");
1467 return S_OK;
1470 if(!event_target->event_table[eid])
1471 return S_OK;
1473 while(i < event_target->event_table[eid]->handler_cnt) {
1474 if(event_target->event_table[eid]->handlers[i] == disp) {
1475 IDispatch_Release(event_target->event_table[eid]->handlers[i]);
1476 event_target->event_table[eid]->handlers[i] = NULL;
1478 i++;
1481 return S_OK;
1484 void bind_elem_event(HTMLDocumentNode *doc, HTMLElement *elem, const WCHAR *event, IDispatch *disp)
1486 eventid_t eid;
1488 TRACE("(%p %p %s %p)\n", doc, elem, debugstr_w(event), disp);
1490 eid = attr_to_eid(event);
1491 if(eid == EVENTID_LAST) {
1492 WARN("Unsupported event %s\n", debugstr_w(event));
1493 return;
1496 set_event_handler_disp(&elem->node.event_target, elem->node.nsnode, doc, eid, disp);
1499 void update_cp_events(HTMLInnerWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp, nsIDOMNode *nsnode)
1501 event_target_t *event_target;
1502 int i;
1504 event_target = get_event_target(event_target_ptr);
1505 if(!event_target)
1506 return; /* FIXME */
1508 for(i=0; i < EVENTID_LAST; i++) {
1509 if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1510 ensure_nsevent_handler(window->doc, event_target, nsnode, i);
1514 void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
1516 const PRUnichar *attr_value;
1517 nsAString attr_name_str, attr_value_str;
1518 IDispatch *disp;
1519 HTMLDOMNode *node;
1520 int i;
1521 nsresult nsres;
1522 HRESULT hres;
1524 nsAString_Init(&attr_value_str, NULL);
1525 nsAString_Init(&attr_name_str, NULL);
1527 for(i=0; i < EVENTID_LAST; i++) {
1528 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
1529 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
1530 if(NS_SUCCEEDED(nsres)) {
1531 nsAString_GetData(&attr_value_str, &attr_value);
1532 if(!*attr_value)
1533 continue;
1535 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
1537 disp = script_parse_event(doc->window, attr_value);
1538 if(disp) {
1539 hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1540 if(SUCCEEDED(hres)) {
1541 set_event_handler_disp(get_node_event_target(node), node->nsnode, node->doc, i, disp);
1542 node_release(node);
1544 IDispatch_Release(disp);
1549 nsAString_Finish(&attr_value_str);
1550 nsAString_Finish(&attr_name_str);
1553 HRESULT doc_init_events(HTMLDocumentNode *doc)
1555 unsigned i;
1556 HRESULT hres;
1558 doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
1559 if(!doc->event_vector)
1560 return E_OUTOFMEMORY;
1562 init_nsevents(doc);
1564 for(i=0; i < EVENTID_LAST; i++) {
1565 if(event_info[i].flags & EVENT_HASDEFAULTHANDLERS) {
1566 hres = ensure_nsevent_handler(doc, NULL, NULL, i);
1567 if(FAILED(hres))
1568 return hres;
1572 return S_OK;
1575 void release_event_target(event_target_t *event_target)
1577 int i;
1578 unsigned int j;
1580 for(i=0; i < EVENTID_LAST; i++) {
1581 if(event_target->event_table[i]) {
1582 if(event_target->event_table[i]->handler_prop)
1583 IDispatch_Release(event_target->event_table[i]->handler_prop);
1584 for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
1585 if(event_target->event_table[i]->handlers[j])
1586 IDispatch_Release(event_target->event_table[i]->handlers[j]);
1590 heap_free(event_target);