msvcrt: Added unicode modes support _setmode function.
[wine] / dlls / mshtml / htmlscript.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdarg.h>
20 #include <assert.h>
21
22 #define COBJMACROS
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
28
29 #include "wine/debug.h"
30
31 #include "mshtml_private.h"
32 #include "htmlscript.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35
36 static inline HTMLScriptElement *impl_from_IHTMLScriptElement(IHTMLScriptElement *iface)
37 {
38     return CONTAINING_RECORD(iface, HTMLScriptElement, IHTMLScriptElement_iface);
39 }
40
41 static HRESULT WINAPI HTMLScriptElement_QueryInterface(IHTMLScriptElement *iface,
42         REFIID riid, void **ppv)
43 {
44     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
45
46     return IHTMLDOMNode_QueryInterface(&This->element.node.IHTMLDOMNode_iface, riid, ppv);
47 }
48
49 static ULONG WINAPI HTMLScriptElement_AddRef(IHTMLScriptElement *iface)
50 {
51     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
52
53     return IHTMLDOMNode_AddRef(&This->element.node.IHTMLDOMNode_iface);
54 }
55
56 static ULONG WINAPI HTMLScriptElement_Release(IHTMLScriptElement *iface)
57 {
58     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
59
60     return IHTMLDOMNode_Release(&This->element.node.IHTMLDOMNode_iface);
61 }
62
63 static HRESULT WINAPI HTMLScriptElement_GetTypeInfoCount(IHTMLScriptElement *iface, UINT *pctinfo)
64 {
65     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
66     return IDispatchEx_GetTypeInfoCount(&This->element.node.dispex.IDispatchEx_iface, pctinfo);
67 }
68
69 static HRESULT WINAPI HTMLScriptElement_GetTypeInfo(IHTMLScriptElement *iface, UINT iTInfo,
70                                               LCID lcid, ITypeInfo **ppTInfo)
71 {
72     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
73     return IDispatchEx_GetTypeInfo(&This->element.node.dispex.IDispatchEx_iface, iTInfo, lcid,
74             ppTInfo);
75 }
76
77 static HRESULT WINAPI HTMLScriptElement_GetIDsOfNames(IHTMLScriptElement *iface, REFIID riid,
78                                                 LPOLESTR *rgszNames, UINT cNames,
79                                                 LCID lcid, DISPID *rgDispId)
80 {
81     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
82     return IDispatchEx_GetIDsOfNames(&This->element.node.dispex.IDispatchEx_iface, riid, rgszNames,
83             cNames, lcid, rgDispId);
84 }
85
86 static HRESULT WINAPI HTMLScriptElement_Invoke(IHTMLScriptElement *iface, DISPID dispIdMember,
87                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
88                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
89 {
90     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
91     return IDispatchEx_Invoke(&This->element.node.dispex.IDispatchEx_iface, dispIdMember, riid,
92             lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
93 }
94
95 static HRESULT WINAPI HTMLScriptElement_put_src(IHTMLScriptElement *iface, BSTR v)
96 {
97     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
98     HTMLInnerWindow *window;
99     nsIDOMNode *parent;
100     nsAString src_str;
101     nsresult nsres;
102
103     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
104
105     if(!This->element.node.doc || !This->element.node.doc->window) {
106         WARN("no windoow\n");
107         return E_UNEXPECTED;
108     }
109
110     window = This->element.node.doc->window;
111
112     nsAString_InitDepend(&src_str, v);
113     nsres = nsIDOMHTMLScriptElement_SetSrc(This->nsscript, &src_str);
114     nsAString_Finish(&src_str);
115     if(NS_FAILED(nsres)) {
116         ERR("SetSrc failed: %08x\n", nsres);
117         return E_FAIL;
118     }
119
120     if(This->parsed) {
121         WARN("already parsed\n");
122         return S_OK;
123     }
124
125     if(window->parser_callback_cnt) {
126         script_queue_entry_t *queue;
127
128         queue = heap_alloc(sizeof(*queue));
129         if(!queue)
130             return E_OUTOFMEMORY;
131
132         IHTMLScriptElement_AddRef(&This->IHTMLScriptElement_iface);
133         queue->script = This;
134
135         list_add_tail(&window->script_queue, &queue->entry);
136         return S_OK;
137     }
138
139     nsres = nsIDOMHTMLScriptElement_GetParentNode(This->nsscript, &parent);
140     if(NS_FAILED(nsres) || !parent) {
141         TRACE("No parent, not executing\n");
142         This->parse_on_bind = TRUE;
143         return S_OK;
144     }
145
146     nsIDOMNode_Release(parent);
147     doc_insert_script(window, This);
148     return S_OK;
149 }
150
151 static HRESULT WINAPI HTMLScriptElement_get_src(IHTMLScriptElement *iface, BSTR *p)
152 {
153     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
154     nsAString src_str;
155     nsresult nsres;
156
157     TRACE("(%p)->(%p)\n", This, p);
158
159     nsAString_Init(&src_str, NULL);
160     nsres = nsIDOMHTMLScriptElement_GetSrc(This->nsscript, &src_str);
161     return return_nsstr(nsres, &src_str, p);
162 }
163
164 static HRESULT WINAPI HTMLScriptElement_put_htmlFor(IHTMLScriptElement *iface, BSTR v)
165 {
166     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
167     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
168     return E_NOTIMPL;
169 }
170
171 static HRESULT WINAPI HTMLScriptElement_get_htmlFor(IHTMLScriptElement *iface, BSTR *p)
172 {
173     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
174     FIXME("(%p)->(%p)\n", This, p);
175     return E_NOTIMPL;
176 }
177
178 static HRESULT WINAPI HTMLScriptElement_put_event(IHTMLScriptElement *iface, BSTR v)
179 {
180     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
181     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
182     return E_NOTIMPL;
183 }
184
185 static HRESULT WINAPI HTMLScriptElement_get_event(IHTMLScriptElement *iface, BSTR *p)
186 {
187     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
188     FIXME("(%p)->(%p)\n", This, p);
189     return E_NOTIMPL;
190 }
191
192 static HRESULT WINAPI HTMLScriptElement_put_text(IHTMLScriptElement *iface, BSTR v)
193 {
194     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
195     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
196     return E_NOTIMPL;
197 }
198
199 static HRESULT WINAPI HTMLScriptElement_get_text(IHTMLScriptElement *iface, BSTR *p)
200 {
201     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
202     nsAString nsstr;
203     nsresult nsres;
204
205     TRACE("(%p)->(%p)\n", This, p);
206
207     nsAString_Init(&nsstr, NULL);
208     nsres = nsIDOMHTMLScriptElement_GetText(This->nsscript, &nsstr);
209     return return_nsstr(nsres, &nsstr, p);
210 }
211
212 static HRESULT WINAPI HTMLScriptElement_put_defer(IHTMLScriptElement *iface, VARIANT_BOOL v)
213 {
214     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
215     HRESULT hr = S_OK;
216     nsresult nsres;
217
218     TRACE("(%p)->(%x)\n", This, v);
219
220     nsres = nsIDOMHTMLScriptElement_SetDefer(This->nsscript, v != VARIANT_FALSE);
221     if(NS_FAILED(nsres))
222     {
223         hr = E_FAIL;
224     }
225
226     return hr;
227 }
228
229 static HRESULT WINAPI HTMLScriptElement_get_defer(IHTMLScriptElement *iface, VARIANT_BOOL *p)
230 {
231     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
232     cpp_bool defer = FALSE;
233     nsresult nsres;
234
235     TRACE("(%p)->(%p)\n", This, p);
236
237     if(!p)
238         return E_INVALIDARG;
239
240     nsres = nsIDOMHTMLScriptElement_GetDefer(This->nsscript, &defer);
241     if(NS_FAILED(nsres)) {
242         ERR("GetSrc failed: %08x\n", nsres);
243     }
244
245     *p = defer ? VARIANT_TRUE : VARIANT_FALSE;
246
247     TRACE("*p = %d\n", *p);
248     return S_OK;
249 }
250
251 static HRESULT WINAPI HTMLScriptElement_get_readyState(IHTMLScriptElement *iface, BSTR *p)
252 {
253     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
254     FIXME("(%p)->(%p)\n", This, p);
255     return E_NOTIMPL;
256 }
257
258 static HRESULT WINAPI HTMLScriptElement_put_onerror(IHTMLScriptElement *iface, VARIANT v)
259 {
260     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
261     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
262     return E_NOTIMPL;
263 }
264
265 static HRESULT WINAPI HTMLScriptElement_get_onerror(IHTMLScriptElement *iface, VARIANT *p)
266 {
267     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
268     FIXME("(%p)->(%p)\n", This, p);
269     return E_NOTIMPL;
270 }
271
272 static HRESULT WINAPI HTMLScriptElement_put_type(IHTMLScriptElement *iface, BSTR v)
273 {
274     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
275     nsAString nstype_str;
276     nsresult nsres;
277
278     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
279
280     nsAString_Init(&nstype_str, v);
281     nsres = nsIDOMHTMLScriptElement_SetType(This->nsscript, &nstype_str);
282     if (NS_FAILED(nsres))
283         ERR("SetType failed: %08x\n", nsres);
284     nsAString_Finish (&nstype_str);
285
286     return S_OK;
287 }
288
289 static HRESULT WINAPI HTMLScriptElement_get_type(IHTMLScriptElement *iface, BSTR *p)
290 {
291     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
292     nsAString nstype_str;
293     nsresult nsres;
294
295     TRACE("(%p)->(%p)\n", This, p);
296
297     nsAString_Init(&nstype_str, NULL);
298     nsres = nsIDOMHTMLScriptElement_GetType(This->nsscript, &nstype_str);
299     return return_nsstr(nsres, &nstype_str, p);
300 }
301
302 static const IHTMLScriptElementVtbl HTMLScriptElementVtbl = {
303     HTMLScriptElement_QueryInterface,
304     HTMLScriptElement_AddRef,
305     HTMLScriptElement_Release,
306     HTMLScriptElement_GetTypeInfoCount,
307     HTMLScriptElement_GetTypeInfo,
308     HTMLScriptElement_GetIDsOfNames,
309     HTMLScriptElement_Invoke,
310     HTMLScriptElement_put_src,
311     HTMLScriptElement_get_src,
312     HTMLScriptElement_put_htmlFor,
313     HTMLScriptElement_get_htmlFor,
314     HTMLScriptElement_put_event,
315     HTMLScriptElement_get_event,
316     HTMLScriptElement_put_text,
317     HTMLScriptElement_get_text,
318     HTMLScriptElement_put_defer,
319     HTMLScriptElement_get_defer,
320     HTMLScriptElement_get_readyState,
321     HTMLScriptElement_put_onerror,
322     HTMLScriptElement_get_onerror,
323     HTMLScriptElement_put_type,
324     HTMLScriptElement_get_type
325 };
326
327 static inline HTMLScriptElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
328 {
329     return CONTAINING_RECORD(iface, HTMLScriptElement, element.node);
330 }
331
332 static HRESULT HTMLScriptElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
333 {
334     HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
335
336     *ppv = NULL;
337
338     if(IsEqualGUID(&IID_IUnknown, riid)) {
339         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
340         *ppv = &This->IHTMLScriptElement_iface;
341     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
342         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
343         *ppv = &This->IHTMLScriptElement_iface;
344     }else if(IsEqualGUID(&IID_IHTMLScriptElement, riid)) {
345         TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This, ppv);
346         *ppv = &This->IHTMLScriptElement_iface;
347     }
348
349     if(*ppv) {
350         IUnknown_AddRef((IUnknown*)*ppv);
351         return S_OK;
352     }
353
354     return HTMLElement_QI(&This->element.node, riid, ppv);
355 }
356
357 static HRESULT HTMLScriptElement_get_readystate(HTMLDOMNode *iface, BSTR *p)
358 {
359     HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
360
361     return IHTMLScriptElement_get_readyState(&This->IHTMLScriptElement_iface, p);
362 }
363
364 static const NodeImplVtbl HTMLScriptElementImplVtbl = {
365     HTMLScriptElement_QI,
366     HTMLElement_destructor,
367     HTMLElement_clone,
368     HTMLElement_handle_event,
369     HTMLElement_get_attr_col,
370     NULL,
371     NULL,
372     NULL,
373     NULL,
374     NULL,
375     HTMLScriptElement_get_readystate
376 };
377
378 HRESULT script_elem_from_nsscript(HTMLDocumentNode *doc, nsIDOMHTMLScriptElement *nsscript, HTMLScriptElement **ret)
379 {
380     HTMLDOMNode *node;
381     HRESULT hres;
382
383     hres = get_node(doc, (nsIDOMNode*)nsscript, TRUE, &node);
384     if(FAILED(hres))
385         return hres;
386
387     assert(node->vtbl == &HTMLScriptElementImplVtbl);
388     *ret = impl_from_HTMLDOMNode(node);
389     return S_OK;
390 }
391
392 static const tid_t HTMLScriptElement_iface_tids[] = {
393     HTMLELEMENT_TIDS,
394     IHTMLScriptElement_tid,
395     0
396 };
397
398 static dispex_static_data_t HTMLScriptElement_dispex = {
399     NULL,
400     DispHTMLScriptElement_tid,
401     NULL,
402     HTMLScriptElement_iface_tids
403 };
404
405 HRESULT HTMLScriptElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, HTMLElement **elem)
406 {
407     HTMLScriptElement *ret;
408     nsresult nsres;
409
410     ret = heap_alloc_zero(sizeof(HTMLScriptElement));
411     if(!ret)
412         return E_OUTOFMEMORY;
413
414     ret->IHTMLScriptElement_iface.lpVtbl = &HTMLScriptElementVtbl;
415     ret->element.node.vtbl = &HTMLScriptElementImplVtbl;
416
417     HTMLElement_Init(&ret->element, doc, nselem, &HTMLScriptElement_dispex);
418
419     nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLScriptElement, (void**)&ret->nsscript);
420
421     /* Share nsscript reference with nsnode */
422     assert(nsres == NS_OK && (nsIDOMNode*)ret->nsscript == ret->element.node.nsnode);
423     nsIDOMNode_Release(ret->element.node.nsnode);
424
425     *elem = &ret->element;
426     return S_OK;
427 }