htmlevent.c 74.2 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
 *
 * 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>
20
#include <assert.h>
21 22 23 24 25 26 27

#define COBJMACROS

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

#include "mshtml_private.h"
31
#include "htmlevent.h"
32
#include "htmlscript.h"
33 34 35 36 37

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

38
typedef enum {
39 40
    LISTENER_TYPE_CAPTURE,
    LISTENER_TYPE_BUBBLE,
41 42 43 44 45
    LISTENER_TYPE_ONEVENT,
    LISTENER_TYPE_ATTACHED
} listener_type_t;

typedef struct {
46
    struct list entry;
47 48 49 50
    listener_type_t type;
    IDispatch *function;
} event_listener_t;

51
typedef struct {
52
    struct wine_rb_entry entry;
53
    struct list listeners;
54
    WCHAR type[1];
55
} listener_container_t;
56

57 58
typedef enum {
    DISPATCH_BOTH,
59
    DISPATCH_STANDARD,
60 61 62
    DISPATCH_LEGACY
} dispatch_mode_t;

63
static const WCHAR abortW[] = {'a','b','o','r','t',0};
64
static const WCHAR beforeactivateW[] = {'b','e','f','o','r','e','a','c','t','i','v','a','t','e',0};
65
static const WCHAR beforeunloadW[] = {'b','e','f','o','r','e','u','n','l','o','a','d',0};
66
static const WCHAR blurW[] = {'b','l','u','r',0};
67
static const WCHAR changeW[] = {'c','h','a','n','g','e',0};
68
static const WCHAR clickW[] = {'c','l','i','c','k',0};
69
static const WCHAR contextmenuW[] = {'c','o','n','t','e','x','t','m','e','n','u',0};
70
static const WCHAR dataavailableW[] = {'d','a','t','a','a','v','a','i','l','a','b','l','e',0};
71
static const WCHAR dblclickW[] = {'d','b','l','c','l','i','c','k',0};
72
static const WCHAR dragW[] = {'d','r','a','g',0};
73
static const WCHAR dragstartW[] = {'d','r','a','g','s','t','a','r','t',0};
74
static const WCHAR errorW[] = {'e','r','r','o','r',0};
75
static const WCHAR focusW[] = {'f','o','c','u','s',0};
76
static const WCHAR focusinW[] = {'f','o','c','u','s','i','n',0};
77
static const WCHAR focusoutW[] = {'f','o','c','u','s','o','u','t',0};
78
static const WCHAR helpW[] = {'h','e','l','p',0};
79
static const WCHAR keydownW[] = {'k','e','y','d','o','w','n',0};
80
static const WCHAR keypressW[] = {'k','e','y','p','r','e','s','s',0};
81
static const WCHAR keyupW[] = {'k','e','y','u','p',0};
82
static const WCHAR loadW[] = {'l','o','a','d',0};
83
static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
84
static const WCHAR mousedownW[] = {'m','o','u','s','e','d','o','w','n',0};
85
static const WCHAR mousemoveW[] = {'m','o','u','s','e','m','o','v','e',0};
86
static const WCHAR mouseoutW[] = {'m','o','u','s','e','o','u','t',0};
87
static const WCHAR mouseoverW[] = {'m','o','u','s','e','o','v','e','r',0};
88
static const WCHAR mouseupW[] = {'m','o','u','s','e','u','p',0};
89
static const WCHAR mousewheelW[] = {'m','o','u','s','e','w','h','e','e','l',0};
90
static const WCHAR pasteW[] = {'p','a','s','t','e',0};
91
static const WCHAR readystatechangeW[] = {'r','e','a','d','y','s','t','a','t','e','c','h','a','n','g','e',0};
92
static const WCHAR resizeW[] = {'r','e','s','i','z','e',0};
93
static const WCHAR scrollW[] = {'s','c','r','o','l','l',0};
94
static const WCHAR selectstartW[] = {'s','e','l','e','c','t','s','t','a','r','t',0};
95
static const WCHAR selectionchangeW[] = {'s','e','l','e','c','t','i','o','n','c','h','a','n','g','e',0};
96
static const WCHAR submitW[] = {'s','u','b','m','i','t',0};
97
static const WCHAR unloadW[] = {'u','n','l','o','a','d',0};
98
static const WCHAR DOMContentLoadedW[] = {'D','O','M','C','o','n','t','e','n','t','L','o','a','d','e','d',0};
99

100 101
static const WCHAR EventW[] = {'E','v','e','n','t',0};
static const WCHAR UIEventW[] = {'U','I','E','v','e','n','t',0};
102 103 104
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};

105 106 107 108 109 110 111 112 113 114
typedef enum {
    EVENT_TYPE_EVENT,
    EVENT_TYPE_UIEVENT,
    EVENT_TYPE_KEYBOARD,
    EVENT_TYPE_MOUSE,
    EVENT_TYPE_FOCUS,
    EVENT_TYPE_DRAG,
    EVENT_TYPE_MESSAGE,
    EVENT_TYPE_CLIPBOARD
} event_type_t;
115 116

static const WCHAR *event_types[] = {
117 118
    EventW,
    UIEventW,
119
    KeyboardEventW,
120 121 122 123 124
    MouseEventW,
    EventW, /* FIXME */
    EventW, /* FIXME */
    EventW, /* FIXME */
    EventW  /* FIXME */
125 126
};

127
typedef struct {
128
    const WCHAR *name;
129
    event_type_t type;
130
    DISPID dispid;
131
    DWORD flags;
132 133
} event_info_t;

134
#define EVENT_DEFAULTLISTENER    0x0001
135
#define EVENT_BUBBLES            0x0002
136
#define EVENT_BIND_TO_BODY       0x0008
137
#define EVENT_CANCELABLE         0x0010
138
#define EVENT_HASDEFAULTHANDLERS 0x0020
139
#define EVENT_FIXME              0x0040
140

141
static const event_info_t event_info[] = {
142
    {abortW,             EVENT_TYPE_EVENT,     DISPID_EVMETH_ONABORT,
143
        EVENT_BIND_TO_BODY},
144 145 146 147 148
    {beforeactivateW,    EVENT_TYPE_EVENT,     DISPID_EVMETH_ONBEFOREACTIVATE,
        EVENT_FIXME | EVENT_BUBBLES | EVENT_CANCELABLE},
    {beforeunloadW,      EVENT_TYPE_EVENT,     DISPID_EVMETH_ONBEFOREUNLOAD,
        EVENT_DEFAULTLISTENER | EVENT_CANCELABLE },
    {blurW,              EVENT_TYPE_FOCUS,     DISPID_EVMETH_ONBLUR,
149
        EVENT_DEFAULTLISTENER},
150 151 152 153 154 155 156 157 158 159
    {changeW,            EVENT_TYPE_EVENT,     DISPID_EVMETH_ONCHANGE,
        EVENT_DEFAULTLISTENER | EVENT_BUBBLES},
    {clickW,             EVENT_TYPE_MOUSE,     DISPID_EVMETH_ONCLICK,
        EVENT_DEFAULTLISTENER | EVENT_HASDEFAULTHANDLERS | EVENT_BUBBLES | EVENT_CANCELABLE },
    {contextmenuW,       EVENT_TYPE_MOUSE,     DISPID_EVMETH_ONCONTEXTMENU,
        EVENT_BUBBLES | EVENT_CANCELABLE},
    {dataavailableW,     EVENT_TYPE_EVENT,     DISPID_EVMETH_ONDATAAVAILABLE,
        EVENT_FIXME | EVENT_BUBBLES},
    {dblclickW,          EVENT_TYPE_MOUSE,     DISPID_EVMETH_ONDBLCLICK,
        EVENT_DEFAULTLISTENER | EVENT_BUBBLES | EVENT_CANCELABLE},
160 161
    {DOMContentLoadedW,  EVENT_TYPE_EVENT,     0,
        EVENT_DEFAULTLISTENER | EVENT_BUBBLES | EVENT_CANCELABLE},
162 163 164 165 166
    {dragW,              EVENT_TYPE_DRAG,      DISPID_EVMETH_ONDRAG,
        EVENT_FIXME | EVENT_BUBBLES | EVENT_CANCELABLE},
    {dragstartW,         EVENT_TYPE_DRAG,      DISPID_EVMETH_ONDRAGSTART,
        EVENT_FIXME | EVENT_BUBBLES | EVENT_CANCELABLE},
    {errorW,             EVENT_TYPE_EVENT,     DISPID_EVMETH_ONERROR,
167
        EVENT_BIND_TO_BODY},
168
    {focusW,             EVENT_TYPE_FOCUS,     DISPID_EVMETH_ONFOCUS,
169
        EVENT_DEFAULTLISTENER},
170 171 172 173 174 175 176 177 178 179 180 181 182
    {focusinW,           EVENT_TYPE_FOCUS,     DISPID_EVMETH_ONFOCUSIN,
        EVENT_BUBBLES},
    {focusoutW,          EVENT_TYPE_FOCUS,     DISPID_EVMETH_ONFOCUSOUT,
        EVENT_BUBBLES},
    {helpW,              EVENT_TYPE_EVENT,     DISPID_EVMETH_ONHELP,
        EVENT_BUBBLES | EVENT_CANCELABLE},
    {keydownW,           EVENT_TYPE_KEYBOARD,  DISPID_EVMETH_ONKEYDOWN,
        EVENT_DEFAULTLISTENER | EVENT_HASDEFAULTHANDLERS | EVENT_BUBBLES | EVENT_CANCELABLE },
    {keypressW,          EVENT_TYPE_KEYBOARD,  DISPID_EVMETH_ONKEYPRESS,
        EVENT_DEFAULTLISTENER | EVENT_BUBBLES | EVENT_CANCELABLE},
    {keyupW,             EVENT_TYPE_KEYBOARD,  DISPID_EVMETH_ONKEYUP,
        EVENT_DEFAULTLISTENER | EVENT_BUBBLES | EVENT_CANCELABLE},
    {loadW,              EVENT_TYPE_UIEVENT,   DISPID_EVMETH_ONLOAD,
183
        EVENT_BIND_TO_BODY},
184
    {messageW,           EVENT_TYPE_MESSAGE,   DISPID_EVMETH_ONMESSAGE,
185
        0},
186 187 188 189 190 191 192 193 194 195 196
    {mousedownW,         EVENT_TYPE_MOUSE,     DISPID_EVMETH_ONMOUSEDOWN,
        EVENT_DEFAULTLISTENER | EVENT_BUBBLES | EVENT_CANCELABLE},
    {mousemoveW,         EVENT_TYPE_MOUSE,     DISPID_EVMETH_ONMOUSEMOVE,
        EVENT_DEFAULTLISTENER | EVENT_BUBBLES | EVENT_CANCELABLE},
    {mouseoutW,          EVENT_TYPE_MOUSE,     DISPID_EVMETH_ONMOUSEOUT,
        EVENT_DEFAULTLISTENER | EVENT_BUBBLES | EVENT_CANCELABLE},
    {mouseoverW,         EVENT_TYPE_MOUSE,     DISPID_EVMETH_ONMOUSEOVER,
        EVENT_DEFAULTLISTENER | EVENT_BUBBLES | EVENT_CANCELABLE},
    {mouseupW,           EVENT_TYPE_MOUSE,     DISPID_EVMETH_ONMOUSEUP,
        EVENT_DEFAULTLISTENER | EVENT_BUBBLES | EVENT_CANCELABLE},
    {mousewheelW,        EVENT_TYPE_MOUSE,     DISPID_EVMETH_ONMOUSEWHEEL,
197
        EVENT_FIXME},
198 199 200
    {pasteW,             EVENT_TYPE_CLIPBOARD, DISPID_EVMETH_ONPASTE,
        EVENT_FIXME | EVENT_BUBBLES | EVENT_CANCELABLE},
    {readystatechangeW,  EVENT_TYPE_EVENT,     DISPID_EVMETH_ONREADYSTATECHANGE,
201
        0},
202 203 204 205 206
    {resizeW,            EVENT_TYPE_UIEVENT,   DISPID_EVMETH_ONRESIZE,
        EVENT_DEFAULTLISTENER},
    {scrollW,            EVENT_TYPE_UIEVENT,   DISPID_EVMETH_ONSCROLL,
        EVENT_DEFAULTLISTENER | EVENT_BUBBLES /* FIXME: not for elements */},
    {selectionchangeW,   EVENT_TYPE_EVENT,     DISPID_EVMETH_ONSELECTIONCHANGE,
207
        EVENT_FIXME},
208 209 210 211 212
    {selectstartW,       EVENT_TYPE_EVENT,     DISPID_EVMETH_ONSELECTSTART,
        EVENT_FIXME | EVENT_BUBBLES | EVENT_CANCELABLE},
    {submitW,            EVENT_TYPE_EVENT,     DISPID_EVMETH_ONSUBMIT,
        EVENT_DEFAULTLISTENER | EVENT_HASDEFAULTHANDLERS | EVENT_BUBBLES | EVENT_CANCELABLE},
    {unloadW,            EVENT_TYPE_UIEVENT,   DISPID_EVMETH_ONUNLOAD,
213
        EVENT_FIXME}
214 215
};

