nsevents.c 8.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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
/*
 * Copyright 2007 Jacek Caban for CodeWeavers
 *
 * 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 "config.h"

#include <stdarg.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"

#include "wine/debug.h"
#include "wine/unicode.h"

#include "mshtml_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

#define NSEVENTLIST_THIS(iface) DEFINE_THIS(nsEventListener, DOMEventListener, iface)

static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
                                                        nsIIDRef riid, nsQIResult result)
{
    nsEventListener *This = NSEVENTLIST_THIS(iface);

    *result = NULL;

    if(IsEqualGUID(&IID_nsISupports, riid)) {
        TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
        *result = NSEVENTLIST(This);
    }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
        TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
        *result = NSEVENTLIST(This);
    }

    if(*result) {
        nsIWebBrowserChrome_AddRef(NSEVENTLIST(This));
        return NS_OK;
    }

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

static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
{
    NSContainer *This = NSEVENTLIST_THIS(iface)->This;
    return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
}

static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
{
    NSContainer *This = NSEVENTLIST_THIS(iface)->This;
    return nsIWebBrowserChrome_Release(NSWBCHROME(This));
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static BOOL is_doc_child_focus(NSContainer *This)
{
    HWND hwnd;

    if(!This->doc)
        return FALSE;

    for(hwnd = GetFocus(); hwnd && hwnd != This->doc->hwnd; hwnd = GetParent(hwnd));

    return hwnd == This->doc->hwnd;
}

static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
{
    NSContainer *This = NSEVENTLIST_THIS(iface)->This;

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

93
    if(!This->reset_focus && This->doc && This->doc->focus && !is_doc_child_focus(This)) {
94 95 96 97 98 99 100 101 102 103 104 105 106
        This->doc->focus = FALSE;
        notif_focus(This->doc);
    }

    return NS_OK;
}

static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
{
    NSContainer *This = NSEVENTLIST_THIS(iface)->This;

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

107
    if(!This->reset_focus && This->doc && !This->doc->focus) {
108 109 110 111 112 113 114
        This->doc->focus = TRUE;
        notif_focus(This->doc);
    }

    return NS_OK;
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128
static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
        nsIDOMEvent *event)
{
    NSContainer *This = NSEVENTLIST_THIS(iface)->This;

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

    update_doc(This->doc, UPDATE_UI);
    if(This->doc->usermode == EDITMODE)
        handle_edit_event(This->doc, event);

    return NS_OK;
}

129 130 131
static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
{
    NSContainer *This = NSEVENTLIST_THIS(iface)->This;
132 133 134
    nsIDOMHTMLDocument *nshtmldoc;
    nsIDOMHTMLElement *nsbody = NULL;
    nsIDOMDocument *nsdoc;
135 136 137 138 139 140 141
    task_t *task;

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

    if(!This->doc)
        return NS_OK;

142
    connect_scripts(This->doc);
143 144
    setup_nswindow(This->doc->window);

145 146 147 148 149 150
    if(This->editor_controller) {
        nsIController_Release(This->editor_controller);
        This->editor_controller = NULL;
    }

    if(This->doc->usermode == EDITMODE)
151
        handle_edit_load(This->doc);
152

153
    task = heap_alloc(sizeof(task_t));
154 155 156 157 158 159 160 161 162 163 164

    task->doc = This->doc;
    task->task_id = TASK_PARSECOMPLETE;
    task->next = NULL;

    /*
     * This should be done in the worker thread that parses HTML,
     * but we don't have such thread (Gecko parses HTML for us).
     */
    push_task(task);

165 166 167 168 169 170 171 172 173 174 175 176 177

    nsIWebNavigation_GetDocument(This->navigation, &nsdoc);
    nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
    nsIDOMDocument_Release(nsdoc);

    nsIDOMHTMLDocument_GetBody(nshtmldoc, &nsbody);
    nsIDOMHTMLDocument_Release(nshtmldoc);

    if(nsbody) {
        fire_event(This->doc, EVENTID_LOAD, (nsIDOMNode*)nsbody);
        nsIDOMHTMLElement_Release(nsbody);
    }

178 179 180
    return NS_OK;
}

