mpr: Don't succeed if drive is not remote in WNetGetUniversalName.
[wine/multimedia.git] / dlls / mshtml / nsevents.c
blobe6d6fbcaefc97bf3562ba06789c19a4df3c0234f
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"
34 #include "mshtml_private.h"
35 #include "htmlevent.h"
36 #include "resource.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40 static const PRUnichar blurW[] = {'b','l','u','r',0};
41 static const PRUnichar focusW[] = {'f','o','c','u','s',0};
42 static const PRUnichar keypressW[] = {'k','e','y','p','r','e','s','s',0};
43 static const PRUnichar loadW[] = {'l','o','a','d',0};
45 typedef struct {
46 nsIDOMEventListener nsIDOMEventListener_iface;
47 nsDocumentEventListener *This;
48 } nsEventListener;
50 struct nsDocumentEventListener {
51 nsEventListener blur_listener;
52 nsEventListener focus_listener;
53 nsEventListener keypress_listener;
54 nsEventListener load_listener;
55 nsEventListener htmlevent_listener;
57 LONG ref;
59 HTMLDocumentNode *doc;
62 static LONG release_listener(nsDocumentEventListener *This)
64 LONG ref = InterlockedDecrement(&This->ref);
66 TRACE("(%p) ref=%d\n", This, ref);
68 if(!ref)
69 heap_free(This);
71 return ref;
74 static inline nsEventListener *impl_from_nsIDOMEventListener(nsIDOMEventListener *iface)
76 return CONTAINING_RECORD(iface, nsEventListener, nsIDOMEventListener_iface);
79 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
80 nsIIDRef riid, void **result)
82 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
84 *result = NULL;
86 if(IsEqualGUID(&IID_nsISupports, riid)) {
87 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
88 *result = &This->nsIDOMEventListener_iface;
89 }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
90 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
91 *result = &This->nsIDOMEventListener_iface;
94 if(*result) {
95 nsIWebBrowserChrome_AddRef(&This->nsIDOMEventListener_iface);
96 return NS_OK;
99 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
100 return NS_NOINTERFACE;
103 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
105 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
106 LONG ref = InterlockedIncrement(&This->This->ref);
108 TRACE("(%p) ref=%d\n", This->This, ref);
110 return ref;
113 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
115 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
117 return release_listener(This->This);
120 static BOOL is_doc_child_focus(NSContainer *nscontainer)
122 HWND hwnd;
124 for(hwnd = GetFocus(); hwnd && hwnd != nscontainer->hwnd; hwnd = GetParent(hwnd));
126 return hwnd != NULL;
129 static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
131 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
132 HTMLDocumentNode *doc = This->This->doc;
133 HTMLDocumentObj *doc_obj;
135 TRACE("(%p)\n", doc);
137 if(!doc || !doc->basedoc.doc_obj)
138 return NS_ERROR_FAILURE;
139 doc_obj = doc->basedoc.doc_obj;
141 if(doc_obj->focus && !is_doc_child_focus(doc_obj->nscontainer)) {
142 doc_obj->focus = FALSE;
143 notif_focus(doc_obj);
146 return NS_OK;
149 static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
151 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
152 HTMLDocumentNode *doc = This->This->doc;
153 HTMLDocumentObj *doc_obj;
155 TRACE("(%p)\n", doc);
157 if(!doc)
158 return NS_ERROR_FAILURE;
159 doc_obj = doc->basedoc.doc_obj;
161 if(!doc_obj->focus) {
162 doc_obj->focus = TRUE;
163 notif_focus(doc_obj);
166 return NS_OK;
169 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
170 nsIDOMEvent *event)
172 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
173 HTMLDocumentNode *doc = This->This->doc;
174 HTMLDocumentObj *doc_obj;
176 if(!doc)
177 return NS_ERROR_FAILURE;
178 doc_obj = doc->basedoc.doc_obj;
180 TRACE("(%p)->(%p)\n", doc, event);
182 update_doc(&doc_obj->basedoc, UPDATE_UI);
183 if(doc_obj->usermode == EDITMODE)
184 handle_edit_event(&doc_obj->basedoc, event);
186 return NS_OK;
189 static void handle_docobj_load(HTMLDocumentObj *doc)
191 IOleCommandTarget *olecmd = NULL;
192 HRESULT hres;
194 if(doc->nscontainer->editor_controller) {
195 nsIController_Release(doc->nscontainer->editor_controller);
196 doc->nscontainer->editor_controller = NULL;
199 if(doc->usermode == EDITMODE)
200 handle_edit_load(&doc->basedoc);
202 if(doc->client) {
203 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
204 if(FAILED(hres))
205 olecmd = NULL;
208 if(doc->download_state) {
209 if(olecmd) {
210 VARIANT progress;
212 V_VT(&progress) = VT_I4;
213 V_I4(&progress) = 0;
214 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS,
215 OLECMDEXECOPT_DONTPROMPTUSER, &progress, NULL);
218 set_download_state(doc, 0);
221 if(olecmd) {
222 IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
223 IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
224 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
226 IOleCommandTarget_Release(olecmd);
230 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
232 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
233 HTMLDocumentNode *doc = This->This->doc;
234 nsIDOMHTMLElement *nsbody = NULL;
235 HTMLDocumentObj *doc_obj = NULL;
237 TRACE("(%p)\n", doc);
239 if(!doc || !doc->basedoc.window)
240 return NS_ERROR_FAILURE;
241 if(doc->basedoc.doc_obj && doc->basedoc.doc_obj->basedoc.doc_node == doc)
242 doc_obj = doc->basedoc.doc_obj;
244 connect_scripts(doc->basedoc.window->base.inner_window);
246 if(doc_obj)
247 handle_docobj_load(doc_obj);
249 set_ready_state(doc->basedoc.window, READYSTATE_COMPLETE);
251 if(doc_obj) {
252 if(doc_obj->view_sink)
253 IAdviseSink_OnViewChange(doc_obj->view_sink, DVASPECT_CONTENT, -1);
255 set_statustext(doc_obj, IDS_STATUS_DONE, NULL);
257 update_title(doc_obj);
260 if(doc_obj && doc_obj->usermode!=EDITMODE && doc_obj->doc_object_service)
261 IDocObjectService_FireDocumentComplete(doc_obj->doc_object_service,
262 &doc->basedoc.window->base.IHTMLWindow2_iface, 0);
264 if(!doc->nsdoc) {
265 ERR("NULL nsdoc\n");
266 return NS_ERROR_FAILURE;
269 nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
270 if(nsbody) {
271 fire_event(doc, EVENTID_LOAD, TRUE, (nsIDOMNode*)nsbody, event);
272 nsIDOMHTMLElement_Release(nsbody);
275 return NS_OK;
278 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
280 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
281 HTMLDocumentNode *doc = This->This->doc;
282 const PRUnichar *type;
283 nsIDOMEventTarget *event_target;
284 nsIDOMNode *nsnode;
285 nsAString type_str;
286 eventid_t eid;
287 nsresult nsres;
289 TRACE("%p\n", This->This);
291 nsAString_Init(&type_str, NULL);
292 nsIDOMEvent_GetType(event, &type_str);
293 nsAString_GetData(&type_str, &type);
294 eid = str_to_eid(type);
295 nsAString_Finish(&type_str);
297 nsres = nsIDOMEvent_GetTarget(event, &event_target);
298 if(NS_FAILED(nsres) || !event_target) {
299 ERR("GetEventTarget failed: %08x\n", nsres);
300 return NS_OK;
303 nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
304 nsIDOMEventTarget_Release(event_target);
305 if(NS_FAILED(nsres)) {
306 ERR("Could not get nsIDOMNode: %08x\n", nsres);
307 return NS_OK;
310 fire_event(doc, eid, TRUE, nsnode, event);
312 nsIDOMNode_Release(nsnode);
314 return NS_OK;
317 #define EVENTLISTENER_VTBL(handler) \
319 nsDOMEventListener_QueryInterface, \
320 nsDOMEventListener_AddRef, \
321 nsDOMEventListener_Release, \
322 handler, \
325 static const nsIDOMEventListenerVtbl blur_vtbl = EVENTLISTENER_VTBL(handle_blur);
326 static const nsIDOMEventListenerVtbl focus_vtbl = EVENTLISTENER_VTBL(handle_focus);
327 static const nsIDOMEventListenerVtbl keypress_vtbl = EVENTLISTENER_VTBL(handle_keypress);
328 static const nsIDOMEventListenerVtbl load_vtbl = EVENTLISTENER_VTBL(handle_load);
329 static const nsIDOMEventListenerVtbl htmlevent_vtbl = EVENTLISTENER_VTBL(handle_htmlevent);
331 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
332 nsIDOMEventListener *listener, BOOL capture)
334 nsAString type_str;
335 nsresult nsres;
337 nsAString_InitDepend(&type_str, type);
338 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture, FALSE, 1);
339 nsAString_Finish(&type_str);
340 if(NS_FAILED(nsres))
341 ERR("AddEventTarget failed: %08x\n", nsres);
345 static void init_listener(nsEventListener *This, nsDocumentEventListener *listener,
346 const nsIDOMEventListenerVtbl *vtbl)
348 This->nsIDOMEventListener_iface.lpVtbl = vtbl;
349 This->This = listener;
352 void add_nsevent_listener(HTMLDocumentNode *doc, nsIDOMNode *nsnode, LPCWSTR type)
354 nsIDOMEventTarget *target;
355 nsresult nsres;
357 if(nsnode)
358 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMEventTarget, (void**)&target);
359 else
360 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
361 if(NS_FAILED(nsres)) {
362 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
363 return;
366 init_event(target, type, &doc->nsevent_listener->htmlevent_listener.nsIDOMEventListener_iface,
367 TRUE);
368 nsIDOMEventTarget_Release(target);
371 static void detach_nslistener(HTMLDocumentNode *doc, const WCHAR *type, nsEventListener *listener, cpp_bool is_capture)
373 nsIDOMEventTarget *target;
374 nsAString type_str;
375 nsresult nsres;
377 if(!doc->basedoc.window)
378 return;
380 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
381 if(NS_FAILED(nsres)) {
382 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
383 return;
386 nsAString_InitDepend(&type_str, type);
387 nsres = nsIDOMEventTarget_RemoveEventListener(target, &type_str,
388 &listener->nsIDOMEventListener_iface, is_capture);
389 nsAString_Finish(&type_str);
390 nsIDOMEventTarget_Release(target);
391 if(NS_FAILED(nsres))
392 ERR("RemoveEventTarget failed: %08x\n", nsres);
395 void detach_nsevent(HTMLDocumentNode *doc, const WCHAR *type)
397 detach_nslistener(doc, type, &doc->nsevent_listener->htmlevent_listener, TRUE);
400 void release_nsevents(HTMLDocumentNode *doc)
402 nsDocumentEventListener *listener = doc->nsevent_listener;
404 TRACE("%p %p\n", doc, doc->nsevent_listener);
406 if(!listener)
407 return;
409 detach_nslistener(doc, blurW, &listener->blur_listener, TRUE);
410 detach_nslistener(doc, focusW, &listener->focus_listener, TRUE);
411 detach_nslistener(doc, keypressW, &listener->keypress_listener, FALSE);
412 detach_nslistener(doc, loadW, &listener->load_listener, TRUE);
414 listener->doc = NULL;
415 release_listener(listener);
416 doc->nsevent_listener = NULL;
419 void init_nsevents(HTMLDocumentNode *doc)
421 nsDocumentEventListener *listener;
422 nsIDOMEventTarget *target;
423 nsresult nsres;
425 listener = heap_alloc(sizeof(nsDocumentEventListener));
426 if(!listener)
427 return;
429 TRACE("%p %p\n", doc, listener);
431 listener->ref = 1;
432 listener->doc = doc;
434 init_listener(&listener->blur_listener, listener, &blur_vtbl);
435 init_listener(&listener->focus_listener, listener, &focus_vtbl);
436 init_listener(&listener->keypress_listener, listener, &keypress_vtbl);
437 init_listener(&listener->load_listener, listener, &load_vtbl);
438 init_listener(&listener->htmlevent_listener, listener, &htmlevent_vtbl);
440 doc->nsevent_listener = listener;
442 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
443 if(NS_FAILED(nsres)) {
444 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
445 return;
448 init_event(target, blurW, &listener->blur_listener.nsIDOMEventListener_iface, TRUE);
449 init_event(target, focusW, &listener->focus_listener.nsIDOMEventListener_iface, TRUE);
450 init_event(target, keypressW, &listener->keypress_listener.nsIDOMEventListener_iface, FALSE);
451 init_event(target, loadW, &listener->load_listener.nsIDOMEventListener_iface, TRUE);
453 nsIDOMEventTarget_Release(target);