2 * Copyright 2007 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
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 #include "mshtml_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
37 #define NSEVENTLIST_THIS(iface) DEFINE_THIS(nsEventListener, DOMEventListener, iface)
39 static nsresult NSAPI
nsDOMEventListener_QueryInterface(nsIDOMEventListener
*iface
,
40 nsIIDRef riid
, nsQIResult result
)
42 nsEventListener
*This
= NSEVENTLIST_THIS(iface
);
46 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
47 TRACE("(%p)->(IID_nsISupports, %p)\n", This
, result
);
48 *result
= NSEVENTLIST(This
);
49 }else if(IsEqualGUID(&IID_nsIDOMEventListener
, riid
)) {
50 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This
, result
);
51 *result
= NSEVENTLIST(This
);
55 nsIWebBrowserChrome_AddRef(NSEVENTLIST(This
));
59 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
60 return NS_NOINTERFACE
;
63 static nsrefcnt NSAPI
nsDOMEventListener_AddRef(nsIDOMEventListener
*iface
)
65 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
66 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This
));
69 static nsrefcnt NSAPI
nsDOMEventListener_Release(nsIDOMEventListener
*iface
)
71 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
72 return nsIWebBrowserChrome_Release(NSWBCHROME(This
));
75 static BOOL
is_doc_child_focus(NSContainer
*This
)
82 for(hwnd
= GetFocus(); hwnd
&& hwnd
!= This
->doc
->hwnd
; hwnd
= GetParent(hwnd
));
84 return hwnd
== This
->doc
->hwnd
;
87 static nsresult NSAPI
handle_blur(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
89 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
91 TRACE("(%p)\n", This
);
93 if(!This
->reset_focus
&& This
->doc
&& This
->doc
->focus
&& !is_doc_child_focus(This
)) {
94 This
->doc
->focus
= FALSE
;
95 notif_focus(This
->doc
);
101 static nsresult NSAPI
handle_focus(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
103 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
105 TRACE("(%p)\n", This
);
107 if(!This
->reset_focus
&& This
->doc
&& !This
->doc
->focus
) {
108 This
->doc
->focus
= TRUE
;
109 notif_focus(This
->doc
);
115 static nsresult NSAPI
handle_keypress(nsIDOMEventListener
*iface
,
118 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
120 TRACE("(%p)->(%p)\n", This
, event
);
122 update_doc(This
->doc
, UPDATE_UI
);
123 if(This
->doc
->usermode
== EDITMODE
)
124 handle_edit_event(This
->doc
, event
);
129 static nsresult NSAPI
handle_load(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
131 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
132 nsIDOMHTMLElement
*nsbody
= NULL
;
135 TRACE("(%p)\n", This
);
140 update_nsdocument(This
->doc
);
141 connect_scripts(This
->doc
);
142 setup_nswindow(This
->doc
->window
);
144 if(This
->editor_controller
) {
145 nsIController_Release(This
->editor_controller
);
146 This
->editor_controller
= NULL
;
149 if(This
->doc
->usermode
== EDITMODE
)
150 handle_edit_load(This
->doc
);
152 task
= heap_alloc(sizeof(task_t
));
154 task
->doc
= This
->doc
;
155 task
->task_id
= TASK_PARSECOMPLETE
;
159 * This should be done in the worker thread that parses HTML,
160 * but we don't have such thread (Gecko parses HTML for us).
164 if(!This
->doc
->nsdoc
) {
166 return NS_ERROR_FAILURE
;
169 nsIDOMHTMLDocument_GetBody(This
->doc
->nsdoc
, &nsbody
);
171 fire_event(This
->doc
, EVENTID_LOAD
, (nsIDOMNode
*)nsbody
);
172 nsIDOMHTMLElement_Release(nsbody
);
178 static nsresult NSAPI
handle_node_insert(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
180 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
181 nsIDOMHTMLScriptElement
*script
;
182 nsIDOMEventTarget
*target
;
186 TRACE("(%p %p)\n", This
, event
);
188 nsres
= nsIDOMEvent_GetTarget(event
, &target
);
189 if(NS_FAILED(nsres
)) {
190 ERR("GetTarget failed: %08x\n", nsres
);
194 nsres
= nsIDOMEventTarget_QueryInterface(target
, &IID_nsIDOMElement
, (void**)&elem
);
195 nsIDOMEventTarget_Release(target
);
199 nsres
= nsIDOMElement_QueryInterface(elem
, &IID_nsIDOMHTMLScriptElement
, (void**)&script
);
200 if(SUCCEEDED(nsres
)) {
201 doc_insert_script(This
->doc
, script
);
202 nsIDOMHTMLScriptElement_Release(script
);
205 check_event_attr(This
->doc
, elem
);
207 nsIDOMNode_Release(elem
);
211 static nsresult NSAPI
handle_htmlevent(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
213 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
214 const PRUnichar
*type
;
215 nsIDOMEventTarget
*event_target
;
221 nsAString_Init(&type_str
, NULL
);
222 nsIDOMEvent_GetType(event
, &type_str
);
223 nsAString_GetData(&type_str
, &type
);
224 eid
= str_to_eid(type
);
225 nsAString_Finish(&type_str
);
227 nsres
= nsIDOMEvent_GetTarget(event
, &event_target
);
228 if(NS_FAILED(nsres
) || !event_target
) {
229 ERR("GetEventTarget failed: %08x\n", nsres
);
233 nsres
= nsIDOMEventTarget_QueryInterface(event_target
, &IID_nsIDOMNode
, (void**)&nsnode
);
234 nsIDOMEventTarget_Release(event_target
);
235 if(NS_FAILED(nsres
)) {
236 ERR("Could not get nsIDOMNode: %08x\n", nsres
);
240 fire_event(This
->doc
, eid
, nsnode
);
242 nsIDOMNode_Release(nsnode
);
247 #undef NSEVENTLIST_THIS
249 #define EVENTLISTENER_VTBL(handler) \
251 nsDOMEventListener_QueryInterface, \
252 nsDOMEventListener_AddRef, \
253 nsDOMEventListener_Release, \
257 static const nsIDOMEventListenerVtbl blur_vtbl
= EVENTLISTENER_VTBL(handle_blur
);
258 static const nsIDOMEventListenerVtbl focus_vtbl
= EVENTLISTENER_VTBL(handle_focus
);
259 static const nsIDOMEventListenerVtbl keypress_vtbl
= EVENTLISTENER_VTBL(handle_keypress
);
260 static const nsIDOMEventListenerVtbl load_vtbl
= EVENTLISTENER_VTBL(handle_load
);
261 static const nsIDOMEventListenerVtbl node_insert_vtbl
= EVENTLISTENER_VTBL(handle_node_insert
);
262 static const nsIDOMEventListenerVtbl htmlevent_vtbl
= EVENTLISTENER_VTBL(handle_htmlevent
);
264 static void init_event(nsIDOMEventTarget
*target
, const PRUnichar
*type
,
265 nsIDOMEventListener
*listener
, BOOL capture
)
270 nsAString_Init(&type_str
, type
);
271 nsres
= nsIDOMEventTarget_AddEventListener(target
, &type_str
, listener
, capture
);
272 nsAString_Finish(&type_str
);
274 ERR("AddEventTarget failed: %08x\n", nsres
);
278 static void init_listener(nsEventListener
*This
, NSContainer
*container
,
279 const nsIDOMEventListenerVtbl
*vtbl
)
281 This
->lpDOMEventListenerVtbl
= vtbl
;
282 This
->This
= container
;
285 void add_nsevent_listener(NSContainer
*container
, LPCWSTR type
)
287 nsIDOMWindow
*dom_window
;
288 nsIDOMEventTarget
*target
;
291 nsres
= nsIWebBrowser_GetContentDOMWindow(container
->webbrowser
, &dom_window
);
292 if(NS_FAILED(nsres
)) {
293 ERR("GetContentDOMWindow failed: %08x\n", nsres
);
297 nsres
= nsIDOMWindow_QueryInterface(dom_window
, &IID_nsIDOMEventTarget
, (void**)&target
);
298 nsIDOMWindow_Release(dom_window
);
299 if(NS_FAILED(nsres
)) {
300 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres
);
304 init_event(target
, type
, NSEVENTLIST(&container
->htmlevent_listener
), TRUE
);
305 nsIDOMEventTarget_Release(target
);
308 void init_nsevents(NSContainer
*This
)
310 nsIDOMWindow
*dom_window
;
311 nsIDOMEventTarget
*target
;
314 static const PRUnichar wsz_blur
[] = {'b','l','u','r',0};
315 static const PRUnichar wsz_focus
[] = {'f','o','c','u','s',0};
316 static const PRUnichar wsz_keypress
[] = {'k','e','y','p','r','e','s','s',0};
317 static const PRUnichar wsz_load
[] = {'l','o','a','d',0};
318 static const PRUnichar DOMNodeInsertedW
[] =
319 {'D','O','M','N','o','d','e','I','n','s','e','r','t','e','d',0};
321 init_listener(&This
->blur_listener
, This
, &blur_vtbl
);
322 init_listener(&This
->focus_listener
, This
, &focus_vtbl
);
323 init_listener(&This
->keypress_listener
, This
, &keypress_vtbl
);
324 init_listener(&This
->load_listener
, This
, &load_vtbl
);
325 init_listener(&This
->node_insert_listener
, This
, &node_insert_vtbl
);
326 init_listener(&This
->htmlevent_listener
, This
, &htmlevent_vtbl
);
328 nsres
= nsIWebBrowser_GetContentDOMWindow(This
->webbrowser
, &dom_window
);
329 if(NS_FAILED(nsres
)) {
330 ERR("GetContentDOMWindow failed: %08x\n", nsres
);
334 nsres
= nsIDOMWindow_QueryInterface(dom_window
, &IID_nsIDOMEventTarget
, (void**)&target
);
335 nsIDOMWindow_Release(dom_window
);
336 if(NS_FAILED(nsres
)) {
337 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres
);
341 init_event(target
, wsz_blur
, NSEVENTLIST(&This
->blur_listener
), TRUE
);
342 init_event(target
, wsz_focus
, NSEVENTLIST(&This
->focus_listener
), TRUE
);
343 init_event(target
, wsz_keypress
, NSEVENTLIST(&This
->keypress_listener
), FALSE
);
344 init_event(target
, wsz_load
, NSEVENTLIST(&This
->load_listener
), TRUE
);
345 init_event(target
, DOMNodeInsertedW
,NSEVENTLIST(&This
->node_insert_listener
),TRUE
);
347 nsIDOMEventTarget_Release(target
);