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
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 #include "mshtml_private.h"
34 #include "htmlevent.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 #define NSEVENTLIST_THIS(iface) DEFINE_THIS(nsEventListener, DOMEventListener, iface)
40 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
41 nsIIDRef riid, nsQIResult result)
43 nsEventListener *This = NSEVENTLIST_THIS(iface);
47 if(IsEqualGUID(&IID_nsISupports, riid)) {
48 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
49 *result = NSEVENTLIST(This);
50 }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
51 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
52 *result = NSEVENTLIST(This);
56 nsIWebBrowserChrome_AddRef(NSEVENTLIST(This));
60 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
61 return NS_NOINTERFACE;
64 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
66 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
67 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
70 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
72 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
73 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
76 static BOOL is_doc_child_focus(NSContainer *This)
83 for(hwnd = GetFocus(); hwnd && hwnd != This->doc->hwnd; hwnd = GetParent(hwnd));
85 return hwnd == This->doc->hwnd;
88 static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
90 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
92 TRACE("(%p)\n", This);
94 if(!This->reset_focus && This->doc && This->doc->focus && !is_doc_child_focus(This)) {
95 This->doc->focus = FALSE;
96 notif_focus(This->doc);
102 static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
104 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
106 TRACE("(%p)\n", This);
108 if(!This->reset_focus && This->doc && !This->doc->focus) {
109 This->doc->focus = TRUE;
110 notif_focus(This->doc);
116 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
119 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
121 TRACE("(%p)->(%p)\n", This, event);
123 update_doc(This->doc, UPDATE_UI);
124 if(This->doc->usermode == EDITMODE)
125 handle_edit_event(This->doc, event);
130 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
132 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
133 nsIDOMHTMLElement *nsbody = NULL;
135 TRACE("(%p)\n", This);
140 update_nsdocument(This->doc);
141 connect_scripts(This->doc);
143 if(This->editor_controller) {
144 nsIController_Release(This->editor_controller);
145 This->editor_controller = NULL;
148 if(This->doc->usermode == EDITMODE)
149 handle_edit_load(This->doc);
151 if(!This->doc->nsdoc) {
153 return NS_ERROR_FAILURE;
156 nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nsbody);
158 fire_event(This->doc, EVENTID_LOAD, (nsIDOMNode*)nsbody, event);
159 nsIDOMHTMLElement_Release(nsbody);
165 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
167 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
168 const PRUnichar *type;
169 nsIDOMEventTarget *event_target;
175 nsAString_Init(&type_str, NULL);
176 nsIDOMEvent_GetType(event, &type_str);
177 nsAString_GetData(&type_str, &type);
178 eid = str_to_eid(type);
179 nsAString_Finish(&type_str);
181 nsres = nsIDOMEvent_GetTarget(event, &event_target);
182 if(NS_FAILED(nsres) || !event_target) {
183 ERR("GetEventTarget failed: %08x\n", nsres);
187 nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
188 nsIDOMEventTarget_Release(event_target);
189 if(NS_FAILED(nsres)) {
190 ERR("Could not get nsIDOMNode: %08x\n", nsres);
194 fire_event(This->doc, eid, nsnode, event);
196 nsIDOMNode_Release(nsnode);
201 #undef NSEVENTLIST_THIS
203 #define EVENTLISTENER_VTBL(handler) \
205 nsDOMEventListener_QueryInterface, \
206 nsDOMEventListener_AddRef, \
207 nsDOMEventListener_Release, \
211 static const nsIDOMEventListenerVtbl blur_vtbl = EVENTLISTENER_VTBL(handle_blur);
212 static const nsIDOMEventListenerVtbl focus_vtbl = EVENTLISTENER_VTBL(handle_focus);
213 static const nsIDOMEventListenerVtbl keypress_vtbl = EVENTLISTENER_VTBL(handle_keypress);
214 static const nsIDOMEventListenerVtbl load_vtbl = EVENTLISTENER_VTBL(handle_load);
215 static const nsIDOMEventListenerVtbl htmlevent_vtbl = EVENTLISTENER_VTBL(handle_htmlevent);
217 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
218 nsIDOMEventListener *listener, BOOL capture)
223 nsAString_Init(&type_str, type);
224 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture);
225 nsAString_Finish(&type_str);
227 ERR("AddEventTarget failed: %08x\n", nsres);
231 static void init_listener(nsEventListener *This, NSContainer *container,
232 const nsIDOMEventListenerVtbl *vtbl)
234 This->lpDOMEventListenerVtbl = vtbl;
235 This->This = container;
238 void add_nsevent_listener(NSContainer *container, LPCWSTR type)
240 nsIDOMWindow *dom_window;
241 nsIDOMEventTarget *target;
244 nsres = nsIWebBrowser_GetContentDOMWindow(container->webbrowser, &dom_window);
245 if(NS_FAILED(nsres)) {
246 ERR("GetContentDOMWindow failed: %08x\n", nsres);
250 nsres = nsIDOMWindow_QueryInterface(dom_window, &IID_nsIDOMEventTarget, (void**)&target);
251 nsIDOMWindow_Release(dom_window);
252 if(NS_FAILED(nsres)) {
253 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
257 init_event(target, type, NSEVENTLIST(&container->htmlevent_listener), TRUE);
258 nsIDOMEventTarget_Release(target);
261 void init_nsevents(NSContainer *This)
263 nsIDOMWindow *dom_window;
264 nsIDOMEventTarget *target;
267 static const PRUnichar wsz_blur[] = {'b','l','u','r',0};
268 static const PRUnichar wsz_focus[] = {'f','o','c','u','s',0};
269 static const PRUnichar wsz_keypress[] = {'k','e','y','p','r','e','s','s',0};
270 static const PRUnichar wsz_load[] = {'l','o','a','d',0};
272 init_listener(&This->blur_listener, This, &blur_vtbl);
273 init_listener(&This->focus_listener, This, &focus_vtbl);
274 init_listener(&This->keypress_listener, This, &keypress_vtbl);
275 init_listener(&This->load_listener, This, &load_vtbl);
276 init_listener(&This->htmlevent_listener, This, &htmlevent_vtbl);
278 nsres = nsIWebBrowser_GetContentDOMWindow(This->webbrowser, &dom_window);
279 if(NS_FAILED(nsres)) {
280 ERR("GetContentDOMWindow failed: %08x\n", nsres);
284 nsres = nsIDOMWindow_QueryInterface(dom_window, &IID_nsIDOMEventTarget, (void**)&target);
285 nsIDOMWindow_Release(dom_window);
286 if(NS_FAILED(nsres)) {
287 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
291 init_event(target, wsz_blur, NSEVENTLIST(&This->blur_listener), TRUE);
292 init_event(target, wsz_focus, NSEVENTLIST(&This->focus_listener), TRUE);
293 init_event(target, wsz_keypress, NSEVENTLIST(&This->keypress_listener), FALSE);
294 init_event(target, wsz_load, NSEVENTLIST(&This->load_listener), TRUE);
296 nsIDOMEventTarget_Release(target);