gphoto2.ds: Set supported groups.
[wine.git] / dlls / mshtml / htmlscript.c
blob49d82aa93c43517c217b8b0647cc351e17fc5aa9
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>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
32 #include "htmlevent.h"
33 #include "htmlscript.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37 static inline HTMLScriptElement *impl_from_IHTMLScriptElement(IHTMLScriptElement *iface)
39 return CONTAINING_RECORD(iface, HTMLScriptElement, IHTMLScriptElement_iface);
42 static HRESULT WINAPI HTMLScriptElement_QueryInterface(IHTMLScriptElement *iface,
43 REFIID riid, void **ppv)
45 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
47 return IHTMLDOMNode_QueryInterface(&This->element.node.IHTMLDOMNode_iface, riid, ppv);
50 static ULONG WINAPI HTMLScriptElement_AddRef(IHTMLScriptElement *iface)
52 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
54 return IHTMLDOMNode_AddRef(&This->element.node.IHTMLDOMNode_iface);
57 static ULONG WINAPI HTMLScriptElement_Release(IHTMLScriptElement *iface)
59 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
61 return IHTMLDOMNode_Release(&This->element.node.IHTMLDOMNode_iface);
64 static HRESULT WINAPI HTMLScriptElement_GetTypeInfoCount(IHTMLScriptElement *iface, UINT *pctinfo)
66 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
67 return IDispatchEx_GetTypeInfoCount(&This->element.node.event_target.dispex.IDispatchEx_iface, pctinfo);
70 static HRESULT WINAPI HTMLScriptElement_GetTypeInfo(IHTMLScriptElement *iface, UINT iTInfo,
71 LCID lcid, ITypeInfo **ppTInfo)
73 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
74 return IDispatchEx_GetTypeInfo(&This->element.node.event_target.dispex.IDispatchEx_iface, iTInfo, lcid,
75 ppTInfo);
78 static HRESULT WINAPI HTMLScriptElement_GetIDsOfNames(IHTMLScriptElement *iface, REFIID riid,
79 LPOLESTR *rgszNames, UINT cNames,
80 LCID lcid, DISPID *rgDispId)
82 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
83 return IDispatchEx_GetIDsOfNames(&This->element.node.event_target.dispex.IDispatchEx_iface, riid, rgszNames,
84 cNames, lcid, rgDispId);
87 static HRESULT WINAPI HTMLScriptElement_Invoke(IHTMLScriptElement *iface, DISPID dispIdMember,
88 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
89 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
91 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
92 return IDispatchEx_Invoke(&This->element.node.event_target.dispex.IDispatchEx_iface, dispIdMember, riid,
93 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
96 static HRESULT WINAPI HTMLScriptElement_put_src(IHTMLScriptElement *iface, BSTR v)
98 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
99 nsAString src_str;
100 nsresult nsres;
101 HRESULT hres;
103 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
105 nsAString_InitDepend(&src_str, v);
106 nsres = nsIDOMHTMLScriptElement_SetSrc(This->nsscript, &src_str);
107 nsAString_Finish(&src_str);
108 if(NS_FAILED(nsres)) {
109 ERR("SetSrc failed: %08x\n", nsres);
110 return E_FAIL;
113 if(This->parsed) {
114 WARN("already parsed\n");
115 return S_OK;
118 if(This->binding) {
119 FIXME("binding in progress\n");
120 return E_FAIL;
123 nsAString_Init(&src_str, NULL);
124 nsres = nsIDOMHTMLScriptElement_GetSrc(This->nsscript, &src_str);
125 if(NS_SUCCEEDED(nsres)) {
126 const PRUnichar *src;
127 nsAString_GetData(&src_str, &src);
128 hres = load_script(This, src, TRUE);
129 }else {
130 ERR("SetSrc failed: %08x\n", nsres);
131 hres = E_FAIL;
133 nsAString_Finish(&src_str);
134 return hres;
137 static HRESULT WINAPI HTMLScriptElement_get_src(IHTMLScriptElement *iface, BSTR *p)
139 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
140 nsAString src_str;
141 nsresult nsres;
143 TRACE("(%p)->(%p)\n", This, p);
145 nsAString_Init(&src_str, NULL);
146 nsres = nsIDOMHTMLScriptElement_GetSrc(This->nsscript, &src_str);
147 return return_nsstr(nsres, &src_str, p);
150 static HRESULT WINAPI HTMLScriptElement_put_htmlFor(IHTMLScriptElement *iface, BSTR v)
152 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
153 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
154 return E_NOTIMPL;
157 static HRESULT WINAPI HTMLScriptElement_get_htmlFor(IHTMLScriptElement *iface, BSTR *p)
159 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
160 FIXME("(%p)->(%p)\n", This, p);
161 return E_NOTIMPL;
164 static HRESULT WINAPI HTMLScriptElement_put_event(IHTMLScriptElement *iface, BSTR v)
166 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
167 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
168 return E_NOTIMPL;
171 static HRESULT WINAPI HTMLScriptElement_get_event(IHTMLScriptElement *iface, BSTR *p)
173 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
174 FIXME("(%p)->(%p)\n", This, p);
175 return E_NOTIMPL;
178 static HRESULT WINAPI HTMLScriptElement_put_text(IHTMLScriptElement *iface, BSTR v)
180 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
181 HTMLInnerWindow *window;
182 nsIDOMNode *parent;
183 nsAString text_str;
184 nsresult nsres;
186 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
188 if(!This->element.node.doc || !This->element.node.doc->window) {
189 WARN("no windoow\n");
190 return E_UNEXPECTED;
193 window = This->element.node.doc->window;
195 nsAString_InitDepend(&text_str, v);
196 nsres = nsIDOMHTMLScriptElement_SetText(This->nsscript, &text_str);
197 nsAString_Finish(&text_str);
198 if(NS_FAILED(nsres)) {
199 ERR("SetSrc failed: %08x\n", nsres);
200 return E_FAIL;
203 nsres = nsIDOMHTMLElement_GetParentNode(This->element.nselem, &parent);
204 if(NS_FAILED(nsres) || !parent) {
205 TRACE("No parent, not executing\n");
206 This->parse_on_bind = TRUE;
207 return S_OK;
210 nsIDOMNode_Release(parent);
211 doc_insert_script(window, This, FALSE);
212 return S_OK;
215 static HRESULT WINAPI HTMLScriptElement_get_text(IHTMLScriptElement *iface, BSTR *p)
217 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
218 nsAString nsstr;
219 nsresult nsres;
221 TRACE("(%p)->(%p)\n", This, p);
223 nsAString_Init(&nsstr, NULL);
224 nsres = nsIDOMHTMLScriptElement_GetText(This->nsscript, &nsstr);
225 return return_nsstr(nsres, &nsstr, p);
228 static HRESULT WINAPI HTMLScriptElement_put_defer(IHTMLScriptElement *iface, VARIANT_BOOL v)
230 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
231 HRESULT hr = S_OK;
232 nsresult nsres;
234 TRACE("(%p)->(%x)\n", This, v);
236 nsres = nsIDOMHTMLScriptElement_SetDefer(This->nsscript, v != VARIANT_FALSE);
237 if(NS_FAILED(nsres))
239 hr = E_FAIL;
242 return hr;
245 static HRESULT WINAPI HTMLScriptElement_get_defer(IHTMLScriptElement *iface, VARIANT_BOOL *p)
247 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
248 cpp_bool defer = FALSE;
249 nsresult nsres;
251 TRACE("(%p)->(%p)\n", This, p);
253 if(!p)
254 return E_INVALIDARG;
256 nsres = nsIDOMHTMLScriptElement_GetDefer(This->nsscript, &defer);
257 if(NS_FAILED(nsres)) {
258 ERR("GetSrc failed: %08x\n", nsres);
261 *p = variant_bool(defer);
262 return S_OK;
265 static HRESULT WINAPI HTMLScriptElement_get_readyState(IHTMLScriptElement *iface, BSTR *p)
267 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
269 TRACE("(%p)->(%p)\n", This, p);
271 return get_readystate_string(This->readystate, p);
274 static HRESULT WINAPI HTMLScriptElement_put_onerror(IHTMLScriptElement *iface, VARIANT v)
276 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
278 FIXME("(%p)->(%s) semi-stub\n", This, debugstr_variant(&v));
280 return set_node_event(&This->element.node, EVENTID_ERROR, &v);
283 static HRESULT WINAPI HTMLScriptElement_get_onerror(IHTMLScriptElement *iface, VARIANT *p)
285 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
287 TRACE("(%p)->(%p)\n", This, p);
289 return get_node_event(&This->element.node, EVENTID_ERROR, p);
292 static HRESULT WINAPI HTMLScriptElement_put_type(IHTMLScriptElement *iface, BSTR v)
294 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
295 nsAString nstype_str;
296 nsresult nsres;
298 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
300 nsAString_Init(&nstype_str, v);
301 nsres = nsIDOMHTMLScriptElement_SetType(This->nsscript, &nstype_str);
302 if (NS_FAILED(nsres))
303 ERR("SetType failed: %08x\n", nsres);
304 nsAString_Finish (&nstype_str);
306 return S_OK;
309 static HRESULT WINAPI HTMLScriptElement_get_type(IHTMLScriptElement *iface, BSTR *p)
311 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
312 nsAString nstype_str;
313 nsresult nsres;
315 TRACE("(%p)->(%p)\n", This, p);
317 nsAString_Init(&nstype_str, NULL);
318 nsres = nsIDOMHTMLScriptElement_GetType(This->nsscript, &nstype_str);
319 return return_nsstr(nsres, &nstype_str, p);
322 static const IHTMLScriptElementVtbl HTMLScriptElementVtbl = {
323 HTMLScriptElement_QueryInterface,
324 HTMLScriptElement_AddRef,
325 HTMLScriptElement_Release,
326 HTMLScriptElement_GetTypeInfoCount,
327 HTMLScriptElement_GetTypeInfo,
328 HTMLScriptElement_GetIDsOfNames,
329 HTMLScriptElement_Invoke,
330 HTMLScriptElement_put_src,
331 HTMLScriptElement_get_src,
332 HTMLScriptElement_put_htmlFor,
333 HTMLScriptElement_get_htmlFor,
334 HTMLScriptElement_put_event,
335 HTMLScriptElement_get_event,
336 HTMLScriptElement_put_text,
337 HTMLScriptElement_get_text,
338 HTMLScriptElement_put_defer,
339 HTMLScriptElement_get_defer,
340 HTMLScriptElement_get_readyState,
341 HTMLScriptElement_put_onerror,
342 HTMLScriptElement_get_onerror,
343 HTMLScriptElement_put_type,
344 HTMLScriptElement_get_type
347 static inline HTMLScriptElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
349 return CONTAINING_RECORD(iface, HTMLScriptElement, element.node);
352 static HRESULT HTMLScriptElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
354 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
356 *ppv = NULL;
358 if(IsEqualGUID(&IID_IUnknown, riid)) {
359 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
360 *ppv = &This->IHTMLScriptElement_iface;
361 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
362 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
363 *ppv = &This->IHTMLScriptElement_iface;
364 }else if(IsEqualGUID(&IID_IHTMLScriptElement, riid)) {
365 TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This, ppv);
366 *ppv = &This->IHTMLScriptElement_iface;
369 if(*ppv) {
370 IUnknown_AddRef((IUnknown*)*ppv);
371 return S_OK;
374 return HTMLElement_QI(&This->element.node, riid, ppv);
377 static void HTMLScriptElement_destructor(HTMLDOMNode *iface)
379 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
380 heap_free(This->src_text);
381 HTMLElement_destructor(&This->element.node);
384 static HRESULT HTMLScriptElement_get_readystate(HTMLDOMNode *iface, BSTR *p)
386 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
388 return IHTMLScriptElement_get_readyState(&This->IHTMLScriptElement_iface, p);
391 static HRESULT HTMLScriptElement_bind_to_tree(HTMLDOMNode *iface)
393 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
395 TRACE("(%p)\n", This);
397 if(!This->parse_on_bind)
398 return S_OK;
400 if(!This->element.node.doc || !This->element.node.doc->window) {
401 ERR("No window\n");
402 return E_UNEXPECTED;
405 This->parse_on_bind = FALSE;
406 doc_insert_script(This->element.node.doc->window, This, FALSE);
407 return S_OK;
410 static void HTMLScriptElement_traverse(HTMLDOMNode *iface, nsCycleCollectionTraversalCallback *cb)
412 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
414 if(This->nsscript)
415 note_cc_edge((nsISupports*)This->nsscript, "This->nsscript", cb);
418 static void HTMLScriptElement_unlink(HTMLDOMNode *iface)
420 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
422 if(This->nsscript) {
423 nsIDOMHTMLScriptElement *nsscript = This->nsscript;
425 This->nsscript = NULL;
426 nsIDOMHTMLScriptElement_Release(nsscript);
430 static const NodeImplVtbl HTMLScriptElementImplVtbl = {
431 &CLSID_HTMLScriptElement,
432 HTMLScriptElement_QI,
433 HTMLScriptElement_destructor,
434 HTMLElement_cpc,
435 HTMLElement_clone,
436 HTMLElement_handle_event,
437 HTMLElement_get_attr_col,
438 NULL,
439 NULL,
440 NULL,
441 NULL,
442 HTMLScriptElement_get_readystate,
443 NULL,
444 NULL,
445 HTMLScriptElement_bind_to_tree,
446 HTMLScriptElement_traverse,
447 HTMLScriptElement_unlink
450 HRESULT script_elem_from_nsscript(HTMLDocumentNode *doc, nsIDOMHTMLScriptElement *nsscript, HTMLScriptElement **ret)
452 nsIDOMNode *nsnode;
453 HTMLDOMNode *node;
454 nsresult nsres;
455 HRESULT hres;
457 nsres = nsIDOMHTMLScriptElement_QueryInterface(nsscript, &IID_nsIDOMNode, (void**)&nsnode);
458 assert(nsres == NS_OK);
460 hres = get_node(doc, nsnode, TRUE, &node);
461 nsIDOMNode_Release(nsnode);
462 if(FAILED(hres))
463 return hres;
465 assert(node->vtbl == &HTMLScriptElementImplVtbl);
466 *ret = impl_from_HTMLDOMNode(node);
467 return S_OK;
470 static const tid_t HTMLScriptElement_iface_tids[] = {
471 HTMLELEMENT_TIDS,
472 IHTMLScriptElement_tid,
476 static dispex_static_data_t HTMLScriptElement_dispex = {
477 NULL,
478 DispHTMLScriptElement_tid,
479 HTMLScriptElement_iface_tids,
480 HTMLElement_init_dispex_info
483 HRESULT HTMLScriptElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, HTMLElement **elem)
485 HTMLScriptElement *ret;
486 nsresult nsres;
488 ret = heap_alloc_zero(sizeof(HTMLScriptElement));
489 if(!ret)
490 return E_OUTOFMEMORY;
492 ret->IHTMLScriptElement_iface.lpVtbl = &HTMLScriptElementVtbl;
493 ret->element.node.vtbl = &HTMLScriptElementImplVtbl;
495 HTMLElement_Init(&ret->element, doc, nselem, &HTMLScriptElement_dispex);
497 nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLScriptElement, (void**)&ret->nsscript);
498 assert(nsres == NS_OK);
500 *elem = &ret->element;
501 return S_OK;