mshtml: Wine Gecko 0.9.0 release.
[wine] / dlls / mshtml / nsembed.c
1 /*
2  * Copyright 2005-2007 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 "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "ole2.h"
30
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
39 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
40 #define NS_PROFILE_CONTRACTID "@mozilla.org/profile/manager;1"
41 #define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
42 #define NS_STRINGSTREAM_CONTRACTID "@mozilla.org/io/string-input-stream;1"
43 #define NS_COMMANDPARAMS_CONTRACTID "@mozilla.org/embedcomp/command-params;1"
44 #define NS_HTMLSERIALIZER_CONTRACTID "@mozilla.org/layout/contentserializer;1?mimetype=text/html"
45 #define NS_EDITORCONTROLLER_CONTRACTID "@mozilla.org/editor/editorcontroller;1"
46 #define NS_ARRAY_CONTRACTID "@mozilla.org/array;1"
47 #define NS_VARIANT_CONTRACTID "@mozilla.org/variant;1"
48 #define NS_PREFERENCES_CONTRACTID "@mozilla.org/preferences;1"
49
50 #define APPSTARTUP_TOPIC "app-startup"
51
52 #define PR_UINT32_MAX 0xffffffff
53
54 struct nsCStringContainer {
55     void *v;
56     void *d1;
57     PRUint32 d2;
58     void *d3;
59 };
60
61 static nsresult (*NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
62 static nsresult (*NS_ShutdownXPCOM)(nsIServiceManager*);
63 static nsresult (*NS_GetComponentRegistrar)(nsIComponentRegistrar**);
64 static nsresult (*NS_StringContainerInit)(nsStringContainer*);
65 static nsresult (*NS_CStringContainerInit)(nsCStringContainer*);
66 static nsresult (*NS_StringContainerFinish)(nsStringContainer*);
67 static nsresult (*NS_CStringContainerFinish)(nsCStringContainer*);
68 static nsresult (*NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
69 static nsresult (*NS_CStringSetData)(nsACString*,const char*,PRUint32);
70 static nsresult (*NS_NewLocalFile)(const nsAString*,PRBool,nsIFile**);
71 static PRUint32 (*NS_StringGetData)(const nsAString*,const PRUnichar **,PRBool*);
72 static PRUint32 (*NS_CStringGetData)(const nsACString*,const char**,PRBool*);
73
74 static HINSTANCE hXPCOM = NULL;
75
76 static nsIServiceManager *pServMgr = NULL;
77 static nsIComponentManager *pCompMgr = NULL;
78 static nsIMemory *nsmem = NULL;
79
80 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
81
82 static ATOM nscontainer_class;
83
84 #define WM_RESETFOCUS_HACK WM_USER+600
85
86 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
87 {
88     NSContainer *This;
89     nsresult nsres;
90
91     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
92
93     if(msg == WM_CREATE) {
94         This = *(NSContainer**)lParam;
95         SetPropW(hwnd, wszTHIS, This);
96     }else {
97         This = GetPropW(hwnd, wszTHIS);
98     }
99
100     switch(msg) {
101     case WM_SIZE:
102         TRACE("(%p)->(WM_SIZE)\n", This);
103
104         nsres = nsIBaseWindow_SetSize(This->window,
105                 LOWORD(lParam), HIWORD(lParam), TRUE);
106         if(NS_FAILED(nsres))
107             WARN("SetSize failed: %08x\n", nsres);
108         break;
109
110     case WM_RESETFOCUS_HACK:
111         /*
112          * FIXME
113          * Gecko grabs focus in edit mode and some apps don't like it.
114          * We should somehow prevent grabbing focus.
115          */
116
117         TRACE("WM_RESETFOCUS_HACK\n");
118
119         if(This->reset_focus) {
120             SetFocus(This->reset_focus);
121             This->reset_focus = NULL;
122             if(This->doc)
123                 This->doc->focus = FALSE;
124         }
125     }
126
127     return DefWindowProcW(hwnd, msg, wParam, lParam);
128 }
129
130
131 static void register_nscontainer_class(void)
132 {
133     static WNDCLASSEXW wndclass = {
134         sizeof(WNDCLASSEXW),
135         CS_DBLCLKS,
136         nsembed_proc,
137         0, 0, NULL, NULL, NULL, NULL, NULL,
138         wszNsContainer,
139         NULL,
140     };
141     wndclass.hInstance = hInst;
142     nscontainer_class = RegisterClassExW(&wndclass);
143 }
144
145 static BOOL load_xpcom(const PRUnichar *gre_path)
146 {
147     WCHAR path_env[MAX_PATH];
148     int len;
149
150     static const WCHAR wszPATH[] = {'P','A','T','H',0};
151     static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
152
153     TRACE("(%s)\n", debugstr_w(gre_path));
154
155     /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
156     GetEnvironmentVariableW(wszPATH, path_env, sizeof(path_env)/sizeof(WCHAR));
157     len = strlenW(path_env);
158     path_env[len++] = ';';
159     strcpyW(path_env+len, gre_path);
160     SetEnvironmentVariableW(wszPATH, path_env);
161
162     hXPCOM = LoadLibraryW(strXPCOM);
163     if(!hXPCOM) {
164         WARN("Could not load XPCOM: %d\n", GetLastError());
165         return FALSE;
166     }
167
168 #define NS_DLSYM(func) \
169     func = (void *)GetProcAddress(hXPCOM, #func); \
170     if(!func) \
171         ERR("Could not GetProcAddress(" #func ") failed\n")
172
173     NS_DLSYM(NS_InitXPCOM2);
174     NS_DLSYM(NS_ShutdownXPCOM);
175     NS_DLSYM(NS_GetComponentRegistrar);
176     NS_DLSYM(NS_StringContainerInit);
177     NS_DLSYM(NS_CStringContainerInit);
178     NS_DLSYM(NS_StringContainerFinish);
179     NS_DLSYM(NS_CStringContainerFinish);
180     NS_DLSYM(NS_StringSetData);
181     NS_DLSYM(NS_CStringSetData);
182     NS_DLSYM(NS_NewLocalFile);
183     NS_DLSYM(NS_StringGetData);
184     NS_DLSYM(NS_CStringGetData);
185
186 #undef NS_DLSYM
187
188     return TRUE;
189 }
190
191 static BOOL check_version(LPCWSTR gre_path, const char *version_string)
192 {
193     WCHAR file_name[MAX_PATH];
194     char version[128];
195     DWORD read=0;
196     HANDLE hfile;
197
198     static const WCHAR wszVersion[] = {'\\','V','E','R','S','I','O','N',0};
199
200     strcpyW(file_name, gre_path);
201     strcatW(file_name, wszVersion);
202
203     hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
204                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
205     if(hfile == INVALID_HANDLE_VALUE) {
206         ERR("Could not open VERSION file\n");
207         return FALSE;
208     }
209
210     ReadFile(hfile, version, sizeof(version), &read, NULL);
211     version[read] = 0;
212     CloseHandle(hfile);
213
214     TRACE("%s\n", debugstr_a(version));
215
216     if(strcmp(version, version_string)) {
217         ERR("Unexpected version %s, expected %s\n", debugstr_a(version),
218             debugstr_a(version_string));
219         return FALSE;
220     }
221
222     return TRUE;
223 }
224
225 static BOOL load_wine_gecko_v(PRUnichar *gre_path, HKEY mshtml_key,
226         const char *version, const char *version_string)
227 {
228     DWORD res, type, size = MAX_PATH;
229     HKEY hkey = mshtml_key;
230
231     static const WCHAR wszGeckoPath[] =
232         {'G','e','c','k','o','P','a','t','h',0};
233
234     if(version) {
235         /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
236         res = RegOpenKeyA(mshtml_key, version, &hkey);
237         if(res != ERROR_SUCCESS)
238             return FALSE;
239     }
240
241     res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
242     if(hkey != mshtml_key)
243         RegCloseKey(hkey);
244     if(res != ERROR_SUCCESS || type != REG_SZ)
245         return FALSE;
246
247     if(!check_version(gre_path, version_string))
248         return FALSE;
249
250     return load_xpcom(gre_path);
251 }
252
253 static BOOL load_wine_gecko(PRUnichar *gre_path)
254 {
255     HKEY hkey;
256     DWORD res;
257     BOOL ret;
258
259     static const WCHAR wszMshtmlKey[] = {
260         'S','o','f','t','w','a','r','e','\\','W','i','n','e',
261         '\\','M','S','H','T','M','L',0};
262
263     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
264     res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
265     if(res != ERROR_SUCCESS)
266         return FALSE;
267
268     ret = load_wine_gecko_v(gre_path, hkey, GECKO_VERSION, GECKO_VERSION_STRING);
269
270     RegCloseKey(hkey);
271     return ret;
272 }
273
274 static void set_lang(nsIPrefBranch *pref)
275 {
276     char langs[100];
277     DWORD res, size, type;
278     HKEY hkey;
279     nsresult nsres;
280
281     static const WCHAR international_keyW[] =
282         {'S','o','f','t','w','a','r','e',
283          '\\','M','i','c','r','o','s','o','f','t',
284          '\\','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
285          '\\','I','n','t','e','r','n','a','t','i','o','n','a','l',0};
286
287     res = RegOpenKeyW(HKEY_CURRENT_USER, international_keyW, &hkey);
288     if(res != ERROR_SUCCESS)
289         return;
290
291     size = sizeof(langs);
292     res = RegQueryValueExA(hkey, "AcceptLanguage", 0, &type, (LPBYTE)langs, &size);
293     RegCloseKey(hkey);
294     if(res != ERROR_SUCCESS || type != REG_SZ)
295         return;
296
297     TRACE("Setting lang %s\n", debugstr_a(langs));
298
299     nsres = nsIPrefBranch_SetCharPref(pref, "intl.accept_languages", langs);
300     if(NS_FAILED(nsres))
301         ERR("SetCharPref failed: %08x\n", nsres);
302 }
303
304 static void set_proxy(nsIPrefBranch *pref)
305 {
306     char proxy[512];
307     char * proxy_port;
308     int proxy_port_num;
309     DWORD enabled = 0, res, size, type;
310     HKEY hkey;
311     nsresult nsres;
312
313     static const WCHAR proxy_keyW[] =
314         {'S','o','f','t','w','a','r','e',
315          '\\','M','i','c','r','o','s','o','f','t',
316          '\\','W','i','n','d','o','w','s',
317          '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
318          '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
319
320     res = RegOpenKeyW(HKEY_CURRENT_USER, proxy_keyW, &hkey);
321     if(res != ERROR_SUCCESS)
322         return;
323
324     size = sizeof(enabled);
325     res = RegQueryValueExA(hkey, "ProxyEnable", 0, &type, (LPBYTE)&enabled, &size);
326     if(res != ERROR_SUCCESS || type != REG_DWORD || enabled == 0)
327     {
328         RegCloseKey(hkey);
329         return;
330     }
331
332     size = sizeof(proxy);
333     res = RegQueryValueExA(hkey, "ProxyServer", 0, &type, (LPBYTE)proxy, &size);
334     RegCloseKey(hkey);
335     if(res != ERROR_SUCCESS || type != REG_SZ)
336         return;
337
338     proxy_port = strchr(proxy, ':');
339     if (!proxy_port)
340         return;
341
342     *proxy_port = 0;
343     proxy_port_num = atoi(proxy_port + 1);
344     TRACE("Setting proxy to %s, port %d\n", debugstr_a(proxy), proxy_port_num);
345
346     nsres = nsIPrefBranch_SetIntPref(pref, "network.proxy.type", 1);
347     if(NS_FAILED(nsres))
348         ERR("SetIntPref network.proxy.type failed: %08x\n", nsres);
349     nsres = nsIPrefBranch_SetCharPref(pref, "network.proxy.http", proxy);
350     if(NS_FAILED(nsres))
351         ERR("SetCharPref network.proxy.http failed: %08x\n", nsres);
352     nsres = nsIPrefBranch_SetIntPref(pref, "network.proxy.http_port", proxy_port_num);
353     if(NS_FAILED(nsres))
354         ERR("SetIntPref network.proxy.http_port failed: %08x\n", nsres);
355     nsres = nsIPrefBranch_SetCharPref(pref, "network.proxy.ssl", proxy);
356     if(NS_FAILED(nsres))
357         ERR("SetCharPref network.proxy.ssl failed: %08x\n", nsres);
358     nsres = nsIPrefBranch_SetIntPref(pref, "network.proxy.ssl_port", proxy_port_num);
359     if(NS_FAILED(nsres))
360         ERR("SetIntPref network.proxy.ssl_port failed: %08x\n", nsres);
361 }
362
363 static void set_bool_pref(nsIPrefBranch *pref, const char *pref_name, BOOL val)
364 {
365     nsresult nsres;
366
367     nsres = nsIPrefBranch_SetBoolPref(pref, pref_name, val);
368     if(NS_FAILED(nsres))
369         ERR("Could not set pref %s\n", debugstr_a(pref_name));
370 }
371
372 static void set_profile(void)
373 {
374     nsIProfile *profile;
375     PRBool exists = FALSE;
376     nsresult nsres;
377
378     static const WCHAR wszMSHTML[] = {'M','S','H','T','M','L',0};
379
380     nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PROFILE_CONTRACTID,
381             &IID_nsIProfile, (void**)&profile);
382     if(NS_FAILED(nsres)) {
383         ERR("Could not get profile service: %08x\n", nsres);
384         return;
385     }
386
387     nsres = nsIProfile_ProfileExists(profile, wszMSHTML, &exists);
388     if(!exists) {
389         nsres = nsIProfile_CreateNewProfile(profile, wszMSHTML, NULL, NULL, FALSE);
390         if(NS_FAILED(nsres))
391             ERR("CreateNewProfile failed: %08x\n", nsres);
392     }
393
394     nsres = nsIProfile_SetCurrentProfile(profile, wszMSHTML);
395     if(NS_FAILED(nsres))
396         ERR("SetCurrentProfile failed: %08x\n", nsres);
397
398     nsIProfile_Release(profile);
399 }
400
401 static void set_preferences(void)
402 {
403     nsIPrefBranch *pref;
404     nsresult nsres;
405
406     nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
407             &IID_nsIPrefBranch, (void**)&pref);
408     if(NS_FAILED(nsres)) {
409         ERR("Could not get preference service: %08x\n", nsres);
410         return;
411     }
412
413     set_lang(pref);
414     set_proxy(pref);
415     set_bool_pref(pref, "security.warn_entering_secure", FALSE);
416     set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
417
418     nsIPrefBranch_Release(pref);
419 }
420
421 static BOOL init_xpcom(const PRUnichar *gre_path)
422 {
423     nsresult nsres;
424     nsIObserver *pStartNotif;
425     nsIComponentRegistrar *registrar = NULL;
426     nsAString path;
427     nsIFile *gre_dir;
428
429     nsAString_Init(&path, gre_path);
430     nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
431     nsAString_Finish(&path);
432     if(NS_FAILED(nsres)) {
433         ERR("NS_NewLocalFile failed: %08x\n", nsres);
434         FreeLibrary(hXPCOM);
435         return FALSE;
436     }
437
438     nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
439     if(NS_FAILED(nsres)) {
440         ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
441         FreeLibrary(hXPCOM);
442         return FALSE;
443     }
444
445     nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
446     if(NS_FAILED(nsres))
447         ERR("Could not get nsIComponentManager: %08x\n", nsres);
448
449     nsres = NS_GetComponentRegistrar(&registrar);
450     if(NS_SUCCEEDED(nsres)) {
451         nsres = nsIComponentRegistrar_AutoRegister(registrar, NULL);
452         if(NS_FAILED(nsres))
453             ERR("AutoRegister(NULL) failed: %08x\n", nsres);
454
455         nsres = nsIComponentRegistrar_AutoRegister(registrar, gre_dir);
456         if(NS_FAILED(nsres))
457             ERR("AutoRegister(gre_dir) failed: %08x\n", nsres);
458
459         init_nsio(pCompMgr, registrar);
460     }else {
461         ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
462     }
463
464     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
465             NULL, &IID_nsIObserver, (void**)&pStartNotif);
466     if(NS_SUCCEEDED(nsres)) {
467         nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
468         if(NS_FAILED(nsres))
469             ERR("Observe failed: %08x\n", nsres);
470
471         nsIObserver_Release(pStartNotif);
472     }else {
473         ERR("could not get appstartup-notifier: %08x\n", nsres);
474     }
475
476     set_profile();
477     set_preferences();
478
479     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_MEMORY_CONTRACTID,
480             NULL, &IID_nsIMemory, (void**)&nsmem);
481     if(NS_FAILED(nsres))
482         ERR("Could not get nsIMemory: %08x\n", nsres);
483
484     if(registrar) {
485         register_nsservice(registrar, pServMgr);
486         nsIComponentRegistrar_Release(registrar);
487     }
488
489     return TRUE;
490 }
491
492 static CRITICAL_SECTION cs_load_gecko;
493 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
494 {
495     0, 0, &cs_load_gecko,
496     { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
497       0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
498 };
499 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
500
501 BOOL load_gecko(BOOL silent)
502 {
503     PRUnichar gre_path[MAX_PATH];
504     BOOL ret = FALSE;
505
506     static DWORD loading_thread;
507
508     TRACE("()\n");
509
510     /* load_gecko may be called recursively */
511     if(loading_thread == GetCurrentThreadId())
512         return pCompMgr != NULL;
513
514     EnterCriticalSection(&cs_load_gecko);
515
516     if(!loading_thread) {
517         loading_thread = GetCurrentThreadId();
518
519         if(load_wine_gecko(gre_path)
520            || (install_wine_gecko(silent) && load_wine_gecko(gre_path)))
521             ret = init_xpcom(gre_path);
522         else
523            MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
524     }else {
525         ret = pCompMgr != NULL;
526     }
527
528     LeaveCriticalSection(&cs_load_gecko);
529
530     return ret;
531 }
532
533 void *nsalloc(size_t size)
534 {
535     return nsIMemory_Alloc(nsmem, size);
536 }
537
538 void nsfree(void *mem)
539 {
540     nsIMemory_Free(nsmem, mem);
541 }
542
543 void nsACString_Init(nsACString *str, const char *data)
544 {
545     NS_CStringContainerInit(str);
546     if(data)
547         nsACString_SetData(str, data);
548 }
549
550 void nsACString_SetData(nsACString *str, const char *data)
551 {
552     NS_CStringSetData(str, data, PR_UINT32_MAX);
553 }
554
555 PRUint32 nsACString_GetData(const nsACString *str, const char **data)
556 {
557     return NS_CStringGetData(str, data, NULL);
558 }
559
560 void nsACString_Finish(nsACString *str)
561 {
562     NS_CStringContainerFinish(str);
563 }
564
565 void nsAString_Init(nsAString *str, const PRUnichar *data)
566 {
567     NS_StringContainerInit(str);
568     if(data)
569         nsAString_SetData(str, data);
570 }
571
572 void nsAString_SetData(nsAString *str, const PRUnichar *data)
573 {
574     NS_StringSetData(str, data, PR_UINT32_MAX);
575 }
576
577 PRUint32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
578 {
579     return NS_StringGetData(str, data, NULL);
580 }
581
582 void nsAString_Finish(nsAString *str)
583 {
584     NS_StringContainerFinish(str);
585 }
586
587 nsIInputStream *create_nsstream(const char *data, PRInt32 data_len)
588 {
589     nsIStringInputStream *ret;
590     nsresult nsres;
591
592     if(!pCompMgr)
593         return NULL;
594
595     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
596             NS_STRINGSTREAM_CONTRACTID, NULL, &IID_nsIStringInputStream,
597             (void**)&ret);
598     if(NS_FAILED(nsres)) {
599         ERR("Could not get nsIStringInputStream\n");
600         return NULL;
601     }
602
603     nsres = nsIStringInputStream_SetData(ret, data, data_len);
604     if(NS_FAILED(nsres)) {
605         ERR("AdoptData failed: %08x\n", nsres);
606         nsIStringInputStream_Release(ret);
607         return NULL;
608     }
609
610     return (nsIInputStream*)ret;
611 }
612
613 nsICommandParams *create_nscommand_params(void)
614 {
615     nsICommandParams *ret = NULL;
616     nsresult nsres;
617
618     if(!pCompMgr)
619         return NULL;
620
621     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
622             NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
623             (void**)&ret);
624     if(NS_FAILED(nsres))
625         ERR("Could not get nsICommandParams\n");
626
627     return ret;
628 }
629
630 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
631 {
632     nsIInterfaceRequestor *iface_req;
633     nsresult nsres;
634
635     nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
636     if(NS_FAILED(nsres))
637         return nsres;
638
639     nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
640     nsIInterfaceRequestor_Release(iface_req);
641
642     return nsres;
643 }
644
645 static void nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
646 {
647     nsIDOMNodeList *node_list = NULL;
648     PRBool has_children = FALSE;
649     PRUint16 type;
650     nsresult nsres;
651
652     nsIDOMNode_HasChildNodes(nsnode, &has_children);
653
654     nsres = nsIDOMNode_GetNodeType(nsnode, &type);
655     if(NS_FAILED(nsres)) {
656         ERR("GetType failed: %08x\n", nsres);
657         return;
658     }
659
660     switch(type) {
661     case ELEMENT_NODE: {
662         nsIDOMElement *nselem;
663         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMElement, (void**)&nselem);
664         nsIContentSerializer_AppendElementStart(serializer, nselem, nselem, str);
665         nsIDOMElement_Release(nselem);
666         break;
667     }
668     case TEXT_NODE: {
669         nsIDOMText *nstext;
670         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMText, (void**)&nstext);
671         nsIContentSerializer_AppendText(serializer, nstext, 0, -1, str);
672         nsIDOMText_Release(nstext);
673         break;
674     }
675     case COMMENT_NODE: {
676         nsIDOMComment *nscomment;
677         nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMComment, (void**)&nscomment);
678         nsres = nsIContentSerializer_AppendComment(serializer, nscomment, 0, -1, str);
679         break;
680     }
681     case DOCUMENT_NODE: {
682         nsIDOMDocument *nsdoc;
683         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMDocument, (void**)&nsdoc);
684         nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
685         nsIDOMDocument_Release(nsdoc);
686         break;
687     }
688     case DOCUMENT_FRAGMENT_NODE:
689         break;
690     default:
691         FIXME("Unhandled type %u\n", type);
692     }
693
694     if(has_children) {
695         PRUint32 child_cnt, i;
696         nsIDOMNode *child_node;
697
698         nsIDOMNode_GetChildNodes(nsnode, &node_list);
699         nsIDOMNodeList_GetLength(node_list, &child_cnt);
700
701         for(i=0; i<child_cnt; i++) {
702             nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
703             if(NS_SUCCEEDED(nsres)) {
704                 nsnode_to_nsstring_rec(serializer, child_node, str);
705                 nsIDOMNode_Release(child_node);
706             }else {
707                 ERR("Item failed: %08x\n", nsres);
708             }
709         }
710
711         nsIDOMNodeList_Release(node_list);
712     }
713
714     if(type == ELEMENT_NODE) {
715         nsIDOMElement *nselem;
716         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMElement, (void**)&nselem);
717         nsIContentSerializer_AppendElementEnd(serializer, nselem, str);
718         nsIDOMElement_Release(nselem);
719     }
720 }
721
722 void nsnode_to_nsstring(nsIDOMNode *nsdoc, nsAString *str)
723 {
724     nsIContentSerializer *serializer;
725     nsIDOMNode *nsnode;
726     nsresult nsres;
727
728     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
729             NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
730             (void**)&serializer);
731     if(NS_FAILED(nsres)) {
732         ERR("Could not get nsIContentSerializer: %08x\n", nsres);
733         return;
734     }
735
736     nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
737     if(NS_FAILED(nsres))
738         ERR("Init failed: %08x\n", nsres);
739
740     nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
741     nsnode_to_nsstring_rec(serializer, nsnode, str);
742     nsIDOMNode_Release(nsnode);
743
744     nsres = nsIContentSerializer_Flush(serializer, str);
745     if(NS_FAILED(nsres))
746         ERR("Flush failed: %08x\n", nsres);
747
748     nsIContentSerializer_Release(serializer);
749 }
750
751 void get_editor_controller(NSContainer *This)
752 {
753     nsIEditingSession *editing_session = NULL;
754     nsIControllerContext *ctrlctx;
755     nsresult nsres;
756
757     if(This->editor) {
758         nsIEditor_Release(This->editor);
759         This->editor = NULL;
760     }
761
762     if(This->editor_controller) {
763         nsIController_Release(This->editor_controller);
764         This->editor_controller = NULL;
765     }
766
767     nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
768             (void**)&editing_session);
769     if(NS_FAILED(nsres)) {
770         ERR("Could not get nsIEditingSession: %08x\n", nsres);
771         return;
772     }
773
774     nsres = nsIEditingSession_GetEditorForWindow(editing_session,
775             This->doc->window->nswindow, &This->editor);
776     nsIEditingSession_Release(editing_session);
777     if(NS_FAILED(nsres)) {
778         ERR("Could not get editor: %08x\n", nsres);
779         return;
780     }
781
782     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
783             NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
784     if(NS_SUCCEEDED(nsres)) {
785         nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
786         if(NS_FAILED(nsres))
787             ERR("SetCommandContext failed: %08x\n", nsres);
788         nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
789                 (void**)&This->editor_controller);
790         nsIControllerContext_Release(ctrlctx);
791         if(NS_FAILED(nsres))
792             ERR("Could not get nsIController interface: %08x\n", nsres);
793     }else {
794         ERR("Could not create edit controller: %08x\n", nsres);
795     }
796 }
797
798 void set_ns_editmode(NSContainer *This)
799 {
800     nsIEditingSession *editing_session = NULL;
801     nsIURIContentListener *listener = NULL;
802     nsIDOMWindow *dom_window = NULL;
803     nsresult nsres;
804
805     nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
806             (void**)&editing_session);
807     if(NS_FAILED(nsres)) {
808         ERR("Could not get nsIEditingSession: %08x\n", nsres);
809         return;
810     }
811
812     nsres = nsIWebBrowser_GetContentDOMWindow(This->webbrowser, &dom_window);
813     if(NS_FAILED(nsres)) {
814         ERR("Could not get content DOM window: %08x\n", nsres);
815         nsIEditingSession_Release(editing_session);
816         return;
817     }
818
819     nsres = nsIEditingSession_MakeWindowEditable(editing_session, dom_window,
820             NULL, FALSE, TRUE, TRUE);
821     nsIEditingSession_Release(editing_session);
822     nsIDOMWindow_Release(dom_window);
823     if(NS_FAILED(nsres)) {
824         ERR("MakeWindowEditable failed: %08x\n", nsres);
825         return;
826     }
827
828     /* MakeWindowEditable changes WebBrowser's parent URI content listener.
829      * It seams to be a bug in Gecko. To workaround it we set our content
830      * listener again and Gecko's one as its parent.
831      */
832     nsIWebBrowser_GetParentURIContentListener(This->webbrowser, &listener);
833     nsIURIContentListener_SetParentContentListener(NSURICL(This), listener);
834     nsIURIContentListener_Release(listener);
835     nsIWebBrowser_SetParentURIContentListener(This->webbrowser, NSURICL(This));
836 }
837
838 void update_nsdocument(HTMLDocument *doc)
839 {
840     nsIDOMHTMLDocument *nsdoc;
841     nsIDOMDocument *nsdomdoc;
842     nsresult nsres;
843
844     if(!doc->nscontainer || !doc->nscontainer->navigation)
845         return;
846
847     nsres = nsIWebNavigation_GetDocument(doc->nscontainer->navigation, &nsdomdoc);
848     if(NS_FAILED(nsres) || !nsdomdoc) {
849         ERR("GetDocument failed: %08x\n", nsres);
850         return;
851     }
852
853     nsres = nsIDOMDocument_QueryInterface(nsdomdoc, &IID_nsIDOMHTMLDocument, (void**)&nsdoc);
854     nsIDOMDocument_Release(nsdomdoc);
855     if(NS_FAILED(nsres)) {
856         ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
857         return;
858     }
859
860     if(nsdoc == doc->nsdoc) {
861         nsIDOMHTMLDocument_Release(nsdoc);
862         return;
863     }
864
865     if(doc->nsdoc)
866         nsIDOMHTMLDocument_Release(doc->nsdoc);
867
868     doc->nsdoc = nsdoc;
869
870     if(nsdoc)
871         set_mutation_observer(doc->nscontainer, nsdoc);
872 }
873
874 void close_gecko(void)
875 {
876     TRACE("()\n");
877
878     release_nsio();
879
880     if(pCompMgr)
881         nsIComponentManager_Release(pCompMgr);
882
883     if(pServMgr)
884         nsIServiceManager_Release(pServMgr);
885
886     if(nsmem)
887         nsIMemory_Release(nsmem);
888
889     if(hXPCOM)
890         FreeLibrary(hXPCOM);
891 }
892
893 /**********************************************************
894  *      nsIWebBrowserChrome interface
895  */
896
897 #define NSWBCHROME_THIS(iface) DEFINE_THIS(NSContainer, WebBrowserChrome, iface)
898
899 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
900         nsIIDRef riid, nsQIResult result)
901 {
902     NSContainer *This = NSWBCHROME_THIS(iface);
903
904     *result = NULL;
905     if(IsEqualGUID(&IID_nsISupports, riid)) {
906         TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
907         *result = NSWBCHROME(This);
908     }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
909         TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
910         *result = NSWBCHROME(This);
911     }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
912         TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
913         *result = NSCML(This);
914     }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
915         TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
916         *result = NSURICL(This);
917     }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
918         TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
919         *result = NSEMBWNDS(This);
920     }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
921         TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
922         *result = NSTOOLTIP(This);
923     }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
924         TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
925         *result = NSIFACEREQ(This);
926     }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
927         TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
928         *result = NSWEAKREF(This);
929     }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
930         TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
931         *result = NSSUPWEAKREF(This);
932     }
933
934     if(*result) {
935         nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
936         return NS_OK;
937     }
938
939     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
940     return NS_NOINTERFACE;
941 }
942
943 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
944 {
945     NSContainer *This = NSWBCHROME_THIS(iface);
946     LONG ref = InterlockedIncrement(&This->ref);
947
948     TRACE("(%p) ref=%d\n", This, ref);
949
950     return ref;
951 }
952
953 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
954 {
955     NSContainer *This = NSWBCHROME_THIS(iface);
956     LONG ref = InterlockedDecrement(&This->ref);
957
958     TRACE("(%p) ref=%d\n", This, ref);
959
960     if(!ref) {
961         heap_free(This->event_vector);
962         if(This->parent)
963             nsIWebBrowserChrome_Release(NSWBCHROME(This->parent));
964         heap_free(This);
965     }
966
967     return ref;
968 }
969
970 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
971         PRUint32 statusType, const PRUnichar *status)
972 {
973     NSContainer *This = NSWBCHROME_THIS(iface);
974
975     TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
976
977     /* FIXME: This hack should be removed when we'll load all pages by URLMoniker */
978     if(This->doc)
979         update_nsdocument(This->doc);
980
981     return NS_OK;
982 }
983
984 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
985         nsIWebBrowser **aWebBrowser)
986 {
987     NSContainer *This = NSWBCHROME_THIS(iface);
988
989     TRACE("(%p)->(%p)\n", This, aWebBrowser);
990
991     if(!aWebBrowser)
992         return NS_ERROR_INVALID_ARG;
993
994     if(This->webbrowser)
995         nsIWebBrowser_AddRef(This->webbrowser);
996     *aWebBrowser = This->webbrowser;
997     return S_OK;
998 }
999
1000 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1001         nsIWebBrowser *aWebBrowser)
1002 {
1003     NSContainer *This = NSWBCHROME_THIS(iface);
1004
1005     TRACE("(%p)->(%p)\n", This, aWebBrowser);
1006
1007     if(aWebBrowser != This->webbrowser)
1008         ERR("Wrong nsWebBrowser!\n");
1009
1010     return NS_OK;
1011 }
1012
1013 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1014         PRUint32 *aChromeFlags)
1015 {
1016     NSContainer *This = NSWBCHROME_THIS(iface);
1017     WARN("(%p)->(%p)\n", This, aChromeFlags);
1018     return NS_ERROR_NOT_IMPLEMENTED;
1019 }
1020
1021 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1022         PRUint32 aChromeFlags)
1023 {
1024     NSContainer *This = NSWBCHROME_THIS(iface);
1025     WARN("(%p)->(%08x)\n", This, aChromeFlags);
1026     return NS_ERROR_NOT_IMPLEMENTED;
1027 }
1028
1029 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1030 {
1031     NSContainer *This = NSWBCHROME_THIS(iface);
1032     TRACE("(%p)\n", This);
1033     return NS_ERROR_NOT_IMPLEMENTED;
1034 }
1035
1036 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1037         PRInt32 aCX, PRInt32 aCY)
1038 {
1039     NSContainer *This = NSWBCHROME_THIS(iface);
1040     WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1041     return NS_ERROR_NOT_IMPLEMENTED;
1042 }
1043
1044 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1045 {
1046     NSContainer *This = NSWBCHROME_THIS(iface);
1047     WARN("(%p)\n", This);
1048     return NS_ERROR_NOT_IMPLEMENTED;
1049 }
1050
1051 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
1052 {
1053     NSContainer *This = NSWBCHROME_THIS(iface);
1054     WARN("(%p)->(%p)\n", This, _retval);
1055     return NS_ERROR_NOT_IMPLEMENTED;
1056 }
1057
1058 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1059         nsresult aStatus)
1060 {
1061     NSContainer *This = NSWBCHROME_THIS(iface);
1062     WARN("(%p)->(%08x)\n", This, aStatus);
1063     return NS_ERROR_NOT_IMPLEMENTED;
1064 }
1065
1066 #undef NSWBCHROME_THIS
1067
1068 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1069     nsWebBrowserChrome_QueryInterface,
1070     nsWebBrowserChrome_AddRef,
1071     nsWebBrowserChrome_Release,
1072     nsWebBrowserChrome_SetStatus,
1073     nsWebBrowserChrome_GetWebBrowser,
1074     nsWebBrowserChrome_SetWebBrowser,
1075     nsWebBrowserChrome_GetChromeFlags,
1076     nsWebBrowserChrome_SetChromeFlags,
1077     nsWebBrowserChrome_DestroyBrowserWindow,
1078     nsWebBrowserChrome_SizeBrowserTo,
1079     nsWebBrowserChrome_ShowAsModal,
1080     nsWebBrowserChrome_IsWindowModal,
1081     nsWebBrowserChrome_ExitModalEventLoop
1082 };
1083
1084 /**********************************************************
1085  *      nsIContextMenuListener interface
1086  */
1087
1088 #define NSCML_THIS(iface) DEFINE_THIS(NSContainer, ContextMenuListener, iface)
1089
1090 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1091         nsIIDRef riid, nsQIResult result)
1092 {
1093     NSContainer *This = NSCML_THIS(iface);
1094     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1095 }
1096
1097 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1098 {
1099     NSContainer *This = NSCML_THIS(iface);
1100     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1101 }
1102
1103 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1104 {
1105     NSContainer *This = NSCML_THIS(iface);
1106     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1107 }
1108
1109 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1110         PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1111 {
1112     NSContainer *This = NSCML_THIS(iface);
1113     nsIDOMMouseEvent *event;
1114     POINT pt;
1115     DWORD dwID = CONTEXT_MENU_DEFAULT;
1116     nsresult nsres;
1117
1118     TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1119
1120     nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
1121     if(NS_FAILED(nsres)) {
1122         ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
1123         return nsres;
1124     }
1125
1126     nsIDOMMouseEvent_GetScreenX(event, &pt.x);
1127     nsIDOMMouseEvent_GetScreenY(event, &pt.y);
1128     nsIDOMMouseEvent_Release(event);
1129
1130     switch(aContextFlags) {
1131     case CONTEXT_NONE:
1132     case CONTEXT_DOCUMENT:
1133     case CONTEXT_TEXT:
1134         dwID = CONTEXT_MENU_DEFAULT;
1135         break;
1136     case CONTEXT_IMAGE:
1137     case CONTEXT_IMAGE|CONTEXT_LINK:
1138         dwID = CONTEXT_MENU_IMAGE;
1139         break;
1140     case CONTEXT_LINK:
1141         dwID = CONTEXT_MENU_ANCHOR;
1142         break;
1143     case CONTEXT_INPUT:
1144         dwID = CONTEXT_MENU_CONTROL;
1145         break;
1146     default:
1147         FIXME("aContextFlags=%08x\n", aContextFlags);
1148     };
1149
1150     show_context_menu(This->doc, dwID, &pt, (IDispatch*)HTMLDOMNODE(get_node(This->doc, aNode, TRUE)));
1151
1152     return NS_OK;
1153 }
1154
1155 #undef NSCML_THIS
1156
1157 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1158     nsContextMenuListener_QueryInterface,
1159     nsContextMenuListener_AddRef,
1160     nsContextMenuListener_Release,
1161     nsContextMenuListener_OnShowContextMenu
1162 };
1163
1164 /**********************************************************
1165  *      nsIURIContentListener interface
1166  */
1167
1168 #define NSURICL_THIS(iface) DEFINE_THIS(NSContainer, URIContentListener, iface)
1169
1170 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1171         nsIIDRef riid, nsQIResult result)
1172 {
1173     NSContainer *This = NSURICL_THIS(iface);
1174     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1175 }
1176
1177 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1178 {
1179     NSContainer *This = NSURICL_THIS(iface);
1180     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1181 }
1182
1183 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1184 {
1185     NSContainer *This = NSURICL_THIS(iface);
1186     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1187 }
1188
1189 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1190                                                           nsIURI *aURI, PRBool *_retval)
1191 {
1192     NSContainer *This = NSURICL_THIS(iface);
1193     nsIWineURI *wine_uri;
1194     nsACString spec_str;
1195     const char *spec;
1196     nsresult nsres;
1197
1198     nsACString_Init(&spec_str, NULL);
1199     nsIURI_GetSpec(aURI, &spec_str);
1200     nsACString_GetData(&spec_str, &spec);
1201
1202     TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1203
1204     nsACString_Finish(&spec_str);
1205
1206     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
1207     if(NS_FAILED(nsres)) {
1208         WARN("Could not get nsIWineURI interface: %08x\n", nsres);
1209         return NS_ERROR_NOT_IMPLEMENTED;
1210     }
1211
1212     nsIWineURI_SetNSContainer(wine_uri, This);
1213     nsIWineURI_SetIsDocumentURI(wine_uri, TRUE);
1214
1215     if(This->bscallback) {
1216         IMoniker *mon = get_channelbsc_mon(This->bscallback);
1217
1218         if(mon) {
1219             LPWSTR wine_url;
1220             HRESULT hres;
1221
1222             hres = IMoniker_GetDisplayName(mon, NULL, 0, &wine_url);
1223             if(SUCCEEDED(hres)) {
1224                 nsIWineURI_SetWineURL(wine_uri, wine_url);
1225                 CoTaskMemFree(wine_url);
1226             }else {
1227                 WARN("GetDisplayName failed: %08x\n", hres);
1228             }
1229
1230             IMoniker_Release(mon);
1231         }
1232     }
1233
1234     nsIWineURI_Release(wine_uri);
1235
1236     *_retval = FALSE;
1237     return This->content_listener
1238         ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1239         : NS_OK;
1240 }
1241
1242 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1243         const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
1244         nsIStreamListener **aContentHandler, PRBool *_retval)
1245 {
1246     NSContainer *This = NSURICL_THIS(iface);
1247
1248     TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1249             aRequest, aContentHandler, _retval);
1250
1251     return This->content_listener
1252         ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1253                   aIsContentPreferred, aRequest, aContentHandler, _retval)
1254         : NS_ERROR_NOT_IMPLEMENTED;
1255 }
1256
1257 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1258         const char *aContentType, char **aDesiredContentType, PRBool *_retval)
1259 {
1260     NSContainer *This = NSURICL_THIS(iface);
1261
1262     TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1263
1264     /* FIXME: Should we do something here? */
1265     *_retval = TRUE; 
1266
1267     return This->content_listener
1268         ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1269                   aDesiredContentType, _retval)
1270         : NS_OK;
1271 }
1272
1273 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1274         const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
1275         PRBool *_retval)
1276 {
1277     NSContainer *This = NSURICL_THIS(iface);
1278
1279     TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1280             aDesiredContentType, _retval);
1281
1282     return This->content_listener
1283         ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1284                 aIsContentPreferred, aDesiredContentType, _retval)
1285         : NS_ERROR_NOT_IMPLEMENTED;
1286 }
1287
1288 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1289         nsISupports **aLoadCookie)
1290 {
1291     NSContainer *This = NSURICL_THIS(iface);
1292
1293     WARN("(%p)->(%p)\n", This, aLoadCookie);
1294
1295     return This->content_listener
1296         ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1297         : NS_ERROR_NOT_IMPLEMENTED;
1298 }
1299
1300 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1301         nsISupports *aLoadCookie)
1302 {
1303     NSContainer *This = NSURICL_THIS(iface);
1304
1305     WARN("(%p)->(%p)\n", This, aLoadCookie);
1306
1307     return This->content_listener
1308         ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1309         : NS_ERROR_NOT_IMPLEMENTED;
1310 }
1311
1312 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1313         nsIURIContentListener **aParentContentListener)
1314 {
1315     NSContainer *This = NSURICL_THIS(iface);
1316
1317     TRACE("(%p)->(%p)\n", This, aParentContentListener);
1318
1319     if(This->content_listener)
1320         nsIURIContentListener_AddRef(This->content_listener);
1321
1322     *aParentContentListener = This->content_listener;
1323     return NS_OK;
1324 }
1325
1326 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1327         nsIURIContentListener *aParentContentListener)
1328 {
1329     NSContainer *This = NSURICL_THIS(iface);
1330
1331     TRACE("(%p)->(%p)\n", This, aParentContentListener);
1332
1333     if(aParentContentListener == NSURICL(This))
1334         return NS_OK;
1335
1336     if(This->content_listener)
1337         nsIURIContentListener_Release(This->content_listener);
1338
1339     This->content_listener = aParentContentListener;
1340     if(This->content_listener)
1341         nsIURIContentListener_AddRef(This->content_listener);
1342
1343     return NS_OK;
1344 }
1345
1346 #undef NSURICL_THIS
1347
1348 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1349     nsURIContentListener_QueryInterface,
1350     nsURIContentListener_AddRef,
1351     nsURIContentListener_Release,
1352     nsURIContentListener_OnStartURIOpen,
1353     nsURIContentListener_DoContent,
1354     nsURIContentListener_IsPreferred,
1355     nsURIContentListener_CanHandleContent,
1356     nsURIContentListener_GetLoadCookie,
1357     nsURIContentListener_SetLoadCookie,
1358     nsURIContentListener_GetParentContentListener,
1359     nsURIContentListener_SetParentContentListener
1360 };
1361
1362 /**********************************************************
1363  *      nsIEmbeddinSiteWindow interface
1364  */
1365
1366 #define NSEMBWNDS_THIS(iface) DEFINE_THIS(NSContainer, EmbeddingSiteWindow, iface)
1367
1368 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1369         nsIIDRef riid, nsQIResult result)
1370 {
1371     NSContainer *This = NSEMBWNDS_THIS(iface);
1372     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1373 }
1374
1375 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1376 {
1377     NSContainer *This = NSEMBWNDS_THIS(iface);
1378     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1379 }
1380
1381 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1382 {
1383     NSContainer *This = NSEMBWNDS_THIS(iface);
1384     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1385 }
1386
1387 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1388         PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
1389 {
1390     NSContainer *This = NSEMBWNDS_THIS(iface);
1391     WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1392     return NS_ERROR_NOT_IMPLEMENTED;
1393 }
1394
1395 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1396         PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
1397 {
1398     NSContainer *This = NSEMBWNDS_THIS(iface);
1399     WARN("(%p)->(%08x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1400     return NS_ERROR_NOT_IMPLEMENTED;
1401 }
1402
1403 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1404 {
1405     NSContainer *This = NSEMBWNDS_THIS(iface);
1406
1407     TRACE("(%p)\n", This);
1408
1409     if(This->reset_focus)
1410         PostMessageW(This->hwnd, WM_RESETFOCUS_HACK, 0, 0);
1411
1412     return nsIBaseWindow_SetFocus(This->window);
1413 }
1414
1415 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1416         PRBool *aVisibility)
1417 {
1418     NSContainer *This = NSEMBWNDS_THIS(iface);
1419
1420     TRACE("(%p)->(%p)\n", This, aVisibility);
1421
1422     *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1423     return NS_OK;
1424 }
1425
1426 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1427         PRBool aVisibility)
1428 {
1429     NSContainer *This = NSEMBWNDS_THIS(iface);
1430
1431     TRACE("(%p)->(%x)\n", This, aVisibility);
1432
1433     return NS_OK;
1434 }
1435
1436 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1437         PRUnichar **aTitle)
1438 {
1439     NSContainer *This = NSEMBWNDS_THIS(iface);
1440     WARN("(%p)->(%p)\n", This, aTitle);
1441     return NS_ERROR_NOT_IMPLEMENTED;
1442 }
1443
1444 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1445         const PRUnichar *aTitle)
1446 {
1447     NSContainer *This = NSEMBWNDS_THIS(iface);
1448     WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1449     return NS_ERROR_NOT_IMPLEMENTED;
1450 }
1451
1452 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1453         void **aSiteWindow)
1454 {
1455     NSContainer *This = NSEMBWNDS_THIS(iface);
1456
1457     TRACE("(%p)->(%p)\n", This, aSiteWindow);
1458
1459     *aSiteWindow = This->hwnd;
1460     return NS_OK;
1461 }
1462
1463 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1464     nsEmbeddingSiteWindow_QueryInterface,
1465     nsEmbeddingSiteWindow_AddRef,
1466     nsEmbeddingSiteWindow_Release,
1467     nsEmbeddingSiteWindow_SetDimensions,
1468     nsEmbeddingSiteWindow_GetDimensions,
1469     nsEmbeddingSiteWindow_SetFocus,
1470     nsEmbeddingSiteWindow_GetVisibility,
1471     nsEmbeddingSiteWindow_SetVisibility,
1472     nsEmbeddingSiteWindow_GetTitle,
1473     nsEmbeddingSiteWindow_SetTitle,
1474     nsEmbeddingSiteWindow_GetSiteWindow
1475 };
1476
1477 #define NSTOOLTIP_THIS(iface) DEFINE_THIS(NSContainer, TooltipListener, iface)
1478
1479 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1480                                                        nsQIResult result)
1481 {
1482     NSContainer *This = NSTOOLTIP_THIS(iface);
1483     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1484 }
1485
1486 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1487 {
1488     NSContainer *This = NSTOOLTIP_THIS(iface);
1489     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1490 }
1491
1492 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1493 {
1494     NSContainer *This = NSTOOLTIP_THIS(iface);
1495     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1496 }
1497
1498 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1499         PRInt32 aXCoord, PRInt32 aYCoord, const PRUnichar *aTipText)
1500 {
1501     NSContainer *This = NSTOOLTIP_THIS(iface);
1502
1503     if (This->doc)
1504         show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1505
1506     return NS_OK;
1507 }
1508
1509 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1510 {
1511     NSContainer *This = NSTOOLTIP_THIS(iface);
1512
1513     if (This->doc)
1514         hide_tooltip(This->doc);
1515
1516     return NS_OK;
1517 }
1518
1519 #undef NSTOOLTIM_THIS
1520
1521 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1522     nsTooltipListener_QueryInterface,
1523     nsTooltipListener_AddRef,
1524     nsTooltipListener_Release,
1525     nsTooltipListener_OnShowTooltip,
1526     nsTooltipListener_OnHideTooltip
1527 };
1528
1529 #define NSIFACEREQ_THIS(iface) DEFINE_THIS(NSContainer, InterfaceRequestor, iface)
1530
1531 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1532                                                           nsIIDRef riid, nsQIResult result)
1533 {
1534     NSContainer *This = NSIFACEREQ_THIS(iface);
1535     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1536 }
1537
1538 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1539 {
1540     NSContainer *This = NSIFACEREQ_THIS(iface);
1541     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1542 }
1543
1544 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1545 {
1546     NSContainer *This = NSIFACEREQ_THIS(iface);
1547     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1548 }
1549
1550 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1551                                                         nsIIDRef riid, nsQIResult result)
1552 {
1553     NSContainer *This = NSIFACEREQ_THIS(iface);
1554
1555     if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1556         TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1557         return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1558     }
1559
1560     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1561 }
1562
1563 #undef NSIFACEREQ_THIS
1564
1565 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1566     nsInterfaceRequestor_QueryInterface,
1567     nsInterfaceRequestor_AddRef,
1568     nsInterfaceRequestor_Release,
1569     nsInterfaceRequestor_GetInterface
1570 };
1571
1572 #define NSWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, WeakReference, iface)
1573
1574 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1575         nsIIDRef riid, nsQIResult result)
1576 {
1577     NSContainer *This = NSWEAKREF_THIS(iface);
1578     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1579 }
1580
1581 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1582 {
1583     NSContainer *This = NSWEAKREF_THIS(iface);
1584     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1585 }
1586
1587 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1588 {
1589     NSContainer *This = NSWEAKREF_THIS(iface);
1590     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1591 }
1592
1593 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1594         const nsIID *riid, void **result)
1595 {
1596     NSContainer *This = NSWEAKREF_THIS(iface);
1597     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1598 }
1599
1600 #undef NSWEAKREF_THIS
1601
1602 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1603     nsWeakReference_QueryInterface,
1604     nsWeakReference_AddRef,
1605     nsWeakReference_Release,
1606     nsWeakReference_QueryReferent
1607 };
1608
1609 #define NSSUPWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, SupportsWeakReference, iface)
1610
1611 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1612         nsIIDRef riid, nsQIResult result)
1613 {
1614     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1615     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1616 }
1617
1618 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1619 {
1620     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1621     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1622 }
1623
1624 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1625 {
1626     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1627     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1628 }
1629
1630 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1631         nsIWeakReference **_retval)
1632 {
1633     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1634
1635     TRACE("(%p)->(%p)\n", This, _retval);
1636
1637     nsIWeakReference_AddRef(NSWEAKREF(This));
1638     *_retval = NSWEAKREF(This);
1639     return NS_OK;
1640 }
1641
1642 #undef NSWEAKREF_THIS
1643
1644 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1645     nsSupportsWeakReference_QueryInterface,
1646     nsSupportsWeakReference_AddRef,
1647     nsSupportsWeakReference_Release,
1648     nsSupportsWeakReference_GetWeakReference
1649 };
1650
1651
1652 NSContainer *NSContainer_Create(HTMLDocument *doc, NSContainer *parent)
1653 {
1654     nsIWebBrowserSetup *wbsetup;
1655     nsIScrollable *scrollable;
1656     NSContainer *ret;
1657     nsresult nsres;
1658
1659     if(!load_gecko(FALSE))
1660         return NULL;
1661
1662     ret = heap_alloc_zero(sizeof(NSContainer));
1663
1664     ret->lpWebBrowserChromeVtbl      = &nsWebBrowserChromeVtbl;
1665     ret->lpContextMenuListenerVtbl   = &nsContextMenuListenerVtbl;
1666     ret->lpURIContentListenerVtbl    = &nsURIContentListenerVtbl;
1667     ret->lpEmbeddingSiteWindowVtbl   = &nsEmbeddingSiteWindowVtbl;
1668     ret->lpTooltipListenerVtbl       = &nsTooltipListenerVtbl;
1669     ret->lpInterfaceRequestorVtbl    = &nsInterfaceRequestorVtbl;
1670     ret->lpWeakReferenceVtbl         = &nsWeakReferenceVtbl;
1671     ret->lpSupportsWeakReferenceVtbl = &nsSupportsWeakReferenceVtbl;
1672
1673     ret->doc = doc;
1674     ret->ref = 1;
1675     init_mutation(ret);
1676
1677     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1678             NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
1679     if(NS_FAILED(nsres)) {
1680         ERR("Creating WebBrowser failed: %08x\n", nsres);
1681         heap_free(ret);
1682         return NULL;
1683     }
1684
1685     if(parent)
1686         nsIWebBrowserChrome_AddRef(NSWBCHROME(parent));
1687     ret->parent = parent;
1688
1689     nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, NSWBCHROME(ret));
1690     if(NS_FAILED(nsres))
1691         ERR("SetContainerWindow failed: %08x\n", nsres);
1692
1693     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
1694             (void**)&ret->window);
1695     if(NS_FAILED(nsres))
1696         ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1697
1698     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
1699                                          (void**)&wbsetup);
1700     if(NS_SUCCEEDED(nsres)) {
1701         nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
1702         nsIWebBrowserSetup_Release(wbsetup);
1703         if(NS_FAILED(nsres))
1704             ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
1705     }else {
1706         ERR("Could not get nsIWebBrowserSetup interface\n");
1707     }
1708
1709     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
1710             (void**)&ret->navigation);
1711     if(NS_FAILED(nsres))
1712         ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1713
1714     nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
1715             (void**)&ret->focus);
1716     if(NS_FAILED(nsres))
1717         ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
1718
1719     if(!nscontainer_class)
1720         register_nscontainer_class();
1721
1722     ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1723             WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1724             GetDesktopWindow(), NULL, hInst, ret);
1725
1726     nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
1727     if(NS_SUCCEEDED(nsres)) {
1728         nsres = nsIBaseWindow_Create(ret->window);
1729         if(NS_FAILED(nsres))
1730             WARN("Creating window failed: %08x\n", nsres);
1731
1732         nsIBaseWindow_SetVisibility(ret->window, FALSE);
1733         nsIBaseWindow_SetEnabled(ret->window, FALSE);
1734     }else {
1735         ERR("InitWindow failed: %08x\n", nsres);
1736     }
1737
1738     nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser, NSURICL(ret));
1739     if(NS_FAILED(nsres))
1740         ERR("SetParentURIContentListener failed: %08x\n", nsres);
1741
1742     init_nsevents(ret);
1743
1744     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
1745     if(NS_SUCCEEDED(nsres)) {
1746         nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1747                 ScrollOrientation_Y, Scrollbar_Always);
1748         if(NS_FAILED(nsres))
1749             ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
1750
1751         nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1752                 ScrollOrientation_X, Scrollbar_Auto);
1753         if(NS_FAILED(nsres))
1754             ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
1755
1756         nsIScrollable_Release(scrollable);
1757     }else {
1758         ERR("Could not get nsIScrollable: %08x\n", nsres);
1759     }
1760
1761     return ret;
1762 }
1763
1764 void NSContainer_Release(NSContainer *This)
1765 {
1766     TRACE("(%p)\n", This);
1767
1768     This->doc = NULL;
1769
1770     ShowWindow(This->hwnd, SW_HIDE);
1771     SetParent(This->hwnd, NULL);
1772
1773     nsIBaseWindow_SetVisibility(This->window, FALSE);
1774     nsIBaseWindow_Destroy(This->window);
1775
1776     nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1777
1778     nsIWebBrowser_Release(This->webbrowser);
1779     This->webbrowser = NULL;
1780
1781     nsIWebNavigation_Release(This->navigation);
1782     This->navigation = NULL;
1783
1784     nsIBaseWindow_Release(This->window);
1785     This->window = NULL;
1786
1787     nsIWebBrowserFocus_Release(This->focus);
1788     This->focus = NULL;
1789
1790     if(This->editor_controller) {
1791         nsIController_Release(This->editor_controller);
1792         This->editor_controller = NULL;
1793     }
1794
1795     if(This->editor) {
1796         nsIEditor_Release(This->editor);
1797         This->editor = NULL;
1798     }
1799
1800     if(This->content_listener) {
1801         nsIURIContentListener_Release(This->content_listener);
1802         This->content_listener = NULL;
1803     }
1804
1805     if(This->hwnd) {
1806         DestroyWindow(This->hwnd);
1807         This->hwnd = NULL;
1808     }
1809
1810     nsIWebBrowserChrome_Release(NSWBCHROME(This));
1811 }