216 217
static BOOL use_event_quirks(EventTarget*);

218
static eventid_t str_to_eid(const WCHAR *str)
219 220 221 222 223 224 225 226 227 228 229
{
    int i;

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

    return EVENTID_LAST;
}

230
static eventid_t attr_to_eid(const WCHAR *str)
231 232 233
{
    int i;

234 235 236
    if((str[0] != 'o' && str[0] != 'O') || (str[1] != 'n' && str[1] != 'N'))
        return EVENTID_LAST;

237
    for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
238
        if(!strcmpW(event_info[i].name, str+2) && event_info[i].dispid)
239 240 241 242 243 244
            return i;
    }

    return EVENTID_LAST;
}

245
static listener_container_t *get_listener_container(EventTarget *event_target, const WCHAR *type, BOOL alloc)
246 247 248 249
{
    const event_target_vtbl_t *vtbl;
    listener_container_t *container;
    struct wine_rb_entry *entry;
250
    size_t type_len;
251
    eventid_t eid;
252

253
    entry = wine_rb_get(&event_target->handler_map, type);
254 255 256 257 258
    if(entry)
        return WINE_RB_ENTRY_VALUE(entry, listener_container_t, entry);
    if(!alloc)
        return NULL;

259 260
    eid = str_to_eid(type);
    if(eid != EVENTID_LAST && (event_info[eid].flags & EVENT_FIXME))
261 262
        FIXME("unimplemented event %s\n", debugstr_w(event_info[eid].name));

263
    type_len = strlenW(type);
264
    container = heap_alloc(FIELD_OFFSET(listener_container_t, type[type_len+1]));
265 266
    if(!container)
        return NULL;
267
    memcpy(container->type, type, (type_len + 1) * sizeof(WCHAR));
268 269 270 271 272 273 274
    list_init(&container->listeners);
    vtbl = dispex_get_vtbl(&event_target->dispex);
    if(vtbl->bind_event)
        vtbl->bind_event(&event_target->dispex, eid);
    else
        FIXME("Unsupported event binding on target %p\n", event_target);

275
    wine_rb_put(&event_target->handler_map, container->type, &container->entry);
276 277 278
    return container;
}

279
static void remove_event_listener(EventTarget *event_target, const WCHAR *type_name, listener_type_t type, IDispatch *function)
280 281 282 283
{
    listener_container_t *container;
    event_listener_t *listener;

284
    container = get_listener_container(event_target, type_name, FALSE);
285 286 287 288 289 290 291 292 293 294 295 296 297
    if(!container)
        return;

    LIST_FOR_EACH_ENTRY(listener, &container->listeners, event_listener_t, entry) {
        if(listener->function == function && listener->type == type) {
            IDispatch_Release(listener->function);
            list_remove(&listener->entry);
            heap_free(listener);
            break;
        }
    }
}

298
typedef struct {
299
    DispatchEx dispex;
300
    IHTMLEventObj IHTMLEventObj_iface;
301

302
    LONG ref;
303

304
    DOMEvent *event;
305
    VARIANT return_value;
306
} HTMLEventObj;
307

308 309 310 311
static inline HTMLEventObj *impl_from_IHTMLEventObj(IHTMLEventObj *iface)
{
    return CONTAINING_RECORD(iface, HTMLEventObj, IHTMLEventObj_iface);
}
312 313 314

static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
{
315
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
316

317
    TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
318 319

    if(IsEqualGUID(&IID_IUnknown, riid)) {
320
        *ppv = &This->IHTMLEventObj_iface;
321
    }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
322
        *ppv = &This->IHTMLEventObj_iface;
323 324
    }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
        return *ppv ? S_OK : E_NOINTERFACE;
325 326 327 328
    }else {
        *ppv = NULL;
        WARN("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
        return E_NOINTERFACE;
329 330
    }

331 332
    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
333 334 335 336
}

static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
{
337
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
338 339 340 341 342 343 344 345 346
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
{
347
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
348 349 350 351
    LONG ref = InterlockedDecrement(&This->ref);

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

352
    if(!ref) {
353 354
        if(This->event)
            IDOMEvent_Release(&This->event->IDOMEvent_iface);
355
        release_dispex(&This->dispex);
356
        heap_free(This);
357
    }
358 359 360 361 362 363

    return ref;
}

static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
{
364
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
365
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
366 367 368 369 370
}

static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
                                              LCID lcid, ITypeInfo **ppTInfo)
{
371
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
372
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
373 374 375 376 377 378
}

static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
                                                LPOLESTR *rgszNames, UINT cNames,
                                                LCID lcid, DISPID *rgDispId)
{
379
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
380 381
    return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
            lcid, rgDispId);
382 383 384 385 386 387
}

static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
388
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
389
    return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
390
            wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
391 392 393 394
}

static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
{
395
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
396 397 398

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

399
    *p = NULL;
400 401
    if(This->event && This->event->target)
        IDispatchEx_QueryInterface(&This->event->target->dispex.IDispatchEx_iface, &IID_IHTMLElement, (void**)p);
402
    return S_OK;
403 404 405 406
}

static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
{
407
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
408
    cpp_bool ret = FALSE;
409 410 411

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

412
    if(This->event) {
413 414 415
        nsIDOMKeyEvent *key_event;
        nsresult nsres;

416
        nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
417 418 419 420 421 422
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMKeyEvent_GetAltKey(key_event, &ret);
            nsIDOMKeyEvent_Release(key_event);
        }else {
            nsIDOMMouseEvent *mouse_event;

423
            nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
424 425 426 427 428 429 430
            if(NS_SUCCEEDED(nsres)) {
                nsIDOMMouseEvent_GetAltKey(mouse_event, &ret);
                nsIDOMMouseEvent_Release(mouse_event);
            }
        }
    }

431
    *p = variant_bool(ret);
432
    return S_OK;
433 434 435 436
}

static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
{
437
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
438
    cpp_bool ret = FALSE;
439 440 441

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

442
    if(This->event) {
443 444 445
        nsIDOMKeyEvent *key_event;
        nsresult nsres;

446
        nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
447 448 449 450 451 452
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMKeyEvent_GetCtrlKey(key_event, &ret);
            nsIDOMKeyEvent_Release(key_event);
        }else {
            nsIDOMMouseEvent *mouse_event;

453
            nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
454 455 456 457 458 459 460
            if(NS_SUCCEEDED(nsres)) {
                nsIDOMMouseEvent_GetCtrlKey(mouse_event, &ret);
                nsIDOMMouseEvent_Release(mouse_event);
            }
        }
    }

461
    *p = variant_bool(ret);
462
    return S_OK;
463 464 465 466
}

static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
{
467
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
468
    cpp_bool ret = FALSE;
469 470 471

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

472
    if(This->event) {
473 474 475
        nsIDOMKeyEvent *key_event;
        nsresult nsres;

476
        nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
477 478 479 480 481 482
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMKeyEvent_GetShiftKey(key_event, &ret);
            nsIDOMKeyEvent_Release(key_event);
        }else {
            nsIDOMMouseEvent *mouse_event;

483
            nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
484 485 486 487 488 489 490
            if(NS_SUCCEEDED(nsres)) {
                nsIDOMMouseEvent_GetShiftKey(mouse_event, &ret);
                nsIDOMMouseEvent_Release(mouse_event);
            }
        }
    }

491
    *p = variant_bool(ret);
492
    return S_OK;
493 494 495 496
}

static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
{
497
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
498 499 500 501

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

    if(V_VT(&v) != VT_BOOL) {
502
        FIXME("unsupported value %s\n", debugstr_variant(&v));
503 504 505
        return DISP_E_BADVARTYPE;
    }

506
    This->return_value = v;
507 508
    if(!V_BOOL(&v) && This->event)
        IDOMEvent_preventDefault(&This->event->IDOMEvent_iface);
509
    return S_OK;
510 511 512 513
}

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

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

518
    V_VT(p) = VT_EMPTY;
519
    return VariantCopy(p, &This->return_value);
520 521 522 523
}

