nsevents.c 14.4 KB
Newer Older
1
/*
2
 * Copyright 2007-2008 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 29
#include "mshtmcid.h"
#include "shlguid.h"
30 31 32 33

#include "wine/debug.h"

#include "mshtml_private.h"
34
#include "htmlscript.h"
35
#include "htmlevent.h"
36
#include "binding.h"
37
#include "resource.h"
38 39 40

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

41 42 43 44 45
static const PRUnichar blurW[]      = {'b','l','u','r',0};
static const PRUnichar focusW[]     = {'f','o','c','u','s',0};
static const PRUnichar keypressW[]  = {'k','e','y','p','r','e','s','s',0};
static const PRUnichar loadW[]      = {'l','o','a','d',0};

46
typedef struct {
47
    nsIDOMEventListener nsIDOMEventListener_iface;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    nsDocumentEventListener *This;
} nsEventListener;

struct nsDocumentEventListener {
    nsEventListener blur_listener;
    nsEventListener focus_listener;
    nsEventListener keypress_listener;
    nsEventListener load_listener;
    nsEventListener htmlevent_listener;

    LONG ref;

    HTMLDocumentNode *doc;
};

static LONG release_listener(nsDocumentEventListener *This)
{
    LONG ref = InterlockedDecrement(&This->ref);

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

    if(!ref)
        heap_free(This);

    return ref;
}

75 76 77 78
static inline nsEventListener *impl_from_nsIDOMEventListener(nsIDOMEventListener *iface)
{
    return CONTAINING_RECORD(iface, nsEventListener, nsIDOMEventListener_iface);
}
79 80

static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
81
        nsIIDRef riid, void **result)
82
{
83
    nsEventListener *This = impl_from_nsIDOMEventListener(iface);
84 85 86 87 88

    *result = NULL;

    if(IsEqualGUID(&IID_nsISupports, riid)) {
        TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
89
        *result = &This->nsIDOMEventListener_iface;
90 91
    }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
        TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
92
        *result = &This->nsIDOMEventListener_iface;
93 94 95
    }

    if(*result) {
96
        nsIDOMEventListener_AddRef(&This->nsIDOMEventListener_iface);
97 98 99 100 101 102 103 104 105
        return NS_OK;
    }

    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
    return NS_NOINTERFACE;
}

static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
{
106 107
    nsEventListener *This = impl_from_nsIDOMEventListener(iface);
    LONG ref = InterlockedIncrement(&This->This->ref);
108

109
    TRACE("(%p) ref=%d\n", This->This, ref);
110 111

    return ref;
112 113 114 115
}

static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
{
116
    nsEventListener *This = impl_from_nsIDOMEventListener(iface);
117

118
    return release_listener(This->This);
119 120
}

121
static BOOL is_doc_child_focus(GeckoBrowser *nscontainer)
122 123 124
{
    HWND hwnd;

125
    for(hwnd = GetFocus(); hwnd && hwnd != nscontainer->hwnd; hwnd = GetParent(hwnd));
126

127
    return hwnd != NULL;
128 129 130 131
}

static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
{
132 133
    nsEventListener *This = impl_from_nsIDOMEventListener(iface);
    HTMLDocumentNode *doc = This->This->doc;
134
    HTMLDocumentObj *doc_obj;
135

136 137
    TRACE("(%p)\n", doc);

138
    if(!doc || !doc->basedoc.doc_obj)
139 140
        return NS_ERROR_FAILURE;
    doc_obj = doc->basedoc.doc_obj;
141

142
    if(doc_obj->focus && !is_doc_child_focus(doc_obj->nscontainer)) {
143 144
        doc_obj->focus = FALSE;
        notif_focus(doc_obj);
145 146 147 148 149 150 151
    }

    return NS_OK;
}

static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
{
152 153
    nsEventListener *This = impl_from_nsIDOMEventListener(iface);
    HTMLDocumentNode *doc = This->This->doc;
154
    HTMLDocumentObj *doc_obj;
155

156
    TRACE("(%p)\n", doc);
157

158 159 160 161
    if(!doc)
        return NS_ERROR_FAILURE;
    doc_obj = doc->basedoc.doc_obj;

162
    if(!doc_obj->focus) {
163 164
        doc_obj->focus = TRUE;
        notif_focus(doc_obj);
165 166 167 168 169
    }

    return NS_OK;
}

170 171 172
static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
        nsIDOMEvent *event)
{
173 174
    nsEventListener *This = impl_from_nsIDOMEventListener(iface);
    HTMLDocumentNode *doc = This->This->doc;
175

176
    if(!doc || !doc->browser)
177
        return NS_ERROR_FAILURE;
178

179
    TRACE("(%p)->(%p)\n", doc, event);
180

181
    update_doc(doc->browser->doc, UPDATE_UI);
182
    if(doc->browser->usermode == EDITMODE)
183
        handle_edit_event(doc, event);
184 185 186 187

    return NS_OK;
}

188 189
static void handle_docobj_load(HTMLDocumentObj *doc)
{
190
    IOleCommandTarget *olecmd = NULL;
191 192
    HRESULT hres;

193 194 195 196
    if(doc->nscontainer->editor_controller) {
        nsIController_Release(doc->nscontainer->editor_controller);
        doc->nscontainer->editor_controller = NULL;
    }
197

198
    if(doc->nscontainer->usermode == EDITMODE)
199
        setup_editor_controller(doc->nscontainer);
200

201 202
    if(doc->client) {
        hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
203 204 205 206 207 208 209 210 211 212 213 214
        if(FAILED(hres))
            olecmd = NULL;
    }

    if(doc->download_state) {
        if(olecmd) {
            VARIANT progress;

            V_VT(&progress) = VT_I4;
            V_I4(&progress) = 0;
            IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS,
                    OLECMDEXECOPT_DONTPROMPTUSER, &progress, NULL);
215
        }
216 217

        set_download_state(doc, 0);
218
    }
219

220 221 222 223 224 225 226
    if(olecmd) {
        IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
        IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
        IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);

        IOleCommandTarget_Release(olecmd);
    }
227 228
}

229 230
static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
{
231 232
    nsEventListener *This = impl_from_nsIDOMEventListener(iface);
    HTMLDocumentNode *doc = This->This->doc;
233
    HTMLDocumentObj *doc_obj = NULL;
234 235
    DOMEvent *load_event;
    HRESULT hres;
236

237
    TRACE("(%p)\n", doc);
238

239
    if(!doc || !doc->basedoc.window)
240
        return NS_ERROR_FAILURE;
241 242
    if(doc->basedoc.doc_obj && doc->basedoc.doc_obj->basedoc.doc_node == doc)
        doc_obj = doc->basedoc.doc_obj;
243

244
    connect_scripts(doc->window);
245

246 247
    htmldoc_addref(&doc->basedoc);

248
    if(doc_obj)
249 250 251 252
        handle_docobj_load(doc_obj);

    set_ready_state(doc->basedoc.window, READYSTATE_COMPLETE);

253
    if(doc_obj) {
254 255 256
        if(doc_obj->view_sink)
            IAdviseSink_OnViewChange(doc_obj->view_sink, DVASPECT_CONTENT, -1);

257
        set_statustext(doc_obj, IDS_STATUS_DONE, NULL);
258 259 260

        update_title(doc_obj);
    }
261

262
    if(doc_obj && doc_obj->nscontainer->usermode != EDITMODE && doc_obj->doc_object_service
263
       && !(doc->basedoc.window->load_flags & BINDING_REFRESH))
264
        IDocObjectService_FireDocumentComplete(doc_obj->doc_object_service,
265
                &doc->basedoc.window->base.IHTMLWindow2_iface, 0);
266

267
    if(doc->nsdoc) {
268 269
        hres = create_document_event(doc, EVENTID_LOAD, &load_event);
        if(SUCCEEDED(hres)) {
270
            dispatch_event(&doc->node.event_target, load_event);
271 272
            IDOMEvent_Release(&load_event->IDOMEvent_iface);
        }
273 274 275
    }else {
        WARN("no nsdoc\n");
    }
276

277
    if(doc->window) {
278 279
        hres = create_event_from_nsevent(event, &load_event);
        if(SUCCEEDED(hres)) {
280
            dispatch_event(&doc->window->event_target, load_event);
281 282
            IDOMEvent_Release(&load_event->IDOMEvent_iface);
        }
283
    }else {
284
        WARN("no window\n");
285
    }
286

287
    htmldoc_release(&doc->basedoc);
288
    return NS_OK;
289 290
}

291
static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *nsevent)
292
{
293 294
    nsEventListener *This = impl_from_nsIDOMEventListener(iface);
    HTMLDocumentNode *doc = This->This->doc;
295 296
    nsIDOMEventTarget *event_target;
    nsIDOMNode *nsnode;
297
    HTMLDOMNode *node;
298
    DOMEvent *event;
299
    nsresult nsres;
300
    HRESULT hres;
301

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

304 305 306 307 308
    if(!doc) {
        WARN("NULL doc\n");
        return NS_OK;
    }

309
    nsres = nsIDOMEvent_GetTarget(nsevent, &event_target);
310 311 312 313 314 315 316 317 318 319 320 321
    if(NS_FAILED(nsres) || !event_target) {
        ERR("GetEventTarget failed: %08x\n", nsres);
        return NS_OK;
    }

    nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
    nsIDOMEventTarget_Release(event_target);
    if(NS_FAILED(nsres)) {
        ERR("Could not get nsIDOMNode: %08x\n", nsres);
        return NS_OK;
    }

322
    hres = get_node(nsnode, TRUE, &node);
323
    nsIDOMNode_Release(nsnode);
324 325 326
    if(FAILED(hres))
        return NS_OK;

327 328 329 330 331 332
    hres = create_event_from_nsevent(nsevent, &event);
    if(FAILED(hres)) {
        node_release(node);
        return NS_OK;
    }

333
    /* If we fine need for more special cases here, we may consider handling it in a more generic way. */
