mshtml: Add VT_I4 support to set_nsstyle_attr_var.
[wine] / dlls / mshtml / htmloption.c
1 /*
2  * Copyright 2007 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 IHTMLOptionElementVtbl *lpHTMLOptionElementVtbl;
38
39     nsIDOMHTMLOptionElement *nsoption;
40 } HTMLOptionElement;
41
42 #define HTMLOPTION(x)  (&(x)->lpHTMLOptionElementVtbl)
43
44 #define HTMLOPTION_THIS(iface) DEFINE_THIS(HTMLOptionElement, HTMLOptionElement, iface)
45
46 static HRESULT WINAPI HTMLOptionElement_QueryInterface(IHTMLOptionElement *iface,
47         REFIID riid, void **ppv)
48 {
49     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
50
51     return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(&This->element.node), riid, ppv);
52 }
53
54 static ULONG WINAPI HTMLOptionElement_AddRef(IHTMLOptionElement *iface)
55 {
56     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
57
58     return IHTMLDOMNode_AddRef(HTMLDOMNODE(&This->element.node));
59 }
60
61 static ULONG WINAPI HTMLOptionElement_Release(IHTMLOptionElement *iface)
62 {
63     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
64
65     return IHTMLDOMNode_Release(HTMLDOMNODE(&This->element.node));
66 }
67
68 static HRESULT WINAPI HTMLOptionElement_GetTypeInfoCount(IHTMLOptionElement *iface, UINT *pctinfo)
69 {
70     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
71     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->element.node.dispex), pctinfo);
72 }
73
74 static HRESULT WINAPI HTMLOptionElement_GetTypeInfo(IHTMLOptionElement *iface, UINT iTInfo,
75                                               LCID lcid, ITypeInfo **ppTInfo)
76 {
77     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
78     return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->element.node.dispex), iTInfo, lcid, ppTInfo);
79 }
80
81 static HRESULT WINAPI HTMLOptionElement_GetIDsOfNames(IHTMLOptionElement *iface, REFIID riid,
82                                                 LPOLESTR *rgszNames, UINT cNames,
83                                                 LCID lcid, DISPID *rgDispId)
84 {
85     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
86     return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->element.node.dispex), riid, rgszNames, cNames, lcid, rgDispId);
87 }
88
89 static HRESULT WINAPI HTMLOptionElement_Invoke(IHTMLOptionElement *iface, DISPID dispIdMember,
90                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
91                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
92 {
93     HTMLOptionElement *This = HTMLOPTION_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 HTMLOptionElement_put_selected(IHTMLOptionElement *iface, VARIANT_BOOL v)
99 {
100     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
101     FIXME("(%p)->(%x)\n", This, v);
102     return E_NOTIMPL;
103 }
104
105 static HRESULT WINAPI HTMLOptionElement_get_selected(IHTMLOptionElement *iface, VARIANT_BOOL *p)
106 {
107     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
108     FIXME("(%p)->(%p)\n", This, p);
109     return E_NOTIMPL;
110 }
111
112 static HRESULT WINAPI HTMLOptionElement_put_value(IHTMLOptionElement *iface, BSTR v)
113 {
114     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
115     nsAString value_str;
116     nsresult nsres;
117
118     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
119
120     nsAString_Init(&value_str, v);
121     nsres = nsIDOMHTMLOptionElement_SetValue(This->nsoption, &value_str);
122     nsAString_Finish(&value_str);
123     if(NS_FAILED(nsres))
124         ERR("SetValue failed: %08x\n", nsres);
125
126     return S_OK;
127 }
128
129 static HRESULT WINAPI HTMLOptionElement_get_value(IHTMLOptionElement *iface, BSTR *p)
130 {
131     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
132     nsAString value_str;
133     const PRUnichar *value;
134     nsresult nsres;
135
136     TRACE("(%p)->(%p)\n", This, p);
137
138     nsAString_Init(&value_str, NULL);
139     nsres = nsIDOMHTMLOptionElement_GetValue(This->nsoption, &value_str);
140     if(NS_SUCCEEDED(nsres)) {
141         nsAString_GetData(&value_str, &value);
142         *p = SysAllocString(value);
143     }else {
144         ERR("GetValue failed: %08x\n", nsres);
145         *p = NULL;
146     }
147     nsAString_Finish(&value_str);
148
149     return S_OK;
150 }
151
152 static HRESULT WINAPI HTMLOptionElement_put_defaultSelected(IHTMLOptionElement *iface, VARIANT_BOOL v)
153 {
154     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
155     FIXME("(%p)->(%x)\n", This, v);
156     return E_NOTIMPL;
157 }
158
159 static HRESULT WINAPI HTMLOptionElement_get_defaultSelected(IHTMLOptionElement *iface, VARIANT_BOOL *p)
160 {
161     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
162     FIXME("(%p)->(%p)\n", This, p);
163     return E_NOTIMPL;
164 }
165
166 static HRESULT WINAPI HTMLOptionElement_put_index(IHTMLOptionElement *iface, LONG v)
167 {
168     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
169     FIXME("(%p)->(%d)\n", This, v);
170     return E_NOTIMPL;
171 }
172
173 static HRESULT WINAPI HTMLOptionElement_get_index(IHTMLOptionElement *iface, LONG *p)
174 {
175     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
176     FIXME("(%p)->(%p)\n", This, p);
177     return E_NOTIMPL;
178 }
179
180 static HRESULT WINAPI HTMLOptionElement_put_text(IHTMLOptionElement *iface, BSTR v)
181 {
182     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
183     nsIDOMText *text_node;
184     nsAString text_str;
185     nsIDOMNode *tmp;
186     nsresult nsres;
187
188     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
189
190     if(!This->element.node.doc->nsdoc) {
191         WARN("NULL nsdoc\n");
192         return E_UNEXPECTED;
193     }
194
195     while(1) {
196         nsIDOMNode *child;
197
198         nsres = nsIDOMHTMLOptionElement_GetFirstChild(This->nsoption, &child);
199         if(NS_FAILED(nsres) || !child)
200             break;
201
202         nsres = nsIDOMHTMLOptionElement_RemoveChild(This->nsoption, child, &tmp);
203         nsIDOMNode_Release(child);
204         if(NS_SUCCEEDED(nsres)) {
205             nsIDOMNode_Release(tmp);
206         }else {
207             ERR("RemoveChild failed: %08x\n", nsres);
208             break;
209         }
210     }
211
212     nsAString_Init(&text_str, v);
213     nsres = nsIDOMHTMLDocument_CreateTextNode(This->element.node.doc->nsdoc, &text_str, &text_node);
214     nsAString_Finish(&text_str);
215     if(NS_FAILED(nsres)) {
216         ERR("CreateTextNode failed: %08x\n", nsres);
217         return E_FAIL;
218     }
219
220     nsres = nsIDOMHTMLOptionElement_AppendChild(This->nsoption, (nsIDOMNode*)text_node, &tmp);
221     if(NS_SUCCEEDED(nsres))
222         nsIDOMNode_Release(tmp);
223     else
224         ERR("AppendChild failed: %08x\n", nsres);
225
226     return S_OK;
227 }
228
229 static HRESULT WINAPI HTMLOptionElement_get_text(IHTMLOptionElement *iface, BSTR *p)
230 {
231     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
232     nsAString text_str;
233     const PRUnichar *text;
234     nsresult nsres;
235
236     TRACE("(%p)->(%p)\n", This, p);
237
238     nsAString_Init(&text_str, NULL);
239     nsres = nsIDOMHTMLOptionElement_GetText(This->nsoption, &text_str);
240     if(NS_SUCCEEDED(nsres)) {
241         nsAString_GetData(&text_str, &text);
242         *p = SysAllocString(text);
243     }else {
244         ERR("GetText failed: %08x\n", nsres);
245         *p = NULL;
246     }
247     nsAString_Finish(&text_str);
248
249     return S_OK;
250 }
251
252 static HRESULT WINAPI HTMLOptionElement_get_form(IHTMLOptionElement *iface, IHTMLFormElement **p)
253 {
254     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
255     FIXME("(%p)->(%p)\n", This, p);
256     return E_NOTIMPL;
257 }
258
259 #undef HTMLOPTION_THIS
260
261 static const IHTMLOptionElementVtbl HTMLOptionElementVtbl = {
262     HTMLOptionElement_QueryInterface,
263     HTMLOptionElement_AddRef,
264     HTMLOptionElement_Release,
265     HTMLOptionElement_GetTypeInfoCount,
266     HTMLOptionElement_GetTypeInfo,
267     HTMLOptionElement_GetIDsOfNames,
268     HTMLOptionElement_Invoke,
269     HTMLOptionElement_put_selected,
270     HTMLOptionElement_get_selected,
271     HTMLOptionElement_put_value,
272     HTMLOptionElement_get_value,
273     HTMLOptionElement_put_defaultSelected,
274     HTMLOptionElement_get_defaultSelected,
275     HTMLOptionElement_put_index,
276     HTMLOptionElement_get_index,
277     HTMLOptionElement_put_text,
278     HTMLOptionElement_get_text,
279     HTMLOptionElement_get_form
280 };
281
282 #define HTMLOPTION_NODE_THIS(iface) DEFINE_THIS2(HTMLOptionElement, element.node, iface)
283
284 static HRESULT HTMLOptionElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
285 {
286     HTMLOptionElement *This = HTMLOPTION_NODE_THIS(iface);
287
288     *ppv = NULL;
289
290     if(IsEqualGUID(&IID_IUnknown, riid)) {
291         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
292         *ppv = HTMLOPTION(This);
293     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
294         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
295         *ppv = HTMLOPTION(This);
296     }else if(IsEqualGUID(&IID_IHTMLOptionElement, riid)) {
297         TRACE("(%p)->(IID_IHTMLOptionElement %p)\n", This, ppv);
298         *ppv = HTMLOPTION(This);
299     }
300
301     if(*ppv) {
302         IUnknown_AddRef((IUnknown*)*ppv);
303         return S_OK;
304     }
305
306     return HTMLElement_QI(&This->element.node, riid, ppv);
307 }
308
309 static void HTMLOptionElement_destructor(HTMLDOMNode *iface)
310 {
311     HTMLOptionElement *This = HTMLOPTION_NODE_THIS(iface);
312
313     if(This->nsoption)
314         nsIDOMHTMLOptionElement_Release(This->nsoption);
315
316     HTMLElement_destructor(&This->element.node);
317 }
318
319 #undef HTMLOPTION_NODE_THIS
320
321 static const NodeImplVtbl HTMLOptionElementImplVtbl = {
322     HTMLOptionElement_QI,
323     HTMLOptionElement_destructor
324 };
325
326 static const tid_t HTMLOptionElement_iface_tids[] = {
327     IHTMLDOMNode_tid,
328     IHTMLDOMNode2_tid,
329     IHTMLElement_tid,
330     IHTMLElement2_tid,
331     IHTMLElement3_tid,
332     IHTMLOptionElement_tid,
333     0
334 };
335 static dispex_static_data_t HTMLOptionElement_dispex = {
336     NULL,
337     DispHTMLOptionElement_tid,
338     NULL,
339     HTMLOptionElement_iface_tids
340 };
341
342 HTMLElement *HTMLOptionElement_Create(nsIDOMHTMLElement *nselem)
343 {
344     HTMLOptionElement *ret = heap_alloc_zero(sizeof(HTMLOptionElement));
345     nsresult nsres;
346
347     ret->lpHTMLOptionElementVtbl = &HTMLOptionElementVtbl;
348     ret->element.node.vtbl = &HTMLOptionElementImplVtbl;
349
350     HTMLElement_Init(&ret->element);
351     init_dispex(&ret->element.node.dispex, (IUnknown*)HTMLOPTION(ret), &HTMLOptionElement_dispex);
352
353     nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLOptionElement, (void**)&ret->nsoption);
354     if(NS_FAILED(nsres))
355         ERR("Could not get nsIDOMHTMLOptionElement interface: %08x\n", nsres);
356
357     return &ret->element;
358 }
359
360 #define HTMLOPTFACTORY_THIS(iface) DEFINE_THIS(HTMLOptionElementFactory, HTMLOptionElementFactory, iface)
361
362 static HRESULT WINAPI HTMLOptionElementFactory_QueryInterface(IHTMLOptionElementFactory *iface,
363                                                               REFIID riid, void **ppv)
364 {
365     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
366
367     *ppv = NULL;
368
369     if(IsEqualGUID(&IID_IUnknown, riid)) {
370         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
371         *ppv = HTMLOPTFACTORY(This);
372     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
373         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
374         *ppv = HTMLOPTFACTORY(This);
375     }else if(IsEqualGUID(&IID_IHTMLOptionElementFactory, riid)) {
376         TRACE("(%p)->(IID_IHTMLOptionElementFactory %p)\n", This, ppv);
377         *ppv = HTMLOPTFACTORY(This);
378     }
379
380     if(*ppv) {
381         IUnknown_AddRef((IUnknown*)*ppv);
382         return S_OK;
383     }
384
385     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
386     return E_NOINTERFACE;
387 }
388
389 static ULONG WINAPI HTMLOptionElementFactory_AddRef(IHTMLOptionElementFactory *iface)
390 {
391     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
392     LONG ref = InterlockedIncrement(&This->ref);
393
394     TRACE("(%p) ref=%d\n", This, ref);
395
396     return ref;
397 }
398
399 static ULONG WINAPI HTMLOptionElementFactory_Release(IHTMLOptionElementFactory *iface)
400 {
401     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
402     LONG ref = InterlockedDecrement(&This->ref);
403
404     TRACE("(%p) ref=%d\n", This, ref);
405
406     if(!ref)
407         heap_free(This);
408
409     return ref;
410 }
411
412 static HRESULT WINAPI HTMLOptionElementFactory_GetTypeInfoCount(IHTMLOptionElementFactory *iface, UINT *pctinfo)
413 {
414     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
415     FIXME("(%p)->(%p)\n", This, pctinfo);
416     return E_NOTIMPL;
417 }
418
419 static HRESULT WINAPI HTMLOptionElementFactory_GetTypeInfo(IHTMLOptionElementFactory *iface, UINT iTInfo,
420                                               LCID lcid, ITypeInfo **ppTInfo)
421 {
422     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
423     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
424     return E_NOTIMPL;
425 }
426
427 static HRESULT WINAPI HTMLOptionElementFactory_GetIDsOfNames(IHTMLOptionElementFactory *iface, REFIID riid,
428                                                 LPOLESTR *rgszNames, UINT cNames,
429                                                 LCID lcid, DISPID *rgDispId)
430 {
431     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
432     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
433                                         lcid, rgDispId);
434     return E_NOTIMPL;
435 }
436
437 static HRESULT WINAPI HTMLOptionElementFactory_Invoke(IHTMLOptionElementFactory *iface, DISPID dispIdMember,
438                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
439                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
440 {
441     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
442     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
443             lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
444     return E_NOTIMPL;
445 }
446
447 static HRESULT WINAPI HTMLOptionElementFactory_create(IHTMLOptionElementFactory *iface,
448         VARIANT text, VARIANT value, VARIANT defaultselected, VARIANT selected,
449         IHTMLOptionElement **optelem)
450 {
451     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
452     nsIDOMElement *nselem;
453     nsAString option_str;
454     nsresult nsres;
455     HRESULT hres;
456
457     static const PRUnichar optionW[] = {'O','P','T','I','O','N',0};
458
459     TRACE("(%p)->(%s %s %s %s %p)\n", This, debugstr_variant(&text), debugstr_variant(&value),
460           debugstr_variant(&defaultselected), debugstr_variant(&selected), optelem);
461
462     if(!This->doc->nsdoc) {
463         WARN("NULL nsdoc\n");
464         return E_UNEXPECTED;
465     }
466
467     *optelem = NULL;
468
469     nsAString_Init(&option_str, optionW);
470     nsres = nsIDOMHTMLDocument_CreateElement(This->doc->nsdoc, &option_str, &nselem);
471     nsAString_Finish(&option_str);
472     if(NS_FAILED(nsres)) {
473         ERR("CreateElement failed: %08x\n", nsres);
474         return E_FAIL;
475     }
476
477     hres = IHTMLDOMNode_QueryInterface(HTMLDOMNODE(get_node(This->doc, (nsIDOMNode*)nselem, TRUE)),
478             &IID_IHTMLOptionElement, (void**)optelem);
479     nsIDOMElement_Release(nselem);
480
481     if(V_VT(&text) == VT_BSTR)
482         IHTMLOptionElement_put_text(*optelem, V_BSTR(&text));
483     else if(V_VT(&text) != VT_EMPTY)
484         FIXME("Unsupported text vt=%d\n", V_VT(&text));
485
486     if(V_VT(&value) == VT_BSTR)
487         IHTMLOptionElement_put_value(*optelem, V_BSTR(&value));
488     else if(V_VT(&value) != VT_EMPTY)
489         FIXME("Unsupported value vt=%d\n", V_VT(&value));
490
491     if(V_VT(&defaultselected) != VT_EMPTY)
492         FIXME("Unsupported defaultselected vt=%d\n", V_VT(&defaultselected));
493     if(V_VT(&selected) != VT_EMPTY)
494         FIXME("Unsupported selected vt=%d\n", V_VT(&selected));
495
496     return S_OK;
497 }
498
499 #undef HTMLOPTFACTORY_THIS
500
501 static const IHTMLOptionElementFactoryVtbl HTMLOptionElementFactoryVtbl = {
502     HTMLOptionElementFactory_QueryInterface,
503     HTMLOptionElementFactory_AddRef,
504     HTMLOptionElementFactory_Release,
505     HTMLOptionElementFactory_GetTypeInfoCount,
506     HTMLOptionElementFactory_GetTypeInfo,
507     HTMLOptionElementFactory_GetIDsOfNames,
508     HTMLOptionElementFactory_Invoke,
509     HTMLOptionElementFactory_create
510 };
511
512 HTMLOptionElementFactory *HTMLOptionElementFactory_Create(HTMLDocument *doc)
513 {
514     HTMLOptionElementFactory *ret;
515
516     ret = heap_alloc(sizeof(HTMLOptionElementFactory));
517
518     ret->lpHTMLOptionElementFactoryVtbl = &HTMLOptionElementFactoryVtbl;
519     ret->ref = 1;
520     ret->doc = doc;
521
522     return ret;
523 }