static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
{
524
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
525 526 527

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

528 529
    if(This->event)
        IDOMEvent_stopPropagation(&This->event->IDOMEvent_iface);
530
    return S_OK;
531 532 533 534
}

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

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

539
    *p = variant_bool(This->event && This->event->stop_propagation);
540
    return S_OK;
541 542 543 544
}

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

547
    FIXME("(%p)->(%p)\n", This, p);
548 549 550

    *p = NULL;
    return S_OK;
551 552 553 554
}

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

557
    FIXME("(%p)->(%p)\n", This, p);
558 559 560

    *p = NULL;
    return S_OK;
561 562
}

563
static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
564
{
565
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
566
    FIXME("(%p)->(%d)\n", This, v);
567 568 569
    return E_NOTIMPL;
}

570
static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
571
{
572
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
573
    UINT32 key_code = 0;
574 575 576

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

577
    if(This->event) {
578 579 580
        nsIDOMKeyEvent *key_event;
        nsresult nsres;

581
        nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMKeyEvent, (void**)&key_event);
582 583 584 585 586 587 588 589
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMKeyEvent_GetKeyCode(key_event, &key_code);
            nsIDOMKeyEvent_Release(key_event);
        }
    }

    *p = key_code;
    return S_OK;
590 591
}

592
static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
593
{
594
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
595
    INT16 button = 0;
596 597 598

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

599
    if(This->event) {
600 601 602
        nsIDOMMouseEvent *mouse_event;
        nsresult nsres;

603
        nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
604 605 606 607 608 609 610 611
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMMouseEvent_GetButton(mouse_event, &button);
            nsIDOMMouseEvent_Release(mouse_event);
        }
    }

    *p = button;
    return S_OK;
612 613 614 615
}

static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
{
616
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
617 618 619

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

620
    if(!This->event) {
621 622 623 624
        *p = NULL;
        return S_OK;
    }

625
    return IDOMEvent_get_type(&This->event->IDOMEvent_iface, p);
626 627 628 629
}

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

632
    FIXME("(%p)->(%p)\n", This, p);
633 634 635

    *p = NULL;
    return S_OK;
636 637
}

638
static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
639
{
640
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
641

642
    FIXME("(%p)->(%p)\n", This, p);
643 644 645

    *p = 0;
    return S_OK;
646 647
}

648
static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
649
{
650
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
651
    LONG x = 0;
652

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

655
    if(This->event) {
656 657 658
        nsIDOMUIEvent *ui_event;
        nsresult nsres;

659
        nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMUIEvent, (void**)&ui_event);
660 661 662 663 664 665 666 667 668
        if(NS_SUCCEEDED(nsres)) {
            /* NOTE: pageX is not exactly right here. */
            nsres = nsIDOMUIEvent_GetPageX(ui_event, &x);
            assert(nsres == NS_OK);
            nsIDOMUIEvent_Release(ui_event);
        }
    }

    *p = x;
669
    return S_OK;
670 671
}

672
static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
673
{
674
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
675
    LONG y = 0;
676

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

679
    if(This->event) {
680 681 682
        nsIDOMUIEvent *ui_event;
        nsresult nsres;

683
        nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMUIEvent, (void**)&ui_event);
684 685 686 687 688 689 690
        if(NS_SUCCEEDED(nsres)) {
            /* NOTE: pageY is not exactly right here. */
            nsres = nsIDOMUIEvent_GetPageY(ui_event, &y);
            assert(nsres == NS_OK);
            nsIDOMUIEvent_Release(ui_event);
        }
    }
691

692
    *p = y;
693
    return S_OK;
694 695
}

696
static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, LONG *p)
697
{
698
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
699
    LONG x = 0;
700 701 702

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

703
    if(This->event) {
704 705 706
        nsIDOMMouseEvent *mouse_event;
        nsresult nsres;

707
        nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
708 709 710 711 712 713 714 715
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMMouseEvent_GetClientX(mouse_event, &x);
            nsIDOMMouseEvent_Release(mouse_event);
        }
    }

    *p = x;
    return S_OK;
716 717
}

718
static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
719
{
720
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
721
    LONG y = 0;
722 723 724

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

725
    if(This->event) {
726 727 728
        nsIDOMMouseEvent *mouse_event;
        nsresult nsres;

729
        nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
730 731 732 733 734 735 736 737
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMMouseEvent_GetClientY(mouse_event, &y);
            nsIDOMMouseEvent_Release(mouse_event);
        }
    }

    *p = y;
    return S_OK;
738 739
}

740
static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
741
{
742
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
743

744
    FIXME("(%p)->(%p)\n", This, p);
745 746 747

    *p = 0;
    return S_OK;
748 749
}

750
static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
751
{
752
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
753

754
    FIXME("(%p)->(%p)\n", This, p);
755 756 757

    *p = 0;
    return S_OK;
758 759
}

760
static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, LONG *p)
761
{
762
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
763
    LONG x = 0;
764 765 766

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

767
    if(This->event) {
768 769 770
        nsIDOMMouseEvent *mouse_event;
        nsresult nsres;

771
        nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
772 773 774 775 776 777 778 779
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMMouseEvent_GetScreenX(mouse_event, &x);
            nsIDOMMouseEvent_Release(mouse_event);
        }
    }

    *p = x;
    return S_OK;
780 781
}

782
static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
783
{
784
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
785
    LONG y = 0;
786 787 788

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

789
    if(This->event) {
790 791 792
        nsIDOMMouseEvent *mouse_event;
        nsresult nsres;

793
        nsres = nsIDOMEvent_QueryInterface(This->event->nsevent, &IID_nsIDOMMouseEvent, (void**)&mouse_event);
794 795 796 797 798 799 800 801
        if(NS_SUCCEEDED(nsres)) {
            nsIDOMMouseEvent_GetScreenY(mouse_event, &y);
            nsIDOMMouseEvent_Release(mouse_event);
        }
    }

    *p = y;
    return S_OK;
802 803 804 805
}

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

808
    FIXME("(%p)->(%p)\n", This, p);
809 810 811

    *p = NULL;
    return S_OK;
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
}

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
};

849 850 851 852 853
static inline HTMLEventObj *unsafe_impl_from_IHTMLEventObj(IHTMLEventObj *iface)
{
    return iface->lpVtbl == &HTMLEventObjVtbl ? impl_from_IHTMLEventObj(iface) : NULL;
}

854 855 856 857 858 859 860 861 862 863 864
static const tid_t HTMLEventObj_iface_tids[] = {
    IHTMLEventObj_tid,
    0
};

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

865
static HTMLEventObj *alloc_event_obj(DOMEvent *event)
866
{
867
    HTMLEventObj *event_obj;
868

869 870
    event_obj = heap_alloc_zero(sizeof(*event_obj));
    if(!event_obj)
871 872
        return NULL;

873 874 875 876 877 878 879 880 881 882 883 884 885
    event_obj->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
    event_obj->ref = 1;
    event_obj->event = event;
    if(event)
        IDOMEvent_AddRef(&event->IDOMEvent_iface);

    init_dispex(&event_obj->dispex, (IUnknown*)&event_obj->IHTMLEventObj_iface, &HTMLEventObj_dispex);
    return event_obj;
}

HRESULT create_event_obj(IHTMLEventObj **ret)
{
    HTMLEventObj *event_obj;
886

887 888 889
    event_obj = alloc_event_obj(NULL);
    if(!event_obj)
        return E_OUTOFMEMORY;
890

891 892
    *ret = &event_obj->IHTMLEventObj_iface;
    return S_OK;
893 894
}

895 896 897 898 899
static inline DOMEvent *impl_from_IDOMEvent(IDOMEvent *iface)
{
    return CONTAINING_RECORD(iface, DOMEvent, IDOMEvent_iface);
}

900 901 902 903 904 905 906
static const IDOMEventVtbl DOMEventVtbl;

static inline DOMEvent *unsafe_impl_from_IDOMEvent(IDOMEvent *iface)
{
    return iface && iface->lpVtbl == &DOMEventVtbl ? impl_from_IDOMEvent(iface) : NULL;
}

907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
static HRESULT WINAPI DOMEvent_QueryInterface(IDOMEvent *iface, REFIID riid, void **ppv)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);

    TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);

    if(IsEqualGUID(&IID_IUnknown, riid))
        *ppv = &This->IDOMEvent_iface;
    else if(IsEqualGUID(&IID_IDOMEvent, riid))
        *ppv = &This->IDOMEvent_iface;
    else if(dispex_query_interface(&This->dispex, riid, ppv))
        return *ppv ? S_OK : E_NOINTERFACE;
    else {
        *ppv = NULL;
        WARN("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
        return E_NOINTERFACE;
    }

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

static ULONG WINAPI DOMEvent_AddRef(IDOMEvent *iface)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI DOMEvent_Release(IDOMEvent *iface)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
    LONG ref = InterlockedDecrement(&This->ref);

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

    if(!ref) {
947 948
        if(This->target)
            IDispatchEx_Release(&This->target->dispex.IDispatchEx_iface);
949 950
        nsIDOMEvent_Release(This->nsevent);
        release_dispex(&This->dispex);
951
        heap_free(This->type);
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI DOMEvent_GetTypeInfoCount(IDOMEvent *iface, UINT *pctinfo)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
}

static HRESULT WINAPI DOMEvent_GetTypeInfo(IDOMEvent *iface, UINT iTInfo,
                                              LCID lcid, ITypeInfo **ppTInfo)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
}

static HRESULT WINAPI DOMEvent_GetIDsOfNames(IDOMEvent *iface, REFIID riid,
        LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
    return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
            lcid, rgDispId);
}

static HRESULT WINAPI DOMEvent_Invoke(IDOMEvent *iface, DISPID dispIdMember,
        REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
        EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
    return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
            wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
}

static HRESULT WINAPI DOMEvent_get_bubbles(IDOMEvent *iface, VARIANT_BOOL *p)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
991 992 993 994 995

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

    *p = variant_bool(This->bubbles);
    return S_OK;
996 997 998 999 1000
}

static HRESULT WINAPI DOMEvent_get_cancelable(IDOMEvent *iface, VARIANT_BOOL *p)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
1001 1002 1003 1004 1005

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

    *p = variant_bool(This->cancelable);
    return S_OK;
1006 1007 1008 1009 1010
}

static HRESULT WINAPI DOMEvent_get_currentTarget(IDOMEvent *iface, IEventTarget **p)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
1011 1012 1013 1014 1015 1016 1017 1018

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

    if(This->current_target)
        IEventTarget_AddRef(*p = &This->current_target->IEventTarget_iface);
    else
        *p = NULL;
    return S_OK;
