2 * Copyright 2007-2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 #include "mshtml_private.h"
34 #include "htmlevent.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39 const nsIDOMEventListenerVtbl *lpDOMEventListenerVtbl;
40 nsDocumentEventListener *This;
43 struct nsDocumentEventListener {
44 nsEventListener blur_listener;
45 nsEventListener focus_listener;
46 nsEventListener keypress_listener;
47 nsEventListener load_listener;
48 nsEventListener htmlevent_listener;
52 HTMLDocumentNode *doc;
55 static LONG release_listener(nsDocumentEventListener *This)
57 LONG ref = InterlockedDecrement(&This->ref);
59 TRACE("(%p) ref=%d\n", This, ref);
67 #define NSEVENTLIST_THIS(iface) DEFINE_THIS(nsEventListener, DOMEventListener, iface)
69 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
70 nsIIDRef riid, nsQIResult result)
72 nsEventListener *This = NSEVENTLIST_THIS(iface);
76 if(IsEqualGUID(&IID_nsISupports, riid)) {
77 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
78 *result = NSEVENTLIST(This);
79 }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
80 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
81 *result = NSEVENTLIST(This);
85 nsIWebBrowserChrome_AddRef(NSEVENTLIST(This));
89 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
90 return NS_NOINTERFACE;
93 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
95 nsDocumentEventListener *This = NSEVENTLIST_THIS(iface)->This;
96 LONG ref = InterlockedIncrement(&This->ref);
98 TRACE("(%p) ref=%d\n", This, ref);
103 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
105 nsDocumentEventListener *This = NSEVENTLIST_THIS(iface)->This;
107 return release_listener(This);
110 static BOOL is_doc_child_focus(HTMLDocumentObj *doc)
114 for(hwnd = GetFocus(); hwnd && hwnd != doc->hwnd; hwnd = GetParent(hwnd));
119 static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
121 HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
122 HTMLDocumentObj *doc_obj;
124 TRACE("(%p)\n", doc);
126 if(!doc || !doc->basedoc.doc_obj)
127 return NS_ERROR_FAILURE;
128 doc_obj = doc->basedoc.doc_obj;
130 if(!doc_obj->nscontainer->reset_focus && doc_obj->focus && !is_doc_child_focus(doc_obj)) {
131 doc_obj->focus = FALSE;
132 notif_focus(doc_obj);
138 static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
140 HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
141 HTMLDocumentObj *doc_obj;
143 TRACE("(%p)\n", doc);
146 return NS_ERROR_FAILURE;
147 doc_obj = doc->basedoc.doc_obj;
149 if(!doc_obj->nscontainer->reset_focus && !doc_obj->focus) {
150 doc_obj->focus = TRUE;
151 notif_focus(doc_obj);
157 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
160 HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
161 HTMLDocumentObj *doc_obj;
164 return NS_ERROR_FAILURE;
165 doc_obj = doc->basedoc.doc_obj;
167 TRACE("(%p)->(%p)\n", doc, event);
169 update_doc(&doc_obj->basedoc, UPDATE_UI);
170 if(doc_obj->usermode == EDITMODE)
171 handle_edit_event(&doc_obj->basedoc, event);
176 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
178 HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
179 HTMLDocumentObj *doc_obj;
180 nsIDOMHTMLElement *nsbody = NULL;
182 TRACE("(%p)\n", doc);
185 return NS_ERROR_FAILURE;
186 doc_obj = doc->basedoc.doc_obj;
188 connect_scripts(doc->basedoc.window);
190 if(doc_obj->nscontainer->editor_controller) {
191 nsIController_Release(doc_obj->nscontainer->editor_controller);
192 doc_obj->nscontainer->editor_controller = NULL;
195 if(doc_obj->usermode == EDITMODE)
196 handle_edit_load(&doc_obj->basedoc);
198 if(doc->basedoc.window->readystate != READYSTATE_COMPLETE)
199 set_ready_state(doc->basedoc.window, READYSTATE_COMPLETE);
203 return NS_ERROR_FAILURE;
206 nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
208 fire_event(doc, EVENTID_LOAD, (nsIDOMNode*)nsbody, event);
209 nsIDOMHTMLElement_Release(nsbody);
215 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
217 HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
218 const PRUnichar *type;
219 nsIDOMEventTarget *event_target;
225 nsAString_Init(&type_str, NULL);
226 nsIDOMEvent_GetType(event, &type_str);
227 nsAString_GetData(&type_str, &type);
228 eid = str_to_eid(type);
229 nsAString_Finish(&type_str);
231 nsres = nsIDOMEvent_GetTarget(event, &event_target);
232 if(NS_FAILED(nsres) || !event_target) {
233 ERR("GetEventTarget failed: %08x\n", nsres);
237 nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
238 nsIDOMEventTarget_Release(event_target);
239 if(NS_FAILED(nsres)) {
240 ERR("Could not get nsIDOMNode: %08x\n", nsres);
244 fire_event(doc, eid, nsnode, event);
246 nsIDOMNode_Release(nsnode);
251 #undef NSEVENTLIST_THIS
253 #define EVENTLISTENER_VTBL(handler) \
255 nsDOMEventListener_QueryInterface, \
256 nsDOMEventListener_AddRef, \
257 nsDOMEventListener_Release, \
261 static const nsIDOMEventListenerVtbl blur_vtbl = EVENTLISTENER_VTBL(handle_blur);
262 static const nsIDOMEventListenerVtbl focus_vtbl = EVENTLISTENER_VTBL(handle_focus);
263 static const nsIDOMEventListenerVtbl keypress_vtbl = EVENTLISTENER_VTBL(handle_keypress);
264 static const nsIDOMEventListenerVtbl load_vtbl = EVENTLISTENER_VTBL(handle_load);
265 static const nsIDOMEventListenerVtbl htmlevent_vtbl = EVENTLISTENER_VTBL(handle_htmlevent);
267 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
268 nsIDOMEventListener *listener, BOOL capture)
273 nsAString_Init(&type_str, type);
274 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture);
275 nsAString_Finish(&type_str);
277 ERR("AddEventTarget failed: %08x\n", nsres);
281 static void init_listener(nsEventListener *This, nsDocumentEventListener *listener,
282 const nsIDOMEventListenerVtbl *vtbl)
284 This->lpDOMEventListenerVtbl = vtbl;
285 This->This = listener;
288 void add_nsevent_listener(HTMLDocumentNode *doc, LPCWSTR type)
290 nsIDOMEventTarget *target;
293 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
294 if(NS_FAILED(nsres)) {
295 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
299 init_event(target, type, NSEVENTLIST(&doc->nsevent_listener->htmlevent_listener), TRUE);
300 nsIDOMEventTarget_Release(target);
303 void release_nsevents(HTMLDocumentNode *doc)
305 if(doc->nsevent_listener) {
306 doc->nsevent_listener->doc = NULL;
307 release_listener(doc->nsevent_listener);
308 doc->nsevent_listener = NULL;
312 void init_nsevents(HTMLDocumentNode *doc)
314 nsDocumentEventListener *listener;
315 nsIDOMEventTarget *target;
318 static const PRUnichar wsz_blur[] = {'b','l','u','r',0};
319 static const PRUnichar wsz_focus[] = {'f','o','c','u','s',0};
320 static const PRUnichar wsz_keypress[] = {'k','e','y','p','r','e','s','s',0};
321 static const PRUnichar wsz_load[] = {'l','o','a','d',0};
323 listener = heap_alloc(sizeof(nsDocumentEventListener));
330 init_listener(&listener->blur_listener, listener, &blur_vtbl);
331 init_listener(&listener->focus_listener, listener, &focus_vtbl);
332 init_listener(&listener->keypress_listener, listener, &keypress_vtbl);
333 init_listener(&listener->load_listener, listener, &load_vtbl);
334 init_listener(&listener->htmlevent_listener, listener, &htmlevent_vtbl);
336 doc->nsevent_listener = listener;
338 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
339 if(NS_FAILED(nsres)) {
340 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
344 init_event(target, wsz_blur, NSEVENTLIST(&listener->blur_listener), TRUE);
345 init_event(target, wsz_focus, NSEVENTLIST(&listener->focus_listener), TRUE);
346 init_event(target, wsz_keypress, NSEVENTLIST(&listener->keypress_listener), FALSE);
347 init_event(target, wsz_load, NSEVENTLIST(&listener->load_listener), TRUE);
349 nsIDOMEventTarget_Release(target);