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 const nsIDOMEventListenerVtbl
*lpDOMEventListenerVtbl
;
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 #define NSEVENTLIST_THIS(iface) DEFINE_THIS(nsEventListener, DOMEventListener, iface)
72 static nsresult NSAPI
nsDOMEventListener_QueryInterface(nsIDOMEventListener
*iface
,
73 nsIIDRef riid
, void **result
)
75 nsEventListener
*This
= NSEVENTLIST_THIS(iface
);
79 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
80 TRACE("(%p)->(IID_nsISupports, %p)\n", This
, result
);
81 *result
= NSEVENTLIST(This
);
82 }else if(IsEqualGUID(&IID_nsIDOMEventListener
, riid
)) {
83 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This
, result
);
84 *result
= NSEVENTLIST(This
);
88 nsIWebBrowserChrome_AddRef(NSEVENTLIST(This
));
92 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
93 return NS_NOINTERFACE
;
96 static nsrefcnt NSAPI
nsDOMEventListener_AddRef(nsIDOMEventListener
*iface
)
98 nsDocumentEventListener
*This
= NSEVENTLIST_THIS(iface
)->This
;
99 LONG ref
= InterlockedIncrement(&This
->ref
);
101 TRACE("(%p) ref=%d\n", This
, ref
);
106 static nsrefcnt NSAPI
nsDOMEventListener_Release(nsIDOMEventListener
*iface
)
108 nsDocumentEventListener
*This
= NSEVENTLIST_THIS(iface
)->This
;
110 return release_listener(This
);
113 static BOOL
is_doc_child_focus(NSContainer
*nscontainer
)
117 for(hwnd
= GetFocus(); hwnd
&& hwnd
!= nscontainer
->hwnd
; hwnd
= GetParent(hwnd
));
122 static nsresult NSAPI
handle_blur(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
124 HTMLDocumentNode
*doc
= NSEVENTLIST_THIS(iface
)->This
->doc
;
125 HTMLDocumentObj
*doc_obj
;
127 TRACE("(%p)\n", doc
);
129 if(!doc
|| !doc
->basedoc
.doc_obj
)
130 return NS_ERROR_FAILURE
;
131 doc_obj
= doc
->basedoc
.doc_obj
;
133 if(doc_obj
->focus
&& !is_doc_child_focus(doc_obj
->nscontainer
)) {
134 doc_obj
->focus
= FALSE
;
135 notif_focus(doc_obj
);
141 static nsresult NSAPI
handle_focus(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
143 HTMLDocumentNode
*doc
= NSEVENTLIST_THIS(iface
)->This
->doc
;
144 HTMLDocumentObj
*doc_obj
;
146 TRACE("(%p)\n", doc
);
149 return NS_ERROR_FAILURE
;
150 doc_obj
= doc
->basedoc
.doc_obj
;
152 if(!doc_obj
->focus
) {
153 doc_obj
->focus
= TRUE
;
154 notif_focus(doc_obj
);
160 static nsresult NSAPI
handle_keypress(nsIDOMEventListener
*iface
,
163 HTMLDocumentNode
*doc
= NSEVENTLIST_THIS(iface
)->This
->doc
;
164 HTMLDocumentObj
*doc_obj
;
167 return NS_ERROR_FAILURE
;
168 doc_obj
= doc
->basedoc
.doc_obj
;
170 TRACE("(%p)->(%p)\n", doc
, event
);
172 update_doc(&doc_obj
->basedoc
, UPDATE_UI
);
173 if(doc_obj
->usermode
== EDITMODE
)
174 handle_edit_event(&doc_obj
->basedoc
, event
);
179 static void handle_docobj_load(HTMLDocumentObj
*doc
)
181 IOleCommandTarget
*olecmd
= NULL
;
187 hres
= IOleClientSite_QueryInterface(doc
->client
, &IID_IOleCommandTarget
, (void**)&olecmd
);
188 if(SUCCEEDED(hres
)) {
189 if(doc
->download_state
) {
190 VARIANT state
, progress
;
192 V_VT(&progress
) = VT_I4
;
194 IOleCommandTarget_Exec(olecmd
, NULL
, OLECMDID_SETPROGRESSPOS
,
195 OLECMDEXECOPT_DONTPROMPTUSER
, &progress
, NULL
);
197 V_VT(&state
) = VT_I4
;
199 IOleCommandTarget_Exec(olecmd
, NULL
, OLECMDID_SETDOWNLOADSTATE
,
200 OLECMDEXECOPT_DONTPROMPTUSER
, &state
, NULL
);
203 IOleCommandTarget_Exec(olecmd
, &CGID_ShellDocView
, 103, 0, NULL
, NULL
);
204 IOleCommandTarget_Exec(olecmd
, &CGID_MSHTML
, IDM_PARSECOMPLETE
, 0, NULL
, NULL
);
205 IOleCommandTarget_Exec(olecmd
, NULL
, OLECMDID_HTTPEQUIV_DONE
, 0, NULL
, NULL
);
207 IOleCommandTarget_Release(olecmd
);
209 doc
->download_state
= 0;
212 static nsresult NSAPI
handle_load(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
214 HTMLDocumentNode
*doc
= NSEVENTLIST_THIS(iface
)->This
->doc
;
215 HTMLDocumentObj
*doc_obj
;
216 nsIDOMHTMLElement
*nsbody
= NULL
;
218 TRACE("(%p)\n", doc
);
220 if(!doc
|| !doc
->basedoc
.window
)
221 return NS_ERROR_FAILURE
;
222 doc_obj
= doc
->basedoc
.doc_obj
;
224 connect_scripts(doc
->basedoc
.window
);
226 if(doc_obj
->nscontainer
->editor_controller
) {
227 nsIController_Release(doc_obj
->nscontainer
->editor_controller
);
228 doc_obj
->nscontainer
->editor_controller
= NULL
;
231 if(doc_obj
->usermode
== EDITMODE
)
232 handle_edit_load(&doc_obj
->basedoc
);
234 if(doc
== doc_obj
->basedoc
.doc_node
)
235 handle_docobj_load(doc_obj
);
237 set_ready_state(doc
->basedoc
.window
, READYSTATE_COMPLETE
);
239 if(doc
== doc_obj
->basedoc
.doc_node
) {
240 if(doc_obj
->view_sink
)
241 IAdviseSink_OnViewChange(doc_obj
->view_sink
, DVASPECT_CONTENT
, -1);
243 set_statustext(doc_obj
, IDS_STATUS_DONE
, NULL
);
245 update_title(doc_obj
);
250 return NS_ERROR_FAILURE
;
253 nsIDOMHTMLDocument_GetBody(doc
->nsdoc
, &nsbody
);
255 fire_event(doc
, EVENTID_LOAD
, TRUE
, (nsIDOMNode
*)nsbody
, event
);
256 nsIDOMHTMLElement_Release(nsbody
);
262 static nsresult NSAPI
handle_htmlevent(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
264 HTMLDocumentNode
*doc
= NSEVENTLIST_THIS(iface
)->This
->doc
;
265 const PRUnichar
*type
;
266 nsIDOMEventTarget
*event_target
;
274 nsAString_Init(&type_str
, NULL
);
275 nsIDOMEvent_GetType(event
, &type_str
);
276 nsAString_GetData(&type_str
, &type
);
277 eid
= str_to_eid(type
);
278 nsAString_Finish(&type_str
);
280 nsres
= nsIDOMEvent_GetTarget(event
, &event_target
);
281 if(NS_FAILED(nsres
) || !event_target
) {
282 ERR("GetEventTarget failed: %08x\n", nsres
);
286 nsres
= nsIDOMEventTarget_QueryInterface(event_target
, &IID_nsIDOMNode
, (void**)&nsnode
);
287 nsIDOMEventTarget_Release(event_target
);
288 if(NS_FAILED(nsres
)) {
289 ERR("Could not get nsIDOMNode: %08x\n", nsres
);
293 fire_event(doc
, eid
, TRUE
, nsnode
, event
);
295 nsIDOMNode_Release(nsnode
);
300 #undef NSEVENTLIST_THIS
302 #define EVENTLISTENER_VTBL(handler) \
304 nsDOMEventListener_QueryInterface, \
305 nsDOMEventListener_AddRef, \
306 nsDOMEventListener_Release, \
310 static const nsIDOMEventListenerVtbl blur_vtbl
= EVENTLISTENER_VTBL(handle_blur
);
311 static const nsIDOMEventListenerVtbl focus_vtbl
= EVENTLISTENER_VTBL(handle_focus
);
312 static const nsIDOMEventListenerVtbl keypress_vtbl
= EVENTLISTENER_VTBL(handle_keypress
);
313 static const nsIDOMEventListenerVtbl load_vtbl
= EVENTLISTENER_VTBL(handle_load
);
314 static const nsIDOMEventListenerVtbl htmlevent_vtbl
= EVENTLISTENER_VTBL(handle_htmlevent
);
316 static void init_event(nsIDOMEventTarget
*target
, const PRUnichar
*type
,
317 nsIDOMEventListener
*listener
, BOOL capture
)
322 nsAString_InitDepend(&type_str
, type
);
323 nsres
= nsIDOMEventTarget_AddEventListener(target
, &type_str
, listener
, capture
);
324 nsAString_Finish(&type_str
);
326 ERR("AddEventTarget failed: %08x\n", nsres
);
330 static void init_listener(nsEventListener
*This
, nsDocumentEventListener
*listener
,
331 const nsIDOMEventListenerVtbl
*vtbl
)
333 This
->lpDOMEventListenerVtbl
= vtbl
;
334 This
->This
= listener
;
337 void add_nsevent_listener(HTMLDocumentNode
*doc
, nsIDOMNode
*nsnode
, LPCWSTR type
)
339 nsIDOMEventTarget
*target
;
343 nsres
= nsIDOMNode_QueryInterface(nsnode
, &IID_nsIDOMEventTarget
, (void**)&target
);
345 nsres
= nsIDOMWindow_QueryInterface(doc
->basedoc
.window
->nswindow
, &IID_nsIDOMEventTarget
, (void**)&target
);
346 if(NS_FAILED(nsres
)) {
347 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres
);
351 init_event(target
, type
, NSEVENTLIST(&doc
->nsevent_listener
->htmlevent_listener
), TRUE
);
352 nsIDOMEventTarget_Release(target
);
355 void release_nsevents(HTMLDocumentNode
*doc
)
357 if(doc
->nsevent_listener
) {
358 doc
->nsevent_listener
->doc
= NULL
;
359 release_listener(doc
->nsevent_listener
);
360 doc
->nsevent_listener
= NULL
;
364 void init_nsevents(HTMLDocumentNode
*doc
)
366 nsDocumentEventListener
*listener
;
367 nsIDOMEventTarget
*target
;
370 static const PRUnichar wsz_blur
[] = {'b','l','u','r',0};
371 static const PRUnichar wsz_focus
[] = {'f','o','c','u','s',0};
372 static const PRUnichar wsz_keypress
[] = {'k','e','y','p','r','e','s','s',0};
373 static const PRUnichar wsz_load
[] = {'l','o','a','d',0};
375 listener
= heap_alloc(sizeof(nsDocumentEventListener
));
382 init_listener(&listener
->blur_listener
, listener
, &blur_vtbl
);
383 init_listener(&listener
->focus_listener
, listener
, &focus_vtbl
);
384 init_listener(&listener
->keypress_listener
, listener
, &keypress_vtbl
);
385 init_listener(&listener
->load_listener
, listener
, &load_vtbl
);
386 init_listener(&listener
->htmlevent_listener
, listener
, &htmlevent_vtbl
);
388 doc
->nsevent_listener
= listener
;
390 nsres
= nsIDOMWindow_QueryInterface(doc
->basedoc
.window
->nswindow
, &IID_nsIDOMEventTarget
, (void**)&target
);
391 if(NS_FAILED(nsres
)) {
392 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres
);
396 init_event(target
, wsz_blur
, NSEVENTLIST(&listener
->blur_listener
), TRUE
);
397 init_event(target
, wsz_focus
, NSEVENTLIST(&listener
->focus_listener
), TRUE
);
398 init_event(target
, wsz_keypress
, NSEVENTLIST(&listener
->keypress_listener
), FALSE
);
399 init_event(target
, wsz_load
, NSEVENTLIST(&listener
->load_listener
), TRUE
);
401 nsIDOMEventTarget_Release(target
);