mshtml: Add support for IHTMLStyle3 interface.
[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->hwnd; hwnd = GetParent(hwnd));
84
85     return hwnd == This->doc->hwnd;
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->focus && !is_doc_child_focus(This)) {
95         This->doc->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, UPDATE_UI);
124     if(This->doc->usermode == EDITMODE)
125         handle_edit_event(This->doc, 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     task_t *task;
135
136     TRACE("(%p)\n", This);
137
138     if(!This->doc)
139         return NS_OK;
140
141     update_nsdocument(This->doc);
142     connect_scripts(This->doc);
143
144     if(This->editor_controller) {
145         nsIController_Release(This->editor_controller);
146         This->editor_controller = NULL;
147     }
148
149     if(This->doc->usermode == EDITMODE)
150         handle_edit_load(This->doc);
151
152     task = heap_alloc(sizeof(task_t));
153
154     task->doc = This->doc;
155     task->task_id = TASK_PARSECOMPLETE;
156     task->next = NULL;
157
158     /*
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).
161      */
162     push_task(task);
163
164     if(!This->doc->nsdoc) {
165         ERR("NULL nsdoc\n");
166         return NS_ERROR_FAILURE;
167     }
168
169     nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nsbody);
170     if(nsbody) {
171         fire_event(This->doc, EVENTID_LOAD, (nsIDOMNode*)nsbody);
172         nsIDOMHTMLElement_Release(nsbody);
173     }
174
175     return NS_OK;
176 }
177
178 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
179 {
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;
187
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);
193
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;
198     }
199
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;
205     }
206
207     fire_event(This->doc, eid, nsnode);
208
209     nsIDOMNode_Release(nsnode);
210
211     return NS_OK;
212 }
213
214 #undef NSEVENTLIST_THIS
215
216 #define EVENTLISTENER_VTBL(handler) \
217     { \
218         nsDOMEventListener_QueryInterface, \
219         nsDOMEventListener_AddRef, \
220         nsDOMEventListener_Release, \
221         handler, \
222     }
223
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);
229
230 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
231         nsIDOMEventListener *listener, BOOL capture)
232 {
233     nsAString type_str;
234     nsresult nsres;
235
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);
241
242 }
243
244 static void init_listener(nsEventListener *This, NSContainer *container,
245         const nsIDOMEventListenerVtbl *vtbl)
246 {
247     This->lpDOMEventListenerVtbl = vtbl;
248     This->This = container;
249 }
250
251 void add_nsevent_listener(NSContainer *container, LPCWSTR type)
252 {
253     nsIDOMWindow *dom_window;
254     nsIDOMEventTarget *target;
255     nsresult nsres;
256
257     nsres = nsIWebBrowser_GetContentDOMWindow(container->webbrowser, &dom_window);
258     if(NS_FAILED(nsres)) {
259         ERR("GetContentDOMWindow failed: %08x\n", nsres);
260         return;
261     }
262
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;
268     }
269
270     init_event(target, type, NSEVENTLIST(&container->htmlevent_listener), TRUE);
271     nsIDOMEventTarget_Release(target);
272 }
273
274 void init_nsevents(NSContainer *This)
275 {
276     nsIDOMWindow *dom_window;
277     nsIDOMEventTarget *target;
278     nsresult nsres;
279
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};
284
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);
290
291     nsres = nsIWebBrowser_GetContentDOMWindow(This->webbrowser, &dom_window);
292     if(NS_FAILED(nsres)) {
293         ERR("GetContentDOMWindow failed: %08x\n", nsres);
294         return;
295     }
296
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;
302     }
303
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);
308
309     nsIDOMEventTarget_Release(target);
310 }