1019 1020 1021 1022 1023
}

static HRESULT WINAPI DOMEvent_get_defaultPrevented(IDOMEvent *iface, VARIANT_BOOL *p)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
1024 1025 1026 1027 1028

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

    *p = variant_bool(This->prevent_default);
    return S_OK;
1029 1030 1031 1032 1033
}

static HRESULT WINAPI DOMEvent_get_eventPhase(IDOMEvent *iface, USHORT *p)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
1034 1035 1036 1037 1038

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

    *p = This->phase;
    return S_OK;
1039 1040 1041 1042 1043
}

static HRESULT WINAPI DOMEvent_get_target(IDOMEvent *iface, IEventTarget **p)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
1044 1045 1046 1047 1048 1049 1050 1051

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

    if(This->target)
        IEventTarget_AddRef(*p = &This->target->IEventTarget_iface);
    else
        *p = NULL;
    return S_OK;
1052 1053 1054 1055 1056
}

static HRESULT WINAPI DOMEvent_get_timeStamp(IDOMEvent *iface, ULONGLONG *p)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
1057 1058 1059 1060 1061

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

    *p = This->time_stamp;
    return S_OK;
1062 1063 1064 1065 1066
}

static HRESULT WINAPI DOMEvent_get_type(IDOMEvent *iface, BSTR *p)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

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

    if(This->type) {
        *p = SysAllocString(This->type);
        if(!*p)
            return E_OUTOFMEMORY;
    }else {
        *p = NULL;
    }
    return S_OK;
1078 1079
}

1080 1081 1082 1083 1084 1085 1086
#ifdef __i386__
#define nsIDOMEvent_InitEvent(_this,type,bubbles,cancelable) \
    ((void (WINAPI*)(void*,nsIDOMEvent*,const nsAString*,cpp_bool,cpp_bool)) \
     &call_thiscall_func)((_this)->lpVtbl->InitEvent,_this,type,bubbles,cancelable)

#endif

1087 1088 1089
static HRESULT WINAPI DOMEvent_initEvent(IDOMEvent *iface, BSTR type, VARIANT_BOOL can_bubble, VARIANT_BOOL cancelable)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
    nsAString nsstr;

    TRACE("(%p)->(%s %x %x)\n", This, debugstr_w(type), can_bubble, cancelable);

    if(This->target) {
        TRACE("called on already dispatched event\n");
        return S_OK;
    }

    heap_free(This->type);
    This->type = heap_strdupW(type);
    if(!This->type)
        return E_OUTOFMEMORY;
    This->event_id = str_to_eid(type);

    This->bubbles = !!can_bubble;
    This->cancelable = !!cancelable;

    nsAString_InitDepend(&nsstr, type);
    nsIDOMEvent_InitEvent(This->nsevent, &nsstr, This->bubbles, This->cancelable);
    nsAString_Finish(&nsstr);

    return S_OK;
1113 1114 1115 1116 1117
}

static HRESULT WINAPI DOMEvent_preventDefault(IDOMEvent *iface)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
1118 1119 1120

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

1121 1122 1123 1124
    if(This->current_target && This->cancelable) {
        This->prevent_default = TRUE;
        nsIDOMEvent_PreventDefault(This->nsevent);
    }
1125
    return S_OK;
1126 1127 1128 1129 1130
}

static HRESULT WINAPI DOMEvent_stopPropagation(IDOMEvent *iface)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
1131 1132 1133 1134 1135 1136

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

    This->stop_propagation = TRUE;
    nsIDOMEvent_StopPropagation(This->nsevent);
    return S_OK;
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
}

static HRESULT WINAPI DOMEvent_stopImmediatePropagation(IDOMEvent *iface)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI DOMEvent_get_isTrusted(IDOMEvent *iface, VARIANT_BOOL *p)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI DOMEvent_put_cancelBubble(IDOMEvent *iface, VARIANT_BOOL v)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
    FIXME("(%p)->(%x)\n", This, v);
    return E_NOTIMPL;
}

static HRESULT WINAPI DOMEvent_get_cancelBubble(IDOMEvent *iface, VARIANT_BOOL *p)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI DOMEvent_get_srcElement(IDOMEvent *iface, IHTMLElement **p)
{
    DOMEvent *This = impl_from_IDOMEvent(iface);
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static const IDOMEventVtbl DOMEventVtbl = {
    DOMEvent_QueryInterface,
    DOMEvent_AddRef,
    DOMEvent_Release,
    DOMEvent_GetTypeInfoCount,
    DOMEvent_GetTypeInfo,
    DOMEvent_GetIDsOfNames,
    DOMEvent_Invoke,
    DOMEvent_get_bubbles,
    DOMEvent_get_cancelable,
    DOMEvent_get_currentTarget,
    DOMEvent_get_defaultPrevented,
    DOMEvent_get_eventPhase,
    DOMEvent_get_target,
    DOMEvent_get_timeStamp,
    DOMEvent_get_type,
    DOMEvent_initEvent,
    DOMEvent_preventDefault,
    DOMEvent_stopPropagation,
    DOMEvent_stopImmediatePropagation,
    DOMEvent_get_isTrusted,
    DOMEvent_put_cancelBubble,
    DOMEvent_get_cancelBubble,
    DOMEvent_get_srcElement
};

static const tid_t DOMEvent_iface_tids[] = {
    IDOMEvent_tid,
    0
};

static dispex_static_data_t DOMEvent_dispex = {
    NULL,
    IDOMEvent_tid,
    DOMEvent_iface_tids
};

1211
static DOMEvent *alloc_event(nsIDOMEvent *nsevent, eventid_t event_id)
1212 1213
{
    DOMEvent *event;
1214 1215 1216 1217
    FILETIME time;

    /* 1601 to 1970 is 369 years plus 89 leap days */
    const ULONGLONG time_epoch = (ULONGLONG)(369 * 365 + 89) * 86400 * 1000;
1218 1219 1220

    event = heap_alloc_zero(sizeof(*event));
    if(!event)
1221
        return NULL;
1222 1223 1224 1225

    init_dispex(&event->dispex, (IUnknown*)&event->IDOMEvent_iface, &DOMEvent_dispex);
    event->IDOMEvent_iface.lpVtbl = &DOMEventVtbl;
    event->ref = 1;
1226 1227 1228 1229 1230 1231 1232
    event->event_id = event_id;
    if(event_id != EVENTID_LAST) {
        event->type = heap_strdupW(event_info[event_id].name);
        if(!event->type) {
            IDOMEvent_Release(&event->IDOMEvent_iface);
            return NULL;
        }
1233
        event->bubbles = (event_info[event_id].flags & EVENT_BUBBLES) != 0;
1234
        event->cancelable = (event_info[event_id].flags & EVENT_CANCELABLE) != 0;
1235
    }
1236
    nsIDOMEvent_AddRef(event->nsevent = nsevent);
1237 1238 1239 1240 1241

    GetSystemTimeAsFileTime(&time);
    event->time_stamp = (((ULONGLONG)time.dwHighDateTime<<32) + time.dwLowDateTime) / 10000
        - time_epoch;

1242 1243 1244
    return event;
}

1245
HRESULT create_event_from_nsevent(nsIDOMEvent *nsevent, DOMEvent **ret_event)
1246
{
1247
    eventid_t event_id = EVENTID_LAST;
1248 1249 1250 1251 1252
    DOMEvent *event;
    nsAString nsstr;
    nsresult nsres;

    nsAString_Init(&nsstr, NULL);
1253
    nsres = nsIDOMEvent_GetType(nsevent, &nsstr);
1254 1255 1256
    if(NS_SUCCEEDED(nsres)) {
        const WCHAR *type;
        nsAString_GetData(&nsstr, &type);
1257 1258
        event_id = str_to_eid(type);
        if(event_id == EVENTID_LAST)
1259 1260 1261 1262 1263
            FIXME("unknown event type %s\n", debugstr_w(type));
    }else {
        ERR("GetType failed: %08x\n", nsres);
    }
    nsAString_Finish(&nsstr);
1264

1265 1266 1267 1268
    event = alloc_event(nsevent, event_id);
    if(!event)
        return E_OUTOFMEMORY;

1269
    *ret_event = event;
1270 1271 1272 1273 1274 1275
    return S_OK;
}

HRESULT create_document_event_str(HTMLDocumentNode *doc, const WCHAR *type, IDOMEvent **ret_event)
{
    nsIDOMEvent *nsevent;
1276
    DOMEvent *event;
1277 1278 1279 1280 1281 1282 1283
    nsAString nsstr;
    nsresult nsres;

    nsAString_InitDepend(&nsstr, type);
    nsres = nsIDOMHTMLDocument_CreateEvent(doc->nsdoc, &nsstr, &nsevent);
    nsAString_Finish(&nsstr);
    if(NS_FAILED(nsres)) {
1284
        FIXME("CreateEvent(%s) failed: %08x\n", debugstr_w(type), nsres);
1285 1286 1287
        return E_FAIL;
    }

1288
    event = alloc_event(nsevent, EVENTID_LAST);
1289
    nsIDOMEvent_Release(nsevent);
1290 1291
    if(!event)
        return E_OUTOFMEMORY;
1292

1293
    *ret_event = &event->IDOMEvent_iface;
1294
    return S_OK;
1295 1296
}

1297
HRESULT create_document_event(HTMLDocumentNode *doc, eventid_t event_id, DOMEvent **ret_event)
1298
{
1299
    nsIDOMEvent *nsevent;
1300
    DOMEvent *event;
1301 1302
    nsAString nsstr;
    nsresult nsres;
1303

1304 1305 1306 1307 1308 1309 1310
    nsAString_InitDepend(&nsstr, event_types[event_info[event_id].type]);
    nsres = nsIDOMHTMLDocument_CreateEvent(doc->nsdoc, &nsstr, &nsevent);
    nsAString_Finish(&nsstr);
    if(NS_FAILED(nsres)) {
        FIXME("CreateEvent(%s) failed: %08x\n", debugstr_w(event_types[event_info[event_id].type]), nsres);
        return E_FAIL;
    }
1311

1312
    event = alloc_event(nsevent, event_id);
1313 1314 1315 1316 1317 1318
    if(!event)
        return E_OUTOFMEMORY;

    event->event_id = event_id;
    *ret_event = event;
    return S_OK;
1319 1320
}

1321
static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
1322 1323
{
    IDispatchEx *dispex;
1324
    EXCEPINFO ei;
1325 1326 1327 1328 1329 1330
    HRESULT hres;

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

    hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
    if(SUCCEEDED(hres)) {
1331
        hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, retv, &ei, NULL);
1332 1333 1334 1335
        IDispatchEx_Release(dispex);
    }else {
        TRACE("Could not get IDispatchEx interface: %08x\n", hres);
        hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
1336
                dp, retv, &ei, NULL);
1337 1338 1339 1340 1341
    }

    return hres;
}

