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 focusW
[] = {'f','o','c','u','s',0};
72 static const WCHAR onfocusW
[] = {'o','n','f','o','c','u','s',0};
74 static const WCHAR keydownW
[] = {'k','e','y','d','o','w','n',0};
75 static const WCHAR onkeydownW
[] = {'o','n','k','e','y','d','o','w','n',0};
77 static const WCHAR keyupW
[] = {'k','e','y','u','p',0};
78 static const WCHAR onkeyupW
[] = {'o','n','k','e','y','u','p',0};
80 static const WCHAR loadW
[] = {'l','o','a','d',0};
81 static const WCHAR onloadW
[] = {'o','n','l','o','a','d',0};
83 static const WCHAR mousedownW
[] = {'m','o','u','s','e','d','o','w','n',0};
84 static const WCHAR onmousedownW
[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
86 static const WCHAR mouseoutW
[] = {'m','o','u','s','e','o','u','t',0};
87 static const WCHAR onmouseoutW
[] = {'o','n','m','o','u','s','e','o','u','t',0};
89 static const WCHAR mouseoverW
[] = {'m','o','u','s','e','o','v','e','r',0};
90 static const WCHAR onmouseoverW
[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
92 static const WCHAR mouseupW
[] = {'m','o','u','s','e','u','p',0};
93 static const WCHAR onmouseupW
[] = {'o','n','m','o','u','s','e','u','p',0};
95 static const WCHAR pasteW
[] = {'p','a','s','t','e',0};
96 static const WCHAR onpasteW
[] = {'o','n','p','a','s','t','e',0};
98 static const WCHAR readystatechangeW
[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
99 static const WCHAR onreadystatechangeW
[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
101 static const WCHAR resizeW
[] = {'r','e','s','i','z','e',0};
102 static const WCHAR onresizeW
[] = {'o','n','r','e','s','i','z','e',0};
104 static const WCHAR selectstartW
[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
105 static const WCHAR onselectstartW
[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
107 static const WCHAR HTMLEventsW
[] = {'H','T','M','L','E','v','e','n','t','s',0};
108 static const WCHAR KeyboardEventW
[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
109 static const WCHAR MouseEventW
[] = {'M','o','u','s','e','E','v','e','n','t',0};
118 static const WCHAR
*event_types
[] = {
133 #define EVENT_DEFAULTLISTENER 0x0001
134 #define EVENT_BUBBLE 0x0002
135 #define EVENT_FORWARDBODY 0x0004
136 #define EVENT_NODEHANDLER 0x0008
138 static const event_info_t event_info
[] = {
139 {beforeunloadW
, onbeforeunloadW
, EVENTT_NONE
, DISPID_EVMETH_ONBEFOREUNLOAD
,
140 EVENT_DEFAULTLISTENER
|EVENT_FORWARDBODY
},
141 {blurW
, onblurW
, EVENTT_HTML
, DISPID_EVMETH_ONBLUR
,
142 EVENT_DEFAULTLISTENER
},
143 {changeW
, onchangeW
, EVENTT_HTML
, DISPID_EVMETH_ONCHANGE
,
144 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
145 {clickW
, onclickW
, EVENTT_MOUSE
, DISPID_EVMETH_ONCLICK
,
146 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
147 {contextmenuW
, oncontextmenuW
, EVENTT_MOUSE
, DISPID_EVMETH_ONCONTEXTMENU
,
149 {dblclickW
, ondblclickW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDBLCLICK
,
150 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
151 {dragW
, ondragW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDRAG
,
153 {dragstartW
, ondragstartW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDRAGSTART
,
155 {focusW
, onfocusW
, EVENTT_HTML
, DISPID_EVMETH_ONFOCUS
,
156 EVENT_DEFAULTLISTENER
},
157 {keydownW
, onkeydownW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYDOWN
,
158 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
159 {keyupW
, onkeyupW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYUP
,
160 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
161 {loadW
, onloadW
, EVENTT_HTML
, DISPID_EVMETH_ONLOAD
,
163 {mousedownW
, onmousedownW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEDOWN
,
164 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
165 {mouseoutW
, onmouseoutW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEOUT
,
166 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
167 {mouseoverW
, onmouseoverW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEOVER
,
168 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
169 {mouseupW
, onmouseupW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEUP
,
170 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
171 {pasteW
, onpasteW
, EVENTT_NONE
, DISPID_EVMETH_ONPASTE
,
173 {readystatechangeW
, onreadystatechangeW
, EVENTT_NONE
, DISPID_EVMETH_ONREADYSTATECHANGE
,
175 {resizeW
, onresizeW
, EVENTT_NONE
, DISPID_EVMETH_ONRESIZE
,
176 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
177 {selectstartW
, onselectstartW
, EVENTT_MOUSE
, DISPID_EVMETH_ONSELECTSTART
,
181 static const eventid_t node_handled_list
[] = { EVENTID_LOAD
};
183 eventid_t
str_to_eid(LPCWSTR str
)
187 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
188 if(!strcmpW(event_info
[i
].name
, str
))
192 ERR("unknown type %s\n", debugstr_w(str
));
196 static eventid_t
attr_to_eid(LPCWSTR str
)
200 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
201 if(!strcmpW(event_info
[i
].attr_name
, str
))
208 static DWORD
get_node_handler_mask(eventid_t eid
)
212 for(i
=0; i
<sizeof(node_handled_list
)/sizeof(*node_handled_list
); i
++) {
213 if(node_handled_list
[i
] == eid
)
217 ERR("Invalid eid %d\n", eid
);
223 const IHTMLEventObjVtbl
*lpIHTMLEventObjVtbl
;
228 const event_info_t
*type
;
229 nsIDOMEvent
*nsevent
;
232 #define HTMLEVENTOBJ(x) ((IHTMLEventObj*) &(x)->lpIHTMLEventObjVtbl)
234 #define HTMLEVENTOBJ_THIS(iface) DEFINE_THIS(HTMLEventObj, IHTMLEventObj, iface)
236 static HRESULT WINAPI
HTMLEventObj_QueryInterface(IHTMLEventObj
*iface
, REFIID riid
, void **ppv
)
238 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
242 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
243 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
244 *ppv
= HTMLEVENTOBJ(This
);
245 }else if(IsEqualGUID(&IID_IHTMLEventObj
, riid
)) {
246 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This
, ppv
);
247 *ppv
= HTMLEVENTOBJ(This
);
248 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
249 return *ppv
? S_OK
: E_NOINTERFACE
;
253 IUnknown_AddRef((IUnknown
*)*ppv
);
257 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
258 return E_NOINTERFACE
;
261 static ULONG WINAPI
HTMLEventObj_AddRef(IHTMLEventObj
*iface
)
263 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
264 LONG ref
= InterlockedIncrement(&This
->ref
);
266 TRACE("(%p) ref=%d\n", This
, ref
);
271 static ULONG WINAPI
HTMLEventObj_Release(IHTMLEventObj
*iface
)
273 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
274 LONG ref
= InterlockedDecrement(&This
->ref
);
276 TRACE("(%p) ref=%d\n", This
, ref
);
280 nsIDOMEvent_Release(This
->nsevent
);
281 release_dispex(&This
->dispex
);
288 static HRESULT WINAPI
HTMLEventObj_GetTypeInfoCount(IHTMLEventObj
*iface
, UINT
*pctinfo
)
290 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
291 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This
->dispex
), pctinfo
);
294 static HRESULT WINAPI
HTMLEventObj_GetTypeInfo(IHTMLEventObj
*iface
, UINT iTInfo
,
295 LCID lcid
, ITypeInfo
**ppTInfo
)
297 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
298 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This
->dispex
), iTInfo
, lcid
, ppTInfo
);
301 static HRESULT WINAPI
HTMLEventObj_GetIDsOfNames(IHTMLEventObj
*iface
, REFIID riid
,
302 LPOLESTR
*rgszNames
, UINT cNames
,
303 LCID lcid
, DISPID
*rgDispId
)
305 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
306 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This
->dispex
), riid
, rgszNames
, cNames
, lcid
, rgDispId
);
309 static HRESULT WINAPI
HTMLEventObj_Invoke(IHTMLEventObj
*iface
, DISPID dispIdMember
,
310 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
311 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
313 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
314 return IDispatchEx_Invoke(DISPATCHEX(&This
->dispex
), dispIdMember
, riid
, lcid
,
315 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
318 static HRESULT WINAPI
HTMLEventObj_get_srcElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
320 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
322 TRACE("(%p)->(%p)\n", This
, p
);
324 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(This
->target
), &IID_IHTMLElement
, (void**)p
);
327 static HRESULT WINAPI
HTMLEventObj_get_altKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
329 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
332 TRACE("(%p)->(%p)\n", This
, p
);
335 nsIDOMKeyEvent
*key_event
;
338 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
339 if(NS_SUCCEEDED(nsres
)) {
340 nsIDOMKeyEvent_GetAltKey(key_event
, &ret
);
341 nsIDOMKeyEvent_Release(key_event
);
343 nsIDOMMouseEvent
*mouse_event
;
345 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
346 if(NS_SUCCEEDED(nsres
)) {
347 nsIDOMMouseEvent_GetAltKey(mouse_event
, &ret
);
348 nsIDOMMouseEvent_Release(mouse_event
);
353 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
357 static HRESULT WINAPI
HTMLEventObj_get_ctrlKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
359 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
362 TRACE("(%p)->(%p)\n", This
, p
);
365 nsIDOMKeyEvent
*key_event
;
368 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
369 if(NS_SUCCEEDED(nsres
)) {
370 nsIDOMKeyEvent_GetCtrlKey(key_event
, &ret
);
371 nsIDOMKeyEvent_Release(key_event
);
373 nsIDOMMouseEvent
*mouse_event
;
375 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
376 if(NS_SUCCEEDED(nsres
)) {
377 nsIDOMMouseEvent_GetCtrlKey(mouse_event
, &ret
);
378 nsIDOMMouseEvent_Release(mouse_event
);
383 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
387 static HRESULT WINAPI
HTMLEventObj_get_shiftKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
389 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
392 TRACE("(%p)->(%p)\n", This
, p
);
395 nsIDOMKeyEvent
*key_event
;
398 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
399 if(NS_SUCCEEDED(nsres
)) {
400 nsIDOMKeyEvent_GetShiftKey(key_event
, &ret
);
401 nsIDOMKeyEvent_Release(key_event
);
403 nsIDOMMouseEvent
*mouse_event
;
405 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
406 if(NS_SUCCEEDED(nsres
)) {
407 nsIDOMMouseEvent_GetShiftKey(mouse_event
, &ret
);
408 nsIDOMMouseEvent_Release(mouse_event
);
413 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
417 static HRESULT WINAPI
HTMLEventObj_put_returnValue(IHTMLEventObj
*iface
, VARIANT v
)
419 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
420 FIXME("(%p)->()\n", This
);
424 static HRESULT WINAPI
HTMLEventObj_get_returnValue(IHTMLEventObj
*iface
, VARIANT
*p
)
426 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
428 FIXME("(%p)->(%p)\n", This
, p
);
434 static HRESULT WINAPI
HTMLEventObj_put_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL v
)
436 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
437 FIXME("(%p)->(%x)\n", This
, v
);
441 static HRESULT WINAPI
HTMLEventObj_get_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
443 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
445 FIXME("(%p)->(%p)\n", This
, p
);
451 static HRESULT WINAPI
HTMLEventObj_get_fromElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
453 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
455 FIXME("(%p)->(%p)\n", This
, p
);
461 static HRESULT WINAPI
HTMLEventObj_get_toElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
463 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
465 FIXME("(%p)->(%p)\n", This
, p
);
471 static HRESULT WINAPI
HTMLEventObj_put_keyCode(IHTMLEventObj
*iface
, LONG v
)
473 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
474 FIXME("(%p)->(%d)\n", This
, v
);
478 static HRESULT WINAPI
HTMLEventObj_get_keyCode(IHTMLEventObj
*iface
, LONG
*p
)
480 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
481 PRUint32 key_code
= 0;
483 TRACE("(%p)->(%p)\n", This
, p
);
486 nsIDOMKeyEvent
*key_event
;
489 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
490 if(NS_SUCCEEDED(nsres
)) {
491 nsIDOMKeyEvent_GetKeyCode(key_event
, &key_code
);
492 nsIDOMKeyEvent_Release(key_event
);
500 static HRESULT WINAPI
HTMLEventObj_get_button(IHTMLEventObj
*iface
, LONG
*p
)
502 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
505 TRACE("(%p)->(%p)\n", This
, p
);
508 nsIDOMMouseEvent
*mouse_event
;
511 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
512 if(NS_SUCCEEDED(nsres
)) {
513 nsIDOMMouseEvent_GetButton(mouse_event
, &button
);
514 nsIDOMMouseEvent_Release(mouse_event
);
522 static HRESULT WINAPI
HTMLEventObj_get_type(IHTMLEventObj
*iface
, BSTR
*p
)
524 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
526 TRACE("(%p)->(%p)\n", This
, p
);
528 *p
= SysAllocString(This
->type
->name
);
529 return *p
? S_OK
: E_OUTOFMEMORY
;
532 static HRESULT WINAPI
HTMLEventObj_get_qualifier(IHTMLEventObj
*iface
, BSTR
*p
)
534 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
536 FIXME("(%p)->(%p)\n", This
, p
);
542 static HRESULT WINAPI
HTMLEventObj_get_reason(IHTMLEventObj
*iface
, LONG
*p
)
544 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
546 FIXME("(%p)->(%p)\n", This
, p
);
552 static HRESULT WINAPI
HTMLEventObj_get_x(IHTMLEventObj
*iface
, LONG
*p
)
554 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
556 FIXME("(%p)->(%p)\n", This
, p
);
562 static HRESULT WINAPI
HTMLEventObj_get_y(IHTMLEventObj
*iface
, LONG
*p
)
564 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
566 FIXME("(%p)->(%p)\n", This
, p
);
572 static HRESULT WINAPI
HTMLEventObj_get_clientX(IHTMLEventObj
*iface
, LONG
*p
)
574 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
577 TRACE("(%p)->(%p)\n", This
, p
);
580 nsIDOMMouseEvent
*mouse_event
;
583 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
584 if(NS_SUCCEEDED(nsres
)) {
585 nsIDOMMouseEvent_GetClientX(mouse_event
, &x
);
586 nsIDOMMouseEvent_Release(mouse_event
);
594 static HRESULT WINAPI
HTMLEventObj_get_clientY(IHTMLEventObj
*iface
, LONG
*p
)
596 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
599 TRACE("(%p)->(%p)\n", This
, p
);
602 nsIDOMMouseEvent
*mouse_event
;
605 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
606 if(NS_SUCCEEDED(nsres
)) {
607 nsIDOMMouseEvent_GetClientY(mouse_event
, &y
);
608 nsIDOMMouseEvent_Release(mouse_event
);
616 static HRESULT WINAPI
HTMLEventObj_get_offsetX(IHTMLEventObj
*iface
, LONG
*p
)
618 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
620 FIXME("(%p)->(%p)\n", This
, p
);
626 static HRESULT WINAPI
HTMLEventObj_get_offsetY(IHTMLEventObj
*iface
, LONG
*p
)
628 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
630 FIXME("(%p)->(%p)\n", This
, p
);
636 static HRESULT WINAPI
HTMLEventObj_get_screenX(IHTMLEventObj
*iface
, LONG
*p
)
638 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
641 TRACE("(%p)->(%p)\n", This
, p
);
644 nsIDOMMouseEvent
*mouse_event
;
647 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
648 if(NS_SUCCEEDED(nsres
)) {
649 nsIDOMMouseEvent_GetScreenX(mouse_event
, &x
);
650 nsIDOMMouseEvent_Release(mouse_event
);
658 static HRESULT WINAPI
HTMLEventObj_get_screenY(IHTMLEventObj
*iface
, LONG
*p
)
660 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
663 TRACE("(%p)->(%p)\n", This
, p
);
666 nsIDOMMouseEvent
*mouse_event
;
669 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
670 if(NS_SUCCEEDED(nsres
)) {
671 nsIDOMMouseEvent_GetScreenY(mouse_event
, &y
);
672 nsIDOMMouseEvent_Release(mouse_event
);
680 static HRESULT WINAPI
HTMLEventObj_get_srcFilter(IHTMLEventObj
*iface
, IDispatch
**p
)
682 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
684 FIXME("(%p)->(%p)\n", This
, p
);
690 #undef HTMLEVENTOBJ_THIS
692 static const IHTMLEventObjVtbl HTMLEventObjVtbl
= {
693 HTMLEventObj_QueryInterface
,
695 HTMLEventObj_Release
,
696 HTMLEventObj_GetTypeInfoCount
,
697 HTMLEventObj_GetTypeInfo
,
698 HTMLEventObj_GetIDsOfNames
,
700 HTMLEventObj_get_srcElement
,
701 HTMLEventObj_get_altKey
,
702 HTMLEventObj_get_ctrlKey
,
703 HTMLEventObj_get_shiftKey
,
704 HTMLEventObj_put_returnValue
,
705 HTMLEventObj_get_returnValue
,
706 HTMLEventObj_put_cancelBubble
,
707 HTMLEventObj_get_cancelBubble
,
708 HTMLEventObj_get_fromElement
,
709 HTMLEventObj_get_toElement
,
710 HTMLEventObj_put_keyCode
,
711 HTMLEventObj_get_keyCode
,
712 HTMLEventObj_get_button
,
713 HTMLEventObj_get_type
,
714 HTMLEventObj_get_qualifier
,
715 HTMLEventObj_get_reason
,
718 HTMLEventObj_get_clientX
,
719 HTMLEventObj_get_clientY
,
720 HTMLEventObj_get_offsetX
,
721 HTMLEventObj_get_offsetY
,
722 HTMLEventObj_get_screenX
,
723 HTMLEventObj_get_screenY
,
724 HTMLEventObj_get_srcFilter
727 static const tid_t HTMLEventObj_iface_tids
[] = {
732 static dispex_static_data_t HTMLEventObj_dispex
= {
736 HTMLEventObj_iface_tids
739 static IHTMLEventObj
*create_event(HTMLDOMNode
*target
, eventid_t eid
, nsIDOMEvent
*nsevent
)
743 ret
= heap_alloc(sizeof(*ret
));
747 ret
->lpIHTMLEventObjVtbl
= &HTMLEventObjVtbl
;
749 ret
->type
= event_info
+eid
;
751 ret
->nsevent
= nsevent
;
753 nsIDOMEvent_AddRef(nsevent
);
754 }else if(event_types
[event_info
[eid
].type
]) {
755 nsIDOMDocumentEvent
*doc_event
;
758 nsres
= nsIDOMHTMLDocument_QueryInterface(target
->doc
->nsdoc
, &IID_nsIDOMDocumentEvent
,
760 if(NS_SUCCEEDED(nsres
)) {
763 nsAString_InitDepend(&type_str
, event_types
[event_info
[eid
].type
]);
764 nsres
= nsIDOMDocumentEvent_CreateEvent(doc_event
, &type_str
, &ret
->nsevent
);
765 nsAString_Finish(&type_str
);
766 nsIDOMDocumentEvent_Release(doc_event
);
768 if(NS_FAILED(nsres
)) {
769 ERR("Could not create event: %08x\n", nsres
);
770 IHTMLEventObj_Release(HTMLEVENTOBJ(ret
));
775 ret
->target
= target
;
776 IHTMLDOMNode_AddRef(HTMLDOMNODE(target
));
778 init_dispex(&ret
->dispex
, (IUnknown
*)HTMLEVENTOBJ(ret
), &HTMLEventObj_dispex
);
780 return HTMLEVENTOBJ(ret
);
783 static HRESULT
call_cp_func(IDispatch
*disp
, DISPID dispid
)
785 DISPPARAMS dp
= {NULL
,NULL
,0,0};
791 V_VT(&vres
) = VT_EMPTY
;
792 memset(&ei
, 0, sizeof(ei
));
793 hres
= IDispatch_Invoke(disp
, dispid
, &IID_NULL
, 0, DISPATCH_METHOD
, &dp
, &vres
, &ei
, &argerr
);
794 if(SUCCEEDED(hres
) && V_VT(&vres
) != VT_EMPTY
) {
795 FIXME("handle result %s\n", debugstr_variant(&vres
));
802 static BOOL
is_cp_event(cp_static_data_t
*data
, DISPID dispid
)
811 hres
= get_dispids(data
->tid
, &data
->id_cnt
, &data
->ids
);
817 max
= data
->id_cnt
-1;
820 if(data
->ids
[i
] == dispid
)
823 if(data
->ids
[i
] < dispid
)
832 static void call_event_handlers(HTMLDocumentNode
*doc
, IHTMLEventObj
*event_obj
, event_target_t
*event_target
,
833 ConnectionPointContainer
*cp_container
, eventid_t eid
, IDispatch
*this_obj
)
835 handler_vector_t
*handler_vector
= NULL
;
840 handler_vector
= event_target
->event_table
[eid
];
842 if(handler_vector
&& handler_vector
->handler_prop
) {
843 DISPID named_arg
= DISPID_THIS
;
845 DISPPARAMS dp
= {&arg
, &named_arg
, 1, 1};
847 V_VT(&arg
) = VT_DISPATCH
;
848 V_DISPATCH(&arg
) = this_obj
;
850 TRACE("%s >>>\n", debugstr_w(event_info
[eid
].name
));
851 hres
= call_disp_func(handler_vector
->handler_prop
, &dp
);
853 TRACE("%s <<<\n", debugstr_w(event_info
[eid
].name
));
855 WARN("%s <<< %08x\n", debugstr_w(event_info
[eid
].name
), hres
);
858 if(handler_vector
&& handler_vector
->handler_cnt
) {
860 DISPPARAMS dp
= {&arg
, NULL
, 1, 0};
862 V_VT(&arg
) = VT_DISPATCH
;
863 V_DISPATCH(&arg
) = (IDispatch
*)event_obj
;
865 i
= handler_vector
->handler_cnt
;
867 if(handler_vector
->handlers
[i
]) {
868 TRACE("%s [%d] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
869 hres
= call_disp_func(handler_vector
->handlers
[i
], &dp
);
871 TRACE("%s [%d] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
873 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
881 if(cp_container
->forward_container
)
882 cp_container
= cp_container
->forward_container
;
884 for(cp
= cp_container
->cp_list
; cp
; cp
= cp
->next
) {
885 if(cp
->sinks_size
&& is_cp_event(cp
->data
, event_info
[eid
].dispid
)) {
886 for(i
=0; i
< cp
->sinks_size
; i
++) {
887 if(!cp
->sinks
[i
].disp
)
890 TRACE("cp %s [%d] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
891 hres
= call_cp_func(cp
->sinks
[i
].disp
, event_info
[eid
].dispid
);
893 TRACE("cp %s [%d] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
895 WARN("cp %s [%d] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
902 void fire_event(HTMLDocumentNode
*doc
, eventid_t eid
, BOOL set_event
, nsIDOMNode
*target
, nsIDOMEvent
*nsevent
)
904 IHTMLEventObj
*prev_event
, *event_obj
= NULL
;
905 nsIDOMNode
*parent
, *nsnode
;
909 TRACE("(%p) %s\n", doc
, debugstr_w(event_info
[eid
].name
));
911 prev_event
= doc
->basedoc
.window
->event
;
913 event_obj
= create_event(get_node(doc
, target
, TRUE
), eid
, nsevent
);
914 doc
->basedoc
.window
->event
= event_obj
;
916 nsIDOMNode_GetNodeType(target
, &node_type
);
918 nsIDOMNode_AddRef(nsnode
);
923 node
= get_node(doc
, nsnode
, FALSE
);
925 call_event_handlers(doc
, event_obj
, *get_node_event_target(node
), node
->cp_container
, eid
,
926 (IDispatch
*)HTMLDOMNODE(node
));
928 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
))
931 nsIDOMNode_GetParentNode(nsnode
, &parent
);
932 nsIDOMNode_Release(nsnode
);
937 nsIDOMNode_GetNodeType(nsnode
, &node_type
);
938 }while(node_type
== ELEMENT_NODE
);
940 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
))
944 if(event_info
[eid
].flags
& EVENT_FORWARDBODY
) {
945 nsIDOMHTMLElement
*nsbody
;
948 nsres
= nsIDOMHTMLDocument_GetBody(doc
->nsdoc
, &nsbody
);
949 if(NS_SUCCEEDED(nsres
) && nsbody
) {
950 node
= get_node(doc
, (nsIDOMNode
*)nsbody
, FALSE
);
952 call_event_handlers(doc
, event_obj
, *get_node_event_target(node
), node
->cp_container
,
953 eid
, (IDispatch
*)HTMLDOMNODE(node
));
954 nsIDOMHTMLElement_Release(nsbody
);
956 ERR("Could not get body: %08x\n", nsres
);
960 call_event_handlers(doc
, event_obj
, doc
->node
.event_target
, &doc
->basedoc
.cp_container
, eid
,
961 (IDispatch
*)HTMLDOC(&doc
->basedoc
));
965 FIXME("unimplemented node type %d\n", node_type
);
969 nsIDOMNode_Release(nsnode
);
972 IHTMLEventObj_Release(event_obj
);
973 doc
->basedoc
.window
->event
= prev_event
;
976 HRESULT
dispatch_event(HTMLDOMNode
*node
, const WCHAR
*event_name
, VARIANT
*event_obj
, VARIANT_BOOL
*cancelled
)
980 eid
= attr_to_eid(event_name
);
981 if(eid
== EVENTID_LAST
) {
982 WARN("unknown event %s\n", debugstr_w(event_name
));
986 if(event_obj
&& V_VT(event_obj
) != VT_EMPTY
&& V_VT(event_obj
) != VT_ERROR
)
987 FIXME("event_obj not implemented\n");
989 if(!(event_info
[eid
].flags
& EVENT_DEFAULTLISTENER
)) {
990 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
994 fire_event(node
->doc
, eid
, TRUE
, node
->nsnode
, NULL
);
996 *cancelled
= VARIANT_TRUE
; /* FIXME */
1000 HRESULT
call_event(HTMLDOMNode
*node
, eventid_t eid
)
1004 if(node
->vtbl
->call_event
) {
1005 BOOL handled
= FALSE
;
1007 hres
= node
->vtbl
->call_event(node
, eid
, &handled
);
1012 fire_event(node
->doc
, eid
, TRUE
, node
->nsnode
, NULL
);
1016 static inline event_target_t
*get_event_target(event_target_t
**event_target_ptr
)
1018 if(!*event_target_ptr
)
1019 *event_target_ptr
= heap_alloc_zero(sizeof(event_target_t
));
1020 return *event_target_ptr
;
1023 static BOOL
alloc_handler_vector(event_target_t
*event_target
, eventid_t eid
, int cnt
)
1025 handler_vector_t
*new_vector
, *handler_vector
= event_target
->event_table
[eid
];
1027 if(handler_vector
) {
1028 if(cnt
<= handler_vector
->handler_cnt
)
1031 new_vector
= heap_realloc_zero(handler_vector
, sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
1033 new_vector
= heap_alloc_zero(sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
1039 new_vector
->handler_cnt
= cnt
;
1040 event_target
->event_table
[eid
] = new_vector
;
1044 static HRESULT
ensure_nsevent_handler(HTMLDocumentNode
*doc
, event_target_t
*event_target
, nsIDOMNode
*nsnode
, eventid_t eid
)
1049 if(event_info
[eid
].flags
& EVENT_NODEHANDLER
) {
1052 mask
= get_node_handler_mask(eid
);
1053 if(event_target
->node_handlers_mask
& mask
)
1056 add_nsevent_listener(doc
, nsnode
, event_info
[eid
].name
);
1057 event_target
->node_handlers_mask
|= mask
;
1061 if(!(event_info
[eid
].flags
& EVENT_DEFAULTLISTENER
))
1064 if(!doc
->event_vector
) {
1065 doc
->event_vector
= heap_alloc_zero(EVENTID_LAST
*sizeof(BOOL
));
1066 if(!doc
->event_vector
)
1067 return E_OUTOFMEMORY
;
1070 if(!doc
->event_vector
[eid
]) {
1071 doc
->event_vector
[eid
] = TRUE
;
1072 add_nsevent_listener(doc
, NULL
, event_info
[eid
].name
);
1078 static HRESULT
remove_event_handler(event_target_t
**event_target
, eventid_t eid
)
1080 if(*event_target
&& (*event_target
)->event_table
[eid
] && (*event_target
)->event_table
[eid
]->handler_prop
) {
1081 IDispatch_Release((*event_target
)->event_table
[eid
]->handler_prop
);
1082 (*event_target
)->event_table
[eid
]->handler_prop
= NULL
;
1088 static HRESULT
set_event_handler_disp(event_target_t
**event_target_ptr
, nsIDOMNode
*nsnode
, HTMLDocumentNode
*doc
,
1089 eventid_t eid
, IDispatch
*disp
)
1091 event_target_t
*event_target
;
1094 return remove_event_handler(event_target_ptr
, eid
);
1096 event_target
= get_event_target(event_target_ptr
);
1098 return E_OUTOFMEMORY
;
1100 if(!alloc_handler_vector(event_target
, eid
, 0))
1101 return E_OUTOFMEMORY
;
1103 if(event_target
->event_table
[eid
]->handler_prop
)
1104 IDispatch_Release(event_target
->event_table
[eid
]->handler_prop
);
1106 event_target
->event_table
[eid
]->handler_prop
= disp
;
1107 IDispatch_AddRef(disp
);
1109 return ensure_nsevent_handler(doc
, event_target
, nsnode
, eid
);
1112 HRESULT
set_event_handler(event_target_t
**event_target
, nsIDOMNode
*nsnode
, HTMLDocumentNode
*doc
, eventid_t eid
, VARIANT
*var
)
1116 return remove_event_handler(event_target
, eid
);
1119 return set_event_handler_disp(event_target
, nsnode
, doc
, eid
, V_DISPATCH(var
));
1122 FIXME("not supported vt=%d\n", V_VT(var
));
1130 HRESULT
get_event_handler(event_target_t
**event_target
, eventid_t eid
, VARIANT
*var
)
1132 if(*event_target
&& (*event_target
)->event_table
[eid
] && (*event_target
)->event_table
[eid
]->handler_prop
) {
1133 V_VT(var
) = VT_DISPATCH
;
1134 V_DISPATCH(var
) = (*event_target
)->event_table
[eid
]->handler_prop
;
1135 IDispatch_AddRef(V_DISPATCH(var
));
1137 V_VT(var
) = VT_NULL
;
1143 HRESULT
attach_event(event_target_t
**event_target_ptr
, nsIDOMNode
*nsnode
, HTMLDocument
*doc
, BSTR name
,
1144 IDispatch
*disp
, VARIANT_BOOL
*res
)
1146 event_target_t
*event_target
;
1150 eid
= attr_to_eid(name
);
1151 if(eid
== EVENTID_LAST
) {
1152 WARN("Unknown event\n");
1153 *res
= VARIANT_TRUE
;
1157 event_target
= get_event_target(event_target_ptr
);
1159 return E_OUTOFMEMORY
;
1161 if(event_target
->event_table
[eid
]) {
1162 while(i
< event_target
->event_table
[eid
]->handler_cnt
&& event_target
->event_table
[eid
]->handlers
[i
])
1164 if(i
== event_target
->event_table
[eid
]->handler_cnt
&& !alloc_handler_vector(event_target
, eid
, i
+1))
1165 return E_OUTOFMEMORY
;
1166 }else if(!alloc_handler_vector(event_target
, eid
, i
+1)) {
1167 return E_OUTOFMEMORY
;
1170 IDispatch_AddRef(disp
);
1171 event_target
->event_table
[eid
]->handlers
[i
] = disp
;
1173 *res
= VARIANT_TRUE
;
1174 return ensure_nsevent_handler(doc
->doc_node
, event_target
, nsnode
, eid
);
1177 HRESULT
detach_event(event_target_t
*event_target
, HTMLDocument
*doc
, BSTR name
, IDispatch
*disp
)
1185 eid
= attr_to_eid(name
);
1186 if(eid
== EVENTID_LAST
) {
1187 WARN("Unknown event\n");
1191 if(!event_target
->event_table
[eid
])
1194 while(i
< event_target
->event_table
[eid
]->handler_cnt
) {
1195 if(event_target
->event_table
[eid
]->handlers
[i
] == disp
) {
1196 IDispatch_Release(event_target
->event_table
[eid
]->handlers
[i
]);
1197 event_target
->event_table
[eid
]->handlers
[i
] = NULL
;
1205 void update_cp_events(HTMLWindow
*window
, event_target_t
**event_target_ptr
, cp_static_data_t
*cp
, nsIDOMNode
*nsnode
)
1207 event_target_t
*event_target
;
1210 event_target
= get_event_target(event_target_ptr
);
1214 for(i
=0; i
< EVENTID_LAST
; i
++) {
1215 if((event_info
[i
].flags
& EVENT_DEFAULTLISTENER
) && is_cp_event(cp
, event_info
[i
].dispid
))
1216 ensure_nsevent_handler(window
->doc
, event_target
, nsnode
, i
);
1220 void check_event_attr(HTMLDocumentNode
*doc
, nsIDOMElement
*nselem
)
1222 const PRUnichar
*attr_value
;
1223 nsAString attr_name_str
, attr_value_str
;
1229 nsAString_Init(&attr_value_str
, NULL
);
1230 nsAString_Init(&attr_name_str
, NULL
);
1232 for(i
=0; i
< EVENTID_LAST
; i
++) {
1233 nsAString_SetData(&attr_name_str
, event_info
[i
].attr_name
);
1234 nsres
= nsIDOMElement_GetAttribute(nselem
, &attr_name_str
, &attr_value_str
);
1235 if(NS_SUCCEEDED(nsres
)) {
1236 nsAString_GetData(&attr_value_str
, &attr_value
);
1240 TRACE("%p.%s = %s\n", nselem
, debugstr_w(event_info
[i
].attr_name
), debugstr_w(attr_value
));
1242 disp
= script_parse_event(doc
->basedoc
.window
, attr_value
);
1244 node
= get_node(doc
, (nsIDOMNode
*)nselem
, TRUE
);
1245 set_event_handler_disp(get_node_event_target(node
), node
->nsnode
, node
->doc
, i
, disp
);
1246 IDispatch_Release(disp
);
1251 nsAString_Finish(&attr_value_str
);
1252 nsAString_Finish(&attr_name_str
);
1255 void release_event_target(event_target_t
*event_target
)
1259 for(i
=0; i
< EVENTID_LAST
; i
++) {
1260 if(event_target
->event_table
[i
]) {
1261 if(event_target
->event_table
[i
]->handler_prop
)
1262 IDispatch_Release(event_target
->event_table
[i
]->handler_prop
);
1263 for(j
=0; j
< event_target
->event_table
[i
]->handler_cnt
; j
++)
1264 IDispatch_Release(event_target
->event_table
[i
]->handlers
[j
]);
1268 heap_free(event_target
);