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
28 #include "mshtml_private.h"
29 #include "htmlevent.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36 IDispatch *handler_prop;
38 IDispatch *handlers[0];
41 struct event_target_t {
42 handler_vector_t *event_table[EVENTID_LAST];
45 static const WCHAR beforeunloadW[] = {'b','e','f','o','r','e','u','n','l','o','a','d',0};
46 static const WCHAR onbeforeunloadW[] = {'o','n','b','e','f','o','r','e','u','n','l','o','a','d',0};
48 static const WCHAR blurW[] = {'b','l','u','r',0};
49 static const WCHAR onblurW[] = {'o','n','b','l','u','r',0};
51 static const WCHAR changeW[] = {'c','h','a','n','g','e',0};
52 static const WCHAR onchangeW[] = {'o','n','c','h','a','n','g','e',0};
54 static const WCHAR clickW[] = {'c','l','i','c','k',0};
55 static const WCHAR onclickW[] = {'o','n','c','l','i','c','k',0};
57 static const WCHAR dblclickW[] = {'d','b','l','c','l','i','c','k',0};
58 static const WCHAR ondblclickW[] = {'o','n','d','b','l','c','l','i','c','k',0};
60 static const WCHAR dragW[] = {'d','r','a','g',0};
61 static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};
63 static const WCHAR dragstartW[] = {'d','r','a','g','s','t','a','r','t',0};
64 static const WCHAR ondragstartW[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
66 static const WCHAR focusW[] = {'f','o','c','u','s',0};
67 static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};
69 static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
70 static const WCHAR onkeydownW[] = {'o','n','k','e','y','d','o','w','n',0};
72 static const WCHAR keyupW[] = {'k','e','y','u','p',0};
73 static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};
75 static const WCHAR loadW[] = {'l','o','a','d',0};
76 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
78 static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
79 static const WCHAR onmousedownW[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
81 static const WCHAR mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
82 static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};
84 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
85 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
87 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
88 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
90 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
91 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
93 static const WCHAR readystatechangeW[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
94 static const WCHAR onreadystatechangeW[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
96 static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
97 static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
105 #define EVENT_DEFAULTLISTENER 0x0001
106 #define EVENT_BUBBLE 0x0002
107 #define EVENT_FORWARDBODY 0x0004
109 static const event_info_t event_info[] = {
110 {beforeunloadW, onbeforeunloadW, EVENT_DEFAULTLISTENER|EVENT_FORWARDBODY},
111 {blurW, onblurW, EVENT_DEFAULTLISTENER},
112 {changeW, onchangeW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
113 {clickW, onclickW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
114 {dblclickW, ondblclickW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
116 {dragstartW, ondragstartW, 0},
117 {focusW, onfocusW, EVENT_DEFAULTLISTENER},
118 {keydownW, onkeydownW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
119 {keyupW, onkeyupW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
121 {mousedownW, onmousedownW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
122 {mouseoutW, onmouseoutW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
123 {mouseoverW, onmouseoverW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
124 {mouseupW, onmouseupW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
125 {pasteW, onpasteW, 0},
126 {readystatechangeW, onreadystatechangeW, 0},
127 {selectstartW, onselectstartW, 0}
130 eventid_t str_to_eid(LPCWSTR str)
134 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
135 if(!strcmpW(event_info[i].name, str))
139 ERR("unknown type %s\n", debugstr_w(str));
143 static eventid_t attr_to_eid(LPCWSTR str)
147 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
148 if(!strcmpW(event_info[i].attr_name, str))
157 const IHTMLEventObjVtbl *lpIHTMLEventObjVtbl;
162 const event_info_t *type;
163 nsIDOMEvent *nsevent;
166 #define HTMLEVENTOBJ(x) ((IHTMLEventObj*) &(x)->lpIHTMLEventObjVtbl)
168 #define HTMLEVENTOBJ_THIS(iface) DEFINE_THIS(HTMLEventObj, IHTMLEventObj, iface)
170 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
172 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
176 if(IsEqualGUID(&IID_IUnknown, riid)) {
177 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
178 *ppv = HTMLEVENTOBJ(This);
179 }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
180 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
181 *ppv = HTMLEVENTOBJ(This);
182 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
183 return *ppv ? S_OK : E_NOINTERFACE;
187 IUnknown_AddRef((IUnknown*)*ppv);
191 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
192 return E_NOINTERFACE;
195 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
197 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
198 LONG ref = InterlockedIncrement(&This->ref);
200 TRACE("(%p) ref=%d\n", This, ref);
205 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
207 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
208 LONG ref = InterlockedDecrement(&This->ref);
210 TRACE("(%p) ref=%d\n", This, ref);
214 nsIDOMEvent_Release(This->nsevent);
215 release_dispex(&This->dispex);
222 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
224 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
225 FIXME("(%p)->(%p)\n", This, pctinfo);
229 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
230 LCID lcid, ITypeInfo **ppTInfo)
232 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
233 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
237 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
238 LPOLESTR *rgszNames, UINT cNames,
239 LCID lcid, DISPID *rgDispId)
241 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
242 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
247 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
248 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
249 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
251 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
252 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
253 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
257 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
259 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
261 TRACE("(%p)->(%p)\n", This, p);
263 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(This->target), &IID_IHTMLElement, (void**)p);
266 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
268 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
271 TRACE("(%p)->(%p)\n", This, p);
274 nsIDOMKeyEvent *key_event;
277 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
278 if(NS_SUCCEEDED(nsres)) {
279 nsIDOMKeyEvent_GetAltKey(key_event, &ret);
280 nsIDOMKeyEvent_Release(key_event);
282 nsIDOMMouseEvent *mouse_event;
284 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
285 if(NS_SUCCEEDED(nsres)) {
286 nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
287 nsIDOMMouseEvent_Release(mouse_event);
292 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
296 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
298 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
301 TRACE("(%p)->(%p)\n", This, p);
304 nsIDOMKeyEvent *key_event;
307 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
308 if(NS_SUCCEEDED(nsres)) {
309 nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
310 nsIDOMKeyEvent_Release(key_event);
312 nsIDOMMouseEvent *mouse_event;
314 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
315 if(NS_SUCCEEDED(nsres)) {
316 nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
317 nsIDOMMouseEvent_Release(mouse_event);
322 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
326 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
328 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
331 TRACE("(%p)->(%p)\n", This, p);
334 nsIDOMKeyEvent *key_event;
337 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
338 if(NS_SUCCEEDED(nsres)) {
339 nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
340 nsIDOMKeyEvent_Release(key_event);
342 nsIDOMMouseEvent *mouse_event;
344 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
345 if(NS_SUCCEEDED(nsres)) {
346 nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
347 nsIDOMMouseEvent_Release(mouse_event);
352 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
356 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
358 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
359 FIXME("(%p)->()\n", This);
363 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
365 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
367 FIXME("(%p)->(%p)\n", This, p);
373 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
375 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
376 FIXME("(%p)->(%x)\n", This, v);
380 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
382 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
384 FIXME("(%p)->(%p)\n", This, p);
390 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
392 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
394 FIXME("(%p)->(%p)\n", This, p);
400 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
402 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
404 FIXME("(%p)->(%p)\n", This, p);
410 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
412 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
413 FIXME("(%p)->(%d)\n", This, v);
417 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
419 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
420 PRUint32 key_code = 0;
422 TRACE("(%p)->(%p)\n", This, p);
425 nsIDOMKeyEvent *key_event;
428 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
429 if(NS_SUCCEEDED(nsres)) {
430 nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
431 nsIDOMKeyEvent_Release(key_event);
439 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
441 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
444 TRACE("(%p)->(%p)\n", This, p);
447 nsIDOMMouseEvent *mouse_event;
450 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
451 if(NS_SUCCEEDED(nsres)) {
452 nsIDOMMouseEvent_GetButton(mouse_event, &button);
453 nsIDOMMouseEvent_Release(mouse_event);
461 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
463 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
465 TRACE("(%p)->(%p)\n", This, p);
467 *p = SysAllocString(This->type->name);
468 return *p ? S_OK : E_OUTOFMEMORY;
471 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
473 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
475 FIXME("(%p)->(%p)\n", This, p);
481 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
483 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
485 FIXME("(%p)->(%p)\n", This, p);
491 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
493 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
495 FIXME("(%p)->(%p)\n", This, p);
501 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
503 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
505 FIXME("(%p)->(%p)\n", This, p);
511 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
513 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
516 TRACE("(%p)->(%p)\n", This, p);
519 nsIDOMMouseEvent *mouse_event;
522 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
523 if(NS_SUCCEEDED(nsres)) {
524 nsIDOMMouseEvent_GetClientX(mouse_event, &x);
525 nsIDOMMouseEvent_Release(mouse_event);
533 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
535 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
538 TRACE("(%p)->(%p)\n", This, p);
541 nsIDOMMouseEvent *mouse_event;
544 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
545 if(NS_SUCCEEDED(nsres)) {
546 nsIDOMMouseEvent_GetClientY(mouse_event, &y);
547 nsIDOMMouseEvent_Release(mouse_event);
555 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
557 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
559 FIXME("(%p)->(%p)\n", This, p);
565 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
567 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
569 FIXME("(%p)->(%p)\n", This, p);
575 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
577 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
580 TRACE("(%p)->(%p)\n", This, p);
583 nsIDOMMouseEvent *mouse_event;
586 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
587 if(NS_SUCCEEDED(nsres)) {
588 nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
589 nsIDOMMouseEvent_Release(mouse_event);
597 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
599 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
602 TRACE("(%p)->(%p)\n", This, p);
605 nsIDOMMouseEvent *mouse_event;
608 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
609 if(NS_SUCCEEDED(nsres)) {
610 nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
611 nsIDOMMouseEvent_Release(mouse_event);
619 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
621 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
623 FIXME("(%p)->(%p)\n", This, p);
629 #undef HTMLEVENTOBJ_THIS
631 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
632 HTMLEventObj_QueryInterface,
634 HTMLEventObj_Release,
635 HTMLEventObj_GetTypeInfoCount,
636 HTMLEventObj_GetTypeInfo,
637 HTMLEventObj_GetIDsOfNames,
639 HTMLEventObj_get_srcElement,
640 HTMLEventObj_get_altKey,
641 HTMLEventObj_get_ctrlKey,
642 HTMLEventObj_get_shiftKey,
643 HTMLEventObj_put_returnValue,
644 HTMLEventObj_get_returnValue,
645 HTMLEventObj_put_cancelBubble,
646 HTMLEventObj_get_cancelBubble,
647 HTMLEventObj_get_fromElement,
648 HTMLEventObj_get_toElement,
649 HTMLEventObj_put_keyCode,
650 HTMLEventObj_get_keyCode,
651 HTMLEventObj_get_button,
652 HTMLEventObj_get_type,
653 HTMLEventObj_get_qualifier,
654 HTMLEventObj_get_reason,
657 HTMLEventObj_get_clientX,
658 HTMLEventObj_get_clientY,
659 HTMLEventObj_get_offsetX,
660 HTMLEventObj_get_offsetY,
661 HTMLEventObj_get_screenX,
662 HTMLEventObj_get_screenY,
663 HTMLEventObj_get_srcFilter
666 static const tid_t HTMLEventObj_iface_tids[] = {
671 static dispex_static_data_t HTMLEventObj_dispex = {
675 HTMLEventObj_iface_tids
678 static IHTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
682 ret = heap_alloc(sizeof(*ret));
683 ret->lpIHTMLEventObjVtbl = &HTMLEventObjVtbl;
685 ret->type = event_info+eid;
686 ret->target = target;
687 IHTMLDOMNode_AddRef(HTMLDOMNODE(target));
689 ret->nsevent = nsevent;
691 nsIDOMEvent_AddRef(nsevent);
693 init_dispex(&ret->dispex, (IUnknown*)HTMLEVENTOBJ(ret), &HTMLEventObj_dispex);
695 return HTMLEVENTOBJ(ret);
698 static void call_event_handlers(HTMLDocumentNode *doc, IHTMLEventObj *event_obj, event_target_t *event_target,
699 eventid_t eid, IDispatch *this_obj)
701 handler_vector_t *handler_vector;
705 if(!event_target || !(handler_vector = event_target->event_table[eid]))
708 if(handler_vector->handler_prop) {
709 DISPID named_arg = DISPID_THIS;
711 DISPPARAMS dp = {&arg, &named_arg, 1, 1};
713 V_VT(&arg) = VT_DISPATCH;
714 V_DISPATCH(&arg) = this_obj;
716 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
717 hres = call_disp_func(handler_vector->handler_prop, &dp);
719 TRACE("%s <<<\n", debugstr_w(event_info[eid].name));
721 WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
724 if(handler_vector->handler_cnt) {
726 DISPPARAMS dp = {&arg, NULL, 1, 0};
728 V_VT(&arg) = VT_DISPATCH;
729 V_DISPATCH(&arg) = (IDispatch*)event_obj;
731 for(i=0; i < handler_vector->handler_cnt; i++) {
732 if(handler_vector->handlers[i]) {
733 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
734 hres = call_disp_func(handler_vector->handlers[i], &dp);
736 TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
738 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
744 void fire_event(HTMLDocumentNode *doc, eventid_t eid, nsIDOMNode *target, nsIDOMEvent *nsevent)
746 IHTMLEventObj *prev_event, *event_obj = NULL;
747 nsIDOMNode *parent, *nsnode;
751 TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
753 prev_event = doc->basedoc.window->event;
754 event_obj = doc->basedoc.window->event = create_event(get_node(doc, target, TRUE), eid, nsevent);
756 nsIDOMNode_GetNodeType(target, &node_type);
758 nsIDOMNode_AddRef(nsnode);
763 node = get_node(doc, nsnode, FALSE);
765 call_event_handlers(doc, event_obj, *get_node_event_target(node), eid, (IDispatch*)HTMLDOMNODE(node));
767 if(!(event_info[eid].flags & EVENT_BUBBLE))
770 nsIDOMNode_GetParentNode(nsnode, &parent);
771 nsIDOMNode_Release(nsnode);
776 nsIDOMNode_GetNodeType(nsnode, &node_type);
777 }while(node_type == ELEMENT_NODE);
779 if(!(event_info[eid].flags & EVENT_BUBBLE))
783 if(event_info[eid].flags & EVENT_FORWARDBODY) {
784 nsIDOMHTMLElement *nsbody;
787 nsres = nsIDOMHTMLDocument_GetBody(doc->basedoc.nsdoc, &nsbody);
788 if(NS_SUCCEEDED(nsres) && nsbody) {
789 node = get_node(doc, (nsIDOMNode*)nsbody, FALSE);
791 call_event_handlers(doc, event_obj, *get_node_event_target(node), eid, (IDispatch*)HTMLDOMNODE(node));
792 nsIDOMHTMLElement_Release(nsbody);
794 ERR("Could not get body: %08x\n", nsres);
798 call_event_handlers(doc, event_obj, doc->basedoc.doc_node->node.event_target, eid,
799 (IDispatch*)HTMLDOC(&doc->basedoc));
803 FIXME("unimplemented node type %d\n", node_type);
807 nsIDOMNode_Release(nsnode);
809 IHTMLEventObj_Release(event_obj);
810 doc->basedoc.window->event = prev_event;
813 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
815 if(!*event_target_ptr)
816 *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
817 return *event_target_ptr;
820 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
822 handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
825 if(cnt <= handler_vector->handler_cnt)
828 new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
830 new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
836 new_vector->handler_cnt = cnt;
837 event_target->event_table[eid] = new_vector;
841 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, HTMLWindow *window, eventid_t eid, IDispatch *disp)
843 event_target_t *event_target;
845 event_target = get_event_target(event_target_ptr);
847 return E_OUTOFMEMORY;
849 if(!alloc_handler_vector(event_target, eid, 0))
850 return E_OUTOFMEMORY;
852 if(event_target->event_table[eid]->handler_prop)
853 IDispatch_Release(event_target->event_table[eid]->handler_prop);
855 event_target->event_table[eid]->handler_prop = disp;
858 IDispatch_AddRef(disp);
860 if(window->nswindow && (event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
861 if(!window->event_vector) {
862 window->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
863 if(!window->event_vector)
864 return E_OUTOFMEMORY;
867 if(!window->event_vector[eid]) {
868 window->event_vector[eid] = TRUE;
869 add_nsevent_listener(window, event_info[eid].name);
876 HRESULT set_event_handler(event_target_t **event_target, HTMLDocument *doc, eventid_t eid, VARIANT *var)
880 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
881 IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
882 (*event_target)->event_table[eid]->handler_prop = NULL;
887 return set_event_handler_disp(event_target, doc->window, eid, V_DISPATCH(var));
890 FIXME("not supported vt=%d\n", V_VT(var));
897 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
899 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
900 V_VT(var) = VT_DISPATCH;
901 V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
902 IDispatch_AddRef(V_DISPATCH(var));
910 HRESULT attach_event(event_target_t **event_target_ptr, HTMLDocument *doc, BSTR name, IDispatch *disp, VARIANT_BOOL *res)
912 event_target_t *event_target;
916 eid = attr_to_eid(name);
917 if(eid == EVENTID_LAST) {
918 WARN("Unknown event\n");
923 event_target = get_event_target(event_target_ptr);
925 return E_OUTOFMEMORY;
927 if(event_target->event_table[eid]) {
928 while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
930 if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
931 return E_OUTOFMEMORY;
932 }else if(!alloc_handler_vector(event_target, eid, i+1)) {
933 return E_OUTOFMEMORY;
936 IDispatch_AddRef(disp);
937 event_target->event_table[eid]->handlers[i] = disp;
943 void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
945 const PRUnichar *attr_value;
946 nsAString attr_name_str, attr_value_str;
952 nsAString_Init(&attr_value_str, NULL);
953 nsAString_Init(&attr_name_str, NULL);
955 for(i=0; i < EVENTID_LAST; i++) {
956 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
957 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
958 if(NS_SUCCEEDED(nsres)) {
959 nsAString_GetData(&attr_value_str, &attr_value);
963 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
965 disp = script_parse_event(doc->basedoc.window, attr_value);
967 node = get_node(doc, (nsIDOMNode*)nselem, TRUE);
968 set_event_handler_disp(get_node_event_target(node), node->doc->basedoc.window, i, disp);
969 IDispatch_Release(disp);
974 nsAString_Finish(&attr_value_str);
975 nsAString_Finish(&attr_name_str);
978 void release_event_target(event_target_t *event_target)
982 for(i=0; i < EVENTID_LAST; i++) {
983 if(event_target->event_table[i]) {
984 if(event_target->event_table[i]->handler_prop)
985 IDispatch_Release(event_target->event_table[i]->handler_prop);
986 for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
987 IDispatch_Release(event_target->event_table[i]->handlers[j]);
991 heap_free(event_target);