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"
34 #include "mshtml_private.h"
35 #include "htmlevent.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
41 nsIDOMEventListener nsIDOMEventListener_iface
;
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 static inline nsEventListener
*impl_from_nsIDOMEventListener(nsIDOMEventListener
*iface
)
71 return CONTAINING_RECORD(iface
, nsEventListener
, nsIDOMEventListener_iface
);
74 static nsresult NSAPI
nsDOMEventListener_QueryInterface(nsIDOMEventListener
*iface
,
75 nsIIDRef riid
, void **result
)
77 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
81 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
82 TRACE("(%p)->(IID_nsISupports, %p)\n", This
, result
);
83 *result
= &This
->nsIDOMEventListener_iface
;
84 }else if(IsEqualGUID(&IID_nsIDOMEventListener
, riid
)) {
85 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This
, result
);
86 *result
= &This
->nsIDOMEventListener_iface
;
90 nsIWebBrowserChrome_AddRef(&This
->nsIDOMEventListener_iface
);
94 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
95 return NS_NOINTERFACE
;
98 static nsrefcnt NSAPI
nsDOMEventListener_AddRef(nsIDOMEventListener
*iface
)
100 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
101 LONG ref
= InterlockedIncrement(&This
->This
->ref
);
103 TRACE("(%p) ref=%d\n", This
->This
, ref
);
108 static nsrefcnt NSAPI
nsDOMEventListener_Release(nsIDOMEventListener
*iface
)
110 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
112 return release_listener(This
->This
);
115 static BOOL
is_doc_child_focus(NSContainer
*nscontainer
)
119 for(hwnd
= GetFocus(); hwnd
&& hwnd
!= nscontainer
->hwnd
; hwnd
= GetParent(hwnd
));
124 static nsresult NSAPI
handle_blur(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
126 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
127 HTMLDocumentNode
*doc
= This
->This
->doc
;
128 HTMLDocumentObj
*doc_obj
;
130 TRACE("(%p)\n", doc
);
132 if(!doc
|| !doc
->basedoc
.doc_obj
)
133 return NS_ERROR_FAILURE
;
134 doc_obj
= doc
->basedoc
.doc_obj
;
136 if(doc_obj
->focus
&& !is_doc_child_focus(doc_obj
->nscontainer
)) {
137 doc_obj
->focus
= FALSE
;
138 notif_focus(doc_obj
);
144 static nsresult NSAPI
handle_focus(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
146 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
147 HTMLDocumentNode
*doc
= This
->This
->doc
;
148 HTMLDocumentObj
*doc_obj
;
150 TRACE("(%p)\n", doc
);
153 return NS_ERROR_FAILURE
;
154 doc_obj
= doc
->basedoc
.doc_obj
;
156 if(!doc_obj
->focus
) {
157 doc_obj
->focus
= TRUE
;
158 notif_focus(doc_obj
);
164 static nsresult NSAPI
handle_keypress(nsIDOMEventListener
*iface
,
167 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
168 HTMLDocumentNode
*doc
= This
->This
->doc
;
169 HTMLDocumentObj
*doc_obj
;
172 return NS_ERROR_FAILURE
;
173 doc_obj
= doc
->basedoc
.doc_obj
;
175 TRACE("(%p)->(%p)\n", doc
, event
);
177 update_doc(&doc_obj
->basedoc
, UPDATE_UI
);
178 if(doc_obj
->usermode
== EDITMODE
)
179 handle_edit_event(&doc_obj
->basedoc
, event
);
184 static void handle_docobj_load(HTMLDocumentObj
*doc
)
186 IOleCommandTarget
*olecmd
= NULL
;
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 hres
= IOleClientSite_QueryInterface(doc
->client
, &IID_IOleCommandTarget
, (void**)&olecmd
);
203 if(doc
->download_state
) {
207 V_VT(&progress
) = VT_I4
;
209 IOleCommandTarget_Exec(olecmd
, NULL
, OLECMDID_SETPROGRESSPOS
,
210 OLECMDEXECOPT_DONTPROMPTUSER
, &progress
, NULL
);
213 set_download_state(doc
, 0);
217 IOleCommandTarget_Exec(olecmd
, &CGID_ShellDocView
, 103, 0, NULL
, NULL
);
218 IOleCommandTarget_Exec(olecmd
, &CGID_MSHTML
, IDM_PARSECOMPLETE
, 0, NULL
, NULL
);
219 IOleCommandTarget_Exec(olecmd
, NULL
, OLECMDID_HTTPEQUIV_DONE
, 0, NULL
, NULL
);
221 IOleCommandTarget_Release(olecmd
);
225 static nsresult NSAPI
handle_load(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
227 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
228 HTMLDocumentNode
*doc
= This
->This
->doc
;
229 nsIDOMHTMLElement
*nsbody
= NULL
;
230 HTMLDocumentObj
*doc_obj
= NULL
;
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
->basedoc
.window
);
242 handle_docobj_load(doc_obj
);
244 set_ready_state(doc
->basedoc
.window
, READYSTATE_COMPLETE
);
247 if(doc_obj
->view_sink
)
248 IAdviseSink_OnViewChange(doc_obj
->view_sink
, DVASPECT_CONTENT
, -1);
250 set_statustext(doc_obj
, IDS_STATUS_DONE
, NULL
);
252 update_title(doc_obj
);
255 if(doc_obj
&& doc_obj
->usermode
!=EDITMODE
&& doc_obj
->doc_object_service
)
256 IDocObjectService_FireDocumentComplete(doc_obj
->doc_object_service
,
257 &doc
->basedoc
.window
->IHTMLWindow2_iface
, 0);
261 return NS_ERROR_FAILURE
;
264 nsIDOMHTMLDocument_GetBody(doc
->nsdoc
, &nsbody
);
266 fire_event(doc
, EVENTID_LOAD
, TRUE
, (nsIDOMNode
*)nsbody
, event
);
267 nsIDOMHTMLElement_Release(nsbody
);
273 static nsresult NSAPI
handle_htmlevent(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
275 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
276 HTMLDocumentNode
*doc
= This
->This
->doc
;
277 const PRUnichar
*type
;
278 nsIDOMEventTarget
*event_target
;
286 nsAString_Init(&type_str
, NULL
);
287 nsIDOMEvent_GetType(event
, &type_str
);
288 nsAString_GetData(&type_str
, &type
);
289 eid
= str_to_eid(type
);
290 nsAString_Finish(&type_str
);
292 nsres
= nsIDOMEvent_GetTarget(event
, &event_target
);
293 if(NS_FAILED(nsres
) || !event_target
) {
294 ERR("GetEventTarget failed: %08x\n", nsres
);
298 nsres
= nsIDOMEventTarget_QueryInterface(event_target
, &IID_nsIDOMNode
, (void**)&nsnode
);
299 nsIDOMEventTarget_Release(event_target
);
300 if(NS_FAILED(nsres
)) {
301 ERR("Could not get nsIDOMNode: %08x\n", nsres
);
305 fire_event(doc
, eid
, TRUE
, nsnode
, event
);
307 nsIDOMNode_Release(nsnode
);
312 #define EVENTLISTENER_VTBL(handler) \
314 nsDOMEventListener_QueryInterface, \
315 nsDOMEventListener_AddRef, \
316 nsDOMEventListener_Release, \
320 static const nsIDOMEventListenerVtbl blur_vtbl
= EVENTLISTENER_VTBL(handle_blur
);
321 static const nsIDOMEventListenerVtbl focus_vtbl
= EVENTLISTENER_VTBL(handle_focus
);
322 static const nsIDOMEventListenerVtbl keypress_vtbl
= EVENTLISTENER_VTBL(handle_keypress
);
323 static const nsIDOMEventListenerVtbl load_vtbl
= EVENTLISTENER_VTBL(handle_load
);
324 static const nsIDOMEventListenerVtbl htmlevent_vtbl
= EVENTLISTENER_VTBL(handle_htmlevent
);
326 static void init_event(nsIDOMEventTarget
*target
, const PRUnichar
*type
,
327 nsIDOMEventListener
*listener
, BOOL capture
)
332 nsAString_InitDepend(&type_str
, type
);
333 nsres
= nsIDOMEventTarget_AddEventListener(target
, &type_str
, listener
, capture
);
334 nsAString_Finish(&type_str
);
336 ERR("AddEventTarget failed: %08x\n", nsres
);
340 static void init_listener(nsEventListener
*This
, nsDocumentEventListener
*listener
,
341 const nsIDOMEventListenerVtbl
*vtbl
)
343 This
->nsIDOMEventListener_iface
.lpVtbl
= vtbl
;
344 This
->This
= listener
;
347 void add_nsevent_listener(HTMLDocumentNode
*doc
, nsIDOMNode
*nsnode
, LPCWSTR type
)
349 nsIDOMEventTarget
*target
;
353 nsres
= nsIDOMNode_QueryInterface(nsnode
, &IID_nsIDOMEventTarget
, (void**)&target
);
355 nsres
= nsIDOMWindow_QueryInterface(doc
->basedoc
.window
->nswindow
, &IID_nsIDOMEventTarget
, (void**)&target
);
356 if(NS_FAILED(nsres
)) {
357 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres
);
361 init_event(target
, type
, &doc
->nsevent_listener
->htmlevent_listener
.nsIDOMEventListener_iface
,
363 nsIDOMEventTarget_Release(target
);
366 void release_nsevents(HTMLDocumentNode
*doc
)
368 if(doc
->nsevent_listener
) {
369 doc
->nsevent_listener
->doc
= NULL
;
370 release_listener(doc
->nsevent_listener
);
371 doc
->nsevent_listener
= NULL
;
375 void init_nsevents(HTMLDocumentNode
*doc
)
377 nsDocumentEventListener
*listener
;
378 nsIDOMEventTarget
*target
;
381 static const PRUnichar wsz_blur
[] = {'b','l','u','r',0};
382 static const PRUnichar wsz_focus
[] = {'f','o','c','u','s',0};
383 static const PRUnichar wsz_keypress
[] = {'k','e','y','p','r','e','s','s',0};
384 static const PRUnichar wsz_load
[] = {'l','o','a','d',0};
386 listener
= heap_alloc(sizeof(nsDocumentEventListener
));
393 init_listener(&listener
->blur_listener
, listener
, &blur_vtbl
);
394 init_listener(&listener
->focus_listener
, listener
, &focus_vtbl
);
395 init_listener(&listener
->keypress_listener
, listener
, &keypress_vtbl
);
396 init_listener(&listener
->load_listener
, listener
, &load_vtbl
);
397 init_listener(&listener
->htmlevent_listener
, listener
, &htmlevent_vtbl
);
399 doc
->nsevent_listener
= listener
;
401 nsres
= nsIDOMWindow_QueryInterface(doc
->basedoc
.window
->nswindow
, &IID_nsIDOMEventTarget
, (void**)&target
);
402 if(NS_FAILED(nsres
)) {
403 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres
);
407 init_event(target
, wsz_blur
, &listener
->blur_listener
.nsIDOMEventListener_iface
, TRUE
);
408 init_event(target
, wsz_focus
, &listener
->focus_listener
.nsIDOMEventListener_iface
, TRUE
);
409 init_event(target
, wsz_keypress
, &listener
->keypress_listener
.nsIDOMEventListener_iface
, FALSE
);
410 init_event(target
, wsz_load
, &listener
->load_listener
.nsIDOMEventListener_iface
, TRUE
);
412 nsIDOMEventTarget_Release(target
);