mapi32: Use 0 instead of (wrongly) casting NULL to an integer.
[wine] / dlls / mshtml / nsevents.c
1 /*
2  * Copyright 2007-2008 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
29
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
32
33 #include "mshtml_private.h"
34 #include "htmlevent.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 #define NSEVENTLIST_THIS(iface) DEFINE_THIS(nsEventListener, DOMEventListener, iface)
39
40 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
41                                                         nsIIDRef riid, nsQIResult result)
42 {
43     nsEventListener *This = NSEVENTLIST_THIS(iface);
44
45     *result = NULL;
46
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);
53     }
54
55     if(*result) {
56         nsIWebBrowserChrome_AddRef(NSEVENTLIST(This));
57         return NS_OK;
58     }
59
60     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
61     return NS_NOINTERFACE;
62 }
63
64 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
65 {
66     NSContainer *This = NSEVENTLIST_THIS(iface)->This;
67     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
68 }
69
70 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
71 {
72     NSContainer *This = NSEVENTLIST_THIS(iface)->This;
73     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
74 }
75
76 static BOOL is_doc_child_focus(NSContainer *This)
77 {
78     HWND hwnd;
79
80     if(!This->doc)
81         return FALSE;
82
83     for(hwnd = GetFocus(); hwnd && hwnd != This->doc->basedoc.doc_obj->hwnd; hwnd = GetParent(hwnd));
84
85     return hwnd != NULL;
86 }
87
88 static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
89 {
90     NSContainer *This = NSEVENTLIST_THIS(iface)->This;
91
92     TRACE("(%p)\n", This);
93
94     if(!This->reset_focus && This->doc && This->doc->basedoc.doc_obj->focus && !is_doc_child_focus(This)) {
95         This->doc->basedoc.doc_obj->focus = FALSE;
96         notif_focus(This->doc);
97     }
98
99     return NS_OK;
100 }
101
102 static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
103 {
104     NSContainer *This = NSEVENTLIST_THIS(iface)->This;
105
106     TRACE("(%p)\n", This);
107
108     if(!This->reset_focus && This->doc && !This->doc->focus) {
109         This->doc->focus = TRUE;
110         notif_focus(This->doc);
111     }
112
113     return NS_OK;
114 }
115
116 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
117         nsIDOMEvent *event)
118 {
119     NSContainer *This = NSEVENTLIST_THIS(iface)->This;
120
121     TRACE("(%p)->(%p)\n", This, event);
122
123     update_doc(&This->doc->basedoc, UPDATE_UI);
124     if(This->doc->usermode == EDITMODE)
125         handle_edit_event(&This->doc->basedoc, event);
126
127     return NS_OK;
128 }
129
130 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
131 {
132     NSContainer *This = NSEVENTLIST_THIS(iface)->This;
133     nsIDOMHTMLElement *nsbody = NULL;
134
135     TRACE("(%p)\n", This);
136
137     if(!This->doc)
138         return NS_OK;
139
140     update_nsdocument(This->doc);
141     connect_scripts(This->doc->basedoc.window);
142
143     if(This->editor_controller) {
144         nsIController_Release(This->editor_controller);
145         This->editor_controller = NULL;
146     }
147
148     if(This->doc->usermode == EDITMODE)
149         handle_edit_load(&This->doc->basedoc);
150
151     if(!This->doc->basedoc.nsdoc) {
152         ERR("NULL nsdoc\n");
153         return NS_ERROR_FAILURE;
154     }
155
156     nsIDOMHTMLDocument_GetBody(This->doc->basedoc.nsdoc, &nsbody);
157     if(nsbody) {
158         fire_event(This->doc->basedoc.doc_node, EVENTID_LOAD, (nsIDOMNode*)nsbody, event);
159         nsIDOMHTMLElement_Release(nsbody);
160     }
161
162     return NS_OK;
163 }
164
165 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
166 {
167     NSContainer *This = NSEVENTLIST_THIS(iface)->This;
168     const PRUnichar *type;
169     nsIDOMEventTarget *event_target;
170     nsIDOMNode *nsnode;
171     nsAString type_str;
172     eventid_t eid;
173     nsresult nsres;
174
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);
180
181     nsres = nsIDOMEvent_GetTarget(event, &event_target);
182     if(NS_FAILED(nsres) || !event_target) {
183         ERR("GetEventTarget failed: %08x\n", nsres);
184         return NS_OK;
185     }
186
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);
191         return NS_OK;
192     }
193
194     fire_event(This->doc->basedoc.doc_node, eid, nsnode, event);
195
196     nsIDOMNode_Release(nsnode);
197
198     return NS_OK;
199 }
200
201 #undef NSEVENTLIST_THIS
202
203 #define EVENTLISTENER_VTBL(handler) \
204     { \
205         nsDOMEventListener_QueryInterface, \
206         nsDOMEventListener_AddRef, \
207         nsDOMEventListener_Release, \
208         handler, \
209     }
210
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);
216
217 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
218         nsIDOMEventListener *listener, BOOL capture)
219 {
220     nsAString type_str;
221     nsresult nsres;
222
223     nsAString_Init(&type_str, type);
224     nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture);
225     nsAString_Finish(&type_str);
226     if(NS_FAILED(nsres))
227         ERR("AddEventTarget failed: %08x\n", nsres);
228
229 }
230
231 static void init_listener(nsEventListener *This, NSContainer *container,
232         const nsIDOMEventListenerVtbl *vtbl)
233 {
234     This->lpDOMEventListenerVtbl = vtbl;
235     This->This = container;
236 }
237
238 void add_nsevent_listener(HTMLWindow *window, LPCWSTR type)
239 {
240     nsIDOMEventTarget *target;
241     nsresult nsres;
242
243     nsres = nsIDOMWindow_QueryInterface(window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
244     if(NS_FAILED(nsres)) {
245         ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
246         return;
247     }
248
249     init_event(target, type, NSEVENTLIST(&window->doc_obj->nscontainer->htmlevent_listener), TRUE);
250     nsIDOMEventTarget_Release(target);
251 }
252
253 void init_nsevents(NSContainer *This)
254 {
255     nsIDOMWindow *dom_window;
256     nsIDOMEventTarget *target;
257     nsresult nsres;
258
259     static const PRUnichar wsz_blur[]      = {'b','l','u','r',0};
260     static const PRUnichar wsz_focus[]     = {'f','o','c','u','s',0};
261     static const PRUnichar wsz_keypress[]  = {'k','e','y','p','r','e','s','s',0};
262     static const PRUnichar wsz_load[]      = {'l','o','a','d',0};
263
264     init_listener(&This->blur_listener,        This, &blur_vtbl);
265     init_listener(&This->focus_listener,       This, &focus_vtbl);
266     init_listener(&This->keypress_listener,    This, &keypress_vtbl);
267     init_listener(&This->load_listener,        This, &load_vtbl);
268     init_listener(&This->htmlevent_listener,   This, &htmlevent_vtbl);
269
270     nsres = nsIWebBrowser_GetContentDOMWindow(This->webbrowser, &dom_window);
271     if(NS_FAILED(nsres)) {
272         ERR("GetContentDOMWindow failed: %08x\n", nsres);
273         return;
274     }
275
276     nsres = nsIDOMWindow_QueryInterface(dom_window, &IID_nsIDOMEventTarget, (void**)&target);
277     nsIDOMWindow_Release(dom_window);
278     if(NS_FAILED(nsres)) {
279         ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
280         return;
281     }
282
283     init_event(target, wsz_blur,       NSEVENTLIST(&This->blur_listener),        TRUE);
284     init_event(target, wsz_focus,      NSEVENTLIST(&This->focus_listener),       TRUE);
285     init_event(target, wsz_keypress,   NSEVENTLIST(&This->keypress_listener),    FALSE);
286     init_event(target, wsz_load,       NSEVENTLIST(&This->load_listener),        TRUE);
287
288     nsIDOMEventTarget_Release(target);
289 }