mshtml: Wine Gecko 2.34-beta2 release.
[wine/wine-gecko.git] / dlls / mshtml / htmlevent.c
blob99632939b2b92f4fa0db551359d4de71a533619d
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 handler_vector_t *event_table[EVENTID_LAST];
48 static const WCHAR abortW[] = {'a','b','o','r','t',0};
49 static const WCHAR onabortW[] = {'o','n','a','b','o','r','t',0};
51 static const WCHAR beforeunloadW[] = {'b','e','f','o','r','e','u','n','l','o','a','d',0};
52 static const WCHAR onbeforeunloadW[] = {'o','n','b','e','f','o','r','e','u','n','l','o','a','d',0};
54 static const WCHAR blurW[] = {'b','l','u','r',0};
55 static const WCHAR onblurW[] = {'o','n','b','l','u','r',0};
57 static const WCHAR changeW[] = {'c','h','a','n','g','e',0};
58 static const WCHAR onchangeW[] = {'o','n','c','h','a','n','g','e',0};
60 static const WCHAR clickW[] = {'c','l','i','c','k',0};
61 static const WCHAR onclickW[] = {'o','n','c','l','i','c','k',0};
63 static const WCHAR contextmenuW[] = {'c','o','n','t','e','x','t','m','e','n','u',0};
64 static const WCHAR oncontextmenuW[] = {'o','n','c','o','n','t','e','x','t','m','e','n','u',0};
66 static const WCHAR dataavailableW[] = {'d','a','t','a','a','v','a','i','l','a','b','l','e',0};
67 static const WCHAR ondataavailableW[] = {'o','n','d','a','t','a','a','v','a','i','l','a','b','l','e',0};
69 static const WCHAR dblclickW[] = {'d','b','l','c','l','i','c','k',0};
70 static const WCHAR ondblclickW[] = {'o','n','d','b','l','c','l','i','c','k',0};
72 static const WCHAR dragW[] = {'d','r','a','g',0};
73 static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};
75 static const WCHAR dragstartW[] = {'d','r','a','g','s','t','a','r','t',0};
76 static const WCHAR ondragstartW[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
78 static const WCHAR errorW[] = {'e','r','r','o','r',0};
79 static const WCHAR onerrorW[] = {'o','n','e','r','r','o','r',0};
81 static const WCHAR focusW[] = {'f','o','c','u','s',0};
82 static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};
84 static const WCHAR helpW[] = {'h','e','l','p',0};
85 static const WCHAR onhelpW[] = {'o','n','h','e','l','p',0};
87 static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
88 static const WCHAR onkeydownW[] = {'o','n','k','e','y','d','o','w','n',0};
90 static const WCHAR keypressW[] = {'k','e','y','p','r','e','s','s',0};
91 static const WCHAR onkeypressW[] = {'o','n','k','e','y','p','r','e','s','s',0};
93 static const WCHAR keyupW[] = {'k','e','y','u','p',0};
94 static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};
96 static const WCHAR loadW[] = {'l','o','a','d',0};
97 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
99 static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
100 static const WCHAR onmousedownW[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
102 static const WCHAR mousemoveW[] = {'m','o','u','s','e','m','o','v','e',0};
103 static const WCHAR onmousemoveW[] = {'o','n','m','o','u','s','e','m','o','v','e',0};
105 static const WCHAR mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
106 static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};
108 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
109 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
111 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
112 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
114 static const WCHAR mousewheelW[] = {'m','o','u','s','e','w','h','e','e','l',0};
115 static const WCHAR onmousewheelW[] = {'o','n','m','o','u','s','e','w','h','e','e','l',0};
117 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
118 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
120 static const WCHAR readystatechangeW[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
121 static const WCHAR onreadystatechangeW[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
123 static const WCHAR resizeW[] = {'r','e','s','i','z','e',0};
124 static const WCHAR onresizeW[] = {'o','n','r','e','s','i','z','e',0};
126 static const WCHAR scrollW[] = {'s','c','r','o','l','l',0};
127 static const WCHAR onscrollW[] = {'o','n','s','c','r','o','l','l',0};
129 static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
130 static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
132 static const WCHAR submitW[] = {'s','u','b','m','i','t',0};
133 static const WCHAR onsubmitW[] = {'o','n','s','u','b','m','i','t',0};
135 static const WCHAR HTMLEventsW[] = {'H','T','M','L','E','v','e','n','t','s',0};
136 static const WCHAR KeyboardEventW[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
137 static const WCHAR MouseEventW[] = {'M','o','u','s','e','E','v','e','n','t',0};
139 enum {
140 EVENTT_NONE,
141 EVENTT_HTML,
142 EVENTT_KEY,
143 EVENTT_MOUSE
146 static const WCHAR *event_types[] = {
147 NULL,
148 HTMLEventsW,
149 KeyboardEventW,
150 MouseEventW
153 typedef struct {
154 LPCWSTR name;
155 LPCWSTR attr_name;
156 DWORD type;
157 DISPID dispid;
158 DWORD flags;
159 } event_info_t;
161 #define EVENT_DEFAULTLISTENER 0x0001
162 #define EVENT_BUBBLE 0x0002
163 #define EVENT_FORWARDBODY 0x0004
164 #define EVENT_BIND_TO_BODY 0x0008
165 #define EVENT_CANCELABLE 0x0010
166 #define EVENT_HASDEFAULTHANDLERS 0x0020
168 static const event_info_t event_info[] = {
169 {abortW, onabortW, EVENTT_NONE, DISPID_EVMETH_ONABORT,
170 EVENT_BIND_TO_BODY},
171 {beforeunloadW, onbeforeunloadW, EVENTT_NONE, DISPID_EVMETH_ONBEFOREUNLOAD,
172 EVENT_DEFAULTLISTENER|EVENT_FORWARDBODY},
173 {blurW, onblurW, EVENTT_HTML, DISPID_EVMETH_ONBLUR,
174 EVENT_DEFAULTLISTENER},
175 {changeW, onchangeW, EVENTT_HTML, DISPID_EVMETH_ONCHANGE,
176 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
177 {clickW, onclickW, EVENTT_MOUSE, DISPID_EVMETH_ONCLICK,
178 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE|EVENT_HASDEFAULTHANDLERS},
179 {contextmenuW, oncontextmenuW, EVENTT_MOUSE, DISPID_EVMETH_ONCONTEXTMENU,
180 EVENT_BUBBLE|EVENT_CANCELABLE},
181 {dataavailableW, ondataavailableW, EVENTT_NONE, DISPID_EVMETH_ONDATAAVAILABLE,
182 EVENT_BUBBLE},
183 {dblclickW, ondblclickW, EVENTT_MOUSE, DISPID_EVMETH_ONDBLCLICK,
184 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE},
185 {dragW, ondragW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAG,
186 EVENT_CANCELABLE},
187 {dragstartW, ondragstartW, EVENTT_MOUSE, DISPID_EVMETH_ONDRAGSTART,
188 EVENT_CANCELABLE},
189 {errorW, onerrorW, EVENTT_NONE, DISPID_EVMETH_ONERROR,
190 EVENT_BIND_TO_BODY},
191 {focusW, onfocusW, EVENTT_HTML, DISPID_EVMETH_ONFOCUS,
192 EVENT_DEFAULTLISTENER},
193 {helpW, onhelpW, EVENTT_KEY, DISPID_EVMETH_ONHELP,
194 EVENT_BUBBLE|EVENT_CANCELABLE},
195 {keydownW, onkeydownW, EVENTT_KEY, DISPID_EVMETH_ONKEYDOWN,
196 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_HASDEFAULTHANDLERS},
197 {keypressW, onkeypressW, EVENTT_KEY, DISPID_EVMETH_ONKEYPRESS,
198 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
199 {keyupW, onkeyupW, EVENTT_KEY, DISPID_EVMETH_ONKEYUP,
200 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
201 {loadW, onloadW, EVENTT_HTML, DISPID_EVMETH_ONLOAD,
202 EVENT_BIND_TO_BODY},
203 {mousedownW, onmousedownW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEDOWN,
204 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
205 {mousemoveW, onmousemoveW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEMOVE,
206 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
207 {mouseoutW, onmouseoutW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEOUT,
208 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
209 {mouseoverW, onmouseoverW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEOVER,
210 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
211 {mouseupW, onmouseupW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEUP,
212 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
213 {mousewheelW, onmousewheelW, EVENTT_MOUSE, DISPID_EVMETH_ONMOUSEWHEEL,
215 {pasteW, onpasteW, EVENTT_NONE, DISPID_EVMETH_ONPASTE,
216 EVENT_CANCELABLE},
217 {readystatechangeW, onreadystatechangeW, EVENTT_NONE, DISPID_EVMETH_ONREADYSTATECHANGE,
219 {resizeW, onresizeW, EVENTT_NONE, DISPID_EVMETH_ONRESIZE,
220 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
221 {scrollW, onscrollW, EVENTT_HTML, DISPID_EVMETH_ONSCROLL,
222 EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
223 {selectstartW, onselectstartW, EVENTT_MOUSE, DISPID_EVMETH_ONSELECTSTART,
224 EVENT_CANCELABLE},
225 {submitW, onsubmitW, EVENTT_HTML, DISPID_EVMETH_ONSUBMIT,
226 EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE|EVENT_HASDEFAULTHANDLERS}
229 eventid_t str_to_eid(LPCWSTR str)
231 int i;
233 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
234 if(!strcmpW(event_info[i].name, str))
235 return i;
238 ERR("unknown type %s\n", debugstr_w(str));
239 return EVENTID_LAST;
242 static eventid_t attr_to_eid(LPCWSTR str)
244 int i;
246 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
247 if(!strcmpW(event_info[i].attr_name, str))
248 return i;
251 return EVENTID_LAST;
254 typedef struct {
255 DispatchEx dispex;
256 IHTMLEventObj IHTMLEventObj_iface;
258 LONG ref;
260 HTMLDOMNode *target;
261 const event_info_t *type;
262 nsIDOMEvent *nsevent;
263 BOOL prevent_default;
264 BOOL cancel_bubble;
265 } HTMLEventObj;
267 static inline HTMLEventObj *impl_from_IHTMLEventObj(IHTMLEventObj *iface)
269 return CONTAINING_RECORD(iface, HTMLEventObj, IHTMLEventObj_iface);
272 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
274 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
276 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
278 if(IsEqualGUID(&IID_IUnknown, riid)) {
279 *ppv = &This->IHTMLEventObj_iface;
280 }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
281 *ppv = &This->IHTMLEventObj_iface;
282 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
283 return *ppv ? S_OK : E_NOINTERFACE;
284 }else {
285 *ppv = NULL;
286 WARN("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
287 return E_NOINTERFACE;
290 IUnknown_AddRef((IUnknown*)*ppv);
291 return S_OK;
294 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
296 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
297 LONG ref = InterlockedIncrement(&This->ref);
299 TRACE("(%p) ref=%d\n", This, ref);
301 return ref;
304 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
306 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
307 LONG ref = InterlockedDecrement(&This->ref);
309 TRACE("(%p) ref=%d\n", This, ref);
311 if(!ref) {
312 if(This->target)
313 IHTMLDOMNode_Release(&This->target->IHTMLDOMNode_iface);
314 if(This->nsevent)
315 nsIDOMEvent_Release(This->nsevent);
316 release_dispex(&This->dispex);
317 heap_free(This);
320 return ref;
323 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
325 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
326 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
329 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
330 LCID lcid, ITypeInfo **ppTInfo)
332 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
333 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
336 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
337 LPOLESTR *rgszNames, UINT cNames,
338 LCID lcid, DISPID *rgDispId)
340 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
341 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
342 lcid, rgDispId);
345 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
346 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
347 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
349 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
350 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
351 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
354 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
356 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
358 TRACE("(%p)->(%p)\n", This, p);
360 *p = NULL;
361 if(This->target)
362 IHTMLDOMNode_QueryInterface(&This->target->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)p);
363 return S_OK;
366 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
368 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
369 cpp_bool ret = FALSE;
371 TRACE("(%p)->(%p)\n", This, p);
373 if(This->nsevent) {
374 nsIDOMKeyEvent *key_event;
375 nsresult nsres;
377 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
378 if(NS_SUCCEEDED(nsres)) {
379 nsIDOMKeyEvent_GetAltKey(key_event, &ret);
380 nsIDOMKeyEvent_Release(key_event);
381 }else {
382 nsIDOMMouseEvent *mouse_event;
384 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
385 if(NS_SUCCEEDED(nsres)) {
386 nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
387 nsIDOMMouseEvent_Release(mouse_event);
392 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
393 return S_OK;
396 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
398 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
399 cpp_bool ret = FALSE;
401 TRACE("(%p)->(%p)\n", This, p);
403 if(This->nsevent) {
404 nsIDOMKeyEvent *key_event;
405 nsresult nsres;
407 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
408 if(NS_SUCCEEDED(nsres)) {
409 nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
410 nsIDOMKeyEvent_Release(key_event);
411 }else {
412 nsIDOMMouseEvent *mouse_event;
414 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
415 if(NS_SUCCEEDED(nsres)) {
416 nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
417 nsIDOMMouseEvent_Release(mouse_event);
422 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
423 return S_OK;
426 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
428 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
429 cpp_bool ret = FALSE;
431 TRACE("(%p)->(%p)\n", This, p);
433 if(This->nsevent) {
434 nsIDOMKeyEvent *key_event;
435 nsresult nsres;
437 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
438 if(NS_SUCCEEDED(nsres)) {
439 nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
440 nsIDOMKeyEvent_Release(key_event);
441 }else {
442 nsIDOMMouseEvent *mouse_event;
444 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
445 if(NS_SUCCEEDED(nsres)) {
446 nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
447 nsIDOMMouseEvent_Release(mouse_event);
452 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
453 return S_OK;
456 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
458 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
460 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
462 if(V_VT(&v) != VT_BOOL) {
463 FIXME("unsupported value %s\n", debugstr_variant(&v));
464 return DISP_E_BADVARTYPE;
467 if(!V_BOOL(&v))
468 This->prevent_default = TRUE;
469 return S_OK;
472 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
474 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
476 FIXME("(%p)->(%p)\n", This, p);
478 V_VT(p) = VT_EMPTY;
479 return S_OK;
482 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
484 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
486 TRACE("(%p)->(%x)\n", This, v);
488 This->cancel_bubble = !!v;
489 return S_OK;
492 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
494 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
496 TRACE("(%p)->(%p)\n", This, p);
498 *p = This->cancel_bubble ? VARIANT_TRUE : VARIANT_FALSE;
499 return S_OK;
502 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
504 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
506 FIXME("(%p)->(%p)\n", This, p);
508 *p = NULL;
509 return S_OK;
512 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
514 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
516 FIXME("(%p)->(%p)\n", This, p);
518 *p = NULL;
519 return S_OK;
522 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
524 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
525 FIXME("(%p)->(%d)\n", This, v);
526 return E_NOTIMPL;
529 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
531 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
532 UINT32 key_code = 0;
534 TRACE("(%p)->(%p)\n", This, p);
536 if(This->nsevent) {
537 nsIDOMKeyEvent *key_event;
538 nsresult nsres;
540 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
541 if(NS_SUCCEEDED(nsres)) {
542 nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
543 nsIDOMKeyEvent_Release(key_event);
547 *p = key_code;
548 return S_OK;
551 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
553 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
554 INT16 button = 0;
556 TRACE("(%p)->(%p)\n", This, p);
558 if(This->nsevent) {
559 nsIDOMMouseEvent *mouse_event;
560 nsresult nsres;
562 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
563 if(NS_SUCCEEDED(nsres)) {
564 nsIDOMMouseEvent_GetButton(mouse_event, &button);
565 nsIDOMMouseEvent_Release(mouse_event);
569 *p = button;
570 return S_OK;
573 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
575 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
577 TRACE("(%p)->(%p)\n", This, p);
579 if(!This->type) {
580 *p = NULL;
581 return S_OK;
584 *p = SysAllocString(This->type->name);
585 return *p ? S_OK : E_OUTOFMEMORY;
588 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
590 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
592 FIXME("(%p)->(%p)\n", This, p);
594 *p = NULL;
595 return S_OK;
598 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
600 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
602 FIXME("(%p)->(%p)\n", This, p);
604 *p = 0;
605 return S_OK;
608 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
610 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
611 LONG x = 0;
613 TRACE("(%p)->(%p)\n", This, p);
615 if(This->nsevent) {
616 nsIDOMUIEvent *ui_event;
617 nsresult nsres;
619 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMUIEvent, (void**)&ui_event);
620 if(NS_SUCCEEDED(nsres)) {
621 /* NOTE: pageX is not exactly right here. */
622 nsres = nsIDOMUIEvent_GetPageX(ui_event, &x);
623 assert(nsres == NS_OK);
624 nsIDOMUIEvent_Release(ui_event);
628 *p = x;
629 return S_OK;
632 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
634 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
635 LONG y = 0;
637 TRACE("(%p)->(%p)\n", This, p);
639 if(This->nsevent) {
640 nsIDOMUIEvent *ui_event;
641 nsresult nsres;
643 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMUIEvent, (void**)&ui_event);
644 if(NS_SUCCEEDED(nsres)) {
645 /* NOTE: pageY is not exactly right here. */
646 nsres = nsIDOMUIEvent_GetPageY(ui_event, &y);
647 assert(nsres == NS_OK);
648 nsIDOMUIEvent_Release(ui_event);
652 *p = y;
653 return S_OK;
656 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
658 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
659 LONG x = 0;
661 TRACE("(%p)->(%p)\n", This, p);
663 if(This->nsevent) {
664 nsIDOMMouseEvent *mouse_event;
665 nsresult nsres;
667 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
668 if(NS_SUCCEEDED(nsres)) {
669 nsIDOMMouseEvent_GetClientX(mouse_event, &x);
670 nsIDOMMouseEvent_Release(mouse_event);
674 *p = x;
675 return S_OK;
678 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
680 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
681 LONG y = 0;
683 TRACE("(%p)->(%p)\n", This, p);
685 if(This->nsevent) {
686 nsIDOMMouseEvent *mouse_event;
687 nsresult nsres;
689 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
690 if(NS_SUCCEEDED(nsres)) {
691 nsIDOMMouseEvent_GetClientY(mouse_event, &y);
692 nsIDOMMouseEvent_Release(mouse_event);
696 *p = y;
697 return S_OK;
700 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
702 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
704 FIXME("(%p)->(%p)\n", This, p);
706 *p = 0;
707 return S_OK;
710 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
712 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
714 FIXME("(%p)->(%p)\n", This, p);
716 *p = 0;
717 return S_OK;
720 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
722 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
723 LONG x = 0;
725 TRACE("(%p)->(%p)\n", This, p);
727 if(This->nsevent) {
728 nsIDOMMouseEvent *mouse_event;
729 nsresult nsres;
731 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
732 if(NS_SUCCEEDED(nsres)) {
733 nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
734 nsIDOMMouseEvent_Release(mouse_event);
738 *p = x;
739 return S_OK;
742 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
744 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
745 LONG y = 0;
747 TRACE("(%p)->(%p)\n", This, p);
749 if(This->nsevent) {
750 nsIDOMMouseEvent *mouse_event;
751 nsresult nsres;
753 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
754 if(NS_SUCCEEDED(nsres)) {
755 nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
756 nsIDOMMouseEvent_Release(mouse_event);
760 *p = y;
761 return S_OK;
764 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
766 HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
768 FIXME("(%p)->(%p)\n", This, p);
770 *p = NULL;
771 return S_OK;
774 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
775 HTMLEventObj_QueryInterface,
776 HTMLEventObj_AddRef,
777 HTMLEventObj_Release,
778 HTMLEventObj_GetTypeInfoCount,
779 HTMLEventObj_GetTypeInfo,
780 HTMLEventObj_GetIDsOfNames,
781 HTMLEventObj_Invoke,
782 HTMLEventObj_get_srcElement,
783 HTMLEventObj_get_altKey,
784 HTMLEventObj_get_ctrlKey,
785 HTMLEventObj_get_shiftKey,
786 HTMLEventObj_put_returnValue,
787 HTMLEventObj_get_returnValue,
788 HTMLEventObj_put_cancelBubble,
789 HTMLEventObj_get_cancelBubble,
790 HTMLEventObj_get_fromElement,
791 HTMLEventObj_get_toElement,
792 HTMLEventObj_put_keyCode,
793 HTMLEventObj_get_keyCode,
794 HTMLEventObj_get_button,
795 HTMLEventObj_get_type,
796 HTMLEventObj_get_qualifier,
797 HTMLEventObj_get_reason,
798 HTMLEventObj_get_x,
799 HTMLEventObj_get_y,
800 HTMLEventObj_get_clientX,
801 HTMLEventObj_get_clientY,
802 HTMLEventObj_get_offsetX,
803 HTMLEventObj_get_offsetY,
804 HTMLEventObj_get_screenX,
805 HTMLEventObj_get_screenY,
806 HTMLEventObj_get_srcFilter
809 static inline HTMLEventObj *unsafe_impl_from_IHTMLEventObj(IHTMLEventObj *iface)
811 return iface->lpVtbl == &HTMLEventObjVtbl ? impl_from_IHTMLEventObj(iface) : NULL;
814 static const tid_t HTMLEventObj_iface_tids[] = {
815 IHTMLEventObj_tid,
819 static dispex_static_data_t HTMLEventObj_dispex = {
820 NULL,
821 DispCEventObj_tid,
822 NULL,
823 HTMLEventObj_iface_tids
826 static HTMLEventObj *create_event(void)
828 HTMLEventObj *ret;
830 ret = heap_alloc_zero(sizeof(*ret));
831 if(!ret)
832 return NULL;
834 ret->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
835 ret->ref = 1;
837 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLEventObj_iface, &HTMLEventObj_dispex);
839 return ret;
842 static HRESULT set_event_info(HTMLEventObj *event, HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
844 event->type = event_info+eid;
845 event->nsevent = nsevent;
847 if(nsevent) {
848 nsIDOMEvent_AddRef(nsevent);
849 }else if(event_types[event_info[eid].type]) {
850 nsAString type_str;
851 nsresult nsres;
853 nsAString_InitDepend(&type_str, event_types[event_info[eid].type]);
854 nsres = nsIDOMHTMLDocument_CreateEvent(target->doc->nsdoc, &type_str, &event->nsevent);
855 nsAString_Finish(&type_str);
856 if(NS_FAILED(nsres)) {
857 ERR("Could not create event: %08x\n", nsres);
858 return E_FAIL;
862 event->target = target;
863 if(target)
864 IHTMLDOMNode_AddRef(&target->IHTMLDOMNode_iface);
865 return S_OK;
868 HRESULT create_event_obj(IHTMLEventObj **ret)
870 HTMLEventObj *event;
872 event = create_event();
873 if(!event)
874 return E_OUTOFMEMORY;
876 *ret = &event->IHTMLEventObj_iface;
877 return S_OK;
880 static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
882 IDispatchEx *dispex;
883 EXCEPINFO ei;
884 HRESULT hres;
886 memset(&ei, 0, sizeof(ei));
888 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
889 if(SUCCEEDED(hres)) {
890 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, retv, &ei, NULL);
891 IDispatchEx_Release(dispex);
892 }else {
893 TRACE("Could not get IDispatchEx interface: %08x\n", hres);
894 hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
895 dp, retv, &ei, NULL);
898 return hres;
901 static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, HTMLEventObj *event_obj, VARIANT *retv)
903 DISPPARAMS dp = {NULL,NULL,0,0};
904 VARIANT event_arg;
905 ULONG argerr;
906 EXCEPINFO ei;
908 if(event_obj) {
909 V_VT(&event_arg) = VT_DISPATCH;
910 V_DISPATCH(&event_arg) = (IDispatch*)&event_obj->IHTMLEventObj_iface;
911 dp.rgvarg = &event_arg;
912 dp.cArgs = 1;
915 memset(&ei, 0, sizeof(ei));
916 return IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, retv, &ei, &argerr);
919 static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
921 int min, max, i;
922 HRESULT hres;
924 if(!data)
925 return FALSE;
927 if(!data->ids) {
928 hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
929 if(FAILED(hres))
930 return FALSE;
933 min = 0;
934 max = data->id_cnt-1;
935 while(min <= max) {
936 i = (min+max)/2;
937 if(data->ids[i] == dispid)
938 return TRUE;
940 if(data->ids[i] < dispid)
941 min = i+1;
942 else
943 max = i-1;
946 return FALSE;
949 static void call_event_handlers(HTMLDocumentNode *doc, HTMLEventObj *event_obj, event_target_t *event_target,
950 ConnectionPointContainer *cp_container, eventid_t eid, IDispatch *this_obj)
952 const BOOL cancelable = event_info[eid].flags & EVENT_CANCELABLE;
953 handler_vector_t *handler_vector = NULL;
954 VARIANT v;
955 HRESULT hres;
957 if(event_target)
958 handler_vector = event_target->event_table[eid];
960 if(handler_vector && handler_vector->handler_prop) {
961 DISPID named_arg = DISPID_THIS;
962 VARIANTARG arg;
963 DISPPARAMS dp = {&arg, &named_arg, 1, 1};
965 V_VT(&arg) = VT_DISPATCH;
966 V_DISPATCH(&arg) = this_obj;
967 V_VT(&v) = VT_EMPTY;
969 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
970 hres = call_disp_func(handler_vector->handler_prop, &dp, &v);
971 if(hres == S_OK) {
972 TRACE("%s <<< %s\n", debugstr_w(event_info[eid].name), debugstr_variant(&v));
974 if(cancelable) {
975 if(V_VT(&v) == VT_BOOL) {
976 if(!V_BOOL(&v))
977 event_obj->prevent_default = TRUE;
978 }else if(V_VT(&v) != VT_EMPTY) {
979 FIXME("unhandled result %s\n", debugstr_variant(&v));
982 VariantClear(&v);
983 }else {
984 WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
988 if(handler_vector && handler_vector->handler_cnt) {
989 VARIANTARG arg;
990 DISPPARAMS dp = {&arg, NULL, 1, 0};
991 int i;
993 V_VT(&arg) = VT_DISPATCH;
994 V_DISPATCH(&arg) = (IDispatch*)event_obj;
996 i = handler_vector->handler_cnt;
997 while(i--) {
998 if(handler_vector->handlers[i]) {
999 V_VT(&v) = VT_EMPTY;
1001 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
1002 hres = call_disp_func(handler_vector->handlers[i], &dp, &v);
1003 if(hres == S_OK) {
1004 TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
1006 if(cancelable) {
1007 if(V_VT(&v) == VT_BOOL) {
1008 if(!V_BOOL(&v))
1009 event_obj->prevent_default = TRUE;
1010 }else if(V_VT(&v) != VT_EMPTY) {
1011 FIXME("unhandled result %s\n", debugstr_variant(&v));
1014 VariantClear(&v);
1015 }else {
1016 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
1023 * NOTE: CP events may require doc_obj reference, which we don't own. We make sure that
1024 * it's safe to call event handler by checking nsevent_listener, which is NULL for
1025 * detached documents.
1027 if(cp_container && cp_container->forward_container)
1028 cp_container = cp_container->forward_container;
1029 if(cp_container && cp_container->cps && doc->nsevent_listener) {
1030 ConnectionPoint *cp;
1031 unsigned i, j;
1033 for(j=0; cp_container->cp_entries[j].riid; j++) {
1034 cp = cp_container->cps + j;
1035 if(!cp->sinks_size || !is_cp_event(cp->data, event_info[eid].dispid))
1036 continue;
1038 for(i=0; doc->nsevent_listener && i < cp->sinks_size; i++) {
1039 if(!cp->sinks[i].disp)
1040 continue;
1042 V_VT(&v) = VT_EMPTY;
1044 TRACE("cp %s [%u] >>>\n", debugstr_w(event_info[eid].name), i);
1045 hres = call_cp_func(cp->sinks[i].disp, event_info[eid].dispid,
1046 cp->data->pass_event_arg ? event_obj : NULL, &v);
1047 if(hres == S_OK) {
1048 TRACE("cp %s [%u] <<<\n", debugstr_w(event_info[eid].name), i);
1050 if(cancelable) {
1051 if(V_VT(&v) == VT_BOOL) {
1052 if(!V_BOOL(&v))
1053 event_obj->prevent_default = TRUE;
1054 }else if(V_VT(&v) != VT_EMPTY) {
1055 FIXME("unhandled result %s\n", debugstr_variant(&v));
1058 VariantClear(&v);
1059 }else {
1060 WARN("cp %s [%u] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
1064 if(!doc->nsevent_listener)
1065 break;
1070 static void fire_event_obj(HTMLDocumentNode *doc, eventid_t eid, HTMLEventObj *event_obj,
1071 nsIDOMNode *target, IDispatch *script_this)
1073 IHTMLEventObj *prev_event;
1074 nsIDOMNode *parent, *nsnode;
1075 BOOL prevent_default = FALSE;
1076 HTMLInnerWindow *window;
1077 HTMLDOMNode *node;
1078 UINT16 node_type;
1079 nsresult nsres;
1080 HRESULT hres;
1082 TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
1084 window = doc->window;
1085 if(!window) {
1086 WARN("NULL window\n");
1087 return;
1090 htmldoc_addref(&doc->basedoc);
1092 prev_event = window->event;
1093 window->event = event_obj ? &event_obj->IHTMLEventObj_iface : NULL;
1095 nsIDOMNode_GetNodeType(target, &node_type);
1096 nsnode = target;
1097 nsIDOMNode_AddRef(nsnode);
1099 switch(node_type) {
1100 case ELEMENT_NODE:
1101 do {
1102 hres = get_node(doc, nsnode, FALSE, &node);
1103 if(SUCCEEDED(hres) && node) {
1104 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1105 node->cp_container, eid, script_this ? script_this : (IDispatch*)&node->IHTMLDOMNode_iface);
1106 node_release(node);
1109 if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1110 break;
1112 nsIDOMNode_GetParentNode(nsnode, &parent);
1113 nsIDOMNode_Release(nsnode);
1114 nsnode = parent;
1115 if(!nsnode)
1116 break;
1118 nsIDOMNode_GetNodeType(nsnode, &node_type);
1119 }while(node_type == ELEMENT_NODE);
1121 if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1122 break;
1124 case DOCUMENT_NODE:
1125 if(event_info[eid].flags & EVENT_FORWARDBODY) {
1126 nsIDOMHTMLElement *nsbody;
1127 nsresult nsres;
1129 nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
1130 if(NS_SUCCEEDED(nsres) && nsbody) {
1131 hres = get_node(doc, (nsIDOMNode*)nsbody, FALSE, &node);
1132 if(SUCCEEDED(hres) && node) {
1133 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1134 node->cp_container, eid, script_this ? script_this : (IDispatch*)&node->IHTMLDOMNode_iface);
1135 node_release(node);
1137 nsIDOMHTMLElement_Release(nsbody);
1138 }else {
1139 ERR("Could not get body: %08x\n", nsres);
1143 call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
1144 script_this ? script_this : (IDispatch*)&doc->basedoc.IHTMLDocument2_iface);
1145 break;
1147 default:
1148 FIXME("unimplemented node type %d\n", node_type);
1151 if(nsnode)
1152 nsIDOMNode_Release(nsnode);
1154 if(event_obj && event_obj->prevent_default)
1155 prevent_default = TRUE;
1156 window->event = prev_event;
1158 if(!prevent_default && (event_info[eid].flags & EVENT_HASDEFAULTHANDLERS)) {
1159 nsIDOMNode_AddRef(target);
1160 nsnode = target;
1162 do {
1163 hres = get_node(doc, nsnode, TRUE, &node);
1164 if(FAILED(hres))
1165 break;
1167 if(node) {
1168 if(node->vtbl->handle_event)
1169 hres = node->vtbl->handle_event(node, eid, event_obj ? event_obj->nsevent : NULL, &prevent_default);
1170 node_release(node);
1171 if(FAILED(hres) || prevent_default || (event_obj && event_obj->cancel_bubble))
1172 break;
1175 nsres = nsIDOMNode_GetParentNode(nsnode, &parent);
1176 if(NS_FAILED(nsres))
1177 break;
1179 nsIDOMNode_Release(nsnode);
1180 nsnode = parent;
1181 } while(nsnode);
1183 if(nsnode)
1184 nsIDOMNode_Release(nsnode);
1187 if(prevent_default && event_obj && event_obj->nsevent) {
1188 TRACE("calling PreventDefault\n");
1189 nsIDOMEvent_PreventDefault(event_obj->nsevent);
1192 htmldoc_release(&doc->basedoc);
1195 void fire_event(HTMLDocumentNode *doc, eventid_t eid, BOOL set_event, nsIDOMNode *target, nsIDOMEvent *nsevent,
1196 IDispatch *script_this)
1198 HTMLEventObj *event_obj = NULL;
1199 HTMLDOMNode *node;
1200 HRESULT hres;
1202 if(set_event) {
1203 hres = get_node(doc, target, TRUE, &node);
1204 if(FAILED(hres))
1205 return;
1207 event_obj = create_event();
1208 node_release(node);
1209 if(!event_obj)
1210 return;
1212 hres = set_event_info(event_obj, node, eid, nsevent);
1213 if(FAILED(hres)) {
1214 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1215 return;
1219 fire_event_obj(doc, eid, event_obj, target, script_this);
1221 if(event_obj)
1222 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1225 HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_var, VARIANT_BOOL *cancelled)
1227 HTMLEventObj *event_obj = NULL;
1228 eventid_t eid;
1229 HRESULT hres;
1231 eid = attr_to_eid(event_name);
1232 if(eid == EVENTID_LAST) {
1233 WARN("unknown event %s\n", debugstr_w(event_name));
1234 return E_INVALIDARG;
1237 if(event_var && V_VT(event_var) != VT_EMPTY && V_VT(event_var) != VT_ERROR) {
1238 if(V_VT(event_var) != VT_DISPATCH) {
1239 FIXME("event_var %s not supported\n", debugstr_variant(event_var));
1240 return E_NOTIMPL;
1243 if(V_DISPATCH(event_var)) {
1244 IHTMLEventObj *event_iface;
1246 hres = IDispatch_QueryInterface(V_DISPATCH(event_var), &IID_IHTMLEventObj, (void**)&event_iface);
1247 if(FAILED(hres)) {
1248 FIXME("No IHTMLEventObj iface\n");
1249 return hres;
1252 event_obj = unsafe_impl_from_IHTMLEventObj(event_iface);
1253 if(!event_obj) {
1254 ERR("Not our IHTMLEventObj?\n");
1255 IHTMLEventObj_Release(event_iface);
1256 return E_FAIL;
1261 if(event_obj) {
1262 hres = set_event_info(event_obj, node, eid, NULL);
1263 if(SUCCEEDED(hres))
1264 fire_event_obj(node->doc, eid, event_obj, node->nsnode, NULL);
1266 IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1267 if(FAILED(hres))
1268 return hres;
1269 }else {
1270 if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
1271 FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1272 return E_NOTIMPL;
1275 fire_event(node->doc, eid, TRUE, node->nsnode, NULL, NULL);
1278 *cancelled = VARIANT_TRUE; /* FIXME */
1279 return S_OK;
1282 HRESULT call_fire_event(HTMLDOMNode *node, eventid_t eid)
1284 HRESULT hres;
1286 if(node->vtbl->fire_event) {
1287 BOOL handled = FALSE;
1289 hres = node->vtbl->fire_event(node, eid, &handled);
1290 if(handled)
1291 return hres;
1294 fire_event(node->doc, eid, TRUE, node->nsnode, NULL, NULL);
1295 return S_OK;
1298 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
1300 if(!*event_target_ptr)
1301 *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
1302 return *event_target_ptr;
1305 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
1307 handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
1309 if(handler_vector) {
1310 if(cnt <= handler_vector->handler_cnt)
1311 return TRUE;
1313 new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1314 }else {
1315 new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1318 if(!new_vector)
1319 return FALSE;
1321 new_vector->handler_cnt = cnt;
1322 event_target->event_table[eid] = new_vector;
1323 return TRUE;
1326 static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, eventid_t eid)
1328 nsIDOMNode *nsnode = NULL;
1330 TRACE("%s\n", debugstr_w(event_info[eid].name));
1332 if(!doc->nsdoc || doc->event_vector[eid] || !(event_info[eid].flags & (EVENT_DEFAULTLISTENER|EVENT_BIND_TO_BODY)))
1333 return S_OK;
1335 if(event_info[eid].flags & EVENT_BIND_TO_BODY) {
1336 nsnode = doc->node.nsnode;
1337 nsIDOMNode_AddRef(nsnode);
1340 doc->event_vector[eid] = TRUE;
1341 add_nsevent_listener(doc, nsnode, event_info[eid].name);
1343 if(nsnode)
1344 nsIDOMNode_Release(nsnode);
1345 return S_OK;
1348 void detach_events(HTMLDocumentNode *doc)
1350 if(doc->event_vector) {
1351 int i;
1353 for(i=0; i < EVENTID_LAST; i++) {
1354 if(doc->event_vector[i]) {
1355 detach_nsevent(doc, event_info[i].name);
1356 doc->event_vector[i] = FALSE;
1361 release_nsevents(doc);
1365 static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
1367 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1368 IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
1369 (*event_target)->event_table[eid]->handler_prop = NULL;
1372 return S_OK;
1375 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, HTMLDocumentNode *doc,
1376 eventid_t eid, IDispatch *disp)
1378 event_target_t *event_target;
1380 if(!disp)
1381 return remove_event_handler(event_target_ptr, eid);
1383 event_target = get_event_target(event_target_ptr);
1384 if(!event_target)
1385 return E_OUTOFMEMORY;
1387 if(!alloc_handler_vector(event_target, eid, 0))
1388 return E_OUTOFMEMORY;
1390 if(event_target->event_table[eid]->handler_prop)
1391 IDispatch_Release(event_target->event_table[eid]->handler_prop);
1393 event_target->event_table[eid]->handler_prop = disp;
1394 IDispatch_AddRef(disp);
1396 return ensure_nsevent_handler(doc, event_target, eid);
1399 HRESULT set_event_handler(event_target_t **event_target, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1401 switch(V_VT(var)) {
1402 case VT_NULL:
1403 return remove_event_handler(event_target, eid);
1405 case VT_DISPATCH:
1406 return set_event_handler_disp(event_target, doc, eid, V_DISPATCH(var));
1408 default:
1409 FIXME("not handler %s\n", debugstr_variant(var));
1410 /* fall through */
1411 case VT_EMPTY:
1412 return E_NOTIMPL;
1415 return S_OK;
1418 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
1420 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1421 V_VT(var) = VT_DISPATCH;
1422 V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1423 IDispatch_AddRef(V_DISPATCH(var));
1424 }else {
1425 V_VT(var) = VT_NULL;
1428 return S_OK;
1431 HRESULT attach_event(event_target_t **event_target_ptr, HTMLDocument *doc, BSTR name,
1432 IDispatch *disp, VARIANT_BOOL *res)
1434 event_target_t *event_target;
1435 eventid_t eid;
1436 DWORD i = 0;
1438 eid = attr_to_eid(name);
1439 if(eid == EVENTID_LAST) {
1440 WARN("Unknown event\n");
1441 *res = VARIANT_TRUE;
1442 return S_OK;
1445 event_target = get_event_target(event_target_ptr);
1446 if(!event_target)
1447 return E_OUTOFMEMORY;
1449 if(event_target->event_table[eid]) {
1450 while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
1451 i++;
1452 if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
1453 return E_OUTOFMEMORY;
1454 }else if(!alloc_handler_vector(event_target, eid, i+1)) {
1455 return E_OUTOFMEMORY;
1458 IDispatch_AddRef(disp);
1459 event_target->event_table[eid]->handlers[i] = disp;
1461 *res = VARIANT_TRUE;
1462 return ensure_nsevent_handler(doc->doc_node, event_target, eid);
1465 HRESULT detach_event(event_target_t *event_target, HTMLDocument *doc, BSTR name, IDispatch *disp)
1467 eventid_t eid;
1468 DWORD i = 0;
1470 if(!event_target)
1471 return S_OK;
1473 eid = attr_to_eid(name);
1474 if(eid == EVENTID_LAST) {
1475 WARN("Unknown event\n");
1476 return S_OK;
1479 if(!event_target->event_table[eid])
1480 return S_OK;
1482 while(i < event_target->event_table[eid]->handler_cnt) {
1483 if(event_target->event_table[eid]->handlers[i] == disp) {
1484 IDispatch_Release(event_target->event_table[eid]->handlers[i]);
1485 event_target->event_table[eid]->handlers[i] = NULL;
1487 i++;
1490 return S_OK;
1493 void bind_node_event(HTMLDocumentNode *doc, event_target_t **event_target, HTMLDOMNode *node, const WCHAR *event, IDispatch *disp)
1495 eventid_t eid;
1497 TRACE("(%p %p %p %s %p)\n", doc, event_target, node, debugstr_w(event), disp);
1499 eid = attr_to_eid(event);
1500 if(eid == EVENTID_LAST) {
1501 WARN("Unsupported event %s\n", debugstr_w(event));
1502 return;
1505 set_event_handler_disp(event_target, doc, eid, disp);
1508 void update_cp_events(HTMLInnerWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp)
1510 event_target_t *event_target;
1511 int i;
1513 event_target = get_event_target(event_target_ptr);
1514 if(!event_target)
1515 return; /* FIXME */
1517 for(i=0; i < EVENTID_LAST; i++) {
1518 if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1519 ensure_nsevent_handler(window->doc, event_target, i);
1523 void check_event_attr(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem)
1525 const PRUnichar *attr_value;
1526 nsAString attr_value_str;
1527 IDispatch *disp;
1528 HTMLDOMNode *node;
1529 int i;
1530 nsresult nsres;
1531 HRESULT hres;
1533 for(i=0; i < EVENTID_LAST; i++) {
1534 nsres = get_elem_attr_value(nselem, event_info[i].attr_name, &attr_value_str, &attr_value);
1535 if(NS_SUCCEEDED(nsres)) {
1536 if(!*attr_value)
1537 continue;
1539 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
1541 disp = script_parse_event(doc->window, attr_value);
1542 if(disp) {
1543 hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1544 if(SUCCEEDED(hres)) {
1545 set_event_handler_disp(get_node_event_target(node), node->doc, i, disp);
1546 node_release(node);
1548 IDispatch_Release(disp);
1550 nsAString_Finish(&attr_value_str);
1555 HRESULT doc_init_events(HTMLDocumentNode *doc)
1557 unsigned i;
1558 HRESULT hres;
1560 doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
1561 if(!doc->event_vector)
1562 return E_OUTOFMEMORY;
1564 init_nsevents(doc);
1566 for(i=0; i < EVENTID_LAST; i++) {
1567 if(event_info[i].flags & EVENT_HASDEFAULTHANDLERS) {
1568 hres = ensure_nsevent_handler(doc, NULL, i);
1569 if(FAILED(hres))
1570 return hres;
1574 return S_OK;
1577 void release_event_target(event_target_t *event_target)
1579 int i;
1580 unsigned int j;
1582 for(i=0; i < EVENTID_LAST; i++) {
1583 if(event_target->event_table[i]) {
1584 if(event_target->event_table[i]->handler_prop)
1585 IDispatch_Release(event_target->event_table[i]->handler_prop);
1586 for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
1587 if(event_target->event_table[i]->handlers[j])
1588 IDispatch_Release(event_target->event_table[i]->handlers[j]);
1592 heap_free(event_target);