winspool: Revise EnumPrinterDriversW to fix the incorrect handling of 'all'. EnumPrin...
[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 #include "mshtmcid.h"
30 #include "shlguid.h"
31
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34
35 #include "mshtml_private.h"
36 #include "htmlevent.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39
40 typedef struct {
41     const nsIDOMEventListenerVtbl      *lpDOMEventListenerVtbl;
42     nsDocumentEventListener *This;
43 } nsEventListener;
44
45 struct nsDocumentEventListener {
46     nsEventListener blur_listener;
47     nsEventListener focus_listener;
48     nsEventListener keypress_listener;
49     nsEventListener load_listener;
50     nsEventListener htmlevent_listener;
51
52     LONG ref;
53
54     HTMLDocumentNode *doc;
55 };
56
57 static LONG release_listener(nsDocumentEventListener *This)
58 {
59     LONG ref = InterlockedDecrement(&This->ref);
60
61     TRACE("(%p) ref=%d\n", This, ref);
62
63     if(!ref)
64         heap_free(This);
65
66     return ref;
67 }
68
69 #define NSEVENTLIST_THIS(iface) DEFINE_THIS(nsEventListener, DOMEventListener, iface)
70
71 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
72                                                         nsIIDRef riid, nsQIResult result)
73 {
74     nsEventListener *This = NSEVENTLIST_THIS(iface);
75
76     *result = NULL;
77
78     if(IsEqualGUID(&IID_nsISupports, riid)) {
79         TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
80         *result = NSEVENTLIST(This);
81     }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
82         TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
83         *result = NSEVENTLIST(This);
84     }
85
86     if(*result) {
87         nsIWebBrowserChrome_AddRef(NSEVENTLIST(This));
88         return NS_OK;
89     }
90
91     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
92     return NS_NOINTERFACE;
93 }
94
95 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
96 {
97     nsDocumentEventListener *This = NSEVENTLIST_THIS(iface)->This;
98     LONG ref = InterlockedIncrement(&This->ref);
99
100     TRACE("(%p) ref=%d\n", This, ref);
101
102     return ref;
103 }
104
105 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
106 {
107     nsDocumentEventListener *This = NSEVENTLIST_THIS(iface)->This;
108
109     return release_listener(This);
110 }
111
112 static BOOL is_doc_child_focus(HTMLDocumentObj *doc)
113 {
114     HWND hwnd;
115
116     for(hwnd = GetFocus(); hwnd && hwnd != doc->hwnd; hwnd = GetParent(hwnd));
117
118     return hwnd != NULL;
119 }
120
121 static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
122 {
123     HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
124     HTMLDocumentObj *doc_obj;
125
126     TRACE("(%p)\n", doc);
127
128     if(!doc || !doc->basedoc.doc_obj)
129         return NS_ERROR_FAILURE;
130     doc_obj = doc->basedoc.doc_obj;
131
132     if(!doc_obj->nscontainer->reset_focus && doc_obj->focus && !is_doc_child_focus(doc_obj)) {
133         doc_obj->focus = FALSE;
134         notif_focus(doc_obj);
135     }
136
137     return NS_OK;
138 }
139
140 static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
141 {
142     HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
143     HTMLDocumentObj *doc_obj;
144
145     TRACE("(%p)\n", doc);
146
147     if(!doc)
148         return NS_ERROR_FAILURE;
149     doc_obj = doc->basedoc.doc_obj;
150
151     if(!doc_obj->nscontainer->reset_focus && !doc_obj->focus) {
152         doc_obj->focus = TRUE;
153         notif_focus(doc_obj);
154     }
155
156     return NS_OK;
157 }
158
159 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
160         nsIDOMEvent *event)
161 {
162     HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
163     HTMLDocumentObj *doc_obj;
164
165     if(!doc)
166         return NS_ERROR_FAILURE;
167     doc_obj = doc->basedoc.doc_obj;
168
169     TRACE("(%p)->(%p)\n", doc, event);
170
171     update_doc(&doc_obj->basedoc, UPDATE_UI);
172     if(doc_obj->usermode == EDITMODE)
173         handle_edit_event(&doc_obj->basedoc, event);
174
175     return NS_OK;
176 }
177
178 static void handle_docobj_load(HTMLDocumentObj *doc)
179 {
180     IOleCommandTarget *olecmd = NULL;
181     HRESULT hres;
182
183     if(!doc->client)
184         return;
185
186     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
187     if(SUCCEEDED(hres)) {
188         VARIANT state, progress;
189
190         V_VT(&progress) = VT_I4;
191         V_I4(&progress) = 0;
192         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
193                                &progress, NULL);
194
195         V_VT(&state) = VT_I4;
196         V_I4(&state) = 0;
197         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
198                                &state, NULL);
199
200         IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
201         IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
202         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
203
204         IOleCommandTarget_Release(olecmd);
205     }
206 }
207
208 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
209 {
210     HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
211     HTMLDocumentObj *doc_obj;
212     nsIDOMHTMLElement *nsbody = NULL;
213
214     TRACE("(%p)\n", doc);
215
216     if(!doc)
217         return NS_ERROR_FAILURE;
218     doc_obj = doc->basedoc.doc_obj;
219
220     connect_scripts(doc->basedoc.window);
221
222     if(doc_obj->nscontainer->editor_controller) {
223         nsIController_Release(doc_obj->nscontainer->editor_controller);
224         doc_obj->nscontainer->editor_controller = NULL;
225     }
226
227     if(doc_obj->usermode == EDITMODE)
228         handle_edit_load(&doc_obj->basedoc);
229
230     if(doc == doc_obj->basedoc.doc_node)
231         handle_docobj_load(doc_obj);
232
233     set_ready_state(doc->basedoc.window, READYSTATE_COMPLETE);
234
235     if(doc == doc_obj->basedoc.doc_node) {
236         if(doc_obj->frame) {
237             static const WCHAR wszDone[] = {'D','o','n','e',0};
238             IOleInPlaceFrame_SetStatusText(doc_obj->frame, wszDone);
239         }
240
241         update_title(doc_obj);
242     }
243
244     if(!doc->nsdoc) {
245         ERR("NULL nsdoc\n");
246         return NS_ERROR_FAILURE;
247     }
248
249     nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
250     if(nsbody) {
251         fire_event(doc, EVENTID_LOAD, (nsIDOMNode*)nsbody, event);
252         nsIDOMHTMLElement_Release(nsbody);
253     }
254
255     return NS_OK;
256 }
257
258 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
259 {
260     HTMLDocumentNode *doc = NSEVENTLIST_THIS(iface)->This->doc;
261     const PRUnichar *type;
262     nsIDOMEventTarget *event_target;
263     nsIDOMNode *nsnode;
264     nsAString type_str;
265     eventid_t eid;
266     nsresult nsres;
267
268     nsAString_Init(&type_str, NULL);
269     nsIDOMEvent_GetType(event, &type_str);
270     nsAString_GetData(&type_str, &type);
271     eid = str_to_eid(type);
272     nsAString_Finish(&type_str);
273
274     nsres = nsIDOMEvent_GetTarget(event, &event_target);
275     if(NS_FAILED(nsres) || !event_target) {
276         ERR("GetEventTarget failed: %08x\n", nsres);
277         return NS_OK;
278     }
279
280     nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
281     nsIDOMEventTarget_Release(event_target);
282     if(NS_FAILED(nsres)) {
283         ERR("Could not get nsIDOMNode: %08x\n", nsres);
284         return NS_OK;
285     }
286
287     fire_event(doc, eid, nsnode, event);
288
289     nsIDOMNode_Release(nsnode);
290
291     return NS_OK;
292 }
293
294 #undef NSEVENTLIST_THIS
295
296 #define EVENTLISTENER_VTBL(handler) \
297     { \
298         nsDOMEventListener_QueryInterface, \
299         nsDOMEventListener_AddRef, \
300         nsDOMEventListener_Release, \
301         handler, \
302     }
303
304 static const nsIDOMEventListenerVtbl blur_vtbl =      EVENTLISTENER_VTBL(handle_blur);
305 static const nsIDOMEventListenerVtbl focus_vtbl =     EVENTLISTENER_VTBL(handle_focus);
306 static const nsIDOMEventListenerVtbl keypress_vtbl =  EVENTLISTENER_VTBL(handle_keypress);
307 static const nsIDOMEventListenerVtbl load_vtbl =      EVENTLISTENER_VTBL(handle_load);
308 static const nsIDOMEventListenerVtbl htmlevent_vtbl = EVENTLISTENER_VTBL(handle_htmlevent);
309
310 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
311         nsIDOMEventListener *listener, BOOL capture)
312 {
313     nsAString type_str;
314     nsresult nsres;
315
316     nsAString_Init(&type_str, type);
317     nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture);
318     nsAString_Finish(&type_str);
319     if(NS_FAILED(nsres))
320         ERR("AddEventTarget failed: %08x\n", nsres);
321
322 }
323
324 static void init_listener(nsEventListener *This, nsDocumentEventListener *listener,
325         const nsIDOMEventListenerVtbl *vtbl)
326 {
327     This->lpDOMEventListenerVtbl = vtbl;
328     This->This = listener;
329 }
330
331 void add_nsevent_listener(HTMLDocumentNode *doc, LPCWSTR type)
332 {
333     nsIDOMEventTarget *target;
334     nsresult nsres;
335
336     nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
337     if(NS_FAILED(nsres)) {
338         ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
339         return;
340     }
341
342     init_event(target, type, NSEVENTLIST(&doc->nsevent_listener->htmlevent_listener), TRUE);
343     nsIDOMEventTarget_Release(target);
344 }
345
346 void release_nsevents(HTMLDocumentNode *doc)
347 {
348     if(doc->nsevent_listener) {
349         doc->nsevent_listener->doc = NULL;
350         release_listener(doc->nsevent_listener);
351         doc->nsevent_listener = NULL;
352     }
353 }
354
355 void init_nsevents(HTMLDocumentNode *doc)
356 {
357     nsDocumentEventListener *listener;
358     nsIDOMEventTarget *target;
359     nsresult nsres;
360
361     static const PRUnichar wsz_blur[]      = {'b','l','u','r',0};
362     static const PRUnichar wsz_focus[]     = {'f','o','c','u','s',0};
363     static const PRUnichar wsz_keypress[]  = {'k','e','y','p','r','e','s','s',0};
364     static const PRUnichar wsz_load[]      = {'l','o','a','d',0};
365
366     listener = heap_alloc(sizeof(nsDocumentEventListener));
367     if(!listener)
368         return;
369
370     listener->ref = 1;
371     listener->doc = doc;
372
373     init_listener(&listener->blur_listener,        listener, &blur_vtbl);
374     init_listener(&listener->focus_listener,       listener, &focus_vtbl);
375     init_listener(&listener->keypress_listener,    listener, &keypress_vtbl);
376     init_listener(&listener->load_listener,        listener, &load_vtbl);
377     init_listener(&listener->htmlevent_listener,   listener, &htmlevent_vtbl);
378
379     doc->nsevent_listener = listener;
380
381     nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
382     if(NS_FAILED(nsres)) {
383         ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
384         return;
385     }
386
387     init_event(target, wsz_blur,       NSEVENTLIST(&listener->blur_listener),        TRUE);
388     init_event(target, wsz_focus,      NSEVENTLIST(&listener->focus_listener),       TRUE);
389     init_event(target, wsz_keypress,   NSEVENTLIST(&listener->keypress_listener),    FALSE);
390     init_event(target, wsz_load,       NSEVENTLIST(&listener->load_listener),        TRUE);
391
392     nsIDOMEventTarget_Release(target);
393 }