d3d9/tests: Add test for regression in IDirect3DDevice9_Reset().
[wine.git] / dlls / mshtml / nsevents.c
blobd1d0512fceb560664e24a209beb76cd7eb89425c
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 "htmlscript.h"
36 #include "htmlevent.h"
37 #include "binding.h"
38 #include "resource.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
42 static const PRUnichar blurW[] = {'b','l','u','r',0};
43 static const PRUnichar focusW[] = {'f','o','c','u','s',0};
44 static const PRUnichar keypressW[] = {'k','e','y','p','r','e','s','s',0};
45 static const PRUnichar loadW[] = {'l','o','a','d',0};
47 typedef struct {
48 nsIDOMEventListener nsIDOMEventListener_iface;
49 nsDocumentEventListener *This;
50 } nsEventListener;
52 struct nsDocumentEventListener {
53 nsEventListener blur_listener;
54 nsEventListener focus_listener;
55 nsEventListener keypress_listener;
56 nsEventListener load_listener;
57 nsEventListener htmlevent_listener;
59 LONG ref;
61 HTMLDocumentNode *doc;
64 static LONG release_listener(nsDocumentEventListener *This)
66 LONG ref = InterlockedDecrement(&This->ref);
68 TRACE("(%p) ref=%d\n", This, ref);
70 if(!ref)
71 heap_free(This);
73 return ref;
76 static inline nsEventListener *impl_from_nsIDOMEventListener(nsIDOMEventListener *iface)
78 return CONTAINING_RECORD(iface, nsEventListener, nsIDOMEventListener_iface);
81 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
82 nsIIDRef riid, void **result)
84 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
86 *result = NULL;
88 if(IsEqualGUID(&IID_nsISupports, riid)) {
89 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
90 *result = &This->nsIDOMEventListener_iface;
91 }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
92 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
93 *result = &This->nsIDOMEventListener_iface;
96 if(*result) {
97 nsIDOMEventListener_AddRef(&This->nsIDOMEventListener_iface);
98 return NS_OK;
101 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
102 return NS_NOINTERFACE;
105 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
107 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
108 LONG ref = InterlockedIncrement(&This->This->ref);
110 TRACE("(%p) ref=%d\n", This->This, ref);
112 return ref;
115 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
117 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
119 return release_listener(This->This);
122 static BOOL is_doc_child_focus(NSContainer *nscontainer)
124 HWND hwnd;
126 for(hwnd = GetFocus(); hwnd && hwnd != nscontainer->hwnd; hwnd = GetParent(hwnd));
128 return hwnd != NULL;
131 static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
133 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
134 HTMLDocumentNode *doc = This->This->doc;
135 HTMLDocumentObj *doc_obj;
137 TRACE("(%p)\n", doc);
139 if(!doc || !doc->basedoc.doc_obj)
140 return NS_ERROR_FAILURE;
141 doc_obj = doc->basedoc.doc_obj;
143 if(doc_obj->focus && !is_doc_child_focus(doc_obj->nscontainer)) {
144 doc_obj->focus = FALSE;
145 notif_focus(doc_obj);
148 return NS_OK;
151 static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
153 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
154 HTMLDocumentNode *doc = This->This->doc;
155 HTMLDocumentObj *doc_obj;
157 TRACE("(%p)\n", doc);
159 if(!doc)
160 return NS_ERROR_FAILURE;
161 doc_obj = doc->basedoc.doc_obj;
163 if(!doc_obj->focus) {
164 doc_obj->focus = TRUE;
165 notif_focus(doc_obj);
168 return NS_OK;
171 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
172 nsIDOMEvent *event)
174 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
175 HTMLDocumentNode *doc = This->This->doc;
176 HTMLDocumentObj *doc_obj;
178 if(!doc)
179 return NS_ERROR_FAILURE;
180 doc_obj = doc->basedoc.doc_obj;
182 TRACE("(%p)->(%p)\n", doc, event);
184 update_doc(&doc_obj->basedoc, UPDATE_UI);
185 if(doc_obj->usermode == EDITMODE)
186 handle_edit_event(&doc_obj->basedoc, event);
188 return NS_OK;
191 static void handle_docobj_load(HTMLDocumentObj *doc)
193 IOleCommandTarget *olecmd = NULL;
194 HRESULT hres;
196 if(doc->nscontainer->editor_controller) {
197 nsIController_Release(doc->nscontainer->editor_controller);
198 doc->nscontainer->editor_controller = NULL;
201 if(doc->usermode == EDITMODE)
202 handle_edit_load(&doc->basedoc);
204 if(doc->client) {
205 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
206 if(FAILED(hres))
207 olecmd = NULL;
210 if(doc->download_state) {
211 if(olecmd) {
212 VARIANT progress;
214 V_VT(&progress) = VT_I4;
215 V_I4(&progress) = 0;
216 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS,
217 OLECMDEXECOPT_DONTPROMPTUSER, &progress, NULL);
220 set_download_state(doc, 0);
223 if(olecmd) {
224 IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
225 IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
226 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
228 IOleCommandTarget_Release(olecmd);
232 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
234 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
235 HTMLDocumentNode *doc = This->This->doc;
236 HTMLDocumentObj *doc_obj = NULL;
237 nsresult nsres = NS_OK;
239 TRACE("(%p)\n", doc);
241 if(!doc || !doc->basedoc.window)
242 return NS_ERROR_FAILURE;
243 if(doc->basedoc.doc_obj && doc->basedoc.doc_obj->basedoc.doc_node == doc)
244 doc_obj = doc->basedoc.doc_obj;
246 connect_scripts(doc->window);
248 htmldoc_addref(&doc->basedoc);
250 if(doc_obj)
251 handle_docobj_load(doc_obj);
253 set_ready_state(doc->basedoc.window, READYSTATE_COMPLETE);
255 if(doc_obj) {
256 if(doc_obj->view_sink)
257 IAdviseSink_OnViewChange(doc_obj->view_sink, DVASPECT_CONTENT, -1);
259 set_statustext(doc_obj, IDS_STATUS_DONE, NULL);
261 update_title(doc_obj);
264 if(doc_obj && doc_obj->usermode!=EDITMODE && doc_obj->doc_object_service
265 && !(doc->basedoc.window->load_flags & BINDING_REFRESH))
266 IDocObjectService_FireDocumentComplete(doc_obj->doc_object_service,
267 &doc->basedoc.window->base.IHTMLWindow2_iface, 0);
269 if(doc->nsdoc) {
270 nsIDOMHTMLElement *nsbody;
272 flush_pending_tasks(doc->basedoc.task_magic);
274 nsres = nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
275 if(NS_SUCCEEDED(nsres) && nsbody) {
276 HTMLDOMNode *node;
277 HRESULT hres;
279 hres = get_node(doc, (nsIDOMNode*)nsbody, TRUE, &node);
280 nsIDOMHTMLElement_Release(nsbody);
281 if(SUCCEEDED(hres)) {
282 fire_event(doc, EVENTID_LOAD, TRUE, node, event,
283 (IDispatch*)&doc->window->base.IDispatchEx_iface);
284 node_release(node);
287 }else {
288 ERR("NULL nsdoc\n");
289 nsres = NS_ERROR_FAILURE;
292 htmldoc_release(&doc->basedoc);
293 return nsres;
296 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
298 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
299 HTMLDocumentNode *doc = This->This->doc;
300 const PRUnichar *type;
301 nsIDOMEventTarget *event_target;
302 nsIDOMNode *nsnode;
303 nsAString type_str;
304 HTMLDOMNode *node;
305 eventid_t eid;
306 nsresult nsres;
307 HRESULT hres;
309 TRACE("%p\n", This->This);
311 if(!doc) {
312 WARN("NULL doc\n");
313 return NS_OK;
316 nsAString_Init(&type_str, NULL);
317 nsIDOMEvent_GetType(event, &type_str);
318 nsAString_GetData(&type_str, &type);
319 eid = str_to_eid(type);
320 nsAString_Finish(&type_str);
322 nsres = nsIDOMEvent_GetTarget(event, &event_target);
323 if(NS_FAILED(nsres) || !event_target) {
324 ERR("GetEventTarget failed: %08x\n", nsres);
325 return NS_OK;
328 nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
329 nsIDOMEventTarget_Release(event_target);
330 if(NS_FAILED(nsres)) {
331 ERR("Could not get nsIDOMNode: %08x\n", nsres);
332 return NS_OK;
335 hres = get_node(doc, nsnode, TRUE, &node);
336 nsIDOMNode_Release(nsnode);
337 if(FAILED(hres))
338 return NS_OK;
340 /* If we fine need for more special cases here, we may consider handling it in a more generic way. */
341 switch(eid) {
342 case EVENTID_FOCUS:
343 if(doc->event_vector[EVENTID_FOCUSIN])
344 fire_event(doc, EVENTID_FOCUSIN, TRUE, node, NULL, NULL);
345 break;
346 case EVENTID_BLUR:
347 if(doc->event_vector[EVENTID_FOCUSOUT])
348 fire_event(doc, EVENTID_FOCUSOUT, TRUE, node, NULL, NULL);
349 break;
350 default:
351 break;
354 fire_event(doc, eid, TRUE, node, event, NULL);
356 node_release(node);
357 return NS_OK;
360 #define EVENTLISTENER_VTBL(handler) \
362 nsDOMEventListener_QueryInterface, \
363 nsDOMEventListener_AddRef, \
364 nsDOMEventListener_Release, \
365 handler, \
368 static const nsIDOMEventListenerVtbl blur_vtbl = EVENTLISTENER_VTBL(handle_blur);
369 static const nsIDOMEventListenerVtbl focus_vtbl = EVENTLISTENER_VTBL(handle_focus);
370 static const nsIDOMEventListenerVtbl keypress_vtbl = EVENTLISTENER_VTBL(handle_keypress);
371 static const nsIDOMEventListenerVtbl load_vtbl = EVENTLISTENER_VTBL(handle_load);
372 static const nsIDOMEventListenerVtbl htmlevent_vtbl = EVENTLISTENER_VTBL(handle_htmlevent);
374 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
375 nsIDOMEventListener *listener, BOOL capture)
377 nsAString type_str;
378 nsresult nsres;
380 nsAString_InitDepend(&type_str, type);
381 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture, FALSE, 1);
382 nsAString_Finish(&type_str);
383 if(NS_FAILED(nsres))
384 ERR("AddEventTarget failed: %08x\n", nsres);
388 static void init_listener(nsEventListener *This, nsDocumentEventListener *listener,
389 const nsIDOMEventListenerVtbl *vtbl)
391 This->nsIDOMEventListener_iface.lpVtbl = vtbl;
392 This->This = listener;
395 void add_nsevent_listener(HTMLDocumentNode *doc, nsIDOMNode *nsnode, LPCWSTR type)
397 nsIDOMEventTarget *target;
398 nsresult nsres;
400 if(nsnode)
401 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMEventTarget, (void**)&target);
402 else
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, type, &doc->nsevent_listener->htmlevent_listener.nsIDOMEventListener_iface,
410 TRUE);
411 nsIDOMEventTarget_Release(target);
414 static void detach_nslistener(HTMLDocumentNode *doc, const WCHAR *type, nsEventListener *listener, cpp_bool is_capture)
416 nsIDOMEventTarget *target;
417 nsAString type_str;
418 nsresult nsres;
420 if(!doc->basedoc.window)
421 return;
423 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
424 if(NS_FAILED(nsres)) {
425 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
426 return;
429 nsAString_InitDepend(&type_str, type);
430 nsres = nsIDOMEventTarget_RemoveEventListener(target, &type_str,
431 &listener->nsIDOMEventListener_iface, is_capture);
432 nsAString_Finish(&type_str);
433 nsIDOMEventTarget_Release(target);
434 if(NS_FAILED(nsres))
435 ERR("RemoveEventTarget failed: %08x\n", nsres);
438 void detach_nsevent(HTMLDocumentNode *doc, const WCHAR *type)
440 detach_nslistener(doc, type, &doc->nsevent_listener->htmlevent_listener, TRUE);
443 void release_nsevents(HTMLDocumentNode *doc)
445 nsDocumentEventListener *listener = doc->nsevent_listener;
447 TRACE("%p %p\n", doc, doc->nsevent_listener);
449 if(!listener)
450 return;
452 detach_nslistener(doc, blurW, &listener->blur_listener, TRUE);
453 detach_nslistener(doc, focusW, &listener->focus_listener, TRUE);
454 detach_nslistener(doc, keypressW, &listener->keypress_listener, FALSE);
455 detach_nslistener(doc, loadW, &listener->load_listener, TRUE);
457 listener->doc = NULL;
458 release_listener(listener);
459 doc->nsevent_listener = NULL;
462 void init_nsevents(HTMLDocumentNode *doc)
464 nsDocumentEventListener *listener;
465 nsIDOMEventTarget *target;
466 nsresult nsres;
468 listener = heap_alloc(sizeof(nsDocumentEventListener));
469 if(!listener)
470 return;
472 TRACE("%p %p\n", doc, listener);
474 listener->ref = 1;
475 listener->doc = doc;
477 init_listener(&listener->blur_listener, listener, &blur_vtbl);
478 init_listener(&listener->focus_listener, listener, &focus_vtbl);
479 init_listener(&listener->keypress_listener, listener, &keypress_vtbl);
480 init_listener(&listener->load_listener, listener, &load_vtbl);
481 init_listener(&listener->htmlevent_listener, listener, &htmlevent_vtbl);
483 doc->nsevent_listener = listener;
485 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
486 if(NS_FAILED(nsres)) {
487 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
488 return;
491 init_event(target, blurW, &listener->blur_listener.nsIDOMEventListener_iface, TRUE);
492 init_event(target, focusW, &listener->focus_listener.nsIDOMEventListener_iface, TRUE);
493 init_event(target, keypressW, &listener->keypress_listener.nsIDOMEventListener_iface, FALSE);
494 init_event(target, loadW, &listener->load_listener.nsIDOMEventListener_iface, TRUE);
496 nsIDOMEventTarget_Release(target);