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