181 182 183 184 185
static nsresult NSAPI handle_node_insert(nsIDOMEventListener *iface, nsIDOMEvent *event)
{
    NSContainer *This = NSEVENTLIST_THIS(iface)->This;
    nsIDOMHTMLScriptElement *script;
    nsIDOMEventTarget *target;
186
    nsIDOMElement *elem;
187 188 189 190 191 192 193
    nsresult nsres;

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

    nsres = nsIDOMEvent_GetTarget(event, &target);
    if(NS_FAILED(nsres)) {
        ERR("GetTarget failed: %08x\n", nsres);
194
        return NS_OK;
195 196
    }

197 198 199 200 201 202
    nsres = nsIDOMEventTarget_QueryInterface(target, &IID_nsIDOMElement, (void**)&elem);
    nsIDOMEventTarget_Release(target);
    if(NS_FAILED(nsres))
        return NS_OK;

    nsres = nsIDOMElement_QueryInterface(elem, &IID_nsIDOMHTMLScriptElement, (void**)&script);
203 204 205 206 207
    if(SUCCEEDED(nsres)) {
        doc_insert_script(This->doc, script);
        nsIDOMHTMLScriptElement_Release(script);
    }

208 209 210
    check_event_attr(This->doc, elem);

    nsIDOMNode_Release(elem);
211 212 213
    return NS_OK;
}

214 215 216 217 218 219 220 221
#undef NSEVENTLIST_THIS

#define EVENTLISTENER_VTBL(handler) \
    { \
        nsDOMEventListener_QueryInterface, \
        nsDOMEventListener_AddRef, \
        nsDOMEventListener_Release, \
        handler, \
222
    }
223

224 225
static const nsIDOMEventListenerVtbl blur_vtbl =      EVENTLISTENER_VTBL(handle_blur);
static const nsIDOMEventListenerVtbl focus_vtbl =     EVENTLISTENER_VTBL(handle_focus);
226
static const nsIDOMEventListenerVtbl keypress_vtbl =  EVENTLISTENER_VTBL(handle_keypress);
227
static const nsIDOMEventListenerVtbl load_vtbl =      EVENTLISTENER_VTBL(handle_load);
228
static const nsIDOMEventListenerVtbl node_insert_vtbl = EVENTLISTENER_VTBL(handle_node_insert);
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

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

    nsAString_Init(&type_str, type);
    nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture);
    nsAString_Finish(&type_str);
    if(NS_FAILED(nsres))
        ERR("AddEventTarget failed: %08x\n", nsres);

}

static void init_listener(nsEventListener *This, NSContainer *container,
        const nsIDOMEventListenerVtbl *vtbl)
{
    This->lpDOMEventListenerVtbl = vtbl;
    This->This = container;
}

void init_nsevents(NSContainer *This)
{
    nsIDOMWindow *dom_window;
    nsIDOMEventTarget *target;
    nsresult nsres;

257 258
    static const PRUnichar wsz_blur[]      = {'b','l','u','r',0};
    static const PRUnichar wsz_focus[]     = {'f','o','c','u','s',0};
259
    static const PRUnichar wsz_keypress[]  = {'k','e','y','p','r','e','s','s',0};
260
    static const PRUnichar wsz_load[]      = {'l','o','a','d',0};
261 262
    static const PRUnichar DOMNodeInsertedW[] =
        {'D','O','M','N','o','d','e','I','n','s','e','r','t','e','d',0};
263

264 265
    init_listener(&This->blur_listener,        This, &blur_vtbl);
    init_listener(&This->focus_listener,       This, &focus_vtbl);
266
    init_listener(&This->keypress_listener,    This, &keypress_vtbl);
267
    init_listener(&This->load_listener,        This, &load_vtbl);
268
    init_listener(&This->node_insert_listener, This, &node_insert_vtbl);
269 270 271 272 273 274 275 276 277 278 279 280 281 282

    nsres = nsIWebBrowser_GetContentDOMWindow(This->webbrowser, &dom_window);
    if(NS_FAILED(nsres)) {
        ERR("GetContentDOMWindow failed: %08x\n", nsres);
        return;
    }

    nsres = nsIDOMWindow_QueryInterface(dom_window, &IID_nsIDOMEventTarget, (void**)&target);
    nsIDOMWindow_Release(dom_window);
    if(NS_FAILED(nsres)) {
        ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
        return;
    }

283 284
    init_event(target, wsz_blur,       NSEVENTLIST(&This->blur_listener),        TRUE);
    init_event(target, wsz_focus,      NSEVENTLIST(&This->focus_listener),       TRUE);
285
    init_event(target, wsz_keypress,   NSEVENTLIST(&This->keypress_listener),    FALSE);
286
    init_event(target, wsz_load,       NSEVENTLIST(&This->load_listener),        TRUE);
287
    init_event(target, DOMNodeInsertedW,NSEVENTLIST(&This->node_insert_listener),TRUE);
288 289 290

    nsIDOMEventTarget_Release(target);
}