Unicodify wineesd.
[wine] / dlls / mshtml / nsembed.c
1 /*
2  * Copyright 2005 Jacek Caban
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "ole2.h"
30 #include "docobj.h"
31
32 #include "mshtml.h"
33 #include "mshtmhst.h"
34
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
37
38 #include "mshtml_private.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41
42 DEFINE_GUID(CLSID_StartupNotifier, 0x1f59b001,0x02c9,0x11d5,0xae,0x76,0xcc,0x92,0xf7,0xdb,0x9e,0x03);
43 DEFINE_GUID(CLSID_nsWebBrowser, 0xf1eac761,0x87e9,0x11d3,0xaf,0x80,0x00,0xa0,0x24,0xff,0xc0,0x8c);
44
45 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
46 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
47
48 #define APPSTARTUP_TOPIC "app-startup"
49
50 #define PR_UINT32_MAX 0xffffffff
51
52 typedef struct {
53     void *d1;
54     PRUint32 d2;
55     void *d3;
56 } nsString;
57
58 static nsresult (*NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
59 static nsresult (*NS_ShutdownXPCOM)(nsIServiceManager*);
60 static nsresult (*NS_StringContainerInit)(nsString*);
61 static nsresult (*NS_StringContainerFinish)(nsString*);
62 static nsresult (*NS_StringSetData)(nsString*,const PRUnichar*,PRUint32);
63 static nsresult (*NS_NewLocalFile)(const nsString*,PRBool,nsIFile**);
64
65 static HINSTANCE hXPCOM = NULL;
66
67 static nsIServiceManager *pServMgr = NULL;
68 static nsIComponentManager *pCompMgr = NULL;
69
70 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
71
72 static ATOM nscontainer_class;
73
74 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
75 {
76     HTMLDocument *This;
77     nsresult nsres;
78
79     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
80
81     if(msg == WM_CREATE) {
82         This = *(HTMLDocument**)lParam;
83         SetPropW(hwnd, wszTHIS, This);
84     }else {
85         This = (HTMLDocument*)GetPropW(hwnd, wszTHIS);
86     }
87
88     switch(msg) {
89         case WM_SIZE:
90             TRACE("(%p)->(WM_SIZE)\n", This);
91
92             nsres = nsIBaseWindow_SetSize(This->nscontainer->window,
93                     LOWORD(lParam), HIWORD(lParam), TRUE);
94             if(NS_FAILED(nsres))
95                 WARN("SetSize failed: %08lx\n", nsres);
96     }
97
98     return DefWindowProcW(hwnd, msg, wParam, lParam);
99 }
100
101
102 static void register_nscontainer_class(void)
103 {
104     static WNDCLASSEXW wndclass = {
105         sizeof(WNDCLASSEXW),
106         CS_DBLCLKS,
107         nsembed_proc,
108         0, 0, NULL, NULL, NULL, NULL, NULL,
109         wszNsContainer,
110         NULL,
111     };
112     wndclass.hInstance = hInst;
113     nscontainer_class = RegisterClassExW(&wndclass);
114 }
115
116 static BOOL get_mozilla_path(PRUnichar *gre_path)
117 {
118     DWORD res, type, i, size = MAX_PATH;
119     HKEY mozilla_key, hkey;
120     WCHAR key_name[100];
121     BOOL ret = FALSE;
122
123     static const WCHAR wszGreKey[] =
124         {'S','o','f','t','w','a','r','e','\\',
125             'm','o','z','i','l','l','a','.','o','r','g','\\',
126                 'G','R','E',0};
127
128     static const WCHAR wszGreHome[] = {'G','r','e','H','o','m','e',0};
129
130     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszGreKey, &mozilla_key);
131     if(res != ERROR_SUCCESS) {
132         TRACE("Could not open key %s\n", debugstr_w(wszGreKey));
133         return FALSE;
134     }
135
136     for(i=0; !ret && RegEnumKeyW(mozilla_key, i, key_name, sizeof(key_name)/sizeof(WCHAR)) == ERROR_SUCCESS; i++) {
137         RegOpenKeyW(mozilla_key, key_name, &hkey);
138         res = RegQueryValueExW(hkey, wszGreHome, NULL, &type, (LPBYTE)gre_path, &size);
139         if(res == ERROR_SUCCESS)
140             ret = TRUE;
141         RegCloseKey(hkey);
142     }
143
144     RegCloseKey(mozilla_key);
145     return ret;
146 }
147
148 static BOOL get_mozctl_path(PRUnichar *gre_path)
149 {
150     HKEY hkey;
151     DWORD res, type, size = MAX_PATH;
152
153     static const WCHAR wszMozCtlKey[] =
154         {'S','o','f','t','w','a','r','e','\\','M','o','z','i','l','l','a',0};
155     static const WCHAR wszBinDirectoryPath[] =
156         {'B','i','n','D','i','r','e','c','t','o','r','y','P','a','t','h',0};
157
158     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszMozCtlKey, &hkey);
159     if(res != ERROR_SUCCESS) {
160         TRACE("Could not open key %s\n", debugstr_w(wszMozCtlKey));
161         return FALSE;
162     }
163
164     res = RegQueryValueExW(hkey, wszBinDirectoryPath, NULL, &type, (LPBYTE)gre_path, &size);
165     if(res != ERROR_SUCCESS) {
166         ERR("Could not get value %s\n", debugstr_w(wszBinDirectoryPath));
167         return FALSE;
168     }
169
170     return TRUE;
171 }
172
173 static BOOL load_gecko()
174 {
175     nsresult nsres;
176     nsIObserver *pStartNotif;
177     nsString path;
178     nsIFile *gre_dir;
179     PRUnichar gre_path[MAX_PATH];
180
181     static BOOL tried_load = FALSE;
182     static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
183
184     TRACE("()\n");
185
186     if(tried_load)
187         return pCompMgr != NULL;
188     tried_load = TRUE;
189
190     if(!get_mozctl_path(gre_path) && !get_mozilla_path(gre_path)) {
191         MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
192         return FALSE;
193     }
194     
195     TRACE("found path %s\n", debugstr_w(gre_path));
196
197     hXPCOM = LoadLibraryW(strXPCOM);
198     if(!hXPCOM) {
199         /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
200         WCHAR path_env[MAX_PATH];
201         static WCHAR wszPATH[] = {'P','A','T','H',0};
202         int len;
203
204         GetEnvironmentVariableW(wszPATH, path_env, sizeof(path_env)/sizeof(WCHAR));
205         len = strlenW(path_env);
206         path_env[len++] = ';';
207         strcpyW(path_env+len, gre_path);
208         SetEnvironmentVariableW(wszPATH, path_env);
209
210         hXPCOM = LoadLibraryW(strXPCOM);
211         if(!hXPCOM) {
212             ERR("Could not load XPCOM: %ld\n", GetLastError());
213             return FALSE;
214         }
215     }
216
217 #define NS_DLSYM(func) \
218     func = (typeof(func))GetProcAddress(hXPCOM, #func); \
219     if(!func) \
220         ERR("Could not GetProcAddress(" #func ") failed\n");
221
222     NS_DLSYM(NS_InitXPCOM2)
223     NS_DLSYM(NS_ShutdownXPCOM)
224     NS_DLSYM(NS_StringContainerInit)
225     NS_DLSYM(NS_StringContainerFinish)
226     NS_DLSYM(NS_StringSetData)
227     NS_DLSYM(NS_NewLocalFile)
228
229 #undef NS_DLSYM
230
231     NS_StringContainerInit(&path);
232     NS_StringSetData(&path, gre_path, PR_UINT32_MAX);
233     nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
234     NS_StringContainerFinish(&path);
235     if(NS_FAILED(nsres)) {
236         ERR("NS_NewLocalFile failed: %08lx\n", nsres);
237         FreeLibrary(hXPCOM);
238         return FALSE;
239     }
240
241     nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
242     if(NS_FAILED(nsres)) {
243         ERR("NS_InitXPCOM2 failed: %08lx\n", nsres);
244         FreeLibrary(hXPCOM);
245         return FALSE;
246     }
247
248     nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
249     if(NS_FAILED(nsres))
250         ERR("Could not get nsIComponentManager: %08lx\n", nsres);
251
252     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
253             NULL, &IID_nsIObserver, (void**)&pStartNotif);
254     if(NS_SUCCEEDED(nsres)) {
255         nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
256         if(NS_FAILED(nsres))
257             ERR("Observe failed: %08lx\n", nsres);
258
259         nsIObserver_Release(pStartNotif);
260     }else {
261         ERR("could not get appstartup-notifier: %08lx\n", nsres);
262     }
263
264     return TRUE;
265 }
266
267 void close_gecko()
268 {
269     TRACE("()\n");
270
271     if(pCompMgr)
272         nsIComponentManager_Release(pCompMgr);
273
274     if(pServMgr)
275         nsIServiceManager_Release(pServMgr);
276
277     if(hXPCOM)
278         FreeLibrary(hXPCOM);
279 }
280
281 void HTMLDocument_NSContainer_Init(HTMLDocument *This)
282 {
283     nsIWebBrowserSetup *wbsetup;
284     nsresult nsres;
285
286     This->nscontainer = NULL;
287
288     if(!load_gecko())
289         return;
290
291     This->nscontainer = HeapAlloc(GetProcessHeap(), 0, sizeof(NSContainer));
292
293     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
294             NULL, &IID_nsIWebBrowser, (void**)&This->nscontainer->webbrowser);
295     if(NS_FAILED(nsres))
296         ERR("Creating WebBrowser failed: %08lx\n", nsres);
297
298     nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser, &IID_nsIBaseWindow,
299             (void**)&This->nscontainer->window);
300     if(NS_FAILED(nsres))
301         ERR("Could not get nsIBaseWindow interface: %08lx\n", nsres);
302
303     nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser,
304             &IID_nsIWebBrowserSetup, (void**)&wbsetup);
305     if(NS_SUCCEEDED(nsres)) {
306         nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, TRUE);
307         if(NS_FAILED(nsres))
308             ERR("SetProperty failed: %08lx\n", nsres);
309         nsIWebBrowserSetup_Release(wbsetup);
310     }else {
311         ERR("Could not get nsIWebBrowserSetup interface\n");
312     }
313
314     nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser, &IID_nsIWebNavigation,
315             (void**)&This->nscontainer->navigation);
316     if(NS_FAILED(nsres))
317         ERR("Could not get nsIWebNavigation interface: %08lx\n", nsres);
318
319     if(!nscontainer_class)
320         register_nscontainer_class();
321
322     This->nscontainer->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
323             WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
324             GetDesktopWindow(), NULL, hInst, This);
325
326     nsres = nsIBaseWindow_InitWindow(This->nscontainer->window, This->nscontainer->hwnd, NULL,
327             0, 0, 100, 100);
328     if(NS_SUCCEEDED(nsres)) {
329         nsres = nsIBaseWindow_Create(This->nscontainer->window);
330         if(NS_FAILED(nsres))
331             WARN("Creating window failed: %08lx\n", nsres);
332
333         nsIBaseWindow_SetVisibility(This->nscontainer->window, FALSE);
334         nsIBaseWindow_SetEnabled(This->nscontainer->window, FALSE);
335     }else {
336         ERR("InitWindow failed: %08lx\n", nsres);
337     }
338 }
339
340 void HTMLDocument_NSContainer_Destroy(HTMLDocument *This)
341 {
342     TRACE("(%p)\n", This);
343
344     nsIWebBrowser_Release(This->nscontainer->webbrowser);
345     nsIWebNavigation_Release(This->nscontainer->navigation);
346     nsIBaseWindow_Release(This->nscontainer->window);
347
348     HeapFree(GetProcessHeap(), 0, This->nscontainer);
349 }