msvcrt: ungetc must create a buffer, even if _IONBF has been set with setvbuf.
[wine/multimedia.git] / dlls / mshtml / htmlevent.c
blobd0e8814e87a18d4be693556e7c885f2b1b7643b5
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->target)
293 IHTMLDOMNode_Release(&This->target->IHTMLDOMNode_iface);
294 if(This->nsevent)
295 nsIDOMEvent_Release(This->nsevent);
296 release_dispex(&This->dispex);
297 heap_free(This);
300 return ref;
303 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
305 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
306 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
309 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
310 LCID lcid, ITypeInfo **ppTInfo)
312 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
313 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
316 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
317 LPOLESTR *rgszNames, UINT cNames,
318 LCID lcid, DISPID *rgDispId)
320 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
321 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
322 lcid, rgDispId);
325 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
326 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
327 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
329 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
330 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
331 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
334 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
336 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
338 TRACE("(%p)->(%p)\n", This, p);
340 return IHTMLDOMNode_QueryInterface(&This->target->IHTMLDOMNode_iface, &IID_IHTMLElement,
341 (void**)p);
344 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
346 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
347 PRBool ret = FALSE;
349 TRACE("(%p)->(%p)\n", This, p);
351 if(This->nsevent) {
352 nsIDOMKeyEvent *key_event;
353 nsresult nsres;
355 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
356 if(NS_SUCCEEDED(nsres)) {
357 nsIDOMKeyEvent_GetAltKey(key_event, &ret);
358 nsIDOMKeyEvent_Release(key_event);
359 }else {
360 nsIDOMMouseEvent *mouse_event;
362 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
363 if(NS_SUCCEEDED(nsres)) {
364 nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
365 nsIDOMMouseEvent_Release(mouse_event);
370 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
371 return S_OK;
374 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
376 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
377 PRBool ret = FALSE;
379 TRACE("(%p)->(%p)\n", This, p);
381 if(This->nsevent) {
382 nsIDOMKeyEvent *key_event;
383 nsresult nsres;
385 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
386 if(NS_SUCCEEDED(nsres)) {
387 nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
388 nsIDOMKeyEvent_Release(key_event);
389 }else {
390 nsIDOMMouseEvent *mouse_event;
392 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
393 if(NS_SUCCEEDED(nsres)) {
394 nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
395 nsIDOMMouseEvent_Release(mouse_event);
400 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
401 return S_OK;
404 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
406 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
407 PRBool ret = FALSE;
409 TRACE("(%p)->(%p)\n", This, p);
411 if(This->nsevent) {
412 nsIDOMKeyEvent *key_event;
413 nsresult nsres;
415 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
416 if(NS_SUCCEEDED(nsres)) {
417 nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
418 nsIDOMKeyEvent_Release(key_event);
419 }else {
420 nsIDOMMouseEvent *mouse_event;
422 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
423 if(NS_SUCCEEDED(nsres)) {
424 nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
425 nsIDOMMouseEvent_Release(mouse_event);
430 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
431 return S_OK;
434 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
436 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
437 FIXME("(%p)->()\n", This);
438 return E_NOTIMPL;
441 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
443 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
445 FIXME("(%p)->(%p)\n", This, p);
447 V_VT(p) = VT_EMPTY;
448 return S_OK;
451 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
453 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
454 FIXME("(%p)->(%x)\n", This, v);
455 return E_NOTIMPL;
458 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
460 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
462 FIXME("(%p)->(%p)\n", This, p);
464 *p = VARIANT_FALSE;
465 return S_OK;
468 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
470 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
472 FIXME("(%p)->(%p)\n", This, p);
474 *p = NULL;
475 return S_OK;
478 static HRESULT WINAPI HTMLEventObj_get_toElement(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_put_keyCode(IHTMLEventObj *iface, LONG v)
490 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
491 FIXME("(%p)->(%d)\n", This, v);
492 return E_NOTIMPL;
495 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
497 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
498 PRUint32 key_code = 0;
500 TRACE("(%p)->(%p)\n", This, p);
502 if(This->nsevent) {
503 nsIDOMKeyEvent *key_event;
504 nsresult nsres;
506 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
507 if(NS_SUCCEEDED(nsres)) {
508 nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
509 nsIDOMKeyEvent_Release(key_event);
513 *p = key_code;
514 return S_OK;
517 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
519 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
520 PRUint16 button = 0;
522 TRACE("(%p)->(%p)\n", This, p);
524 if(This->nsevent) {
525 nsIDOMMouseEvent *mouse_event;
526 nsresult nsres;
528 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
529 if(NS_SUCCEEDED(nsres)) {
530 nsIDOMMouseEvent_GetButton(mouse_event, &button);
531 nsIDOMMouseEvent_Release(mouse_event);
535 *p = button;
536 return S_OK;
539 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
541 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
543 TRACE("(%p)->(%p)\n", This, p);
545 *p = SysAllocString(This->type->name);
546 return *p ? S_OK : E_OUTOFMEMORY;
549 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
551 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
553 FIXME("(%p)->(%p)\n", This, p);
555 *p = NULL;
556 return S_OK;
559 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
561 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
563 FIXME("(%p)->(%p)\n", This, p);
565 *p = 0;
566 return S_OK;
569 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
571 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
573 FIXME("(%p)->(%p)\n", This, p);
575 *p = -1;
576 return S_OK;
579 static HRESULT WINAPI HTMLEventObj_get_y(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_clientX(IHTMLEventObj *iface, LONG *p)
591 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
592 PRInt32 x = 0;
594 TRACE("(%p)->(%p)\n", This, p);
596 if(This->nsevent) {
597 nsIDOMMouseEvent *mouse_event;
598 nsresult nsres;
600 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
601 if(NS_SUCCEEDED(nsres)) {
602 nsIDOMMouseEvent_GetClientX(mouse_event, &x);
603 nsIDOMMouseEvent_Release(mouse_event);
607 *p = x;
608 return S_OK;
611 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
613 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
614 PRInt32 y = 0;
616 TRACE("(%p)->(%p)\n", This, p);
618 if(This->nsevent) {
619 nsIDOMMouseEvent *mouse_event;
620 nsresult nsres;
622 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
623 if(NS_SUCCEEDED(nsres)) {
624 nsIDOMMouseEvent_GetClientY(mouse_event, &y);
625 nsIDOMMouseEvent_Release(mouse_event);
629 *p = y;
630 return S_OK;
633 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
635 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
637 FIXME("(%p)->(%p)\n", This, p);
639 *p = 0;
640 return S_OK;
643 static HRESULT WINAPI HTMLEventObj_get_offsetY(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_screenX(IHTMLEventObj *iface, LONG *p)
655 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
656 PRInt32 x = 0;
658 TRACE("(%p)->(%p)\n", This, p);
660 if(This->nsevent) {
661 nsIDOMMouseEvent *mouse_event;
662 nsresult nsres;
664 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
665 if(NS_SUCCEEDED(nsres)) {
666 nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
667 nsIDOMMouseEvent_Release(mouse_event);
671 *p = x;
672 return S_OK;
675 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
677 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
678 PRInt32 y = 0;
680 TRACE("(%p)->(%p)\n", This, p);
682 if(This->nsevent) {
683 nsIDOMMouseEvent *mouse_event;
684 nsresult nsres;
686 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
687 if(NS_SUCCEEDED(nsres)) {
688 nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
689 nsIDOMMouseEvent_Release(mouse_event);
693 *p = y;
694 return S_OK;
697 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
699 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
701 FIXME("(%p)->(%p)\n", This, p);
703 *p = NULL;
704 return S_OK;
707 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
708 HTMLEventObj_QueryInterface,
709 HTMLEventObj_AddRef,
710 HTMLEventObj_Release,
711 HTMLEventObj_GetTypeInfoCount,
712 HTMLEventObj_GetTypeInfo,
713 HTMLEventObj_GetIDsOfNames,
714 HTMLEventObj_Invoke,
715 HTMLEventObj_get_srcElement,
716 HTMLEventObj_get_altKey,
717 HTMLEventObj_get_ctrlKey,
718 HTMLEventObj_get_shiftKey,
719 HTMLEventObj_put_returnValue,
720 HTMLEventObj_get_returnValue,
721 HTMLEventObj_put_cancelBubble,
722 HTMLEventObj_get_cancelBubble,
723 HTMLEventObj_get_fromElement,
724 HTMLEventObj_get_toElement,
725 HTMLEventObj_put_keyCode,
726 HTMLEventObj_get_keyCode,
727 HTMLEventObj_get_button,
728 HTMLEventObj_get_type,
729 HTMLEventObj_get_qualifier,
730 HTMLEventObj_get_reason,
731 HTMLEventObj_get_x,
732 HTMLEventObj_get_y,
733 HTMLEventObj_get_clientX,
734 HTMLEventObj_get_clientY,
735 HTMLEventObj_get_offsetX,
736 HTMLEventObj_get_offsetY,
737 HTMLEventObj_get_screenX,
738 HTMLEventObj_get_screenY,
739 HTMLEventObj_get_srcFilter
742 static const tid_t HTMLEventObj_iface_tids[] = {
743 IHTMLEventObj_tid,
747 static dispex_static_data_t HTMLEventObj_dispex = {
748 NULL,
749 DispCEventObj_tid,
750 NULL,
751 HTMLEventObj_iface_tids
754 static HTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
756 HTMLEventObj *ret;
758 ret = heap_alloc_zero(sizeof(*ret));
759 if(!ret)
760 return NULL;
762 ret->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
763 ret->ref = 1;
764 ret->type = event_info+eid;
766 ret->nsevent = nsevent;
767 if(nsevent) {
768 nsIDOMEvent_AddRef(nsevent);
769 }else if(event_types[event_info[eid].type]) {
770 nsIDOMDocumentEvent *doc_event;
771 nsresult nsres;
773 nsres = nsIDOMHTMLDocument_QueryInterface(target->doc->nsdoc, &IID_nsIDOMDocumentEvent,
774 (void**)&doc_event);
775 if(NS_SUCCEEDED(nsres)) {
776 nsAString type_str;
778 nsAString_InitDepend(&type_str, event_types[event_info[eid].type]);
779 nsres = nsIDOMDocumentEvent_CreateEvent(doc_event, &type_str, &ret->nsevent);
780 nsAString_Finish(&type_str);
781 nsIDOMDocumentEvent_Release(doc_event);
783 if(NS_FAILED(nsres)) {
784 ERR("Could not create event: %08x\n", nsres);
785 IHTMLEventObj_Release(&ret->IHTMLEventObj_iface);
786 return NULL;
790 ret->target = target;
791 IHTMLDOMNode_AddRef(&target->IHTMLDOMNode_iface);
793 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLEventObj_iface, &HTMLEventObj_dispex);
795 return ret;
798 static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
800 IDispatchEx *dispex;
801 EXCEPINFO ei;
802 HRESULT hres;
804 memset(&ei, 0, sizeof(ei));
806 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
807 if(SUCCEEDED(hres)) {
808 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, retv, &ei, NULL);
809 IDispatchEx_Release(dispex);
810 }else {
811 TRACE("Could not get IDispatchEx interface: %08x\n", hres);
812 hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
813 dp, retv, &ei, NULL);
816 return hres;
819 static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, VARIANT *retv)
821 DISPPARAMS dp = {NULL,NULL,0,0};
822 ULONG argerr;
823 EXCEPINFO ei;
825 memset(&ei, 0, sizeof(ei));
826 return IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, retv, &ei, &argerr);
829 static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
831 int min, max, i;
832 HRESULT hres;
834 if(!data)
835 return FALSE;
837 if(!data->ids) {
838 hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
839 if(FAILED(hres))
840 return FALSE;
843 min = 0;
844 max = data->id_cnt-1;
845 while(min <= max) {
846 i = (min+max)/2;
847 if(data->ids[i] == dispid)
848 return TRUE;
850 if(data->ids[i] < dispid)
851 min = i+1;
852 else
853 max = i-1;
856 return FALSE;
859 static void call_event_handlers(HTMLDocumentNode *doc, HTMLEventObj *event_obj, event_target_t *event_target,
860 ConnectionPointContainer *cp_container, eventid_t eid, IDispatch *this_obj)
862 const BOOL cancelable = event_info[eid].flags & EVENT_CANCELABLE;
863 handler_vector_t *handler_vector = NULL;
864 VARIANT v;
865 int i;
866 HRESULT hres;
868 if(event_target)
869 handler_vector = event_target->event_table[eid];
871 if(handler_vector && handler_vector->handler_prop) {
872 DISPID named_arg = DISPID_THIS;
873 VARIANTARG arg;
874 DISPPARAMS dp = {&arg, &named_arg, 1, 1};
876 V_VT(&arg) = VT_DISPATCH;
877 V_DISPATCH(&arg) = this_obj;
878 V_VT(&v) = VT_EMPTY;
880 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
881 hres = call_disp_func(handler_vector->handler_prop, &dp, &v);
882 if(hres == S_OK) {
883 TRACE("%s <<< %s\n", debugstr_w(event_info[eid].name), debugstr_variant(&v));
885 if(cancelable) {
886 if(V_VT(&v) == VT_BOOL) {
887 if(!V_BOOL(&v))
888 event_obj->prevent_default = TRUE;
889 }else if(V_VT(&v) != VT_EMPTY) {
890 FIXME("unhandled result %s\n", debugstr_variant(&v));
893 VariantClear(&v);
894 }else {
895 WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
899 if(handler_vector && handler_vector->handler_cnt) {
900 VARIANTARG arg;
901 DISPPARAMS dp = {&arg, NULL, 1, 0};
903 V_VT(&arg) = VT_DISPATCH;
904 V_DISPATCH(&arg) = (IDispatch*)event_obj;
906 i = handler_vector->handler_cnt;
907 while(i--) {
908 if(handler_vector->handlers[i]) {
909 V_VT(&v) = VT_EMPTY;
911 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
912 hres = call_disp_func(handler_vector->handlers[i], &dp, &v);
913 if(hres == S_OK) {
914 TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
916 if(cancelable) {
917 if(V_VT(&v) == VT_BOOL) {
918 if(!V_BOOL(&v))
919 event_obj->prevent_default = TRUE;
920 }else if(V_VT(&v) != VT_EMPTY) {
921 FIXME("unhandled result %s\n", debugstr_variant(&v));
924 VariantClear(&v);
925 }else {
926 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
932 if(cp_container) {
933 ConnectionPoint *cp;
935 if(cp_container->forward_container)
936 cp_container = cp_container->forward_container;
938 for(cp = cp_container->cp_list; cp; cp = cp->next) {
939 if(cp->sinks_size && is_cp_event(cp->data, event_info[eid].dispid)) {
940 for(i=0; i < cp->sinks_size; i++) {
941 if(!cp->sinks[i].disp)
942 continue;
944 V_VT(&v) = VT_EMPTY;
946 TRACE("cp %s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
947 hres = call_cp_func(cp->sinks[i].disp, event_info[eid].dispid, &v);
948 if(hres == S_OK) {
949 TRACE("cp %s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
951 if(cancelable) {
952 if(V_VT(&v) == VT_BOOL) {
953 if(!V_BOOL(&v))
954 event_obj->prevent_default = TRUE;
955 }else if(V_VT(&v) != VT_EMPTY) {
956 FIXME("unhandled result %s\n", debugstr_variant(&v));
959 VariantClear(&v);
960 }else {
961 WARN("cp %s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
969 void fire_event(HTMLDocumentNode *doc, eventid_t eid, BOOL set_event, nsIDOMNode *target, nsIDOMEvent *nsevent)
971 HTMLEventObj *event_obj = NULL;
972 IHTMLEventObj *prev_event;
973 nsIDOMNode *parent, *nsnode;
974 HTMLDOMNode *node;
975 PRUint16 node_type;
976 HRESULT hres;
978 TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
980 prev_event = doc->basedoc.window->event;
981 if(set_event) {
982 hres = get_node(doc, target, TRUE, &node);
983 if(FAILED(hres))
984 return;
986 event_obj = create_event(node, eid, nsevent);
987 doc->basedoc.window->event = &event_obj->IHTMLEventObj_iface;
988 }else {
989 doc->basedoc.window->event = NULL;
992 nsIDOMNode_GetNodeType(target, &node_type);
993 nsnode = target;
994 nsIDOMNode_AddRef(nsnode);
996 switch(node_type) {
997 case ELEMENT_NODE:
998 do {
999 hres = get_node(doc, nsnode, FALSE, &node);
1000 if(SUCCEEDED(hres) && node)
1001 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1002 node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1004 if(!(event_info[eid].flags & EVENT_BUBBLE))
1005 break;
1007 nsIDOMNode_GetParentNode(nsnode, &parent);
1008 nsIDOMNode_Release(nsnode);
1009 nsnode = parent;
1010 if(!nsnode)
1011 break;
1013 nsIDOMNode_GetNodeType(nsnode, &node_type);
1014 }while(node_type == ELEMENT_NODE);
1016 if(!(event_info[eid].flags & EVENT_BUBBLE))
1017 break;
1019 case DOCUMENT_NODE:
1020 if(event_info[eid].flags & EVENT_FORWARDBODY) {
1021 nsIDOMHTMLElement *nsbody;
1022 nsresult nsres;
1024 nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
1025 if(NS_SUCCEEDED(nsres) && nsbody) {
1026 hres = get_node(doc, (nsIDOMNode*)nsbody, FALSE, &node);
1027 if(SUCCEEDED(hres) && node)
1028 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1029 node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1030 nsIDOMHTMLElement_Release(nsbody);
1031 }else {
1032 ERR("Could not get body: %08x\n", nsres);
1036 call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
1037 (IDispatch*)&doc->basedoc.IHTMLDocument2_iface);
1038 break;
1040 default:
1041 FIXME("unimplemented node type %d\n", node_type);
1044 if(nsnode)
1045 nsIDOMNode_Release(nsnode);
1047 doc->basedoc.window->event = prev_event;
1049 if(event_obj) {
1050 if(event_obj->prevent_default && nsevent) {
1051 TRACE("calling PreventDefault\n");
1052 nsIDOMEvent_PreventDefault(nsevent);
1054 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1058 HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_obj, VARIANT_BOOL *cancelled)
1060 eventid_t eid;
1062 eid = attr_to_eid(event_name);
1063 if(eid == EVENTID_LAST) {
1064 WARN("unknown event %s\n", debugstr_w(event_name));
1065 return E_INVALIDARG;
1068 if(event_obj && V_VT(event_obj) != VT_EMPTY && V_VT(event_obj) != VT_ERROR)
1069 FIXME("event_obj not implemented\n");
1071 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
1072 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1073 return E_NOTIMPL;
1076 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1078 *cancelled = VARIANT_TRUE; /* FIXME */
1079 return S_OK;
1082 HRESULT call_event(HTMLDOMNode *node, eventid_t eid)
1084 HRESULT hres;
1086 if(node->vtbl->call_event) {
1087 BOOL handled = FALSE;
1089 hres = node->vtbl->call_event(node, eid, &handled);
1090 if(handled)
1091 return hres;
1094 fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1095 return S_OK;
1098 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
1100 if(!*event_target_ptr)
1101 *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
1102 return *event_target_ptr;
1105 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
1107 handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
1109 if(handler_vector) {
1110 if(cnt <= handler_vector->handler_cnt)
1111 return TRUE;
1113 new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1114 }else {
1115 new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1118 if(!new_vector)
1119 return FALSE;
1121 new_vector->handler_cnt = cnt;
1122 event_target->event_table[eid] = new_vector;
1123 return TRUE;
1126 static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1128 if(!doc->nsdoc)
1129 return S_OK;
1131 if(event_info[eid].flags & EVENT_NODEHANDLER) {
1132 DWORD mask;
1134 mask = get_node_handler_mask(eid);
1135 if(event_target->node_handlers_mask & mask)
1136 return S_OK;
1138 add_nsevent_listener(doc, nsnode, event_info[eid].name);
1139 event_target->node_handlers_mask |= mask;
1140 return S_OK;
1143 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER))
1144 return S_OK;
1146 if(!doc->event_vector) {
1147 doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
1148 if(!doc->event_vector)
1149 return E_OUTOFMEMORY;
1152 if(!doc->event_vector[eid]) {
1153 doc->event_vector[eid] = TRUE;
1154 add_nsevent_listener(doc, NULL, event_info[eid].name);
1157 return S_OK;
1160 static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
1162 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1163 IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
1164 (*event_target)->event_table[eid]->handler_prop = NULL;
1167 return S_OK;
1170 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocumentNode *doc,
1171 eventid_t eid, IDispatch *disp)
1173 event_target_t *event_target;
1175 if(!disp)
1176 return remove_event_handler(event_target_ptr, eid);
1178 event_target = get_event_target(event_target_ptr);
1179 if(!event_target)
1180 return E_OUTOFMEMORY;
1182 if(!alloc_handler_vector(event_target, eid, 0))
1183 return E_OUTOFMEMORY;
1185 if(event_target->event_table[eid]->handler_prop)
1186 IDispatch_Release(event_target->event_table[eid]->handler_prop);
1188 event_target->event_table[eid]->handler_prop = disp;
1189 IDispatch_AddRef(disp);
1191 return ensure_nsevent_handler(doc, event_target, nsnode, eid);
1194 HRESULT set_event_handler(event_target_t **event_target, nsIDOMNode *nsnode, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1196 switch(V_VT(var)) {
1197 case VT_NULL:
1198 return remove_event_handler(event_target, eid);
1200 case VT_DISPATCH:
1201 return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1203 default:
1204 FIXME("not supported vt=%d\n", V_VT(var));
1205 case VT_EMPTY:
1206 return E_NOTIMPL;
1209 return S_OK;
1212 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
1214 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1215 V_VT(var) = VT_DISPATCH;
1216 V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1217 IDispatch_AddRef(V_DISPATCH(var));
1218 }else {
1219 V_VT(var) = VT_NULL;
1222 return S_OK;
1225 HRESULT attach_event(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocument *doc, BSTR name,
1226 IDispatch *disp, VARIANT_BOOL *res)
1228 event_target_t *event_target;
1229 eventid_t eid;
1230 DWORD i = 0;
1232 eid = attr_to_eid(name);
1233 if(eid == EVENTID_LAST) {
1234 WARN("Unknown event\n");
1235 *res = VARIANT_TRUE;
1236 return S_OK;
1239 event_target = get_event_target(event_target_ptr);
1240 if(!event_target)
1241 return E_OUTOFMEMORY;
1243 if(event_target->event_table[eid]) {
1244 while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
1245 i++;
1246 if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
1247 return E_OUTOFMEMORY;
1248 }else if(!alloc_handler_vector(event_target, eid, i+1)) {
1249 return E_OUTOFMEMORY;
1252 IDispatch_AddRef(disp);
1253 event_target->event_table[eid]->handlers[i] = disp;
1255 *res = VARIANT_TRUE;
1256 return ensure_nsevent_handler(doc->doc_node, event_target, nsnode, eid);
1259 HRESULT detach_event(event_target_t *event_target, HTMLDocument *doc, BSTR name, IDispatch *disp)
1261 eventid_t eid;
1262 DWORD i = 0;
1264 if(!event_target)
1265 return S_OK;
1267 eid = attr_to_eid(name);
1268 if(eid == EVENTID_LAST) {
1269 WARN("Unknown event\n");
1270 return S_OK;
1273 if(!event_target->event_table[eid])
1274 return S_OK;
1276 while(i < event_target->event_table[eid]->handler_cnt) {
1277 if(event_target->event_table[eid]->handlers[i] == disp) {
1278 IDispatch_Release(event_target->event_table[eid]->handlers[i]);
1279 event_target->event_table[eid]->handlers[i] = NULL;
1281 i++;
1284 return S_OK;
1287 void update_cp_events(HTMLWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp, nsIDOMNode *nsnode)
1289 event_target_t *event_target;
1290 int i;
1292 event_target = get_event_target(event_target_ptr);
1293 if(!event_target)
1294 return; /* FIXME */
1296 for(i=0; i < EVENTID_LAST; i++) {
1297 if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1298 ensure_nsevent_handler(window->doc, event_target, nsnode, i);
1302 void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
1304 const PRUnichar *attr_value;
1305 nsAString attr_name_str, attr_value_str;
1306 IDispatch *disp;
1307 HTMLDOMNode *node;
1308 int i;
1309 nsresult nsres;
1310 HRESULT hres;
1312 nsAString_Init(&attr_value_str, NULL);
1313 nsAString_Init(&attr_name_str, NULL);
1315 for(i=0; i < EVENTID_LAST; i++) {
1316 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
1317 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
1318 if(NS_SUCCEEDED(nsres)) {
1319 nsAString_GetData(&attr_value_str, &attr_value);
1320 if(!*attr_value)
1321 continue;
1323 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
1325 disp = script_parse_event(doc->basedoc.window, attr_value);
1326 if(disp) {
1327 hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1328 if(SUCCEEDED(hres))
1329 set_event_handler_disp(get_node_event_target(node), node->nsnode, node->doc, i, disp);
1330 IDispatch_Release(disp);
1335 nsAString_Finish(&attr_value_str);
1336 nsAString_Finish(&attr_name_str);
1339 void release_event_target(event_target_t *event_target)
1341 int i, j;
1343 for(i=0; i < EVENTID_LAST; i++) {
1344 if(event_target->event_table[i]) {
1345 if(event_target->event_table[i]->handler_prop)
1346 IDispatch_Release(event_target->event_table[i]->handler_prop);
1347 for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
1348 if(event_target->event_table[i]->handlers[j])
1349 IDispatch_Release(event_target->event_table[i]->handlers[j]);
1353 heap_free(event_target);