2 * Copyright 2008-2009 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "mshtml_private.h"
30 #include "htmlevent.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
37 IDispatch
*handler_prop
;
39 IDispatch
*handlers
[0];
42 struct event_target_t
{
43 DWORD node_handlers_mask
;
44 handler_vector_t
*event_table
[EVENTID_LAST
];
47 static const WCHAR beforeunloadW
[] = {'b','e','f','o','r','e','u','n','l','o','a','d',0};
48 static const WCHAR onbeforeunloadW
[] = {'o','n','b','e','f','o','r','e','u','n','l','o','a','d',0};
50 static const WCHAR blurW
[] = {'b','l','u','r',0};
51 static const WCHAR onblurW
[] = {'o','n','b','l','u','r',0};
53 static const WCHAR changeW
[] = {'c','h','a','n','g','e',0};
54 static const WCHAR onchangeW
[] = {'o','n','c','h','a','n','g','e',0};
56 static const WCHAR clickW
[] = {'c','l','i','c','k',0};
57 static const WCHAR onclickW
[] = {'o','n','c','l','i','c','k',0};
59 static const WCHAR contextmenuW
[] = {'c','o','n','t','e','x','t','m','e','n','u',0};
60 static const WCHAR oncontextmenuW
[] = {'o','n','c','o','n','t','e','x','t','m','e','n','u',0};
62 static const WCHAR dblclickW
[] = {'d','b','l','c','l','i','c','k',0};
63 static const WCHAR ondblclickW
[] = {'o','n','d','b','l','c','l','i','c','k',0};
65 static const WCHAR dragW
[] = {'d','r','a','g',0};
66 static const WCHAR ondragW
[] = {'o','n','d','r','a','g',0};
68 static const WCHAR dragstartW
[] = {'d','r','a','g','s','t','a','r','t',0};
69 static const WCHAR ondragstartW
[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
71 static const WCHAR focusW
[] = {'f','o','c','u','s',0};
72 static const WCHAR onfocusW
[] = {'o','n','f','o','c','u','s',0};
74 static const WCHAR keydownW
[] = {'k','e','y','d','o','w','n',0};
75 static const WCHAR onkeydownW
[] = {'o','n','k','e','y','d','o','w','n',0};
77 static const WCHAR keyupW
[] = {'k','e','y','u','p',0};
78 static const WCHAR onkeyupW
[] = {'o','n','k','e','y','u','p',0};
80 static const WCHAR loadW
[] = {'l','o','a','d',0};
81 static const WCHAR onloadW
[] = {'o','n','l','o','a','d',0};
83 static const WCHAR mousedownW
[] = {'m','o','u','s','e','d','o','w','n',0};
84 static const WCHAR onmousedownW
[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
86 static const WCHAR mousemoveW
[] = {'m','o','u','s','e','m','o','v','e',0};
87 static const WCHAR onmousemoveW
[] = {'o','n','m','o','u','s','e','m','o','v','e',0};
89 static const WCHAR mouseoutW
[] = {'m','o','u','s','e','o','u','t',0};
90 static const WCHAR onmouseoutW
[] = {'o','n','m','o','u','s','e','o','u','t',0};
92 static const WCHAR mouseoverW
[] = {'m','o','u','s','e','o','v','e','r',0};
93 static const WCHAR onmouseoverW
[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
95 static const WCHAR mouseupW
[] = {'m','o','u','s','e','u','p',0};
96 static const WCHAR onmouseupW
[] = {'o','n','m','o','u','s','e','u','p',0};
98 static const WCHAR pasteW
[] = {'p','a','s','t','e',0};
99 static const WCHAR onpasteW
[] = {'o','n','p','a','s','t','e',0};
101 static const WCHAR readystatechangeW
[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
102 static const WCHAR onreadystatechangeW
[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
104 static const WCHAR resizeW
[] = {'r','e','s','i','z','e',0};
105 static const WCHAR onresizeW
[] = {'o','n','r','e','s','i','z','e',0};
107 static const WCHAR selectstartW
[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
108 static const WCHAR onselectstartW
[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
110 static const WCHAR submitW
[] = {'s','u','b','m','i','t',0};
111 static const WCHAR onsubmitW
[] = {'o','n','s','u','b','m','i','t',0};
113 static const WCHAR HTMLEventsW
[] = {'H','T','M','L','E','v','e','n','t','s',0};
114 static const WCHAR KeyboardEventW
[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
115 static const WCHAR MouseEventW
[] = {'M','o','u','s','e','E','v','e','n','t',0};
124 static const WCHAR
*event_types
[] = {
139 #define EVENT_DEFAULTLISTENER 0x0001
140 #define EVENT_BUBBLE 0x0002
141 #define EVENT_FORWARDBODY 0x0004
142 #define EVENT_NODEHANDLER 0x0008
143 #define EVENT_CANCELABLE 0x0010
145 static const event_info_t event_info
[] = {
146 {beforeunloadW
, onbeforeunloadW
, EVENTT_NONE
, DISPID_EVMETH_ONBEFOREUNLOAD
,
147 EVENT_DEFAULTLISTENER
|EVENT_FORWARDBODY
},
148 {blurW
, onblurW
, EVENTT_HTML
, DISPID_EVMETH_ONBLUR
,
149 EVENT_DEFAULTLISTENER
},
150 {changeW
, onchangeW
, EVENTT_HTML
, DISPID_EVMETH_ONCHANGE
,
151 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
152 {clickW
, onclickW
, EVENTT_MOUSE
, DISPID_EVMETH_ONCLICK
,
153 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
},
154 {contextmenuW
, oncontextmenuW
, EVENTT_MOUSE
, DISPID_EVMETH_ONCONTEXTMENU
,
155 EVENT_BUBBLE
|EVENT_CANCELABLE
},
156 {dblclickW
, ondblclickW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDBLCLICK
,
157 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
},
158 {dragW
, ondragW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDRAG
,
160 {dragstartW
, ondragstartW
, EVENTT_MOUSE
, DISPID_EVMETH_ONDRAGSTART
,
162 {focusW
, onfocusW
, EVENTT_HTML
, DISPID_EVMETH_ONFOCUS
,
163 EVENT_DEFAULTLISTENER
},
164 {keydownW
, onkeydownW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYDOWN
,
165 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
166 {keyupW
, onkeyupW
, EVENTT_KEY
, DISPID_EVMETH_ONKEYUP
,
167 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
168 {loadW
, onloadW
, EVENTT_HTML
, DISPID_EVMETH_ONLOAD
,
170 {mousedownW
, onmousedownW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEDOWN
,
171 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
172 {mousemoveW
, onmousemoveW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEMOVE
,
173 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
174 {mouseoutW
, onmouseoutW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEOUT
,
175 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
176 {mouseoverW
, onmouseoverW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEOVER
,
177 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
178 {mouseupW
, onmouseupW
, EVENTT_MOUSE
, DISPID_EVMETH_ONMOUSEUP
,
179 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
180 {pasteW
, onpasteW
, EVENTT_NONE
, DISPID_EVMETH_ONPASTE
,
182 {readystatechangeW
, onreadystatechangeW
, EVENTT_NONE
, DISPID_EVMETH_ONREADYSTATECHANGE
,
184 {resizeW
, onresizeW
, EVENTT_NONE
, DISPID_EVMETH_ONRESIZE
,
185 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
186 {selectstartW
, onselectstartW
, EVENTT_MOUSE
, DISPID_EVMETH_ONSELECTSTART
,
188 {submitW
, onsubmitW
, EVENTT_HTML
, DISPID_EVMETH_ONSUBMIT
,
189 EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
|EVENT_CANCELABLE
}
192 static const eventid_t node_handled_list
[] = { EVENTID_LOAD
};
194 eventid_t
str_to_eid(LPCWSTR str
)
198 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
199 if(!strcmpW(event_info
[i
].name
, str
))
203 ERR("unknown type %s\n", debugstr_w(str
));
207 static eventid_t
attr_to_eid(LPCWSTR str
)
211 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
212 if(!strcmpW(event_info
[i
].attr_name
, str
))
219 static DWORD
get_node_handler_mask(eventid_t eid
)
223 for(i
=0; i
<sizeof(node_handled_list
)/sizeof(*node_handled_list
); i
++) {
224 if(node_handled_list
[i
] == eid
)
228 ERR("Invalid eid %d\n", eid
);
234 IHTMLEventObj IHTMLEventObj_iface
;
239 const event_info_t
*type
;
240 nsIDOMEvent
*nsevent
;
241 BOOL prevent_default
;
244 static inline HTMLEventObj
*impl_from_IHTMLEventObj(IHTMLEventObj
*iface
)
246 return CONTAINING_RECORD(iface
, HTMLEventObj
, IHTMLEventObj_iface
);
249 static HRESULT WINAPI
HTMLEventObj_QueryInterface(IHTMLEventObj
*iface
, REFIID riid
, void **ppv
)
251 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
255 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
256 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
257 *ppv
= &This
->IHTMLEventObj_iface
;
258 }else if(IsEqualGUID(&IID_IHTMLEventObj
, riid
)) {
259 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This
, ppv
);
260 *ppv
= &This
->IHTMLEventObj_iface
;
261 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
262 return *ppv
? S_OK
: E_NOINTERFACE
;
266 IUnknown_AddRef((IUnknown
*)*ppv
);
270 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
271 return E_NOINTERFACE
;
274 static ULONG WINAPI
HTMLEventObj_AddRef(IHTMLEventObj
*iface
)
276 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
277 LONG ref
= InterlockedIncrement(&This
->ref
);
279 TRACE("(%p) ref=%d\n", This
, ref
);
284 static ULONG WINAPI
HTMLEventObj_Release(IHTMLEventObj
*iface
)
286 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
287 LONG ref
= InterlockedDecrement(&This
->ref
);
289 TRACE("(%p) ref=%d\n", This
, ref
);
293 nsIDOMEvent_Release(This
->nsevent
);
294 release_dispex(&This
->dispex
);
301 static HRESULT WINAPI
HTMLEventObj_GetTypeInfoCount(IHTMLEventObj
*iface
, UINT
*pctinfo
)
303 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
304 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
307 static HRESULT WINAPI
HTMLEventObj_GetTypeInfo(IHTMLEventObj
*iface
, UINT iTInfo
,
308 LCID lcid
, ITypeInfo
**ppTInfo
)
310 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
311 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
314 static HRESULT WINAPI
HTMLEventObj_GetIDsOfNames(IHTMLEventObj
*iface
, REFIID riid
,
315 LPOLESTR
*rgszNames
, UINT cNames
,
316 LCID lcid
, DISPID
*rgDispId
)
318 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
319 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
323 static HRESULT WINAPI
HTMLEventObj_Invoke(IHTMLEventObj
*iface
, DISPID dispIdMember
,
324 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
325 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
327 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
328 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
329 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
332 static HRESULT WINAPI
HTMLEventObj_get_srcElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
334 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
336 TRACE("(%p)->(%p)\n", This
, p
);
338 return IHTMLDOMNode_QueryInterface(&This
->target
->IHTMLDOMNode_iface
, &IID_IHTMLElement
,
342 static HRESULT WINAPI
HTMLEventObj_get_altKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
344 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
347 TRACE("(%p)->(%p)\n", This
, p
);
350 nsIDOMKeyEvent
*key_event
;
353 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
354 if(NS_SUCCEEDED(nsres
)) {
355 nsIDOMKeyEvent_GetAltKey(key_event
, &ret
);
356 nsIDOMKeyEvent_Release(key_event
);
358 nsIDOMMouseEvent
*mouse_event
;
360 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
361 if(NS_SUCCEEDED(nsres
)) {
362 nsIDOMMouseEvent_GetAltKey(mouse_event
, &ret
);
363 nsIDOMMouseEvent_Release(mouse_event
);
368 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
372 static HRESULT WINAPI
HTMLEventObj_get_ctrlKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
374 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
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_GetCtrlKey(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_GetCtrlKey(mouse_event
, &ret
);
393 nsIDOMMouseEvent_Release(mouse_event
);
398 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
402 static HRESULT WINAPI
HTMLEventObj_get_shiftKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
404 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
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_GetShiftKey(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_GetShiftKey(mouse_event
, &ret
);
423 nsIDOMMouseEvent_Release(mouse_event
);
428 *p
= ret
? VARIANT_TRUE
: VARIANT_FALSE
;
432 static HRESULT WINAPI
HTMLEventObj_put_returnValue(IHTMLEventObj
*iface
, VARIANT v
)
434 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
435 FIXME("(%p)->()\n", This
);
439 static HRESULT WINAPI
HTMLEventObj_get_returnValue(IHTMLEventObj
*iface
, VARIANT
*p
)
441 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
443 FIXME("(%p)->(%p)\n", This
, p
);
449 static HRESULT WINAPI
HTMLEventObj_put_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL v
)
451 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
452 FIXME("(%p)->(%x)\n", This
, v
);
456 static HRESULT WINAPI
HTMLEventObj_get_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
458 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
460 FIXME("(%p)->(%p)\n", This
, p
);
466 static HRESULT WINAPI
HTMLEventObj_get_fromElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
468 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
470 FIXME("(%p)->(%p)\n", This
, p
);
476 static HRESULT WINAPI
HTMLEventObj_get_toElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
478 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
480 FIXME("(%p)->(%p)\n", This
, p
);
486 static HRESULT WINAPI
HTMLEventObj_put_keyCode(IHTMLEventObj
*iface
, LONG v
)
488 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
489 FIXME("(%p)->(%d)\n", This
, v
);
493 static HRESULT WINAPI
HTMLEventObj_get_keyCode(IHTMLEventObj
*iface
, LONG
*p
)
495 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
496 PRUint32 key_code
= 0;
498 TRACE("(%p)->(%p)\n", This
, p
);
501 nsIDOMKeyEvent
*key_event
;
504 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMKeyEvent
, (void**)&key_event
);
505 if(NS_SUCCEEDED(nsres
)) {
506 nsIDOMKeyEvent_GetKeyCode(key_event
, &key_code
);
507 nsIDOMKeyEvent_Release(key_event
);
515 static HRESULT WINAPI
HTMLEventObj_get_button(IHTMLEventObj
*iface
, LONG
*p
)
517 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
520 TRACE("(%p)->(%p)\n", This
, p
);
523 nsIDOMMouseEvent
*mouse_event
;
526 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
527 if(NS_SUCCEEDED(nsres
)) {
528 nsIDOMMouseEvent_GetButton(mouse_event
, &button
);
529 nsIDOMMouseEvent_Release(mouse_event
);
537 static HRESULT WINAPI
HTMLEventObj_get_type(IHTMLEventObj
*iface
, BSTR
*p
)
539 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
541 TRACE("(%p)->(%p)\n", This
, p
);
543 *p
= SysAllocString(This
->type
->name
);
544 return *p
? S_OK
: E_OUTOFMEMORY
;
547 static HRESULT WINAPI
HTMLEventObj_get_qualifier(IHTMLEventObj
*iface
, BSTR
*p
)
549 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
551 FIXME("(%p)->(%p)\n", This
, p
);
557 static HRESULT WINAPI
HTMLEventObj_get_reason(IHTMLEventObj
*iface
, LONG
*p
)
559 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
561 FIXME("(%p)->(%p)\n", This
, p
);
567 static HRESULT WINAPI
HTMLEventObj_get_x(IHTMLEventObj
*iface
, LONG
*p
)
569 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
571 FIXME("(%p)->(%p)\n", This
, p
);
577 static HRESULT WINAPI
HTMLEventObj_get_y(IHTMLEventObj
*iface
, LONG
*p
)
579 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
581 FIXME("(%p)->(%p)\n", This
, p
);
587 static HRESULT WINAPI
HTMLEventObj_get_clientX(IHTMLEventObj
*iface
, LONG
*p
)
589 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
592 TRACE("(%p)->(%p)\n", This
, p
);
595 nsIDOMMouseEvent
*mouse_event
;
598 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
599 if(NS_SUCCEEDED(nsres
)) {
600 nsIDOMMouseEvent_GetClientX(mouse_event
, &x
);
601 nsIDOMMouseEvent_Release(mouse_event
);
609 static HRESULT WINAPI
HTMLEventObj_get_clientY(IHTMLEventObj
*iface
, LONG
*p
)
611 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
614 TRACE("(%p)->(%p)\n", This
, p
);
617 nsIDOMMouseEvent
*mouse_event
;
620 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
621 if(NS_SUCCEEDED(nsres
)) {
622 nsIDOMMouseEvent_GetClientY(mouse_event
, &y
);
623 nsIDOMMouseEvent_Release(mouse_event
);
631 static HRESULT WINAPI
HTMLEventObj_get_offsetX(IHTMLEventObj
*iface
, LONG
*p
)
633 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
635 FIXME("(%p)->(%p)\n", This
, p
);
641 static HRESULT WINAPI
HTMLEventObj_get_offsetY(IHTMLEventObj
*iface
, LONG
*p
)
643 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
645 FIXME("(%p)->(%p)\n", This
, p
);
651 static HRESULT WINAPI
HTMLEventObj_get_screenX(IHTMLEventObj
*iface
, LONG
*p
)
653 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
656 TRACE("(%p)->(%p)\n", This
, p
);
659 nsIDOMMouseEvent
*mouse_event
;
662 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
663 if(NS_SUCCEEDED(nsres
)) {
664 nsIDOMMouseEvent_GetScreenX(mouse_event
, &x
);
665 nsIDOMMouseEvent_Release(mouse_event
);
673 static HRESULT WINAPI
HTMLEventObj_get_screenY(IHTMLEventObj
*iface
, LONG
*p
)
675 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
678 TRACE("(%p)->(%p)\n", This
, p
);
681 nsIDOMMouseEvent
*mouse_event
;
684 nsres
= nsIDOMEvent_QueryInterface(This
->nsevent
, &IID_nsIDOMMouseEvent
, (void**)&mouse_event
);
685 if(NS_SUCCEEDED(nsres
)) {
686 nsIDOMMouseEvent_GetScreenY(mouse_event
, &y
);
687 nsIDOMMouseEvent_Release(mouse_event
);
695 static HRESULT WINAPI
HTMLEventObj_get_srcFilter(IHTMLEventObj
*iface
, IDispatch
**p
)
697 HTMLEventObj
*This
= impl_from_IHTMLEventObj(iface
);
699 FIXME("(%p)->(%p)\n", This
, p
);
705 static const IHTMLEventObjVtbl HTMLEventObjVtbl
= {
706 HTMLEventObj_QueryInterface
,
708 HTMLEventObj_Release
,
709 HTMLEventObj_GetTypeInfoCount
,
710 HTMLEventObj_GetTypeInfo
,
711 HTMLEventObj_GetIDsOfNames
,
713 HTMLEventObj_get_srcElement
,
714 HTMLEventObj_get_altKey
,
715 HTMLEventObj_get_ctrlKey
,
716 HTMLEventObj_get_shiftKey
,
717 HTMLEventObj_put_returnValue
,
718 HTMLEventObj_get_returnValue
,
719 HTMLEventObj_put_cancelBubble
,
720 HTMLEventObj_get_cancelBubble
,
721 HTMLEventObj_get_fromElement
,
722 HTMLEventObj_get_toElement
,
723 HTMLEventObj_put_keyCode
,
724 HTMLEventObj_get_keyCode
,
725 HTMLEventObj_get_button
,
726 HTMLEventObj_get_type
,
727 HTMLEventObj_get_qualifier
,
728 HTMLEventObj_get_reason
,
731 HTMLEventObj_get_clientX
,
732 HTMLEventObj_get_clientY
,
733 HTMLEventObj_get_offsetX
,
734 HTMLEventObj_get_offsetY
,
735 HTMLEventObj_get_screenX
,
736 HTMLEventObj_get_screenY
,
737 HTMLEventObj_get_srcFilter
740 static const tid_t HTMLEventObj_iface_tids
[] = {
745 static dispex_static_data_t HTMLEventObj_dispex
= {
749 HTMLEventObj_iface_tids
752 static HTMLEventObj
*create_event(HTMLDOMNode
*target
, eventid_t eid
, nsIDOMEvent
*nsevent
)
756 ret
= heap_alloc_zero(sizeof(*ret
));
760 ret
->IHTMLEventObj_iface
.lpVtbl
= &HTMLEventObjVtbl
;
762 ret
->type
= event_info
+eid
;
764 ret
->nsevent
= nsevent
;
766 nsIDOMEvent_AddRef(nsevent
);
767 }else if(event_types
[event_info
[eid
].type
]) {
768 nsIDOMDocumentEvent
*doc_event
;
771 nsres
= nsIDOMHTMLDocument_QueryInterface(target
->doc
->nsdoc
, &IID_nsIDOMDocumentEvent
,
773 if(NS_SUCCEEDED(nsres
)) {
776 nsAString_InitDepend(&type_str
, event_types
[event_info
[eid
].type
]);
777 nsres
= nsIDOMDocumentEvent_CreateEvent(doc_event
, &type_str
, &ret
->nsevent
);
778 nsAString_Finish(&type_str
);
779 nsIDOMDocumentEvent_Release(doc_event
);
781 if(NS_FAILED(nsres
)) {
782 ERR("Could not create event: %08x\n", nsres
);
783 IHTMLEventObj_Release(&ret
->IHTMLEventObj_iface
);
788 ret
->target
= target
;
789 IHTMLDOMNode_AddRef(&target
->IHTMLDOMNode_iface
);
791 init_dispex(&ret
->dispex
, (IUnknown
*)&ret
->IHTMLEventObj_iface
, &HTMLEventObj_dispex
);
796 static HRESULT
call_disp_func(IDispatch
*disp
, DISPPARAMS
*dp
, VARIANT
*retv
)
802 memset(&ei
, 0, sizeof(ei
));
804 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
805 if(SUCCEEDED(hres
)) {
806 hres
= IDispatchEx_InvokeEx(dispex
, 0, GetUserDefaultLCID(), DISPATCH_METHOD
, dp
, retv
, &ei
, NULL
);
807 IDispatchEx_Release(dispex
);
809 TRACE("Could not get IDispatchEx interface: %08x\n", hres
);
810 hres
= IDispatch_Invoke(disp
, 0, &IID_NULL
, GetUserDefaultLCID(), DISPATCH_METHOD
,
811 dp
, retv
, &ei
, NULL
);
817 static HRESULT
call_cp_func(IDispatch
*disp
, DISPID dispid
, VARIANT
*retv
)
819 DISPPARAMS dp
= {NULL
,NULL
,0,0};
823 memset(&ei
, 0, sizeof(ei
));
824 return IDispatch_Invoke(disp
, dispid
, &IID_NULL
, 0, DISPATCH_METHOD
, &dp
, retv
, &ei
, &argerr
);
827 static BOOL
is_cp_event(cp_static_data_t
*data
, DISPID dispid
)
836 hres
= get_dispids(data
->tid
, &data
->id_cnt
, &data
->ids
);
842 max
= data
->id_cnt
-1;
845 if(data
->ids
[i
] == dispid
)
848 if(data
->ids
[i
] < dispid
)
857 static void call_event_handlers(HTMLDocumentNode
*doc
, HTMLEventObj
*event_obj
, event_target_t
*event_target
,
858 ConnectionPointContainer
*cp_container
, eventid_t eid
, IDispatch
*this_obj
)
860 const BOOL cancelable
= event_info
[eid
].flags
& EVENT_CANCELABLE
;
861 handler_vector_t
*handler_vector
= NULL
;
867 handler_vector
= event_target
->event_table
[eid
];
869 if(handler_vector
&& handler_vector
->handler_prop
) {
870 DISPID named_arg
= DISPID_THIS
;
872 DISPPARAMS dp
= {&arg
, &named_arg
, 1, 1};
874 V_VT(&arg
) = VT_DISPATCH
;
875 V_DISPATCH(&arg
) = this_obj
;
878 TRACE("%s >>>\n", debugstr_w(event_info
[eid
].name
));
879 hres
= call_disp_func(handler_vector
->handler_prop
, &dp
, &v
);
881 TRACE("%s <<< %s\n", debugstr_w(event_info
[eid
].name
), debugstr_variant(&v
));
884 if(V_VT(&v
) == VT_BOOL
) {
886 event_obj
->prevent_default
= TRUE
;
887 }else if(V_VT(&v
) != VT_EMPTY
) {
888 FIXME("unhandled result %s\n", debugstr_variant(&v
));
893 WARN("%s <<< %08x\n", debugstr_w(event_info
[eid
].name
), hres
);
897 if(handler_vector
&& handler_vector
->handler_cnt
) {
899 DISPPARAMS dp
= {&arg
, NULL
, 1, 0};
901 V_VT(&arg
) = VT_DISPATCH
;
902 V_DISPATCH(&arg
) = (IDispatch
*)event_obj
;
904 i
= handler_vector
->handler_cnt
;
906 if(handler_vector
->handlers
[i
]) {
909 TRACE("%s [%d] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
910 hres
= call_disp_func(handler_vector
->handlers
[i
], &dp
, &v
);
912 TRACE("%s [%d] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
915 if(V_VT(&v
) == VT_BOOL
) {
917 event_obj
->prevent_default
= TRUE
;
918 }else if(V_VT(&v
) != VT_EMPTY
) {
919 FIXME("unhandled result %s\n", debugstr_variant(&v
));
924 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
933 if(cp_container
->forward_container
)
934 cp_container
= cp_container
->forward_container
;
936 for(cp
= cp_container
->cp_list
; cp
; cp
= cp
->next
) {
937 if(cp
->sinks_size
&& is_cp_event(cp
->data
, event_info
[eid
].dispid
)) {
938 for(i
=0; i
< cp
->sinks_size
; i
++) {
939 if(!cp
->sinks
[i
].disp
)
944 TRACE("cp %s [%d] >>>\n", debugstr_w(event_info
[eid
].name
), i
);
945 hres
= call_cp_func(cp
->sinks
[i
].disp
, event_info
[eid
].dispid
, &v
);
947 TRACE("cp %s [%d] <<<\n", debugstr_w(event_info
[eid
].name
), i
);
950 if(V_VT(&v
) == VT_BOOL
) {
952 event_obj
->prevent_default
= TRUE
;
953 }else if(V_VT(&v
) != VT_EMPTY
) {
954 FIXME("unhandled result %s\n", debugstr_variant(&v
));
959 WARN("cp %s [%d] <<< %08x\n", debugstr_w(event_info
[eid
].name
), i
, hres
);
967 void fire_event(HTMLDocumentNode
*doc
, eventid_t eid
, BOOL set_event
, nsIDOMNode
*target
, nsIDOMEvent
*nsevent
)
969 HTMLEventObj
*event_obj
= NULL
;
970 IHTMLEventObj
*prev_event
;
971 nsIDOMNode
*parent
, *nsnode
;
976 TRACE("(%p) %s\n", doc
, debugstr_w(event_info
[eid
].name
));
978 prev_event
= doc
->basedoc
.window
->event
;
980 hres
= get_node(doc
, target
, TRUE
, &node
);
984 event_obj
= create_event(node
, eid
, nsevent
);
986 doc
->basedoc
.window
->event
= &event_obj
->IHTMLEventObj_iface
;
988 nsIDOMNode_GetNodeType(target
, &node_type
);
990 nsIDOMNode_AddRef(nsnode
);
995 hres
= get_node(doc
, nsnode
, FALSE
, &node
);
996 if(SUCCEEDED(hres
) && node
)
997 call_event_handlers(doc
, event_obj
, *get_node_event_target(node
),
998 node
->cp_container
, eid
, (IDispatch
*)&node
->IHTMLDOMNode_iface
);
1000 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
))
1003 nsIDOMNode_GetParentNode(nsnode
, &parent
);
1004 nsIDOMNode_Release(nsnode
);
1009 nsIDOMNode_GetNodeType(nsnode
, &node_type
);
1010 }while(node_type
== ELEMENT_NODE
);
1012 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
))
1016 if(event_info
[eid
].flags
& EVENT_FORWARDBODY
) {
1017 nsIDOMHTMLElement
*nsbody
;
1020 nsres
= nsIDOMHTMLDocument_GetBody(doc
->nsdoc
, &nsbody
);
1021 if(NS_SUCCEEDED(nsres
) && nsbody
) {
1022 hres
= get_node(doc
, (nsIDOMNode
*)nsbody
, FALSE
, &node
);
1023 if(SUCCEEDED(hres
) && node
)
1024 call_event_handlers(doc
, event_obj
, *get_node_event_target(node
),
1025 node
->cp_container
, eid
, (IDispatch
*)&node
->IHTMLDOMNode_iface
);
1026 nsIDOMHTMLElement_Release(nsbody
);
1028 ERR("Could not get body: %08x\n", nsres
);
1032 call_event_handlers(doc
, event_obj
, doc
->node
.event_target
, &doc
->basedoc
.cp_container
, eid
,
1033 (IDispatch
*)&doc
->basedoc
.IHTMLDocument2_iface
);
1037 FIXME("unimplemented node type %d\n", node_type
);
1041 nsIDOMNode_Release(nsnode
);
1043 doc
->basedoc
.window
->event
= prev_event
;
1046 if(event_obj
->prevent_default
&& nsevent
) {
1047 TRACE("calling PreventDefault\n");
1048 nsIDOMEvent_PreventDefault(nsevent
);
1050 IHTMLEventObj_Release(&event_obj
->IHTMLEventObj_iface
);
1054 HRESULT
dispatch_event(HTMLDOMNode
*node
, const WCHAR
*event_name
, VARIANT
*event_obj
, VARIANT_BOOL
*cancelled
)
1058 eid
= attr_to_eid(event_name
);
1059 if(eid
== EVENTID_LAST
) {
1060 WARN("unknown event %s\n", debugstr_w(event_name
));
1061 return E_INVALIDARG
;
1064 if(event_obj
&& V_VT(event_obj
) != VT_EMPTY
&& V_VT(event_obj
) != VT_ERROR
)
1065 FIXME("event_obj not implemented\n");
1067 if(!(event_info
[eid
].flags
& EVENT_DEFAULTLISTENER
)) {
1068 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1072 fire_event(node
->doc
, eid
, TRUE
, node
->nsnode
, NULL
);
1074 *cancelled
= VARIANT_TRUE
; /* FIXME */
1078 HRESULT
call_event(HTMLDOMNode
*node
, eventid_t eid
)
1082 if(node
->vtbl
->call_event
) {
1083 BOOL handled
= FALSE
;
1085 hres
= node
->vtbl
->call_event(node
, eid
, &handled
);
1090 fire_event(node
->doc
, eid
, TRUE
, node
->nsnode
, NULL
);
1094 static inline event_target_t
*get_event_target(event_target_t
**event_target_ptr
)
1096 if(!*event_target_ptr
)
1097 *event_target_ptr
= heap_alloc_zero(sizeof(event_target_t
));
1098 return *event_target_ptr
;
1101 static BOOL
alloc_handler_vector(event_target_t
*event_target
, eventid_t eid
, int cnt
)
1103 handler_vector_t
*new_vector
, *handler_vector
= event_target
->event_table
[eid
];
1105 if(handler_vector
) {
1106 if(cnt
<= handler_vector
->handler_cnt
)
1109 new_vector
= heap_realloc_zero(handler_vector
, sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
1111 new_vector
= heap_alloc_zero(sizeof(handler_vector_t
) + sizeof(IDispatch
*)*cnt
);
1117 new_vector
->handler_cnt
= cnt
;
1118 event_target
->event_table
[eid
] = new_vector
;
1122 static HRESULT
ensure_nsevent_handler(HTMLDocumentNode
*doc
, event_target_t
*event_target
, nsIDOMNode
*nsnode
, eventid_t eid
)
1127 if(event_info
[eid
].flags
& EVENT_NODEHANDLER
) {
1130 mask
= get_node_handler_mask(eid
);
1131 if(event_target
->node_handlers_mask
& mask
)
1134 add_nsevent_listener(doc
, nsnode
, event_info
[eid
].name
);
1135 event_target
->node_handlers_mask
|= mask
;
1139 if(!(event_info
[eid
].flags
& EVENT_DEFAULTLISTENER
))
1142 if(!doc
->event_vector
) {
1143 doc
->event_vector
= heap_alloc_zero(EVENTID_LAST
*sizeof(BOOL
));
1144 if(!doc
->event_vector
)
1145 return E_OUTOFMEMORY
;
1148 if(!doc
->event_vector
[eid
]) {
1149 doc
->event_vector
[eid
] = TRUE
;
1150 add_nsevent_listener(doc
, NULL
, event_info
[eid
].name
);
1156 static HRESULT
remove_event_handler(event_target_t
**event_target
, eventid_t eid
)
1158 if(*event_target
&& (*event_target
)->event_table
[eid
] && (*event_target
)->event_table
[eid
]->handler_prop
) {
1159 IDispatch_Release((*event_target
)->event_table
[eid
]->handler_prop
);
1160 (*event_target
)->event_table
[eid
]->handler_prop
= NULL
;
1166 static HRESULT
set_event_handler_disp(event_target_t
**event_target_ptr
, nsIDOMNode
*nsnode
, HTMLDocumentNode
*doc
,
1167 eventid_t eid
, IDispatch
*disp
)
1169 event_target_t
*event_target
;
1172 return remove_event_handler(event_target_ptr
, eid
);
1174 event_target
= get_event_target(event_target_ptr
);
1176 return E_OUTOFMEMORY
;
1178 if(!alloc_handler_vector(event_target
, eid
, 0))
1179 return E_OUTOFMEMORY
;
1181 if(event_target
->event_table
[eid
]->handler_prop
)
1182 IDispatch_Release(event_target
->event_table
[eid
]->handler_prop
);
1184 event_target
->event_table
[eid
]->handler_prop
= disp
;
1185 IDispatch_AddRef(disp
);
1187 return ensure_nsevent_handler(doc
, event_target
, nsnode
, eid
);
1190 HRESULT
set_event_handler(event_target_t
**event_target
, nsIDOMNode
*nsnode
, HTMLDocumentNode
*doc
, eventid_t eid
, VARIANT
*var
)
1194 return remove_event_handler(event_target
, eid
);
1197 return set_event_handler_disp(event_target
, nsnode
, doc
, eid
, V_DISPATCH(var
));
1200 FIXME("not supported vt=%d\n", V_VT(var
));
1208 HRESULT
get_event_handler(event_target_t
**event_target
, eventid_t eid
, VARIANT
*var
)
1210 if(*event_target
&& (*event_target
)->event_table
[eid
] && (*event_target
)->event_table
[eid
]->handler_prop
) {
1211 V_VT(var
) = VT_DISPATCH
;
1212 V_DISPATCH(var
) = (*event_target
)->event_table
[eid
]->handler_prop
;
1213 IDispatch_AddRef(V_DISPATCH(var
));
1215 V_VT(var
) = VT_NULL
;
1221 HRESULT
attach_event(event_target_t
**event_target_ptr
, nsIDOMNode
*nsnode
, HTMLDocument
*doc
, BSTR name
,
1222 IDispatch
*disp
, VARIANT_BOOL
*res
)
1224 event_target_t
*event_target
;
1228 eid
= attr_to_eid(name
);
1229 if(eid
== EVENTID_LAST
) {
1230 WARN("Unknown event\n");
1231 *res
= VARIANT_TRUE
;
1235 event_target
= get_event_target(event_target_ptr
);
1237 return E_OUTOFMEMORY
;
1239 if(event_target
->event_table
[eid
]) {
1240 while(i
< event_target
->event_table
[eid
]->handler_cnt
&& event_target
->event_table
[eid
]->handlers
[i
])
1242 if(i
== event_target
->event_table
[eid
]->handler_cnt
&& !alloc_handler_vector(event_target
, eid
, i
+1))
1243 return E_OUTOFMEMORY
;
1244 }else if(!alloc_handler_vector(event_target
, eid
, i
+1)) {
1245 return E_OUTOFMEMORY
;
1248 IDispatch_AddRef(disp
);
1249 event_target
->event_table
[eid
]->handlers
[i
] = disp
;
1251 *res
= VARIANT_TRUE
;
1252 return ensure_nsevent_handler(doc
->doc_node
, event_target
, nsnode
, eid
);
1255 HRESULT
detach_event(event_target_t
*event_target
, HTMLDocument
*doc
, BSTR name
, IDispatch
*disp
)
1263 eid
= attr_to_eid(name
);
1264 if(eid
== EVENTID_LAST
) {
1265 WARN("Unknown event\n");
1269 if(!event_target
->event_table
[eid
])
1272 while(i
< event_target
->event_table
[eid
]->handler_cnt
) {
1273 if(event_target
->event_table
[eid
]->handlers
[i
] == disp
) {
1274 IDispatch_Release(event_target
->event_table
[eid
]->handlers
[i
]);
1275 event_target
->event_table
[eid
]->handlers
[i
] = NULL
;
1283 void update_cp_events(HTMLWindow
*window
, event_target_t
**event_target_ptr
, cp_static_data_t
*cp
, nsIDOMNode
*nsnode
)
1285 event_target_t
*event_target
;
1288 event_target
= get_event_target(event_target_ptr
);
1292 for(i
=0; i
< EVENTID_LAST
; i
++) {
1293 if((event_info
[i
].flags
& EVENT_DEFAULTLISTENER
) && is_cp_event(cp
, event_info
[i
].dispid
))
1294 ensure_nsevent_handler(window
->doc
, event_target
, nsnode
, i
);
1298 void check_event_attr(HTMLDocumentNode
*doc
, nsIDOMElement
*nselem
)
1300 const PRUnichar
*attr_value
;
1301 nsAString attr_name_str
, attr_value_str
;
1308 nsAString_Init(&attr_value_str
, NULL
);
1309 nsAString_Init(&attr_name_str
, NULL
);
1311 for(i
=0; i
< EVENTID_LAST
; i
++) {
1312 nsAString_SetData(&attr_name_str
, event_info
[i
].attr_name
);
1313 nsres
= nsIDOMElement_GetAttribute(nselem
, &attr_name_str
, &attr_value_str
);
1314 if(NS_SUCCEEDED(nsres
)) {
1315 nsAString_GetData(&attr_value_str
, &attr_value
);
1319 TRACE("%p.%s = %s\n", nselem
, debugstr_w(event_info
[i
].attr_name
), debugstr_w(attr_value
));
1321 disp
= script_parse_event(doc
->basedoc
.window
, attr_value
);
1323 hres
= get_node(doc
, (nsIDOMNode
*)nselem
, TRUE
, &node
);
1325 set_event_handler_disp(get_node_event_target(node
), node
->nsnode
, node
->doc
, i
, disp
);
1326 IDispatch_Release(disp
);
1331 nsAString_Finish(&attr_value_str
);
1332 nsAString_Finish(&attr_name_str
);
1335 void release_event_target(event_target_t
*event_target
)
1339 for(i
=0; i
< EVENTID_LAST
; i
++) {
1340 if(event_target
->event_table
[i
]) {
1341 if(event_target
->event_table
[i
]->handler_prop
)
1342 IDispatch_Release(event_target
->event_table
[i
]->handler_prop
);
1343 for(j
=0; j
< event_target
->event_table
[i
]->handler_cnt
; j
++)
1344 IDispatch_Release(event_target
->event_table
[i
]->handlers
[j
]);
1348 heap_free(event_target
);