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