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