makefiles: Add makedep pragmas for typelib idl files.
[wine.git] / dlls / mshtml / htmlevent.c
bloba564907d774be2c4c09b80066df335bfc4837ed3
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>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
28 #include "mshtmdid.h"
30 #include "mshtml_private.h"
31 #include "htmlevent.h"
32 #include "htmlscript.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 typedef struct {
39 IDispatch *handler_prop;
40 DWORD handler_cnt;
41 IDispatch *handlers[0];
42 } handler_vector_t;
44 struct event_target_t {
45 DWORD node_handlers_mask;
46 handler_vector_t *event_table[EVENTID_LAST];
49 static const WCHAR abortW[] = {'a','b','o','r','t',0};
50 static const WCHAR onabortW[] = {'o','n','a','b','o','r','t',0};
52 static const WCHAR beforeunloadW[] = {'b','e','f','o','r','e','u','n','l','o','a','d',0};
53 static const WCHAR onbeforeunloadW[] = {'o','n','b','e','f','o','r','e','u','n','l','o','a','d',0};
55 static const WCHAR blurW[] = {'b','l','u','r',0};
56 static const WCHAR onblurW[] = {'o','n','b','l','u','r',0};
58 static const WCHAR changeW[] = {'c','h','a','n','g','e',0};
59 static const WCHAR onchangeW[] = {'o','n','c','h','a','n','g','e',0};
61 static const WCHAR clickW[] = {'c','l','i','c','k',0};
62 static const WCHAR onclickW[] = {'o','n','c','l','i','c','k',0};
64 static const WCHAR contextmenuW[] = {'c','o','n','t','e','x','t','m','e','n','u',0};
65 static const WCHAR oncontextmenuW[] = {'o','n','c','o','n','t','e','x','t','m','e','n','u',0};
67 static const WCHAR dataavailableW[] = {'d','a','t','a','a','v','a','i','l','a','b','l','e',0};
68 static const WCHAR ondataavailableW[] = {'o','n','d','a','t','a','a','v','a','i','l','a','b','l','e',0};
70 static const WCHAR dblclickW[] = {'d','b','l','c','l','i','c','k',0};
71 static const WCHAR ondblclickW[] = {'o','n','d','b','l','c','l','i','c','k',0};
73 static const WCHAR dragW[] = {'d','r','a','g',0};
74 static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};
76 static const WCHAR dragstartW[] = {'d','r','a','g','s','t','a','r','t',0};
77 static const WCHAR ondragstartW[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
79 static const WCHAR errorW[] = {'e','r','r','o','r',0};
80 static const WCHAR onerrorW[] = {'o','n','e','r','r','o','r',0};
82 static const WCHAR focusW[] = {'f','o','c','u','s',0};
83 static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};
85 static const WCHAR helpW[] = {'h','e','l','p',0};
86 static const WCHAR onhelpW[] = {'o','n','h','e','l','p',0};
88 static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
89 static const WCHAR onkeydownW[] = {'o','n','k','e','y','d','o','w','n',0};
91 static const WCHAR keypressW[] = {'k','e','y','p','r','e','s','s',0};
92 static const WCHAR onkeypressW[] = {'o','n','k','e','y','p','r','e','s','s',0};
94 static const WCHAR keyupW[] = {'k','e','y','u','p',0};
95 static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};
97 static const WCHAR loadW[] = {'l','o','a','d',0};
98 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
100 static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
101 static const WCHAR onmousedownW[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
103 static const WCHAR mousemoveW[] = {'m','o','u','s','e','m','o','v','e',0};
104 static const WCHAR onmousemoveW[] = {'o','n','m','o','u','s','e','m','o','v','e',0};
106 static const WCHAR mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
107 static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};
109 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
110 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
112 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
113 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
115 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
116 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
118 static const WCHAR readystatechangeW[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
119 static const WCHAR onreadystatechangeW[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
121 static const WCHAR resizeW[] = {'r','e','s','i','z','e',0};
122 static const WCHAR onresizeW[] = {'o','n','r','e','s','i','z','e',0};
124 static const WCHAR scrollW[] = {'s','c','r','o','l','l',0};
125 static const WCHAR onscrollW[] = {'o','n','s','c','r','o','l','l',0};
127 static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
128 static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
130 static const WCHAR submitW[] = {'s','u','b','m','i','t',0};
131 static const WCHAR onsubmitW[] = {'o','n','s','u','b','m','i','t',0};
133 static const WCHAR HTMLEventsW[] = {'H','T','M','L','E','v','e','n','t','s',0};
134 static const WCHAR KeyboardEventW[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
135 static const WCHAR MouseEventW[] = {'M','o','u','s','e','E','v','e','n','t',0};
137 enum {
138 EVENTT_NONE,
139 EVENTT_HTML,
140 EVENTT_KEY,
141 EVENTT_MOUSE
144 static const WCHAR *event_types[] = {
145 NULL,
146 HTMLEventsW,
147 KeyboardEventW,
148 MouseEventW
151 typedef struct {
152 LPCWSTR name;
153 LPCWSTR attr_name;
154 DWORD type;
155 DISPID dispid;
156 DWORD flags;
157 } event_info_t;
159 #define EVENT_DEFAULTLISTENER 0x0001
160 #define EVENT_BUBBLE 0x0002
161 #define EVENT_FORWARDBODY 0x0004
162 #define EVENT_NODEHANDLER 0x0008
163 #define EVENT_CANCELABLE 0x0010
164 #define EVENT_HASDEFAULTHANDLERS 0x0020
166 static const event_info_t event_info[] = {
167 {abortW, onabortW, EVENTT_NONE, DISPID_EVMETH_ONABORT,
168 EVENT_NODEHANDLER},
169 {beforeunloadW, onbeforeunloadW, EVENTT_NONE, DISPID_EVMETH_ONBEFOREUNLOAD,
170 EVENT_DEFAULTLISTENER|EVENT_FORWARDBODY},
171 {blurW, onblurW, EVENTT_HTML, DISPID_EVMETH_ONBLUR,
172 EVENT_DEFAULTLISTENER},
173 {changeW, onchangeW, EVENTT_HTML, DISPID_EVMETH_ONCHANGE,
174 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
175 {clickW, onclickW, EVENTT_MOUSE, DISPID_EVMETH_ONCLICK,
176 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE|EVENT_HASDEFAULTHANDLERS},
177 {contextmenuW, oncontextmenuW, EVENTT_MOUSE, DISPID_EVMETH_ONCONTEXTMENU,
178 EVENT_BUBBLE|EVENT_CANCELABLE},
179 {dataavailableW, ondataavailableW, EVENTT_NONE, DISPID_EVMETH_ONDATAAVAILABLE,
180 EVENT_BUBBLE},
181 {dblclickW, ondblclickW, EVENTT_MOUSE, DISPID_EVMETH_ONDBLCLICK,
182 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE},
183 {dragW, ondragW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAG,
184 EVENT_CANCELABLE},
185 {dragstartW, ondragstartW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAGSTART,
186 EVENT_CANCELABLE},
187 {errorW, onerrorW, EVENTT_NONE, DISPID_EVMETH_ONERROR,
188 EVENT_NODEHANDLER},
189 {focusW, onfocusW, EVENTT_HTML, DISPID_EVMETH_ONFOCUS,
190 EVENT_DEFAULTLISTENER},
191 {helpW, onhelpW, EVENTT_KEY, DISPID_EVMETH_ONHELP,
192 EVENT_BUBBLE|EVENT_CANCELABLE},
193 {keydownW, onkeydownW, EVENTT_KEY, DISPID_EVMETH_ONKEYDOWN,
194 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_HASDEFAULTHANDLERS},
195 {keypressW, onkeypressW, EVENTT_KEY, DISPID_EVMETH_ONKEYPRESS,
196 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
197 {keyupW, onkeyupW, EVENTT_KEY, DISPID_EVMETH_ONKEYUP,
198 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
199 {loadW, onloadW, EVENTT_HTML, DISPID_EVMETH_ONLOAD,
200 EVENT_NODEHANDLER},
201 {mousedownW, onmousedownW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEDOWN,
202 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
203 {mousemoveW, onmousemoveW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEMOVE,
204 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
205 {mouseoutW, onmouseoutW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEOUT,
206 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
207 {mouseoverW, onmouseoverW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEOVER,
208 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
209 {mouseupW, onmouseupW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEUP,
210 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
211 {pasteW, onpasteW, EVENTT_NONE, DISPID_EVMETH_ONPASTE,
212 EVENT_CANCELABLE},
213 {readystatechangeW, onreadystatechangeW, EVENTT_NONE, DISPID_EVMETH_ONREADYSTATECHANGE,
215 {resizeW, onresizeW, EVENTT_NONE, DISPID_EVMETH_ONRESIZE,
216 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
217 {scrollW, onscrollW, EVENTT_HTML, DISPID_EVMETH_ONSCROLL,
218 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
219 {selectstartW, onselectstartW, EVENTT_MOUSE, DISPID_EVMETH_ONSELECTSTART,
220 EVENT_CANCELABLE},
221 {submitW, onsubmitW, EVENTT_HTML, DISPID_EVMETH_ONSUBMIT,
222 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE}
225 static const eventid_t node_handled_list[] = { EVENTID_ABORT, EVENTID_ERROR, EVENTID_LOAD };
227 eventid_t str_to_eid(LPCWSTR str)
229 int i;
231 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
232 if(!strcmpW(event_info[i].name, str))
233 return i;
236 ERR("unknown type %s\n", debugstr_w(str));
237 return EVENTID_LAST;
240 static eventid_t attr_to_eid(LPCWSTR str)
242 int i;
244 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
245 if(!strcmpW(event_info[i].attr_name, str))
246 return i;
249 return EVENTID_LAST;
252 static DWORD get_node_handler_mask(eventid_t eid)
254 DWORD i;
256 for(i=0; i<sizeof(node_handled_list)/sizeof(*node_handled_list); i++) {
257 if(node_handled_list[i] == eid)
258 return 1 << i;
261 ERR("Invalid eid %d\n", eid);
262 return ~0;
265 typedef struct {
266 DispatchEx dispex;
267 IHTMLEventObj IHTMLEventObj_iface;
269 LONG ref;
271 HTMLDOMNode *target;
272 const event_info_t *type;
273 nsIDOMEvent *nsevent;
274 BOOL prevent_default;
275 BOOL cancel_bubble;
276 } HTMLEventObj;
278 static inline HTMLEventObj *impl_from_IHTMLEventObj(IHTMLEventObj *iface)
280 return CONTAINING_RECORD(iface, HTMLEventObj, IHTMLEventObj_iface);
283 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
285 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
287 *ppv = NULL;
289 if(IsEqualGUID(&IID_IUnknown, riid)) {
290 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
291 *ppv = &This->IHTMLEventObj_iface;
292 }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
293 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
294 *ppv = &This->IHTMLEventObj_iface;
295 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
296 return *ppv ? S_OK : E_NOINTERFACE;
299 if(*ppv) {
300 IUnknown_AddRef((IUnknown*)*ppv);
301 return S_OK;
304 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
305 return E_NOINTERFACE;
308 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
310 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
311 LONG ref = InterlockedIncrement(&This->ref);
313 TRACE("(%p) ref=%d\n", This, ref);
315 return ref;
318 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
320 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
321 LONG ref = InterlockedDecrement(&This->ref);
323 TRACE("(%p) ref=%d\n", This, ref);
325 if(!ref) {
326 if(This->target)
327 IHTMLDOMNode_Release(&This->target->IHTMLDOMNode_iface);
328 if(This->nsevent)
329 nsIDOMEvent_Release(This->nsevent);
330 release_dispex(&This->dispex);
331 heap_free(This);
334 return ref;
337 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
339 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
340 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
343 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
344 LCID lcid, ITypeInfo **ppTInfo)
346 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
347 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
350 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
351 LPOLESTR *rgszNames, UINT cNames,
352 LCID lcid, DISPID *rgDispId)
354 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
355 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
356 lcid, rgDispId);
359 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
360 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
361 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
363 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
364 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
365 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
368 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
370 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
372 TRACE("(%p)->(%p)\n", This, p);
374 *p = NULL;
375 if(This->target)
376 IHTMLDOMNode_QueryInterface(&This->target->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)p);
377 return S_OK;
380 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
382 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
383 cpp_bool ret = FALSE;
385 TRACE("(%p)->(%p)\n", This, p);
387 if(This->nsevent) {
388 nsIDOMKeyEvent *key_event;
389 nsresult nsres;
391 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
392 if(NS_SUCCEEDED(nsres)) {
393 nsIDOMKeyEvent_GetAltKey(key_event, &ret);
394 nsIDOMKeyEvent_Release(key_event);
395 }else {
396 nsIDOMMouseEvent *mouse_event;
398 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
399 if(NS_SUCCEEDED(nsres)) {
400 nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
401 nsIDOMMouseEvent_Release(mouse_event);
406 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
407 return S_OK;
410 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
412 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
413 cpp_bool ret = FALSE;
415 TRACE("(%p)->(%p)\n", This, p);
417 if(This->nsevent) {
418 nsIDOMKeyEvent *key_event;
419 nsresult nsres;
421 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
422 if(NS_SUCCEEDED(nsres)) {
423 nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
424 nsIDOMKeyEvent_Release(key_event);
425 }else {
426 nsIDOMMouseEvent *mouse_event;
428 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
429 if(NS_SUCCEEDED(nsres)) {
430 nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
431 nsIDOMMouseEvent_Release(mouse_event);
436 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
437 return S_OK;
440 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
442 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
443 cpp_bool ret = FALSE;
445 TRACE("(%p)->(%p)\n", This, p);
447 if(This->nsevent) {
448 nsIDOMKeyEvent *key_event;
449 nsresult nsres;
451 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
452 if(NS_SUCCEEDED(nsres)) {
453 nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
454 nsIDOMKeyEvent_Release(key_event);
455 }else {
456 nsIDOMMouseEvent *mouse_event;
458 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
459 if(NS_SUCCEEDED(nsres)) {
460 nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
461 nsIDOMMouseEvent_Release(mouse_event);
466 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
467 return S_OK;
470 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
472 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
474 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
476 if(V_VT(&v) != VT_BOOL) {
477 FIXME("unsupported value %s\n", debugstr_variant(&v));
478 return DISP_E_BADVARTYPE;
481 if(!V_BOOL(&v))
482 This->prevent_default = TRUE;
483 return S_OK;
486 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
488 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
490 FIXME("(%p)->(%p)\n", This, p);
492 V_VT(p) = VT_EMPTY;
493 return S_OK;
496 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
498 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
500 TRACE("(%p)->(%x)\n", This, v);
502 This->cancel_bubble = !!v;
503 return S_OK;
506 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
508 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
510 TRACE("(%p)->(%p)\n", This, p);
512 *p = This->cancel_bubble ? VARIANT_TRUE : VARIANT_FALSE;
513 return S_OK;
516 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
518 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
520 FIXME("(%p)->(%p)\n", This, p);
522 *p = NULL;
523 return S_OK;
526 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
528 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
530 FIXME("(%p)->(%p)\n", This, p);
532 *p = NULL;
533 return S_OK;
536 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
538 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
539 FIXME("(%p)->(%d)\n", This, v);
540 return E_NOTIMPL;
543 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
545 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
546 UINT32 key_code = 0;
548 TRACE("(%p)->(%p)\n", This, p);
550 if(This->nsevent) {
551 nsIDOMKeyEvent *key_event;
552 nsresult nsres;
554 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
555 if(NS_SUCCEEDED(nsres)) {
556 nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
557 nsIDOMKeyEvent_Release(key_event);
561 *p = key_code;
562 return S_OK;
565 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
567 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
568 UINT16 button = 0;
570 TRACE("(%p)->(%p)\n", This, p);
572 if(This->nsevent) {
573 nsIDOMMouseEvent *mouse_event;
574 nsresult nsres;
576 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
577 if(NS_SUCCEEDED(nsres)) {
578 nsIDOMMouseEvent_GetButton(mouse_event, &button);
579 nsIDOMMouseEvent_Release(mouse_event);
583 *p = button;
584 return S_OK;
587 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
589 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
591 TRACE("(%p)->(%p)\n", This, p);
593 if(!This->type) {
594 *p = NULL;
595 return S_OK;
598 *p = SysAllocString(This->type->name);
599 return *p ? S_OK : E_OUTOFMEMORY;
602 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
604 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
606 FIXME("(%p)->(%p)\n", This, p);
608 *p = NULL;
609 return S_OK;
612 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
614 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
616 FIXME("(%p)->(%p)\n", This, p);
618 *p = 0;
619 return S_OK;
622 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
624 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
625 LONG x = 0;
627 TRACE("(%p)->(%p)\n", This, p);
629 if(This->nsevent) {
630 nsIDOMUIEvent *ui_event;
631 nsresult nsres;
633 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMUIEvent, (void**)&ui_event);
634 if(NS_SUCCEEDED(nsres)) {
635 /* NOTE: pageX is not exactly right here. */
636 nsres = nsIDOMUIEvent_GetPageX(ui_event, &x);
637 assert(nsres == NS_OK);
638 nsIDOMUIEvent_Release(ui_event);
642 *p = x;
643 return S_OK;
646 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
648 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
649 LONG y = 0;
651 TRACE("(%p)->(%p)\n", This, p);
653 if(This->nsevent) {
654 nsIDOMUIEvent *ui_event;
655 nsresult nsres;
657 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMUIEvent, (void**)&ui_event);
658 if(NS_SUCCEEDED(nsres)) {
659 /* NOTE: pageY is not exactly right here. */
660 nsres = nsIDOMUIEvent_GetPageY(ui_event, &y);
661 assert(nsres == NS_OK);
662 nsIDOMUIEvent_Release(ui_event);
666 *p = y;
667 return S_OK;
670 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
672 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
673 LONG x = 0;
675 TRACE("(%p)->(%p)\n", This, p);
677 if(This->nsevent) {
678 nsIDOMMouseEvent *mouse_event;
679 nsresult nsres;
681 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
682 if(NS_SUCCEEDED(nsres)) {
683 nsIDOMMouseEvent_GetClientX(mouse_event, &x);
684 nsIDOMMouseEvent_Release(mouse_event);
688 *p = x;
689 return S_OK;
692 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
694 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
695 LONG y = 0;
697 TRACE("(%p)->(%p)\n", This, p);
699 if(This->nsevent) {
700 nsIDOMMouseEvent *mouse_event;
701 nsresult nsres;
703 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
704 if(NS_SUCCEEDED(nsres)) {
705 nsIDOMMouseEvent_GetClientY(mouse_event, &y);
706 nsIDOMMouseEvent_Release(mouse_event);
710 *p = y;
711 return S_OK;
714 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
716 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
718 FIXME("(%p)->(%p)\n", This, p);
720 *p = 0;
721 return S_OK;
724 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
726 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
728 FIXME("(%p)->(%p)\n", This, p);
730 *p = 0;
731 return S_OK;
734 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
736 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
737 LONG x = 0;
739 TRACE("(%p)->(%p)\n", This, p);
741 if(This->nsevent) {
742 nsIDOMMouseEvent *mouse_event;
743 nsresult nsres;
745 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
746 if(NS_SUCCEEDED(nsres)) {
747 nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
748 nsIDOMMouseEvent_Release(mouse_event);
752 *p = x;
753 return S_OK;
756 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
758 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
759 LONG y = 0;
761 TRACE("(%p)->(%p)\n", This, p);
763 if(This->nsevent) {
764 nsIDOMMouseEvent *mouse_event;
765 nsresult nsres;
767 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
768 if(NS_SUCCEEDED(nsres)) {
769 nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
770 nsIDOMMouseEvent_Release(mouse_event);
774 *p = y;
775 return S_OK;
778 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
780 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
782 FIXME("(%p)->(%p)\n", This, p);
784 *p = NULL;
785 return S_OK;
788 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
789 HTMLEventObj_QueryInterface,
790 HTMLEventObj_AddRef,
791 HTMLEventObj_Release,
792 HTMLEventObj_GetTypeInfoCount,
793 HTMLEventObj_GetTypeInfo,
794 HTMLEventObj_GetIDsOfNames,
795 HTMLEventObj_Invoke,
796 HTMLEventObj_get_srcElement,
797 HTMLEventObj_get_altKey,
798 HTMLEventObj_get_ctrlKey,
799 HTMLEventObj_get_shiftKey,
800 HTMLEventObj_put_returnValue,
801 HTMLEventObj_get_returnValue,
802 HTMLEventObj_put_cancelBubble,
803 HTMLEventObj_get_cancelBubble,
804 HTMLEventObj_get_fromElement,
805 HTMLEventObj_get_toElement,
806 HTMLEventObj_put_keyCode,
807 HTMLEventObj_get_keyCode,
808 HTMLEventObj_get_button,
809 HTMLEventObj_get_type,
810 HTMLEventObj_get_qualifier,
811 HTMLEventObj_get_reason,
812 HTMLEventObj_get_x,
813 HTMLEventObj_get_y,
814 HTMLEventObj_get_clientX,
815 HTMLEventObj_get_clientY,
816 HTMLEventObj_get_offsetX,
817 HTMLEventObj_get_offsetY,
818 HTMLEventObj_get_screenX,
819 HTMLEventObj_get_screenY,
820 HTMLEventObj_get_srcFilter
823 static inline HTMLEventObj *unsafe_impl_from_IHTMLEventObj(IHTMLEventObj *iface)
825 return iface->lpVtbl == &HTMLEventObjVtbl ? impl_from_IHTMLEventObj(iface) : NULL;
828 static const tid_t HTMLEventObj_iface_tids[] = {
829 IHTMLEventObj_tid,
833 static dispex_static_data_t HTMLEventObj_dispex = {
834 NULL,
835 DispCEventObj_tid,
836 NULL,
837 HTMLEventObj_iface_tids
840 static HTMLEventObj *create_event(void)
842 HTMLEventObj *ret;
844 ret = heap_alloc_zero(sizeof(*ret));
845 if(!ret)
846 return NULL;
848 ret->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
849 ret->ref = 1;
851 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLEventObj_iface, &HTMLEventObj_dispex);
853 return ret;
856 static HRESULT set_event_info(HTMLEventObj *event, HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
858 event->type = event_info+eid;
859 event->nsevent = nsevent;
861 if(nsevent) {
862 nsIDOMEvent_AddRef(nsevent);
863 }else if(event_types[event_info[eid].type]) {
864 nsAString type_str;
865 nsresult nsres;
867 nsAString_InitDepend(&type_str, event_types[event_info[eid].type]);
868 nsres = nsIDOMHTMLDocument_CreateEvent(target->doc->nsdoc, &type_str, &event->nsevent);
869 nsAString_Finish(&type_str);
870 if(NS_FAILED(nsres)) {
871 ERR("Could not create event: %08x\n", nsres);
872 return E_FAIL;
876 event->target = target;
877 if(target)
878 IHTMLDOMNode_AddRef(&target->IHTMLDOMNode_iface);
879 return S_OK;
882 HRESULT create_event_obj(IHTMLEventObj **ret)
884 HTMLEventObj *event;
886 event = create_event();
887 if(!event)
888 return E_OUTOFMEMORY;
890 *ret = &event->IHTMLEventObj_iface;
891 return S_OK;
894 static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
896 IDispatchEx *dispex;
897 EXCEPINFO ei;
898 HRESULT hres;
900 memset(&ei, 0, sizeof(ei));
902 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
903 if(SUCCEEDED(hres)) {
904 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, retv, &ei, NULL);
905 IDispatchEx_Release(dispex);
906 }else {
907 TRACE("Could not get IDispatchEx interface: %08x\n", hres);
908 hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
909 dp, retv, &ei, NULL);
912 return hres;
915 static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, HTMLEventObj *event_obj, VARIANT *retv)
917 DISPPARAMS dp = {NULL,NULL,0,0};
918 VARIANT event_arg;
919 ULONG argerr;
920 EXCEPINFO ei;
922 if(event_obj) {
923 V_VT(&event_arg) = VT_DISPATCH;
924 V_DISPATCH(&event_arg) = (IDispatch*)&event_obj->IHTMLEventObj_iface;
925 dp.rgvarg = &event_arg;
926 dp.cArgs = 1;
929 memset(&ei, 0, sizeof(ei));
930 return IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, retv, &ei, &argerr);
933 static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
935 int min, max, i;
936 HRESULT hres;
938 if(!data)
939 return FALSE;
941 if(!data->ids) {
942 hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
943 if(FAILED(hres))
944 return FALSE;
947 min = 0;
948 max = data->id_cnt-1;
949 while(min <= max) {
950 i = (min+max)/2;
951 if(data->ids[i] == dispid)
952 return TRUE;
954 if(data->ids[i] < dispid)
955 min = i+1;
956 else
957 max = i-1;
960 return FALSE;
963 static void call_event_handlers(HTMLDocumentNode *doc, HTMLEventObj *event_obj, event_target_t *event_target,
964 ConnectionPointContainer *cp_container, eventid_t eid, IDispatch *this_obj)
966 const BOOL cancelable = event_info[eid].flags & EVENT_CANCELABLE;
967 handler_vector_t *handler_vector = NULL;
968 VARIANT v;
969 HRESULT hres;
971 if(event_target)
972 handler_vector = event_target->event_table[eid];
974 if(handler_vector && handler_vector->handler_prop) {
975 DISPID named_arg = DISPID_THIS;
976 VARIANTARG arg;
977 DISPPARAMS dp = {&arg, &named_arg, 1, 1};
979 V_VT(&arg) = VT_DISPATCH;
980 V_DISPATCH(&arg) = this_obj;
981 V_VT(&v) = VT_EMPTY;
983 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
984 hres = call_disp_func(handler_vector->handler_prop, &dp, &v);
985 if(hres == S_OK) {
986 TRACE("%s <<< %s\n", debugstr_w(event_info[eid].name), debugstr_variant(&v));
988 if(cancelable) {
989 if(V_VT(&v) == VT_BOOL) {
990 if(!V_BOOL(&v))
991 event_obj->prevent_default = TRUE;
992 }else if(V_VT(&v) != VT_EMPTY) {
993 FIXME("unhandled result %s\n", debugstr_variant(&v));
996 VariantClear(&v);
997 }else {
998 WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
1002 if(handler_vector && handler_vector->handler_cnt) {
1003 VARIANTARG arg;
1004 DISPPARAMS dp = {&arg, NULL, 1, 0};
1005 int i;
1007 V_VT(&arg) = VT_DISPATCH;
1008 V_DISPATCH(&arg) = (IDispatch*)event_obj;
1010 i = handler_vector->handler_cnt;
1011 while(i--) {
1012 if(handler_vector->handlers[i]) {
1013 V_VT(&v) = VT_EMPTY;
1015 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
1016 hres = call_disp_func(handler_vector->handlers[i], &dp, &v);
1017 if(hres == S_OK) {
1018 TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
1020 if(cancelable) {
1021 if(V_VT(&v) == VT_BOOL) {
1022 if(!V_BOOL(&v))
1023 event_obj->prevent_default = TRUE;
1024 }else if(V_VT(&v) != VT_EMPTY) {
1025 FIXME("unhandled result %s\n", debugstr_variant(&v));
1028 VariantClear(&v);
1029 }else {
1030 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
1037 * NOTE: CP events may require doc_obj reference, which we don't own. We make sure that
1038 * it's safe to call event handler by checking nsevent_listener, which is NULL for
1039 * detached documents.
1041 if(cp_container && cp_container->forward_container)
1042 cp_container = cp_container->forward_container;
1043 if(cp_container && cp_container->cps && doc->nsevent_listener) {
1044 ConnectionPoint *cp;
1045 unsigned i, j;
1047 for(j=0; cp_container->cp_entries[j].riid; j++) {
1048 cp = cp_container->cps + j;
1049 if(!cp->sinks_size || !is_cp_event(cp->data, event_info[eid].dispid))
1050 continue;
1052 for(i=0; doc->nsevent_listener && i < cp->sinks_size; i++) {
1053 if(!cp->sinks[i].disp)
1054 continue;
1056 V_VT(&v) = VT_EMPTY;
1058 TRACE("cp %s [%u] >>>\n", debugstr_w(event_info[eid].name), i);
1059 hres = call_cp_func(cp->sinks[i].disp, event_info[eid].dispid,
1060 cp->data->pass_event_arg ? event_obj : NULL, &v);
1061 if(hres == S_OK) {
1062 TRACE("cp %s [%u] <<<\n", debugstr_w(event_info[eid].name), i);
1064 if(cancelable) {
1065 if(V_VT(&v) == VT_BOOL) {
1066 if(!V_BOOL(&v))
1067 event_obj->prevent_default = TRUE;
1068 }else if(V_VT(&v) != VT_EMPTY) {
1069 FIXME("unhandled result %s\n", debugstr_variant(&v));
1072 VariantClear(&v);
1073 }else {
1074 WARN("cp %s [%u] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
1078 if(!doc->nsevent_listener)
1079 break;
1084 static void fire_event_obj(HTMLDocumentNode *doc, eventid_t eid, HTMLEventObj *event_obj,
1085 nsIDOMNode *target, IDispatch *script_this)
1087 IHTMLEventObj *prev_event;
1088 nsIDOMNode *parent, *nsnode;
1089 BOOL prevent_default = FALSE;
1090 HTMLInnerWindow *window;
1091 HTMLDOMNode *node;
1092 UINT16 node_type;
1093 nsresult nsres;
1094 HRESULT hres;
1096 TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
1098 window = doc->window;
1099 if(!window) {
1100 WARN("NULL window\n");
1101 return;
1104 htmldoc_addref(&doc->basedoc);
1106 prev_event = window->event;
1107 window->event = event_obj ? &event_obj->IHTMLEventObj_iface : NULL;
1109 nsIDOMNode_GetNodeType(target, &node_type);
1110 nsnode = target;
1111 nsIDOMNode_AddRef(nsnode);
1113 switch(node_type) {
1114 case ELEMENT_NODE:
1115 do {
1116 hres = get_node(doc, nsnode, FALSE, &node);
1117 if(SUCCEEDED(hres) && node) {
1118 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1119 node->cp_container, eid, script_this ? script_this : (IDispatch*)&node->IHTMLDOMNode_iface);
1120 node_release(node);
1123 if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1124 break;
1126 nsIDOMNode_GetParentNode(nsnode, &parent);
1127 nsIDOMNode_Release(nsnode);
1128 nsnode = parent;
1129 if(!nsnode)
1130 break;
1132 nsIDOMNode_GetNodeType(nsnode, &node_type);
1133 }while(node_type == ELEMENT_NODE);
1135 if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1136 break;
1138 case DOCUMENT_NODE:
1139 if(event_info[eid].flags & EVENT_FORWARDBODY) {
1140 nsIDOMHTMLElement *nsbody;
1141 nsresult nsres;
1143 nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
1144 if(NS_SUCCEEDED(nsres) && nsbody) {
1145 hres = get_node(doc, (nsIDOMNode*)nsbody, FALSE, &node);
1146 if(SUCCEEDED(hres) && node) {
1147 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1148 node->cp_container, eid, script_this ? script_this : (IDispatch*)&node->IHTMLDOMNode_iface);
1149 node_release(node);
1151 nsIDOMHTMLElement_Release(nsbody);
1152 }else {
1153 ERR("Could not get body: %08x\n", nsres);
1157 call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
1158 script_this ? script_this : (IDispatch*)&doc->basedoc.IHTMLDocument2_iface);
1159 break;
1161 default:
1162 FIXME("unimplemented node type %d\n", node_type);
1165 if(nsnode)
1166 nsIDOMNode_Release(nsnode);
1168 if(event_obj && event_obj->prevent_default)
1169 prevent_default = TRUE;
1170 window->event = prev_event;
1172 if(!prevent_default && (event_info[eid].flags & EVENT_HASDEFAULTHANDLERS)) {
1173 nsIDOMNode_AddRef(target);
1174 nsnode = target;
1176 do {
1177 hres = get_node(doc, nsnode, TRUE, &node);
1178 if(FAILED(hres))
1179 break;
1181 if(node) {
1182 if(node->vtbl->handle_event)
1183 hres = node->vtbl->handle_event(node, eid, event_obj ? event_obj->nsevent : NULL, &prevent_default);
1184 node_release(node);
1185 if(FAILED(hres) || prevent_default || (event_obj && event_obj->cancel_bubble))
1186 break;
1189 nsres = nsIDOMNode_GetParentNode(nsnode, &parent);
1190 if(NS_FAILED(nsres))
1191 break;
1193 nsIDOMNode_Release(nsnode);
1194 nsnode = parent;
1195 } while(nsnode);
1197 if(nsnode)
1198 nsIDOMNode_Release(nsnode);
1201 if(prevent_default && event_obj && event_obj->nsevent) {
1202 TRACE("calling PreventDefault\n");
1203 nsIDOMEvent_PreventDefault(event_obj->nsevent);
1206 htmldoc_release(&doc->basedoc);
1209 void fire_event(HTMLDocumentNode *doc, eventid_t eid, BOOL set_event, nsIDOMNode *target, nsIDOMEvent *nsevent,
1210 IDispatch *script_this)
1212 HTMLEventObj *event_obj = NULL;
1213 HTMLDOMNode *node;
1214 HRESULT hres;
1216 if(set_event) {
1217 hres = get_node(doc, target, TRUE, &node);
1218 if(FAILED(hres))
1219 return;
1221 event_obj = create_event();
1222 node_release(node);
1223 if(!event_obj)
1224 return;
1226 hres = set_event_info(event_obj, node, eid, nsevent);
1227 if(FAILED(hres)) {
1228 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1229 return;
1233 fire_event_obj(doc, eid, event_obj, target, script_this);
1235 if(event_obj)
1236 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1239 HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_var, VARIANT_BOOL *cancelled)
1241 HTMLEventObj *event_obj = NULL;
1242 eventid_t eid;
1243 HRESULT hres;
1245 eid = attr_to_eid(event_name);
1246 if(eid == EVENTID_LAST) {
1247 WARN("unknown event %s\n", debugstr_w(event_name));
1248 return E_INVALIDARG;
1251 if(event_var && V_VT(event_var) != VT_EMPTY && V_VT(event_var) != VT_ERROR) {
1252 if(V_VT(event_var) != VT_DISPATCH) {
1253 FIXME("event_var %s not supported\n", debugstr_variant(event_var));
1254 return E_NOTIMPL;
1257 if(V_DISPATCH(event_var)) {
1258 IHTMLEventObj *event_iface;
1260 hres = IDispatch_QueryInterface(V_DISPATCH(event_var), &IID_IHTMLEventObj, (void**)&event_iface);
1261 if(FAILED(hres)) {
1262 FIXME("No IHTMLEventObj iface\n");
1263 return hres;
1266 event_obj = unsafe_impl_from_IHTMLEventObj(event_iface);
1267 if(!event_obj) {
1268 ERR("Not our IHTMLEventObj?\n");
1269 IHTMLEventObj_Release(event_iface);
1270 return E_FAIL;
1275 if(event_obj) {
1276 hres = set_event_info(event_obj, node, eid, NULL);
1277 if(SUCCEEDED(hres))
1278 fire_event_obj(node->doc, eid, event_obj, node->nsnode, NULL);
1280 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1281 if(FAILED(hres))
1282 return hres;
1283 }else {
1284 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
1285 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1286 return E_NOTIMPL;
1289 fire_event(node->doc, eid, TRUE, node->nsnode, NULL, NULL);
1292 *cancelled = VARIANT_TRUE; /* FIXME */
1293 return S_OK;
1296 HRESULT call_fire_event(HTMLDOMNode *node, eventid_t eid)
1298 HRESULT hres;
1300 if(node->vtbl->fire_event) {
1301 BOOL handled = FALSE;
1303 hres = node->vtbl->fire_event(node, eid, &handled);
1304 if(handled)
1305 return hres;
1308 fire_event(node->doc, eid, TRUE, node->nsnode, NULL, NULL);
1309 return S_OK;
1312 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
1314 if(!*event_target_ptr)
1315 *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
1316 return *event_target_ptr;
1319 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
1321 handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
1323 if(handler_vector) {
1324 if(cnt <= handler_vector->handler_cnt)
1325 return TRUE;
1327 new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1328 }else {
1329 new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1332 if(!new_vector)
1333 return FALSE;
1335 new_vector->handler_cnt = cnt;
1336 event_target->event_table[eid] = new_vector;
1337 return TRUE;
1340 static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1342 if(!doc->nsdoc)
1343 return S_OK;
1345 if(event_info[eid].flags & EVENT_NODEHANDLER) {
1346 DWORD mask;
1348 mask = get_node_handler_mask(eid);
1349 if(event_target->node_handlers_mask & mask)
1350 return S_OK;
1352 add_nsevent_listener(doc, nsnode, event_info[eid].name);
1353 event_target->node_handlers_mask |= mask;
1354 return S_OK;
1357 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER))
1358 return S_OK;
1360 if(!doc->event_vector[eid]) {
1361 doc->event_vector[eid] = TRUE;
1362 add_nsevent_listener(doc, NULL, event_info[eid].name);
1365 return S_OK;
1368 void detach_events(HTMLDocumentNode *doc)
1370 if(doc->event_vector) {
1371 int i;
1373 for(i=0; i < EVENTID_LAST; i++) {
1374 if(doc->event_vector[i]) {
1375 detach_nsevent(doc, event_info[i].name);
1376 doc->event_vector[i] = FALSE;
1381 release_nsevents(doc);
1385 static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
1387 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1388 IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
1389 (*event_target)->event_table[eid]->handler_prop = NULL;
1392 return S_OK;
1395 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocumentNode *doc,
1396 eventid_t eid, IDispatch *disp)
1398 event_target_t *event_target;
1400 if(!disp)
1401 return remove_event_handler(event_target_ptr, eid);
1403 event_target = get_event_target(event_target_ptr);
1404 if(!event_target)
1405 return E_OUTOFMEMORY;
1407 if(!alloc_handler_vector(event_target, eid, 0))
1408 return E_OUTOFMEMORY;
1410 if(event_target->event_table[eid]->handler_prop)
1411 IDispatch_Release(event_target->event_table[eid]->handler_prop);
1413 event_target->event_table[eid]->handler_prop = disp;
1414 IDispatch_AddRef(disp);
1416 return ensure_nsevent_handler(doc, event_target, nsnode, eid);
1419 HRESULT set_event_handler(event_target_t **event_target, nsIDOMNode *nsnode, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1421 switch(V_VT(var)) {
1422 case VT_NULL:
1423 return remove_event_handler(event_target, eid);
1425 case VT_DISPATCH:
1426 return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1428 default:
1429 FIXME("not handler %s\n", debugstr_variant(var));
1430 /* fall through */
1431 case VT_EMPTY:
1432 return E_NOTIMPL;
1435 return S_OK;
1438 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
1440 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1441 V_VT(var) = VT_DISPATCH;
1442 V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1443 IDispatch_AddRef(V_DISPATCH(var));
1444 }else {
1445 V_VT(var) = VT_NULL;
1448 return S_OK;
1451 HRESULT attach_event(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocument *doc, BSTR name,
1452 IDispatch *disp, VARIANT_BOOL *res)
1454 event_target_t *event_target;
1455 eventid_t eid;
1456 DWORD i = 0;
1458 eid = attr_to_eid(name);
1459 if(eid == EVENTID_LAST) {
1460 WARN("Unknown event\n");
1461 *res = VARIANT_TRUE;
1462 return S_OK;
1465 event_target = get_event_target(event_target_ptr);
1466 if(!event_target)
1467 return E_OUTOFMEMORY;
1469 if(event_target->event_table[eid]) {
1470 while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
1471 i++;
1472 if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
1473 return E_OUTOFMEMORY;
1474 }else if(!alloc_handler_vector(event_target, eid, i+1)) {
1475 return E_OUTOFMEMORY;
1478 IDispatch_AddRef(disp);
1479 event_target->event_table[eid]->handlers[i] = disp;
1481 *res = VARIANT_TRUE;
1482 return ensure_nsevent_handler(doc->doc_node, event_target, nsnode, eid);
1485 HRESULT detach_event(event_target_t *event_target, HTMLDocument *doc, BSTR name, IDispatch *disp)
1487 eventid_t eid;
1488 DWORD i = 0;
1490 if(!event_target)
1491 return S_OK;
1493 eid = attr_to_eid(name);
1494 if(eid == EVENTID_LAST) {
1495 WARN("Unknown event\n");
1496 return S_OK;
1499 if(!event_target->event_table[eid])
1500 return S_OK;
1502 while(i < event_target->event_table[eid]->handler_cnt) {
1503 if(event_target->event_table[eid]->handlers[i] == disp) {
1504 IDispatch_Release(event_target->event_table[eid]->handlers[i]);
1505 event_target->event_table[eid]->handlers[i] = NULL;
1507 i++;
1510 return S_OK;
1513 void bind_node_event(HTMLDocumentNode *doc, event_target_t **event_target, HTMLDOMNode *node, const WCHAR *event, IDispatch *disp)
1515 eventid_t eid;
1517 TRACE("(%p %p %p %s %p)\n", doc, event_target, node, debugstr_w(event), disp);
1519 eid = attr_to_eid(event);
1520 if(eid == EVENTID_LAST) {
1521 WARN("Unsupported event %s\n", debugstr_w(event));
1522 return;
1525 set_event_handler_disp(event_target, node ? node->nsnode : NULL, doc, eid, disp);
1528 void update_cp_events(HTMLInnerWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp, nsIDOMNode *nsnode)
1530 event_target_t *event_target;
1531 int i;
1533 event_target = get_event_target(event_target_ptr);
1534 if(!event_target)
1535 return; /* FIXME */
1537 for(i=0; i < EVENTID_LAST; i++) {
1538 if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1539 ensure_nsevent_handler(window->doc, event_target, nsnode, i);
1543 void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
1545 const PRUnichar *attr_value;
1546 nsAString attr_name_str, attr_value_str;
1547 IDispatch *disp;
1548 HTMLDOMNode *node;
1549 int i;
1550 nsresult nsres;
1551 HRESULT hres;
1553 nsAString_Init(&attr_value_str, NULL);
1554 nsAString_Init(&attr_name_str, NULL);
1556 for(i=0; i < EVENTID_LAST; i++) {
1557 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
1558 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
1559 if(NS_SUCCEEDED(nsres)) {
1560 nsAString_GetData(&attr_value_str, &attr_value);
1561 if(!*attr_value)
1562 continue;
1564 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
1566 disp = script_parse_event(doc->window, attr_value);
1567 if(disp) {
1568 hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1569 if(SUCCEEDED(hres)) {
1570 set_event_handler_disp(get_node_event_target(node), node->nsnode, node->doc, i, disp);
1571 node_release(node);
1573 IDispatch_Release(disp);
1578 nsAString_Finish(&attr_value_str);
1579 nsAString_Finish(&attr_name_str);
1582 HRESULT doc_init_events(HTMLDocumentNode *doc)
1584 unsigned i;
1585 HRESULT hres;
1587 doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
1588 if(!doc->event_vector)
1589 return E_OUTOFMEMORY;
1591 init_nsevents(doc);
1593 for(i=0; i < EVENTID_LAST; i++) {
1594 if(event_info[i].flags & EVENT_HASDEFAULTHANDLERS) {
1595 hres = ensure_nsevent_handler(doc, NULL, NULL, i);
1596 if(FAILED(hres))
1597 return hres;
1601 return S_OK;
1604 void release_event_target(event_target_t *event_target)
1606 int i;
1607 unsigned int j;
1609 for(i=0; i < EVENTID_LAST; i++) {
1610 if(event_target->event_table[i]) {
1611 if(event_target->event_table[i]->handler_prop)
1612 IDispatch_Release(event_target->event_table[i]->handler_prop);
1613 for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
1614 if(event_target->event_table[i]->handlers[j])
1615 IDispatch_Release(event_target->event_table[i]->handlers[j]);
1619 heap_free(event_target);