include: Added inspectable.idl file.
[wine.git] / dlls / mshtml / htmloption.c
blob896f812c0bedc3f90912378a53ab36e4a6fab38f
1 /*
2 * Copyright 2007 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <stdarg.h>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35 struct HTMLOptionElement {
36 HTMLElement element;
38 IHTMLOptionElement IHTMLOptionElement_iface;
40 nsIDOMHTMLOptionElement *nsoption;
43 static inline HTMLOptionElement *impl_from_IHTMLOptionElement(IHTMLOptionElement *iface)
45 return CONTAINING_RECORD(iface, HTMLOptionElement, IHTMLOptionElement_iface);
48 static HRESULT WINAPI HTMLOptionElement_QueryInterface(IHTMLOptionElement *iface,
49 REFIID riid, void **ppv)
51 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
53 return IHTMLDOMNode_QueryInterface(&This->element.node.IHTMLDOMNode_iface, riid, ppv);
56 static ULONG WINAPI HTMLOptionElement_AddRef(IHTMLOptionElement *iface)
58 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
60 return IHTMLDOMNode_AddRef(&This->element.node.IHTMLDOMNode_iface);
63 static ULONG WINAPI HTMLOptionElement_Release(IHTMLOptionElement *iface)
65 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
67 return IHTMLDOMNode_Release(&This->element.node.IHTMLDOMNode_iface);
70 static HRESULT WINAPI HTMLOptionElement_GetTypeInfoCount(IHTMLOptionElement *iface, UINT *pctinfo)
72 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
73 return IDispatchEx_GetTypeInfoCount(&This->element.node.event_target.dispex.IDispatchEx_iface, pctinfo);
76 static HRESULT WINAPI HTMLOptionElement_GetTypeInfo(IHTMLOptionElement *iface, UINT iTInfo,
77 LCID lcid, ITypeInfo **ppTInfo)
79 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
80 return IDispatchEx_GetTypeInfo(&This->element.node.event_target.dispex.IDispatchEx_iface, iTInfo, lcid,
81 ppTInfo);
84 static HRESULT WINAPI HTMLOptionElement_GetIDsOfNames(IHTMLOptionElement *iface, REFIID riid,
85 LPOLESTR *rgszNames, UINT cNames,
86 LCID lcid, DISPID *rgDispId)
88 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
89 return IDispatchEx_GetIDsOfNames(&This->element.node.event_target.dispex.IDispatchEx_iface, riid, rgszNames,
90 cNames, lcid, rgDispId);
93 static HRESULT WINAPI HTMLOptionElement_Invoke(IHTMLOptionElement *iface, DISPID dispIdMember,
94 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
95 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
97 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
98 return IDispatchEx_Invoke(&This->element.node.event_target.dispex.IDispatchEx_iface, dispIdMember, riid,
99 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
102 static HRESULT WINAPI HTMLOptionElement_put_selected(IHTMLOptionElement *iface, VARIANT_BOOL v)
104 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
105 nsresult nsres;
107 TRACE("(%p)->(%x)\n", This, v);
109 nsres = nsIDOMHTMLOptionElement_SetSelected(This->nsoption, v != VARIANT_FALSE);
110 if(NS_FAILED(nsres)) {
111 ERR("SetSelected failed: %08x\n", nsres);
112 return E_FAIL;
115 return S_OK;
118 static HRESULT WINAPI HTMLOptionElement_get_selected(IHTMLOptionElement *iface, VARIANT_BOOL *p)
120 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
121 cpp_bool selected;
122 nsresult nsres;
124 TRACE("(%p)->(%p)\n", This, p);
126 nsres = nsIDOMHTMLOptionElement_GetSelected(This->nsoption, &selected);
127 if(NS_FAILED(nsres)) {
128 ERR("GetSelected failed: %08x\n", nsres);
129 return E_FAIL;
132 *p = selected ? VARIANT_TRUE : VARIANT_FALSE;
133 return S_OK;
136 static HRESULT WINAPI HTMLOptionElement_put_value(IHTMLOptionElement *iface, BSTR v)
138 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
139 nsAString value_str;
140 nsresult nsres;
142 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
144 nsAString_InitDepend(&value_str, v);
145 nsres = nsIDOMHTMLOptionElement_SetValue(This->nsoption, &value_str);
146 nsAString_Finish(&value_str);
147 if(NS_FAILED(nsres))
148 ERR("SetValue failed: %08x\n", nsres);
150 return S_OK;
153 static HRESULT WINAPI HTMLOptionElement_get_value(IHTMLOptionElement *iface, BSTR *p)
155 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
156 nsAString value_str;
157 nsresult nsres;
159 TRACE("(%p)->(%p)\n", This, p);
161 nsAString_Init(&value_str, NULL);
162 nsres = nsIDOMHTMLOptionElement_GetValue(This->nsoption, &value_str);
163 return return_nsstr(nsres, &value_str, p);
166 static HRESULT WINAPI HTMLOptionElement_put_defaultSelected(IHTMLOptionElement *iface, VARIANT_BOOL v)
168 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
169 FIXME("(%p)->(%x)\n", This, v);
170 return E_NOTIMPL;
173 static HRESULT WINAPI HTMLOptionElement_get_defaultSelected(IHTMLOptionElement *iface, VARIANT_BOOL *p)
175 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
176 FIXME("(%p)->(%p)\n", This, p);
177 return E_NOTIMPL;
180 static HRESULT WINAPI HTMLOptionElement_put_index(IHTMLOptionElement *iface, LONG v)
182 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
183 FIXME("(%p)->(%d)\n", This, v);
184 return E_NOTIMPL;
187 static HRESULT WINAPI HTMLOptionElement_get_index(IHTMLOptionElement *iface, LONG *p)
189 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
190 FIXME("(%p)->(%p)\n", This, p);
191 return E_NOTIMPL;
194 static HRESULT WINAPI HTMLOptionElement_put_text(IHTMLOptionElement *iface, BSTR v)
196 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
197 nsIDOMText *text_node;
198 nsAString text_str;
199 nsIDOMNode *tmp;
200 nsresult nsres;
202 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
204 if(!This->element.node.doc->nsdoc) {
205 WARN("NULL nsdoc\n");
206 return E_UNEXPECTED;
209 while(1) {
210 nsIDOMNode *child;
212 nsres = nsIDOMHTMLElement_GetFirstChild(This->element.nselem, &child);
213 if(NS_FAILED(nsres) || !child)
214 break;
216 nsres = nsIDOMHTMLElement_RemoveChild(This->element.nselem, child, &tmp);
217 nsIDOMNode_Release(child);
218 if(NS_SUCCEEDED(nsres)) {
219 nsIDOMNode_Release(tmp);
220 }else {
221 ERR("RemoveChild failed: %08x\n", nsres);
222 break;
226 nsAString_InitDepend(&text_str, v);
227 nsres = nsIDOMHTMLDocument_CreateTextNode(This->element.node.doc->nsdoc, &text_str, &text_node);
228 nsAString_Finish(&text_str);
229 if(NS_FAILED(nsres)) {
230 ERR("CreateTextNode failed: %08x\n", nsres);
231 return E_FAIL;
234 nsres = nsIDOMHTMLElement_AppendChild(This->element.nselem, (nsIDOMNode*)text_node, &tmp);
235 if(NS_SUCCEEDED(nsres))
236 nsIDOMNode_Release(tmp);
237 else
238 ERR("AppendChild failed: %08x\n", nsres);
240 return S_OK;
243 static HRESULT WINAPI HTMLOptionElement_get_text(IHTMLOptionElement *iface, BSTR *p)
245 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
246 nsAString text_str;
247 nsresult nsres;
249 TRACE("(%p)->(%p)\n", This, p);
251 nsAString_Init(&text_str, NULL);
252 nsres = nsIDOMHTMLOptionElement_GetText(This->nsoption, &text_str);
253 return return_nsstr(nsres, &text_str, p);
256 static HRESULT WINAPI HTMLOptionElement_get_form(IHTMLOptionElement *iface, IHTMLFormElement **p)
258 HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
259 FIXME("(%p)->(%p)\n", This, p);
260 return E_NOTIMPL;
263 static const IHTMLOptionElementVtbl HTMLOptionElementVtbl = {
264 HTMLOptionElement_QueryInterface,
265 HTMLOptionElement_AddRef,
266 HTMLOptionElement_Release,
267 HTMLOptionElement_GetTypeInfoCount,
268 HTMLOptionElement_GetTypeInfo,
269 HTMLOptionElement_GetIDsOfNames,
270 HTMLOptionElement_Invoke,
271 HTMLOptionElement_put_selected,
272 HTMLOptionElement_get_selected,
273 HTMLOptionElement_put_value,
274 HTMLOptionElement_get_value,
275 HTMLOptionElement_put_defaultSelected,
276 HTMLOptionElement_get_defaultSelected,
277 HTMLOptionElement_put_index,
278 HTMLOptionElement_get_index,
279 HTMLOptionElement_put_text,
280 HTMLOptionElement_get_text,
281 HTMLOptionElement_get_form
284 static inline HTMLOptionElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
286 return CONTAINING_RECORD(iface, HTMLOptionElement, element.node);
289 static HRESULT HTMLOptionElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
291 HTMLOptionElement *This = impl_from_HTMLDOMNode(iface);
293 *ppv = NULL;
295 if(IsEqualGUID(&IID_IUnknown, riid)) {
296 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
297 *ppv = &This->IHTMLOptionElement_iface;
298 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
299 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
300 *ppv = &This->IHTMLOptionElement_iface;
301 }else if(IsEqualGUID(&IID_IHTMLOptionElement, riid)) {
302 TRACE("(%p)->(IID_IHTMLOptionElement %p)\n", This, ppv);
303 *ppv = &This->IHTMLOptionElement_iface;
306 if(*ppv) {
307 IUnknown_AddRef((IUnknown*)*ppv);
308 return S_OK;
311 return HTMLElement_QI(&This->element.node, riid, ppv);
314 static void HTMLOptionElement_traverse(HTMLDOMNode *iface, nsCycleCollectionTraversalCallback *cb)
316 HTMLOptionElement *This = impl_from_HTMLDOMNode(iface);
318 if(This->nsoption)
319 note_cc_edge((nsISupports*)This->nsoption, "This->nsoption", cb);
322 static void HTMLOptionElement_unlink(HTMLDOMNode *iface)
324 HTMLOptionElement *This = impl_from_HTMLDOMNode(iface);
326 if(This->nsoption) {
327 nsIDOMHTMLOptionElement *nsoption = This->nsoption;
329 This->nsoption = NULL;
330 nsIDOMHTMLOptionElement_Release(nsoption);
334 static const NodeImplVtbl HTMLOptionElementImplVtbl = {
335 HTMLOptionElement_QI,
336 HTMLElement_destructor,
337 HTMLElement_cpc,
338 HTMLElement_clone,
339 HTMLElement_handle_event,
340 HTMLElement_get_attr_col,
341 NULL,
342 NULL,
343 NULL,
344 NULL,
345 NULL,
346 NULL,
347 NULL,
348 NULL,
349 NULL,
350 HTMLOptionElement_traverse,
351 HTMLOptionElement_unlink
354 static const tid_t HTMLOptionElement_iface_tids[] = {
355 HTMLELEMENT_TIDS,
356 IHTMLOptionElement_tid,
359 static dispex_static_data_t HTMLOptionElement_dispex = {
360 NULL,
361 DispHTMLOptionElement_tid,
362 NULL,
363 HTMLOptionElement_iface_tids
366 HRESULT HTMLOptionElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, HTMLElement **elem)
368 HTMLOptionElement *ret;
369 nsresult nsres;
371 ret = heap_alloc_zero(sizeof(HTMLOptionElement));
372 if(!ret)
373 return E_OUTOFMEMORY;
375 ret->IHTMLOptionElement_iface.lpVtbl = &HTMLOptionElementVtbl;
376 ret->element.node.vtbl = &HTMLOptionElementImplVtbl;
378 HTMLElement_Init(&ret->element, doc, nselem, &HTMLOptionElement_dispex);
380 nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLOptionElement, (void**)&ret->nsoption);
381 assert(nsres == NS_OK);
383 *elem = &ret->element;
384 return S_OK;
387 static inline HTMLOptionElementFactory *impl_from_IHTMLOptionElementFactory(IHTMLOptionElementFactory *iface)
389 return CONTAINING_RECORD(iface, HTMLOptionElementFactory, IHTMLOptionElementFactory_iface);
392 static HRESULT WINAPI HTMLOptionElementFactory_QueryInterface(IHTMLOptionElementFactory *iface,
393 REFIID riid, void **ppv)
395 HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
397 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
399 if(IsEqualGUID(&IID_IUnknown, riid)) {
400 *ppv = &This->IHTMLOptionElementFactory_iface;
401 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
402 *ppv = &This->IHTMLOptionElementFactory_iface;
403 }else if(IsEqualGUID(&IID_IHTMLOptionElementFactory, riid)) {
404 *ppv = &This->IHTMLOptionElementFactory_iface;
405 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
406 return *ppv ? S_OK : E_NOINTERFACE;
407 }else {
408 *ppv = NULL;
409 WARN("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
410 return E_NOINTERFACE;
413 IUnknown_AddRef((IUnknown*)*ppv);
414 return S_OK;
417 static ULONG WINAPI HTMLOptionElementFactory_AddRef(IHTMLOptionElementFactory *iface)
419 HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
420 LONG ref = InterlockedIncrement(&This->ref);
422 TRACE("(%p) ref=%d\n", This, ref);
424 return ref;
427 static ULONG WINAPI HTMLOptionElementFactory_Release(IHTMLOptionElementFactory *iface)
429 HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
430 LONG ref = InterlockedDecrement(&This->ref);
432 TRACE("(%p) ref=%d\n", This, ref);
434 if(!ref) {
435 release_dispex(&This->dispex);
436 heap_free(This);
439 return ref;
442 static HRESULT WINAPI HTMLOptionElementFactory_GetTypeInfoCount(IHTMLOptionElementFactory *iface, UINT *pctinfo)
444 HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
445 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
448 static HRESULT WINAPI HTMLOptionElementFactory_GetTypeInfo(IHTMLOptionElementFactory *iface, UINT iTInfo,
449 LCID lcid, ITypeInfo **ppTInfo)
451 HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
452 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
455 static HRESULT WINAPI HTMLOptionElementFactory_GetIDsOfNames(IHTMLOptionElementFactory *iface, REFIID riid,
456 LPOLESTR *rgszNames, UINT cNames,
457 LCID lcid, DISPID *rgDispId)
459 HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
460 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames, lcid, rgDispId);
463 static HRESULT WINAPI HTMLOptionElementFactory_Invoke(IHTMLOptionElementFactory *iface, DISPID dispIdMember,
464 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
465 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
467 HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
468 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid, wFlags, pDispParams,
469 pVarResult, pExcepInfo, puArgErr);
472 static HRESULT WINAPI HTMLOptionElementFactory_create(IHTMLOptionElementFactory *iface,
473 VARIANT text, VARIANT value, VARIANT defaultselected, VARIANT selected,
474 IHTMLOptionElement **optelem)
476 HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
477 nsIDOMHTMLElement *nselem;
478 HTMLDOMNode *node;
479 HRESULT hres;
481 static const PRUnichar optionW[] = {'O','P','T','I','O','N',0};
483 TRACE("(%p)->(%s %s %s %s %p)\n", This, debugstr_variant(&text), debugstr_variant(&value),
484 debugstr_variant(&defaultselected), debugstr_variant(&selected), optelem);
486 if(!This->window || !This->window->doc) {
487 WARN("NULL doc\n");
488 return E_UNEXPECTED;
491 *optelem = NULL;
493 hres = create_nselem(This->window->doc, optionW, &nselem);
494 if(FAILED(hres))
495 return hres;
497 hres = get_node(This->window->doc, (nsIDOMNode*)nselem, TRUE, &node);
498 nsIDOMHTMLElement_Release(nselem);
499 if(FAILED(hres))
500 return hres;
502 hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface,
503 &IID_IHTMLOptionElement, (void**)optelem);
504 node_release(node);
506 if(V_VT(&text) == VT_BSTR)
507 IHTMLOptionElement_put_text(*optelem, V_BSTR(&text));
508 else if(V_VT(&text) != VT_EMPTY)
509 FIXME("Unsupported text %s\n", debugstr_variant(&text));
511 if(V_VT(&value) == VT_BSTR)
512 IHTMLOptionElement_put_value(*optelem, V_BSTR(&value));
513 else if(V_VT(&value) != VT_EMPTY)
514 FIXME("Unsupported value %s\n", debugstr_variant(&value));
516 if(V_VT(&defaultselected) != VT_EMPTY)
517 FIXME("Unsupported defaultselected %s\n", debugstr_variant(&defaultselected));
518 if(V_VT(&selected) != VT_EMPTY)
519 FIXME("Unsupported selected %s\n", debugstr_variant(&selected));
521 return S_OK;
524 static const IHTMLOptionElementFactoryVtbl HTMLOptionElementFactoryVtbl = {
525 HTMLOptionElementFactory_QueryInterface,
526 HTMLOptionElementFactory_AddRef,
527 HTMLOptionElementFactory_Release,
528 HTMLOptionElementFactory_GetTypeInfoCount,
529 HTMLOptionElementFactory_GetTypeInfo,
530 HTMLOptionElementFactory_GetIDsOfNames,
531 HTMLOptionElementFactory_Invoke,
532 HTMLOptionElementFactory_create
535 static const tid_t HTMLOptionElementFactory_iface_tids[] = {
536 IHTMLOptionElementFactory_tid,
540 static dispex_static_data_t HTMLOptionElementFactory_dispex = {
541 NULL,
542 IHTMLOptionElementFactory_tid,
543 NULL,
544 HTMLOptionElementFactory_iface_tids
547 HRESULT HTMLOptionElementFactory_Create(HTMLInnerWindow *window, HTMLOptionElementFactory **ret_ptr)
549 HTMLOptionElementFactory *ret;
551 ret = heap_alloc(sizeof(*ret));
552 if(!ret)
553 return E_OUTOFMEMORY;
555 ret->IHTMLOptionElementFactory_iface.lpVtbl = &HTMLOptionElementFactoryVtbl;
556 ret->ref = 1;
557 ret->window = window;
559 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLOptionElementFactory_iface,
560 &HTMLOptionElementFactory_dispex);
562 *ret_ptr = ret;
563 return S_OK;