include: Document definitions missing from cryptuiapi.h.
[wine/wine64.git] / dlls / mshtml / nsevents.c
blob806e9eb6d4ba3ec3be08107cd7fd2a7c4d98a44c
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"
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 #include "mshtml_private.h"
34 #include "htmlevent.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 #define NSEVENTLIST_THIS(iface) DEFINE_THIS(nsEventListener, DOMEventListener, iface)
40 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
41 nsIIDRef riid, nsQIResult result)
43 nsEventListener *This = NSEVENTLIST_THIS(iface);
45 *result = NULL;
47 if(IsEqualGUID(&IID_nsISupports, riid)) {
48 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
49 *result = NSEVENTLIST(This);
50 }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
51 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
52 *result = NSEVENTLIST(This);
55 if(*result) {
56 nsIWebBrowserChrome_AddRef(NSEVENTLIST(This));
57 return NS_OK;
60 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
61 return NS_NOINTERFACE;
64 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
66 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
67 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
70 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
72 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
73 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
76 static BOOL is_doc_child_focus(NSContainer *This)
78 HWND hwnd;
80 if(!This->doc)
81 return FALSE;
83 for(hwnd = GetFocus(); hwnd && hwnd != This->doc->hwnd; hwnd = GetParent(hwnd));
85 return hwnd == This->doc->hwnd;
88 static nsresult NSAPI handle_blur(nsIDOMEventListener *iface, nsIDOMEvent *event)
90 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
92 TRACE("(%p)\n", This);
94 if(!This->reset_focus && This->doc && This->doc->focus && !is_doc_child_focus(This)) {
95 This->doc->focus = FALSE;
96 notif_focus(This->doc);
99 return NS_OK;
102 static nsresult NSAPI handle_focus(nsIDOMEventListener *iface, nsIDOMEvent *event)
104 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
106 TRACE("(%p)\n", This);
108 if(!This->reset_focus && This->doc && !This->doc->focus) {
109 This->doc->focus = TRUE;
110 notif_focus(This->doc);
113 return NS_OK;
116 static nsresult NSAPI handle_keypress(nsIDOMEventListener *iface,
117 nsIDOMEvent *event)
119 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
121 TRACE("(%p)->(%p)\n", This, event);
123 update_doc(This->doc, UPDATE_UI);
124 if(This->doc->usermode == EDITMODE)
125 handle_edit_event(This->doc, event);
127 return NS_OK;
130 static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event)
132 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
133 nsIDOMHTMLElement *nsbody = NULL;
134 task_t *task;
136 TRACE("(%p)\n", This);
138 if(!This->doc)
139 return NS_OK;
141 update_nsdocument(This->doc);
142 connect_scripts(This->doc);
144 if(This->editor_controller) {
145 nsIController_Release(This->editor_controller);
146 This->editor_controller = NULL;
149 if(This->doc->usermode == EDITMODE)
150 handle_edit_load(This->doc);
152 task = heap_alloc(sizeof(task_t));
154 task->doc = This->doc;
155 task->task_id = TASK_PARSECOMPLETE;
156 task->next = NULL;
159 * This should be done in the worker thread that parses HTML,
160 * but we don't have such thread (Gecko parses HTML for us).
162 push_task(task);
164 if(!This->doc->nsdoc) {
165 ERR("NULL nsdoc\n");
166 return NS_ERROR_FAILURE;
169 nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nsbody);
170 if(nsbody) {
171 fire_event(This->doc, EVENTID_LOAD, (nsIDOMNode*)nsbody);
172 nsIDOMHTMLElement_Release(nsbody);
175 return NS_OK;
178 #define IE_MAJOR_VERSION 7
179 #define IE_MINOR_VERSION 0
181 static BOOL handle_insert_comment(HTMLDocument *doc, const PRUnichar *comment)
183 DWORD len;
184 int majorv = 0, minorv = 0;
185 const PRUnichar *ptr, *end;
186 nsAString nsstr;
187 PRUnichar *buf;
188 nsresult nsres;
190 enum {
191 CMP_EQ,
192 CMP_LT,
193 CMP_LTE,
194 CMP_GT,
195 CMP_GTE
196 } cmpt = CMP_EQ;
198 static const PRUnichar endifW[] = {'<','!','[','e','n','d','i','f',']'};
200 if(comment[0] != '[' || comment[1] != 'i' || comment[2] != 'f')
201 return FALSE;
203 ptr = comment+3;
204 while(isspaceW(*ptr))
205 ptr++;
207 if(ptr[0] == 'l' && ptr[1] == 't') {
208 ptr += 2;
209 if(*ptr == 'e') {
210 cmpt = CMP_LTE;
211 ptr++;
212 }else {
213 cmpt = CMP_LT;
215 }else if(ptr[0] == 'g' && ptr[1] == 't') {
216 ptr += 2;
217 if(*ptr == 'e') {
218 cmpt = CMP_GTE;
219 ptr++;
220 }else {
221 cmpt = CMP_GT;
225 if(!isspaceW(*ptr++))
226 return FALSE;
227 while(isspaceW(*ptr))
228 ptr++;
230 if(ptr[0] != 'I' || ptr[1] != 'E')
231 return FALSE;
233 ptr +=2;
234 if(!isspaceW(*ptr++))
235 return FALSE;
236 while(isspaceW(*ptr))
237 ptr++;
239 if(!isdigitW(*ptr))
240 return FALSE;
241 while(isdigitW(*ptr))
242 majorv = majorv*10 + (*ptr++ - '0');
244 if(*ptr == '.') {
245 if(!isdigitW(*ptr))
246 return FALSE;
247 while(isdigitW(*ptr))
248 minorv = minorv*10 + (*ptr++ - '0');
251 while(isspaceW(*ptr))
252 ptr++;
253 if(ptr[0] != ']' || ptr[1] != '>')
254 return FALSE;
255 ptr += 2;
257 len = strlenW(ptr);
258 if(len < sizeof(endifW)/sizeof(WCHAR))
259 return FALSE;
261 end = ptr + len-sizeof(endifW)/sizeof(WCHAR);
262 if(memcmp(end, endifW, sizeof(endifW)))
263 return FALSE;
265 switch(cmpt) {
266 case CMP_EQ:
267 if(majorv == IE_MAJOR_VERSION && minorv == IE_MINOR_VERSION)
268 break;
269 return FALSE;
270 case CMP_LT:
271 if(majorv > IE_MAJOR_VERSION)
272 break;
273 if(majorv == IE_MAJOR_VERSION && minorv > IE_MINOR_VERSION)
274 break;
275 return FALSE;
276 case CMP_LTE:
277 if(majorv > IE_MAJOR_VERSION)
278 break;
279 if(majorv == IE_MAJOR_VERSION && minorv >= IE_MINOR_VERSION)
280 break;
281 return FALSE;
282 case CMP_GT:
283 if(majorv < IE_MAJOR_VERSION)
284 break;
285 if(majorv == IE_MAJOR_VERSION && minorv < IE_MINOR_VERSION)
286 break;
287 return FALSE;
288 case CMP_GTE:
289 if(majorv < IE_MAJOR_VERSION)
290 break;
291 if(majorv == IE_MAJOR_VERSION && minorv <= IE_MINOR_VERSION)
292 break;
293 return FALSE;
296 buf = heap_alloc((end-ptr+1)*sizeof(WCHAR));
297 if(!buf)
298 return FALSE;
300 memcpy(buf, ptr, (end-ptr)*sizeof(WCHAR));
301 buf[end-ptr] = 0;
302 nsAString_Init(&nsstr, buf);
303 heap_free(buf);
305 /* FIXME: Find better way to insert HTML to document. */
306 nsres = nsIDOMHTMLDocument_Write(doc->nsdoc, &nsstr);
307 nsAString_Finish(&nsstr);
308 if(NS_FAILED(nsres)) {
309 ERR("Write failed: %08x\n", nsres);
310 return FALSE;
313 return TRUE;
316 static nsresult NSAPI handle_node_insert(nsIDOMEventListener *iface, nsIDOMEvent *event)
318 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
319 nsIDOMEventTarget *target;
320 nsIDOMComment *nscomment;
321 nsIDOMElement *elem;
322 nsresult nsres;
324 TRACE("(%p %p)\n", This, event);
326 nsres = nsIDOMEvent_GetTarget(event, &target);
327 if(NS_FAILED(nsres)) {
328 ERR("GetTarget failed: %08x\n", nsres);
329 return NS_OK;
332 nsres = nsIDOMEventTarget_QueryInterface(target, &IID_nsIDOMElement, (void**)&elem);
333 if(NS_SUCCEEDED(nsres)) {
334 nsIDOMHTMLScriptElement *script;
336 nsres = nsIDOMElement_QueryInterface(elem, &IID_nsIDOMHTMLScriptElement, (void**)&script);
337 if(NS_SUCCEEDED(nsres)) {
338 doc_insert_script(This->doc, script);
339 nsIDOMHTMLScriptElement_Release(script);
342 check_event_attr(This->doc, elem);
344 nsIDOMEventTarget_Release(target);
345 nsIDOMNode_Release(elem);
346 return NS_OK;
349 nsres = nsIDOMEventTarget_QueryInterface(target, &IID_nsIDOMComment, (void**)&nscomment);
350 if(NS_SUCCEEDED(nsres)) {
351 nsAString comment_str;
352 BOOL remove_comment = FALSE;
354 nsAString_Init(&comment_str, NULL);
355 nsres = nsIDOMComment_GetData(nscomment, &comment_str);
356 if(NS_SUCCEEDED(nsres)) {
357 const PRUnichar *comment;
359 nsAString_GetData(&comment_str, &comment);
360 remove_comment = handle_insert_comment(This->doc, comment);
363 nsAString_Finish(&comment_str);
365 if(remove_comment) {
366 nsIDOMNode *nsparent, *tmp;
368 nsIDOMComment_GetParentNode(nscomment, &nsparent);
369 nsIDOMNode_RemoveChild(nsparent, (nsIDOMNode*)nscomment, &tmp);
370 nsIDOMNode_Release(nsparent);
371 nsIDOMNode_Release(tmp);
374 nsIDOMComment_Release(nscomment);
377 return NS_OK;
380 static nsresult NSAPI handle_htmlevent(nsIDOMEventListener *iface, nsIDOMEvent *event)
382 NSContainer *This = NSEVENTLIST_THIS(iface)->This;
383 const PRUnichar *type;
384 nsIDOMEventTarget *event_target;
385 nsIDOMNode *nsnode;
386 nsAString type_str;
387 eventid_t eid;
388 nsresult nsres;
390 nsAString_Init(&type_str, NULL);
391 nsIDOMEvent_GetType(event, &type_str);
392 nsAString_GetData(&type_str, &type);
393 eid = str_to_eid(type);
394 nsAString_Finish(&type_str);
396 nsres = nsIDOMEvent_GetTarget(event, &event_target);
397 if(NS_FAILED(nsres) || !event_target) {
398 ERR("GetEventTarget failed: %08x\n", nsres);
399 return NS_OK;
402 nsres = nsIDOMEventTarget_QueryInterface(event_target, &IID_nsIDOMNode, (void**)&nsnode);
403 nsIDOMEventTarget_Release(event_target);
404 if(NS_FAILED(nsres)) {
405 ERR("Could not get nsIDOMNode: %08x\n", nsres);
406 return NS_OK;
409 fire_event(This->doc, eid, nsnode);
411 nsIDOMNode_Release(nsnode);
413 return NS_OK;
416 #undef NSEVENTLIST_THIS
418 #define EVENTLISTENER_VTBL(handler) \
420 nsDOMEventListener_QueryInterface, \
421 nsDOMEventListener_AddRef, \
422 nsDOMEventListener_Release, \
423 handler, \
426 static const nsIDOMEventListenerVtbl blur_vtbl = EVENTLISTENER_VTBL(handle_blur);
427 static const nsIDOMEventListenerVtbl focus_vtbl = EVENTLISTENER_VTBL(handle_focus);
428 static const nsIDOMEventListenerVtbl keypress_vtbl = EVENTLISTENER_VTBL(handle_keypress);
429 static const nsIDOMEventListenerVtbl load_vtbl = EVENTLISTENER_VTBL(handle_load);
430 static const nsIDOMEventListenerVtbl node_insert_vtbl = EVENTLISTENER_VTBL(handle_node_insert);
431 static const nsIDOMEventListenerVtbl htmlevent_vtbl = EVENTLISTENER_VTBL(handle_htmlevent);
433 static void init_event(nsIDOMEventTarget *target, const PRUnichar *type,
434 nsIDOMEventListener *listener, BOOL capture)
436 nsAString type_str;
437 nsresult nsres;
439 nsAString_Init(&type_str, type);
440 nsres = nsIDOMEventTarget_AddEventListener(target, &type_str, listener, capture);
441 nsAString_Finish(&type_str);
442 if(NS_FAILED(nsres))
443 ERR("AddEventTarget failed: %08x\n", nsres);
447 static void init_listener(nsEventListener *This, NSContainer *container,
448 const nsIDOMEventListenerVtbl *vtbl)
450 This->lpDOMEventListenerVtbl = vtbl;
451 This->This = container;
454 void add_nsevent_listener(NSContainer *container, LPCWSTR type)
456 nsIDOMWindow *dom_window;
457 nsIDOMEventTarget *target;
458 nsresult nsres;
460 nsres = nsIWebBrowser_GetContentDOMWindow(container->webbrowser, &dom_window);
461 if(NS_FAILED(nsres)) {
462 ERR("GetContentDOMWindow failed: %08x\n", nsres);
463 return;
466 nsres = nsIDOMWindow_QueryInterface(dom_window, &IID_nsIDOMEventTarget, (void**)&target);
467 nsIDOMWindow_Release(dom_window);
468 if(NS_FAILED(nsres)) {
469 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
470 return;
473 init_event(target, type, NSEVENTLIST(&container->htmlevent_listener), TRUE);
474 nsIDOMEventTarget_Release(target);
477 void init_nsevents(NSContainer *This)
479 nsIDOMWindow *dom_window;
480 nsIDOMEventTarget *target;
481 nsresult nsres;
483 static const PRUnichar wsz_blur[] = {'b','l','u','r',0};
484 static const PRUnichar wsz_focus[] = {'f','o','c','u','s',0};
485 static const PRUnichar wsz_keypress[] = {'k','e','y','p','r','e','s','s',0};
486 static const PRUnichar wsz_load[] = {'l','o','a','d',0};
487 static const PRUnichar DOMNodeInsertedW[] =
488 {'D','O','M','N','o','d','e','I','n','s','e','r','t','e','d',0};
490 init_listener(&This->blur_listener, This, &blur_vtbl);
491 init_listener(&This->focus_listener, This, &focus_vtbl);
492 init_listener(&This->keypress_listener, This, &keypress_vtbl);
493 init_listener(&This->load_listener, This, &load_vtbl);
494 init_listener(&This->node_insert_listener, This, &node_insert_vtbl);
495 init_listener(&This->htmlevent_listener, This, &htmlevent_vtbl);
497 nsres = nsIWebBrowser_GetContentDOMWindow(This->webbrowser, &dom_window);
498 if(NS_FAILED(nsres)) {
499 ERR("GetContentDOMWindow failed: %08x\n", nsres);
500 return;
503 nsres = nsIDOMWindow_QueryInterface(dom_window, &IID_nsIDOMEventTarget, (void**)&target);
504 nsIDOMWindow_Release(dom_window);
505 if(NS_FAILED(nsres)) {
506 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
507 return;
510 init_event(target, wsz_blur, NSEVENTLIST(&This->blur_listener), TRUE);
511 init_event(target, wsz_focus, NSEVENTLIST(&This->focus_listener), TRUE);
512 init_event(target, wsz_keypress, NSEVENTLIST(&This->keypress_listener), FALSE);
513 init_event(target, wsz_load, NSEVENTLIST(&This->load_listener), TRUE);
514 init_event(target, DOMNodeInsertedW,NSEVENTLIST(&This->node_insert_listener),TRUE);
516 nsIDOMEventTarget_Release(target);