1342
static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, IHTMLEventObj *event_obj, VARIANT *retv)
1343 1344
{
    DISPPARAMS dp = {NULL,NULL,0,0};
1345
    VARIANT event_arg;
1346 1347 1348
    ULONG argerr;
    EXCEPINFO ei;

1349 1350
    if(event_obj) {
        V_VT(&event_arg) = VT_DISPATCH;
1351
        V_DISPATCH(&event_arg) = (IDispatch*)event_obj;
1352 1353 1354 1355
        dp.rgvarg = &event_arg;
        dp.cArgs = 1;
    }

1356
    memset(&ei, 0, sizeof(ei));
1357
    return IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, retv, &ei, &argerr);
1358 1359
}

1360
static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
1361
{
1362
    int min, max, i;
1363 1364 1365 1366 1367 1368 1369 1370
    HRESULT hres;

    if(!data)
        return FALSE;

    if(!data->ids) {
        hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
        if(FAILED(hres))
1371
            return FALSE;
1372 1373 1374
    }

    min = 0;
1375
    max = data->id_cnt-1;
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
    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;
}

1390
static void call_event_handlers(EventTarget *event_target, DOMEvent *event, dispatch_mode_t dispatch_mode)
1391
{
1392
    const listener_container_t *container = get_listener_container(event_target, event->type, FALSE);
1393
    const BOOL use_quirks = use_event_quirks(event_target);
1394 1395
    event_listener_t *listener, listeners_buf[8], *listeners = listeners_buf;
    unsigned listeners_cnt, listeners_size;
1396
    ConnectionPointContainer *cp_container = NULL;
1397
    const event_target_vtbl_t *vtbl = NULL;
1398
    VARIANT v;
1399 1400
    HRESULT hres;

1401 1402 1403
    assert(!event->current_target);
    event->current_target = event_target;

1404 1405
    if(use_quirks && container && !list_empty(&container->listeners)
       && event->phase != DEP_CAPTURING_PHASE) {
1406
        listener = LIST_ENTRY(list_tail(&container->listeners), event_listener_t, entry);
1407
        if(listener && listener->function && listener->type == LISTENER_TYPE_ONEVENT) {
1408 1409 1410
            DISPID named_arg = DISPID_THIS;
            VARIANTARG arg;
            DISPPARAMS dp = {&arg, &named_arg, 1, 1};
1411

1412 1413 1414 1415
            V_VT(&arg) = VT_DISPATCH;
            V_DISPATCH(&arg) = (IDispatch*)&event_target->dispex.IDispatchEx_iface;
            V_VT(&v) = VT_EMPTY;

1416
            TRACE("%s >>>\n", debugstr_w(event->type));
1417 1418
            hres = call_disp_func(listener->function, &dp, &v);
            if(hres == S_OK) {
1419
                TRACE("%s <<< %s\n", debugstr_w(event->type), debugstr_variant(&v));
1420

1421
                if(event->cancelable) {
1422 1423 1424 1425 1426 1427
                    if(V_VT(&v) == VT_BOOL) {
                        if(!V_BOOL(&v))
                            IDOMEvent_preventDefault(&event->IDOMEvent_iface);
                    }else if(V_VT(&v) != VT_EMPTY) {
                        FIXME("unhandled result %s\n", debugstr_variant(&v));
                    }
1428
                }
1429 1430
                VariantClear(&v);
            }else {
1431
                WARN("%s <<< %08x\n", debugstr_w(event->type), hres);
1432 1433
            }
        }
1434
    }
1435

1436 1437
    listeners_cnt = 0;
    listeners_size = sizeof(listeners_buf)/sizeof(*listeners_buf);
1438

1439
    if(container) {
1440 1441
        LIST_FOR_EACH_ENTRY(listener, &container->listeners, event_listener_t, entry) {
            if(!listener->function)
1442
                continue;
1443 1444 1445 1446 1447
            switch(listener->type) {
            case LISTENER_TYPE_ONEVENT:
                if(use_quirks || event->phase == DEP_CAPTURING_PHASE)
                    continue;
                break;
1448
            case LISTENER_TYPE_CAPTURE:
1449
                if(event->phase == DEP_BUBBLING_PHASE || dispatch_mode == DISPATCH_LEGACY)
1450 1451 1452
                    continue;
                break;
            case LISTENER_TYPE_BUBBLE:
1453
                if(event->phase == DEP_CAPTURING_PHASE || dispatch_mode == DISPATCH_LEGACY)
1454
                    continue;
1455
                break;
1456
            case LISTENER_TYPE_ATTACHED:
1457
                if(event->phase == DEP_CAPTURING_PHASE || dispatch_mode == DISPATCH_STANDARD)
1458 1459 1460
                    continue;
                break;
            }
1461 1462 1463 1464 1465 1466 1467 1468

            if(listeners_cnt == listeners_size) {
                event_listener_t *new_listeners;
                if(listeners == listeners_buf) {
                    new_listeners = heap_alloc(listeners_size * 2 * sizeof(*new_listeners));
                    if(!new_listeners)
                        break;
                    memcpy(new_listeners, listeners, listeners_cnt * sizeof(*listeners));
1469
                }else {
1470
                    new_listeners = heap_realloc(listeners, listeners_size * 2 * sizeof(*new_listeners));
1471
                }
1472 1473 1474 1475
                listeners = new_listeners;
                listeners_size *= 2;
            }

1476 1477
            listeners[listeners_cnt].type = listener->type;
            IDispatch_AddRef(listeners[listeners_cnt].function = listener->function);
1478 1479 1480 1481 1482
            listeners_cnt++;
        }
    }

    for(listener = listeners; listener < listeners + listeners_cnt; listener++) {
1483 1484 1485 1486 1487 1488 1489 1490
        if(listener->type != LISTENER_TYPE_ATTACHED) {
            DISPID named_arg = DISPID_THIS;
            VARIANTARG args[2];
            DISPPARAMS dp = {args, &named_arg, 2, 1};

            V_VT(args) = VT_DISPATCH;
            V_DISPATCH(args) = (IDispatch*)&event_target->dispex.IDispatchEx_iface;
            V_VT(args+1) = VT_DISPATCH;
1491
            V_DISPATCH(args+1) = dispatch_mode == DISPATCH_LEGACY
1492 1493 1494
                ? (IDispatch*)event->event_obj : (IDispatch*)&event->IDOMEvent_iface;
            V_VT(&v) = VT_EMPTY;

1495
            TRACE("%s >>>\n", debugstr_w(event->type));
1496 1497
            hres = call_disp_func(listener->function, &dp, &v);
            if(hres == S_OK) {
1498
                TRACE("%s <<< %s\n", debugstr_w(event->type),
1499 1500
                      debugstr_variant(&v));

1501
                if(event->cancelable) {
1502 1503 1504 1505 1506 1507 1508 1509 1510
                    if(V_VT(&v) == VT_BOOL) {
                        if(!V_BOOL(&v))
                            IDOMEvent_preventDefault(&event->IDOMEvent_iface);
                    }else if(V_VT(&v) != VT_EMPTY) {
                        FIXME("unhandled result %s\n", debugstr_variant(&v));
                    }
                }
                VariantClear(&v);
            }else {
1511
                WARN("%s <<< %08x\n", debugstr_w(event->type), hres);
1512 1513
            }
        }else {
1514 1515 1516 1517 1518 1519 1520
            VARIANTARG arg;
            DISPPARAMS dp = {&arg, NULL, 1, 0};

            V_VT(&arg) = VT_DISPATCH;
            V_DISPATCH(&arg) = (IDispatch*)event->event_obj;
            V_VT(&v) = VT_EMPTY;

1521
            TRACE("%s attached >>>\n", debugstr_w(event->type));
1522 1523
            hres = call_disp_func(listener->function, &dp, &v);
            if(hres == S_OK) {
1524
                TRACE("%s attached <<<\n", debugstr_w(event->type));
1525

1526
                if(event->cancelable) {
1527 1528 1529 1530 1531 1532 1533 1534 1535
                    if(V_VT(&v) == VT_BOOL) {
                        if(!V_BOOL(&v))
                            IDOMEvent_preventDefault(&event->IDOMEvent_iface);
                    }else if(V_VT(&v) != VT_EMPTY) {
                        FIXME("unhandled result %s\n", debugstr_variant(&v));
                    }
                }
                VariantClear(&v);
            }else {
1536
                WARN("%s attached <<< %08x\n", debugstr_w(event->type), hres);
1537 1538 1539
            }
        }
    }
1540

1541 1542
    for(listener = listeners; listener < listeners + listeners_cnt; listener++)
        IDispatch_Release(listener->function);
1543 1544 1545
    if(listeners != listeners_buf)
        heap_free(listeners);

1546 1547
    if(event->phase != DEP_CAPTURING_PHASE && event->event_id != EVENTID_LAST
       && event_info[event->event_id].dispid && (vtbl = dispex_get_vtbl(&event_target->dispex))
1548
       && vtbl->get_cp_container)
1549 1550 1551 1552 1553
        cp_container = vtbl->get_cp_container(&event_target->dispex);
    if(cp_container) {
        if(cp_container->cps) {
            ConnectionPoint *cp;
            unsigned i, j;
1554

1555 1556
            for(j=0; cp_container->cp_entries[j].riid; j++) {
                cp = cp_container->cps + j;
1557
                if(!cp->sinks_size || !is_cp_event(cp->data, event_info[event->event_id].dispid))
1558
                    continue;
1559

1560 1561 1562 1563 1564 1565
                for(i=0; i < cp->sinks_size; i++) {
                    if(!cp->sinks[i].disp)
                        continue;

                    V_VT(&v) = VT_EMPTY;

1566
                    TRACE("cp %s [%u] >>>\n", debugstr_w(event->type), i);
1567
                    hres = call_cp_func(cp->sinks[i].disp, event_info[event->event_id].dispid,
1568
                            cp->data->pass_event_arg ? event->event_obj : NULL, &v);
1569
                    if(hres == S_OK) {
1570
                        TRACE("cp %s [%u] <<<\n", debugstr_w(event->type), i);
1571

1572
                        if(event->cancelable) {
1573 1574
                            if(V_VT(&v) == VT_BOOL) {
                                if(!V_BOOL(&v))
1575
                                    IDOMEvent_preventDefault(&event->IDOMEvent_iface);
1576 1577 1578
                            }else if(V_VT(&v) != VT_EMPTY) {
                                FIXME("unhandled result %s\n", debugstr_variant(&v));
                            }
1579
                        }
1580 1581
                        VariantClear(&v);
                    }else {
1582
                        WARN("cp %s [%u] <<< %08x\n", debugstr_w(event->type), i, hres);
1583
                    }
1584 1585 1586
                }
            }
        }
1587
        IConnectionPointContainer_Release(&cp_container->IConnectionPointContainer_iface);
1588
    }
1589 1590

    event->current_target = NULL;
1591 1592
}

