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
30 #include "mshtml_private.h"
31 #include "htmlevent.h"
32 #include "htmlscript.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
39 IDispatch
*handler_prop
;
41 IDispatch
*handlers
[0];
44 struct event_target_t
{
45 DWORD node_handlers_mask
;
46 handler_vector_t
*event_table
[EVENTID_LAST
];
49 static const WCHAR abortW
[] = {'a','b','o','r','t',0};
50 static const WCHAR onabortW
[] = {'o','n','a','b','o','r','t',0};
52 static const WCHAR beforeunloadW
[] = {'b','e','f','o','r','e','u','n','l','o','a','d',0};
53 static const WCHAR onbeforeunloadW
[] = {'o','n','b','e','f','o','r','e','u','n','l','o','a','d',0};
55 static const WCHAR blurW
[] = {'b','l','u','r',0};
56 static const WCHAR onblurW
[] = {'o','n','b','l','u','r',0};
58 static const WCHAR changeW
[] = {'c','h','a','n','g','e',0};
59 static const WCHAR onchangeW
[] = {'o','n','c','h','a','n','g','e',0};
61 static const WCHAR clickW
[] = {'c','l','i','c','k',0};
62 static const WCHAR onclickW
[] = {'o','n','c','l','i','c','k',0};
64 static const WCHAR contextmenuW
[] = {'c','o','n','t','e','x','t','m','e','n','u',0};
65 static const WCHAR oncontextmenuW
[] = {'o','n','c','o','n','t','e','x','t','m','e','n','u',0};
67 static const WCHAR dataavailableW
[] = {'d','a','t','a','a','v','a','i','l','a','b','l','e',0};
68 static const WCHAR ondataavailableW
[] = {'o','n','d','a','t','a','a','v','a','i','l','a','b','l','e',0};
70 static const WCHAR dblclickW
[] = {'d','b','l','c','l','i','c','k',0};
71 static const WCHAR ondblclickW
[] = {'o','n','d','b','l','c','l','i','c','k',0};
73 static const WCHAR dragW
[] = {'d','r','a','g',0};
74 static const WCHAR ondragW
[] = {'o','n','d','r','a','g',0};
76 static const WCHAR dragstartW
[] = {'d','r','a','g','s','t','a','r','t',0};
77 static const WCHAR ondragstartW
[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
79 static const WCHAR errorW
[] = {'e','r','r','o','r',0};
80 static const WCHAR onerrorW
[] = {'o','n','e','r','r','o','r',0};
82 static const WCHAR focusW
[] = {'f','o','c','u','s',0};
83 static const WCHAR onfocusW
[] = {'o','n','f','o','c','u','s',0};
85 static const WCHAR helpW
[] = {'h','e','l','p',0};
86 static const WCHAR onhelpW
[] = {'o','n','h','e','l','p',0};
88 static const WCHAR keydownW
[] = {'k','e','y','d','o','w','n',0};
89 static const WCHAR onkeydownW
[] = {'o','n','k','e','y','d','o','w','n',0};
91 static const WCHAR keypressW
[] = {'k','e','y','p','r','e','s','s',0};
92 static const WCHAR onkeypressW
[] = {'o','n','k','e','y','p','r','e','s','s',0};
94 static const WCHAR keyupW
[] = {'k','e','y','u','p',0};
95 static const WCHAR onkeyupW
[] = {'o','n','k','e','y','u','p',0};
97 static const WCHAR loadW
[] = {'l','o','a','d',0};
98 static const WCHAR onloadW
[] = {'o','n','l','o','a','d',0};
100 static const WCHAR mousedownW
[] = {'m','o','u','s','e','d','o','w','n',0};
101 static const WCHAR onmousedownW
[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
103 static const WCHAR mousemoveW
[] = {'m','o','u','s','e','m','o','v','e',0};
104 static const WCHAR onmousemoveW
[] = {'o','n','m','o','u','s','e','m','o','v','e',0};
106 static const WCHAR mouseoutW
[] = {'m','o','u','s','e','o','u','t',0};
107 static const WCHAR onmouseoutW
[] = {'o','n','m','o','u','s','e','o','u','t',0};
109 static const WCHAR mouseoverW
[] = {'m','o','u','s','e','o','v','e','r',0};
110 static const WCHAR onmouseoverW
[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
112 static const WCHAR mouseupW
[] = {'m','o','u','s','e','u','p',0};
113 static const WCHAR onmouseupW
[] = {'o','n','m','o','u','s','e','u','p',0};
115 static const WCHAR pasteW
[] = {'p','a','s','t','e',0};
116 static const WCHAR onpasteW
[] = {'o','n','p','a','s','t','e',0};
118 static const WCHAR readystatechangeW
[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
119 static const WCHAR onreadystatechangeW
[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
121 static const WCHAR resizeW
[] = {'r','e','s','i','z','e',0};
122 static const WCHAR onresizeW
[] = {'o','n','r','e','s','i','z','e',0};
124 static const WCHAR scrollW
[] = {'s','c','r','o','l','l',0};
125 static const WCHAR onscrollW
[] = {'o','n','s','c','r','o','l','l',0};
127 static const WCHAR selectstartW
[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
128 static const WCHAR onselectstartW
[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
130 static const WCHAR submitW
[] = {'s','u','b','m','i','t',0};
131 static const WCHAR onsubmitW
[] = {'o','n','s','u','b','m','i','t',0};
133 static const WCHAR HTMLEventsW
[] = {'H','T','M','L','E','v','e','n','t','s',0};
134 static const WCHAR KeyboardEventW
[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
135 static const WCHAR MouseEventW
[] = {'M','o','u','s','e','E','v','e','n','t',0};
144 static const WCHAR
*event_types
[] = {
159 #define EVENT_DEFAULTLISTENER 0x0001
160 #define EVENT_BUBBLE 0x0002
161 #define EVENT_FORWARDBODY 0x0004
162 #define EVENT_NODEHANDLER 0x0008
163 #define EVENT_CANCELABLE 0x0010
164 #define EVENT_HASDEFAULTHANDLERS 0x0020
166 static const event_info_t event_info
[] = {
167 {abortW
, onabortW
, EVENTT_NONE
, DISPID_EVMETH_ONABORT
,
169 {beforeunloadW
, onbeforeunloadW
, EVENTT_NONE
, DISPID_EVMETH_ONBEFOREUNLOAD
,
170 EVENT_DEFAULTLISTENER
|EVENT_FORWARDBODY
},
171 {blurW
, onblurW
, EVENTT_HTML
, DISPID_EVMETH_ONBLUR
,
172 EVENT_DEFAULTLISTENER
},
173 {changeW
, onchangeW
, EVENTT_HTML
, DISPID_EVMETH_ONCHANGE
,
174 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
175 {clickW
, onclickW
, EVENTT_MOUSE
, DISPID_EVMETH_ONCLICK
,
176 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
|EVENT_HASDEFAULTHANDLERS
},
177 {contextmenuW
, oncontextmenuW
, EVENTT_MOUSE
, DISPID_EVMETH_ONCONTEXTMENU
,
178 EVENT_BUBBLE
|EVENT_CANCELABLE
},
179 {dataavailableW
, ondataavailableW
, EVENTT_NONE
, DISPID_EVMETH_ONDATAAVAILABLE
,
181 {dblclickW
, ondblclickW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDBLCLICK
,
182 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
},
183 {dragW
, ondragW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDRAG
,
185 {dragstartW
, ondragstartW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDRAGSTART
,
187 {errorW
, onerrorW
, EVENTT_NONE
, DISPID_EVMETH_ONERROR
,
189 {focusW
, onfocusW
, EVENTT_HTML
, DISPID_EVMETH_ONFOCUS
,
190 EVENT_DEFAULTLISTENER
},
191 {helpW
, onhelpW
, EVENTT_KEY
, DISPID_EVMETH_ONHELP
,
192 EVENT_BUBBLE
|EVENT_CANCELABLE
},
193 {keydownW
, onkeydownW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYDOWN
,
194 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_HASDEFAULTHANDLERS
},
195 {keypressW
, onkeypressW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYPRESS
,
196 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
197 {keyupW
, onkeyupW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYUP
,
198 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
199 {loadW
, onloadW
, EVENTT_HTML
, DISPID_EVMETH_ONLOAD
,
201 {mousedownW
, onmousedownW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEDOWN
,
202 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
203 {mousemoveW
, onmousemoveW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEMOVE
,
204 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
205 {mouseoutW
, onmouseoutW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEOUT
,
206 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
207 {mouseoverW
, onmouseoverW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEOVER
,
208 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
209 {mouseupW
, onmouseupW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEUP
,
210 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
211 {pasteW
, onpasteW
, EVENTT_NONE
, DISPID_EVMETH_ONPASTE
,
213 {readystatechangeW
, onreadystatechangeW
, EVENTT_NONE
, DISPID_EVMETH_ONREADYSTATECHANGE
,
215 {resizeW
, onresizeW
, EVENTT_NONE
, DISPID_EVMETH_ONRESIZE
,
216 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
217 {scrollW
, onscrollW
, EVENTT_HTML
, DISPID_EVMETH_ONSCROLL
,
218 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
219 {selectstartW
, onselectstartW
, EVENTT_MOUSE
, DISPID_EVMETH_ONSELECTSTART
,
221 {submitW
, onsubmitW
, EVENTT_HTML
, DISPID_EVMETH_ONSUBMIT
,
222 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
}
225 static const eventid_t node_handled_list
[] = { EVENTID_ABORT
, EVENTID_ERROR
, EVENTID_LOAD
};
227 eventid_t
str_to_eid(LPCWSTR str
)
231 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
232 if(!strcmpW(event_info
[i
].name
, str
))
236 ERR("unknown type %s\n", debugstr_w(str
));
240 static eventid_t
attr_to_eid(LPCWSTR str
)
244 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
245 if(!strcmpW(event_info
[i
].attr_name
, str
))
252 static DWORD
get_node_handler_mask(eventid_t eid
)
256 for(i
=0; i
<sizeof(node_handled_list
)/sizeof(*node_handled_list
); i
++) {
257 if(node_handled_list
[i
] == eid
)
261 ERR("Invalid eid %d\n", eid
);
267 IHTMLEventObj IHTMLEventObj_iface
;
272 const event_info_t
*type
;
273 nsIDOMEvent
*nsevent
;
274 BOOL prevent_default
;
278 static inline HTMLEventObj
*impl_from_IHTMLEventObj(IHTMLEventObj
*iface
)
280 return CONTAINING_RECORD(iface
, HTMLEventObj
, IHTMLEventObj_iface
);
283 static HRESULT WINAPI
HTMLEventObj_QueryInterface(IHTMLEventObj
*iface
, REFIID riid
, void **ppv
)
285 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
289 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
290 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
291 *ppv
= &This
->IHTMLEventObj_iface
;
292 }else if(IsEqualGUID(&IID_IHTMLEventObj
, riid
)) {
293 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This
, ppv
);
294 *ppv
= &This
->IHTMLEventObj_iface
;
295 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
296 return *ppv
? S_OK
: E_NOINTERFACE
;
300 IUnknown_AddRef((IUnknown
*)*ppv
);
304 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
305 return E_NOINTERFACE
;
308 static ULONG WINAPI
HTMLEventObj_AddRef(IHTMLEventObj
*iface
)
310 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
311 LONG ref
= InterlockedIncrement(&This
->ref
);
313 TRACE("(%p) ref=%d\n", This
, ref
);
318 static ULONG WINAPI
HTMLEventObj_Release(IHTMLEventObj
*iface
)
320 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
321 LONG ref
= InterlockedDecrement(&This
->ref
);
323 TRACE("(%p) ref=%d\n", This
, ref
);
327 IHTMLDOMNode_Release(&This
->target
->IHTMLDOMNode_iface
);
329 nsIDOMEvent_Release(This
->nsevent
);
330 release_dispex(&This
->dispex
);
337 static HRESULT WINAPI
HTMLEventObj_GetTypeInfoCount(IHTMLEventObj
*iface
, UINT
*pctinfo
)
339 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
340 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
343 static HRESULT WINAPI
HTMLEventObj_GetTypeInfo(IHTMLEventObj
*iface
, UINT iTInfo
,
344 LCID lcid
, ITypeInfo
**ppTInfo
)
346 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
347 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
350 static HRESULT WINAPI
HTMLEventObj_GetIDsOfNames(IHTMLEventObj
*iface
, REFIID riid
,
351 LPOLESTR
*rgszNames
, UINT cNames
,
352 LCID lcid
, DISPID
*rgDispId
)
354 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
355 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
359 static HRESULT WINAPI
HTMLEventObj_Invoke(IHTMLEventObj
*iface
, DISPID dispIdMember
,
360 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
361 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
363 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
364 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
365 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
368 static HRESULT WINAPI
HTMLEventObj_get_srcElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
370 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
372 TRACE("(%p)->(%p)\n", This
, p
);
376 IHTMLDOMNode_QueryInterface(&This
->target
->IHTMLDOMNode_iface
, &IID_IHTMLElement
, (void**)p
);
380 static HRESULT WINAPI
HTMLEventObj_get_altKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
382 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
383 cpp_bool ret
= FALSE
;
385 TRACE("(%p)->(%p)\n", This
, p
);
388 nsIDOMKeyEvent
*key_event
;
391 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
392 if(NS_SUCCEEDED(nsres
)) {
393 nsIDOMKeyEvent_GetAltKey(key_event
, &ret
);
394 nsIDOMKeyEvent_Release(key_event
);
396 nsIDOMMouseEvent
*mouse_event
;
398 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
399 if(NS_SUCCEEDED(nsres
)) {
400 nsIDOMMouseEvent_GetAltKey(mouse_event
, &ret
);
401 nsIDOMMouseEvent_Release(mouse_event
);
406 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
410 static HRESULT WINAPI
HTMLEventObj_get_ctrlKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
412 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
413 cpp_bool ret
= FALSE
;
415 TRACE("(%p)->(%p)\n", This
, p
);
418 nsIDOMKeyEvent
*key_event
;
421 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
422 if(NS_SUCCEEDED(nsres
)) {
423 nsIDOMKeyEvent_GetCtrlKey(key_event
, &ret
);
424 nsIDOMKeyEvent_Release(key_event
);
426 nsIDOMMouseEvent
*mouse_event
;
428 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
429 if(NS_SUCCEEDED(nsres
)) {
430 nsIDOMMouseEvent_GetCtrlKey(mouse_event
, &ret
);
431 nsIDOMMouseEvent_Release(mouse_event
);
436 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
440 static HRESULT WINAPI
HTMLEventObj_get_shiftKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
442 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
443 cpp_bool ret
= FALSE
;
445 TRACE("(%p)->(%p)\n", This
, p
);
448 nsIDOMKeyEvent
*key_event
;
451 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
452 if(NS_SUCCEEDED(nsres
)) {
453 nsIDOMKeyEvent_GetShiftKey(key_event
, &ret
);
454 nsIDOMKeyEvent_Release(key_event
);
456 nsIDOMMouseEvent
*mouse_event
;
458 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
459 if(NS_SUCCEEDED(nsres
)) {
460 nsIDOMMouseEvent_GetShiftKey(mouse_event
, &ret
);
461 nsIDOMMouseEvent_Release(mouse_event
);
466 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
470 static HRESULT WINAPI
HTMLEventObj_put_returnValue(IHTMLEventObj
*iface
, VARIANT v
)
472 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
474 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&v
));
476 if(V_VT(&v
) != VT_BOOL
) {
477 FIXME("unsupported value %s\n", debugstr_variant(&v
));
478 return DISP_E_BADVARTYPE
;
482 This
->prevent_default
= TRUE
;
486 static HRESULT WINAPI
HTMLEventObj_get_returnValue(IHTMLEventObj
*iface
, VARIANT
*p
)
488 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
490 FIXME("(%p)->(%p)\n", This
, p
);
496 static HRESULT WINAPI
HTMLEventObj_put_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL v
)
498 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
500 TRACE("(%p)->(%x)\n", This
, v
);
502 This
->cancel_bubble
= !!v
;
506 static HRESULT WINAPI
HTMLEventObj_get_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
508 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
510 TRACE("(%p)->(%p)\n", This
, p
);
512 *p
= This
->cancel_bubble
? VARIANT_TRUE
: VARIANT_FALSE
;
516 static HRESULT WINAPI
HTMLEventObj_get_fromElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
518 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
520 FIXME("(%p)->(%p)\n", This
, p
);
526 static HRESULT WINAPI
HTMLEventObj_get_toElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
528 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
530 FIXME("(%p)->(%p)\n", This
, p
);
536 static HRESULT WINAPI
HTMLEventObj_put_keyCode(IHTMLEventObj
*iface
, LONG v
)
538 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
539 FIXME("(%p)->(%d)\n", This
, v
);
543 static HRESULT WINAPI
HTMLEventObj_get_keyCode(IHTMLEventObj
*iface
, LONG
*p
)
545 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
548 TRACE("(%p)->(%p)\n", This
, p
);
551 nsIDOMKeyEvent
*key_event
;
554 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
555 if(NS_SUCCEEDED(nsres
)) {
556 nsIDOMKeyEvent_GetKeyCode(key_event
, &key_code
);
557 nsIDOMKeyEvent_Release(key_event
);
565 static HRESULT WINAPI
HTMLEventObj_get_button(IHTMLEventObj
*iface
, LONG
*p
)
567 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
570 TRACE("(%p)->(%p)\n", This
, p
);
573 nsIDOMMouseEvent
*mouse_event
;
576 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
577 if(NS_SUCCEEDED(nsres
)) {
578 nsIDOMMouseEvent_GetButton(mouse_event
, &button
);
579 nsIDOMMouseEvent_Release(mouse_event
);
587 static HRESULT WINAPI
HTMLEventObj_get_type(IHTMLEventObj
*iface
, BSTR
*p
)
589 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
591 TRACE("(%p)->(%p)\n", This
, p
);
598 *p
= SysAllocString(This
->type
->name
);
599 return *p
? S_OK
: E_OUTOFMEMORY
;
602 static HRESULT WINAPI
HTMLEventObj_get_qualifier(IHTMLEventObj
*iface
, BSTR
*p
)
604 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
606 FIXME("(%p)->(%p)\n", This
, p
);
612 static HRESULT WINAPI
HTMLEventObj_get_reason(IHTMLEventObj
*iface
, LONG
*p
)
614 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
616 FIXME("(%p)->(%p)\n", This
, p
);
622 static HRESULT WINAPI
HTMLEventObj_get_x(IHTMLEventObj
*iface
, LONG
*p
)
624 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
627 TRACE("(%p)->(%p)\n", This
, p
);
630 nsIDOMUIEvent
*ui_event
;
633 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMUIEvent
, (void**)&ui_event
);
634 if(NS_SUCCEEDED(nsres
)) {
635 /* NOTE: pageX is not exactly right here. */
636 nsres
= nsIDOMUIEvent_GetPageX(ui_event
, &x
);
637 assert(nsres
== NS_OK
);
638 nsIDOMUIEvent_Release(ui_event
);
646 static HRESULT WINAPI
HTMLEventObj_get_y(IHTMLEventObj
*iface
, LONG
*p
)
648 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
651 TRACE("(%p)->(%p)\n", This
, p
);
654 nsIDOMUIEvent
*ui_event
;
657 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMUIEvent
, (void**)&ui_event
);
658 if(NS_SUCCEEDED(nsres
)) {
659 /* NOTE: pageY is not exactly right here. */
660 nsres
= nsIDOMUIEvent_GetPageY(ui_event
, &y
);
661 assert(nsres
== NS_OK
);
662 nsIDOMUIEvent_Release(ui_event
);
670 static HRESULT WINAPI
HTMLEventObj_get_clientX(IHTMLEventObj
*iface
, LONG
*p
)
672 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
675 TRACE("(%p)->(%p)\n", This
, p
);
678 nsIDOMMouseEvent
*mouse_event
;
681 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
682 if(NS_SUCCEEDED(nsres
)) {
683 nsIDOMMouseEvent_GetClientX(mouse_event
, &x
);
684 nsIDOMMouseEvent_Release(mouse_event
);
692 static HRESULT WINAPI
HTMLEventObj_get_clientY(IHTMLEventObj
*iface
, LONG
*p
)
694 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
697 TRACE("(%p)->(%p)\n", This
, p
);
700 nsIDOMMouseEvent
*mouse_event
;
703 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
704 if(NS_SUCCEEDED(nsres
)) {
705 nsIDOMMouseEvent_GetClientY(mouse_event
, &y
);
706 nsIDOMMouseEvent_Release(mouse_event
);
714 static HRESULT WINAPI
HTMLEventObj_get_offsetX(IHTMLEventObj
*iface
, LONG
*p
)
716 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
718 FIXME("(%p)->(%p)\n", This
, p
);
724 static HRESULT WINAPI
HTMLEventObj_get_offsetY(IHTMLEventObj
*iface
, LONG
*p
)
726 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
728 FIXME("(%p)->(%p)\n", This
, p
);
734 static HRESULT WINAPI
HTMLEventObj_get_screenX(IHTMLEventObj
*iface
, LONG
*p
)
736 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
739 TRACE("(%p)->(%p)\n", This
, p
);
742 nsIDOMMouseEvent
*mouse_event
;
745 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
746 if(NS_SUCCEEDED(nsres
)) {
747 nsIDOMMouseEvent_GetScreenX(mouse_event
, &x
);
748 nsIDOMMouseEvent_Release(mouse_event
);
756 static HRESULT WINAPI
HTMLEventObj_get_screenY(IHTMLEventObj
*iface
, LONG
*p
)
758 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
761 TRACE("(%p)->(%p)\n", This
, p
);
764 nsIDOMMouseEvent
*mouse_event
;
767 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
768 if(NS_SUCCEEDED(nsres
)) {
769 nsIDOMMouseEvent_GetScreenY(mouse_event
, &y
);
770 nsIDOMMouseEvent_Release(mouse_event
);
778 static HRESULT WINAPI
HTMLEventObj_get_srcFilter(IHTMLEventObj
*iface
, IDispatch
**p
)
780 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
782 FIXME("(%p)->(%p)\n", This
, p
);
788 static const IHTMLEventObjVtbl HTMLEventObjVtbl
= {
789 HTMLEventObj_QueryInterface
,
791 HTMLEventObj_Release
,
792 HTMLEventObj_GetTypeInfoCount
,
793 HTMLEventObj_GetTypeInfo
,
794 HTMLEventObj_GetIDsOfNames
,
796 HTMLEventObj_get_srcElement
,
797 HTMLEventObj_get_altKey
,
798 HTMLEventObj_get_ctrlKey
,
799 HTMLEventObj_get_shiftKey
,
800 HTMLEventObj_put_returnValue
,
801 HTMLEventObj_get_returnValue
,
802 HTMLEventObj_put_cancelBubble
,
803 HTMLEventObj_get_cancelBubble
,
804 HTMLEventObj_get_fromElement
,
805 HTMLEventObj_get_toElement
,
806 HTMLEventObj_put_keyCode
,
807 HTMLEventObj_get_keyCode
,
808 HTMLEventObj_get_button
,
809 HTMLEventObj_get_type
,
810 HTMLEventObj_get_qualifier
,
811 HTMLEventObj_get_reason
,
814 HTMLEventObj_get_clientX
,
815 HTMLEventObj_get_clientY
,
816 HTMLEventObj_get_offsetX
,
817 HTMLEventObj_get_offsetY
,
818 HTMLEventObj_get_screenX
,
819 HTMLEventObj_get_screenY
,
820 HTMLEventObj_get_srcFilter
823 static inline HTMLEventObj
*unsafe_impl_from_IHTMLEventObj(IHTMLEventObj
*iface
)
825 return iface
->lpVtbl
== &HTMLEventObjVtbl
? impl_from_IHTMLEventObj(iface
) : NULL
;
828 static const tid_t HTMLEventObj_iface_tids
[] = {
833 static dispex_static_data_t HTMLEventObj_dispex
= {
837 HTMLEventObj_iface_tids
840 static HTMLEventObj
*create_event(void)
844 ret
= heap_alloc_zero(sizeof(*ret
));
848 ret
->IHTMLEventObj_iface
.lpVtbl
= &HTMLEventObjVtbl
;
851 init_dispex(&ret
->dispex
, (IUnknown
*)&ret
->IHTMLEventObj_iface
, &HTMLEventObj_dispex
);
856 static HRESULT
set_event_info(HTMLEventObj
*event
, HTMLDOMNode
*target
, eventid_t eid
, nsIDOMEvent
*nsevent
)
858 event
->type
= event_info
+eid
;
859 event
->nsevent
= nsevent
;
862 nsIDOMEvent_AddRef(nsevent
);
863 }else if(event_types
[event_info
[eid
].type
]) {
867 nsAString_InitDepend(&type_str
, event_types
[event_info
[eid
].type
]);
868 nsres
= nsIDOMHTMLDocument_CreateEvent(target
->doc
->nsdoc
, &type_str
, &event
->nsevent
);
869 nsAString_Finish(&type_str
);
870 if(NS_FAILED(nsres
)) {
871 ERR("Could not create event: %08x\n", nsres
);
876 event
->target
= target
;
878 IHTMLDOMNode_AddRef(&target
->IHTMLDOMNode_iface
);
882 HRESULT
create_event_obj(IHTMLEventObj
**ret
)
886 event
= create_event();
888 return E_OUTOFMEMORY
;
890 *ret
= &event
->IHTMLEventObj_iface
;
894 static HRESULT
call_disp_func(IDispatch
*disp
, DISPPARAMS
*dp
, VARIANT
*retv
)
900 memset(&ei
, 0, sizeof(ei
));
902 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
903 if(SUCCEEDED(hres
)) {
904 hres
= IDispatchEx_InvokeEx(dispex
, 0, GetUserDefaultLCID(), DISPATCH_METHOD
, dp
, retv
, &ei
, NULL
);
905 IDispatchEx_Release(dispex
);
907 TRACE("Could not get IDispatchEx interface: %08x\n", hres
);
908 hres
= IDispatch_Invoke(disp
, 0, &IID_NULL
, GetUserDefaultLCID(), DISPATCH_METHOD
,
909 dp
, retv
, &ei
, NULL
);
915 static HRESULT
call_cp_func(IDispatch
*disp
, DISPID dispid
, HTMLEventObj
*event_obj
, VARIANT
*retv
)
917 DISPPARAMS dp
= {NULL
,NULL
,0,0};
923 V_VT(&event_arg
) = VT_DISPATCH
;
924 V_DISPATCH(&event_arg
) = (IDispatch
*)&event_obj
->IHTMLEventObj_iface
;
925 dp
.rgvarg
= &event_arg
;
929 memset(&ei
, 0, sizeof(ei
));
930 return IDispatch_Invoke(disp
, dispid
, &IID_NULL
, 0, DISPATCH_METHOD
, &dp
, retv
, &ei
, &argerr
);
933 static BOOL
is_cp_event(cp_static_data_t
*data
, DISPID dispid
)
942 hres
= get_dispids(data
->tid
, &data
->id_cnt
, &data
->ids
);
948 max
= data
->id_cnt
-1;
951 if(data
->ids
[i
] == dispid
)
954 if(data
->ids
[i
] < dispid
)
963 static void call_event_handlers(HTMLDocumentNode
*doc
, HTMLEventObj
*event_obj
, event_target_t
*event_target
,
964 ConnectionPointContainer
*cp_container
, eventid_t eid
, IDispatch
*this_obj
)
966 const BOOL cancelable
= event_info
[eid
].flags
& EVENT_CANCELABLE
;
967 handler_vector_t
*handler_vector
= NULL
;
972 handler_vector
= event_target
->event_table
[eid
];
974 if(handler_vector
&& handler_vector
->handler_prop
) {
975 DISPID named_arg
= DISPID_THIS
;
977 DISPPARAMS dp
= {&arg
, &named_arg
, 1, 1};
979 V_VT(&arg
) = VT_DISPATCH
;
980 V_DISPATCH(&arg
) = this_obj
;
983 TRACE("%s >>>\n", debugstr_w(event_info
[eid
].name
));
984 hres
= call_disp_func(handler_vector
->handler_prop
, &dp
, &v
);
986 TRACE("%s <<< %s\n", debugstr_w(event_info
[eid
].name
), debugstr_variant(&v
));
989 if(V_VT(&v
) == VT_BOOL
) {
991 event_obj
->prevent_default
= TRUE
;
992 }else if(V_VT(&v
) != VT_EMPTY
) {
993 FIXME("unhandled result %s\n", debugstr_variant(&v
));
998 WARN("%s <<< %08x\n", debugstr_w(event_info
[eid
].name
), hres
);
1002 if(handler_vector
&& handler_vector
->handler_cnt
) {
1004 DISPPARAMS dp
= {&arg
, NULL
, 1, 0};
1007 V_VT(&arg
) = VT_DISPATCH
;
1008 V_DISPATCH(&arg
) = (IDispatch
*)event_obj
;
1010 i
= handler_vector
->handler_cnt
;
1012 if(handler_vector
->handlers
[i
]) {
1013 V_VT(&v
) = VT_EMPTY
;
1015 TRACE("%s [%d] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
1016 hres
= call_disp_func(handler_vector
->handlers
[i
], &dp
, &v
);
1018 TRACE("%s [%d] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
1021 if(V_VT(&v
) == VT_BOOL
) {
1023 event_obj
->prevent_default
= TRUE
;
1024 }else if(V_VT(&v
) != VT_EMPTY
) {
1025 FIXME("unhandled result %s\n", debugstr_variant(&v
));
1030 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
1037 * NOTE: CP events may require doc_obj reference, which we don't own. We make sure that
1038 * it's safe to call event handler by checking nsevent_listener, which is NULL for
1039 * detached documents.
1041 if(cp_container
&& cp_container
->forward_container
)
1042 cp_container
= cp_container
->forward_container
;
1043 if(cp_container
&& cp_container
->cps
&& doc
->nsevent_listener
) {
1044 ConnectionPoint
*cp
;
1047 for(j
=0; cp_container
->cp_entries
[j
].riid
; j
++) {
1048 cp
= cp_container
->cps
+ j
;
1049 if(!cp
->sinks_size
|| !is_cp_event(cp
->data
, event_info
[eid
].dispid
))
1052 for(i
=0; doc
->nsevent_listener
&& i
< cp
->sinks_size
; i
++) {
1053 if(!cp
->sinks
[i
].disp
)
1056 V_VT(&v
) = VT_EMPTY
;
1058 TRACE("cp %s [%u] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
1059 hres
= call_cp_func(cp
->sinks
[i
].disp
, event_info
[eid
].dispid
,
1060 cp
->data
->pass_event_arg
? event_obj
: NULL
, &v
);
1062 TRACE("cp %s [%u] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
1065 if(V_VT(&v
) == VT_BOOL
) {
1067 event_obj
->prevent_default
= TRUE
;
1068 }else if(V_VT(&v
) != VT_EMPTY
) {
1069 FIXME("unhandled result %s\n", debugstr_variant(&v
));
1074 WARN("cp %s [%u] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
1078 if(!doc
->nsevent_listener
)
1084 static void fire_event_obj(HTMLDocumentNode
*doc
, eventid_t eid
, HTMLEventObj
*event_obj
,
1085 nsIDOMNode
*target
, IDispatch
*script_this
)
1087 IHTMLEventObj
*prev_event
;
1088 nsIDOMNode
*parent
, *nsnode
;
1089 BOOL prevent_default
= FALSE
;
1090 HTMLInnerWindow
*window
;
1096 TRACE("(%p) %s\n", doc
, debugstr_w(event_info
[eid
].name
));
1098 window
= doc
->window
;
1100 WARN("NULL window\n");
1104 htmldoc_addref(&doc
->basedoc
);
1106 prev_event
= window
->event
;
1107 window
->event
= event_obj
? &event_obj
->IHTMLEventObj_iface
: NULL
;
1109 nsIDOMNode_GetNodeType(target
, &node_type
);
1111 nsIDOMNode_AddRef(nsnode
);
1116 hres
= get_node(doc
, nsnode
, FALSE
, &node
);
1117 if(SUCCEEDED(hres
) && node
) {
1118 call_event_handlers(doc
, event_obj
, *get_node_event_target(node
),
1119 node
->cp_container
, eid
, script_this
? script_this
: (IDispatch
*)&node
->IHTMLDOMNode_iface
);
1123 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
) || (event_obj
&& event_obj
->cancel_bubble
))
1126 nsIDOMNode_GetParentNode(nsnode
, &parent
);
1127 nsIDOMNode_Release(nsnode
);
1132 nsIDOMNode_GetNodeType(nsnode
, &node_type
);
1133 }while(node_type
== ELEMENT_NODE
);
1135 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
) || (event_obj
&& event_obj
->cancel_bubble
))
1139 if(event_info
[eid
].flags
& EVENT_FORWARDBODY
) {
1140 nsIDOMHTMLElement
*nsbody
;
1143 nsres
= nsIDOMHTMLDocument_GetBody(doc
->nsdoc
, &nsbody
);
1144 if(NS_SUCCEEDED(nsres
) && nsbody
) {
1145 hres
= get_node(doc
, (nsIDOMNode
*)nsbody
, FALSE
, &node
);
1146 if(SUCCEEDED(hres
) && node
) {
1147 call_event_handlers(doc
, event_obj
, *get_node_event_target(node
),
1148 node
->cp_container
, eid
, script_this
? script_this
: (IDispatch
*)&node
->IHTMLDOMNode_iface
);
1151 nsIDOMHTMLElement_Release(nsbody
);
1153 ERR("Could not get body: %08x\n", nsres
);
1157 call_event_handlers(doc
, event_obj
, doc
->node
.event_target
, &doc
->basedoc
.cp_container
, eid
,
1158 script_this
? script_this
: (IDispatch
*)&doc
->basedoc
.IHTMLDocument2_iface
);
1162 FIXME("unimplemented node type %d\n", node_type
);
1166 nsIDOMNode_Release(nsnode
);
1168 if(event_obj
&& event_obj
->prevent_default
)
1169 prevent_default
= TRUE
;
1170 window
->event
= prev_event
;
1172 if(!prevent_default
&& (event_info
[eid
].flags
& EVENT_HASDEFAULTHANDLERS
)) {
1173 nsIDOMNode_AddRef(target
);
1177 hres
= get_node(doc
, nsnode
, TRUE
, &node
);
1182 if(node
->vtbl
->handle_event
)
1183 hres
= node
->vtbl
->handle_event(node
, eid
, event_obj
? event_obj
->nsevent
: NULL
, &prevent_default
);
1185 if(FAILED(hres
) || prevent_default
|| (event_obj
&& event_obj
->cancel_bubble
))
1189 nsres
= nsIDOMNode_GetParentNode(nsnode
, &parent
);
1190 if(NS_FAILED(nsres
))
1193 nsIDOMNode_Release(nsnode
);
1198 nsIDOMNode_Release(nsnode
);
1201 if(prevent_default
&& event_obj
&& event_obj
->nsevent
) {
1202 TRACE("calling PreventDefault\n");
1203 nsIDOMEvent_PreventDefault(event_obj
->nsevent
);
1206 htmldoc_release(&doc
->basedoc
);
1209 void fire_event(HTMLDocumentNode
*doc
, eventid_t eid
, BOOL set_event
, nsIDOMNode
*target
, nsIDOMEvent
*nsevent
,
1210 IDispatch
*script_this
)
1212 HTMLEventObj
*event_obj
= NULL
;
1217 hres
= get_node(doc
, target
, TRUE
, &node
);
1221 event_obj
= create_event();
1226 hres
= set_event_info(event_obj
, node
, eid
, nsevent
);
1228 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1233 fire_event_obj(doc
, eid
, event_obj
, target
, script_this
);
1236 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1239 HRESULT
dispatch_event(HTMLDOMNode
*node
, const WCHAR
*event_name
, VARIANT
*event_var
, VARIANT_BOOL
*cancelled
)
1241 HTMLEventObj
*event_obj
= NULL
;
1245 eid
= attr_to_eid(event_name
);
1246 if(eid
== EVENTID_LAST
) {
1247 WARN("unknown event %s\n", debugstr_w(event_name
));
1248 return E_INVALIDARG
;
1251 if(event_var
&& V_VT(event_var
) != VT_EMPTY
&& V_VT(event_var
) != VT_ERROR
) {
1252 if(V_VT(event_var
) != VT_DISPATCH
) {
1253 FIXME("event_var %s not supported\n", debugstr_variant(event_var
));
1257 if(V_DISPATCH(event_var
)) {
1258 IHTMLEventObj
*event_iface
;
1260 hres
= IDispatch_QueryInterface(V_DISPATCH(event_var
), &IID_IHTMLEventObj
, (void**)&event_iface
);
1262 FIXME("No IHTMLEventObj iface\n");
1266 event_obj
= unsafe_impl_from_IHTMLEventObj(event_iface
);
1268 ERR("Not our IHTMLEventObj?\n");
1269 IHTMLEventObj_Release(event_iface
);
1276 hres
= set_event_info(event_obj
, node
, eid
, NULL
);
1278 fire_event_obj(node
->doc
, eid
, event_obj
, node
->nsnode
, NULL
);
1280 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1284 if(!(event_info
[eid
].flags
& EVENT_DEFAULTLISTENER
)) {
1285 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1289 fire_event(node
->doc
, eid
, TRUE
, node
->nsnode
, NULL
, NULL
);
1292 *cancelled
= VARIANT_TRUE
; /* FIXME */
1296 HRESULT
call_fire_event(HTMLDOMNode
*node
, eventid_t eid
)
1300 if(node
->vtbl
->fire_event
) {
1301 BOOL handled
= FALSE
;
1303 hres
= node
->vtbl
->fire_event(node
, eid
, &handled
);
1308 fire_event(node
->doc
, eid
, TRUE
, node
->nsnode
, NULL
, NULL
);
1312 static inline event_target_t
*get_event_target(event_target_t
**event_target_ptr
)
1314 if(!*event_target_ptr
)
1315 *event_target_ptr
= heap_alloc_zero(sizeof(event_target_t
));
1316 return *event_target_ptr
;
1319 static BOOL
alloc_handler_vector(event_target_t
*event_target
, eventid_t eid
, int cnt
)
1321 handler_vector_t
*new_vector
, *handler_vector
= event_target
->event_table
[eid
];
1323 if(handler_vector
) {
1324 if(cnt
<= handler_vector
->handler_cnt
)
1327 new_vector
= heap_realloc_zero(handler_vector
, sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
1329 new_vector
= heap_alloc_zero(sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
1335 new_vector
->handler_cnt
= cnt
;
1336 event_target
->event_table
[eid
] = new_vector
;
1340 static HRESULT
ensure_nsevent_handler(HTMLDocumentNode
*doc
, event_target_t
*event_target
, nsIDOMNode
*nsnode
, eventid_t eid
)
1345 if(event_info
[eid
].flags
& EVENT_NODEHANDLER
) {
1348 mask
= get_node_handler_mask(eid
);
1349 if(event_target
->node_handlers_mask
& mask
)
1352 add_nsevent_listener(doc
, nsnode
, event_info
[eid
].name
);
1353 event_target
->node_handlers_mask
|= mask
;
1357 if(!(event_info
[eid
].flags
& EVENT_DEFAULTLISTENER
))
1360 if(!doc
->event_vector
[eid
]) {
1361 doc
->event_vector
[eid
] = TRUE
;
1362 add_nsevent_listener(doc
, NULL
, event_info
[eid
].name
);
1368 void detach_events(HTMLDocumentNode
*doc
)
1370 if(doc
->event_vector
) {
1373 for(i
=0; i
< EVENTID_LAST
; i
++) {
1374 if(doc
->event_vector
[i
]) {
1375 detach_nsevent(doc
, event_info
[i
].name
);
1376 doc
->event_vector
[i
] = FALSE
;
1381 release_nsevents(doc
);
1385 static HRESULT
remove_event_handler(event_target_t
**event_target
, eventid_t eid
)
1387 if(*event_target
&& (*event_target
)->event_table
[eid
] && (*event_target
)->event_table
[eid
]->handler_prop
) {
1388 IDispatch_Release((*event_target
)->event_table
[eid
]->handler_prop
);
1389 (*event_target
)->event_table
[eid
]->handler_prop
= NULL
;
1395 static HRESULT
set_event_handler_disp(event_target_t
**event_target_ptr
, nsIDOMNode
*nsnode
, HTMLDocumentNode
*doc
,
1396 eventid_t eid
, IDispatch
*disp
)
1398 event_target_t
*event_target
;
1401 return remove_event_handler(event_target_ptr
, eid
);
1403 event_target
= get_event_target(event_target_ptr
);
1405 return E_OUTOFMEMORY
;
1407 if(!alloc_handler_vector(event_target
, eid
, 0))
1408 return E_OUTOFMEMORY
;
1410 if(event_target
->event_table
[eid
]->handler_prop
)
1411 IDispatch_Release(event_target
->event_table
[eid
]->handler_prop
);
1413 event_target
->event_table
[eid
]->handler_prop
= disp
;
1414 IDispatch_AddRef(disp
);
1416 return ensure_nsevent_handler(doc
, event_target
, nsnode
, eid
);
1419 HRESULT
set_event_handler(event_target_t
**event_target
, nsIDOMNode
*nsnode
, HTMLDocumentNode
*doc
, eventid_t eid
, VARIANT
*var
)
1423 return remove_event_handler(event_target
, eid
);
1426 return set_event_handler_disp(event_target
, nsnode
, doc
, eid
, V_DISPATCH(var
));
1429 FIXME("not handler %s\n", debugstr_variant(var
));
1438 HRESULT
get_event_handler(event_target_t
**event_target
, eventid_t eid
, VARIANT
*var
)
1440 if(*event_target
&& (*event_target
)->event_table
[eid
] && (*event_target
)->event_table
[eid
]->handler_prop
) {
1441 V_VT(var
) = VT_DISPATCH
;
1442 V_DISPATCH(var
) = (*event_target
)->event_table
[eid
]->handler_prop
;
1443 IDispatch_AddRef(V_DISPATCH(var
));
1445 V_VT(var
) = VT_NULL
;
1451 HRESULT
attach_event(event_target_t
**event_target_ptr
, nsIDOMNode
*nsnode
, HTMLDocument
*doc
, BSTR name
,
1452 IDispatch
*disp
, VARIANT_BOOL
*res
)
1454 event_target_t
*event_target
;
1458 eid
= attr_to_eid(name
);
1459 if(eid
== EVENTID_LAST
) {
1460 WARN("Unknown event\n");
1461 *res
= VARIANT_TRUE
;
1465 event_target
= get_event_target(event_target_ptr
);
1467 return E_OUTOFMEMORY
;
1469 if(event_target
->event_table
[eid
]) {
1470 while(i
< event_target
->event_table
[eid
]->handler_cnt
&& event_target
->event_table
[eid
]->handlers
[i
])
1472 if(i
== event_target
->event_table
[eid
]->handler_cnt
&& !alloc_handler_vector(event_target
, eid
, i
+1))
1473 return E_OUTOFMEMORY
;
1474 }else if(!alloc_handler_vector(event_target
, eid
, i
+1)) {
1475 return E_OUTOFMEMORY
;
1478 IDispatch_AddRef(disp
);
1479 event_target
->event_table
[eid
]->handlers
[i
] = disp
;
1481 *res
= VARIANT_TRUE
;
1482 return ensure_nsevent_handler(doc
->doc_node
, event_target
, nsnode
, eid
);
1485 HRESULT
detach_event(event_target_t
*event_target
, HTMLDocument
*doc
, BSTR name
, IDispatch
*disp
)
1493 eid
= attr_to_eid(name
);
1494 if(eid
== EVENTID_LAST
) {
1495 WARN("Unknown event\n");
1499 if(!event_target
->event_table
[eid
])
1502 while(i
< event_target
->event_table
[eid
]->handler_cnt
) {
1503 if(event_target
->event_table
[eid
]->handlers
[i
] == disp
) {
1504 IDispatch_Release(event_target
->event_table
[eid
]->handlers
[i
]);
1505 event_target
->event_table
[eid
]->handlers
[i
] = NULL
;
1513 void bind_node_event(HTMLDocumentNode
*doc
, event_target_t
**event_target
, HTMLDOMNode
*node
, const WCHAR
*event
, IDispatch
*disp
)
1517 TRACE("(%p %p %p %s %p)\n", doc
, event_target
, node
, debugstr_w(event
), disp
);
1519 eid
= attr_to_eid(event
);
1520 if(eid
== EVENTID_LAST
) {
1521 WARN("Unsupported event %s\n", debugstr_w(event
));
1525 set_event_handler_disp(event_target
, node
? node
->nsnode
: NULL
, doc
, eid
, disp
);
1528 void update_cp_events(HTMLInnerWindow
*window
, event_target_t
**event_target_ptr
, cp_static_data_t
*cp
, nsIDOMNode
*nsnode
)
1530 event_target_t
*event_target
;
1533 event_target
= get_event_target(event_target_ptr
);
1537 for(i
=0; i
< EVENTID_LAST
; i
++) {
1538 if((event_info
[i
].flags
& EVENT_DEFAULTLISTENER
) && is_cp_event(cp
, event_info
[i
].dispid
))
1539 ensure_nsevent_handler(window
->doc
, event_target
, nsnode
, i
);
1543 void check_event_attr(HTMLDocumentNode
*doc
, nsIDOMElement
*nselem
)
1545 const PRUnichar
*attr_value
;
1546 nsAString attr_name_str
, attr_value_str
;
1553 nsAString_Init(&attr_value_str
, NULL
);
1554 nsAString_Init(&attr_name_str
, NULL
);
1556 for(i
=0; i
< EVENTID_LAST
; i
++) {
1557 nsAString_SetData(&attr_name_str
, event_info
[i
].attr_name
);
1558 nsres
= nsIDOMElement_GetAttribute(nselem
, &attr_name_str
, &attr_value_str
);
1559 if(NS_SUCCEEDED(nsres
)) {
1560 nsAString_GetData(&attr_value_str
, &attr_value
);
1564 TRACE("%p.%s = %s\n", nselem
, debugstr_w(event_info
[i
].attr_name
), debugstr_w(attr_value
));
1566 disp
= script_parse_event(doc
->window
, attr_value
);
1568 hres
= get_node(doc
, (nsIDOMNode
*)nselem
, TRUE
, &node
);
1569 if(SUCCEEDED(hres
)) {
1570 set_event_handler_disp(get_node_event_target(node
), node
->nsnode
, node
->doc
, i
, disp
);
1573 IDispatch_Release(disp
);
1578 nsAString_Finish(&attr_value_str
);
1579 nsAString_Finish(&attr_name_str
);
1582 HRESULT
doc_init_events(HTMLDocumentNode
*doc
)
1587 doc
->event_vector
= heap_alloc_zero(EVENTID_LAST
*sizeof(BOOL
));
1588 if(!doc
->event_vector
)
1589 return E_OUTOFMEMORY
;
1593 for(i
=0; i
< EVENTID_LAST
; i
++) {
1594 if(event_info
[i
].flags
& EVENT_HASDEFAULTHANDLERS
) {
1595 hres
= ensure_nsevent_handler(doc
, NULL
, NULL
, i
);
1604 void release_event_target(event_target_t
*event_target
)
1609 for(i
=0; i
< EVENTID_LAST
; i
++) {
1610 if(event_target
->event_table
[i
]) {
1611 if(event_target
->event_table
[i
]->handler_prop
)
1612 IDispatch_Release(event_target
->event_table
[i
]->handler_prop
);
1613 for(j
=0; j
< event_target
->event_table
[i
]->handler_cnt
; j
++)
1614 if(event_target
->event_table
[i
]->handlers
[j
])
1615 IDispatch_Release(event_target
->event_table
[i
]->handlers
[j
]);
1619 heap_free(event_target
);