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
);
257 if(doc_obj
&& doc_obj
->doc_object_service
)
258 IDocObjectService_FireDocumentComplete(doc_obj
->doc_object_service
,
259 &doc
->basedoc
.window
->IHTMLWindow2_iface
, 0);
263 return NS_ERROR_FAILURE
;
266 nsIDOMHTMLDocument_GetBody(doc
->nsdoc
, &nsbody
);
268 fire_event(doc
, EVENTID_LOAD
, TRUE
, (nsIDOMNode
*)nsbody
, event
);
269 nsIDOMHTMLElement_Release(nsbody
);
275 static nsresult NSAPI
handle_htmlevent(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
277 nsEventListener
*This
= impl_from_nsIDOMEventListener(iface
);
278 HTMLDocumentNode
*doc
= This
->This
->doc
;
279 const PRUnichar
*type
;
280 nsIDOMEventTarget
*event_target
;
288 nsAString_Init(&type_str
, NULL
);
289 nsIDOMEvent_GetType(event
, &type_str
);
290 nsAString_GetData(&type_str
, &type
);
291 eid
= str_to_eid(type
);
292 nsAString_Finish(&type_str
);
294 nsres
= nsIDOMEvent_GetTarget(event
, &event_target
);
295 if(NS_FAILED(nsres
) || !event_target
) {
296 ERR("GetEventTarget failed: %08x\n", nsres
);
300 nsres
= nsIDOMEventTarget_QueryInterface(event_target
, &IID_nsIDOMNode
, (void**)&nsnode
);
301 nsIDOMEventTarget_Release(event_target
);
302 if(NS_FAILED(nsres
)) {
303 ERR("Could not get nsIDOMNode: %08x\n", nsres
);
307 fire_event(doc
, eid
, TRUE
, nsnode
, event
);
309 nsIDOMNode_Release(nsnode
);
314 #define EVENTLISTENER_VTBL(handler) \
316 nsDOMEventListener_QueryInterface, \
317 nsDOMEventListener_AddRef, \
318 nsDOMEventListener_Release, \
322 static const nsIDOMEventListenerVtbl blur_vtbl
= EVENTLISTENER_VTBL(handle_blur
);
323 static const nsIDOMEventListenerVtbl focus_vtbl
= EVENTLISTENER_VTBL(handle_focus
);
324 static const nsIDOMEventListenerVtbl keypress_vtbl
= EVENTLISTENER_VTBL(handle_keypress
);
325 static const nsIDOMEventListenerVtbl load_vtbl
= EVENTLISTENER_VTBL(handle_load
);
326 static const nsIDOMEventListenerVtbl htmlevent_vtbl
= EVENTLISTENER_VTBL(handle_htmlevent
);
328 static void init_event(nsIDOMEventTarget
*target
, const PRUnichar
*type
,
329 nsIDOMEventListener
*listener
, BOOL capture
)
334 nsAString_InitDepend(&type_str
, type
);
335 nsres
= nsIDOMEventTarget_AddEventListener(target
, &type_str
, listener
, capture
);
336 nsAString_Finish(&type_str
);
338 ERR("AddEventTarget failed: %08x\n", nsres
);
342 static void init_listener(nsEventListener
*This
, nsDocumentEventListener
*listener
,
343 const nsIDOMEventListenerVtbl
*vtbl
)
345 This
->nsIDOMEventListener_iface
.lpVtbl
= vtbl
;
346 This
->This
= listener
;
349 void add_nsevent_listener(HTMLDocumentNode
*doc
, nsIDOMNode
*nsnode
, LPCWSTR type
)
351 nsIDOMEventTarget
*target
;
355 nsres
= nsIDOMNode_QueryInterface(nsnode
, &IID_nsIDOMEventTarget
, (void**)&target
);
357 nsres
= nsIDOMWindow_QueryInterface(doc
->basedoc
.window
->nswindow
, &IID_nsIDOMEventTarget
, (void**)&target
);
358 if(NS_FAILED(nsres
)) {
359 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres
);
363 init_event(target
, type
, &doc
->nsevent_listener
->htmlevent_listener
.nsIDOMEventListener_iface
,
365 nsIDOMEventTarget_Release(target
);
368 void release_nsevents(HTMLDocumentNode
*doc
)
370 if(doc
->nsevent_listener
) {
371 doc
->nsevent_listener
->doc
= NULL
;
372 release_listener(doc
->nsevent_listener
);
373 doc
->nsevent_listener
= NULL
;
377 void init_nsevents(HTMLDocumentNode
*doc
)
379 nsDocumentEventListener
*listener
;
380 nsIDOMEventTarget
*target
;
383 static const PRUnichar wsz_blur
[] = {'b','l','u','r',0};
384 static const PRUnichar wsz_focus
[] = {'f','o','c','u','s',0};
385 static const PRUnichar wsz_keypress
[] = {'k','e','y','p','r','e','s','s',0};
386 static const PRUnichar wsz_load
[] = {'l','o','a','d',0};
388 listener
= heap_alloc(sizeof(nsDocumentEventListener
));
395 init_listener(&listener
->blur_listener
, listener
, &blur_vtbl
);
396 init_listener(&listener
->focus_listener
, listener
, &focus_vtbl
);
397 init_listener(&listener
->keypress_listener
, listener
, &keypress_vtbl
);
398 init_listener(&listener
->load_listener
, listener
, &load_vtbl
);
399 init_listener(&listener
->htmlevent_listener
, listener
, &htmlevent_vtbl
);
401 doc
->nsevent_listener
= listener
;
403 nsres
= nsIDOMWindow_QueryInterface(doc
->basedoc
.window
->nswindow
, &IID_nsIDOMEventTarget
, (void**)&target
);
404 if(NS_FAILED(nsres
)) {
405 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres
);
409 init_event(target
, wsz_blur
, &listener
->blur_listener
.nsIDOMEventListener_iface
, TRUE
);
410 init_event(target
, wsz_focus
, &listener
->focus_listener
.nsIDOMEventListener_iface
, TRUE
);
411 init_event(target
, wsz_keypress
, &listener
->keypress_listener
.nsIDOMEventListener_iface
, FALSE
);
412 init_event(target
, wsz_load
, &listener
->load_listener
.nsIDOMEventListener_iface
, TRUE
);
414 nsIDOMEventTarget_Release(target
);