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"
31 #include "htmlscript.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
38 IDispatch
*handler_prop
;
40 IDispatch
*handlers
[0];
43 struct event_target_t
{
44 DWORD node_handlers_mask
;
45 handler_vector_t
*event_table
[EVENTID_LAST
];
48 static const WCHAR abortW
[] = {'a','b','o','r','t',0};
49 static const WCHAR onabortW
[] = {'o','n','a','b','o','r','t',0};
51 static const WCHAR beforeunloadW
[] = {'b','e','f','o','r','e','u','n','l','o','a','d',0};
52 static const WCHAR onbeforeunloadW
[] = {'o','n','b','e','f','o','r','e','u','n','l','o','a','d',0};
54 static const WCHAR blurW
[] = {'b','l','u','r',0};
55 static const WCHAR onblurW
[] = {'o','n','b','l','u','r',0};
57 static const WCHAR changeW
[] = {'c','h','a','n','g','e',0};
58 static const WCHAR onchangeW
[] = {'o','n','c','h','a','n','g','e',0};
60 static const WCHAR clickW
[] = {'c','l','i','c','k',0};
61 static const WCHAR onclickW
[] = {'o','n','c','l','i','c','k',0};
63 static const WCHAR contextmenuW
[] = {'c','o','n','t','e','x','t','m','e','n','u',0};
64 static const WCHAR oncontextmenuW
[] = {'o','n','c','o','n','t','e','x','t','m','e','n','u',0};
66 static const WCHAR dataavailableW
[] = {'d','a','t','a','a','v','a','i','l','a','b','l','e',0};
67 static const WCHAR ondataavailableW
[] = {'o','n','d','a','t','a','a','v','a','i','l','a','b','l','e',0};
69 static const WCHAR dblclickW
[] = {'d','b','l','c','l','i','c','k',0};
70 static const WCHAR ondblclickW
[] = {'o','n','d','b','l','c','l','i','c','k',0};
72 static const WCHAR dragW
[] = {'d','r','a','g',0};
73 static const WCHAR ondragW
[] = {'o','n','d','r','a','g',0};
75 static const WCHAR dragstartW
[] = {'d','r','a','g','s','t','a','r','t',0};
76 static const WCHAR ondragstartW
[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
78 static const WCHAR errorW
[] = {'e','r','r','o','r',0};
79 static const WCHAR onerrorW
[] = {'o','n','e','r','r','o','r',0};
81 static const WCHAR focusW
[] = {'f','o','c','u','s',0};
82 static const WCHAR onfocusW
[] = {'o','n','f','o','c','u','s',0};
84 static const WCHAR helpW
[] = {'h','e','l','p',0};
85 static const WCHAR onhelpW
[] = {'o','n','h','e','l','p',0};
87 static const WCHAR keydownW
[] = {'k','e','y','d','o','w','n',0};
88 static const WCHAR onkeydownW
[] = {'o','n','k','e','y','d','o','w','n',0};
90 static const WCHAR keypressW
[] = {'k','e','y','p','r','e','s','s',0};
91 static const WCHAR onkeypressW
[] = {'o','n','k','e','y','p','r','e','s','s',0};
93 static const WCHAR keyupW
[] = {'k','e','y','u','p',0};
94 static const WCHAR onkeyupW
[] = {'o','n','k','e','y','u','p',0};
96 static const WCHAR loadW
[] = {'l','o','a','d',0};
97 static const WCHAR onloadW
[] = {'o','n','l','o','a','d',0};
99 static const WCHAR mousedownW
[] = {'m','o','u','s','e','d','o','w','n',0};
100 static const WCHAR onmousedownW
[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
102 static const WCHAR mousemoveW
[] = {'m','o','u','s','e','m','o','v','e',0};
103 static const WCHAR onmousemoveW
[] = {'o','n','m','o','u','s','e','m','o','v','e',0};
105 static const WCHAR mouseoutW
[] = {'m','o','u','s','e','o','u','t',0};
106 static const WCHAR onmouseoutW
[] = {'o','n','m','o','u','s','e','o','u','t',0};
108 static const WCHAR mouseoverW
[] = {'m','o','u','s','e','o','v','e','r',0};
109 static const WCHAR onmouseoverW
[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
111 static const WCHAR mouseupW
[] = {'m','o','u','s','e','u','p',0};
112 static const WCHAR onmouseupW
[] = {'o','n','m','o','u','s','e','u','p',0};
114 static const WCHAR pasteW
[] = {'p','a','s','t','e',0};
115 static const WCHAR onpasteW
[] = {'o','n','p','a','s','t','e',0};
117 static const WCHAR readystatechangeW
[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
118 static const WCHAR onreadystatechangeW
[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
120 static const WCHAR resizeW
[] = {'r','e','s','i','z','e',0};
121 static const WCHAR onresizeW
[] = {'o','n','r','e','s','i','z','e',0};
123 static const WCHAR scrollW
[] = {'s','c','r','o','l','l',0};
124 static const WCHAR onscrollW
[] = {'o','n','s','c','r','o','l','l',0};
126 static const WCHAR selectstartW
[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
127 static const WCHAR onselectstartW
[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
129 static const WCHAR submitW
[] = {'s','u','b','m','i','t',0};
130 static const WCHAR onsubmitW
[] = {'o','n','s','u','b','m','i','t',0};
132 static const WCHAR HTMLEventsW
[] = {'H','T','M','L','E','v','e','n','t','s',0};
133 static const WCHAR KeyboardEventW
[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
134 static const WCHAR MouseEventW
[] = {'M','o','u','s','e','E','v','e','n','t',0};
143 static const WCHAR
*event_types
[] = {
158 #define EVENT_DEFAULTLISTENER 0x0001
159 #define EVENT_BUBBLE 0x0002
160 #define EVENT_FORWARDBODY 0x0004
161 #define EVENT_NODEHANDLER 0x0008
162 #define EVENT_CANCELABLE 0x0010
163 #define EVENT_HASDEFAULTHANDLERS 0x0020
165 static const event_info_t event_info
[] = {
166 {abortW
, onabortW
, EVENTT_NONE
, DISPID_EVMETH_ONABORT
,
168 {beforeunloadW
, onbeforeunloadW
, EVENTT_NONE
, DISPID_EVMETH_ONBEFOREUNLOAD
,
169 EVENT_DEFAULTLISTENER
|EVENT_FORWARDBODY
},
170 {blurW
, onblurW
, EVENTT_HTML
, DISPID_EVMETH_ONBLUR
,
171 EVENT_DEFAULTLISTENER
},
172 {changeW
, onchangeW
, EVENTT_HTML
, DISPID_EVMETH_ONCHANGE
,
173 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
174 {clickW
, onclickW
, EVENTT_MOUSE
, DISPID_EVMETH_ONCLICK
,
175 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
|EVENT_HASDEFAULTHANDLERS
},
176 {contextmenuW
, oncontextmenuW
, EVENTT_MOUSE
, DISPID_EVMETH_ONCONTEXTMENU
,
177 EVENT_BUBBLE
|EVENT_CANCELABLE
},
178 {dataavailableW
, ondataavailableW
, EVENTT_NONE
, DISPID_EVMETH_ONDATAAVAILABLE
,
180 {dblclickW
, ondblclickW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDBLCLICK
,
181 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
},
182 {dragW
, ondragW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDRAG
,
184 {dragstartW
, ondragstartW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDRAGSTART
,
186 {errorW
, onerrorW
, EVENTT_NONE
, DISPID_EVMETH_ONERROR
,
188 {focusW
, onfocusW
, EVENTT_HTML
, DISPID_EVMETH_ONFOCUS
,
189 EVENT_DEFAULTLISTENER
},
190 {helpW
, onhelpW
, EVENTT_KEY
, DISPID_EVMETH_ONHELP
,
191 EVENT_BUBBLE
|EVENT_CANCELABLE
},
192 {keydownW
, onkeydownW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYDOWN
,
193 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_HASDEFAULTHANDLERS
},
194 {keypressW
, onkeypressW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYPRESS
,
195 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
196 {keyupW
, onkeyupW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYUP
,
197 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
198 {loadW
, onloadW
, EVENTT_HTML
, DISPID_EVMETH_ONLOAD
,
200 {mousedownW
, onmousedownW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEDOWN
,
201 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
202 {mousemoveW
, onmousemoveW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEMOVE
,
203 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
204 {mouseoutW
, onmouseoutW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEOUT
,
205 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
206 {mouseoverW
, onmouseoverW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEOVER
,
207 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
208 {mouseupW
, onmouseupW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEUP
,
209 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
210 {pasteW
, onpasteW
, EVENTT_NONE
, DISPID_EVMETH_ONPASTE
,
212 {readystatechangeW
, onreadystatechangeW
, EVENTT_NONE
, DISPID_EVMETH_ONREADYSTATECHANGE
,
214 {resizeW
, onresizeW
, EVENTT_NONE
, DISPID_EVMETH_ONRESIZE
,
215 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
216 {scrollW
, onscrollW
, EVENTT_HTML
, DISPID_EVMETH_ONSCROLL
,
217 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
218 {selectstartW
, onselectstartW
, EVENTT_MOUSE
, DISPID_EVMETH_ONSELECTSTART
,
220 {submitW
, onsubmitW
, EVENTT_HTML
, DISPID_EVMETH_ONSUBMIT
,
221 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
}
224 static const eventid_t node_handled_list
[] = { EVENTID_ABORT
, EVENTID_ERROR
, EVENTID_LOAD
};
226 eventid_t
str_to_eid(LPCWSTR str
)
230 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
231 if(!strcmpW(event_info
[i
].name
, str
))
235 ERR("unknown type %s\n", debugstr_w(str
));
239 static eventid_t
attr_to_eid(LPCWSTR str
)
243 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
244 if(!strcmpW(event_info
[i
].attr_name
, str
))
251 static DWORD
get_node_handler_mask(eventid_t eid
)
255 for(i
=0; i
<sizeof(node_handled_list
)/sizeof(*node_handled_list
); i
++) {
256 if(node_handled_list
[i
] == eid
)
260 ERR("Invalid eid %d\n", eid
);
266 IHTMLEventObj IHTMLEventObj_iface
;
271 const event_info_t
*type
;
272 nsIDOMEvent
*nsevent
;
273 BOOL prevent_default
;
277 static inline HTMLEventObj
*impl_from_IHTMLEventObj(IHTMLEventObj
*iface
)
279 return CONTAINING_RECORD(iface
, HTMLEventObj
, IHTMLEventObj_iface
);
282 static HRESULT WINAPI
HTMLEventObj_QueryInterface(IHTMLEventObj
*iface
, REFIID riid
, void **ppv
)
284 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
288 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
289 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
290 *ppv
= &This
->IHTMLEventObj_iface
;
291 }else if(IsEqualGUID(&IID_IHTMLEventObj
, riid
)) {
292 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This
, ppv
);
293 *ppv
= &This
->IHTMLEventObj_iface
;
294 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
295 return *ppv
? S_OK
: E_NOINTERFACE
;
299 IUnknown_AddRef((IUnknown
*)*ppv
);
303 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
304 return E_NOINTERFACE
;
307 static ULONG WINAPI
HTMLEventObj_AddRef(IHTMLEventObj
*iface
)
309 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
310 LONG ref
= InterlockedIncrement(&This
->ref
);
312 TRACE("(%p) ref=%d\n", This
, ref
);
317 static ULONG WINAPI
HTMLEventObj_Release(IHTMLEventObj
*iface
)
319 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
320 LONG ref
= InterlockedDecrement(&This
->ref
);
322 TRACE("(%p) ref=%d\n", This
, ref
);
326 IHTMLDOMNode_Release(&This
->target
->IHTMLDOMNode_iface
);
328 nsIDOMEvent_Release(This
->nsevent
);
329 release_dispex(&This
->dispex
);
336 static HRESULT WINAPI
HTMLEventObj_GetTypeInfoCount(IHTMLEventObj
*iface
, UINT
*pctinfo
)
338 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
339 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
342 static HRESULT WINAPI
HTMLEventObj_GetTypeInfo(IHTMLEventObj
*iface
, UINT iTInfo
,
343 LCID lcid
, ITypeInfo
**ppTInfo
)
345 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
346 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
349 static HRESULT WINAPI
HTMLEventObj_GetIDsOfNames(IHTMLEventObj
*iface
, REFIID riid
,
350 LPOLESTR
*rgszNames
, UINT cNames
,
351 LCID lcid
, DISPID
*rgDispId
)
353 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
354 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
358 static HRESULT WINAPI
HTMLEventObj_Invoke(IHTMLEventObj
*iface
, DISPID dispIdMember
,
359 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
360 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
362 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
363 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
364 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
367 static HRESULT WINAPI
HTMLEventObj_get_srcElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
369 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
371 TRACE("(%p)->(%p)\n", This
, p
);
375 IHTMLDOMNode_QueryInterface(&This
->target
->IHTMLDOMNode_iface
, &IID_IHTMLElement
, (void**)p
);
379 static HRESULT WINAPI
HTMLEventObj_get_altKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
381 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
382 cpp_bool ret
= FALSE
;
384 TRACE("(%p)->(%p)\n", This
, p
);
387 nsIDOMKeyEvent
*key_event
;
390 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
391 if(NS_SUCCEEDED(nsres
)) {
392 nsIDOMKeyEvent_GetAltKey(key_event
, &ret
);
393 nsIDOMKeyEvent_Release(key_event
);
395 nsIDOMMouseEvent
*mouse_event
;
397 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
398 if(NS_SUCCEEDED(nsres
)) {
399 nsIDOMMouseEvent_GetAltKey(mouse_event
, &ret
);
400 nsIDOMMouseEvent_Release(mouse_event
);
405 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
409 static HRESULT WINAPI
HTMLEventObj_get_ctrlKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
411 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
412 cpp_bool ret
= FALSE
;
414 TRACE("(%p)->(%p)\n", This
, p
);
417 nsIDOMKeyEvent
*key_event
;
420 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
421 if(NS_SUCCEEDED(nsres
)) {
422 nsIDOMKeyEvent_GetCtrlKey(key_event
, &ret
);
423 nsIDOMKeyEvent_Release(key_event
);
425 nsIDOMMouseEvent
*mouse_event
;
427 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
428 if(NS_SUCCEEDED(nsres
)) {
429 nsIDOMMouseEvent_GetCtrlKey(mouse_event
, &ret
);
430 nsIDOMMouseEvent_Release(mouse_event
);
435 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
439 static HRESULT WINAPI
HTMLEventObj_get_shiftKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
441 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
442 cpp_bool ret
= FALSE
;
444 TRACE("(%p)->(%p)\n", This
, p
);
447 nsIDOMKeyEvent
*key_event
;
450 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
451 if(NS_SUCCEEDED(nsres
)) {
452 nsIDOMKeyEvent_GetShiftKey(key_event
, &ret
);
453 nsIDOMKeyEvent_Release(key_event
);
455 nsIDOMMouseEvent
*mouse_event
;
457 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
458 if(NS_SUCCEEDED(nsres
)) {
459 nsIDOMMouseEvent_GetShiftKey(mouse_event
, &ret
);
460 nsIDOMMouseEvent_Release(mouse_event
);
465 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
469 static HRESULT WINAPI
HTMLEventObj_put_returnValue(IHTMLEventObj
*iface
, VARIANT v
)
471 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
473 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&v
));
475 if(V_VT(&v
) != VT_BOOL
) {
476 FIXME("unsupported value %s\n", debugstr_variant(&v
));
477 return DISP_E_BADVARTYPE
;
481 This
->prevent_default
= TRUE
;
485 static HRESULT WINAPI
HTMLEventObj_get_returnValue(IHTMLEventObj
*iface
, VARIANT
*p
)
487 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
489 FIXME("(%p)->(%p)\n", This
, p
);
495 static HRESULT WINAPI
HTMLEventObj_put_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL v
)
497 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
499 TRACE("(%p)->(%x)\n", This
, v
);
501 This
->cancel_bubble
= !!v
;
505 static HRESULT WINAPI
HTMLEventObj_get_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
507 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
509 TRACE("(%p)->(%p)\n", This
, p
);
511 *p
= This
->cancel_bubble
? VARIANT_TRUE
: VARIANT_FALSE
;
515 static HRESULT WINAPI
HTMLEventObj_get_fromElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
517 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
519 FIXME("(%p)->(%p)\n", This
, p
);
525 static HRESULT WINAPI
HTMLEventObj_get_toElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
527 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
529 FIXME("(%p)->(%p)\n", This
, p
);
535 static HRESULT WINAPI
HTMLEventObj_put_keyCode(IHTMLEventObj
*iface
, LONG v
)
537 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
538 FIXME("(%p)->(%d)\n", This
, v
);
542 static HRESULT WINAPI
HTMLEventObj_get_keyCode(IHTMLEventObj
*iface
, LONG
*p
)
544 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
547 TRACE("(%p)->(%p)\n", This
, p
);
550 nsIDOMKeyEvent
*key_event
;
553 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
554 if(NS_SUCCEEDED(nsres
)) {
555 nsIDOMKeyEvent_GetKeyCode(key_event
, &key_code
);
556 nsIDOMKeyEvent_Release(key_event
);
564 static HRESULT WINAPI
HTMLEventObj_get_button(IHTMLEventObj
*iface
, LONG
*p
)
566 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
569 TRACE("(%p)->(%p)\n", This
, p
);
572 nsIDOMMouseEvent
*mouse_event
;
575 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
576 if(NS_SUCCEEDED(nsres
)) {
577 nsIDOMMouseEvent_GetButton(mouse_event
, &button
);
578 nsIDOMMouseEvent_Release(mouse_event
);
586 static HRESULT WINAPI
HTMLEventObj_get_type(IHTMLEventObj
*iface
, BSTR
*p
)
588 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
590 TRACE("(%p)->(%p)\n", This
, p
);
597 *p
= SysAllocString(This
->type
->name
);
598 return *p
? S_OK
: E_OUTOFMEMORY
;
601 static HRESULT WINAPI
HTMLEventObj_get_qualifier(IHTMLEventObj
*iface
, BSTR
*p
)
603 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
605 FIXME("(%p)->(%p)\n", This
, p
);
611 static HRESULT WINAPI
HTMLEventObj_get_reason(IHTMLEventObj
*iface
, LONG
*p
)
613 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
615 FIXME("(%p)->(%p)\n", This
, p
);
621 static HRESULT WINAPI
HTMLEventObj_get_x(IHTMLEventObj
*iface
, LONG
*p
)
623 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
625 FIXME("(%p)->(%p)\n", This
, p
);
631 static HRESULT WINAPI
HTMLEventObj_get_y(IHTMLEventObj
*iface
, LONG
*p
)
633 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
635 FIXME("(%p)->(%p)\n", This
, p
);
641 static HRESULT WINAPI
HTMLEventObj_get_clientX(IHTMLEventObj
*iface
, LONG
*p
)
643 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
646 TRACE("(%p)->(%p)\n", This
, p
);
649 nsIDOMMouseEvent
*mouse_event
;
652 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
653 if(NS_SUCCEEDED(nsres
)) {
654 nsIDOMMouseEvent_GetClientX(mouse_event
, &x
);
655 nsIDOMMouseEvent_Release(mouse_event
);
663 static HRESULT WINAPI
HTMLEventObj_get_clientY(IHTMLEventObj
*iface
, LONG
*p
)
665 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
668 TRACE("(%p)->(%p)\n", This
, p
);
671 nsIDOMMouseEvent
*mouse_event
;
674 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
675 if(NS_SUCCEEDED(nsres
)) {
676 nsIDOMMouseEvent_GetClientY(mouse_event
, &y
);
677 nsIDOMMouseEvent_Release(mouse_event
);
685 static HRESULT WINAPI
HTMLEventObj_get_offsetX(IHTMLEventObj
*iface
, LONG
*p
)
687 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
689 FIXME("(%p)->(%p)\n", This
, p
);
695 static HRESULT WINAPI
HTMLEventObj_get_offsetY(IHTMLEventObj
*iface
, LONG
*p
)
697 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
699 FIXME("(%p)->(%p)\n", This
, p
);
705 static HRESULT WINAPI
HTMLEventObj_get_screenX(IHTMLEventObj
*iface
, LONG
*p
)
707 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
710 TRACE("(%p)->(%p)\n", This
, p
);
713 nsIDOMMouseEvent
*mouse_event
;
716 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
717 if(NS_SUCCEEDED(nsres
)) {
718 nsIDOMMouseEvent_GetScreenX(mouse_event
, &x
);
719 nsIDOMMouseEvent_Release(mouse_event
);
727 static HRESULT WINAPI
HTMLEventObj_get_screenY(IHTMLEventObj
*iface
, LONG
*p
)
729 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
732 TRACE("(%p)->(%p)\n", This
, p
);
735 nsIDOMMouseEvent
*mouse_event
;
738 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
739 if(NS_SUCCEEDED(nsres
)) {
740 nsIDOMMouseEvent_GetScreenY(mouse_event
, &y
);
741 nsIDOMMouseEvent_Release(mouse_event
);
749 static HRESULT WINAPI
HTMLEventObj_get_srcFilter(IHTMLEventObj
*iface
, IDispatch
**p
)
751 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
753 FIXME("(%p)->(%p)\n", This
, p
);
759 static const IHTMLEventObjVtbl HTMLEventObjVtbl
= {
760 HTMLEventObj_QueryInterface
,
762 HTMLEventObj_Release
,
763 HTMLEventObj_GetTypeInfoCount
,
764 HTMLEventObj_GetTypeInfo
,
765 HTMLEventObj_GetIDsOfNames
,
767 HTMLEventObj_get_srcElement
,
768 HTMLEventObj_get_altKey
,
769 HTMLEventObj_get_ctrlKey
,
770 HTMLEventObj_get_shiftKey
,
771 HTMLEventObj_put_returnValue
,
772 HTMLEventObj_get_returnValue
,
773 HTMLEventObj_put_cancelBubble
,
774 HTMLEventObj_get_cancelBubble
,
775 HTMLEventObj_get_fromElement
,
776 HTMLEventObj_get_toElement
,
777 HTMLEventObj_put_keyCode
,
778 HTMLEventObj_get_keyCode
,
779 HTMLEventObj_get_button
,
780 HTMLEventObj_get_type
,
781 HTMLEventObj_get_qualifier
,
782 HTMLEventObj_get_reason
,
785 HTMLEventObj_get_clientX
,
786 HTMLEventObj_get_clientY
,
787 HTMLEventObj_get_offsetX
,
788 HTMLEventObj_get_offsetY
,
789 HTMLEventObj_get_screenX
,
790 HTMLEventObj_get_screenY
,
791 HTMLEventObj_get_srcFilter
794 static inline HTMLEventObj
*unsafe_impl_from_IHTMLEventObj(IHTMLEventObj
*iface
)
796 return iface
->lpVtbl
== &HTMLEventObjVtbl
? impl_from_IHTMLEventObj(iface
) : NULL
;
799 static const tid_t HTMLEventObj_iface_tids
[] = {
804 static dispex_static_data_t HTMLEventObj_dispex
= {
808 HTMLEventObj_iface_tids
811 static HTMLEventObj
*create_event(void)
815 ret
= heap_alloc_zero(sizeof(*ret
));
819 ret
->IHTMLEventObj_iface
.lpVtbl
= &HTMLEventObjVtbl
;
822 init_dispex(&ret
->dispex
, (IUnknown
*)&ret
->IHTMLEventObj_iface
, &HTMLEventObj_dispex
);
827 static HRESULT
set_event_info(HTMLEventObj
*event
, HTMLDOMNode
*target
, eventid_t eid
, nsIDOMEvent
*nsevent
)
829 event
->type
= event_info
+eid
;
830 event
->nsevent
= nsevent
;
833 nsIDOMEvent_AddRef(nsevent
);
834 }else if(event_types
[event_info
[eid
].type
]) {
838 nsAString_InitDepend(&type_str
, event_types
[event_info
[eid
].type
]);
839 nsres
= nsIDOMHTMLDocument_CreateEvent(target
->doc
->nsdoc
, &type_str
, &event
->nsevent
);
840 nsAString_Finish(&type_str
);
841 if(NS_FAILED(nsres
)) {
842 ERR("Could not create event: %08x\n", nsres
);
847 event
->target
= target
;
849 IHTMLDOMNode_AddRef(&target
->IHTMLDOMNode_iface
);
853 HRESULT
create_event_obj(IHTMLEventObj
**ret
)
857 event
= create_event();
859 return E_OUTOFMEMORY
;
861 *ret
= &event
->IHTMLEventObj_iface
;
865 static HRESULT
call_disp_func(IDispatch
*disp
, DISPPARAMS
*dp
, VARIANT
*retv
)
871 memset(&ei
, 0, sizeof(ei
));
873 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
874 if(SUCCEEDED(hres
)) {
875 hres
= IDispatchEx_InvokeEx(dispex
, 0, GetUserDefaultLCID(), DISPATCH_METHOD
, dp
, retv
, &ei
, NULL
);
876 IDispatchEx_Release(dispex
);
878 TRACE("Could not get IDispatchEx interface: %08x\n", hres
);
879 hres
= IDispatch_Invoke(disp
, 0, &IID_NULL
, GetUserDefaultLCID(), DISPATCH_METHOD
,
880 dp
, retv
, &ei
, NULL
);
886 static HRESULT
call_cp_func(IDispatch
*disp
, DISPID dispid
, HTMLEventObj
*event_obj
, VARIANT
*retv
)
888 DISPPARAMS dp
= {NULL
,NULL
,0,0};
894 V_VT(&event_arg
) = VT_DISPATCH
;
895 V_DISPATCH(&event_arg
) = (IDispatch
*)&event_obj
->IHTMLEventObj_iface
;
896 dp
.rgvarg
= &event_arg
;
900 memset(&ei
, 0, sizeof(ei
));
901 return IDispatch_Invoke(disp
, dispid
, &IID_NULL
, 0, DISPATCH_METHOD
, &dp
, retv
, &ei
, &argerr
);
904 static BOOL
is_cp_event(cp_static_data_t
*data
, DISPID dispid
)
913 hres
= get_dispids(data
->tid
, &data
->id_cnt
, &data
->ids
);
919 max
= data
->id_cnt
-1;
922 if(data
->ids
[i
] == dispid
)
925 if(data
->ids
[i
] < dispid
)
934 static void call_event_handlers(HTMLDocumentNode
*doc
, HTMLEventObj
*event_obj
, event_target_t
*event_target
,
935 ConnectionPointContainer
*cp_container
, eventid_t eid
, IDispatch
*this_obj
)
937 const BOOL cancelable
= event_info
[eid
].flags
& EVENT_CANCELABLE
;
938 handler_vector_t
*handler_vector
= NULL
;
943 handler_vector
= event_target
->event_table
[eid
];
945 if(handler_vector
&& handler_vector
->handler_prop
) {
946 DISPID named_arg
= DISPID_THIS
;
948 DISPPARAMS dp
= {&arg
, &named_arg
, 1, 1};
950 V_VT(&arg
) = VT_DISPATCH
;
951 V_DISPATCH(&arg
) = this_obj
;
954 TRACE("%s >>>\n", debugstr_w(event_info
[eid
].name
));
955 hres
= call_disp_func(handler_vector
->handler_prop
, &dp
, &v
);
957 TRACE("%s <<< %s\n", debugstr_w(event_info
[eid
].name
), debugstr_variant(&v
));
960 if(V_VT(&v
) == VT_BOOL
) {
962 event_obj
->prevent_default
= TRUE
;
963 }else if(V_VT(&v
) != VT_EMPTY
) {
964 FIXME("unhandled result %s\n", debugstr_variant(&v
));
969 WARN("%s <<< %08x\n", debugstr_w(event_info
[eid
].name
), hres
);
973 if(handler_vector
&& handler_vector
->handler_cnt
) {
975 DISPPARAMS dp
= {&arg
, NULL
, 1, 0};
978 V_VT(&arg
) = VT_DISPATCH
;
979 V_DISPATCH(&arg
) = (IDispatch
*)event_obj
;
981 i
= handler_vector
->handler_cnt
;
983 if(handler_vector
->handlers
[i
]) {
986 TRACE("%s [%d] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
987 hres
= call_disp_func(handler_vector
->handlers
[i
], &dp
, &v
);
989 TRACE("%s [%d] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
992 if(V_VT(&v
) == VT_BOOL
) {
994 event_obj
->prevent_default
= TRUE
;
995 }else if(V_VT(&v
) != VT_EMPTY
) {
996 FIXME("unhandled result %s\n", debugstr_variant(&v
));
1001 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
1008 * NOTE: CP events may require doc_obj reference, which we don't own. We make sure that
1009 * it's safe to call event handler by checking nsevent_listener, which is NULL for
1010 * detached documents.
1012 if(cp_container
&& cp_container
->forward_container
)
1013 cp_container
= cp_container
->forward_container
;
1014 if(cp_container
&& cp_container
->cps
&& doc
->nsevent_listener
) {
1015 ConnectionPoint
*cp
;
1018 for(j
=0; cp_container
->cp_entries
[j
].riid
; j
++) {
1019 cp
= cp_container
->cps
+ j
;
1020 if(!cp
->sinks_size
|| !is_cp_event(cp
->data
, event_info
[eid
].dispid
))
1023 for(i
=0; doc
->nsevent_listener
&& i
< cp
->sinks_size
; i
++) {
1024 if(!cp
->sinks
[i
].disp
)
1027 V_VT(&v
) = VT_EMPTY
;
1029 TRACE("cp %s [%u] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
1030 hres
= call_cp_func(cp
->sinks
[i
].disp
, event_info
[eid
].dispid
,
1031 cp
->data
->pass_event_arg
? event_obj
: NULL
, &v
);
1033 TRACE("cp %s [%u] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
1036 if(V_VT(&v
) == VT_BOOL
) {
1038 event_obj
->prevent_default
= TRUE
;
1039 }else if(V_VT(&v
) != VT_EMPTY
) {
1040 FIXME("unhandled result %s\n", debugstr_variant(&v
));
1045 WARN("cp %s [%u] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
1049 if(!doc
->nsevent_listener
)
1055 static void fire_event_obj(HTMLDocumentNode
*doc
, eventid_t eid
, HTMLEventObj
*event_obj
,
1056 nsIDOMNode
*target
, IDispatch
*script_this
)
1058 IHTMLEventObj
*prev_event
;
1059 nsIDOMNode
*parent
, *nsnode
;
1060 BOOL prevent_default
= FALSE
;
1061 HTMLInnerWindow
*window
;
1067 TRACE("(%p) %s\n", doc
, debugstr_w(event_info
[eid
].name
));
1069 window
= doc
->window
;
1071 WARN("NULL window\n");
1075 htmldoc_addref(&doc
->basedoc
);
1077 prev_event
= window
->event
;
1078 window
->event
= event_obj
? &event_obj
->IHTMLEventObj_iface
: NULL
;
1080 nsIDOMNode_GetNodeType(target
, &node_type
);
1082 nsIDOMNode_AddRef(nsnode
);
1087 hres
= get_node(doc
, nsnode
, FALSE
, &node
);
1088 if(SUCCEEDED(hres
) && node
) {
1089 call_event_handlers(doc
, event_obj
, *get_node_event_target(node
),
1090 node
->cp_container
, eid
, script_this
? script_this
: (IDispatch
*)&node
->IHTMLDOMNode_iface
);
1094 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
) || (event_obj
&& event_obj
->cancel_bubble
))
1097 nsIDOMNode_GetParentNode(nsnode
, &parent
);
1098 nsIDOMNode_Release(nsnode
);
1103 nsIDOMNode_GetNodeType(nsnode
, &node_type
);
1104 }while(node_type
== ELEMENT_NODE
);
1106 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
) || (event_obj
&& event_obj
->cancel_bubble
))
1110 if(event_info
[eid
].flags
& EVENT_FORWARDBODY
) {
1111 nsIDOMHTMLElement
*nsbody
;
1114 nsres
= nsIDOMHTMLDocument_GetBody(doc
->nsdoc
, &nsbody
);
1115 if(NS_SUCCEEDED(nsres
) && nsbody
) {
1116 hres
= get_node(doc
, (nsIDOMNode
*)nsbody
, 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
);
1122 nsIDOMHTMLElement_Release(nsbody
);
1124 ERR("Could not get body: %08x\n", nsres
);
1128 call_event_handlers(doc
, event_obj
, doc
->node
.event_target
, &doc
->basedoc
.cp_container
, eid
,
1129 script_this
? script_this
: (IDispatch
*)&doc
->basedoc
.IHTMLDocument2_iface
);
1133 FIXME("unimplemented node type %d\n", node_type
);
1137 nsIDOMNode_Release(nsnode
);
1139 if(event_obj
&& event_obj
->prevent_default
)
1140 prevent_default
= TRUE
;
1141 window
->event
= prev_event
;
1143 if(!prevent_default
&& (event_info
[eid
].flags
& EVENT_HASDEFAULTHANDLERS
)) {
1144 nsIDOMNode_AddRef(target
);
1148 hres
= get_node(doc
, nsnode
, TRUE
, &node
);
1153 if(node
->vtbl
->handle_event
)
1154 hres
= node
->vtbl
->handle_event(node
, eid
, event_obj
? event_obj
->nsevent
: NULL
, &prevent_default
);
1156 if(FAILED(hres
) || prevent_default
|| (event_obj
&& event_obj
->cancel_bubble
))
1160 nsres
= nsIDOMNode_GetParentNode(nsnode
, &parent
);
1161 if(NS_FAILED(nsres
))
1164 nsIDOMNode_Release(nsnode
);
1169 nsIDOMNode_Release(nsnode
);
1172 if(prevent_default
&& event_obj
&& event_obj
->nsevent
) {
1173 TRACE("calling PreventDefault\n");
1174 nsIDOMEvent_PreventDefault(event_obj
->nsevent
);
1177 htmldoc_release(&doc
->basedoc
);
1180 void fire_event(HTMLDocumentNode
*doc
, eventid_t eid
, BOOL set_event
, nsIDOMNode
*target
, nsIDOMEvent
*nsevent
,
1181 IDispatch
*script_this
)
1183 HTMLEventObj
*event_obj
= NULL
;
1188 hres
= get_node(doc
, target
, TRUE
, &node
);
1192 event_obj
= create_event();
1197 hres
= set_event_info(event_obj
, node
, eid
, nsevent
);
1199 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1204 fire_event_obj(doc
, eid
, event_obj
, target
, script_this
);
1207 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1210 HRESULT
dispatch_event(HTMLDOMNode
*node
, const WCHAR
*event_name
, VARIANT
*event_var
, VARIANT_BOOL
*cancelled
)
1212 HTMLEventObj
*event_obj
= NULL
;
1216 eid
= attr_to_eid(event_name
);
1217 if(eid
== EVENTID_LAST
) {
1218 WARN("unknown event %s\n", debugstr_w(event_name
));
1219 return E_INVALIDARG
;
1222 if(event_var
&& V_VT(event_var
) != VT_EMPTY
&& V_VT(event_var
) != VT_ERROR
) {
1223 if(V_VT(event_var
) != VT_DISPATCH
) {
1224 FIXME("event_var %s not supported\n", debugstr_variant(event_var
));
1228 if(V_DISPATCH(event_var
)) {
1229 IHTMLEventObj
*event_iface
;
1231 hres
= IDispatch_QueryInterface(V_DISPATCH(event_var
), &IID_IHTMLEventObj
, (void**)&event_iface
);
1233 FIXME("No IHTMLEventObj iface\n");
1237 event_obj
= unsafe_impl_from_IHTMLEventObj(event_iface
);
1239 ERR("Not our IHTMLEventObj?\n");
1240 IHTMLEventObj_Release(event_iface
);
1247 hres
= set_event_info(event_obj
, node
, eid
, NULL
);
1249 fire_event_obj(node
->doc
, eid
, event_obj
, node
->nsnode
, NULL
);
1251 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1255 if(!(event_info
[eid
].flags
& EVENT_DEFAULTLISTENER
)) {
1256 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1260 fire_event(node
->doc
, eid
, TRUE
, node
->nsnode
, NULL
, NULL
);
1263 *cancelled
= VARIANT_TRUE
; /* FIXME */
1267 HRESULT
call_fire_event(HTMLDOMNode
*node
, eventid_t eid
)
1271 if(node
->vtbl
->fire_event
) {
1272 BOOL handled
= FALSE
;
1274 hres
= node
->vtbl
->fire_event(node
, eid
, &handled
);
1279 fire_event(node
->doc
, eid
, TRUE
, node
->nsnode
, NULL
, NULL
);
1283 static inline event_target_t
*get_event_target(event_target_t
**event_target_ptr
)
1285 if(!*event_target_ptr
)
1286 *event_target_ptr
= heap_alloc_zero(sizeof(event_target_t
));
1287 return *event_target_ptr
;
1290 static BOOL
alloc_handler_vector(event_target_t
*event_target
, eventid_t eid
, int cnt
)
1292 handler_vector_t
*new_vector
, *handler_vector
= event_target
->event_table
[eid
];
1294 if(handler_vector
) {
1295 if(cnt
<= handler_vector
->handler_cnt
)
1298 new_vector
= heap_realloc_zero(handler_vector
, sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
1300 new_vector
= heap_alloc_zero(sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
1306 new_vector
->handler_cnt
= cnt
;
1307 event_target
->event_table
[eid
] = new_vector
;
1311 static HRESULT
ensure_nsevent_handler(HTMLDocumentNode
*doc
, event_target_t
*event_target
, nsIDOMNode
*nsnode
, eventid_t eid
)
1316 if(event_info
[eid
].flags
& EVENT_NODEHANDLER
) {
1319 mask
= get_node_handler_mask(eid
);
1320 if(event_target
->node_handlers_mask
& mask
)
1323 add_nsevent_listener(doc
, nsnode
, event_info
[eid
].name
);
1324 event_target
->node_handlers_mask
|= mask
;
1328 if(!(event_info
[eid
].flags
& EVENT_DEFAULTLISTENER
))
1331 if(!doc
->event_vector
[eid
]) {
1332 doc
->event_vector
[eid
] = TRUE
;
1333 add_nsevent_listener(doc
, NULL
, event_info
[eid
].name
);
1339 void detach_events(HTMLDocumentNode
*doc
)
1341 if(doc
->event_vector
) {
1344 for(i
=0; i
< EVENTID_LAST
; i
++) {
1345 if(doc
->event_vector
[i
]) {
1346 detach_nsevent(doc
, event_info
[i
].name
);
1347 doc
->event_vector
[i
] = FALSE
;
1352 release_nsevents(doc
);
1356 static HRESULT
remove_event_handler(event_target_t
**event_target
, eventid_t eid
)
1358 if(*event_target
&& (*event_target
)->event_table
[eid
] && (*event_target
)->event_table
[eid
]->handler_prop
) {
1359 IDispatch_Release((*event_target
)->event_table
[eid
]->handler_prop
);
1360 (*event_target
)->event_table
[eid
]->handler_prop
= NULL
;
1366 static HRESULT
set_event_handler_disp(event_target_t
**event_target_ptr
, nsIDOMNode
*nsnode
, HTMLDocumentNode
*doc
,
1367 eventid_t eid
, IDispatch
*disp
)
1369 event_target_t
*event_target
;
1372 return remove_event_handler(event_target_ptr
, eid
);
1374 event_target
= get_event_target(event_target_ptr
);
1376 return E_OUTOFMEMORY
;
1378 if(!alloc_handler_vector(event_target
, eid
, 0))
1379 return E_OUTOFMEMORY
;
1381 if(event_target
->event_table
[eid
]->handler_prop
)
1382 IDispatch_Release(event_target
->event_table
[eid
]->handler_prop
);
1384 event_target
->event_table
[eid
]->handler_prop
= disp
;
1385 IDispatch_AddRef(disp
);
1387 return ensure_nsevent_handler(doc
, event_target
, nsnode
, eid
);
1390 HRESULT
set_event_handler(event_target_t
**event_target
, nsIDOMNode
*nsnode
, HTMLDocumentNode
*doc
, eventid_t eid
, VARIANT
*var
)
1394 return remove_event_handler(event_target
, eid
);
1397 return set_event_handler_disp(event_target
, nsnode
, doc
, eid
, V_DISPATCH(var
));
1400 FIXME("not handler %s\n", debugstr_variant(var
));
1409 HRESULT
get_event_handler(event_target_t
**event_target
, eventid_t eid
, VARIANT
*var
)
1411 if(*event_target
&& (*event_target
)->event_table
[eid
] && (*event_target
)->event_table
[eid
]->handler_prop
) {
1412 V_VT(var
) = VT_DISPATCH
;
1413 V_DISPATCH(var
) = (*event_target
)->event_table
[eid
]->handler_prop
;
1414 IDispatch_AddRef(V_DISPATCH(var
));
1416 V_VT(var
) = VT_NULL
;
1422 HRESULT
attach_event(event_target_t
**event_target_ptr
, nsIDOMNode
*nsnode
, HTMLDocument
*doc
, BSTR name
,
1423 IDispatch
*disp
, VARIANT_BOOL
*res
)
1425 event_target_t
*event_target
;
1429 eid
= attr_to_eid(name
);
1430 if(eid
== EVENTID_LAST
) {
1431 WARN("Unknown event\n");
1432 *res
= VARIANT_TRUE
;
1436 event_target
= get_event_target(event_target_ptr
);
1438 return E_OUTOFMEMORY
;
1440 if(event_target
->event_table
[eid
]) {
1441 while(i
< event_target
->event_table
[eid
]->handler_cnt
&& event_target
->event_table
[eid
]->handlers
[i
])
1443 if(i
== event_target
->event_table
[eid
]->handler_cnt
&& !alloc_handler_vector(event_target
, eid
, i
+1))
1444 return E_OUTOFMEMORY
;
1445 }else if(!alloc_handler_vector(event_target
, eid
, i
+1)) {
1446 return E_OUTOFMEMORY
;
1449 IDispatch_AddRef(disp
);
1450 event_target
->event_table
[eid
]->handlers
[i
] = disp
;
1452 *res
= VARIANT_TRUE
;
1453 return ensure_nsevent_handler(doc
->doc_node
, event_target
, nsnode
, eid
);
1456 HRESULT
detach_event(event_target_t
*event_target
, HTMLDocument
*doc
, BSTR name
, IDispatch
*disp
)
1464 eid
= attr_to_eid(name
);
1465 if(eid
== EVENTID_LAST
) {
1466 WARN("Unknown event\n");
1470 if(!event_target
->event_table
[eid
])
1473 while(i
< event_target
->event_table
[eid
]->handler_cnt
) {
1474 if(event_target
->event_table
[eid
]->handlers
[i
] == disp
) {
1475 IDispatch_Release(event_target
->event_table
[eid
]->handlers
[i
]);
1476 event_target
->event_table
[eid
]->handlers
[i
] = NULL
;
1484 void bind_elem_event(HTMLDocumentNode
*doc
, HTMLElement
*elem
, const WCHAR
*event
, IDispatch
*disp
)
1488 TRACE("(%p %p %s %p)\n", doc
, elem
, debugstr_w(event
), disp
);
1490 eid
= attr_to_eid(event
);
1491 if(eid
== EVENTID_LAST
) {
1492 WARN("Unsupported event %s\n", debugstr_w(event
));
1496 set_event_handler_disp(&elem
->node
.event_target
, elem
->node
.nsnode
, doc
, eid
, disp
);
1499 void update_cp_events(HTMLInnerWindow
*window
, event_target_t
**event_target_ptr
, cp_static_data_t
*cp
, nsIDOMNode
*nsnode
)
1501 event_target_t
*event_target
;
1504 event_target
= get_event_target(event_target_ptr
);
1508 for(i
=0; i
< EVENTID_LAST
; i
++) {
1509 if((event_info
[i
].flags
& EVENT_DEFAULTLISTENER
) && is_cp_event(cp
, event_info
[i
].dispid
))
1510 ensure_nsevent_handler(window
->doc
, event_target
, nsnode
, i
);
1514 void check_event_attr(HTMLDocumentNode
*doc
, nsIDOMElement
*nselem
)
1516 const PRUnichar
*attr_value
;
1517 nsAString attr_name_str
, attr_value_str
;
1524 nsAString_Init(&attr_value_str
, NULL
);
1525 nsAString_Init(&attr_name_str
, NULL
);
1527 for(i
=0; i
< EVENTID_LAST
; i
++) {
1528 nsAString_SetData(&attr_name_str
, event_info
[i
].attr_name
);
1529 nsres
= nsIDOMElement_GetAttribute(nselem
, &attr_name_str
, &attr_value_str
);
1530 if(NS_SUCCEEDED(nsres
)) {
1531 nsAString_GetData(&attr_value_str
, &attr_value
);
1535 TRACE("%p.%s = %s\n", nselem
, debugstr_w(event_info
[i
].attr_name
), debugstr_w(attr_value
));
1537 disp
= script_parse_event(doc
->window
, attr_value
);
1539 hres
= get_node(doc
, (nsIDOMNode
*)nselem
, TRUE
, &node
);
1540 if(SUCCEEDED(hres
)) {
1541 set_event_handler_disp(get_node_event_target(node
), node
->nsnode
, node
->doc
, i
, disp
);
1544 IDispatch_Release(disp
);
1549 nsAString_Finish(&attr_value_str
);
1550 nsAString_Finish(&attr_name_str
);
1553 HRESULT
doc_init_events(HTMLDocumentNode
*doc
)
1558 doc
->event_vector
= heap_alloc_zero(EVENTID_LAST
*sizeof(BOOL
));
1559 if(!doc
->event_vector
)
1560 return E_OUTOFMEMORY
;
1564 for(i
=0; i
< EVENTID_LAST
; i
++) {
1565 if(event_info
[i
].flags
& EVENT_HASDEFAULTHANDLERS
) {
1566 hres
= ensure_nsevent_handler(doc
, NULL
, NULL
, i
);
1575 void release_event_target(event_target_t
*event_target
)
1580 for(i
=0; i
< EVENTID_LAST
; i
++) {
1581 if(event_target
->event_table
[i
]) {
1582 if(event_target
->event_table
[i
]->handler_prop
)
1583 IDispatch_Release(event_target
->event_table
[i
]->handler_prop
);
1584 for(j
=0; j
< event_target
->event_table
[i
]->handler_cnt
; j
++)
1585 if(event_target
->event_table
[i
]->handlers
[j
])
1586 IDispatch_Release(event_target
->event_table
[i
]->handlers
[j
]);
1590 heap_free(event_target
);