htmlevent.c 46.3 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 dataavailableW[] = {'d','a','t','a','a','v','a','i','l','a','b','l','e',0};
static const WCHAR ondataavailableW[] = {'o','n','d','a','t','a','a','v','a','i','l','a','b','l','e',0};

65 66 67
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};

68 69 70
static const WCHAR dragW[] = {'d','r','a','g',0};
static const WCHAR ondragW[] = {'o','n','d','r','a','g',0};

71 72 73
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};

74 75 76
static const WCHAR errorW[] = {'e','r','r','o','r',0};
static const WCHAR onerrorW[] = {'o','n','e','r','r','o','r',0};

77 78 79
static const WCHAR focusW[] = {'f','o','c','u','s',0};
static const WCHAR onfocusW[] = {'o','n','f','o','c','u','s',0};

80 81 82
static const WCHAR helpW[] = {'h','e','l','p',0};
static const WCHAR onhelpW[] = {'o','n','h','e','l','p',0};

83 84 85
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};

86 87 88
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};

89 90 91
static const WCHAR keyupW[] = {'k','e','y','u','p',0};
static const WCHAR onkeyupW[] = {'o','n','k','e','y','u','p',0};

92 93 94
static const WCHAR loadW[] = {'l','o','a','d',0};
static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};

95 96 97
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};

98 99 100
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};

101 102 103
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};

104 105 106
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};

107 108 109
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};

110 111 112
static const WCHAR pasteW[] = {'p','a','s','t','e',0};
static const WCHAR onpasteW[] = {'o','n','p','a','s','t','e',0};

113 114 115
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};

116 117 118
static const WCHAR resizeW[] = {'r','e','s','i','z','e',0};
static const WCHAR onresizeW[] = {'o','n','r','e','s','i','z','e',0};

119 120 121
static const WCHAR scrollW[] = {'s','c','r','o','l','l',0};
static const WCHAR onscrollW[] = {'o','n','s','c','r','o','l','l',0};

122 123 124
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};

125 126 127
static const WCHAR submitW[] = {'s','u','b','m','i','t',0};
static const WCHAR onsubmitW[] = {'o','n','s','u','b','m','i','t',0};

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
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
};

146 147 148
typedef struct {
    LPCWSTR name;
    LPCWSTR attr_name;
149
    DWORD type;
150
    DISPID dispid;
151
    DWORD flags;
152 153
} event_info_t;

154
#define EVENT_DEFAULTLISTENER    0x0001
155
#define EVENT_BUBBLE             0x0002
156
#define EVENT_FORWARDBODY        0x0004
157
#define EVENT_NODEHANDLER        0x0008
158
#define EVENT_CANCELABLE         0x0010
159
#define EVENT_HASDEFAULTHANDLERS 0x0020
160

