mshtml: Use stored nsdoc in IHTMLDocument2::get_title.
[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     const IHTMLScriptElementVtbl *lpHTMLScriptElementVtbl;
38
39     nsIDOMHTMLScriptElement *nsscript;
40 } HTMLScriptElement;
41
42 #define HTMLSCRIPT(x)  ((IHTMLScriptElement*)  &(x)->lpHTMLScriptElementVtbl)
43
44 #define HTMLSCRIPT_THIS(iface) DEFINE_THIS(HTMLScriptElement, HTMLScriptElement, iface)
45
46 static HRESULT WINAPI HTMLScriptElement_QueryInterface(IHTMLScriptElement *iface,
47         REFIID riid, void **ppv)
48 {
49     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
50
51     return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(&This->element.node), riid, ppv);
52 }
53
54 static ULONG WINAPI HTMLScriptElement_AddRef(IHTMLScriptElement *iface)
55 {
56     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
57
58     return IHTMLDOMNode_AddRef(HTMLDOMNODE(&This->element.node));
59 }
60
61 static ULONG WINAPI HTMLScriptElement_Release(IHTMLScriptElement *iface)
62 {
63     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
64
65     return IHTMLDOMNode_Release(HTMLDOMNODE(&This->element.node));
66 }
67
68 static HRESULT WINAPI HTMLScriptElement_GetTypeInfoCount(IHTMLScriptElement *iface, UINT *pctinfo)
69 {
70     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
71     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->element.node.dispex), pctinfo);
72 }
73
74 static HRESULT WINAPI HTMLScriptElement_GetTypeInfo(IHTMLScriptElement *iface, UINT iTInfo,
75                                               LCID lcid, ITypeInfo **ppTInfo)
76 {
77     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
78     return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->element.node.dispex), iTInfo, lcid, ppTInfo);
79 }
80
81 static HRESULT WINAPI HTMLScriptElement_GetIDsOfNames(IHTMLScriptElement *iface, REFIID riid,
82                                                 LPOLESTR *rgszNames, UINT cNames,
83                                                 LCID lcid, DISPID *rgDispId)
84 {
85     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
86     return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->element.node.dispex), riid, rgszNames, cNames, lcid, rgDispId);
87 }
88
89 static HRESULT WINAPI HTMLScriptElement_Invoke(IHTMLScriptElement *iface, DISPID dispIdMember,
90                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
91                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
92 {
93     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
94     return IDispatchEx_Invoke(DISPATCHEX(&This->element.node.dispex), dispIdMember, riid, lcid, wFlags, pDispParams,
95             pVarResult, pExcepInfo, puArgErr);
96 }
97
98 static HRESULT WINAPI HTMLScriptElement_put_src(IHTMLScriptElement *iface, BSTR v)
99 {
100     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
101     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
102     return E_NOTIMPL;
103 }
104
105 static HRESULT WINAPI HTMLScriptElement_get_src(IHTMLScriptElement *iface, BSTR *p)
106 {
107     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
108     FIXME("(%p)->(%p)\n", This, p);
109     return E_NOTIMPL;
110 }
111
112 static HRESULT WINAPI HTMLScriptElement_put_htmlFor(IHTMLScriptElement *iface, BSTR v)
113 {
114     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
115     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
116     return E_NOTIMPL;
117 }
118
119 static HRESULT WINAPI HTMLScriptElement_get_htmlFor(IHTMLScriptElement *iface, BSTR *p)
120 {
121     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
122     FIXME("(%p)->(%p)\n", This, p);
123     return E_NOTIMPL;
124 }
125
126 static HRESULT WINAPI HTMLScriptElement_put_event(IHTMLScriptElement *iface, BSTR v)
127 {
128     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
129     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
130     return E_NOTIMPL;
131 }
132
133 static HRESULT WINAPI HTMLScriptElement_get_event(IHTMLScriptElement *iface, BSTR *p)
134 {
135     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
136     FIXME("(%p)->(%p)\n", This, p);
137     return E_NOTIMPL;
138 }
139
140 static HRESULT WINAPI HTMLScriptElement_put_text(IHTMLScriptElement *iface, BSTR v)
141 {
142     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
143     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
144     return E_NOTIMPL;
145 }
146
147 static HRESULT WINAPI HTMLScriptElement_get_text(IHTMLScriptElement *iface, BSTR *p)
148 {
149     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
150     FIXME("(%p)->(%p)\n", This, p);
151     return E_NOTIMPL;
152 }
153
154 static HRESULT WINAPI HTMLScriptElement_put_defer(IHTMLScriptElement *iface, VARIANT_BOOL v)
155 {
156     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
157     FIXME("(%p)->(%x)\n", This, v);
158     return E_NOTIMPL;
159 }
160
161 static HRESULT WINAPI HTMLScriptElement_get_defer(IHTMLScriptElement *iface, VARIANT_BOOL *p)
162 {
163     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
164     FIXME("(%p)->(%p)\n", This, p);
165     return E_NOTIMPL;
166 }
167
168 static HRESULT WINAPI HTMLScriptElement_get_readyState(IHTMLScriptElement *iface, BSTR *p)
169 {
170     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
171     FIXME("(%p)->(%p)\n", This, p);
172     return E_NOTIMPL;
173 }
174
175 static HRESULT WINAPI HTMLScriptElement_put_onerror(IHTMLScriptElement *iface, VARIANT v)
176 {
177     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
178     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
179     return E_NOTIMPL;
180 }
181
182 static HRESULT WINAPI HTMLScriptElement_get_onerror(IHTMLScriptElement *iface, VARIANT *p)
183 {
184     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
185     FIXME("(%p)->(%p)\n", This, p);
186     return E_NOTIMPL;
187 }
188
189 static HRESULT WINAPI HTMLScriptElement_put_type(IHTMLScriptElement *iface, BSTR v)
190 {
191     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
192     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
193     return E_NOTIMPL;
194 }
195
196 static HRESULT WINAPI HTMLScriptElement_get_type(IHTMLScriptElement *iface, BSTR *p)
197 {
198     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
199     const PRUnichar *nstype;
200     nsAString nstype_str;
201     nsresult nsres;
202
203     TRACE("(%p)->(%p)\n", This, p);
204
205     nsAString_Init(&nstype_str, NULL);
206     nsres = nsIDOMHTMLScriptElement_GetType(This->nsscript, &nstype_str);
207     if(NS_FAILED(nsres))
208         ERR("GetType failed: %08x\n", nsres);
209
210     nsAString_GetData(&nstype_str, &nstype);
211     *p = *nstype ? SysAllocString(nstype) : NULL;
212     nsAString_Finish(&nstype_str);
213
214     return S_OK;
215 }
216
217 static const IHTMLScriptElementVtbl HTMLScriptElementVtbl = {
218     HTMLScriptElement_QueryInterface,
219     HTMLScriptElement_AddRef,
220     HTMLScriptElement_Release,
221     HTMLScriptElement_GetTypeInfoCount,
222     HTMLScriptElement_GetTypeInfo,
223     HTMLScriptElement_GetIDsOfNames,
224     HTMLScriptElement_Invoke,
225     HTMLScriptElement_put_src,
226     HTMLScriptElement_get_src,
227     HTMLScriptElement_put_htmlFor,
228     HTMLScriptElement_get_htmlFor,
229     HTMLScriptElement_put_event,
230     HTMLScriptElement_get_event,
231     HTMLScriptElement_put_text,
232     HTMLScriptElement_get_text,
233     HTMLScriptElement_put_defer,
234     HTMLScriptElement_get_defer,
235     HTMLScriptElement_get_readyState,
236     HTMLScriptElement_put_onerror,
237     HTMLScriptElement_get_onerror,
238     HTMLScriptElement_put_type,
239     HTMLScriptElement_get_type
240 };
241
242 #undef HTMLSCRIPT_THIS
243
244 #define HTMLSCRIPT_NODE_THIS(iface) DEFINE_THIS2(HTMLScriptElement, element.node, iface)
245
246 static HRESULT HTMLScriptElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
247 {
248     HTMLScriptElement *This = HTMLSCRIPT_NODE_THIS(iface);
249
250     *ppv = NULL;
251
252     if(IsEqualGUID(&IID_IUnknown, riid)) {
253         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
254         *ppv = HTMLSCRIPT(This);
255     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
256         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
257         *ppv = HTMLSCRIPT(This);
258     }else if(IsEqualGUID(&IID_IHTMLScriptElement, riid)) {
259         TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This, ppv);
260         *ppv = HTMLSCRIPT(This);
261     }
262
263     if(*ppv) {
264         IUnknown_AddRef((IUnknown*)*ppv);
265         return S_OK;
266     }
267
268     return HTMLElement_QI(&This->element.node, riid, ppv);
269 }
270
271 static void HTMLScriptElement_destructor(HTMLDOMNode *iface)
272 {
273     HTMLScriptElement *This = HTMLSCRIPT_NODE_THIS(iface);
274     HTMLElement_destructor(&This->element.node);
275 }
276
277 #undef HTMLSCRIPT_NODE_THIS
278
279 static const NodeImplVtbl HTMLScriptElementImplVtbl = {
280     HTMLScriptElement_QI,
281     HTMLScriptElement_destructor
282 };
283
284 HTMLElement *HTMLScriptElement_Create(nsIDOMHTMLElement *nselem)
285 {
286     HTMLScriptElement *ret = heap_alloc_zero(sizeof(HTMLScriptElement));
287     nsresult nsres;
288
289     HTMLElement_Init(&ret->element);
290
291     ret->lpHTMLScriptElementVtbl = &HTMLScriptElementVtbl;
292     ret->element.node.vtbl = &HTMLScriptElementImplVtbl;
293
294     nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLScriptElement, (void**)&ret->nsscript);
295     if(NS_FAILED(nsres))
296         ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
297
298     return &ret->element;
299 }