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