htmlevent.c 43.5 KB
Newer Older
1
/*
2
 * Copyright 2008-2009 Jacek Caban for CodeWeavers
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"
27
#include "mshtmdid.h"
28 29

#include "mshtml_private.h"
30
#include "htmlevent.h"
31 32 33 34 35

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

36 37 38 39 40 41
typedef struct {
    IDispatch *handler_prop;
    DWORD handler_cnt;
    IDispatch *handlers[0];
} handler_vector_t;

42
struct event_target_t {
43
    DWORD node_handlers_mask;
44
    handler_vector_t *event_table[EVENTID_LAST];
45 46
};

47 48 49
static const WCHAR beforeunloadW[] = {'b','e','f','o','r','e','u','n','l','o','a','d',0};
static const WCHAR onbeforeunloadW[] = {'o','n','b','e','f','o','r','e','u','n','l','o','a','d',0};

50 51 52
static const WCHAR blurW[] = {'b','l','u','r',0};
static const WCHAR onblurW[] = {'o','n','b','l','u','r',0};

53 54 55
static const WCHAR changeW[] = {'c','h','a','n','g','e',0};
static const WCHAR onchangeW[] = {'o','n','c','h','a','n','g','e',0};

56 57 58
static const WCHAR clickW[] = {'c','l','i','c','k',0};
static const WCHAR onclickW[] = {'o','n','c','l','i','c','k',0};

59 60 61
static const WCHAR contextmenuW[] = {'c','o','n','t','e','x','t','m','e','n','u',0};
static const WCHAR oncontextmenuW[] = {'o','n','c','o','n','t','e','x','t','m','e','n','u',0};

62 63 64
static const WCHAR dblclickW[] = {'d','b','l','c','l','i','c','k',0};
static const WCHAR ondblclickW[] = {'o','n','d','b','l','c','l','i','c','k',0};

65 66 67
static const WCHAR dragW[] = {'d','r','a','g',0};
static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};

68 69 70
static const WCHAR dragstartW[] = {'d','r','a','g','s','t','a','r','t',0};
static const WCHAR ondragstartW[] = {'o','n','d','r','a','g','s','t','a','r','t',0};

71 72 73
static const WCHAR errorW[] = {'e','r','r','o','r',0};
static const WCHAR onerrorW[] = {'o','n','e','r','r','o','r',0};

74 75 76
static const WCHAR focusW[] = {'f','o','c','u','s',0};
static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};

77 78 79
static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
static const WCHAR onkeydownW[] = {'o','n','k','e','y','d','o','w','n',0};

80 81 82
static const WCHAR keypressW[] = {'k','e','y','p','r','e','s','s',0};
static const WCHAR onkeypressW[] = {'o','n','k','e','y','p','r','e','s','s',0};

83 84 85
static const WCHAR keyupW[] = {'k','e','y','u','p',0};
static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};

86 87 88
static const WCHAR loadW[] = {'l','o','a','d',0};
static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};

89 90 91
static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
static const WCHAR onmousedownW[] = {'o','n','m','o','u','s','e','d','o','w','n',0};

92 93 94
static const WCHAR mousemoveW[] = {'m','o','u','s','e','m','o','v','e',0};
static const WCHAR onmousemoveW[] = {'o','n','m','o','u','s','e','m','o','v','e',0};

95 96 97
static const WCHAR mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
static const WCHAR onmouseoutW[] = {'o','n','m','o','u','s','e','o','u','t',0};

98 99 100
static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
static const WCHAR onmouseoverW[] = {'o','n','m','o','u','s','e','o','v','e','r',0};

101 102 103
static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
static const WCHAR onmouseupW[] = {'o','n','m','o','u','s','e','u','p',0};

104 105 106
static const WCHAR pasteW[] = {'p','a','s','t','e',0};
static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};

107 108 109
static const WCHAR readystatechangeW[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
static const WCHAR onreadystatechangeW[] = {'o','n','r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};

110 111 112
static const WCHAR resizeW[] = {'r','e','s','i','z','e',0};
static const WCHAR onresizeW[] = {'o','n','r','e','s','i','z','e',0};

113 114 115
static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
static const WCHAR onselectstartW[] = {'o','n','s','e','l','e','c','t','s','t','a','r','t',0};

116 117 118
static const WCHAR submitW[] = {'s','u','b','m','i','t',0};
static const WCHAR onsubmitW[] = {'o','n','s','u','b','m','i','t',0};

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
static const WCHAR HTMLEventsW[] = {'H','T','M','L','E','v','e','n','t','s',0};
static const WCHAR KeyboardEventW[] = {'K','e','y','b','o','a','r','d','E','v','e','n','t',0};
static const WCHAR MouseEventW[] = {'M','o','u','s','e','E','v','e','n','t',0};

enum {
    EVENTT_NONE,
    EVENTT_HTML,
    EVENTT_KEY,
    EVENTT_MOUSE
};

static const WCHAR *event_types[] = {
    NULL,
    HTMLEventsW,
    KeyboardEventW,
    MouseEventW
};

137 138 139
typedef struct {
    LPCWSTR name;
    LPCWSTR attr_name;
140
    DWORD type;
141
    DISPID dispid;
142
    DWORD flags;
143 144
} event_info_t;

145
#define EVENT_DEFAULTLISTENER    0x0001
146
#define EVENT_BUBBLE             0x0002
147
#define EVENT_FORWARDBODY        0x0004
148
#define EVENT_NODEHANDLER        0x0008
149
#define EVENT_CANCELABLE         0x0010
150
#define EVENT_HASDEFAULTHANDLERS 0x0020
151

152
static const event_info_t event_info[] = {
153 154 155 156 157 158 159
    {beforeunloadW,      onbeforeunloadW,      EVENTT_NONE,   DISPID_EVMETH_ONBEFOREUNLOAD,
        EVENT_DEFAULTLISTENER|EVENT_FORWARDBODY},
    {blurW,              onblurW,              EVENTT_HTML,   DISPID_EVMETH_ONBLUR,
        EVENT_DEFAULTLISTENER},
    {changeW,            onchangeW,            EVENTT_HTML,   DISPID_EVMETH_ONCHANGE,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
    {clickW,             onclickW,             EVENTT_MOUSE,  DISPID_EVMETH_ONCLICK,
160
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE|EVENT_HASDEFAULTHANDLERS},
161
    {contextmenuW,       oncontextmenuW,       EVENTT_MOUSE,  DISPID_EVMETH_ONCONTEXTMENU,
162
        EVENT_BUBBLE|EVENT_CANCELABLE},
163
    {dblclickW,          ondblclickW,          EVENTT_MOUSE,  DISPID_EVMETH_ONDBLCLICK,
164
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE},
165
    {dragW,              ondragW,              EVENTT_MOUSE,  DISPID_EVMETH_ONDRAG,
166
        EVENT_CANCELABLE},
167
    {dragstartW,         ondragstartW,         EVENTT_MOUSE,  DISPID_EVMETH_ONDRAGSTART,
168
        EVENT_CANCELABLE},
169
    {errorW,             onerrorW,             EVENTT_NONE,   DISPID_EVMETH_ONERROR,
170
        EVENT_NODEHANDLER},
171 172 173 174
    {focusW,             onfocusW,             EVENTT_HTML,   DISPID_EVMETH_ONFOCUS,
        EVENT_DEFAULTLISTENER},
    {keydownW,           onkeydownW,           EVENTT_KEY,    DISPID_EVMETH_ONKEYDOWN,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
175 176
    {keypressW,          onkeypressW,          EVENTT_KEY,    DISPID_EVMETH_ONKEYPRESS,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
177 178 179
    {keyupW,             onkeyupW,             EVENTT_KEY,    DISPID_EVMETH_ONKEYUP,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
    {loadW,              onloadW,              EVENTT_HTML,   DISPID_EVMETH_ONLOAD,
180
        EVENT_NODEHANDLER},
181 182
    {mousedownW,         onmousedownW,         EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEDOWN,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
183 184
    {mousemoveW,         onmousemoveW,         EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEMOVE,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
185 186 187 188 189 190 191
    {mouseoutW,          onmouseoutW,          EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEOUT,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
    {mouseoverW,         onmouseoverW,         EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEOVER,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
    {mouseupW,           onmouseupW,           EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEUP,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
    {pasteW,             onpasteW,             EVENTT_NONE,   DISPID_EVMETH_ONPASTE,
192
        EVENT_CANCELABLE},
193 194
    {readystatechangeW,  onreadystatechangeW,  EVENTT_NONE,   DISPID_EVMETH_ONREADYSTATECHANGE,
        0},
195 196
    {resizeW,            onresizeW,            EVENTT_NONE,   DISPID_EVMETH_ONRESIZE,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
197
    {selectstartW,       onselectstartW,       EVENTT_MOUSE,  DISPID_EVMETH_ONSELECTSTART,
198
        EVENT_CANCELABLE},
199
    {submitW,            onsubmitW,            EVENTT_HTML,   DISPID_EVMETH_ONSUBMIT,
200
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE}
201 202
};

203
static const eventid_t node_handled_list[] = { EVENTID_ERROR, EVENTID_LOAD };
204

205 206 207 208 209 210 211 212 213 214 215 216 217
eventid_t str_to_eid(LPCWSTR str)
{
    int i;

    for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
        if(!strcmpW(event_info[i].name, str))
            return i;
    }

    ERR("unknown type %s\n", debugstr_w(str));
    return EVENTID_LAST;
}

218 219 220 221 222 223 224 225 226 227 228 229
static eventid_t attr_to_eid(LPCWSTR str)
{
    int i;

    for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
        if(!strcmpW(event_info[i].attr_name, str))
            return i;
    }

    return EVENTID_LAST;
}

230 231 232 233 234 235 236 237 238 239 240 241 242
static DWORD get_node_handler_mask(eventid_t eid)
{
    DWORD i;

    for(i=0; i<sizeof(node_handled_list)/sizeof(*node_handled_list); i++) {
        if(node_handled_list[i] == eid)
            return 1 << i;
    }

    ERR("Invalid eid %d\n", eid);
    return ~0;
}

243
typedef struct {
244
    DispatchEx dispex;
245
    IHTMLEventObj IHTMLEventObj_iface;
246

247
    LONG ref;
248 249

    HTMLDOMNode *target;
250
    const event_info_t *type;
251
    nsIDOMEvent *nsevent;
252
    BOOL prevent_default;
253
    BOOL cancel_bubble;
254 255
} HTMLEventObj;

256 257 258 259
static inline HTMLEventObj *impl_from_IHTMLEventObj(IHTMLEventObj *iface)
{
    return CONTAINING_RECORD(iface, HTMLEventObj, IHTMLEventObj_iface);
}
260 261 262

static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
{
263
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
264 265 266 267 268

    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
269
        *ppv = &This->IHTMLEventObj_iface;
270 271
    }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
        TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
272
        *ppv = &This->IHTMLEventObj_iface;
273 274
    }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
        return *ppv ? S_OK : E_NOINTERFACE;
275 276 277 278 279 280 281 282 283 284 285 286 287
    }

    if(*ppv) {
        IUnknown_AddRef((IUnknown*)*ppv);
        return S_OK;
    }

    WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
    return E_NOINTERFACE;
}

static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
{
288
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
289 290 291 292 293 294 295 296 297
    LONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    return ref;
}

static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
{
298
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
299 300 301 302
    LONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

303
    if(!ref) {
304 305
        if(This->target)
            IHTMLDOMNode_Release(&This->target->IHTMLDOMNode_iface);
306 307
        if(This->nsevent)
            nsIDOMEvent_Release(This->nsevent);
308
        release_dispex(&This->dispex);
309
        heap_free(This);
310
    }
311 312 313 314 315 316

    return ref;
}

static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
{
317
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
318
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
319 320 321 322 323
}

static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
                                              LCID lcid, ITypeInfo **ppTInfo)
{
324
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
325
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
326 327 328 329 330 331
}

static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
                                                LPOLESTR *rgszNames, UINT cNames,
                                                LCID lcid, DISPID *rgDispId)
{
332
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
333 334
    return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
            lcid, rgDispId);
335 336 337 338 339 340
}

static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
341
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
342
    return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
343
            wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
344 345 346 347
}

static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
{
348
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
349 350 351

    TRACE("(%p)->(%p)\n", This, p);

352 353
    return IHTMLDOMNode_QueryInterface(&This->target->IHTMLDOMNode_iface, &IID_IHTMLElement,
            (void**)p);
354 355 356 357
}

static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
{
358
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
359
    cpp_bool ret = FALSE;
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

    TRACE("(%p)->(%p)\n", This, p);

    if(This->nsevent) {
        nsIDOMKeyEvent *key_event;
        nsresult nsres;

        nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMKeyEvent_GetAltKey(key_event, &ret);
            nsIDOMKeyEvent_Release(key_event);
        }else {
            nsIDOMMouseEvent *mouse_event;

            nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
            if(NS_SUCCEEDED(nsres)) {
                nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
                nsIDOMMouseEvent_Release(mouse_event);
            }
        }
    }

    *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
    return S_OK;
384 385 386 387
}

static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
{
388
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
389
    cpp_bool ret = FALSE;
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

    TRACE("(%p)->(%p)\n", This, p);

    if(This->nsevent) {
        nsIDOMKeyEvent *key_event;
        nsresult nsres;

        nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
            nsIDOMKeyEvent_Release(key_event);
        }else {
            nsIDOMMouseEvent *mouse_event;

            nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
            if(NS_SUCCEEDED(nsres)) {
                nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
                nsIDOMMouseEvent_Release(mouse_event);
            }
        }
    }

    *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
    return S_OK;
414 415 416 417
}

static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
{
418
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
419
    cpp_bool ret = FALSE;
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

    TRACE("(%p)->(%p)\n", This, p);

    if(This->nsevent) {
        nsIDOMKeyEvent *key_event;
        nsresult nsres;

        nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
            nsIDOMKeyEvent_Release(key_event);
        }else {
            nsIDOMMouseEvent *mouse_event;

            nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
            if(NS_SUCCEEDED(nsres)) {
                nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
                nsIDOMMouseEvent_Release(mouse_event);
            }
        }
    }

    *p = ret ? VARIANT_TRUE : VARIANT_FALSE;
    return S_OK;
444 445 446 447
}

static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
{
448
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
449 450 451 452

    TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));

    if(V_VT(&v) != VT_BOOL) {
453
        FIXME("unsupported value %s\n", debugstr_variant(&v));
454 455 456 457 458 459
        return DISP_E_BADVARTYPE;
    }

    if(!V_BOOL(&v))
        This->prevent_default = TRUE;
    return S_OK;
460 461 462 463
}

static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
{
464
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
465

466
    FIXME("(%p)->(%p)\n", This, p);
467 468 469

    V_VT(p) = VT_EMPTY;
    return S_OK;
470 471 472 473
}

static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
{
474
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
475 476 477 478 479

    TRACE("(%p)->(%x)\n", This, v);

    This->cancel_bubble = !!v;
    return S_OK;
480 481 482 483
}

static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
{
484
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
485

486
    TRACE("(%p)->(%p)\n", This, p);
487

488
    *p = This->cancel_bubble ? VARIANT_TRUE : VARIANT_FALSE;
489
    return S_OK;
490 491 492 493
}

static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
{
494
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
495

496
    FIXME("(%p)->(%p)\n", This, p);
497 498 499

    *p = NULL;
    return S_OK;
500 501 502 503
}

static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
{
504
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
505

506
    FIXME("(%p)->(%p)\n", This, p);
507 508 509

    *p = NULL;
    return S_OK;
510 511
}

512
static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
513
{
514
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
515
    FIXME("(%p)->(%d)\n", This, v);
516 517 518
    return E_NOTIMPL;
}

519
static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
520
{
521
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
    PRUint32 key_code = 0;

    TRACE("(%p)->(%p)\n", This, p);

    if(This->nsevent) {
        nsIDOMKeyEvent *key_event;
        nsresult nsres;

        nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
            nsIDOMKeyEvent_Release(key_event);
        }
    }

    *p = key_code;
    return S_OK;
539 540
}

541
static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
542
{
543
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
    PRUint16 button = 0;

    TRACE("(%p)->(%p)\n", This, p);

    if(This->nsevent) {
        nsIDOMMouseEvent *mouse_event;
        nsresult nsres;

        nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMMouseEvent_GetButton(mouse_event, &button);
            nsIDOMMouseEvent_Release(mouse_event);
        }
    }

    *p = button;
    return S_OK;
561 562 563 564
}

static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
{
565
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
566 567 568 569 570

    TRACE("(%p)->(%p)\n", This, p);

    *p = SysAllocString(This->type->name);
    return *p ? S_OK : E_OUTOFMEMORY;
571 572 573 574
}

static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
{
575
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
576

577
    FIXME("(%p)->(%p)\n", This, p);
578 579 580

    *p = NULL;
    return S_OK;
581 582
}

583
static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
584
{
585
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
586

587
    FIXME("(%p)->(%p)\n", This, p);
588 589 590

    *p = 0;
    return S_OK;
591 592
}

593
static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
594
{
595
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
596

597
    FIXME("(%p)->(%p)\n", This, p);
598 599 600

    *p = -1;
    return S_OK;
601 602
}

603
static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
604
{
605
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
606

607
    FIXME("(%p)->(%p)\n", This, p);
608 609 610

    *p = -1;
    return S_OK;
611 612
}

613
static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
614
{
615
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
    PRInt32 x = 0;

    TRACE("(%p)->(%p)\n", This, p);

    if(This->nsevent) {
        nsIDOMMouseEvent *mouse_event;
        nsresult nsres;

        nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMMouseEvent_GetClientX(mouse_event, &x);
            nsIDOMMouseEvent_Release(mouse_event);
        }
    }

    *p = x;
    return S_OK;
633 634
}

635
static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
636
{
637
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    PRInt32 y = 0;

    TRACE("(%p)->(%p)\n", This, p);

    if(This->nsevent) {
        nsIDOMMouseEvent *mouse_event;
        nsresult nsres;

        nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMMouseEvent_GetClientY(mouse_event, &y);
            nsIDOMMouseEvent_Release(mouse_event);
        }
    }

    *p = y;
    return S_OK;
655 656
}

657
static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
658
{
659
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
660

661
    FIXME("(%p)->(%p)\n", This, p);
662 663 664

    *p = 0;
    return S_OK;
665 666
}

667
static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
668
{
669
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
670

671
    FIXME("(%p)->(%p)\n", This, p);
672 673 674

    *p = 0;
    return S_OK;
675 676
}

677
static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
678
{
679
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
    PRInt32 x = 0;

    TRACE("(%p)->(%p)\n", This, p);

    if(This->nsevent) {
        nsIDOMMouseEvent *mouse_event;
        nsresult nsres;

        nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
            nsIDOMMouseEvent_Release(mouse_event);
        }
    }

    *p = x;
    return S_OK;
697 698
}

699
static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
700
{
701
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
    PRInt32 y = 0;

    TRACE("(%p)->(%p)\n", This, p);

    if(This->nsevent) {
        nsIDOMMouseEvent *mouse_event;
        nsresult nsres;

        nsres = nsIDOMEvent_QueryInterface(This->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
            nsIDOMMouseEvent_Release(mouse_event);
        }
    }

    *p = y;
    return S_OK;
719 720 721 722
}

static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
{
723
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
724

725
    FIXME("(%p)->(%p)\n", This, p);
726 727 728

    *p = NULL;
    return S_OK;
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
}

static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
    HTMLEventObj_QueryInterface,
    HTMLEventObj_AddRef,
    HTMLEventObj_Release,
    HTMLEventObj_GetTypeInfoCount,
    HTMLEventObj_GetTypeInfo,
    HTMLEventObj_GetIDsOfNames,
    HTMLEventObj_Invoke,
    HTMLEventObj_get_srcElement,
    HTMLEventObj_get_altKey,
    HTMLEventObj_get_ctrlKey,
    HTMLEventObj_get_shiftKey,
    HTMLEventObj_put_returnValue,
    HTMLEventObj_get_returnValue,
    HTMLEventObj_put_cancelBubble,
    HTMLEventObj_get_cancelBubble,
    HTMLEventObj_get_fromElement,
    HTMLEventObj_get_toElement,
    HTMLEventObj_put_keyCode,
    HTMLEventObj_get_keyCode,
    HTMLEventObj_get_button,
    HTMLEventObj_get_type,
    HTMLEventObj_get_qualifier,
    HTMLEventObj_get_reason,
    HTMLEventObj_get_x,
    HTMLEventObj_get_y,
    HTMLEventObj_get_clientX,
    HTMLEventObj_get_clientY,
    HTMLEventObj_get_offsetX,
    HTMLEventObj_get_offsetY,
    HTMLEventObj_get_screenX,
    HTMLEventObj_get_screenY,
    HTMLEventObj_get_srcFilter
};

766 767 768 769 770 771 772 773 774 775 776 777
static const tid_t HTMLEventObj_iface_tids[] = {
    IHTMLEventObj_tid,
    0
};

static dispex_static_data_t HTMLEventObj_dispex = {
    NULL,
    DispCEventObj_tid,
    NULL,
    HTMLEventObj_iface_tids
};

778
static HTMLEventObj *create_event(HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
779 780 781
{
    HTMLEventObj *ret;

782
    ret = heap_alloc_zero(sizeof(*ret));
783 784 785
    if(!ret)
        return NULL;

786
    ret->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
787
    ret->ref = 1;
788
    ret->type = event_info+eid;
789

790
    ret->nsevent = nsevent;
791
    if(nsevent) {
792
        nsIDOMEvent_AddRef(nsevent);
793
    }else if(event_types[event_info[eid].type]) {
794
        nsAString type_str;
795 796
        nsresult nsres;

797 798 799
        nsAString_InitDepend(&type_str, event_types[event_info[eid].type]);
        nsres = nsIDOMHTMLDocument_CreateEvent(target->doc->nsdoc, &type_str, &ret->nsevent);
        nsAString_Finish(&type_str);
800 801
        if(NS_FAILED(nsres)) {
            ERR("Could not create event: %08x\n", nsres);
802
            IHTMLEventObj_Release(&ret->IHTMLEventObj_iface);
803 804 805 806 807
            return NULL;
        }
    }

    ret->target = target;
808
    IHTMLDOMNode_AddRef(&target->IHTMLDOMNode_iface);
809

810
    init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLEventObj_iface, &HTMLEventObj_dispex);
811

812
    return ret;
813 814
}

815
static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
816 817
{
    IDispatchEx *dispex;
818
    EXCEPINFO ei;
819 820 821 822 823 824
    HRESULT hres;

    memset(&ei, 0, sizeof(ei));

    hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
    if(SUCCEEDED(hres)) {
825
        hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, retv, &ei, NULL);
826 827 828 829
        IDispatchEx_Release(dispex);
    }else {
        TRACE("Could not get IDispatchEx interface: %08x\n", hres);
        hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
830
                dp, retv, &ei, NULL);
831 832 833 834 835
    }

    return hres;
}

836
static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, VARIANT *retv)
837 838 839 840 841 842
{
    DISPPARAMS dp = {NULL,NULL,0,0};
    ULONG argerr;
    EXCEPINFO ei;

    memset(&ei, 0, sizeof(ei));
843
    return IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, retv, &ei, &argerr);
844 845
}

846
static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
847
{
848
    int min, max, i;
849 850 851 852 853 854 855 856
    HRESULT hres;

    if(!data)
        return FALSE;

    if(!data->ids) {
        hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
        if(FAILED(hres))
857
            return FALSE;
858 859 860
    }

    min = 0;
861
    max = data->id_cnt-1;
862 863 864 865 866 867 868 869 870 871 872 873 874 875
    while(min <= max) {
        i = (min+max)/2;
        if(data->ids[i] == dispid)
            return TRUE;

        if(data->ids[i] < dispid)
            min = i+1;
        else
            max = i-1;
    }

    return FALSE;
}

876
static void call_event_handlers(HTMLDocumentNode *doc, HTMLEventObj *event_obj, event_target_t *event_target,
877
        ConnectionPointContainer *cp_container, eventid_t eid, IDispatch *this_obj)
878
{
879
    const BOOL cancelable = event_info[eid].flags & EVENT_CANCELABLE;
880
    handler_vector_t *handler_vector = NULL;
881
    VARIANT v;
882
    int i;
883 884
    HRESULT hres;

885 886
    if(event_target)
        handler_vector = event_target->event_table[eid];
887

888
    if(handler_vector && handler_vector->handler_prop) {
889 890 891 892 893 894
        DISPID named_arg = DISPID_THIS;
        VARIANTARG arg;
        DISPPARAMS dp = {&arg, &named_arg, 1, 1};

        V_VT(&arg) = VT_DISPATCH;
        V_DISPATCH(&arg) = this_obj;
895
        V_VT(&v) = VT_EMPTY;
896 897

        TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
898 899 900 901 902 903 904 905 906 907 908 909 910 911
        hres = call_disp_func(handler_vector->handler_prop, &dp, &v);
        if(hres == S_OK) {
            TRACE("%s <<< %s\n", debugstr_w(event_info[eid].name), debugstr_variant(&v));

            if(cancelable) {
                if(V_VT(&v) == VT_BOOL) {
                    if(!V_BOOL(&v))
                        event_obj->prevent_default = TRUE;
                }else if(V_VT(&v) != VT_EMPTY) {
                    FIXME("unhandled result %s\n", debugstr_variant(&v));
                }
            }
            VariantClear(&v);
        }else {
912
            WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
913
        }
914
    }
915

916
    if(handler_vector && handler_vector->handler_cnt) {
917 918 919 920 921 922
        VARIANTARG arg;
        DISPPARAMS dp = {&arg, NULL, 1, 0};

        V_VT(&arg) = VT_DISPATCH;
        V_DISPATCH(&arg) = (IDispatch*)event_obj;

923 924
        i = handler_vector->handler_cnt;
        while(i--) {
925
            if(handler_vector->handlers[i]) {
926 927
                V_VT(&v) = VT_EMPTY;

928
                TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
929 930
                hres = call_disp_func(handler_vector->handlers[i], &dp, &v);
                if(hres == S_OK) {
931
                    TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
932 933 934 935 936 937 938 939 940 941 942

                    if(cancelable) {
                        if(V_VT(&v) == VT_BOOL) {
                            if(!V_BOOL(&v))
                                event_obj->prevent_default = TRUE;
                        }else if(V_VT(&v) != VT_EMPTY) {
                            FIXME("unhandled result %s\n", debugstr_variant(&v));
                        }
                    }
                    VariantClear(&v);
                }else {
943
                    WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
944
                }
945 946 947
            }
        }
    }
948 949 950 951 952 953 954 955

    if(cp_container) {
        ConnectionPoint *cp;

        if(cp_container->forward_container)
            cp_container = cp_container->forward_container;

        for(cp = cp_container->cp_list; cp; cp = cp->next) {
956
            if(cp->sinks_size && is_cp_event(cp->data, event_info[eid].dispid)) {
957
                for(i=0; i < cp->sinks_size; i++) {
958 959 960
                    if(!cp->sinks[i].disp)
                        continue;

961 962
                    V_VT(&v) = VT_EMPTY;

963
                    TRACE("cp %s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
964 965
                    hres = call_cp_func(cp->sinks[i].disp, event_info[eid].dispid, &v);
                    if(hres == S_OK) {
966
                        TRACE("cp %s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
967 968 969 970 971 972 973 974 975 976 977

                        if(cancelable) {
                            if(V_VT(&v) == VT_BOOL) {
                                if(!V_BOOL(&v))
                                    event_obj->prevent_default = TRUE;
                            }else if(V_VT(&v) != VT_EMPTY) {
                                FIXME("unhandled result %s\n", debugstr_variant(&v));
                            }
                        }
                        VariantClear(&v);
                    }else {
978
                        WARN("cp %s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
979
                    }
980 981 982 983
                }
            }
        }
    }
984 985
}

986
void fire_event(HTMLDocumentNode *doc, eventid_t eid, BOOL set_event, nsIDOMNode *target, nsIDOMEvent *nsevent)
987
{
988 989
    HTMLEventObj *event_obj = NULL;
    IHTMLEventObj *prev_event;
990
    nsIDOMNode *parent, *nsnode;
991
    BOOL prevent_default = FALSE;
992
    HTMLInnerWindow *window;
993
    HTMLDOMNode *node;
994
    PRUint16 node_type;
995
    nsresult nsres;
996
    HRESULT hres;
997

998
    TRACE("(%p) %s\n", doc, debugstr_w(event_info[eid].name));
999

1000 1001
    window = doc->basedoc.window->base.inner_window;
    prev_event = window->event;
1002 1003 1004 1005 1006 1007
    if(set_event) {
        hres = get_node(doc, target, TRUE, &node);
        if(FAILED(hres))
            return;

        event_obj = create_event(node, eid, nsevent);
1008
        node_release(node);
1009
        window->event = &event_obj->IHTMLEventObj_iface;
1010
    }else {
1011
        window->event = NULL;
1012
    }
1013 1014

    nsIDOMNode_GetNodeType(target, &node_type);
1015 1016 1017
    nsnode = target;
    nsIDOMNode_AddRef(nsnode);

1018 1019 1020
    switch(node_type) {
    case ELEMENT_NODE:
        do {
1021
            hres = get_node(doc, nsnode, FALSE, &node);
1022
            if(SUCCEEDED(hres) && node) {
1023 1024
                call_event_handlers(doc, event_obj, *get_node_event_target(node),
                        node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1025 1026
                node_release(node);
            }
1027

1028
            if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1029 1030 1031 1032 1033 1034 1035
                break;

            nsIDOMNode_GetParentNode(nsnode, &parent);
            nsIDOMNode_Release(nsnode);
            nsnode = parent;
            if(!nsnode)
                break;
1036

1037 1038
            nsIDOMNode_GetNodeType(nsnode, &node_type);
        }while(node_type == ELEMENT_NODE);
1039

1040
        if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1041
            break;
1042

1043
    case DOCUMENT_NODE:
1044 1045 1046 1047
        if(event_info[eid].flags & EVENT_FORWARDBODY) {
            nsIDOMHTMLElement *nsbody;
            nsresult nsres;

1048
            nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
1049
            if(NS_SUCCEEDED(nsres) && nsbody) {
1050
                hres = get_node(doc, (nsIDOMNode*)nsbody, FALSE, &node);
1051
                if(SUCCEEDED(hres) && node) {
1052 1053
                    call_event_handlers(doc, event_obj, *get_node_event_target(node),
                            node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1054 1055
                    node_release(node);
                }
1056 1057 1058 1059 1060 1061
                nsIDOMHTMLElement_Release(nsbody);
            }else {
                ERR("Could not get body: %08x\n", nsres);
            }
        }

1062
        call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
1063
                (IDispatch*)&doc->basedoc.IHTMLDocument2_iface);
1064
        break;
1065

1066 1067
    default:
        FIXME("unimplemented node type %d\n", node_type);
1068 1069 1070 1071 1072
    }

    if(nsnode)
        nsIDOMNode_Release(nsnode);

1073 1074
    if(event_obj && event_obj->prevent_default)
        prevent_default = TRUE;
1075
    window->event = prev_event;
1076
    if(event_obj)
1077
        IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1078

1079
    if(!prevent_default && (event_info[eid].flags & EVENT_HASDEFAULTHANDLERS)) {
1080 1081 1082 1083 1084 1085 1086 1087
        nsIDOMNode_AddRef(target);
        nsnode = target;

        do {
            hres = get_node(doc, nsnode, TRUE, &node);
            if(FAILED(hres))
                break;

1088 1089 1090 1091
            if(node) {
                if(node->vtbl->handle_event)
                    hres = node->vtbl->handle_event(node, eid, nsevent, &prevent_default);
                node_release(node);
1092
                if(FAILED(hres) || prevent_default || (event_obj && event_obj->cancel_bubble))
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
                    break;
            }

            nsres = nsIDOMNode_GetParentNode(nsnode, &parent);
            if(NS_FAILED(nsres))
                break;

            nsIDOMNode_Release(nsnode);
            nsnode = parent;
        } while(nsnode);

        if(nsnode)
            nsIDOMNode_Release(nsnode);
    }

    if(prevent_default && nsevent) {
        TRACE("calling PreventDefault\n");
        nsIDOMEvent_PreventDefault(nsevent);
1111
    }
1112 1113
}

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_obj, VARIANT_BOOL *cancelled)
{
    eventid_t eid;

    eid = attr_to_eid(event_name);
    if(eid == EVENTID_LAST) {
        WARN("unknown event %s\n", debugstr_w(event_name));
        return E_INVALIDARG;
    }

    if(event_obj && V_VT(event_obj) != VT_EMPTY && V_VT(event_obj) != VT_ERROR)
        FIXME("event_obj not implemented\n");

    if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
        FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
        return E_NOTIMPL;
    }

1132
    fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1133 1134 1135 1136 1137

    *cancelled = VARIANT_TRUE; /* FIXME */
    return S_OK;
}

