msacm32: acmFormatEnum: Implement ACM_FORMATENUMF_SUGGEST.
[wine] / dlls / mshtml / htmlevent.c
1 /*
2  * Copyright 2008 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 struct event_target_t {
36     IDispatch *event_table[EVENTID_LAST];
37 };
38
39 static const WCHAR blurW[] = {'b','l','u','r',0};
40 static const WCHAR onblurW[] = {'o','n','b','l','u','r',0};
41
42 static const WCHAR changeW[] = {'c','h','a','n','g','e',0};
43 static const WCHAR onchangeW[] = {'o','n','c','h','a','n','g','e',0};
44
45 static const WCHAR clickW[] = {'c','l','i','c','k',0};
46 static const WCHAR onclickW[] = {'o','n','c','l','i','c','k',0};
47
48 static const WCHAR dragW[] = {'d','r','a','g',0};
49 static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};
50
51 static const WCHAR dragstartW[] = {'d','r','a','g','s','t','a','r','t',0};
52 static const WCHAR ondragstartW[] = {'o','n','d','r','a','g','s','t','a','r','t',0};
53
54 static const WCHAR focusW[] = {'f','o','c','u','s',0};
55 static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};
56
57 static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
58 static const WCHAR onkeydownW[] = {'o','n','k','e','y','d','o','w','n',0};
59
60 static const WCHAR keyupW[] = {'k','e','y','u','p',0};
61 static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};
62
63 static const WCHAR loadW[] = {'l','o','a','d',0};
64 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
65
66 static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
67 static const WCHAR onmousedownW[] = {'o','n','m','o','u','s','e','d','o','w','n',0};
68
69 static const WCHAR mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
70 static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};
71
72 static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
73 static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};
74
75 static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
76 static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};
77
78 static const WCHAR pasteW[] = {'p','a','s','t','e',0};
79 static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};
80
81 static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
82 static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};
83
84 typedef struct {
85     LPCWSTR name;
86     LPCWSTR attr_name;
87     DWORD flags;
88 } event_info_t;
89
90 #define EVENT_DEFAULTLISTENER    0x0001
91 #define EVENT_BUBBLE             0x0002
92
93 static const event_info_t event_info[] = {
94     {blurW,         onblurW,         EVENT_DEFAULTLISTENER},
95     {changeW,       onchangeW,       EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
96     {clickW,        onclickW,        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
97     {dragW,         ondragW,         0},
98     {dragstartW,    ondragstartW,    0},
99     {focusW,        onfocusW,        EVENT_DEFAULTLISTENER},
100     {keydownW,      onkeydownW,      EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
101     {keyupW,        onkeyupW,        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
102     {loadW,         onloadW,         0},
103     {mousedownW,    onmousedownW,    EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
104     {mouseoutW,     onmouseoutW,     EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
105     {mouseoverW,    onmouseoverW,    EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
106     {mouseupW,      onmouseupW,      EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
107     {pasteW,        onpasteW,        0},
108     {selectstartW,  onselectstartW,  0}
109 };
110
111 eventid_t str_to_eid(LPCWSTR str)
112 {
113     int i;
114
115     for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
116         if(!strcmpW(event_info[i].name, str))
117             return i;
118     }
119
120     ERR("unknown type %s\n", debugstr_w(str));
121     return EVENTID_LAST;
122 }
123
124 typedef struct {
125     DispatchEx dispex;
126     const IHTMLEventObjVtbl  *lpIHTMLEventObjVtbl;
127
128     LONG ref;
129
130     HTMLDOMNode *target;
131 } HTMLEventObj;
132
133 #define HTMLEVENTOBJ(x) ((IHTMLEventObj*) &(x)->lpIHTMLEventObjVtbl)
134
135 #define HTMLEVENTOBJ_THIS(iface) DEFINE_THIS(HTMLEventObj, IHTMLEventObj, iface)
136
137 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
138 {
139     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
140
141     *ppv = NULL;
142
143     if(IsEqualGUID(&IID_IUnknown, riid)) {
144         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
145         *ppv = HTMLEVENTOBJ(This);
146     }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
147         TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
148         *ppv = HTMLEVENTOBJ(This);
149     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
150         return *ppv ? S_OK : E_NOINTERFACE;
151     }
152
153     if(*ppv) {
154         IUnknown_AddRef((IUnknown*)*ppv);
155         return S_OK;
156     }
157
158     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
159     return E_NOINTERFACE;
160 }
161
162 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
163 {
164     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
165     LONG ref = InterlockedIncrement(&This->ref);
166
167     TRACE("(%p) ref=%d\n", This, ref);
168
169     return ref;
170 }
171
172 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
173 {
174     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
175     LONG ref = InterlockedDecrement(&This->ref);
176
177     TRACE("(%p) ref=%d\n", This, ref);
178
179     if(!ref)
180         heap_free(This);
181
182     return ref;
183 }
184
185 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
186 {
187     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
188     FIXME("(%p)->(%p)\n", This, pctinfo);
189     return E_NOTIMPL;
190 }
191
192 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
193                                               LCID lcid, ITypeInfo **ppTInfo)
194 {
195     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
196     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
197     return E_NOTIMPL;
198 }
199
200 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
201                                                 LPOLESTR *rgszNames, UINT cNames,
202                                                 LCID lcid, DISPID *rgDispId)
203 {
204     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
205     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
206           lcid, rgDispId);
207     return E_NOTIMPL;
208 }
209
210 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
211                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
212                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
213 {
214     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
215     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
216           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
217     return E_NOTIMPL;
218 }
219
220 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
221 {
222     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
223
224     TRACE("(%p)->(%p)\n", This, p);
225
226     return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(This->target), &IID_IHTMLElement, (void**)p);
227 }
228
229 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
230 {
231     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
232     FIXME("(%p)->(%p)\n", This, p);
233     return E_NOTIMPL;
234 }
235
236 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
237 {
238     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
239     FIXME("(%p)->(%p)\n", This, p);
240     return E_NOTIMPL;
241 }
242
243 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
244 {
245     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
246     FIXME("(%p)->(%p)\n", This, p);
247     return E_NOTIMPL;
248 }
249
250 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
251 {
252     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
253     FIXME("(%p)->()\n", This);
254     return E_NOTIMPL;
255 }
256
257 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
258 {
259     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
260     FIXME("(%p)->(%p)\n", This, p);
261     return E_NOTIMPL;
262 }
263
264 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
265 {
266     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
267     FIXME("(%p)->(%x)\n", This, v);
268     return E_NOTIMPL;
269 }
270
271 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
272 {
273     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
274     FIXME("(%p)->(%p)\n", This, p);
275     return E_NOTIMPL;
276 }
277
278 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
279 {
280     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
281     FIXME("(%p)->(%p)\n", This, p);
282     return E_NOTIMPL;
283 }
284
285 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
286 {
287     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
288     FIXME("(%p)->(%p)\n", This, p);
289     return E_NOTIMPL;
290 }
291
292 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
293 {
294     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
295     FIXME("(%p)->(%d)\n", This, v);
296     return E_NOTIMPL;
297 }
298
299 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
300 {
301     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
302     FIXME("(%p)->(%p)\n", This, p);
303     return E_NOTIMPL;
304 }
305
306 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
307 {
308     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
309     FIXME("(%p)->(%p)\n", This, p);
310     return E_NOTIMPL;
311 }
312
313 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
314 {
315     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
316     FIXME("(%p)->(%p)\n", This, p);
317     return E_NOTIMPL;
318 }
319
320 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
321 {
322     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
323     FIXME("(%p)->(%p)\n", This, p);
324     return E_NOTIMPL;
325 }
326
327 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
328 {
329     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
330     FIXME("(%p)->(%p)\n", This, p);
331     return E_NOTIMPL;
332 }
333
334 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
335 {
336     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
337     FIXME("(%p)->(%p)\n", This, p);
338     return E_NOTIMPL;
339 }
340
341 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
342 {
343     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
344     FIXME("(%p)->(%p)\n", This, p);
345     return E_NOTIMPL;
346 }
347
348 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
349 {
350     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
351     FIXME("(%p)->(%p)\n", This, p);
352     return E_NOTIMPL;
353 }
354
355 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
356 {
357     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
358     FIXME("(%p)->(%p)\n", This, p);
359     return E_NOTIMPL;
360 }
361
362 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
363 {
364     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
365     FIXME("(%p)->(%p)\n", This, p);
366     return E_NOTIMPL;
367 }
368
369 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
370 {
371     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
372     FIXME("(%p)->(%p)\n", This, p);
373     return E_NOTIMPL;
374 }
375
376 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
377 {
378     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
379     FIXME("(%p)->(%p)\n", This, p);
380     return E_NOTIMPL;
381 }
382
383 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
384 {
385     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
386     FIXME("(%p)->(%p)\n", This, p);
387     return E_NOTIMPL;
388 }
389
390 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
391 {
392     HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
393     FIXME("(%p)->(%p)\n", This, p);
394     return E_NOTIMPL;
395 }
396
397 #undef HTMLEVENTOBJ_THIS
398
399 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
400     HTMLEventObj_QueryInterface,
401     HTMLEventObj_AddRef,
402     HTMLEventObj_Release,
403     HTMLEventObj_GetTypeInfoCount,
404     HTMLEventObj_GetTypeInfo,
405     HTMLEventObj_GetIDsOfNames,
406     HTMLEventObj_Invoke,
407     HTMLEventObj_get_srcElement,
408     HTMLEventObj_get_altKey,
409     HTMLEventObj_get_ctrlKey,
410     HTMLEventObj_get_shiftKey,
411     HTMLEventObj_put_returnValue,
412     HTMLEventObj_get_returnValue,
413     HTMLEventObj_put_cancelBubble,
414     HTMLEventObj_get_cancelBubble,
415     HTMLEventObj_get_fromElement,
416     HTMLEventObj_get_toElement,
417     HTMLEventObj_put_keyCode,
418     HTMLEventObj_get_keyCode,
419     HTMLEventObj_get_button,
420     HTMLEventObj_get_type,
421     HTMLEventObj_get_qualifier,
422     HTMLEventObj_get_reason,
423     HTMLEventObj_get_x,
424     HTMLEventObj_get_y,
425     HTMLEventObj_get_clientX,
426     HTMLEventObj_get_clientY,
427     HTMLEventObj_get_offsetX,
428     HTMLEventObj_get_offsetY,
429     HTMLEventObj_get_screenX,
430     HTMLEventObj_get_screenY,
431     HTMLEventObj_get_srcFilter
432 };
433
434 static const tid_t HTMLEventObj_iface_tids[] = {
435     IHTMLEventObj_tid,
436     0
437 };
438
439 static dispex_static_data_t HTMLEventObj_dispex = {
440     NULL,
441     DispCEventObj_tid,
442     NULL,
443     HTMLEventObj_iface_tids
444 };
445
446 static IHTMLEventObj *create_event(HTMLDOMNode *target)
447 {
448     HTMLEventObj *ret;
449
450     ret = heap_alloc(sizeof(*ret));
451     ret->lpIHTMLEventObjVtbl = &HTMLEventObjVtbl;
452     ret->ref = 1;
453     ret->target = target;
454     IHTMLDOMNode_AddRef(HTMLDOMNODE(target));
455
456     init_dispex(&ret->dispex, (IUnknown*)HTMLEVENTOBJ(ret), &HTMLEventObj_dispex);
457
458     return HTMLEVENTOBJ(ret);
459 }
460
461 void fire_event(HTMLDocument *doc, eventid_t eid, nsIDOMNode *target)
462 {
463     IHTMLEventObj *prev_event, *event_obj = NULL;
464     nsIDOMNode *parent, *nsnode;
465     HTMLDOMNode *node;
466     PRUint16 node_type;
467
468     nsIDOMNode_GetNodeType(target, &node_type);
469     if(node_type != ELEMENT_NODE) {
470         FIXME("node type %d node supported\n", node_type);
471         return;
472     }
473
474     prev_event = doc->window->event;
475     nsnode = target;
476     nsIDOMNode_AddRef(nsnode);
477
478     while(1) {
479         node = get_node(doc, nsnode, FALSE);
480
481         if(node && node->event_target && node->event_target->event_table[eid]) {
482             if(!event_obj)
483                 event_obj = doc->window->event = create_event(get_node(doc, target, TRUE));
484
485             TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
486             call_disp_func(doc, node->event_target->event_table[eid], (IDispatch*)HTMLDOMNODE(node));
487             TRACE("%s <<<\n", debugstr_w(event_info[eid].name));
488         }
489
490         if(!(event_info[eid].flags & EVENT_BUBBLE))
491             break;
492
493         nsIDOMNode_GetParentNode(nsnode, &parent);
494         nsIDOMNode_Release(nsnode);
495         nsnode = parent;
496         if(!nsnode)
497             break;
498
499         nsIDOMNode_GetNodeType(nsnode, &node_type);
500         if(node_type != ELEMENT_NODE)
501             break;
502     }
503
504     if(nsnode)
505         nsIDOMNode_Release(nsnode);
506
507     if((event_info[eid].flags & EVENT_BUBBLE) && doc->event_target && doc->event_target->event_table[eid]) {
508         if(!event_obj)
509             event_obj = doc->window->event = create_event(get_node(doc, target, TRUE));
510
511         TRACE("doc %s >>>\n", debugstr_w(event_info[eid].name));
512         call_disp_func(doc, doc->event_target->event_table[eid], (IDispatch*)HTMLDOC(doc));
513         TRACE("doc %s <<<\n", debugstr_w(event_info[eid].name));
514     }
515
516     if(event_obj) {
517         IHTMLEventObj_Release(event_obj);
518         doc->window->event = prev_event;
519     }
520 }
521
522 static HRESULT set_event_handler_disp(event_target_t **event_target, HTMLDocument *doc, eventid_t eid, IDispatch *disp)
523 {
524     if(!*event_target)
525         *event_target = heap_alloc_zero(sizeof(event_target_t));
526     else if((*event_target)->event_table[eid])
527         IDispatch_Release((*event_target)->event_table[eid]);
528
529     (*event_target)->event_table[eid] = disp;
530     if(!disp)
531         return S_OK;
532     IDispatch_AddRef(disp);
533
534     if(doc->nscontainer && (event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
535         if(!doc->nscontainer->event_vector) {
536             doc->nscontainer->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
537             if(!doc->nscontainer->event_vector)
538                 return E_OUTOFMEMORY;
539         }
540
541         if(!doc->nscontainer->event_vector[eid]) {
542             doc->nscontainer->event_vector[eid] = TRUE;
543             add_nsevent_listener(doc->nscontainer, event_info[eid].name);
544         }
545     }
546
547     return S_OK;
548 }
549
550 HRESULT set_event_handler(event_target_t **event_target, HTMLDocument *doc, eventid_t eid, VARIANT *var)
551 {
552     switch(V_VT(var)) {
553     case VT_NULL:
554         if(*event_target && (*event_target)->event_table[eid]) {
555             IDispatch_Release((*event_target)->event_table[eid]);
556             (*event_target)->event_table[eid] = NULL;
557         }
558         break;
559
560     case VT_DISPATCH:
561         return set_event_handler_disp(event_target, doc, eid, V_DISPATCH(var));
562
563     default:
564         FIXME("not supported vt=%d\n", V_VT(var));
565         return E_NOTIMPL;
566     }
567
568     return S_OK;
569 }
570
571 HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
572 {
573     if(*event_target && (*event_target)->event_table[eid]) {
574         V_VT(var) = VT_DISPATCH;
575         V_DISPATCH(var) = (*event_target)->event_table[eid];
576         IDispatch_AddRef(V_DISPATCH(var));
577     }else {
578         V_VT(var) = VT_NULL;
579     }
580
581     return S_OK;
582 }
583
584 void check_event_attr(HTMLDocument *doc, nsIDOMElement *nselem)
585 {
586     const PRUnichar *attr_value;
587     nsAString attr_name_str, attr_value_str;
588     IDispatch *disp;
589     HTMLDOMNode *node;
590     int i;
591     nsresult nsres;
592
593     nsAString_Init(&attr_value_str, NULL);
594     nsAString_Init(&attr_name_str, NULL);
595
596     for(i=0; i < EVENTID_LAST; i++) {
597         nsAString_SetData(&attr_name_str, event_info[i].attr_name);
598         nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
599         if(NS_SUCCEEDED(nsres)) {
600             nsAString_GetData(&attr_value_str, &attr_value);
601             if(!*attr_value)
602                 continue;
603
604             TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
605
606             disp = script_parse_event(doc, attr_value);
607             if(disp) {
608                 node = get_node(doc, (nsIDOMNode*)nselem, TRUE);
609                 set_event_handler_disp(&node->event_target, node->doc, i, disp);
610                 IDispatch_Release(disp);
611             }
612         }
613     }
614
615     nsAString_Finish(&attr_value_str);
616     nsAString_Finish(&attr_name_str);
617 }
618
619 void release_event_target(event_target_t *event_target)
620 {
621     int i;
622
623     for(i=0; i < EVENTID_LAST; i++) {
624         if(event_target->event_table[i])
625             IDispatch_Release(event_target->event_table[i]);
626     }
627
628     heap_free(event_target);
629 }