mshtml: Sign-compare warnings fix.
[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 = (NSContainer*)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     nsIPrefBranch *pref;
375     nsIProfile *profile;
376     PRBool exists = FALSE;
377     nsresult nsres;
378
379     static const WCHAR wszMSHTML[] = {'M','S','H','T','M','L',0};
380
381     nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PROFILE_CONTRACTID,
382             &IID_nsIProfile, (void**)&profile);
383     if(NS_FAILED(nsres)) {
384         ERR("Could not get profile service: %08x\n", nsres);
385         return;
386     }
387
388     nsres = nsIProfile_ProfileExists(profile, wszMSHTML, &exists);
389     if(!exists) {
390         nsres = nsIProfile_CreateNewProfile(profile, wszMSHTML, NULL, NULL, FALSE);
391         if(NS_FAILED(nsres))
392             ERR("CreateNewProfile failed: %08x\n", nsres);
393     }
394
395     nsres = nsIProfile_SetCurrentProfile(profile, wszMSHTML);
396     if(NS_FAILED(nsres))
397         ERR("SetCurrentProfile failed: %08x\n", nsres);
398
399     nsIProfile_Release(profile);
400
401     nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
402             &IID_nsIPrefBranch, (void**)&pref);
403     if(NS_FAILED(nsres)) {
404         ERR("Could not get preference service: %08x\n", nsres);
405         return;
406     }
407
408     set_lang(pref);
409     set_proxy(pref);
410     set_bool_pref(pref, "security.warn_entering_secure", FALSE);
411     set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
412
413     nsIPrefBranch_Release(pref);
414 }
415
416 static BOOL init_xpcom(const PRUnichar *gre_path)
417 {
418     nsresult nsres;
419     nsIObserver *pStartNotif;
420     nsIComponentRegistrar *registrar = NULL;
421     nsAString path;
422     nsIFile *gre_dir;
423
424     nsAString_Init(&path, gre_path);
425     nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
426     nsAString_Finish(&path);
427     if(NS_FAILED(nsres)) {
428         ERR("NS_NewLocalFile failed: %08x\n", nsres);
429         FreeLibrary(hXPCOM);
430         return FALSE;
431     }
432
433     nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
434     if(NS_FAILED(nsres)) {
435         ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
436         FreeLibrary(hXPCOM);
437         return FALSE;
438     }
439
440     nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
441     if(NS_FAILED(nsres))
442         ERR("Could not get nsIComponentManager: %08x\n", nsres);
443
444     nsres = NS_GetComponentRegistrar(&registrar);
445     if(NS_SUCCEEDED(nsres)) {
446         nsres = nsIComponentRegistrar_AutoRegister(registrar, NULL);
447         if(NS_FAILED(nsres))
448             ERR("AutoRegister(NULL) failed: %08x\n", nsres);
449
450         nsres = nsIComponentRegistrar_AutoRegister(registrar, gre_dir);
451         if(NS_FAILED(nsres))
452             ERR("AutoRegister(gre_dir) failed: %08x\n", nsres);
453
454         init_nsio(pCompMgr, registrar);
455     }else {
456         ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
457     }
458
459     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
460             NULL, &IID_nsIObserver, (void**)&pStartNotif);
461     if(NS_SUCCEEDED(nsres)) {
462         nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
463         if(NS_FAILED(nsres))
464             ERR("Observe failed: %08x\n", nsres);
465
466         nsIObserver_Release(pStartNotif);
467     }else {
468         ERR("could not get appstartup-notifier: %08x\n", nsres);
469     }
470
471     set_profile();
472
473     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_MEMORY_CONTRACTID,
474             NULL, &IID_nsIMemory, (void**)&nsmem);
475     if(NS_FAILED(nsres))
476         ERR("Could not get nsIMemory: %08x\n", nsres);
477
478     if(registrar) {
479         register_nsservice(registrar, pServMgr);
480         nsIComponentRegistrar_Release(registrar);
481     }
482
483     return TRUE;
484 }
485
486 static CRITICAL_SECTION cs_load_gecko;
487 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
488 {
489     0, 0, &cs_load_gecko,
490     { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
491       0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
492 };
493 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
494
495 BOOL load_gecko(BOOL silent)
496 {
497     PRUnichar gre_path[MAX_PATH];
498     BOOL ret = FALSE;
499
500     static DWORD loading_thread;
501
502     TRACE("()\n");
503
504     /* load_gecko may be called recursively */
505     if(loading_thread == GetCurrentThreadId())
506         return pCompMgr != NULL;
507
508     EnterCriticalSection(&cs_load_gecko);
509
510     if(!loading_thread) {
511         loading_thread = GetCurrentThreadId();
512
513         if(load_wine_gecko(gre_path)
514            || (install_wine_gecko(silent) && load_wine_gecko(gre_path)))
515             ret = init_xpcom(gre_path);
516         else
517            MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
518     }else {
519         ret = pCompMgr != NULL;
520     }
521
522     LeaveCriticalSection(&cs_load_gecko);
523
524     return ret;
525 }
526
527 void *nsalloc(size_t size)
528 {
529     return nsIMemory_Alloc(nsmem, size);
530 }
531
532 void nsfree(void *mem)
533 {
534     nsIMemory_Free(nsmem, mem);
535 }
536
537 void nsACString_Init(nsACString *str, const char *data)
538 {
539     NS_CStringContainerInit(str);
540     if(data)
541         nsACString_SetData(str, data);
542 }
543
544 void nsACString_SetData(nsACString *str, const char *data)
545 {
546     NS_CStringSetData(str, data, PR_UINT32_MAX);
547 }
548
549 PRUint32 nsACString_GetData(const nsACString *str, const char **data)
550 {
551     return NS_CStringGetData(str, data, NULL);
552 }
553
554 void nsACString_Finish(nsACString *str)
555 {
556     NS_CStringContainerFinish(str);
557 }
558
559 void nsAString_Init(nsAString *str, const PRUnichar *data)
560 {
561     NS_StringContainerInit(str);
562     if(data)
563         nsAString_SetData(str, data);
564 }
565
566 void nsAString_SetData(nsAString *str, const PRUnichar *data)
567 {
568     NS_StringSetData(str, data, PR_UINT32_MAX);
569 }
570
571 PRUint32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
572 {
573     return NS_StringGetData(str, data, NULL);
574 }
575
576 void nsAString_Finish(nsAString *str)
577 {
578     NS_StringContainerFinish(str);
579 }
580
581 nsIInputStream *create_nsstream(const char *data, PRInt32 data_len)
582 {
583     nsIStringInputStream *ret;
584     nsresult nsres;
585
586     if(!pCompMgr)
587         return NULL;
588
589     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
590             NS_STRINGSTREAM_CONTRACTID, NULL, &IID_nsIStringInputStream,
591             (void**)&ret);
592     if(NS_FAILED(nsres)) {
593         ERR("Could not get nsIStringInputStream\n");
594         return NULL;
595     }
596
597     nsres = nsIStringInputStream_SetData(ret, data, data_len);
598     if(NS_FAILED(nsres)) {
599         ERR("AdoptData failed: %08x\n", nsres);
600         nsIStringInputStream_Release(ret);
601         return NULL;
602     }
603
604     return (nsIInputStream*)ret;
605 }
606
607 nsIMutableArray *create_nsarray(void)
608 {
609     nsIMutableArray *ret;
610     nsresult nsres;
611
612     if(!pCompMgr)
613         return NULL;
614
615     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
616             NS_ARRAY_CONTRACTID, NULL, &IID_nsIMutableArray,
617             (void**)&ret);
618     if(NS_FAILED(nsres)) {
619         ERR("Could not get nsIArray: %08x\n", nsres);
620         return NULL;
621     }
622
623     return ret;
624 }
625
626 nsIWritableVariant *create_nsvariant(void)
627 {
628     nsIWritableVariant *ret;
629     nsresult nsres;
630
631     if(!pCompMgr)
632         return NULL;
633
634     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
635             NS_VARIANT_CONTRACTID, NULL, &IID_nsIWritableVariant,
636             (void**)&ret);
637     if(NS_FAILED(nsres)) {
638         ERR("Could not get nsIWritableVariant: %08x\n", nsres);
639         return NULL;
640     }
641
642     return ret;
643 }
644
645 nsICommandParams *create_nscommand_params(void)
646 {
647     nsICommandParams *ret = NULL;
648     nsresult nsres;
649
650     if(!pCompMgr)
651         return NULL;
652
653     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
654             NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
655             (void**)&ret);
656     if(NS_FAILED(nsres))
657         ERR("Could not get nsICommandParams\n");
658
659     return ret;
660 }
661
662 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
663 {
664     nsIInterfaceRequestor *iface_req;
665     nsresult nsres;
666
667     nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
668     if(NS_FAILED(nsres))
669         return nsres;
670
671     nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
672     nsIInterfaceRequestor_Release(iface_req);
673
674     return nsres;
675 }
676
677 static void nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
678 {
679     nsIDOMNodeList *node_list = NULL;
680     PRBool has_children = FALSE;
681     PRUint16 type;
682     nsresult nsres;
683
684     nsIDOMNode_HasChildNodes(nsnode, &has_children);
685
686     nsres = nsIDOMNode_GetNodeType(nsnode, &type);
687     if(NS_FAILED(nsres)) {
688         ERR("GetType failed: %08x\n", nsres);
689         return;
690     }
691
692     switch(type) {
693     case ELEMENT_NODE: {
694         nsIDOMElement *nselem;
695         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMElement, (void**)&nselem);
696         nsIContentSerializer_AppendElementStart(serializer, nselem, has_children, str);
697         nsIDOMElement_Release(nselem);
698         break;
699     }
700     case TEXT_NODE: {
701         nsIDOMText *nstext;
702         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMText, (void**)&nstext);
703         nsIContentSerializer_AppendText(serializer, nstext, 0, -1, str);
704         nsIDOMText_Release(nstext);
705         break;
706     }
707     case COMMENT_NODE: {
708         nsIDOMComment *nscomment;
709         nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMComment, (void**)&nscomment);
710         nsres = nsIContentSerializer_AppendComment(serializer, nscomment, 0, -1, str);
711         break;
712     }
713     case DOCUMENT_NODE: {
714         nsIDOMDocument *nsdoc;
715         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMDocument, (void**)&nsdoc);
716         nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
717         nsIDOMDocument_Release(nsdoc);
718         break;
719     }
720     case DOCUMENT_FRAGMENT_NODE:
721         break;
722     default:
723         FIXME("Unhandled type %u\n", type);
724     }
725
726     if(has_children) {
727         PRUint32 child_cnt, i;
728         nsIDOMNode *child_node;
729
730         nsIDOMNode_GetChildNodes(nsnode, &node_list);
731         nsIDOMNodeList_GetLength(node_list, &child_cnt);
732
733         for(i=0; i<child_cnt; i++) {
734             nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
735             if(NS_SUCCEEDED(nsres)) {
736                 nsnode_to_nsstring_rec(serializer, child_node, str);
737                 nsIDOMNode_Release(child_node);
738             }else {
739                 ERR("Item failed: %08x\n", nsres);
740             }
741         }
742
743         nsIDOMNodeList_Release(node_list);
744     }
745
746     if(type == ELEMENT_NODE) {
747         nsIDOMElement *nselem;
748         nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMElement, (void**)&nselem);
749         nsIContentSerializer_AppendElementEnd(serializer, nselem, str);
750         nsIDOMElement_Release(nselem);
751     }
752 }
753
754 void nsnode_to_nsstring(nsIDOMNode *nsdoc, nsAString *str)
755 {
756     nsIContentSerializer *serializer;
757     nsIDOMNode *nsnode;
758     nsresult nsres;
759
760     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
761             NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
762             (void**)&serializer);
763     if(NS_FAILED(nsres)) {
764         ERR("Could not get nsIContentSerializer: %08x\n", nsres);
765         return;
766     }
767
768     nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE);
769     if(NS_FAILED(nsres))
770         ERR("Init failed: %08x\n", nsres);
771
772     nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
773     nsnode_to_nsstring_rec(serializer, nsnode, str);
774     nsIDOMNode_Release(nsnode);
775
776     nsres = nsIContentSerializer_Flush(serializer, str);
777     if(NS_FAILED(nsres))
778         ERR("Flush failed: %08x\n", nsres);
779
780     nsIContentSerializer_Release(serializer);
781 }
782
783 void get_editor_controller(NSContainer *This)
784 {
785     nsIEditingSession *editing_session = NULL;
786     nsIControllerContext *ctrlctx;
787     nsresult nsres;
788
789     if(This->editor) {
790         nsIEditor_Release(This->editor);
791         This->editor = NULL;
792     }
793
794     if(This->editor_controller) {
795         nsIController_Release(This->editor_controller);
796         This->editor_controller = NULL;
797     }
798
799     nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
800             (void**)&editing_session);
801     if(NS_FAILED(nsres)) {
802         ERR("Could not get nsIEditingSession: %08x\n", nsres);
803         return;
804     }
805
806     nsres = nsIEditingSession_GetEditorForWindow(editing_session,
807             This->doc->window->nswindow, &This->editor);
808     nsIEditingSession_Release(editing_session);
809     if(NS_FAILED(nsres)) {
810         ERR("Could not get editor: %08x\n", nsres);
811         return;
812     }
813
814     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
815             NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
816     if(NS_SUCCEEDED(nsres)) {
817         nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
818         if(NS_FAILED(nsres))
819             ERR("SetCommandContext failed: %08x\n", nsres);
820         nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
821                 (void**)&This->editor_controller);
822         nsIControllerContext_Release(ctrlctx);
823         if(NS_FAILED(nsres))
824             ERR("Could not get nsIController interface: %08x\n", nsres);
825     }else {
826         ERR("Could not create edit controller: %08x\n", nsres);
827     }
828 }
829
830 void set_ns_editmode(NSContainer *This)
831 {
832     nsIEditingSession *editing_session = NULL;
833     nsIURIContentListener *listener = NULL;
834     nsIDOMWindow *dom_window = NULL;
835     nsresult nsres;
836
837     nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
838             (void**)&editing_session);
839     if(NS_FAILED(nsres)) {
840         ERR("Could not get nsIEditingSession: %08x\n", nsres);
841         return;
842     }
843
844     nsres = nsIWebBrowser_GetContentDOMWindow(This->webbrowser, &dom_window);
845     if(NS_FAILED(nsres)) {
846         ERR("Could not get content DOM window: %08x\n", nsres);
847         nsIEditingSession_Release(editing_session);
848         return;
849     }
850
851     nsres = nsIEditingSession_MakeWindowEditable(editing_session, dom_window, NULL, FALSE);
852     nsIEditingSession_Release(editing_session);
853     nsIDOMWindow_Release(dom_window);
854     if(NS_FAILED(nsres)) {
855         ERR("MakeWindowEditable failed: %08x\n", nsres);
856         return;
857     }
858
859     /* MakeWindowEditable changes WebBrowser's parent URI content listener.
860      * It seams to be a bug in Gecko. To workaround it we set our content
861      * listener again and Gecko's one as its parent.
862      */
863     nsIWebBrowser_GetParentURIContentListener(This->webbrowser, &listener);
864     nsIURIContentListener_SetParentContentListener(NSURICL(This), listener);
865     nsIURIContentListener_Release(listener);
866     nsIWebBrowser_SetParentURIContentListener(This->webbrowser, NSURICL(This));
867 }
868
869 void update_nsdocument(HTMLDocument *doc)
870 {
871     nsIDOMHTMLDocument *nsdoc;
872     nsIDOMDocument *nsdomdoc;
873     nsresult nsres;
874
875     if(!doc->nscontainer || !doc->nscontainer->navigation)
876         return;
877
878     nsres = nsIWebNavigation_GetDocument(doc->nscontainer->navigation, &nsdomdoc);
879     if(NS_FAILED(nsres) || !nsdomdoc) {
880         ERR("GetDocument failed: %08x\n", nsres);
881         return;
882     }
883
884     nsres = nsIDOMDocument_QueryInterface(nsdomdoc, &IID_nsIDOMHTMLDocument, (void**)&nsdoc);
885     nsIDOMDocument_Release(nsdomdoc);
886     if(NS_FAILED(nsres)) {
887         ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
888         return;
889     }
890
891     if(nsdoc == doc->nsdoc) {
892         nsIDOMHTMLDocument_Release(nsdoc);
893         return;
894     }
895
896     if(doc->nsdoc)
897         nsIDOMHTMLDocument_Release(doc->nsdoc);
898
899     doc->nsdoc = nsdoc;
900 }
901
902 void close_gecko(void)
903 {
904     TRACE("()\n");
905
906     if(pCompMgr)
907         nsIComponentManager_Release(pCompMgr);
908
909     if(pServMgr)
910         nsIServiceManager_Release(pServMgr);
911
912     if(nsmem)
913         nsIMemory_Release(nsmem);
914
915     if(hXPCOM)
916         FreeLibrary(hXPCOM);
917 }
918
919 /**********************************************************
920  *      nsIWebBrowserChrome interface
921  */
922
923 #define NSWBCHROME_THIS(iface) DEFINE_THIS(NSContainer, WebBrowserChrome, iface)
924
925 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
926         nsIIDRef riid, nsQIResult result)
927 {
928     NSContainer *This = NSWBCHROME_THIS(iface);
929
930     *result = NULL;
931     if(IsEqualGUID(&IID_nsISupports, riid)) {
932         TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
933         *result = NSWBCHROME(This);
934     }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
935         TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
936         *result = NSWBCHROME(This);
937     }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
938         TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
939         *result = NSCML(This);
940     }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
941         TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
942         *result = NSURICL(This);
943     }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
944         TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
945         *result = NSEMBWNDS(This);
946     }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
947         TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
948         *result = NSTOOLTIP(This);
949     }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
950         TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
951         *result = NSIFACEREQ(This);
952     }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
953         TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
954         *result = NSWEAKREF(This);
955     }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
956         TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
957         *result = NSSUPWEAKREF(This);
958     }
959
960     if(*result) {
961         nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
962         return NS_OK;
963     }
964
965     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
966     return NS_NOINTERFACE;
967 }
968
969 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
970 {
971     NSContainer *This = NSWBCHROME_THIS(iface);
972     LONG ref = InterlockedIncrement(&This->ref);
973
974     TRACE("(%p) ref=%d\n", This, ref);
975
976     return ref;
977 }
978
979 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
980 {
981     NSContainer *This = NSWBCHROME_THIS(iface);
982     LONG ref = InterlockedDecrement(&This->ref);
983
984     TRACE("(%p) ref=%d\n", This, ref);
985
986     if(!ref) {
987         heap_free(This->event_vector);
988         if(This->parent)
989             nsIWebBrowserChrome_Release(NSWBCHROME(This->parent));
990         heap_free(This);
991     }
992
993     return ref;
994 }
995
996 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
997         PRUint32 statusType, const PRUnichar *status)
998 {
999     NSContainer *This = NSWBCHROME_THIS(iface);
1000
1001     TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
1002
1003     /* FIXME: This hack should be removed when we'll load all pages by URLMoniker */
1004     if(This->doc)
1005         update_nsdocument(This->doc);
1006
1007     return NS_OK;
1008 }
1009
1010 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
1011         nsIWebBrowser **aWebBrowser)
1012 {
1013     NSContainer *This = NSWBCHROME_THIS(iface);
1014
1015     TRACE("(%p)->(%p)\n", This, aWebBrowser);
1016
1017     if(!aWebBrowser)
1018         return NS_ERROR_INVALID_ARG;
1019
1020     if(This->webbrowser)
1021         nsIWebBrowser_AddRef(This->webbrowser);
1022     *aWebBrowser = This->webbrowser;
1023     return S_OK;
1024 }
1025
1026 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
1027         nsIWebBrowser *aWebBrowser)
1028 {
1029     NSContainer *This = NSWBCHROME_THIS(iface);
1030
1031     TRACE("(%p)->(%p)\n", This, aWebBrowser);
1032
1033     if(aWebBrowser != This->webbrowser)
1034         ERR("Wrong nsWebBrowser!\n");
1035
1036     return NS_OK;
1037 }
1038
1039 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1040         PRUint32 *aChromeFlags)
1041 {
1042     NSContainer *This = NSWBCHROME_THIS(iface);
1043     WARN("(%p)->(%p)\n", This, aChromeFlags);
1044     return NS_ERROR_NOT_IMPLEMENTED;
1045 }
1046
1047 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1048         PRUint32 aChromeFlags)
1049 {
1050     NSContainer *This = NSWBCHROME_THIS(iface);
1051     WARN("(%p)->(%08x)\n", This, aChromeFlags);
1052     return NS_ERROR_NOT_IMPLEMENTED;
1053 }
1054
1055 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1056 {
1057     NSContainer *This = NSWBCHROME_THIS(iface);
1058     TRACE("(%p)\n", This);
1059     return NS_ERROR_NOT_IMPLEMENTED;
1060 }
1061
1062 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1063         PRInt32 aCX, PRInt32 aCY)
1064 {
1065     NSContainer *This = NSWBCHROME_THIS(iface);
1066     WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1067     return NS_ERROR_NOT_IMPLEMENTED;
1068 }
1069
1070 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1071 {
1072     NSContainer *This = NSWBCHROME_THIS(iface);
1073     WARN("(%p)\n", This);
1074     return NS_ERROR_NOT_IMPLEMENTED;
1075 }
1076
1077 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
1078 {
1079     NSContainer *This = NSWBCHROME_THIS(iface);
1080     WARN("(%p)->(%p)\n", This, _retval);
1081     return NS_ERROR_NOT_IMPLEMENTED;
1082 }
1083
1084 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1085         nsresult aStatus)
1086 {
1087     NSContainer *This = NSWBCHROME_THIS(iface);
1088     WARN("(%p)->(%08x)\n", This, aStatus);
1089     return NS_ERROR_NOT_IMPLEMENTED;
1090 }
1091
1092 #undef NSWBCHROME_THIS
1093
1094 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1095     nsWebBrowserChrome_QueryInterface,
1096     nsWebBrowserChrome_AddRef,
1097     nsWebBrowserChrome_Release,
1098     nsWebBrowserChrome_SetStatus,
1099     nsWebBrowserChrome_GetWebBrowser,
1100     nsWebBrowserChrome_SetWebBrowser,
1101     nsWebBrowserChrome_GetChromeFlags,
1102     nsWebBrowserChrome_SetChromeFlags,
1103     nsWebBrowserChrome_DestroyBrowserWindow,
1104     nsWebBrowserChrome_SizeBrowserTo,
1105     nsWebBrowserChrome_ShowAsModal,
1106     nsWebBrowserChrome_IsWindowModal,
1107     nsWebBrowserChrome_ExitModalEventLoop
1108 };
1109
1110 /**********************************************************
1111  *      nsIContextMenuListener interface
1112  */
1113
1114 #define NSCML_THIS(iface) DEFINE_THIS(NSContainer, ContextMenuListener, iface)
1115
1116 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1117         nsIIDRef riid, nsQIResult result)
1118 {
1119     NSContainer *This = NSCML_THIS(iface);
1120     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1121 }
1122
1123 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1124 {
1125     NSContainer *This = NSCML_THIS(iface);
1126     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1127 }
1128
1129 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1130 {
1131     NSContainer *This = NSCML_THIS(iface);
1132     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1133 }
1134
1135 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1136         PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1137 {
1138     NSContainer *This = NSCML_THIS(iface);
1139     nsIDOMMouseEvent *event;
1140     POINT pt;
1141     DWORD dwID = CONTEXT_MENU_DEFAULT;
1142     nsresult nsres;
1143
1144     TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1145
1146     nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
1147     if(NS_FAILED(nsres)) {
1148         ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
1149         return nsres;
1150     }
1151
1152     nsIDOMMouseEvent_GetScreenX(event, &pt.x);
1153     nsIDOMMouseEvent_GetScreenY(event, &pt.y);
1154     nsIDOMMouseEvent_Release(event);
1155
1156     switch(aContextFlags) {
1157     case CONTEXT_NONE:
1158     case CONTEXT_DOCUMENT:
1159     case CONTEXT_TEXT:
1160         dwID = CONTEXT_MENU_DEFAULT;
1161         break;
1162     case CONTEXT_IMAGE:
1163     case CONTEXT_IMAGE|CONTEXT_LINK:
1164         dwID = CONTEXT_MENU_IMAGE;
1165         break;
1166     case CONTEXT_LINK:
1167         dwID = CONTEXT_MENU_ANCHOR;
1168         break;
1169     case CONTEXT_INPUT:
1170         dwID = CONTEXT_MENU_CONTROL;
1171         break;
1172     default:
1173         FIXME("aContextFlags=%08x\n", aContextFlags);
1174     };
1175
1176     show_context_menu(This->doc, dwID, &pt, (IDispatch*)HTMLDOMNODE(get_node(This->doc, aNode, TRUE)));
1177
1178     return NS_OK;
1179 }
1180
1181 #undef NSCML_THIS
1182
1183 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1184     nsContextMenuListener_QueryInterface,
1185     nsContextMenuListener_AddRef,
1186     nsContextMenuListener_Release,
1187     nsContextMenuListener_OnShowContextMenu
1188 };
1189
1190 /**********************************************************
1191  *      nsIURIContentListener interface
1192  */
1193
1194 #define NSURICL_THIS(iface) DEFINE_THIS(NSContainer, URIContentListener, iface)
1195
1196 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1197         nsIIDRef riid, nsQIResult result)
1198 {
1199     NSContainer *This = NSURICL_THIS(iface);
1200     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1201 }
1202
1203 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1204 {
1205     NSContainer *This = NSURICL_THIS(iface);
1206     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1207 }
1208
1209 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1210 {
1211     NSContainer *This = NSURICL_THIS(iface);
1212     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1213 }
1214
1215 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1216                                                           nsIURI *aURI, PRBool *_retval)
1217 {
1218     NSContainer *This = NSURICL_THIS(iface);
1219     nsIWineURI *wine_uri;
1220     nsACString spec_str;
1221     const char *spec;
1222     nsresult nsres;
1223
1224     nsACString_Init(&spec_str, NULL);
1225     nsIURI_GetSpec(aURI, &spec_str);
1226     nsACString_GetData(&spec_str, &spec);
1227
1228     TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1229
1230     nsACString_Finish(&spec_str);
1231
1232     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
1233     if(NS_FAILED(nsres)) {
1234         WARN("Could not get nsIWineURI interface: %08x\n", nsres);
1235         return NS_ERROR_NOT_IMPLEMENTED;
1236     }
1237
1238     nsIWineURI_SetNSContainer(wine_uri, This);
1239     nsIWineURI_SetIsDocumentURI(wine_uri, TRUE);
1240
1241     if(This->bscallback) {
1242         IMoniker *mon = get_channelbsc_mon(This->bscallback);
1243
1244         if(mon) {
1245             LPWSTR wine_url;
1246             HRESULT hres;
1247
1248             hres = IMoniker_GetDisplayName(mon, NULL, 0, &wine_url);
1249             if(SUCCEEDED(hres)) {
1250                 nsIWineURI_SetWineURL(wine_uri, wine_url);
1251                 CoTaskMemFree(wine_url);
1252             }else {
1253                 WARN("GetDisplayName failed: %08x\n", hres);
1254             }
1255
1256             IMoniker_Release(mon);
1257         }
1258     }
1259
1260     nsIWineURI_Release(wine_uri);
1261
1262     *_retval = FALSE;
1263     return This->content_listener
1264         ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1265         : NS_OK;
1266 }
1267
1268 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1269         const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
1270         nsIStreamListener **aContentHandler, PRBool *_retval)
1271 {
1272     NSContainer *This = NSURICL_THIS(iface);
1273
1274     TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1275             aRequest, aContentHandler, _retval);
1276
1277     return This->content_listener
1278         ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1279                   aIsContentPreferred, aRequest, aContentHandler, _retval)
1280         : NS_ERROR_NOT_IMPLEMENTED;
1281 }
1282
1283 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1284         const char *aContentType, char **aDesiredContentType, PRBool *_retval)
1285 {
1286     NSContainer *This = NSURICL_THIS(iface);
1287
1288     TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1289
1290     /* FIXME: Should we do something here? */
1291     *_retval = TRUE; 
1292
1293     return This->content_listener
1294         ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1295                   aDesiredContentType, _retval)
1296         : NS_OK;
1297 }
1298
1299 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1300         const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
1301         PRBool *_retval)
1302 {
1303     NSContainer *This = NSURICL_THIS(iface);
1304
1305     TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1306             aDesiredContentType, _retval);
1307
1308     return This->content_listener
1309         ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1310                 aIsContentPreferred, aDesiredContentType, _retval)
1311         : NS_ERROR_NOT_IMPLEMENTED;
1312 }
1313
1314 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1315         nsISupports **aLoadCookie)
1316 {
1317     NSContainer *This = NSURICL_THIS(iface);
1318
1319     WARN("(%p)->(%p)\n", This, aLoadCookie);
1320
1321     return This->content_listener
1322         ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1323         : NS_ERROR_NOT_IMPLEMENTED;
1324 }
1325
1326 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1327         nsISupports *aLoadCookie)
1328 {
1329     NSContainer *This = NSURICL_THIS(iface);
1330
1331     WARN("(%p)->(%p)\n", This, aLoadCookie);
1332
1333     return This->content_listener
1334         ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1335         : NS_ERROR_NOT_IMPLEMENTED;
1336 }
1337
1338 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1339         nsIURIContentListener **aParentContentListener)
1340 {
1341     NSContainer *This = NSURICL_THIS(iface);
1342
1343     TRACE("(%p)->(%p)\n", This, aParentContentListener);
1344
1345     if(This->content_listener)
1346         nsIURIContentListener_AddRef(This->content_listener);
1347
1348     *aParentContentListener = This->content_listener;
1349     return NS_OK;
1350 }
1351
1352 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1353         nsIURIContentListener *aParentContentListener)
1354 {
1355     NSContainer *This = NSURICL_THIS(iface);
1356
1357     TRACE("(%p)->(%p)\n", This, aParentContentListener);
1358
1359     if(aParentContentListener == NSURICL(This))
1360         return NS_OK;
1361
1362     if(This->content_listener)
1363         nsIURIContentListener_Release(This->content_listener);
1364
1365     This->content_listener = aParentContentListener;
1366     if(This->content_listener)
1367         nsIURIContentListener_AddRef(This->content_listener);
1368
1369     return NS_OK;
1370 }
1371
1372 #undef NSURICL_THIS
1373
1374 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1375     nsURIContentListener_QueryInterface,
1376     nsURIContentListener_AddRef,
1377     nsURIContentListener_Release,
1378     nsURIContentListener_OnStartURIOpen,
1379     nsURIContentListener_DoContent,
1380     nsURIContentListener_IsPreferred,
1381     nsURIContentListener_CanHandleContent,
1382     nsURIContentListener_GetLoadCookie,
1383     nsURIContentListener_SetLoadCookie,
1384     nsURIContentListener_GetParentContentListener,
1385     nsURIContentListener_SetParentContentListener
1386 };
1387
1388 /**********************************************************
1389  *      nsIEmbeddinSiteWindow interface
1390  */
1391
1392 #define NSEMBWNDS_THIS(iface) DEFINE_THIS(NSContainer, EmbeddingSiteWindow, iface)
1393
1394 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1395         nsIIDRef riid, nsQIResult result)
1396 {
1397     NSContainer *This = NSEMBWNDS_THIS(iface);
1398     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1399 }
1400
1401 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1402 {
1403     NSContainer *This = NSEMBWNDS_THIS(iface);
1404     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1405 }
1406
1407 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1408 {
1409     NSContainer *This = NSEMBWNDS_THIS(iface);
1410     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1411 }
1412
1413 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1414         PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
1415 {
1416     NSContainer *This = NSEMBWNDS_THIS(iface);
1417     WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1418     return NS_ERROR_NOT_IMPLEMENTED;
1419 }
1420
1421 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1422         PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
1423 {
1424     NSContainer *This = NSEMBWNDS_THIS(iface);
1425     WARN("(%p)->(%08x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1426     return NS_ERROR_NOT_IMPLEMENTED;
1427 }
1428
1429 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1430 {
1431     NSContainer *This = NSEMBWNDS_THIS(iface);
1432
1433     TRACE("(%p)\n", This);
1434
1435     if(This->reset_focus)
1436         PostMessageW(This->hwnd, WM_RESETFOCUS_HACK, 0, 0);
1437
1438     return nsIBaseWindow_SetFocus(This->window);
1439 }
1440
1441 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1442         PRBool *aVisibility)
1443 {
1444     NSContainer *This = NSEMBWNDS_THIS(iface);
1445
1446     TRACE("(%p)->(%p)\n", This, aVisibility);
1447
1448     *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1449     return NS_OK;
1450 }
1451
1452 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1453         PRBool aVisibility)
1454 {
1455     NSContainer *This = NSEMBWNDS_THIS(iface);
1456
1457     TRACE("(%p)->(%x)\n", This, aVisibility);
1458
1459     return NS_OK;
1460 }
1461
1462 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1463         PRUnichar **aTitle)
1464 {
1465     NSContainer *This = NSEMBWNDS_THIS(iface);
1466     WARN("(%p)->(%p)\n", This, aTitle);
1467     return NS_ERROR_NOT_IMPLEMENTED;
1468 }
1469
1470 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1471         const PRUnichar *aTitle)
1472 {
1473     NSContainer *This = NSEMBWNDS_THIS(iface);
1474     WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1475     return NS_ERROR_NOT_IMPLEMENTED;
1476 }
1477
1478 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1479         void **aSiteWindow)
1480 {
1481     NSContainer *This = NSEMBWNDS_THIS(iface);
1482
1483     TRACE("(%p)->(%p)\n", This, aSiteWindow);
1484
1485     *aSiteWindow = This->hwnd;
1486     return NS_OK;
1487 }
1488
1489 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1490     nsEmbeddingSiteWindow_QueryInterface,
1491     nsEmbeddingSiteWindow_AddRef,
1492     nsEmbeddingSiteWindow_Release,
1493     nsEmbeddingSiteWindow_SetDimensions,
1494     nsEmbeddingSiteWindow_GetDimensions,
1495     nsEmbeddingSiteWindow_SetFocus,
1496     nsEmbeddingSiteWindow_GetVisibility,
1497     nsEmbeddingSiteWindow_SetVisibility,
1498     nsEmbeddingSiteWindow_GetTitle,
1499     nsEmbeddingSiteWindow_SetTitle,
1500     nsEmbeddingSiteWindow_GetSiteWindow
1501 };
1502
1503 #define NSTOOLTIP_THIS(iface) DEFINE_THIS(NSContainer, TooltipListener, iface)
1504
1505 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1506                                                        nsQIResult result)
1507 {
1508     NSContainer *This = NSTOOLTIP_THIS(iface);
1509     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1510 }
1511
1512 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1513 {
1514     NSContainer *This = NSTOOLTIP_THIS(iface);
1515     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1516 }
1517
1518 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1519 {
1520     NSContainer *This = NSTOOLTIP_THIS(iface);
1521     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1522 }
1523
1524 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1525         PRInt32 aXCoord, PRInt32 aYCoord, const PRUnichar *aTipText)
1526 {
1527     NSContainer *This = NSTOOLTIP_THIS(iface);
1528
1529     show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1530
1531     return NS_OK;
1532 }
1533
1534 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1535 {
1536     NSContainer *This = NSTOOLTIP_THIS(iface);
1537
1538     hide_tooltip(This->doc);
1539
1540     return NS_OK;
1541 }
1542
1543 #undef NSTOOLTIM_THIS
1544
1545 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1546     nsTooltipListener_QueryInterface,
1547     nsTooltipListener_AddRef,
1548     nsTooltipListener_Release,
1549     nsTooltipListener_OnShowTooltip,
1550     nsTooltipListener_OnHideTooltip
1551 };
1552
1553 #define NSIFACEREQ_THIS(iface) DEFINE_THIS(NSContainer, InterfaceRequestor, iface)
1554
1555 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1556                                                           nsIIDRef riid, nsQIResult result)
1557 {
1558     NSContainer *This = NSIFACEREQ_THIS(iface);
1559     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1560 }
1561
1562 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1563 {
1564     NSContainer *This = NSIFACEREQ_THIS(iface);
1565     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1566 }
1567
1568 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1569 {
1570     NSContainer *This = NSIFACEREQ_THIS(iface);
1571     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1572 }
1573
1574 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1575                                                         nsIIDRef riid, nsQIResult result)
1576 {
1577     NSContainer *This = NSIFACEREQ_THIS(iface);
1578
1579     if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1580         TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1581         return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1582     }
1583
1584     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1585 }
1586
1587 #undef NSIFACEREQ_THIS
1588
1589 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1590     nsInterfaceRequestor_QueryInterface,
1591     nsInterfaceRequestor_AddRef,
1592     nsInterfaceRequestor_Release,
1593     nsInterfaceRequestor_GetInterface
1594 };
1595
1596 #define NSWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, WeakReference, iface)
1597
1598 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1599         nsIIDRef riid, nsQIResult result)
1600 {
1601     NSContainer *This = NSWEAKREF_THIS(iface);
1602     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1603 }
1604
1605 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1606 {
1607     NSContainer *This = NSWEAKREF_THIS(iface);
1608     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1609 }
1610
1611 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1612 {
1613     NSContainer *This = NSWEAKREF_THIS(iface);
1614     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1615 }
1616
1617 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1618         const nsIID *riid, void **result)
1619 {
1620     NSContainer *This = NSWEAKREF_THIS(iface);
1621     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1622 }
1623
1624 #undef NSWEAKREF_THIS
1625
1626 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1627     nsWeakReference_QueryInterface,
1628     nsWeakReference_AddRef,
1629     nsWeakReference_Release,
1630     nsWeakReference_QueryReferent
1631 };
1632
1633 #define NSSUPWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, SupportsWeakReference, iface)
1634
1635 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1636         nsIIDRef riid, nsQIResult result)
1637 {
1638     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1639     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1640 }
1641
1642 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1643 {
1644     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1645     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1646 }
1647
1648 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1649 {
1650     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1651     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1652 }
1653
1654 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1655         nsIWeakReference **_retval)
1656 {
1657     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1658
1659     TRACE("(%p)->(%p)\n", This, _retval);
1660
1661     nsIWeakReference_AddRef(NSWEAKREF(This));
1662     *_retval = NSWEAKREF(This);
1663     return NS_OK;
1664 }
1665
1666 #undef NSWEAKREF_THIS
1667
1668 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1669     nsSupportsWeakReference_QueryInterface,
1670     nsSupportsWeakReference_AddRef,
1671     nsSupportsWeakReference_Release,
1672     nsSupportsWeakReference_GetWeakReference
1673 };
1674
1675
1676 NSContainer *NSContainer_Create(HTMLDocument *doc, NSContainer *parent)
1677 {
1678     nsIWebBrowserSetup *wbsetup;
1679     nsIScrollable *scrollable;
1680     NSContainer *ret;
1681     nsresult nsres;
1682
1683     if(!load_gecko(FALSE))
1684         return NULL;
1685
1686     ret = heap_alloc_zero(sizeof(NSContainer));
1687
1688     ret->lpWebBrowserChromeVtbl      = &nsWebBrowserChromeVtbl;
1689     ret->lpContextMenuListenerVtbl   = &nsContextMenuListenerVtbl;
1690     ret->lpURIContentListenerVtbl    = &nsURIContentListenerVtbl;
1691     ret->lpEmbeddingSiteWindowVtbl   = &nsEmbeddingSiteWindowVtbl;
1692     ret->lpTooltipListenerVtbl       = &nsTooltipListenerVtbl;
1693     ret->lpInterfaceRequestorVtbl    = &nsInterfaceRequestorVtbl;
1694     ret->lpWeakReferenceVtbl         = &nsWeakReferenceVtbl;
1695     ret->lpSupportsWeakReferenceVtbl = &nsSupportsWeakReferenceVtbl;
1696
1697     ret->doc = doc;
1698     ret->ref = 1;
1699
1700     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1701             NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
1702     if(NS_FAILED(nsres)) {
1703         ERR("Creating WebBrowser failed: %08x\n", nsres);
1704         heap_free(ret);
1705         return NULL;
1706     }
1707
1708     if(parent)
1709         nsIWebBrowserChrome_AddRef(NSWBCHROME(parent));
1710     ret->parent = parent;
1711
1712     nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, NSWBCHROME(ret));
1713     if(NS_FAILED(nsres))
1714         ERR("SetContainerWindow failed: %08x\n", nsres);
1715
1716     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
1717             (void**)&ret->window);
1718     if(NS_FAILED(nsres))
1719         ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1720
1721     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
1722                                          (void**)&wbsetup);
1723     if(NS_SUCCEEDED(nsres)) {
1724         nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
1725         nsIWebBrowserSetup_Release(wbsetup);
1726         if(NS_FAILED(nsres))
1727             ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
1728     }else {
1729         ERR("Could not get nsIWebBrowserSetup interface\n");
1730     }
1731
1732     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
1733             (void**)&ret->navigation);
1734     if(NS_FAILED(nsres))
1735         ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1736
1737     nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
1738             (void**)&ret->focus);
1739     if(NS_FAILED(nsres))
1740         ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
1741
1742     if(!nscontainer_class)
1743         register_nscontainer_class();
1744
1745     ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1746             WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1747             GetDesktopWindow(), NULL, hInst, ret);
1748
1749     nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
1750     if(NS_SUCCEEDED(nsres)) {
1751         nsres = nsIBaseWindow_Create(ret->window);
1752         if(NS_FAILED(nsres))
1753             WARN("Creating window failed: %08x\n", nsres);
1754
1755         nsIBaseWindow_SetVisibility(ret->window, FALSE);
1756         nsIBaseWindow_SetEnabled(ret->window, FALSE);
1757     }else {
1758         ERR("InitWindow failed: %08x\n", nsres);
1759     }
1760
1761     nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser, NSURICL(ret));
1762     if(NS_FAILED(nsres))
1763         ERR("SetParentURIContentListener failed: %08x\n", nsres);
1764
1765     init_nsevents(ret);
1766
1767     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
1768     if(NS_SUCCEEDED(nsres)) {
1769         nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1770                 ScrollOrientation_Y, Scrollbar_Always);
1771         if(NS_FAILED(nsres))
1772             ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
1773
1774         nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1775                 ScrollOrientation_X, Scrollbar_Auto);
1776         if(NS_FAILED(nsres))
1777             ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
1778
1779         nsIScrollable_Release(scrollable);
1780     }else {
1781         ERR("Could not get nsIScrollable: %08x\n", nsres);
1782     }
1783
1784     return ret;
1785 }
1786
1787 void NSContainer_Release(NSContainer *This)
1788 {
1789     TRACE("(%p)\n", This);
1790
1791     This->doc = NULL;
1792
1793     ShowWindow(This->hwnd, SW_HIDE);
1794     SetParent(This->hwnd, NULL);
1795
1796     nsIBaseWindow_SetVisibility(This->window, FALSE);
1797     nsIBaseWindow_Destroy(This->window);
1798
1799     nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1800
1801     nsIWebBrowser_Release(This->webbrowser);
1802     This->webbrowser = NULL;
1803
1804     nsIWebNavigation_Release(This->navigation);
1805     This->navigation = NULL;
1806
1807     nsIBaseWindow_Release(This->window);
1808     This->window = NULL;
1809
1810     nsIWebBrowserFocus_Release(This->focus);
1811     This->focus = NULL;
1812
1813     if(This->editor_controller) {
1814         nsIController_Release(This->editor_controller);
1815         This->editor_controller = NULL;
1816     }
1817
1818     if(This->editor) {
1819         nsIEditor_Release(This->editor);
1820         This->editor = NULL;
1821     }
1822
1823     if(This->content_listener) {
1824         nsIURIContentListener_Release(This->content_listener);
1825         This->content_listener = NULL;
1826     }
1827
1828     if(This->hwnd) {
1829         DestroyWindow(This->hwnd);
1830         This->hwnd = NULL;
1831     }
1832
1833     nsIWebBrowserChrome_Release(NSWBCHROME(This));
1834 }