2 * Copyright 2008 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
28 #include "mshtml_private.h"
29 #include "htmlevent.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
35 struct event_target_t
{
36 IDispatch
*event_table
[EVENTID_LAST
];
39 static const WCHAR blurW
[] = {'b','l','u','r',0};
40 static const WCHAR onblurW
[] = {'o','n','b','l','u','r',0};
42 static const WCHAR changeW
[] = {'c','h','a','n','g','e',0};
43 static const WCHAR onchangeW
[] = {'o','n','c','h','a','n','g','e',0};
45 static const WCHAR clickW
[] = {'c','l','i','c','k',0};
46 static const WCHAR onclickW
[] = {'o','n','c','l','i','c','k',0};
48 static const WCHAR dragW
[] = {'d','r','a','g',0};
49 static const WCHAR ondragW
[] = {'o','n','d','r','a','g',0};
51 static const WCHAR dragstartW
[] = {'d','r','a','g','s','t','a','r','t',0};
52 static const WCHAR ondragstartW
[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
54 static const WCHAR focusW
[] = {'f','o','c','u','s',0};
55 static const WCHAR onfocusW
[] = {'o','n','f','o','c','u','s',0};
57 static const WCHAR keydownW
[] = {'k','e','y','d','o','w','n',0};
58 static const WCHAR onkeydownW
[] = {'o','n','k','e','y','d','o','w','n',0};
60 static const WCHAR keyupW
[] = {'k','e','y','u','p',0};
61 static const WCHAR onkeyupW
[] = {'o','n','k','e','y','u','p',0};
63 static const WCHAR loadW
[] = {'l','o','a','d',0};
64 static const WCHAR onloadW
[] = {'o','n','l','o','a','d',0};
66 static const WCHAR mousedownW
[] = {'m','o','u','s','e','d','o','w','n',0};
67 static const WCHAR onmousedownW
[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
69 static const WCHAR mouseoverW
[] = {'m','o','u','s','e','o','v','e','r',0};
70 static const WCHAR onmouseoverW
[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
72 static const WCHAR mouseupW
[] = {'m','o','u','s','e','u','p',0};
73 static const WCHAR onmouseupW
[] = {'o','n','m','o','u','s','e','u','p',0};
75 static const WCHAR pasteW
[] = {'p','a','s','t','e',0};
76 static const WCHAR onpasteW
[] = {'o','n','p','a','s','t','e',0};
78 static const WCHAR selectstartW
[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
79 static const WCHAR onselectstartW
[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
87 #define EVENT_DEFAULTLISTENER 0x0001
88 #define EVENT_BUBBLE 0x0002
90 static const event_info_t event_info
[] = {
91 {blurW
, onblurW
, EVENT_DEFAULTLISTENER
},
92 {changeW
, onchangeW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
93 {clickW
, onclickW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
95 {dragstartW
, ondragstartW
, 0},
96 {focusW
, onfocusW
, EVENT_DEFAULTLISTENER
},
97 {keydownW
, onkeydownW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
98 {keyupW
, onkeyupW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
100 {mousedownW
, onmousedownW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
101 {mouseoverW
, onmouseoverW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
102 {mouseupW
, onmouseupW
, EVENT_DEFAULTLISTENER
|EVENT_BUBBLE
},
103 {pasteW
, onpasteW
, 0},
104 {selectstartW
, onselectstartW
, 0}
107 eventid_t
str_to_eid(LPCWSTR str
)
111 for(i
=0; i
< sizeof(event_info
)/sizeof(event_info
[0]); i
++) {
112 if(!strcmpW(event_info
[i
].name
, str
))
116 ERR("unknown type %s\n", debugstr_w(str
));
122 const IHTMLEventObjVtbl
*lpIHTMLEventObjVtbl
;
129 #define HTMLEVENTOBJ(x) ((IHTMLEventObj*) &(x)->lpIHTMLEventObjVtbl)
131 #define HTMLEVENTOBJ_THIS(iface) DEFINE_THIS(HTMLEventObj, IHTMLEventObj, iface)
133 static HRESULT WINAPI
HTMLEventObj_QueryInterface(IHTMLEventObj
*iface
, REFIID riid
, void **ppv
)
135 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
139 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
140 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
141 *ppv
= HTMLEVENTOBJ(This
);
142 }else if(IsEqualGUID(&IID_IHTMLEventObj
, riid
)) {
143 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This
, ppv
);
144 *ppv
= HTMLEVENTOBJ(This
);
145 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
146 return *ppv
? S_OK
: E_NOINTERFACE
;
150 IUnknown_AddRef((IUnknown
*)*ppv
);
154 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
155 return E_NOINTERFACE
;
158 static ULONG WINAPI
HTMLEventObj_AddRef(IHTMLEventObj
*iface
)
160 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
161 LONG ref
= InterlockedIncrement(&This
->ref
);
163 TRACE("(%p) ref=%d\n", This
, ref
);
168 static ULONG WINAPI
HTMLEventObj_Release(IHTMLEventObj
*iface
)
170 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
171 LONG ref
= InterlockedDecrement(&This
->ref
);
173 TRACE("(%p) ref=%d\n", This
, ref
);
181 static HRESULT WINAPI
HTMLEventObj_GetTypeInfoCount(IHTMLEventObj
*iface
, UINT
*pctinfo
)
183 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
184 FIXME("(%p)->(%p)\n", This
, pctinfo
);
188 static HRESULT WINAPI
HTMLEventObj_GetTypeInfo(IHTMLEventObj
*iface
, UINT iTInfo
,
189 LCID lcid
, ITypeInfo
**ppTInfo
)
191 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
192 FIXME("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
196 static HRESULT WINAPI
HTMLEventObj_GetIDsOfNames(IHTMLEventObj
*iface
, REFIID riid
,
197 LPOLESTR
*rgszNames
, UINT cNames
,
198 LCID lcid
, DISPID
*rgDispId
)
200 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
201 FIXME("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
206 static HRESULT WINAPI
HTMLEventObj_Invoke(IHTMLEventObj
*iface
, DISPID dispIdMember
,
207 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
208 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
210 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
211 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
212 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
216 static HRESULT WINAPI
HTMLEventObj_get_srcElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
218 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
220 TRACE("(%p)->(%p)\n", This
, p
);
222 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(This
->target
), &IID_IHTMLElement
, (void**)p
);
225 static HRESULT WINAPI
HTMLEventObj_get_altKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
227 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
228 FIXME("(%p)->(%p)\n", This
, p
);
232 static HRESULT WINAPI
HTMLEventObj_get_ctrlKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
234 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
235 FIXME("(%p)->(%p)\n", This
, p
);
239 static HRESULT WINAPI
HTMLEventObj_get_shiftKey(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
241 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
242 FIXME("(%p)->(%p)\n", This
, p
);
246 static HRESULT WINAPI
HTMLEventObj_put_returnValue(IHTMLEventObj
*iface
, VARIANT v
)
248 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
249 FIXME("(%p)->()\n", This
);
253 static HRESULT WINAPI
HTMLEventObj_get_returnValue(IHTMLEventObj
*iface
, VARIANT
*p
)
255 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
256 FIXME("(%p)->(%p)\n", This
, p
);
260 static HRESULT WINAPI
HTMLEventObj_put_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL v
)
262 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
263 FIXME("(%p)->(%x)\n", This
, v
);
267 static HRESULT WINAPI
HTMLEventObj_get_cancelBubble(IHTMLEventObj
*iface
, VARIANT_BOOL
*p
)
269 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
270 FIXME("(%p)->(%p)\n", This
, p
);
274 static HRESULT WINAPI
HTMLEventObj_get_fromElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
276 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
277 FIXME("(%p)->(%p)\n", This
, p
);
281 static HRESULT WINAPI
HTMLEventObj_get_toElement(IHTMLEventObj
*iface
, IHTMLElement
**p
)
283 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
284 FIXME("(%p)->(%p)\n", This
, p
);
288 static HRESULT WINAPI
HTMLEventObj_put_keyCode(IHTMLEventObj
*iface
, long v
)
290 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
291 FIXME("(%p)->(%ld)\n", This
, v
);
295 static HRESULT WINAPI
HTMLEventObj_get_keyCode(IHTMLEventObj
*iface
, long *p
)
297 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
298 FIXME("(%p)->(%p)\n", This
, p
);
302 static HRESULT WINAPI
HTMLEventObj_get_button(IHTMLEventObj
*iface
, long *p
)
304 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
305 FIXME("(%p)->(%p)\n", This
, p
);
309 static HRESULT WINAPI
HTMLEventObj_get_type(IHTMLEventObj
*iface
, BSTR
*p
)
311 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
312 FIXME("(%p)->(%p)\n", This
, p
);
316 static HRESULT WINAPI
HTMLEventObj_get_qualifier(IHTMLEventObj
*iface
, BSTR
*p
)
318 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
319 FIXME("(%p)->(%p)\n", This
, p
);
323 static HRESULT WINAPI
HTMLEventObj_get_reason(IHTMLEventObj
*iface
, long *p
)
325 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
326 FIXME("(%p)->(%p)\n", This
, p
);
330 static HRESULT WINAPI
HTMLEventObj_get_x(IHTMLEventObj
*iface
, long *p
)
332 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
333 FIXME("(%p)->(%p)\n", This
, p
);
337 static HRESULT WINAPI
HTMLEventObj_get_y(IHTMLEventObj
*iface
, long *p
)
339 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
340 FIXME("(%p)->(%p)\n", This
, p
);
344 static HRESULT WINAPI
HTMLEventObj_get_clientX(IHTMLEventObj
*iface
, long *p
)
346 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
347 FIXME("(%p)->(%p)\n", This
, p
);
351 static HRESULT WINAPI
HTMLEventObj_get_clientY(IHTMLEventObj
*iface
, long *p
)
353 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
354 FIXME("(%p)->(%p)\n", This
, p
);
358 static HRESULT WINAPI
HTMLEventObj_get_offsetX(IHTMLEventObj
*iface
, long *p
)
360 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
361 FIXME("(%p)->(%p)\n", This
, p
);
365 static HRESULT WINAPI
HTMLEventObj_get_offsetY(IHTMLEventObj
*iface
, long *p
)
367 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
368 FIXME("(%p)->(%p)\n", This
, p
);
372 static HRESULT WINAPI
HTMLEventObj_get_screenX(IHTMLEventObj
*iface
, long *p
)
374 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
375 FIXME("(%p)->(%p)\n", This
, p
);
379 static HRESULT WINAPI
HTMLEventObj_get_screenY(IHTMLEventObj
*iface
, long *p
)
381 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
382 FIXME("(%p)->(%p)\n", This
, p
);
386 static HRESULT WINAPI
HTMLEventObj_get_srcFilter(IHTMLEventObj
*iface
, IDispatch
**p
)
388 HTMLEventObj
*This
= HTMLEVENTOBJ_THIS(iface
);
389 FIXME("(%p)->(%p)\n", This
, p
);
393 #undef HTMLEVENTOBJ_THIS
395 static const IHTMLEventObjVtbl HTMLEventObjVtbl
= {
396 HTMLEventObj_QueryInterface
,
398 HTMLEventObj_Release
,
399 HTMLEventObj_GetTypeInfoCount
,
400 HTMLEventObj_GetTypeInfo
,
401 HTMLEventObj_GetIDsOfNames
,
403 HTMLEventObj_get_srcElement
,
404 HTMLEventObj_get_altKey
,
405 HTMLEventObj_get_ctrlKey
,
406 HTMLEventObj_get_shiftKey
,
407 HTMLEventObj_put_returnValue
,
408 HTMLEventObj_get_returnValue
,
409 HTMLEventObj_put_cancelBubble
,
410 HTMLEventObj_get_cancelBubble
,
411 HTMLEventObj_get_fromElement
,
412 HTMLEventObj_get_toElement
,
413 HTMLEventObj_put_keyCode
,
414 HTMLEventObj_get_keyCode
,
415 HTMLEventObj_get_button
,
416 HTMLEventObj_get_type
,
417 HTMLEventObj_get_qualifier
,
418 HTMLEventObj_get_reason
,
421 HTMLEventObj_get_clientX
,
422 HTMLEventObj_get_clientY
,
423 HTMLEventObj_get_offsetX
,
424 HTMLEventObj_get_offsetY
,
425 HTMLEventObj_get_screenX
,
426 HTMLEventObj_get_screenY
,
427 HTMLEventObj_get_srcFilter
430 static const tid_t HTMLEventObj_iface_tids
[] = {
435 static dispex_static_data_t HTMLEventObj_dispex
= {
439 HTMLEventObj_iface_tids
442 static IHTMLEventObj
*create_event(HTMLDOMNode
*target
)
446 ret
= heap_alloc(sizeof(*ret
));
447 ret
->lpIHTMLEventObjVtbl
= &HTMLEventObjVtbl
;
449 ret
->target
= target
;
450 IHTMLDOMNode_AddRef(HTMLDOMNODE(target
));
452 init_dispex(&ret
->dispex
, (IUnknown
*)HTMLEVENTOBJ(ret
), &HTMLEventObj_dispex
);
454 return HTMLEVENTOBJ(ret
);
457 void fire_event(HTMLDocument
*doc
, eventid_t eid
, nsIDOMNode
*target
)
459 IHTMLEventObj
*prev_event
, *event_obj
= NULL
;
460 nsIDOMNode
*parent
, *nsnode
;
464 nsIDOMNode_GetNodeType(target
, &node_type
);
465 if(node_type
!= ELEMENT_NODE
) {
466 FIXME("node type %d node supported\n", node_type
);
470 prev_event
= doc
->window
->event
;
472 nsIDOMNode_AddRef(nsnode
);
475 node
= get_node(doc
, nsnode
, FALSE
);
477 if(node
&& node
->event_target
&& node
->event_target
->event_table
[eid
]) {
479 event_obj
= doc
->window
->event
= create_event(get_node(doc
, target
, TRUE
));
481 TRACE("%s >>>\n", debugstr_w(event_info
[eid
].name
));
482 call_disp_func(doc
, node
->event_target
->event_table
[eid
], (IDispatch
*)HTMLDOMNODE(node
));
483 TRACE("%s <<<\n", debugstr_w(event_info
[eid
].name
));
486 if(!(event_info
[eid
].flags
& EVENT_BUBBLE
))
489 nsIDOMNode_GetParentNode(nsnode
, &parent
);
490 nsIDOMNode_Release(nsnode
);
495 nsIDOMNode_GetNodeType(nsnode
, &node_type
);
496 if(node_type
!= ELEMENT_NODE
)
501 nsIDOMNode_Release(nsnode
);
503 if((event_info
[eid
].flags
& EVENT_BUBBLE
) && doc
->event_target
&& doc
->event_target
->event_table
[eid
]) {
505 event_obj
= doc
->window
->event
= create_event(get_node(doc
, target
, TRUE
));
507 TRACE("doc %s >>>\n", debugstr_w(event_info
[eid
].name
));
508 call_disp_func(doc
, doc
->event_target
->event_table
[eid
], (IDispatch
*)HTMLDOC(doc
));
509 TRACE("doc %s <<<\n", debugstr_w(event_info
[eid
].name
));
513 IHTMLEventObj_Release(event_obj
);
514 doc
->window
->event
= prev_event
;
518 static HRESULT
set_event_handler_disp(event_target_t
**event_target
, HTMLDocument
*doc
, eventid_t eid
, IDispatch
*disp
)
521 *event_target
= heap_alloc_zero(sizeof(event_target_t
));
522 else if((*event_target
)->event_table
[eid
])
523 IDispatch_Release((*event_target
)->event_table
[eid
]);
525 (*event_target
)->event_table
[eid
] = disp
;
528 IDispatch_AddRef(disp
);
530 if(event_info
[eid
].flags
& EVENT_DEFAULTLISTENER
) {
531 if(!doc
->nscontainer
->event_vector
) {
532 doc
->nscontainer
->event_vector
= heap_alloc_zero(EVENTID_LAST
*sizeof(BOOL
));
533 if(!doc
->nscontainer
->event_vector
)
534 return E_OUTOFMEMORY
;
537 if(!doc
->nscontainer
->event_vector
[eid
]) {
538 doc
->nscontainer
->event_vector
[eid
] = TRUE
;
539 add_nsevent_listener(doc
->nscontainer
, event_info
[eid
].name
);
546 HRESULT
set_event_handler(event_target_t
**event_target
, HTMLDocument
*doc
, eventid_t eid
, VARIANT
*var
)
550 if(*event_target
&& (*event_target
)->event_table
[eid
]) {
551 IDispatch_Release((*event_target
)->event_table
[eid
]);
552 (*event_target
)->event_table
[eid
] = NULL
;
557 return set_event_handler_disp(event_target
, doc
, eid
, V_DISPATCH(var
));
560 FIXME("not supported vt=%d\n", V_VT(var
));
567 HRESULT
get_event_handler(event_target_t
**event_target
, eventid_t eid
, VARIANT
*var
)
569 if(*event_target
&& (*event_target
)->event_table
[eid
]) {
570 V_VT(var
) = VT_DISPATCH
;
571 V_DISPATCH(var
) = (*event_target
)->event_table
[eid
];
572 IDispatch_AddRef(V_DISPATCH(var
));
580 void check_event_attr(HTMLDocument
*doc
, nsIDOMElement
*nselem
)
582 const PRUnichar
*attr_value
;
583 nsAString attr_name_str
, attr_value_str
;
589 nsAString_Init(&attr_value_str
, NULL
);
590 nsAString_Init(&attr_name_str
, NULL
);
592 for(i
=0; i
< EVENTID_LAST
; i
++) {
593 nsAString_SetData(&attr_name_str
, event_info
[i
].attr_name
);
594 nsres
= nsIDOMElement_GetAttribute(nselem
, &attr_name_str
, &attr_value_str
);
595 if(NS_SUCCEEDED(nsres
)) {
596 nsAString_GetData(&attr_value_str
, &attr_value
);
600 TRACE("%p.%s = %s\n", nselem
, debugstr_w(event_info
[i
].attr_name
), debugstr_w(attr_value
));
602 disp
= script_parse_event(doc
, attr_value
);
604 node
= get_node(doc
, (nsIDOMNode
*)nselem
, TRUE
);
605 set_event_handler_disp(&node
->event_target
, node
->doc
, i
, disp
);
606 IDispatch_Release(disp
);
611 nsAString_Finish(&attr_value_str
);
612 nsAString_Finish(&attr_name_str
);
615 void release_event_target(event_target_t
*event_target
)
619 for(i
=0; i
< EVENTID_LAST
; i
++) {
620 if(event_target
->event_table
[i
])
621 IDispatch_Release(event_target
->event_table
[i
]);
624 heap_free(event_target
);