1138
HRESULT call_fire_event(HTMLDOMNode *node, eventid_t eid)
1139 1140 1141
{
    HRESULT hres;

1142
    if(node->vtbl->fire_event) {
1143 1144
        BOOL handled = FALSE;

1145
        hres = node->vtbl->fire_event(node, eid, &handled);
1146 1147 1148 1149
        if(handled)
            return hres;
    }

1150
    fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1151 1152 1153
    return S_OK;
}

1154 1155 1156 1157 1158 1159 1160 1161
static inline event_target_t *get_event_target(event_target_t **event_target_ptr)
{
    if(!*event_target_ptr)
        *event_target_ptr = heap_alloc_zero(sizeof(event_target_t));
    return *event_target_ptr;
}

static BOOL alloc_handler_vector(event_target_t *event_target, eventid_t eid, int cnt)
1162
{
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
    handler_vector_t *new_vector, *handler_vector = event_target->event_table[eid];

    if(handler_vector) {
        if(cnt <= handler_vector->handler_cnt)
            return TRUE;

        new_vector = heap_realloc_zero(handler_vector, sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
    }else {
        new_vector = heap_alloc_zero(sizeof(handler_vector_t) + sizeof(IDispatch*)*cnt);
    }

    if(!new_vector)
        return FALSE;
1176

1177 1178 1179 1180 1181
    new_vector->handler_cnt = cnt;
    event_target->event_table[eid] = new_vector;
    return TRUE;
}

1182
static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1183
{
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
    if(!doc->nsdoc)
        return S_OK;

    if(event_info[eid].flags & EVENT_NODEHANDLER) {
        DWORD mask;

        mask = get_node_handler_mask(eid);
        if(event_target->node_handlers_mask & mask)
            return S_OK;

        add_nsevent_listener(doc, nsnode, event_info[eid].name);
        event_target->node_handlers_mask |= mask;
        return S_OK;
    }

    if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER))
