joy.cpl: Remove uneeded FIXME message.
[wine] / dlls / mshtml / htmlwindow.c
1 /*
2  * Copyright 2006-2012 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 #include <assert.h>
21
22 #define COBJMACROS
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
28 #include "mshtmdid.h"
29 #include "shlguid.h"
30 #include "shobjidl.h"
31
32 #define NO_SHLWAPI_REG
33 #include "shlwapi.h"
34
35 #include "wine/debug.h"
36
37 #include "mshtml_private.h"
38 #include "htmlevent.h"
39 #include "binding.h"
40 #include "resource.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
43
44 static struct list window_list = LIST_INIT(window_list);
45
46 static inline BOOL is_outer_window(HTMLWindow *window)
47 {
48     return &window->outer_window->base == window;
49 }
50
51 static void release_children(HTMLOuterWindow *This)
52 {
53     HTMLOuterWindow *child;
54
55     while(!list_empty(&This->children)) {
56         child = LIST_ENTRY(list_tail(&This->children), HTMLOuterWindow, sibling_entry);
57
58         list_remove(&child->sibling_entry);
59         child->parent = NULL;
60         IHTMLWindow2_Release(&child->base.IHTMLWindow2_iface);
61     }
62 }
63
64 static HRESULT get_location(HTMLOuterWindow *This, HTMLLocation **ret)
65 {
66     if(This->location) {
67         IHTMLLocation_AddRef(&This->location->IHTMLLocation_iface);
68     }else {
69         HRESULT hres;
70
71         hres = HTMLLocation_Create(This, &This->location);
72         if(FAILED(hres))
73             return hres;
74     }
75
76     *ret = This->location;
77     return S_OK;
78 }
79
80 void get_top_window(HTMLOuterWindow *window, HTMLOuterWindow **ret)
81 {
82     HTMLOuterWindow *iter;
83
84     for(iter = window; iter->parent; iter = iter->parent);
85     *ret = iter;
86 }
87
88 static inline HRESULT set_window_event(HTMLWindow *window, eventid_t eid, VARIANT *var)
89 {
90     if(!window->inner_window->doc) {
91         FIXME("No document\n");
92         return E_FAIL;
93     }
94
95     return set_event_handler(&window->inner_window->doc->body_event_target, NULL, window->inner_window->doc, eid, var);
96 }
97
98 static inline HRESULT get_window_event(HTMLWindow *window, eventid_t eid, VARIANT *var)
99 {
100     if(!window->inner_window->doc) {
101         FIXME("No document\n");
102         return E_FAIL;
103     }
104
105     return get_event_handler(&window->inner_window->doc->body_event_target, eid, var);
106 }
107
108 static void detach_inner_window(HTMLOuterWindow *outer_window)
109 {
110     HTMLInnerWindow *window = outer_window->base.inner_window;
111
112     if(!window)
113         return;
114
115     if(outer_window->doc_obj && outer_window == outer_window->doc_obj->basedoc.window)
116         window->doc->basedoc.cp_container.forward_container = NULL;
117
118     detach_events(window->doc);
119     abort_window_bindings(window);
120     release_script_hosts(window);
121     window->doc->basedoc.window = NULL;
122     window->base.outer_window = NULL;
123
124     IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
125 }
126
127 static inline HTMLWindow *impl_from_IHTMLWindow2(IHTMLWindow2 *iface)
128 {
129     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow2_iface);
130 }
131
132 static HRESULT WINAPI HTMLWindow2_QueryInterface(IHTMLWindow2 *iface, REFIID riid, void **ppv)
133 {
134     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
135
136     *ppv = NULL;
137
138     if(IsEqualGUID(&IID_IUnknown, riid)) {
139         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
140         *ppv = &This->IHTMLWindow2_iface;
141     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
142         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
143         *ppv = &This->IHTMLWindow2_iface;
144     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
145         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
146         *ppv = &This->IDispatchEx_iface;
147     }else if(IsEqualGUID(&IID_IHTMLFramesCollection2, riid)) {
148         TRACE("(%p)->(IID_IHTMLFramesCollection2 %p)\n", This, ppv);
149         *ppv = &This->IHTMLWindow2_iface;
150     }else if(IsEqualGUID(&IID_IHTMLWindow2, riid)) {
151         TRACE("(%p)->(IID_IHTMLWindow2 %p)\n", This, ppv);
152         *ppv = &This->IHTMLWindow2_iface;
153     }else if(IsEqualGUID(&IID_IHTMLWindow3, riid)) {
154         TRACE("(%p)->(IID_IHTMLWindow3 %p)\n", This, ppv);
155         *ppv = &This->IHTMLWindow3_iface;
156     }else if(IsEqualGUID(&IID_IHTMLWindow4, riid)) {
157         TRACE("(%p)->(IID_IHTMLWindow4 %p)\n", This, ppv);
158         *ppv = &This->IHTMLWindow4_iface;
159     }else if(IsEqualGUID(&IID_IHTMLWindow5, riid)) {
160         TRACE("(%p)->(IID_IHTMLWindow5 %p)\n", This, ppv);
161         *ppv = &This->IHTMLWindow5_iface;
162     }else if(IsEqualGUID(&IID_IHTMLWindow6, riid)) {
163         TRACE("(%p)->(IID_IHTMLWindow6 %p)\n", This, ppv);
164         *ppv = &This->IHTMLWindow6_iface;
165     }else if(IsEqualGUID(&IID_IHTMLPrivateWindow, riid)) {
166         TRACE("(%p)->(IID_IHTMLPrivateWindow %p)\n", This, ppv);
167         *ppv = &This->IHTMLPrivateWindow_iface;
168     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
169         TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
170         *ppv = &This->IServiceProvider_iface;
171     }else if(IsEqualGUID(&IID_ITravelLogClient, riid)) {
172         TRACE("(%p)->(IID_ITravelLogClient %p)\n", This, ppv);
173         *ppv = &This->ITravelLogClient_iface;
174     }else if(dispex_query_interface(&This->inner_window->dispex, riid, ppv)) {
175         assert(!*ppv);
176         return E_NOINTERFACE;
177     }
178
179     if(*ppv) {
180         IUnknown_AddRef((IUnknown*)*ppv);
181         return S_OK;
182     }
183
184     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
185     return E_NOINTERFACE;
186 }
187
188 static ULONG WINAPI HTMLWindow2_AddRef(IHTMLWindow2 *iface)
189 {
190     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
191     LONG ref = InterlockedIncrement(&This->ref);
192
193     TRACE("(%p) ref=%d\n", This, ref);
194
195     return ref;
196 }
197
198 static void release_outer_window(HTMLOuterWindow *This)
199 {
200     if(This->pending_window) {
201         abort_window_bindings(This->pending_window);
202         This->pending_window->base.outer_window = NULL;
203         IHTMLWindow2_Release(&This->pending_window->base.IHTMLWindow2_iface);
204     }
205
206     remove_target_tasks(This->task_magic);
207     set_current_mon(This, NULL);
208     detach_inner_window(This);
209     release_children(This);
210
211     if(This->secmgr)
212         IInternetSecurityManager_Release(This->secmgr);
213
214     if(This->frame_element)
215         This->frame_element->content_window = NULL;
216
217     if(This->location) {
218         This->location->window = NULL;
219         IHTMLLocation_Release(&This->location->IHTMLLocation_iface);
220     }
221
222     This->window_ref->window = NULL;
223     windowref_release(This->window_ref);
224
225     if(This->nswindow)
226         nsIDOMWindow_Release(This->nswindow);
227
228     list_remove(&This->entry);
229     heap_free(This);
230 }
231
232 static void release_inner_window(HTMLInnerWindow *This)
233 {
234     unsigned i;
235
236     TRACE("%p\n", This);
237
238     remove_target_tasks(This->task_magic);
239     abort_window_bindings(This);
240     release_script_hosts(This);
241
242     if(This->doc) {
243         This->doc->window = NULL;
244         htmldoc_release(&This->doc->basedoc);
245     }
246
247     release_dispex(&This->dispex);
248
249     for(i=0; i < This->global_prop_cnt; i++)
250         heap_free(This->global_props[i].name);
251     heap_free(This->global_props);
252
253     if(This->image_factory) {
254         This->image_factory->window = NULL;
255         IHTMLImageElementFactory_Release(&This->image_factory->IHTMLImageElementFactory_iface);
256     }
257
258     if(This->option_factory) {
259         This->option_factory->window = NULL;
260         IHTMLOptionElementFactory_Release(&This->option_factory->IHTMLOptionElementFactory_iface);
261     }
262
263     if(This->screen)
264         IHTMLScreen_Release(This->screen);
265     if(This->history)
266         IOmHistory_Release(This->history);
267
268     heap_free(This);
269 }
270
271 static ULONG WINAPI HTMLWindow2_Release(IHTMLWindow2 *iface)
272 {
273     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
274     LONG ref = InterlockedDecrement(&This->ref);
275
276     TRACE("(%p) ref=%d\n", This, ref);
277
278     if(!ref) {
279         if(is_outer_window(This))
280             release_outer_window(This->outer_window);
281         else
282             release_inner_window(This->inner_window);
283     }
284
285     return ref;
286 }
287
288 static HRESULT WINAPI HTMLWindow2_GetTypeInfoCount(IHTMLWindow2 *iface, UINT *pctinfo)
289 {
290     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
291
292     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
293 }
294
295 static HRESULT WINAPI HTMLWindow2_GetTypeInfo(IHTMLWindow2 *iface, UINT iTInfo,
296                                               LCID lcid, ITypeInfo **ppTInfo)
297 {
298     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
299
300     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
301 }
302
303 static HRESULT WINAPI HTMLWindow2_GetIDsOfNames(IHTMLWindow2 *iface, REFIID riid,
304                                                 LPOLESTR *rgszNames, UINT cNames,
305                                                 LCID lcid, DISPID *rgDispId)
306 {
307     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
308
309     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
310             rgDispId);
311 }
312
313 static HRESULT WINAPI HTMLWindow2_Invoke(IHTMLWindow2 *iface, DISPID dispIdMember,
314                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
315                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
316 {
317     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
318
319     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
320             pDispParams, pVarResult, pExcepInfo, puArgErr);
321 }
322
323 static HRESULT get_frame_by_index(nsIDOMWindowCollection *nsFrames, PRUint32 index, HTMLOuterWindow **ret)
324 {
325     PRUint32 length;
326     nsIDOMWindow *nsWindow;
327     nsresult nsres;
328
329     nsres = nsIDOMWindowCollection_GetLength(nsFrames, &length);
330     if(NS_FAILED(nsres)) {
331         FIXME("nsIDOMWindowCollection_GetLength failed: 0x%08x\n", nsres);
332         return E_FAIL;
333     }
334
335     if(index >= length)
336         return DISP_E_MEMBERNOTFOUND;
337
338     nsres = nsIDOMWindowCollection_Item(nsFrames, index, &nsWindow);
339     if(NS_FAILED(nsres)) {
340         FIXME("nsIDOMWindowCollection_Item failed: 0x%08x\n", nsres);
341         return E_FAIL;
342     }
343
344     *ret = nswindow_to_window(nsWindow);
345
346     nsIDOMWindow_Release(nsWindow);
347
348     return S_OK;
349 }
350
351 static HRESULT WINAPI HTMLWindow2_item(IHTMLWindow2 *iface, VARIANT *pvarIndex, VARIANT *pvarResult)
352 {
353     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
354     nsIDOMWindowCollection *nsFrames;
355     HTMLOuterWindow *window = NULL;
356     HRESULT hres = S_OK;
357     nsresult nsres;
358
359     TRACE("(%p)->(%p %p)\n", This, pvarIndex, pvarResult);
360
361     nsres = nsIDOMWindow_GetFrames(This->outer_window->nswindow, &nsFrames);
362     if(NS_FAILED(nsres)) {
363         FIXME("nsIDOMWindow_GetFrames failed: 0x%08x\n", nsres);
364         return E_FAIL;
365     }
366
367     switch(V_VT(pvarIndex)) {
368     case VT_I4: {
369         int index = V_I4(pvarIndex);
370         TRACE("Getting index %d\n", index);
371         if(index < 0)
372             break;
373         hres = get_frame_by_index(nsFrames, index, &window);
374         break;
375     }
376     case VT_UINT: {
377         unsigned int index = V_UINT(pvarIndex);
378         TRACE("Getting index %u\n", index);
379         hres = get_frame_by_index(nsFrames, index, &window);
380         break;
381     }
382     case VT_BSTR: {
383         BSTR str = V_BSTR(pvarIndex);
384         PRUint32 length, i;
385
386         TRACE("Getting name %s\n", wine_dbgstr_w(str));
387
388         nsIDOMWindowCollection_GetLength(nsFrames, &length);
389
390         window = NULL;
391         for(i = 0; i < length && !window; ++i) {
392             HTMLOuterWindow *cur_window;
393             nsIDOMWindow *nsWindow;
394             BSTR id;
395
396             nsres = nsIDOMWindowCollection_Item(nsFrames, i, &nsWindow);
397             if(NS_FAILED(nsres)) {
398                 FIXME("nsIDOMWindowCollection_Item failed: 0x%08x\n", nsres);
399                 hres = E_FAIL;
400                 break;
401             }
402
403             cur_window = nswindow_to_window(nsWindow);
404
405             nsIDOMWindow_Release(nsWindow);
406
407             hres = IHTMLElement_get_id(&cur_window->frame_element->element.IHTMLElement_iface, &id);
408             if(FAILED(hres)) {
409                 FIXME("IHTMLElement_get_id failed: 0x%08x\n", hres);
410                 break;
411             }
412
413             if(!strcmpW(id, str))
414                 window = cur_window;
415
416             SysFreeString(id);
417         }
418         break;
419     }
420     default:
421         hres = E_INVALIDARG;
422     }
423
424     nsIDOMWindowCollection_Release(nsFrames);
425     if(FAILED(hres))
426         return hres;
427     if(!window)
428         return DISP_E_MEMBERNOTFOUND;
429
430     IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
431     V_VT(pvarResult) = VT_DISPATCH;
432     V_DISPATCH(pvarResult) = (IDispatch*)window;
433     return S_OK;
434 }
435
436 static HRESULT WINAPI HTMLWindow2_get_length(IHTMLWindow2 *iface, LONG *p)
437 {
438     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
439     nsIDOMWindowCollection *nscollection;
440     PRUint32 length;
441     nsresult nsres;
442
443     TRACE("(%p)->(%p)\n", This, p);
444
445     nsres = nsIDOMWindow_GetFrames(This->outer_window->nswindow, &nscollection);
446     if(NS_FAILED(nsres)) {
447         ERR("GetFrames failed: %08x\n", nsres);
448         return E_FAIL;
449     }
450
451     nsres = nsIDOMWindowCollection_GetLength(nscollection, &length);
452     nsIDOMWindowCollection_Release(nscollection);
453     if(NS_FAILED(nsres)) {
454         ERR("GetLength failed: %08x\n", nsres);
455         return E_FAIL;
456     }
457
458     *p = length;
459     return S_OK;
460 }
461
462 static HRESULT WINAPI HTMLWindow2_get_frames(IHTMLWindow2 *iface, IHTMLFramesCollection2 **p)
463 {
464     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
465     FIXME("(%p)->(%p): semi-stub\n", This, p);
466
467     /* FIXME: Should return a separate Window object */
468     *p = (IHTMLFramesCollection2*)&This->IHTMLWindow2_iface;
469     HTMLWindow2_AddRef(iface);
470     return S_OK;
471 }
472
473 static HRESULT WINAPI HTMLWindow2_put_defaultStatus(IHTMLWindow2 *iface, BSTR v)
474 {
475     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
476     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
477     return E_NOTIMPL;
478 }
479
480 static HRESULT WINAPI HTMLWindow2_get_defaultStatus(IHTMLWindow2 *iface, BSTR *p)
481 {
482     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
483     FIXME("(%p)->(%p)\n", This, p);
484     return E_NOTIMPL;
485 }
486
487 static HRESULT WINAPI HTMLWindow2_put_status(IHTMLWindow2 *iface, BSTR v)
488 {
489     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
490
491     WARN("(%p)->(%s)\n", This, debugstr_w(v));
492
493     /*
494      * FIXME: Since IE7, setting status is blocked, but still possible in certain circumstances.
495      * Ignoring the call should be enough for us.
496      */
497     return S_OK;
498 }
499
500 static HRESULT WINAPI HTMLWindow2_get_status(IHTMLWindow2 *iface, BSTR *p)
501 {
502     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
503
504     TRACE("(%p)->(%p)\n", This, p);
505
506     /* See put_status */
507     *p = NULL;
508     return S_OK;
509 }
510
511 static HRESULT WINAPI HTMLWindow2_setTimeout(IHTMLWindow2 *iface, BSTR expression,
512         LONG msec, VARIANT *language, LONG *timerID)
513 {
514     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
515     VARIANT expr_var;
516
517     TRACE("(%p)->(%s %d %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
518
519     V_VT(&expr_var) = VT_BSTR;
520     V_BSTR(&expr_var) = expression;
521
522     return IHTMLWindow3_setTimeout(&This->IHTMLWindow3_iface, &expr_var, msec, language, timerID);
523 }
524
525 static HRESULT WINAPI HTMLWindow2_clearTimeout(IHTMLWindow2 *iface, LONG timerID)
526 {
527     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
528
529     TRACE("(%p)->(%d)\n", This, timerID);
530
531     return clear_task_timer(&This->inner_window->doc->basedoc, FALSE, timerID);
532 }
533
534 #define MAX_MESSAGE_LEN 2000
535
536 static HRESULT WINAPI HTMLWindow2_alert(IHTMLWindow2 *iface, BSTR message)
537 {
538     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
539     WCHAR title[100], *msg = message;
540     DWORD len;
541
542     TRACE("(%p)->(%s)\n", This, debugstr_w(message));
543
544     if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, title,
545                     sizeof(title)/sizeof(WCHAR))) {
546         WARN("Could not load message box title: %d\n", GetLastError());
547         return S_OK;
548     }
549
550     len = SysStringLen(message);
551     if(len > MAX_MESSAGE_LEN) {
552         msg = heap_alloc((MAX_MESSAGE_LEN+1)*sizeof(WCHAR));
553         if(!msg)
554             return E_OUTOFMEMORY;
555         memcpy(msg, message, MAX_MESSAGE_LEN*sizeof(WCHAR));
556         msg[MAX_MESSAGE_LEN] = 0;
557     }
558
559     MessageBoxW(This->outer_window->doc_obj->hwnd, msg, title, MB_ICONWARNING);
560     if(msg != message)
561         heap_free(msg);
562     return S_OK;
563 }
564
565 static HRESULT WINAPI HTMLWindow2_confirm(IHTMLWindow2 *iface, BSTR message,
566         VARIANT_BOOL *confirmed)
567 {
568     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
569     WCHAR wszTitle[100];
570
571     TRACE("(%p)->(%s %p)\n", This, debugstr_w(message), confirmed);
572
573     if(!confirmed) return E_INVALIDARG;
574
575     if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, wszTitle,
576                 sizeof(wszTitle)/sizeof(WCHAR))) {
577         WARN("Could not load message box title: %d\n", GetLastError());
578         *confirmed = VARIANT_TRUE;
579         return S_OK;
580     }
581
582     if(MessageBoxW(This->outer_window->doc_obj->hwnd, message, wszTitle,
583                 MB_OKCANCEL|MB_ICONQUESTION)==IDOK)
584         *confirmed = VARIANT_TRUE;
585     else *confirmed = VARIANT_FALSE;
586
587     return S_OK;
588 }
589
590 typedef struct
591 {
592     BSTR message;
593     BSTR dststr;
594     VARIANT *textdata;
595 }prompt_arg;
596
597 static INT_PTR CALLBACK prompt_dlgproc(HWND hwnd, UINT msg,
598         WPARAM wparam, LPARAM lparam)
599 {
600     switch(msg)
601     {
602         case WM_INITDIALOG:
603         {
604             prompt_arg *arg = (prompt_arg*)lparam;
605             WCHAR wszTitle[100];
606
607             if(!LoadStringW(get_shdoclc(), IDS_MESSAGE_BOX_TITLE, wszTitle,
608                         sizeof(wszTitle)/sizeof(WCHAR))) {
609                 WARN("Could not load message box title: %d\n", GetLastError());
610                 EndDialog(hwnd, wparam);
611                 return FALSE;
612             }
613
614             SetWindowLongPtrW(hwnd, DWLP_USER, lparam);
615             SetWindowTextW(hwnd, wszTitle);
616             SetWindowTextW(GetDlgItem(hwnd, ID_PROMPT_PROMPT), arg->message);
617             SetWindowTextW(GetDlgItem(hwnd, ID_PROMPT_EDIT), arg->dststr);
618             return FALSE;
619         }
620         case WM_COMMAND:
621             switch(wparam)
622             {
623                 case MAKEWPARAM(IDCANCEL, BN_CLICKED):
624                     EndDialog(hwnd, wparam);
625                     return TRUE;
626                 case MAKEWPARAM(IDOK, BN_CLICKED):
627                 {
628                     prompt_arg *arg =
629                         (prompt_arg*)GetWindowLongPtrW(hwnd, DWLP_USER);
630                     HWND hwndPrompt = GetDlgItem(hwnd, ID_PROMPT_EDIT);
631                     INT len = GetWindowTextLengthW(hwndPrompt);
632
633                     if(!arg->textdata)
634                     {
635                         EndDialog(hwnd, wparam);
636                         return TRUE;
637                     }
638
639                     V_VT(arg->textdata) = VT_BSTR;
640                     if(!len && !arg->dststr)
641                         V_BSTR(arg->textdata) = NULL;
642                     else
643                     {
644                         V_BSTR(arg->textdata) = SysAllocStringLen(NULL, len);
645                         GetWindowTextW(hwndPrompt, V_BSTR(arg->textdata), len+1);
646                     }
647                     EndDialog(hwnd, wparam);
648                     return TRUE;
649                 }
650             }
651             return FALSE;
652         case WM_CLOSE:
653             EndDialog(hwnd, IDCANCEL);
654             return TRUE;
655         default:
656             return FALSE;
657     }
658 }
659
660 static HRESULT WINAPI HTMLWindow2_prompt(IHTMLWindow2 *iface, BSTR message,
661         BSTR dststr, VARIANT *textdata)
662 {
663     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
664     prompt_arg arg;
665
666     TRACE("(%p)->(%s %s %p)\n", This, debugstr_w(message), debugstr_w(dststr), textdata);
667
668     if(textdata) V_VT(textdata) = VT_NULL;
669
670     arg.message = message;
671     arg.dststr = dststr;
672     arg.textdata = textdata;
673
674     DialogBoxParamW(hInst, MAKEINTRESOURCEW(ID_PROMPT_DIALOG),
675             This->outer_window->doc_obj->hwnd, prompt_dlgproc, (LPARAM)&arg);
676     return S_OK;
677 }
678
679 static HRESULT WINAPI HTMLWindow2_get_Image(IHTMLWindow2 *iface, IHTMLImageElementFactory **p)
680 {
681     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
682     HTMLInnerWindow *window = This->inner_window;
683
684     TRACE("(%p)->(%p)\n", This, p);
685
686     if(!window->image_factory) {
687         HRESULT hres;
688
689         hres = HTMLImageElementFactory_Create(window, &window->image_factory);
690         if(FAILED(hres))
691             return hres;
692     }
693
694     *p = &window->image_factory->IHTMLImageElementFactory_iface;
695     IHTMLImageElementFactory_AddRef(*p);
696
697     return S_OK;
698 }
699
700 static HRESULT WINAPI HTMLWindow2_get_location(IHTMLWindow2 *iface, IHTMLLocation **p)
701 {
702     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
703     HTMLLocation *location;
704     HRESULT hres;
705
706     TRACE("(%p)->(%p)\n", This, p);
707
708     hres = get_location(This->outer_window, &location);
709     if(FAILED(hres))
710         return hres;
711
712     *p = &location->IHTMLLocation_iface;
713     return S_OK;
714 }
715
716 static HRESULT WINAPI HTMLWindow2_get_history(IHTMLWindow2 *iface, IOmHistory **p)
717 {
718     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
719     HTMLInnerWindow *window = This->inner_window;
720
721     TRACE("(%p)->(%p)\n", This, p);
722
723     if(!window->history) {
724         HRESULT hres;
725
726         hres = create_history(&window->history);
727         if(FAILED(hres))
728             return hres;
729     }
730
731     IOmHistory_AddRef(window->history);
732     *p = window->history;
733     return S_OK;
734 }
735
736 static HRESULT WINAPI HTMLWindow2_close(IHTMLWindow2 *iface)
737 {
738     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
739     FIXME("(%p)->()\n", This);
740     return E_NOTIMPL;
741 }
742
743 static HRESULT WINAPI HTMLWindow2_put_opener(IHTMLWindow2 *iface, VARIANT v)
744 {
745     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
746     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
747     return E_NOTIMPL;
748 }
749
750 static HRESULT WINAPI HTMLWindow2_get_opener(IHTMLWindow2 *iface, VARIANT *p)
751 {
752     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
753
754     FIXME("(%p)->(%p) returning empty\n", This, p);
755
756     V_VT(p) = VT_EMPTY;
757     return S_OK;
758 }
759
760 static HRESULT WINAPI HTMLWindow2_get_navigator(IHTMLWindow2 *iface, IOmNavigator **p)
761 {
762     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
763
764     TRACE("(%p)->(%p)\n", This, p);
765
766     *p = OmNavigator_Create();
767     return S_OK;
768 }
769
770 static HRESULT WINAPI HTMLWindow2_put_name(IHTMLWindow2 *iface, BSTR v)
771 {
772     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
773     nsAString name_str;
774     nsresult nsres;
775
776     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
777
778     nsAString_InitDepend(&name_str, v);
779     nsres = nsIDOMWindow_SetName(This->outer_window->nswindow, &name_str);
780     nsAString_Finish(&name_str);
781     if(NS_FAILED(nsres))
782         ERR("SetName failed: %08x\n", nsres);
783
784     return S_OK;
785 }
786
787 static HRESULT WINAPI HTMLWindow2_get_name(IHTMLWindow2 *iface, BSTR *p)
788 {
789     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
790     nsAString name_str;
791     nsresult nsres;
792
793     TRACE("(%p)->(%p)\n", This, p);
794
795     nsAString_Init(&name_str, NULL);
796     nsres = nsIDOMWindow_GetName(This->outer_window->nswindow, &name_str);
797     return return_nsstr(nsres, &name_str, p);
798 }
799
800 static HRESULT WINAPI HTMLWindow2_get_parent(IHTMLWindow2 *iface, IHTMLWindow2 **p)
801 {
802     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
803     HTMLOuterWindow *window = This->outer_window;
804
805     TRACE("(%p)->(%p)\n", This, p);
806
807     if(!window->parent)
808         return IHTMLWindow2_get_self(&This->IHTMLWindow2_iface, p);
809
810     *p = &window->parent->base.IHTMLWindow2_iface;
811     IHTMLWindow2_AddRef(*p);
812     return S_OK;
813 }
814
815 static HRESULT WINAPI HTMLWindow2_open(IHTMLWindow2 *iface, BSTR url, BSTR name,
816          BSTR features, VARIANT_BOOL replace, IHTMLWindow2 **pomWindowResult)
817 {
818     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
819     HTMLOuterWindow *window = This->outer_window;
820     INewWindowManager *new_window_mgr;
821     IUri *uri;
822     HRESULT hres;
823
824     TRACE("(%p)->(%s %s %s %x %p)\n", This, debugstr_w(url), debugstr_w(name),
825           debugstr_w(features), replace, pomWindowResult);
826
827     if(!window->doc_obj)
828         return E_UNEXPECTED;
829
830     if(name && *name == '_') {
831         FIXME("Unsupported name %s\n", debugstr_w(name));
832         return E_NOTIMPL;
833     }
834
835     hres = do_query_service((IUnknown*)window->doc_obj->client, &SID_SNewWindowManager, &IID_INewWindowManager,
836             (void**)&new_window_mgr);
837     if(FAILED(hres)) {
838         FIXME("No INewWindowManager\n");
839         return E_NOTIMPL;
840     }
841
842     hres = INewWindowManager_EvaluateNewWindow(new_window_mgr, url, name, window->url,
843             features, !!replace, window->doc_obj->has_popup ? 0 : NWMF_FIRST, 0);
844     INewWindowManager_Release(new_window_mgr);
845     window->doc_obj->has_popup = TRUE;
846     if(FAILED(hres)) {
847         *pomWindowResult = NULL;
848         return S_OK;
849     }
850
851     hres = create_relative_uri(window, url, &uri);
852     if(FAILED(hres))
853         return hres;
854
855     hres = navigate_new_window(window, uri, name, pomWindowResult);
856     IUri_Release(uri);
857     return hres;
858 }
859
860 static HRESULT WINAPI HTMLWindow2_get_self(IHTMLWindow2 *iface, IHTMLWindow2 **p)
861 {
862     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
863
864     TRACE("(%p)->(%p)\n", This, p);
865
866     /* FIXME: We should return kind of proxy window here. */
867     IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
868     *p = &This->IHTMLWindow2_iface;
869     return S_OK;
870 }
871
872 static HRESULT WINAPI HTMLWindow2_get_top(IHTMLWindow2 *iface, IHTMLWindow2 **p)
873 {
874     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
875     HTMLOuterWindow *top;
876
877     TRACE("(%p)->(%p)\n", This, p);
878
879     get_top_window(This->outer_window, &top);
880     *p = &top->base.IHTMLWindow2_iface;
881     IHTMLWindow2_AddRef(*p);
882
883     return S_OK;
884 }
885
886 static HRESULT WINAPI HTMLWindow2_get_window(IHTMLWindow2 *iface, IHTMLWindow2 **p)
887 {
888     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
889
890     TRACE("(%p)->(%p)\n", This, p);
891
892     /* FIXME: We should return kind of proxy window here. */
893     IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
894     *p = &This->IHTMLWindow2_iface;
895     return S_OK;
896 }
897
898 static HRESULT WINAPI HTMLWindow2_navigate(IHTMLWindow2 *iface, BSTR url)
899 {
900     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
901     FIXME("(%p)->(%s)\n", This, debugstr_w(url));
902     return E_NOTIMPL;
903 }
904
905 static HRESULT WINAPI HTMLWindow2_put_onfocus(IHTMLWindow2 *iface, VARIANT v)
906 {
907     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
908     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
909     return E_NOTIMPL;
910 }
911
912 static HRESULT WINAPI HTMLWindow2_get_onfocus(IHTMLWindow2 *iface, VARIANT *p)
913 {
914     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
915     FIXME("(%p)->(%p)\n", This, p);
916     return E_NOTIMPL;
917 }
918
919 static HRESULT WINAPI HTMLWindow2_put_onblur(IHTMLWindow2 *iface, VARIANT v)
920 {
921     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
922     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
923     return E_NOTIMPL;
924 }
925
926 static HRESULT WINAPI HTMLWindow2_get_onblur(IHTMLWindow2 *iface, VARIANT *p)
927 {
928     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
929     FIXME("(%p)->(%p)\n", This, p);
930     return E_NOTIMPL;
931 }
932
933 static HRESULT WINAPI HTMLWindow2_put_onload(IHTMLWindow2 *iface, VARIANT v)
934 {
935     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
936
937     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
938
939     return set_window_event(This, EVENTID_LOAD, &v);
940 }
941
942 static HRESULT WINAPI HTMLWindow2_get_onload(IHTMLWindow2 *iface, VARIANT *p)
943 {
944     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
945
946     TRACE("(%p)->(%p)\n", This, p);
947
948     return get_window_event(This, EVENTID_LOAD, p);
949 }
950
951 static HRESULT WINAPI HTMLWindow2_put_onbeforeunload(IHTMLWindow2 *iface, VARIANT v)
952 {
953     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
954
955     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
956
957     return set_window_event(This, EVENTID_BEFOREUNLOAD, &v);
958 }
959
960 static HRESULT WINAPI HTMLWindow2_get_onbeforeunload(IHTMLWindow2 *iface, VARIANT *p)
961 {
962     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
963
964     TRACE("(%p)->(%p)\n", This, p);
965
966     return get_window_event(This, EVENTID_BEFOREUNLOAD, p);
967 }
968
969 static HRESULT WINAPI HTMLWindow2_put_onunload(IHTMLWindow2 *iface, VARIANT v)
970 {
971     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
972     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
973     return E_NOTIMPL;
974 }
975
976 static HRESULT WINAPI HTMLWindow2_get_onunload(IHTMLWindow2 *iface, VARIANT *p)
977 {
978     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
979     FIXME("(%p)->(%p)\n", This, p);
980     return E_NOTIMPL;
981 }
982
983 static HRESULT WINAPI HTMLWindow2_put_onhelp(IHTMLWindow2 *iface, VARIANT v)
984 {
985     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
986     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
987     return E_NOTIMPL;
988 }
989
990 static HRESULT WINAPI HTMLWindow2_get_onhelp(IHTMLWindow2 *iface, VARIANT *p)
991 {
992     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
993     FIXME("(%p)->(%p)\n", This, p);
994     return E_NOTIMPL;
995 }
996
997 static HRESULT WINAPI HTMLWindow2_put_onerror(IHTMLWindow2 *iface, VARIANT v)
998 {
999     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1000
1001     FIXME("(%p)->(%s) semi-stub\n", This, debugstr_variant(&v));
1002
1003     return set_window_event(This, EVENTID_ERROR, &v);
1004 }
1005
1006 static HRESULT WINAPI HTMLWindow2_get_onerror(IHTMLWindow2 *iface, VARIANT *p)
1007 {
1008     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1009
1010     TRACE("(%p)->(%p)\n", This, p);
1011
1012     return get_window_event(This, EVENTID_ERROR, p);
1013 }
1014
1015 static HRESULT WINAPI HTMLWindow2_put_onresize(IHTMLWindow2 *iface, VARIANT v)
1016 {
1017     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1018
1019     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1020
1021     return set_window_event(This, EVENTID_RESIZE, &v);
1022 }
1023
1024 static HRESULT WINAPI HTMLWindow2_get_onresize(IHTMLWindow2 *iface, VARIANT *p)
1025 {
1026     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1027
1028     TRACE("(%p)->(%p)\n", This, p);
1029
1030     return get_window_event(This, EVENTID_RESIZE, p);
1031 }
1032
1033 static HRESULT WINAPI HTMLWindow2_put_onscroll(IHTMLWindow2 *iface, VARIANT v)
1034 {
1035     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1036     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1037     return E_NOTIMPL;
1038 }
1039
1040 static HRESULT WINAPI HTMLWindow2_get_onscroll(IHTMLWindow2 *iface, VARIANT *p)
1041 {
1042     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1043     FIXME("(%p)->(%p)\n", This, p);
1044     return E_NOTIMPL;
1045 }
1046
1047 static HRESULT WINAPI HTMLWindow2_get_document(IHTMLWindow2 *iface, IHTMLDocument2 **p)
1048 {
1049     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1050
1051     TRACE("(%p)->(%p)\n", This, p);
1052
1053     if(This->inner_window->doc) {
1054         /* FIXME: We should return a wrapper object here */
1055         *p = &This->inner_window->doc->basedoc.IHTMLDocument2_iface;
1056         IHTMLDocument2_AddRef(*p);
1057     }else {
1058         *p = NULL;
1059     }
1060
1061     return S_OK;
1062 }
1063
1064 static HRESULT WINAPI HTMLWindow2_get_event(IHTMLWindow2 *iface, IHTMLEventObj **p)
1065 {
1066     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1067     HTMLInnerWindow *window = This->inner_window;
1068
1069     TRACE("(%p)->(%p)\n", This, p);
1070
1071     if(window->event)
1072         IHTMLEventObj_AddRef(window->event);
1073     *p = window->event;
1074     return S_OK;
1075 }
1076
1077 static HRESULT WINAPI HTMLWindow2_get__newEnum(IHTMLWindow2 *iface, IUnknown **p)
1078 {
1079     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1080     FIXME("(%p)->(%p)\n", This, p);
1081     return E_NOTIMPL;
1082 }
1083
1084 static HRESULT WINAPI HTMLWindow2_showModalDialog(IHTMLWindow2 *iface, BSTR dialog,
1085         VARIANT *varArgIn, VARIANT *varOptions, VARIANT *varArgOut)
1086 {
1087     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1088     FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(dialog), varArgIn, varOptions, varArgOut);
1089     return E_NOTIMPL;
1090 }
1091
1092 static HRESULT WINAPI HTMLWindow2_showHelp(IHTMLWindow2 *iface, BSTR helpURL, VARIANT helpArg,
1093         BSTR features)
1094 {
1095     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1096     FIXME("(%p)->(%s %s %s)\n", This, debugstr_w(helpURL), debugstr_variant(&helpArg), debugstr_w(features));
1097     return E_NOTIMPL;
1098 }
1099
1100 static HRESULT WINAPI HTMLWindow2_get_screen(IHTMLWindow2 *iface, IHTMLScreen **p)
1101 {
1102     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1103     HTMLInnerWindow *window = This->inner_window;
1104
1105     TRACE("(%p)->(%p)\n", This, p);
1106
1107     if(!window->screen) {
1108         HRESULT hres;
1109
1110         hres = HTMLScreen_Create(&window->screen);
1111         if(FAILED(hres))
1112             return hres;
1113     }
1114
1115     *p = window->screen;
1116     IHTMLScreen_AddRef(window->screen);
1117     return S_OK;
1118 }
1119
1120 static HRESULT WINAPI HTMLWindow2_get_Option(IHTMLWindow2 *iface, IHTMLOptionElementFactory **p)
1121 {
1122     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1123     HTMLInnerWindow *window = This->inner_window;
1124
1125     TRACE("(%p)->(%p)\n", This, p);
1126
1127     if(!window->option_factory) {
1128         HRESULT hres;
1129
1130         hres = HTMLOptionElementFactory_Create(window, &window->option_factory);
1131         if(FAILED(hres))
1132             return hres;
1133     }
1134
1135     *p = &window->option_factory->IHTMLOptionElementFactory_iface;
1136     IHTMLOptionElementFactory_AddRef(*p);
1137
1138     return S_OK;
1139 }
1140
1141 static HRESULT WINAPI HTMLWindow2_focus(IHTMLWindow2 *iface)
1142 {
1143     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1144
1145     TRACE("(%p)->()\n", This);
1146
1147     if(This->outer_window->doc_obj)
1148         SetFocus(This->outer_window->doc_obj->hwnd);
1149     return S_OK;
1150 }
1151
1152 static HRESULT WINAPI HTMLWindow2_get_closed(IHTMLWindow2 *iface, VARIANT_BOOL *p)
1153 {
1154     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1155     FIXME("(%p)->(%p)\n", This, p);
1156     return E_NOTIMPL;
1157 }
1158
1159 static HRESULT WINAPI HTMLWindow2_blur(IHTMLWindow2 *iface)
1160 {
1161     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1162     FIXME("(%p)->()\n", This);
1163     return E_NOTIMPL;
1164 }
1165
1166 static HRESULT WINAPI HTMLWindow2_scroll(IHTMLWindow2 *iface, LONG x, LONG y)
1167 {
1168     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1169     FIXME("(%p)->(%d %d)\n", This, x, y);
1170     return E_NOTIMPL;
1171 }
1172
1173 static HRESULT WINAPI HTMLWindow2_get_clientInformation(IHTMLWindow2 *iface, IOmNavigator **p)
1174 {
1175     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1176     FIXME("(%p)->(%p)\n", This, p);
1177     return E_NOTIMPL;
1178 }
1179
1180 static HRESULT WINAPI HTMLWindow2_setInterval(IHTMLWindow2 *iface, BSTR expression,
1181         LONG msec, VARIANT *language, LONG *timerID)
1182 {
1183     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1184     VARIANT expr;
1185
1186     TRACE("(%p)->(%s %d %p %p)\n", This, debugstr_w(expression), msec, language, timerID);
1187
1188     V_VT(&expr) = VT_BSTR;
1189     V_BSTR(&expr) = expression;
1190     return IHTMLWindow3_setInterval(&This->IHTMLWindow3_iface, &expr, msec, language, timerID);
1191 }
1192
1193 static HRESULT WINAPI HTMLWindow2_clearInterval(IHTMLWindow2 *iface, LONG timerID)
1194 {
1195     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1196
1197     TRACE("(%p)->(%d)\n", This, timerID);
1198
1199     return clear_task_timer(&This->inner_window->doc->basedoc, TRUE, timerID);
1200 }
1201
1202 static HRESULT WINAPI HTMLWindow2_put_offscreenBuffering(IHTMLWindow2 *iface, VARIANT v)
1203 {
1204     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1205     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1206     return E_NOTIMPL;
1207 }
1208
1209 static HRESULT WINAPI HTMLWindow2_get_offscreenBuffering(IHTMLWindow2 *iface, VARIANT *p)
1210 {
1211     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1212     FIXME("(%p)->(%p)\n", This, p);
1213     return E_NOTIMPL;
1214 }
1215
1216 static HRESULT WINAPI HTMLWindow2_execScript(IHTMLWindow2 *iface, BSTR scode, BSTR language,
1217         VARIANT *pvarRet)
1218 {
1219     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1220
1221     TRACE("(%p)->(%s %s %p)\n", This, debugstr_w(scode), debugstr_w(language), pvarRet);
1222
1223     return exec_script(This->inner_window, scode, language, pvarRet);
1224 }
1225
1226 static HRESULT WINAPI HTMLWindow2_toString(IHTMLWindow2 *iface, BSTR *String)
1227 {
1228     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1229
1230     static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
1231
1232     TRACE("(%p)->(%p)\n", This, String);
1233
1234     if(!String)
1235         return E_INVALIDARG;
1236
1237     *String = SysAllocString(objectW);
1238     return *String ? S_OK : E_OUTOFMEMORY;
1239 }
1240
1241 static HRESULT WINAPI HTMLWindow2_scrollBy(IHTMLWindow2 *iface, LONG x, LONG y)
1242 {
1243     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1244     nsresult nsres;
1245
1246     TRACE("(%p)->(%d %d)\n", This, x, y);
1247
1248     nsres = nsIDOMWindow_ScrollBy(This->outer_window->nswindow, x, y);
1249     if(NS_FAILED(nsres))
1250         ERR("ScrollBy failed: %08x\n", nsres);
1251
1252     return S_OK;
1253 }
1254
1255 static HRESULT WINAPI HTMLWindow2_scrollTo(IHTMLWindow2 *iface, LONG x, LONG y)
1256 {
1257     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1258     nsresult nsres;
1259
1260     TRACE("(%p)->(%d %d)\n", This, x, y);
1261
1262     nsres = nsIDOMWindow_ScrollTo(This->outer_window->nswindow, x, y);
1263     if(NS_FAILED(nsres))
1264         ERR("ScrollTo failed: %08x\n", nsres);
1265
1266     return S_OK;
1267 }
1268
1269 static HRESULT WINAPI HTMLWindow2_moveTo(IHTMLWindow2 *iface, LONG x, LONG y)
1270 {
1271     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1272     FIXME("(%p)->(%d %d)\n", This, x, y);
1273     return E_NOTIMPL;
1274 }
1275
1276 static HRESULT WINAPI HTMLWindow2_moveBy(IHTMLWindow2 *iface, LONG x, LONG y)
1277 {
1278     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1279     FIXME("(%p)->(%d %d)\n", This, x, y);
1280     return E_NOTIMPL;
1281 }
1282
1283 static HRESULT WINAPI HTMLWindow2_resizeTo(IHTMLWindow2 *iface, LONG x, LONG y)
1284 {
1285     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1286     FIXME("(%p)->(%d %d)\n", This, x, y);
1287     return E_NOTIMPL;
1288 }
1289
1290 static HRESULT WINAPI HTMLWindow2_resizeBy(IHTMLWindow2 *iface, LONG x, LONG y)
1291 {
1292     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1293     FIXME("(%p)->(%d %d)\n", This, x, y);
1294     return E_NOTIMPL;
1295 }
1296
1297 static HRESULT WINAPI HTMLWindow2_get_external(IHTMLWindow2 *iface, IDispatch **p)
1298 {
1299     HTMLWindow *This = impl_from_IHTMLWindow2(iface);
1300
1301     TRACE("(%p)->(%p)\n", This, p);
1302
1303     *p = NULL;
1304
1305     if(!This->outer_window->doc_obj->hostui)
1306         return S_OK;
1307
1308     return IDocHostUIHandler_GetExternal(This->outer_window->doc_obj->hostui, p);
1309 }
1310
1311 static const IHTMLWindow2Vtbl HTMLWindow2Vtbl = {
1312     HTMLWindow2_QueryInterface,
1313     HTMLWindow2_AddRef,
1314     HTMLWindow2_Release,
1315     HTMLWindow2_GetTypeInfoCount,
1316     HTMLWindow2_GetTypeInfo,
1317     HTMLWindow2_GetIDsOfNames,
1318     HTMLWindow2_Invoke,
1319     HTMLWindow2_item,
1320     HTMLWindow2_get_length,
1321     HTMLWindow2_get_frames,
1322     HTMLWindow2_put_defaultStatus,
1323     HTMLWindow2_get_defaultStatus,
1324     HTMLWindow2_put_status,
1325     HTMLWindow2_get_status,
1326     HTMLWindow2_setTimeout,
1327     HTMLWindow2_clearTimeout,
1328     HTMLWindow2_alert,
1329     HTMLWindow2_confirm,
1330     HTMLWindow2_prompt,
1331     HTMLWindow2_get_Image,
1332     HTMLWindow2_get_location,
1333     HTMLWindow2_get_history,
1334     HTMLWindow2_close,
1335     HTMLWindow2_put_opener,
1336     HTMLWindow2_get_opener,
1337     HTMLWindow2_get_navigator,
1338     HTMLWindow2_put_name,
1339     HTMLWindow2_get_name,
1340     HTMLWindow2_get_parent,
1341     HTMLWindow2_open,
1342     HTMLWindow2_get_self,
1343     HTMLWindow2_get_top,
1344     HTMLWindow2_get_window,
1345     HTMLWindow2_navigate,
1346     HTMLWindow2_put_onfocus,
1347     HTMLWindow2_get_onfocus,
1348     HTMLWindow2_put_onblur,
1349     HTMLWindow2_get_onblur,
1350     HTMLWindow2_put_onload,
1351     HTMLWindow2_get_onload,
1352     HTMLWindow2_put_onbeforeunload,
1353     HTMLWindow2_get_onbeforeunload,
1354     HTMLWindow2_put_onunload,
1355     HTMLWindow2_get_onunload,
1356     HTMLWindow2_put_onhelp,
1357     HTMLWindow2_get_onhelp,
1358     HTMLWindow2_put_onerror,
1359     HTMLWindow2_get_onerror,
1360     HTMLWindow2_put_onresize,
1361     HTMLWindow2_get_onresize,
1362     HTMLWindow2_put_onscroll,
1363     HTMLWindow2_get_onscroll,
1364     HTMLWindow2_get_document,
1365     HTMLWindow2_get_event,
1366     HTMLWindow2_get__newEnum,
1367     HTMLWindow2_showModalDialog,
1368     HTMLWindow2_showHelp,
1369     HTMLWindow2_get_screen,
1370     HTMLWindow2_get_Option,
1371     HTMLWindow2_focus,
1372     HTMLWindow2_get_closed,
1373     HTMLWindow2_blur,
1374     HTMLWindow2_scroll,
1375     HTMLWindow2_get_clientInformation,
1376     HTMLWindow2_setInterval,
1377     HTMLWindow2_clearInterval,
1378     HTMLWindow2_put_offscreenBuffering,
1379     HTMLWindow2_get_offscreenBuffering,
1380     HTMLWindow2_execScript,
1381     HTMLWindow2_toString,
1382     HTMLWindow2_scrollBy,
1383     HTMLWindow2_scrollTo,
1384     HTMLWindow2_moveTo,
1385     HTMLWindow2_moveBy,
1386     HTMLWindow2_resizeTo,
1387     HTMLWindow2_resizeBy,
1388     HTMLWindow2_get_external
1389 };
1390
1391 static inline HTMLWindow *impl_from_IHTMLWindow3(IHTMLWindow3 *iface)
1392 {
1393     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow3_iface);
1394 }
1395
1396 static HRESULT WINAPI HTMLWindow3_QueryInterface(IHTMLWindow3 *iface, REFIID riid, void **ppv)
1397 {
1398     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1399
1400     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1401 }
1402
1403 static ULONG WINAPI HTMLWindow3_AddRef(IHTMLWindow3 *iface)
1404 {
1405     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1406
1407     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1408 }
1409
1410 static ULONG WINAPI HTMLWindow3_Release(IHTMLWindow3 *iface)
1411 {
1412     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1413
1414     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1415 }
1416
1417 static HRESULT WINAPI HTMLWindow3_GetTypeInfoCount(IHTMLWindow3 *iface, UINT *pctinfo)
1418 {
1419     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1420
1421     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
1422 }
1423
1424 static HRESULT WINAPI HTMLWindow3_GetTypeInfo(IHTMLWindow3 *iface, UINT iTInfo,
1425                                               LCID lcid, ITypeInfo **ppTInfo)
1426 {
1427     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1428
1429     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1430 }
1431
1432 static HRESULT WINAPI HTMLWindow3_GetIDsOfNames(IHTMLWindow3 *iface, REFIID riid,
1433                                                 LPOLESTR *rgszNames, UINT cNames,
1434                                                 LCID lcid, DISPID *rgDispId)
1435 {
1436     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1437
1438     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
1439             rgDispId);
1440 }
1441
1442 static HRESULT WINAPI HTMLWindow3_Invoke(IHTMLWindow3 *iface, DISPID dispIdMember,
1443                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1444                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1445 {
1446     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1447
1448     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
1449             pDispParams, pVarResult, pExcepInfo, puArgErr);
1450 }
1451
1452 static HRESULT WINAPI HTMLWindow3_get_screenLeft(IHTMLWindow3 *iface, LONG *p)
1453 {
1454     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1455     FIXME("(%p)->(%p)\n", This, p);
1456     return E_NOTIMPL;
1457 }
1458
1459 static HRESULT WINAPI HTMLWindow3_get_screenTop(IHTMLWindow3 *iface, LONG *p)
1460 {
1461     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1462     FIXME("(%p)->(%p)\n", This, p);
1463     return E_NOTIMPL;
1464 }
1465
1466 static HRESULT WINAPI HTMLWindow3_attachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp, VARIANT_BOOL *pfResult)
1467 {
1468     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1469     HTMLInnerWindow *window = This->inner_window;
1470
1471     TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(event), pDisp, pfResult);
1472
1473     if(!window->doc) {
1474         FIXME("No document\n");
1475         return E_FAIL;
1476     }
1477
1478     return attach_event(&window->doc->body_event_target, NULL, &window->doc->basedoc, event, pDisp, pfResult);
1479 }
1480
1481 static HRESULT WINAPI HTMLWindow3_detachEvent(IHTMLWindow3 *iface, BSTR event, IDispatch *pDisp)
1482 {
1483     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1484     HTMLInnerWindow *window = This->inner_window;
1485
1486     TRACE("(%p)->()\n", This);
1487
1488     if(!window->doc) {
1489         FIXME("No document\n");
1490         return E_FAIL;
1491     }
1492
1493     return detach_event(window->doc->body_event_target, &window->doc->basedoc, event, pDisp);
1494 }
1495
1496 static HRESULT window_set_timer(HTMLInnerWindow *This, VARIANT *expr, LONG msec, VARIANT *language,
1497         BOOL interval, LONG *timer_id)
1498 {
1499     IDispatch *disp = NULL;
1500
1501     switch(V_VT(expr)) {
1502     case VT_DISPATCH:
1503         disp = V_DISPATCH(expr);
1504         IDispatch_AddRef(disp);
1505         break;
1506
1507     case VT_BSTR:
1508         disp = script_parse_event(This->base.inner_window, V_BSTR(expr));
1509         break;
1510
1511     default:
1512         FIXME("unimplemented expr %s\n", debugstr_variant(expr));
1513         return E_NOTIMPL;
1514     }
1515
1516     if(!disp)
1517         return E_FAIL;
1518
1519     *timer_id = set_task_timer(&This->doc->basedoc, msec, interval, disp);
1520     IDispatch_Release(disp);
1521
1522     return S_OK;
1523 }
1524
1525 static HRESULT WINAPI HTMLWindow3_setTimeout(IHTMLWindow3 *iface, VARIANT *expression, LONG msec,
1526         VARIANT *language, LONG *timerID)
1527 {
1528     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1529
1530     TRACE("(%p)->(%s %d %s %p)\n", This, debugstr_variant(expression), msec, debugstr_variant(language), timerID);
1531
1532     return window_set_timer(This->inner_window, expression, msec, language, FALSE, timerID);
1533 }
1534
1535 static HRESULT WINAPI HTMLWindow3_setInterval(IHTMLWindow3 *iface, VARIANT *expression, LONG msec,
1536         VARIANT *language, LONG *timerID)
1537 {
1538     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1539
1540     TRACE("(%p)->(%p %d %p %p)\n", This, expression, msec, language, timerID);
1541
1542     return window_set_timer(This->inner_window, expression, msec, language, TRUE, timerID);
1543 }
1544
1545 static HRESULT WINAPI HTMLWindow3_print(IHTMLWindow3 *iface)
1546 {
1547     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1548     FIXME("(%p)\n", This);
1549     return E_NOTIMPL;
1550 }
1551
1552 static HRESULT WINAPI HTMLWindow3_put_onbeforeprint(IHTMLWindow3 *iface, VARIANT v)
1553 {
1554     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1555     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1556     return E_NOTIMPL;
1557 }
1558
1559 static HRESULT WINAPI HTMLWindow3_get_onbeforeprint(IHTMLWindow3 *iface, VARIANT *p)
1560 {
1561     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1562     FIXME("(%p)->(%p)\n", This, p);
1563     return E_NOTIMPL;
1564 }
1565
1566 static HRESULT WINAPI HTMLWindow3_put_onafterprint(IHTMLWindow3 *iface, VARIANT v)
1567 {
1568     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1569     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1570     return E_NOTIMPL;
1571 }
1572
1573 static HRESULT WINAPI HTMLWindow3_get_onafterprint(IHTMLWindow3 *iface, VARIANT *p)
1574 {
1575     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1576     FIXME("(%p)->(%p)\n", This, p);
1577     return E_NOTIMPL;
1578 }
1579
1580 static HRESULT WINAPI HTMLWindow3_get_clipboardData(IHTMLWindow3 *iface, IHTMLDataTransfer **p)
1581 {
1582     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1583     FIXME("(%p)->(%p)\n", This, p);
1584     return E_NOTIMPL;
1585 }
1586
1587 static HRESULT WINAPI HTMLWindow3_showModelessDialog(IHTMLWindow3 *iface, BSTR url,
1588         VARIANT *varArgIn, VARIANT *options, IHTMLWindow2 **pDialog)
1589 {
1590     HTMLWindow *This = impl_from_IHTMLWindow3(iface);
1591     FIXME("(%p)->(%s %p %p %p)\n", This, debugstr_w(url), varArgIn, options, pDialog);
1592     return E_NOTIMPL;
1593 }
1594
1595 static const IHTMLWindow3Vtbl HTMLWindow3Vtbl = {
1596     HTMLWindow3_QueryInterface,
1597     HTMLWindow3_AddRef,
1598     HTMLWindow3_Release,
1599     HTMLWindow3_GetTypeInfoCount,
1600     HTMLWindow3_GetTypeInfo,
1601     HTMLWindow3_GetIDsOfNames,
1602     HTMLWindow3_Invoke,
1603     HTMLWindow3_get_screenLeft,
1604     HTMLWindow3_get_screenTop,
1605     HTMLWindow3_attachEvent,
1606     HTMLWindow3_detachEvent,
1607     HTMLWindow3_setTimeout,
1608     HTMLWindow3_setInterval,
1609     HTMLWindow3_print,
1610     HTMLWindow3_put_onbeforeprint,
1611     HTMLWindow3_get_onbeforeprint,
1612     HTMLWindow3_put_onafterprint,
1613     HTMLWindow3_get_onafterprint,
1614     HTMLWindow3_get_clipboardData,
1615     HTMLWindow3_showModelessDialog
1616 };
1617
1618 static inline HTMLWindow *impl_from_IHTMLWindow4(IHTMLWindow4 *iface)
1619 {
1620     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow4_iface);
1621 }
1622
1623 static HRESULT WINAPI HTMLWindow4_QueryInterface(IHTMLWindow4 *iface, REFIID riid, void **ppv)
1624 {
1625     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1626
1627     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1628 }
1629
1630 static ULONG WINAPI HTMLWindow4_AddRef(IHTMLWindow4 *iface)
1631 {
1632     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1633
1634     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1635 }
1636
1637 static ULONG WINAPI HTMLWindow4_Release(IHTMLWindow4 *iface)
1638 {
1639     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1640
1641     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1642 }
1643
1644 static HRESULT WINAPI HTMLWindow4_GetTypeInfoCount(IHTMLWindow4 *iface, UINT *pctinfo)
1645 {
1646     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1647
1648     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
1649 }
1650
1651 static HRESULT WINAPI HTMLWindow4_GetTypeInfo(IHTMLWindow4 *iface, UINT iTInfo,
1652                                               LCID lcid, ITypeInfo **ppTInfo)
1653 {
1654     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1655
1656     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1657 }
1658
1659 static HRESULT WINAPI HTMLWindow4_GetIDsOfNames(IHTMLWindow4 *iface, REFIID riid,
1660                                                 LPOLESTR *rgszNames, UINT cNames,
1661                                                 LCID lcid, DISPID *rgDispId)
1662 {
1663     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1664
1665     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
1666             rgDispId);
1667 }
1668
1669 static HRESULT WINAPI HTMLWindow4_Invoke(IHTMLWindow4 *iface, DISPID dispIdMember,
1670                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1671                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1672 {
1673     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1674
1675     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
1676             pDispParams, pVarResult, pExcepInfo, puArgErr);
1677 }
1678
1679 static HRESULT WINAPI HTMLWindow4_createPopup(IHTMLWindow4 *iface, VARIANT *varArgIn,
1680                             IDispatch **ppPopup)
1681 {
1682     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1683     FIXME("(%p)->(%p %p)\n", This, varArgIn, ppPopup);
1684     return E_NOTIMPL;
1685 }
1686
1687 static HRESULT WINAPI HTMLWindow4_get_frameElement(IHTMLWindow4 *iface, IHTMLFrameBase **p)
1688 {
1689     HTMLWindow *This = impl_from_IHTMLWindow4(iface);
1690     TRACE("(%p)->(%p)\n", This, p);
1691
1692     if(This->outer_window->frame_element) {
1693         *p = &This->outer_window->frame_element->IHTMLFrameBase_iface;
1694         IHTMLFrameBase_AddRef(*p);
1695     }else
1696         *p = NULL;
1697
1698     return S_OK;
1699 }
1700
1701 static const IHTMLWindow4Vtbl HTMLWindow4Vtbl = {
1702     HTMLWindow4_QueryInterface,
1703     HTMLWindow4_AddRef,
1704     HTMLWindow4_Release,
1705     HTMLWindow4_GetTypeInfoCount,
1706     HTMLWindow4_GetTypeInfo,
1707     HTMLWindow4_GetIDsOfNames,
1708     HTMLWindow4_Invoke,
1709     HTMLWindow4_createPopup,
1710     HTMLWindow4_get_frameElement
1711 };
1712
1713 static inline HTMLWindow *impl_from_IHTMLWindow5(IHTMLWindow5 *iface)
1714 {
1715     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow5_iface);
1716 }
1717
1718 static HRESULT WINAPI HTMLWindow5_QueryInterface(IHTMLWindow5 *iface, REFIID riid, void **ppv)
1719 {
1720     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1721
1722     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1723 }
1724
1725 static ULONG WINAPI HTMLWindow5_AddRef(IHTMLWindow5 *iface)
1726 {
1727     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1728
1729     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1730 }
1731
1732 static ULONG WINAPI HTMLWindow5_Release(IHTMLWindow5 *iface)
1733 {
1734     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1735
1736     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1737 }
1738
1739 static HRESULT WINAPI HTMLWindow5_GetTypeInfoCount(IHTMLWindow5 *iface, UINT *pctinfo)
1740 {
1741     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1742
1743     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
1744 }
1745
1746 static HRESULT WINAPI HTMLWindow5_GetTypeInfo(IHTMLWindow5 *iface, UINT iTInfo,
1747         LCID lcid, ITypeInfo **ppTInfo)
1748 {
1749     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1750
1751     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1752 }
1753
1754 static HRESULT WINAPI HTMLWindow5_GetIDsOfNames(IHTMLWindow5 *iface, REFIID riid,
1755         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1756 {
1757     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1758
1759     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
1760             rgDispId);
1761 }
1762
1763 static HRESULT WINAPI HTMLWindow5_Invoke(IHTMLWindow5 *iface, DISPID dispIdMember,
1764         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1765         VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1766 {
1767     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1768
1769     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
1770             pDispParams, pVarResult, pExcepInfo, puArgErr);
1771 }
1772
1773 static HRESULT WINAPI HTMLWindow5_put_XMLHttpRequest(IHTMLWindow5 *iface, VARIANT v)
1774 {
1775     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1776     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1777     return E_NOTIMPL;
1778 }
1779
1780 static HRESULT WINAPI HTMLWindow5_get_XMLHttpRequest(IHTMLWindow5 *iface, VARIANT *p)
1781 {
1782     HTMLWindow *This = impl_from_IHTMLWindow5(iface);
1783     FIXME("(%p)->(%p)\n", This, p);
1784     return E_NOTIMPL;
1785 }
1786
1787 static const IHTMLWindow5Vtbl HTMLWindow5Vtbl = {
1788     HTMLWindow5_QueryInterface,
1789     HTMLWindow5_AddRef,
1790     HTMLWindow5_Release,
1791     HTMLWindow5_GetTypeInfoCount,
1792     HTMLWindow5_GetTypeInfo,
1793     HTMLWindow5_GetIDsOfNames,
1794     HTMLWindow5_Invoke,
1795     HTMLWindow5_put_XMLHttpRequest,
1796     HTMLWindow5_get_XMLHttpRequest
1797 };
1798
1799 static inline HTMLWindow *impl_from_IHTMLWindow6(IHTMLWindow6 *iface)
1800 {
1801     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLWindow6_iface);
1802 }
1803
1804 static HRESULT WINAPI HTMLWindow6_QueryInterface(IHTMLWindow6 *iface, REFIID riid, void **ppv)
1805 {
1806     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1807
1808     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1809 }
1810
1811 static ULONG WINAPI HTMLWindow6_AddRef(IHTMLWindow6 *iface)
1812 {
1813     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1814
1815     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1816 }
1817
1818 static ULONG WINAPI HTMLWindow6_Release(IHTMLWindow6 *iface)
1819 {
1820     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1821
1822     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1823 }
1824
1825 static HRESULT WINAPI HTMLWindow6_GetTypeInfoCount(IHTMLWindow6 *iface, UINT *pctinfo)
1826 {
1827     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1828
1829     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
1830 }
1831
1832 static HRESULT WINAPI HTMLWindow6_GetTypeInfo(IHTMLWindow6 *iface, UINT iTInfo,
1833                                               LCID lcid, ITypeInfo **ppTInfo)
1834 {
1835     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1836
1837     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1838 }
1839
1840 static HRESULT WINAPI HTMLWindow6_GetIDsOfNames(IHTMLWindow6 *iface, REFIID riid,
1841                                                 LPOLESTR *rgszNames, UINT cNames,
1842                                                 LCID lcid, DISPID *rgDispId)
1843 {
1844     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1845
1846     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
1847             rgDispId);
1848 }
1849
1850 static HRESULT WINAPI HTMLWindow6_Invoke(IHTMLWindow6 *iface, DISPID dispIdMember,
1851                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1852                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1853 {
1854     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1855
1856     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
1857             pDispParams, pVarResult, pExcepInfo, puArgErr);
1858 }
1859
1860 static HRESULT WINAPI HTMLWindow6_put_XDomainRequest(IHTMLWindow6 *iface, VARIANT v)
1861 {
1862     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1863     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1864     return E_NOTIMPL;
1865 }
1866
1867 static HRESULT WINAPI HTMLWindow6_get_XDomainRequest(IHTMLWindow6 *iface, VARIANT *p)
1868 {
1869     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1870     FIXME("(%p)->(%p)\n", This, p);
1871     return E_NOTIMPL;
1872 }
1873
1874 static HRESULT WINAPI HTMLWindow6_get_sessionStorage(IHTMLWindow6 *iface, IHTMLStorage **p)
1875 {
1876     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1877     FIXME("(%p)->(%p)\n", This, p);
1878     return E_NOTIMPL;
1879 }
1880
1881 static HRESULT WINAPI HTMLWindow6_get_localStorage(IHTMLWindow6 *iface, IHTMLStorage **p)
1882 {
1883     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1884     FIXME("(%p)->(%p)\n", This, p);
1885     return E_NOTIMPL;
1886 }
1887
1888 static HRESULT WINAPI HTMLWindow6_put_onhashchange(IHTMLWindow6 *iface, VARIANT v)
1889 {
1890     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1891     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1892     return E_NOTIMPL;
1893 }
1894
1895 static HRESULT WINAPI HTMLWindow6_get_onhashchange(IHTMLWindow6 *iface, VARIANT *p)
1896 {
1897     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1898     FIXME("(%p)->(%p)\n", This, p);
1899     return E_NOTIMPL;
1900 }
1901
1902 static HRESULT WINAPI HTMLWindow6_get_maxConnectionsPerServer(IHTMLWindow6 *iface, LONG *p)
1903 {
1904     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1905     FIXME("(%p)->(%p)\n", This, p);
1906     return E_NOTIMPL;
1907 }
1908
1909 static HRESULT WINAPI HTMLWindow6_postMessage(IHTMLWindow6 *iface, BSTR msg, VARIANT targetOrigin)
1910 {
1911     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1912     FIXME("(%p)->(%s %s)\n", This, debugstr_w(msg), debugstr_variant(&targetOrigin));
1913     return E_NOTIMPL;
1914 }
1915
1916 static HRESULT WINAPI HTMLWindow6_toStaticHTML(IHTMLWindow6 *iface, BSTR bstrHTML, BSTR *pbstrStaticHTML)
1917 {
1918     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1919     FIXME("(%p)->(%s %p)\n", This, debugstr_w(bstrHTML), pbstrStaticHTML);
1920     return E_NOTIMPL;
1921 }
1922
1923 static HRESULT WINAPI HTMLWindow6_put_onmessage(IHTMLWindow6 *iface, VARIANT v)
1924 {
1925     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1926     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1927     return E_NOTIMPL;
1928 }
1929
1930 static HRESULT WINAPI HTMLWindow6_get_onmessage(IHTMLWindow6 *iface, VARIANT *p)
1931 {
1932     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1933     FIXME("(%p)->(%p)\n", This, p);
1934     return E_NOTIMPL;
1935 }
1936
1937 static HRESULT WINAPI HTMLWindow6_msWriteProfilerMark(IHTMLWindow6 *iface, BSTR bstrProfilerMark)
1938 {
1939     HTMLWindow *This = impl_from_IHTMLWindow6(iface);
1940     FIXME("(%p)->(%s)\n", This, debugstr_w(bstrProfilerMark));
1941     return E_NOTIMPL;
1942 }
1943
1944 static const IHTMLWindow6Vtbl HTMLWindow6Vtbl = {
1945     HTMLWindow6_QueryInterface,
1946     HTMLWindow6_AddRef,
1947     HTMLWindow6_Release,
1948     HTMLWindow6_GetTypeInfoCount,
1949     HTMLWindow6_GetTypeInfo,
1950     HTMLWindow6_GetIDsOfNames,
1951     HTMLWindow6_Invoke,
1952     HTMLWindow6_put_XDomainRequest,
1953     HTMLWindow6_get_XDomainRequest,
1954     HTMLWindow6_get_sessionStorage,
1955     HTMLWindow6_get_localStorage,
1956     HTMLWindow6_put_onhashchange,
1957     HTMLWindow6_get_onhashchange,
1958     HTMLWindow6_get_maxConnectionsPerServer,
1959     HTMLWindow6_postMessage,
1960     HTMLWindow6_toStaticHTML,
1961     HTMLWindow6_put_onmessage,
1962     HTMLWindow6_get_onmessage,
1963     HTMLWindow6_msWriteProfilerMark
1964 };
1965
1966 static inline HTMLWindow *impl_from_IHTMLPrivateWindow(IHTMLPrivateWindow *iface)
1967 {
1968     return CONTAINING_RECORD(iface, HTMLWindow, IHTMLPrivateWindow_iface);
1969 }
1970
1971 static HRESULT WINAPI HTMLPrivateWindow_QueryInterface(IHTMLPrivateWindow *iface, REFIID riid, void **ppv)
1972 {
1973     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1974
1975     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
1976 }
1977
1978 static ULONG WINAPI HTMLPrivateWindow_AddRef(IHTMLPrivateWindow *iface)
1979 {
1980     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1981
1982     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
1983 }
1984
1985 static ULONG WINAPI HTMLPrivateWindow_Release(IHTMLPrivateWindow *iface)
1986 {
1987     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1988
1989     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
1990 }
1991
1992 static HRESULT WINAPI HTMLPrivateWindow_SuperNavigate(IHTMLPrivateWindow *iface, BSTR url, BSTR arg2, BSTR arg3,
1993         BSTR arg4, VARIANT *post_data_var, VARIANT *headers_var, ULONG flags)
1994 {
1995     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
1996     HTMLOuterWindow *window = This->outer_window;
1997     OLECHAR *translated_url = NULL;
1998     DWORD post_data_size = 0;
1999     BYTE *post_data = NULL;
2000     WCHAR *headers = NULL;
2001     IUri *uri;
2002     HRESULT hres;
2003
2004     TRACE("(%p)->(%s %s %s %s %s %s %x)\n", This, debugstr_w(url), debugstr_w(arg2), debugstr_w(arg3), debugstr_w(arg4),
2005           debugstr_variant(post_data_var), debugstr_variant(headers_var), flags);
2006
2007     if(window->doc_obj->hostui) {
2008         hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url, &translated_url);
2009         if(hres != S_OK)
2010             translated_url = NULL;
2011     }
2012
2013     hres = CreateUri(translated_url ? translated_url : url, 0, 0, &uri);
2014     CoTaskMemFree(translated_url);
2015     if(FAILED(hres))
2016         return hres;
2017
2018     if(post_data_var) {
2019         if(V_VT(post_data_var) == (VT_ARRAY|VT_UI1)) {
2020             SafeArrayAccessData(V_ARRAY(post_data_var), (void**)&post_data);
2021             post_data_size = V_ARRAY(post_data_var)->rgsabound[0].cElements;
2022         }
2023     }
2024
2025     if(headers_var && V_VT(headers_var) != VT_EMPTY && V_VT(headers_var) != VT_ERROR) {
2026         if(V_VT(headers_var) != VT_BSTR)
2027             return E_INVALIDARG;
2028
2029         headers = V_BSTR(headers_var);
2030     }
2031
2032     hres = super_navigate(window, uri, headers, post_data, post_data_size);
2033     IUri_Release(uri);
2034     if(post_data)
2035         SafeArrayUnaccessData(V_ARRAY(post_data_var));
2036
2037     return hres;
2038 }
2039
2040 static HRESULT WINAPI HTMLPrivateWindow_GetPendingUrl(IHTMLPrivateWindow *iface, BSTR *url)
2041 {
2042     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2043     FIXME("(%p)->(%p)\n", This, url);
2044     return E_NOTIMPL;
2045 }
2046
2047 static HRESULT WINAPI HTMLPrivateWindow_SetPICSTarget(IHTMLPrivateWindow *iface, IOleCommandTarget *cmdtrg)
2048 {
2049     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2050     FIXME("(%p)->(%p)\n", This, cmdtrg);
2051     return E_NOTIMPL;
2052 }
2053
2054 static HRESULT WINAPI HTMLPrivateWindow_PICSComplete(IHTMLPrivateWindow *iface, int arg)
2055 {
2056     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2057     FIXME("(%p)->(%x)\n", This, arg);
2058     return E_NOTIMPL;
2059 }
2060
2061 static HRESULT WINAPI HTMLPrivateWindow_FindWindowByName(IHTMLPrivateWindow *iface, LPCWSTR name, IHTMLWindow2 **ret)
2062 {
2063     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2064     FIXME("(%p)->(%s %p)\n", This, debugstr_w(name), ret);
2065     return E_NOTIMPL;
2066 }
2067
2068 static HRESULT WINAPI HTMLPrivateWindow_GetAddressBarUrl(IHTMLPrivateWindow *iface, BSTR *url)
2069 {
2070     HTMLWindow *This = impl_from_IHTMLPrivateWindow(iface);
2071     TRACE("(%p)->(%p)\n", This, url);
2072
2073     if(!url)
2074         return E_INVALIDARG;
2075
2076     *url = SysAllocString(This->outer_window->url);
2077     return S_OK;
2078 }
2079
2080 static const IHTMLPrivateWindowVtbl HTMLPrivateWindowVtbl = {
2081     HTMLPrivateWindow_QueryInterface,
2082     HTMLPrivateWindow_AddRef,
2083     HTMLPrivateWindow_Release,
2084     HTMLPrivateWindow_SuperNavigate,
2085     HTMLPrivateWindow_GetPendingUrl,
2086     HTMLPrivateWindow_SetPICSTarget,
2087     HTMLPrivateWindow_PICSComplete,
2088     HTMLPrivateWindow_FindWindowByName,
2089     HTMLPrivateWindow_GetAddressBarUrl
2090 };
2091
2092 static inline HTMLWindow *impl_from_ITravelLogClient(ITravelLogClient *iface)
2093 {
2094     return CONTAINING_RECORD(iface, HTMLWindow, ITravelLogClient_iface);
2095 }
2096
2097 static HRESULT WINAPI TravelLogClient_QueryInterface(ITravelLogClient *iface, REFIID riid, void **ppv)
2098 {
2099     HTMLWindow *This = impl_from_ITravelLogClient(iface);
2100
2101     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2102 }
2103
2104 static ULONG WINAPI TravelLogClient_AddRef(ITravelLogClient *iface)
2105 {
2106     HTMLWindow *This = impl_from_ITravelLogClient(iface);
2107
2108     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
2109 }
2110
2111 static ULONG WINAPI TravelLogClient_Release(ITravelLogClient *iface)
2112 {
2113     HTMLWindow *This = impl_from_ITravelLogClient(iface);
2114
2115     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
2116 }
2117
2118 static HRESULT WINAPI TravelLogClient_FindWindowByIndex(ITravelLogClient *iface, DWORD dwID, IUnknown **ppunk)
2119 {
2120     HTMLWindow *This = impl_from_ITravelLogClient(iface);
2121
2122     FIXME("(%p)->(%d %p) semi-stub\n", This, dwID, ppunk);
2123
2124     *ppunk = NULL;
2125     return E_FAIL;
2126 }
2127
2128 static HRESULT WINAPI TravelLogClient_GetWindowData(ITravelLogClient *iface, IStream *pStream, LPWINDOWDATA pWinData)
2129 {
2130     HTMLWindow *This = impl_from_ITravelLogClient(iface);
2131     FIXME("(%p)->(%p %p)\n", This, pStream, pWinData);
2132     return E_NOTIMPL;
2133 }
2134
2135 static HRESULT WINAPI TravelLogClient_LoadHistoryPosition(ITravelLogClient *iface, LPWSTR pszUrlLocation, DWORD dwPosition)
2136 {
2137     HTMLWindow *This = impl_from_ITravelLogClient(iface);
2138     FIXME("(%p)->(%s %d)\n", This, debugstr_w(pszUrlLocation), dwPosition);
2139     return E_NOTIMPL;
2140 }
2141
2142 static const ITravelLogClientVtbl TravelLogClientVtbl = {
2143     TravelLogClient_QueryInterface,
2144     TravelLogClient_AddRef,
2145     TravelLogClient_Release,
2146     TravelLogClient_FindWindowByIndex,
2147     TravelLogClient_GetWindowData,
2148     TravelLogClient_LoadHistoryPosition
2149 };
2150
2151 static inline HTMLWindow *impl_from_IDispatchEx(IDispatchEx *iface)
2152 {
2153     return CONTAINING_RECORD(iface, HTMLWindow, IDispatchEx_iface);
2154 }
2155
2156 static HRESULT WINAPI WindowDispEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
2157 {
2158     HTMLWindow *This = impl_from_IDispatchEx(iface);
2159
2160     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2161 }
2162
2163 static ULONG WINAPI WindowDispEx_AddRef(IDispatchEx *iface)
2164 {
2165     HTMLWindow *This = impl_from_IDispatchEx(iface);
2166
2167     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
2168 }
2169
2170 static ULONG WINAPI WindowDispEx_Release(IDispatchEx *iface)
2171 {
2172     HTMLWindow *This = impl_from_IDispatchEx(iface);
2173
2174     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
2175 }
2176
2177 static HRESULT WINAPI WindowDispEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
2178 {
2179     HTMLWindow *This = impl_from_IDispatchEx(iface);
2180
2181     TRACE("(%p)->(%p)\n", This, pctinfo);
2182
2183     return IDispatchEx_GetTypeInfoCount(&This->inner_window->dispex.IDispatchEx_iface, pctinfo);
2184 }
2185
2186 static HRESULT WINAPI WindowDispEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
2187                                                LCID lcid, ITypeInfo **ppTInfo)
2188 {
2189     HTMLWindow *This = impl_from_IDispatchEx(iface);
2190
2191     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
2192
2193     return IDispatchEx_GetTypeInfo(&This->inner_window->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
2194 }
2195
2196 static HRESULT WINAPI WindowDispEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
2197                                                  LPOLESTR *rgszNames, UINT cNames,
2198                                                  LCID lcid, DISPID *rgDispId)
2199 {
2200     HTMLWindow *This = impl_from_IDispatchEx(iface);
2201     UINT i;
2202     HRESULT hres;
2203
2204     WARN("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
2205           lcid, rgDispId);
2206
2207     for(i=0; i < cNames; i++) {
2208         /* We shouldn't use script's IDispatchEx here, so we shouldn't use GetDispID */
2209         hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
2210         if(FAILED(hres))
2211             return hres;
2212     }
2213
2214     return S_OK;
2215 }
2216
2217 static HRESULT WINAPI WindowDispEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
2218                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
2219                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
2220 {
2221     HTMLWindow *This = impl_from_IDispatchEx(iface);
2222
2223     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
2224           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
2225
2226     /* FIXME: Use script dispatch */
2227
2228     return IDispatchEx_Invoke(&This->inner_window->dispex.IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
2229             pDispParams, pVarResult, pExcepInfo, puArgErr);
2230 }
2231
2232 static global_prop_t *alloc_global_prop(HTMLInnerWindow *This, global_prop_type_t type, BSTR name)
2233 {
2234     if(This->global_prop_cnt == This->global_prop_size) {
2235         global_prop_t *new_props;
2236         DWORD new_size;
2237
2238         if(This->global_props) {
2239             new_size = This->global_prop_size*2;
2240             new_props = heap_realloc(This->global_props, new_size*sizeof(global_prop_t));
2241         }else {
2242             new_size = 16;
2243             new_props = heap_alloc(new_size*sizeof(global_prop_t));
2244         }
2245         if(!new_props)
2246             return NULL;
2247         This->global_props = new_props;
2248         This->global_prop_size = new_size;
2249     }
2250
2251     This->global_props[This->global_prop_cnt].name = heap_strdupW(name);
2252     if(!This->global_props[This->global_prop_cnt].name)
2253         return NULL;
2254
2255     This->global_props[This->global_prop_cnt].type = type;
2256     return This->global_props + This->global_prop_cnt++;
2257 }
2258
2259 static inline DWORD prop_to_dispid(HTMLInnerWindow *This, global_prop_t *prop)
2260 {
2261     return MSHTML_DISPID_CUSTOM_MIN + (prop-This->global_props);
2262 }
2263
2264 HRESULT search_window_props(HTMLInnerWindow *This, BSTR bstrName, DWORD grfdex, DISPID *pid)
2265 {
2266     DWORD i;
2267     ScriptHost *script_host;
2268     DISPID id;
2269
2270     for(i=0; i < This->global_prop_cnt; i++) {
2271         /* FIXME: case sensitivity */
2272         if(!strcmpW(This->global_props[i].name, bstrName)) {
2273             *pid = MSHTML_DISPID_CUSTOM_MIN+i;
2274             return S_OK;
2275         }
2276     }
2277
2278     if(find_global_prop(This->base.inner_window, bstrName, grfdex, &script_host, &id)) {
2279         global_prop_t *prop;
2280
2281         prop = alloc_global_prop(This, GLOBAL_SCRIPTVAR, bstrName);
2282         if(!prop)
2283             return E_OUTOFMEMORY;
2284
2285         prop->script_host = script_host;
2286         prop->id = id;
2287
2288         *pid = prop_to_dispid(This, prop);
2289         return S_OK;
2290     }
2291
2292     return DISP_E_UNKNOWNNAME;
2293 }
2294
2295 static HRESULT WINAPI WindowDispEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
2296 {
2297     HTMLWindow *This = impl_from_IDispatchEx(iface);
2298     HTMLInnerWindow *window = This->inner_window;
2299     HRESULT hres;
2300
2301     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
2302
2303     hres = search_window_props(window, bstrName, grfdex, pid);
2304     if(hres != DISP_E_UNKNOWNNAME)
2305         return hres;
2306
2307     hres = IDispatchEx_GetDispID(&window->base.inner_window->dispex.IDispatchEx_iface, bstrName, grfdex, pid);
2308     if(hres != DISP_E_UNKNOWNNAME)
2309         return hres;
2310
2311     if(window->base.inner_window->doc) {
2312         global_prop_t *prop;
2313         IHTMLElement *elem;
2314
2315         hres = IHTMLDocument3_getElementById(&window->base.inner_window->doc->basedoc.IHTMLDocument3_iface,
2316                                              bstrName, &elem);
2317         if(SUCCEEDED(hres) && elem) {
2318             IHTMLElement_Release(elem);
2319
2320             prop = alloc_global_prop(window, GLOBAL_ELEMENTVAR, bstrName);
2321             if(!prop)
2322                 return E_OUTOFMEMORY;
2323
2324             *pid = prop_to_dispid(window, prop);
2325             return S_OK;
2326         }
2327     }
2328
2329     return DISP_E_UNKNOWNNAME;
2330 }
2331
2332 static HRESULT WINAPI WindowDispEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
2333         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
2334 {
2335     HTMLWindow *This = impl_from_IDispatchEx(iface);
2336     HTMLOuterWindow *window = This->outer_window;
2337
2338     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
2339
2340     if(id == DISPID_IHTMLWINDOW2_LOCATION && (wFlags & DISPATCH_PROPERTYPUT)) {
2341         HTMLLocation *location;
2342         HRESULT hres;
2343
2344         TRACE("forwarding to location.href\n");
2345
2346         hres = get_location(window, &location);
2347         if(FAILED(hres))
2348             return hres;
2349
2350         hres = IDispatchEx_InvokeEx(&location->dispex.IDispatchEx_iface, DISPID_VALUE, lcid,
2351                 wFlags, pdp, pvarRes, pei, pspCaller);
2352         IHTMLLocation_Release(&location->IHTMLLocation_iface);
2353         return hres;
2354     }
2355
2356     return IDispatchEx_InvokeEx(&window->base.inner_window->dispex.IDispatchEx_iface, id, lcid, wFlags, pdp, pvarRes,
2357             pei, pspCaller);
2358 }
2359
2360 static HRESULT WINAPI WindowDispEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
2361 {
2362     HTMLWindow *This = impl_from_IDispatchEx(iface);
2363
2364     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
2365
2366     return IDispatchEx_DeleteMemberByName(&This->inner_window->dispex.IDispatchEx_iface, bstrName, grfdex);
2367 }
2368
2369 static HRESULT WINAPI WindowDispEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
2370 {
2371     HTMLWindow *This = impl_from_IDispatchEx(iface);
2372
2373     TRACE("(%p)->(%x)\n", This, id);
2374
2375     return IDispatchEx_DeleteMemberByDispID(&This->inner_window->dispex.IDispatchEx_iface, id);
2376 }
2377
2378 static HRESULT WINAPI WindowDispEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
2379 {
2380     HTMLWindow *This = impl_from_IDispatchEx(iface);
2381
2382     TRACE("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
2383
2384     return IDispatchEx_GetMemberProperties(&This->inner_window->dispex.IDispatchEx_iface, id, grfdexFetch,
2385             pgrfdex);
2386 }
2387
2388 static HRESULT WINAPI WindowDispEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
2389 {
2390     HTMLWindow *This = impl_from_IDispatchEx(iface);
2391
2392     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
2393
2394     return IDispatchEx_GetMemberName(&This->inner_window->dispex.IDispatchEx_iface, id, pbstrName);
2395 }
2396
2397 static HRESULT WINAPI WindowDispEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
2398 {
2399     HTMLWindow *This = impl_from_IDispatchEx(iface);
2400
2401     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
2402
2403     return IDispatchEx_GetNextDispID(&This->inner_window->dispex.IDispatchEx_iface, grfdex, id, pid);
2404 }
2405
2406 static HRESULT WINAPI WindowDispEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
2407 {
2408     HTMLWindow *This = impl_from_IDispatchEx(iface);
2409
2410     TRACE("(%p)->(%p)\n", This, ppunk);
2411
2412     *ppunk = NULL;
2413     return S_OK;
2414 }
2415
2416 static const IDispatchExVtbl WindowDispExVtbl = {
2417     WindowDispEx_QueryInterface,
2418     WindowDispEx_AddRef,
2419     WindowDispEx_Release,
2420     WindowDispEx_GetTypeInfoCount,
2421     WindowDispEx_GetTypeInfo,
2422     WindowDispEx_GetIDsOfNames,
2423     WindowDispEx_Invoke,
2424     WindowDispEx_GetDispID,
2425     WindowDispEx_InvokeEx,
2426     WindowDispEx_DeleteMemberByName,
2427     WindowDispEx_DeleteMemberByDispID,
2428     WindowDispEx_GetMemberProperties,
2429     WindowDispEx_GetMemberName,
2430     WindowDispEx_GetNextDispID,
2431     WindowDispEx_GetNameSpaceParent
2432 };
2433
2434 static inline HTMLWindow *impl_from_IServiceProvider(IServiceProvider *iface)
2435 {
2436     return CONTAINING_RECORD(iface, HTMLWindow, IServiceProvider_iface);
2437 }
2438
2439 static HRESULT WINAPI HTMLWindowSP_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
2440 {
2441     HTMLWindow *This = impl_from_IServiceProvider(iface);
2442     return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2443 }
2444
2445 static ULONG WINAPI HTMLWindowSP_AddRef(IServiceProvider *iface)
2446 {
2447     HTMLWindow *This = impl_from_IServiceProvider(iface);
2448     return IHTMLWindow2_AddRef(&This->IHTMLWindow2_iface);
2449 }
2450
2451 static ULONG WINAPI HTMLWindowSP_Release(IServiceProvider *iface)
2452 {
2453     HTMLWindow *This = impl_from_IServiceProvider(iface);
2454     return IHTMLWindow2_Release(&This->IHTMLWindow2_iface);
2455 }
2456
2457 static HRESULT WINAPI HTMLWindowSP_QueryService(IServiceProvider *iface, REFGUID guidService, REFIID riid, void **ppv)
2458 {
2459     HTMLWindow *This = impl_from_IServiceProvider(iface);
2460
2461     if(IsEqualGUID(guidService, &IID_IHTMLWindow2)) {
2462         TRACE("IID_IHTMLWindow2\n");
2463         return IHTMLWindow2_QueryInterface(&This->IHTMLWindow2_iface, riid, ppv);
2464     }
2465
2466     TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
2467
2468     if(!This->outer_window->doc_obj)
2469         return E_NOINTERFACE;
2470
2471     return IServiceProvider_QueryService(&This->outer_window->doc_obj->basedoc.IServiceProvider_iface,
2472             guidService, riid, ppv);
2473 }
2474
2475 static const IServiceProviderVtbl ServiceProviderVtbl = {
2476     HTMLWindowSP_QueryInterface,
2477     HTMLWindowSP_AddRef,
2478     HTMLWindowSP_Release,
2479     HTMLWindowSP_QueryService
2480 };
2481
2482 static inline HTMLInnerWindow *impl_from_DispatchEx(DispatchEx *iface)
2483 {
2484     return CONTAINING_RECORD(iface, HTMLInnerWindow, dispex);
2485 }
2486
2487 static HRESULT HTMLWindow_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
2488         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
2489 {
2490     HTMLInnerWindow *This = impl_from_DispatchEx(dispex);
2491     global_prop_t *prop;
2492     DWORD idx;
2493     HRESULT hres;
2494
2495     idx = id - MSHTML_DISPID_CUSTOM_MIN;
2496     if(idx >= This->global_prop_cnt)
2497         return DISP_E_MEMBERNOTFOUND;
2498
2499     prop = This->global_props+idx;
2500
2501     switch(prop->type) {
2502     case GLOBAL_SCRIPTVAR: {
2503         IDispatchEx *iface;
2504         IDispatch *disp;
2505
2506         disp = get_script_disp(prop->script_host);
2507         if(!disp)
2508             return E_UNEXPECTED;
2509
2510         hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&iface);
2511         if(SUCCEEDED(hres)) {
2512             TRACE("%s >>>\n", debugstr_w(prop->name));
2513             hres = IDispatchEx_InvokeEx(iface, prop->id, lcid, flags, params, res, ei, caller);
2514             if(hres == S_OK)
2515                 TRACE("%s <<<\n", debugstr_w(prop->name));
2516             else
2517                 WARN("%s <<< %08x\n", debugstr_w(prop->name), hres);
2518             IDispatchEx_Release(iface);
2519         }else {
2520             FIXME("No IDispatchEx\n");
2521         }
2522         IDispatch_Release(disp);
2523         break;
2524     }
2525     case GLOBAL_ELEMENTVAR:
2526         switch(flags) {
2527         case DISPATCH_PROPERTYGET: {
2528             IHTMLElement *elem;
2529
2530             hres = IHTMLDocument3_getElementById(&This->base.inner_window->doc->basedoc.IHTMLDocument3_iface,
2531                     prop->name, &elem);
2532             if(FAILED(hres))
2533                 return hres;
2534
2535             if(!elem)
2536                 return DISP_E_MEMBERNOTFOUND;
2537
2538             V_VT(res) = VT_DISPATCH;
2539             V_DISPATCH(res) = (IDispatch*)elem;
2540             return S_OK;
2541         }
2542         case DISPATCH_PROPERTYPUT: {
2543             DISPID dispex_id;
2544
2545             hres = dispex_get_dynid(&This->dispex, prop->name, &dispex_id);
2546             if(FAILED(hres))
2547                 return hres;
2548
2549             prop->type = GLOBAL_DISPEXVAR;
2550             prop->id = dispex_id;
2551             return IDispatchEx_InvokeEx(&This->dispex.IDispatchEx_iface, dispex_id, 0, flags, params, res, ei, caller);
2552         }
2553         default:
2554             FIXME("Not supported flags: %x\n", flags);
2555             return E_NOTIMPL;
2556         }
2557     case GLOBAL_DISPEXVAR:
2558         return IDispatchEx_InvokeEx(&This->dispex.IDispatchEx_iface, prop->id, 0, flags, params, res, ei, caller);
2559     default:
2560         ERR("invalid type %d\n", prop->type);
2561         hres = DISP_E_MEMBERNOTFOUND;
2562     }
2563
2564     return hres;
2565 }
2566
2567
2568 static const dispex_static_data_vtbl_t HTMLWindow_dispex_vtbl = {
2569     NULL,
2570     NULL,
2571     HTMLWindow_invoke,
2572     NULL
2573 };
2574
2575 static const tid_t HTMLWindow_iface_tids[] = {
2576     IHTMLWindow2_tid,
2577     IHTMLWindow3_tid,
2578     IHTMLWindow4_tid,
2579     IHTMLWindow6_tid,
2580     0
2581 };
2582
2583 static dispex_static_data_t HTMLWindow_dispex = {
2584     &HTMLWindow_dispex_vtbl,
2585     DispHTMLWindow2_tid,
2586     NULL,
2587     HTMLWindow_iface_tids
2588 };
2589
2590 static void *alloc_window(size_t size)
2591 {
2592     HTMLWindow *window;
2593
2594     window = heap_alloc_zero(size);
2595     if(!window)
2596         return NULL;
2597
2598     window->IHTMLWindow2_iface.lpVtbl = &HTMLWindow2Vtbl;
2599     window->IHTMLWindow3_iface.lpVtbl = &HTMLWindow3Vtbl;
2600     window->IHTMLWindow4_iface.lpVtbl = &HTMLWindow4Vtbl;
2601     window->IHTMLWindow5_iface.lpVtbl = &HTMLWindow5Vtbl;
2602     window->IHTMLWindow6_iface.lpVtbl = &HTMLWindow6Vtbl;
2603     window->IHTMLPrivateWindow_iface.lpVtbl = &HTMLPrivateWindowVtbl;
2604     window->IDispatchEx_iface.lpVtbl = &WindowDispExVtbl;
2605     window->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
2606     window->ITravelLogClient_iface.lpVtbl = &TravelLogClientVtbl;
2607     window->ref = 1;
2608
2609     return window;
2610 }
2611
2612 static HRESULT create_inner_window(HTMLOuterWindow *outer_window, HTMLInnerWindow **ret)
2613 {
2614     HTMLInnerWindow *window;
2615
2616     window = alloc_window(sizeof(HTMLInnerWindow));
2617     if(!window)
2618         return E_OUTOFMEMORY;
2619
2620     list_init(&window->script_hosts);
2621     list_init(&window->bindings);
2622
2623     window->base.outer_window = outer_window;
2624     window->base.inner_window = window;
2625
2626     init_dispex(&window->dispex, (IUnknown*)&window->base.IHTMLWindow2_iface, &HTMLWindow_dispex);
2627
2628     window->task_magic = get_task_target_magic();
2629
2630     *ret = window;
2631     return S_OK;
2632 }
2633
2634 HRESULT HTMLOuterWindow_Create(HTMLDocumentObj *doc_obj, nsIDOMWindow *nswindow,
2635         HTMLOuterWindow *parent, HTMLOuterWindow **ret)
2636 {
2637     HTMLOuterWindow *window;
2638     HRESULT hres;
2639
2640     window = alloc_window(sizeof(HTMLOuterWindow));
2641     if(!window)
2642         return E_OUTOFMEMORY;
2643
2644     window->base.outer_window = window;
2645     window->base.inner_window = NULL;
2646
2647     window->window_ref = heap_alloc(sizeof(windowref_t));
2648     if(!window->window_ref) {
2649         heap_free(window);
2650         return E_OUTOFMEMORY;
2651     }
2652
2653     window->doc_obj = doc_obj;
2654
2655     window->window_ref->window = window;
2656     window->window_ref->ref = 1;
2657
2658     if(nswindow) {
2659         nsIDOMWindow_AddRef(nswindow);
2660         window->nswindow = nswindow;
2661     }
2662
2663     window->scriptmode = parent ? parent->scriptmode : SCRIPTMODE_GECKO;
2664     window->readystate = READYSTATE_UNINITIALIZED;
2665
2666     hres = create_pending_window(window, NULL);
2667     if(SUCCEEDED(hres))
2668         hres = update_window_doc(window->pending_window);
2669     if(FAILED(hres)) {
2670         IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
2671         return hres;
2672     }
2673
2674     hres = CoInternetCreateSecurityManager(NULL, &window->secmgr, 0);
2675     if(FAILED(hres)) {
2676         IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
2677         return hres;
2678     }
2679
2680     window->task_magic = get_task_target_magic();
2681
2682     list_init(&window->children);
2683     list_add_head(&window_list, &window->entry);
2684
2685     if(parent) {
2686         IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
2687
2688         window->parent = parent;
2689         list_add_tail(&parent->children, &window->sibling_entry);
2690     }
2691
2692     TRACE("%p inner_window %p\n", window, window->base.inner_window);
2693
2694     *ret = window;
2695     return S_OK;
2696 }
2697
2698 HRESULT create_pending_window(HTMLOuterWindow *outer_window, nsChannelBSC *channelbsc)
2699 {
2700     HTMLInnerWindow *pending_window;
2701     HRESULT hres;
2702
2703     hres = create_inner_window(outer_window, &pending_window);
2704     if(FAILED(hres))
2705         return hres;
2706
2707     if(channelbsc) {
2708         IBindStatusCallback_AddRef(&channelbsc->bsc.IBindStatusCallback_iface);
2709         pending_window->bscallback = channelbsc;
2710     }
2711
2712     if(outer_window->pending_window) {
2713         abort_window_bindings(outer_window->pending_window);
2714         outer_window->pending_window->base.outer_window = NULL;
2715         IHTMLWindow2_Release(&outer_window->pending_window->base.IHTMLWindow2_iface);
2716     }
2717
2718     outer_window->pending_window = pending_window;
2719     return S_OK;
2720 }
2721
2722 HRESULT update_window_doc(HTMLInnerWindow *window)
2723 {
2724     HTMLOuterWindow *outer_window = window->base.outer_window;
2725     nsIDOMHTMLDocument *nshtmldoc;
2726     nsIDOMDocument *nsdoc;
2727     nsresult nsres;
2728     HRESULT hres;
2729
2730     assert(!window->doc);
2731
2732     if(!outer_window) {
2733         ERR("NULL outer window\n");
2734         return E_UNEXPECTED;
2735     }
2736
2737     nsres = nsIDOMWindow_GetDocument(outer_window->nswindow, &nsdoc);
2738     if(NS_FAILED(nsres) || !nsdoc) {
2739         ERR("GetDocument failed: %08x\n", nsres);
2740         return E_FAIL;
2741     }
2742
2743     nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
2744     nsIDOMDocument_Release(nsdoc);
2745     if(NS_FAILED(nsres)) {
2746         ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
2747         return E_FAIL;
2748     }
2749
2750     hres = create_doc_from_nsdoc(nshtmldoc, outer_window->doc_obj, window, &window->doc);
2751     nsIDOMHTMLDocument_Release(nshtmldoc);
2752     if(FAILED(hres))
2753         return hres;
2754
2755     if(outer_window->doc_obj->usermode == EDITMODE) {
2756         nsAString mode_str;
2757         nsresult nsres;
2758
2759         static const PRUnichar onW[] = {'o','n',0};
2760
2761         nsAString_Init(&mode_str, onW);
2762         nsres = nsIDOMHTMLDocument_SetDesignMode(window->doc->nsdoc, &mode_str);
2763         nsAString_Finish(&mode_str);
2764         if(NS_FAILED(nsres))
2765             ERR("SetDesignMode failed: %08x\n", nsres);
2766     }
2767
2768     if(window != outer_window->pending_window) {
2769         ERR("not current pending window\n");
2770         return S_OK;
2771     }
2772
2773     detach_inner_window(outer_window);
2774     outer_window->base.inner_window = window;
2775     outer_window->pending_window = NULL;
2776
2777     if(outer_window->doc_obj->basedoc.window == outer_window || !outer_window->doc_obj->basedoc.window) {
2778         if(outer_window->doc_obj->basedoc.doc_node)
2779             htmldoc_release(&outer_window->doc_obj->basedoc.doc_node->basedoc);
2780         outer_window->doc_obj->basedoc.doc_node = window->doc;
2781         htmldoc_addref(&window->doc->basedoc);
2782     }
2783
2784     return hres;
2785 }
2786
2787 HTMLOuterWindow *nswindow_to_window(const nsIDOMWindow *nswindow)
2788 {
2789     HTMLOuterWindow *iter;
2790
2791     LIST_FOR_EACH_ENTRY(iter, &window_list, HTMLOuterWindow, entry) {
2792         if(iter->nswindow == nswindow)
2793             return iter;
2794     }
2795
2796     return NULL;
2797 }