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 focusW[] = {'f','o','c','u','s',0};
52 static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};
54 static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
55 static const WCHAR onkeydownW[] = {'o','n','k','e','y','d','o','w','n',0};
57 static const WCHAR keyupW[] = {'k','e','y','u','p',0};
58 static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};
60 static const WCHAR loadW[] = {'l','o','a','d',0};
61 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
63 static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
64 static const WCHAR onmousedownW[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
66 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
67 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
69 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
70 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
72 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
73 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
81 #define EVENT_DEFAULTLISTENER 0x0001
82 #define EVENT_BUBBLE 0x0002
84 static const event_info_t event_info[] = {
85 {blurW, onblurW, EVENT_DEFAULTLISTENER},
86 {changeW, onchangeW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
87 {clickW, onclickW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
89 {focusW, onfocusW, EVENT_DEFAULTLISTENER},
90 {keydownW, onkeydownW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
91 {keyupW, onkeyupW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
93 {mousedownW, onmousedownW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
94 {mouseoverW, onmouseoverW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
95 {mouseupW, onmouseupW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
99 eventid_t str_to_eid(LPCWSTR str)
103 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
104 if(!strcmpW(event_info[i].name, str))
108 ERR("unknown type %s\n", debugstr_w(str));
114 const IHTMLEventObjVtbl *lpIHTMLEventObjVtbl;
121 #define HTMLEVENTOBJ(x) ((IHTMLEventObj*) &(x)->lpIHTMLEventObjVtbl)
123 #define HTMLEVENTOBJ_THIS(iface) DEFINE_THIS(HTMLEventObj, IHTMLEventObj, iface)
125 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
127 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
131 if(IsEqualGUID(&IID_IUnknown, riid)) {
132 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
133 *ppv = HTMLEVENTOBJ(This);
134 }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
135 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
136 *ppv = HTMLEVENTOBJ(This);
137 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
138 return *ppv ? S_OK : E_NOINTERFACE;
142 IUnknown_AddRef((IUnknown*)*ppv);
146 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
147 return E_NOINTERFACE;
150 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
152 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
153 LONG ref = InterlockedIncrement(&This->ref);
155 TRACE("(%p) ref=%d\n", This, ref);
160 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
162 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
163 LONG ref = InterlockedDecrement(&This->ref);
165 TRACE("(%p) ref=%d\n", This, ref);
173 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
175 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
176 FIXME("(%p)->(%p)\n", This, pctinfo);
180 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
181 LCID lcid, ITypeInfo **ppTInfo)
183 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
184 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
188 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
189 LPOLESTR *rgszNames, UINT cNames,
190 LCID lcid, DISPID *rgDispId)
192 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
193 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
198 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
199 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
200 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
202 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
203 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
204 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
208 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
210 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
212 TRACE("(%p)->(%p)\n", This, p);
214 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(This->target), &IID_IHTMLElement, (void**)p);
217 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
219 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
220 FIXME("(%p)->(%p)\n", This, p);
224 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
226 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
227 FIXME("(%p)->(%p)\n", This, p);
231 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
233 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
234 FIXME("(%p)->(%p)\n", This, p);
238 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
240 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
241 FIXME("(%p)->()\n", This);
245 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
247 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
248 FIXME("(%p)->(%p)\n", This, p);
252 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
254 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
255 FIXME("(%p)->(%x)\n", This, v);
259 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
261 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
262 FIXME("(%p)->(%p)\n", This, p);
266 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
268 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
269 FIXME("(%p)->(%p)\n", This, p);
273 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
275 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
276 FIXME("(%p)->(%p)\n", This, p);
280 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, long v)
282 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
283 FIXME("(%p)->(%ld)\n", This, v);
287 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, long *p)
289 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
290 FIXME("(%p)->(%p)\n", This, p);
294 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, long *p)
296 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
297 FIXME("(%p)->(%p)\n", This, p);
301 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
303 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
304 FIXME("(%p)->(%p)\n", This, p);
308 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
310 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
311 FIXME("(%p)->(%p)\n", This, p);
315 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, long *p)
317 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
318 FIXME("(%p)->(%p)\n", This, p);
322 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, long *p)
324 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
325 FIXME("(%p)->(%p)\n", This, p);
329 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, long *p)
331 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
332 FIXME("(%p)->(%p)\n", This, p);
336 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, long *p)
338 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
339 FIXME("(%p)->(%p)\n", This, p);
343 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, long *p)
345 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
346 FIXME("(%p)->(%p)\n", This, p);
350 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, long *p)
352 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
353 FIXME("(%p)->(%p)\n", This, p);
357 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, long *p)
359 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
360 FIXME("(%p)->(%p)\n", This, p);
364 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, long *p)
366 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
367 FIXME("(%p)->(%p)\n", This, p);
371 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, long *p)
373 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
374 FIXME("(%p)->(%p)\n", This, p);
378 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
380 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
381 FIXME("(%p)->(%p)\n", This, p);
385 #undef HTMLEVENTOBJ_THIS
387 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
388 HTMLEventObj_QueryInterface,
390 HTMLEventObj_Release,
391 HTMLEventObj_GetTypeInfoCount,
392 HTMLEventObj_GetTypeInfo,
393 HTMLEventObj_GetIDsOfNames,
395 HTMLEventObj_get_srcElement,
396 HTMLEventObj_get_altKey,
397 HTMLEventObj_get_ctrlKey,
398 HTMLEventObj_get_shiftKey,
399 HTMLEventObj_put_returnValue,
400 HTMLEventObj_get_returnValue,
401 HTMLEventObj_put_cancelBubble,
402 HTMLEventObj_get_cancelBubble,
403 HTMLEventObj_get_fromElement,
404 HTMLEventObj_get_toElement,
405 HTMLEventObj_put_keyCode,
406 HTMLEventObj_get_keyCode,
407 HTMLEventObj_get_button,
408 HTMLEventObj_get_type,
409 HTMLEventObj_get_qualifier,
410 HTMLEventObj_get_reason,
413 HTMLEventObj_get_clientX,
414 HTMLEventObj_get_clientY,
415 HTMLEventObj_get_offsetX,
416 HTMLEventObj_get_offsetY,
417 HTMLEventObj_get_screenX,
418 HTMLEventObj_get_screenY,
419 HTMLEventObj_get_srcFilter
422 static const tid_t HTMLEventObj_iface_tids[] = {
427 static dispex_static_data_t HTMLEventObj_dispex = {
431 HTMLEventObj_iface_tids
434 static IHTMLEventObj *create_event(HTMLDOMNode *target)
438 ret = heap_alloc(sizeof(*ret));
439 ret->lpIHTMLEventObjVtbl = &HTMLEventObjVtbl;
441 ret->target = target;
442 IHTMLDOMNode_AddRef(HTMLDOMNODE(target));
444 init_dispex(&ret->dispex, (IUnknown*)HTMLEVENTOBJ(ret), &HTMLEventObj_dispex);
446 return HTMLEVENTOBJ(ret);
449 void fire_event(HTMLDocument *doc, eventid_t eid, nsIDOMNode *target)
451 IHTMLEventObj *prev_event, *event_obj = NULL;
452 nsIDOMNode *parent, *nsnode;
456 nsIDOMNode_GetNodeType(target, &node_type);
457 if(node_type != ELEMENT_NODE) {
458 FIXME("node type %d node supported\n", node_type);
462 prev_event = doc->window->event;
464 nsIDOMNode_AddRef(nsnode);
467 node = get_node(doc, nsnode, FALSE);
469 if(node && node->event_target && node->event_target->event_table[eid]) {
471 event_obj = doc->window->event = create_event(get_node(doc, target, TRUE));
473 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
474 call_disp_func(doc, node->event_target->event_table[eid], (IDispatch*)HTMLDOMNODE(node));
475 TRACE("%s <<<\n", debugstr_w(event_info[eid].name));
478 if(!(event_info[eid].flags & EVENT_BUBBLE))
481 nsIDOMNode_GetParentNode(nsnode, &parent);
482 nsIDOMNode_Release(nsnode);
487 nsIDOMNode_GetNodeType(nsnode, &node_type);
488 if(node_type != ELEMENT_NODE)
493 nsIDOMNode_Release(nsnode);
495 if((event_info[eid].flags & EVENT_BUBBLE) && doc->event_target && doc->event_target->event_table[eid]) {
497 event_obj = doc->window->event = create_event(get_node(doc, target, TRUE));
499 TRACE("doc %s >>>\n", debugstr_w(event_info[eid].name));
500 call_disp_func(doc, doc->event_target->event_table[eid], (IDispatch*)HTMLDOC(doc));
501 TRACE("doc %s <<<\n", debugstr_w(event_info[eid].name));
505 IHTMLEventObj_Release(event_obj);
506 doc->window->event = prev_event;
510 static HRESULT set_event_handler_disp(event_target_t **event_target, HTMLDocument *doc, eventid_t eid, IDispatch *disp)
513 *event_target = heap_alloc_zero(sizeof(event_target_t));
514 else if((*event_target)->event_table[eid])
515 IDispatch_Release((*event_target)->event_table[eid]);
517 (*event_target)->event_table[eid] = disp;
520 IDispatch_AddRef(disp);
522 if(event_info[eid].flags & EVENT_DEFAULTLISTENER) {
523 if(!doc->nscontainer->event_vector) {
524 doc->nscontainer->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
525 if(!doc->nscontainer->event_vector)
526 return E_OUTOFMEMORY;
529 if(!doc->nscontainer->event_vector[eid]) {
530 doc->nscontainer->event_vector[eid] = TRUE;
531 add_nsevent_listener(doc->nscontainer, event_info[eid].name);
538 HRESULT set_event_handler(event_target_t **event_target, HTMLDocument *doc, eventid_t eid, VARIANT *var)
542 if(*event_target && (*event_target)->event_table[eid]) {
543 IDispatch_Release((*event_target)->event_table[eid]);
544 (*event_target)->event_table[eid] = NULL;
549 return set_event_handler_disp(event_target, doc, eid, V_DISPATCH(var));
552 FIXME("not supported vt=%d\n", V_VT(var));
559 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
561 if(*event_target && (*event_target)->event_table[eid]) {
562 V_VT(var) = VT_DISPATCH;
563 V_DISPATCH(var) = (*event_target)->event_table[eid];
564 IDispatch_AddRef(V_DISPATCH(var));
572 void check_event_attr(HTMLDocument *doc, nsIDOMElement *nselem)
574 const PRUnichar *attr_value;
575 nsAString attr_name_str, attr_value_str;
581 nsAString_Init(&attr_value_str, NULL);
582 nsAString_Init(&attr_name_str, NULL);
584 for(i=0; i < EVENTID_LAST; i++) {
585 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
586 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
587 if(NS_SUCCEEDED(nsres)) {
588 nsAString_GetData(&attr_value_str, &attr_value);
592 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
594 disp = script_parse_event(doc, attr_value);
596 node = get_node(doc, (nsIDOMNode*)nselem, TRUE);
597 set_event_handler_disp(&node->event_target, node->doc, i, disp);
598 IDispatch_Release(disp);
603 nsAString_Finish(&attr_value_str);
604 nsAString_Finish(&attr_name_str);
607 void release_event_target(event_target_t *event_target)
611 for(i=0; i < EVENTID_LAST; i++) {
612 if(event_target->event_table[i])
613 IDispatch_Release(event_target->event_table[i]);
616 heap_free(event_target);