334 335 336 337 338
    if(event->event_id == EVENTID_FOCUS || event->event_id == EVENTID_BLUR) {
        DOMEvent *focus_event;

        hres = create_document_event(doc, event->event_id == EVENTID_FOCUS ? EVENTID_FOCUSIN : EVENTID_FOCUSOUT, &focus_event);
        if(SUCCEEDED(hres)) {
339
            dispatch_event(&node->event_target, focus_event);
340 341
            IDOMEvent_Release(&focus_event->IDOMEvent_iface);
        }
342 343
    }

344
    dispatch_event(&node->event_target, event);
345

346
    IDOMEvent_Release(&event->IDOMEvent_iface);
347
    node_release(node);
348 349 350
    return NS_OK;
}

351 352 353 354 355 356
#define EVENTLISTENER_VTBL(handler) \
    { \
        nsDOMEventListener_QueryInterface, \
        nsDOMEventListener_AddRef, \
        nsDOMEventListener_Release, \
        handler, \
357
    }
358

359 360
static const nsIDOMEventListenerVtbl blur_vtbl =      EVENTLISTENER_VTBL(handle_blur);
static const nsIDOMEventListenerVtbl focus_vtbl =     EVENTLISTENER_VTBL(handle_focus);
361
static const nsIDOMEventListenerVtbl keypress_vtbl =  EVENTLISTENER_VTBL(handle_keypress);
362
static const nsIDOMEventListenerVtbl load_vtbl =      EVENTLISTENER_VTBL(handle_load);
363
static const nsIDOMEventListenerVtbl htmlevent_vtbl = EVENTLISTENER_VTBL(handle_htmlevent);
364 365 366 367 368 369 370

