mshtml: Moved getting select's option element by index to separated function.
[wine/hacks.git] / dlls / mshtml / htmlselect.c
blobb9be8976e118c841499f0b483f91d46463323c1b
1 /*
2 * Copyright 2006 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>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
28 #include "wine/debug.h"
30 #include "mshtml_private.h"
31 #include "htmlevent.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35 typedef struct {
36 HTMLElement element;
38 const IHTMLSelectElementVtbl *lpHTMLSelectElementVtbl;
40 nsIDOMHTMLSelectElement *nsselect;
41 } HTMLSelectElement;
43 #define HTMLSELECT(x) ((IHTMLSelectElement*) &(x)->lpHTMLSelectElementVtbl)
45 static HRESULT htmlselect_item(HTMLSelectElement *This, int i, IDispatch **ret)
47 nsIDOMHTMLOptionsCollection *nscol;
48 nsIDOMNode *nsnode;
49 nsresult nsres;
51 nsres = nsIDOMHTMLSelectElement_GetOptions(This->nsselect, &nscol);
52 if(NS_FAILED(nsres)) {
53 ERR("GetOptions failed: %08x\n", nsres);
54 return E_FAIL;
57 nsres = nsIDOMHTMLOptionsCollection_Item(nscol, i, &nsnode);
58 nsIDOMHTMLOptionsCollection_Release(nscol);
59 if(NS_FAILED(nsres)) {
60 ERR("Item failed: %08x\n", nsres);
61 return E_FAIL;
64 if(nsnode) {
65 HTMLDOMNode *node;
67 node = get_node(This->element.node.doc, nsnode, TRUE);
68 nsIDOMNode_Release(nsnode);
69 if(!node) {
70 ERR("Could not find node\n");
71 return E_FAIL;
74 IHTMLDOMNode_AddRef(HTMLDOMNODE(node));
75 *ret = (IDispatch*)HTMLDOMNODE(node);
76 }else {
77 *ret = NULL;
79 return S_OK;
82 #define HTMLSELECT_THIS(iface) DEFINE_THIS(HTMLSelectElement, HTMLSelectElement, iface)
84 static HRESULT WINAPI HTMLSelectElement_QueryInterface(IHTMLSelectElement *iface,
85 REFIID riid, void **ppv)
87 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
89 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(&This->element.node), riid, ppv);
92 static ULONG WINAPI HTMLSelectElement_AddRef(IHTMLSelectElement *iface)
94 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
96 return IHTMLDOMNode_AddRef(HTMLDOMNODE(&This->element.node));
99 static ULONG WINAPI HTMLSelectElement_Release(IHTMLSelectElement *iface)
101 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
103 return IHTMLDOMNode_Release(HTMLDOMNODE(&This->element.node));
106 static HRESULT WINAPI HTMLSelectElement_GetTypeInfoCount(IHTMLSelectElement *iface, UINT *pctinfo)
108 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
110 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->element.node.dispex), pctinfo);
113 static HRESULT WINAPI HTMLSelectElement_GetTypeInfo(IHTMLSelectElement *iface, UINT iTInfo,
114 LCID lcid, ITypeInfo **ppTInfo)
116 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
118 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->element.node.dispex), iTInfo, lcid, ppTInfo);
121 static HRESULT WINAPI HTMLSelectElement_GetIDsOfNames(IHTMLSelectElement *iface, REFIID riid,
122 LPOLESTR *rgszNames, UINT cNames,
123 LCID lcid, DISPID *rgDispId)
125 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
127 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->element.node.dispex), riid, rgszNames, cNames, lcid, rgDispId);
130 static HRESULT WINAPI HTMLSelectElement_Invoke(IHTMLSelectElement *iface, DISPID dispIdMember,
131 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
132 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
134 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
136 return IDispatchEx_Invoke(DISPATCHEX(&This->element.node.dispex), dispIdMember, riid, lcid,
137 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
140 static HRESULT WINAPI HTMLSelectElement_put_size(IHTMLSelectElement *iface, LONG v)
142 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
143 FIXME("(%p)->(%d)\n", This, v);
144 return E_NOTIMPL;
147 static HRESULT WINAPI HTMLSelectElement_get_size(IHTMLSelectElement *iface, LONG *p)
149 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
150 FIXME("(%p)->(%p)\n", This, p);
151 return E_NOTIMPL;
154 static HRESULT WINAPI HTMLSelectElement_put_multiple(IHTMLSelectElement *iface, VARIANT_BOOL v)
156 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
157 FIXME("(%p)->(%x)\n", This, v);
158 return E_NOTIMPL;
161 static HRESULT WINAPI HTMLSelectElement_get_multiple(IHTMLSelectElement *iface, VARIANT_BOOL *p)
163 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
164 FIXME("(%p)->(%p)\n", This, p);
165 return E_NOTIMPL;
168 static HRESULT WINAPI HTMLSelectElement_put_name(IHTMLSelectElement *iface, BSTR v)
170 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
171 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
172 return E_NOTIMPL;
175 static HRESULT WINAPI HTMLSelectElement_get_name(IHTMLSelectElement *iface, BSTR *p)
177 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
178 nsAString name_str;
179 const PRUnichar *name = NULL;
180 nsresult nsres;
182 TRACE("(%p)->(%p)\n", This, p);
184 nsAString_Init(&name_str, NULL);
186 nsres = nsIDOMHTMLSelectElement_GetName(This->nsselect, &name_str);
187 if(NS_SUCCEEDED(nsres)) {
188 static const WCHAR wszGarbage[] = {'g','a','r','b','a','g','e',0};
190 nsAString_GetData(&name_str, &name);
193 * Native never returns empty string here. If an element has no name,
194 * name of previous element or ramdom data is returned.
196 *p = SysAllocString(*name ? name : wszGarbage);
197 }else {
198 ERR("GetName failed: %08x\n", nsres);
201 nsAString_Finish(&name_str);
203 TRACE("name=%s\n", debugstr_w(*p));
204 return S_OK;
207 static HRESULT WINAPI HTMLSelectElement_get_options(IHTMLSelectElement *iface, IDispatch **p)
209 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
211 TRACE("(%p)->(%p)\n", This, p);
213 *p = (IDispatch*)HTMLSELECT(This);
214 IDispatch_AddRef(*p);
215 return S_OK;
218 static HRESULT WINAPI HTMLSelectElement_put_onchange(IHTMLSelectElement *iface, VARIANT v)
220 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
222 TRACE("(%p)->()\n", This);
224 return set_node_event(&This->element.node, EVENTID_CHANGE, &v);
227 static HRESULT WINAPI HTMLSelectElement_get_onchange(IHTMLSelectElement *iface, VARIANT *p)
229 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
230 FIXME("(%p)->(%p)\n", This, p);
231 return E_NOTIMPL;
234 static HRESULT WINAPI HTMLSelectElement_put_selectedIndex(IHTMLSelectElement *iface, LONG v)
236 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
237 nsresult nsres;
239 TRACE("(%p)->(%d)\n", This, v);
241 nsres = nsIDOMHTMLSelectElement_SetSelectedIndex(This->nsselect, v);
242 if(NS_FAILED(nsres))
243 ERR("SetSelectedIndex failed: %08x\n", nsres);
245 return S_OK;
248 static HRESULT WINAPI HTMLSelectElement_get_selectedIndex(IHTMLSelectElement *iface, LONG *p)
250 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
251 PRInt32 idx = 0;
252 nsresult nsres;
254 TRACE("(%p)->(%p)\n", This, p);
256 nsres = nsIDOMHTMLSelectElement_GetSelectedIndex(This->nsselect, &idx);
257 if(NS_FAILED(nsres))
258 ERR("GetSelectedIndex failed: %08x\n", nsres);
260 *p = idx;
261 return S_OK;
264 static HRESULT WINAPI HTMLSelectElement_get_type(IHTMLSelectElement *iface, BSTR *p)
266 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
267 const PRUnichar *type;
268 nsAString type_str;
269 nsresult nsres;
270 HRESULT hres = S_OK;
272 TRACE("(%p)->(%p)\n", This, p);
274 nsAString_Init(&type_str, NULL);
275 nsres = nsIDOMHTMLSelectElement_GetType(This->nsselect, &type_str);
276 if(NS_SUCCEEDED(nsres)) {
277 nsAString_GetData(&type_str, &type);
278 *p = *type ? SysAllocString(type) : NULL;
279 }else {
280 ERR("GetType failed: %08x\n", nsres);
281 hres = E_FAIL;
284 nsAString_Finish(&type_str);
286 return hres;
289 static HRESULT WINAPI HTMLSelectElement_put_value(IHTMLSelectElement *iface, BSTR v)
291 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
292 nsAString value_str;
293 nsresult nsres;
295 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
297 nsAString_InitDepend(&value_str, v);
298 nsres = nsIDOMHTMLSelectElement_SetValue(This->nsselect, &value_str);
299 nsAString_Finish(&value_str);
300 if(NS_FAILED(nsres))
301 ERR("SetValue failed: %08x\n", nsres);
303 return S_OK;
306 static HRESULT WINAPI HTMLSelectElement_get_value(IHTMLSelectElement *iface, BSTR *p)
308 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
309 nsAString value_str;
310 const PRUnichar *value = NULL;
311 nsresult nsres;
313 TRACE("(%p)->(%p)\n", This, p);
315 nsAString_Init(&value_str, NULL);
317 nsres = nsIDOMHTMLSelectElement_GetValue(This->nsselect, &value_str);
318 if(NS_SUCCEEDED(nsres)) {
319 nsAString_GetData(&value_str, &value);
320 *p = *value ? SysAllocString(value) : NULL;
321 }else {
322 ERR("GetValue failed: %08x\n", nsres);
325 nsAString_Finish(&value_str);
327 TRACE("value=%s\n", debugstr_w(*p));
328 return S_OK;
331 static HRESULT WINAPI HTMLSelectElement_put_disabled(IHTMLSelectElement *iface, VARIANT_BOOL v)
333 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
334 nsresult nsres;
336 TRACE("(%p)->(%x)\n", This, v);
338 nsres = nsIDOMHTMLSelectElement_SetDisabled(This->nsselect, v != VARIANT_FALSE);
339 if(NS_FAILED(nsres)) {
340 ERR("SetDisabled failed: %08x\n", nsres);
341 return E_FAIL;
344 return S_OK;
347 static HRESULT WINAPI HTMLSelectElement_get_disabled(IHTMLSelectElement *iface, VARIANT_BOOL *p)
349 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
350 PRBool disabled = FALSE;
351 nsresult nsres;
353 TRACE("(%p)->(%p)\n", This, p);
355 nsres = nsIDOMHTMLSelectElement_GetDisabled(This->nsselect, &disabled);
356 if(NS_FAILED(nsres)) {
357 ERR("GetDisabled failed: %08x\n", nsres);
358 return E_FAIL;
361 *p = disabled ? VARIANT_TRUE : VARIANT_FALSE;
362 return S_OK;
365 static HRESULT WINAPI HTMLSelectElement_get_form(IHTMLSelectElement *iface, IHTMLFormElement **p)
367 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
368 FIXME("(%p)->(%p)\n", This, p);
369 return E_NOTIMPL;
372 static HRESULT WINAPI HTMLSelectElement_add(IHTMLSelectElement *iface, IHTMLElement *element,
373 VARIANT before)
375 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
376 IHTMLDOMNode *node, *tmp;
377 HRESULT hres;
379 FIXME("(%p)->(%p %s): semi-stub\n", This, element, debugstr_variant(&before));
381 if(V_VT(&before) != VT_EMPTY) {
382 FIXME("unhandled before %s\n", debugstr_variant(&before));
383 return E_NOTIMPL;
386 hres = IHTMLElement_QueryInterface(element, &IID_IHTMLDOMNode, (void**)&node);
387 if(FAILED(hres))
388 return hres;
390 hres = IHTMLDOMNode_appendChild(HTMLDOMNODE(&This->element.node), node, &tmp);
391 IHTMLDOMNode_Release(node);
392 if(SUCCEEDED(hres) && tmp)
393 IHTMLDOMNode_Release(tmp);
395 return hres;
398 static HRESULT WINAPI HTMLSelectElement_remove(IHTMLSelectElement *iface, LONG index)
400 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
401 FIXME("(%p)->(%d)\n", This, index);
402 return E_NOTIMPL;
405 static HRESULT WINAPI HTMLSelectElement_put_length(IHTMLSelectElement *iface, LONG v)
407 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
408 nsresult nsres;
410 TRACE("(%p)->(%d)\n", This, v);
412 nsres = nsIDOMHTMLSelectElement_SetLength(This->nsselect, v);
413 if(NS_FAILED(nsres))
414 ERR("SetLength failed: %08x\n", nsres);
416 return S_OK;
419 static HRESULT WINAPI HTMLSelectElement_get_length(IHTMLSelectElement *iface, LONG *p)
421 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
422 PRUint32 length = 0;
423 nsresult nsres;
425 TRACE("(%p)->(%p)\n", This, p);
427 nsres = nsIDOMHTMLSelectElement_GetLength(This->nsselect, &length);
428 if(NS_FAILED(nsres))
429 ERR("GetLength failed: %08x\n", nsres);
431 *p = length;
433 TRACE("ret %d\n", *p);
434 return S_OK;
437 static HRESULT WINAPI HTMLSelectElement_get__newEnum(IHTMLSelectElement *iface, IUnknown **p)
439 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
440 FIXME("(%p)->(%p)\n", This, p);
441 return E_NOTIMPL;
444 static HRESULT WINAPI HTMLSelectElement_item(IHTMLSelectElement *iface, VARIANT name,
445 VARIANT index, IDispatch **pdisp)
447 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
448 FIXME("(%p)->(v v %p)\n", This, pdisp);
449 return E_NOTIMPL;
452 static HRESULT WINAPI HTMLSelectElement_tags(IHTMLSelectElement *iface, VARIANT tagName,
453 IDispatch **pdisp)
455 HTMLSelectElement *This = HTMLSELECT_THIS(iface);
456 FIXME("(%p)->(v %p)\n", This, pdisp);
457 return E_NOTIMPL;
460 #undef HTMLSELECT_THIS
462 static const IHTMLSelectElementVtbl HTMLSelectElementVtbl = {
463 HTMLSelectElement_QueryInterface,
464 HTMLSelectElement_AddRef,
465 HTMLSelectElement_Release,
466 HTMLSelectElement_GetTypeInfoCount,
467 HTMLSelectElement_GetTypeInfo,
468 HTMLSelectElement_GetIDsOfNames,
469 HTMLSelectElement_Invoke,
470 HTMLSelectElement_put_size,
471 HTMLSelectElement_get_size,
472 HTMLSelectElement_put_multiple,
473 HTMLSelectElement_get_multiple,
474 HTMLSelectElement_put_name,
475 HTMLSelectElement_get_name,
476 HTMLSelectElement_get_options,
477 HTMLSelectElement_put_onchange,
478 HTMLSelectElement_get_onchange,
479 HTMLSelectElement_put_selectedIndex,
480 HTMLSelectElement_get_selectedIndex,
481 HTMLSelectElement_get_type,
482 HTMLSelectElement_put_value,
483 HTMLSelectElement_get_value,
484 HTMLSelectElement_put_disabled,
485 HTMLSelectElement_get_disabled,
486 HTMLSelectElement_get_form,
487 HTMLSelectElement_add,
488 HTMLSelectElement_remove,
489 HTMLSelectElement_put_length,
490 HTMLSelectElement_get_length,
491 HTMLSelectElement_get__newEnum,
492 HTMLSelectElement_item,
493 HTMLSelectElement_tags
496 #define HTMLSELECT_NODE_THIS(iface) DEFINE_THIS2(HTMLSelectElement, element.node, iface)
498 static HRESULT HTMLSelectElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
500 HTMLSelectElement *This = HTMLSELECT_NODE_THIS(iface);
502 *ppv = NULL;
504 if(IsEqualGUID(&IID_IUnknown, riid)) {
505 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
506 *ppv = HTMLSELECT(This);
507 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
508 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
509 *ppv = HTMLSELECT(This);
510 }else if(IsEqualGUID(&IID_IHTMLSelectElement, riid)) {
511 TRACE("(%p)->(IID_IHTMLSelectElement %p)\n", This, ppv);
512 *ppv = HTMLSELECT(This);
515 if(*ppv) {
516 IUnknown_AddRef((IUnknown*)*ppv);
517 return S_OK;
520 return HTMLElement_QI(&This->element.node, riid, ppv);
523 static void HTMLSelectElement_destructor(HTMLDOMNode *iface)
525 HTMLSelectElement *This = HTMLSELECT_NODE_THIS(iface);
527 nsIDOMHTMLSelectElement_Release(This->nsselect);
529 HTMLElement_destructor(&This->element.node);
532 static HRESULT HTMLSelectElementImpl_put_disabled(HTMLDOMNode *iface, VARIANT_BOOL v)
534 HTMLSelectElement *This = HTMLSELECT_NODE_THIS(iface);
535 return IHTMLSelectElement_put_disabled(HTMLSELECT(This), v);
538 static HRESULT HTMLSelectElementImpl_get_disabled(HTMLDOMNode *iface, VARIANT_BOOL *p)
540 HTMLSelectElement *This = HTMLSELECT_NODE_THIS(iface);
541 return IHTMLSelectElement_get_disabled(HTMLSELECT(This), p);
544 #define DISPID_OPTIONCOL_0 MSHTML_DISPID_CUSTOM_MIN
546 static HRESULT HTMLSelectElement_get_dispid(HTMLDOMNode *iface, BSTR name, DWORD flags, DISPID *dispid)
548 const WCHAR *ptr;
549 DWORD idx = 0;
551 for(ptr = name; *ptr && isdigitW(*ptr); ptr++) {
552 idx = idx*10 + (*ptr-'0');
553 if(idx > MSHTML_CUSTOM_DISPID_CNT) {
554 WARN("too big idx\n");
555 return DISP_E_UNKNOWNNAME;
558 if(*ptr)
559 return DISP_E_UNKNOWNNAME;
561 *dispid = DISPID_OPTIONCOL_0 + idx;
562 return S_OK;
565 static HRESULT HTMLSelectElement_invoke(HTMLDOMNode *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
566 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
568 HTMLSelectElement *This = HTMLSELECT_NODE_THIS(iface);
570 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
572 switch(flags) {
573 case DISPATCH_PROPERTYGET: {
574 IDispatch *ret;
575 HRESULT hres;
577 hres = htmlselect_item(This, id-DISPID_OPTIONCOL_0, &ret);
578 if(FAILED(hres))
579 return hres;
581 if(ret) {
582 V_VT(res) = VT_DISPATCH;
583 V_DISPATCH(res) = ret;
584 }else {
585 V_VT(res) = VT_NULL;
587 break;
590 default:
591 FIXME("unimplemented flags %x\n", flags);
592 return E_NOTIMPL;
595 return S_OK;
598 #undef HTMLSELECT_NODE_THIS
600 static const NodeImplVtbl HTMLSelectElementImplVtbl = {
601 HTMLSelectElement_QI,
602 HTMLSelectElement_destructor,
603 NULL,
604 NULL,
605 HTMLSelectElementImpl_put_disabled,
606 HTMLSelectElementImpl_get_disabled,
607 NULL,
608 NULL,
609 HTMLSelectElement_get_dispid,
610 HTMLSelectElement_invoke
613 static const tid_t HTMLSelectElement_tids[] = {
614 HTMLELEMENT_TIDS,
615 IHTMLSelectElement_tid,
619 static dispex_static_data_t HTMLSelectElement_dispex = {
620 NULL,
621 DispHTMLSelectElement_tid,
622 NULL,
623 HTMLSelectElement_tids
626 HTMLElement *HTMLSelectElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem)
628 HTMLSelectElement *ret = heap_alloc_zero(sizeof(HTMLSelectElement));
629 nsresult nsres;
631 ret->lpHTMLSelectElementVtbl = &HTMLSelectElementVtbl;
632 ret->element.node.vtbl = &HTMLSelectElementImplVtbl;
634 HTMLElement_Init(&ret->element, doc, nselem, &HTMLSelectElement_dispex);
636 nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLSelectElement,
637 (void**)&ret->nsselect);
638 if(NS_FAILED(nsres))
639 ERR("Could not get nsIDOMHTMLSelectElement interfce: %08x\n", nsres);
641 return &ret->element;