1200 1201 1202 1203
        return S_OK;

    if(!doc->event_vector[eid]) {
        doc->event_vector[eid] = TRUE;
1204
        add_nsevent_listener(doc, NULL, event_info[eid].name);
1205 1206 1207 1208 1209
    }

    return S_OK;
}

1210 1211
void detach_events(HTMLDocumentNode *doc)
{
1212 1213
    if(doc->event_vector) {
        int i;
1214

1215 1216 1217 1218
        for(i=0; i < EVENTID_LAST; i++) {
            if(doc->event_vector[i])
                detach_nsevent(doc, event_info[i].name);
        }
1219
    }
1220 1221

    release_nsevents(doc);
1222 1223 1224
}


1225 1226
static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
{
1227
    if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1228 1229 1230 1231 1232 1233 1234
        IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
        (*event_target)->event_table[eid]->handler_prop = NULL;
    }

    return S_OK;
}

1235
static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocumentNode *doc,
1236
        eventid_t eid, IDispatch *disp)
1237 1238 1239
{
    event_target_t *event_target;

1240 1241 1242
    if(!disp)
        return remove_event_handler(event_target_ptr, eid);

1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
    event_target = get_event_target(event_target_ptr);
    if(!event_target)
        return E_OUTOFMEMORY;

    if(!alloc_handler_vector(event_target, eid, 0))
        return E_OUTOFMEMORY;

    if(event_target->event_table[eid]->handler_prop)
        IDispatch_Release(event_target->event_table[eid]->handler_prop);

    event_target->event_table[eid]->handler_prop = disp;
1254 1255
    IDispatch_AddRef(disp);

1256
    return ensure_nsevent_handler(doc, event_target, nsnode, eid);
1257 1258
}

