Init nsWebBrowser window while creating nscontainer.
[wine/wine64.git] / dlls / mshtml / nsembed.c
blobf6e72d939a473f6f85823681dffd2adeaf81edb6
1 /*
2 * Copyright 2005 Jacek Caban
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.
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.
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
19 #include "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "ole2.h"
30 #include "docobj.h"
32 #include "mshtml.h"
33 #include "mshtmhst.h"
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 #include "mshtml_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
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);
45 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
46 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
48 #define APPSTARTUP_TOPIC "app-startup"
50 #define PR_UINT32_MAX 0xffffffff
52 typedef struct {
53 void *d1;
54 PRUint32 d2;
55 void *d3;
56 } nsString;
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**);
65 static HINSTANCE hXPCOM = NULL;
67 static nsIServiceManager *pServMgr = NULL;
68 static nsIComponentManager *pCompMgr = NULL;
70 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
72 static ATOM nscontainer_class;
74 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
76 HTMLDocument *This;
77 nsresult nsres;
79 static const WCHAR wszTHIS[] = {'T','H','I','S',0};
81 if(msg == WM_CREATE) {
82 This = *(HTMLDocument**)lParam;
83 SetPropW(hwnd, wszTHIS, This);
84 }else {
85 This = (HTMLDocument*)GetPropW(hwnd, wszTHIS);
88 switch(msg) {
89 case WM_SIZE:
90 TRACE("(%p)->(WM_SIZE)\n", This);
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);
98 return DefWindowProcW(hwnd, msg, wParam, lParam);
102 static void register_nscontainer_class(void)
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,
112 wndclass.hInstance = hInst;
113 nscontainer_class = RegisterClassExW(&wndclass);
116 static BOOL get_mozilla_path(PRUnichar *gre_path)
118 DWORD res, type, i, size = MAX_PATH;
119 HKEY mozilla_key, hkey;
120 WCHAR key_name[100];
121 BOOL ret = FALSE;
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};
128 static const WCHAR wszGreHome[] = {'G','r','e','H','o','m','e',0};
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;
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);
144 RegCloseKey(mozilla_key);
145 return ret;
148 static BOOL get_mozctl_path(PRUnichar *gre_path)
150 HKEY hkey;
151 DWORD res, type, size = MAX_PATH;
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};
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;
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;
170 return TRUE;
173 static BOOL load_gecko()
175 nsresult nsres;
176 nsIObserver *pStartNotif;
177 nsString path;
178 nsIFile *gre_dir;
179 PRUnichar gre_path[MAX_PATH];
181 static BOOL tried_load = FALSE;
182 static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
184 TRACE("()\n");
186 if(tried_load)
187 return pCompMgr != NULL;
188 tried_load = TRUE;
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;
195 TRACE("found path %s\n", debugstr_w(gre_path));
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;
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);
210 hXPCOM = LoadLibraryW(strXPCOM);
211 if(!hXPCOM) {
212 ERR("Could not load XPCOM: %ld\n", GetLastError());
213 return FALSE;
217 #define NS_DLSYM(func) \
218 func = (typeof(func))GetProcAddress(hXPCOM, #func); \
219 if(!func) \
220 ERR("Could not GetProcAddress(" #func ") failed\n");
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)
229 #undef NS_DLSYM
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;
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;
248 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
249 if(NS_FAILED(nsres))
250 ERR("Could not get nsIComponentManager: %08lx\n", nsres);
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);
259 nsIObserver_Release(pStartNotif);
260 }else {
261 ERR("could not get appstartup-notifier: %08lx\n", nsres);
264 return TRUE;
267 void close_gecko()
269 TRACE("()\n");
271 if(pCompMgr)
272 nsIComponentManager_Release(pCompMgr);
274 if(pServMgr)
275 nsIServiceManager_Release(pServMgr);
277 if(hXPCOM)
278 FreeLibrary(hXPCOM);
281 void HTMLDocument_NSContainer_Init(HTMLDocument *This)
283 nsIWebBrowserSetup *wbsetup;
284 nsresult nsres;
286 This->nscontainer = NULL;
288 if(!load_gecko())
289 return;
291 This->nscontainer = HeapAlloc(GetProcessHeap(), 0, sizeof(NSContainer));
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);
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);
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");
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);
319 if(!nscontainer_class)
320 register_nscontainer_class();
322 This->nscontainer->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
323 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
324 GetDesktopWindow(), NULL, hInst, This);
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);
333 nsIBaseWindow_SetVisibility(This->nscontainer->window, FALSE);
334 nsIBaseWindow_SetEnabled(This->nscontainer->window, FALSE);
335 }else {
336 ERR("InitWindow failed: %08lx\n", nsres);
340 void HTMLDocument_NSContainer_Destroy(HTMLDocument *This)
342 TRACE("(%p)\n", This);
344 nsIWebBrowser_Release(This->nscontainer->webbrowser);
345 nsIWebNavigation_Release(This->nscontainer->navigation);
346 nsIBaseWindow_Release(This->nscontainer->window);
348 HeapFree(GetProcessHeap(), 0, This->nscontainer);