161
static const event_info_t event_info[] = {
162 163 164 165 166 167 168
    {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,
169
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE|EVENT_HASDEFAULTHANDLERS},
170
    {contextmenuW,       oncontextmenuW,       EVENTT_MOUSE,  DISPID_EVMETH_ONCONTEXTMENU,
171
        EVENT_BUBBLE|EVENT_CANCELABLE},
172 173
    {dataavailableW,     ondataavailableW,     EVENTT_NONE,   DISPID_EVMETH_ONDATAAVAILABLE,
        EVENT_BUBBLE},
174
    {dblclickW,          ondblclickW,          EVENTT_MOUSE,  DISPID_EVMETH_ONDBLCLICK,
175
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE},
176
    {dragW,              ondragW,              EVENTT_MOUSE,  DISPID_EVMETH_ONDRAG,
177
        EVENT_CANCELABLE},
178
    {dragstartW,         ondragstartW,         EVENTT_MOUSE,  DISPID_EVMETH_ONDRAGSTART,
179
        EVENT_CANCELABLE},
180
    {errorW,             onerrorW,             EVENTT_NONE,   DISPID_EVMETH_ONERROR,
181
        EVENT_NODEHANDLER},
182 183
    {focusW,             onfocusW,             EVENTT_HTML,   DISPID_EVMETH_ONFOCUS,
        EVENT_DEFAULTLISTENER},
184 185
    {helpW,              onhelpW,              EVENTT_KEY,    DISPID_EVMETH_ONHELP,
        EVENT_BUBBLE|EVENT_CANCELABLE},
186
    {keydownW,           onkeydownW,           EVENTT_KEY,    DISPID_EVMETH_ONKEYDOWN,
187
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_HASDEFAULTHANDLERS},
188 189
    {keypressW,          onkeypressW,          EVENTT_KEY,    DISPID_EVMETH_ONKEYPRESS,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
190 191 192
    {keyupW,             onkeyupW,             EVENTT_KEY,    DISPID_EVMETH_ONKEYUP,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
    {loadW,              onloadW,              EVENTT_HTML,   DISPID_EVMETH_ONLOAD,
193
        EVENT_NODEHANDLER},
194 195
    {mousedownW,         onmousedownW,         EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEDOWN,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
196 197
    {mousemoveW,         onmousemoveW,         EVENTT_MOUSE,  DISPID_EVMETH_ONMOUSEMOVE,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
198 199 200 201 202 203 204
    {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,
205
        EVENT_CANCELABLE},
206 207
    {readystatechangeW,  onreadystatechangeW,  EVENTT_NONE,   DISPID_EVMETH_ONREADYSTATECHANGE,
        0},
208 209
    {resizeW,            onresizeW,            EVENTT_NONE,   DISPID_EVMETH_ONRESIZE,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
210 211
    {scrollW,            onscrollW,            EVENTT_HTML,   DISPID_EVMETH_ONSCROLL,
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE},
212
    {selectstartW,       onselectstartW,       EVENTT_MOUSE,  DISPID_EVMETH_ONSELECTSTART,
213
        EVENT_CANCELABLE},
214
    {submitW,            onsubmitW,            EVENTT_HTML,   DISPID_EVMETH_ONSUBMIT,
215
        EVENT_DEFAULTLISTENER|EVENT_BUBBLE|EVENT_CANCELABLE}
216 217
};

218
static const eventid_t node_handled_list[] = { EVENTID_ERROR, EVENTID_LOAD };
219

220 221 222 223 224 225 226 227 228 229 230 231 232
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;
}

233 234 235 236 237 238 239 240 241 242 243 244
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;
}

245 246 247 248 249 250 251 252 253 254 255 256 257
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;
}

258
typedef struct {
259
    DispatchEx dispex;
260
    IHTMLEventObj IHTMLEventObj_iface;
261

262
    LONG ref;
263 264

    HTMLDOMNode *target;
265
    const event_info_t *type;
266
    nsIDOMEvent *nsevent;
267
    BOOL prevent_default;
268
    BOOL cancel_bubble;
269 270
} HTMLEventObj;

271 272 273 274
static inline HTMLEventObj *impl_from_IHTMLEventObj(IHTMLEventObj *iface)
{
    return CONTAINING_RECORD(iface, HTMLEventObj, IHTMLEventObj_iface);
}
275 276 277

static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
{
278
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
279 280 281 282 283

    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
284
        *ppv = &This->IHTMLEventObj_iface;
285 286
    }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
        TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
287
        *ppv = &This->IHTMLEventObj_iface;
288 289
    }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
        return *ppv ? S_OK : E_NOINTERFACE;
290 291 292 293 294 295 296 297 298 299 300 301 302
    }

    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)
{
303
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
304 305 306 307 308 309 310 311 312
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
{
313
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
314 315 316 317
    LONG ref = InterlockedDecrement(&This->ref);

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

318
    if(!ref) {
319 320
        if(This->target)
            IHTMLDOMNode_Release(&This->target->IHTMLDOMNode_iface);
321 322
        if(This->nsevent)
            nsIDOMEvent_Release(This->nsevent);
323
        release_dispex(&This->dispex);
324
        heap_free(This);
325
    }
326 327 328 329 330 331

    return ref;
}

static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
{
332
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
333
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
334 335 336 337 338
}

static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
                                              LCID lcid, ITypeInfo **ppTInfo)
{
339
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
340
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
341 342 343 344 345 346
}

