msxml4/tests: Copy namespaces as attributes tests.
[wine.git] / dlls / mshtml / nsevents.c
blob78a54c83955038113cafbc4531270bafa2a50c39
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 <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "mshtmcid.h"
28 #include "shlguid.h"
30 #include "wine/debug.h"
32 #include "mshtml_private.h"
33 #include "htmlscript.h"
34 #include "htmlevent.h"
35 #include "binding.h"
36 #include "resource.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40 typedef nsresult (*handler_t)(HTMLDocumentNode*,nsIDOMEvent*);
42 typedef struct {
43 nsIDOMEventListener nsIDOMEventListener_iface;
44 nsDocumentEventListener *This;
45 handler_t handler;
46 } nsEventListener;
48 static nsresult handle_blur(HTMLDocumentNode*,nsIDOMEvent*);
49 static nsresult handle_focus(HTMLDocumentNode*,nsIDOMEvent*);
50 static nsresult handle_keypress(HTMLDocumentNode*,nsIDOMEvent*);
51 static nsresult handle_dom_content_loaded(HTMLDocumentNode*,nsIDOMEvent*);
52 static nsresult handle_pageshow(HTMLDocumentNode*,nsIDOMEvent*);
53 static nsresult handle_pagehide(HTMLDocumentNode*,nsIDOMEvent*);
54 static nsresult handle_load(HTMLDocumentNode*,nsIDOMEvent*);
55 static nsresult handle_beforeunload(HTMLDocumentNode*,nsIDOMEvent*);
56 static nsresult handle_unload(HTMLDocumentNode*,nsIDOMEvent*);
58 enum doc_event_listener_flags {
59 BUBBLES = 0x0001,
60 OVERRIDE = 0x0002,
63 static const struct {
64 eventid_t id;
65 enum doc_event_listener_flags flags;
66 handler_t handler;
67 } doc_event_listeners[] = {
68 { EVENTID_BLUR, 0, handle_blur },
69 { EVENTID_FOCUS, 0, handle_focus },
70 { EVENTID_KEYPRESS, BUBBLES, handle_keypress },
71 { EVENTID_DOMCONTENTLOADED, OVERRIDE, handle_dom_content_loaded },
72 { EVENTID_PAGESHOW, OVERRIDE, handle_pageshow },
73 { EVENTID_PAGEHIDE, OVERRIDE, handle_pagehide },
74 { EVENTID_LOAD, OVERRIDE, handle_load },
75 { EVENTID_BEFOREUNLOAD, OVERRIDE, handle_beforeunload },
76 { EVENTID_UNLOAD, OVERRIDE, handle_unload },
79 struct nsDocumentEventListener {
80 nsEventListener listener[ARRAY_SIZE(doc_event_listeners)];
81 nsEventListener htmlevent_listener;
83 LONG ref;
85 HTMLDocumentNode *doc;
88 static LONG release_listener(nsDocumentEventListener *This)
90 LONG ref = InterlockedDecrement(&This->ref);
92 TRACE("(%p) ref=%ld\n", This, ref);
94 if(!ref)
95 free(This);
97 return ref;
100 static inline nsEventListener *impl_from_nsIDOMEventListener(nsIDOMEventListener *iface)
102 return CONTAINING_RECORD(iface, nsEventListener, nsIDOMEventListener_iface);
105 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
106 nsIIDRef riid, void **result)
108 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
110 *result = NULL;
112 if(IsEqualGUID(&IID_nsISupports, riid)) {
113 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
114 *result = &This->nsIDOMEventListener_iface;
115 }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
116 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
117 *result = &This->nsIDOMEventListener_iface;
120 if(*result) {
121 nsIDOMEventListener_AddRef(&This->nsIDOMEventListener_iface);
122 return NS_OK;
125 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
126 return NS_NOINTERFACE;
129 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
131 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
132 LONG ref = InterlockedIncrement(&This->This->ref);
134 TRACE("(%p) ref=%ld\n", This->This, ref);
136 return ref;
139 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
141 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
143 return release_listener(This->This);
146 static nsresult NSAPI nsDOMEventListener_HandleEvent(nsIDOMEventListener *iface, nsIDOMEvent *event)
148 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
149 HTMLDocumentNode *doc = This->This->doc;
150 HTMLInnerWindow *window;
151 nsresult nsres;
153 if(!doc) {
154 WARN("NULL doc!\n");
155 return NS_ERROR_FAILURE;
158 /* Hold a ref to the window, as some apps load another document during handlers */
159 window = doc->window;
160 if(window)
161 IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
163 nsres = This->handler(doc, event);
165 if(window)
166 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
168 return nsres;
171 static const nsIDOMEventListenerVtbl nsDOMEventListenerVtbl = {
172 nsDOMEventListener_QueryInterface,
173 nsDOMEventListener_AddRef,
174 nsDOMEventListener_Release,
175 nsDOMEventListener_HandleEvent
178 static BOOL is_doc_child_focus(GeckoBrowser *nscontainer)
180 HWND hwnd;
182 for(hwnd = GetFocus(); hwnd && hwnd != nscontainer->hwnd; hwnd = GetParent(hwnd));
184 return hwnd != NULL;
187 static nsresult handle_blur(HTMLDocumentNode *doc, nsIDOMEvent *event)
189 HTMLDocumentObj *doc_obj;
191 TRACE("(%p)\n", doc);
193 if(!doc->doc_obj)
194 return NS_ERROR_FAILURE;
195 doc_obj = doc->doc_obj;
197 if(doc_obj->focus && !is_doc_child_focus(doc_obj->nscontainer)) {
198 doc_obj->focus = FALSE;
199 notif_focus(doc_obj);
202 return NS_OK;
205 static nsresult handle_focus(HTMLDocumentNode *doc, nsIDOMEvent *event)
207 HTMLDocumentObj *doc_obj;
209 TRACE("(%p)\n", doc);
211 doc_obj = doc->doc_obj;
213 if(!doc_obj->focus) {
214 doc_obj->focus = TRUE;
215 notif_focus(doc_obj);
218 return NS_OK;
221 static nsresult handle_keypress(HTMLDocumentNode *doc, nsIDOMEvent *event)
223 if(!doc->browser)
224 return NS_ERROR_FAILURE;
226 TRACE("(%p)->(%p)\n", doc, event);
228 update_doc(doc->browser->doc, UPDATE_UI);
229 if(doc->browser->usermode == EDITMODE)
230 handle_edit_event(doc, event);
232 return NS_OK;
235 static nsresult handle_dom_content_loaded(HTMLDocumentNode *doc, nsIDOMEvent *nsevent)
237 DOMEvent *event;
238 HRESULT hres;
240 if(doc->window)
241 doc->window->dom_content_loaded_event_start_time = get_time_stamp();
243 hres = create_event_from_nsevent(nsevent, dispex_compat_mode(&doc->node.event_target.dispex), &event);
244 if(SUCCEEDED(hres)) {
245 dispatch_event(&doc->node.event_target, event);
246 IDOMEvent_Release(&event->IDOMEvent_iface);
249 if(doc->window)
250 doc->window->dom_content_loaded_event_end_time = get_time_stamp();
252 return NS_OK;
255 static nsresult handle_pageshow(HTMLDocumentNode *doc, nsIDOMEvent *nsevent)
257 HTMLInnerWindow *window;
258 DOMEvent *event;
259 HRESULT hres;
261 if(!(window = doc->window) || !doc->dom_document || doc->document_mode < COMPAT_MODE_IE11)
262 return NS_OK;
264 hres = create_document_event(doc, EVENTID_PAGESHOW, &event);
265 if(SUCCEEDED(hres)) {
266 dispatch_event(&window->event_target, event);
267 IDOMEvent_Release(&event->IDOMEvent_iface);
270 return NS_OK;
273 static nsresult handle_pagehide(HTMLDocumentNode *doc, nsIDOMEvent *nsevent)
275 HTMLInnerWindow *window;
276 DOMEvent *event;
277 HRESULT hres;
279 if(!(window = doc->window) || !doc->dom_document || doc->document_mode < COMPAT_MODE_IE11 || doc->unload_sent)
280 return NS_OK;
282 hres = create_document_event(doc, EVENTID_PAGEHIDE, &event);
283 if(SUCCEEDED(hres)) {
284 dispatch_event(&window->event_target, event);
285 IDOMEvent_Release(&event->IDOMEvent_iface);
288 return NS_OK;
291 static void handle_docobj_load(HTMLDocumentObj *doc)
293 IOleCommandTarget *olecmd = NULL;
294 HRESULT hres;
296 unlink_ref(&doc->nscontainer->editor_controller);
298 if(doc->nscontainer->usermode == EDITMODE)
299 setup_editor_controller(doc->nscontainer);
301 if(doc->client) {
302 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
303 if(FAILED(hres))
304 olecmd = NULL;
307 if(doc->download_state) {
308 if(olecmd) {
309 VARIANT progress;
311 V_VT(&progress) = VT_I4;
312 V_I4(&progress) = 0;
313 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS,
314 OLECMDEXECOPT_DONTPROMPTUSER, &progress, NULL);
317 set_download_state(doc, 0);
320 if(olecmd) {
321 IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
322 IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
323 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
325 IOleCommandTarget_Release(olecmd);
329 static nsresult handle_load(HTMLDocumentNode *doc, nsIDOMEvent *event)
331 HTMLDocumentObj *doc_obj = NULL;
332 DOMEvent *load_event;
333 HRESULT hres;
335 TRACE("(%p)\n", doc);
337 if(!doc->outer_window)
338 return NS_ERROR_FAILURE;
339 if(doc->doc_obj && doc->doc_obj->doc_node == doc) {
340 doc_obj = doc->doc_obj;
341 IUnknown_AddRef(doc_obj->outer_unk);
343 connect_scripts(doc->window);
345 if(doc_obj)
346 handle_docobj_load(doc_obj);
348 doc->window->dom_complete_time = get_time_stamp();
349 set_ready_state(doc->outer_window, READYSTATE_COMPLETE);
351 if(doc_obj) {
352 if(doc_obj->view_sink)
353 IAdviseSink_OnViewChange(doc_obj->view_sink, DVASPECT_CONTENT, -1);
355 set_statustext(doc_obj, IDS_STATUS_DONE, NULL);
357 update_title(doc_obj);
359 if(doc_obj->doc_object_service && !(doc->outer_window->load_flags & BINDING_REFRESH))
360 IDocObjectService_FireDocumentComplete(doc_obj->doc_object_service,
361 &doc->outer_window->base.IHTMLWindow2_iface, 0);
363 IUnknown_Release(doc_obj->outer_unk);
366 doc->window->load_event_start_time = get_time_stamp();
368 if(doc->dom_document) {
369 hres = create_document_event(doc, EVENTID_LOAD, &load_event);
370 if(SUCCEEDED(hres)) {
371 dispatch_event(&doc->node.event_target, load_event);
372 IDOMEvent_Release(&load_event->IDOMEvent_iface);
374 }else {
375 WARN("no dom_document\n");
378 hres = create_event_from_nsevent(event, dispex_compat_mode(&doc->node.event_target.dispex), &load_event);
379 if(SUCCEEDED(hres)) {
380 dispatch_event(&doc->window->event_target, load_event);
381 IDOMEvent_Release(&load_event->IDOMEvent_iface);
384 doc->window->load_event_end_time = get_time_stamp();
385 return NS_OK;
388 static nsresult handle_beforeunload(HTMLDocumentNode *doc, nsIDOMEvent *nsevent)
390 HTMLInnerWindow *window;
391 DOMEvent *event;
392 HRESULT hres;
394 if(!(window = doc->window))
395 return NS_OK;
397 /* Gecko dispatches this to the document, but IE dispatches it to the window */
398 hres = create_event_from_nsevent(nsevent, dispex_compat_mode(&doc->node.event_target.dispex), &event);
399 if(SUCCEEDED(hres)) {
400 dispatch_event(&window->event_target, event);
401 IDOMEvent_Release(&event->IDOMEvent_iface);
404 return NS_OK;
407 static nsresult handle_unload(HTMLDocumentNode *doc, nsIDOMEvent *nsevent)
409 HTMLInnerWindow *window, *pending_window;
410 DOMEvent *event;
411 HRESULT hres;
413 if(!(window = doc->window) || doc->unload_sent)
414 return NS_OK;
415 doc->unload_sent = TRUE;
417 pending_window = window->base.outer_window->pending_window;
418 if(pending_window)
419 pending_window->unload_event_start_time = get_time_stamp();
421 hres = create_event_from_nsevent(nsevent, dispex_compat_mode(&doc->node.event_target.dispex), &event);
422 if(SUCCEEDED(hres)) {
423 dispatch_event(&window->event_target, event);
424 IDOMEvent_Release(&event->IDOMEvent_iface);
427 if(pending_window)
428 pending_window->unload_event_end_time = get_time_stamp();
430 return NS_OK;
433 static nsresult handle_htmlevent(HTMLDocumentNode *doc, nsIDOMEvent *nsevent)
435 nsIDOMEventTarget *event_target;
436 EventTarget *target;
437 nsIDOMNode *nsnode;
438 DOMEvent *event;
439 nsresult nsres;
440 HRESULT hres;
442 TRACE("%p\n", doc);
444 nsres = nsIDOMEvent_GetTarget(nsevent, &event_target);
445 if(NS_FAILED(nsres) || !event_target) {
446 ERR("GetEventTarget failed: %08lx\n", nsres);
447 return NS_OK;
450 nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
451 nsIDOMEventTarget_Release(event_target);
452 if(NS_FAILED(nsres)) {
453 if(!doc->window)
454 return NS_OK;
455 target = &doc->window->event_target;
456 IHTMLWindow2_AddRef(&doc->window->base.IHTMLWindow2_iface);
457 }else {
458 HTMLDOMNode *node;
459 hres = get_node(nsnode, TRUE, &node);
460 nsIDOMNode_Release(nsnode);
461 if(FAILED(hres))
462 return NS_OK;
463 target = &node->event_target;
466 hres = create_event_from_nsevent(nsevent, dispex_compat_mode(&doc->node.event_target.dispex), &event);
467 if(FAILED(hres)) {
468 IEventTarget_Release(&target->IEventTarget_iface);
469 return NS_OK;
472 /* If we fine need for more special cases here, we may consider handling it in a more generic way. */
473 if(event->event_id == EVENTID_FOCUS || event->event_id == EVENTID_BLUR) {
474 DOMEvent *focus_event;
476 hres = create_document_event(doc, event->event_id == EVENTID_FOCUS ? EVENTID_FOCUSIN : EVENTID_FOCUSOUT, &focus_event);
477 if(SUCCEEDED(hres)) {
478 dispatch_event(target, focus_event);
479 IDOMEvent_Release(&focus_event->IDOMEvent_iface);
483 dispatch_event(target, event);
485 IDOMEvent_Release(&event->IDOMEvent_iface);
486 IEventTarget_Release(&target->IEventTarget_iface);
487 return NS_OK;
490 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
491 nsIDOMEventListener *listener, BOOL capture)
493 nsAString type_str;
494 nsresult nsres;
496 nsAString_InitDepend(&type_str, type);
497 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture, FALSE, 1);
498 nsAString_Finish(&type_str);
499 if(NS_FAILED(nsres))
500 ERR("AddEventTarget failed: %08lx\n", nsres);
504 static void init_listener(nsEventListener *This, nsDocumentEventListener *listener, handler_t handler)
506 This->nsIDOMEventListener_iface.lpVtbl = &nsDOMEventListenerVtbl;
507 This->handler = handler;
508 This->This = listener;
511 static nsIDOMEventTarget *get_default_document_target(HTMLDocumentNode *doc)
513 nsIDOMEventTarget *target;
514 nsISupports *target_iface;
515 nsresult nsres;
517 target_iface = doc->window ? (nsISupports*)doc->outer_window->nswindow : (nsISupports*)doc->dom_document;
518 nsres = nsISupports_QueryInterface(target_iface, &IID_nsIDOMEventTarget, (void**)&target);
519 return NS_SUCCEEDED(nsres) ? target : NULL;
522 void add_nsevent_listener(HTMLDocumentNode *doc, nsIDOMNode *nsnode, LPCWSTR type)
524 nsIDOMEventTarget *target;
525 nsresult nsres;
527 if(nsnode) {
528 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMEventTarget, (void**)&target);
529 assert(nsres == NS_OK);
530 }else {
531 target = get_default_document_target(doc);
532 if(!target)
533 return;
536 init_event(target, type, &doc->nsevent_listener->htmlevent_listener.nsIDOMEventListener_iface,
537 TRUE);
538 nsIDOMEventTarget_Release(target);
541 static void detach_nslistener(HTMLDocumentNode *doc, const WCHAR *type, nsEventListener *listener, cpp_bool is_capture)
543 nsIDOMEventTarget *target;
544 nsAString type_str;
545 nsresult nsres;
547 target = get_default_document_target(doc);
548 if(!target)
549 return;
551 nsAString_InitDepend(&type_str, type);
552 nsres = nsIDOMEventTarget_RemoveEventListener(target, &type_str,
553 &listener->nsIDOMEventListener_iface, is_capture);
554 nsAString_Finish(&type_str);
555 nsIDOMEventTarget_Release(target);
556 if(NS_FAILED(nsres))
557 ERR("RemoveEventTarget failed: %08lx\n", nsres);
560 void detach_nsevent(HTMLDocumentNode *doc, const WCHAR *type)
562 detach_nslistener(doc, type, &doc->nsevent_listener->htmlevent_listener, TRUE);
565 void release_nsevents(HTMLDocumentNode *doc)
567 nsDocumentEventListener *listener = doc->nsevent_listener;
568 unsigned i;
570 TRACE("%p %p\n", doc, doc->nsevent_listener);
572 if(!listener)
573 return;
575 for(i = 0; i < ARRAY_SIZE(doc_event_listeners); i++)
576 detach_nslistener(doc, get_event_name(doc_event_listeners[i].id), &listener->listener[i], !(doc_event_listeners[i].flags & BUBBLES));
578 listener->doc = NULL;
579 release_listener(listener);
580 doc->nsevent_listener = NULL;
583 void init_nsevents(HTMLDocumentNode *doc)
585 nsDocumentEventListener *listener;
586 nsIDOMEventTarget *target;
587 unsigned i;
589 listener = malloc(sizeof(nsDocumentEventListener));
590 if(!listener)
591 return;
593 TRACE("%p %p\n", doc, listener);
595 listener->ref = 1;
596 listener->doc = doc;
598 for(i = 0; i < ARRAY_SIZE(doc_event_listeners); i++)
599 init_listener(&listener->listener[i], listener, doc_event_listeners[i].handler);
600 init_listener(&listener->htmlevent_listener, listener, handle_htmlevent);
602 doc->nsevent_listener = listener;
604 target = get_default_document_target(doc);
605 if(!target)
606 return;
608 for(i = 0; i < ARRAY_SIZE(doc_event_listeners); i++) {
609 init_event(target, get_event_name(doc_event_listeners[i].id), &listener->listener[i].nsIDOMEventListener_iface,
610 !(doc_event_listeners[i].flags & BUBBLES));
612 if(doc_event_listeners[i].flags & OVERRIDE)
613 doc->event_vector[doc_event_listeners[i].id] = TRUE;
616 nsIDOMEventTarget_Release(target);