1259
HRESULT set_event_handler(event_target_t **event_target, nsIDOMNode *nsnode, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1260 1261
{
    switch(V_VT(var)) {
1262
    case VT_NULL:
1263
        return remove_event_handler(event_target, eid);
1264

1265
    case VT_DISPATCH:
1266
        return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1267 1268

    default:
1269
        FIXME("not handler %s\n", debugstr_variant(var));
1270
        /* fall through */
1271
    case VT_EMPTY:
1272
        return E_NOTIMPL;
1273 1274
    }

1275
    return S_OK;
1276 1277
}

1278 1279
HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
{
1280
    if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1281
        V_VT(var) = VT_DISPATCH;
1282
        V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1283 1284 1285 1286 1287 1288 1289 1290
        IDispatch_AddRef(V_DISPATCH(var));
    }else {
        V_VT(var) = VT_NULL;
    }

    return S_OK;
}

1291 1292
HRESULT attach_event(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocument *doc, BSTR name,
        IDispatch *disp, VARIANT_BOOL *res)
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
{
    event_target_t *event_target;
    eventid_t eid;
    DWORD i = 0;

    eid = attr_to_eid(name);
    if(eid == EVENTID_LAST) {
        WARN("Unknown event\n");
        *res = VARIANT_TRUE;
        return S_OK;
    }

    event_target = get_event_target(event_target_ptr);
    if(!event_target)
        return E_OUTOFMEMORY;

    if(event_target->event_table[eid]) {
        while(i < event_target->event_table[eid]->handler_cnt && event_target->event_table[eid]->handlers[i])
            i++;
        if(i == event_target->event_table[eid]->handler_cnt && !alloc_handler_vector(event_target, eid, i+1))
            return E_OUTOFMEMORY;
    }else if(!alloc_handler_vector(event_target, eid, i+1)) {
        return E_OUTOFMEMORY;
    }

    IDispatch_AddRef(disp);
    event_target->event_table[eid]->handlers[i] = disp;

    *res = VARIANT_TRUE;
1322
    return ensure_nsevent_handler(doc->doc_node, event_target, nsnode, eid);
1323 1324
}