static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
                                                LPOLESTR *rgszNames, UINT cNames,
                                                LCID lcid, DISPID *rgDispId)
{
347
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
348 349
    return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
            lcid, rgDispId);
350 351 352 353 354 355
}

static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
356
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
357
    return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
358
            wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
359 360 361 362
}

static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
{
363
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
364 365 366

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

367 368 369 370
    *p = NULL;
    if(This->target)
        IHTMLDOMNode_QueryInterface(&This->target->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)p);
    return S_OK;
371 372 373 374
}

static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
{
375
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
376
    cpp_bool ret = FALSE;
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

    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;
401 402 403 404
}

static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
{
405
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
406
    cpp_bool ret = FALSE;
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

    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;
431 432 433 434
}

static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
{
435
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
436
    cpp_bool ret = FALSE;
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

    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;
461 462 463 464
}

static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
{
465
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
466 467 468 469

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

    if(V_VT(&v) != VT_BOOL) {
470
        FIXME("unsupported value %s\n", debugstr_variant(&v));
471 472 473 474 475 476
        return DISP_E_BADVARTYPE;
    }

    if(!V_BOOL(&v))
        This->prevent_default = TRUE;
    return S_OK;
477 478 479 480
}

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

483
    FIXME("(%p)->(%p)\n", This, p);
484 485 486

    V_VT(p) = VT_EMPTY;
    return S_OK;
487 488 489 490
}

static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
{
491
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
492 493 494 495 496

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

    This->cancel_bubble = !!v;
    return S_OK;
497 498 499 500
}

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

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

505
    *p = This->cancel_bubble ? VARIANT_TRUE : VARIANT_FALSE;
506
    return S_OK;
507 508 509 510
}

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

513
    FIXME("(%p)->(%p)\n", This, p);
514 515 516

    *p = NULL;
    return S_OK;
517 518 519 520
}

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

523
    FIXME("(%p)->(%p)\n", This, p);
524 525 526

    *p = NULL;
    return S_OK;
527 528
}

529
static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, LONG v)
530
{
531
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
532
    FIXME("(%p)->(%d)\n", This, v);
533 534 535
    return E_NOTIMPL;
}

536
static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, LONG *p)
537
{
538
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
    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;
556 557
}

558
static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, LONG *p)
559
{
560
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    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;
578 579 580 581
}

static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
{
582
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
583 584 585

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

586 587 588 589 590
    if(!This->type) {
        *p = NULL;
        return S_OK;
    }

591 592
    *p = SysAllocString(This->type->name);
    return *p ? S_OK : E_OUTOFMEMORY;
593 594 595 596
}

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

599
    FIXME("(%p)->(%p)\n", This, p);
600 601 602

    *p = NULL;
    return S_OK;
603 604
}

605
static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, LONG *p)
606
{
607
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
608

609
    FIXME("(%p)->(%p)\n", This, p);
610 611 612

    *p = 0;
    return S_OK;
613 614
}

615
static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, LONG *p)
616
{
617
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
618

619
    FIXME("(%p)->(%p)\n", This, p);
620 621 622

    *p = -1;
    return S_OK;
623 624
}

625
static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, LONG *p)
626
{
627
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
628

629
    FIXME("(%p)->(%p)\n", This, p);
630 631 632

    *p = -1;
    return S_OK;
633 634
}

635
static HRESULT WINAPI HTMLEventObj_get_clientX(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 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;
655 656
}

657
static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, LONG *p)
658
{
659
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
    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;
677 678
}

679
static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, LONG *p)
680
{
681
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
682

683
    FIXME("(%p)->(%p)\n", This, p);
684 685 686

    *p = 0;
    return S_OK;
687 688
}

689
static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, LONG *p)
690
{
691
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
692

693
    FIXME("(%p)->(%p)\n", This, p);
694 695 696

    *p = 0;
    return S_OK;
697 698
}

699
static HRESULT WINAPI HTMLEventObj_get_screenX(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 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;
719 720
}