1593 1594
static HRESULT dispatch_event_object(EventTarget *event_target, DOMEvent *event,
                                     dispatch_mode_t dispatch_mode, VARIANT_BOOL *r)
1595
{
1596 1597
    EventTarget *target_chain_buf[8], **target_chain = target_chain_buf;
    unsigned chain_cnt, chain_buf_size, i;
1598
    const event_target_vtbl_t *vtbl, *target_vtbl;
1599
    HTMLEventObj *event_obj_ref = NULL;
1600
    IHTMLEventObj *prev_event = NULL;
1601
    EventTarget *iter;
1602
    HRESULT hres;
1603

1604 1605
    TRACE("(%p) %s\n", event_target, debugstr_w(event->type));

1606 1607 1608
    if(!event->type) {
        FIXME("Uninitialized event.\n");
        return E_FAIL;
1609 1610
    }

1611 1612
    if(event->current_target) {
        FIXME("event is being dispatched.\n");
1613
        return E_FAIL;
1614
    }
1615

1616 1617
    iter = event_target;
    IDispatchEx_AddRef(&event_target->dispex.IDispatchEx_iface);
1618

1619 1620
    chain_cnt = 0;
    chain_buf_size = sizeof(target_chain_buf)/sizeof(*target_chain_buf);
1621

1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
    do {
        if(chain_cnt == chain_buf_size) {
            EventTarget **new_chain;
            if(target_chain == target_chain_buf) {
                new_chain = heap_alloc(chain_buf_size * 2 * sizeof(*new_chain));
                if(!new_chain)
                    break;
                memcpy(new_chain, target_chain, chain_buf_size * sizeof(*new_chain));
            }else {
                new_chain = heap_realloc(target_chain, chain_buf_size * 2 * sizeof(*new_chain));
                if(!new_chain)
                    break;
            }
            chain_buf_size *= 2;
            target_chain = new_chain;
        }
1638

1639
        target_chain[chain_cnt++] = iter;
1640

1641
        if(!(vtbl = dispex_get_vtbl(&iter->dispex)) || !vtbl->get_parent_event_target)
1642
            break;
1643 1644
        iter = vtbl->get_parent_event_target(&iter->dispex);
    } while(iter);
1645

1646 1647
    if(!event->event_obj && !event->no_event_obj) {
        event_obj_ref = alloc_event_obj(event);
1648
        if(event_obj_ref)
1649 1650 1651
            event->event_obj = &event_obj_ref->IHTMLEventObj_iface;
    }

1652 1653
    target_vtbl = dispex_get_vtbl(&event_target->dispex);
    if(target_vtbl && target_vtbl->set_current_event)
1654
        prev_event = target_vtbl->set_current_event(&event_target->dispex, event->event_obj);
1655

1656 1657
    if(event->target)
        IDispatchEx_Release(&event->target->dispex.IDispatchEx_iface);
1658 1659
    event->target = event_target;
    IDispatchEx_AddRef(&event_target->dispex.IDispatchEx_iface);
1660

1661 1662 1663
    event->phase = DEP_CAPTURING_PHASE;
    i = chain_cnt-1;
    while(!event->stop_propagation && i)
1664
        call_event_handlers(target_chain[i--], event, dispatch_mode);
1665 1666 1667

    if(!event->stop_propagation) {
        event->phase = DEP_AT_TARGET;
1668
        call_event_handlers(target_chain[0], event, dispatch_mode);
1669
    }
1670

1671
    if(event->bubbles) {
1672 1673
        event->phase = DEP_BUBBLING_PHASE;
        for(i = 1; !event->stop_propagation && i < chain_cnt; i++)
1674
            call_event_handlers(target_chain[i], event, dispatch_mode);
1675 1676
    }

1677 1678 1679
    if(r)
        *r = variant_bool(!event->prevent_default);

1680 1681 1682 1683 1684 1685
    if(target_vtbl && target_vtbl->set_current_event) {
        prev_event = target_vtbl->set_current_event(&event_target->dispex, prev_event);
        if(prev_event)
            IHTMLEventObj_Release(prev_event);
    }

1686
    if(event->event_id != EVENTID_LAST && (event_info[event->event_id].flags & EVENT_HASDEFAULTHANDLERS)) {
1687 1688
        for(i = 0; !event->prevent_default && i < chain_cnt; i++) {
            BOOL prevent_default = FALSE;
1689 1690 1691
            vtbl = dispex_get_vtbl(&target_chain[i]->dispex);
            if(!vtbl || !vtbl->handle_event_default)
                continue;
1692 1693
            hres = vtbl->handle_event_default(&event_target->dispex, event->event_id,
                    event->nsevent, &prevent_default);
1694
            if(FAILED(hres) || event->stop_propagation)
1695
                break;
1696 1697
            if(prevent_default)
                IDOMEvent_preventDefault(&event->IDOMEvent_iface);
1698
        }
1699 1700
    }

1701
    event->prevent_default = FALSE;
1702 1703 1704 1705 1706
    if(event_obj_ref) {
        event->event_obj = NULL;
        IHTMLEventObj_Release(&event_obj_ref->IHTMLEventObj_iface);
    }

1707 1708 1709 1710
    for(i = 0; i < chain_cnt; i++)
        IDispatchEx_Release(&target_chain[i]->dispex.IDispatchEx_iface);
    if(target_chain != target_chain_buf)
        heap_free(target_chain);
1711 1712

    return S_OK;
1713 1714
}

1715 1716
void dispatch_event(EventTarget *event_target, DOMEvent *event)
{
1717
    dispatch_event_object(event_target, event, DISPATCH_BOTH, NULL);
1718 1719
}

