mshtml: Use an iface instead of a vtbl pointer in HTMLScriptElement.
[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
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27
28 #include "wine/debug.h"
29
30 #include "mshtml_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
33
34 typedef struct {
35     HTMLElement element;
36
37     IHTMLScriptElement IHTMLScriptElement_iface;
38
39     nsIDOMHTMLScriptElement *nsscript;
40 } HTMLScriptElement;
41
42 static inline HTMLScriptElement *impl_from_IHTMLScriptElement(IHTMLScriptElement *iface)
43 {
44     return CONTAINING_RECORD(iface, HTMLScriptElement, IHTMLScriptElement_iface);
45 }
46
47 static HRESULT WINAPI HTMLScriptElement_QueryInterface(IHTMLScriptElement *iface,
48         REFIID riid, void **ppv)
49 {
50     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
51
52     return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(&This->element.node), riid, ppv);
53 }
54
55 static ULONG WINAPI HTMLScriptElement_AddRef(IHTMLScriptElement *iface)
56 {
57     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
58
59     return IHTMLDOMNode_AddRef(HTMLDOMNODE(&This->element.node));
60 }
61
62 static ULONG WINAPI HTMLScriptElement_Release(IHTMLScriptElement *iface)
63 {
64     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
65
66     return IHTMLDOMNode_Release(HTMLDOMNODE(&This->element.node));
67 }
68
69 static HRESULT WINAPI HTMLScriptElement_GetTypeInfoCount(IHTMLScriptElement *iface, UINT *pctinfo)
70 {
71     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
72     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->element.node.dispex), pctinfo);
73 }
74
75 static HRESULT WINAPI HTMLScriptElement_GetTypeInfo(IHTMLScriptElement *iface, UINT iTInfo,
76                                               LCID lcid, ITypeInfo **ppTInfo)
77 {
78     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
79     return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->element.node.dispex), iTInfo, lcid, ppTInfo);
80 }
81
82 static HRESULT WINAPI HTMLScriptElement_GetIDsOfNames(IHTMLScriptElement *iface, REFIID riid,
83                                                 LPOLESTR *rgszNames, UINT cNames,
84                                                 LCID lcid, DISPID *rgDispId)
85 {
86     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
87     return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->element.node.dispex), riid, rgszNames, cNames, lcid, rgDispId);
88 }
89
90 static HRESULT WINAPI HTMLScriptElement_Invoke(IHTMLScriptElement *iface, DISPID dispIdMember,
91                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
92                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
93 {
94     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
95     return IDispatchEx_Invoke(DISPATCHEX(&This->element.node.dispex), dispIdMember, riid, lcid, wFlags, pDispParams,
96             pVarResult, pExcepInfo, puArgErr);
97 }
98
99 static HRESULT WINAPI HTMLScriptElement_put_src(IHTMLScriptElement *iface, BSTR v)
100 {
101     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
102     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
103     return E_NOTIMPL;
104 }
105
106 static HRESULT WINAPI HTMLScriptElement_get_src(IHTMLScriptElement *iface, BSTR *p)
107 {
108     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
109     FIXME("(%p)->(%p)\n", This, p);
110     return E_NOTIMPL;
111 }
112
113 static HRESULT WINAPI HTMLScriptElement_put_htmlFor(IHTMLScriptElement *iface, BSTR v)
114 {
115     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
116     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
117     return E_NOTIMPL;
118 }
119
120 static HRESULT WINAPI HTMLScriptElement_get_htmlFor(IHTMLScriptElement *iface, BSTR *p)
121 {
122     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
123     FIXME("(%p)->(%p)\n", This, p);
124     return E_NOTIMPL;
125 }
126
127 static HRESULT WINAPI HTMLScriptElement_put_event(IHTMLScriptElement *iface, BSTR v)
128 {
129     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
130     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
131     return E_NOTIMPL;
132 }
133
134 static HRESULT WINAPI HTMLScriptElement_get_event(IHTMLScriptElement *iface, BSTR *p)
135 {
136     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
137     FIXME("(%p)->(%p)\n", This, p);
138     return E_NOTIMPL;
139 }
140
141 static HRESULT WINAPI HTMLScriptElement_put_text(IHTMLScriptElement *iface, BSTR v)
142 {
143     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
144     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
145     return E_NOTIMPL;
146 }
147
148 static HRESULT WINAPI HTMLScriptElement_get_text(IHTMLScriptElement *iface, BSTR *p)
149 {
150     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
151     FIXME("(%p)->(%p)\n", This, p);
152     return E_NOTIMPL;
153 }
154
155 static HRESULT WINAPI HTMLScriptElement_put_defer(IHTMLScriptElement *iface, VARIANT_BOOL v)
156 {
157     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
158     HRESULT hr = S_OK;
159     nsresult nsres;
160
161     TRACE("(%p)->(%x)\n", This, v);
162
163     nsres = nsIDOMHTMLScriptElement_SetDefer(This->nsscript, v != VARIANT_FALSE);
164     if(NS_FAILED(nsres))
165     {
166         hr = E_FAIL;
167     }
168
169     return hr;
170 }
171
172 static HRESULT WINAPI HTMLScriptElement_get_defer(IHTMLScriptElement *iface, VARIANT_BOOL *p)
173 {
174     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
175     PRBool defer = FALSE;
176     nsresult nsres;
177
178     TRACE("(%p)->(%p)\n", This, p);
179
180     if(!p)
181         return E_INVALIDARG;
182
183     nsres = nsIDOMHTMLScriptElement_GetDefer(This->nsscript, &defer);
184     if(NS_FAILED(nsres)) {
185         ERR("GetSrc failed: %08x\n", nsres);
186     }
187
188     *p = defer ? VARIANT_TRUE : VARIANT_FALSE;
189
190     TRACE("*p = %d\n", *p);
191     return S_OK;
192 }
193
194 static HRESULT WINAPI HTMLScriptElement_get_readyState(IHTMLScriptElement *iface, BSTR *p)
195 {
196     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
197     FIXME("(%p)->(%p)\n", This, p);
198     return E_NOTIMPL;
199 }
200
201 static HRESULT WINAPI HTMLScriptElement_put_onerror(IHTMLScriptElement *iface, VARIANT v)
202 {
203     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
204     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
205     return E_NOTIMPL;
206 }
207
208 static HRESULT WINAPI HTMLScriptElement_get_onerror(IHTMLScriptElement *iface, VARIANT *p)
209 {
210     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
211     FIXME("(%p)->(%p)\n", This, p);
212     return E_NOTIMPL;
213 }
214
215 static HRESULT WINAPI HTMLScriptElement_put_type(IHTMLScriptElement *iface, BSTR v)
216 {
217     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
218     nsAString nstype_str;
219     nsresult nsres;
220
221     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
222
223     nsAString_Init(&nstype_str, v);
224     nsres = nsIDOMHTMLScriptElement_SetType(This->nsscript, &nstype_str);
225     if (NS_FAILED(nsres))
226         ERR("SetType failed: %08x\n", nsres);
227     nsAString_Finish (&nstype_str);
228
229     return S_OK;
230 }
231
232 static HRESULT WINAPI HTMLScriptElement_get_type(IHTMLScriptElement *iface, BSTR *p)
233 {
234     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
235     const PRUnichar *nstype;
236     nsAString nstype_str;
237     nsresult nsres;
238
239     TRACE("(%p)->(%p)\n", This, p);
240
241     nsAString_Init(&nstype_str, NULL);
242     nsres = nsIDOMHTMLScriptElement_GetType(This->nsscript, &nstype_str);
243     if(NS_FAILED(nsres))
244         ERR("GetType failed: %08x\n", nsres);
245
246     nsAString_GetData(&nstype_str, &nstype);
247     *p = *nstype ? SysAllocString(nstype) : NULL;
248     nsAString_Finish(&nstype_str);
249
250     return S_OK;
251 }
252
253 static const IHTMLScriptElementVtbl HTMLScriptElementVtbl = {
254     HTMLScriptElement_QueryInterface,
255     HTMLScriptElement_AddRef,
256     HTMLScriptElement_Release,
257     HTMLScriptElement_GetTypeInfoCount,
258     HTMLScriptElement_GetTypeInfo,
259     HTMLScriptElement_GetIDsOfNames,
260     HTMLScriptElement_Invoke,
261     HTMLScriptElement_put_src,
262     HTMLScriptElement_get_src,
263     HTMLScriptElement_put_htmlFor,
264     HTMLScriptElement_get_htmlFor,
265     HTMLScriptElement_put_event,
266     HTMLScriptElement_get_event,
267     HTMLScriptElement_put_text,
268     HTMLScriptElement_get_text,
269     HTMLScriptElement_put_defer,
270     HTMLScriptElement_get_defer,
271     HTMLScriptElement_get_readyState,
272     HTMLScriptElement_put_onerror,
273     HTMLScriptElement_get_onerror,
274     HTMLScriptElement_put_type,
275     HTMLScriptElement_get_type
276 };
277
278 #define HTMLSCRIPT_NODE_THIS(iface) DEFINE_THIS2(HTMLScriptElement, element.node, iface)
279
280 static HRESULT HTMLScriptElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
281 {
282     HTMLScriptElement *This = HTMLSCRIPT_NODE_THIS(iface);
283
284     *ppv = NULL;
285
286     if(IsEqualGUID(&IID_IUnknown, riid)) {
287         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
288         *ppv = &This->IHTMLScriptElement_iface;
289     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
290         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
291         *ppv = &This->IHTMLScriptElement_iface;
292     }else if(IsEqualGUID(&IID_IHTMLScriptElement, riid)) {
293         TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This, ppv);
294         *ppv = &This->IHTMLScriptElement_iface;
295     }
296
297     if(*ppv) {
298         IUnknown_AddRef((IUnknown*)*ppv);
299         return S_OK;
300     }
301
302     return HTMLElement_QI(&This->element.node, riid, ppv);
303 }
304
305 static void HTMLScriptElement_destructor(HTMLDOMNode *iface)
306 {
307     HTMLScriptElement *This = HTMLSCRIPT_NODE_THIS(iface);
308     HTMLElement_destructor(&This->element.node);
309 }
310
311 static HRESULT HTMLScriptElement_get_readystate(HTMLDOMNode *iface, BSTR *p)
312 {
313     HTMLScriptElement *This = HTMLSCRIPT_NODE_THIS(iface);
314
315     return IHTMLScriptElement_get_readyState(&This->IHTMLScriptElement_iface, p);
316 }
317
318 #undef HTMLSCRIPT_NODE_THIS
319
320 static const NodeImplVtbl HTMLScriptElementImplVtbl = {
321     HTMLScriptElement_QI,
322     HTMLScriptElement_destructor,
323     HTMLElement_clone,
324     NULL,
325     NULL,
326     NULL,
327     NULL,
328     NULL,
329     HTMLScriptElement_get_readystate
330 };
331
332 static const tid_t HTMLScriptElement_iface_tids[] = {
333     HTMLELEMENT_TIDS,
334     IHTMLScriptElement_tid,
335     0
336 };
337
338 static dispex_static_data_t HTMLScriptElement_dispex = {
339     NULL,
340     DispHTMLScriptElement_tid,
341     NULL,
342     HTMLScriptElement_iface_tids
343 };
344
345 HRESULT HTMLScriptElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, HTMLElement **elem)
346 {
347     HTMLScriptElement *ret;
348     nsresult nsres;
349
350     ret = heap_alloc_zero(sizeof(HTMLScriptElement));
351     if(!ret)
352         return E_OUTOFMEMORY;
353
354     ret->IHTMLScriptElement_iface.lpVtbl = &HTMLScriptElementVtbl;
355     ret->element.node.vtbl = &HTMLScriptElementImplVtbl;
356
357     nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLScriptElement, (void**)&ret->nsscript);
358     if(NS_FAILED(nsres)) {
359         ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
360         heap_free(ret);
361         return E_FAIL;
362     }
363
364     HTMLElement_Init(&ret->element, doc, nselem, &HTMLScriptElement_dispex);
365
366     *elem = &ret->element;
367     return S_OK;
368 }