kernel32/tests: Don't crash on Vista.
[wine/hacks.git] / dlls / mshtml / nsevents.c
blob255716c11afb81ae36555e3336ad3251532bc426
1 /*
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
19 #include "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
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);
44 *result = NULL;
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);
54 if(*result) {
55 nsIWebBrowserChrome_AddRef(NSEVENTLIST(This));
56 return NS_OK;
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)
77 HWND hwnd;
79 if(!This->doc)
80 return FALSE;
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->doc && This->doc->focus && !is_doc_child_focus(This)) {
94 This->doc->focus = FALSE;
95 notif_focus(This->doc);
98 return NS_OK;
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->doc && !This->doc->focus) {
108 This->doc->focus = TRUE;
109 notif_focus(This->doc);
112 return NS_OK;
115 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
116 nsIDOMEvent *event)
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);
126 return NS_OK;
129 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
131 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
132 task_t *task;
134 TRACE("(%p)\n", This);
136 if(!This->doc)
137 return NS_OK;
139 if(This->editor_controller) {
140 nsIController_Release(This->editor_controller);
141 This->editor_controller = NULL;
144 if(This->doc->usermode == EDITMODE)
145 This->editor_controller = get_editor_controller(This);
147 task = mshtml_alloc(sizeof(task_t));
149 task->doc = This->doc;
150 task->task_id = TASK_PARSECOMPLETE;
151 task->next = NULL;
154 * This should be done in the worker thread that parses HTML,
155 * but we don't have such thread (Gecko parses HTML for us).
157 push_task(task);
159 return NS_OK;
162 #undef NSEVENTLIST_THIS
164 #define EVENTLISTENER_VTBL(handler) \
166 nsDOMEventListener_QueryInterface, \
167 nsDOMEventListener_AddRef, \
168 nsDOMEventListener_Release, \
169 handler, \
172 static const nsIDOMEventListenerVtbl blur_vtbl = EVENTLISTENER_VTBL(handle_blur);
173 static const nsIDOMEventListenerVtbl focus_vtbl = EVENTLISTENER_VTBL(handle_focus);
174 static const nsIDOMEventListenerVtbl keypress_vtbl = EVENTLISTENER_VTBL(handle_keypress);
175 static const nsIDOMEventListenerVtbl load_vtbl = EVENTLISTENER_VTBL(handle_load);
177 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
178 nsIDOMEventListener *listener, BOOL capture)
180 nsAString type_str;
181 nsresult nsres;
183 nsAString_Init(&type_str, type);
184 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture);
185 nsAString_Finish(&type_str);
186 if(NS_FAILED(nsres))
187 ERR("AddEventTarget failed: %08x\n", nsres);
191 static void init_listener(nsEventListener *This, NSContainer *container,
192 const nsIDOMEventListenerVtbl *vtbl)
194 This->lpDOMEventListenerVtbl = vtbl;
195 This->This = container;
198 void init_nsevents(NSContainer *This)
200 nsIDOMWindow *dom_window;
201 nsIDOMEventTarget *target;
202 nsresult nsres;
204 static const PRUnichar wsz_blur[] = {'b','l','u','r',0};
205 static const PRUnichar wsz_focus[] = {'f','o','c','u','s',0};
206 static const PRUnichar wsz_keypress[] = {'k','e','y','p','r','e','s','s',0};
207 static const PRUnichar wsz_load[] = {'l','o','a','d',0};
209 init_listener(&This->blur_listener, This, &blur_vtbl);
210 init_listener(&This->focus_listener, This, &focus_vtbl);
211 init_listener(&This->keypress_listener, This, &keypress_vtbl);
212 init_listener(&This->load_listener, This, &load_vtbl);
214 nsres = nsIWebBrowser_GetContentDOMWindow(This->webbrowser, &dom_window);
215 if(NS_FAILED(nsres)) {
216 ERR("GetContentDOMWindow failed: %08x\n", nsres);
217 return;
220 nsres = nsIDOMWindow_QueryInterface(dom_window, &IID_nsIDOMEventTarget, (void**)&target);
221 nsIDOMWindow_Release(dom_window);
222 if(NS_FAILED(nsres)) {
223 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
224 return;
227 init_event(target, wsz_blur, NSEVENTLIST(&This->blur_listener), TRUE);
228 init_event(target, wsz_focus, NSEVENTLIST(&This->focus_listener), TRUE);
229 init_event(target, wsz_keypress, NSEVENTLIST(&This->keypress_listener), FALSE);
230 init_event(target, wsz_load, NSEVENTLIST(&This->load_listener), TRUE);
232 nsIDOMEventTarget_Release(target);