mscoree: COM cleanup in metahost.c.
[wine.git] / dlls / mshtml / nsevents.c
blob957b892736ead71ab1db4c63a38d57f690ca588b
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"
29 #include "mshtmcid.h"
30 #include "shlguid.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
35 #include "mshtml_private.h"
36 #include "htmlevent.h"
37 #include "resource.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41 typedef struct {
42 nsIDOMEventListener nsIDOMEventListener_iface;
43 nsDocumentEventListener *This;
44 } nsEventListener;
46 struct nsDocumentEventListener {
47 nsEventListener blur_listener;
48 nsEventListener focus_listener;
49 nsEventListener keypress_listener;
50 nsEventListener load_listener;
51 nsEventListener htmlevent_listener;
53 LONG ref;
55 HTMLDocumentNode *doc;
58 static LONG release_listener(nsDocumentEventListener *This)
60 LONG ref = InterlockedDecrement(&This->ref);
62 TRACE("(%p) ref=%d\n", This, ref);
64 if(!ref)
65 heap_free(This);
67 return ref;
70 static inline nsEventListener *impl_from_nsIDOMEventListener(nsIDOMEventListener *iface)
72 return CONTAINING_RECORD(iface, nsEventListener, nsIDOMEventListener_iface);
75 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
76 nsIIDRef riid, void **result)
78 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
80 *result = NULL;
82 if(IsEqualGUID(&IID_nsISupports, riid)) {
83 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
84 *result = &This->nsIDOMEventListener_iface;
85 }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
86 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
87 *result = &This->nsIDOMEventListener_iface;
90 if(*result) {
91 nsIWebBrowserChrome_AddRef(&This->nsIDOMEventListener_iface);
92 return NS_OK;
95 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
96 return NS_NOINTERFACE;
99 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
101 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
102 LONG ref = InterlockedIncrement(&This->This->ref);
104 TRACE("(%p) ref=%d\n", This->This, ref);
106 return ref;
109 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
111 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
113 return release_listener(This->This);
116 static BOOL is_doc_child_focus(NSContainer *nscontainer)
118 HWND hwnd;
120 for(hwnd = GetFocus(); hwnd && hwnd != nscontainer->hwnd; hwnd = GetParent(hwnd));
122 return hwnd != NULL;
125 static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
127 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
128 HTMLDocumentNode *doc = This->This->doc;
129 HTMLDocumentObj *doc_obj;
131 TRACE("(%p)\n", doc);
133 if(!doc || !doc->basedoc.doc_obj)
134 return NS_ERROR_FAILURE;
135 doc_obj = doc->basedoc.doc_obj;
137 if(doc_obj->focus && !is_doc_child_focus(doc_obj->nscontainer)) {
138 doc_obj->focus = FALSE;
139 notif_focus(doc_obj);
142 return NS_OK;
145 static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
147 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
148 HTMLDocumentNode *doc = This->This->doc;
149 HTMLDocumentObj *doc_obj;
151 TRACE("(%p)\n", doc);
153 if(!doc)
154 return NS_ERROR_FAILURE;
155 doc_obj = doc->basedoc.doc_obj;
157 if(!doc_obj->focus) {
158 doc_obj->focus = TRUE;
159 notif_focus(doc_obj);
162 return NS_OK;
165 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
166 nsIDOMEvent *event)
168 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
169 HTMLDocumentNode *doc = This->This->doc;
170 HTMLDocumentObj *doc_obj;
172 if(!doc)
173 return NS_ERROR_FAILURE;
174 doc_obj = doc->basedoc.doc_obj;
176 TRACE("(%p)->(%p)\n", doc, event);
178 update_doc(&doc_obj->basedoc, UPDATE_UI);
179 if(doc_obj->usermode == EDITMODE)
180 handle_edit_event(&doc_obj->basedoc, event);
182 return NS_OK;
185 static void handle_docobj_load(HTMLDocumentObj *doc)
187 HRESULT hres;
189 if(doc->nscontainer->editor_controller) {
190 nsIController_Release(doc->nscontainer->editor_controller);
191 doc->nscontainer->editor_controller = NULL;
194 if(doc->usermode == EDITMODE)
195 handle_edit_load(&doc->basedoc);
197 if(doc->client) {
198 IOleCommandTarget *olecmd = NULL;
200 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
201 if(SUCCEEDED(hres)) {
202 if(doc->download_state) {
203 VARIANT state, progress;
205 V_VT(&progress) = VT_I4;
206 V_I4(&progress) = 0;
207 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS,
208 OLECMDEXECOPT_DONTPROMPTUSER, &progress, NULL);
210 V_VT(&state) = VT_I4;
211 V_I4(&state) = 0;
212 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE,
213 OLECMDEXECOPT_DONTPROMPTUSER, &state, NULL);
216 IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
217 IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
218 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
220 IOleCommandTarget_Release(olecmd);
224 doc->download_state = 0;
227 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
229 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
230 HTMLDocumentNode *doc = This->This->doc;
231 nsIDOMHTMLElement *nsbody = NULL;
232 HTMLDocumentObj *doc_obj = NULL;
234 TRACE("(%p)\n", doc);
236 if(!doc || !doc->basedoc.window)
237 return NS_ERROR_FAILURE;
238 if(doc->basedoc.doc_obj && doc->basedoc.doc_obj->basedoc.doc_node == doc)
239 doc_obj = doc->basedoc.doc_obj;
241 connect_scripts(doc->basedoc.window);
243 if(doc_obj)
244 handle_docobj_load(doc_obj);
246 set_ready_state(doc->basedoc.window, READYSTATE_COMPLETE);
248 if(doc_obj) {
249 if(doc_obj->view_sink)
250 IAdviseSink_OnViewChange(doc_obj->view_sink, DVASPECT_CONTENT, -1);
252 set_statustext(doc_obj, IDS_STATUS_DONE, NULL);
254 update_title(doc_obj);
257 if(doc_obj && doc_obj->doc_object_service)
258 IDocObjectService_FireDocumentComplete(doc_obj->doc_object_service,
259 &doc->basedoc.window->IHTMLWindow2_iface, 0);
261 if(!doc->nsdoc) {
262 ERR("NULL nsdoc\n");
263 return NS_ERROR_FAILURE;
266 nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
267 if(nsbody) {
268 fire_event(doc, EVENTID_LOAD, TRUE, (nsIDOMNode*)nsbody, event);
269 nsIDOMHTMLElement_Release(nsbody);
272 return NS_OK;
275 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
277 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
278 HTMLDocumentNode *doc = This->This->doc;
279 const PRUnichar *type;
280 nsIDOMEventTarget *event_target;
281 nsIDOMNode *nsnode;
282 nsAString type_str;
283 eventid_t eid;
284 nsresult nsres;
286 TRACE("\n");
288 nsAString_Init(&type_str, NULL);
289 nsIDOMEvent_GetType(event, &type_str);
290 nsAString_GetData(&type_str, &type);
291 eid = str_to_eid(type);
292 nsAString_Finish(&type_str);
294 nsres = nsIDOMEvent_GetTarget(event, &event_target);
295 if(NS_FAILED(nsres) || !event_target) {
296 ERR("GetEventTarget failed: %08x\n", nsres);
297 return NS_OK;
300 nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
301 nsIDOMEventTarget_Release(event_target);
302 if(NS_FAILED(nsres)) {
303 ERR("Could not get nsIDOMNode: %08x\n", nsres);
304 return NS_OK;
307 fire_event(doc, eid, TRUE, nsnode, event);
309 nsIDOMNode_Release(nsnode);
311 return NS_OK;
314 #define EVENTLISTENER_VTBL(handler) \
316 nsDOMEventListener_QueryInterface, \
317 nsDOMEventListener_AddRef, \
318 nsDOMEventListener_Release, \
319 handler, \
322 static const nsIDOMEventListenerVtbl blur_vtbl = EVENTLISTENER_VTBL(handle_blur);
323 static const nsIDOMEventListenerVtbl focus_vtbl = EVENTLISTENER_VTBL(handle_focus);
324 static const nsIDOMEventListenerVtbl keypress_vtbl = EVENTLISTENER_VTBL(handle_keypress);
325 static const nsIDOMEventListenerVtbl load_vtbl = EVENTLISTENER_VTBL(handle_load);
326 static const nsIDOMEventListenerVtbl htmlevent_vtbl = EVENTLISTENER_VTBL(handle_htmlevent);
328 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
329 nsIDOMEventListener *listener, BOOL capture)
331 nsAString type_str;
332 nsresult nsres;
334 nsAString_InitDepend(&type_str, type);
335 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture);
336 nsAString_Finish(&type_str);
337 if(NS_FAILED(nsres))
338 ERR("AddEventTarget failed: %08x\n", nsres);
342 static void init_listener(nsEventListener *This, nsDocumentEventListener *listener,
343 const nsIDOMEventListenerVtbl *vtbl)
345 This->nsIDOMEventListener_iface.lpVtbl = vtbl;
346 This->This = listener;
349 void add_nsevent_listener(HTMLDocumentNode *doc, nsIDOMNode *nsnode, LPCWSTR type)
351 nsIDOMEventTarget *target;
352 nsresult nsres;
354 if(nsnode)
355 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMEventTarget, (void**)&target);
356 else
357 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
358 if(NS_FAILED(nsres)) {
359 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
360 return;
363 init_event(target, type, &doc->nsevent_listener->htmlevent_listener.nsIDOMEventListener_iface,
364 TRUE);
365 nsIDOMEventTarget_Release(target);
368 void release_nsevents(HTMLDocumentNode *doc)
370 if(doc->nsevent_listener) {
371 doc->nsevent_listener->doc = NULL;
372 release_listener(doc->nsevent_listener);
373 doc->nsevent_listener = NULL;
377 void init_nsevents(HTMLDocumentNode *doc)
379 nsDocumentEventListener *listener;
380 nsIDOMEventTarget *target;
381 nsresult nsres;
383 static const PRUnichar wsz_blur[] = {'b','l','u','r',0};
384 static const PRUnichar wsz_focus[] = {'f','o','c','u','s',0};
385 static const PRUnichar wsz_keypress[] = {'k','e','y','p','r','e','s','s',0};
386 static const PRUnichar wsz_load[] = {'l','o','a','d',0};
388 listener = heap_alloc(sizeof(nsDocumentEventListener));
389 if(!listener)
390 return;
392 listener->ref = 1;
393 listener->doc = doc;
395 init_listener(&listener->blur_listener, listener, &blur_vtbl);
396 init_listener(&listener->focus_listener, listener, &focus_vtbl);
397 init_listener(&listener->keypress_listener, listener, &keypress_vtbl);
398 init_listener(&listener->load_listener, listener, &load_vtbl);
399 init_listener(&listener->htmlevent_listener, listener, &htmlevent_vtbl);
401 doc->nsevent_listener = listener;
403 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
404 if(NS_FAILED(nsres)) {
405 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
406 return;
409 init_event(target, wsz_blur, &listener->blur_listener.nsIDOMEventListener_iface, TRUE);
410 init_event(target, wsz_focus, &listener->focus_listener.nsIDOMEventListener_iface, TRUE);
411 init_event(target, wsz_keypress, &listener->keypress_listener.nsIDOMEventListener_iface, FALSE);
412 init_event(target, wsz_load, &listener->load_listener.nsIDOMEventListener_iface, TRUE);
414 nsIDOMEventTarget_Release(target);