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