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