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 dblclickW[] = {'d','b','l','c','l','i','c','k',0};
60 static const WCHAR ondblclickW[] = {'o','n','d','b','l','c','l','i','c','k',0};
62 static const WCHAR dragW[] = {'d','r','a','g',0};
63 static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};
65 static const WCHAR dragstartW[] = {'d','r','a','g','s','t','a','r','t',0};
66 static const WCHAR ondragstartW[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
68 static const WCHAR focusW[] = {'f','o','c','u','s',0};
69 static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};
71 static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
72 static const WCHAR onkeydownW[] = {'o','n','k','e','y','d','o','w','n',0};
74 static const WCHAR keyupW[] = {'k','e','y','u','p',0};
75 static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};
77 static const WCHAR loadW[] = {'l','o','a','d',0};
78 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
80 static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
81 static const WCHAR onmousedownW[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
83 static const WCHAR mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
84 static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};
86 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
87 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
89 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
90 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
92 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
93 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
95 static const WCHAR readystatechangeW[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
96 static const WCHAR onreadystatechangeW[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
98 static const WCHAR resizeW[] = {'r','e','s','i','z','e',0};
99 static const WCHAR onresizeW[] = {'o','n','r','e','s','i','z','e',0};
101 static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
102 static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
104 static const WCHAR HTMLEventsW[] = {'H','T','M','L','E','v','e','n','t','s',0};
105 static const WCHAR KeyboardEventW[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
106 static const WCHAR MouseEventW[] = {'M','o','u','s','e','E','v','e','n','t',0};
115 static const WCHAR *event_types[] = {
130 #define EVENT_DEFAULTLISTENER 0x0001
131 #define EVENT_BUBBLE 0x0002
132 #define EVENT_FORWARDBODY 0x0004
133 #define EVENT_NODEHANDLER 0x0008
135 static const event_info_t event_info[] = {
136 {beforeunloadW, onbeforeunloadW, EVENTT_NONE, DISPID_EVMETH_ONBEFOREUNLOAD,
137 EVENT_DEFAULTLISTENER|EVENT_FORWARDBODY},
138 {blurW, onblurW, EVENTT_HTML, DISPID_EVMETH_ONBLUR,
139 EVENT_DEFAULTLISTENER},
140 {changeW, onchangeW, EVENTT_HTML, DISPID_EVMETH_ONCHANGE,
141 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
142 {clickW, onclickW, EVENTT_MOUSE, DISPID_EVMETH_ONCLICK,
143 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
144 {dblclickW, ondblclickW, EVENTT_MOUSE, DISPID_EVMETH_ONDBLCLICK,
145 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
146 {dragW, ondragW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAG,
148 {dragstartW, ondragstartW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAGSTART,
150 {focusW, onfocusW, EVENTT_HTML, DISPID_EVMETH_ONFOCUS,
151 EVENT_DEFAULTLISTENER},
152 {keydownW, onkeydownW, EVENTT_KEY, DISPID_EVMETH_ONKEYDOWN,
153 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
154 {keyupW, onkeyupW, EVENTT_KEY, DISPID_EVMETH_ONKEYUP,
155 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
156 {loadW, onloadW, EVENTT_HTML, DISPID_EVMETH_ONLOAD,
158 {mousedownW, onmousedownW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEDOWN,
159 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
160 {mouseoutW, onmouseoutW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEOUT,
161 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
162 {mouseoverW, onmouseoverW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEOVER,
163 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
164 {mouseupW, onmouseupW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEUP,
165 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
166 {pasteW, onpasteW, EVENTT_NONE, DISPID_EVMETH_ONPASTE,
168 {readystatechangeW, onreadystatechangeW, EVENTT_NONE, DISPID_EVMETH_ONREADYSTATECHANGE,
170 {resizeW, onresizeW, EVENTT_NONE, DISPID_EVMETH_ONRESIZE,
171 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
172 {selectstartW, onselectstartW, EVENTT_MOUSE, DISPID_EVMETH_ONSELECTSTART,
176 static const eventid_t node_handled_list[] = { EVENTID_LOAD };
178 eventid_t str_to_eid(LPCWSTR str)
182 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
183 if(!strcmpW(event_info[i].name, str))
187 ERR("unknown type %s\n", debugstr_w(str));
191 static eventid_t attr_to_eid(LPCWSTR str)
195 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
196 if(!strcmpW(event_info[i].attr_name, str))
203 static DWORD get_node_handler_mask(eventid_t eid)
207 for(i=0; i<sizeof(node_handled_list)/sizeof(*node_handled_list); i++) {
208 if(node_handled_list[i] == eid)
212 ERR("Invalid eid %d\n", eid);
218 const IHTMLEventObjVtbl *lpIHTMLEventObjVtbl;
223 const event_info_t *type;
224 nsIDOMEvent *nsevent;
227 #define HTMLEVENTOBJ(x) ((IHTMLEventObj*) &(x)->lpIHTMLEventObjVtbl)
229 #define HTMLEVENTOBJ_THIS(iface) DEFINE_THIS(HTMLEventObj, IHTMLEventObj, iface)
231 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
233 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
237 if(IsEqualGUID(&IID_IUnknown, riid)) {
238 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
239 *ppv = HTMLEVENTOBJ(This);
240 }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
241 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
242 *ppv = HTMLEVENTOBJ(This);
243 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
244 return *ppv ? S_OK : E_NOINTERFACE;
248 IUnknown_AddRef((IUnknown*)*ppv);
252 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
253 return E_NOINTERFACE;
256 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
258 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
259 LONG ref = InterlockedIncrement(&This->ref);
261 TRACE("(%p) ref=%d\n", This, ref);
266 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
268 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
269 LONG ref = InterlockedDecrement(&This->ref);
271 TRACE("(%p) ref=%d\n", This, ref);
275 nsIDOMEvent_Release(This->nsevent);
276 release_dispex(&This->dispex);
283 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
285 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
286 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
289 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
290 LCID lcid, ITypeInfo **ppTInfo)
292 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
293 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
296 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
297 LPOLESTR *rgszNames, UINT cNames,
298 LCID lcid, DISPID *rgDispId)
300 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
301 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
304 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
305 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
306 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
308 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
309 return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid,
310 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
313 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
315 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
317 TRACE("(%p)->(%p)\n", This, p);
319 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(This->target), &IID_IHTMLElement, (void**)p);
322 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
324 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
327 TRACE("(%p)->(%p)\n", This, p);
330 nsIDOMKeyEvent *key_event;
333 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
334 if(NS_SUCCEEDED(nsres)) {
335 nsIDOMKeyEvent_GetAltKey(key_event, &ret);
336 nsIDOMKeyEvent_Release(key_event);
338 nsIDOMMouseEvent *mouse_event;
340 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
341 if(NS_SUCCEEDED(nsres)) {
342 nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
343 nsIDOMMouseEvent_Release(mouse_event);
348 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
352 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
354 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
357 TRACE("(%p)->(%p)\n", This, p);
360 nsIDOMKeyEvent *key_event;
363 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
364 if(NS_SUCCEEDED(nsres)) {
365 nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
366 nsIDOMKeyEvent_Release(key_event);
368 nsIDOMMouseEvent *mouse_event;
370 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
371 if(NS_SUCCEEDED(nsres)) {
372 nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
373 nsIDOMMouseEvent_Release(mouse_event);
378 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
382 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
384 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
387 TRACE("(%p)->(%p)\n", This, p);
390 nsIDOMKeyEvent *key_event;
393 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
394 if(NS_SUCCEEDED(nsres)) {
395 nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
396 nsIDOMKeyEvent_Release(key_event);
398 nsIDOMMouseEvent *mouse_event;
400 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
401 if(NS_SUCCEEDED(nsres)) {
402 nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
403 nsIDOMMouseEvent_Release(mouse_event);
408 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
412 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
414 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
415 FIXME("(%p)->()\n", This);
419 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
421 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
423 FIXME("(%p)->(%p)\n", This, p);
429 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
431 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
432 FIXME("(%p)->(%x)\n", This, v);
436 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
438 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
440 FIXME("(%p)->(%p)\n", This, p);
446 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
448 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
450 FIXME("(%p)->(%p)\n", This, p);
456 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
458 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
460 FIXME("(%p)->(%p)\n", This, p);
466 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
468 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
469 FIXME("(%p)->(%d)\n", This, v);
473 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
475 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
476 PRUint32 key_code = 0;
478 TRACE("(%p)->(%p)\n", This, p);
481 nsIDOMKeyEvent *key_event;
484 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
485 if(NS_SUCCEEDED(nsres)) {
486 nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
487 nsIDOMKeyEvent_Release(key_event);
495 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
497 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
500 TRACE("(%p)->(%p)\n", This, p);
503 nsIDOMMouseEvent *mouse_event;
506 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
507 if(NS_SUCCEEDED(nsres)) {
508 nsIDOMMouseEvent_GetButton(mouse_event, &button);
509 nsIDOMMouseEvent_Release(mouse_event);
517 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
519 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
521 TRACE("(%p)->(%p)\n", This, p);
523 *p = SysAllocString(This->type->name);
524 return *p ? S_OK : E_OUTOFMEMORY;
527 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
529 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
531 FIXME("(%p)->(%p)\n", This, p);
537 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
539 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
541 FIXME("(%p)->(%p)\n", This, p);
547 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
549 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
551 FIXME("(%p)->(%p)\n", This, p);
557 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
559 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
561 FIXME("(%p)->(%p)\n", This, p);
567 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
569 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
572 TRACE("(%p)->(%p)\n", This, p);
575 nsIDOMMouseEvent *mouse_event;
578 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
579 if(NS_SUCCEEDED(nsres)) {
580 nsIDOMMouseEvent_GetClientX(mouse_event, &x);
581 nsIDOMMouseEvent_Release(mouse_event);
589 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
591 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
594 TRACE("(%p)->(%p)\n", This, p);
597 nsIDOMMouseEvent *mouse_event;
600 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
601 if(NS_SUCCEEDED(nsres)) {
602 nsIDOMMouseEvent_GetClientY(mouse_event, &y);
603 nsIDOMMouseEvent_Release(mouse_event);
611 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
613 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
615 FIXME("(%p)->(%p)\n", This, p);
621 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
623 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
625 FIXME("(%p)->(%p)\n", This, p);
631 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
633 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
636 TRACE("(%p)->(%p)\n", This, p);
639 nsIDOMMouseEvent *mouse_event;
642 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
643 if(NS_SUCCEEDED(nsres)) {
644 nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
645 nsIDOMMouseEvent_Release(mouse_event);
653 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
655 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
658 TRACE("(%p)->(%p)\n", This, p);
661 nsIDOMMouseEvent *mouse_event;
664 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
665 if(NS_SUCCEEDED(nsres)) {
666 nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
667 nsIDOMMouseEvent_Release(mouse_event);
675 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
677 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
679 FIXME("(%p)->(%p)\n", This, p);
685 #undef HTMLEVENTOBJ_THIS
687 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
688 HTMLEventObj_QueryInterface,
690 HTMLEventObj_Release,
691 HTMLEventObj_GetTypeInfoCount,
692 HTMLEventObj_GetTypeInfo,
693 HTMLEventObj_GetIDsOfNames,
695 HTMLEventObj_get_srcElement,
696 HTMLEventObj_get_altKey,
697 HTMLEventObj_get_ctrlKey,
698 HTMLEventObj_get_shiftKey,
699 HTMLEventObj_put_returnValue,
700 HTMLEventObj_get_returnValue,
701 HTMLEventObj_put_cancelBubble,
702 HTMLEventObj_get_cancelBubble,
703 HTMLEventObj_get_fromElement,
704 HTMLEventObj_get_toElement,
705 HTMLEventObj_put_keyCode,
706 HTMLEventObj_get_keyCode,
707 HTMLEventObj_get_button,
708 HTMLEventObj_get_type,
709 HTMLEventObj_get_qualifier,
710 HTMLEventObj_get_reason,
713 HTMLEventObj_get_clientX,
714 HTMLEventObj_get_clientY,
715 HTMLEventObj_get_offsetX,
716 HTMLEventObj_get_offsetY,
717 HTMLEventObj_get_screenX,
718 HTMLEventObj_get_screenY,
719 HTMLEventObj_get_srcFilter
722 static const tid_t HTMLEventObj_iface_tids[] = {
727 static dispex_static_data_t HTMLEventObj_dispex = {
731 HTMLEventObj_iface_tids
734 static IHTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
738 ret = heap_alloc(sizeof(*ret));
742 ret->lpIHTMLEventObjVtbl = &HTMLEventObjVtbl;
744 ret->type = event_info+eid;
746 ret->nsevent = nsevent;
748 nsIDOMEvent_AddRef(nsevent);
749 }else if(event_types[event_info[eid].type]) {
750 nsIDOMDocumentEvent *doc_event;
753 nsres = nsIDOMHTMLDocument_QueryInterface(target->doc->nsdoc, &IID_nsIDOMDocumentEvent,
755 if(NS_SUCCEEDED(nsres)) {
758 nsAString_InitDepend(&type_str, event_types[event_info[eid].type]);
759 nsres = nsIDOMDocumentEvent_CreateEvent(doc_event, &type_str, &ret->nsevent);
760 nsAString_Finish(&type_str);
761 nsIDOMDocumentEvent_Release(doc_event);
763 if(NS_FAILED(nsres)) {
764 ERR("Could not create event: %08x\n", nsres);
765 IHTMLEventObj_Release(HTMLEVENTOBJ(ret));
770 ret->target = target;
771 IHTMLDOMNode_AddRef(HTMLDOMNODE(target));
773 init_dispex(&ret->dispex, (IUnknown*)HTMLEVENTOBJ(ret), &HTMLEventObj_dispex);
775 return HTMLEVENTOBJ(ret);
778 static HRESULT call_cp_func(IDispatch *disp, DISPID dispid)
780 DISPPARAMS dp = {NULL,NULL,0,0};
786 V_VT(&vres) = VT_EMPTY;
787 memset(&ei, 0, sizeof(ei));
788 hres = IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, &vres, &ei, &argerr);
789 if(SUCCEEDED(hres) && V_VT(&vres) != VT_EMPTY) {
790 FIXME("handle result %s\n", debugstr_variant(&vres));
797 static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
806 hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
812 max = data->id_cnt-1;
815 if(data->ids[i] == dispid)
818 if(data->ids[i] < dispid)
827 static void call_event_handlers(HTMLDocumentNode *doc, IHTMLEventObj *event_obj, event_target_t *event_target,
828 ConnectionPointContainer *cp_container, eventid_t eid, IDispatch *this_obj)
830 handler_vector_t *handler_vector = NULL;
835 handler_vector = event_target->event_table[eid];
837 if(handler_vector && handler_vector->handler_prop) {
838 DISPID named_arg = DISPID_THIS;
840 DISPPARAMS dp = {&arg, &named_arg, 1, 1};
842 V_VT(&arg) = VT_DISPATCH;
843 V_DISPATCH(&arg) = this_obj;
845 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
846 hres = call_disp_func(handler_vector->handler_prop, &dp);
848 TRACE("%s <<<\n", debugstr_w(event_info[eid].name));
850 WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
853 if(handler_vector && handler_vector->handler_cnt) {
855 DISPPARAMS dp = {&arg, NULL, 1, 0};
857 V_VT(&arg) = VT_DISPATCH;
858 V_DISPATCH(&arg) = (IDispatch*)event_obj;
860 i = handler_vector->handler_cnt;
862 if(handler_vector->handlers[i]) {
863 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
864 hres = call_disp_func(handler_vector->handlers[i], &dp);
866 TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
868 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
876 if(cp_container->forward_container)
877 cp_container = cp_container->forward_container;
879 for(cp = cp_container->cp_list; cp; cp = cp->next) {
880 if(cp->sinks_size && is_cp_event(cp->data, event_info[eid].dispid)) {
881 for(i=0; i < cp->sinks_size; i++) {
882 if(!cp->sinks[i].disp)
885 TRACE("cp %s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
886 hres = call_cp_func(cp->sinks[i].disp, event_info[eid].dispid);
888 TRACE("cp %s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
890 WARN("cp %s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
897 void fire_event(HTMLDocumentNode *doc, eventid_t eid, BOOL set_event, nsIDOMNode *target, nsIDOMEvent *nsevent)
899 IHTMLEventObj *prev_event, *event_obj = NULL;
900 nsIDOMNode *parent, *nsnode;
904 TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
906 prev_event = doc->basedoc.window->event;
908 event_obj = create_event(get_node(doc, target, TRUE), eid, nsevent);
909 doc->basedoc.window->event = event_obj;
911 nsIDOMNode_GetNodeType(target, &node_type);
913 nsIDOMNode_AddRef(nsnode);
918 node = get_node(doc, nsnode, FALSE);
920 call_event_handlers(doc, event_obj, *get_node_event_target(node), node->cp_container, eid,
921 (IDispatch*)HTMLDOMNODE(node));
923 if(!(event_info[eid].flags & EVENT_BUBBLE))
926 nsIDOMNode_GetParentNode(nsnode, &parent);
927 nsIDOMNode_Release(nsnode);
932 nsIDOMNode_GetNodeType(nsnode, &node_type);
933 }while(node_type == ELEMENT_NODE);
935 if(!(event_info[eid].flags & EVENT_BUBBLE))
939 if(event_info[eid].flags & EVENT_FORWARDBODY) {
940 nsIDOMHTMLElement *nsbody;
943 nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
944 if(NS_SUCCEEDED(nsres) && nsbody) {
945 node = get_node(doc, (nsIDOMNode*)nsbody, FALSE);
947 call_event_handlers(doc, event_obj, *get_node_event_target(node), node->cp_container,
948 eid, (IDispatch*)HTMLDOMNODE(node));
949 nsIDOMHTMLElement_Release(nsbody);
951 ERR("Could not get body: %08x\n", nsres);
955 call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
956 (IDispatch*)HTMLDOC(&doc->basedoc));
960 FIXME("unimplemented node type %d\n", node_type);
964 nsIDOMNode_Release(nsnode);
967 IHTMLEventObj_Release(event_obj);
968 doc->basedoc.window->event = prev_event;
971 HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_obj, VARIANT_BOOL *cancelled)
975 eid = attr_to_eid(event_name);
976 if(eid == EVENTID_LAST) {
977 WARN("unknown event %s\n", debugstr_w(event_name));
981 if(event_obj && V_VT(event_obj) != VT_EMPTY && V_VT(event_obj) != VT_ERROR)
982 FIXME("event_obj not implemented\n");
984 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
985 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
989 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
991 *cancelled = VARIANT_TRUE; /* FIXME */
995 HRESULT call_event(HTMLDOMNode *node, eventid_t eid)
999 if(node->vtbl->call_event) {
1000 BOOL handled = FALSE;
1002 hres = node->vtbl->call_event(node, eid, &handled);
1007 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1011 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
1013 if(!*event_target_ptr)
1014 *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
1015 return *event_target_ptr;
1018 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
1020 handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
1022 if(handler_vector) {
1023 if(cnt <= handler_vector->handler_cnt)
1026 new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1028 new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1034 new_vector->handler_cnt = cnt;
1035 event_target->event_table[eid] = new_vector;
1039 static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1044 if(event_info[eid].flags & EVENT_NODEHANDLER) {
1047 mask = get_node_handler_mask(eid);
1048 if(event_target->node_handlers_mask & mask)
1051 add_nsevent_listener(doc, nsnode, event_info[eid].name);
1052 event_target->node_handlers_mask |= mask;
1056 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER))
1059 if(!doc->event_vector) {
1060 doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
1061 if(!doc->event_vector)
1062 return E_OUTOFMEMORY;
1065 if(!doc->event_vector[eid]) {
1066 doc->event_vector[eid] = TRUE;
1067 add_nsevent_listener(doc, NULL, event_info[eid].name);
1073 static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
1075 if(*event_target && (*event_target)->event_table[eid]->handler_prop) {
1076 IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
1077 (*event_target)->event_table[eid]->handler_prop = NULL;
1083 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocumentNode *doc,
1084 eventid_t eid, IDispatch *disp)
1086 event_target_t *event_target;
1089 return remove_event_handler(event_target_ptr, eid);
1091 event_target = get_event_target(event_target_ptr);
1093 return E_OUTOFMEMORY;
1095 if(!alloc_handler_vector(event_target, eid, 0))
1096 return E_OUTOFMEMORY;
1098 if(event_target->event_table[eid]->handler_prop)
1099 IDispatch_Release(event_target->event_table[eid]->handler_prop);
1101 event_target->event_table[eid]->handler_prop = disp;
1102 IDispatch_AddRef(disp);
1104 return ensure_nsevent_handler(doc, event_target, nsnode, eid);
1107 HRESULT set_event_handler(event_target_t **event_target, nsIDOMNode *nsnode, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1111 return remove_event_handler(event_target, eid);
1114 return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1117 FIXME("not supported vt=%d\n", V_VT(var));
1125 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
1127 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1128 V_VT(var) = VT_DISPATCH;
1129 V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1130 IDispatch_AddRef(V_DISPATCH(var));
1132 V_VT(var) = VT_NULL;
1138 HRESULT attach_event(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocument *doc, BSTR name,
1139 IDispatch *disp, VARIANT_BOOL *res)
1141 event_target_t *event_target;
1145 eid = attr_to_eid(name);
1146 if(eid == EVENTID_LAST) {
1147 WARN("Unknown event\n");
1148 *res = VARIANT_TRUE;
1152 event_target = get_event_target(event_target_ptr);
1154 return E_OUTOFMEMORY;
1156 if(event_target->event_table[eid]) {
1157 while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
1159 if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
1160 return E_OUTOFMEMORY;
1161 }else if(!alloc_handler_vector(event_target, eid, i+1)) {
1162 return E_OUTOFMEMORY;
1165 IDispatch_AddRef(disp);
1166 event_target->event_table[eid]->handlers[i] = disp;
1168 *res = VARIANT_TRUE;
1169 return ensure_nsevent_handler(doc->doc_node, event_target, nsnode, eid);
1172 HRESULT detach_event(event_target_t *event_target, HTMLDocument *doc, BSTR name, IDispatch *disp)
1180 eid = attr_to_eid(name);
1181 if(eid == EVENTID_LAST) {
1182 WARN("Unknown event\n");
1186 if(!event_target->event_table[eid])
1189 while(i < event_target->event_table[eid]->handler_cnt) {
1190 if(event_target->event_table[eid]->handlers[i] == disp) {
1191 IDispatch_Release(event_target->event_table[eid]->handlers[i]);
1192 event_target->event_table[eid]->handlers[i] = NULL;
1200 void update_cp_events(HTMLWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp, nsIDOMNode *nsnode)
1202 event_target_t *event_target;
1205 event_target = get_event_target(event_target_ptr);
1209 for(i=0; i < EVENTID_LAST; i++) {
1210 if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1211 ensure_nsevent_handler(window->doc, event_target, nsnode, i);
1215 void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
1217 const PRUnichar *attr_value;
1218 nsAString attr_name_str, attr_value_str;
1224 nsAString_Init(&attr_value_str, NULL);
1225 nsAString_Init(&attr_name_str, NULL);
1227 for(i=0; i < EVENTID_LAST; i++) {
1228 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
1229 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
1230 if(NS_SUCCEEDED(nsres)) {
1231 nsAString_GetData(&attr_value_str, &attr_value);
1235 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
1237 disp = script_parse_event(doc->basedoc.window, attr_value);
1239 node = get_node(doc, (nsIDOMNode*)nselem, TRUE);
1240 set_event_handler_disp(get_node_event_target(node), node->nsnode, node->doc, i, disp);
1241 IDispatch_Release(disp);
1246 nsAString_Finish(&attr_value_str);
1247 nsAString_Finish(&attr_name_str);
1250 void release_event_target(event_target_t *event_target)
1254 for(i=0; i < EVENTID_LAST; i++) {
1255 if(event_target->event_table[i]) {
1256 if(event_target->event_table[i]->handler_prop)
1257 IDispatch_Release(event_target->event_table[i]->handler_prop);
1258 for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
1259 IDispatch_Release(event_target->event_table[i]->handlers[j]);
1263 heap_free(event_target);