mshtml: Added IHTMLElement::put_onclick implementation.
[wine/gsoc_dplay.git] / dlls / mshtml / htmlevent.c
blobcfd6ebc1a9c82c58eedc6693574466d96a14987d
1 /*
2 * Copyright 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"
28 #include "mshtml_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
34 struct event_target_t {
35 IDispatch *event_table[EVENTID_LAST];
38 static const WCHAR clickW[] = {'c','l','i','c','k',0};
39 static const WCHAR onclickW[] = {'o','n','c','l','i','c','k',0};
41 static const WCHAR loadW[] = {'l','o','a','d',0};
42 static const WCHAR onloadW[] = {'o','n','l','o','a','d',0};
44 typedef struct {
45 LPCWSTR name;
46 LPCWSTR attr_name;
47 DWORD flags;
48 } event_info_t;
50 #define EVENT_DEFAULTLISTENER 0x0001
52 static const event_info_t event_info[] = {
53 {clickW, onclickW, EVENT_DEFAULTLISTENER},
54 {loadW, onloadW, 0}
57 eventid_t str_to_eid(LPCWSTR str)
59 int i;
61 for(i=0; i < sizeof(event_info)/sizeof(event_info[0]); i++) {
62 if(!strcmpW(event_info[i].name, str))
63 return i;
66 ERR("unknown type %s\n", debugstr_w(str));
67 return EVENTID_LAST;
70 typedef struct {
71 const IHTMLEventObjVtbl *lpIHTMLEventObjVtbl;
72 LONG ref;
73 } HTMLEventObj;
75 #define HTMLEVENTOBJ(x) ((IHTMLEventObj*) &(x)->lpIHTMLEventObjVtbl)
77 #define HTMLEVENTOBJ_THIS(iface) DEFINE_THIS(HTMLEventObj, IHTMLEventObj, iface)
79 static HRESULT WINAPI HTMLEventObj_QueryInterface(IHTMLEventObj *iface, REFIID riid, void **ppv)
81 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
83 *ppv = NULL;
85 if(IsEqualGUID(&IID_IUnknown, riid)) {
86 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
87 *ppv = HTMLEVENTOBJ(This);
88 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
89 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
90 *ppv = HTMLEVENTOBJ(This);
91 }else if(IsEqualGUID(&IID_IHTMLEventObj, riid)) {
92 TRACE("(%p)->(IID_IHTMLEventObj %p)\n", This, ppv);
93 *ppv = HTMLEVENTOBJ(This);
96 if(*ppv) {
97 IUnknown_AddRef((IUnknown*)*ppv);
98 return S_OK;
101 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
102 return E_NOINTERFACE;
105 static ULONG WINAPI HTMLEventObj_AddRef(IHTMLEventObj *iface)
107 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
108 LONG ref = InterlockedIncrement(&This->ref);
110 TRACE("(%p) ref=%d\n", This, ref);
112 return ref;
115 static ULONG WINAPI HTMLEventObj_Release(IHTMLEventObj *iface)
117 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
118 LONG ref = InterlockedDecrement(&This->ref);
120 TRACE("(%p) ref=%d\n", This, ref);
122 if(!ref)
123 heap_free(This);
125 return ref;
128 static HRESULT WINAPI HTMLEventObj_GetTypeInfoCount(IHTMLEventObj *iface, UINT *pctinfo)
130 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
131 FIXME("(%p)->(%p)\n", This, pctinfo);
132 return E_NOTIMPL;
135 static HRESULT WINAPI HTMLEventObj_GetTypeInfo(IHTMLEventObj *iface, UINT iTInfo,
136 LCID lcid, ITypeInfo **ppTInfo)
138 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
139 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
140 return E_NOTIMPL;
143 static HRESULT WINAPI HTMLEventObj_GetIDsOfNames(IHTMLEventObj *iface, REFIID riid,
144 LPOLESTR *rgszNames, UINT cNames,
145 LCID lcid, DISPID *rgDispId)
147 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
148 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
149 lcid, rgDispId);
150 return E_NOTIMPL;
153 static HRESULT WINAPI HTMLEventObj_Invoke(IHTMLEventObj *iface, DISPID dispIdMember,
154 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
155 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
157 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
158 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
159 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
160 return E_NOTIMPL;
163 static HRESULT WINAPI HTMLEventObj_get_srcElement(IHTMLEventObj *iface, IHTMLElement **p)
165 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
166 FIXME("(%p)->(%p)\n", This, p);
167 return E_NOTIMPL;
170 static HRESULT WINAPI HTMLEventObj_get_altKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
172 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
173 FIXME("(%p)->(%p)\n", This, p);
174 return E_NOTIMPL;
177 static HRESULT WINAPI HTMLEventObj_get_ctrlKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
179 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
180 FIXME("(%p)->(%p)\n", This, p);
181 return E_NOTIMPL;
184 static HRESULT WINAPI HTMLEventObj_get_shiftKey(IHTMLEventObj *iface, VARIANT_BOOL *p)
186 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
187 FIXME("(%p)->(%p)\n", This, p);
188 return E_NOTIMPL;
191 static HRESULT WINAPI HTMLEventObj_put_returnValue(IHTMLEventObj *iface, VARIANT v)
193 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
194 FIXME("(%p)->()\n", This);
195 return E_NOTIMPL;
198 static HRESULT WINAPI HTMLEventObj_get_returnValue(IHTMLEventObj *iface, VARIANT *p)
200 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
201 FIXME("(%p)->(%p)\n", This, p);
202 return E_NOTIMPL;
205 static HRESULT WINAPI HTMLEventObj_put_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL v)
207 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
208 FIXME("(%p)->(%x)\n", This, v);
209 return E_NOTIMPL;
212 static HRESULT WINAPI HTMLEventObj_get_cancelBubble(IHTMLEventObj *iface, VARIANT_BOOL *p)
214 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
215 FIXME("(%p)->(%p)\n", This, p);
216 return E_NOTIMPL;
219 static HRESULT WINAPI HTMLEventObj_get_fromElement(IHTMLEventObj *iface, IHTMLElement **p)
221 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
222 FIXME("(%p)->(%p)\n", This, p);
223 return E_NOTIMPL;
226 static HRESULT WINAPI HTMLEventObj_get_toElement(IHTMLEventObj *iface, IHTMLElement **p)
228 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
229 FIXME("(%p)->(%p)\n", This, p);
230 return E_NOTIMPL;
233 static HRESULT WINAPI HTMLEventObj_put_keyCode(IHTMLEventObj *iface, long v)
235 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
236 FIXME("(%p)->(%ld)\n", This, v);
237 return E_NOTIMPL;
240 static HRESULT WINAPI HTMLEventObj_get_keyCode(IHTMLEventObj *iface, long *p)
242 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
243 FIXME("(%p)->(%p)\n", This, p);
244 return E_NOTIMPL;
247 static HRESULT WINAPI HTMLEventObj_get_button(IHTMLEventObj *iface, long *p)
249 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
250 FIXME("(%p)->(%p)\n", This, p);
251 return E_NOTIMPL;
254 static HRESULT WINAPI HTMLEventObj_get_type(IHTMLEventObj *iface, BSTR *p)
256 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
257 FIXME("(%p)->(%p)\n", This, p);
258 return E_NOTIMPL;
261 static HRESULT WINAPI HTMLEventObj_get_qualifier(IHTMLEventObj *iface, BSTR *p)
263 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
264 FIXME("(%p)->(%p)\n", This, p);
265 return E_NOTIMPL;
268 static HRESULT WINAPI HTMLEventObj_get_reason(IHTMLEventObj *iface, long *p)
270 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
271 FIXME("(%p)->(%p)\n", This, p);
272 return E_NOTIMPL;
275 static HRESULT WINAPI HTMLEventObj_get_x(IHTMLEventObj *iface, long *p)
277 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
278 FIXME("(%p)->(%p)\n", This, p);
279 return E_NOTIMPL;
282 static HRESULT WINAPI HTMLEventObj_get_y(IHTMLEventObj *iface, long *p)
284 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
285 FIXME("(%p)->(%p)\n", This, p);
286 return E_NOTIMPL;
289 static HRESULT WINAPI HTMLEventObj_get_clientX(IHTMLEventObj *iface, long *p)
291 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
292 FIXME("(%p)->(%p)\n", This, p);
293 return E_NOTIMPL;
296 static HRESULT WINAPI HTMLEventObj_get_clientY(IHTMLEventObj *iface, long *p)
298 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
299 FIXME("(%p)->(%p)\n", This, p);
300 return E_NOTIMPL;
303 static HRESULT WINAPI HTMLEventObj_get_offsetX(IHTMLEventObj *iface, long *p)
305 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
306 FIXME("(%p)->(%p)\n", This, p);
307 return E_NOTIMPL;
310 static HRESULT WINAPI HTMLEventObj_get_offsetY(IHTMLEventObj *iface, long *p)
312 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
313 FIXME("(%p)->(%p)\n", This, p);
314 return E_NOTIMPL;
317 static HRESULT WINAPI HTMLEventObj_get_screenX(IHTMLEventObj *iface, long *p)
319 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
320 FIXME("(%p)->(%p)\n", This, p);
321 return E_NOTIMPL;
324 static HRESULT WINAPI HTMLEventObj_get_screenY(IHTMLEventObj *iface, long *p)
326 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
327 FIXME("(%p)->(%p)\n", This, p);
328 return E_NOTIMPL;
331 static HRESULT WINAPI HTMLEventObj_get_srcFilter(IHTMLEventObj *iface, IDispatch **p)
333 HTMLEventObj *This = HTMLEVENTOBJ_THIS(iface);
334 FIXME("(%p)->(%p)\n", This, p);
335 return E_NOTIMPL;
338 #undef HTMLEVENTOBJ_THIS
340 static const IHTMLEventObjVtbl HTMLEventObjVtbl = {
341 HTMLEventObj_QueryInterface,
342 HTMLEventObj_AddRef,
343 HTMLEventObj_Release,
344 HTMLEventObj_GetTypeInfoCount,
345 HTMLEventObj_GetTypeInfo,
346 HTMLEventObj_GetIDsOfNames,
347 HTMLEventObj_Invoke,
348 HTMLEventObj_get_srcElement,
349 HTMLEventObj_get_altKey,
350 HTMLEventObj_get_ctrlKey,
351 HTMLEventObj_get_shiftKey,
352 HTMLEventObj_put_returnValue,
353 HTMLEventObj_get_returnValue,
354 HTMLEventObj_put_cancelBubble,
355 HTMLEventObj_get_cancelBubble,
356 HTMLEventObj_get_fromElement,
357 HTMLEventObj_get_toElement,
358 HTMLEventObj_put_keyCode,
359 HTMLEventObj_get_keyCode,
360 HTMLEventObj_get_button,
361 HTMLEventObj_get_type,
362 HTMLEventObj_get_qualifier,
363 HTMLEventObj_get_reason,
364 HTMLEventObj_get_x,
365 HTMLEventObj_get_y,
366 HTMLEventObj_get_clientX,
367 HTMLEventObj_get_clientY,
368 HTMLEventObj_get_offsetX,
369 HTMLEventObj_get_offsetY,
370 HTMLEventObj_get_screenX,
371 HTMLEventObj_get_screenY,
372 HTMLEventObj_get_srcFilter
375 static IHTMLEventObj *create_event(void)
377 HTMLEventObj *ret;
379 ret = heap_alloc(sizeof(*ret));
380 ret->lpIHTMLEventObjVtbl = &HTMLEventObjVtbl;
381 ret->ref = 1;
383 return HTMLEVENTOBJ(ret);
386 void fire_event(HTMLDocument *doc, eventid_t eid, nsIDOMNode *target)
388 HTMLDOMNode *node;
390 node = get_node(doc, target, FALSE);
391 if(!node)
392 return;
394 if(node->event_target && node->event_target->event_table[eid]) {
395 doc->window->event = create_event();
397 call_disp_func(doc, node->event_target->event_table[eid]);
399 IHTMLEventObj_Release(doc->window->event);
400 doc->window->event = NULL;
404 static HRESULT set_node_event_disp(HTMLDOMNode *node, eventid_t eid, IDispatch *disp)
406 if(!node->event_target)
407 node->event_target = heap_alloc_zero(sizeof(event_target_t));
408 else if(node->event_target->event_table[eid])
409 IDispatch_Release(node->event_target->event_table[eid]);
411 IDispatch_AddRef(disp);
412 node->event_target->event_table[eid] = disp;
414 if((event_info[eid].flags & EVENT_DEFAULTLISTENER) && !node->doc->nscontainer->event_vector[eid]) {
415 node->doc->nscontainer->event_vector[eid] = TRUE;
416 add_nsevent_listener(node->doc->nscontainer, event_info[eid].name);
419 return S_OK;
422 HRESULT set_node_event(HTMLDOMNode *node, eventid_t eid, VARIANT *var)
424 switch(V_VT(var)) {
425 case VT_DISPATCH:
426 return set_node_event_disp(node, eid, V_DISPATCH(var));
428 default:
429 FIXME("not supported vt=%d\n", V_VT(var));
432 return E_NOTIMPL;
435 void check_event_attr(HTMLDocument *doc, nsIDOMElement *nselem)
437 const PRUnichar *attr_value;
438 nsAString attr_name_str, attr_value_str;
439 IDispatch *disp;
440 HTMLDOMNode *node;
441 int i;
442 nsresult nsres;
444 nsAString_Init(&attr_value_str, NULL);
445 nsAString_Init(&attr_name_str, NULL);
447 for(i=0; i < EVENTID_LAST; i++) {
448 nsAString_SetData(&attr_name_str, event_info[i].attr_name);
449 nsres = nsIDOMElement_GetAttribute(nselem, &attr_name_str, &attr_value_str);
450 if(NS_SUCCEEDED(nsres)) {
451 nsAString_GetData(&attr_value_str, &attr_value);
452 if(!*attr_value)
453 continue;
455 TRACE("%p.%s = %s\n", nselem, debugstr_w(event_info[i].attr_name), debugstr_w(attr_value));
457 disp = script_parse_event(doc, attr_value);
458 if(disp) {
459 node = get_node(doc, (nsIDOMNode*)nselem, TRUE);
460 set_node_event_disp(node, i, disp);
461 IDispatch_Release(disp);
466 nsAString_Finish(&attr_value_str);
467 nsAString_Finish(&attr_name_str);
470 void release_event_target(event_target_t *event_target)
472 int i;
474 for(i=0; i < EVENTID_LAST; i++) {
475 if(event_target->event_table[i])
476 IDispatch_Release(event_target->event_table[i]);
479 heap_free(event_target);