kernel32: Update Italian translation.
[wine/testsucceed.git] / dlls / mshtml / htmlevent.c
blob6925a0cf2ace9791f8c014367ed4e63f6a7ec707
1 /*
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
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "mshtmdid.h"
29 #include "mshtml_private.h"
30 #include "htmlevent.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36 typedef struct {
37 IDispatch *handler_prop;
38 DWORD handler_cnt;
39 IDispatch *handlers[0];
40 } handler_vector_t;
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 mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
87 static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};
89 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
90 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
92 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
93 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
95 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
96 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
98 static const WCHAR readystatechangeW[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
99 static const WCHAR onreadystatechangeW[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
101 static const WCHAR resizeW[] = {'r','e','s','i','z','e',0};
102 static const WCHAR onresizeW[] = {'o','n','r','e','s','i','z','e',0};
104 static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
105 static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
107 static const WCHAR HTMLEventsW[] = {'H','T','M','L','E','v','e','n','t','s',0};
108 static const WCHAR KeyboardEventW[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
109 static const WCHAR MouseEventW[] = {'M','o','u','s','e','E','v','e','n','t',0};
111 enum {
112 EVENTT_NONE,
113 EVENTT_HTML,
114 EVENTT_KEY,
115 EVENTT_MOUSE
118 static const WCHAR *event_types[] = {
119 NULL,
120 HTMLEventsW,
121 KeyboardEventW,
122 MouseEventW
125 typedef struct {
126 LPCWSTR name;
127 LPCWSTR attr_name;
128 DWORD type;
129 DISPID dispid;
130 DWORD flags;
131 } event_info_t;
133 #define EVENT_DEFAULTLISTENER 0x0001
134 #define EVENT_BUBBLE 0x0002
135 #define EVENT_FORWARDBODY 0x0004
136 #define EVENT_NODEHANDLER 0x0008
138 static const event_info_t event_info[] = {
139 {beforeunloadW, onbeforeunloadW, EVENTT_NONE, DISPID_EVMETH_ONBEFOREUNLOAD,
140 EVENT_DEFAULTLISTENER|EVENT_FORWARDBODY},
141 {blurW, onblurW, EVENTT_HTML, DISPID_EVMETH_ONBLUR,
142 EVENT_DEFAULTLISTENER},
143 {changeW, onchangeW, EVENTT_HTML, DISPID_EVMETH_ONCHANGE,
144 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
145 {clickW, onclickW, EVENTT_MOUSE, DISPID_EVMETH_ONCLICK,
146 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
147 {contextmenuW, oncontextmenuW, EVENTT_MOUSE, DISPID_EVMETH_ONCONTEXTMENU,
148 EVENT_BUBBLE},
149 {dblclickW, ondblclickW, EVENTT_MOUSE, DISPID_EVMETH_ONDBLCLICK,
150 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
151 {dragW, ondragW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAG,
153 {dragstartW, ondragstartW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAGSTART,
155 {focusW, onfocusW, EVENTT_HTML, DISPID_EVMETH_ONFOCUS,
156 EVENT_DEFAULTLISTENER},
157 {keydownW, onkeydownW, EVENTT_KEY, DISPID_EVMETH_ONKEYDOWN,
158 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
159 {keyupW, onkeyupW, EVENTT_KEY, DISPID_EVMETH_ONKEYUP,
160 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
161 {loadW, onloadW, EVENTT_HTML, DISPID_EVMETH_ONLOAD,
162 EVENT_NODEHANDLER},
163 {mousedownW, onmousedownW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEDOWN,
164 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
165 {mouseoutW, onmouseoutW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEOUT,
166 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
167 {mouseoverW, onmouseoverW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEOVER,
168 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
169 {mouseupW, onmouseupW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEUP,
170 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
171 {pasteW, onpasteW, EVENTT_NONE, DISPID_EVMETH_ONPASTE,
173 {readystatechangeW, onreadystatechangeW, EVENTT_NONE, DISPID_EVMETH_ONREADYSTATECHANGE,
175 {resizeW, onresizeW, EVENTT_NONE, DISPID_EVMETH_ONRESIZE,
176 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
177 {selectstartW, onselectstartW, EVENTT_MOUSE, DISPID_EVMETH_ONSELECTSTART,
181 static const eventid_t node_handled_list[] = { EVENTID_LOAD };
183 eventid_t str_to_eid(LPCWSTR str)
185 int i;
187 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
188 if(!strcmpW(event_info[i].name, str))
189 return i;
192 ERR("unknown type %s\n", debugstr_w(str));
193 return EVENTID_LAST;
196 static eventid_t attr_to_eid(LPCWSTR str)
198 int i;
200 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
201 if(!strcmpW(event_info[i].attr_name, str))
202 return i;
205 return EVENTID_LAST;
208 static DWORD get_node_handler_mask(eventid_t eid)
210 DWORD i;
212 for(i=0; i<sizeof(node_handled_list)/sizeof(*node_handled_list); i++) {
213 if(node_handled_list[i] == eid)
214 return 1 << i;
217 ERR("Invalid eid %d\n", eid);
218 return ~0;
221 typedef struct {
222 DispatchEx dispex;
223 const IHTMLEventObjVtbl *lpIHTMLEventObjVtbl;
225 LONG ref;
227 HTMLDOMNode *target;
228 const event_info_t *type;
229 nsIDOMEvent *nsevent;
230 } HTMLEventObj;
232 #define HTMLEVENTOBJ(x) ((IHTMLEventObj*) &(x)->lpIHTMLEventObjVtbl)
234 #define HTMLEVENTOBJ_THIS(iface) DEFINE_THIS(HTMLEventObj, IHTMLEventObj, iface)
236 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
238 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
240 *ppv = NULL;
242 if(IsEqualGUID(&IID_IUnknown, riid)) {
243 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
244 *ppv = HTMLEVENTOBJ(This);
245 }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
246 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
247 *ppv = HTMLEVENTOBJ(This);
248 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
249 return *ppv ? S_OK : E_NOINTERFACE;
252 if(*ppv) {
253 IUnknown_AddRef((IUnknown*)*ppv);
254 return S_OK;
257 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
258 return E_NOINTERFACE;
261 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
263 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
264 LONG ref = InterlockedIncrement(&This->ref);
266 TRACE("(%p) ref=%d\n", This, ref);
268 return ref;
271 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
273 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
274 LONG ref = InterlockedDecrement(&This->ref);
276 TRACE("(%p) ref=%d\n", This, ref);
278 if(!ref) {
279 if(This->nsevent)
280 nsIDOMEvent_Release(This->nsevent);
281 release_dispex(&This->dispex);
282 heap_free(This);
285 return ref;
288 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
290 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
291 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
294 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
295 LCID lcid, ITypeInfo **ppTInfo)
297 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
298 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
301 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
302 LPOLESTR *rgszNames, UINT cNames,
303 LCID lcid, DISPID *rgDispId)
305 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
306 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
309 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
310 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
311 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
313 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
314 return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid,
315 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
318 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
320 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
322 TRACE("(%p)->(%p)\n", This, p);
324 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(This->target), &IID_IHTMLElement, (void**)p);
327 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
329 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
330 PRBool ret = FALSE;
332 TRACE("(%p)->(%p)\n", This, p);
334 if(This->nsevent) {
335 nsIDOMKeyEvent *key_event;
336 nsresult nsres;
338 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
339 if(NS_SUCCEEDED(nsres)) {
340 nsIDOMKeyEvent_GetAltKey(key_event, &ret);
341 nsIDOMKeyEvent_Release(key_event);
342 }else {
343 nsIDOMMouseEvent *mouse_event;
345 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
346 if(NS_SUCCEEDED(nsres)) {
347 nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
348 nsIDOMMouseEvent_Release(mouse_event);
353 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
354 return S_OK;
357 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
359 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
360 PRBool ret = FALSE;
362 TRACE("(%p)->(%p)\n", This, p);
364 if(This->nsevent) {
365 nsIDOMKeyEvent *key_event;
366 nsresult nsres;
368 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
369 if(NS_SUCCEEDED(nsres)) {
370 nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
371 nsIDOMKeyEvent_Release(key_event);
372 }else {
373 nsIDOMMouseEvent *mouse_event;
375 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
376 if(NS_SUCCEEDED(nsres)) {
377 nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
378 nsIDOMMouseEvent_Release(mouse_event);
383 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
384 return S_OK;
387 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
389 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
390 PRBool ret = FALSE;
392 TRACE("(%p)->(%p)\n", This, p);
394 if(This->nsevent) {
395 nsIDOMKeyEvent *key_event;
396 nsresult nsres;
398 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
399 if(NS_SUCCEEDED(nsres)) {
400 nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
401 nsIDOMKeyEvent_Release(key_event);
402 }else {
403 nsIDOMMouseEvent *mouse_event;
405 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
406 if(NS_SUCCEEDED(nsres)) {
407 nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
408 nsIDOMMouseEvent_Release(mouse_event);
413 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
414 return S_OK;
417 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
419 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
420 FIXME("(%p)->()\n", This);
421 return E_NOTIMPL;
424 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
426 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
428 FIXME("(%p)->(%p)\n", This, p);
430 V_VT(p) = VT_EMPTY;
431 return S_OK;
434 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
436 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
437 FIXME("(%p)->(%x)\n", This, v);
438 return E_NOTIMPL;
441 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
443 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
445 FIXME("(%p)->(%p)\n", This, p);
447 *p = VARIANT_FALSE;
448 return S_OK;
451 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
453 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
455 FIXME("(%p)->(%p)\n", This, p);
457 *p = NULL;
458 return S_OK;
461 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
463 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
465 FIXME("(%p)->(%p)\n", This, p);
467 *p = NULL;
468 return S_OK;
471 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
473 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
474 FIXME("(%p)->(%d)\n", This, v);
475 return E_NOTIMPL;
478 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
480 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
481 PRUint32 key_code = 0;
483 TRACE("(%p)->(%p)\n", This, p);
485 if(This->nsevent) {
486 nsIDOMKeyEvent *key_event;
487 nsresult nsres;
489 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
490 if(NS_SUCCEEDED(nsres)) {
491 nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
492 nsIDOMKeyEvent_Release(key_event);
496 *p = key_code;
497 return S_OK;
500 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
502 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
503 PRUint16 button = 0;
505 TRACE("(%p)->(%p)\n", This, p);
507 if(This->nsevent) {
508 nsIDOMMouseEvent *mouse_event;
509 nsresult nsres;
511 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
512 if(NS_SUCCEEDED(nsres)) {
513 nsIDOMMouseEvent_GetButton(mouse_event, &button);
514 nsIDOMMouseEvent_Release(mouse_event);
518 *p = button;
519 return S_OK;
522 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
524 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
526 TRACE("(%p)->(%p)\n", This, p);
528 *p = SysAllocString(This->type->name);
529 return *p ? S_OK : E_OUTOFMEMORY;
532 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
534 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
536 FIXME("(%p)->(%p)\n", This, p);
538 *p = NULL;
539 return S_OK;
542 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
544 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
546 FIXME("(%p)->(%p)\n", This, p);
548 *p = 0;
549 return S_OK;
552 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
554 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
556 FIXME("(%p)->(%p)\n", This, p);
558 *p = -1;
559 return S_OK;
562 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
564 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
566 FIXME("(%p)->(%p)\n", This, p);
568 *p = -1;
569 return S_OK;
572 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
574 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
575 PRInt32 x = 0;
577 TRACE("(%p)->(%p)\n", This, p);
579 if(This->nsevent) {
580 nsIDOMMouseEvent *mouse_event;
581 nsresult nsres;
583 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
584 if(NS_SUCCEEDED(nsres)) {
585 nsIDOMMouseEvent_GetClientX(mouse_event, &x);
586 nsIDOMMouseEvent_Release(mouse_event);
590 *p = x;
591 return S_OK;
594 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
596 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
597 PRInt32 y = 0;
599 TRACE("(%p)->(%p)\n", This, p);
601 if(This->nsevent) {
602 nsIDOMMouseEvent *mouse_event;
603 nsresult nsres;
605 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
606 if(NS_SUCCEEDED(nsres)) {
607 nsIDOMMouseEvent_GetClientY(mouse_event, &y);
608 nsIDOMMouseEvent_Release(mouse_event);
612 *p = y;
613 return S_OK;
616 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
618 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
620 FIXME("(%p)->(%p)\n", This, p);
622 *p = 0;
623 return S_OK;
626 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
628 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
630 FIXME("(%p)->(%p)\n", This, p);
632 *p = 0;
633 return S_OK;
636 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
638 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
639 PRInt32 x = 0;
641 TRACE("(%p)->(%p)\n", This, p);
643 if(This->nsevent) {
644 nsIDOMMouseEvent *mouse_event;
645 nsresult nsres;
647 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
648 if(NS_SUCCEEDED(nsres)) {
649 nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
650 nsIDOMMouseEvent_Release(mouse_event);
654 *p = x;
655 return S_OK;
658 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
660 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
661 PRInt32 y = 0;
663 TRACE("(%p)->(%p)\n", This, p);
665 if(This->nsevent) {
666 nsIDOMMouseEvent *mouse_event;
667 nsresult nsres;
669 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
670 if(NS_SUCCEEDED(nsres)) {
671 nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
672 nsIDOMMouseEvent_Release(mouse_event);
676 *p = y;
677 return S_OK;
680 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
682 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
684 FIXME("(%p)->(%p)\n", This, p);
686 *p = NULL;
687 return S_OK;
690 #undef HTMLEVENTOBJ_THIS
692 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
693 HTMLEventObj_QueryInterface,
694 HTMLEventObj_AddRef,
695 HTMLEventObj_Release,
696 HTMLEventObj_GetTypeInfoCount,
697 HTMLEventObj_GetTypeInfo,
698 HTMLEventObj_GetIDsOfNames,
699 HTMLEventObj_Invoke,
700 HTMLEventObj_get_srcElement,
701 HTMLEventObj_get_altKey,
702 HTMLEventObj_get_ctrlKey,
703 HTMLEventObj_get_shiftKey,
704 HTMLEventObj_put_returnValue,
705 HTMLEventObj_get_returnValue,
706 HTMLEventObj_put_cancelBubble,
707 HTMLEventObj_get_cancelBubble,
708 HTMLEventObj_get_fromElement,
709 HTMLEventObj_get_toElement,
710 HTMLEventObj_put_keyCode,
711 HTMLEventObj_get_keyCode,
712 HTMLEventObj_get_button,
713 HTMLEventObj_get_type,
714 HTMLEventObj_get_qualifier,
715 HTMLEventObj_get_reason,
716 HTMLEventObj_get_x,
717 HTMLEventObj_get_y,
718 HTMLEventObj_get_clientX,
719 HTMLEventObj_get_clientY,
720 HTMLEventObj_get_offsetX,
721 HTMLEventObj_get_offsetY,
722 HTMLEventObj_get_screenX,
723 HTMLEventObj_get_screenY,
724 HTMLEventObj_get_srcFilter
727 static const tid_t HTMLEventObj_iface_tids[] = {
728 IHTMLEventObj_tid,
732 static dispex_static_data_t HTMLEventObj_dispex = {
733 NULL,
734 DispCEventObj_tid,
735 NULL,
736 HTMLEventObj_iface_tids
739 static IHTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
741 HTMLEventObj *ret;
743 ret = heap_alloc(sizeof(*ret));
744 if(!ret)
745 return NULL;
747 ret->lpIHTMLEventObjVtbl = &HTMLEventObjVtbl;
748 ret->ref = 1;
749 ret->type = event_info+eid;
751 ret->nsevent = nsevent;
752 if(nsevent) {
753 nsIDOMEvent_AddRef(nsevent);
754 }else if(event_types[event_info[eid].type]) {
755 nsIDOMDocumentEvent *doc_event;
756 nsresult nsres;
758 nsres = nsIDOMHTMLDocument_QueryInterface(target->doc->nsdoc, &IID_nsIDOMDocumentEvent,
759 (void**)&doc_event);
760 if(NS_SUCCEEDED(nsres)) {
761 nsAString type_str;
763 nsAString_InitDepend(&type_str, event_types[event_info[eid].type]);
764 nsres = nsIDOMDocumentEvent_CreateEvent(doc_event, &type_str, &ret->nsevent);
765 nsAString_Finish(&type_str);
766 nsIDOMDocumentEvent_Release(doc_event);
768 if(NS_FAILED(nsres)) {
769 ERR("Could not create event: %08x\n", nsres);
770 IHTMLEventObj_Release(HTMLEVENTOBJ(ret));
771 return NULL;
775 ret->target = target;
776 IHTMLDOMNode_AddRef(HTMLDOMNODE(target));
778 init_dispex(&ret->dispex, (IUnknown*)HTMLEVENTOBJ(ret), &HTMLEventObj_dispex);
780 return HTMLEVENTOBJ(ret);
783 static HRESULT call_cp_func(IDispatch *disp, DISPID dispid)
785 DISPPARAMS dp = {NULL,NULL,0,0};
786 ULONG argerr;
787 EXCEPINFO ei;
788 VARIANT vres;
789 HRESULT hres;
791 V_VT(&vres) = VT_EMPTY;
792 memset(&ei, 0, sizeof(ei));
793 hres = IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, &vres, &ei, &argerr);
794 if(SUCCEEDED(hres) && V_VT(&vres) != VT_EMPTY) {
795 FIXME("handle result %s\n", debugstr_variant(&vres));
796 VariantClear(&vres);
799 return hres;
802 static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
804 int min, max, i;
805 HRESULT hres;
807 if(!data)
808 return FALSE;
810 if(!data->ids) {
811 hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
812 if(FAILED(hres))
813 return FALSE;
816 min = 0;
817 max = data->id_cnt-1;
818 while(min <= max) {
819 i = (min+max)/2;
820 if(data->ids[i] == dispid)
821 return TRUE;
823 if(data->ids[i] < dispid)
824 min = i+1;
825 else
826 max = i-1;
829 return FALSE;
832 static void call_event_handlers(HTMLDocumentNode *doc, IHTMLEventObj *event_obj, event_target_t *event_target,
833 ConnectionPointContainer *cp_container, eventid_t eid, IDispatch *this_obj)
835 handler_vector_t *handler_vector = NULL;
836 int i;
837 HRESULT hres;
839 if(event_target)
840 handler_vector = event_target->event_table[eid];
842 if(handler_vector && handler_vector->handler_prop) {
843 DISPID named_arg = DISPID_THIS;
844 VARIANTARG arg;
845 DISPPARAMS dp = {&arg, &named_arg, 1, 1};
847 V_VT(&arg) = VT_DISPATCH;
848 V_DISPATCH(&arg) = this_obj;
850 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
851 hres = call_disp_func(handler_vector->handler_prop, &dp);
852 if(hres == S_OK)
853 TRACE("%s <<<\n", debugstr_w(event_info[eid].name));
854 else
855 WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
858 if(handler_vector && handler_vector->handler_cnt) {
859 VARIANTARG arg;
860 DISPPARAMS dp = {&arg, NULL, 1, 0};
862 V_VT(&arg) = VT_DISPATCH;
863 V_DISPATCH(&arg) = (IDispatch*)event_obj;
865 i = handler_vector->handler_cnt;
866 while(i--) {
867 if(handler_vector->handlers[i]) {
868 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
869 hres = call_disp_func(handler_vector->handlers[i], &dp);
870 if(hres == S_OK)
871 TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
872 else
873 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
878 if(cp_container) {
879 ConnectionPoint *cp;
881 if(cp_container->forward_container)
882 cp_container = cp_container->forward_container;
884 for(cp = cp_container->cp_list; cp; cp = cp->next) {
885 if(cp->sinks_size && is_cp_event(cp->data, event_info[eid].dispid)) {
886 for(i=0; i < cp->sinks_size; i++) {
887 if(!cp->sinks[i].disp)
888 continue;
890 TRACE("cp %s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
891 hres = call_cp_func(cp->sinks[i].disp, event_info[eid].dispid);
892 if(hres == S_OK)
893 TRACE("cp %s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
894 else
895 WARN("cp %s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
902 void fire_event(HTMLDocumentNode *doc, eventid_t eid, BOOL set_event, nsIDOMNode *target, nsIDOMEvent *nsevent)
904 IHTMLEventObj *prev_event, *event_obj = NULL;
905 nsIDOMNode *parent, *nsnode;
906 HTMLDOMNode *node;
907 PRUint16 node_type;
909 TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
911 prev_event = doc->basedoc.window->event;
912 if(set_event)
913 event_obj = create_event(get_node(doc, target, TRUE), eid, nsevent);
914 doc->basedoc.window->event = event_obj;
916 nsIDOMNode_GetNodeType(target, &node_type);
917 nsnode = target;
918 nsIDOMNode_AddRef(nsnode);
920 switch(node_type) {
921 case ELEMENT_NODE:
922 do {
923 node = get_node(doc, nsnode, FALSE);
924 if(node)
925 call_event_handlers(doc, event_obj, *get_node_event_target(node), node->cp_container, eid,
926 (IDispatch*)HTMLDOMNODE(node));
928 if(!(event_info[eid].flags & EVENT_BUBBLE))
929 break;
931 nsIDOMNode_GetParentNode(nsnode, &parent);
932 nsIDOMNode_Release(nsnode);
933 nsnode = parent;
934 if(!nsnode)
935 break;
937 nsIDOMNode_GetNodeType(nsnode, &node_type);
938 }while(node_type == ELEMENT_NODE);
940 if(!(event_info[eid].flags & EVENT_BUBBLE))
941 break;
943 case DOCUMENT_NODE:
944 if(event_info[eid].flags & EVENT_FORWARDBODY) {
945 nsIDOMHTMLElement *nsbody;
946 nsresult nsres;
948 nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
949 if(NS_SUCCEEDED(nsres) && nsbody) {
950 node = get_node(doc, (nsIDOMNode*)nsbody, FALSE);
951 if(node)
952 call_event_handlers(doc, event_obj, *get_node_event_target(node), node->cp_container,
953 eid, (IDispatch*)HTMLDOMNODE(node));
954 nsIDOMHTMLElement_Release(nsbody);
955 }else {
956 ERR("Could not get body: %08x\n", nsres);
960 call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
961 (IDispatch*)HTMLDOC(&doc->basedoc));
962 break;
964 default:
965 FIXME("unimplemented node type %d\n", node_type);
968 if(nsnode)
969 nsIDOMNode_Release(nsnode);
971 if(event_obj)
972 IHTMLEventObj_Release(event_obj);
973 doc->basedoc.window->event = prev_event;
976 HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_obj, VARIANT_BOOL *cancelled)
978 eventid_t eid;
980 eid = attr_to_eid(event_name);
981 if(eid == EVENTID_LAST) {
982 WARN("unknown event %s\n", debugstr_w(event_name));
983 return E_INVALIDARG;
986 if(event_obj && V_VT(event_obj) != VT_EMPTY && V_VT(event_obj) != VT_ERROR)
987 FIXME("event_obj not implemented\n");
989 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
990 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
991 return E_NOTIMPL;
994 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
996 *cancelled = VARIANT_TRUE; /* FIXME */
997 return S_OK;
1000 HRESULT call_event(HTMLDOMNode *node, eventid_t eid)
1002 HRESULT hres;
1004 if(node->vtbl->call_event) {
1005 BOOL handled = FALSE;
1007 hres = node->vtbl->call_event(node, eid, &handled);
1008 if(handled)
1009 return hres;
1012 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1013 return S_OK;
1016 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
1018 if(!*event_target_ptr)
1019 *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
1020 return *event_target_ptr;
1023 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
1025 handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
1027 if(handler_vector) {
1028 if(cnt <= handler_vector->handler_cnt)
1029 return TRUE;
1031 new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1032 }else {
1033 new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1036 if(!new_vector)
1037 return FALSE;
1039 new_vector->handler_cnt = cnt;
1040 event_target->event_table[eid] = new_vector;
1041 return TRUE;
1044 static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1046 if(!doc->nsdoc)
1047 return S_OK;
1049 if(event_info[eid].flags & EVENT_NODEHANDLER) {
1050 DWORD mask;
1052 mask = get_node_handler_mask(eid);
1053 if(event_target->node_handlers_mask & mask)
1054 return S_OK;
1056 add_nsevent_listener(doc, nsnode, event_info[eid].name);
1057 event_target->node_handlers_mask |= mask;
1058 return S_OK;
1061 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER))
1062 return S_OK;
1064 if(!doc->event_vector) {
1065 doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
1066 if(!doc->event_vector)
1067 return E_OUTOFMEMORY;
1070 if(!doc->event_vector[eid]) {
1071 doc->event_vector[eid] = TRUE;
1072 add_nsevent_listener(doc, NULL, event_info[eid].name);
1075 return S_OK;
1078 static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
1080 if(*event_target && (*event_target)->event_table[eid]->handler_prop) {
1081 IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
1082 (*event_target)->event_table[eid]->handler_prop = NULL;
1085 return S_OK;
1088 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocumentNode *doc,
1089 eventid_t eid, IDispatch *disp)
1091 event_target_t *event_target;
1093 if(!disp)
1094 return remove_event_handler(event_target_ptr, eid);
1096 event_target = get_event_target(event_target_ptr);
1097 if(!event_target)
1098 return E_OUTOFMEMORY;
1100 if(!alloc_handler_vector(event_target, eid, 0))
1101 return E_OUTOFMEMORY;
1103 if(event_target->event_table[eid]->handler_prop)
1104 IDispatch_Release(event_target->event_table[eid]->handler_prop);
1106 event_target->event_table[eid]->handler_prop = disp;
1107 IDispatch_AddRef(disp);
1109 return ensure_nsevent_handler(doc, event_target, nsnode, eid);
1112 HRESULT set_event_handler(event_target_t **event_target, nsIDOMNode *nsnode, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1114 switch(V_VT(var)) {
1115 case VT_NULL:
1116 return remove_event_handler(event_target, eid);
1118 case VT_DISPATCH:
1119 return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1121 default:
1122 FIXME("not supported vt=%d\n", V_VT(var));
1123 case VT_EMPTY:
1124 return E_NOTIMPL;
1127 return S_OK;
1130 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
1132 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1133 V_VT(var) = VT_DISPATCH;
1134 V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1135 IDispatch_AddRef(V_DISPATCH(var));
1136 }else {
1137 V_VT(var) = VT_NULL;
1140 return S_OK;
1143 HRESULT attach_event(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocument *doc, BSTR name,
1144 IDispatch *disp, VARIANT_BOOL *res)
1146 event_target_t *event_target;
1147 eventid_t eid;
1148 DWORD i = 0;
1150 eid = attr_to_eid(name);
1151 if(eid == EVENTID_LAST) {
1152 WARN("Unknown event\n");
1153 *res = VARIANT_TRUE;
1154 return S_OK;
1157 event_target = get_event_target(event_target_ptr);
1158 if(!event_target)
1159 return E_OUTOFMEMORY;
1161 if(event_target->event_table[eid]) {
1162 while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
1163 i++;
1164 if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
1165 return E_OUTOFMEMORY;
1166 }else if(!alloc_handler_vector(event_target, eid, i+1)) {
1167 return E_OUTOFMEMORY;
1170 IDispatch_AddRef(disp);
1171 event_target->event_table[eid]->handlers[i] = disp;
1173 *res = VARIANT_TRUE;
1174 return ensure_nsevent_handler(doc->doc_node, event_target, nsnode, eid);
1177 HRESULT detach_event(event_target_t *event_target, HTMLDocument *doc, BSTR name, IDispatch *disp)
1179 eventid_t eid;
1180 DWORD i = 0;
1182 if(!event_target)
1183 return S_OK;
1185 eid = attr_to_eid(name);
1186 if(eid == EVENTID_LAST) {
1187 WARN("Unknown event\n");
1188 return S_OK;
1191 if(!event_target->event_table[eid])
1192 return S_OK;
1194 while(i < event_target->event_table[eid]->handler_cnt) {
1195 if(event_target->event_table[eid]->handlers[i] == disp) {
1196 IDispatch_Release(event_target->event_table[eid]->handlers[i]);
1197 event_target->event_table[eid]->handlers[i] = NULL;
1199 i++;
1202 return S_OK;
1205 void update_cp_events(HTMLWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp, nsIDOMNode *nsnode)
1207 event_target_t *event_target;
1208 int i;
1210 event_target = get_event_target(event_target_ptr);
1211 if(!event_target)
1212 return; /* FIXME */
1214 for(i=0; i < EVENTID_LAST; i++) {
1215 if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1216 ensure_nsevent_handler(window->doc, event_target, nsnode, i);
1220 void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
1222 const PRUnichar *attr_value;
1223 nsAString attr_name_str, attr_value_str;
1224 IDispatch *disp;
1225 HTMLDOMNode *node;
1226 int i;
1227 nsresult nsres;
1229 nsAString_Init(&attr_value_str, NULL);
1230 nsAString_Init(&attr_name_str, NULL);
1232 for(i=0; i < EVENTID_LAST; i++) {
1233 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
1234 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
1235 if(NS_SUCCEEDED(nsres)) {
1236 nsAString_GetData(&attr_value_str, &attr_value);
1237 if(!*attr_value)
1238 continue;
1240 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
1242 disp = script_parse_event(doc->basedoc.window, attr_value);
1243 if(disp) {
1244 node = get_node(doc, (nsIDOMNode*)nselem, TRUE);
1245 set_event_handler_disp(get_node_event_target(node), node->nsnode, node->doc, i, disp);
1246 IDispatch_Release(disp);
1251 nsAString_Finish(&attr_value_str);
1252 nsAString_Finish(&attr_name_str);
1255 void release_event_target(event_target_t *event_target)
1257 int i, j;
1259 for(i=0; i < EVENTID_LAST; i++) {
1260 if(event_target->event_table[i]) {
1261 if(event_target->event_table[i]->handler_prop)
1262 IDispatch_Release(event_target->event_table[i]->handler_prop);
1263 for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
1264 IDispatch_Release(event_target->event_table[i]->handlers[j]);
1268 heap_free(event_target);