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"
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
42 nsIDOMEventListener nsIDOMEventListener_iface;
43 nsDocumentEventListener *This;
46 struct nsDocumentEventListener {
47 nsEventListener blur_listener;
48 nsEventListener focus_listener;
49 nsEventListener keypress_listener;
50 nsEventListener load_listener;
51 nsEventListener htmlevent_listener;
55 HTMLDocumentNode *doc;
58 static LONG release_listener(nsDocumentEventListener *This)
60 LONG ref = InterlockedDecrement(&This->ref);
62 TRACE("(%p) ref=%d\n", This, ref);
70 static inline nsEventListener *impl_from_nsIDOMEventListener(nsIDOMEventListener *iface)
72 return CONTAINING_RECORD(iface, nsEventListener, nsIDOMEventListener_iface);
75 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
76 nsIIDRef riid, void **result)
78 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
82 if(IsEqualGUID(&IID_nsISupports, riid)) {
83 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
84 *result = &This->nsIDOMEventListener_iface;
85 }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
86 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
87 *result = &This->nsIDOMEventListener_iface;
91 nsIWebBrowserChrome_AddRef(&This->nsIDOMEventListener_iface);
95 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
96 return NS_NOINTERFACE;
99 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
101 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
102 LONG ref = InterlockedIncrement(&This->This->ref);
104 TRACE("(%p) ref=%d\n", This->This, ref);
109 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
111 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
113 return release_listener(This->This);
116 static BOOL is_doc_child_focus(NSContainer *nscontainer)
120 for(hwnd = GetFocus(); hwnd && hwnd != nscontainer->hwnd; hwnd = GetParent(hwnd));
125 static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
127 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
128 HTMLDocumentNode *doc = This->This->doc;
129 HTMLDocumentObj *doc_obj;
131 TRACE("(%p)\n", doc);
133 if(!doc || !doc->basedoc.doc_obj)
134 return NS_ERROR_FAILURE;
135 doc_obj = doc->basedoc.doc_obj;
137 if(doc_obj->focus && !is_doc_child_focus(doc_obj->nscontainer)) {
138 doc_obj->focus = FALSE;
139 notif_focus(doc_obj);
145 static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
147 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
148 HTMLDocumentNode *doc = This->This->doc;
149 HTMLDocumentObj *doc_obj;
151 TRACE("(%p)\n", doc);
154 return NS_ERROR_FAILURE;
155 doc_obj = doc->basedoc.doc_obj;
157 if(!doc_obj->focus) {
158 doc_obj->focus = TRUE;
159 notif_focus(doc_obj);
165 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
168 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
169 HTMLDocumentNode *doc = This->This->doc;
170 HTMLDocumentObj *doc_obj;
173 return NS_ERROR_FAILURE;
174 doc_obj = doc->basedoc.doc_obj;
176 TRACE("(%p)->(%p)\n", doc, event);
178 update_doc(&doc_obj->basedoc, UPDATE_UI);
179 if(doc_obj->usermode == EDITMODE)
180 handle_edit_event(&doc_obj->basedoc, event);
185 static void handle_docobj_load(HTMLDocumentObj *doc)
189 if(doc->nscontainer->editor_controller) {
190 nsIController_Release(doc->nscontainer->editor_controller);
191 doc->nscontainer->editor_controller = NULL;
194 if(doc->usermode == EDITMODE)
195 handle_edit_load(&doc->basedoc);
198 IOleCommandTarget *olecmd = NULL;
200 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
201 if(SUCCEEDED(hres)) {
202 if(doc->download_state) {
203 VARIANT state, progress;
205 V_VT(&progress) = VT_I4;
207 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS,
208 OLECMDEXECOPT_DONTPROMPTUSER, &progress, NULL);
210 V_VT(&state) = VT_I4;
212 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE,
213 OLECMDEXECOPT_DONTPROMPTUSER, &state, NULL);
216 IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
217 IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
218 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
220 IOleCommandTarget_Release(olecmd);
224 doc->download_state = 0;
227 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
229 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
230 HTMLDocumentNode *doc = This->This->doc;
231 nsIDOMHTMLElement *nsbody = NULL;
232 HTMLDocumentObj *doc_obj = NULL;
234 TRACE("(%p)\n", doc);
236 if(!doc || !doc->basedoc.window)
237 return NS_ERROR_FAILURE;
238 if(doc->basedoc.doc_obj && doc->basedoc.doc_obj->basedoc.doc_node == doc)
239 doc_obj = doc->basedoc.doc_obj;
241 connect_scripts(doc->basedoc.window);
244 handle_docobj_load(doc_obj);
246 set_ready_state(doc->basedoc.window, READYSTATE_COMPLETE);
249 if(doc_obj->view_sink)
250 IAdviseSink_OnViewChange(doc_obj->view_sink, DVASPECT_CONTENT, -1);
252 set_statustext(doc_obj, IDS_STATUS_DONE, NULL);
254 update_title(doc_obj);
259 return NS_ERROR_FAILURE;
262 nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
264 fire_event(doc, EVENTID_LOAD, TRUE, (nsIDOMNode*)nsbody, event);
265 nsIDOMHTMLElement_Release(nsbody);
271 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
273 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
274 HTMLDocumentNode *doc = This->This->doc;
275 const PRUnichar *type;
276 nsIDOMEventTarget *event_target;
284 nsAString_Init(&type_str, NULL);
285 nsIDOMEvent_GetType(event, &type_str);
286 nsAString_GetData(&type_str, &type);
287 eid = str_to_eid(type);
288 nsAString_Finish(&type_str);
290 nsres = nsIDOMEvent_GetTarget(event, &event_target);
291 if(NS_FAILED(nsres) || !event_target) {
292 ERR("GetEventTarget failed: %08x\n", nsres);
296 nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
297 nsIDOMEventTarget_Release(event_target);
298 if(NS_FAILED(nsres)) {
299 ERR("Could not get nsIDOMNode: %08x\n", nsres);
303 fire_event(doc, eid, TRUE, nsnode, event);
305 nsIDOMNode_Release(nsnode);
310 #define EVENTLISTENER_VTBL(handler) \
312 nsDOMEventListener_QueryInterface, \
313 nsDOMEventListener_AddRef, \
314 nsDOMEventListener_Release, \
318 static const nsIDOMEventListenerVtbl blur_vtbl = EVENTLISTENER_VTBL(handle_blur);
319 static const nsIDOMEventListenerVtbl focus_vtbl = EVENTLISTENER_VTBL(handle_focus);
320 static const nsIDOMEventListenerVtbl keypress_vtbl = EVENTLISTENER_VTBL(handle_keypress);
321 static const nsIDOMEventListenerVtbl load_vtbl = EVENTLISTENER_VTBL(handle_load);
322 static const nsIDOMEventListenerVtbl htmlevent_vtbl = EVENTLISTENER_VTBL(handle_htmlevent);
324 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
325 nsIDOMEventListener *listener, BOOL capture)
330 nsAString_InitDepend(&type_str, type);
331 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture);
332 nsAString_Finish(&type_str);
334 ERR("AddEventTarget failed: %08x\n", nsres);
338 static void init_listener(nsEventListener *This, nsDocumentEventListener *listener,
339 const nsIDOMEventListenerVtbl *vtbl)
341 This->nsIDOMEventListener_iface.lpVtbl = vtbl;
342 This->This = listener;
345 void add_nsevent_listener(HTMLDocumentNode *doc, nsIDOMNode *nsnode, LPCWSTR type)
347 nsIDOMEventTarget *target;
351 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMEventTarget, (void**)&target);
353 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
354 if(NS_FAILED(nsres)) {
355 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
359 init_event(target, type, &doc->nsevent_listener->htmlevent_listener.nsIDOMEventListener_iface,
361 nsIDOMEventTarget_Release(target);
364 void release_nsevents(HTMLDocumentNode *doc)
366 if(doc->nsevent_listener) {
367 doc->nsevent_listener->doc = NULL;
368 release_listener(doc->nsevent_listener);
369 doc->nsevent_listener = NULL;
373 void init_nsevents(HTMLDocumentNode *doc)
375 nsDocumentEventListener *listener;
376 nsIDOMEventTarget *target;
379 static const PRUnichar wsz_blur[] = {'b','l','u','r',0};
380 static const PRUnichar wsz_focus[] = {'f','o','c','u','s',0};
381 static const PRUnichar wsz_keypress[] = {'k','e','y','p','r','e','s','s',0};
382 static const PRUnichar wsz_load[] = {'l','o','a','d',0};
384 listener = heap_alloc(sizeof(nsDocumentEventListener));
391 init_listener(&listener->blur_listener, listener, &blur_vtbl);
392 init_listener(&listener->focus_listener, listener, &focus_vtbl);
393 init_listener(&listener->keypress_listener, listener, &keypress_vtbl);
394 init_listener(&listener->load_listener, listener, &load_vtbl);
395 init_listener(&listener->htmlevent_listener, listener, &htmlevent_vtbl);
397 doc->nsevent_listener = listener;
399 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
400 if(NS_FAILED(nsres)) {
401 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
405 init_event(target, wsz_blur, &listener->blur_listener.nsIDOMEventListener_iface, TRUE);
406 init_event(target, wsz_focus, &listener->focus_listener.nsIDOMEventListener_iface, TRUE);
407 init_event(target, wsz_keypress, &listener->keypress_listener.nsIDOMEventListener_iface, FALSE);
408 init_event(target, wsz_load, &listener->load_listener.nsIDOMEventListener_iface, TRUE);
410 nsIDOMEventTarget_Release(target);