static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
        nsIDOMEventListener *listener, BOOL capture)
{
    nsAString type_str;
    nsresult nsres;

371
    nsAString_InitDepend(&type_str, type);
372
    nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture, FALSE, 1);
373 374 375 376 377 378
    nsAString_Finish(&type_str);
    if(NS_FAILED(nsres))
        ERR("AddEventTarget failed: %08x\n", nsres);

}

379
static void init_listener(nsEventListener *This, nsDocumentEventListener *listener,
380 381
        const nsIDOMEventListenerVtbl *vtbl)
{
382
    This->nsIDOMEventListener_iface.lpVtbl = vtbl;
383
    This->This = listener;
384 385
}

386 387 388 389 390 391 392 393 394 395 396
static nsIDOMEventTarget *get_default_document_target(HTMLDocumentNode *doc)
{
    nsIDOMEventTarget *target;
    nsISupports *target_iface;
    nsresult nsres;

    target_iface = doc->window ? (nsISupports*)doc->basedoc.window->nswindow : (nsISupports*)doc->nsdoc;
    nsres = nsISupports_QueryInterface(target_iface, &IID_nsIDOMEventTarget, (void**)&target);
    return NS_SUCCEEDED(nsres) ? target : NULL;
}

397
void add_nsevent_listener(HTMLDocumentNode *doc, nsIDOMNode *nsnode, LPCWSTR type)
398 399 400 401
{
    nsIDOMEventTarget *target;
    nsresult nsres;

402
    if(nsnode) {
403
        nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMEventTarget, (void**)&target);
404 405 406 407 408
        assert(nsres == NS_OK);
    }else {
        target = get_default_document_target(doc);
        if(!target)
            return;
409 410
    }