721
static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, LONG *p)
722
{
723
    HTMLEventObj *This = impl_from_IHTMLEventObj(iface);
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
    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;
741 742 743 744
}

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

747
    FIXME("(%p)->(%p)\n", This, p);
748 749 750

    *p = NULL;
    return S_OK;
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
}

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

788 789 790 791 792
static inline HTMLEventObj *unsafe_impl_from_IHTMLEventObj(IHTMLEventObj *iface)
{
    return iface->lpVtbl == &HTMLEventObjVtbl ? impl_from_IHTMLEventObj(iface) : NULL;
}

793 794 795 796 797 798 799 800 801 802 803 804
static const tid_t HTMLEventObj_iface_tids[] = {
    IHTMLEventObj_tid,
    0
};

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

805
static HTMLEventObj *create_event(void)
806 807 808
{
    HTMLEventObj *ret;

809
    ret = heap_alloc_zero(sizeof(*ret));
810 811 812
    if(!ret)
        return NULL;

813
    ret->IHTMLEventObj_iface.lpVtbl = &HTMLEventObjVtbl;
814 815
    ret->ref = 1;

816 817 818 819 820 821 822 823 824 825
    init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLEventObj_iface, &HTMLEventObj_dispex);

    return ret;
}

static HRESULT set_event_info(HTMLEventObj *event, HTMLDOMNode *target, eventid_t eid, nsIDOMEvent *nsevent)
{
    event->type = event_info+eid;
    event->nsevent = nsevent;

826
    if(nsevent) {
827
        nsIDOMEvent_AddRef(nsevent);
828
    }else if(event_types[event_info[eid].type]) {
829
        nsAString type_str;
830 831
        nsresult nsres;

832
        nsAString_InitDepend(&type_str, event_types[event_info[eid].type]);
833
        nsres = nsIDOMHTMLDocument_CreateEvent(target->doc->nsdoc, &type_str, &event->nsevent);
834
        nsAString_Finish(&type_str);
835 836
        if(NS_FAILED(nsres)) {
            ERR("Could not create event: %08x\n", nsres);
837
            return E_FAIL;
838 839 840
        }
    }

841
    event->target = target;
842 843
    if(target)
        IHTMLDOMNode_AddRef(&target->IHTMLDOMNode_iface);
844
    return S_OK;
845 846
}

847 848 849 850
HRESULT create_event_obj(IHTMLEventObj **ret)
{
    HTMLEventObj *event;

851
    event = create_event();
852 853 854 855 856 857 858
    if(!event)
        return E_OUTOFMEMORY;

    *ret = &event->IHTMLEventObj_iface;
    return S_OK;
}

859
static HRESULT call_disp_func(IDispatch *disp, DISPPARAMS *dp, VARIANT *retv)
860 861
{
    IDispatchEx *dispex;
862
    EXCEPINFO ei;
863 864 865 866 867 868
    HRESULT hres;

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

    hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
    if(SUCCEEDED(hres)) {
869
        hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, dp, retv, &ei, NULL);
870 871 872 873
        IDispatchEx_Release(dispex);
    }else {
        TRACE("Could not get IDispatchEx interface: %08x\n", hres);
        hres = IDispatch_Invoke(disp, 0, &IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD,
874
                dp, retv, &ei, NULL);
875 876 877 878 879
    }

    return hres;
}

880
static HRESULT call_cp_func(IDispatch *disp, DISPID dispid, VARIANT *retv)
881 882 883 884 885 886
{
    DISPPARAMS dp = {NULL,NULL,0,0};
    ULONG argerr;
    EXCEPINFO ei;

    memset(&ei, 0, sizeof(ei));
887
    return IDispatch_Invoke(disp, dispid, &IID_NULL, 0, DISPATCH_METHOD, &dp, retv, &ei, &argerr);
888 889
}