1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
HRESULT detach_event(event_target_t *event_target, HTMLDocument *doc, BSTR name, IDispatch *disp)
{
    eventid_t eid;
    DWORD i = 0;

    if(!event_target)
        return S_OK;

    eid = attr_to_eid(name);
    if(eid == EVENTID_LAST) {
        WARN("Unknown event\n");
        return S_OK;
    }

    if(!event_target->event_table[eid])
        return S_OK;

    while(i < event_target->event_table[eid]->handler_cnt) {
        if(event_target->event_table[eid]->handlers[i] == disp) {
            IDispatch_Release(event_target->event_table[eid]->handlers[i]);
            event_target->event_table[eid]->handlers[i] = NULL;
        }
        i++;
    }

    return S_OK;
}

1353
void update_cp_events(HTMLInnerWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp, nsIDOMNode *nsnode)
1354
{
1355
    event_target_t *event_target;
1356 1357
    int i;

1358 1359 1360 1361
    event_target = get_event_target(event_target_ptr);
    if(!event_target)
        return; /* FIXME */

1362 1363
    for(i=0; i < EVENTID_LAST; i++) {
        if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1364
            ensure_nsevent_handler(window->doc, event_target, nsnode, i);
1365 1366 1367
    }
}

1368
void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
1369 1370 1371 1372 1373 1374 1375
{
    const PRUnichar *attr_value;
    nsAString attr_name_str, attr_value_str;
    IDispatch *disp;
    HTMLDOMNode *node;
    int i;
    nsresult nsres;
1376
    HRESULT hres;
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390

    nsAString_Init(&attr_value_str, NULL);
    nsAString_Init(&attr_name_str, NULL);

    for(i=0; i < EVENTID_LAST; i++) {
        nsAString_SetData(&attr_name_str, event_info[i].attr_name);
        nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
        if(NS_SUCCEEDED(nsres)) {
            nsAString_GetData(&attr_value_str, &attr_value);
            if(!*attr_value)
                continue;

            TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));

1391
            disp = script_parse_event(doc->basedoc.window->base.inner_window, attr_value);
1392
            if(disp) {
1393
                hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1394
                if(SUCCEEDED(hres)) {
1395
                    set_event_handler_disp(get_node_event_target(node), node->nsnode, node->doc, i, disp);
1396 1397
                    node_release(node);
                }
1398
                IDispatch_Release(disp);
1399 1400 1401 1402 1403 1404 1405 1406
            }
        }
    }

    nsAString_Finish(&attr_value_str);
    nsAString_Finish(&attr_name_str);
}

