opengl32/tests: Do not pass NULL attrib list to wglCreatePBufferARB.
[wine] / dlls / mshtml / htmlevent.c
1 /*
2  * Copyright 2008-2009 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "mshtmdid.h"
28
29 #include "mshtml_private.h"
30 #include "htmlevent.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35
36 typedef struct {
37     IDispatch *handler_prop;
38     DWORD handler_cnt;
39     IDispatch *handlers[0];
40 } handler_vector_t;
41
42 struct event_target_t {
43     DWORD node_handlers_mask;
44     handler_vector_t *event_table[EVENTID_LAST];
45 };
46
47 static const WCHAR beforeunloadW[] = {'b','e','f','o','r','e','u','n','l','o','a','d',0};
48 static const WCHAR onbeforeunloadW[] = {'o','n','b','e','f','o','r','e','u','n','l','o','a','d',0};
49
50 static const WCHAR blurW[] = {'b','l','u','r',0};
51 static const WCHAR onblurW[] = {'o','n','b','l','u','r',0};
52
53 static const WCHAR changeW[] = {'c','h','a','n','g','e',0};
54 static const WCHAR onchangeW[] = {'o','n','c','h','a','n','g','e',0};
55
56 static const WCHAR clickW[] = {'c','l','i','c','k',0};
57 static const WCHAR onclickW[] = {'o','n','c','l','i','c','k',0};
58
59 static const WCHAR contextmenuW[] = {'c','o','n','t','e','x','t','m','e','n','u',0};
60 static const WCHAR oncontextmenuW[] = {'o','n','c','o','n','t','e','x','t','m','e','n','u',0};
61
62 static const WCHAR dblclickW[] = {'d','b','l','c','l','i','c','k',0};
63 static const WCHAR ondblclickW[] = {'o','n','d','b','l','c','l','i','c','k',0};
64
65 static const WCHAR dragW[] = {'d','r','a','g',0};
66 static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};
67
68 static const WCHAR dragstartW[] = {'d','r','a','g','s','t','a','r','t',0};
69 static const WCHAR ondragstartW[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
70
71 static const WCHAR errorW[] = {'e','r','r','o','r',0};
72 static const WCHAR onerrorW[] = {'o','n','e','r','r','o','r',0};
73
74 static const WCHAR focusW[] = {'f','o','c','u','s',0};
75 static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};
76
77 static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
78 static const WCHAR onkeydownW[] = {'o','n','k','e','y','d','o','w','n',0};
79
80 static const WCHAR keypressW[] = {'k','e','y','p','r','e','s','s',0};
81 static const WCHAR onkeypressW[] = {'o','n','k','e','y','p','r','e','s','s',0};
82
83 static const WCHAR keyupW[] = {'k','e','y','u','p',0};
84 static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};
85
86 static const WCHAR loadW[] = {'l','o','a','d',0};
87 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
88
89 static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
90 static const WCHAR onmousedownW[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
91
92 static const WCHAR mousemoveW[] = {'m','o','u','s','e','m','o','v','e',0};
93 static const WCHAR onmousemoveW[] = {'o','n','m','o','u','s','e','m','o','v','e',0};
94
95 static const WCHAR mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
96 static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};
97
98 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
99 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
100
101 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
102 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
103
104 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
105 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
106
107 static const WCHAR readystatechangeW[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
108 static const WCHAR onreadystatechangeW[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
109
110 static const WCHAR resizeW[] = {'r','e','s','i','z','e',0};
111 static const WCHAR onresizeW[] = {'o','n','r','e','s','i','z','e',0};
112
113 static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
114 static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
115
116 static const WCHAR submitW[] = {'s','u','b','m','i','t',0};
117 static const WCHAR onsubmitW[] = {'o','n','s','u','b','m','i','t',0};
118
119 static const WCHAR HTMLEventsW[] = {'H','T','M','L','E','v','e','n','t','s',0};
120 static const WCHAR KeyboardEventW[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
121 static const WCHAR MouseEventW[] = {'M','o','u','s','e','E','v','e','n','t',0};
122
123 enum {
124     EVENTT_NONE,
125     EVENTT_HTML,
126     EVENTT_KEY,
127     EVENTT_MOUSE
128 };
129
130 static const WCHAR *event_types[] = {
131     NULL,
132     HTMLEventsW,
133     KeyboardEventW,
134     MouseEventW
135 };
136
137 typedef struct {
138     LPCWSTR name;
139     LPCWSTR attr_name;
140     DWORD type;
141     DISPID dispid;
142     DWORD flags;
143 } event_info_t;
144
145 #define EVENT_DEFAULTLISTENER    0x0001
146 #define EVENT_BUBBLE             0x0002
147 #define EVENT_FORWARDBODY        0x0004
148 #define EVENT_NODEHANDLER        0x0008
149 #define EVENT_CANCELABLE         0x0010
150 #define EVENT_HASDEFAULTHANDLERS 0x0020
151
152 static const event_info_t event_info[] = {
153     {beforeunloadW,      onbeforeunloadW,      EVENTT_NONE,   DISPID_EVMETH_ONBEFOREUNLOAD,
154         EVENT_DEFAULTLISTENER|EVENT_FORWARDBODY},
155     {blurW,              onblurW,              EVENTT_HTML,   DISPID_EVMETH_ONBLUR,
156         EVENT_DEFAULTLISTENER},
157     {changeW,            onchangeW,            EVENTT_HTML,   DISPID_EVMETH_ONCHANGE,
158         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
159     {clickW,             onclickW,             EVENTT_MOUSE,  DISPID_EVMETH_ONCLICK,
160         EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE|EVENT_HASDEFAULTHANDLERS},
161     {contextmenuW,       oncontextmenuW,       EVENTT_MOUSE,  DISPID_EVMETH_ONCONTEXTMENU,
162         EVENT_BUBBLE|EVENT_CANCELABLE},
163     {dblclickW,          ondblclickW,          EVENTT_MOUSE,  DISPID_EVMETH_ONDBLCLICK,
164         EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE},
165     {dragW,              ondragW,              EVENTT_MOUSE,  DISPID_EVMETH_ONDRAG,
166         EVENT_CANCELABLE},
167     {dragstartW,         ondragstartW,         EVENTT_MOUSE,  DISPID_EVMETH_ONDRAGSTART,
168         EVENT_CANCELABLE},
169     {errorW,             onerrorW,             EVENTT_NONE,   DISPID_EVMETH_ONERROR,
170         EVENT_NODEHANDLER},
171     {focusW,             onfocusW,             EVENTT_HTML,   DISPID_EVMETH_ONFOCUS,
172         EVENT_DEFAULTLISTENER},
173     {keydownW,           onkeydownW,           EVENTT_KEY,    DISPID_EVMETH_ONKEYDOWN,
174         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
175     {keypressW,          onkeypressW,          EVENTT_KEY,    DISPID_EVMETH_ONKEYPRESS,
176         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
177     {keyupW,             onkeyupW,             EVENTT_KEY,    DISPID_EVMETH_ONKEYUP,
178         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
179     {loadW,              onloadW,              EVENTT_HTML,   DISPID_EVMETH_ONLOAD,
180         EVENT_NODEHANDLER},
181     {mousedownW,         onmousedownW,         EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEDOWN,
182         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
183     {mousemoveW,         onmousemoveW,         EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEMOVE,
184         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
185     {mouseoutW,          onmouseoutW,          EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEOUT,
186         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
187     {mouseoverW,         onmouseoverW,         EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEOVER,
188         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
189     {mouseupW,           onmouseupW,           EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEUP,
190         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
191     {pasteW,             onpasteW,             EVENTT_NONE,   DISPID_EVMETH_ONPASTE,
192         EVENT_CANCELABLE},
193     {readystatechangeW,  onreadystatechangeW,  EVENTT_NONE,   DISPID_EVMETH_ONREADYSTATECHANGE,
194         0},
195     {resizeW,            onresizeW,            EVENTT_NONE,   DISPID_EVMETH_ONRESIZE,
196         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
197     {selectstartW,       onselectstartW,       EVENTT_MOUSE,  DISPID_EVMETH_ONSELECTSTART,
198         EVENT_CANCELABLE},
199     {submitW,            onsubmitW,            EVENTT_HTML,   DISPID_EVMETH_ONSUBMIT,
200         EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE}
201 };
202
203 static const eventid_t node_handled_list[] = { EVENTID_ERROR, EVENTID_LOAD };
204
205 eventid_t str_to_eid(LPCWSTR str)
206 {
207     int i;
208
209     for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
210         if(!strcmpW(event_info[i].name, str))
211             return i;
212     }
213
214     ERR("unknown type %s\n", debugstr_w(str));
215     return EVENTID_LAST;
216 }
217
218 static eventid_t attr_to_eid(LPCWSTR str)
219 {
220     int i;
221
222     for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
223         if(!strcmpW(event_info[i].attr_name, str))
224             return i;
225     }
226
227     return EVENTID_LAST;
228 }
229
230 static DWORD get_node_handler_mask(eventid_t eid)
231 {
232     DWORD i;
233
234     for(i=0; i<sizeof(node_handled_list)/sizeof(*node_handled_list); i++) {
235         if(node_handled_list[i] == eid)
236             return 1 << i;
237     }
238
239     ERR("Invalid eid %d\n", eid);
240     return ~0;
241 }
242
243 typedef struct {
244     DispatchEx dispex;
245     IHTMLEventObj IHTMLEventObj_iface;
246
247     LONG ref;
248
249     HTMLDOMNode *target;
250     const event_info_t *type;
251     nsIDOMEvent *nsevent;
252     BOOL prevent_default;
253     BOOL cancel_bubble;
254 } HTMLEventObj;
255
256 static inline HTMLEventObj *impl_from_IHTMLEventObj(IHTMLEventObj *iface)
257 {
258     return CONTAINING_RECORD(iface, HTMLEventObj, IHTMLEventObj_iface);
259 }
260
261 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
262 {
263     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
264
265     *ppv = NULL;
266
267     if(IsEqualGUID(&IID_IUnknown, riid)) {
268         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
269         *ppv = &This->IHTMLEventObj_iface;
270     }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
271         TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
272         *ppv = &This->IHTMLEventObj_iface;
273     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
274         return *ppv ? S_OK : E_NOINTERFACE;
275     }
276
277     if(*ppv) {
278         IUnknown_AddRef((IUnknown*)*ppv);
279         return S_OK;
280     }
281
282     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
283     return E_NOINTERFACE;
284 }
285
286 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
287 {
288     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
289     LONG ref = InterlockedIncrement(&This->ref);
290
291     TRACE("(%p) ref=%d\n", This, ref);
292
293     return ref;
294 }
295
296 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
297 {
298     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
299     LONG ref = InterlockedDecrement(&This->ref);
300
301     TRACE("(%p) ref=%d\n", This, ref);
302
303     if(!ref) {
304         if(This->target)
305             IHTMLDOMNode_Release(&This->target->IHTMLDOMNode_iface);
306         if(This->nsevent)
307             nsIDOMEvent_Release(This->nsevent);
308         release_dispex(&This->dispex);
309         heap_free(This);
310     }
311
312     return ref;
313 }
314
315 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
316 {
317     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
318     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
319 }
320
321 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
322                                               LCID lcid, ITypeInfo **ppTInfo)
323 {
324     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
325     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
326 }
327
328 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
329                                                 LPOLESTR *rgszNames, UINT cNames,
330                                                 LCID lcid, DISPID *rgDispId)
331 {
332     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
333     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
334             lcid, rgDispId);
335 }
336
337 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
338                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
339                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
340 {
341     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
342     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
343             wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
344 }
345
346 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
347 {
348     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
349
350     TRACE("(%p)->(%p)\n", This, p);
351
352     return IHTMLDOMNode_QueryInterface(&This->target->IHTMLDOMNode_iface, &IID_IHTMLElement,
353             (void**)p);
354 }
355
356 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
357 {
358     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
359     cpp_bool ret = FALSE;
360
361     TRACE("(%p)->(%p)\n", This, p);
362
363     if(This->nsevent) {
364         nsIDOMKeyEvent *key_event;
365         nsresult nsres;
366
367         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
368         if(NS_SUCCEEDED(nsres)) {
369             nsIDOMKeyEvent_GetAltKey(key_event, &ret);
370             nsIDOMKeyEvent_Release(key_event);
371         }else {
372             nsIDOMMouseEvent *mouse_event;
373
374             nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
375             if(NS_SUCCEEDED(nsres)) {
376                 nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
377                 nsIDOMMouseEvent_Release(mouse_event);
378             }
379         }
380     }
381
382     *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
383     return S_OK;
384 }
385
386 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
387 {
388     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
389     cpp_bool ret = FALSE;
390
391     TRACE("(%p)->(%p)\n", This, p);
392
393     if(This->nsevent) {
394         nsIDOMKeyEvent *key_event;
395         nsresult nsres;
396
397         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
398         if(NS_SUCCEEDED(nsres)) {
399             nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
400             nsIDOMKeyEvent_Release(key_event);
401         }else {
402             nsIDOMMouseEvent *mouse_event;
403
404             nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
405             if(NS_SUCCEEDED(nsres)) {
406                 nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
407                 nsIDOMMouseEvent_Release(mouse_event);
408             }
409         }
410     }
411
412     *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
413     return S_OK;
414 }
415
416 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
417 {
418     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
419     cpp_bool ret = FALSE;
420
421     TRACE("(%p)->(%p)\n", This, p);
422
423     if(This->nsevent) {
424         nsIDOMKeyEvent *key_event;
425         nsresult nsres;
426
427         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
428         if(NS_SUCCEEDED(nsres)) {
429             nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
430             nsIDOMKeyEvent_Release(key_event);
431         }else {
432             nsIDOMMouseEvent *mouse_event;
433
434             nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
435             if(NS_SUCCEEDED(nsres)) {
436                 nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
437                 nsIDOMMouseEvent_Release(mouse_event);
438             }
439         }
440     }
441
442     *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
443     return S_OK;
444 }
445
446 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
447 {
448     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
449
450     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
451
452     if(V_VT(&v) != VT_BOOL) {
453         FIXME("unsupported value %s\n", debugstr_variant(&v));
454         return DISP_E_BADVARTYPE;
455     }
456
457     if(!V_BOOL(&v))
458         This->prevent_default = TRUE;
459     return S_OK;
460 }
461
462 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
463 {
464     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
465
466     FIXME("(%p)->(%p)\n", This, p);
467
468     V_VT(p) = VT_EMPTY;
469     return S_OK;
470 }
471
472 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
473 {
474     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
475
476     TRACE("(%p)->(%x)\n", This, v);
477
478     This->cancel_bubble = !!v;
479     return S_OK;
480 }
481
482 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
483 {
484     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
485
486     TRACE("(%p)->(%p)\n", This, p);
487
488     *p = This->cancel_bubble ? VARIANT_TRUE : VARIANT_FALSE;
489     return S_OK;
490 }
491
492 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
493 {
494     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
495
496     FIXME("(%p)->(%p)\n", This, p);
497
498     *p = NULL;
499     return S_OK;
500 }
501
502 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
503 {
504     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
505
506     FIXME("(%p)->(%p)\n", This, p);
507
508     *p = NULL;
509     return S_OK;
510 }
511
512 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
513 {
514     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
515     FIXME("(%p)->(%d)\n", This, v);
516     return E_NOTIMPL;
517 }
518
519 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
520 {
521     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
522     PRUint32 key_code = 0;
523
524     TRACE("(%p)->(%p)\n", This, p);
525
526     if(This->nsevent) {
527         nsIDOMKeyEvent *key_event;
528         nsresult nsres;
529
530         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
531         if(NS_SUCCEEDED(nsres)) {
532             nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
533             nsIDOMKeyEvent_Release(key_event);
534         }
535     }
536
537     *p = key_code;
538     return S_OK;
539 }
540
541 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
542 {
543     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
544     PRUint16 button = 0;
545
546     TRACE("(%p)->(%p)\n", This, p);
547
548     if(This->nsevent) {
549         nsIDOMMouseEvent *mouse_event;
550         nsresult nsres;
551
552         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
553         if(NS_SUCCEEDED(nsres)) {
554             nsIDOMMouseEvent_GetButton(mouse_event, &button);
555             nsIDOMMouseEvent_Release(mouse_event);
556         }
557     }
558
559     *p = button;
560     return S_OK;
561 }
562
563 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
564 {
565     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
566
567     TRACE("(%p)->(%p)\n", This, p);
568
569     *p = SysAllocString(This->type->name);
570     return *p ? S_OK : E_OUTOFMEMORY;
571 }
572
573 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
574 {
575     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
576
577     FIXME("(%p)->(%p)\n", This, p);
578
579     *p = NULL;
580     return S_OK;
581 }
582
583 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
584 {
585     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
586
587     FIXME("(%p)->(%p)\n", This, p);
588
589     *p = 0;
590     return S_OK;
591 }
592
593 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
594 {
595     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
596
597     FIXME("(%p)->(%p)\n", This, p);
598
599     *p = -1;
600     return S_OK;
601 }
602
603 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
604 {
605     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
606
607     FIXME("(%p)->(%p)\n", This, p);
608
609     *p = -1;
610     return S_OK;
611 }
612
613 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
614 {
615     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
616     PRInt32 x = 0;
617
618     TRACE("(%p)->(%p)\n", This, p);
619
620     if(This->nsevent) {
621         nsIDOMMouseEvent *mouse_event;
622         nsresult nsres;
623
624         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
625         if(NS_SUCCEEDED(nsres)) {
626             nsIDOMMouseEvent_GetClientX(mouse_event, &x);
627             nsIDOMMouseEvent_Release(mouse_event);
628         }
629     }
630
631     *p = x;
632     return S_OK;
633 }
634
635 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
636 {
637     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
638     PRInt32 y = 0;
639
640     TRACE("(%p)->(%p)\n", This, p);
641
642     if(This->nsevent) {
643         nsIDOMMouseEvent *mouse_event;
644         nsresult nsres;
645
646         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
647         if(NS_SUCCEEDED(nsres)) {
648             nsIDOMMouseEvent_GetClientY(mouse_event, &y);
649             nsIDOMMouseEvent_Release(mouse_event);
650         }
651     }
652
653     *p = y;
654     return S_OK;
655 }
656
657 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
658 {
659     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
660
661     FIXME("(%p)->(%p)\n", This, p);
662
663     *p = 0;
664     return S_OK;
665 }
666
667 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
668 {
669     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
670
671     FIXME("(%p)->(%p)\n", This, p);
672
673     *p = 0;
674     return S_OK;
675 }
676
677 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
678 {
679     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
680     PRInt32 x = 0;
681
682     TRACE("(%p)->(%p)\n", This, p);
683
684     if(This->nsevent) {
685         nsIDOMMouseEvent *mouse_event;
686         nsresult nsres;
687
688         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
689         if(NS_SUCCEEDED(nsres)) {
690             nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
691             nsIDOMMouseEvent_Release(mouse_event);
692         }
693     }
694
695     *p = x;
696     return S_OK;
697 }
698
699 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
700 {
701     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
702     PRInt32 y = 0;
703
704     TRACE("(%p)->(%p)\n", This, p);
705
706     if(This->nsevent) {
707         nsIDOMMouseEvent *mouse_event;
708         nsresult nsres;
709
710         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
711         if(NS_SUCCEEDED(nsres)) {
712             nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
713             nsIDOMMouseEvent_Release(mouse_event);
714         }
715     }
716
717     *p = y;
718     return S_OK;
719 }
720
721 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
722 {
723     HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
724
725     FIXME("(%p)->(%p)\n", This, p);
726
727     *p = NULL;
728     return S_OK;
729 }
730
731 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
732     HTMLEventObj_QueryInterface,
733     HTMLEventObj_AddRef,
734     HTMLEventObj_Release,
735     HTMLEventObj_GetTypeInfoCount,
736     HTMLEventObj_GetTypeInfo,
737     HTMLEventObj_GetIDsOfNames,
738     HTMLEventObj_Invoke,
739     HTMLEventObj_get_srcElement,
740     HTMLEventObj_get_altKey,
741     HTMLEventObj_get_ctrlKey,
742     HTMLEventObj_get_shiftKey,
743     HTMLEventObj_put_returnValue,
744     HTMLEventObj_get_returnValue,
745     HTMLEventObj_put_cancelBubble,
746     HTMLEventObj_get_cancelBubble,
747     HTMLEventObj_get_fromElement,
748     HTMLEventObj_get_toElement,
749     HTMLEventObj_put_keyCode,
750     HTMLEventObj_get_keyCode,
751     HTMLEventObj_get_button,
752     HTMLEventObj_get_type,
753     HTMLEventObj_get_qualifier,
754     HTMLEventObj_get_reason,
755     HTMLEventObj_get_x,
756     HTMLEventObj_get_y,
757     HTMLEventObj_get_clientX,
758     HTMLEventObj_get_clientY,
759     HTMLEventObj_get_offsetX,
760     HTMLEventObj_get_offsetY,
761     HTMLEventObj_get_screenX,
762     HTMLEventObj_get_screenY,
763     HTMLEventObj_get_srcFilter
764 };
765
766 static const tid_t HTMLEventObj_iface_tids[] = {
767     IHTMLEventObj_tid,
768     0
769 };
770
771 static dispex_static_data_t HTMLEventObj_dispex = {
772     NULL,
773     DispCEventObj_tid,
774     NULL,
775     HTMLEventObj_iface_tids
776 };
777
778 static HTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
779 {
780     HTMLEventObj *ret;
781
782     ret = heap_alloc_zero(sizeof(*ret));
783     if(!ret)
784         return NULL;
785
786     ret->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
787     ret->ref = 1;
788     ret->type = event_info+eid;
789
790     ret->nsevent = nsevent;
791     if(nsevent) {
792         nsIDOMEvent_AddRef(nsevent);
793     }else if(event_types[event_info[eid].type]) {
794         nsAString type_str;
795         nsresult nsres;
796
797         nsAString_InitDepend(&type_str, event_types[event_info[eid].type]);
798         nsres = nsIDOMHTMLDocument_CreateEvent(target->doc->nsdoc, &type_str, &ret->nsevent);
799         nsAString_Finish(&type_str);
800         if(NS_FAILED(nsres)) {
801             ERR("Could not create event: %08x\n", nsres);
802             IHTMLEventObj_Release(&ret->IHTMLEventObj_iface);
803             return NULL;
804         }
805     }
806
807     ret->target = target;
808     IHTMLDOMNode_AddRef(&target->IHTMLDOMNode_iface);
809
810     init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLEventObj_iface, &HTMLEventObj_dispex);
811
812     return ret;
813 }
814
815 static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
816 {
817     IDispatchEx *dispex;
818     EXCEPINFO ei;
819     HRESULT hres;
820
821     memset(&ei, 0, sizeof(ei));
822
823     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
824     if(SUCCEEDED(hres)) {
825         hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, retv, &ei, NULL);
826         IDispatchEx_Release(dispex);
827     }else {
828         TRACE("Could not get IDispatchEx interface: %08x\n", hres);
829         hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
830                 dp, retv, &ei, NULL);
831     }
832
833     return hres;
834 }
835
836 static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, VARIANT *retv)
837 {
838     DISPPARAMS dp = {NULL,NULL,0,0};
839     ULONG argerr;
840     EXCEPINFO ei;
841
842     memset(&ei, 0, sizeof(ei));
843     return IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, retv, &ei, &argerr);
844 }
845
846 static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
847 {
848     int min, max, i;
849     HRESULT hres;
850
851     if(!data)
852         return FALSE;
853
854     if(!data->ids) {
855         hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
856         if(FAILED(hres))
857             return FALSE;
858     }
859
860     min = 0;
861     max = data->id_cnt-1;
862     while(min <= max) {
863         i = (min+max)/2;
864         if(data->ids[i] == dispid)
865             return TRUE;
866
867         if(data->ids[i] < dispid)
868             min = i+1;
869         else
870             max = i-1;
871     }
872
873     return FALSE;
874 }
875
876 static void call_event_handlers(HTMLDocumentNode *doc, HTMLEventObj *event_obj, event_target_t *event_target,
877         ConnectionPointContainer *cp_container, eventid_t eid, IDispatch *this_obj)
878 {
879     const BOOL cancelable = event_info[eid].flags & EVENT_CANCELABLE;
880     handler_vector_t *handler_vector = NULL;
881     VARIANT v;
882     int i;
883     HRESULT hres;
884
885     if(event_target)
886         handler_vector = event_target->event_table[eid];
887
888     if(handler_vector && handler_vector->handler_prop) {
889         DISPID named_arg = DISPID_THIS;
890         VARIANTARG arg;
891         DISPPARAMS dp = {&arg, &named_arg, 1, 1};
892
893         V_VT(&arg) = VT_DISPATCH;
894         V_DISPATCH(&arg) = this_obj;
895         V_VT(&v) = VT_EMPTY;
896
897         TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
898         hres = call_disp_func(handler_vector->handler_prop, &dp, &v);
899         if(hres == S_OK) {
900             TRACE("%s <<< %s\n", debugstr_w(event_info[eid].name), debugstr_variant(&v));
901
902             if(cancelable) {
903                 if(V_VT(&v) == VT_BOOL) {
904                     if(!V_BOOL(&v))
905                         event_obj->prevent_default = TRUE;
906                 }else if(V_VT(&v) != VT_EMPTY) {
907                     FIXME("unhandled result %s\n", debugstr_variant(&v));
908                 }
909             }
910             VariantClear(&v);
911         }else {
912             WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
913         }
914     }
915
916     if(handler_vector && handler_vector->handler_cnt) {
917         VARIANTARG arg;
918         DISPPARAMS dp = {&arg, NULL, 1, 0};
919
920         V_VT(&arg) = VT_DISPATCH;
921         V_DISPATCH(&arg) = (IDispatch*)event_obj;
922
923         i = handler_vector->handler_cnt;
924         while(i--) {
925             if(handler_vector->handlers[i]) {
926                 V_VT(&v) = VT_EMPTY;
927
928                 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
929                 hres = call_disp_func(handler_vector->handlers[i], &dp, &v);
930                 if(hres == S_OK) {
931                     TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
932
933                     if(cancelable) {
934                         if(V_VT(&v) == VT_BOOL) {
935                             if(!V_BOOL(&v))
936                                 event_obj->prevent_default = TRUE;
937                         }else if(V_VT(&v) != VT_EMPTY) {
938                             FIXME("unhandled result %s\n", debugstr_variant(&v));
939                         }
940                     }
941                     VariantClear(&v);
942                 }else {
943                     WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
944                 }
945             }
946         }
947     }
948
949     if(cp_container) {
950         ConnectionPoint *cp;
951
952         if(cp_container->forward_container)
953             cp_container = cp_container->forward_container;
954
955         for(cp = cp_container->cp_list; cp; cp = cp->next) {
956             if(cp->sinks_size && is_cp_event(cp->data, event_info[eid].dispid)) {
957                 for(i=0; i < cp->sinks_size; i++) {
958                     if(!cp->sinks[i].disp)
959                         continue;
960
961                     V_VT(&v) = VT_EMPTY;
962
963                     TRACE("cp %s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
964                     hres = call_cp_func(cp->sinks[i].disp, event_info[eid].dispid, &v);
965                     if(hres == S_OK) {
966                         TRACE("cp %s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
967
968                         if(cancelable) {
969                             if(V_VT(&v) == VT_BOOL) {
970                                 if(!V_BOOL(&v))
971                                     event_obj->prevent_default = TRUE;
972                             }else if(V_VT(&v) != VT_EMPTY) {
973                                 FIXME("unhandled result %s\n", debugstr_variant(&v));
974                             }
975                         }
976                         VariantClear(&v);
977                     }else {
978                         WARN("cp %s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
979                     }
980                 }
981             }
982         }
983     }
984 }
985
986 void fire_event(HTMLDocumentNode *doc, eventid_t eid, BOOL set_event, nsIDOMNode *target, nsIDOMEvent *nsevent)
987 {
988     HTMLEventObj *event_obj = NULL;
989     IHTMLEventObj *prev_event;
990     nsIDOMNode *parent, *nsnode;
991     BOOL prevent_default = FALSE;
992     HTMLInnerWindow *window;
993     HTMLDOMNode *node;
994     PRUint16 node_type;
995     nsresult nsres;
996     HRESULT hres;
997
998     TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
999
1000     window = doc->window;
1001     prev_event = window->event;
1002     if(set_event) {
1003         hres = get_node(doc, target, TRUE, &node);
1004         if(FAILED(hres))
1005             return;
1006
1007         event_obj = create_event(node, eid, nsevent);
1008         node_release(node);
1009         window->event = &event_obj->IHTMLEventObj_iface;
1010     }else {
1011         window->event = NULL;
1012     }
1013
1014     nsIDOMNode_GetNodeType(target, &node_type);
1015     nsnode = target;
1016     nsIDOMNode_AddRef(nsnode);
1017
1018     switch(node_type) {
1019     case ELEMENT_NODE:
1020         do {
1021             hres = get_node(doc, nsnode, FALSE, &node);
1022             if(SUCCEEDED(hres) && node) {
1023                 call_event_handlers(doc, event_obj, *get_node_event_target(node),
1024                         node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1025                 node_release(node);
1026             }
1027
1028             if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1029                 break;
1030
1031             nsIDOMNode_GetParentNode(nsnode, &parent);
1032             nsIDOMNode_Release(nsnode);
1033             nsnode = parent;
1034             if(!nsnode)
1035                 break;
1036
1037             nsIDOMNode_GetNodeType(nsnode, &node_type);
1038         }while(node_type == ELEMENT_NODE);
1039
1040         if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1041             break;
1042
1043     case DOCUMENT_NODE:
1044         if(event_info[eid].flags & EVENT_FORWARDBODY) {
1045             nsIDOMHTMLElement *nsbody;
1046             nsresult nsres;
1047
1048             nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
1049             if(NS_SUCCEEDED(nsres) && nsbody) {
1050                 hres = get_node(doc, (nsIDOMNode*)nsbody, FALSE, &node);
1051                 if(SUCCEEDED(hres) && node) {
1052                     call_event_handlers(doc, event_obj, *get_node_event_target(node),
1053                             node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1054                     node_release(node);
1055                 }
1056                 nsIDOMHTMLElement_Release(nsbody);
1057             }else {
1058                 ERR("Could not get body: %08x\n", nsres);
1059             }
1060         }
1061
1062         call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
1063                 (IDispatch*)&doc->basedoc.IHTMLDocument2_iface);
1064         break;
1065
1066     default:
1067         FIXME("unimplemented node type %d\n", node_type);
1068     }
1069
1070     if(nsnode)
1071         nsIDOMNode_Release(nsnode);
1072
1073     if(event_obj && event_obj->prevent_default)
1074         prevent_default = TRUE;
1075     window->event = prev_event;
1076     if(event_obj)
1077         IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1078
1079     if(!prevent_default && (event_info[eid].flags & EVENT_HASDEFAULTHANDLERS)) {
1080         nsIDOMNode_AddRef(target);
1081         nsnode = target;
1082
1083         do {
1084             hres = get_node(doc, nsnode, TRUE, &node);
1085             if(FAILED(hres))
1086                 break;
1087
1088             if(node) {
1089                 if(node->vtbl->handle_event)
1090                     hres = node->vtbl->handle_event(node, eid, nsevent, &prevent_default);
1091                 node_release(node);
1092                 if(FAILED(hres) || prevent_default || (event_obj && event_obj->cancel_bubble))
1093                     break;
1094             }
1095
1096             nsres = nsIDOMNode_GetParentNode(nsnode, &parent);
1097             if(NS_FAILED(nsres))
1098                 break;
1099
1100             nsIDOMNode_Release(nsnode);
1101             nsnode = parent;
1102         } while(nsnode);
1103
1104         if(nsnode)
1105             nsIDOMNode_Release(nsnode);
1106     }
1107
1108     if(prevent_default && nsevent) {
1109         TRACE("calling PreventDefault\n");
1110         nsIDOMEvent_PreventDefault(nsevent);
1111     }
1112 }
1113
1114 HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_obj, VARIANT_BOOL *cancelled)
1115 {
1116     eventid_t eid;
1117
1118     eid = attr_to_eid(event_name);
1119     if(eid == EVENTID_LAST) {
1120         WARN("unknown event %s\n", debugstr_w(event_name));
1121         return E_INVALIDARG;
1122     }
1123
1124     if(event_obj && V_VT(event_obj) != VT_EMPTY && V_VT(event_obj) != VT_ERROR)
1125         FIXME("event_obj not implemented\n");
1126
1127     if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
1128         FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
1129         return E_NOTIMPL;
1130     }
1131
1132     fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1133
1134     *cancelled = VARIANT_TRUE; /* FIXME */
1135     return S_OK;
1136 }
1137
1138 HRESULT call_fire_event(HTMLDOMNode *node, eventid_t eid)
1139 {
1140     HRESULT hres;
1141
1142     if(node->vtbl->fire_event) {
1143         BOOL handled = FALSE;
1144
1145         hres = node->vtbl->fire_event(node, eid, &handled);
1146         if(handled)
1147             return hres;
1148     }
1149
1150     fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1151     return S_OK;
1152 }
1153
1154 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
1155 {
1156     if(!*event_target_ptr)
1157         *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
1158     return *event_target_ptr;
1159 }
1160
1161 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
1162 {
1163     handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
1164
1165     if(handler_vector) {
1166         if(cnt <= handler_vector->handler_cnt)
1167             return TRUE;
1168
1169         new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1170     }else {
1171         new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
1172     }
1173
1174     if(!new_vector)
1175         return FALSE;
1176
1177     new_vector->handler_cnt = cnt;
1178     event_target->event_table[eid] = new_vector;
1179     return TRUE;
1180 }
1181
1182 static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1183 {
1184     if(!doc->nsdoc)
1185         return S_OK;
1186
1187     if(event_info[eid].flags & EVENT_NODEHANDLER) {
1188         DWORD mask;
1189
1190         mask = get_node_handler_mask(eid);
1191         if(event_target->node_handlers_mask & mask)
1192             return S_OK;
1193
1194         add_nsevent_listener(doc, nsnode, event_info[eid].name);
1195         event_target->node_handlers_mask |= mask;
1196         return S_OK;
1197     }
1198
1199     if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER))
1200         return S_OK;
1201
1202     if(!doc->event_vector[eid]) {
1203         doc->event_vector[eid] = TRUE;
1204         add_nsevent_listener(doc, NULL, event_info[eid].name);
1205     }
1206
1207     return S_OK;
1208 }
1209
1210 void detach_events(HTMLDocumentNode *doc)
1211 {
1212     if(doc->event_vector) {
1213         int i;
1214
1215         for(i=0; i < EVENTID_LAST; i++) {
1216             if(doc->event_vector[i])
1217                 detach_nsevent(doc, event_info[i].name);
1218         }
1219     }
1220
1221     release_nsevents(doc);
1222 }
1223
1224
1225 static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
1226 {
1227     if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1228         IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
1229         (*event_target)->event_table[eid]->handler_prop = NULL;
1230     }
1231
1232     return S_OK;
1233 }
1234
1235 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocumentNode *doc,
1236         eventid_t eid, IDispatch *disp)
1237 {
1238     event_target_t *event_target;
1239
1240     if(!disp)
1241         return remove_event_handler(event_target_ptr, eid);
1242
1243     event_target = get_event_target(event_target_ptr);
1244     if(!event_target)
1245         return E_OUTOFMEMORY;
1246
1247     if(!alloc_handler_vector(event_target, eid, 0))
1248         return E_OUTOFMEMORY;
1249
1250     if(event_target->event_table[eid]->handler_prop)
1251         IDispatch_Release(event_target->event_table[eid]->handler_prop);
1252
1253     event_target->event_table[eid]->handler_prop = disp;
1254     IDispatch_AddRef(disp);
1255
1256     return ensure_nsevent_handler(doc, event_target, nsnode, eid);
1257 }
1258
1259 HRESULT set_event_handler(event_target_t **event_target, nsIDOMNode *nsnode, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1260 {
1261     switch(V_VT(var)) {
1262     case VT_NULL:
1263         return remove_event_handler(event_target, eid);
1264
1265     case VT_DISPATCH:
1266         return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1267
1268     default:
1269         FIXME("not handler %s\n", debugstr_variant(var));
1270         /* fall through */
1271     case VT_EMPTY:
1272         return E_NOTIMPL;
1273     }
1274
1275     return S_OK;
1276 }
1277
1278 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
1279 {
1280     if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1281         V_VT(var) = VT_DISPATCH;
1282         V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1283         IDispatch_AddRef(V_DISPATCH(var));
1284     }else {
1285         V_VT(var) = VT_NULL;
1286     }
1287
1288     return S_OK;
1289 }
1290
1291 HRESULT attach_event(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocument *doc, BSTR name,
1292         IDispatch *disp, VARIANT_BOOL *res)
1293 {
1294     event_target_t *event_target;
1295     eventid_t eid;
1296     DWORD i = 0;
1297
1298     eid = attr_to_eid(name);
1299     if(eid == EVENTID_LAST) {
1300         WARN("Unknown event\n");
1301         *res = VARIANT_TRUE;
1302         return S_OK;
1303     }
1304
1305     event_target = get_event_target(event_target_ptr);
1306     if(!event_target)
1307         return E_OUTOFMEMORY;
1308
1309     if(event_target->event_table[eid]) {
1310         while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
1311             i++;
1312         if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
1313             return E_OUTOFMEMORY;
1314     }else if(!alloc_handler_vector(event_target, eid, i+1)) {
1315         return E_OUTOFMEMORY;
1316     }
1317
1318     IDispatch_AddRef(disp);
1319     event_target->event_table[eid]->handlers[i] = disp;
1320
1321     *res = VARIANT_TRUE;
1322     return ensure_nsevent_handler(doc->doc_node, event_target, nsnode, eid);
1323 }
1324
1325 HRESULT detach_event(event_target_t *event_target, HTMLDocument *doc, BSTR name, IDispatch *disp)
1326 {
1327     eventid_t eid;
1328     DWORD i = 0;
1329
1330     if(!event_target)
1331         return S_OK;
1332
1333     eid = attr_to_eid(name);
1334     if(eid == EVENTID_LAST) {
1335         WARN("Unknown event\n");
1336         return S_OK;
1337     }
1338
1339     if(!event_target->event_table[eid])
1340         return S_OK;
1341
1342     while(i < event_target->event_table[eid]->handler_cnt) {
1343         if(event_target->event_table[eid]->handlers[i] == disp) {
1344             IDispatch_Release(event_target->event_table[eid]->handlers[i]);
1345             event_target->event_table[eid]->handlers[i] = NULL;
1346         }
1347         i++;
1348     }
1349
1350     return S_OK;
1351 }
1352
1353 void update_cp_events(HTMLInnerWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp, nsIDOMNode *nsnode)
1354 {
1355     event_target_t *event_target;
1356     int i;
1357
1358     event_target = get_event_target(event_target_ptr);
1359     if(!event_target)
1360         return; /* FIXME */
1361
1362     for(i=0; i < EVENTID_LAST; i++) {
1363         if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1364             ensure_nsevent_handler(window->doc, event_target, nsnode, i);
1365     }
1366 }
1367
1368 void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
1369 {
1370     const PRUnichar *attr_value;
1371     nsAString attr_name_str, attr_value_str;
1372     IDispatch *disp;
1373     HTMLDOMNode *node;
1374     int i;
1375     nsresult nsres;
1376     HRESULT hres;
1377
1378     nsAString_Init(&attr_value_str, NULL);
1379     nsAString_Init(&attr_name_str, NULL);
1380
1381     for(i=0; i < EVENTID_LAST; i++) {
1382         nsAString_SetData(&attr_name_str, event_info[i].attr_name);
1383         nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
1384         if(NS_SUCCEEDED(nsres)) {
1385             nsAString_GetData(&attr_value_str, &attr_value);
1386             if(!*attr_value)
1387                 continue;
1388
1389             TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
1390
1391             disp = script_parse_event(doc->window, attr_value);
1392             if(disp) {
1393                 hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1394                 if(SUCCEEDED(hres)) {
1395                     set_event_handler_disp(get_node_event_target(node), node->nsnode, node->doc, i, disp);
1396                     node_release(node);
1397                 }
1398                 IDispatch_Release(disp);
1399             }
1400         }
1401     }
1402
1403     nsAString_Finish(&attr_value_str);
1404     nsAString_Finish(&attr_name_str);
1405 }
1406
1407 HRESULT doc_init_events(HTMLDocumentNode *doc)
1408 {
1409     unsigned i;
1410     HRESULT hres;
1411
1412     doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
1413     if(!doc->event_vector)
1414         return E_OUTOFMEMORY;
1415
1416     init_nsevents(doc);
1417
1418     for(i=0; i < EVENTID_LAST; i++) {
1419         if(event_info[i].flags & EVENT_HASDEFAULTHANDLERS) {
1420             hres = ensure_nsevent_handler(doc, NULL, NULL, i);
1421             if(FAILED(hres))
1422                 return hres;
1423         }
1424     }
1425
1426     return S_OK;
1427 }
1428
1429 void release_event_target(event_target_t *event_target)
1430 {
1431     int i, j;
1432
1433     for(i=0; i < EVENTID_LAST; i++) {
1434         if(event_target->event_table[i]) {
1435             if(event_target->event_table[i]->handler_prop)
1436                 IDispatch_Release(event_target->event_table[i]->handler_prop);
1437             for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
1438                 if(event_target->event_table[i]->handlers[j])
1439                     IDispatch_Release(event_target->event_table[i]->handlers[j]);
1440         }
1441     }
1442
1443     heap_free(event_target);
1444 }