d3drm: Implement CreateMesh method and stubbed IDirect3DRMMesh interface.
[wine/multimedia.git] / dlls / mshtml / nsevents.c
blobd4cdc1036a2a884d1c2030ddac8fd371fa7b48ad
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 typedef struct {
41 nsIDOMEventListener nsIDOMEventListener_iface;
42 nsDocumentEventListener *This;
43 } nsEventListener;
45 struct nsDocumentEventListener {
46 nsEventListener blur_listener;
47 nsEventListener focus_listener;
48 nsEventListener keypress_listener;
49 nsEventListener load_listener;
50 nsEventListener htmlevent_listener;
52 LONG ref;
54 HTMLDocumentNode *doc;
57 static LONG release_listener(nsDocumentEventListener *This)
59 LONG ref = InterlockedDecrement(&This->ref);
61 TRACE("(%p) ref=%d\n", This, ref);
63 if(!ref)
64 heap_free(This);
66 return ref;
69 static inline nsEventListener *impl_from_nsIDOMEventListener(nsIDOMEventListener *iface)
71 return CONTAINING_RECORD(iface, nsEventListener, nsIDOMEventListener_iface);
74 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
75 nsIIDRef riid, void **result)
77 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
79 *result = NULL;
81 if(IsEqualGUID(&IID_nsISupports, riid)) {
82 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
83 *result = &This->nsIDOMEventListener_iface;
84 }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
85 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
86 *result = &This->nsIDOMEventListener_iface;
89 if(*result) {
90 nsIWebBrowserChrome_AddRef(&This->nsIDOMEventListener_iface);
91 return NS_OK;
94 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
95 return NS_NOINTERFACE;
98 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
100 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
101 LONG ref = InterlockedIncrement(&This->This->ref);
103 TRACE("(%p) ref=%d\n", This->This, ref);
105 return ref;
108 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
110 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
112 return release_listener(This->This);
115 static BOOL is_doc_child_focus(NSContainer *nscontainer)
117 HWND hwnd;
119 for(hwnd = GetFocus(); hwnd && hwnd != nscontainer->hwnd; hwnd = GetParent(hwnd));
121 return hwnd != NULL;
124 static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
126 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
127 HTMLDocumentNode *doc = This->This->doc;
128 HTMLDocumentObj *doc_obj;
130 TRACE("(%p)\n", doc);
132 if(!doc || !doc->basedoc.doc_obj)
133 return NS_ERROR_FAILURE;
134 doc_obj = doc->basedoc.doc_obj;
136 if(doc_obj->focus && !is_doc_child_focus(doc_obj->nscontainer)) {
137 doc_obj->focus = FALSE;
138 notif_focus(doc_obj);
141 return NS_OK;
144 static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
146 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
147 HTMLDocumentNode *doc = This->This->doc;
148 HTMLDocumentObj *doc_obj;
150 TRACE("(%p)\n", doc);
152 if(!doc)
153 return NS_ERROR_FAILURE;
154 doc_obj = doc->basedoc.doc_obj;
156 if(!doc_obj->focus) {
157 doc_obj->focus = TRUE;
158 notif_focus(doc_obj);
161 return NS_OK;
164 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
165 nsIDOMEvent *event)
167 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
168 HTMLDocumentNode *doc = This->This->doc;
169 HTMLDocumentObj *doc_obj;
171 if(!doc)
172 return NS_ERROR_FAILURE;
173 doc_obj = doc->basedoc.doc_obj;
175 TRACE("(%p)->(%p)\n", doc, event);
177 update_doc(&doc_obj->basedoc, UPDATE_UI);
178 if(doc_obj->usermode == EDITMODE)
179 handle_edit_event(&doc_obj->basedoc, event);
181 return NS_OK;
184 static void handle_docobj_load(HTMLDocumentObj *doc)
186 IOleCommandTarget *olecmd = NULL;
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 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
199 if(FAILED(hres))
200 olecmd = NULL;
203 if(doc->download_state) {
204 if(olecmd) {
205 VARIANT progress;
207 V_VT(&progress) = VT_I4;
208 V_I4(&progress) = 0;
209 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS,
210 OLECMDEXECOPT_DONTPROMPTUSER, &progress, NULL);
213 set_download_state(doc, 0);
216 if(olecmd) {
217 IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
218 IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
219 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
221 IOleCommandTarget_Release(olecmd);
225 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
227 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
228 HTMLDocumentNode *doc = This->This->doc;
229 nsIDOMHTMLElement *nsbody = NULL;
230 HTMLDocumentObj *doc_obj = NULL;
232 TRACE("(%p)\n", doc);
234 if(!doc || !doc->basedoc.window)
235 return NS_ERROR_FAILURE;
236 if(doc->basedoc.doc_obj && doc->basedoc.doc_obj->basedoc.doc_node == doc)
237 doc_obj = doc->basedoc.doc_obj;
239 connect_scripts(doc->basedoc.window);
241 if(doc_obj)
242 handle_docobj_load(doc_obj);
244 set_ready_state(doc->basedoc.window, READYSTATE_COMPLETE);
246 if(doc_obj) {
247 if(doc_obj->view_sink)
248 IAdviseSink_OnViewChange(doc_obj->view_sink, DVASPECT_CONTENT, -1);
250 set_statustext(doc_obj, IDS_STATUS_DONE, NULL);
252 update_title(doc_obj);
255 if(doc_obj && doc_obj->usermode!=EDITMODE && doc_obj->doc_object_service)
256 IDocObjectService_FireDocumentComplete(doc_obj->doc_object_service,
257 &doc->basedoc.window->IHTMLWindow2_iface, 0);
259 if(!doc->nsdoc) {
260 ERR("NULL nsdoc\n");
261 return NS_ERROR_FAILURE;
264 nsIDOMHTMLDocument_GetBody(doc->nsdoc, &nsbody);
265 if(nsbody) {
266 fire_event(doc, EVENTID_LOAD, TRUE, (nsIDOMNode*)nsbody, event);
267 nsIDOMHTMLElement_Release(nsbody);
270 return NS_OK;
273 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
275 nsEventListener *This = impl_from_nsIDOMEventListener(iface);
276 HTMLDocumentNode *doc = This->This->doc;
277 const PRUnichar *type;
278 nsIDOMEventTarget *event_target;
279 nsIDOMNode *nsnode;
280 nsAString type_str;
281 eventid_t eid;
282 nsresult nsres;
284 TRACE("%p\n", This->This);
286 nsAString_Init(&type_str, NULL);
287 nsIDOMEvent_GetType(event, &type_str);
288 nsAString_GetData(&type_str, &type);
289 eid = str_to_eid(type);
290 nsAString_Finish(&type_str);
292 nsres = nsIDOMEvent_GetTarget(event, &event_target);
293 if(NS_FAILED(nsres) || !event_target) {
294 ERR("GetEventTarget failed: %08x\n", nsres);
295 return NS_OK;
298 nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
299 nsIDOMEventTarget_Release(event_target);
300 if(NS_FAILED(nsres)) {
301 ERR("Could not get nsIDOMNode: %08x\n", nsres);
302 return NS_OK;
305 fire_event(doc, eid, TRUE, nsnode, event);
307 nsIDOMNode_Release(nsnode);
309 return NS_OK;
312 #define EVENTLISTENER_VTBL(handler) \
314 nsDOMEventListener_QueryInterface, \
315 nsDOMEventListener_AddRef, \
316 nsDOMEventListener_Release, \
317 handler, \
320 static const nsIDOMEventListenerVtbl blur_vtbl = EVENTLISTENER_VTBL(handle_blur);
321 static const nsIDOMEventListenerVtbl focus_vtbl = EVENTLISTENER_VTBL(handle_focus);
322 static const nsIDOMEventListenerVtbl keypress_vtbl = EVENTLISTENER_VTBL(handle_keypress);
323 static const nsIDOMEventListenerVtbl load_vtbl = EVENTLISTENER_VTBL(handle_load);
324 static const nsIDOMEventListenerVtbl htmlevent_vtbl = EVENTLISTENER_VTBL(handle_htmlevent);
326 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
327 nsIDOMEventListener *listener, BOOL capture)
329 nsAString type_str;
330 nsresult nsres;
332 nsAString_InitDepend(&type_str, type);
333 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture, FALSE, 1);
334 nsAString_Finish(&type_str);
335 if(NS_FAILED(nsres))
336 ERR("AddEventTarget failed: %08x\n", nsres);
340 static void init_listener(nsEventListener *This, nsDocumentEventListener *listener,
341 const nsIDOMEventListenerVtbl *vtbl)
343 This->nsIDOMEventListener_iface.lpVtbl = vtbl;
344 This->This = listener;
347 void add_nsevent_listener(HTMLDocumentNode *doc, nsIDOMNode *nsnode, LPCWSTR type)
349 nsIDOMEventTarget *target;
350 nsresult nsres;
352 if(nsnode)
353 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMEventTarget, (void**)&target);
354 else
355 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
356 if(NS_FAILED(nsres)) {
357 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
358 return;
361 init_event(target, type, &doc->nsevent_listener->htmlevent_listener.nsIDOMEventListener_iface,
362 TRUE);
363 nsIDOMEventTarget_Release(target);
366 void detach_nsevent(HTMLDocumentNode *doc, const WCHAR *type)
368 nsIDOMEventTarget *target;
369 nsAString type_str;
370 nsresult nsres;
372 if(!doc->basedoc.window)
373 return;
375 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
376 if(NS_FAILED(nsres)) {
377 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
378 return;
381 nsAString_InitDepend(&type_str, type);
382 nsres = nsIDOMEventTarget_RemoveEventListener(target, &type_str,
383 &doc->nsevent_listener->htmlevent_listener.nsIDOMEventListener_iface, TRUE);
384 nsAString_Finish(&type_str);
385 nsIDOMEventTarget_Release(target);
386 if(NS_FAILED(nsres))
387 ERR("RemoveEventTarget failed: %08x\n", nsres);
390 void release_nsevents(HTMLDocumentNode *doc)
392 TRACE("%p %p\n", doc, doc->nsevent_listener);
394 if(doc->nsevent_listener) {
395 doc->nsevent_listener->doc = NULL;
396 release_listener(doc->nsevent_listener);
397 doc->nsevent_listener = NULL;
401 void init_nsevents(HTMLDocumentNode *doc)
403 nsDocumentEventListener *listener;
404 nsIDOMEventTarget *target;
405 nsresult nsres;
407 static const PRUnichar wsz_blur[] = {'b','l','u','r',0};
408 static const PRUnichar wsz_focus[] = {'f','o','c','u','s',0};
409 static const PRUnichar wsz_keypress[] = {'k','e','y','p','r','e','s','s',0};
410 static const PRUnichar wsz_load[] = {'l','o','a','d',0};
412 listener = heap_alloc(sizeof(nsDocumentEventListener));
413 if(!listener)
414 return;
416 TRACE("%p %p\n", doc, listener);
418 listener->ref = 1;
419 listener->doc = doc;
421 init_listener(&listener->blur_listener, listener, &blur_vtbl);
422 init_listener(&listener->focus_listener, listener, &focus_vtbl);
423 init_listener(&listener->keypress_listener, listener, &keypress_vtbl);
424 init_listener(&listener->load_listener, listener, &load_vtbl);
425 init_listener(&listener->htmlevent_listener, listener, &htmlevent_vtbl);
427 doc->nsevent_listener = listener;
429 nsres = nsIDOMWindow_QueryInterface(doc->basedoc.window->nswindow, &IID_nsIDOMEventTarget, (void**)&target);
430 if(NS_FAILED(nsres)) {
431 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
432 return;
435 init_event(target, wsz_blur, &listener->blur_listener.nsIDOMEventListener_iface, TRUE);
436 init_event(target, wsz_focus, &listener->focus_listener.nsIDOMEventListener_iface, TRUE);
437 init_event(target, wsz_keypress, &listener->keypress_listener.nsIDOMEventListener_iface, FALSE);
438 init_event(target, wsz_load, &listener->load_listener.nsIDOMEventListener_iface, TRUE);
440 nsIDOMEventTarget_Release(target);