890
static BOOL is_cp_event(cp_static_data_t *data, DISPID dispid)
891
{
892
    int min, max, i;
893 894 895 896 897 898 899 900
    HRESULT hres;

    if(!data)
        return FALSE;

    if(!data->ids) {
        hres = get_dispids(data->tid, &data->id_cnt, &data->ids);
        if(FAILED(hres))
901
            return FALSE;
902 903 904
    }

    min = 0;
905
    max = data->id_cnt-1;
906 907 908 909 910 911 912 913 914 915 916 917 918 919
    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;
}

920
static void call_event_handlers(HTMLDocumentNode *doc, HTMLEventObj *event_obj, event_target_t *event_target,
921
        ConnectionPointContainer *cp_container, eventid_t eid, IDispatch *this_obj)
922
{
923
    const BOOL cancelable = event_info[eid].flags & EVENT_CANCELABLE;
924
    handler_vector_t *handler_vector = NULL;
925
    VARIANT v;
926
    int i;
927 928
    HRESULT hres;

929 930
    if(event_target)
        handler_vector = event_target->event_table[eid];
931

932
    if(handler_vector && handler_vector->handler_prop) {
933 934 935 936 937 938
        DISPID named_arg = DISPID_THIS;
        VARIANTARG arg;
        DISPPARAMS dp = {&arg, &named_arg, 1, 1};

        V_VT(&arg) = VT_DISPATCH;
        V_DISPATCH(&arg) = this_obj;
939
        V_VT(&v) = VT_EMPTY;
940 941

        TRACE("%s >>>\n", debugstr_w(event_info[eid].name));
942 943 944 945 946 947 948 949 950 951 952 953 954 955
        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 {
956
            WARN("%s <<< %08x\n", debugstr_w(event_info[eid].name), hres);
957
        }
958
    }
959

960
    if(handler_vector && handler_vector->handler_cnt) {
961 962 963 964 965 966
        VARIANTARG arg;
        DISPPARAMS dp = {&arg, NULL, 1, 0};

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

967 968
        i = handler_vector->handler_cnt;
        while(i--) {
969
            if(handler_vector->handlers[i]) {
970 971
                V_VT(&v) = VT_EMPTY;

972
                TRACE("%s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
973 974
                hres = call_disp_func(handler_vector->handlers[i], &dp, &v);
                if(hres == S_OK) {
975
                    TRACE("%s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
976 977 978 979 980 981 982 983 984 985 986

                    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 {
987
                    WARN("%s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
988
                }
989 990 991
            }
        }
    }
992 993 994 995 996 997 998 999

    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) {
1000
            if(cp->sinks_size && is_cp_event(cp->data, event_info[eid].dispid)) {
1001
                for(i=0; i < cp->sinks_size; i++) {
1002 1003 1004
                    if(!cp->sinks[i].disp)
                        continue;

1005 1006
                    V_VT(&v) = VT_EMPTY;

1007
                    TRACE("cp %s [%d] >>>\n", debugstr_w(event_info[eid].name), i);
1008 1009
                    hres = call_cp_func(cp->sinks[i].disp, event_info[eid].dispid, &v);
                    if(hres == S_OK) {
1010
                        TRACE("cp %s [%d] <<<\n", debugstr_w(event_info[eid].name), i);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

                        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 {
1022
                        WARN("cp %s [%d] <<< %08x\n", debugstr_w(event_info[eid].name), i, hres);
1023
                    }
1024 1025 1026 1027
                }
            }
        }
    }
1028 1029
}

1030
static void fire_event_obj(HTMLDocumentNode *doc, eventid_t eid, HTMLEventObj *event_obj, nsIDOMNode *target)
1031
{
1032
    IHTMLEventObj *prev_event;
1033
    nsIDOMNode *parent, *nsnode;
1034
    BOOL prevent_default = FALSE;
1035
    HTMLInnerWindow *window;
1036
    HTMLDOMNode *node;
1037
    PRUint16 node_type;
1038
    nsresult nsres;
1039
    HRESULT hres;
1040

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

1043
    window = doc->window;
1044
    prev_event = window->event;
1045
    window->event = event_obj ? &event_obj->IHTMLEventObj_iface : NULL;
1046 1047

    nsIDOMNode_GetNodeType(target, &node_type);
1048 1049 1050
    nsnode = target;
    nsIDOMNode_AddRef(nsnode);

1051 1052 1053
    switch(node_type) {
    case ELEMENT_NODE:
        do {
1054
            hres = get_node(doc, nsnode, FALSE, &node);
1055
            if(SUCCEEDED(hres) && node) {
1056 1057
                call_event_handlers(doc, event_obj, *get_node_event_target(node),
                        node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1058 1059
                node_release(node);
            }
1060

1061
            if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1062 1063 1064 1065 1066 1067 1068
                break;

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

1070 1071
            nsIDOMNode_GetNodeType(nsnode, &node_type);
        }while(node_type == ELEMENT_NODE);
1072

1073
        if(!(event_info[eid].flags & EVENT_BUBBLE) || (event_obj && event_obj->cancel_bubble))
1074
            break;
1075

1076
    case DOCUMENT_NODE:
1077 1078 1079 1080
        if(event_info[eid].flags & EVENT_FORWARDBODY) {
            nsIDOMHTMLElement *nsbody;
            nsresult nsres;

1081
            nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
1082
            if(NS_SUCCEEDED(nsres) && nsbody) {
1083
                hres = get_node(doc, (nsIDOMNode*)nsbody, FALSE, &node);
1084
                if(SUCCEEDED(hres) && node) {
1085 1086
                    call_event_handlers(doc, event_obj, *get_node_event_target(node),
                            node->cp_container, eid, (IDispatch*)&node->IHTMLDOMNode_iface);
1087 1088
                    node_release(node);
                }
1089 1090 1091 1092 1093 1094
                nsIDOMHTMLElement_Release(nsbody);
            }else {
                ERR("Could not get body: %08x\n", nsres);
            }
        }

1095
        call_event_handlers(doc, event_obj, doc->node.event_target, &doc->basedoc.cp_container, eid,
1096
                (IDispatch*)&doc->basedoc.IHTMLDocument2_iface);
1097
        break;
1098

1099 1100
    default:
        FIXME("unimplemented node type %d\n", node_type);
1101 1102 1103 1104 1105
    }

    if(nsnode)
        nsIDOMNode_Release(nsnode);

1106 1107
    if(event_obj && event_obj->prevent_default)
        prevent_default = TRUE;
1108
    window->event = prev_event;
1109

1110
    if(!prevent_default && (event_info[eid].flags & EVENT_HASDEFAULTHANDLERS)) {
1111 1112 1113 1114 1115 1116 1117 1118
        nsIDOMNode_AddRef(target);
        nsnode = target;

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

1119 1120
            if(node) {
                if(node->vtbl->handle_event)
1121
                    hres = node->vtbl->handle_event(node, eid, event_obj ? event_obj->nsevent : NULL, &prevent_default);
1122
                node_release(node);
1123
                if(FAILED(hres) || prevent_default || (event_obj && event_obj->cancel_bubble))
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
                    break;
            }

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

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

        if(nsnode)
            nsIDOMNode_Release(nsnode);
    }

1139
    if(prevent_default && event_obj && event_obj->nsevent) {
1140
        TRACE("calling PreventDefault\n");
1141
        nsIDOMEvent_PreventDefault(event_obj->nsevent);
1142
    }
1143 1144
}

1145
void fire_event(HTMLDocumentNode *doc, eventid_t eid, BOOL set_event, nsIDOMNode *target, nsIDOMEvent *nsevent)
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
    HTMLEventObj *event_obj = NULL;
    HTMLDOMNode *node;
    HRESULT hres;

    if(set_event) {
        hres = get_node(doc, target, TRUE, &node);
        if(FAILED(hres))
            return;

        event_obj = create_event();
        node_release(node);
        if(!event_obj)
            return;

        hres = set_event_info(event_obj, node, eid, nsevent);
        if(FAILED(hres)) {
            IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
            return;
        }
    }

    fire_event_obj(doc, eid, event_obj, target);

    if(event_obj)
        IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
}

HRESULT dispatch_event(HTMLDOMNode *node, const WCHAR *event_name, VARIANT *event_var, VARIANT_BOOL *cancelled)
{
    HTMLEventObj *event_obj = NULL;
1177
    eventid_t eid;
1178
    HRESULT hres;
1179 1180 1181 1182 1183 1184 1185

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

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
    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;
            }
1200

1201 1202 1203 1204 1205 1206 1207
            event_obj = unsafe_impl_from_IHTMLEventObj(event_iface);
            if(!event_obj) {
                ERR("Not our IHTMLEventObj?\n");
                IHTMLEventObj_Release(event_iface);
                return E_FAIL;
            }
        }
1208 1209
    }

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
    if(event_obj) {
        hres = set_event_info(event_obj, node, eid, NULL);
        if(SUCCEEDED(hres))
            fire_event_obj(node->doc, eid, event_obj, node->nsnode);

        IHTMLEventObj_Release(&event_obj->IHTMLEventObj_iface);
        if(FAILED(hres))
            return hres;
    }else {
        if(!(event_info[eid].flags & EVENT_DEFAULTLISTENER)) {
            FIXME("not EVENT_DEFAULTEVENTHANDLER\n");
            return E_NOTIMPL;
        }

        fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
    }
1226 1227 1228 1229 1230

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

1231
HRESULT call_fire_event(HTMLDOMNode *node, eventid_t eid)
1232 1233 1234
{
    HRESULT hres;

1235
    if(node->vtbl->fire_event) {
1236 1237
        BOOL handled = FALSE;

1238
        hres = node->vtbl->fire_event(node, eid, &handled);
1239 1240 1241 1242
        if(handled)
            return hres;
    }

1243
    fire_event(node->doc, eid, TRUE, node->nsnode, NULL);
1244 1245 1246
    return S_OK;
}

1247 1248 1249 1250 1251 1252 1253 1254
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)
1255
{
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
    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;
1269

1270 1271 1272 1273 1274
    new_vector->handler_cnt = cnt;
    event_target->event_table[eid] = new_vector;
    return TRUE;
}

