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 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 focusinW
[] = {'f','o','c','u','s','i','n',0};
85 static const WCHAR onfocusinW
[] = {'o','n','f','o','c','u','s','i','n',0};
87 static const WCHAR helpW
[] = {'h','e','l','p',0};
88 static const WCHAR onhelpW
[] = {'o','n','h','e','l','p',0};
90 static const WCHAR keydownW
[] = {'k','e','y','d','o','w','n',0};
91 static const WCHAR onkeydownW
[] = {'o','n','k','e','y','d','o','w','n',0};
93 static const WCHAR keypressW
[] = {'k','e','y','p','r','e','s','s',0};
94 static const WCHAR onkeypressW
[] = {'o','n','k','e','y','p','r','e','s','s',0};
96 static const WCHAR keyupW
[] = {'k','e','y','u','p',0};
97 static const WCHAR onkeyupW
[] = {'o','n','k','e','y','u','p',0};
99 static const WCHAR loadW
[] = {'l','o','a','d',0};
100 static const WCHAR onloadW
[] = {'o','n','l','o','a','d',0};
102 static const WCHAR mousedownW
[] = {'m','o','u','s','e','d','o','w','n',0};
103 static const WCHAR onmousedownW
[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
105 static const WCHAR mousemoveW
[] = {'m','o','u','s','e','m','o','v','e',0};
106 static const WCHAR onmousemoveW
[] = {'o','n','m','o','u','s','e','m','o','v','e',0};
108 static const WCHAR mouseoutW
[] = {'m','o','u','s','e','o','u','t',0};
109 static const WCHAR onmouseoutW
[] = {'o','n','m','o','u','s','e','o','u','t',0};
111 static const WCHAR mouseoverW
[] = {'m','o','u','s','e','o','v','e','r',0};
112 static const WCHAR onmouseoverW
[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
114 static const WCHAR mouseupW
[] = {'m','o','u','s','e','u','p',0};
115 static const WCHAR onmouseupW
[] = {'o','n','m','o','u','s','e','u','p',0};
117 static const WCHAR mousewheelW
[] = {'m','o','u','s','e','w','h','e','e','l',0};
118 static const WCHAR onmousewheelW
[] = {'o','n','m','o','u','s','e','w','h','e','e','l',0};
120 static const WCHAR pasteW
[] = {'p','a','s','t','e',0};
121 static const WCHAR onpasteW
[] = {'o','n','p','a','s','t','e',0};
123 static const WCHAR readystatechangeW
[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
124 static const WCHAR onreadystatechangeW
[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
126 static const WCHAR resizeW
[] = {'r','e','s','i','z','e',0};
127 static const WCHAR onresizeW
[] = {'o','n','r','e','s','i','z','e',0};
129 static const WCHAR scrollW
[] = {'s','c','r','o','l','l',0};
130 static const WCHAR onscrollW
[] = {'o','n','s','c','r','o','l','l',0};
132 static const WCHAR selectstartW
[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
133 static const WCHAR onselectstartW
[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
135 static const WCHAR submitW
[] = {'s','u','b','m','i','t',0};
136 static const WCHAR onsubmitW
[] = {'o','n','s','u','b','m','i','t',0};
138 static const WCHAR HTMLEventsW
[] = {'H','T','M','L','E','v','e','n','t','s',0};
139 static const WCHAR KeyboardEventW
[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
140 static const WCHAR MouseEventW
[] = {'M','o','u','s','e','E','v','e','n','t',0};
149 static const WCHAR
*event_types
[] = {
164 #define EVENT_DEFAULTLISTENER 0x0001
165 #define EVENT_BUBBLE 0x0002
166 #define EVENT_FORWARDBODY 0x0004
167 #define EVENT_BIND_TO_BODY 0x0008
168 #define EVENT_CANCELABLE 0x0010
169 #define EVENT_HASDEFAULTHANDLERS 0x0020
171 static const event_info_t event_info
[] = {
172 {abortW
, onabortW
, EVENTT_NONE
, DISPID_EVMETH_ONABORT
,
174 {beforeunloadW
, onbeforeunloadW
, EVENTT_NONE
, DISPID_EVMETH_ONBEFOREUNLOAD
,
175 EVENT_DEFAULTLISTENER
|EVENT_FORWARDBODY
},
176 {blurW
, onblurW
, EVENTT_HTML
, DISPID_EVMETH_ONBLUR
,
177 EVENT_DEFAULTLISTENER
},
178 {changeW
, onchangeW
, EVENTT_HTML
, DISPID_EVMETH_ONCHANGE
,
179 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
180 {clickW
, onclickW
, EVENTT_MOUSE
, DISPID_EVMETH_ONCLICK
,
181 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
|EVENT_HASDEFAULTHANDLERS
},
182 {contextmenuW
, oncontextmenuW
, EVENTT_MOUSE
, DISPID_EVMETH_ONCONTEXTMENU
,
183 EVENT_BUBBLE
|EVENT_CANCELABLE
},
184 {dataavailableW
, ondataavailableW
, EVENTT_NONE
, DISPID_EVMETH_ONDATAAVAILABLE
,
186 {dblclickW
, ondblclickW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDBLCLICK
,
187 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
},
188 {dragW
, ondragW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDRAG
,
190 {dragstartW
, ondragstartW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDRAGSTART
,
192 {errorW
, onerrorW
, EVENTT_NONE
, DISPID_EVMETH_ONERROR
,
194 {focusW
, onfocusW
, EVENTT_HTML
, DISPID_EVMETH_ONFOCUS
,
195 EVENT_DEFAULTLISTENER
},
196 {focusinW
, onfocusinW
, EVENTT_HTML
, DISPID_EVMETH_ONFOCUSIN
,
198 {helpW
, onhelpW
, EVENTT_KEY
, DISPID_EVMETH_ONHELP
,
199 EVENT_BUBBLE
|EVENT_CANCELABLE
},
200 {keydownW
, onkeydownW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYDOWN
,
201 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_HASDEFAULTHANDLERS
},
202 {keypressW
, onkeypressW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYPRESS
,
203 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
204 {keyupW
, onkeyupW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYUP
,
205 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
206 {loadW
, onloadW
, EVENTT_HTML
, DISPID_EVMETH_ONLOAD
,
208 {mousedownW
, onmousedownW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEDOWN
,
209 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
},
210 {mousemoveW
, onmousemoveW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEMOVE
,
211 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
212 {mouseoutW
, onmouseoutW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEOUT
,
213 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
214 {mouseoverW
, onmouseoverW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEOVER
,
215 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
216 {mouseupW
, onmouseupW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEUP
,
217 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
218 {mousewheelW
, onmousewheelW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEWHEEL
,
220 {pasteW
, onpasteW
, EVENTT_NONE
, DISPID_EVMETH_ONPASTE
,
222 {readystatechangeW
, onreadystatechangeW
, EVENTT_NONE
, DISPID_EVMETH_ONREADYSTATECHANGE
,
224 {resizeW
, onresizeW
, EVENTT_NONE
, DISPID_EVMETH_ONRESIZE
,
225 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
226 {scrollW
, onscrollW
, EVENTT_HTML
, DISPID_EVMETH_ONSCROLL
,
227 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
228 {selectstartW
, onselectstartW
, EVENTT_MOUSE
, DISPID_EVMETH_ONSELECTSTART
,
230 {submitW
, onsubmitW
, EVENTT_HTML
, DISPID_EVMETH_ONSUBMIT
,
231 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
|EVENT_HASDEFAULTHANDLERS
}
234 eventid_t
str_to_eid(LPCWSTR str
)
238 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
239 if(!strcmpW(event_info
[i
].name
, str
))
243 ERR("unknown type %s\n", debugstr_w(str
));
247 static eventid_t
attr_to_eid(LPCWSTR str
)
251 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
252 if(!strcmpW(event_info
[i
].attr_name
, str
))
259 struct HTMLEventObj
{
261 IHTMLEventObj IHTMLEventObj_iface
;
266 const event_info_t
*type
;
267 nsIDOMEvent
*nsevent
;
268 VARIANT return_value
;
269 BOOL prevent_default
;
273 static inline HTMLEventObj
*impl_from_IHTMLEventObj(IHTMLEventObj
*iface
)
275 return CONTAINING_RECORD(iface
, HTMLEventObj
, IHTMLEventObj_iface
);
278 static HRESULT WINAPI
HTMLEventObj_QueryInterface(IHTMLEventObj
*iface
, REFIID riid
, void **ppv
)
280 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
282 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
284 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
285 *ppv
= &This
->IHTMLEventObj_iface
;
286 }else if(IsEqualGUID(&IID_IHTMLEventObj
, riid
)) {
287 *ppv
= &This
->IHTMLEventObj_iface
;
288 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
289 return *ppv
? S_OK
: E_NOINTERFACE
;
292 WARN("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
293 return E_NOINTERFACE
;
296 IUnknown_AddRef((IUnknown
*)*ppv
);
300 static ULONG WINAPI
HTMLEventObj_AddRef(IHTMLEventObj
*iface
)
302 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
303 LONG ref
= InterlockedIncrement(&This
->ref
);
305 TRACE("(%p) ref=%d\n", This
, ref
);
310 static ULONG WINAPI
HTMLEventObj_Release(IHTMLEventObj
*iface
)
312 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
313 LONG ref
= InterlockedDecrement(&This
->ref
);
315 TRACE("(%p) ref=%d\n", This
, ref
);
319 IHTMLDOMNode_Release(&This
->target
->IHTMLDOMNode_iface
);
321 nsIDOMEvent_Release(This
->nsevent
);
322 release_dispex(&This
->dispex
);
329 static HRESULT WINAPI
HTMLEventObj_GetTypeInfoCount(IHTMLEventObj
*iface
, UINT
*pctinfo
)
331 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
332 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
335 static HRESULT WINAPI
HTMLEventObj_GetTypeInfo(IHTMLEventObj
*iface
, UINT iTInfo
,
336 LCID lcid
, ITypeInfo
**ppTInfo
)
338 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
339 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
342 static HRESULT WINAPI
HTMLEventObj_GetIDsOfNames(IHTMLEventObj
*iface
, REFIID riid
,
343 LPOLESTR
*rgszNames
, UINT cNames
,
344 LCID lcid
, DISPID
*rgDispId
)
346 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
347 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
351 static HRESULT WINAPI
HTMLEventObj_Invoke(IHTMLEventObj
*iface
, DISPID dispIdMember
,
352 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
353 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
355 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
356 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
357 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
360 static HRESULT WINAPI
HTMLEventObj_get_srcElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
362 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
364 TRACE("(%p)->(%p)\n", This
, p
);
368 IHTMLDOMNode_QueryInterface(&This
->target
->IHTMLDOMNode_iface
, &IID_IHTMLElement
, (void**)p
);
372 static HRESULT WINAPI
HTMLEventObj_get_altKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
374 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
375 cpp_bool ret
= FALSE
;
377 TRACE("(%p)->(%p)\n", This
, p
);
380 nsIDOMKeyEvent
*key_event
;
383 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
384 if(NS_SUCCEEDED(nsres
)) {
385 nsIDOMKeyEvent_GetAltKey(key_event
, &ret
);
386 nsIDOMKeyEvent_Release(key_event
);
388 nsIDOMMouseEvent
*mouse_event
;
390 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
391 if(NS_SUCCEEDED(nsres
)) {
392 nsIDOMMouseEvent_GetAltKey(mouse_event
, &ret
);
393 nsIDOMMouseEvent_Release(mouse_event
);
398 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
402 static HRESULT WINAPI
HTMLEventObj_get_ctrlKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
404 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
405 cpp_bool ret
= FALSE
;
407 TRACE("(%p)->(%p)\n", This
, p
);
410 nsIDOMKeyEvent
*key_event
;
413 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
414 if(NS_SUCCEEDED(nsres
)) {
415 nsIDOMKeyEvent_GetCtrlKey(key_event
, &ret
);
416 nsIDOMKeyEvent_Release(key_event
);
418 nsIDOMMouseEvent
*mouse_event
;
420 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
421 if(NS_SUCCEEDED(nsres
)) {
422 nsIDOMMouseEvent_GetCtrlKey(mouse_event
, &ret
);
423 nsIDOMMouseEvent_Release(mouse_event
);
428 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
432 static HRESULT WINAPI
HTMLEventObj_get_shiftKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
434 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
435 cpp_bool ret
= FALSE
;
437 TRACE("(%p)->(%p)\n", This
, p
);
440 nsIDOMKeyEvent
*key_event
;
443 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
444 if(NS_SUCCEEDED(nsres
)) {
445 nsIDOMKeyEvent_GetShiftKey(key_event
, &ret
);
446 nsIDOMKeyEvent_Release(key_event
);
448 nsIDOMMouseEvent
*mouse_event
;
450 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
451 if(NS_SUCCEEDED(nsres
)) {
452 nsIDOMMouseEvent_GetShiftKey(mouse_event
, &ret
);
453 nsIDOMMouseEvent_Release(mouse_event
);
458 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
462 static HRESULT WINAPI
HTMLEventObj_put_returnValue(IHTMLEventObj
*iface
, VARIANT v
)
464 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
466 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&v
));
468 if(V_VT(&v
) != VT_BOOL
) {
469 FIXME("unsupported value %s\n", debugstr_variant(&v
));
470 return DISP_E_BADVARTYPE
;
473 This
->return_value
= v
;
475 This
->prevent_default
= TRUE
;
479 static HRESULT WINAPI
HTMLEventObj_get_returnValue(IHTMLEventObj
*iface
, VARIANT
*p
)
481 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
483 TRACE("(%p)->(%p)\n", This
, p
);
486 return VariantCopy(p
, &This
->return_value
);
489 static HRESULT WINAPI
HTMLEventObj_put_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL v
)
491 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
493 TRACE("(%p)->(%x)\n", This
, v
);
495 This
->cancel_bubble
= !!v
;
499 static HRESULT WINAPI
HTMLEventObj_get_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
501 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
503 TRACE("(%p)->(%p)\n", This
, p
);
505 *p
= This
->cancel_bubble
? VARIANT_TRUE
: VARIANT_FALSE
;
509 static HRESULT WINAPI
HTMLEventObj_get_fromElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
511 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
513 FIXME("(%p)->(%p)\n", This
, p
);
519 static HRESULT WINAPI
HTMLEventObj_get_toElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
521 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
523 FIXME("(%p)->(%p)\n", This
, p
);
529 static HRESULT WINAPI
HTMLEventObj_put_keyCode(IHTMLEventObj
*iface
, LONG v
)
531 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
532 FIXME("(%p)->(%d)\n", This
, v
);
536 static HRESULT WINAPI
HTMLEventObj_get_keyCode(IHTMLEventObj
*iface
, LONG
*p
)
538 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
541 TRACE("(%p)->(%p)\n", This
, p
);
544 nsIDOMKeyEvent
*key_event
;
547 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
548 if(NS_SUCCEEDED(nsres
)) {
549 nsIDOMKeyEvent_GetKeyCode(key_event
, &key_code
);
550 nsIDOMKeyEvent_Release(key_event
);
558 static HRESULT WINAPI
HTMLEventObj_get_button(IHTMLEventObj
*iface
, LONG
*p
)
560 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
563 TRACE("(%p)->(%p)\n", This
, p
);
566 nsIDOMMouseEvent
*mouse_event
;
569 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
570 if(NS_SUCCEEDED(nsres
)) {
571 nsIDOMMouseEvent_GetButton(mouse_event
, &button
);
572 nsIDOMMouseEvent_Release(mouse_event
);
580 static HRESULT WINAPI
HTMLEventObj_get_type(IHTMLEventObj
*iface
, BSTR
*p
)
582 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
584 TRACE("(%p)->(%p)\n", This
, p
);
591 *p
= SysAllocString(This
->type
->name
);
592 return *p
? S_OK
: E_OUTOFMEMORY
;
595 static HRESULT WINAPI
HTMLEventObj_get_qualifier(IHTMLEventObj
*iface
, BSTR
*p
)
597 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
599 FIXME("(%p)->(%p)\n", This
, p
);
605 static HRESULT WINAPI
HTMLEventObj_get_reason(IHTMLEventObj
*iface
, LONG
*p
)
607 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
609 FIXME("(%p)->(%p)\n", This
, p
);
615 static HRESULT WINAPI
HTMLEventObj_get_x(IHTMLEventObj
*iface
, LONG
*p
)
617 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
620 TRACE("(%p)->(%p)\n", This
, p
);
623 nsIDOMUIEvent
*ui_event
;
626 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMUIEvent
, (void**)&ui_event
);
627 if(NS_SUCCEEDED(nsres
)) {
628 /* NOTE: pageX is not exactly right here. */
629 nsres
= nsIDOMUIEvent_GetPageX(ui_event
, &x
);
630 assert(nsres
== NS_OK
);
631 nsIDOMUIEvent_Release(ui_event
);
639 static HRESULT WINAPI
HTMLEventObj_get_y(IHTMLEventObj
*iface
, LONG
*p
)
641 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
644 TRACE("(%p)->(%p)\n", This
, p
);
647 nsIDOMUIEvent
*ui_event
;
650 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMUIEvent
, (void**)&ui_event
);
651 if(NS_SUCCEEDED(nsres
)) {
652 /* NOTE: pageY is not exactly right here. */
653 nsres
= nsIDOMUIEvent_GetPageY(ui_event
, &y
);
654 assert(nsres
== NS_OK
);
655 nsIDOMUIEvent_Release(ui_event
);
663 static HRESULT WINAPI
HTMLEventObj_get_clientX(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_GetClientX(mouse_event
, &x
);
677 nsIDOMMouseEvent_Release(mouse_event
);
685 static HRESULT WINAPI
HTMLEventObj_get_clientY(IHTMLEventObj
*iface
, LONG
*p
)
687 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
690 TRACE("(%p)->(%p)\n", This
, p
);
693 nsIDOMMouseEvent
*mouse_event
;
696 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
697 if(NS_SUCCEEDED(nsres
)) {
698 nsIDOMMouseEvent_GetClientY(mouse_event
, &y
);
699 nsIDOMMouseEvent_Release(mouse_event
);
707 static HRESULT WINAPI
HTMLEventObj_get_offsetX(IHTMLEventObj
*iface
, LONG
*p
)
709 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
711 FIXME("(%p)->(%p)\n", This
, p
);
717 static HRESULT WINAPI
HTMLEventObj_get_offsetY(IHTMLEventObj
*iface
, LONG
*p
)
719 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
721 FIXME("(%p)->(%p)\n", This
, p
);
727 static HRESULT WINAPI
HTMLEventObj_get_screenX(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_GetScreenX(mouse_event
, &x
);
741 nsIDOMMouseEvent_Release(mouse_event
);
749 static HRESULT WINAPI
HTMLEventObj_get_screenY(IHTMLEventObj
*iface
, LONG
*p
)
751 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
754 TRACE("(%p)->(%p)\n", This
, p
);
757 nsIDOMMouseEvent
*mouse_event
;
760 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
761 if(NS_SUCCEEDED(nsres
)) {
762 nsIDOMMouseEvent_GetScreenY(mouse_event
, &y
);
763 nsIDOMMouseEvent_Release(mouse_event
);
771 static HRESULT WINAPI
HTMLEventObj_get_srcFilter(IHTMLEventObj
*iface
, IDispatch
**p
)
773 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
775 FIXME("(%p)->(%p)\n", This
, p
);
781 static const IHTMLEventObjVtbl HTMLEventObjVtbl
= {
782 HTMLEventObj_QueryInterface
,
784 HTMLEventObj_Release
,
785 HTMLEventObj_GetTypeInfoCount
,
786 HTMLEventObj_GetTypeInfo
,
787 HTMLEventObj_GetIDsOfNames
,
789 HTMLEventObj_get_srcElement
,
790 HTMLEventObj_get_altKey
,
791 HTMLEventObj_get_ctrlKey
,
792 HTMLEventObj_get_shiftKey
,
793 HTMLEventObj_put_returnValue
,
794 HTMLEventObj_get_returnValue
,
795 HTMLEventObj_put_cancelBubble
,
796 HTMLEventObj_get_cancelBubble
,
797 HTMLEventObj_get_fromElement
,
798 HTMLEventObj_get_toElement
,
799 HTMLEventObj_put_keyCode
,
800 HTMLEventObj_get_keyCode
,
801 HTMLEventObj_get_button
,
802 HTMLEventObj_get_type
,
803 HTMLEventObj_get_qualifier
,
804 HTMLEventObj_get_reason
,
807 HTMLEventObj_get_clientX
,
808 HTMLEventObj_get_clientY
,
809 HTMLEventObj_get_offsetX
,
810 HTMLEventObj_get_offsetY
,
811 HTMLEventObj_get_screenX
,
812 HTMLEventObj_get_screenY
,
813 HTMLEventObj_get_srcFilter
816 static inline HTMLEventObj
*unsafe_impl_from_IHTMLEventObj(IHTMLEventObj
*iface
)
818 return iface
->lpVtbl
== &HTMLEventObjVtbl
? impl_from_IHTMLEventObj(iface
) : NULL
;
821 static const tid_t HTMLEventObj_iface_tids
[] = {
826 static dispex_static_data_t HTMLEventObj_dispex
= {
830 HTMLEventObj_iface_tids
833 static HTMLEventObj
*create_event(void)
837 ret
= heap_alloc_zero(sizeof(*ret
));
841 ret
->IHTMLEventObj_iface
.lpVtbl
= &HTMLEventObjVtbl
;
844 init_dispex(&ret
->dispex
, (IUnknown
*)&ret
->IHTMLEventObj_iface
, &HTMLEventObj_dispex
);
849 static HRESULT
set_event_info(HTMLEventObj
*event
, HTMLDOMNode
*target
, eventid_t eid
, nsIDOMEvent
*nsevent
)
851 event
->type
= event_info
+eid
;
852 event
->nsevent
= nsevent
;
855 nsIDOMEvent_AddRef(nsevent
);
856 }else if(event_types
[event_info
[eid
].type
]) {
860 nsAString_InitDepend(&type_str
, event_types
[event_info
[eid
].type
]);
861 nsres
= nsIDOMHTMLDocument_CreateEvent(target
->doc
->nsdoc
, &type_str
, &event
->nsevent
);
862 nsAString_Finish(&type_str
);
863 if(NS_FAILED(nsres
)) {
864 ERR("Could not create event: %08x\n", nsres
);
869 event
->target
= target
;
871 IHTMLDOMNode_AddRef(&target
->IHTMLDOMNode_iface
);
875 HRESULT
create_event_obj(IHTMLEventObj
**ret
)
879 event
= create_event();
881 return E_OUTOFMEMORY
;
883 *ret
= &event
->IHTMLEventObj_iface
;
887 static inline event_target_t
*get_event_target_data(EventTarget
*event_target
, BOOL alloc
)
889 event_target_t
**ptr
;
891 ptr
= event_target
->dispex
.data
->vtbl
&& event_target
->dispex
.data
->vtbl
->get_event_target_ptr
892 ? event_target
->dispex
.data
->vtbl
->get_event_target_ptr(&event_target
->dispex
)
893 : &event_target
->ptr
;
897 return *ptr
= heap_alloc_zero(sizeof(event_target_t
));
900 static HRESULT
call_disp_func(IDispatch
*disp
, DISPPARAMS
*dp
, VARIANT
*retv
)
906 memset(&ei
, 0, sizeof(ei
));
908 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
909 if(SUCCEEDED(hres
)) {
910 hres
= IDispatchEx_InvokeEx(dispex
, 0, GetUserDefaultLCID(), DISPATCH_METHOD
, dp
, retv
, &ei
, NULL
);
911 IDispatchEx_Release(dispex
);
913 TRACE("Could not get IDispatchEx interface: %08x\n", hres
);
914 hres
= IDispatch_Invoke(disp
, 0, &IID_NULL
, GetUserDefaultLCID(), DISPATCH_METHOD
,
915 dp
, retv
, &ei
, NULL
);
921 static HRESULT
call_cp_func(IDispatch
*disp
, DISPID dispid
, HTMLEventObj
*event_obj
, VARIANT
*retv
)
923 DISPPARAMS dp
= {NULL
,NULL
,0,0};
929 V_VT(&event_arg
) = VT_DISPATCH
;
930 V_DISPATCH(&event_arg
) = (IDispatch
*)&event_obj
->IHTMLEventObj_iface
;
931 dp
.rgvarg
= &event_arg
;
935 memset(&ei
, 0, sizeof(ei
));
936 return IDispatch_Invoke(disp
, dispid
, &IID_NULL
, 0, DISPATCH_METHOD
, &dp
, retv
, &ei
, &argerr
);
939 static BOOL
is_cp_event(cp_static_data_t
*data
, DISPID dispid
)
948 hres
= get_dispids(data
->tid
, &data
->id_cnt
, &data
->ids
);
954 max
= data
->id_cnt
-1;
957 if(data
->ids
[i
] == dispid
)
960 if(data
->ids
[i
] < dispid
)
969 void call_event_handlers(HTMLDocumentNode
*doc
, HTMLEventObj
*event_obj
, EventTarget
*event_target
,
970 ConnectionPointContainer
*cp_container
, eventid_t eid
, IDispatch
*this_obj
)
972 event_target_t
*data
= get_event_target_data(event_target
, FALSE
);
973 const BOOL cancelable
= event_info
[eid
].flags
& EVENT_CANCELABLE
;
977 if(data
&& data
->event_table
[eid
] && data
->event_table
[eid
]->handler_prop
) {
978 DISPID named_arg
= DISPID_THIS
;
980 DISPPARAMS dp
= {&arg
, &named_arg
, 1, 1};
982 V_VT(&arg
) = VT_DISPATCH
;
983 V_DISPATCH(&arg
) = this_obj
;
986 TRACE("%s >>>\n", debugstr_w(event_info
[eid
].name
));
987 hres
= call_disp_func(data
->event_table
[eid
]->handler_prop
, &dp
, &v
);
989 TRACE("%s <<< %s\n", debugstr_w(event_info
[eid
].name
), debugstr_variant(&v
));
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 <<< %08x\n", debugstr_w(event_info
[eid
].name
), hres
);
1005 if(data
&& data
->event_table
[eid
] && data
->event_table
[eid
]->handler_cnt
) {
1007 DISPPARAMS dp
= {&arg
, NULL
, 1, 0};
1010 V_VT(&arg
) = VT_DISPATCH
;
1011 V_DISPATCH(&arg
) = (IDispatch
*)event_obj
;
1013 i
= data
->event_table
[eid
]->handler_cnt
;
1015 if(data
->event_table
[eid
]->handlers
[i
]) {
1016 V_VT(&v
) = VT_EMPTY
;
1018 TRACE("%s [%d] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
1019 hres
= call_disp_func(data
->event_table
[eid
]->handlers
[i
], &dp
, &v
);
1021 TRACE("%s [%d] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
1024 if(V_VT(&v
) == VT_BOOL
) {
1026 event_obj
->prevent_default
= TRUE
;
1027 }else if(V_VT(&v
) != VT_EMPTY
) {
1028 FIXME("unhandled result %s\n", debugstr_variant(&v
));
1033 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
1040 * NOTE: CP events may require doc_obj reference, which we don't own. We make sure that
1041 * it's safe to call event handler by checking nsevent_listener, which is NULL for
1042 * detached documents.
1044 if(cp_container
&& cp_container
->forward_container
)
1045 cp_container
= cp_container
->forward_container
;
1046 if(cp_container
&& cp_container
->cps
&& doc
->nsevent_listener
) {
1047 ConnectionPoint
*cp
;
1050 for(j
=0; cp_container
->cp_entries
[j
].riid
; j
++) {
1051 cp
= cp_container
->cps
+ j
;
1052 if(!cp
->sinks_size
|| !is_cp_event(cp
->data
, event_info
[eid
].dispid
))
1055 for(i
=0; doc
->nsevent_listener
&& i
< cp
->sinks_size
; i
++) {
1056 if(!cp
->sinks
[i
].disp
)
1059 V_VT(&v
) = VT_EMPTY
;
1061 TRACE("cp %s [%u] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
1062 hres
= call_cp_func(cp
->sinks
[i
].disp
, event_info
[eid
].dispid
,
1063 cp
->data
->pass_event_arg
? event_obj
: NULL
, &v
);
1065 TRACE("cp %s [%u] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
1068 if(V_VT(&v
) == VT_BOOL
) {
1070 event_obj
->prevent_default
= TRUE
;
1071 }else if(V_VT(&v
) != VT_EMPTY
) {
1072 FIXME("unhandled result %s\n", debugstr_variant(&v
));
1077 WARN("cp %s [%u] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
1081 if(!doc
->nsevent_listener
)
1087 static void fire_event_obj(HTMLDocumentNode
*doc
, eventid_t eid
, HTMLEventObj
*event_obj
,
1088 nsIDOMNode
*target
, IDispatch
*script_this
)
1090 IHTMLEventObj
*prev_event
;
1091 nsIDOMNode
*parent
, *nsnode
;
1092 BOOL prevent_default
= FALSE
;
1093 HTMLInnerWindow
*window
;
1099 TRACE("(%p) %s\n", doc
, debugstr_w(event_info
[eid
].name
));
1101 window
= doc
->window
;
1103 WARN("NULL window\n");
1107 htmldoc_addref(&doc
->basedoc
);
1109 prev_event
= window
->event
;
1110 window
->event
= event_obj
? &event_obj
->IHTMLEventObj_iface
: NULL
;
1112 nsIDOMNode_GetNodeType(target
, &node_type
);
1114 nsIDOMNode_AddRef(nsnode
);
1119 hres
= get_node(doc
, nsnode
, FALSE
, &node
);
1120 if(SUCCEEDED(hres
) && node
) {
1121 call_event_handlers(doc
, event_obj
, &node
->event_target
, node
->cp_container
, eid
,
1122 script_this
? script_this
: (IDispatch
*)&node
->IHTMLDOMNode_iface
);
1126 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
) || (event_obj
&& event_obj
->cancel_bubble
))
1129 nsIDOMNode_GetParentNode(nsnode
, &parent
);
1130 nsIDOMNode_Release(nsnode
);
1135 nsIDOMNode_GetNodeType(nsnode
, &node_type
);
1136 }while(node_type
== ELEMENT_NODE
);
1138 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
) || (event_obj
&& event_obj
->cancel_bubble
))
1142 if(event_info
[eid
].flags
& EVENT_FORWARDBODY
) {
1143 nsIDOMHTMLElement
*nsbody
;
1146 nsres
= nsIDOMHTMLDocument_GetBody(doc
->nsdoc
, &nsbody
);
1147 if(NS_SUCCEEDED(nsres
) && nsbody
) {
1148 hres
= get_node(doc
, (nsIDOMNode
*)nsbody
, FALSE
, &node
);
1149 if(SUCCEEDED(hres
) && node
) {
1150 call_event_handlers(doc
, event_obj
, &node
->event_target
, node
->cp_container
, eid
,
1151 script_this
? script_this
: (IDispatch
*)&node
->IHTMLDOMNode_iface
);
1154 nsIDOMHTMLElement_Release(nsbody
);
1156 ERR("Could not get body: %08x\n", nsres
);
1160 call_event_handlers(doc
, event_obj
, &doc
->node
.event_target
, &doc
->basedoc
.cp_container
, eid
,
1161 script_this
? script_this
: (IDispatch
*)&doc
->basedoc
.IHTMLDocument2_iface
);
1165 FIXME("unimplemented node type %d\n", node_type
);
1169 nsIDOMNode_Release(nsnode
);
1171 if(event_obj
&& event_obj
->prevent_default
)
1172 prevent_default
= TRUE
;
1173 window
->event
= prev_event
;
1175 if(!prevent_default
&& (event_info
[eid
].flags
& EVENT_HASDEFAULTHANDLERS
)) {
1176 nsIDOMNode_AddRef(target
);
1180 hres
= get_node(doc
, nsnode
, TRUE
, &node
);
1185 if(node
->vtbl
->handle_event
)
1186 hres
= node
->vtbl
->handle_event(node
, eid
, event_obj
? event_obj
->nsevent
: NULL
, &prevent_default
);
1188 if(FAILED(hres
) || prevent_default
|| (event_obj
&& event_obj
->cancel_bubble
))
1192 nsres
= nsIDOMNode_GetParentNode(nsnode
, &parent
);
1193 if(NS_FAILED(nsres
))
1196 nsIDOMNode_Release(nsnode
);
1201 nsIDOMNode_Release(nsnode
);
1204 if(prevent_default
&& event_obj
&& event_obj
->nsevent
) {
1205 TRACE("calling PreventDefault\n");
1206 nsIDOMEvent_PreventDefault(event_obj
->nsevent
);
1209 htmldoc_release(&doc
->basedoc
);
1212 void fire_event(HTMLDocumentNode
*doc
, eventid_t eid
, BOOL set_event
, nsIDOMNode
*target
, nsIDOMEvent
*nsevent
,
1213 IDispatch
*script_this
)
1215 HTMLEventObj
*event_obj
= NULL
;
1220 hres
= get_node(doc
, target
, TRUE
, &node
);
1224 event_obj
= create_event();
1229 hres
= set_event_info(event_obj
, node
, eid
, nsevent
);
1231 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1236 fire_event_obj(doc
, eid
, event_obj
, target
, script_this
);
1239 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1242 HRESULT
dispatch_event(HTMLDOMNode
*node
, const WCHAR
*event_name
, VARIANT
*event_var
, VARIANT_BOOL
*cancelled
)
1244 HTMLEventObj
*event_obj
= NULL
;
1248 eid
= attr_to_eid(event_name
);
1249 if(eid
== EVENTID_LAST
) {
1250 WARN("unknown event %s\n", debugstr_w(event_name
));
1251 return E_INVALIDARG
;
1254 if(event_var
&& V_VT(event_var
) != VT_EMPTY
&& V_VT(event_var
) != VT_ERROR
) {
1255 if(V_VT(event_var
) != VT_DISPATCH
) {
1256 FIXME("event_var %s not supported\n", debugstr_variant(event_var
));
1260 if(V_DISPATCH(event_var
)) {
1261 IHTMLEventObj
*event_iface
;
1263 hres
= IDispatch_QueryInterface(V_DISPATCH(event_var
), &IID_IHTMLEventObj
, (void**)&event_iface
);
1265 FIXME("No IHTMLEventObj iface\n");
1269 event_obj
= unsafe_impl_from_IHTMLEventObj(event_iface
);
1271 ERR("Not our IHTMLEventObj?\n");
1272 IHTMLEventObj_Release(event_iface
);
1279 hres
= set_event_info(event_obj
, node
, eid
, NULL
);
1281 fire_event_obj(node
->doc
, eid
, event_obj
, node
->nsnode
, NULL
);
1283 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1287 if(!(event_info
[eid
].flags
& EVENT_DEFAULTLISTENER
)) {
1288 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1292 fire_event(node
->doc
, eid
, TRUE
, node
->nsnode
, NULL
, NULL
);
1295 *cancelled
= VARIANT_TRUE
; /* FIXME */
1299 HRESULT
call_fire_event(HTMLDOMNode
*node
, eventid_t eid
)
1303 if(node
->vtbl
->fire_event
) {
1304 BOOL handled
= FALSE
;
1306 hres
= node
->vtbl
->fire_event(node
, eid
, &handled
);
1311 fire_event(node
->doc
, eid
, TRUE
, node
->nsnode
, NULL
, NULL
);
1315 static BOOL
alloc_handler_vector(event_target_t
*event_target
, eventid_t eid
, int cnt
)
1317 handler_vector_t
*new_vector
, *handler_vector
= event_target
->event_table
[eid
];
1319 if(handler_vector
) {
1320 if(cnt
<= handler_vector
->handler_cnt
)
1323 new_vector
= heap_realloc_zero(handler_vector
, sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
1325 new_vector
= heap_alloc_zero(sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
1331 new_vector
->handler_cnt
= cnt
;
1332 event_target
->event_table
[eid
] = new_vector
;
1336 HRESULT
ensure_doc_nsevent_handler(HTMLDocumentNode
*doc
, eventid_t eid
)
1338 nsIDOMNode
*nsnode
= NULL
;
1340 TRACE("%s\n", debugstr_w(event_info
[eid
].name
));
1342 if(!doc
->nsdoc
|| doc
->event_vector
[eid
] || !(event_info
[eid
].flags
& (EVENT_DEFAULTLISTENER
|EVENT_BIND_TO_BODY
)))
1345 if(event_info
[eid
].flags
& EVENT_BIND_TO_BODY
) {
1346 nsnode
= doc
->node
.nsnode
;
1347 nsIDOMNode_AddRef(nsnode
);
1350 doc
->event_vector
[eid
] = TRUE
;
1351 add_nsevent_listener(doc
, nsnode
, event_info
[eid
].name
);
1354 nsIDOMNode_Release(nsnode
);
1358 void detach_events(HTMLDocumentNode
*doc
)
1360 if(doc
->event_vector
) {
1363 for(i
=0; i
< EVENTID_LAST
; i
++) {
1364 if(doc
->event_vector
[i
]) {
1365 detach_nsevent(doc
, event_info
[i
].name
);
1366 doc
->event_vector
[i
] = FALSE
;
1371 release_nsevents(doc
);
1374 static void bind_event(EventTarget
*event_target
, eventid_t eid
)
1376 if(event_target
->dispex
.data
->vtbl
->bind_event
)
1377 event_target
->dispex
.data
->vtbl
->bind_event(&event_target
->dispex
, eid
);
1379 FIXME("Unsupported event binding on target %p\n", event_target
);
1382 static void remove_event_handler(EventTarget
*event_target
, eventid_t eid
)
1384 event_target_t
*data
;
1388 hres
= dispex_get_dprop_ref(&event_target
->dispex
, event_info
[eid
].attr_name
, FALSE
, &store
);
1390 VariantClear(store
);
1392 data
= get_event_target_data(event_target
, FALSE
);
1393 if(data
&& data
->event_table
[eid
] && data
->event_table
[eid
]->handler_prop
) {
1394 IDispatch_Release(data
->event_table
[eid
]->handler_prop
);
1395 data
->event_table
[eid
]->handler_prop
= NULL
;
1399 static HRESULT
set_event_handler_disp(EventTarget
*event_target
, eventid_t eid
, IDispatch
*disp
)
1401 event_target_t
*data
;
1403 remove_event_handler(event_target
, eid
);
1407 data
= get_event_target_data(event_target
, TRUE
);
1409 return E_OUTOFMEMORY
;
1411 if(!alloc_handler_vector(data
, eid
, 0))
1412 return E_OUTOFMEMORY
;
1414 data
->event_table
[eid
]->handler_prop
= disp
;
1415 IDispatch_AddRef(disp
);
1417 bind_event(event_target
, eid
);
1421 HRESULT
set_event_handler(EventTarget
*event_target
, eventid_t eid
, VARIANT
*var
)
1425 remove_event_handler(event_target
, eid
);
1429 return set_event_handler_disp(event_target
, eid
, V_DISPATCH(var
));
1436 * Setting event handler to string is a rare case and we don't want to
1437 * complicate nor increase memory of event_target_t for that. Instead,
1438 * we store the value in DispatchEx, which can already handle custom
1441 remove_event_handler(event_target
, eid
);
1443 hres
= dispex_get_dprop_ref(&event_target
->dispex
, event_info
[eid
].attr_name
, TRUE
, &v
);
1447 V_BSTR(v
) = SysAllocString(V_BSTR(var
));
1449 return E_OUTOFMEMORY
;
1455 FIXME("not handler %s\n", debugstr_variant(var
));
1464 HRESULT
get_event_handler(EventTarget
*event_target
, eventid_t eid
, VARIANT
*var
)
1466 event_target_t
*data
;
1470 hres
= dispex_get_dprop_ref(&event_target
->dispex
, event_info
[eid
].attr_name
, FALSE
, &v
);
1471 if(SUCCEEDED(hres
) && V_VT(v
) != VT_EMPTY
)
1472 return VariantCopy(var
, v
);
1474 data
= get_event_target_data(event_target
, FALSE
);
1475 if(data
&& data
->event_table
[eid
] && data
->event_table
[eid
]->handler_prop
) {
1476 V_VT(var
) = VT_DISPATCH
;
1477 V_DISPATCH(var
) = data
->event_table
[eid
]->handler_prop
;
1478 IDispatch_AddRef(V_DISPATCH(var
));
1480 V_VT(var
) = VT_NULL
;
1486 HRESULT
attach_event(EventTarget
*event_target
, BSTR name
, IDispatch
*disp
, VARIANT_BOOL
*res
)
1488 event_target_t
*data
;
1492 eid
= attr_to_eid(name
);
1493 if(eid
== EVENTID_LAST
) {
1494 WARN("Unknown event\n");
1495 *res
= VARIANT_TRUE
;
1499 data
= get_event_target_data(event_target
, TRUE
);
1501 return E_OUTOFMEMORY
;
1503 if(data
->event_table
[eid
]) {
1504 while(i
< data
->event_table
[eid
]->handler_cnt
&& data
->event_table
[eid
]->handlers
[i
])
1506 if(i
== data
->event_table
[eid
]->handler_cnt
&& !alloc_handler_vector(data
, eid
, i
+1))
1507 return E_OUTOFMEMORY
;
1508 }else if(!alloc_handler_vector(data
, eid
, i
+1)) {
1509 return E_OUTOFMEMORY
;
1512 IDispatch_AddRef(disp
);
1513 data
->event_table
[eid
]->handlers
[i
] = disp
;
1515 bind_event(event_target
, eid
);
1517 *res
= VARIANT_TRUE
;
1521 HRESULT
detach_event(EventTarget
*event_target
, BSTR name
, IDispatch
*disp
)
1523 event_target_t
*data
;
1527 eid
= attr_to_eid(name
);
1528 if(eid
== EVENTID_LAST
) {
1529 WARN("Unknown event\n");
1533 data
= get_event_target_data(event_target
, FALSE
);
1537 if(!data
->event_table
[eid
])
1540 while(i
< data
->event_table
[eid
]->handler_cnt
) {
1541 if(data
->event_table
[eid
]->handlers
[i
] == disp
) {
1542 IDispatch_Release(data
->event_table
[eid
]->handlers
[i
]);
1543 data
->event_table
[eid
]->handlers
[i
] = NULL
;
1551 void bind_target_event(HTMLDocumentNode
*doc
, EventTarget
*event_target
, const WCHAR
*event
, IDispatch
*disp
)
1555 TRACE("(%p %p %s %p)\n", doc
, event_target
, debugstr_w(event
), disp
);
1557 eid
= attr_to_eid(event
);
1558 if(eid
== EVENTID_LAST
) {
1559 WARN("Unsupported event %s\n", debugstr_w(event
));
1563 set_event_handler_disp(event_target
, eid
, disp
);
1566 void update_doc_cp_events(HTMLDocumentNode
*doc
, cp_static_data_t
*cp
)
1570 for(i
=0; i
< EVENTID_LAST
; i
++) {
1571 if((event_info
[i
].flags
& EVENT_DEFAULTLISTENER
) && is_cp_event(cp
, event_info
[i
].dispid
))
1572 ensure_doc_nsevent_handler(doc
, i
);
1576 void check_event_attr(HTMLDocumentNode
*doc
, nsIDOMHTMLElement
*nselem
)
1578 const PRUnichar
*attr_value
;
1579 nsAString attr_value_str
;
1586 for(i
=0; i
< EVENTID_LAST
; i
++) {
1587 nsres
= get_elem_attr_value(nselem
, event_info
[i
].attr_name
, &attr_value_str
, &attr_value
);
1588 if(NS_SUCCEEDED(nsres
)) {
1592 TRACE("%p.%s = %s\n", nselem
, debugstr_w(event_info
[i
].attr_name
), debugstr_w(attr_value
));
1594 disp
= script_parse_event(doc
->window
, attr_value
);
1596 hres
= get_node(doc
, (nsIDOMNode
*)nselem
, TRUE
, &node
);
1597 if(SUCCEEDED(hres
)) {
1598 set_event_handler_disp(&node
->event_target
, i
, disp
);
1601 IDispatch_Release(disp
);
1603 nsAString_Finish(&attr_value_str
);
1608 HRESULT
doc_init_events(HTMLDocumentNode
*doc
)
1613 doc
->event_vector
= heap_alloc_zero(EVENTID_LAST
*sizeof(BOOL
));
1614 if(!doc
->event_vector
)
1615 return E_OUTOFMEMORY
;
1619 for(i
=0; i
< EVENTID_LAST
; i
++) {
1620 if(event_info
[i
].flags
& EVENT_HASDEFAULTHANDLERS
) {
1621 hres
= ensure_doc_nsevent_handler(doc
, i
);
1630 void release_event_target(event_target_t
*event_target
)
1635 for(i
=0; i
< EVENTID_LAST
; i
++) {
1636 if(event_target
->event_table
[i
]) {
1637 if(event_target
->event_table
[i
]->handler_prop
)
1638 IDispatch_Release(event_target
->event_table
[i
]->handler_prop
);
1639 for(j
=0; j
< event_target
->event_table
[i
]->handler_cnt
; j
++)
1640 if(event_target
->event_table
[i
]->handlers
[j
])
1641 IDispatch_Release(event_target
->event_table
[i
]->handlers
[j
]);
1645 heap_free(event_target
);