dsound: Added missing LeaveCriticalSection (Coverity).
[wine/multimedia.git] / dlls / mshtml / htmlevent.c
blob2b9e62e98fb8da36e2d4c02eadc6159e3e4ad55f
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
144 #define EVENT_HASDEFAULTHANDLERS 0x0020
146 static const event_info_t event_info[] = {
147 {beforeunloadW, onbeforeunloadW, EVENTT_NONE, DISPID_EVMETH_ONBEFOREUNLOAD,
148 EVENT_DEFAULTLISTENER|EVENT_FORWARDBODY},
149 {blurW, onblurW, EVENTT_HTML, DISPID_EVMETH_ONBLUR,
150 EVENT_DEFAULTLISTENER},
151 {changeW, onchangeW, EVENTT_HTML, DISPID_EVMETH_ONCHANGE,
152 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
153 {clickW, onclickW, EVENTT_MOUSE, DISPID_EVMETH_ONCLICK,
154 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE|EVENT_HASDEFAULTHANDLERS},
155 {contextmenuW, oncontextmenuW, EVENTT_MOUSE, DISPID_EVMETH_ONCONTEXTMENU,
156 EVENT_BUBBLE|EVENT_CANCELABLE},
157 {dblclickW, ondblclickW, EVENTT_MOUSE, DISPID_EVMETH_ONDBLCLICK,
158 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE},
159 {dragW, ondragW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAG,
160 EVENT_CANCELABLE},
161 {dragstartW, ondragstartW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAGSTART,
162 EVENT_CANCELABLE},
163 {focusW, onfocusW, EVENTT_HTML, DISPID_EVMETH_ONFOCUS,
164 EVENT_DEFAULTLISTENER},
165 {keydownW, onkeydownW, EVENTT_KEY, DISPID_EVMETH_ONKEYDOWN,
166 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
167 {keyupW, onkeyupW, EVENTT_KEY, DISPID_EVMETH_ONKEYUP,
168 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
169 {loadW, onloadW, EVENTT_HTML, DISPID_EVMETH_ONLOAD,
170 EVENT_NODEHANDLER},
171 {mousedownW, onmousedownW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEDOWN,
172 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
173 {mousemoveW, onmousemoveW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEMOVE,
174 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
175 {mouseoutW, onmouseoutW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEOUT,
176 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
177 {mouseoverW, onmouseoverW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEOVER,
178 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
179 {mouseupW, onmouseupW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEUP,
180 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
181 {pasteW, onpasteW, EVENTT_NONE, DISPID_EVMETH_ONPASTE,
182 EVENT_CANCELABLE},
183 {readystatechangeW, onreadystatechangeW, EVENTT_NONE, DISPID_EVMETH_ONREADYSTATECHANGE,
185 {resizeW, onresizeW, EVENTT_NONE, DISPID_EVMETH_ONRESIZE,
186 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
187 {selectstartW, onselectstartW, EVENTT_MOUSE, DISPID_EVMETH_ONSELECTSTART,
188 EVENT_CANCELABLE},
189 {submitW, onsubmitW, EVENTT_HTML, DISPID_EVMETH_ONSUBMIT,
190 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE}
193 static const eventid_t node_handled_list[] = { EVENTID_LOAD };
195 eventid_t str_to_eid(LPCWSTR str)
197 int i;
199 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
200 if(!strcmpW(event_info[i].name, str))
201 return i;
204 ERR("unknown type %s\n", debugstr_w(str));
205 return EVENTID_LAST;
208 static eventid_t attr_to_eid(LPCWSTR str)
210 int i;
212 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
213 if(!strcmpW(event_info[i].attr_name, str))
214 return i;
217 return EVENTID_LAST;
220 static DWORD get_node_handler_mask(eventid_t eid)
222 DWORD i;
224 for(i=0; i<sizeof(node_handled_list)/sizeof(*node_handled_list); i++) {
225 if(node_handled_list[i] == eid)
226 return 1 << i;
229 ERR("Invalid eid %d\n", eid);
230 return ~0;
233 typedef struct {
234 DispatchEx dispex;
235 IHTMLEventObj IHTMLEventObj_iface;
237 LONG ref;
239 HTMLDOMNode *target;
240 const event_info_t *type;
241 nsIDOMEvent *nsevent;
242 BOOL prevent_default;
243 } HTMLEventObj;
245 static inline HTMLEventObj *impl_from_IHTMLEventObj(IHTMLEventObj *iface)
247 return CONTAINING_RECORD(iface, HTMLEventObj, IHTMLEventObj_iface);
250 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
252 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
254 *ppv = NULL;
256 if(IsEqualGUID(&IID_IUnknown, riid)) {
257 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
258 *ppv = &This->IHTMLEventObj_iface;
259 }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
260 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
261 *ppv = &This->IHTMLEventObj_iface;
262 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
263 return *ppv ? S_OK : E_NOINTERFACE;
266 if(*ppv) {
267 IUnknown_AddRef((IUnknown*)*ppv);
268 return S_OK;
271 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
272 return E_NOINTERFACE;
275 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
277 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
278 LONG ref = InterlockedIncrement(&This->ref);
280 TRACE("(%p) ref=%d\n", This, ref);
282 return ref;
285 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
287 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
288 LONG ref = InterlockedDecrement(&This->ref);
290 TRACE("(%p) ref=%d\n", This, ref);
292 if(!ref) {
293 if(This->target)
294 IHTMLDOMNode_Release(&This->target->IHTMLDOMNode_iface);
295 if(This->nsevent)
296 nsIDOMEvent_Release(This->nsevent);
297 release_dispex(&This->dispex);
298 heap_free(This);
301 return ref;
304 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
306 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
307 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
310 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
311 LCID lcid, ITypeInfo **ppTInfo)
313 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
314 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
317 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
318 LPOLESTR *rgszNames, UINT cNames,
319 LCID lcid, DISPID *rgDispId)
321 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
322 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
323 lcid, rgDispId);
326 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
327 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
328 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
330 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
331 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
332 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
335 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
337 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
339 TRACE("(%p)->(%p)\n", This, p);
341 return IHTMLDOMNode_QueryInterface(&This->target->IHTMLDOMNode_iface, &IID_IHTMLElement,
342 (void**)p);
345 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
347 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
348 PRBool ret = FALSE;
350 TRACE("(%p)->(%p)\n", This, p);
352 if(This->nsevent) {
353 nsIDOMKeyEvent *key_event;
354 nsresult nsres;
356 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
357 if(NS_SUCCEEDED(nsres)) {
358 nsIDOMKeyEvent_GetAltKey(key_event, &ret);
359 nsIDOMKeyEvent_Release(key_event);
360 }else {
361 nsIDOMMouseEvent *mouse_event;
363 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
364 if(NS_SUCCEEDED(nsres)) {
365 nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
366 nsIDOMMouseEvent_Release(mouse_event);
371 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
372 return S_OK;
375 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
377 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
378 PRBool ret = FALSE;
380 TRACE("(%p)->(%p)\n", This, p);
382 if(This->nsevent) {
383 nsIDOMKeyEvent *key_event;
384 nsresult nsres;
386 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
387 if(NS_SUCCEEDED(nsres)) {
388 nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
389 nsIDOMKeyEvent_Release(key_event);
390 }else {
391 nsIDOMMouseEvent *mouse_event;
393 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
394 if(NS_SUCCEEDED(nsres)) {
395 nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
396 nsIDOMMouseEvent_Release(mouse_event);
401 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
402 return S_OK;
405 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
407 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
408 PRBool ret = FALSE;
410 TRACE("(%p)->(%p)\n", This, p);
412 if(This->nsevent) {
413 nsIDOMKeyEvent *key_event;
414 nsresult nsres;
416 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
417 if(NS_SUCCEEDED(nsres)) {
418 nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
419 nsIDOMKeyEvent_Release(key_event);
420 }else {
421 nsIDOMMouseEvent *mouse_event;
423 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
424 if(NS_SUCCEEDED(nsres)) {
425 nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
426 nsIDOMMouseEvent_Release(mouse_event);
431 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
432 return S_OK;
435 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
437 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
439 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
441 if(V_VT(&v) != VT_BOOL) {
442 FIXME("unsupported vt %d\n", V_VT(&v));
443 return DISP_E_BADVARTYPE;
446 if(!V_BOOL(&v))
447 This->prevent_default = TRUE;
448 return S_OK;
451 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
453 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
455 FIXME("(%p)->(%p)\n", This, p);
457 V_VT(p) = VT_EMPTY;
458 return S_OK;
461 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
463 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
464 FIXME("(%p)->(%x)\n", This, v);
465 return E_NOTIMPL;
468 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
470 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
472 FIXME("(%p)->(%p)\n", This, p);
474 *p = VARIANT_FALSE;
475 return S_OK;
478 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
480 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
482 FIXME("(%p)->(%p)\n", This, p);
484 *p = NULL;
485 return S_OK;
488 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
490 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
492 FIXME("(%p)->(%p)\n", This, p);
494 *p = NULL;
495 return S_OK;
498 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
500 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
501 FIXME("(%p)->(%d)\n", This, v);
502 return E_NOTIMPL;
505 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
507 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
508 PRUint32 key_code = 0;
510 TRACE("(%p)->(%p)\n", This, p);
512 if(This->nsevent) {
513 nsIDOMKeyEvent *key_event;
514 nsresult nsres;
516 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
517 if(NS_SUCCEEDED(nsres)) {
518 nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
519 nsIDOMKeyEvent_Release(key_event);
523 *p = key_code;
524 return S_OK;
527 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
529 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
530 PRUint16 button = 0;
532 TRACE("(%p)->(%p)\n", This, p);
534 if(This->nsevent) {
535 nsIDOMMouseEvent *mouse_event;
536 nsresult nsres;
538 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
539 if(NS_SUCCEEDED(nsres)) {
540 nsIDOMMouseEvent_GetButton(mouse_event, &button);
541 nsIDOMMouseEvent_Release(mouse_event);
545 *p = button;
546 return S_OK;
549 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
551 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
553 TRACE("(%p)->(%p)\n", This, p);
555 *p = SysAllocString(This->type->name);
556 return *p ? S_OK : E_OUTOFMEMORY;
559 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
561 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
563 FIXME("(%p)->(%p)\n", This, p);
565 *p = NULL;
566 return S_OK;
569 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
571 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
573 FIXME("(%p)->(%p)\n", This, p);
575 *p = 0;
576 return S_OK;
579 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
581 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
583 FIXME("(%p)->(%p)\n", This, p);
585 *p = -1;
586 return S_OK;
589 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
591 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
593 FIXME("(%p)->(%p)\n", This, p);
595 *p = -1;
596 return S_OK;
599 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
601 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
602 PRInt32 x = 0;
604 TRACE("(%p)->(%p)\n", This, p);
606 if(This->nsevent) {
607 nsIDOMMouseEvent *mouse_event;
608 nsresult nsres;
610 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
611 if(NS_SUCCEEDED(nsres)) {
612 nsIDOMMouseEvent_GetClientX(mouse_event, &x);
613 nsIDOMMouseEvent_Release(mouse_event);
617 *p = x;
618 return S_OK;
621 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
623 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
624 PRInt32 y = 0;
626 TRACE("(%p)->(%p)\n", This, p);
628 if(This->nsevent) {
629 nsIDOMMouseEvent *mouse_event;
630 nsresult nsres;
632 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
633 if(NS_SUCCEEDED(nsres)) {
634 nsIDOMMouseEvent_GetClientY(mouse_event, &y);
635 nsIDOMMouseEvent_Release(mouse_event);
639 *p = y;
640 return S_OK;
643 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
645 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
647 FIXME("(%p)->(%p)\n", This, p);
649 *p = 0;
650 return S_OK;
653 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
655 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
657 FIXME("(%p)->(%p)\n", This, p);
659 *p = 0;
660 return S_OK;
663 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
665 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
666 PRInt32 x = 0;
668 TRACE("(%p)->(%p)\n", This, p);
670 if(This->nsevent) {
671 nsIDOMMouseEvent *mouse_event;
672 nsresult nsres;
674 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
675 if(NS_SUCCEEDED(nsres)) {
676 nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
677 nsIDOMMouseEvent_Release(mouse_event);
681 *p = x;
682 return S_OK;
685 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
687 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
688 PRInt32 y = 0;
690 TRACE("(%p)->(%p)\n", This, p);
692 if(This->nsevent) {
693 nsIDOMMouseEvent *mouse_event;
694 nsresult nsres;
696 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
697 if(NS_SUCCEEDED(nsres)) {
698 nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
699 nsIDOMMouseEvent_Release(mouse_event);
703 *p = y;
704 return S_OK;
707 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
709 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
711 FIXME("(%p)->(%p)\n", This, p);
713 *p = NULL;
714 return S_OK;
717 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
718 HTMLEventObj_QueryInterface,
719 HTMLEventObj_AddRef,
720 HTMLEventObj_Release,
721 HTMLEventObj_GetTypeInfoCount,
722 HTMLEventObj_GetTypeInfo,
723 HTMLEventObj_GetIDsOfNames,
724 HTMLEventObj_Invoke,
725 HTMLEventObj_get_srcElement,
726 HTMLEventObj_get_altKey,
727 HTMLEventObj_get_ctrlKey,
728 HTMLEventObj_get_shiftKey,
729 HTMLEventObj_put_returnValue,
730 HTMLEventObj_get_returnValue,
731 HTMLEventObj_put_cancelBubble,
732 HTMLEventObj_get_cancelBubble,
733 HTMLEventObj_get_fromElement,
734 HTMLEventObj_get_toElement,
735 HTMLEventObj_put_keyCode,
736 HTMLEventObj_get_keyCode,
737 HTMLEventObj_get_button,
738 HTMLEventObj_get_type,
739 HTMLEventObj_get_qualifier,
740 HTMLEventObj_get_reason,
741 HTMLEventObj_get_x,
742 HTMLEventObj_get_y,
743 HTMLEventObj_get_clientX,
744 HTMLEventObj_get_clientY,
745 HTMLEventObj_get_offsetX,
746 HTMLEventObj_get_offsetY,
747 HTMLEventObj_get_screenX,
748 HTMLEventObj_get_screenY,
749 HTMLEventObj_get_srcFilter
752 static const tid_t HTMLEventObj_iface_tids[] = {
753 IHTMLEventObj_tid,
757 static dispex_static_data_t HTMLEventObj_dispex = {
758 NULL,
759 DispCEventObj_tid,
760 NULL,
761 HTMLEventObj_iface_tids
764 static HTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
766 HTMLEventObj *ret;
768 ret = heap_alloc_zero(sizeof(*ret));
769 if(!ret)
770 return NULL;
772 ret->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
773 ret->ref = 1;
774 ret->type = event_info+eid;
776 ret->nsevent = nsevent;
777 if(nsevent) {
778 nsIDOMEvent_AddRef(nsevent);
779 }else if(event_types[event_info[eid].type]) {
780 nsAString type_str;
781 nsresult nsres;
783 nsAString_InitDepend(&type_str, event_types[event_info[eid].type]);
784 nsres = nsIDOMHTMLDocument_CreateEvent(target->doc->nsdoc, &type_str, &ret->nsevent);
785 nsAString_Finish(&type_str);
786 if(NS_FAILED(nsres)) {
787 ERR("Could not create event: %08x\n", nsres);
788 IHTMLEventObj_Release(&ret->IHTMLEventObj_iface);
789 return NULL;
793 ret->target = target;
794 IHTMLDOMNode_AddRef(&target->IHTMLDOMNode_iface);
796 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLEventObj_iface, &HTMLEventObj_dispex);
798 return ret;
801 static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
803 IDispatchEx *dispex;
804 EXCEPINFO ei;
805 HRESULT hres;
807 memset(&ei, 0, sizeof(ei));
809 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
810 if(SUCCEEDED(hres)) {
811 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, retv, &ei, NULL);
812 IDispatchEx_Release(dispex);
813 }else {
814 TRACE("Could not get IDispatchEx interface: %08x\n", hres);
815 hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
816 dp, retv, &ei, NULL);
819 return hres;
822 static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, VARIANT *retv)
824 DISPPARAMS dp = {NULL,NULL,0,0};
825 ULONG argerr;
826 EXCEPINFO ei;
828 memset(&ei, 0, sizeof(ei));
829 return IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, retv, &ei, &argerr);
832 static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
834 int min, max, i;
835 HRESULT hres;
837 if(!data)
838 return FALSE;
840 if(!data->ids) {
841 hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
842 if(FAILED(hres))
843 return FALSE;
846 min = 0;
847 max = data->id_cnt-1;
848 while(min <= max) {
849 i = (min+max)/2;
850 if(data->ids[i] == dispid)
851 return TRUE;
853 if(data->ids[i] < dispid)
854 min = i+1;
855 else
856 max = i-1;
859 return FALSE;
862 static void call_event_handlers(HTMLDocumentNode *doc, HTMLEventObj *event_obj, event_target_t *event_target,
863 ConnectionPointContainer *cp_container, eventid_t eid, IDispatch *this_obj)
865 const BOOL cancelable = event_info[eid].flags & EVENT_CANCELABLE;
866 handler_vector_t *handler_vector = NULL;
867 VARIANT v;
868 int i;
869 HRESULT hres;
871 if(event_target)
872 handler_vector = event_target->event_table[eid];
874 if(handler_vector && handler_vector->handler_prop) {
875 DISPID named_arg = DISPID_THIS;
876 VARIANTARG arg;
877 DISPPARAMS dp = {&arg, &named_arg, 1, 1};
879 V_VT(&arg) = VT_DISPATCH;
880 V_DISPATCH(&arg) = this_obj;
881 V_VT(&v) = VT_EMPTY;
883 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
884 hres = call_disp_func(handler_vector->handler_prop, &dp, &v);
885 if(hres == S_OK) {
886 TRACE("%s <<< %s\n", debugstr_w(event_info[eid].name), debugstr_variant(&v));
888 if(cancelable) {
889 if(V_VT(&v) == VT_BOOL) {
890 if(!V_BOOL(&v))
891 event_obj->prevent_default = TRUE;
892 }else if(V_VT(&v) != VT_EMPTY) {
893 FIXME("unhandled result %s\n", debugstr_variant(&v));
896 VariantClear(&v);
897 }else {
898 WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
902 if(handler_vector && handler_vector->handler_cnt) {
903 VARIANTARG arg;
904 DISPPARAMS dp = {&arg, NULL, 1, 0};
906 V_VT(&arg) = VT_DISPATCH;
907 V_DISPATCH(&arg) = (IDispatch*)event_obj;
909 i = handler_vector->handler_cnt;
910 while(i--) {
911 if(handler_vector->handlers[i]) {
912 V_VT(&v) = VT_EMPTY;
914 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
915 hres = call_disp_func(handler_vector->handlers[i], &dp, &v);
916 if(hres == S_OK) {
917 TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
919 if(cancelable) {
920 if(V_VT(&v) == VT_BOOL) {
921 if(!V_BOOL(&v))
922 event_obj->prevent_default = TRUE;
923 }else if(V_VT(&v) != VT_EMPTY) {
924 FIXME("unhandled result %s\n", debugstr_variant(&v));
927 VariantClear(&v);
928 }else {
929 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
935 if(cp_container) {
936 ConnectionPoint *cp;
938 if(cp_container->forward_container)
939 cp_container = cp_container->forward_container;
941 for(cp = cp_container->cp_list; cp; cp = cp->next) {
942 if(cp->sinks_size && is_cp_event(cp->data, event_info[eid].dispid)) {
943 for(i=0; i < cp->sinks_size; i++) {
944 if(!cp->sinks[i].disp)
945 continue;
947 V_VT(&v) = VT_EMPTY;
949 TRACE("cp %s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
950 hres = call_cp_func(cp->sinks[i].disp, event_info[eid].dispid, &v);
951 if(hres == S_OK) {
952 TRACE("cp %s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
954 if(cancelable) {
955 if(V_VT(&v) == VT_BOOL) {
956 if(!V_BOOL(&v))
957 event_obj->prevent_default = TRUE;
958 }else if(V_VT(&v) != VT_EMPTY) {
959 FIXME("unhandled result %s\n", debugstr_variant(&v));
962 VariantClear(&v);
963 }else {
964 WARN("cp %s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
972 void fire_event(HTMLDocumentNode *doc, eventid_t eid, BOOL set_event, nsIDOMNode *target, nsIDOMEvent *nsevent)
974 HTMLEventObj *event_obj = NULL;
975 IHTMLEventObj *prev_event;
976 nsIDOMNode *parent, *nsnode;
977 BOOL prevent_default = FALSE;
978 HTMLDOMNode *node;
979 PRUint16 node_type;
980 nsresult nsres;
981 HRESULT hres;
983 TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
985 prev_event = doc->basedoc.window->event;
986 if(set_event) {
987 hres = get_node(doc, target, TRUE, &node);
988 if(FAILED(hres))
989 return;
991 event_obj = create_event(node, eid, nsevent);
992 doc->basedoc.window->event = &event_obj->IHTMLEventObj_iface;
993 }else {
994 doc->basedoc.window->event = NULL;
997 nsIDOMNode_GetNodeType(target, &node_type);
998 nsnode = target;
999 nsIDOMNode_AddRef(nsnode);
1001 switch(node_type) {
1002 case ELEMENT_NODE:
1003 do {
1004 hres = get_node(doc, nsnode, FALSE, &node);
1005 if(SUCCEEDED(hres) && node)
1006 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1007 node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1009 if(!(event_info[eid].flags & EVENT_BUBBLE))
1010 break;
1012 nsIDOMNode_GetParentNode(nsnode, &parent);
1013 nsIDOMNode_Release(nsnode);
1014 nsnode = parent;
1015 if(!nsnode)
1016 break;
1018 nsIDOMNode_GetNodeType(nsnode, &node_type);
1019 }while(node_type == ELEMENT_NODE);
1021 if(!(event_info[eid].flags & EVENT_BUBBLE))
1022 break;
1024 case DOCUMENT_NODE:
1025 if(event_info[eid].flags & EVENT_FORWARDBODY) {
1026 nsIDOMHTMLElement *nsbody;
1027 nsresult nsres;
1029 nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
1030 if(NS_SUCCEEDED(nsres) && nsbody) {
1031 hres = get_node(doc, (nsIDOMNode*)nsbody, FALSE, &node);
1032 if(SUCCEEDED(hres) && node)
1033 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1034 node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1035 nsIDOMHTMLElement_Release(nsbody);
1036 }else {
1037 ERR("Could not get body: %08x\n", nsres);
1041 call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
1042 (IDispatch*)&doc->basedoc.IHTMLDocument2_iface);
1043 break;
1045 default:
1046 FIXME("unimplemented node type %d\n", node_type);
1049 if(nsnode)
1050 nsIDOMNode_Release(nsnode);
1052 if(event_obj && event_obj->prevent_default)
1053 prevent_default = TRUE;
1054 doc->basedoc.window->event = prev_event;
1055 if(event_obj)
1056 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1058 if(!prevent_default && (event_info[eid].flags & EVENT_HASDEFAULTHANDLERS)) {
1059 nsIDOMNode_AddRef(target);
1060 nsnode = target;
1062 do {
1063 hres = get_node(doc, nsnode, TRUE, &node);
1064 if(FAILED(hres))
1065 break;
1067 if(node && node->vtbl->handle_event) {
1068 hres = node->vtbl->handle_event(node, eid, &prevent_default);
1069 if(FAILED(hres) || prevent_default)
1070 break;
1073 nsres = nsIDOMNode_GetParentNode(nsnode, &parent);
1074 if(NS_FAILED(nsres))
1075 break;
1077 nsIDOMNode_Release(nsnode);
1078 nsnode = parent;
1079 } while(nsnode);
1081 if(nsnode)
1082 nsIDOMNode_Release(nsnode);
1085 if(prevent_default && nsevent) {
1086 TRACE("calling PreventDefault\n");
1087 nsIDOMEvent_PreventDefault(nsevent);
1091 HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_obj, VARIANT_BOOL *cancelled)
1093 eventid_t eid;
1095 eid = attr_to_eid(event_name);
1096 if(eid == EVENTID_LAST) {
1097 WARN("unknown event %s\n", debugstr_w(event_name));
1098 return E_INVALIDARG;
1101 if(event_obj && V_VT(event_obj) != VT_EMPTY && V_VT(event_obj) != VT_ERROR)
1102 FIXME("event_obj not implemented\n");
1104 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
1105 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1106 return E_NOTIMPL;
1109 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1111 *cancelled = VARIANT_TRUE; /* FIXME */
1112 return S_OK;
1115 HRESULT call_fire_event(HTMLDOMNode *node, eventid_t eid)
1117 HRESULT hres;
1119 if(node->vtbl->fire_event) {
1120 BOOL handled = FALSE;
1122 hres = node->vtbl->fire_event(node, eid, &handled);
1123 if(handled)
1124 return hres;
1127 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1128 return S_OK;
1131 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
1133 if(!*event_target_ptr)
1134 *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
1135 return *event_target_ptr;
1138 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
1140 handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
1142 if(handler_vector) {
1143 if(cnt <= handler_vector->handler_cnt)
1144 return TRUE;
1146 new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1147 }else {
1148 new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1151 if(!new_vector)
1152 return FALSE;
1154 new_vector->handler_cnt = cnt;
1155 event_target->event_table[eid] = new_vector;
1156 return TRUE;
1159 static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1161 if(!doc->nsdoc)
1162 return S_OK;
1164 if(event_info[eid].flags & EVENT_NODEHANDLER) {
1165 DWORD mask;
1167 mask = get_node_handler_mask(eid);
1168 if(event_target->node_handlers_mask & mask)
1169 return S_OK;
1171 add_nsevent_listener(doc, nsnode, event_info[eid].name);
1172 event_target->node_handlers_mask |= mask;
1173 return S_OK;
1176 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER))
1177 return S_OK;
1179 if(!doc->event_vector[eid]) {
1180 doc->event_vector[eid] = TRUE;
1181 add_nsevent_listener(doc, NULL, event_info[eid].name);
1184 return S_OK;
1187 static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
1189 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1190 IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
1191 (*event_target)->event_table[eid]->handler_prop = NULL;
1194 return S_OK;
1197 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocumentNode *doc,
1198 eventid_t eid, IDispatch *disp)
1200 event_target_t *event_target;
1202 if(!disp)
1203 return remove_event_handler(event_target_ptr, eid);
1205 event_target = get_event_target(event_target_ptr);
1206 if(!event_target)
1207 return E_OUTOFMEMORY;
1209 if(!alloc_handler_vector(event_target, eid, 0))
1210 return E_OUTOFMEMORY;
1212 if(event_target->event_table[eid]->handler_prop)
1213 IDispatch_Release(event_target->event_table[eid]->handler_prop);
1215 event_target->event_table[eid]->handler_prop = disp;
1216 IDispatch_AddRef(disp);
1218 return ensure_nsevent_handler(doc, event_target, nsnode, eid);
1221 HRESULT set_event_handler(event_target_t **event_target, nsIDOMNode *nsnode, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1223 switch(V_VT(var)) {
1224 case VT_NULL:
1225 return remove_event_handler(event_target, eid);
1227 case VT_DISPATCH:
1228 return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1230 default:
1231 FIXME("not supported vt=%d\n", V_VT(var));
1232 /* fall through */
1233 case VT_EMPTY:
1234 return E_NOTIMPL;
1237 return S_OK;
1240 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
1242 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1243 V_VT(var) = VT_DISPATCH;
1244 V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1245 IDispatch_AddRef(V_DISPATCH(var));
1246 }else {
1247 V_VT(var) = VT_NULL;
1250 return S_OK;
1253 HRESULT attach_event(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocument *doc, BSTR name,
1254 IDispatch *disp, VARIANT_BOOL *res)
1256 event_target_t *event_target;
1257 eventid_t eid;
1258 DWORD i = 0;
1260 eid = attr_to_eid(name);
1261 if(eid == EVENTID_LAST) {
1262 WARN("Unknown event\n");
1263 *res = VARIANT_TRUE;
1264 return S_OK;
1267 event_target = get_event_target(event_target_ptr);
1268 if(!event_target)
1269 return E_OUTOFMEMORY;
1271 if(event_target->event_table[eid]) {
1272 while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
1273 i++;
1274 if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
1275 return E_OUTOFMEMORY;
1276 }else if(!alloc_handler_vector(event_target, eid, i+1)) {
1277 return E_OUTOFMEMORY;
1280 IDispatch_AddRef(disp);
1281 event_target->event_table[eid]->handlers[i] = disp;
1283 *res = VARIANT_TRUE;
1284 return ensure_nsevent_handler(doc->doc_node, event_target, nsnode, eid);
1287 HRESULT detach_event(event_target_t *event_target, HTMLDocument *doc, BSTR name, IDispatch *disp)
1289 eventid_t eid;
1290 DWORD i = 0;
1292 if(!event_target)
1293 return S_OK;
1295 eid = attr_to_eid(name);
1296 if(eid == EVENTID_LAST) {
1297 WARN("Unknown event\n");
1298 return S_OK;
1301 if(!event_target->event_table[eid])
1302 return S_OK;
1304 while(i < event_target->event_table[eid]->handler_cnt) {
1305 if(event_target->event_table[eid]->handlers[i] == disp) {
1306 IDispatch_Release(event_target->event_table[eid]->handlers[i]);
1307 event_target->event_table[eid]->handlers[i] = NULL;
1309 i++;
1312 return S_OK;
1315 void update_cp_events(HTMLWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp, nsIDOMNode *nsnode)
1317 event_target_t *event_target;
1318 int i;
1320 event_target = get_event_target(event_target_ptr);
1321 if(!event_target)
1322 return; /* FIXME */
1324 for(i=0; i < EVENTID_LAST; i++) {
1325 if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1326 ensure_nsevent_handler(window->doc, event_target, nsnode, i);
1330 void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
1332 const PRUnichar *attr_value;
1333 nsAString attr_name_str, attr_value_str;
1334 IDispatch *disp;
1335 HTMLDOMNode *node;
1336 int i;
1337 nsresult nsres;
1338 HRESULT hres;
1340 nsAString_Init(&attr_value_str, NULL);
1341 nsAString_Init(&attr_name_str, NULL);
1343 for(i=0; i < EVENTID_LAST; i++) {
1344 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
1345 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
1346 if(NS_SUCCEEDED(nsres)) {
1347 nsAString_GetData(&attr_value_str, &attr_value);
1348 if(!*attr_value)
1349 continue;
1351 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
1353 disp = script_parse_event(doc->basedoc.window, attr_value);
1354 if(disp) {
1355 hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1356 if(SUCCEEDED(hres))
1357 set_event_handler_disp(get_node_event_target(node), node->nsnode, node->doc, i, disp);
1358 IDispatch_Release(disp);
1363 nsAString_Finish(&attr_value_str);
1364 nsAString_Finish(&attr_name_str);
1367 HRESULT doc_init_events(HTMLDocumentNode *doc)
1369 unsigned i;
1370 HRESULT hres;
1372 doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
1373 if(!doc->event_vector)
1374 return E_OUTOFMEMORY;
1376 init_nsevents(doc);
1378 for(i=0; i < EVENTID_LAST; i++) {
1379 if(event_info[i].flags & EVENT_HASDEFAULTHANDLERS) {
1380 hres = ensure_nsevent_handler(doc, NULL, NULL, i);
1381 if(FAILED(hres))
1382 return hres;
1386 return S_OK;
1389 void release_event_target(event_target_t *event_target)
1391 int i, j;
1393 for(i=0; i < EVENTID_LAST; i++) {
1394 if(event_target->event_table[i]) {
1395 if(event_target->event_table[i]->handler_prop)
1396 IDispatch_Release(event_target->event_table[i]->handler_prop);
1397 for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
1398 if(event_target->event_table[i]->handlers[j])
1399 IDispatch_Release(event_target->event_table[i]->handlers[j]);
1403 heap_free(event_target);