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 blurW
[] = {'b','l','u','r',0};
46 static const WCHAR onblurW
[] = {'o','n','b','l','u','r',0};
48 static const WCHAR changeW
[] = {'c','h','a','n','g','e',0};
49 static const WCHAR onchangeW
[] = {'o','n','c','h','a','n','g','e',0};
51 static const WCHAR clickW
[] = {'c','l','i','c','k',0};
52 static const WCHAR onclickW
[] = {'o','n','c','l','i','c','k',0};
54 static const WCHAR dragW
[] = {'d','r','a','g',0};
55 static const WCHAR ondragW
[] = {'o','n','d','r','a','g',0};
57 static const WCHAR dragstartW
[] = {'d','r','a','g','s','t','a','r','t',0};
58 static const WCHAR ondragstartW
[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
60 static const WCHAR focusW
[] = {'f','o','c','u','s',0};
61 static const WCHAR onfocusW
[] = {'o','n','f','o','c','u','s',0};
63 static const WCHAR keydownW
[] = {'k','e','y','d','o','w','n',0};
64 static const WCHAR onkeydownW
[] = {'o','n','k','e','y','d','o','w','n',0};
66 static const WCHAR keyupW
[] = {'k','e','y','u','p',0};
67 static const WCHAR onkeyupW
[] = {'o','n','k','e','y','u','p',0};
69 static const WCHAR loadW
[] = {'l','o','a','d',0};
70 static const WCHAR onloadW
[] = {'o','n','l','o','a','d',0};
72 static const WCHAR mousedownW
[] = {'m','o','u','s','e','d','o','w','n',0};
73 static const WCHAR onmousedownW
[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
75 static const WCHAR mouseoutW
[] = {'m','o','u','s','e','o','u','t',0};
76 static const WCHAR onmouseoutW
[] = {'o','n','m','o','u','s','e','o','u','t',0};
78 static const WCHAR mouseoverW
[] = {'m','o','u','s','e','o','v','e','r',0};
79 static const WCHAR onmouseoverW
[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
81 static const WCHAR mouseupW
[] = {'m','o','u','s','e','u','p',0};
82 static const WCHAR onmouseupW
[] = {'o','n','m','o','u','s','e','u','p',0};
84 static const WCHAR pasteW
[] = {'p','a','s','t','e',0};
85 static const WCHAR onpasteW
[] = {'o','n','p','a','s','t','e',0};
87 static const WCHAR selectstartW
[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
88 static const WCHAR onselectstartW
[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
96 #define EVENT_DEFAULTLISTENER 0x0001
97 #define EVENT_BUBBLE 0x0002
99 static const event_info_t event_info
[] = {
100 {blurW
, onblurW
, EVENT_DEFAULTLISTENER
},
101 {changeW
, onchangeW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
102 {clickW
, onclickW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
104 {dragstartW
, ondragstartW
, 0},
105 {focusW
, onfocusW
, EVENT_DEFAULTLISTENER
},
106 {keydownW
, onkeydownW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
107 {keyupW
, onkeyupW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
109 {mousedownW
, onmousedownW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
110 {mouseoutW
, onmouseoutW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
111 {mouseoverW
, onmouseoverW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
112 {mouseupW
, onmouseupW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
113 {pasteW
, onpasteW
, 0},
114 {selectstartW
, onselectstartW
, 0}
117 eventid_t
str_to_eid(LPCWSTR str
)
121 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
122 if(!strcmpW(event_info
[i
].name
, str
))
126 ERR("unknown type %s\n", debugstr_w(str
));
130 static eventid_t
attr_to_eid(LPCWSTR str
)
134 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
135 if(!strcmpW(event_info
[i
].attr_name
, str
))
144 const IHTMLEventObjVtbl
*lpIHTMLEventObjVtbl
;
149 const event_info_t
*type
;
150 nsIDOMEvent
*nsevent
;
153 #define HTMLEVENTOBJ(x) ((IHTMLEventObj*) &(x)->lpIHTMLEventObjVtbl)
155 #define HTMLEVENTOBJ_THIS(iface) DEFINE_THIS(HTMLEventObj, IHTMLEventObj, iface)
157 static HRESULT WINAPI
HTMLEventObj_QueryInterface(IHTMLEventObj
*iface
, REFIID riid
, void **ppv
)
159 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
163 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
164 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
165 *ppv
= HTMLEVENTOBJ(This
);
166 }else if(IsEqualGUID(&IID_IHTMLEventObj
, riid
)) {
167 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This
, ppv
);
168 *ppv
= HTMLEVENTOBJ(This
);
169 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
170 return *ppv
? S_OK
: E_NOINTERFACE
;
174 IUnknown_AddRef((IUnknown
*)*ppv
);
178 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
179 return E_NOINTERFACE
;
182 static ULONG WINAPI
HTMLEventObj_AddRef(IHTMLEventObj
*iface
)
184 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
185 LONG ref
= InterlockedIncrement(&This
->ref
);
187 TRACE("(%p) ref=%d\n", This
, ref
);
192 static ULONG WINAPI
HTMLEventObj_Release(IHTMLEventObj
*iface
)
194 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
195 LONG ref
= InterlockedDecrement(&This
->ref
);
197 TRACE("(%p) ref=%d\n", This
, ref
);
201 nsIDOMEvent_Release(This
->nsevent
);
202 release_dispex(&This
->dispex
);
209 static HRESULT WINAPI
HTMLEventObj_GetTypeInfoCount(IHTMLEventObj
*iface
, UINT
*pctinfo
)
211 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
212 FIXME("(%p)->(%p)\n", This
, pctinfo
);
216 static HRESULT WINAPI
HTMLEventObj_GetTypeInfo(IHTMLEventObj
*iface
, UINT iTInfo
,
217 LCID lcid
, ITypeInfo
**ppTInfo
)
219 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
220 FIXME("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
224 static HRESULT WINAPI
HTMLEventObj_GetIDsOfNames(IHTMLEventObj
*iface
, REFIID riid
,
225 LPOLESTR
*rgszNames
, UINT cNames
,
226 LCID lcid
, DISPID
*rgDispId
)
228 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
229 FIXME("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
234 static HRESULT WINAPI
HTMLEventObj_Invoke(IHTMLEventObj
*iface
, DISPID dispIdMember
,
235 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
236 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
238 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
239 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
240 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
244 static HRESULT WINAPI
HTMLEventObj_get_srcElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
246 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
248 TRACE("(%p)->(%p)\n", This
, p
);
250 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(This
->target
), &IID_IHTMLElement
, (void**)p
);
253 static HRESULT WINAPI
HTMLEventObj_get_altKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
255 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
258 TRACE("(%p)->(%p)\n", This
, p
);
261 nsIDOMKeyEvent
*key_event
;
264 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
265 if(NS_SUCCEEDED(nsres
)) {
266 nsIDOMKeyEvent_GetAltKey(key_event
, &ret
);
267 nsIDOMKeyEvent_Release(key_event
);
269 nsIDOMMouseEvent
*mouse_event
;
271 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
272 if(NS_SUCCEEDED(nsres
)) {
273 nsIDOMMouseEvent_GetAltKey(mouse_event
, &ret
);
274 nsIDOMMouseEvent_Release(mouse_event
);
279 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
283 static HRESULT WINAPI
HTMLEventObj_get_ctrlKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
285 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
288 TRACE("(%p)->(%p)\n", This
, p
);
291 nsIDOMKeyEvent
*key_event
;
294 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
295 if(NS_SUCCEEDED(nsres
)) {
296 nsIDOMKeyEvent_GetCtrlKey(key_event
, &ret
);
297 nsIDOMKeyEvent_Release(key_event
);
299 nsIDOMMouseEvent
*mouse_event
;
301 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
302 if(NS_SUCCEEDED(nsres
)) {
303 nsIDOMMouseEvent_GetCtrlKey(mouse_event
, &ret
);
304 nsIDOMMouseEvent_Release(mouse_event
);
309 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
313 static HRESULT WINAPI
HTMLEventObj_get_shiftKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
315 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
318 TRACE("(%p)->(%p)\n", This
, p
);
321 nsIDOMKeyEvent
*key_event
;
324 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
325 if(NS_SUCCEEDED(nsres
)) {
326 nsIDOMKeyEvent_GetShiftKey(key_event
, &ret
);
327 nsIDOMKeyEvent_Release(key_event
);
329 nsIDOMMouseEvent
*mouse_event
;
331 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
332 if(NS_SUCCEEDED(nsres
)) {
333 nsIDOMMouseEvent_GetShiftKey(mouse_event
, &ret
);
334 nsIDOMMouseEvent_Release(mouse_event
);
339 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
343 static HRESULT WINAPI
HTMLEventObj_put_returnValue(IHTMLEventObj
*iface
, VARIANT v
)
345 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
346 FIXME("(%p)->()\n", This
);
350 static HRESULT WINAPI
HTMLEventObj_get_returnValue(IHTMLEventObj
*iface
, VARIANT
*p
)
352 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
354 FIXME("(%p)->(%p)\n", This
, p
);
360 static HRESULT WINAPI
HTMLEventObj_put_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL v
)
362 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
363 FIXME("(%p)->(%x)\n", This
, v
);
367 static HRESULT WINAPI
HTMLEventObj_get_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
369 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
371 FIXME("(%p)->(%p)\n", This
, p
);
377 static HRESULT WINAPI
HTMLEventObj_get_fromElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
379 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
381 FIXME("(%p)->(%p)\n", This
, p
);
387 static HRESULT WINAPI
HTMLEventObj_get_toElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
389 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
391 FIXME("(%p)->(%p)\n", This
, p
);
397 static HRESULT WINAPI
HTMLEventObj_put_keyCode(IHTMLEventObj
*iface
, LONG v
)
399 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
400 FIXME("(%p)->(%d)\n", This
, v
);
404 static HRESULT WINAPI
HTMLEventObj_get_keyCode(IHTMLEventObj
*iface
, LONG
*p
)
406 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
407 PRUint32 key_code
= 0;
409 TRACE("(%p)->(%p)\n", This
, p
);
412 nsIDOMKeyEvent
*key_event
;
415 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
416 if(NS_SUCCEEDED(nsres
)) {
417 nsIDOMKeyEvent_GetKeyCode(key_event
, &key_code
);
418 nsIDOMKeyEvent_Release(key_event
);
426 static HRESULT WINAPI
HTMLEventObj_get_button(IHTMLEventObj
*iface
, LONG
*p
)
428 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
431 TRACE("(%p)->(%p)\n", This
, p
);
434 nsIDOMMouseEvent
*mouse_event
;
437 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
438 if(NS_SUCCEEDED(nsres
)) {
439 nsIDOMMouseEvent_GetButton(mouse_event
, &button
);
440 nsIDOMMouseEvent_Release(mouse_event
);
448 static HRESULT WINAPI
HTMLEventObj_get_type(IHTMLEventObj
*iface
, BSTR
*p
)
450 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
452 TRACE("(%p)->(%p)\n", This
, p
);
454 *p
= SysAllocString(This
->type
->name
);
455 return *p
? S_OK
: E_OUTOFMEMORY
;
458 static HRESULT WINAPI
HTMLEventObj_get_qualifier(IHTMLEventObj
*iface
, BSTR
*p
)
460 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
462 FIXME("(%p)->(%p)\n", This
, p
);
468 static HRESULT WINAPI
HTMLEventObj_get_reason(IHTMLEventObj
*iface
, LONG
*p
)
470 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
472 FIXME("(%p)->(%p)\n", This
, p
);
478 static HRESULT WINAPI
HTMLEventObj_get_x(IHTMLEventObj
*iface
, LONG
*p
)
480 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
482 FIXME("(%p)->(%p)\n", This
, p
);
488 static HRESULT WINAPI
HTMLEventObj_get_y(IHTMLEventObj
*iface
, LONG
*p
)
490 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
492 FIXME("(%p)->(%p)\n", This
, p
);
498 static HRESULT WINAPI
HTMLEventObj_get_clientX(IHTMLEventObj
*iface
, LONG
*p
)
500 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
503 TRACE("(%p)->(%p)\n", This
, p
);
506 nsIDOMMouseEvent
*mouse_event
;
509 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
510 if(NS_SUCCEEDED(nsres
)) {
511 nsIDOMMouseEvent_GetClientX(mouse_event
, &x
);
512 nsIDOMMouseEvent_Release(mouse_event
);
520 static HRESULT WINAPI
HTMLEventObj_get_clientY(IHTMLEventObj
*iface
, LONG
*p
)
522 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
525 TRACE("(%p)->(%p)\n", This
, p
);
528 nsIDOMMouseEvent
*mouse_event
;
531 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
532 if(NS_SUCCEEDED(nsres
)) {
533 nsIDOMMouseEvent_GetClientY(mouse_event
, &y
);
534 nsIDOMMouseEvent_Release(mouse_event
);
542 static HRESULT WINAPI
HTMLEventObj_get_offsetX(IHTMLEventObj
*iface
, LONG
*p
)
544 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
546 FIXME("(%p)->(%p)\n", This
, p
);
552 static HRESULT WINAPI
HTMLEventObj_get_offsetY(IHTMLEventObj
*iface
, LONG
*p
)
554 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
556 FIXME("(%p)->(%p)\n", This
, p
);
562 static HRESULT WINAPI
HTMLEventObj_get_screenX(IHTMLEventObj
*iface
, LONG
*p
)
564 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
567 TRACE("(%p)->(%p)\n", This
, p
);
570 nsIDOMMouseEvent
*mouse_event
;
573 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
574 if(NS_SUCCEEDED(nsres
)) {
575 nsIDOMMouseEvent_GetScreenX(mouse_event
, &x
);
576 nsIDOMMouseEvent_Release(mouse_event
);
584 static HRESULT WINAPI
HTMLEventObj_get_screenY(IHTMLEventObj
*iface
, LONG
*p
)
586 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
589 TRACE("(%p)->(%p)\n", This
, p
);
592 nsIDOMMouseEvent
*mouse_event
;
595 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
596 if(NS_SUCCEEDED(nsres
)) {
597 nsIDOMMouseEvent_GetScreenY(mouse_event
, &y
);
598 nsIDOMMouseEvent_Release(mouse_event
);
606 static HRESULT WINAPI
HTMLEventObj_get_srcFilter(IHTMLEventObj
*iface
, IDispatch
**p
)
608 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
610 FIXME("(%p)->(%p)\n", This
, p
);
616 #undef HTMLEVENTOBJ_THIS
618 static const IHTMLEventObjVtbl HTMLEventObjVtbl
= {
619 HTMLEventObj_QueryInterface
,
621 HTMLEventObj_Release
,
622 HTMLEventObj_GetTypeInfoCount
,
623 HTMLEventObj_GetTypeInfo
,
624 HTMLEventObj_GetIDsOfNames
,
626 HTMLEventObj_get_srcElement
,
627 HTMLEventObj_get_altKey
,
628 HTMLEventObj_get_ctrlKey
,
629 HTMLEventObj_get_shiftKey
,
630 HTMLEventObj_put_returnValue
,
631 HTMLEventObj_get_returnValue
,
632 HTMLEventObj_put_cancelBubble
,
633 HTMLEventObj_get_cancelBubble
,
634 HTMLEventObj_get_fromElement
,
635 HTMLEventObj_get_toElement
,
636 HTMLEventObj_put_keyCode
,
637 HTMLEventObj_get_keyCode
,
638 HTMLEventObj_get_button
,
639 HTMLEventObj_get_type
,
640 HTMLEventObj_get_qualifier
,
641 HTMLEventObj_get_reason
,
644 HTMLEventObj_get_clientX
,
645 HTMLEventObj_get_clientY
,
646 HTMLEventObj_get_offsetX
,
647 HTMLEventObj_get_offsetY
,
648 HTMLEventObj_get_screenX
,
649 HTMLEventObj_get_screenY
,
650 HTMLEventObj_get_srcFilter
653 static const tid_t HTMLEventObj_iface_tids
[] = {
658 static dispex_static_data_t HTMLEventObj_dispex
= {
662 HTMLEventObj_iface_tids
665 static IHTMLEventObj
*create_event(HTMLDOMNode
*target
, eventid_t eid
, nsIDOMEvent
*nsevent
)
669 ret
= heap_alloc(sizeof(*ret
));
670 ret
->lpIHTMLEventObjVtbl
= &HTMLEventObjVtbl
;
672 ret
->type
= event_info
+eid
;
673 ret
->target
= target
;
674 IHTMLDOMNode_AddRef(HTMLDOMNODE(target
));
676 ret
->nsevent
= nsevent
;
678 nsIDOMEvent_AddRef(nsevent
);
680 init_dispex(&ret
->dispex
, (IUnknown
*)HTMLEVENTOBJ(ret
), &HTMLEventObj_dispex
);
682 return HTMLEVENTOBJ(ret
);
685 static void call_event_handlers(HTMLDocument
*doc
, IHTMLEventObj
*event_obj
, event_target_t
*event_target
,
686 eventid_t eid
, IDispatch
*this_obj
)
688 handler_vector_t
*handler_vector
;
692 if(!event_target
|| !(handler_vector
= event_target
->event_table
[eid
]))
695 if(handler_vector
->handler_prop
) {
696 DISPID named_arg
= DISPID_THIS
;
698 DISPPARAMS dp
= {&arg
, &named_arg
, 1, 1};
700 V_VT(&arg
) = VT_DISPATCH
;
701 V_DISPATCH(&arg
) = this_obj
;
703 TRACE("%s >>>\n", debugstr_w(event_info
[eid
].name
));
704 hres
= call_disp_func(handler_vector
->handler_prop
, &dp
);
706 TRACE("%s <<<\n", debugstr_w(event_info
[eid
].name
));
708 WARN("%s <<< %08x\n", debugstr_w(event_info
[eid
].name
), hres
);
711 if(handler_vector
->handler_cnt
) {
713 DISPPARAMS dp
= {&arg
, NULL
, 1, 0};
715 V_VT(&arg
) = VT_DISPATCH
;
716 V_DISPATCH(&arg
) = (IDispatch
*)event_obj
;
718 for(i
=0; i
< handler_vector
->handler_cnt
; i
++) {
719 if(handler_vector
->handlers
[i
]) {
720 TRACE("%s [%d] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
721 hres
= call_disp_func(handler_vector
->handlers
[i
], &dp
);
723 TRACE("%s [%d] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
725 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
731 void fire_event(HTMLDocument
*doc
, eventid_t eid
, nsIDOMNode
*target
, nsIDOMEvent
*nsevent
)
733 IHTMLEventObj
*prev_event
, *event_obj
= NULL
;
734 nsIDOMNode
*parent
, *nsnode
;
738 nsIDOMNode_GetNodeType(target
, &node_type
);
739 if(node_type
!= ELEMENT_NODE
) {
740 FIXME("node type %d node supported\n", node_type
);
744 prev_event
= doc
->window
->event
;
745 event_obj
= doc
->window
->event
= create_event(get_node(doc
, target
, TRUE
), eid
, nsevent
);
747 nsIDOMNode_AddRef(nsnode
);
750 node
= get_node(doc
, nsnode
, FALSE
);
753 call_event_handlers(doc
, event_obj
, node
->event_target
, eid
, (IDispatch
*)HTMLDOMNODE(node
));
755 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
))
758 nsIDOMNode_GetParentNode(nsnode
, &parent
);
759 nsIDOMNode_Release(nsnode
);
764 nsIDOMNode_GetNodeType(nsnode
, &node_type
);
765 if(node_type
!= ELEMENT_NODE
)
770 nsIDOMNode_Release(nsnode
);
772 if(event_info
[eid
].flags
& EVENT_BUBBLE
)
773 call_event_handlers(doc
, event_obj
, doc
->event_target
, eid
, (IDispatch
*)HTMLDOC(doc
));
775 IHTMLEventObj_Release(event_obj
);
776 doc
->window
->event
= prev_event
;
779 static inline event_target_t
*get_event_target(event_target_t
**event_target_ptr
)
781 if(!*event_target_ptr
)
782 *event_target_ptr
= heap_alloc_zero(sizeof(event_target_t
));
783 return *event_target_ptr
;
786 static BOOL
alloc_handler_vector(event_target_t
*event_target
, eventid_t eid
, int cnt
)
788 handler_vector_t
*new_vector
, *handler_vector
= event_target
->event_table
[eid
];
791 if(cnt
<= handler_vector
->handler_cnt
)
794 new_vector
= heap_realloc_zero(handler_vector
, sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
796 new_vector
= heap_alloc_zero(sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
802 new_vector
->handler_cnt
= cnt
;
803 event_target
->event_table
[eid
] = new_vector
;
807 static HRESULT
set_event_handler_disp(event_target_t
**event_target_ptr
, HTMLDocument
*doc
, eventid_t eid
, IDispatch
*disp
)
809 event_target_t
*event_target
;
811 event_target
= get_event_target(event_target_ptr
);
813 return E_OUTOFMEMORY
;
815 if(!alloc_handler_vector(event_target
, eid
, 0))
816 return E_OUTOFMEMORY
;
818 if(event_target
->event_table
[eid
]->handler_prop
)
819 IDispatch_Release(event_target
->event_table
[eid
]->handler_prop
);
821 event_target
->event_table
[eid
]->handler_prop
= disp
;
824 IDispatch_AddRef(disp
);
826 if(doc
->nscontainer
&& (event_info
[eid
].flags
& EVENT_DEFAULTLISTENER
)) {
827 if(!doc
->nscontainer
->event_vector
) {
828 doc
->nscontainer
->event_vector
= heap_alloc_zero(EVENTID_LAST
*sizeof(BOOL
));
829 if(!doc
->nscontainer
->event_vector
)
830 return E_OUTOFMEMORY
;
833 if(!doc
->nscontainer
->event_vector
[eid
]) {
834 doc
->nscontainer
->event_vector
[eid
] = TRUE
;
835 add_nsevent_listener(doc
->nscontainer
, event_info
[eid
].name
);
842 HRESULT
set_event_handler(event_target_t
**event_target
, HTMLDocument
*doc
, eventid_t eid
, VARIANT
*var
)
846 if(*event_target
&& (*event_target
)->event_table
[eid
] && (*event_target
)->event_table
[eid
]->handler_prop
) {
847 IDispatch_Release((*event_target
)->event_table
[eid
]->handler_prop
);
848 (*event_target
)->event_table
[eid
]->handler_prop
= NULL
;
853 return set_event_handler_disp(event_target
, doc
, eid
, V_DISPATCH(var
));
856 FIXME("not supported vt=%d\n", V_VT(var
));
863 HRESULT
get_event_handler(event_target_t
**event_target
, eventid_t eid
, VARIANT
*var
)
865 if(*event_target
&& (*event_target
)->event_table
[eid
] && (*event_target
)->event_table
[eid
]->handler_prop
) {
866 V_VT(var
) = VT_DISPATCH
;
867 V_DISPATCH(var
) = (*event_target
)->event_table
[eid
]->handler_prop
;
868 IDispatch_AddRef(V_DISPATCH(var
));
876 HRESULT
attach_event(event_target_t
**event_target_ptr
, HTMLDocument
*doc
, BSTR name
, IDispatch
*disp
, VARIANT_BOOL
*res
)
878 event_target_t
*event_target
;
882 eid
= attr_to_eid(name
);
883 if(eid
== EVENTID_LAST
) {
884 WARN("Unknown event\n");
889 event_target
= get_event_target(event_target_ptr
);
891 return E_OUTOFMEMORY
;
893 if(event_target
->event_table
[eid
]) {
894 while(i
< event_target
->event_table
[eid
]->handler_cnt
&& event_target
->event_table
[eid
]->handlers
[i
])
896 if(i
== event_target
->event_table
[eid
]->handler_cnt
&& !alloc_handler_vector(event_target
, eid
, i
+1))
897 return E_OUTOFMEMORY
;
898 }else if(!alloc_handler_vector(event_target
, eid
, i
+1)) {
899 return E_OUTOFMEMORY
;
902 IDispatch_AddRef(disp
);
903 event_target
->event_table
[eid
]->handlers
[i
] = disp
;
909 void check_event_attr(HTMLDocument
*doc
, nsIDOMElement
*nselem
)
911 const PRUnichar
*attr_value
;
912 nsAString attr_name_str
, attr_value_str
;
918 nsAString_Init(&attr_value_str
, NULL
);
919 nsAString_Init(&attr_name_str
, NULL
);
921 for(i
=0; i
< EVENTID_LAST
; i
++) {
922 nsAString_SetData(&attr_name_str
, event_info
[i
].attr_name
);
923 nsres
= nsIDOMElement_GetAttribute(nselem
, &attr_name_str
, &attr_value_str
);
924 if(NS_SUCCEEDED(nsres
)) {
925 nsAString_GetData(&attr_value_str
, &attr_value
);
929 TRACE("%p.%s = %s\n", nselem
, debugstr_w(event_info
[i
].attr_name
), debugstr_w(attr_value
));
931 disp
= script_parse_event(doc
, attr_value
);
933 node
= get_node(doc
, (nsIDOMNode
*)nselem
, TRUE
);
934 set_event_handler_disp(&node
->event_target
, node
->doc
, i
, disp
);
935 IDispatch_Release(disp
);
940 nsAString_Finish(&attr_value_str
);
941 nsAString_Finish(&attr_name_str
);
944 void release_event_target(event_target_t
*event_target
)
948 for(i
=0; i
< EVENTID_LAST
; i
++) {
949 if(event_target
->event_table
[i
]) {
950 if(event_target
->event_table
[i
]->handler_prop
)
951 IDispatch_Release(event_target
->event_table
[i
]->handler_prop
);
952 for(j
=0; j
< event_target
->event_table
[i
]->handler_cnt
; j
++)
953 IDispatch_Release(event_target
->event_table
[i
]->handlers
[j
]);
957 heap_free(event_target
);