1407 1408
HRESULT doc_init_events(HTMLDocumentNode *doc)
{
1409 1410 1411
    unsigned i;
    HRESULT hres;

1412 1413 1414 1415 1416
    doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
    if(!doc->event_vector)
        return E_OUTOFMEMORY;

    init_nsevents(doc);
1417 1418

    for(i=0; i < EVENTID_LAST; i++) {
1419
        if(event_info[i].flags & EVENT_HASDEFAULTHANDLERS) {
1420 1421 1422 1423 1424 1425
            hres = ensure_nsevent_handler(doc, NULL, NULL, i);
            if(FAILED(hres))
                return hres;
        }
    }

1426 1427 1428
    return S_OK;
}

1429 1430
void release_event_target(event_target_t *event_target)
{
1431
    int i, j;
1432 1433

    for(i=0; i < EVENTID_LAST; i++) {
1434 1435 1436 1437
        if(event_target->event_table[i]) {
            if(event_target->event_table[i]->handler_prop)
                IDispatch_Release(event_target->event_table[i]->handler_prop);
            for(j=0; j < event_target->event_table[i]->handler_cnt; j++)
1438 1439
                if(event_target->event_table[i]->handlers[j])
                    IDispatch_Release(event_target->event_table[i]->handlers[j]);
1440
        }
1441 1442 1443 1444
    }

    heap_free(event_target);
}