1720
HRESULT fire_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_var, VARIANT_BOOL *cancelled)
1721 1722
{
    HTMLEventObj *event_obj = NULL;
1723
    eventid_t eid;
1724
    HRESULT hres = S_OK;
1725 1726 1727 1728 1729 1730 1731

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

1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
    if(event_var && V_VT(event_var) != VT_EMPTY && V_VT(event_var) != VT_ERROR) {
        if(V_VT(event_var) != VT_DISPATCH) {
            FIXME("event_var %s not supported\n", debugstr_variant(event_var));
            return E_NOTIMPL;
        }

        if(V_DISPATCH(event_var)) {
            IHTMLEventObj *event_iface;

            hres = IDispatch_QueryInterface(V_DISPATCH(event_var), &IID_IHTMLEventObj, (void**)&event_iface);
            if(FAILED(hres)) {
                FIXME("No IHTMLEventObj iface\n");
                return hres;
            }
1746

1747 1748 1749 1750 1751 1752 1753
            event_obj = unsafe_impl_from_IHTMLEventObj(event_iface);
            if(!event_obj) {
                ERR("Not our IHTMLEventObj?\n");
                IHTMLEventObj_Release(event_iface);
                return E_FAIL;
            }
        }
1754 1755
    }

1756 1757 1758 1759 1760 1761 1762 1763
    if(!event_obj) {
        event_obj = alloc_event_obj(NULL);
        if(!event_obj)
            return E_OUTOFMEMORY;
    }

    if(!event_obj->event)
        hres = create_document_event(node->doc, eid, &event_obj->event);
1764

1765 1766
    if(SUCCEEDED(hres)) {
        event_obj->event->event_obj = &event_obj->IHTMLEventObj_iface;
1767
        dispatch_event_object(&node->event_target, event_obj->event, DISPATCH_LEGACY, NULL);
1768 1769
        event_obj->event->event_obj = NULL;
    }
1770

1771
    IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
1772 1773
    if(FAILED(hres))
        return hres;
1774 1775 1776 1777 1778

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

1779
HRESULT ensure_doc_nsevent_handler(HTMLDocumentNode *doc, eventid_t eid)
1780
{
1781
    nsIDOMNode *nsnode = NULL;
1782

1783
    TRACE("%s\n", debugstr_w(event_info[eid].name));
1784

1785 1786 1787 1788 1789 1790 1791 1792
    if(!doc->nsdoc)
        return S_OK;

    switch(eid) {
    case EVENTID_FOCUSIN:
        doc->event_vector[eid] = TRUE;
        eid = EVENTID_FOCUS;
        break;
1793 1794 1795 1796
    case EVENTID_FOCUSOUT:
        doc->event_vector[eid] = TRUE;
        eid = EVENTID_BLUR;
        break;
1797 1798 1799 1800 1801
    default:
        break;
    }

    if(doc->event_vector[eid] || !(event_info[eid].flags & (EVENT_DEFAULTLISTENER|EVENT_BIND_TO_BODY)))
1802 1803
        return S_OK;

1804
    if(event_info[eid].flags & EVENT_BIND_TO_BODY) {
1805 1806
        nsnode = doc->node.nsnode;
        nsIDOMNode_AddRef(nsnode);
1807 1808
    }

1809 1810 1811 1812 1813
    doc->event_vector[eid] = TRUE;
    add_nsevent_listener(doc, nsnode, event_info[eid].name);

    if(nsnode)
        nsIDOMNode_Release(nsnode);
1814 1815 1816
    return S_OK;
}

1817 1818
void detach_events(HTMLDocumentNode *doc)
{
1819 1820
    if(doc->event_vector) {
        int i;
1821

1822
        for(i=0; i < EVENTID_LAST; i++) {
1823
            if(doc->event_vector[i]) {
1824
                detach_nsevent(doc, event_info[i].name);
1825 1826
                doc->event_vector[i] = FALSE;
            }
1827
        }
1828
    }
1829 1830

    release_nsevents(doc);
1831 1832
}

1833 1834 1835 1836 1837 1838 1839 1840 1841
static HRESULT get_event_dispex_ref(EventTarget *event_target, eventid_t eid, BOOL alloc, VARIANT **ret)
{
    WCHAR buf[64];
    buf[0] = 'o';
    buf[1] = 'n';
    strcpyW(buf+2, event_info[eid].name);
    return dispex_get_dprop_ref(&event_target->dispex, buf, alloc, ret);
}

1842 1843 1844 1845 1846
static event_listener_t *get_onevent_listener(EventTarget *event_target, eventid_t eid, BOOL alloc)
{
    listener_container_t *container;
    event_listener_t *listener;

1847
    container = get_listener_container(event_target, event_info[eid].name, alloc);
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
    if(!container)
        return NULL;

    LIST_FOR_EACH_ENTRY_REV(listener, &container->listeners, event_listener_t, entry) {
        if(listener->type == LISTENER_TYPE_ONEVENT)
            return listener;
    }

    if(!alloc)
        return NULL;

    listener = heap_alloc(sizeof(*listener));
    if(!listener)
        return NULL;

    listener->type = LISTENER_TYPE_ONEVENT;
    listener->function = NULL;
    list_add_tail(&container->listeners, &listener->entry);
    return listener;
}

1869
static void remove_event_handler(EventTarget *event_target, eventid_t eid)
1870
{
1871
    event_listener_t *listener;
1872 1873 1874
    VARIANT *store;
    HRESULT hres;

1875
    hres = get_event_dispex_ref(event_target, eid, FALSE, &store);
1876 1877 1878
    if(SUCCEEDED(hres))
        VariantClear(store);

1879 1880 1881 1882
    listener = get_onevent_listener(event_target, eid, FALSE);
    if(listener && listener->function) {
        IDispatch_Release(listener->function);
        listener->function = NULL;
1883 1884 1885
    }
}

1886
static HRESULT set_event_handler_disp(EventTarget *event_target, eventid_t eid, IDispatch *disp)
1887
{
1888
    event_listener_t *listener;
1889

1890 1891 1892
    if(event_info[eid].flags & EVENT_FIXME)
        FIXME("unimplemented event %s\n", debugstr_w(event_info[eid].name));

1893
    remove_event_handler(event_target, eid);
1894
    if(!disp)
1895
        return S_OK;
1896

1897 1898
    listener = get_onevent_listener(event_target, eid, TRUE);
    if(!listener)
1899 1900
        return E_OUTOFMEMORY;

1901 1902
    if(listener->function)
        IDispatch_Release(listener->function);
1903

1904
    IDispatch_AddRef(listener->function = disp);
1905
    return S_OK;
1906 1907
}

1908
HRESULT set_event_handler(EventTarget *event_target, eventid_t eid, VARIANT *var)
1909 1910
{
    switch(V_VT(var)) {
1911 1912 1913 1914 1915 1916
    case VT_EMPTY:
        if(use_event_quirks(event_target)) {
            WARN("attempt to set to VT_EMPTY in quirks mode\n");
            return E_NOTIMPL;
        }
        /* fall through */
1917
    case VT_NULL:
1918
        remove_event_handler(event_target, eid);
1919
        return S_OK;
1920

1921
    case VT_DISPATCH:
1922
        return set_event_handler_disp(event_target, eid, V_DISPATCH(var));
1923 1924 1925 1926 1927

    case VT_BSTR: {
        VARIANT *v;
        HRESULT hres;

1928 1929 1930
        if(!use_event_quirks(event_target))
            FIXME("Setting to string %s not supported\n", debugstr_w(V_BSTR(var)));

1931 1932
        /*
         * Setting event handler to string is a rare case and we don't want to
1933
         * complicate nor increase memory of listener_container_t for that. Instead,
1934 1935 1936
         * we store the value in DispatchEx, which can already handle custom
         * properties.
         */
1937
        remove_event_handler(event_target, eid);
1938

1939
        hres = get_event_dispex_ref(event_target, eid, TRUE, &v);
1940 1941 1942 1943 1944 1945 1946 1947 1948
        if(FAILED(hres))
            return hres;

        V_BSTR(v) = SysAllocString(V_BSTR(var));
        if(!V_BSTR(v))
            return E_OUTOFMEMORY;
        V_VT(v) = VT_BSTR;
        return S_OK;
    }
1949 1950

    default:
1951
        FIXME("not handler %s\n", debugstr_variant(var));
1952
        return E_NOTIMPL;
1953 1954
    }

1955
    return S_OK;
1956 1957
}

1958
HRESULT get_event_handler(EventTarget *event_target, eventid_t eid, VARIANT *var)
1959
{
1960
    event_listener_t *listener;
1961 1962 1963
    VARIANT *v;
    HRESULT hres;

1964
    hres = get_event_dispex_ref(event_target, eid, FALSE, &v);
1965 1966
    if(SUCCEEDED(hres) && V_VT(v) != VT_EMPTY) {
        V_VT(var) = VT_EMPTY;
1967
        return VariantCopy(var, v);
1968
    }
1969

1970 1971
    listener = get_onevent_listener(event_target, eid, FALSE);
    if(listener && listener->function) {
1972
        V_VT(var) = VT_DISPATCH;
1973
        V_DISPATCH(var) = listener->function;
1974 1975 1976 1977 1978 1979 1980 1981
        IDispatch_AddRef(V_DISPATCH(var));
    }else {
        V_VT(var) = VT_NULL;
    }

    return S_OK;
}

1982
HRESULT attach_event(EventTarget *event_target, BSTR name, IDispatch *disp, VARIANT_BOOL *res)
1983
{
1984 1985
    listener_container_t *container;
    event_listener_t *listener;
1986 1987 1988 1989 1990 1991 1992 1993 1994
    eventid_t eid;

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

1995
    container = get_listener_container(event_target, event_info[eid].name, TRUE);
1996
    if(!container)
1997 1998
        return E_OUTOFMEMORY;

1999 2000 2001
    listener = heap_alloc(sizeof(*listener));
    if(!listener)
        return E_OUTOFMEMORY;
2002

2003 2004
    listener->type = LISTENER_TYPE_ATTACHED;
    IDispatch_AddRef(listener->function = disp);
2005 2006 2007 2008
    if(use_event_quirks(event_target))
        list_add_head(&container->listeners, &listener->entry);
    else
        list_add_tail(&container->listeners, &listener->entry);
2009 2010

    *res = VARIANT_TRUE;
2011
    return S_OK;
2012 2013
}

2014
HRESULT detach_event(EventTarget *event_target, BSTR name, IDispatch *disp)
2015 2016 2017 2018 2019 2020 2021 2022 2023
{
    eventid_t eid;

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

2024
    remove_event_listener(event_target, event_info[eid].name, LISTENER_TYPE_ATTACHED, disp);
2025 2026 2027
    return S_OK;
}

2028
void bind_target_event(HTMLDocumentNode *doc, EventTarget *event_target, const WCHAR *event, IDispatch *disp)
2029 2030 2031
{
    eventid_t eid;

2032
    TRACE("(%p %p %s %p)\n", doc, event_target, debugstr_w(event), disp);
2033 2034 2035 2036 2037 2038 2039

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

2040
    set_event_handler_disp(event_target, eid, disp);
2041 2042
}

2043
void update_doc_cp_events(HTMLDocumentNode *doc, cp_static_data_t *cp)
2044 2045 2046 2047 2048
{
    int i;

    for(i=0; i < EVENTID_LAST; i++) {
        if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
2049
            ensure_doc_nsevent_handler(doc, i);
2050 2051 2052
    }
}

2053
void check_event_attr(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem)
2054
{
2055 2056 2057 2058 2059 2060
    nsIDOMMozNamedAttrMap *attr_map;
    const PRUnichar *name, *value;
    nsAString name_str, value_str;
    HTMLDOMNode *node = NULL;
    cpp_bool has_attrs;
    nsIDOMAttr *attr;
2061
    IDispatch *disp;
2062 2063
    UINT32 length, i;
    eventid_t eid;
2064
    nsresult nsres;
2065
    HRESULT hres;
2066

2067 2068 2069
    nsres = nsIDOMHTMLElement_HasAttributes(nselem, &has_attrs);
    if(NS_FAILED(nsres) || !has_attrs)
        return;
2070

2071 2072 2073
    nsres = nsIDOMHTMLElement_GetAttributes(nselem, &attr_map);
    if(NS_FAILED(nsres))
        return;
2074

2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
    nsres = nsIDOMMozNamedAttrMap_GetLength(attr_map, &length);
    assert(nsres == NS_OK);

    nsAString_Init(&name_str, NULL);
    nsAString_Init(&value_str, NULL);

    for(i = 0; i < length; i++) {
        nsres = nsIDOMMozNamedAttrMap_Item(attr_map, i, &attr);
        if(NS_FAILED(nsres))
            continue;

        nsres = nsIDOMAttr_GetName(attr, &name_str);
        if(NS_FAILED(nsres)) {
            nsIDOMAttr_Release(attr);
            continue;
        }

        nsAString_GetData(&name_str, &name);
        eid = attr_to_eid(name);
        if(eid == EVENTID_LAST) {
            nsIDOMAttr_Release(attr);
            continue;
        }

        nsres = nsIDOMAttr_GetValue(attr, &value_str);
        nsIDOMAttr_Release(attr);
        if(NS_FAILED(nsres))
            continue;

        nsAString_GetData(&value_str, &value);
        if(!*value)
            continue;

        TRACE("%p.%s = %s\n", nselem, debugstr_w(name), debugstr_w(value));

        disp = script_parse_event(doc->window, value);
        if(!disp)
            continue;

        if(!node) {
            hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
            if(FAILED(hres)) {
2117
                IDispatch_Release(disp);
2118
                break;
2119 2120
            }
        }
2121

2122
        set_event_handler_disp(get_node_event_prop_target(node, eid), eid, disp);
2123
        IDispatch_Release(disp);
2124
    }
2125 2126 2127 2128 2129 2130

    if(node)
        node_release(node);
    nsAString_Finish(&name_str);
    nsAString_Finish(&value_str);
    nsIDOMMozNamedAttrMap_Release(attr_map);
2131 2132
}

2133 2134
HRESULT doc_init_events(HTMLDocumentNode *doc)
{
2135 2136 2137
    unsigned i;
    HRESULT hres;

2138 2139 2140 2141 2142
    doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
    if(!doc->event_vector)
        return E_OUTOFMEMORY;

    init_nsevents(doc);
2143 2144

    for(i=0; i < EVENTID_LAST; i++) {
2145
        if(event_info[i].flags & EVENT_HASDEFAULTHANDLERS) {
2146
            hres = ensure_doc_nsevent_handler(doc, i);
2147 2148 2149 2150 2151
            if(FAILED(hres))
                return hres;
        }
    }

2152 2153 2154
    return S_OK;
}

2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
static inline EventTarget *impl_from_IEventTarget(IEventTarget *iface)
{
    return CONTAINING_RECORD(iface, EventTarget, IEventTarget_iface);
}

static HRESULT WINAPI EventTarget_QueryInterface(IEventTarget *iface, REFIID riid, void **ppv)
{
    EventTarget *This = impl_from_IEventTarget(iface);
    return IDispatchEx_QueryInterface(&This->dispex.IDispatchEx_iface, riid, ppv);
}

static ULONG WINAPI EventTarget_AddRef(IEventTarget *iface)
{
    EventTarget *This = impl_from_IEventTarget(iface);
    return IDispatchEx_AddRef(&This->dispex.IDispatchEx_iface);
}

static ULONG WINAPI EventTarget_Release(IEventTarget *iface)
{
    EventTarget *This = impl_from_IEventTarget(iface);
    return IDispatchEx_Release(&This->dispex.IDispatchEx_iface);
}

static HRESULT WINAPI EventTarget_GetTypeInfoCount(IEventTarget *iface, UINT *pctinfo)
{
    EventTarget *This = impl_from_IEventTarget(iface);
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
}

static HRESULT WINAPI EventTarget_GetTypeInfo(IEventTarget *iface, UINT iTInfo,
                                              LCID lcid, ITypeInfo **ppTInfo)
{
    EventTarget *This = impl_from_IEventTarget(iface);
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
}

static HRESULT WINAPI EventTarget_GetIDsOfNames(IEventTarget *iface, REFIID riid, LPOLESTR *rgszNames,
                                                UINT cNames, LCID lcid, DISPID *rgDispId)
{
    EventTarget *This = impl_from_IEventTarget(iface);
    return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid,
            rgszNames, cNames, lcid, rgDispId);
}

