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
29 #include "mshtml_private.h"
30 #include "htmlevent.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37 IDispatch *handler_prop;
39 IDispatch *handlers[0];
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};
130 static const WCHAR *event_types[] = {
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,
167 {dragstartW, ondragstartW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAGSTART,
169 {errorW, onerrorW, EVENTT_NONE, DISPID_EVMETH_ONERROR,
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,
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,
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,
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)
209 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
210 if(!strcmpW(event_info[i].name, str))
214 ERR("unknown type %s\n", debugstr_w(str));
218 static eventid_t attr_to_eid(LPCWSTR str)
222 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
223 if(!strcmpW(event_info[i].attr_name, str))
230 static DWORD get_node_handler_mask(eventid_t eid)
234 for(i=0; i<sizeof(node_handled_list)/sizeof(*node_handled_list); i++) {
235 if(node_handled_list[i] == eid)
239 ERR("Invalid eid %d\n", eid);
245 IHTMLEventObj IHTMLEventObj_iface;
250 const event_info_t *type;
251 nsIDOMEvent *nsevent;
252 BOOL prevent_default;
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);
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;
278 IUnknown_AddRef((IUnknown*)*ppv);
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);
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);
305 IHTMLDOMNode_Release(&This->target->IHTMLDOMNode_iface);
307 nsIDOMEvent_Release(This->nsevent);
308 release_dispex(&This->dispex);
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,
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,
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);
364 nsIDOMKeyEvent *key_event;
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);
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;
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);
394 nsIDOMKeyEvent *key_event;
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);
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;
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);
424 nsIDOMKeyEvent *key_event;
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);
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;
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;
458 This->prevent_default = TRUE;
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);
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;
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;
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);
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);
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);
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);
527 nsIDOMKeyEvent *key_event;
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);
541 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
543 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
546 TRACE("(%p)->(%p)\n", This, p);
549 nsIDOMMouseEvent *mouse_event;
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);
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);
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);
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);
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);
613 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
615 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
618 TRACE("(%p)->(%p)\n", This, p);
621 nsIDOMMouseEvent *mouse_event;
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);
635 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
637 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
640 TRACE("(%p)->(%p)\n", This, p);
643 nsIDOMMouseEvent *mouse_event;
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);
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);
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);
677 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
679 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
682 TRACE("(%p)->(%p)\n", This, p);
685 nsIDOMMouseEvent *mouse_event;
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);
699 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
701 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
704 TRACE("(%p)->(%p)\n", This, p);
707 nsIDOMMouseEvent *mouse_event;
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);
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);
731 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
732 HTMLEventObj_QueryInterface,
734 HTMLEventObj_Release,
735 HTMLEventObj_GetTypeInfoCount,
736 HTMLEventObj_GetTypeInfo,
737 HTMLEventObj_GetIDsOfNames,
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,
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[] = {
771 static dispex_static_data_t HTMLEventObj_dispex = {
775 HTMLEventObj_iface_tids
778 static HTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
782 ret = heap_alloc_zero(sizeof(*ret));
786 ret->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
788 ret->type = event_info+eid;
790 ret->nsevent = nsevent;
792 nsIDOMEvent_AddRef(nsevent);
793 }else if(event_types[event_info[eid].type]) {
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);
807 ret->target = target;
808 IHTMLDOMNode_AddRef(&target->IHTMLDOMNode_iface);
810 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLEventObj_iface, &HTMLEventObj_dispex);
815 static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
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);
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);
836 static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, VARIANT *retv)
838 DISPPARAMS dp = {NULL,NULL,0,0};
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)
855 hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
861 max = data->id_cnt-1;
864 if(data->ids[i] == dispid)
867 if(data->ids[i] < dispid)
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;
886 handler_vector = event_target->event_table[eid];
888 if(handler_vector && handler_vector->handler_prop) {
889 DISPID named_arg = DISPID_THIS;
891 DISPPARAMS dp = {&arg, &named_arg, 1, 1};
893 V_VT(&arg) = VT_DISPATCH;
894 V_DISPATCH(&arg) = this_obj;
897 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
898 hres = call_disp_func(handler_vector->handler_prop, &dp, &v);
900 TRACE("%s <<< %s\n", debugstr_w(event_info[eid].name), debugstr_variant(&v));
903 if(V_VT(&v) == VT_BOOL) {
905 event_obj->prevent_default = TRUE;
906 }else if(V_VT(&v) != VT_EMPTY) {
907 FIXME("unhandled result %s\n", debugstr_variant(&v));
912 WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
916 if(handler_vector && handler_vector->handler_cnt) {
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;
925 if(handler_vector->handlers[i]) {
928 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
929 hres = call_disp_func(handler_vector->handlers[i], &dp, &v);
931 TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
934 if(V_VT(&v) == VT_BOOL) {
936 event_obj->prevent_default = TRUE;
937 }else if(V_VT(&v) != VT_EMPTY) {
938 FIXME("unhandled result %s\n", debugstr_variant(&v));
943 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
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)
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);
966 TRACE("cp %s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
969 if(V_VT(&v) == VT_BOOL) {
971 event_obj->prevent_default = TRUE;
972 }else if(V_VT(&v) != VT_EMPTY) {
973 FIXME("unhandled result %s\n", debugstr_variant(&v));
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 HTMLInnerWindow *window;
998 TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
1000 window = doc->window;
1001 prev_event = window->event;
1003 hres = get_node(doc, target, TRUE, &node);
1007 event_obj = create_event(node, eid, nsevent);
1009 window->event = &event_obj->IHTMLEventObj_iface;
1011 window->event = NULL;
1014 nsIDOMNode_GetNodeType(target, &node_type);
1016 nsIDOMNode_AddRef(nsnode);
1021 hres = get_node(doc, nsnode, FALSE, &node);
1022 if(SUCCEEDED(hres) && node) {
1023 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1024 node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1028 if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1031 nsIDOMNode_GetParentNode(nsnode, &parent);
1032 nsIDOMNode_Release(nsnode);
1037 nsIDOMNode_GetNodeType(nsnode, &node_type);
1038 }while(node_type == ELEMENT_NODE);
1040 if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1044 if(event_info[eid].flags & EVENT_FORWARDBODY) {
1045 nsIDOMHTMLElement *nsbody;
1048 nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
1049 if(NS_SUCCEEDED(nsres) && nsbody) {
1050 hres = get_node(doc, (nsIDOMNode*)nsbody, FALSE, &node);
1051 if(SUCCEEDED(hres) && node) {
1052 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1053 node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1056 nsIDOMHTMLElement_Release(nsbody);
1058 ERR("Could not get body: %08x\n", nsres);
1062 call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
1063 (IDispatch*)&doc->basedoc.IHTMLDocument2_iface);
1067 FIXME("unimplemented node type %d\n", node_type);
1071 nsIDOMNode_Release(nsnode);
1073 if(event_obj && event_obj->prevent_default)
1074 prevent_default = TRUE;
1075 window->event = prev_event;
1077 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1079 if(!prevent_default && (event_info[eid].flags & EVENT_HASDEFAULTHANDLERS)) {
1080 nsIDOMNode_AddRef(target);
1084 hres = get_node(doc, nsnode, TRUE, &node);
1089 if(node->vtbl->handle_event)
1090 hres = node->vtbl->handle_event(node, eid, nsevent, &prevent_default);
1092 if(FAILED(hres) || prevent_default || (event_obj && event_obj->cancel_bubble))
1096 nsres = nsIDOMNode_GetParentNode(nsnode, &parent);
1097 if(NS_FAILED(nsres))
1100 nsIDOMNode_Release(nsnode);
1105 nsIDOMNode_Release(nsnode);
1108 if(prevent_default && nsevent) {
1109 TRACE("calling PreventDefault\n");
1110 nsIDOMEvent_PreventDefault(nsevent);
1114 HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_obj, VARIANT_BOOL *cancelled)
1118 eid = attr_to_eid(event_name);
1119 if(eid == EVENTID_LAST) {
1120 WARN("unknown event %s\n", debugstr_w(event_name));
1121 return E_INVALIDARG;
1124 if(event_obj && V_VT(event_obj) != VT_EMPTY && V_VT(event_obj) != VT_ERROR)
1125 FIXME("event_obj not implemented\n");
1127 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
1128 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1132 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1134 *cancelled = VARIANT_TRUE; /* FIXME */
1138 HRESULT call_fire_event(HTMLDOMNode *node, eventid_t eid)
1142 if(node->vtbl->fire_event) {
1143 BOOL handled = FALSE;
1145 hres = node->vtbl->fire_event(node, eid, &handled);
1150 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1154 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
1156 if(!*event_target_ptr)
1157 *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
1158 return *event_target_ptr;
1161 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
1163 handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
1165 if(handler_vector) {
1166 if(cnt <= handler_vector->handler_cnt)
1169 new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1171 new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1177 new_vector->handler_cnt = cnt;
1178 event_target->event_table[eid] = new_vector;
1182 static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1187 if(event_info[eid].flags & EVENT_NODEHANDLER) {
1190 mask = get_node_handler_mask(eid);
1191 if(event_target->node_handlers_mask & mask)
1194 add_nsevent_listener(doc, nsnode, event_info[eid].name);
1195 event_target->node_handlers_mask |= mask;
1199 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER))
1202 if(!doc->event_vector[eid]) {
1203 doc->event_vector[eid] = TRUE;
1204 add_nsevent_listener(doc, NULL, event_info[eid].name);
1210 void detach_events(HTMLDocumentNode *doc)
1212 if(doc->event_vector) {
1215 for(i=0; i < EVENTID_LAST; i++) {
1216 if(doc->event_vector[i])
1217 detach_nsevent(doc, event_info[i].name);
1221 release_nsevents(doc);
1225 static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
1227 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1228 IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
1229 (*event_target)->event_table[eid]->handler_prop = NULL;
1235 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocumentNode *doc,
1236 eventid_t eid, IDispatch *disp)
1238 event_target_t *event_target;
1241 return remove_event_handler(event_target_ptr, eid);
1243 event_target = get_event_target(event_target_ptr);
1245 return E_OUTOFMEMORY;
1247 if(!alloc_handler_vector(event_target, eid, 0))
1248 return E_OUTOFMEMORY;
1250 if(event_target->event_table[eid]->handler_prop)
1251 IDispatch_Release(event_target->event_table[eid]->handler_prop);
1253 event_target->event_table[eid]->handler_prop = disp;
1254 IDispatch_AddRef(disp);
1256 return ensure_nsevent_handler(doc, event_target, nsnode, eid);
1259 HRESULT set_event_handler(event_target_t **event_target, nsIDOMNode *nsnode, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1263 return remove_event_handler(event_target, eid);
1266 return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1269 FIXME("not handler %s\n", debugstr_variant(var));
1278 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
1280 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1281 V_VT(var) = VT_DISPATCH;
1282 V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1283 IDispatch_AddRef(V_DISPATCH(var));
1285 V_VT(var) = VT_NULL;
1291 HRESULT attach_event(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocument *doc, BSTR name,
1292 IDispatch *disp, VARIANT_BOOL *res)
1294 event_target_t *event_target;
1298 eid = attr_to_eid(name);
1299 if(eid == EVENTID_LAST) {
1300 WARN("Unknown event\n");
1301 *res = VARIANT_TRUE;
1305 event_target = get_event_target(event_target_ptr);
1307 return E_OUTOFMEMORY;
1309 if(event_target->event_table[eid]) {
1310 while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
1312 if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
1313 return E_OUTOFMEMORY;
1314 }else if(!alloc_handler_vector(event_target, eid, i+1)) {
1315 return E_OUTOFMEMORY;
1318 IDispatch_AddRef(disp);
1319 event_target->event_table[eid]->handlers[i] = disp;
1321 *res = VARIANT_TRUE;
1322 return ensure_nsevent_handler(doc->doc_node, event_target, nsnode, eid);
1325 HRESULT detach_event(event_target_t *event_target, HTMLDocument *doc, BSTR name, IDispatch *disp)
1333 eid = attr_to_eid(name);
1334 if(eid == EVENTID_LAST) {
1335 WARN("Unknown event\n");
1339 if(!event_target->event_table[eid])
1342 while(i < event_target->event_table[eid]->handler_cnt) {
1343 if(event_target->event_table[eid]->handlers[i] == disp) {
1344 IDispatch_Release(event_target->event_table[eid]->handlers[i]);
1345 event_target->event_table[eid]->handlers[i] = NULL;
1353 void update_cp_events(HTMLInnerWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp, nsIDOMNode *nsnode)
1355 event_target_t *event_target;
1358 event_target = get_event_target(event_target_ptr);
1362 for(i=0; i < EVENTID_LAST; i++) {
1363 if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1364 ensure_nsevent_handler(window->doc, event_target, nsnode, i);
1368 void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
1370 const PRUnichar *attr_value;
1371 nsAString attr_name_str, attr_value_str;
1378 nsAString_Init(&attr_value_str, NULL);
1379 nsAString_Init(&attr_name_str, NULL);
1381 for(i=0; i < EVENTID_LAST; i++) {
1382 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
1383 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
1384 if(NS_SUCCEEDED(nsres)) {
1385 nsAString_GetData(&attr_value_str, &attr_value);
1389 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
1391 disp = script_parse_event(doc->window, attr_value);
1393 hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1394 if(SUCCEEDED(hres)) {
1395 set_event_handler_disp(get_node_event_target(node), node->nsnode, node->doc, i, disp);
1398 IDispatch_Release(disp);
1403 nsAString_Finish(&attr_value_str);
1404 nsAString_Finish(&attr_name_str);
1407 HRESULT doc_init_events(HTMLDocumentNode *doc)
1412 doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
1413 if(!doc->event_vector)
1414 return E_OUTOFMEMORY;
1418 for(i=0; i < EVENTID_LAST; i++) {
1419 if(event_info[i].flags & EVENT_HASDEFAULTHANDLERS) {
1420 hres = ensure_nsevent_handler(doc, NULL, NULL, i);
1429 void release_event_target(event_target_t *event_target)
1433 for(i=0; i < EVENTID_LAST; i++) {
1434 if(event_target->event_table[i]) {
1435 if(event_target->event_table[i]->handler_prop)
1436 IDispatch_Release(event_target->event_table[i]->handler_prop);
1437 for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
1438 if(event_target->event_table[i]->handlers[j])
1439 IDispatch_Release(event_target->event_table[i]->handlers[j]);
1443 heap_free(event_target);