mshtml: Move node list to HTMLDocumentNode.
[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
28 #include "mshtml_private.h"
29 #include "htmlevent.h"
30
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
34
35 typedef struct {
36     IDispatch *handler_prop;
37     DWORD handler_cnt;
38     IDispatch *handlers[0];
39 } handler_vector_t;
40
41 struct event_target_t {
42     handler_vector_t *event_table[EVENTID_LAST];
43 };
44
45 static const WCHAR beforeunloadW[] = {'b','e','f','o','r','e','u','n','l','o','a','d',0};
46 static const WCHAR onbeforeunloadW[] = {'o','n','b','e','f','o','r','e','u','n','l','o','a','d',0};
47
48 static const WCHAR blurW[] = {'b','l','u','r',0};
49 static const WCHAR onblurW[] = {'o','n','b','l','u','r',0};
50
51 static const WCHAR changeW[] = {'c','h','a','n','g','e',0};
52 static const WCHAR onchangeW[] = {'o','n','c','h','a','n','g','e',0};
53
54 static const WCHAR clickW[] = {'c','l','i','c','k',0};
55 static const WCHAR onclickW[] = {'o','n','c','l','i','c','k',0};
56
57 static const WCHAR dblclickW[] = {'d','b','l','c','l','i','c','k',0};
58 static const WCHAR ondblclickW[] = {'o','n','d','b','l','c','l','i','c','k',0};
59
60 static const WCHAR dragW[] = {'d','r','a','g',0};
61 static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};
62
63 static const WCHAR dragstartW[] = {'d','r','a','g','s','t','a','r','t',0};
64 static const WCHAR ondragstartW[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
65
66 static const WCHAR focusW[] = {'f','o','c','u','s',0};
67 static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};
68
69 static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
70 static const WCHAR onkeydownW[] = {'o','n','k','e','y','d','o','w','n',0};
71
72 static const WCHAR keyupW[] = {'k','e','y','u','p',0};
73 static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};
74
75 static const WCHAR loadW[] = {'l','o','a','d',0};
76 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
77
78 static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
79 static const WCHAR onmousedownW[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
80
81 static const WCHAR mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
82 static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};
83
84 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
85 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
86
87 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
88 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
89
90 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
91 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
92
93 static const WCHAR readystatechangeW[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
94 static const WCHAR onreadystatechangeW[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
95
96 static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
97 static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
98
99 typedef struct {
100     LPCWSTR name;
101     LPCWSTR attr_name;
102     DWORD flags;
103 } event_info_t;
104
105 #define EVENT_DEFAULTLISTENER    0x0001
106 #define EVENT_BUBBLE             0x0002
107 #define EVENT_FORWARDBODY        0x0004
108
109 static const event_info_t event_info[] = {
110     {beforeunloadW,      onbeforeunloadW,      EVENT_DEFAULTLISTENER|EVENT_FORWARDBODY},
111     {blurW,              onblurW,              EVENT_DEFAULTLISTENER},
112     {changeW,            onchangeW,            EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
113     {clickW,             onclickW,             EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
114     {dblclickW,          ondblclickW,          EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
115     {dragW,              ondragW,              0},
116     {dragstartW,         ondragstartW,         0},
117     {focusW,             onfocusW,             EVENT_DEFAULTLISTENER},
118     {keydownW,           onkeydownW,           EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
119     {keyupW,             onkeyupW,             EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
120     {loadW,              onloadW,              0},
121     {mousedownW,         onmousedownW,         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
122     {mouseoutW,          onmouseoutW,          EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
123     {mouseoverW,         onmouseoverW,         EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
124     {mouseupW,           onmouseupW,           EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
125     {pasteW,             onpasteW,             0},
126     {readystatechangeW,  onreadystatechangeW,  0},
127     {selectstartW,       onselectstartW,       0}
128 };
129
130 eventid_t str_to_eid(LPCWSTR str)
131 {
132     int i;
133
134     for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
135         if(!strcmpW(event_info[i].name, str))
136             return i;
137     }
138
139     ERR("unknown type %s\n", debugstr_w(str));
140     return EVENTID_LAST;
141 }
142
143 static eventid_t attr_to_eid(LPCWSTR str)
144 {
145     int i;
146
147     for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
148         if(!strcmpW(event_info[i].attr_name, str))
149             return i;
150     }
151
152     return EVENTID_LAST;
153 }
154
155 typedef struct {
156     DispatchEx dispex;
157     const IHTMLEventObjVtbl  *lpIHTMLEventObjVtbl;
158
159     LONG ref;
160
161     HTMLDOMNode *target;
162     const event_info_t *type;
163     nsIDOMEvent *nsevent;
164 } HTMLEventObj;
165
166 #define HTMLEVENTOBJ(x) ((IHTMLEventObj*) &(x)->lpIHTMLEventObjVtbl)
167
168 #define HTMLEVENTOBJ_THIS(iface) DEFINE_THIS(HTMLEventObj, IHTMLEventObj, iface)
169
170 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
171 {
172     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
173
174     *ppv = NULL;
175
176     if(IsEqualGUID(&IID_IUnknown, riid)) {
177         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
178         *ppv = HTMLEVENTOBJ(This);
179     }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
180         TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
181         *ppv = HTMLEVENTOBJ(This);
182     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
183         return *ppv ? S_OK : E_NOINTERFACE;
184     }
185
186     if(*ppv) {
187         IUnknown_AddRef((IUnknown*)*ppv);
188         return S_OK;
189     }
190
191     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
192     return E_NOINTERFACE;
193 }
194
195 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
196 {
197     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
198     LONG ref = InterlockedIncrement(&This->ref);
199
200     TRACE("(%p) ref=%d\n", This, ref);
201
202     return ref;
203 }
204
205 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
206 {
207     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
208     LONG ref = InterlockedDecrement(&This->ref);
209
210     TRACE("(%p) ref=%d\n", This, ref);
211
212     if(!ref) {
213         if(This->nsevent)
214             nsIDOMEvent_Release(This->nsevent);
215         release_dispex(&This->dispex);
216         heap_free(This);
217     }
218
219     return ref;
220 }
221
222 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
223 {
224     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
225     FIXME("(%p)->(%p)\n", This, pctinfo);
226     return E_NOTIMPL;
227 }
228
229 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
230                                               LCID lcid, ITypeInfo **ppTInfo)
231 {
232     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
233     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
234     return E_NOTIMPL;
235 }
236
237 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
238                                                 LPOLESTR *rgszNames, UINT cNames,
239                                                 LCID lcid, DISPID *rgDispId)
240 {
241     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
242     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
243           lcid, rgDispId);
244     return E_NOTIMPL;
245 }
246
247 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
248                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
249                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
250 {
251     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
252     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
253           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
254     return E_NOTIMPL;
255 }
256
257 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
258 {
259     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
260
261     TRACE("(%p)->(%p)\n", This, p);
262
263     return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(This->target), &IID_IHTMLElement, (void**)p);
264 }
265
266 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
267 {
268     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
269     PRBool ret = FALSE;
270
271     TRACE("(%p)->(%p)\n", This, p);
272
273     if(This->nsevent) {
274         nsIDOMKeyEvent *key_event;
275         nsresult nsres;
276
277         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
278         if(NS_SUCCEEDED(nsres)) {
279             nsIDOMKeyEvent_GetAltKey(key_event, &ret);
280             nsIDOMKeyEvent_Release(key_event);
281         }else {
282             nsIDOMMouseEvent *mouse_event;
283
284             nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
285             if(NS_SUCCEEDED(nsres)) {
286                 nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
287                 nsIDOMMouseEvent_Release(mouse_event);
288             }
289         }
290     }
291
292     *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
293     return S_OK;
294 }
295
296 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
297 {
298     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
299     PRBool ret = FALSE;
300
301     TRACE("(%p)->(%p)\n", This, p);
302
303     if(This->nsevent) {
304         nsIDOMKeyEvent *key_event;
305         nsresult nsres;
306
307         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
308         if(NS_SUCCEEDED(nsres)) {
309             nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
310             nsIDOMKeyEvent_Release(key_event);
311         }else {
312             nsIDOMMouseEvent *mouse_event;
313
314             nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
315             if(NS_SUCCEEDED(nsres)) {
316                 nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
317                 nsIDOMMouseEvent_Release(mouse_event);
318             }
319         }
320     }
321
322     *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
323     return S_OK;
324 }
325
326 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
327 {
328     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
329     PRBool ret = FALSE;
330
331     TRACE("(%p)->(%p)\n", This, p);
332
333     if(This->nsevent) {
334         nsIDOMKeyEvent *key_event;
335         nsresult nsres;
336
337         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
338         if(NS_SUCCEEDED(nsres)) {
339             nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
340             nsIDOMKeyEvent_Release(key_event);
341         }else {
342             nsIDOMMouseEvent *mouse_event;
343
344             nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
345             if(NS_SUCCEEDED(nsres)) {
346                 nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
347                 nsIDOMMouseEvent_Release(mouse_event);
348             }
349         }
350     }
351
352     *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
353     return S_OK;
354 }
355
356 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
357 {
358     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
359     FIXME("(%p)->()\n", This);
360     return E_NOTIMPL;
361 }
362
363 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
364 {
365     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
366
367     FIXME("(%p)->(%p)\n", This, p);
368
369     V_VT(p) = VT_EMPTY;
370     return S_OK;
371 }
372
373 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
374 {
375     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
376     FIXME("(%p)->(%x)\n", This, v);
377     return E_NOTIMPL;
378 }
379
380 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
381 {
382     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
383
384     FIXME("(%p)->(%p)\n", This, p);
385
386     *p = VARIANT_FALSE;
387     return S_OK;
388 }
389
390 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
391 {
392     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
393
394     FIXME("(%p)->(%p)\n", This, p);
395
396     *p = NULL;
397     return S_OK;
398 }
399
400 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
401 {
402     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
403
404     FIXME("(%p)->(%p)\n", This, p);
405
406     *p = NULL;
407     return S_OK;
408 }
409
410 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
411 {
412     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
413     FIXME("(%p)->(%d)\n", This, v);
414     return E_NOTIMPL;
415 }
416
417 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
418 {
419     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
420     PRUint32 key_code = 0;
421
422     TRACE("(%p)->(%p)\n", This, p);
423
424     if(This->nsevent) {
425         nsIDOMKeyEvent *key_event;
426         nsresult nsres;
427
428         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
429         if(NS_SUCCEEDED(nsres)) {
430             nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
431             nsIDOMKeyEvent_Release(key_event);
432         }
433     }
434
435     *p = key_code;
436     return S_OK;
437 }
438
439 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
440 {
441     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
442     PRUint16 button = 0;
443
444     TRACE("(%p)->(%p)\n", This, p);
445
446     if(This->nsevent) {
447         nsIDOMMouseEvent *mouse_event;
448         nsresult nsres;
449
450         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
451         if(NS_SUCCEEDED(nsres)) {
452             nsIDOMMouseEvent_GetButton(mouse_event, &button);
453             nsIDOMMouseEvent_Release(mouse_event);
454         }
455     }
456
457     *p = button;
458     return S_OK;
459 }
460
461 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
462 {
463     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
464
465     TRACE("(%p)->(%p)\n", This, p);
466
467     *p = SysAllocString(This->type->name);
468     return *p ? S_OK : E_OUTOFMEMORY;
469 }
470
471 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
472 {
473     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
474
475     FIXME("(%p)->(%p)\n", This, p);
476
477     *p = NULL;
478     return S_OK;
479 }
480
481 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
482 {
483     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
484
485     FIXME("(%p)->(%p)\n", This, p);
486
487     *p = 0;
488     return S_OK;
489 }
490
491 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
492 {
493     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
494
495     FIXME("(%p)->(%p)\n", This, p);
496
497     *p = -1;
498     return S_OK;
499 }
500
501 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
502 {
503     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
504
505     FIXME("(%p)->(%p)\n", This, p);
506
507     *p = -1;
508     return S_OK;
509 }
510
511 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
512 {
513     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
514     PRInt32 x = 0;
515
516     TRACE("(%p)->(%p)\n", This, p);
517
518     if(This->nsevent) {
519         nsIDOMMouseEvent *mouse_event;
520         nsresult nsres;
521
522         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
523         if(NS_SUCCEEDED(nsres)) {
524             nsIDOMMouseEvent_GetClientX(mouse_event, &x);
525             nsIDOMMouseEvent_Release(mouse_event);
526         }
527     }
528
529     *p = x;
530     return S_OK;
531 }
532
533 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
534 {
535     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
536     PRInt32 y = 0;
537
538     TRACE("(%p)->(%p)\n", This, p);
539
540     if(This->nsevent) {
541         nsIDOMMouseEvent *mouse_event;
542         nsresult nsres;
543
544         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
545         if(NS_SUCCEEDED(nsres)) {
546             nsIDOMMouseEvent_GetClientY(mouse_event, &y);
547             nsIDOMMouseEvent_Release(mouse_event);
548         }
549     }
550
551     *p = y;
552     return S_OK;
553 }
554
555 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
556 {
557     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
558
559     FIXME("(%p)->(%p)\n", This, p);
560
561     *p = 0;
562     return S_OK;
563 }
564
565 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
566 {
567     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
568
569     FIXME("(%p)->(%p)\n", This, p);
570
571     *p = 0;
572     return S_OK;
573 }
574
575 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
576 {
577     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
578     PRInt32 x = 0;
579
580     TRACE("(%p)->(%p)\n", This, p);
581
582     if(This->nsevent) {
583         nsIDOMMouseEvent *mouse_event;
584         nsresult nsres;
585
586         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
587         if(NS_SUCCEEDED(nsres)) {
588             nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
589             nsIDOMMouseEvent_Release(mouse_event);
590         }
591     }
592
593     *p = x;
594     return S_OK;
595 }
596
597 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
598 {
599     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
600     PRInt32 y = 0;
601
602     TRACE("(%p)->(%p)\n", This, p);
603
604     if(This->nsevent) {
605         nsIDOMMouseEvent *mouse_event;
606         nsresult nsres;
607
608         nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
609         if(NS_SUCCEEDED(nsres)) {
610             nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
611             nsIDOMMouseEvent_Release(mouse_event);
612         }
613     }
614
615     *p = y;
616     return S_OK;
617 }
618
619 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
620 {
621     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
622
623     FIXME("(%p)->(%p)\n", This, p);
624
625     *p = NULL;
626     return S_OK;
627 }
628
629 #undef HTMLEVENTOBJ_THIS
630
631 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
632     HTMLEventObj_QueryInterface,
633     HTMLEventObj_AddRef,
634     HTMLEventObj_Release,
635     HTMLEventObj_GetTypeInfoCount,
636     HTMLEventObj_GetTypeInfo,
637     HTMLEventObj_GetIDsOfNames,
638     HTMLEventObj_Invoke,
639     HTMLEventObj_get_srcElement,
640     HTMLEventObj_get_altKey,
641     HTMLEventObj_get_ctrlKey,
642     HTMLEventObj_get_shiftKey,
643     HTMLEventObj_put_returnValue,
644     HTMLEventObj_get_returnValue,
645     HTMLEventObj_put_cancelBubble,
646     HTMLEventObj_get_cancelBubble,
647     HTMLEventObj_get_fromElement,
648     HTMLEventObj_get_toElement,
649     HTMLEventObj_put_keyCode,
650     HTMLEventObj_get_keyCode,
651     HTMLEventObj_get_button,
652     HTMLEventObj_get_type,
653     HTMLEventObj_get_qualifier,
654     HTMLEventObj_get_reason,
655     HTMLEventObj_get_x,
656     HTMLEventObj_get_y,
657     HTMLEventObj_get_clientX,
658     HTMLEventObj_get_clientY,
659     HTMLEventObj_get_offsetX,
660     HTMLEventObj_get_offsetY,
661     HTMLEventObj_get_screenX,
662     HTMLEventObj_get_screenY,
663     HTMLEventObj_get_srcFilter
664 };
665
666 static const tid_t HTMLEventObj_iface_tids[] = {
667     IHTMLEventObj_tid,
668     0
669 };
670
671 static dispex_static_data_t HTMLEventObj_dispex = {
672     NULL,
673     DispCEventObj_tid,
674     NULL,
675     HTMLEventObj_iface_tids
676 };
677
678 static IHTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
679 {
680     HTMLEventObj *ret;
681
682     ret = heap_alloc(sizeof(*ret));
683     ret->lpIHTMLEventObjVtbl = &HTMLEventObjVtbl;
684     ret->ref = 1;
685     ret->type = event_info+eid;
686     ret->target = target;
687     IHTMLDOMNode_AddRef(HTMLDOMNODE(target));
688
689     ret->nsevent = nsevent;
690     if(nsevent)
691         nsIDOMEvent_AddRef(nsevent);
692
693     init_dispex(&ret->dispex, (IUnknown*)HTMLEVENTOBJ(ret), &HTMLEventObj_dispex);
694
695     return HTMLEVENTOBJ(ret);
696 }
697
698 static void call_event_handlers(HTMLDocumentNode *doc, IHTMLEventObj *event_obj, event_target_t *event_target,
699         eventid_t eid, IDispatch *this_obj)
700 {
701     handler_vector_t *handler_vector;
702     DWORD i;
703     HRESULT hres;
704
705     if(!event_target || !(handler_vector = event_target->event_table[eid]))
706         return;
707
708     if(handler_vector->handler_prop) {
709         DISPID named_arg = DISPID_THIS;
710         VARIANTARG arg;
711         DISPPARAMS dp = {&arg, &named_arg, 1, 1};
712
713         V_VT(&arg) = VT_DISPATCH;
714         V_DISPATCH(&arg) = this_obj;
715
716         TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
717         hres = call_disp_func(handler_vector->handler_prop, &dp);
718         if(hres == S_OK)
719             TRACE("%s <<<\n", debugstr_w(event_info[eid].name));
720         else
721             WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
722     }
723
724     if(handler_vector->handler_cnt) {
725         VARIANTARG arg;
726         DISPPARAMS dp = {&arg, NULL, 1, 0};
727
728         V_VT(&arg) = VT_DISPATCH;
729         V_DISPATCH(&arg) = (IDispatch*)event_obj;
730
731         for(i=0; i < handler_vector->handler_cnt; i++) {
732             if(handler_vector->handlers[i]) {
733                 TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
734                 hres = call_disp_func(handler_vector->handlers[i], &dp);
735                 if(hres == S_OK)
736                     TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
737                 else
738                     WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
739             }
740         }
741     }
742 }
743
744 void fire_event(HTMLDocumentNode *doc, eventid_t eid, nsIDOMNode *target, nsIDOMEvent *nsevent)
745 {
746     IHTMLEventObj *prev_event, *event_obj = NULL;
747     nsIDOMNode *parent, *nsnode;
748     HTMLDOMNode *node;
749     PRUint16 node_type;
750
751     TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
752
753     prev_event = doc->basedoc.window->event;
754     event_obj = doc->basedoc.window->event = create_event(get_node(doc, target, TRUE), eid, nsevent);
755
756     nsIDOMNode_GetNodeType(target, &node_type);
757     nsnode = target;
758     nsIDOMNode_AddRef(nsnode);
759
760     switch(node_type) {
761     case ELEMENT_NODE:
762         do {
763             node = get_node(doc, nsnode, FALSE);
764             if(node)
765                 call_event_handlers(doc, event_obj, *get_node_event_target(node), eid, (IDispatch*)HTMLDOMNODE(node));
766
767             if(!(event_info[eid].flags & EVENT_BUBBLE))
768                 break;
769
770             nsIDOMNode_GetParentNode(nsnode, &parent);
771             nsIDOMNode_Release(nsnode);
772             nsnode = parent;
773             if(!nsnode)
774                 break;
775
776             nsIDOMNode_GetNodeType(nsnode, &node_type);
777         }while(node_type == ELEMENT_NODE);
778
779         if(!(event_info[eid].flags & EVENT_BUBBLE))
780             break;
781
782     case DOCUMENT_NODE:
783         if(event_info[eid].flags & EVENT_FORWARDBODY) {
784             nsIDOMHTMLElement *nsbody;
785             nsresult nsres;
786
787             nsres = nsIDOMHTMLDocument_GetBody(doc->basedoc.nsdoc, &nsbody);
788             if(NS_SUCCEEDED(nsres) && nsbody) {
789                 node = get_node(doc, (nsIDOMNode*)nsbody, FALSE);
790                 if(node)
791                     call_event_handlers(doc, event_obj, *get_node_event_target(node), eid, (IDispatch*)HTMLDOMNODE(node));
792                 nsIDOMHTMLElement_Release(nsbody);
793             }else {
794                 ERR("Could not get body: %08x\n", nsres);
795             }
796         }
797
798         call_event_handlers(doc, event_obj, doc->basedoc.event_target, eid, (IDispatch*)HTMLDOC(&doc->basedoc));
799         break;
800
801     default:
802         FIXME("unimplemented node type %d\n", node_type);
803     }
804
805     if(nsnode)
806         nsIDOMNode_Release(nsnode);
807
808     IHTMLEventObj_Release(event_obj);
809     doc->basedoc.window->event = prev_event;
810 }
811
812 static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
813 {
814     if(!*event_target_ptr)
815         *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
816     return *event_target_ptr;
817 }
818
819 static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
820 {
821     handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];
822
823     if(handler_vector) {
824         if(cnt <= handler_vector->handler_cnt)
825             return TRUE;
826
827         new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
828     }else {
829         new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
830     }
831
832     if(!new_vector)
833         return FALSE;
834
835     new_vector->handler_cnt = cnt;
836     event_target->event_table[eid] = new_vector;
837     return TRUE;
838 }
839
840 static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, HTMLWindow *window, eventid_t eid, IDispatch *disp)
841 {
842     event_target_t *event_target;
843
844     event_target = get_event_target(event_target_ptr);
845     if(!event_target)
846         return E_OUTOFMEMORY;
847
848     if(!alloc_handler_vector(event_target, eid, 0))
849         return E_OUTOFMEMORY;
850
851     if(event_target->event_table[eid]->handler_prop)
852         IDispatch_Release(event_target->event_table[eid]->handler_prop);
853
854     event_target->event_table[eid]->handler_prop = disp;
855     if(!disp)
856         return S_OK;
857     IDispatch_AddRef(disp);
858
859     if(window->nswindow && (event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
860         if(!window->event_vector) {
861             window->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
862             if(!window->event_vector)
863                 return E_OUTOFMEMORY;
864         }
865
866         if(!window->event_vector[eid]) {
867             window->event_vector[eid] = TRUE;
868             add_nsevent_listener(window, event_info[eid].name);
869         }
870     }
871
872     return S_OK;
873 }
874
875 HRESULT set_event_handler(event_target_t **event_target, HTMLDocument *doc, eventid_t eid, VARIANT *var)
876 {
877     switch(V_VT(var)) {
878     case VT_NULL:
879         if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
880             IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
881             (*event_target)->event_table[eid]->handler_prop = NULL;
882         }
883         break;
884
885     case VT_DISPATCH:
886         return set_event_handler_disp(event_target, doc->window, eid, V_DISPATCH(var));
887
888     default:
889         FIXME("not supported vt=%d\n", V_VT(var));
890         return E_NOTIMPL;
891     }
892
893     return S_OK;
894 }
895
896 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
897 {
898     if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
899         V_VT(var) = VT_DISPATCH;
900         V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
901         IDispatch_AddRef(V_DISPATCH(var));
902     }else {
903         V_VT(var) = VT_NULL;
904     }
905
906     return S_OK;
907 }
908
909 HRESULT attach_event(event_target_t **event_target_ptr, HTMLDocument *doc, BSTR name, IDispatch *disp, VARIANT_BOOL *res)
910 {
911     event_target_t *event_target;
912     eventid_t eid;
913     DWORD i = 0;
914
915     eid = attr_to_eid(name);
916     if(eid == EVENTID_LAST) {
917         WARN("Unknown event\n");
918         *res = VARIANT_TRUE;
919         return S_OK;
920     }
921
922     event_target = get_event_target(event_target_ptr);
923     if(!event_target)
924         return E_OUTOFMEMORY;
925
926     if(event_target->event_table[eid]) {
927         while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
928             i++;
929         if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
930             return E_OUTOFMEMORY;
931     }else if(!alloc_handler_vector(event_target, eid, i+1)) {
932         return E_OUTOFMEMORY;
933     }
934
935     IDispatch_AddRef(disp);
936     event_target->event_table[eid]->handlers[i] = disp;
937
938     *res = VARIANT_TRUE;
939     return S_OK;
940 }
941
942 void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
943 {
944     const PRUnichar *attr_value;
945     nsAString attr_name_str, attr_value_str;
946     IDispatch *disp;
947     HTMLDOMNode *node;
948     int i;
949     nsresult nsres;
950
951     nsAString_Init(&attr_value_str, NULL);
952     nsAString_Init(&attr_name_str, NULL);
953
954     for(i=0; i < EVENTID_LAST; i++) {
955         nsAString_SetData(&attr_name_str, event_info[i].attr_name);
956         nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
957         if(NS_SUCCEEDED(nsres)) {
958             nsAString_GetData(&attr_value_str, &attr_value);
959             if(!*attr_value)
960                 continue;
961
962             TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
963
964             disp = script_parse_event(doc->basedoc.window, attr_value);
965             if(disp) {
966                 node = get_node(doc, (nsIDOMNode*)nselem, TRUE);
967                 set_event_handler_disp(get_node_event_target(node), node->doc->basedoc.window, i, disp);
968                 IDispatch_Release(disp);
969             }
970         }
971     }
972
973     nsAString_Finish(&attr_value_str);
974     nsAString_Finish(&attr_name_str);
975 }
976
977 void release_event_target(event_target_t *event_target)
978 {
979     int i, j;
980
981     for(i=0; i < EVENTID_LAST; i++) {
982         if(event_target->event_table[i]) {
983             if(event_target->event_table[i]->handler_prop)
984                 IDispatch_Release(event_target->event_table[i]->handler_prop);
985             for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
986                 IDispatch_Release(event_target->event_table[i]->handlers[j]);
987         }
988     }
989
990     heap_free(event_target);
991 }