static HRESULT WINAPI EventTarget_Invoke(IEventTarget *iface, DISPID dispIdMember,
                                         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                                         VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    EventTarget *This = impl_from_IEventTarget(iface);
    return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember,
            riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
}

static HRESULT WINAPI EventTarget_addEventListener(IEventTarget *iface, BSTR type,
2209
                                                   IDispatch *function, VARIANT_BOOL capture)
2210 2211
{
    EventTarget *This = impl_from_IEventTarget(iface);
2212 2213 2214 2215 2216 2217
    listener_type_t listener_type = capture ? LISTENER_TYPE_CAPTURE : LISTENER_TYPE_BUBBLE;
    listener_container_t *container;
    event_listener_t *listener;

    TRACE("(%p)->(%s %p %x)\n", This, debugstr_w(type), function, capture);

2218
    container = get_listener_container(This, type, TRUE);
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
    if(!container)
        return E_OUTOFMEMORY;

    /* check for duplicates */
    LIST_FOR_EACH_ENTRY(listener, &container->listeners, event_listener_t, entry) {
        if(listener->type == listener_type && listener->function == function)
            return S_OK;
    }

    listener = heap_alloc(sizeof(*listener));
    if(!listener)
        return E_OUTOFMEMORY;

    listener->type = listener_type;
    IDispatch_AddRef(listener->function = function);
    list_add_tail(&container->listeners, &listener->entry);
    return S_OK;
2236 2237 2238 2239 2240 2241
}

static HRESULT WINAPI EventTarget_removeEventListener(IEventTarget *iface, BSTR type,
                                                      IDispatch *listener, VARIANT_BOOL capture)
{
    EventTarget *This = impl_from_IEventTarget(iface);
2242 2243 2244

    TRACE("(%p)->(%s %p %x)\n", This, debugstr_w(type), listener, capture);

2245
    remove_event_listener(This, type, capture ? LISTENER_TYPE_CAPTURE : LISTENER_TYPE_BUBBLE, listener);
2246
    return S_OK;
2247 2248
}

2249
static HRESULT WINAPI EventTarget_dispatchEvent(IEventTarget *iface, IDOMEvent *event_iface, VARIANT_BOOL *result)
2250 2251
{
    EventTarget *This = impl_from_IEventTarget(iface);
2252 2253 2254 2255 2256 2257 2258 2259 2260 2261
    DOMEvent *event = unsafe_impl_from_IDOMEvent(event_iface);

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

    if(!event) {
        WARN("Invalid event\n");
        return E_INVALIDARG;
    }

    return dispatch_event_object(This, event, DISPATCH_STANDARD, result);
2262 2263
}

2264
static HRESULT IEventTarget_addEventListener_hook(DispatchEx *dispex, LCID lcid, WORD flags,
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
        DISPPARAMS *dp, VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
{
    /* If only two arguments were given, implicitly set capture to false */
    if((flags & DISPATCH_METHOD) && dp->cArgs == 2 && !dp->cNamedArgs) {
        VARIANT args[3];
        DISPPARAMS new_dp = {args, NULL, 3, 0};
        V_VT(args) = VT_BOOL;
        V_BOOL(args) = VARIANT_FALSE;
        args[1] = dp->rgvarg[0];
        args[2] = dp->rgvarg[1];

        TRACE("implicit capture\n");

        return IDispatchEx_InvokeEx(&dispex->IDispatchEx_iface, DISPID_IEVENTTARGET_ADDEVENTLISTENER,
                                    lcid, flags, &new_dp, res, ei, caller);
    }

    return S_FALSE; /* fallback to default */
}

2285
static HRESULT IEventTarget_removeEventListener_hook(DispatchEx *dispex, LCID lcid, WORD flags,
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
        DISPPARAMS *dp, VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
{
    /* If only two arguments were given, implicitly set capture to false */
    if((flags & DISPATCH_METHOD) && dp->cArgs == 2 && !dp->cNamedArgs) {
        VARIANT args[3];
        DISPPARAMS new_dp = {args, NULL, 3, 0};
        V_VT(args) = VT_BOOL;
        V_BOOL(args) = VARIANT_FALSE;
        args[1] = dp->rgvarg[0];
        args[2] = dp->rgvarg[1];

        TRACE("implicit capture\n");

        return IDispatchEx_InvokeEx(&dispex->IDispatchEx_iface, DISPID_IEVENTTARGET_REMOVEEVENTLISTENER,
                                    lcid, flags, &new_dp, res, ei, caller);
    }

    return S_FALSE; /* fallback to default */
}

2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
static const IEventTargetVtbl EventTargetVtbl = {
    EventTarget_QueryInterface,
    EventTarget_AddRef,
    EventTarget_Release,
    EventTarget_GetTypeInfoCount,
    EventTarget_GetTypeInfo,
    EventTarget_GetIDsOfNames,
    EventTarget_Invoke,
    EventTarget_addEventListener,
    EventTarget_removeEventListener,
    EventTarget_dispatchEvent
};

#define DELAY_INIT_VTBL ((const IEventTargetVtbl*)1)

static BOOL use_event_quirks(EventTarget *event_target)
{
    if(event_target->IEventTarget_iface.lpVtbl == DELAY_INIT_VTBL) {
        event_target->IEventTarget_iface.lpVtbl =
            dispex_compat_mode(&event_target->dispex) >= COMPAT_MODE_IE9
            ? &EventTargetVtbl : NULL;
    }
    return !event_target->IEventTarget_iface.lpVtbl;
}

2331 2332
HRESULT EventTarget_QI(EventTarget *event_target, REFIID riid, void **ppv)
{
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343
    if(IsEqualGUID(riid, &IID_IEventTarget)) {
        if(use_event_quirks(event_target)) {
            WARN("IEventTarget queried, but not supported by in document mode\n");
            *ppv = NULL;
            return E_NOINTERFACE;
        }
        IEventTarget_AddRef(&event_target->IEventTarget_iface);
        *ppv = &event_target->IEventTarget_iface;
        return S_OK;
    }

2344 2345 2346 2347 2348 2349 2350 2351
    if(dispex_query_interface(&event_target->dispex, riid, ppv))
        return *ppv ? S_OK : E_NOINTERFACE;

    WARN("(%p)->(%s %p)\n", event_target, debugstr_mshtml_guid(riid), ppv);
    *ppv = NULL;
    return E_NOINTERFACE;
}

2352 2353
void EventTarget_init_dispex_info(dispex_data_t *dispex_info, compat_mode_t compat_mode)
{
2354 2355
    static const dispex_hook_t IEventTarget_hooks[] = {
        {DISPID_IEVENTTARGET_ADDEVENTLISTENER, IEventTarget_addEventListener_hook},
2356
        {DISPID_IEVENTTARGET_REMOVEEVENTLISTENER, IEventTarget_removeEventListener_hook},
2357 2358 2359
        {DISPID_UNKNOWN}
    };

2360
    if(compat_mode >= COMPAT_MODE_IE9)
2361
        dispex_info_add_interface(dispex_info, IEventTarget_tid, IEventTarget_hooks);
2362 2363
}

2364
static int event_id_cmp(const void *key, const struct wine_rb_entry *entry)
2365
{
2366
    return strcmpW(key, WINE_RB_ENTRY_VALUE(entry, listener_container_t, entry)->type);
2367
}
2368

2369 2370
void EventTarget_Init(EventTarget *event_target, IUnknown *outer, dispex_static_data_t *dispex_data,
                      compat_mode_t compat_mode)
2371
{
2372
    init_dispex_with_compat_mode(&event_target->dispex, outer, dispex_data, compat_mode);
2373
    wine_rb_init(&event_target->handler_map, event_id_cmp);
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387

    /*
     * IEventTarget is supported by the object or not depending on compatibility mode.
     * We use NULL vtbl for objects in compatibility mode not supporting the interface.
     * For targets that don't know compatibility mode at creation time, we set vtbl
     * to special DELAY_INIT_VTBL value so that vtbl will be set to proper value
     * when it's needed.
     */
    if(compat_mode == COMPAT_MODE_QUIRKS && dispex_data->vtbl && dispex_data->vtbl->get_compat_mode)
        event_target->IEventTarget_iface.lpVtbl = DELAY_INIT_VTBL;
    else if(compat_mode < COMPAT_MODE_IE9)
        event_target->IEventTarget_iface.lpVtbl = NULL;
    else
        event_target->IEventTarget_iface.lpVtbl = &EventTargetVtbl;
2388
}
2389

2390 2391
void release_event_target(EventTarget *event_target)
{
2392
    listener_container_t *iter, *iter2;
2393

2394 2395 2396 2397 2398 2399 2400 2401
    WINE_RB_FOR_EACH_ENTRY_DESTRUCTOR(iter, iter2, &event_target->handler_map, listener_container_t, entry) {
        while(!list_empty(&iter->listeners)) {
            event_listener_t *listener = LIST_ENTRY(list_head(&iter->listeners), event_listener_t, entry);
            if(listener->function)
                IDispatch_Release(listener->function);
            list_remove(&listener->entry);
            heap_free(listener);
        }
2402 2403
        heap_free(iter);
    }
2404
}