mshtml: Added IHTMLElement2::attachEvent implementation.
[wine.git] / dlls / mshtml / htmlevent.c
blob4bde49a91fc22e95128416d7b5a1338fe5a81d2f
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"
28 #include "mshtml_private.h"
29 #include "htmlevent.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35 typedef struct {
36 IDispatch *handler_prop;
37 DWORD handler_cnt;
38 IDispatch *handlers[0];
39 } handler_vector_t;
41 struct event_target_t {
42 handler_vector_t *event_table[EVENTID_LAST];
45 static const WCHAR blurW[] = {'b','l','u','r',0};
46 static const WCHAR onblurW[] = {'o','n','b','l','u','r',0};
48 static const WCHAR changeW[] = {'c','h','a','n','g','e',0};
49 static const WCHAR onchangeW[] = {'o','n','c','h','a','n','g','e',0};
51 static const WCHAR clickW[] = {'c','l','i','c','k',0};
52 static const WCHAR onclickW[] = {'o','n','c','l','i','c','k',0};
54 static const WCHAR dragW[] = {'d','r','a','g',0};
55 static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};
57 static const WCHAR dragstartW[] = {'d','r','a','g','s','t','a','r','t',0};
58 static const WCHAR ondragstartW[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
60 static const WCHAR focusW[] = {'f','o','c','u','s',0};
61 static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};
63 static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
64 static const WCHAR onkeydownW[] = {'o','n','k','e','y','d','o','w','n',0};
66 static const WCHAR keyupW[] = {'k','e','y','u','p',0};
67 static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};
69 static const WCHAR loadW[] = {'l','o','a','d',0};
70 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
72 static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
73 static const WCHAR onmousedownW[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
75 static const WCHAR mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
76 static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};
78 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
79 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
81 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
82 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
84 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
85 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
87 static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
88 static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
90 typedef struct {
91 LPCWSTR name;
92 LPCWSTR attr_name;
93 DWORD flags;
94 } event_info_t;
96 #define EVENT_DEFAULTLISTENER 0x0001
97 #define EVENT_BUBBLE 0x0002
99 static const event_info_t event_info[] = {
100 {blurW, onblurW, EVENT_DEFAULTLISTENER},
101 {changeW, onchangeW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
102 {clickW, onclickW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
103 {dragW, ondragW, 0},
104 {dragstartW, ondragstartW, 0},
105 {focusW, onfocusW, EVENT_DEFAULTLISTENER},
106 {keydownW, onkeydownW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
107 {keyupW, onkeyupW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
108 {loadW, onloadW, 0},
109 {mousedownW, onmousedownW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
110 {mouseoutW, onmouseoutW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
111 {mouseoverW, onmouseoverW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
112 {mouseupW, onmouseupW, EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
113 {pasteW, onpasteW, 0},
114 {selectstartW, onselectstartW, 0}
117 eventid_t str_to_eid(LPCWSTR str)
119 int i;
121 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
122 if(!strcmpW(event_info[i].name, str))
123 return i;
126 ERR("unknown type %s\n", debugstr_w(str));
127 return EVENTID_LAST;
130 static eventid_t attr_to_eid(LPCWSTR str)
132 int i;
134 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
135 if(!strcmpW(event_info[i].attr_name, str))
136 return i;
139 return EVENTID_LAST;
142 typedef struct {
143 DispatchEx dispex;
144 const IHTMLEventObjVtbl *lpIHTMLEventObjVtbl;
146 LONG ref;
148 HTMLDOMNode *target;
149 const event_info_t *type;
150 nsIDOMEvent *nsevent;
151 } HTMLEventObj;
153 #define HTMLEVENTOBJ(x) ((IHTMLEventObj*) &(x)->lpIHTMLEventObjVtbl)
155 #define HTMLEVENTOBJ_THIS(iface) DEFINE_THIS(HTMLEventObj, IHTMLEventObj, iface)
157 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
159 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
161 *ppv = NULL;
163 if(IsEqualGUID(&IID_IUnknown, riid)) {
164 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
165 *ppv = HTMLEVENTOBJ(This);
166 }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
167 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
168 *ppv = HTMLEVENTOBJ(This);
169 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
170 return *ppv ? S_OK : E_NOINTERFACE;
173 if(*ppv) {
174 IUnknown_AddRef((IUnknown*)*ppv);
175 return S_OK;
178 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
179 return E_NOINTERFACE;
182 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
184 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
185 LONG ref = InterlockedIncrement(&This->ref);
187 TRACE("(%p) ref=%d\n", This, ref);
189 return ref;
192 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
194 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
195 LONG ref = InterlockedDecrement(&This->ref);
197 TRACE("(%p) ref=%d\n", This, ref);
199 if(!ref) {
200 if(This->nsevent)
201 nsIDOMEvent_Release(This->nsevent);
202 release_dispex(&This->dispex);
203 heap_free(This);
206 return ref;
209 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
211 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
212 FIXME("(%p)->(%p)\n", This, pctinfo);
213 return E_NOTIMPL;
216 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
217 LCID lcid, ITypeInfo **ppTInfo)
219 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
220 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
221 return E_NOTIMPL;
224 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
225 LPOLESTR *rgszNames, UINT cNames,
226 LCID lcid, DISPID *rgDispId)
228 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
229 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
230 lcid, rgDispId);
231 return E_NOTIMPL;
234 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
235 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
236 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
238 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
239 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
240 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
241 return E_NOTIMPL;
244 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
246 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
248 TRACE("(%p)->(%p)\n", This, p);
250 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(This->target), &IID_IHTMLElement, (void**)p);
253 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
255 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
256 PRBool ret = FALSE;
258 TRACE("(%p)->(%p)\n", This, p);
260 if(This->nsevent) {
261 nsIDOMKeyEvent *key_event;
262 nsresult nsres;
264 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
265 if(NS_SUCCEEDED(nsres)) {
266 nsIDOMKeyEvent_GetAltKey(key_event, &ret);
267 nsIDOMKeyEvent_Release(key_event);
268 }else {
269 nsIDOMMouseEvent *mouse_event;
271 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
272 if(NS_SUCCEEDED(nsres)) {
273 nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
274 nsIDOMMouseEvent_Release(mouse_event);
279 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
280 return S_OK;
283 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
285 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
286 PRBool ret = FALSE;
288 TRACE("(%p)->(%p)\n", This, p);
290 if(This->nsevent) {
291 nsIDOMKeyEvent *key_event;
292 nsresult nsres;
294 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
295 if(NS_SUCCEEDED(nsres)) {
296 nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
297 nsIDOMKeyEvent_Release(key_event);
298 }else {
299 nsIDOMMouseEvent *mouse_event;
301 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
302 if(NS_SUCCEEDED(nsres)) {
303 nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
304 nsIDOMMouseEvent_Release(mouse_event);
309 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
310 return S_OK;
313 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
315 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
316 PRBool ret = FALSE;
318 TRACE("(%p)->(%p)\n", This, p);
320 if(This->nsevent) {
321 nsIDOMKeyEvent *key_event;
322 nsresult nsres;
324 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
325 if(NS_SUCCEEDED(nsres)) {
326 nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
327 nsIDOMKeyEvent_Release(key_event);
328 }else {
329 nsIDOMMouseEvent *mouse_event;
331 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
332 if(NS_SUCCEEDED(nsres)) {
333 nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
334 nsIDOMMouseEvent_Release(mouse_event);
339 *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
340 return S_OK;
343 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
345 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
346 FIXME("(%p)->()\n", This);
347 return E_NOTIMPL;
350 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
352 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
354 FIXME("(%p)->(%p)\n", This, p);
356 V_VT(p) = VT_EMPTY;
357 return S_OK;
360 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
362 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
363 FIXME("(%p)->(%x)\n", This, v);
364 return E_NOTIMPL;
367 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
369 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
371 FIXME("(%p)->(%p)\n", This, p);
373 *p = VARIANT_FALSE;
374 return S_OK;
377 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
379 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
381 FIXME("(%p)->(%p)\n", This, p);
383 *p = NULL;
384 return S_OK;
387 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
389 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
391 FIXME("(%p)->(%p)\n", This, p);
393 *p = NULL;
394 return S_OK;
397 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
399 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
400 FIXME("(%p)->(%d)\n", This, v);
401 return E_NOTIMPL;
404 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
406 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
407 PRUint32 key_code = 0;
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_GetKeyCode(key_event, &key_code);
418 nsIDOMKeyEvent_Release(key_event);
422 *p = key_code;
423 return S_OK;
426 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
428 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
429 PRUint16 button = 0;
431 TRACE("(%p)->(%p)\n", This, p);
433 if(This->nsevent) {
434 nsIDOMMouseEvent *mouse_event;
435 nsresult nsres;
437 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
438 if(NS_SUCCEEDED(nsres)) {
439 nsIDOMMouseEvent_GetButton(mouse_event, &button);
440 nsIDOMMouseEvent_Release(mouse_event);
444 *p = button;
445 return S_OK;
448 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
450 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
452 TRACE("(%p)->(%p)\n", This, p);
454 *p = SysAllocString(This->type->name);
455 return *p ? S_OK : E_OUTOFMEMORY;
458 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
460 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
462 FIXME("(%p)->(%p)\n", This, p);
464 *p = NULL;
465 return S_OK;
468 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
470 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
472 FIXME("(%p)->(%p)\n", This, p);
474 *p = 0;
475 return S_OK;
478 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
480 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
482 FIXME("(%p)->(%p)\n", This, p);
484 *p = -1;
485 return S_OK;
488 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
490 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
492 FIXME("(%p)->(%p)\n", This, p);
494 *p = -1;
495 return S_OK;
498 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
500 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
501 PRInt32 x = 0;
503 TRACE("(%p)->(%p)\n", This, p);
505 if(This->nsevent) {
506 nsIDOMMouseEvent *mouse_event;
507 nsresult nsres;
509 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
510 if(NS_SUCCEEDED(nsres)) {
511 nsIDOMMouseEvent_GetClientX(mouse_event, &x);
512 nsIDOMMouseEvent_Release(mouse_event);
516 *p = x;
517 return S_OK;
520 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
522 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
523 PRInt32 y = 0;
525 TRACE("(%p)->(%p)\n", This, p);
527 if(This->nsevent) {
528 nsIDOMMouseEvent *mouse_event;
529 nsresult nsres;
531 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
532 if(NS_SUCCEEDED(nsres)) {
533 nsIDOMMouseEvent_GetClientY(mouse_event, &y);
534 nsIDOMMouseEvent_Release(mouse_event);
538 *p = y;
539 return S_OK;
542 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
544 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
546 FIXME("(%p)->(%p)\n", This, p);
548 *p = 0;
549 return S_OK;
552 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
554 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
556 FIXME("(%p)->(%p)\n", This, p);
558 *p = 0;
559 return S_OK;
562 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
564 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
565 PRInt32 x = 0;
567 TRACE("(%p)->(%p)\n", This, p);
569 if(This->nsevent) {
570 nsIDOMMouseEvent *mouse_event;
571 nsresult nsres;
573 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
574 if(NS_SUCCEEDED(nsres)) {
575 nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
576 nsIDOMMouseEvent_Release(mouse_event);
580 *p = x;
581 return S_OK;
584 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
586 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
587 PRInt32 y = 0;
589 TRACE("(%p)->(%p)\n", This, p);
591 if(This->nsevent) {
592 nsIDOMMouseEvent *mouse_event;
593 nsresult nsres;
595 nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
596 if(NS_SUCCEEDED(nsres)) {
597 nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
598 nsIDOMMouseEvent_Release(mouse_event);
602 *p = y;
603 return S_OK;
606 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
608 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
610 FIXME("(%p)->(%p)\n", This, p);
612 *p = NULL;
613 return S_OK;
616 #undef HTMLEVENTOBJ_THIS
618 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
619 HTMLEventObj_QueryInterface,
620 HTMLEventObj_AddRef,
621 HTMLEventObj_Release,
622 HTMLEventObj_GetTypeInfoCount,
623 HTMLEventObj_GetTypeInfo,
624 HTMLEventObj_GetIDsOfNames,
625 HTMLEventObj_Invoke,
626 HTMLEventObj_get_srcElement,
627 HTMLEventObj_get_altKey,
628 HTMLEventObj_get_ctrlKey,
629 HTMLEventObj_get_shiftKey,
630 HTMLEventObj_put_returnValue,
631 HTMLEventObj_get_returnValue,
632 HTMLEventObj_put_cancelBubble,
633 HTMLEventObj_get_cancelBubble,
634 HTMLEventObj_get_fromElement,
635 HTMLEventObj_get_toElement,
636 HTMLEventObj_put_keyCode,
637 HTMLEventObj_get_keyCode,
638 HTMLEventObj_get_button,
639 HTMLEventObj_get_type,
640 HTMLEventObj_get_qualifier,
641 HTMLEventObj_get_reason,
642 HTMLEventObj_get_x,
643 HTMLEventObj_get_y,
644 HTMLEventObj_get_clientX,
645 HTMLEventObj_get_clientY,
646 HTMLEventObj_get_offsetX,
647 HTMLEventObj_get_offsetY,
648 HTMLEventObj_get_screenX,
649 HTMLEventObj_get_screenY,
650 HTMLEventObj_get_srcFilter
653 static const tid_t HTMLEventObj_iface_tids[] = {
654 IHTMLEventObj_tid,
658 static dispex_static_data_t HTMLEventObj_dispex = {
659 NULL,
660 DispCEventObj_tid,
661 NULL,
662 HTMLEventObj_iface_tids
665 static IHTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
667 HTMLEventObj *ret;
669 ret = heap_alloc(sizeof(*ret));
670 ret->lpIHTMLEventObjVtbl = &HTMLEventObjVtbl;
671 ret->ref = 1;
672 ret->type = event_info+eid;
673 ret->target = target;
674 IHTMLDOMNode_AddRef(HTMLDOMNODE(target));
676 ret->nsevent = nsevent;
677 if(nsevent)
678 nsIDOMEvent_AddRef(nsevent);
680 init_dispex(&ret->dispex, (IUnknown*)HTMLEVENTOBJ(ret), &HTMLEventObj_dispex);
682 return HTMLEVENTOBJ(ret);
685 static void call_event_handlers(HTMLDocument *doc, IHTMLEventObj *event_obj, event_target_t *event_target,
686 eventid_t eid, IDispatch *this_obj)
688 handler_vector_t *handler_vector;
689 DWORD i;
690 HRESULT hres;
692 if(!event_target || !(handler_vector = event_target->event_table[eid]))
693 return;
695 if(handler_vector->handler_prop) {
696 DISPID named_arg = DISPID_THIS;
697 VARIANTARG arg;
698 DISPPARAMS dp = {&arg, &named_arg, 1, 1};
700 V_VT(&arg) = VT_DISPATCH;
701 V_DISPATCH(&arg) = this_obj;
703 TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
704 hres = call_disp_func(handler_vector->handler_prop, &dp);
705 if(hres == S_OK)
706 TRACE("%s <<<\n", debugstr_w(event_info[eid].name));
707 else
708 WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
711 if(handler_vector->handler_cnt) {
712 VARIANTARG arg;
713 DISPPARAMS dp = {&arg, NULL, 1, 0};
715 V_VT(&arg) = VT_DISPATCH;
716 V_DISPATCH(&arg) = (IDispatch*)event_obj;
718 for(i=0; i < handler_vector->handler_cnt; i++) {
719 if(handler_vector->handlers[i]) {
720 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
721 hres = call_disp_func(handler_vector->handlers[i], &dp);
722 if(hres == S_OK)
723 TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
724 else
725 WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
731 void fire_event(HTMLDocument *doc, eventid_t eid, nsIDOMNode *target, nsIDOMEvent *nsevent)
733 IHTMLEventObj *prev_event, *event_obj = NULL;
734 nsIDOMNode *parent, *nsnode;
735 HTMLDOMNode *node;
736 PRUint16 node_type;
738 nsIDOMNode_GetNodeType(target, &node_type);
739 if(node_type != ELEMENT_NODE) {
740 FIXME("node type %d node supported\n", node_type);
741 return;
744 prev_event = doc->window->event;
745 event_obj = doc->window->event = create_event(get_node(doc, target, TRUE), eid, nsevent);
746 nsnode = target;
747 nsIDOMNode_AddRef(nsnode);
749 while(1) {
750 node = get_node(doc, nsnode, FALSE);
752 if(node)
753 call_event_handlers(doc, event_obj, node->event_target, eid, (IDispatch*)HTMLDOMNODE(node));
755 if(!(event_info[eid].flags & EVENT_BUBBLE))
756 break;
758 nsIDOMNode_GetParentNode(nsnode, &parent);
759 nsIDOMNode_Release(nsnode);
760 nsnode = parent;
761 if(!nsnode)
762 break;
764 nsIDOMNode_GetNodeType(nsnode, &node_type);
765 if(node_type != ELEMENT_NODE)
766 break;
769 if(nsnode)
770 nsIDOMNode_Release(nsnode);
772 if(event_info[eid].flags & EVENT_BUBBLE)
773 call_event_handlers(doc, event_obj, doc->event_target, eid, (IDispatch*)HTMLDOC(doc));
775 IHTMLEventObj_Release(event_obj);
776 doc->window->event = prev_event;
779 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
781 if(!*event_target_ptr)
782 *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
783 return *event_target_ptr;
786 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
788 handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
790 if(handler_vector) {
791 if(cnt <= handler_vector->handler_cnt)
792 return TRUE;
794 new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
795 }else {
796 new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
799 if(!new_vector)
800 return FALSE;
802 new_vector->handler_cnt = cnt;
803 event_target->event_table[eid] = new_vector;
804 return TRUE;
807 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, HTMLDocument *doc, eventid_t eid, IDispatch *disp)
809 event_target_t *event_target;
811 event_target = get_event_target(event_target_ptr);
812 if(!event_target)
813 return E_OUTOFMEMORY;
815 if(!alloc_handler_vector(event_target, eid, 0))
816 return E_OUTOFMEMORY;
818 if(event_target->event_table[eid]->handler_prop)
819 IDispatch_Release(event_target->event_table[eid]->handler_prop);
821 event_target->event_table[eid]->handler_prop = disp;
822 if(!disp)
823 return S_OK;
824 IDispatch_AddRef(disp);
826 if(doc->nscontainer && (event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
827 if(!doc->nscontainer->event_vector) {
828 doc->nscontainer->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
829 if(!doc->nscontainer->event_vector)
830 return E_OUTOFMEMORY;
833 if(!doc->nscontainer->event_vector[eid]) {
834 doc->nscontainer->event_vector[eid] = TRUE;
835 add_nsevent_listener(doc->nscontainer, event_info[eid].name);
839 return S_OK;
842 HRESULT set_event_handler(event_target_t **event_target, HTMLDocument *doc, eventid_t eid, VARIANT *var)
844 switch(V_VT(var)) {
845 case VT_NULL:
846 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
847 IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
848 (*event_target)->event_table[eid]->handler_prop = NULL;
850 break;
852 case VT_DISPATCH:
853 return set_event_handler_disp(event_target, doc, eid, V_DISPATCH(var));
855 default:
856 FIXME("not supported vt=%d\n", V_VT(var));
857 return E_NOTIMPL;
860 return S_OK;
863 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
865 if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
866 V_VT(var) = VT_DISPATCH;
867 V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
868 IDispatch_AddRef(V_DISPATCH(var));
869 }else {
870 V_VT(var) = VT_NULL;
873 return S_OK;
876 HRESULT attach_event(event_target_t **event_target_ptr, HTMLDocument *doc, BSTR name, IDispatch *disp, VARIANT_BOOL *res)
878 event_target_t *event_target;
879 eventid_t eid;
880 DWORD i = 0;
882 eid = attr_to_eid(name);
883 if(eid == EVENTID_LAST) {
884 WARN("Unknown event\n");
885 *res = VARIANT_TRUE;
886 return S_OK;
889 event_target = get_event_target(event_target_ptr);
890 if(!event_target)
891 return E_OUTOFMEMORY;
893 if(event_target->event_table[eid]) {
894 while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
895 i++;
896 if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
897 return E_OUTOFMEMORY;
898 }else if(!alloc_handler_vector(event_target, eid, i+1)) {
899 return E_OUTOFMEMORY;
902 IDispatch_AddRef(disp);
903 event_target->event_table[eid]->handlers[i] = disp;
905 *res = VARIANT_TRUE;
906 return S_OK;
909 void check_event_attr(HTMLDocument *doc, nsIDOMElement *nselem)
911 const PRUnichar *attr_value;
912 nsAString attr_name_str, attr_value_str;
913 IDispatch *disp;
914 HTMLDOMNode *node;
915 int i;
916 nsresult nsres;
918 nsAString_Init(&attr_value_str, NULL);
919 nsAString_Init(&attr_name_str, NULL);
921 for(i=0; i < EVENTID_LAST; i++) {
922 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
923 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
924 if(NS_SUCCEEDED(nsres)) {
925 nsAString_GetData(&attr_value_str, &attr_value);
926 if(!*attr_value)
927 continue;
929 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
931 disp = script_parse_event(doc, attr_value);
932 if(disp) {
933 node = get_node(doc, (nsIDOMNode*)nselem, TRUE);
934 set_event_handler_disp(&node->event_target, node->doc, i, disp);
935 IDispatch_Release(disp);
940 nsAString_Finish(&attr_value_str);
941 nsAString_Finish(&attr_name_str);
944 void release_event_target(event_target_t *event_target)
946 int i, j;
948 for(i=0; i < EVENTID_LAST; i++) {
949 if(event_target->event_table[i]) {
950 if(event_target->event_table[i]->handler_prop)
951 IDispatch_Release(event_target->event_table[i]->handler_prop);
952 for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
953 IDispatch_Release(event_target->event_table[i]->handlers[j]);
957 heap_free(event_target);