1275
static HRESULT ensure_nsevent_handler(HTMLDocumentNode *doc, event_target_t *event_target, nsIDOMNode *nsnode, eventid_t eid)
1276
{
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
    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))
1293 1294 1295 1296
        return S_OK;

    if(!doc->event_vector[eid]) {
        doc->event_vector[eid] = TRUE;
1297
        add_nsevent_listener(doc, NULL, event_info[eid].name);
1298 1299 1300 1301 1302
    }

    return S_OK;
}

1303 1304
void detach_events(HTMLDocumentNode *doc)
{
1305 1306
    if(doc->event_vector) {
        int i;
1307

1308 1309 1310 1311
        for(i=0; i < EVENTID_LAST; i++) {
            if(doc->event_vector[i])
                detach_nsevent(doc, event_info[i].name);
        }
1312
    }
1313 1314

    release_nsevents(doc);
1315 1316 1317
}


1318 1319
static HRESULT remove_event_handler(event_target_t **event_target, eventid_t eid)
{
1320
    if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1321 1322 1323 1324 1325 1326 1327
        IDispatch_Release((*event_target)->event_table[eid]->handler_prop);
        (*event_target)->event_table[eid]->handler_prop = NULL;
    }

    return S_OK;
}

1328
static HRESULT set_event_handler_disp(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocumentNode *doc,
1329
        eventid_t eid, IDispatch *disp)