411 412
    init_event(target, type, &doc->nsevent_listener->htmlevent_listener.nsIDOMEventListener_iface,
            TRUE);
413 414 415
    nsIDOMEventTarget_Release(target);
}

416
static void detach_nslistener(HTMLDocumentNode *doc, const WCHAR *type, nsEventListener *listener, cpp_bool is_capture)
417 418 419 420 421
{
    nsIDOMEventTarget *target;
    nsAString type_str;
    nsresult nsres;

422 423
    target = get_default_document_target(doc);
    if(!target)
424 425 426 427
        return;

    nsAString_InitDepend(&type_str, type);
    nsres = nsIDOMEventTarget_RemoveEventListener(target, &type_str,
428
            &listener->nsIDOMEventListener_iface, is_capture);
429 430 431 432 433 434
    nsAString_Finish(&type_str);
    nsIDOMEventTarget_Release(target);
    if(NS_FAILED(nsres))
        ERR("RemoveEventTarget failed: %08x\n", nsres);
}

435 436 437 438 439
void detach_nsevent(HTMLDocumentNode *doc, const WCHAR *type)
{
    detach_nslistener(doc, type, &doc->nsevent_listener->htmlevent_listener, TRUE);
}

440 441
void release_nsevents(HTMLDocumentNode *doc)
{
442 443
    nsDocumentEventListener *listener = doc->nsevent_listener;

444 445
    TRACE("%p %p\n", doc, doc->nsevent_listener);

446 447 448 449 450 451 452 453 454 455 456
    if(!listener)
        return;

    detach_nslistener(doc, blurW,     &listener->blur_listener,     TRUE);
    detach_nslistener(doc, focusW,    &listener->focus_listener,    TRUE);
    detach_nslistener(doc, keypressW, &listener->keypress_listener, FALSE);
    detach_nslistener(doc, loadW,     &listener->load_listener,     TRUE);

    listener->doc = NULL;
    release_listener(listener);
    doc->nsevent_listener = NULL;
457 458 459
}

void init_nsevents(HTMLDocumentNode *doc)
460
{
461
    nsDocumentEventListener *listener;
462 463
    nsIDOMEventTarget *target;

464 465
    listener = heap_alloc(sizeof(nsDocumentEventListener));
    if(!listener)
466 467
        return;

468 469
    TRACE("%p %p\n", doc, listener);

470 471 472 473 474 475 476 477 478 479 480
    listener->ref = 1;
    listener->doc = doc;

    init_listener(&listener->blur_listener,        listener, &blur_vtbl);
    init_listener(&listener->focus_listener,       listener, &focus_vtbl);
    init_listener(&listener->keypress_listener,    listener, &keypress_vtbl);
    init_listener(&listener->load_listener,        listener, &load_vtbl);
    init_listener(&listener->htmlevent_listener,   listener, &htmlevent_vtbl);

    doc->nsevent_listener = listener;

481 482
    target = get_default_document_target(doc);
    if(!target)
483 484
        return;

485 486 487 488
    init_event(target, blurW,     &listener->blur_listener.nsIDOMEventListener_iface,     TRUE);
    init_event(target, focusW,    &listener->focus_listener.nsIDOMEventListener_iface,    TRUE);
    init_event(target, keypressW, &listener->keypress_listener.nsIDOMEventListener_iface, FALSE);
    init_event(target, loadW,     &listener->load_listener.nsIDOMEventListener_iface,     TRUE);
489 490 491

    nsIDOMEventTarget_Release(target);
}