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
31 #include "wine/debug.h"
33 #include "mshtml_private.h"
34 #include "htmlscript.h"
35 #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 nsIDOMEventListener_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(GeckoBrowser
*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
;
171 if(!doc
|| !doc
->browser
)
172 return NS_ERROR_FAILURE
;
174 TRACE("(%p)->(%p)\n", doc
, event
);
176 update_doc(doc
->browser
->doc
, UPDATE_UI
);
177 if(doc
->browser
->usermode
== EDITMODE
)
178 handle_edit_event(doc
, event
);
183 static void handle_docobj_load(HTMLDocumentObj
*doc
)
185 IOleCommandTarget
*olecmd
= NULL
;
188 if(doc
->nscontainer
->editor_controller
) {
189 nsIController_Release(doc
->nscontainer
->editor_controller
);
190 doc
->nscontainer
->editor_controller
= NULL
;
193 if(doc
->nscontainer
->usermode
== EDITMODE
)
194 setup_editor_controller(doc
->nscontainer
);
197 hres
= IOleClientSite_QueryInterface(doc
->client
, &IID_IOleCommandTarget
, (void**)&olecmd
);
202 if(doc
->download_state
) {
206 V_VT(&progress
) = VT_I4
;
208 IOleCommandTarget_Exec(olecmd
, NULL
, OLECMDID_SETPROGRESSPOS
,
209 OLECMDEXECOPT_DONTPROMPTUSER
, &progress
, NULL
);
212 set_download_state(doc
, 0);
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 static nsresult NSAPI
handle_load(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
226 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
227 HTMLDocumentNode
*doc
= This
->This
->doc
;
228 HTMLDocumentObj
*doc_obj
= NULL
;
229 DOMEvent
*load_event
;
232 TRACE("(%p)\n", doc
);
234 if(!doc
|| !doc
->basedoc
.window
)
235 return NS_ERROR_FAILURE
;
236 if(doc
->basedoc
.doc_obj
&& doc
->basedoc
.doc_obj
->basedoc
.doc_node
== doc
)
237 doc_obj
= doc
->basedoc
.doc_obj
;
239 connect_scripts(doc
->window
);
241 htmldoc_addref(&doc
->basedoc
);
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
);
257 if(doc_obj
&& doc_obj
->nscontainer
->usermode
!= EDITMODE
&& doc_obj
->doc_object_service
258 && !(doc
->basedoc
.window
->load_flags
& BINDING_REFRESH
))
259 IDocObjectService_FireDocumentComplete(doc_obj
->doc_object_service
,
260 &doc
->basedoc
.window
->base
.IHTMLWindow2_iface
, 0);
263 hres
= create_document_event(doc
, EVENTID_LOAD
, &load_event
);
264 if(SUCCEEDED(hres
)) {
265 dispatch_event(&doc
->node
.event_target
, load_event
);
266 IDOMEvent_Release(&load_event
->IDOMEvent_iface
);
273 hres
= create_event_from_nsevent(event
, dispex_compat_mode(&doc
->node
.event_target
.dispex
), &load_event
);
274 if(SUCCEEDED(hres
)) {
275 dispatch_event(&doc
->window
->event_target
, load_event
);
276 IDOMEvent_Release(&load_event
->IDOMEvent_iface
);
282 htmldoc_release(&doc
->basedoc
);
286 static nsresult NSAPI
handle_htmlevent(nsIDOMEventListener
*iface
, nsIDOMEvent
*nsevent
)
288 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
289 HTMLDocumentNode
*doc
= This
->This
->doc
;
290 nsIDOMEventTarget
*event_target
;
297 TRACE("%p\n", This
->This
);
304 nsres
= nsIDOMEvent_GetTarget(nsevent
, &event_target
);
305 if(NS_FAILED(nsres
) || !event_target
) {
306 ERR("GetEventTarget failed: %08x\n", nsres
);
310 nsres
= nsIDOMEventTarget_QueryInterface(event_target
, &IID_nsIDOMNode
, (void**)&nsnode
);
311 nsIDOMEventTarget_Release(event_target
);
312 if(NS_FAILED(nsres
)) {
313 ERR("Could not get nsIDOMNode: %08x\n", nsres
);
317 hres
= get_node(nsnode
, TRUE
, &node
);
318 nsIDOMNode_Release(nsnode
);
322 hres
= create_event_from_nsevent(nsevent
, dispex_compat_mode(&doc
->node
.event_target
.dispex
), &event
);
328 /* If we fine need for more special cases here, we may consider handling it in a more generic way. */
329 if(event
->event_id
== EVENTID_FOCUS
|| event
->event_id
== EVENTID_BLUR
) {
330 DOMEvent
*focus_event
;
332 hres
= create_document_event(doc
, event
->event_id
== EVENTID_FOCUS
? EVENTID_FOCUSIN
: EVENTID_FOCUSOUT
, &focus_event
);
333 if(SUCCEEDED(hres
)) {
334 dispatch_event(&node
->event_target
, focus_event
);
335 IDOMEvent_Release(&focus_event
->IDOMEvent_iface
);
339 dispatch_event(&node
->event_target
, event
);
341 IDOMEvent_Release(&event
->IDOMEvent_iface
);
346 #define EVENTLISTENER_VTBL(handler) \
348 nsDOMEventListener_QueryInterface, \
349 nsDOMEventListener_AddRef, \
350 nsDOMEventListener_Release, \
354 static const nsIDOMEventListenerVtbl blur_vtbl
= EVENTLISTENER_VTBL(handle_blur
);
355 static const nsIDOMEventListenerVtbl focus_vtbl
= EVENTLISTENER_VTBL(handle_focus
);
356 static const nsIDOMEventListenerVtbl keypress_vtbl
= EVENTLISTENER_VTBL(handle_keypress
);
357 static const nsIDOMEventListenerVtbl load_vtbl
= EVENTLISTENER_VTBL(handle_load
);
358 static const nsIDOMEventListenerVtbl htmlevent_vtbl
= EVENTLISTENER_VTBL(handle_htmlevent
);
360 static void init_event(nsIDOMEventTarget
*target
, const PRUnichar
*type
,
361 nsIDOMEventListener
*listener
, BOOL capture
)
366 nsAString_InitDepend(&type_str
, type
);
367 nsres
= nsIDOMEventTarget_AddEventListener(target
, &type_str
, listener
, capture
, FALSE
, 1);
368 nsAString_Finish(&type_str
);
370 ERR("AddEventTarget failed: %08x\n", nsres
);
374 static void init_listener(nsEventListener
*This
, nsDocumentEventListener
*listener
,
375 const nsIDOMEventListenerVtbl
*vtbl
)
377 This
->nsIDOMEventListener_iface
.lpVtbl
= vtbl
;
378 This
->This
= listener
;
381 static nsIDOMEventTarget
*get_default_document_target(HTMLDocumentNode
*doc
)
383 nsIDOMEventTarget
*target
;
384 nsISupports
*target_iface
;
387 target_iface
= doc
->window
? (nsISupports
*)doc
->basedoc
.window
->nswindow
: (nsISupports
*)doc
->nsdoc
;
388 nsres
= nsISupports_QueryInterface(target_iface
, &IID_nsIDOMEventTarget
, (void**)&target
);
389 return NS_SUCCEEDED(nsres
) ? target
: NULL
;
392 void add_nsevent_listener(HTMLDocumentNode
*doc
, nsIDOMNode
*nsnode
, LPCWSTR type
)
394 nsIDOMEventTarget
*target
;
398 nsres
= nsIDOMNode_QueryInterface(nsnode
, &IID_nsIDOMEventTarget
, (void**)&target
);
399 assert(nsres
== NS_OK
);
401 target
= get_default_document_target(doc
);
406 init_event(target
, type
, &doc
->nsevent_listener
->htmlevent_listener
.nsIDOMEventListener_iface
,
408 nsIDOMEventTarget_Release(target
);
411 static void detach_nslistener(HTMLDocumentNode
*doc
, const WCHAR
*type
, nsEventListener
*listener
, cpp_bool is_capture
)
413 nsIDOMEventTarget
*target
;
417 target
= get_default_document_target(doc
);
421 nsAString_InitDepend(&type_str
, type
);
422 nsres
= nsIDOMEventTarget_RemoveEventListener(target
, &type_str
,
423 &listener
->nsIDOMEventListener_iface
, is_capture
);
424 nsAString_Finish(&type_str
);
425 nsIDOMEventTarget_Release(target
);
427 ERR("RemoveEventTarget failed: %08x\n", nsres
);
430 void detach_nsevent(HTMLDocumentNode
*doc
, const WCHAR
*type
)
432 detach_nslistener(doc
, type
, &doc
->nsevent_listener
->htmlevent_listener
, TRUE
);
435 void release_nsevents(HTMLDocumentNode
*doc
)
437 nsDocumentEventListener
*listener
= doc
->nsevent_listener
;
439 TRACE("%p %p\n", doc
, doc
->nsevent_listener
);
444 detach_nslistener(doc
, L
"blur", &listener
->blur_listener
, TRUE
);
445 detach_nslistener(doc
, L
"focus", &listener
->focus_listener
, TRUE
);
446 detach_nslistener(doc
, L
"keypress", &listener
->keypress_listener
, FALSE
);
447 detach_nslistener(doc
, L
"load", &listener
->load_listener
, TRUE
);
449 listener
->doc
= NULL
;
450 release_listener(listener
);
451 doc
->nsevent_listener
= NULL
;
454 void init_nsevents(HTMLDocumentNode
*doc
)
456 nsDocumentEventListener
*listener
;
457 nsIDOMEventTarget
*target
;
459 listener
= heap_alloc(sizeof(nsDocumentEventListener
));
463 TRACE("%p %p\n", doc
, listener
);
468 init_listener(&listener
->blur_listener
, listener
, &blur_vtbl
);
469 init_listener(&listener
->focus_listener
, listener
, &focus_vtbl
);
470 init_listener(&listener
->keypress_listener
, listener
, &keypress_vtbl
);
471 init_listener(&listener
->load_listener
, listener
, &load_vtbl
);
472 init_listener(&listener
->htmlevent_listener
, listener
, &htmlevent_vtbl
);
474 doc
->nsevent_listener
= listener
;
476 target
= get_default_document_target(doc
);
480 init_event(target
, L
"blur", &listener
->blur_listener
.nsIDOMEventListener_iface
, TRUE
);
481 init_event(target
, L
"focus", &listener
->focus_listener
.nsIDOMEventListener_iface
, TRUE
);
482 init_event(target
, L
"keypress", &listener
->keypress_listener
.nsIDOMEventListener_iface
, FALSE
);
483 init_event(target
, L
"load", &listener
->load_listener
.nsIDOMEventListener_iface
, TRUE
);
485 nsIDOMEventTarget_Release(target
);