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
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
35 #include "mshtml_private.h"
36 #include "htmlevent.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41 const nsIDOMEventListenerVtbl *lpDOMEventListenerVtbl;
42 nsDocumentEventListener *This;
45 struct nsDocumentEventListener {
46 nsEventListener blur_listener;
47 nsEventListener focus_listener;
48 nsEventListener keypress_listener;
49 nsEventListener load_listener;
50 nsEventListener htmlevent_listener;
54 HTMLDocumentNode *doc;
57 static LONG release_listener(nsDocumentEventListener *This)
59 LONG ref = InterlockedDecrement(&This->ref);
61 TRACE("(%p) ref=%d\n", This, ref);
69 #define NSEVENTLIST_THIS(iface) DEFINE_THIS(nsEventListener, DOMEventListener, iface)
71 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
72 nsIIDRef riid, nsQIResult result)
74 nsEventListener *This = NSEVENTLIST_THIS(iface);
78 if(IsEqualGUID(&IID_nsISupports, riid)) {
79 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
80 *result = NSEVENTLIST(This);
81 }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
82 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
83 *result = NSEVENTLIST(This);
87 nsIWebBrowserChrome_AddRef(NSEVENTLIST(This));
91 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
92 return NS_NOINTERFACE;
95 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
97 nsDocumentEventListener *This = NSEVENTLIST_THIS(iface)->This;
98 LONG ref = InterlockedIncrement(&This->ref);
100 TRACE("(%p) ref=%d\n", This, ref);
105 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
107 nsDocumentEventListener *This = NSEVENTLIST_THIS(iface)->This;
109 return release_listener(This);
112 static BOOL is_doc_child_focus(HTMLDocumentObj *doc)
116 for(hwnd = GetFocus(); hwnd && hwnd != doc->hwnd; hwnd = GetParent(hwnd));
121 static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
123 HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
124 HTMLDocumentObj *doc_obj;
126 TRACE("(%p)\n", doc);
128 if(!doc || !doc->basedoc.doc_obj)
129 return NS_ERROR_FAILURE;
130 doc_obj = doc->basedoc.doc_obj;
132 if(!doc_obj->nscontainer->reset_focus && doc_obj->focus && !is_doc_child_focus(doc_obj)) {
133 doc_obj->focus = FALSE;
134 notif_focus(doc_obj);
140 static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
142 HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
143 HTMLDocumentObj *doc_obj;
145 TRACE("(%p)\n", doc);
148 return NS_ERROR_FAILURE;
149 doc_obj = doc->basedoc.doc_obj;
151 if(!doc_obj->nscontainer->reset_focus && !doc_obj->focus) {
152 doc_obj->focus = TRUE;
153 notif_focus(doc_obj);
159 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
162 HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
163 HTMLDocumentObj *doc_obj;
166 return NS_ERROR_FAILURE;
167 doc_obj = doc->basedoc.doc_obj;
169 TRACE("(%p)->(%p)\n", doc, event);
171 update_doc(&doc_obj->basedoc, UPDATE_UI);
172 if(doc_obj->usermode == EDITMODE)
173 handle_edit_event(&doc_obj->basedoc, event);
178 static void handle_docobj_load(HTMLDocumentObj *doc)
180 IOleCommandTarget *olecmd = NULL;
186 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
187 if(SUCCEEDED(hres)) {
188 if(doc->download_state) {
189 VARIANT state, progress;
191 V_VT(&progress) = VT_I4;
193 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS,
194 OLECMDEXECOPT_DONTPROMPTUSER, &progress, NULL);
196 V_VT(&state) = VT_I4;
198 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE,
199 OLECMDEXECOPT_DONTPROMPTUSER, &state, NULL);
202 IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
203 IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
204 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
206 IOleCommandTarget_Release(olecmd);
208 doc->download_state = 0;
211 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
213 HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
214 HTMLDocumentObj *doc_obj;
215 nsIDOMHTMLElement *nsbody = NULL;
217 TRACE("(%p)\n", doc);
220 return NS_ERROR_FAILURE;
221 doc_obj = doc->basedoc.doc_obj;
223 connect_scripts(doc->basedoc.window);
225 if(doc_obj->nscontainer->editor_controller) {
226 nsIController_Release(doc_obj->nscontainer->editor_controller);
227 doc_obj->nscontainer->editor_controller = NULL;
230 if(doc_obj->usermode == EDITMODE)
231 handle_edit_load(&doc_obj->basedoc);
233 if(doc == doc_obj->basedoc.doc_node)
234 handle_docobj_load(doc_obj);
236 set_ready_state(doc->basedoc.window, READYSTATE_COMPLETE);
238 if(doc == doc_obj->basedoc.doc_node) {
240 static const WCHAR wszDone[] = {'D','o','n','e',0};
241 IOleInPlaceFrame_SetStatusText(doc_obj->frame, wszDone);
244 update_title(doc_obj);
249 return NS_ERROR_FAILURE;
252 nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
254 fire_event(doc, EVENTID_LOAD, (nsIDOMNode*)nsbody, event);
255 nsIDOMHTMLElement_Release(nsbody);
261 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
263 HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
264 const PRUnichar *type;
265 nsIDOMEventTarget *event_target;
271 nsAString_Init(&type_str, NULL);
272 nsIDOMEvent_GetType(event, &type_str);
273 nsAString_GetData(&type_str, &type);
274 eid = str_to_eid(type);
275 nsAString_Finish(&type_str);
277 nsres = nsIDOMEvent_GetTarget(event, &event_target);
278 if(NS_FAILED(nsres) || !event_target) {
279 ERR("GetEventTarget failed: %08x\n", nsres);
283 nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
284 nsIDOMEventTarget_Release(event_target);
285 if(NS_FAILED(nsres)) {
286 ERR("Could not get nsIDOMNode: %08x\n", nsres);
290 fire_event(doc, eid, nsnode, event);
292 nsIDOMNode_Release(nsnode);
297 #undef NSEVENTLIST_THIS
299 #define EVENTLISTENER_VTBL(handler) \
301 nsDOMEventListener_QueryInterface, \
302 nsDOMEventListener_AddRef, \
303 nsDOMEventListener_Release, \
307 static const nsIDOMEventListenerVtbl blur_vtbl = EVENTLISTENER_VTBL(handle_blur);
308 static const nsIDOMEventListenerVtbl focus_vtbl = EVENTLISTENER_VTBL(handle_focus);
309 static const nsIDOMEventListenerVtbl keypress_vtbl = EVENTLISTENER_VTBL(handle_keypress);
310 static const nsIDOMEventListenerVtbl load_vtbl = EVENTLISTENER_VTBL(handle_load);
311 static const nsIDOMEventListenerVtbl htmlevent_vtbl = EVENTLISTENER_VTBL(handle_htmlevent);
313 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
314 nsIDOMEventListener *listener, BOOL capture)
319 nsAString_Init(&type_str, type);
320 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture);
321 nsAString_Finish(&type_str);
323 ERR("AddEventTarget failed: %08x\n", nsres);
327 static void init_listener(nsEventListener *This, nsDocumentEventListener *listener,
328 const nsIDOMEventListenerVtbl *vtbl)
330 This->lpDOMEventListenerVtbl = vtbl;
331 This->This = listener;
334 void add_nsevent_listener(HTMLDocumentNode *doc, LPCWSTR type)
336 nsIDOMEventTarget *target;
339 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
340 if(NS_FAILED(nsres)) {
341 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
345 init_event(target, type, NSEVENTLIST(&doc->nsevent_listener->htmlevent_listener), TRUE);
346 nsIDOMEventTarget_Release(target);
349 void release_nsevents(HTMLDocumentNode *doc)
351 if(doc->nsevent_listener) {
352 doc->nsevent_listener->doc = NULL;
353 release_listener(doc->nsevent_listener);
354 doc->nsevent_listener = NULL;
358 void init_nsevents(HTMLDocumentNode *doc)
360 nsDocumentEventListener *listener;
361 nsIDOMEventTarget *target;
364 static const PRUnichar wsz_blur[] = {'b','l','u','r',0};
365 static const PRUnichar wsz_focus[] = {'f','o','c','u','s',0};
366 static const PRUnichar wsz_keypress[] = {'k','e','y','p','r','e','s','s',0};
367 static const PRUnichar wsz_load[] = {'l','o','a','d',0};
369 listener = heap_alloc(sizeof(nsDocumentEventListener));
376 init_listener(&listener->blur_listener, listener, &blur_vtbl);
377 init_listener(&listener->focus_listener, listener, &focus_vtbl);
378 init_listener(&listener->keypress_listener, listener, &keypress_vtbl);
379 init_listener(&listener->load_listener, listener, &load_vtbl);
380 init_listener(&listener->htmlevent_listener, listener, &htmlevent_vtbl);
382 doc->nsevent_listener = listener;
384 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
385 if(NS_FAILED(nsres)) {
386 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
390 init_event(target, wsz_blur, NSEVENTLIST(&listener->blur_listener), TRUE);
391 init_event(target, wsz_focus, NSEVENTLIST(&listener->focus_listener), TRUE);
392 init_event(target, wsz_keypress, NSEVENTLIST(&listener->keypress_listener), FALSE);
393 init_event(target, wsz_load, NSEVENTLIST(&listener->load_listener), TRUE);
395 nsIDOMEventTarget_Release(target);