msi: Name all or none of the formal arguments in function prototypes.
[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_GetServiceByContactID(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 static 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 static void handle_load_event(NSContainer *This, nsIDOMEvent *event)
693 {
694     task_t *task;
695
696     TRACE("(%p)\n", This);
697
698     if(!This->doc)
699         return;
700
701     if(This->editor_controller) {
702         nsIController_Release(This->editor_controller);
703         This->editor_controller = NULL;
704     }
705
706     if(This->doc->usermode == EDITMODE)
707         This->editor_controller = get_editor_controller(This);
708
709     task = mshtml_alloc(sizeof(task_t));
710
711     task->doc = This->doc;
712     task->task_id = TASK_PARSECOMPLETE;
713     task->next = NULL;
714
715     /*
716      * This should be done in the worker thread that parses HTML,
717      * but we don't have such thread (Gecko parses HTML for us).
718      */
719     push_task(task);
720 }
721
722 void close_gecko(void)
723 {
724     TRACE("()\n");
725
726     if(pCompMgr)
727         nsIComponentManager_Release(pCompMgr);
728
729     if(pServMgr)
730         nsIServiceManager_Release(pServMgr);
731
732     if(nsmem)
733         nsIMemory_Release(nsmem);
734
735     if(hXPCOM)
736         FreeLibrary(hXPCOM);
737 }
738
739 /**********************************************************
740  *      nsIWebBrowserChrome interface
741  */
742
743 #define NSWBCHROME_THIS(iface) DEFINE_THIS(NSContainer, WebBrowserChrome, iface)
744
745 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
746         nsIIDRef riid, nsQIResult result)
747 {
748     NSContainer *This = NSWBCHROME_THIS(iface);
749
750     *result = NULL;
751     if(IsEqualGUID(&IID_nsISupports, riid)) {
752         TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
753         *result = NSWBCHROME(This);
754     }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
755         TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
756         *result = NSWBCHROME(This);
757     }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
758         TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
759         *result = NSCML(This);
760     }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
761         TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
762         *result = NSURICL(This);
763     }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
764         TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
765         *result = NSEMBWNDS(This);
766     }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
767         TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
768         *result = NSTOOLTIP(This);
769     }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
770         TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
771         *result = NSEVENTLIST(This);
772     }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
773         TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
774         *result = NSIFACEREQ(This);
775     }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
776         TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
777         *result = NSWEAKREF(This);
778     }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
779         TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
780         *result = NSSUPWEAKREF(This);
781     }
782
783     if(*result) {
784         nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
785         return NS_OK;
786     }
787
788     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
789     return NS_NOINTERFACE;
790 }
791
792 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
793 {
794     NSContainer *This = NSWBCHROME_THIS(iface);
795     LONG ref = InterlockedIncrement(&This->ref);
796
797     TRACE("(%p) ref=%d\n", This, ref);
798
799     return ref;
800 }
801
802 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
803 {
804     NSContainer *This = NSWBCHROME_THIS(iface);
805     LONG ref = InterlockedDecrement(&This->ref);
806
807     TRACE("(%p) ref=%d\n", This, ref);
808
809     if(!ref) {
810         if(This->parent)
811             nsIWebBrowserChrome_Release(NSWBCHROME(This->parent));
812         mshtml_free(This);
813     }
814
815     return ref;
816 }
817
818 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
819         PRUint32 statusType, const PRUnichar *status)
820 {
821     NSContainer *This = NSWBCHROME_THIS(iface);
822     TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
823     return NS_ERROR_NOT_IMPLEMENTED;
824 }
825
826 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
827         nsIWebBrowser **aWebBrowser)
828 {
829     NSContainer *This = NSWBCHROME_THIS(iface);
830
831     TRACE("(%p)->(%p)\n", This, aWebBrowser);
832
833     if(!aWebBrowser)
834         return NS_ERROR_INVALID_ARG;
835
836     if(This->webbrowser)
837         nsIWebBrowser_AddRef(This->webbrowser);
838     *aWebBrowser = This->webbrowser;
839     return S_OK;
840 }
841
842 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
843         nsIWebBrowser *aWebBrowser)
844 {
845     NSContainer *This = NSWBCHROME_THIS(iface);
846
847     TRACE("(%p)->(%p)\n", This, aWebBrowser);
848
849     if(aWebBrowser != This->webbrowser)
850         ERR("Wrong nsWebBrowser!\n");
851
852     return NS_OK;
853 }
854
855 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
856         PRUint32 *aChromeFlags)
857 {
858     NSContainer *This = NSWBCHROME_THIS(iface);
859     WARN("(%p)->(%p)\n", This, aChromeFlags);
860     return NS_ERROR_NOT_IMPLEMENTED;
861 }
862
863 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
864         PRUint32 aChromeFlags)
865 {
866     NSContainer *This = NSWBCHROME_THIS(iface);
867     WARN("(%p)->(%08x)\n", This, aChromeFlags);
868     return NS_ERROR_NOT_IMPLEMENTED;
869 }
870
871 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
872 {
873     NSContainer *This = NSWBCHROME_THIS(iface);
874     TRACE("(%p)\n", This);
875     return NS_ERROR_NOT_IMPLEMENTED;
876 }
877
878 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
879         PRInt32 aCX, PRInt32 aCY)
880 {
881     NSContainer *This = NSWBCHROME_THIS(iface);
882     WARN("(%p)->(%d %d)\n", This, aCX, aCY);
883     return NS_ERROR_NOT_IMPLEMENTED;
884 }
885
886 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
887 {
888     NSContainer *This = NSWBCHROME_THIS(iface);
889     WARN("(%p)\n", This);
890     return NS_ERROR_NOT_IMPLEMENTED;
891 }
892
893 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
894 {
895     NSContainer *This = NSWBCHROME_THIS(iface);
896     WARN("(%p)->(%p)\n", This, _retval);
897     return NS_ERROR_NOT_IMPLEMENTED;
898 }
899
900 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
901         nsresult aStatus)
902 {
903     NSContainer *This = NSWBCHROME_THIS(iface);
904     WARN("(%p)->(%08x)\n", This, aStatus);
905     return NS_ERROR_NOT_IMPLEMENTED;
906 }
907
908 #undef NSWBCHROME_THIS
909
910 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
911     nsWebBrowserChrome_QueryInterface,
912     nsWebBrowserChrome_AddRef,
913     nsWebBrowserChrome_Release,
914     nsWebBrowserChrome_SetStatus,
915     nsWebBrowserChrome_GetWebBrowser,
916     nsWebBrowserChrome_SetWebBrowser,
917     nsWebBrowserChrome_GetChromeFlags,
918     nsWebBrowserChrome_SetChromeFlags,
919     nsWebBrowserChrome_DestroyBrowserWindow,
920     nsWebBrowserChrome_SizeBrowserTo,
921     nsWebBrowserChrome_ShowAsModal,
922     nsWebBrowserChrome_IsWindowModal,
923     nsWebBrowserChrome_ExitModalEventLoop
924 };
925
926 /**********************************************************
927  *      nsIContextMenuListener interface
928  */
929
930 #define NSCML_THIS(iface) DEFINE_THIS(NSContainer, ContextMenuListener, iface)
931
932 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
933         nsIIDRef riid, nsQIResult result)
934 {
935     NSContainer *This = NSCML_THIS(iface);
936     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
937 }
938
939 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
940 {
941     NSContainer *This = NSCML_THIS(iface);
942     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
943 }
944
945 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
946 {
947     NSContainer *This = NSCML_THIS(iface);
948     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
949 }
950
951 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
952         PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
953 {
954     NSContainer *This = NSCML_THIS(iface);
955     nsIDOMMouseEvent *event;
956     POINT pt;
957     DWORD dwID = CONTEXT_MENU_DEFAULT;
958     nsresult nsres;
959
960     TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
961
962     nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
963     if(NS_FAILED(nsres)) {
964         ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
965         return nsres;
966     }
967
968     nsIDOMMouseEvent_GetScreenX(event, &pt.x);
969     nsIDOMMouseEvent_GetScreenY(event, &pt.y);
970     nsIDOMMouseEvent_Release(event);
971
972     switch(aContextFlags) {
973     case CONTEXT_NONE:
974     case CONTEXT_DOCUMENT:
975     case CONTEXT_TEXT:
976         dwID = CONTEXT_MENU_DEFAULT;
977         break;
978     case CONTEXT_IMAGE:
979     case CONTEXT_IMAGE|CONTEXT_LINK:
980         dwID = CONTEXT_MENU_IMAGE;
981         break;
982     case CONTEXT_LINK:
983         dwID = CONTEXT_MENU_ANCHOR;
984         break;
985     case CONTEXT_INPUT:
986         dwID = CONTEXT_MENU_CONTROL;
987         break;
988     default:
989         FIXME("aContextFlags=%08x\n", aContextFlags);
990     };
991
992     show_context_menu(This->doc, dwID, &pt);
993
994     return NS_OK;
995 }
996
997 #undef NSCML_THIS
998
999 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1000     nsContextMenuListener_QueryInterface,
1001     nsContextMenuListener_AddRef,
1002     nsContextMenuListener_Release,
1003     nsContextMenuListener_OnShowContextMenu
1004 };
1005
1006 /**********************************************************
1007  *      nsIURIContentListener interface
1008  */
1009
1010 #define NSURICL_THIS(iface) DEFINE_THIS(NSContainer, URIContentListener, iface)
1011
1012 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1013         nsIIDRef riid, nsQIResult result)
1014 {
1015     NSContainer *This = NSURICL_THIS(iface);
1016     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1017 }
1018
1019 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1020 {
1021     NSContainer *This = NSURICL_THIS(iface);
1022     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1023 }
1024
1025 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1026 {
1027     NSContainer *This = NSURICL_THIS(iface);
1028     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1029 }
1030
1031 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1032                                                           nsIURI *aURI, PRBool *_retval)
1033 {
1034     NSContainer *This = NSURICL_THIS(iface);
1035     nsIWineURI *wine_uri;
1036     nsACString spec_str;
1037     const char *spec;
1038     nsresult nsres;
1039
1040     nsACString_Init(&spec_str, NULL);
1041     nsIURI_GetSpec(aURI, &spec_str);
1042     nsACString_GetData(&spec_str, &spec, NULL);
1043
1044     TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1045
1046     nsACString_Finish(&spec_str);
1047
1048     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
1049     if(NS_FAILED(nsres)) {
1050         WARN("Could not get nsIWineURI interface: %08x\n", nsres);
1051         return NS_ERROR_NOT_IMPLEMENTED;
1052     }
1053
1054     nsIWineURI_SetNSContainer(wine_uri, This);
1055     nsIWineURI_SetIsDocumentURI(wine_uri, TRUE);
1056
1057     if(This->bscallback && This->bscallback->mon) {
1058         LPWSTR wine_url;
1059         HRESULT hres;
1060
1061         hres = IMoniker_GetDisplayName(This->bscallback->mon, NULL, 0, &wine_url);
1062         if(SUCCEEDED(hres)) {
1063             nsIWineURI_SetWineURL(wine_uri, wine_url);
1064             CoTaskMemFree(wine_url);
1065         }else {
1066             WARN("GetDisplayName failed: %08x\n", hres);
1067         }
1068     }
1069
1070     nsIWineURI_Release(wine_uri);
1071
1072     *_retval = FALSE;
1073     return This->content_listener
1074         ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1075         : NS_OK;
1076 }
1077
1078 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1079         const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
1080         nsIStreamListener **aContentHandler, PRBool *_retval)
1081 {
1082     NSContainer *This = NSURICL_THIS(iface);
1083
1084     TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1085             aRequest, aContentHandler, _retval);
1086
1087     return This->content_listener
1088         ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1089                   aIsContentPreferred, aRequest, aContentHandler, _retval)
1090         : NS_ERROR_NOT_IMPLEMENTED;
1091 }
1092
1093 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1094         const char *aContentType, char **aDesiredContentType, PRBool *_retval)
1095 {
1096     NSContainer *This = NSURICL_THIS(iface);
1097
1098     TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1099
1100     /* FIXME: Should we do something here? */
1101     *_retval = TRUE; 
1102
1103     return This->content_listener
1104         ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1105                   aDesiredContentType, _retval)
1106         : NS_OK;
1107 }
1108
1109 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1110         const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
1111         PRBool *_retval)
1112 {
1113     NSContainer *This = NSURICL_THIS(iface);
1114
1115     TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1116             aDesiredContentType, _retval);
1117
1118     return This->content_listener
1119         ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1120                 aIsContentPreferred, aDesiredContentType, _retval)
1121         : NS_ERROR_NOT_IMPLEMENTED;
1122 }
1123
1124 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1125         nsISupports **aLoadCookie)
1126 {
1127     NSContainer *This = NSURICL_THIS(iface);
1128
1129     WARN("(%p)->(%p)\n", This, aLoadCookie);
1130
1131     return This->content_listener
1132         ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1133         : NS_ERROR_NOT_IMPLEMENTED;
1134 }
1135
1136 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1137         nsISupports *aLoadCookie)
1138 {
1139     NSContainer *This = NSURICL_THIS(iface);
1140
1141     WARN("(%p)->(%p)\n", This, aLoadCookie);
1142
1143     return This->content_listener
1144         ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1145         : NS_ERROR_NOT_IMPLEMENTED;
1146 }
1147
1148 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1149         nsIURIContentListener **aParentContentListener)
1150 {
1151     NSContainer *This = NSURICL_THIS(iface);
1152
1153     TRACE("(%p)->(%p)\n", This, aParentContentListener);
1154
1155     if(This->content_listener)
1156         nsIURIContentListener_AddRef(This->content_listener);
1157
1158     *aParentContentListener = This->content_listener;
1159     return NS_OK;
1160 }
1161
1162 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1163         nsIURIContentListener *aParentContentListener)
1164 {
1165     NSContainer *This = NSURICL_THIS(iface);
1166
1167     TRACE("(%p)->(%p)\n", This, aParentContentListener);
1168
1169     if(aParentContentListener == NSURICL(This))
1170         return NS_OK;
1171
1172     if(This->content_listener)
1173         nsIURIContentListener_Release(This->content_listener);
1174
1175     This->content_listener = aParentContentListener;
1176     if(This->content_listener)
1177         nsIURIContentListener_AddRef(This->content_listener);
1178
1179     return NS_OK;
1180 }
1181
1182 #undef NSURICL_THIS
1183
1184 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1185     nsURIContentListener_QueryInterface,
1186     nsURIContentListener_AddRef,
1187     nsURIContentListener_Release,
1188     nsURIContentListener_OnStartURIOpen,
1189     nsURIContentListener_DoContent,
1190     nsURIContentListener_IsPreferred,
1191     nsURIContentListener_CanHandleContent,
1192     nsURIContentListener_GetLoadCookie,
1193     nsURIContentListener_SetLoadCookie,
1194     nsURIContentListener_GetParentContentListener,
1195     nsURIContentListener_SetParentContentListener
1196 };
1197
1198 /**********************************************************
1199  *      nsIEmbeddinSiteWindow interface
1200  */
1201
1202 #define NSEMBWNDS_THIS(iface) DEFINE_THIS(NSContainer, EmbeddingSiteWindow, iface)
1203
1204 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1205         nsIIDRef riid, nsQIResult result)
1206 {
1207     NSContainer *This = NSEMBWNDS_THIS(iface);
1208     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1209 }
1210
1211 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1212 {
1213     NSContainer *This = NSEMBWNDS_THIS(iface);
1214     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1215 }
1216
1217 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1218 {
1219     NSContainer *This = NSEMBWNDS_THIS(iface);
1220     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1221 }
1222
1223 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1224         PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
1225 {
1226     NSContainer *This = NSEMBWNDS_THIS(iface);
1227     WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1228     return NS_ERROR_NOT_IMPLEMENTED;
1229 }
1230
1231 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1232         PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
1233 {
1234     NSContainer *This = NSEMBWNDS_THIS(iface);
1235     WARN("(%p)->(%08x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1236     return NS_ERROR_NOT_IMPLEMENTED;
1237 }
1238
1239 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1240 {
1241     NSContainer *This = NSEMBWNDS_THIS(iface);
1242     WARN("(%p)\n", This);
1243     return NS_ERROR_NOT_IMPLEMENTED;
1244 }
1245
1246 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1247         PRBool *aVisibility)
1248 {
1249     NSContainer *This = NSEMBWNDS_THIS(iface);
1250     WARN("(%p)->(%p)\n", This, aVisibility);
1251     return NS_ERROR_NOT_IMPLEMENTED;
1252 }
1253
1254 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1255         PRBool aVisibility)
1256 {
1257     NSContainer *This = NSEMBWNDS_THIS(iface);
1258     WARN("(%p)->(%x)\n", This, aVisibility);
1259     return NS_ERROR_NOT_IMPLEMENTED;
1260 }
1261
1262 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1263         PRUnichar **aTitle)
1264 {
1265     NSContainer *This = NSEMBWNDS_THIS(iface);
1266     WARN("(%p)->(%p)\n", This, aTitle);
1267     return NS_ERROR_NOT_IMPLEMENTED;
1268 }
1269
1270 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1271         const PRUnichar *aTitle)
1272 {
1273     NSContainer *This = NSEMBWNDS_THIS(iface);
1274     WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1275     return NS_ERROR_NOT_IMPLEMENTED;
1276 }
1277
1278 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1279         void **aSiteWindow)
1280 {
1281     NSContainer *This = NSEMBWNDS_THIS(iface);
1282
1283     TRACE("(%p)->(%p)\n", This, aSiteWindow);
1284
1285     *aSiteWindow = This->hwnd;
1286     return NS_OK;
1287 }
1288
1289 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1290     nsEmbeddingSiteWindow_QueryInterface,
1291     nsEmbeddingSiteWindow_AddRef,
1292     nsEmbeddingSiteWindow_Release,
1293     nsEmbeddingSiteWindow_SetDimensions,
1294     nsEmbeddingSiteWindow_GetDimensions,
1295     nsEmbeddingSiteWindow_SetFocus,
1296     nsEmbeddingSiteWindow_GetVisibility,
1297     nsEmbeddingSiteWindow_SetVisibility,
1298     nsEmbeddingSiteWindow_GetTitle,
1299     nsEmbeddingSiteWindow_SetTitle,
1300     nsEmbeddingSiteWindow_GetSiteWindow
1301 };
1302
1303 #define NSTOOLTIP_THIS(iface) DEFINE_THIS(NSContainer, TooltipListener, iface)
1304
1305 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1306                                                        nsQIResult result)
1307 {
1308     NSContainer *This = NSTOOLTIP_THIS(iface);
1309     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1310 }
1311
1312 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1313 {
1314     NSContainer *This = NSTOOLTIP_THIS(iface);
1315     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1316 }
1317
1318 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1319 {
1320     NSContainer *This = NSTOOLTIP_THIS(iface);
1321     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1322 }
1323
1324 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1325         PRInt32 aXCoord, PRInt32 aYCoord, const PRUnichar *aTipText)
1326 {
1327     NSContainer *This = NSTOOLTIP_THIS(iface);
1328
1329     show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1330
1331     return NS_OK;
1332 }
1333
1334 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1335 {
1336     NSContainer *This = NSTOOLTIP_THIS(iface);
1337
1338     hide_tooltip(This->doc);
1339
1340     return NS_OK;
1341 }
1342
1343 #undef NSTOOLTIM_THIS
1344
1345 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1346     nsTooltipListener_QueryInterface,
1347     nsTooltipListener_AddRef,
1348     nsTooltipListener_Release,
1349     nsTooltipListener_OnShowTooltip,
1350     nsTooltipListener_OnHideTooltip
1351 };
1352
1353 #define NSEVENTLIST_THIS(iface) DEFINE_THIS(NSContainer, DOMEventListener, iface)
1354
1355 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
1356                                                         nsIIDRef riid, nsQIResult result)
1357 {
1358     NSContainer *This = NSEVENTLIST_THIS(iface);
1359     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1360 }
1361
1362 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
1363 {
1364     NSContainer *This = NSEVENTLIST_THIS(iface);
1365     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1366 }
1367
1368 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
1369 {
1370     NSContainer *This = NSEVENTLIST_THIS(iface);
1371     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1372 }
1373
1374 static nsresult NSAPI nsDOMEventListener_HandleEvent(nsIDOMEventListener *iface, nsIDOMEvent *event)
1375 {
1376     NSContainer *This = NSEVENTLIST_THIS(iface);
1377     nsAString type_str;
1378     const PRUnichar *type;
1379
1380     static const PRUnichar loadW[] = {'l','o','a','d',0};
1381
1382     nsAString_Init(&type_str, NULL);
1383     nsIDOMEvent_GetType(event, &type_str);
1384     nsAString_GetData(&type_str, &type, NULL);
1385
1386     TRACE("(%p)->(%p) %s\n", This, event, debugstr_w(type));
1387
1388     if(!strcmpW(loadW, type)) {
1389         handle_load_event(This, event);
1390     }else if(This->doc) {
1391         update_doc(This->doc, UPDATE_UI);
1392         if(This->doc->usermode == EDITMODE)
1393             handle_edit_event(This->doc, event);
1394     }
1395
1396     nsAString_Finish(&type_str);
1397
1398     return NS_OK;
1399 }
1400
1401 #undef NSEVENTLIST_THIS
1402
1403 static const nsIDOMEventListenerVtbl nsDOMEventListenerVtbl = {
1404     nsDOMEventListener_QueryInterface,
1405     nsDOMEventListener_AddRef,
1406     nsDOMEventListener_Release,
1407     nsDOMEventListener_HandleEvent
1408 };
1409
1410 #define NSIFACEREQ_THIS(iface) DEFINE_THIS(NSContainer, InterfaceRequestor, iface)
1411
1412 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1413                                                           nsIIDRef riid, nsQIResult result)
1414 {
1415     NSContainer *This = NSIFACEREQ_THIS(iface);
1416     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1417 }
1418
1419 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1420 {
1421     NSContainer *This = NSIFACEREQ_THIS(iface);
1422     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1423 }
1424
1425 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1426 {
1427     NSContainer *This = NSIFACEREQ_THIS(iface);
1428     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1429 }
1430
1431 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1432                                                         nsIIDRef riid, nsQIResult result)
1433 {
1434     NSContainer *This = NSIFACEREQ_THIS(iface);
1435
1436     if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1437         TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1438         return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1439     }
1440
1441     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1442 }
1443
1444 #undef NSIFACEREQ_THIS
1445
1446 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1447     nsInterfaceRequestor_QueryInterface,
1448     nsInterfaceRequestor_AddRef,
1449     nsInterfaceRequestor_Release,
1450     nsInterfaceRequestor_GetInterface
1451 };
1452
1453 #define NSWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, WeakReference, iface)
1454
1455 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1456         nsIIDRef riid, nsQIResult result)
1457 {
1458     NSContainer *This = NSWEAKREF_THIS(iface);
1459     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1460 }
1461
1462 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1463 {
1464     NSContainer *This = NSWEAKREF_THIS(iface);
1465     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1466 }
1467
1468 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1469 {
1470     NSContainer *This = NSWEAKREF_THIS(iface);
1471     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1472 }
1473
1474 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1475         const nsIID *riid, void **result)
1476 {
1477     NSContainer *This = NSWEAKREF_THIS(iface);
1478     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1479 }
1480
1481 #undef NSWEAKREF_THIS
1482
1483 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1484     nsWeakReference_QueryInterface,
1485     nsWeakReference_AddRef,
1486     nsWeakReference_Release,
1487     nsWeakReference_QueryReferent
1488 };
1489
1490 #define NSSUPWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, SupportsWeakReference, iface)
1491
1492 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1493         nsIIDRef riid, nsQIResult result)
1494 {
1495     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1496     return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1497 }
1498
1499 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1500 {
1501     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1502     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1503 }
1504
1505 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1506 {
1507     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1508     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1509 }
1510
1511 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1512         nsIWeakReference **_retval)
1513 {
1514     NSContainer *This = NSSUPWEAKREF_THIS(iface);
1515
1516     TRACE("(%p)->(%p)\n", This, _retval);
1517
1518     nsIWeakReference_AddRef(NSWEAKREF(This));
1519     *_retval = NSWEAKREF(This);
1520     return NS_OK;
1521 }
1522
1523 #undef NSWEAKREF_THIS
1524
1525 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1526     nsSupportsWeakReference_QueryInterface,
1527     nsSupportsWeakReference_AddRef,
1528     nsSupportsWeakReference_Release,
1529     nsSupportsWeakReference_GetWeakReference
1530 };
1531
1532
1533 NSContainer *NSContainer_Create(HTMLDocument *doc, NSContainer *parent)
1534 {
1535     nsIDOMWindow *dom_window;
1536     nsIWebBrowserSetup *wbsetup;
1537     nsIScrollable *scrollable;
1538     NSContainer *ret;
1539     nsresult nsres;
1540
1541     if(!load_gecko())
1542         return NULL;
1543
1544     ret = mshtml_alloc(sizeof(NSContainer));
1545
1546     ret->lpWebBrowserChromeVtbl      = &nsWebBrowserChromeVtbl;
1547     ret->lpContextMenuListenerVtbl   = &nsContextMenuListenerVtbl;
1548     ret->lpURIContentListenerVtbl    = &nsURIContentListenerVtbl;
1549     ret->lpEmbeddingSiteWindowVtbl   = &nsEmbeddingSiteWindowVtbl;
1550     ret->lpTooltipListenerVtbl       = &nsTooltipListenerVtbl;
1551     ret->lpInterfaceRequestorVtbl    = &nsInterfaceRequestorVtbl;
1552     ret->lpWeakReferenceVtbl         = &nsWeakReferenceVtbl;
1553     ret->lpSupportsWeakReferenceVtbl = &nsSupportsWeakReferenceVtbl;
1554     ret->lpDOMEventListenerVtbl      = &nsDOMEventListenerVtbl;
1555
1556     ret->doc = doc;
1557     ret->ref = 1;
1558     ret->bscallback = NULL;
1559     ret->content_listener = NULL;
1560     ret->editor_controller = NULL;
1561
1562     if(parent)
1563         nsIWebBrowserChrome_AddRef(NSWBCHROME(parent));
1564     ret->parent = parent;
1565
1566     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1567             NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
1568     if(NS_FAILED(nsres))
1569         ERR("Creating WebBrowser failed: %08x\n", nsres);
1570
1571     nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, NSWBCHROME(ret));
1572     if(NS_FAILED(nsres))
1573         ERR("SetContainerWindow failed: %08x\n", nsres);
1574
1575     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
1576             (void**)&ret->window);
1577     if(NS_FAILED(nsres))
1578         ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1579
1580     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
1581                                          (void**)&wbsetup);
1582     if(NS_SUCCEEDED(nsres)) {
1583         nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, TRUE);
1584         nsIWebBrowserSetup_Release(wbsetup);
1585         if(NS_FAILED(nsres))
1586             ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
1587     }else {
1588         ERR("Could not get nsIWebBrowserSetup interface\n");
1589     }
1590
1591     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
1592             (void**)&ret->navigation);
1593     if(NS_FAILED(nsres))
1594         ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1595
1596     nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
1597             (void**)&ret->focus);
1598     if(NS_FAILED(nsres))
1599         ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
1600
1601     if(!nscontainer_class)
1602         register_nscontainer_class();
1603
1604     ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1605             WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1606             GetDesktopWindow(), NULL, hInst, ret);
1607
1608     nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
1609     if(NS_SUCCEEDED(nsres)) {
1610         nsres = nsIBaseWindow_Create(ret->window);
1611         if(NS_FAILED(nsres))
1612             WARN("Creating window failed: %08x\n", nsres);
1613
1614         nsIBaseWindow_SetVisibility(ret->window, FALSE);
1615         nsIBaseWindow_SetEnabled(ret->window, FALSE);
1616     }else {
1617         ERR("InitWindow failed: %08x\n", nsres);
1618     }
1619
1620     nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser, NSURICL(ret));
1621     if(NS_FAILED(nsres))
1622         ERR("SetParentURIContentListener failed: %08x\n", nsres);
1623
1624     nsres = nsIWebBrowser_GetContentDOMWindow(ret->webbrowser, &dom_window);
1625     if(NS_SUCCEEDED(nsres)) {
1626         nsIDOMEventTarget *target;
1627         nsres = nsIDOMWindow_QueryInterface(dom_window, &IID_nsIDOMEventTarget, (void**)&target);
1628         nsIDOMWindow_Release(dom_window);
1629         if(NS_SUCCEEDED(nsres)) {
1630             nsAString keypress_str, load_str;
1631             static const PRUnichar wsz_keypress[] = {'k','e','y','p','r','e','s','s',0};
1632             static const PRUnichar wsz_load[] = {'l','o','a','d',0};
1633
1634             nsAString_Init(&keypress_str, wsz_keypress);
1635             nsres = nsIDOMEventTarget_AddEventListener(target, &keypress_str, NSEVENTLIST(ret), FALSE);
1636             nsAString_Finish(&keypress_str);
1637             if(NS_FAILED(nsres))
1638                 ERR("AddEventTarget failed: %08x\n", nsres);
1639
1640             nsAString_Init(&load_str, wsz_load);
1641             nsres = nsIDOMEventTarget_AddEventListener(target, &load_str, NSEVENTLIST(ret), TRUE);
1642             nsAString_Finish(&load_str);
1643             if(NS_FAILED(nsres))
1644                 ERR("AddEventTarget failed: %08x\n", nsres);
1645
1646             nsIDOMEventTarget_Release(target);
1647         }else {
1648             ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
1649         }
1650     }else {
1651         ERR("GetContentDOMWindow failed: %08x\n", nsres);
1652     }
1653
1654     nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
1655     if(NS_SUCCEEDED(nsres)) {
1656         nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1657                 ScrollOrientation_Y, Scrollbar_Always);
1658         if(NS_FAILED(nsres))
1659             ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
1660
1661         nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1662                 ScrollOrientation_X, Scrollbar_Auto);
1663         if(NS_FAILED(nsres))
1664             ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
1665
1666         nsIScrollable_Release(scrollable);
1667     }else {
1668         ERR("Could not get nsIScrollable: %08x\n", nsres);
1669     }
1670
1671     return ret;
1672 }
1673
1674 void NSContainer_Release(NSContainer *This)
1675 {
1676     TRACE("(%p)\n", This);
1677
1678     ShowWindow(This->hwnd, SW_HIDE);
1679     SetParent(This->hwnd, NULL);
1680
1681     nsIBaseWindow_SetVisibility(This->window, FALSE);
1682     nsIBaseWindow_Destroy(This->window);
1683
1684     nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1685
1686     nsIWebBrowser_Release(This->webbrowser);
1687     This->webbrowser = NULL;
1688
1689     nsIWebNavigation_Release(This->navigation);
1690     This->navigation = NULL;
1691
1692     nsIBaseWindow_Release(This->window);
1693     This->window = NULL;
1694
1695     nsIWebBrowserFocus_Release(This->focus);
1696     This->focus = NULL;
1697
1698     if(This->editor_controller) {
1699         nsIController_Release(This->editor_controller);
1700         This->editor_controller = NULL;
1701     }
1702
1703     if(This->content_listener) {
1704         nsIURIContentListener_Release(This->content_listener);
1705         This->content_listener = NULL;
1706     }
1707
1708     if(This->hwnd) {
1709         DestroyWindow(This->hwnd);
1710         This->hwnd = NULL;
1711     }
1712
1713     nsIWebBrowserChrome_Release(NSWBCHROME(This));
1714 }