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 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 mousewheelW
[] = {'m','o','u','s','e','w','h','e','e','l',0};
115 static const WCHAR onmousewheelW
[] = {'o','n','m','o','u','s','e','w','h','e','e','l',0};
117 static const WCHAR pasteW
[] = {'p','a','s','t','e',0};
118 static const WCHAR onpasteW
[] = {'o','n','p','a','s','t','e',0};
120 static const WCHAR readystatechangeW
[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
121 static const WCHAR onreadystatechangeW
[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
123 static const WCHAR resizeW
[] = {'r','e','s','i','z','e',0};
124 static const WCHAR onresizeW
[] = {'o','n','r','e','s','i','z','e',0};
126 static const WCHAR scrollW
[] = {'s','c','r','o','l','l',0};
127 static const WCHAR onscrollW
[] = {'o','n','s','c','r','o','l','l',0};
129 static const WCHAR selectstartW
[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
130 static const WCHAR onselectstartW
[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
132 static const WCHAR submitW
[] = {'s','u','b','m','i','t',0};
133 static const WCHAR onsubmitW
[] = {'o','n','s','u','b','m','i','t',0};
135 static const WCHAR HTMLEventsW
[] = {'H','T','M','L','E','v','e','n','t','s',0};
136 static const WCHAR KeyboardEventW
[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
137 static const WCHAR MouseEventW
[] = {'M','o','u','s','e','E','v','e','n','t',0};
146 static const WCHAR
*event_types
[] = {
161 #define EVENT_DEFAULTLISTENER 0x0001
162 #define EVENT_BUBBLE 0x0002
163 #define EVENT_FORWARDBODY 0x0004
164 #define EVENT_BIND_TO_BODY 0x0008
165 #define EVENT_CANCELABLE 0x0010
166 #define EVENT_HASDEFAULTHANDLERS 0x0020
168 static const event_info_t event_info
[] = {
169 {abortW
, onabortW
, EVENTT_NONE
, DISPID_EVMETH_ONABORT
,
171 {beforeunloadW
, onbeforeunloadW
, EVENTT_NONE
, DISPID_EVMETH_ONBEFOREUNLOAD
,
172 EVENT_DEFAULTLISTENER
|EVENT_FORWARDBODY
},
173 {blurW
, onblurW
, EVENTT_HTML
, DISPID_EVMETH_ONBLUR
,
174 EVENT_DEFAULTLISTENER
},
175 {changeW
, onchangeW
, EVENTT_HTML
, DISPID_EVMETH_ONCHANGE
,
176 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
177 {clickW
, onclickW
, EVENTT_MOUSE
, DISPID_EVMETH_ONCLICK
,
178 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
|EVENT_HASDEFAULTHANDLERS
},
179 {contextmenuW
, oncontextmenuW
, EVENTT_MOUSE
, DISPID_EVMETH_ONCONTEXTMENU
,
180 EVENT_BUBBLE
|EVENT_CANCELABLE
},
181 {dataavailableW
, ondataavailableW
, EVENTT_NONE
, DISPID_EVMETH_ONDATAAVAILABLE
,
183 {dblclickW
, ondblclickW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDBLCLICK
,
184 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
},
185 {dragW
, ondragW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDRAG
,
187 {dragstartW
, ondragstartW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDRAGSTART
,
189 {errorW
, onerrorW
, EVENTT_NONE
, DISPID_EVMETH_ONERROR
,
191 {focusW
, onfocusW
, EVENTT_HTML
, DISPID_EVMETH_ONFOCUS
,
192 EVENT_DEFAULTLISTENER
},
193 {helpW
, onhelpW
, EVENTT_KEY
, DISPID_EVMETH_ONHELP
,
194 EVENT_BUBBLE
|EVENT_CANCELABLE
},
195 {keydownW
, onkeydownW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYDOWN
,
196 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_HASDEFAULTHANDLERS
},
197 {keypressW
, onkeypressW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYPRESS
,
198 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
199 {keyupW
, onkeyupW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYUP
,
200 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
201 {loadW
, onloadW
, EVENTT_HTML
, DISPID_EVMETH_ONLOAD
,
203 {mousedownW
, onmousedownW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEDOWN
,
204 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
205 {mousemoveW
, onmousemoveW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEMOVE
,
206 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
207 {mouseoutW
, onmouseoutW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEOUT
,
208 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
209 {mouseoverW
, onmouseoverW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEOVER
,
210 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
211 {mouseupW
, onmouseupW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEUP
,
212 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
213 {mousewheelW
, onmousewheelW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEWHEEL
,
215 {pasteW
, onpasteW
, EVENTT_NONE
, DISPID_EVMETH_ONPASTE
,
217 {readystatechangeW
, onreadystatechangeW
, EVENTT_NONE
, DISPID_EVMETH_ONREADYSTATECHANGE
,
219 {resizeW
, onresizeW
, EVENTT_NONE
, DISPID_EVMETH_ONRESIZE
,
220 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
221 {scrollW
, onscrollW
, EVENTT_HTML
, DISPID_EVMETH_ONSCROLL
,
222 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
223 {selectstartW
, onselectstartW
, EVENTT_MOUSE
, DISPID_EVMETH_ONSELECTSTART
,
225 {submitW
, onsubmitW
, EVENTT_HTML
, DISPID_EVMETH_ONSUBMIT
,
226 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
|EVENT_HASDEFAULTHANDLERS
}
229 eventid_t
str_to_eid(LPCWSTR str
)
233 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
234 if(!strcmpW(event_info
[i
].name
, str
))
238 ERR("unknown type %s\n", debugstr_w(str
));
242 static eventid_t
attr_to_eid(LPCWSTR str
)
246 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
247 if(!strcmpW(event_info
[i
].attr_name
, str
))
256 IHTMLEventObj IHTMLEventObj_iface
;
261 const event_info_t
*type
;
262 nsIDOMEvent
*nsevent
;
263 BOOL prevent_default
;
267 static inline HTMLEventObj
*impl_from_IHTMLEventObj(IHTMLEventObj
*iface
)
269 return CONTAINING_RECORD(iface
, HTMLEventObj
, IHTMLEventObj_iface
);
272 static HRESULT WINAPI
HTMLEventObj_QueryInterface(IHTMLEventObj
*iface
, REFIID riid
, void **ppv
)
274 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
276 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
278 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
279 *ppv
= &This
->IHTMLEventObj_iface
;
280 }else if(IsEqualGUID(&IID_IHTMLEventObj
, riid
)) {
281 *ppv
= &This
->IHTMLEventObj_iface
;
282 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
283 return *ppv
? S_OK
: E_NOINTERFACE
;
286 WARN("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
287 return E_NOINTERFACE
;
290 IUnknown_AddRef((IUnknown
*)*ppv
);
294 static ULONG WINAPI
HTMLEventObj_AddRef(IHTMLEventObj
*iface
)
296 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
297 LONG ref
= InterlockedIncrement(&This
->ref
);
299 TRACE("(%p) ref=%d\n", This
, ref
);
304 static ULONG WINAPI
HTMLEventObj_Release(IHTMLEventObj
*iface
)
306 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
307 LONG ref
= InterlockedDecrement(&This
->ref
);
309 TRACE("(%p) ref=%d\n", This
, ref
);
313 IHTMLDOMNode_Release(&This
->target
->IHTMLDOMNode_iface
);
315 nsIDOMEvent_Release(This
->nsevent
);
316 release_dispex(&This
->dispex
);
323 static HRESULT WINAPI
HTMLEventObj_GetTypeInfoCount(IHTMLEventObj
*iface
, UINT
*pctinfo
)
325 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
326 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
329 static HRESULT WINAPI
HTMLEventObj_GetTypeInfo(IHTMLEventObj
*iface
, UINT iTInfo
,
330 LCID lcid
, ITypeInfo
**ppTInfo
)
332 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
333 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
336 static HRESULT WINAPI
HTMLEventObj_GetIDsOfNames(IHTMLEventObj
*iface
, REFIID riid
,
337 LPOLESTR
*rgszNames
, UINT cNames
,
338 LCID lcid
, DISPID
*rgDispId
)
340 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
341 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
345 static HRESULT WINAPI
HTMLEventObj_Invoke(IHTMLEventObj
*iface
, DISPID dispIdMember
,
346 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
347 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
349 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
350 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
351 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
354 static HRESULT WINAPI
HTMLEventObj_get_srcElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
356 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
358 TRACE("(%p)->(%p)\n", This
, p
);
362 IHTMLDOMNode_QueryInterface(&This
->target
->IHTMLDOMNode_iface
, &IID_IHTMLElement
, (void**)p
);
366 static HRESULT WINAPI
HTMLEventObj_get_altKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
368 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
369 cpp_bool ret
= FALSE
;
371 TRACE("(%p)->(%p)\n", This
, p
);
374 nsIDOMKeyEvent
*key_event
;
377 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
378 if(NS_SUCCEEDED(nsres
)) {
379 nsIDOMKeyEvent_GetAltKey(key_event
, &ret
);
380 nsIDOMKeyEvent_Release(key_event
);
382 nsIDOMMouseEvent
*mouse_event
;
384 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
385 if(NS_SUCCEEDED(nsres
)) {
386 nsIDOMMouseEvent_GetAltKey(mouse_event
, &ret
);
387 nsIDOMMouseEvent_Release(mouse_event
);
392 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
396 static HRESULT WINAPI
HTMLEventObj_get_ctrlKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
398 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
399 cpp_bool ret
= FALSE
;
401 TRACE("(%p)->(%p)\n", This
, p
);
404 nsIDOMKeyEvent
*key_event
;
407 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
408 if(NS_SUCCEEDED(nsres
)) {
409 nsIDOMKeyEvent_GetCtrlKey(key_event
, &ret
);
410 nsIDOMKeyEvent_Release(key_event
);
412 nsIDOMMouseEvent
*mouse_event
;
414 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
415 if(NS_SUCCEEDED(nsres
)) {
416 nsIDOMMouseEvent_GetCtrlKey(mouse_event
, &ret
);
417 nsIDOMMouseEvent_Release(mouse_event
);
422 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
426 static HRESULT WINAPI
HTMLEventObj_get_shiftKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
428 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
429 cpp_bool ret
= FALSE
;
431 TRACE("(%p)->(%p)\n", This
, p
);
434 nsIDOMKeyEvent
*key_event
;
437 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
438 if(NS_SUCCEEDED(nsres
)) {
439 nsIDOMKeyEvent_GetShiftKey(key_event
, &ret
);
440 nsIDOMKeyEvent_Release(key_event
);
442 nsIDOMMouseEvent
*mouse_event
;
444 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
445 if(NS_SUCCEEDED(nsres
)) {
446 nsIDOMMouseEvent_GetShiftKey(mouse_event
, &ret
);
447 nsIDOMMouseEvent_Release(mouse_event
);
452 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
456 static HRESULT WINAPI
HTMLEventObj_put_returnValue(IHTMLEventObj
*iface
, VARIANT v
)
458 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
460 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&v
));
462 if(V_VT(&v
) != VT_BOOL
) {
463 FIXME("unsupported value %s\n", debugstr_variant(&v
));
464 return DISP_E_BADVARTYPE
;
468 This
->prevent_default
= TRUE
;
472 static HRESULT WINAPI
HTMLEventObj_get_returnValue(IHTMLEventObj
*iface
, VARIANT
*p
)
474 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
476 FIXME("(%p)->(%p)\n", This
, p
);
482 static HRESULT WINAPI
HTMLEventObj_put_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL v
)
484 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
486 TRACE("(%p)->(%x)\n", This
, v
);
488 This
->cancel_bubble
= !!v
;
492 static HRESULT WINAPI
HTMLEventObj_get_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
494 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
496 TRACE("(%p)->(%p)\n", This
, p
);
498 *p
= This
->cancel_bubble
? VARIANT_TRUE
: VARIANT_FALSE
;
502 static HRESULT WINAPI
HTMLEventObj_get_fromElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
504 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
506 FIXME("(%p)->(%p)\n", This
, p
);
512 static HRESULT WINAPI
HTMLEventObj_get_toElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
514 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
516 FIXME("(%p)->(%p)\n", This
, p
);
522 static HRESULT WINAPI
HTMLEventObj_put_keyCode(IHTMLEventObj
*iface
, LONG v
)
524 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
525 FIXME("(%p)->(%d)\n", This
, v
);
529 static HRESULT WINAPI
HTMLEventObj_get_keyCode(IHTMLEventObj
*iface
, LONG
*p
)
531 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
534 TRACE("(%p)->(%p)\n", This
, p
);
537 nsIDOMKeyEvent
*key_event
;
540 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
541 if(NS_SUCCEEDED(nsres
)) {
542 nsIDOMKeyEvent_GetKeyCode(key_event
, &key_code
);
543 nsIDOMKeyEvent_Release(key_event
);
551 static HRESULT WINAPI
HTMLEventObj_get_button(IHTMLEventObj
*iface
, LONG
*p
)
553 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
556 TRACE("(%p)->(%p)\n", This
, p
);
559 nsIDOMMouseEvent
*mouse_event
;
562 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
563 if(NS_SUCCEEDED(nsres
)) {
564 nsIDOMMouseEvent_GetButton(mouse_event
, &button
);
565 nsIDOMMouseEvent_Release(mouse_event
);
573 static HRESULT WINAPI
HTMLEventObj_get_type(IHTMLEventObj
*iface
, BSTR
*p
)
575 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
577 TRACE("(%p)->(%p)\n", This
, p
);
584 *p
= SysAllocString(This
->type
->name
);
585 return *p
? S_OK
: E_OUTOFMEMORY
;
588 static HRESULT WINAPI
HTMLEventObj_get_qualifier(IHTMLEventObj
*iface
, BSTR
*p
)
590 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
592 FIXME("(%p)->(%p)\n", This
, p
);
598 static HRESULT WINAPI
HTMLEventObj_get_reason(IHTMLEventObj
*iface
, LONG
*p
)
600 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
602 FIXME("(%p)->(%p)\n", This
, p
);
608 static HRESULT WINAPI
HTMLEventObj_get_x(IHTMLEventObj
*iface
, LONG
*p
)
610 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
613 TRACE("(%p)->(%p)\n", This
, p
);
616 nsIDOMUIEvent
*ui_event
;
619 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMUIEvent
, (void**)&ui_event
);
620 if(NS_SUCCEEDED(nsres
)) {
621 /* NOTE: pageX is not exactly right here. */
622 nsres
= nsIDOMUIEvent_GetPageX(ui_event
, &x
);
623 assert(nsres
== NS_OK
);
624 nsIDOMUIEvent_Release(ui_event
);
632 static HRESULT WINAPI
HTMLEventObj_get_y(IHTMLEventObj
*iface
, LONG
*p
)
634 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
637 TRACE("(%p)->(%p)\n", This
, p
);
640 nsIDOMUIEvent
*ui_event
;
643 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMUIEvent
, (void**)&ui_event
);
644 if(NS_SUCCEEDED(nsres
)) {
645 /* NOTE: pageY is not exactly right here. */
646 nsres
= nsIDOMUIEvent_GetPageY(ui_event
, &y
);
647 assert(nsres
== NS_OK
);
648 nsIDOMUIEvent_Release(ui_event
);
656 static HRESULT WINAPI
HTMLEventObj_get_clientX(IHTMLEventObj
*iface
, LONG
*p
)
658 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
661 TRACE("(%p)->(%p)\n", This
, p
);
664 nsIDOMMouseEvent
*mouse_event
;
667 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
668 if(NS_SUCCEEDED(nsres
)) {
669 nsIDOMMouseEvent_GetClientX(mouse_event
, &x
);
670 nsIDOMMouseEvent_Release(mouse_event
);
678 static HRESULT WINAPI
HTMLEventObj_get_clientY(IHTMLEventObj
*iface
, LONG
*p
)
680 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
683 TRACE("(%p)->(%p)\n", This
, p
);
686 nsIDOMMouseEvent
*mouse_event
;
689 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
690 if(NS_SUCCEEDED(nsres
)) {
691 nsIDOMMouseEvent_GetClientY(mouse_event
, &y
);
692 nsIDOMMouseEvent_Release(mouse_event
);
700 static HRESULT WINAPI
HTMLEventObj_get_offsetX(IHTMLEventObj
*iface
, LONG
*p
)
702 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
704 FIXME("(%p)->(%p)\n", This
, p
);
710 static HRESULT WINAPI
HTMLEventObj_get_offsetY(IHTMLEventObj
*iface
, LONG
*p
)
712 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
714 FIXME("(%p)->(%p)\n", This
, p
);
720 static HRESULT WINAPI
HTMLEventObj_get_screenX(IHTMLEventObj
*iface
, LONG
*p
)
722 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
725 TRACE("(%p)->(%p)\n", This
, p
);
728 nsIDOMMouseEvent
*mouse_event
;
731 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
732 if(NS_SUCCEEDED(nsres
)) {
733 nsIDOMMouseEvent_GetScreenX(mouse_event
, &x
);
734 nsIDOMMouseEvent_Release(mouse_event
);
742 static HRESULT WINAPI
HTMLEventObj_get_screenY(IHTMLEventObj
*iface
, LONG
*p
)
744 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
747 TRACE("(%p)->(%p)\n", This
, p
);
750 nsIDOMMouseEvent
*mouse_event
;
753 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
754 if(NS_SUCCEEDED(nsres
)) {
755 nsIDOMMouseEvent_GetScreenY(mouse_event
, &y
);
756 nsIDOMMouseEvent_Release(mouse_event
);
764 static HRESULT WINAPI
HTMLEventObj_get_srcFilter(IHTMLEventObj
*iface
, IDispatch
**p
)
766 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
768 FIXME("(%p)->(%p)\n", This
, p
);
774 static const IHTMLEventObjVtbl HTMLEventObjVtbl
= {
775 HTMLEventObj_QueryInterface
,
777 HTMLEventObj_Release
,
778 HTMLEventObj_GetTypeInfoCount
,
779 HTMLEventObj_GetTypeInfo
,
780 HTMLEventObj_GetIDsOfNames
,
782 HTMLEventObj_get_srcElement
,
783 HTMLEventObj_get_altKey
,
784 HTMLEventObj_get_ctrlKey
,
785 HTMLEventObj_get_shiftKey
,
786 HTMLEventObj_put_returnValue
,
787 HTMLEventObj_get_returnValue
,
788 HTMLEventObj_put_cancelBubble
,
789 HTMLEventObj_get_cancelBubble
,
790 HTMLEventObj_get_fromElement
,
791 HTMLEventObj_get_toElement
,
792 HTMLEventObj_put_keyCode
,
793 HTMLEventObj_get_keyCode
,
794 HTMLEventObj_get_button
,
795 HTMLEventObj_get_type
,
796 HTMLEventObj_get_qualifier
,
797 HTMLEventObj_get_reason
,
800 HTMLEventObj_get_clientX
,
801 HTMLEventObj_get_clientY
,
802 HTMLEventObj_get_offsetX
,
803 HTMLEventObj_get_offsetY
,
804 HTMLEventObj_get_screenX
,
805 HTMLEventObj_get_screenY
,
806 HTMLEventObj_get_srcFilter
809 static inline HTMLEventObj
*unsafe_impl_from_IHTMLEventObj(IHTMLEventObj
*iface
)
811 return iface
->lpVtbl
== &HTMLEventObjVtbl
? impl_from_IHTMLEventObj(iface
) : NULL
;
814 static const tid_t HTMLEventObj_iface_tids
[] = {
819 static dispex_static_data_t HTMLEventObj_dispex
= {
823 HTMLEventObj_iface_tids
826 static HTMLEventObj
*create_event(void)
830 ret
= heap_alloc_zero(sizeof(*ret
));
834 ret
->IHTMLEventObj_iface
.lpVtbl
= &HTMLEventObjVtbl
;
837 init_dispex(&ret
->dispex
, (IUnknown
*)&ret
->IHTMLEventObj_iface
, &HTMLEventObj_dispex
);
842 static HRESULT
set_event_info(HTMLEventObj
*event
, HTMLDOMNode
*target
, eventid_t eid
, nsIDOMEvent
*nsevent
)
844 event
->type
= event_info
+eid
;
845 event
->nsevent
= nsevent
;
848 nsIDOMEvent_AddRef(nsevent
);
849 }else if(event_types
[event_info
[eid
].type
]) {
853 nsAString_InitDepend(&type_str
, event_types
[event_info
[eid
].type
]);
854 nsres
= nsIDOMHTMLDocument_CreateEvent(target
->doc
->nsdoc
, &type_str
, &event
->nsevent
);
855 nsAString_Finish(&type_str
);
856 if(NS_FAILED(nsres
)) {
857 ERR("Could not create event: %08x\n", nsres
);
862 event
->target
= target
;
864 IHTMLDOMNode_AddRef(&target
->IHTMLDOMNode_iface
);
868 HRESULT
create_event_obj(IHTMLEventObj
**ret
)
872 event
= create_event();
874 return E_OUTOFMEMORY
;
876 *ret
= &event
->IHTMLEventObj_iface
;
880 static HRESULT
call_disp_func(IDispatch
*disp
, DISPPARAMS
*dp
, VARIANT
*retv
)
886 memset(&ei
, 0, sizeof(ei
));
888 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
889 if(SUCCEEDED(hres
)) {
890 hres
= IDispatchEx_InvokeEx(dispex
, 0, GetUserDefaultLCID(), DISPATCH_METHOD
, dp
, retv
, &ei
, NULL
);
891 IDispatchEx_Release(dispex
);
893 TRACE("Could not get IDispatchEx interface: %08x\n", hres
);
894 hres
= IDispatch_Invoke(disp
, 0, &IID_NULL
, GetUserDefaultLCID(), DISPATCH_METHOD
,
895 dp
, retv
, &ei
, NULL
);
901 static HRESULT
call_cp_func(IDispatch
*disp
, DISPID dispid
, HTMLEventObj
*event_obj
, VARIANT
*retv
)
903 DISPPARAMS dp
= {NULL
,NULL
,0,0};
909 V_VT(&event_arg
) = VT_DISPATCH
;
910 V_DISPATCH(&event_arg
) = (IDispatch
*)&event_obj
->IHTMLEventObj_iface
;
911 dp
.rgvarg
= &event_arg
;
915 memset(&ei
, 0, sizeof(ei
));
916 return IDispatch_Invoke(disp
, dispid
, &IID_NULL
, 0, DISPATCH_METHOD
, &dp
, retv
, &ei
, &argerr
);
919 static BOOL
is_cp_event(cp_static_data_t
*data
, DISPID dispid
)
928 hres
= get_dispids(data
->tid
, &data
->id_cnt
, &data
->ids
);
934 max
= data
->id_cnt
-1;
937 if(data
->ids
[i
] == dispid
)
940 if(data
->ids
[i
] < dispid
)
949 static void call_event_handlers(HTMLDocumentNode
*doc
, HTMLEventObj
*event_obj
, event_target_t
*event_target
,
950 ConnectionPointContainer
*cp_container
, eventid_t eid
, IDispatch
*this_obj
)
952 const BOOL cancelable
= event_info
[eid
].flags
& EVENT_CANCELABLE
;
953 handler_vector_t
*handler_vector
= NULL
;
958 handler_vector
= event_target
->event_table
[eid
];
960 if(handler_vector
&& handler_vector
->handler_prop
) {
961 DISPID named_arg
= DISPID_THIS
;
963 DISPPARAMS dp
= {&arg
, &named_arg
, 1, 1};
965 V_VT(&arg
) = VT_DISPATCH
;
966 V_DISPATCH(&arg
) = this_obj
;
969 TRACE("%s >>>\n", debugstr_w(event_info
[eid
].name
));
970 hres
= call_disp_func(handler_vector
->handler_prop
, &dp
, &v
);
972 TRACE("%s <<< %s\n", debugstr_w(event_info
[eid
].name
), debugstr_variant(&v
));
975 if(V_VT(&v
) == VT_BOOL
) {
977 event_obj
->prevent_default
= TRUE
;
978 }else if(V_VT(&v
) != VT_EMPTY
) {
979 FIXME("unhandled result %s\n", debugstr_variant(&v
));
984 WARN("%s <<< %08x\n", debugstr_w(event_info
[eid
].name
), hres
);
988 if(handler_vector
&& handler_vector
->handler_cnt
) {
990 DISPPARAMS dp
= {&arg
, NULL
, 1, 0};
993 V_VT(&arg
) = VT_DISPATCH
;
994 V_DISPATCH(&arg
) = (IDispatch
*)event_obj
;
996 i
= handler_vector
->handler_cnt
;
998 if(handler_vector
->handlers
[i
]) {
1001 TRACE("%s [%d] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
1002 hres
= call_disp_func(handler_vector
->handlers
[i
], &dp
, &v
);
1004 TRACE("%s [%d] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
1007 if(V_VT(&v
) == VT_BOOL
) {
1009 event_obj
->prevent_default
= TRUE
;
1010 }else if(V_VT(&v
) != VT_EMPTY
) {
1011 FIXME("unhandled result %s\n", debugstr_variant(&v
));
1016 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
1023 * NOTE: CP events may require doc_obj reference, which we don't own. We make sure that
1024 * it's safe to call event handler by checking nsevent_listener, which is NULL for
1025 * detached documents.
1027 if(cp_container
&& cp_container
->forward_container
)
1028 cp_container
= cp_container
->forward_container
;
1029 if(cp_container
&& cp_container
->cps
&& doc
->nsevent_listener
) {
1030 ConnectionPoint
*cp
;
1033 for(j
=0; cp_container
->cp_entries
[j
].riid
; j
++) {
1034 cp
= cp_container
->cps
+ j
;
1035 if(!cp
->sinks_size
|| !is_cp_event(cp
->data
, event_info
[eid
].dispid
))
1038 for(i
=0; doc
->nsevent_listener
&& i
< cp
->sinks_size
; i
++) {
1039 if(!cp
->sinks
[i
].disp
)
1042 V_VT(&v
) = VT_EMPTY
;
1044 TRACE("cp %s [%u] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
1045 hres
= call_cp_func(cp
->sinks
[i
].disp
, event_info
[eid
].dispid
,
1046 cp
->data
->pass_event_arg
? event_obj
: NULL
, &v
);
1048 TRACE("cp %s [%u] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
1051 if(V_VT(&v
) == VT_BOOL
) {
1053 event_obj
->prevent_default
= TRUE
;
1054 }else if(V_VT(&v
) != VT_EMPTY
) {
1055 FIXME("unhandled result %s\n", debugstr_variant(&v
));
1060 WARN("cp %s [%u] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
1064 if(!doc
->nsevent_listener
)
1070 static void fire_event_obj(HTMLDocumentNode
*doc
, eventid_t eid
, HTMLEventObj
*event_obj
,
1071 nsIDOMNode
*target
, IDispatch
*script_this
)
1073 IHTMLEventObj
*prev_event
;
1074 nsIDOMNode
*parent
, *nsnode
;
1075 BOOL prevent_default
= FALSE
;
1076 HTMLInnerWindow
*window
;
1082 TRACE("(%p) %s\n", doc
, debugstr_w(event_info
[eid
].name
));
1084 window
= doc
->window
;
1086 WARN("NULL window\n");
1090 htmldoc_addref(&doc
->basedoc
);
1092 prev_event
= window
->event
;
1093 window
->event
= event_obj
? &event_obj
->IHTMLEventObj_iface
: NULL
;
1095 nsIDOMNode_GetNodeType(target
, &node_type
);
1097 nsIDOMNode_AddRef(nsnode
);
1102 hres
= get_node(doc
, nsnode
, FALSE
, &node
);
1103 if(SUCCEEDED(hres
) && node
) {
1104 call_event_handlers(doc
, event_obj
, *get_node_event_target(node
),
1105 node
->cp_container
, eid
, script_this
? script_this
: (IDispatch
*)&node
->IHTMLDOMNode_iface
);
1109 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
) || (event_obj
&& event_obj
->cancel_bubble
))
1112 nsIDOMNode_GetParentNode(nsnode
, &parent
);
1113 nsIDOMNode_Release(nsnode
);
1118 nsIDOMNode_GetNodeType(nsnode
, &node_type
);
1119 }while(node_type
== ELEMENT_NODE
);
1121 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
) || (event_obj
&& event_obj
->cancel_bubble
))
1125 if(event_info
[eid
].flags
& EVENT_FORWARDBODY
) {
1126 nsIDOMHTMLElement
*nsbody
;
1129 nsres
= nsIDOMHTMLDocument_GetBody(doc
->nsdoc
, &nsbody
);
1130 if(NS_SUCCEEDED(nsres
) && nsbody
) {
1131 hres
= get_node(doc
, (nsIDOMNode
*)nsbody
, FALSE
, &node
);
1132 if(SUCCEEDED(hres
) && node
) {
1133 call_event_handlers(doc
, event_obj
, *get_node_event_target(node
),
1134 node
->cp_container
, eid
, script_this
? script_this
: (IDispatch
*)&node
->IHTMLDOMNode_iface
);
1137 nsIDOMHTMLElement_Release(nsbody
);
1139 ERR("Could not get body: %08x\n", nsres
);
1143 call_event_handlers(doc
, event_obj
, doc
->node
.event_target
, &doc
->basedoc
.cp_container
, eid
,
1144 script_this
? script_this
: (IDispatch
*)&doc
->basedoc
.IHTMLDocument2_iface
);
1148 FIXME("unimplemented node type %d\n", node_type
);
1152 nsIDOMNode_Release(nsnode
);
1154 if(event_obj
&& event_obj
->prevent_default
)
1155 prevent_default
= TRUE
;
1156 window
->event
= prev_event
;
1158 if(!prevent_default
&& (event_info
[eid
].flags
& EVENT_HASDEFAULTHANDLERS
)) {
1159 nsIDOMNode_AddRef(target
);
1163 hres
= get_node(doc
, nsnode
, TRUE
, &node
);
1168 if(node
->vtbl
->handle_event
)
1169 hres
= node
->vtbl
->handle_event(node
, eid
, event_obj
? event_obj
->nsevent
: NULL
, &prevent_default
);
1171 if(FAILED(hres
) || prevent_default
|| (event_obj
&& event_obj
->cancel_bubble
))
1175 nsres
= nsIDOMNode_GetParentNode(nsnode
, &parent
);
1176 if(NS_FAILED(nsres
))
1179 nsIDOMNode_Release(nsnode
);
1184 nsIDOMNode_Release(nsnode
);
1187 if(prevent_default
&& event_obj
&& event_obj
->nsevent
) {
1188 TRACE("calling PreventDefault\n");
1189 nsIDOMEvent_PreventDefault(event_obj
->nsevent
);
1192 htmldoc_release(&doc
->basedoc
);
1195 void fire_event(HTMLDocumentNode
*doc
, eventid_t eid
, BOOL set_event
, nsIDOMNode
*target
, nsIDOMEvent
*nsevent
,
1196 IDispatch
*script_this
)
1198 HTMLEventObj
*event_obj
= NULL
;
1203 hres
= get_node(doc
, target
, TRUE
, &node
);
1207 event_obj
= create_event();
1212 hres
= set_event_info(event_obj
, node
, eid
, nsevent
);
1214 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1219 fire_event_obj(doc
, eid
, event_obj
, target
, script_this
);
1222 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1225 HRESULT
dispatch_event(HTMLDOMNode
*node
, const WCHAR
*event_name
, VARIANT
*event_var
, VARIANT_BOOL
*cancelled
)
1227 HTMLEventObj
*event_obj
= NULL
;
1231 eid
= attr_to_eid(event_name
);
1232 if(eid
== EVENTID_LAST
) {
1233 WARN("unknown event %s\n", debugstr_w(event_name
));
1234 return E_INVALIDARG
;
1237 if(event_var
&& V_VT(event_var
) != VT_EMPTY
&& V_VT(event_var
) != VT_ERROR
) {
1238 if(V_VT(event_var
) != VT_DISPATCH
) {
1239 FIXME("event_var %s not supported\n", debugstr_variant(event_var
));
1243 if(V_DISPATCH(event_var
)) {
1244 IHTMLEventObj
*event_iface
;
1246 hres
= IDispatch_QueryInterface(V_DISPATCH(event_var
), &IID_IHTMLEventObj
, (void**)&event_iface
);
1248 FIXME("No IHTMLEventObj iface\n");
1252 event_obj
= unsafe_impl_from_IHTMLEventObj(event_iface
);
1254 ERR("Not our IHTMLEventObj?\n");
1255 IHTMLEventObj_Release(event_iface
);
1262 hres
= set_event_info(event_obj
, node
, eid
, NULL
);
1264 fire_event_obj(node
->doc
, eid
, event_obj
, node
->nsnode
, NULL
);
1266 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1270 if(!(event_info
[eid
].flags
& EVENT_DEFAULTLISTENER
)) {
1271 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1275 fire_event(node
->doc
, eid
, TRUE
, node
->nsnode
, NULL
, NULL
);
1278 *cancelled
= VARIANT_TRUE
; /* FIXME */
1282 HRESULT
call_fire_event(HTMLDOMNode
*node
, eventid_t eid
)
1286 if(node
->vtbl
->fire_event
) {
1287 BOOL handled
= FALSE
;
1289 hres
= node
->vtbl
->fire_event(node
, eid
, &handled
);
1294 fire_event(node
->doc
, eid
, TRUE
, node
->nsnode
, NULL
, NULL
);
1298 static inline event_target_t
*get_event_target(event_target_t
**event_target_ptr
)
1300 if(!*event_target_ptr
)
1301 *event_target_ptr
= heap_alloc_zero(sizeof(event_target_t
));
1302 return *event_target_ptr
;
1305 static BOOL
alloc_handler_vector(event_target_t
*event_target
, eventid_t eid
, int cnt
)
1307 handler_vector_t
*new_vector
, *handler_vector
= event_target
->event_table
[eid
];
1309 if(handler_vector
) {
1310 if(cnt
<= handler_vector
->handler_cnt
)
1313 new_vector
= heap_realloc_zero(handler_vector
, sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
1315 new_vector
= heap_alloc_zero(sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
1321 new_vector
->handler_cnt
= cnt
;
1322 event_target
->event_table
[eid
] = new_vector
;
1326 static HRESULT
ensure_nsevent_handler(HTMLDocumentNode
*doc
, event_target_t
*event_target
, eventid_t eid
)
1328 nsIDOMNode
*nsnode
= NULL
;
1330 TRACE("%s\n", debugstr_w(event_info
[eid
].name
));
1332 if(!doc
->nsdoc
|| doc
->event_vector
[eid
] || !(event_info
[eid
].flags
& (EVENT_DEFAULTLISTENER
|EVENT_BIND_TO_BODY
)))
1335 if(event_info
[eid
].flags
& EVENT_BIND_TO_BODY
) {
1336 nsnode
= doc
->node
.nsnode
;
1337 nsIDOMNode_AddRef(nsnode
);
1340 doc
->event_vector
[eid
] = TRUE
;
1341 add_nsevent_listener(doc
, nsnode
, event_info
[eid
].name
);
1344 nsIDOMNode_Release(nsnode
);
1348 void detach_events(HTMLDocumentNode
*doc
)
1350 if(doc
->event_vector
) {
1353 for(i
=0; i
< EVENTID_LAST
; i
++) {
1354 if(doc
->event_vector
[i
]) {
1355 detach_nsevent(doc
, event_info
[i
].name
);
1356 doc
->event_vector
[i
] = FALSE
;
1361 release_nsevents(doc
);
1365 static HRESULT
remove_event_handler(event_target_t
**event_target
, eventid_t eid
)
1367 if(*event_target
&& (*event_target
)->event_table
[eid
] && (*event_target
)->event_table
[eid
]->handler_prop
) {
1368 IDispatch_Release((*event_target
)->event_table
[eid
]->handler_prop
);
1369 (*event_target
)->event_table
[eid
]->handler_prop
= NULL
;
1375 static HRESULT
set_event_handler_disp(event_target_t
**event_target_ptr
, HTMLDocumentNode
*doc
,
1376 eventid_t eid
, IDispatch
*disp
)
1378 event_target_t
*event_target
;
1381 return remove_event_handler(event_target_ptr
, eid
);
1383 event_target
= get_event_target(event_target_ptr
);
1385 return E_OUTOFMEMORY
;
1387 if(!alloc_handler_vector(event_target
, eid
, 0))
1388 return E_OUTOFMEMORY
;
1390 if(event_target
->event_table
[eid
]->handler_prop
)
1391 IDispatch_Release(event_target
->event_table
[eid
]->handler_prop
);
1393 event_target
->event_table
[eid
]->handler_prop
= disp
;
1394 IDispatch_AddRef(disp
);
1396 return ensure_nsevent_handler(doc
, event_target
, eid
);
1399 HRESULT
set_event_handler(event_target_t
**event_target
, HTMLDocumentNode
*doc
, eventid_t eid
, VARIANT
*var
)
1403 return remove_event_handler(event_target
, eid
);
1406 return set_event_handler_disp(event_target
, doc
, eid
, V_DISPATCH(var
));
1409 FIXME("not handler %s\n", debugstr_variant(var
));
1418 HRESULT
get_event_handler(event_target_t
**event_target
, eventid_t eid
, VARIANT
*var
)
1420 if(*event_target
&& (*event_target
)->event_table
[eid
] && (*event_target
)->event_table
[eid
]->handler_prop
) {
1421 V_VT(var
) = VT_DISPATCH
;
1422 V_DISPATCH(var
) = (*event_target
)->event_table
[eid
]->handler_prop
;
1423 IDispatch_AddRef(V_DISPATCH(var
));
1425 V_VT(var
) = VT_NULL
;
1431 HRESULT
attach_event(event_target_t
**event_target_ptr
, HTMLDocument
*doc
, BSTR name
,
1432 IDispatch
*disp
, VARIANT_BOOL
*res
)
1434 event_target_t
*event_target
;
1438 eid
= attr_to_eid(name
);
1439 if(eid
== EVENTID_LAST
) {
1440 WARN("Unknown event\n");
1441 *res
= VARIANT_TRUE
;
1445 event_target
= get_event_target(event_target_ptr
);
1447 return E_OUTOFMEMORY
;
1449 if(event_target
->event_table
[eid
]) {
1450 while(i
< event_target
->event_table
[eid
]->handler_cnt
&& event_target
->event_table
[eid
]->handlers
[i
])
1452 if(i
== event_target
->event_table
[eid
]->handler_cnt
&& !alloc_handler_vector(event_target
, eid
, i
+1))
1453 return E_OUTOFMEMORY
;
1454 }else if(!alloc_handler_vector(event_target
, eid
, i
+1)) {
1455 return E_OUTOFMEMORY
;
1458 IDispatch_AddRef(disp
);
1459 event_target
->event_table
[eid
]->handlers
[i
] = disp
;
1461 *res
= VARIANT_TRUE
;
1462 return ensure_nsevent_handler(doc
->doc_node
, event_target
, eid
);
1465 HRESULT
detach_event(event_target_t
*event_target
, HTMLDocument
*doc
, BSTR name
, IDispatch
*disp
)
1473 eid
= attr_to_eid(name
);
1474 if(eid
== EVENTID_LAST
) {
1475 WARN("Unknown event\n");
1479 if(!event_target
->event_table
[eid
])
1482 while(i
< event_target
->event_table
[eid
]->handler_cnt
) {
1483 if(event_target
->event_table
[eid
]->handlers
[i
] == disp
) {
1484 IDispatch_Release(event_target
->event_table
[eid
]->handlers
[i
]);
1485 event_target
->event_table
[eid
]->handlers
[i
] = NULL
;
1493 void bind_node_event(HTMLDocumentNode
*doc
, event_target_t
**event_target
, HTMLDOMNode
*node
, const WCHAR
*event
, IDispatch
*disp
)
1497 TRACE("(%p %p %p %s %p)\n", doc
, event_target
, node
, debugstr_w(event
), disp
);
1499 eid
= attr_to_eid(event
);
1500 if(eid
== EVENTID_LAST
) {
1501 WARN("Unsupported event %s\n", debugstr_w(event
));
1505 set_event_handler_disp(event_target
, doc
, eid
, disp
);
1508 void update_cp_events(HTMLInnerWindow
*window
, event_target_t
**event_target_ptr
, cp_static_data_t
*cp
)
1510 event_target_t
*event_target
;
1513 event_target
= get_event_target(event_target_ptr
);
1517 for(i
=0; i
< EVENTID_LAST
; i
++) {
1518 if((event_info
[i
].flags
& EVENT_DEFAULTLISTENER
) && is_cp_event(cp
, event_info
[i
].dispid
))
1519 ensure_nsevent_handler(window
->doc
, event_target
, i
);
1523 void check_event_attr(HTMLDocumentNode
*doc
, nsIDOMHTMLElement
*nselem
)
1525 const PRUnichar
*attr_value
;
1526 nsAString attr_value_str
;
1533 for(i
=0; i
< EVENTID_LAST
; i
++) {
1534 nsres
= get_elem_attr_value(nselem
, event_info
[i
].attr_name
, &attr_value_str
, &attr_value
);
1535 if(NS_SUCCEEDED(nsres
)) {
1539 TRACE("%p.%s = %s\n", nselem
, debugstr_w(event_info
[i
].attr_name
), debugstr_w(attr_value
));
1541 disp
= script_parse_event(doc
->window
, attr_value
);
1543 hres
= get_node(doc
, (nsIDOMNode
*)nselem
, TRUE
, &node
);
1544 if(SUCCEEDED(hres
)) {
1545 set_event_handler_disp(get_node_event_target(node
), node
->doc
, i
, disp
);
1548 IDispatch_Release(disp
);
1550 nsAString_Finish(&attr_value_str
);
1555 HRESULT
doc_init_events(HTMLDocumentNode
*doc
)
1560 doc
->event_vector
= heap_alloc_zero(EVENTID_LAST
*sizeof(BOOL
));
1561 if(!doc
->event_vector
)
1562 return E_OUTOFMEMORY
;
1566 for(i
=0; i
< EVENTID_LAST
; i
++) {
1567 if(event_info
[i
].flags
& EVENT_HASDEFAULTHANDLERS
) {
1568 hres
= ensure_nsevent_handler(doc
, NULL
, i
);
1577 void release_event_target(event_target_t
*event_target
)
1582 for(i
=0; i
< EVENTID_LAST
; i
++) {
1583 if(event_target
->event_table
[i
]) {
1584 if(event_target
->event_table
[i
]->handler_prop
)
1585 IDispatch_Release(event_target
->event_table
[i
]->handler_prop
);
1586 for(j
=0; j
< event_target
->event_table
[i
]->handler_cnt
; j
++)
1587 if(event_target
->event_table
[i
]->handlers
[j
])
1588 IDispatch_Release(event_target
->event_table
[i
]->handlers
[j
]);
1592 heap_free(event_target
);