xinput1_3: Quiet a noisy fixme.
[wine] / dlls / mshtml / htmlform.c
1 /*
2  * Copyright 2009 Andrew Eikum 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 HTMLFormElement {
35     HTMLElement element;
36
37     const IHTMLFormElementVtbl *lpHTMLFormElementVtbl;
38
39     nsIDOMHTMLFormElement *nsform;
40 };
41
42 #define HTMLFORM(x)  (&(x)->lpHTMLFormElementVtbl)
43
44 static HRESULT htmlform_item(HTMLFormElement *This, int i, IDispatch **ret)
45 {
46     nsIDOMHTMLCollection *elements;
47     nsIDOMNode *item;
48     HTMLDOMNode *node;
49     nsresult nsres;
50
51     nsres = nsIDOMHTMLFormElement_GetElements(This->nsform, &elements);
52     if(NS_FAILED(nsres)) {
53         FIXME("GetElements failed: 0x%08x\n", nsres);
54         return E_FAIL;
55     }
56
57     nsres = nsIDOMHTMLCollection_Item(elements, i, &item);
58     nsIDOMHTMLCollection_Release(elements);
59     if(NS_FAILED(nsres)) {
60         FIXME("Item failed: 0x%08x\n", nsres);
61         return E_FAIL;
62     }
63
64     if(item) {
65         node = get_node(This->element.node.doc, item, TRUE);
66         if(!node)
67             return E_OUTOFMEMORY;
68
69         IHTMLDOMNode_AddRef(HTMLDOMNODE(node));
70         nsIDOMNode_Release(item);
71         *ret = (IDispatch*)HTMLDOMNODE(node);
72     }else {
73         *ret = NULL;
74     }
75
76     return S_OK;
77 }
78
79 #define HTMLFORM_THIS(iface) DEFINE_THIS(HTMLFormElement, HTMLFormElement, iface)
80
81 static HRESULT WINAPI HTMLFormElement_QueryInterface(IHTMLFormElement *iface,
82         REFIID riid, void **ppv)
83 {
84     HTMLFormElement *This = HTMLFORM_THIS(iface);
85
86     return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(&This->element.node), riid, ppv);
87 }
88
89 static ULONG WINAPI HTMLFormElement_AddRef(IHTMLFormElement *iface)
90 {
91     HTMLFormElement *This = HTMLFORM_THIS(iface);
92
93     return IHTMLDOMNode_AddRef(HTMLDOMNODE(&This->element.node));
94 }
95
96 static ULONG WINAPI HTMLFormElement_Release(IHTMLFormElement *iface)
97 {
98     HTMLFormElement *This = HTMLFORM_THIS(iface);
99
100     return IHTMLDOMNode_Release(HTMLDOMNODE(&This->element.node));
101 }
102
103 static HRESULT WINAPI HTMLFormElement_GetTypeInfoCount(IHTMLFormElement *iface, UINT *pctinfo)
104 {
105     HTMLFormElement *This = HTMLFORM_THIS(iface);
106     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->element.node.dispex), pctinfo);
107 }
108
109 static HRESULT WINAPI HTMLFormElement_GetTypeInfo(IHTMLFormElement *iface, UINT iTInfo,
110                                               LCID lcid, ITypeInfo **ppTInfo)
111 {
112     HTMLFormElement *This = HTMLFORM_THIS(iface);
113     return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->element.node.dispex), iTInfo, lcid, ppTInfo);
114 }
115
116 static HRESULT WINAPI HTMLFormElement_GetIDsOfNames(IHTMLFormElement *iface, REFIID riid,
117                                                 LPOLESTR *rgszNames, UINT cNames,
118                                                 LCID lcid, DISPID *rgDispId)
119 {
120     HTMLFormElement *This = HTMLFORM_THIS(iface);
121     return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->element.node.dispex), riid, rgszNames, cNames, lcid, rgDispId);
122 }
123
124 static HRESULT WINAPI HTMLFormElement_Invoke(IHTMLFormElement *iface, DISPID dispIdMember,
125                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
126                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
127 {
128     HTMLFormElement *This = HTMLFORM_THIS(iface);
129     return IDispatchEx_Invoke(DISPATCHEX(&This->element.node.dispex), dispIdMember, riid, lcid, wFlags, pDispParams,
130             pVarResult, pExcepInfo, puArgErr);
131 }
132
133 static HRESULT WINAPI HTMLFormElement_put_action(IHTMLFormElement *iface, BSTR v)
134 {
135     HTMLFormElement *This = HTMLFORM_THIS(iface);
136     nsAString action_str;
137     nsresult nsres;
138
139     TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
140
141     nsAString_InitDepend(&action_str, v);
142     nsres = nsIDOMHTMLFormElement_SetAction(This->nsform, &action_str);
143     nsAString_Finish(&action_str);
144     if(NS_FAILED(nsres)) {
145         ERR("SetAction failed: %08x\n", nsres);
146         return E_FAIL;
147     }
148
149     return S_OK;
150 }
151
152 static HRESULT WINAPI HTMLFormElement_get_action(IHTMLFormElement *iface, BSTR *p)
153 {
154     HTMLFormElement *This = HTMLFORM_THIS(iface);
155     nsAString action_str;
156     nsresult nsres;
157     HRESULT hres;
158
159     TRACE("(%p)->(%p)\n", This, p);
160
161     nsAString_Init(&action_str, NULL);
162     nsres = nsIDOMHTMLFormElement_GetAction(This->nsform, &action_str);
163     if(NS_SUCCEEDED(nsres)) {
164         const PRUnichar *action;
165         nsAString_GetData(&action_str, &action);
166         hres = nsuri_to_url(action, FALSE, p);
167     }else {
168         ERR("GetAction failed: %08x\n", nsres);
169         hres = E_FAIL;
170     }
171
172     return hres;
173 }
174
175 static HRESULT WINAPI HTMLFormElement_put_dir(IHTMLFormElement *iface, BSTR v)
176 {
177     HTMLFormElement *This = HTMLFORM_THIS(iface);
178     FIXME("(%p)->(%s)\n", This, wine_dbgstr_w(v));
179     return E_NOTIMPL;
180 }
181
182 static HRESULT WINAPI HTMLFormElement_get_dir(IHTMLFormElement *iface, BSTR *p)
183 {
184     HTMLFormElement *This = HTMLFORM_THIS(iface);
185     FIXME("(%p)->(%p)\n", This, p);
186     return E_NOTIMPL;
187 }
188
189 static HRESULT WINAPI HTMLFormElement_put_encoding(IHTMLFormElement *iface, BSTR v)
190 {
191     HTMLFormElement *This = HTMLFORM_THIS(iface);
192     FIXME("(%p)->(%s)\n", This, wine_dbgstr_w(v));
193     return E_NOTIMPL;
194 }
195
196 static HRESULT WINAPI HTMLFormElement_get_encoding(IHTMLFormElement *iface, BSTR *p)
197 {
198     HTMLFormElement *This = HTMLFORM_THIS(iface);
199     FIXME("(%p)->(%p)\n", This, p);
200     return E_NOTIMPL;
201 }
202
203 static HRESULT WINAPI HTMLFormElement_put_method(IHTMLFormElement *iface, BSTR v)
204 {
205     HTMLFormElement *This = HTMLFORM_THIS(iface);
206     FIXME("(%p)->(%s)\n", This, wine_dbgstr_w(v));
207     return E_NOTIMPL;
208 }
209
210 static HRESULT WINAPI HTMLFormElement_get_method(IHTMLFormElement *iface, BSTR *p)
211 {
212     HTMLFormElement *This = HTMLFORM_THIS(iface);
213     FIXME("(%p)->(%p)\n", This, p);
214     return E_NOTIMPL;
215 }
216
217 static HRESULT WINAPI HTMLFormElement_get_elements(IHTMLFormElement *iface, IDispatch **p)
218 {
219     HTMLFormElement *This = HTMLFORM_THIS(iface);
220     FIXME("(%p)->(%p)\n", This, p);
221     return E_NOTIMPL;
222 }
223
224 static HRESULT WINAPI HTMLFormElement_put_target(IHTMLFormElement *iface, BSTR v)
225 {
226     HTMLFormElement *This = HTMLFORM_THIS(iface);
227     FIXME("(%p)->(%s)\n", This, wine_dbgstr_w(v));
228     return E_NOTIMPL;
229 }
230
231 static HRESULT WINAPI HTMLFormElement_get_target(IHTMLFormElement *iface, BSTR *p)
232 {
233     HTMLFormElement *This = HTMLFORM_THIS(iface);
234     FIXME("(%p)->(%p)\n", This, p);
235     return E_NOTIMPL;
236 }
237
238 static HRESULT WINAPI HTMLFormElement_put_name(IHTMLFormElement *iface, BSTR v)
239 {
240     HTMLFormElement *This = HTMLFORM_THIS(iface);
241     FIXME("(%p)->(%s)\n", This, wine_dbgstr_w(v));
242     return E_NOTIMPL;
243 }
244
245 static HRESULT WINAPI HTMLFormElement_get_name(IHTMLFormElement *iface, BSTR *p)
246 {
247     HTMLFormElement *This = HTMLFORM_THIS(iface);
248     FIXME("(%p)->(%p)\n", This, p);
249     return E_NOTIMPL;
250 }
251
252 static HRESULT WINAPI HTMLFormElement_put_onsubmit(IHTMLFormElement *iface, VARIANT v)
253 {
254     HTMLFormElement *This = HTMLFORM_THIS(iface);
255     FIXME("(%p)->(v)\n", This);
256     return E_NOTIMPL;
257 }
258
259 static HRESULT WINAPI HTMLFormElement_get_onsubmit(IHTMLFormElement *iface, VARIANT *p)
260 {
261     HTMLFormElement *This = HTMLFORM_THIS(iface);
262     FIXME("(%p)->(%p)\n", This, p);
263     return E_NOTIMPL;
264 }
265
266 static HRESULT WINAPI HTMLFormElement_put_onreset(IHTMLFormElement *iface, VARIANT v)
267 {
268     HTMLFormElement *This = HTMLFORM_THIS(iface);
269     FIXME("(%p)->(v)\n", This);
270     return E_NOTIMPL;
271 }
272
273 static HRESULT WINAPI HTMLFormElement_get_onreset(IHTMLFormElement *iface, VARIANT *p)
274 {
275     HTMLFormElement *This = HTMLFORM_THIS(iface);
276     FIXME("(%p)->(%p)\n", This, p);
277     return E_NOTIMPL;
278 }
279
280 static HRESULT WINAPI HTMLFormElement_submit(IHTMLFormElement *iface)
281 {
282     HTMLFormElement *This = HTMLFORM_THIS(iface);
283     FIXME("(%p)->()\n", This);
284     return E_NOTIMPL;
285 }
286
287 static HRESULT WINAPI HTMLFormElement_reset(IHTMLFormElement *iface)
288 {
289     HTMLFormElement *This = HTMLFORM_THIS(iface);
290     FIXME("(%p)->()\n", This);
291     return E_NOTIMPL;
292 }
293
294 static HRESULT WINAPI HTMLFormElement_put_length(IHTMLFormElement *iface, LONG v)
295 {
296     HTMLFormElement *This = HTMLFORM_THIS(iface);
297     FIXME("(%p)->(%d)\n", This, v);
298     return E_NOTIMPL;
299 }
300
301 static HRESULT WINAPI HTMLFormElement_get_length(IHTMLFormElement *iface, LONG *p)
302 {
303     HTMLFormElement *This = HTMLFORM_THIS(iface);
304     PRInt32 length;
305     nsresult nsres;
306
307     TRACE("(%p)->(%p)\n", This, p);
308
309     nsres = nsIDOMHTMLFormElement_GetLength(This->nsform, &length);
310     if(NS_FAILED(nsres)) {
311         ERR("GetLength failed: %08x\n", nsres);
312         return E_FAIL;
313     }
314
315     *p = length;
316     return S_OK;
317 }
318
319 static HRESULT WINAPI HTMLFormElement__newEnum(IHTMLFormElement *iface, IUnknown **p)
320 {
321     HTMLFormElement *This = HTMLFORM_THIS(iface);
322     FIXME("(%p)->(%p)\n", This, p);
323     return E_NOTIMPL;
324 }
325
326 static HRESULT WINAPI HTMLFormElement_item(IHTMLFormElement *iface, VARIANT name,
327         VARIANT index, IDispatch **pdisp)
328 {
329     HTMLFormElement *This = HTMLFORM_THIS(iface);
330
331     TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
332
333     if(!pdisp)
334         return E_INVALIDARG;
335     *pdisp = NULL;
336
337     if(V_VT(&name) == VT_I4) {
338         if(V_I4(&name) < 0)
339             return E_INVALIDARG;
340         return htmlform_item(This, V_I4(&name), pdisp);
341     }
342
343     FIXME("Unsupported args\n");
344     return E_NOTIMPL;
345 }
346
347 static HRESULT WINAPI HTMLFormElement_tags(IHTMLFormElement *iface, VARIANT tagName,
348         IDispatch **pdisp)
349 {
350     HTMLFormElement *This = HTMLFORM_THIS(iface);
351     FIXME("(%p)->(v %p)\n", This, pdisp);
352     return E_NOTIMPL;
353 }
354
355 #undef HTMLFORM_THIS
356
357 static const IHTMLFormElementVtbl HTMLFormElementVtbl = {
358     HTMLFormElement_QueryInterface,
359     HTMLFormElement_AddRef,
360     HTMLFormElement_Release,
361     HTMLFormElement_GetTypeInfoCount,
362     HTMLFormElement_GetTypeInfo,
363     HTMLFormElement_GetIDsOfNames,
364     HTMLFormElement_Invoke,
365     HTMLFormElement_put_action,
366     HTMLFormElement_get_action,
367     HTMLFormElement_put_dir,
368     HTMLFormElement_get_dir,
369     HTMLFormElement_put_encoding,
370     HTMLFormElement_get_encoding,
371     HTMLFormElement_put_method,
372     HTMLFormElement_get_method,
373     HTMLFormElement_get_elements,
374     HTMLFormElement_put_target,
375     HTMLFormElement_get_target,
376     HTMLFormElement_put_name,
377     HTMLFormElement_get_name,
378     HTMLFormElement_put_onsubmit,
379     HTMLFormElement_get_onsubmit,
380     HTMLFormElement_put_onreset,
381     HTMLFormElement_get_onreset,
382     HTMLFormElement_submit,
383     HTMLFormElement_reset,
384     HTMLFormElement_put_length,
385     HTMLFormElement_get_length,
386     HTMLFormElement__newEnum,
387     HTMLFormElement_item,
388     HTMLFormElement_tags
389 };
390
391 #define HTMLFORM_NODE_THIS(iface) DEFINE_THIS2(HTMLFormElement, element.node, iface)
392
393 static HRESULT HTMLFormElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
394 {
395     HTMLFormElement *This = HTMLFORM_NODE_THIS(iface);
396
397     *ppv = NULL;
398
399     if(IsEqualGUID(&IID_IUnknown, riid)) {
400         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
401         *ppv = HTMLFORM(This);
402     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
403         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
404         *ppv = HTMLFORM(This);
405     }else if(IsEqualGUID(&IID_IHTMLFormElement, riid)) {
406         TRACE("(%p)->(IID_IHTMLFormElement %p)\n", This, ppv);
407         *ppv = HTMLFORM(This);
408     }
409
410     if(*ppv) {
411         IUnknown_AddRef((IUnknown*)*ppv);
412         return S_OK;
413     }
414
415     return HTMLElement_QI(&This->element.node, riid, ppv);
416 }
417
418 static void HTMLFormElement_destructor(HTMLDOMNode *iface)
419 {
420     HTMLFormElement *This = HTMLFORM_NODE_THIS(iface);
421
422     if(This->nsform)
423         nsIDOMHTMLFormElement_Release(This->nsform);
424
425     HTMLElement_destructor(&This->element.node);
426 }
427
428 static HRESULT HTMLFormElement_get_dispid(HTMLDOMNode *iface,
429         BSTR name, DWORD grfdex, DISPID *pid)
430 {
431     HTMLFormElement *This = HTMLFORM_NODE_THIS(iface);
432     nsIDOMHTMLCollection *elements;
433     nsAString nsname, nsstr;
434     PRUint32 len, i;
435     nsresult nsres;
436     HRESULT hres = DISP_E_UNKNOWNNAME;
437
438     static const PRUnichar nameW[] = {'n','a','m','e',0};
439
440     TRACE("(%p)->(%s %x %p)\n", This, wine_dbgstr_w(name), grfdex, pid);
441
442     nsres = nsIDOMHTMLFormElement_GetElements(This->nsform, &elements);
443     if(NS_FAILED(nsres)) {
444         FIXME("GetElements failed: 0x%08x\n", nsres);
445         return E_FAIL;
446     }
447
448     nsres = nsIDOMHTMLCollection_GetLength(elements, &len);
449     if(NS_FAILED(nsres)) {
450         FIXME("GetLength failed: 0x%08x\n", nsres);
451         nsIDOMHTMLCollection_Release(elements);
452         return E_FAIL;
453     }
454
455     nsAString_InitDepend(&nsname, nameW);
456     nsAString_Init(&nsstr, NULL);
457     for(i = 0; i < len; ++i) {
458         nsIDOMNode *nsitem;
459         nsIDOMHTMLElement *nshtml_elem;
460         const PRUnichar *str;
461
462         nsres = nsIDOMHTMLCollection_Item(elements, i, &nsitem);
463         if(NS_FAILED(nsres)) {
464             FIXME("Item failed: 0x%08x\n", nsres);
465             hres = E_FAIL;
466             break;
467         }
468
469         nsres = nsIDOMNode_QueryInterface(nsitem, &IID_nsIDOMHTMLElement, (void**)&nshtml_elem);
470         nsIDOMNode_Release(nsitem);
471         if(NS_FAILED(nsres)) {
472             FIXME("Failed to get nsIDOMHTMLNode interface: 0x%08x\n", nsres);
473             hres = E_FAIL;
474             break;
475         }
476
477         /* compare by id attr */
478         nsres = nsIDOMHTMLElement_GetId(nshtml_elem, &nsstr);
479         if(NS_FAILED(nsres)) {
480             FIXME("GetId failed: 0x%08x\n", nsres);
481             nsIDOMHTMLElement_Release(nshtml_elem);
482             hres = E_FAIL;
483             break;
484         }
485         nsAString_GetData(&nsstr, &str);
486         if(!strcmpiW(str, name)) {
487             nsIDOMHTMLElement_Release(nshtml_elem);
488             /* FIXME: using index for dispid */
489             *pid = MSHTML_DISPID_CUSTOM_MIN + i;
490             hres = S_OK;
491             break;
492         }
493
494         /* compare by name attr */
495         nsres = nsIDOMHTMLElement_GetAttribute(nshtml_elem, &nsname, &nsstr);
496         nsIDOMHTMLElement_Release(nshtml_elem);
497         nsAString_GetData(&nsstr, &str);
498         if(!strcmpiW(str, name)) {
499             /* FIXME: using index for dispid */
500             *pid = MSHTML_DISPID_CUSTOM_MIN + i;
501             hres = S_OK;
502             break;
503         }
504     }
505     nsAString_Finish(&nsname);
506     nsAString_Finish(&nsstr);
507
508     nsIDOMHTMLCollection_Release(elements);
509
510     return hres;
511 }
512
513 static HRESULT HTMLFormElement_invoke(HTMLDOMNode *iface,
514         DISPID id, LCID lcid, WORD flags, DISPPARAMS *params, VARIANT *res,
515         EXCEPINFO *ei, IServiceProvider *caller)
516 {
517     HTMLFormElement *This = HTMLFORM_NODE_THIS(iface);
518     IDispatch *ret;
519     HRESULT hres;
520
521     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
522
523     hres = htmlform_item(This, id - MSHTML_DISPID_CUSTOM_MIN, &ret);
524     if(FAILED(hres))
525         return hres;
526
527     if(ret) {
528         V_VT(res) = VT_DISPATCH;
529         V_DISPATCH(res) = ret;
530     }else {
531         V_VT(res) = VT_NULL;
532     }
533     return S_OK;
534 }
535
536 #undef HTMLFORM_NODE_THIS
537
538 static const NodeImplVtbl HTMLFormElementImplVtbl = {
539     HTMLFormElement_QI,
540     HTMLFormElement_destructor,
541     NULL,
542     NULL,
543     NULL,
544     NULL,
545     NULL,
546     NULL,
547     HTMLFormElement_get_dispid,
548     HTMLFormElement_invoke
549 };
550
551 static const tid_t HTMLFormElement_iface_tids[] = {
552     HTMLELEMENT_TIDS,
553     IHTMLFormElement_tid,
554     0
555 };
556
557 static dispex_static_data_t HTMLFormElement_dispex = {
558     NULL,
559     DispHTMLFormElement_tid,
560     NULL,
561     HTMLFormElement_iface_tids
562 };
563
564 HTMLElement *HTMLFormElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem)
565 {
566     HTMLFormElement *ret = heap_alloc_zero(sizeof(HTMLFormElement));
567     nsresult nsres;
568
569     ret->lpHTMLFormElementVtbl = &HTMLFormElementVtbl;
570     ret->element.node.vtbl = &HTMLFormElementImplVtbl;
571
572     HTMLElement_Init(&ret->element, doc, nselem, &HTMLFormElement_dispex);
573
574     nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLFormElement, (void**)&ret->nsform);
575     if(NS_FAILED(nsres))
576         ERR("Could not get nsIDOMHTMLFormElement interface: %08x\n", nsres);
577
578     return &ret->element;
579 }