wininet: Support the Cache-Control max-age directive for setting url cache entry...
[wine/testsucceed.git] / dlls / mshtml / htmlevent.c
blobfc146d686cbb80624ec23b161fa5b5d200d80dd1
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 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};
117 enum {
118 EVENTT_NONE,
119 EVENTT_HTML,
120 EVENTT_KEY,
121 EVENTT_MOUSE
124 static const WCHAR *event_types[] = {
125 NULL,
126 HTMLEventsW,
127 KeyboardEventW,
128 MouseEventW
131 typedef struct {
132 LPCWSTR name;
133 LPCWSTR attr_name;
134 DWORD type;
135 DISPID dispid;
136 DWORD flags;
137 } event_info_t;
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,
159 EVENT_CANCELABLE},
160 {dragstartW, ondragstartW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAGSTART,
161 EVENT_CANCELABLE},
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,
169 EVENT_NODEHANDLER},
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,
181 EVENT_CANCELABLE},
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,
187 EVENT_CANCELABLE},
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)
196 int i;
198 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
199 if(!strcmpW(event_info[i].name, str))
200 return i;
203 ERR("unknown type %s\n", debugstr_w(str));
204 return EVENTID_LAST;
207 static eventid_t attr_to_eid(LPCWSTR str)
209 int i;
211 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
212 if(!strcmpW(event_info[i].attr_name, str))
213 return i;
216 return EVENTID_LAST;
219 static DWORD get_node_handler_mask(eventid_t eid)
221 DWORD i;
223 for(i=0; i<sizeof(node_handled_list)/sizeof(*node_handled_list); i++) {
224 if(node_handled_list[i] == eid)
225 return 1 << i;
228 ERR("Invalid eid %d\n", eid);
229 return ~0;
232 typedef struct {
233 DispatchEx dispex;
234 IHTMLEventObj IHTMLEventObj_iface;
236 LONG ref;
238 HTMLDOMNode *target;
239 const event_info_t *type;
240 nsIDOMEvent *nsevent;
241 BOOL prevent_default;
242 } HTMLEventObj;
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);
253 *ppv = NULL;
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;
265 if(*ppv) {
266 IUnknown_AddRef((IUnknown*)*ppv);
267 return S_OK;
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);
281 return 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);
291 if(!ref) {
292 if(This->nsevent)
293 nsIDOMEvent_Release(This->nsevent);
294 release_dispex(&This->dispex);
295 heap_free(This);
298 return ref;
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,
320 lcid, rgDispId);
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,
339 (void**)p);
342 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
344 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
345 PRBool ret = FALSE;
347 TRACE("(%p)->(%p)\n", This, p);
349 if(This->nsevent) {
350 nsIDOMKeyEvent *key_event;
351 nsresult nsres;
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);
357 }else {
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;
369 return S_OK;
372 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
374 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
375 PRBool ret = FALSE;
377 TRACE("(%p)->(%p)\n", This, p);
379 if(This->nsevent) {
380 nsIDOMKeyEvent *key_event;
381 nsresult nsres;
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);
387 }else {
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;
399 return S_OK;
402 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
404 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
405 PRBool ret = FALSE;
407 TRACE("(%p)->(%p)\n", This, p);
409 if(This->nsevent) {
410 nsIDOMKeyEvent *key_event;
411 nsresult nsres;
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);
417 }else {
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;
429 return S_OK;
432 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
434 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
435 FIXME("(%p)->()\n", This);
436 return E_NOTIMPL;
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);
445 V_VT(p) = VT_EMPTY;
446 return S_OK;
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);
453 return E_NOTIMPL;
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);
462 *p = VARIANT_FALSE;
463 return S_OK;
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);
472 *p = NULL;
473 return S_OK;
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);
482 *p = NULL;
483 return S_OK;
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);
490 return E_NOTIMPL;
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);
500 if(This->nsevent) {
501 nsIDOMKeyEvent *key_event;
502 nsresult nsres;
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);
511 *p = key_code;
512 return S_OK;
515 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
517 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
518 PRUint16 button = 0;
520 TRACE("(%p)->(%p)\n", This, p);
522 if(This->nsevent) {
523 nsIDOMMouseEvent *mouse_event;
524 nsresult nsres;
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);
533 *p = button;
534 return S_OK;
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);
553 *p = NULL;
554 return S_OK;
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);
563 *p = 0;
564 return S_OK;
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);
573 *p = -1;
574 return S_OK;
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);
583 *p = -1;
584 return S_OK;
587 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
589 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
590 PRInt32 x = 0;
592 TRACE("(%p)->(%p)\n", This, p);
594 if(This->nsevent) {
595 nsIDOMMouseEvent *mouse_event;
596 nsresult nsres;
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);
605 *p = x;
606 return S_OK;
609 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
611 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
612 PRInt32 y = 0;
614 TRACE("(%p)->(%p)\n", This, p);
616 if(This->nsevent) {
617 nsIDOMMouseEvent *mouse_event;
618 nsresult nsres;
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);
627 *p = y;
628 return S_OK;
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);
637 *p = 0;
638 return S_OK;
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);
647 *p = 0;
648 return S_OK;
651 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
653 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
654 PRInt32 x = 0;
656 TRACE("(%p)->(%p)\n", This, p);
658 if(This->nsevent) {
659 nsIDOMMouseEvent *mouse_event;
660 nsresult nsres;
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);
669 *p = x;
670 return S_OK;
673 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
675 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
676 PRInt32 y = 0;
678 TRACE("(%p)->(%p)\n", This, p);
680 if(This->nsevent) {
681 nsIDOMMouseEvent *mouse_event;
682 nsresult nsres;
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);
691 *p = y;
692 return S_OK;
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);
701 *p = NULL;
702 return S_OK;
705 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
706 HTMLEventObj_QueryInterface,
707 HTMLEventObj_AddRef,
708 HTMLEventObj_Release,
709 HTMLEventObj_GetTypeInfoCount,
710 HTMLEventObj_GetTypeInfo,
711 HTMLEventObj_GetIDsOfNames,
712 HTMLEventObj_Invoke,
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,
729 HTMLEventObj_get_x,
730 HTMLEventObj_get_y,
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[] = {
741 IHTMLEventObj_tid,
745 static dispex_static_data_t HTMLEventObj_dispex = {
746 NULL,
747 DispCEventObj_tid,
748 NULL,
749 HTMLEventObj_iface_tids
752 static HTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
754 HTMLEventObj *ret;
756 ret = heap_alloc_zero(sizeof(*ret));
757 if(!ret)
758 return NULL;
760 ret->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
761 ret->ref = 1;
762 ret->type = event_info+eid;
764 ret->nsevent = nsevent;
765 if(nsevent) {
766 nsIDOMEvent_AddRef(nsevent);
767 }else if(event_types[event_info[eid].type]) {
768 nsIDOMDocumentEvent *doc_event;
769 nsresult nsres;
771 nsres = nsIDOMHTMLDocument_QueryInterface(target->doc->nsdoc, &IID_nsIDOMDocumentEvent,
772 (void**)&doc_event);
773 if(NS_SUCCEEDED(nsres)) {
774 nsAString type_str;
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);
784 return NULL;
788 ret->target = target;
789 IHTMLDOMNode_AddRef(&target->IHTMLDOMNode_iface);
791 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLEventObj_iface, &HTMLEventObj_dispex);
793 return ret;
796 static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
798 IDispatchEx *dispex;
799 EXCEPINFO ei;
800 HRESULT hres;
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);
808 }else {
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);
814 return hres;
817 static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, VARIANT *retv)
819 DISPPARAMS dp = {NULL,NULL,0,0};
820 ULONG argerr;
821 EXCEPINFO ei;
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)
829 int min, max, i;
830 HRESULT hres;
832 if(!data)
833 return FALSE;
835 if(!data->ids) {
836 hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
837 if(FAILED(hres))
838 return FALSE;
841 min = 0;
842 max = data->id_cnt-1;
843 while(min <= max) {
844 i = (min+max)/2;
845 if(data->ids[i] == dispid)
846 return TRUE;
848 if(data->ids[i] < dispid)
849 min = i+1;
850 else
851 max = i-1;
854 return FALSE;
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;
862 VARIANT v;
863 int i;
864 HRESULT hres;
866 if(event_target)
867 handler_vector = event_target->event_table[eid];
869 if(handler_vector && handler_vector->handler_prop) {
870 DISPID named_arg = DISPID_THIS;
871 VARIANTARG arg;
872 DISPPARAMS dp = {&arg, &named_arg, 1, 1};
874 V_VT(&arg) = VT_DISPATCH;
875 V_DISPATCH(&arg) = this_obj;
876 V_VT(&v) = VT_EMPTY;
878 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
879 hres = call_disp_func(handler_vector->handler_prop, &dp, &v);
880 if(hres == S_OK) {
881 TRACE("%s <<< %s\n", debugstr_w(event_info[eid].name), debugstr_variant(&v));
883 if(cancelable) {
884 if(V_VT(&v) == VT_BOOL) {
885 if(!V_BOOL(&v))
886 event_obj->prevent_default = TRUE;
887 }else if(V_VT(&v) != VT_EMPTY) {
888 FIXME("unhandled result %s\n", debugstr_variant(&v));
891 VariantClear(&v);
892 }else {
893 WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
897 if(handler_vector && handler_vector->handler_cnt) {
898 VARIANTARG arg;
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;
905 while(i--) {
906 if(handler_vector->handlers[i]) {
907 V_VT(&v) = VT_EMPTY;
909 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
910 hres = call_disp_func(handler_vector->handlers[i], &dp, &v);
911 if(hres == S_OK) {
912 TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
914 if(cancelable) {
915 if(V_VT(&v) == VT_BOOL) {
916 if(!V_BOOL(&v))
917 event_obj->prevent_default = TRUE;
918 }else if(V_VT(&v) != VT_EMPTY) {
919 FIXME("unhandled result %s\n", debugstr_variant(&v));
922 VariantClear(&v);
923 }else {
924 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
930 if(cp_container) {
931 ConnectionPoint *cp;
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)
940 continue;
942 V_VT(&v) = VT_EMPTY;
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);
946 if(hres == S_OK) {
947 TRACE("cp %s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
949 if(cancelable) {
950 if(V_VT(&v) == VT_BOOL) {
951 if(!V_BOOL(&v))
952 event_obj->prevent_default = TRUE;
953 }else if(V_VT(&v) != VT_EMPTY) {
954 FIXME("unhandled result %s\n", debugstr_variant(&v));
957 VariantClear(&v);
958 }else {
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;
972 HTMLDOMNode *node;
973 PRUint16 node_type;
974 HRESULT hres;
976 TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
978 prev_event = doc->basedoc.window->event;
979 if(set_event) {
980 hres = get_node(doc, target, TRUE, &node);
981 if(FAILED(hres))
982 return;
984 event_obj = create_event(node, eid, nsevent);
986 doc->basedoc.window->event = &event_obj->IHTMLEventObj_iface;
988 nsIDOMNode_GetNodeType(target, &node_type);
989 nsnode = target;
990 nsIDOMNode_AddRef(nsnode);
992 switch(node_type) {
993 case ELEMENT_NODE:
994 do {
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))
1001 break;
1003 nsIDOMNode_GetParentNode(nsnode, &parent);
1004 nsIDOMNode_Release(nsnode);
1005 nsnode = parent;
1006 if(!nsnode)
1007 break;
1009 nsIDOMNode_GetNodeType(nsnode, &node_type);
1010 }while(node_type == ELEMENT_NODE);
1012 if(!(event_info[eid].flags & EVENT_BUBBLE))
1013 break;
1015 case DOCUMENT_NODE:
1016 if(event_info[eid].flags & EVENT_FORWARDBODY) {
1017 nsIDOMHTMLElement *nsbody;
1018 nsresult nsres;
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);
1027 }else {
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);
1034 break;
1036 default:
1037 FIXME("unimplemented node type %d\n", node_type);
1040 if(nsnode)
1041 nsIDOMNode_Release(nsnode);
1043 doc->basedoc.window->event = prev_event;
1045 if(event_obj) {
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)
1056 eventid_t eid;
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");
1069 return E_NOTIMPL;
1072 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1074 *cancelled = VARIANT_TRUE; /* FIXME */
1075 return S_OK;
1078 HRESULT call_event(HTMLDOMNode *node, eventid_t eid)
1080 HRESULT hres;
1082 if(node->vtbl->call_event) {
1083 BOOL handled = FALSE;
1085 hres = node->vtbl->call_event(node, eid, &handled);
1086 if(handled)
1087 return hres;
1090 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1091 return S_OK;
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)
1107 return TRUE;
1109 new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1110 }else {
1111 new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1114 if(!new_vector)
1115 return FALSE;
1117 new_vector->handler_cnt = cnt;
1118 event_target->event_table[eid] = new_vector;
1119 return TRUE;
1122 static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1124 if(!doc->nsdoc)
1125 return S_OK;
1127 if(event_info[eid].flags & EVENT_NODEHANDLER) {
1128 DWORD mask;
1130 mask = get_node_handler_mask(eid);
1131 if(event_target->node_handlers_mask & mask)
1132 return S_OK;
1134 add_nsevent_listener(doc, nsnode, event_info[eid].name);
1135 event_target->node_handlers_mask |= mask;
1136 return S_OK;
1139 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER))
1140 return S_OK;
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);
1153 return S_OK;
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;
1163 return S_OK;
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;
1171 if(!disp)
1172 return remove_event_handler(event_target_ptr, eid);
1174 event_target = get_event_target(event_target_ptr);
1175 if(!event_target)
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)
1192 switch(V_VT(var)) {
1193 case VT_NULL:
1194 return remove_event_handler(event_target, eid);
1196 case VT_DISPATCH:
1197 return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1199 default:
1200 FIXME("not supported vt=%d\n", V_VT(var));
1201 case VT_EMPTY:
1202 return E_NOTIMPL;
1205 return S_OK;
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));
1214 }else {
1215 V_VT(var) = VT_NULL;
1218 return S_OK;
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;
1225 eventid_t eid;
1226 DWORD i = 0;
1228 eid = attr_to_eid(name);
1229 if(eid == EVENTID_LAST) {
1230 WARN("Unknown event\n");
1231 *res = VARIANT_TRUE;
1232 return S_OK;
1235 event_target = get_event_target(event_target_ptr);
1236 if(!event_target)
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])
1241 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)
1257 eventid_t eid;
1258 DWORD i = 0;
1260 if(!event_target)
1261 return S_OK;
1263 eid = attr_to_eid(name);
1264 if(eid == EVENTID_LAST) {
1265 WARN("Unknown event\n");
1266 return S_OK;
1269 if(!event_target->event_table[eid])
1270 return S_OK;
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;
1277 i++;
1280 return S_OK;
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;
1286 int i;
1288 event_target = get_event_target(event_target_ptr);
1289 if(!event_target)
1290 return; /* FIXME */
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;
1302 IDispatch *disp;
1303 HTMLDOMNode *node;
1304 int i;
1305 nsresult nsres;
1306 HRESULT hres;
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);
1316 if(!*attr_value)
1317 continue;
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);
1322 if(disp) {
1323 hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1324 if(SUCCEEDED(hres))
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)
1337 int i, j;
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);