1330 1331 1332
{
    event_target_t *event_target;

1333 1334 1335
    if(!disp)
        return remove_event_handler(event_target_ptr, eid);

1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
    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;
1347 1348
    IDispatch_AddRef(disp);

1349
    return ensure_nsevent_handler(doc, event_target, nsnode, eid);
1350 1351
}

1352
HRESULT set_event_handler(event_target_t **event_target, nsIDOMNode *nsnode, HTMLDocumentNode *doc, eventid_t eid, VARIANT *var)
1353 1354
{
    switch(V_VT(var)) {
1355
    case VT_NULL:
1356
        return remove_event_handler(event_target, eid);
1357

1358
    case VT_DISPATCH:
1359
        return set_event_handler_disp(event_target, nsnode, doc, eid, V_DISPATCH(var));
1360 1361

    default:
1362
        FIXME("not handler %s\n", debugstr_variant(var));
1363
        /* fall through */
1364
    case VT_EMPTY:
1365
        return E_NOTIMPL;
1366 1367
    }

1368
    return S_OK;
1369 1370
}

1371 1372
HRESULT get_event_handler(event_target_t **event_target, eventid_t eid, VARIANT *var)
{
1373
    if(*event_target && (*event_target)->event_table[eid] && (*event_target)->event_table[eid]->handler_prop) {
1374
        V_VT(var) = VT_DISPATCH;
1375
        V_DISPATCH(var) = (*event_target)->event_table[eid]->handler_prop;
1376 1377 1378 1379 1380 1381 1382 1383
        IDispatch_AddRef(V_DISPATCH(var));
    }else {
        V_VT(var) = VT_NULL;
    }

    return S_OK;
}

