msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / mshtml / nsevents.c
blob1933c5788cdba6adc4684134b8d2bbd32c4cad25
1 /*
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
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"
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);
45 *result = NULL;
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);
55 if(*result) {
56 nsIWebBrowserChrome_AddRef(NSEVENTLIST(This));
57 return NS_OK;
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)
78 HWND hwnd;
80 if(!This->doc)
81 return FALSE;
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);
99 return NS_OK;
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);
113 return NS_OK;
116 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
117 nsIDOMEvent *event)
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);
127 return NS_OK;
130 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
132 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
133 nsIDOMHTMLElement *nsbody = NULL;
134 task_t *task;
136 TRACE("(%p)\n", This);
138 if(!This->doc)
139 return NS_OK;
141 update_nsdocument(This->doc);
142 connect_scripts(This->doc);
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;
156 task->next = NULL;
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).
162 push_task(task);
164 if(!This->doc->nsdoc) {
165 ERR("NULL nsdoc\n");
166 return NS_ERROR_FAILURE;
169 nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nsbody);
170 if(nsbody) {
171 fire_event(This->doc, EVENTID_LOAD, (nsIDOMNode*)nsbody);
172 nsIDOMHTMLElement_Release(nsbody);
175 return NS_OK;
178 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
180 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
181 const PRUnichar *type;
182 nsIDOMEventTarget *event_target;
183 nsIDOMNode *nsnode;
184 nsAString type_str;
185 eventid_t eid;
186 nsresult nsres;
188 nsAString_Init(&type_str, NULL);
189 nsIDOMEvent_GetType(event, &type_str);
190 nsAString_GetData(&type_str, &type);
191 eid = str_to_eid(type);
192 nsAString_Finish(&type_str);
194 nsres = nsIDOMEvent_GetTarget(event, &event_target);
195 if(NS_FAILED(nsres) || !event_target) {
196 ERR("GetEventTarget failed: %08x\n", nsres);
197 return NS_OK;
200 nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
201 nsIDOMEventTarget_Release(event_target);
202 if(NS_FAILED(nsres)) {
203 ERR("Could not get nsIDOMNode: %08x\n", nsres);
204 return NS_OK;
207 fire_event(This->doc, eid, nsnode);
209 nsIDOMNode_Release(nsnode);
211 return NS_OK;
214 #undef NSEVENTLIST_THIS
216 #define EVENTLISTENER_VTBL(handler) \
218 nsDOMEventListener_QueryInterface, \
219 nsDOMEventListener_AddRef, \
220 nsDOMEventListener_Release, \
221 handler, \
224 static const nsIDOMEventListenerVtbl blur_vtbl = EVENTLISTENER_VTBL(handle_blur);
225 static const nsIDOMEventListenerVtbl focus_vtbl = EVENTLISTENER_VTBL(handle_focus);
226 static const nsIDOMEventListenerVtbl keypress_vtbl = EVENTLISTENER_VTBL(handle_keypress);
227 static const nsIDOMEventListenerVtbl load_vtbl = EVENTLISTENER_VTBL(handle_load);
228 static const nsIDOMEventListenerVtbl htmlevent_vtbl = EVENTLISTENER_VTBL(handle_htmlevent);
230 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
231 nsIDOMEventListener *listener, BOOL capture)
233 nsAString type_str;
234 nsresult nsres;
236 nsAString_Init(&type_str, type);
237 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture);
238 nsAString_Finish(&type_str);
239 if(NS_FAILED(nsres))
240 ERR("AddEventTarget failed: %08x\n", nsres);
244 static void init_listener(nsEventListener *This, NSContainer *container,
245 const nsIDOMEventListenerVtbl *vtbl)
247 This->lpDOMEventListenerVtbl = vtbl;
248 This->This = container;
251 void add_nsevent_listener(NSContainer *container, LPCWSTR type)
253 nsIDOMWindow *dom_window;
254 nsIDOMEventTarget *target;
255 nsresult nsres;
257 nsres = nsIWebBrowser_GetContentDOMWindow(container->webbrowser, &dom_window);
258 if(NS_FAILED(nsres)) {
259 ERR("GetContentDOMWindow failed: %08x\n", nsres);
260 return;
263 nsres = nsIDOMWindow_QueryInterface(dom_window, &IID_nsIDOMEventTarget, (void**)&target);
264 nsIDOMWindow_Release(dom_window);
265 if(NS_FAILED(nsres)) {
266 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
267 return;
270 init_event(target, type, NSEVENTLIST(&container->htmlevent_listener), TRUE);
271 nsIDOMEventTarget_Release(target);
274 void init_nsevents(NSContainer *This)
276 nsIDOMWindow *dom_window;
277 nsIDOMEventTarget *target;
278 nsresult nsres;
280 static const PRUnichar wsz_blur[] = {'b','l','u','r',0};
281 static const PRUnichar wsz_focus[] = {'f','o','c','u','s',0};
282 static const PRUnichar wsz_keypress[] = {'k','e','y','p','r','e','s','s',0};
283 static const PRUnichar wsz_load[] = {'l','o','a','d',0};
285 init_listener(&This->blur_listener, This, &blur_vtbl);
286 init_listener(&This->focus_listener, This, &focus_vtbl);
287 init_listener(&This->keypress_listener, This, &keypress_vtbl);
288 init_listener(&This->load_listener, This, &load_vtbl);
289 init_listener(&This->htmlevent_listener, This, &htmlevent_vtbl);
291 nsres = nsIWebBrowser_GetContentDOMWindow(This->webbrowser, &dom_window);
292 if(NS_FAILED(nsres)) {
293 ERR("GetContentDOMWindow failed: %08x\n", nsres);
294 return;
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);
301 return;
304 init_event(target, wsz_blur, NSEVENTLIST(&This->blur_listener), TRUE);
305 init_event(target, wsz_focus, NSEVENTLIST(&This->focus_listener), TRUE);
306 init_event(target, wsz_keypress, NSEVENTLIST(&This->keypress_listener), FALSE);
307 init_event(target, wsz_load, NSEVENTLIST(&This->load_listener), TRUE);
309 nsIDOMEventTarget_Release(target);