dbghelp: Dwarf abbrev table is now a sparse array.
[wine] / dlls / mshtml / nsembed.c
1 /*
2  * Copyright 2005-2006 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
44 #define APPSTARTUP_TOPIC "app-startup"
45
46 #define PR_UINT32_MAX 0xffffffff
47
48 struct nsCStringContainer {
49     void *v;
50     void *d1;
51     PRUint32 d2;
52     void *d3;
53 };
54
55 static nsresult (*NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
56 static nsresult (*NS_ShutdownXPCOM)(nsIServiceManager*);
57 static nsresult (*NS_GetComponentRegistrar)(nsIComponentRegistrar**);
58 static nsresult (*NS_StringContainerInit)(nsStringContainer*);
59 static nsresult (*NS_CStringContainerInit)(nsCStringContainer*);
60 static nsresult (*NS_StringContainerFinish)(nsStringContainer*);
61 static nsresult (*NS_CStringContainerFinish)(nsCStringContainer*);
62 static nsresult (*NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
63 static nsresult (*NS_CStringSetData)(nsACString*,const char*,PRUint32);
64 static nsresult (*NS_NewLocalFile)(const nsAString*,PRBool,nsIFile**);
65 static PRUint32 (*NS_StringGetData)(const nsAString*,const PRUnichar **,PRBool*);
66 static PRUint32 (*NS_CStringGetData)(const nsACString*,const char**,PRBool*);
67
68 static HINSTANCE hXPCOM = NULL;
69
70 static nsIServiceManager *pServMgr = NULL;
71 static nsIComponentManager *pCompMgr = NULL;
72 static nsIMemory *nsmem = NULL;
73
74 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
75
76 static ATOM nscontainer_class;
77
78 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
79 {
80     NSContainer *This;
81     nsresult nsres;
82
83     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
84
85     if(msg == WM_CREATE) {
86         This = *(NSContainer**)lParam;
87         SetPropW(hwnd, wszTHIS, This);
88     }else {
89         This = (NSContainer*)GetPropW(hwnd, wszTHIS);
90     }
91
92     switch(msg) {
93         case WM_SIZE:
94             TRACE("(%p)->(WM_SIZE)\n", This);
95
96             nsres = nsIBaseWindow_SetSize(This->window,
97                     LOWORD(lParam), HIWORD(lParam), TRUE);
98             if(NS_FAILED(nsres))
99                 WARN("SetSize failed: %08lx\n", nsres);
100     }
101
102     return DefWindowProcW(hwnd, msg, wParam, lParam);
103 }
104
105
106 static void register_nscontainer_class(void)
107 {
108     static WNDCLASSEXW wndclass = {
109         sizeof(WNDCLASSEXW),
110         CS_DBLCLKS,
111         nsembed_proc,
112         0, 0, NULL, NULL, NULL, NULL, NULL,
113         wszNsContainer,
114         NULL,
115     };
116     wndclass.hInstance = hInst;
117     nscontainer_class = RegisterClassExW(&wndclass);
118 }
119
120 static BOOL get_mozilla_path(PRUnichar *gre_path)
121 {
122     DWORD res, type, i, size = MAX_PATH;
123     HKEY mozilla_key, hkey;
124     WCHAR key_name[100];
125     BOOL ret = FALSE;
126
127     static const WCHAR wszGreKey[] =
128         {'S','o','f','t','w','a','r','e','\\',
129             'm','o','z','i','l','l','a','.','o','r','g','\\',
130                 'G','R','E',0};
131
132     static const WCHAR wszGreHome[] = {'G','r','e','H','o','m','e',0};
133
134     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszGreKey, &mozilla_key);
135     if(res != ERROR_SUCCESS) {
136         TRACE("Could not open key %s\n", debugstr_w(wszGreKey));
137         return FALSE;
138     }
139
140     for(i=0; !ret && RegEnumKeyW(mozilla_key, i, key_name, sizeof(key_name)/sizeof(WCHAR)) == ERROR_SUCCESS; i++) {
141         RegOpenKeyW(mozilla_key, key_name, &hkey);
142         res = RegQueryValueExW(hkey, wszGreHome, NULL, &type, (LPBYTE)gre_path, &size);
143         if(res == ERROR_SUCCESS)
144             ret = TRUE;
145         RegCloseKey(hkey);
146     }
147
148     RegCloseKey(mozilla_key);
149     return ret;
150 }
151
152 static BOOL get_mozctl_path(PRUnichar *gre_path)
153 {
154     HKEY hkey;
155     DWORD res, type, size = MAX_PATH;
156
157     static const WCHAR wszMozCtlKey[] =
158         {'S','o','f','t','w','a','r','e','\\','M','o','z','i','l','l','a',0};
159     static const WCHAR wszBinDirectoryPath[] =
160         {'B','i','n','D','i','r','e','c','t','o','r','y','P','a','t','h',0};
161     static const WCHAR wszMozCtlClsidKey[] =
162         {'C','L','S','I','D','\\',
163          '{','1','3','3','9','B','5','4','C','-','3','4','5','3','-','1','1','D','2',
164          '-','9','3','B','9','-','0','0','0','0','0','0','0','0','0','0','0','0','}','\\',
165          'I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
166
167     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszMozCtlKey, &hkey);
168     if(res == ERROR_SUCCESS) {
169         res = RegQueryValueExW(hkey, wszBinDirectoryPath, NULL, &type, (LPBYTE)gre_path, &size);
170         if(res == ERROR_SUCCESS)
171             return TRUE;
172         else
173             ERR("Could not get value %s\n", debugstr_w(wszBinDirectoryPath));
174     }
175
176     res = RegOpenKeyW(HKEY_CLASSES_ROOT, wszMozCtlClsidKey, &hkey);
177     if(res == ERROR_SUCCESS) {
178         res = RegQueryValueExW(hkey, NULL, NULL, &type, (LPBYTE)gre_path, &size);
179         if(res == ERROR_SUCCESS) {
180             WCHAR *ptr;
181             if((ptr = strrchrW(gre_path, '\\')))
182                 ptr[1] = 0;
183             return TRUE;
184         }else {
185             ERR("Could not get value of %s\n", debugstr_w(wszMozCtlClsidKey));
186         }
187     }
188
189     TRACE("Could not find Mozilla ActiveX Control\n");
190
191     return FALSE;
192 }
193
194 static BOOL get_wine_gecko_path(PRUnichar *gre_path)
195 {
196     HKEY hkey;
197     DWORD res, type, size = MAX_PATH;
198
199     static const WCHAR wszMshtmlKey[] = {
200         'S','o','f','t','w','a','r','e','\\','W','i','n','e',
201         '\\','M','S','H','T','M','L',0};
202     static const WCHAR wszGeckoPath[] =
203         {'G','e','c','k','o','P','a','t','h',0};
204
205     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
206     res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
207     if(res != ERROR_SUCCESS)
208         return FALSE;
209
210     res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
211     if(res != ERROR_SUCCESS || type != REG_SZ)
212         return FALSE;
213
214     return TRUE;
215 }
216
217 static void set_profile(void)
218 {
219     nsIProfile *profile;
220     PRBool exists = FALSE;
221     nsresult nsres;
222
223     static const WCHAR wszMSHTML[] = {'M','S','H','T','M','L',0};
224
225     nsres = nsIServiceManager_GetServiceByContactID(pServMgr, NS_PROFILE_CONTRACTID,
226                                          &IID_nsIProfile, (void**)&profile);
227     if(NS_FAILED(nsres)) {
228         ERR("Could not get profile service: %08lx\n", nsres);
229         return;
230     }
231
232     nsres = nsIProfile_ProfileExists(profile, wszMSHTML, &exists);
233     if(!exists) {
234         nsres = nsIProfile_CreateNewProfile(profile, wszMSHTML, NULL, NULL, FALSE);
235         if(NS_FAILED(nsres))
236             ERR("CreateNewProfile failed: %08lx\n", nsres);
237     }
238
239     nsres = nsIProfile_SetCurrentProfile(profile, wszMSHTML);
240     if(NS_FAILED(nsres))
241         ERR("SetCurrentProfile failed: %08lx\n", nsres);
242
243     nsIProfile_Release(profile);
244 }
245
246 static BOOL load_gecko(void)
247 {
248     nsresult nsres;
249     nsIObserver *pStartNotif;
250     nsIComponentRegistrar *registrar = NULL;
251     nsAString path;
252     nsIFile *gre_dir;
253     PRUnichar gre_path[MAX_PATH];
254     WCHAR path_env[MAX_PATH];
255     int len;
256
257     static BOOL tried_load = FALSE;
258     static const WCHAR wszPATH[] = {'P','A','T','H',0};
259     static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
260
261     TRACE("()\n");
262
263     if(tried_load)
264         return pCompMgr != NULL;
265     tried_load = TRUE;
266
267     if(!get_wine_gecko_path(gre_path) && !get_mozctl_path(gre_path)
268        && !get_mozilla_path(gre_path)) {
269         install_wine_gecko();
270         if(!get_wine_gecko_path(gre_path)) {
271             MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
272             return FALSE;
273         }
274     }
275
276     TRACE("found path %s\n", debugstr_w(gre_path));
277
278     /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
279     GetEnvironmentVariableW(wszPATH, path_env, sizeof(path_env)/sizeof(WCHAR));
280     len = strlenW(path_env);
281     path_env[len++] = ';';
282     strcpyW(path_env+len, gre_path);
283     SetEnvironmentVariableW(wszPATH, path_env);
284
285     hXPCOM = LoadLibraryW(strXPCOM);
286     if(!hXPCOM) {
287         ERR("Could not load XPCOM: %ld\n", GetLastError());
288         return FALSE;
289     }
290
291 #define NS_DLSYM(func) \
292     func = (typeof(func))GetProcAddress(hXPCOM, #func); \
293     if(!func) \
294         ERR("Could not GetProcAddress(" #func ") failed\n")
295
296     NS_DLSYM(NS_InitXPCOM2);
297     NS_DLSYM(NS_ShutdownXPCOM);
298     NS_DLSYM(NS_GetComponentRegistrar);
299     NS_DLSYM(NS_StringContainerInit);
300     NS_DLSYM(NS_CStringContainerInit);
301     NS_DLSYM(NS_StringContainerFinish);
302     NS_DLSYM(NS_CStringContainerFinish);
303     NS_DLSYM(NS_StringSetData);
304     NS_DLSYM(NS_CStringSetData);
305     NS_DLSYM(NS_NewLocalFile);
306     NS_DLSYM(NS_StringGetData);
307     NS_DLSYM(NS_CStringGetData);
308
309 #undef NS_DLSYM
310
311     NS_StringContainerInit(&path);
312     NS_StringSetData(&path, gre_path, PR_UINT32_MAX);
313     nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
314     NS_StringContainerFinish(&path);
315     if(NS_FAILED(nsres)) {
316         ERR("NS_NewLocalFile failed: %08lx\n", nsres);
317         FreeLibrary(hXPCOM);
318         return FALSE;
319     }
320
321     nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
322     if(NS_FAILED(nsres)) {
323         ERR("NS_InitXPCOM2 failed: %08lx\n", nsres);
324         FreeLibrary(hXPCOM);
325         return FALSE;
326     }
327
328     nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
329     if(NS_FAILED(nsres))
330         ERR("Could not get nsIComponentManager: %08lx\n", nsres);
331
332     nsres = NS_GetComponentRegistrar(&registrar);
333     if(NS_SUCCEEDED(nsres)) {
334         nsres = nsIComponentRegistrar_AutoRegister(registrar, NULL);
335         if(NS_FAILED(nsres))
336             ERR("AutoRegister(NULL) failed: %08lx\n", nsres);
337
338         nsres = nsIComponentRegistrar_AutoRegister(registrar, gre_dir);
339         if(NS_FAILED(nsres))
340             ERR("AutoRegister(gre_dir) failed: %08lx\n", nsres);
341
342         init_nsio(pCompMgr, registrar);
343     }else {
344         ERR("NS_GetComponentRegistrar failed: %08lx\n", nsres);
345     }
346
347     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
348             NULL, &IID_nsIObserver, (void**)&pStartNotif);
349     if(NS_SUCCEEDED(nsres)) {
350         nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
351         if(NS_FAILED(nsres))
352             ERR("Observe failed: %08lx\n", nsres);
353
354         nsIObserver_Release(pStartNotif);
355     }else {
356         ERR("could not get appstartup-notifier: %08lx\n", nsres);
357     }
358
359     set_profile();
360
361     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_MEMORY_CONTRACTID,
362             NULL, &IID_nsIMemory, (void**)&nsmem);
363     if(NS_FAILED(nsres))
364         ERR("Could not get nsIMemory: %08lx\n", nsres);
365
366     if(registrar) {
367         register_nsservice(registrar, pServMgr);
368         nsIComponentRegistrar_Release(registrar);
369     }
370
371     return TRUE;
372 }
373
374 void *nsalloc(size_t size)
375 {
376     return nsIMemory_Alloc(nsmem, size);
377 }
378
379 void nsfree(void *mem)
380 {
381     nsIMemory_Free(nsmem, mem);
382 }
383
384 void nsACString_Init(nsACString *str, const char *data)
385 {
386     NS_CStringContainerInit(str);
387     if(data)
388         NS_CStringSetData(str, data, PR_UINT32_MAX);
389 }
390
391 PRUint32 nsACString_GetData(const nsACString *str, const char **data, PRBool *termited)
392 {
393     return NS_CStringGetData(str, data, termited);
394 }
395
396 void nsACString_Finish(nsACString *str)
397 {
398     NS_CStringContainerFinish(str);
399 }
400
401 void nsAString_Init(nsAString *str, const PRUnichar *data)
402 {
403     NS_StringContainerInit(str);
404     if(data)
405         NS_StringSetData(str, data, PR_UINT32_MAX);
406 }
407
408 PRUint32 nsAString_GetData(const nsAString *str, const PRUnichar **data, PRBool *termited)
409 {
410     return NS_StringGetData(str, data, termited);
411 }
412
413 void nsAString_Finish(nsAString *str)
414 {
415     NS_StringContainerFinish(str);
416 }
417
418 nsIInputStream *create_nsstream(const char *data, PRInt32 data_len)
419 {
420     nsIStringInputStream *ret;
421     nsresult nsres;
422
423     if(!pCompMgr)
424         return NULL;
425
426     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
427             NS_STRINGSTREAM_CONTRACTID, NULL, &IID_nsIStringInputStream,
428             (void**)&ret);
429     if(NS_FAILED(nsres)) {
430         ERR("Could not get nsIStringInputStream\n");
431         return NULL;
432     }
433
434     nsres = nsIStringInputStream_SetData(ret, data, data_len);
435     if(NS_FAILED(nsres)) {
436         ERR("AdoptData failed: %08lx\n", nsres);
437         nsIStringInputStream_Release(ret);
438         return NULL;
439     }
440
441     return (nsIInputStream*)ret;
442 }
443
444 void close_gecko()
445 {
446     TRACE("()\n");
447
448     if(pCompMgr)
449         nsIComponentManager_Release(pCompMgr);
450
451     if(pServMgr)
452         nsIServiceManager_Release(pServMgr);
453
454     if(nsmem)
455         nsIMemory_Release(nsmem);
456
457     if(hXPCOM)
458         FreeLibrary(hXPCOM);
459 }
460
461 /**********************************************************
462  *      nsIWebBrowserChrome interface
463  */
464
465 #define NSWBCHROME_THIS(iface) DEFINE_THIS(NSContainer, WebBrowserChrome, iface)
466
467 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
468         nsIIDRef riid, nsQIResult result)
469 {
470     NSContainer *This = NSWBCHROME_THIS(iface);
471
472     *result = NULL;
473     if(IsEqualGUID(&IID_nsISupports, riid)) {
474         TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
475         *result = NSWBCHROME(This);
476     }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
477         TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
478         *result = NSWBCHROME(This);
479     }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
480         TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
481         *result = NSCML(This);
482     }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
483         TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
484         *result = NSURICL(This);
485     }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
486         TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
487         *result = NSEMBWNDS(This);
488     }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
489         TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
490         *result = NSTOOLTIP(This);
491     }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
492         TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
493         *result = NSIFACEREQ(This);
494     }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
495         TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
496         *result = NSWEAKREF(This);
497     }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
498         TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
499         *result = NSSUPWEAKREF(This);
500     }
501
502     if(*result) {
503         nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
504         return NS_OK;
505     }
506
507     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
508     return NS_NOINTERFACE;
509 }
510
511 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
512 {
513     NSContainer *This = NSWBCHROME_THIS(iface);
514     LONG ref = InterlockedIncrement(&This->ref);
515
516     TRACE("(%p) ref=%ld\n", This, ref);
517
518     return ref;
519 }
520
521 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
522 {
523     NSContainer *This = NSWBCHROME_THIS(iface);
524     LONG ref = InterlockedDecrement(&This->ref);
525
526     TRACE("(%p) ref=%ld\n", This, ref);
527
528     if(!ref) {
529         if(This->parent)
530             nsIWebBrowserChrome_Release(NSWBCHROME(This->parent));
531         HeapFree(GetProcessHeap(), 0, This);
532     }
533
534     return ref;
535 }
536
537 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
538         PRUint32 statusType, const PRUnichar *status)
539 {
540     NSContainer *This = NSWBCHROME_THIS(iface);
541     TRACE("(%p)->(%ld %s)\n", This, statusType, debugstr_w(status));
542     return NS_ERROR_NOT_IMPLEMENTED;
543 }
544
545 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
546         nsIWebBrowser **aWebBrowser)
547 {
548     NSContainer *This = NSWBCHROME_THIS(iface);
549
550     TRACE("(%p)->(%p)\n", This, aWebBrowser);
551
552     if(!aWebBrowser)
553         return NS_ERROR_INVALID_ARG;
554
555     if(This->webbrowser)
556         nsIWebBrowser_AddRef(This->webbrowser);
557     *aWebBrowser = This->webbrowser;
558     return S_OK;
559 }
560
561 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
562         nsIWebBrowser *aWebBrowser)
563 {
564     NSContainer *This = NSWBCHROME_THIS(iface);
565
566     TRACE("(%p)->(%p)\n", This, aWebBrowser);
567
568     if(aWebBrowser != This->webbrowser)
569         ERR("Wrong nsWebBrowser!\n");
570
571     return NS_OK;
572 }
573
574 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
575         PRUint32 *aChromeFlags)
576 {
577     NSContainer *This = NSWBCHROME_THIS(iface);
578     WARN("(%p)->(%p)\n", This, aChromeFlags);
579     return NS_ERROR_NOT_IMPLEMENTED;
580 }
581
582 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
583         PRUint32 aChromeFlags)
584 {
585     NSContainer *This = NSWBCHROME_THIS(iface);
586     WARN("(%p)->(%08lx)\n", This, aChromeFlags);
587     return NS_ERROR_NOT_IMPLEMENTED;
588 }
589
590 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
591 {
592     NSContainer *This = NSWBCHROME_THIS(iface);
593     TRACE("(%p)\n", This);
594     return NS_ERROR_NOT_IMPLEMENTED;
595 }
596
597 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
598         PRInt32 aCX, PRInt32 aCY)
599 {
600     NSContainer *This = NSWBCHROME_THIS(iface);
601     WARN("(%p)->(%ld %ld)\n", This, aCX, aCY);
602     return NS_ERROR_NOT_IMPLEMENTED;
603 }
604
605 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
606 {
607     NSContainer *This = NSWBCHROME_THIS(iface);
608     WARN("(%p)\n", This);
609     return NS_ERROR_NOT_IMPLEMENTED;
610 }
611
612 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
613 {
614     NSContainer *This = NSWBCHROME_THIS(iface);
615     WARN("(%p)->(%p)\n", This, _retval);
616     return NS_ERROR_NOT_IMPLEMENTED;
617 }
618
619 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
620         nsresult aStatus)
621 {
622     NSContainer *This = NSWBCHROME_THIS(iface);
623     WARN("(%p)->(%08lx)\n", This, aStatus);
624     return NS_ERROR_NOT_IMPLEMENTED;
625 }
626
627 #undef NSWBCHROME_THIS
628
629 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
630     nsWebBrowserChrome_QueryInterface,
631     nsWebBrowserChrome_AddRef,
632     nsWebBrowserChrome_Release,
633     nsWebBrowserChrome_SetStatus,
634     nsWebBrowserChrome_GetWebBrowser,
635     nsWebBrowserChrome_SetWebBrowser,
636     nsWebBrowserChrome_GetChromeFlags,
637     nsWebBrowserChrome_SetChromeFlags,
638     nsWebBrowserChrome_DestroyBrowserWindow,
639     nsWebBrowserChrome_SizeBrowserTo,
640     nsWebBrowserChrome_ShowAsModal,
641     nsWebBrowserChrome_IsWindowModal,
642     nsWebBrowserChrome_ExitModalEventLoop
643 };
644
645 /**********************************************************
646  *      nsIContextMenuListener interface
647  */
648
649 #define NSCML_THIS(iface) DEFINE_THIS(NSContainer, ContextMenuListener, iface)
650
651 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
652         nsIIDRef riid, nsQIResult result)
653 {
654     NSContainer *This = NSCML_THIS(iface);
655     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
656 }
657
658 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
659 {
660     NSContainer *This = NSCML_THIS(iface);
661     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
662 }
663
664 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
665 {
666     NSContainer *This = NSCML_THIS(iface);
667     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
668 }
669
670 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
671         PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
672 {
673     NSContainer *This = NSCML_THIS(iface);
674     nsIDOMMouseEvent *event;
675     POINT pt;
676     DWORD dwID = CONTEXT_MENU_DEFAULT;
677     nsresult nsres;
678
679     TRACE("(%p)->(%08lx %p %p)\n", This, aContextFlags, aEvent, aNode);
680
681     nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
682     if(NS_FAILED(nsres)) {
683         ERR("Could not get nsIDOMMouseEvent interface: %08lx\n", nsres);
684         return nsres;
685     }
686
687     nsIDOMMouseEvent_GetScreenX(event, &pt.x);
688     nsIDOMMouseEvent_GetScreenY(event, &pt.y);
689     nsIDOMMouseEvent_Release(event);
690
691     switch(aContextFlags) {
692     case CONTEXT_NONE:
693     case CONTEXT_DOCUMENT:
694     case CONTEXT_TEXT:
695         dwID = CONTEXT_MENU_DEFAULT;
696         break;
697     case CONTEXT_IMAGE:
698     case CONTEXT_IMAGE|CONTEXT_LINK:
699         dwID = CONTEXT_MENU_IMAGE;
700         break;
701     case CONTEXT_LINK:
702         dwID = CONTEXT_MENU_ANCHOR;
703         break;
704     case CONTEXT_INPUT:
705         dwID = CONTEXT_MENU_CONTROL;
706         break;
707     default:
708         FIXME("aContextFlags=%08lx\n", aContextFlags);
709     };
710
711     HTMLDocument_ShowContextMenu(This->doc, dwID, &pt);
712
713     return NS_OK;
714 }
715
716 #undef NSCML_THIS
717
718 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
719     nsContextMenuListener_QueryInterface,
720     nsContextMenuListener_AddRef,
721     nsContextMenuListener_Release,
722     nsContextMenuListener_OnShowContextMenu
723 };
724
725 /**********************************************************
726  *      nsIURIContentListener interface
727  */
728
729 #define NSURICL_THIS(iface) DEFINE_THIS(NSContainer, URIContentListener, iface)
730
731 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
732         nsIIDRef riid, nsQIResult result)
733 {
734     NSContainer *This = NSURICL_THIS(iface);
735     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
736 }
737
738 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
739 {
740     NSContainer *This = NSURICL_THIS(iface);
741     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
742 }
743
744 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
745 {
746     NSContainer *This = NSURICL_THIS(iface);
747     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
748 }
749
750 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
751                                                           nsIURI *aURI, PRBool *_retval)
752 {
753     NSContainer *This = NSURICL_THIS(iface);
754     nsIWineURI *wine_uri;
755     nsACString spec_str;
756     const char *spec;
757     nsresult nsres;
758
759     nsACString_Init(&spec_str, NULL);
760     nsIURI_GetSpec(aURI, &spec_str);
761     nsACString_GetData(&spec_str, &spec, NULL);
762
763     TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
764
765     nsACString_Finish(&spec_str);
766
767     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
768     if(NS_SUCCEEDED(nsres)) {
769         nsIWineURI_SetNSContainer(wine_uri, This);
770         nsIWineURI_Release(wine_uri);
771     }else {
772         WARN("Could not get nsIWineURI interface: %08lx\n", nsres);
773     }
774
775     return NS_ERROR_NOT_IMPLEMENTED;
776 }
777
778 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
779         const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
780         nsIStreamListener **aContentHandler, PRBool *_retval)
781 {
782     NSContainer *This = NSURICL_THIS(iface);
783     TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
784             aRequest, aContentHandler, _retval);
785     return NS_ERROR_NOT_IMPLEMENTED;
786 }
787
788 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
789         const char *aContentType, char **aDesiredContentType, PRBool *_retval)
790 {
791     NSContainer *This = NSURICL_THIS(iface);
792
793     TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
794
795     /* FIXME: Should we do something here? */
796     *_retval = TRUE; 
797     return NS_OK;
798 }
799
800 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
801         const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
802         PRBool *_retval)
803 {
804     NSContainer *This = NSURICL_THIS(iface);
805     TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
806             aDesiredContentType, _retval);
807     return NS_ERROR_NOT_IMPLEMENTED;
808 }
809
810 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
811         nsISupports **aLoadCookie)
812 {
813     NSContainer *This = NSURICL_THIS(iface);
814     WARN("(%p)->(%p)\n", This, aLoadCookie);
815     return NS_ERROR_NOT_IMPLEMENTED;
816 }
817
818 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
819         nsISupports *aLoadCookie)
820 {
821     NSContainer *This = NSURICL_THIS(iface);
822     WARN("(%p)->(%p)\n", This, aLoadCookie);
823     return NS_ERROR_NOT_IMPLEMENTED;
824 }
825
826 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
827         nsIURIContentListener **aParentContentListener)
828 {
829     NSContainer *This = NSURICL_THIS(iface);
830     WARN("(%p)->(%p)\n", This, aParentContentListener);
831     return NS_ERROR_NOT_IMPLEMENTED;
832 }
833
834 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
835         nsIURIContentListener *aParentContentListener)
836 {
837     NSContainer *This = NSURICL_THIS(iface);
838     WARN("(%p)->(%p)\n", This, aParentContentListener);
839     return NS_ERROR_NOT_IMPLEMENTED;
840 }
841
842 #undef NSURICL_THIS
843
844 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
845     nsURIContentListener_QueryInterface,
846     nsURIContentListener_AddRef,
847     nsURIContentListener_Release,
848     nsURIContentListener_OnStartURIOpen,
849     nsURIContentListener_DoContent,
850     nsURIContentListener_IsPreferred,
851     nsURIContentListener_CanHandleContent,
852     nsURIContentListener_GetLoadCookie,
853     nsURIContentListener_SetLoadCookie,
854     nsURIContentListener_GetParentContentListener,
855     nsURIContentListener_SetParentContentListener
856 };
857
858 /**********************************************************
859  *      nsIEmbeddinSiteWindow interface
860  */
861
862 #define NSEMBWNDS_THIS(iface) DEFINE_THIS(NSContainer, EmbeddingSiteWindow, iface)
863
864 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
865         nsIIDRef riid, nsQIResult result)
866 {
867     NSContainer *This = NSEMBWNDS_THIS(iface);
868     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
869 }
870
871 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
872 {
873     NSContainer *This = NSEMBWNDS_THIS(iface);
874     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
875 }
876
877 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
878 {
879     NSContainer *This = NSEMBWNDS_THIS(iface);
880     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
881 }
882
883 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
884         PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
885 {
886     NSContainer *This = NSEMBWNDS_THIS(iface);
887     WARN("(%p)->(%08lx %ld %ld %ld %ld)\n", This, flags, x, y, cx, cy);
888     return NS_ERROR_NOT_IMPLEMENTED;
889 }
890
891 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
892         PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
893 {
894     NSContainer *This = NSEMBWNDS_THIS(iface);
895     WARN("(%p)->(%08lx %p %p %p %p)\n", This, flags, x, y, cx, cy);
896     return NS_ERROR_NOT_IMPLEMENTED;
897 }
898
899 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
900 {
901     NSContainer *This = NSEMBWNDS_THIS(iface);
902     WARN("(%p)\n", This);
903     return NS_ERROR_NOT_IMPLEMENTED;
904 }
905
906 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
907         PRBool *aVisibility)
908 {
909     NSContainer *This = NSEMBWNDS_THIS(iface);
910     WARN("(%p)->(%p)\n", This, aVisibility);
911     return NS_ERROR_NOT_IMPLEMENTED;
912 }
913
914 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
915         PRBool aVisibility)
916 {
917     NSContainer *This = NSEMBWNDS_THIS(iface);
918     WARN("(%p)->(%x)\n", This, aVisibility);
919     return NS_ERROR_NOT_IMPLEMENTED;
920 }
921
922 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
923         PRUnichar **aTitle)
924 {
925     NSContainer *This = NSEMBWNDS_THIS(iface);
926     WARN("(%p)->(%p)\n", This, aTitle);
927     return NS_ERROR_NOT_IMPLEMENTED;
928 }
929
930 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
931         const PRUnichar *aTitle)
932 {
933     NSContainer *This = NSEMBWNDS_THIS(iface);
934     WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
935     return NS_ERROR_NOT_IMPLEMENTED;
936 }
937
938 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
939         void **aSiteWindow)
940 {
941     NSContainer *This = NSEMBWNDS_THIS(iface);
942
943     TRACE("(%p)->(%p)\n", This, aSiteWindow);
944
945     *aSiteWindow = This->hwnd;
946     return NS_OK;
947 }
948
949 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
950     nsEmbeddingSiteWindow_QueryInterface,
951     nsEmbeddingSiteWindow_AddRef,
952     nsEmbeddingSiteWindow_Release,
953     nsEmbeddingSiteWindow_SetDimensions,
954     nsEmbeddingSiteWindow_GetDimensions,
955     nsEmbeddingSiteWindow_SetFocus,
956     nsEmbeddingSiteWindow_GetVisibility,
957     nsEmbeddingSiteWindow_SetVisibility,
958     nsEmbeddingSiteWindow_GetTitle,
959     nsEmbeddingSiteWindow_SetTitle,
960     nsEmbeddingSiteWindow_GetSiteWindow
961 };
962
963 #define NSTOOLTIP_THIS(iface) DEFINE_THIS(NSContainer, TooltipListener, iface)
964
965 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
966                                                        nsQIResult result)
967 {
968     NSContainer *This = NSTOOLTIP_THIS(iface);
969     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
970 }
971
972 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
973 {
974     NSContainer *This = NSTOOLTIP_THIS(iface);
975     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
976 }
977
978 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
979 {
980     NSContainer *This = NSTOOLTIP_THIS(iface);
981     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
982 }
983
984 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
985         PRInt32 aXCoord, PRInt32 aYCoord, const PRUnichar *aTipText)
986 {
987     NSContainer *This = NSTOOLTIP_THIS(iface);
988
989     show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
990
991     return NS_OK;
992 }
993
994 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
995 {
996     NSContainer *This = NSTOOLTIP_THIS(iface);
997
998     hide_tooltip(This->doc);
999
1000     return NS_OK;
1001 }
1002
1003 #undef NSTOOLTIM_THIS
1004
1005 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1006     nsTooltipListener_QueryInterface,
1007     nsTooltipListener_AddRef,
1008     nsTooltipListener_Release,
1009     nsTooltipListener_OnShowTooltip,
1010     nsTooltipListener_OnHideTooltip
1011 };
1012
1013 #define NSIFACEREQ_THIS(iface) DEFINE_THIS(NSContainer, InterfaceRequestor, iface)
1014
1015 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1016                                                           nsIIDRef riid, nsQIResult result)
1017 {
1018     NSContainer *This = NSIFACEREQ_THIS(iface);
1019     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1020 }
1021
1022 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1023 {
1024     NSContainer *This = NSIFACEREQ_THIS(iface);
1025     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1026 }
1027
1028 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1029 {
1030     NSContainer *This = NSIFACEREQ_THIS(iface);
1031     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1032 }
1033
1034 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1035                                                         nsIIDRef riid, nsQIResult result)
1036 {
1037     NSContainer *This = NSIFACEREQ_THIS(iface);
1038
1039     if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1040         TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1041         return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1042     }
1043
1044     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1045 }
1046
1047 #undef NSIFACEREQ_THIS
1048
1049 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1050     nsInterfaceRequestor_QueryInterface,
1051     nsInterfaceRequestor_AddRef,
1052     nsInterfaceRequestor_Release,
1053     nsInterfaceRequestor_GetInterface
1054 };
1055
1056 #define NSWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, WeakReference, iface)
1057
1058 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1059         nsIIDRef riid, nsQIResult result)
1060 {
1061     NSContainer *This = NSWEAKREF_THIS(iface);
1062     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1063 }
1064
1065 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1066 {
1067     NSContainer *This = NSWEAKREF_THIS(iface);
1068     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1069 }
1070
1071 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1072 {
1073     NSContainer *This = NSWEAKREF_THIS(iface);
1074     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1075 }
1076
1077 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1078         const nsIID *riid, void **result)
1079 {
1080     NSContainer *This = NSWEAKREF_THIS(iface);
1081     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1082 }
1083
1084 #undef NSWEAKREF_THIS
1085
1086 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1087     nsWeakReference_QueryInterface,
1088     nsWeakReference_AddRef,
1089     nsWeakReference_Release,
1090     nsWeakReference_QueryReferent
1091 };
1092
1093 #define NSSUPWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, SupportsWeakReference, iface)
1094
1095 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1096         nsIIDRef riid, nsQIResult result)
1097 {
1098     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1099     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1100 }
1101
1102 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1103 {
1104     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1105     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1106 }
1107
1108 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1109 {
1110     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1111     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1112 }
1113
1114 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1115         nsIWeakReference **_retval)
1116 {
1117     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1118
1119     TRACE("(%p)->(%p)\n", This, _retval);
1120
1121     nsIWeakReference_AddRef(NSWEAKREF(This));
1122     *_retval = NSWEAKREF(This);
1123     return NS_OK;
1124 }
1125
1126 #undef NSWEAKREF_THIS
1127
1128 const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1129     nsSupportsWeakReference_QueryInterface,
1130     nsSupportsWeakReference_AddRef,
1131     nsSupportsWeakReference_Release,
1132     nsSupportsWeakReference_GetWeakReference
1133 };
1134
1135
1136 NSContainer *NSContainer_Create(HTMLDocument *doc, NSContainer *parent)
1137 {
1138     nsIWebBrowserSetup *wbsetup;
1139     NSContainer *ret;
1140     nsresult nsres;
1141
1142     if(!load_gecko())
1143         return NULL;
1144
1145     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(NSContainer));
1146
1147     ret->lpWebBrowserChromeVtbl      = &nsWebBrowserChromeVtbl;
1148     ret->lpContextMenuListenerVtbl   = &nsContextMenuListenerVtbl;
1149     ret->lpURIContentListenerVtbl    = &nsURIContentListenerVtbl;
1150     ret->lpEmbeddingSiteWindowVtbl   = &nsEmbeddingSiteWindowVtbl;
1151     ret->lpTooltipListenerVtbl       = &nsTooltipListenerVtbl;
1152     ret->lpInterfaceRequestorVtbl    = &nsInterfaceRequestorVtbl;
1153     ret->lpWeakReferenceVtbl         = &nsWeakReferenceVtbl;
1154     ret->lpSupportsWeakReferenceVtbl = &nsSupportsWeakReferenceVtbl;
1155
1156
1157     ret->doc = doc;
1158     ret->ref = 1;
1159     ret->bscallback = NULL;
1160
1161     if(parent)
1162         nsIWebBrowserChrome_AddRef(NSWBCHROME(parent));
1163     ret->parent = parent;
1164
1165     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1166             NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
1167     if(NS_FAILED(nsres))
1168         ERR("Creating WebBrowser failed: %08lx\n", nsres);
1169
1170     nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, NSWBCHROME(ret));
1171     if(NS_FAILED(nsres))
1172         ERR("SetContainerWindow failed: %08lx\n", nsres);
1173
1174     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
1175             (void**)&ret->window);
1176     if(NS_FAILED(nsres))
1177         ERR("Could not get nsIBaseWindow interface: %08lx\n", nsres);
1178
1179     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
1180                                          (void**)&wbsetup);
1181     if(NS_SUCCEEDED(nsres)) {
1182         nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, TRUE);
1183         nsIWebBrowserSetup_Release(wbsetup);
1184         if(NS_FAILED(nsres))
1185             ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08lx\n", nsres);
1186     }else {
1187         ERR("Could not get nsIWebBrowserSetup interface\n");
1188     }
1189
1190     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
1191             (void**)&ret->navigation);
1192     if(NS_FAILED(nsres))
1193         ERR("Could not get nsIWebNavigation interface: %08lx\n", nsres);
1194
1195     nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
1196             (void**)&ret->focus);
1197     if(NS_FAILED(nsres))
1198         ERR("Could not get nsIWebBrowserFocus interface: %08lx\n", nsres);
1199
1200     if(!nscontainer_class)
1201         register_nscontainer_class();
1202
1203     ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1204             WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1205             GetDesktopWindow(), NULL, hInst, ret);
1206
1207     nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
1208     if(NS_SUCCEEDED(nsres)) {
1209         nsres = nsIBaseWindow_Create(ret->window);
1210         if(NS_FAILED(nsres))
1211             WARN("Creating window failed: %08lx\n", nsres);
1212
1213         nsIBaseWindow_SetVisibility(ret->window, FALSE);
1214         nsIBaseWindow_SetEnabled(ret->window, FALSE);
1215     }else {
1216         ERR("InitWindow failed: %08lx\n", nsres);
1217     }
1218
1219     nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser, NSURICL(ret));
1220     if(NS_FAILED(nsres))
1221         ERR("SetParentURIContentListener failed: %08lx\n", nsres);
1222
1223     return ret;
1224 }
1225
1226 void NSContainer_Release(NSContainer *This)
1227 {
1228     TRACE("(%p)\n", This);
1229
1230     ShowWindow(This->hwnd, SW_HIDE);
1231     SetParent(This->hwnd, NULL);
1232
1233     nsIBaseWindow_SetVisibility(This->window, FALSE);
1234     nsIBaseWindow_Destroy(This->window);
1235
1236     nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1237
1238     nsIWebBrowser_Release(This->webbrowser);
1239     This->webbrowser = NULL;
1240
1241     nsIWebNavigation_Release(This->navigation);
1242     This->navigation = NULL;
1243
1244     nsIBaseWindow_Release(This->window);
1245     This->window = NULL;
1246
1247     nsIWebBrowserFocus_Release(This->focus);
1248     This->focus = NULL;
1249
1250     if(This->hwnd) {
1251         DestroyWindow(This->hwnd);
1252         This->hwnd = NULL;
1253     }
1254
1255     nsIWebBrowserChrome_Release(NSWBCHROME(This));
1256 }