- Merge from trunk up to r45543
[reactos.git] / dll / win32 / 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 #define HTMLFORM_THIS(iface) DEFINE_THIS(HTMLFormElement, HTMLFormElement, iface)
45
46 static HRESULT WINAPI HTMLFormElement_QueryInterface(IHTMLFormElement *iface,
47 REFIID riid, void **ppv)
48 {
49 HTMLFormElement *This = HTMLFORM_THIS(iface);
50
51 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(&This->element.node), riid, ppv);
52 }
53
54 static ULONG WINAPI HTMLFormElement_AddRef(IHTMLFormElement *iface)
55 {
56 HTMLFormElement *This = HTMLFORM_THIS(iface);
57
58 return IHTMLDOMNode_AddRef(HTMLDOMNODE(&This->element.node));
59 }
60
61 static ULONG WINAPI HTMLFormElement_Release(IHTMLFormElement *iface)
62 {
63 HTMLFormElement *This = HTMLFORM_THIS(iface);
64
65 return IHTMLDOMNode_Release(HTMLDOMNODE(&This->element.node));
66 }
67
68 static HRESULT WINAPI HTMLFormElement_GetTypeInfoCount(IHTMLFormElement *iface, UINT *pctinfo)
69 {
70 HTMLFormElement *This = HTMLFORM_THIS(iface);
71 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->element.node.dispex), pctinfo);
72 }
73
74 static HRESULT WINAPI HTMLFormElement_GetTypeInfo(IHTMLFormElement *iface, UINT iTInfo,
75 LCID lcid, ITypeInfo **ppTInfo)
76 {
77 HTMLFormElement *This = HTMLFORM_THIS(iface);
78 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->element.node.dispex), iTInfo, lcid, ppTInfo);
79 }
80
81 static HRESULT WINAPI HTMLFormElement_GetIDsOfNames(IHTMLFormElement *iface, REFIID riid,
82 LPOLESTR *rgszNames, UINT cNames,
83 LCID lcid, DISPID *rgDispId)
84 {
85 HTMLFormElement *This = HTMLFORM_THIS(iface);
86 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->element.node.dispex), riid, rgszNames, cNames, lcid, rgDispId);
87 }
88
89 static HRESULT WINAPI HTMLFormElement_Invoke(IHTMLFormElement *iface, DISPID dispIdMember,
90 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
91 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
92 {
93 HTMLFormElement *This = HTMLFORM_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 HTMLFormElement_put_action(IHTMLFormElement *iface, BSTR v)
99 {
100 HTMLFormElement *This = HTMLFORM_THIS(iface);
101 FIXME("(%p)->(%s)\n", This, wine_dbgstr_w(v));
102 return E_NOTIMPL;
103 }
104
105 static HRESULT WINAPI HTMLFormElement_get_action(IHTMLFormElement *iface, BSTR *p)
106 {
107 HTMLFormElement *This = HTMLFORM_THIS(iface);
108 FIXME("(%p)->(%p)\n", This, p);
109 return E_NOTIMPL;
110 }
111
112 static HRESULT WINAPI HTMLFormElement_put_dir(IHTMLFormElement *iface, BSTR v)
113 {
114 HTMLFormElement *This = HTMLFORM_THIS(iface);
115 FIXME("(%p)->(%s)\n", This, wine_dbgstr_w(v));
116 return E_NOTIMPL;
117 }
118
119 static HRESULT WINAPI HTMLFormElement_get_dir(IHTMLFormElement *iface, BSTR *p)
120 {
121 HTMLFormElement *This = HTMLFORM_THIS(iface);
122 FIXME("(%p)->(%p)\n", This, p);
123 return E_NOTIMPL;
124 }
125
126 static HRESULT WINAPI HTMLFormElement_put_encoding(IHTMLFormElement *iface, BSTR v)
127 {
128 HTMLFormElement *This = HTMLFORM_THIS(iface);
129 FIXME("(%p)->(%s)\n", This, wine_dbgstr_w(v));
130 return E_NOTIMPL;
131 }
132
133 static HRESULT WINAPI HTMLFormElement_get_encoding(IHTMLFormElement *iface, BSTR *p)
134 {
135 HTMLFormElement *This = HTMLFORM_THIS(iface);
136 FIXME("(%p)->(%p)\n", This, p);
137 return E_NOTIMPL;
138 }
139
140 static HRESULT WINAPI HTMLFormElement_put_method(IHTMLFormElement *iface, BSTR v)
141 {
142 HTMLFormElement *This = HTMLFORM_THIS(iface);
143 FIXME("(%p)->(%s)\n", This, wine_dbgstr_w(v));
144 return E_NOTIMPL;
145 }
146
147 static HRESULT WINAPI HTMLFormElement_get_method(IHTMLFormElement *iface, BSTR *p)
148 {
149 HTMLFormElement *This = HTMLFORM_THIS(iface);
150 FIXME("(%p)->(%p)\n", This, p);
151 return E_NOTIMPL;
152 }
153
154 static HRESULT WINAPI HTMLFormElement_get_elements(IHTMLFormElement *iface, IDispatch **p)
155 {
156 HTMLFormElement *This = HTMLFORM_THIS(iface);
157 FIXME("(%p)->(%p)\n", This, p);
158 return E_NOTIMPL;
159 }
160
161 static HRESULT WINAPI HTMLFormElement_put_target(IHTMLFormElement *iface, BSTR v)
162 {
163 HTMLFormElement *This = HTMLFORM_THIS(iface);
164 FIXME("(%p)->(%s)\n", This, wine_dbgstr_w(v));
165 return E_NOTIMPL;
166 }
167
168 static HRESULT WINAPI HTMLFormElement_get_target(IHTMLFormElement *iface, BSTR *p)
169 {
170 HTMLFormElement *This = HTMLFORM_THIS(iface);
171 FIXME("(%p)->(%p)\n", This, p);
172 return E_NOTIMPL;
173 }
174
175 static HRESULT WINAPI HTMLFormElement_put_name(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_name(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_onsubmit(IHTMLFormElement *iface, VARIANT v)
190 {
191 HTMLFormElement *This = HTMLFORM_THIS(iface);
192 FIXME("(%p)->(v)\n", This);
193 return E_NOTIMPL;
194 }
195
196 static HRESULT WINAPI HTMLFormElement_get_onsubmit(IHTMLFormElement *iface, VARIANT *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_onreset(IHTMLFormElement *iface, VARIANT v)
204 {
205 HTMLFormElement *This = HTMLFORM_THIS(iface);
206 FIXME("(%p)->(v)\n", This);
207 return E_NOTIMPL;
208 }
209
210 static HRESULT WINAPI HTMLFormElement_get_onreset(IHTMLFormElement *iface, VARIANT *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_submit(IHTMLFormElement *iface)
218 {
219 HTMLFormElement *This = HTMLFORM_THIS(iface);
220 FIXME("(%p)->()\n", This);
221 return E_NOTIMPL;
222 }
223
224 static HRESULT WINAPI HTMLFormElement_reset(IHTMLFormElement *iface)
225 {
226 HTMLFormElement *This = HTMLFORM_THIS(iface);
227 FIXME("(%p)->()\n", This);
228 return E_NOTIMPL;
229 }
230
231 static HRESULT WINAPI HTMLFormElement_put_length(IHTMLFormElement *iface, LONG v)
232 {
233 HTMLFormElement *This = HTMLFORM_THIS(iface);
234 FIXME("(%p)->(%d)\n", This, v);
235 return E_NOTIMPL;
236 }
237
238 static HRESULT WINAPI HTMLFormElement_get_length(IHTMLFormElement *iface, LONG *p)
239 {
240 HTMLFormElement *This = HTMLFORM_THIS(iface);
241 FIXME("(%p)->(%p)\n", This, p);
242 return E_NOTIMPL;
243 }
244
245 static HRESULT WINAPI HTMLFormElement__newEnum(IHTMLFormElement *iface, IUnknown **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_item(IHTMLFormElement *iface, VARIANT name,
253 VARIANT index, IDispatch **pdisp)
254 {
255 HTMLFormElement *This = HTMLFORM_THIS(iface);
256 FIXME("(%p)->(v v %p)\n", This, pdisp);
257 return E_NOTIMPL;
258 }
259
260 static HRESULT WINAPI HTMLFormElement_tags(IHTMLFormElement *iface, VARIANT tagName,
261 IDispatch **pdisp)
262 {
263 HTMLFormElement *This = HTMLFORM_THIS(iface);
264 FIXME("(%p)->(v %p)\n", This, pdisp);
265 return E_NOTIMPL;
266 }
267
268 #undef HTMLFORM_THIS
269
270 static const IHTMLFormElementVtbl HTMLFormElementVtbl = {
271 HTMLFormElement_QueryInterface,
272 HTMLFormElement_AddRef,
273 HTMLFormElement_Release,
274 HTMLFormElement_GetTypeInfoCount,
275 HTMLFormElement_GetTypeInfo,
276 HTMLFormElement_GetIDsOfNames,
277 HTMLFormElement_Invoke,
278 HTMLFormElement_put_action,
279 HTMLFormElement_get_action,
280 HTMLFormElement_put_dir,
281 HTMLFormElement_get_dir,
282 HTMLFormElement_put_encoding,
283 HTMLFormElement_get_encoding,
284 HTMLFormElement_put_method,
285 HTMLFormElement_get_method,
286 HTMLFormElement_get_elements,
287 HTMLFormElement_put_target,
288 HTMLFormElement_get_target,
289 HTMLFormElement_put_name,
290 HTMLFormElement_get_name,
291 HTMLFormElement_put_onsubmit,
292 HTMLFormElement_get_onsubmit,
293 HTMLFormElement_put_onreset,
294 HTMLFormElement_get_onreset,
295 HTMLFormElement_submit,
296 HTMLFormElement_reset,
297 HTMLFormElement_put_length,
298 HTMLFormElement_get_length,
299 HTMLFormElement__newEnum,
300 HTMLFormElement_item,
301 HTMLFormElement_tags
302 };
303
304 #define HTMLFORM_NODE_THIS(iface) DEFINE_THIS2(HTMLFormElement, element.node, iface)
305
306 static HRESULT HTMLFormElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
307 {
308 HTMLFormElement *This = HTMLFORM_NODE_THIS(iface);
309
310 *ppv = NULL;
311
312 if(IsEqualGUID(&IID_IUnknown, riid)) {
313 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
314 *ppv = HTMLFORM(This);
315 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
316 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
317 *ppv = HTMLFORM(This);
318 }else if(IsEqualGUID(&IID_IHTMLFormElement, riid)) {
319 TRACE("(%p)->(IID_IHTMLFormElement %p)\n", This, ppv);
320 *ppv = HTMLFORM(This);
321 }
322
323 if(*ppv) {
324 IUnknown_AddRef((IUnknown*)*ppv);
325 return S_OK;
326 }
327
328 return HTMLElement_QI(&This->element.node, riid, ppv);
329 }
330
331 static void HTMLFormElement_destructor(HTMLDOMNode *iface)
332 {
333 HTMLFormElement *This = HTMLFORM_NODE_THIS(iface);
334
335 if(This->nsform)
336 nsIDOMHTMLFormElement_Release(This->nsform);
337
338 HTMLElement_destructor(&This->element.node);
339 }
340
341 static HRESULT HTMLFormElement_get_dispid(HTMLDOMNode *iface,
342 BSTR name, DWORD grfdex, DISPID *pid)
343 {
344 HTMLFormElement *This = HTMLFORM_NODE_THIS(iface);
345 nsIDOMHTMLCollection *elements;
346 PRUint32 len, i;
347 static const PRUnichar nameW[] = {'n','a','m','e',0};
348 nsAString nsname;
349 nsresult nsres;
350
351 TRACE("(%p)->(%s %x %p)\n", This, wine_dbgstr_w(name), grfdex, pid);
352
353 nsres = nsIDOMHTMLFormElement_GetElements(This->nsform, &elements);
354 if(NS_FAILED(nsres)) {
355 FIXME("GetElements failed: 0x%08x\n", nsres);
356 return E_FAIL;
357 }
358
359 nsres = nsIDOMHTMLCollection_GetLength(elements, &len);
360 if(NS_FAILED(nsres)) {
361 FIXME("GetLength failed: 0x%08x\n", nsres);
362 nsIDOMHTMLCollection_Release(elements);
363 return E_FAIL;
364 }
365
366 nsAString_Init(&nsname, nameW);
367 for(i = 0; i < len; ++i) {
368 nsIDOMNode *nsitem;
369 nsIDOMHTMLElement *nshtml_elem;
370 nsAString nsstr;
371 const PRUnichar *str;
372
373 nsres = nsIDOMHTMLCollection_Item(elements, i, &nsitem);
374 if(NS_FAILED(nsres)) {
375 FIXME("Item failed: 0x%08x\n", nsres);
376 nsAString_Finish(&nsname);
377 nsIDOMHTMLCollection_Release(elements);
378 return E_FAIL;
379 }
380
381 nsres = nsIDOMNode_QueryInterface(nsitem, &IID_nsIDOMHTMLElement, (void**)&nshtml_elem);
382 nsIDOMNode_Release(nsitem);
383 if(NS_FAILED(nsres)) {
384 FIXME("Failed to get nsIDOMHTMLNode interface: 0x%08x\n", nsres);
385 nsAString_Finish(&nsname);
386 nsIDOMHTMLCollection_Release(elements);
387 return E_FAIL;
388 }
389
390 /* compare by id attr */
391 nsAString_Init(&nsstr, NULL);
392 nsres = nsIDOMHTMLElement_GetId(nshtml_elem, &nsstr);
393 if(NS_FAILED(nsres)) {
394 FIXME("GetId failed: 0x%08x\n", nsres);
395 nsAString_Finish(&nsname);
396 nsIDOMHTMLElement_Release(nshtml_elem);
397 nsIDOMHTMLCollection_Release(elements);
398 return E_FAIL;
399 }
400 nsAString_GetData(&nsstr, &str);
401 if(!strcmpiW(str, name)) {
402 /* FIXME: using index for dispid */
403 *pid = MSHTML_DISPID_CUSTOM_MIN + i;
404 nsAString_Finish(&nsname);
405 nsAString_Finish(&nsstr);
406 nsIDOMHTMLElement_Release(nshtml_elem);
407 nsIDOMHTMLCollection_Release(elements);
408 return S_OK;
409 }
410
411 /* compare by name attr */
412 nsres = nsIDOMHTMLElement_GetAttribute(nshtml_elem, &nsname, &nsstr);
413 nsAString_GetData(&nsstr, &str);
414 if(!strcmpiW(str, name)) {
415 /* FIXME: using index for dispid */
416 *pid = MSHTML_DISPID_CUSTOM_MIN + i;
417 nsAString_Finish(&nsname);
418 nsAString_Finish(&nsstr);
419 nsIDOMHTMLElement_Release(nshtml_elem);
420 nsIDOMHTMLCollection_Release(elements);
421 return S_OK;
422 }
423 nsAString_Finish(&nsstr);
424
425 nsIDOMHTMLElement_Release(nshtml_elem);
426 }
427 nsAString_Finish(&nsname);
428
429 nsIDOMHTMLCollection_Release(elements);
430
431 return DISP_E_UNKNOWNNAME;
432 }
433
434 static HRESULT HTMLFormElement_invoke(HTMLDOMNode *iface,
435 DISPID id, LCID lcid, WORD flags, DISPPARAMS *params, VARIANT *res,
436 EXCEPINFO *ei, IServiceProvider *caller)
437 {
438 HTMLFormElement *This = HTMLFORM_NODE_THIS(iface);
439 nsIDOMHTMLCollection *elements;
440 nsIDOMNode *item;
441 HTMLDOMNode *node;
442 nsresult nsres;
443
444 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
445
446 nsres = nsIDOMHTMLFormElement_GetElements(This->nsform, &elements);
447 if(NS_FAILED(nsres)) {
448 FIXME("GetElements failed: 0x%08x\n", nsres);
449 return E_FAIL;
450 }
451
452 nsres = nsIDOMHTMLCollection_Item(elements, id - MSHTML_DISPID_CUSTOM_MIN, &item);
453 nsIDOMHTMLCollection_Release(elements);
454 if(NS_FAILED(nsres)) {
455 FIXME("Item failed: 0x%08x\n", nsres);
456 return E_FAIL;
457 }
458
459 node = get_node(This->element.node.doc, item, TRUE);
460
461 V_VT(res) = VT_DISPATCH;
462 V_DISPATCH(res) = (IDispatch*)node;
463
464 IHTMLDOMNode_AddRef(HTMLDOMNODE(node));
465 nsIDOMNode_Release(item);
466
467 return S_OK;
468 }
469
470 #undef HTMLFORM_NODE_THIS
471
472 static const NodeImplVtbl HTMLFormElementImplVtbl = {
473 HTMLFormElement_QI,
474 HTMLFormElement_destructor,
475 NULL,
476 NULL,
477 NULL,
478 NULL,
479 NULL,
480 NULL,
481 HTMLFormElement_get_dispid,
482 HTMLFormElement_invoke
483 };
484
485 static const tid_t HTMLFormElement_iface_tids[] = {
486 IHTMLDOMNode_tid,
487 IHTMLDOMNode2_tid,
488 IHTMLElement_tid,
489 IHTMLElement2_tid,
490 IHTMLElement3_tid,
491 IHTMLFormElement_tid,
492 0
493 };
494
495 static dispex_static_data_t HTMLFormElement_dispex = {
496 NULL,
497 DispHTMLFormElement_tid,
498 NULL,
499 HTMLFormElement_iface_tids
500 };
501
502 HTMLElement *HTMLFormElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem)
503 {
504 HTMLFormElement *ret = heap_alloc_zero(sizeof(HTMLFormElement));
505 nsresult nsres;
506
507 ret->lpHTMLFormElementVtbl = &HTMLFormElementVtbl;
508 ret->element.node.vtbl = &HTMLFormElementImplVtbl;
509
510 HTMLElement_Init(&ret->element, doc, nselem, &HTMLFormElement_dispex);
511
512 nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLFormElement, (void**)&ret->nsform);
513 if(NS_FAILED(nsres))
514 ERR("Could not get nsIDOMHTMLFormElement interface: %08x\n", nsres);
515
516 return &ret->element;
517 }