1384 1385
HRESULT attach_event(event_target_t **event_target_ptr, nsIDOMNode *nsnode, HTMLDocument *doc, BSTR name,
        IDispatch *disp, VARIANT_BOOL *res)
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
{
    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;
1415
    return ensure_nsevent_handler(doc->doc_node, event_target, nsnode, eid);
1416 1417
}

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
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;
}

1446
void update_cp_events(HTMLInnerWindow *window, event_target_t **event_target_ptr, cp_static_data_t *cp, nsIDOMNode *nsnode)
1447
{
1448
    event_target_t *event_target;
1449 1450
    int i;

1451 1452 1453 1454
    event_target = get_event_target(event_target_ptr);
    if(!event_target)
        return; /* FIXME */

1455 1456
    for(i=0; i < EVENTID_LAST; i++) {
        if((event_info[i].flags & EVENT_DEFAULTLISTENER) && is_cp_event(cp, event_info[i].dispid))
1457
            ensure_nsevent_handler(window->doc, event_target, nsnode, i);
1458 1459 1460
    }
}

1461
void check_event_attr(HTMLDocumentNode *doc, nsIDOMElement *nselem)
1462 1463 1464 1465 1466 1467 1468
{
    const PRUnichar *attr_value;
    nsAString attr_name_str, attr_value_str;
    IDispatch *disp;
    HTMLDOMNode *node;
    int i;
    nsresult nsres;
1469
    HRESULT hres;
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483

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

1484
            disp = script_parse_event(doc->window, attr_value);
1485
            if(disp) {
1486
                hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1487
                if(SUCCEEDED(hres)) {
1488
                    set_event_handler_disp(get_node_event_target(node), node->nsnode, node->doc, i, disp);
1489 1490
                    node_release(node);
                }
1491
                IDispatch_Release(disp);
1492 1493 1494 1495 1496 1497 1498 1499
            }
        }
    }

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

1500 1501
HRESULT doc_init_events(HTMLDocumentNode *doc)
{
1502 1503 1504
    unsigned i;
    HRESULT hres;

1505 1506 1507 1508 1509
    doc->event_vector = heap_alloc_zero(EVENTID_LAST*sizeof(BOOL));
    if(!doc->event_vector)
        return E_OUTOFMEMORY;

    init_nsevents(doc);
1510 1511

    for(i=0; i < EVENTID_LAST; i++) {
1512
        if(event_info[i].flags & EVENT_HASDEFAULTHANDLERS) {
1513 1514 1515 1516 1517 1518
            hres = ensure_nsevent_handler(doc, NULL, NULL, i);
            if(FAILED(hres))
                return hres;
        }
    }

1519 1520 1521
    return S_OK;
}

1522 1523
void release_event_target(event_target_t *event_target)
{
1524
    int i, j;
1525 1526

    for(i=0; i < EVENTID_LAST; i++) {
1527 1528 1529 1530
        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++)
1531 1532
                if(event_target->event_table[i]->handlers[j])
                    IDispatch_Release(event_target->event_table[i]->handlers[j]);
1533
        }
1534 1535 1536 1537
    }

    heap_free(event_target);
}