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 mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
70 static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};
72 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
73 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
75 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
76 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
78 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
79 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
81 static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
82 static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
90 #define EVENT_DEFAULTLISTENER 0x0001
91 #define EVENT_BUBBLE 0x0002
93 static const event_info_t event_info[] = {
94 {blurW, onblurW, EVENT_DEFAULTLISTENER},
95 {changeW, onchangeW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
96 {clickW, onclickW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
98 {dragstartW, ondragstartW, 0},
99 {focusW, onfocusW, EVENT_DEFAULTLISTENER},
100 {keydownW, onkeydownW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
101 {keyupW, onkeyupW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
103 {mousedownW, onmousedownW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
104 {mouseoutW, onmouseoutW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
105 {mouseoverW, onmouseoverW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
106 {mouseupW, onmouseupW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
107 {pasteW, onpasteW, 0},
108 {selectstartW, onselectstartW, 0}
111 eventid_t str_to_eid(LPCWSTR str)
115 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
116 if(!strcmpW(event_info[i].name, str))
120 ERR("unknown type %s\n", debugstr_w(str));
126 const IHTMLEventObjVtbl *lpIHTMLEventObjVtbl;
133 #define HTMLEVENTOBJ(x) ((IHTMLEventObj*) &(x)->lpIHTMLEventObjVtbl)
135 #define HTMLEVENTOBJ_THIS(iface) DEFINE_THIS(HTMLEventObj, IHTMLEventObj, iface)
137 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
139 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
143 if(IsEqualGUID(&IID_IUnknown, riid)) {
144 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
145 *ppv = HTMLEVENTOBJ(This);
146 }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
147 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
148 *ppv = HTMLEVENTOBJ(This);
149 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
150 return *ppv ? S_OK : E_NOINTERFACE;
154 IUnknown_AddRef((IUnknown*)*ppv);
158 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
159 return E_NOINTERFACE;
162 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
164 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
165 LONG ref = InterlockedIncrement(&This->ref);
167 TRACE("(%p) ref=%d\n", This, ref);
172 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
174 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
175 LONG ref = InterlockedDecrement(&This->ref);
177 TRACE("(%p) ref=%d\n", This, ref);
185 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
187 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
188 FIXME("(%p)->(%p)\n", This, pctinfo);
192 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
193 LCID lcid, ITypeInfo **ppTInfo)
195 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
196 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
200 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
201 LPOLESTR *rgszNames, UINT cNames,
202 LCID lcid, DISPID *rgDispId)
204 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
205 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
210 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
211 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
212 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
214 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
215 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
216 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
220 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
222 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
224 TRACE("(%p)->(%p)\n", This, p);
226 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(This->target), &IID_IHTMLElement, (void**)p);
229 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
231 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
232 FIXME("(%p)->(%p)\n", This, p);
236 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
238 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
239 FIXME("(%p)->(%p)\n", This, p);
243 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
245 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
246 FIXME("(%p)->(%p)\n", This, p);
250 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
252 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
253 FIXME("(%p)->()\n", This);
257 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
259 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
260 FIXME("(%p)->(%p)\n", This, p);
264 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
266 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
267 FIXME("(%p)->(%x)\n", This, v);
271 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
273 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
274 FIXME("(%p)->(%p)\n", This, p);
278 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
280 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
281 FIXME("(%p)->(%p)\n", This, p);
285 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
287 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
288 FIXME("(%p)->(%p)\n", This, p);
292 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, long v)
294 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
295 FIXME("(%p)->(%ld)\n", This, v);
299 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, long *p)
301 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
302 FIXME("(%p)->(%p)\n", This, p);
306 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, long *p)
308 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
309 FIXME("(%p)->(%p)\n", This, p);
313 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
315 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
316 FIXME("(%p)->(%p)\n", This, p);
320 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
322 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
323 FIXME("(%p)->(%p)\n", This, p);
327 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, long *p)
329 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
330 FIXME("(%p)->(%p)\n", This, p);
334 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, long *p)
336 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
337 FIXME("(%p)->(%p)\n", This, p);
341 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, long *p)
343 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
344 FIXME("(%p)->(%p)\n", This, p);
348 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, long *p)
350 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
351 FIXME("(%p)->(%p)\n", This, p);
355 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, long *p)
357 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
358 FIXME("(%p)->(%p)\n", This, p);
362 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, long *p)
364 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
365 FIXME("(%p)->(%p)\n", This, p);
369 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, long *p)
371 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
372 FIXME("(%p)->(%p)\n", This, p);
376 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, long *p)
378 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
379 FIXME("(%p)->(%p)\n", This, p);
383 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, long *p)
385 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
386 FIXME("(%p)->(%p)\n", This, p);
390 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
392 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
393 FIXME("(%p)->(%p)\n", This, p);
397 #undef HTMLEVENTOBJ_THIS
399 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
400 HTMLEventObj_QueryInterface,
402 HTMLEventObj_Release,
403 HTMLEventObj_GetTypeInfoCount,
404 HTMLEventObj_GetTypeInfo,
405 HTMLEventObj_GetIDsOfNames,
407 HTMLEventObj_get_srcElement,
408 HTMLEventObj_get_altKey,
409 HTMLEventObj_get_ctrlKey,
410 HTMLEventObj_get_shiftKey,
411 HTMLEventObj_put_returnValue,
412 HTMLEventObj_get_returnValue,
413 HTMLEventObj_put_cancelBubble,
414 HTMLEventObj_get_cancelBubble,
415 HTMLEventObj_get_fromElement,
416 HTMLEventObj_get_toElement,
417 HTMLEventObj_put_keyCode,
418 HTMLEventObj_get_keyCode,
419 HTMLEventObj_get_button,
420 HTMLEventObj_get_type,
421 HTMLEventObj_get_qualifier,
422 HTMLEventObj_get_reason,
425 HTMLEventObj_get_clientX,
426 HTMLEventObj_get_clientY,
427 HTMLEventObj_get_offsetX,
428 HTMLEventObj_get_offsetY,
429 HTMLEventObj_get_screenX,
430 HTMLEventObj_get_screenY,
431 HTMLEventObj_get_srcFilter
434 static const tid_t HTMLEventObj_iface_tids[] = {
439 static dispex_static_data_t HTMLEventObj_dispex = {
443 HTMLEventObj_iface_tids
446 static IHTMLEventObj *create_event(HTMLDOMNode *target)
450 ret = heap_alloc(sizeof(*ret));
451 ret->lpIHTMLEventObjVtbl = &HTMLEventObjVtbl;
453 ret->target = target;
454 IHTMLDOMNode_AddRef(HTMLDOMNODE(target));
456 init_dispex(&ret->dispex, (IUnknown*)HTMLEVENTOBJ(ret), &HTMLEventObj_dispex);
458 return HTMLEVENTOBJ(ret);
461 void fire_event(HTMLDocument *doc, eventid_t eid, nsIDOMNode *target)
463 IHTMLEventObj *prev_event, *event_obj = NULL;
464 nsIDOMNode *parent, *nsnode;
468 nsIDOMNode_GetNodeType(target, &node_type);
469 if(node_type != ELEMENT_NODE) {
470 FIXME("node type %d node supported\n", node_type);
474 prev_event = doc->window->event;
476 nsIDOMNode_AddRef(nsnode);
479 node = get_node(doc, nsnode, FALSE);
481 if(node && node->event_target && node->event_target->event_table[eid]) {
483 event_obj = doc->window->event = create_event(get_node(doc, target, TRUE));
485 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
486 call_disp_func(doc, node->event_target->event_table[eid], (IDispatch*)HTMLDOMNODE(node));
487 TRACE("%s <<<\n", debugstr_w(event_info[eid].name));
490 if(!(event_info[eid].flags & EVENT_BUBBLE))
493 nsIDOMNode_GetParentNode(nsnode, &parent);
494 nsIDOMNode_Release(nsnode);
499 nsIDOMNode_GetNodeType(nsnode, &node_type);
500 if(node_type != ELEMENT_NODE)
505 nsIDOMNode_Release(nsnode);
507 if((event_info[eid].flags & EVENT_BUBBLE) && doc->event_target && doc->event_target->event_table[eid]) {
509 event_obj = doc->window->event = create_event(get_node(doc, target, TRUE));
511 TRACE("doc %s >>>\n", debugstr_w(event_info[eid].name));
512 call_disp_func(doc, doc->event_target->event_table[eid], (IDispatch*)HTMLDOC(doc));
513 TRACE("doc %s <<<\n", debugstr_w(event_info[eid].name));
517 IHTMLEventObj_Release(event_obj);
518 doc->window->event = prev_event;
522 static HRESULT set_event_handler_disp(event_target_t **event_target, HTMLDocument *doc, eventid_t eid, IDispatch *disp)
525 *event_target = heap_alloc_zero(sizeof(event_target_t));
526 else if((*event_target)->event_table[eid])
527 IDispatch_Release((*event_target)->event_table[eid]);
529 (*event_target)->event_table[eid] = disp;
532 IDispatch_AddRef(disp);
534 if(doc->nscontainer && (event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
535 if(!doc->nscontainer->event_vector) {
536 doc->nscontainer->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
537 if(!doc->nscontainer->event_vector)
538 return E_OUTOFMEMORY;
541 if(!doc->nscontainer->event_vector[eid]) {
542 doc->nscontainer->event_vector[eid] = TRUE;
543 add_nsevent_listener(doc->nscontainer, event_info[eid].name);
550 HRESULT set_event_handler(event_target_t **event_target, HTMLDocument *doc, eventid_t eid, VARIANT *var)
554 if(*event_target && (*event_target)->event_table[eid]) {
555 IDispatch_Release((*event_target)->event_table[eid]);
556 (*event_target)->event_table[eid] = NULL;
561 return set_event_handler_disp(event_target, doc, eid, V_DISPATCH(var));
564 FIXME("not supported vt=%d\n", V_VT(var));
571 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
573 if(*event_target && (*event_target)->event_table[eid]) {
574 V_VT(var) = VT_DISPATCH;
575 V_DISPATCH(var) = (*event_target)->event_table[eid];
576 IDispatch_AddRef(V_DISPATCH(var));
584 void check_event_attr(HTMLDocument *doc, nsIDOMElement *nselem)
586 const PRUnichar *attr_value;
587 nsAString attr_name_str, attr_value_str;
593 nsAString_Init(&attr_value_str, NULL);
594 nsAString_Init(&attr_name_str, NULL);
596 for(i=0; i < EVENTID_LAST; i++) {
597 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
598 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
599 if(NS_SUCCEEDED(nsres)) {
600 nsAString_GetData(&attr_value_str, &attr_value);
604 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
606 disp = script_parse_event(doc, attr_value);
608 node = get_node(doc, (nsIDOMNode*)nselem, TRUE);
609 set_event_handler_disp(&node->event_target, node->doc, i, disp);
610 IDispatch_Release(disp);
615 nsAString_Finish(&attr_value_str);
616 nsAString_Finish(&attr_name_str);
619 void release_event_target(event_target_t *event_target)
623 for(i=0; i < EVENTID_LAST; i++) {
624 if(event_target->event_table[i])
625 IDispatch_Release(event_target->event_table[i]);
628 heap_free(event_target);