winealsa.drv: Clean up DriverProc.
[wine/multimedia.git] / dlls / mshtml / nsembed.c
blob8a5900792788639530e1c3c1832f7fd0ed9fec3a
1 /*
2 * Copyright 2005-2007 Jacek Caban for CodeWeavers
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 #include "mshtml_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37 WINE_DECLARE_DEBUG_CHANNEL(gecko);
39 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
40 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
41 #define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
42 #define NS_COMMANDPARAMS_CONTRACTID "@mozilla.org/embedcomp/command-params;1"
43 #define NS_HTMLSERIALIZER_CONTRACTID "@mozilla.org/layout/contentserializer;1?mimetype=text/html"
44 #define NS_EDITORCONTROLLER_CONTRACTID "@mozilla.org/editor/editorcontroller;1"
45 #define NS_PREFERENCES_CONTRACTID "@mozilla.org/preferences;1"
47 #define APPSTARTUP_TOPIC "app-startup"
49 #define PR_UINT32_MAX 0xffffffff
51 struct nsCStringContainer {
52 void *v;
53 void *d1;
54 PRUint32 d2;
55 PRUint32 d3;
58 #define NS_STRING_CONTAINER_INIT_DEPEND 0x0002
60 static nsresult (*NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
61 static nsresult (*NS_ShutdownXPCOM)(nsIServiceManager*);
62 static nsresult (*NS_GetComponentRegistrar)(nsIComponentRegistrar**);
63 static nsresult (*NS_StringContainerInit2)(nsStringContainer*,const PRUnichar*,PRUint32,PRUint32);
64 static nsresult (*NS_CStringContainerInit)(nsCStringContainer*);
65 static nsresult (*NS_StringContainerFinish)(nsStringContainer*);
66 static nsresult (*NS_CStringContainerFinish)(nsCStringContainer*);
67 static nsresult (*NS_StringSetData)(nsAString*,const PRUnichar*,PRUint32);
68 static nsresult (*NS_CStringSetData)(nsACString*,const char*,PRUint32);
69 static nsresult (*NS_NewLocalFile)(const nsAString*,PRBool,nsIFile**);
70 static PRUint32 (*NS_StringGetData)(const nsAString*,const PRUnichar **,PRBool*);
71 static PRUint32 (*NS_CStringGetData)(const nsACString*,const char**,PRBool*);
73 static HINSTANCE hXPCOM = NULL;
75 static nsIServiceManager *pServMgr = NULL;
76 static nsIComponentManager *pCompMgr = NULL;
77 static nsIMemory *nsmem = NULL;
79 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
81 static ATOM nscontainer_class;
83 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
85 NSContainer *This;
86 nsresult nsres;
88 static const WCHAR wszTHIS[] = {'T','H','I','S',0};
90 if(msg == WM_CREATE) {
91 This = *(NSContainer**)lParam;
92 SetPropW(hwnd, wszTHIS, This);
93 }else {
94 This = GetPropW(hwnd, wszTHIS);
97 switch(msg) {
98 case WM_SIZE:
99 TRACE("(%p)->(WM_SIZE)\n", This);
101 nsres = nsIBaseWindow_SetSize(This->window,
102 LOWORD(lParam), HIWORD(lParam), TRUE);
103 if(NS_FAILED(nsres))
104 WARN("SetSize failed: %08x\n", nsres);
105 break;
107 case WM_PARENTNOTIFY:
108 TRACE("WM_PARENTNOTIFY %x\n", (unsigned)wParam);
110 switch(wParam) {
111 case WM_LBUTTONDOWN:
112 case WM_RBUTTONDOWN:
113 nsIWebBrowserFocus_Activate(This->focus);
117 return DefWindowProcW(hwnd, msg, wParam, lParam);
121 static void register_nscontainer_class(void)
123 static WNDCLASSEXW wndclass = {
124 sizeof(WNDCLASSEXW),
125 CS_DBLCLKS,
126 nsembed_proc,
127 0, 0, NULL, NULL, NULL, NULL, NULL,
128 wszNsContainer,
129 NULL,
131 wndclass.hInstance = hInst;
132 nscontainer_class = RegisterClassExW(&wndclass);
135 static void set_environment(LPCWSTR gre_path)
137 WCHAR path_env[MAX_PATH], buf[20];
138 int len, debug_level = 0;
140 static const WCHAR pathW[] = {'P','A','T','H',0};
141 static const WCHAR warnW[] = {'w','a','r','n',0};
142 static const WCHAR xpcom_debug_breakW[] =
143 {'X','P','C','O','M','_','D','E','B','U','G','_','B','R','E','A','K',0};
144 static const WCHAR nspr_log_modulesW[] =
145 {'N','S','P','R','_','L','O','G','_','M','O','D','U','L','E','S',0};
146 static const WCHAR debug_formatW[] = {'a','l','l',':','%','d',0};
148 /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
149 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
150 len = strlenW(path_env);
151 path_env[len++] = ';';
152 strcpyW(path_env+len, gre_path);
153 SetEnvironmentVariableW(pathW, path_env);
155 SetEnvironmentVariableW(xpcom_debug_breakW, warnW);
157 if(TRACE_ON(gecko))
158 debug_level = 5;
159 else if(WARN_ON(gecko))
160 debug_level = 3;
161 else if(ERR_ON(gecko))
162 debug_level = 2;
164 sprintfW(buf, debug_formatW, debug_level);
165 SetEnvironmentVariableW(nspr_log_modulesW, buf);
168 static BOOL load_xpcom(const PRUnichar *gre_path)
170 static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
172 TRACE("(%s)\n", debugstr_w(gre_path));
174 set_environment(gre_path);
176 hXPCOM = LoadLibraryW(strXPCOM);
177 if(!hXPCOM) {
178 WARN("Could not load XPCOM: %d\n", GetLastError());
179 return FALSE;
182 #define NS_DLSYM(func) \
183 func = (void *)GetProcAddress(hXPCOM, #func); \
184 if(!func) \
185 ERR("Could not GetProcAddress(" #func ") failed\n")
187 NS_DLSYM(NS_InitXPCOM2);
188 NS_DLSYM(NS_ShutdownXPCOM);
189 NS_DLSYM(NS_GetComponentRegistrar);
190 NS_DLSYM(NS_StringContainerInit2);
191 NS_DLSYM(NS_CStringContainerInit);
192 NS_DLSYM(NS_StringContainerFinish);
193 NS_DLSYM(NS_CStringContainerFinish);
194 NS_DLSYM(NS_StringSetData);
195 NS_DLSYM(NS_CStringSetData);
196 NS_DLSYM(NS_NewLocalFile);
197 NS_DLSYM(NS_StringGetData);
198 NS_DLSYM(NS_CStringGetData);
200 #undef NS_DLSYM
202 return TRUE;
205 static BOOL check_version(LPCWSTR gre_path, const char *version_string)
207 WCHAR file_name[MAX_PATH];
208 char version[128];
209 DWORD read=0;
210 HANDLE hfile;
212 static const WCHAR wszVersion[] = {'\\','V','E','R','S','I','O','N',0};
214 strcpyW(file_name, gre_path);
215 strcatW(file_name, wszVersion);
217 hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
218 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
219 if(hfile == INVALID_HANDLE_VALUE) {
220 ERR("Could not open VERSION file\n");
221 return FALSE;
224 ReadFile(hfile, version, sizeof(version), &read, NULL);
225 version[read] = 0;
226 CloseHandle(hfile);
228 TRACE("%s\n", debugstr_a(version));
230 if(strcmp(version, version_string)) {
231 ERR("Unexpected version %s, expected %s\n", debugstr_a(version),
232 debugstr_a(version_string));
233 return FALSE;
236 return TRUE;
239 static BOOL load_wine_gecko_v(PRUnichar *gre_path, HKEY mshtml_key,
240 const char *version, const char *version_string)
242 DWORD res, type, size = MAX_PATH;
243 HKEY hkey = mshtml_key;
245 static const WCHAR wszGeckoPath[] =
246 {'G','e','c','k','o','P','a','t','h',0};
248 if(version) {
249 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
250 res = RegOpenKeyA(mshtml_key, version, &hkey);
251 if(res != ERROR_SUCCESS)
252 return FALSE;
255 res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
256 if(hkey != mshtml_key)
257 RegCloseKey(hkey);
258 if(res != ERROR_SUCCESS || type != REG_SZ)
259 return FALSE;
261 if(!check_version(gre_path, version_string))
262 return FALSE;
264 return load_xpcom(gre_path);
267 static BOOL load_wine_gecko(PRUnichar *gre_path)
269 HKEY hkey;
270 DWORD res;
271 BOOL ret;
273 static const WCHAR wszMshtmlKey[] = {
274 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
275 '\\','M','S','H','T','M','L',0};
277 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
278 res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
279 if(res != ERROR_SUCCESS)
280 return FALSE;
282 ret = load_wine_gecko_v(gre_path, hkey, GECKO_VERSION, GECKO_VERSION_STRING);
284 RegCloseKey(hkey);
285 return ret;
288 static void set_bool_pref(nsIPrefBranch *pref, const char *pref_name, BOOL val)
290 nsresult nsres;
292 nsres = nsIPrefBranch_SetBoolPref(pref, pref_name, val);
293 if(NS_FAILED(nsres))
294 ERR("Could not set pref %s\n", debugstr_a(pref_name));
297 static void set_int_pref(nsIPrefBranch *pref, const char *pref_name, int val)
299 nsresult nsres;
301 nsres = nsIPrefBranch_SetIntPref(pref, pref_name, val);
302 if(NS_FAILED(nsres))
303 ERR("Could not set pref %s\n", debugstr_a(pref_name));
306 static void set_string_pref(nsIPrefBranch *pref, const char *pref_name, const char *val)
308 nsresult nsres;
310 nsres = nsIPrefBranch_SetCharPref(pref, pref_name, val);
311 if(NS_FAILED(nsres))
312 ERR("Could not set pref %s\n", debugstr_a(pref_name));
315 static void set_lang(nsIPrefBranch *pref)
317 char langs[100];
318 DWORD res, size, type;
319 HKEY hkey;
321 static const WCHAR international_keyW[] =
322 {'S','o','f','t','w','a','r','e',
323 '\\','M','i','c','r','o','s','o','f','t',
324 '\\','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
325 '\\','I','n','t','e','r','n','a','t','i','o','n','a','l',0};
327 res = RegOpenKeyW(HKEY_CURRENT_USER, international_keyW, &hkey);
328 if(res != ERROR_SUCCESS)
329 return;
331 size = sizeof(langs);
332 res = RegQueryValueExA(hkey, "AcceptLanguage", 0, &type, (LPBYTE)langs, &size);
333 RegCloseKey(hkey);
334 if(res != ERROR_SUCCESS || type != REG_SZ)
335 return;
337 TRACE("Setting lang %s\n", debugstr_a(langs));
339 set_string_pref(pref, "intl.accept_languages", langs);
342 static void set_proxy(nsIPrefBranch *pref)
344 char proxy[512];
345 char * proxy_port;
346 int proxy_port_num;
347 DWORD enabled = 0, res, size, type;
348 HKEY hkey;
350 static const WCHAR proxy_keyW[] =
351 {'S','o','f','t','w','a','r','e',
352 '\\','M','i','c','r','o','s','o','f','t',
353 '\\','W','i','n','d','o','w','s',
354 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
355 '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
357 res = RegOpenKeyW(HKEY_CURRENT_USER, proxy_keyW, &hkey);
358 if(res != ERROR_SUCCESS)
359 return;
361 size = sizeof(enabled);
362 res = RegQueryValueExA(hkey, "ProxyEnable", 0, &type, (LPBYTE)&enabled, &size);
363 if(res != ERROR_SUCCESS || type != REG_DWORD || enabled == 0)
365 RegCloseKey(hkey);
366 return;
369 size = sizeof(proxy);
370 res = RegQueryValueExA(hkey, "ProxyServer", 0, &type, (LPBYTE)proxy, &size);
371 RegCloseKey(hkey);
372 if(res != ERROR_SUCCESS || type != REG_SZ)
373 return;
375 proxy_port = strchr(proxy, ':');
376 if (!proxy_port)
377 return;
379 *proxy_port = 0;
380 proxy_port_num = atoi(proxy_port + 1);
381 TRACE("Setting proxy to %s, port %d\n", debugstr_a(proxy), proxy_port_num);
383 set_string_pref(pref, "network.proxy.http", proxy);
384 set_string_pref(pref, "network.proxy.ssl", proxy);
386 set_int_pref(pref, "network.proxy.type", 1);
387 set_int_pref(pref, "network.proxy.http_port", proxy_port_num);
388 set_int_pref(pref, "network.proxy.ssl_port", proxy_port_num);
391 static void set_preferences(void)
393 nsIPrefBranch *pref;
394 nsresult nsres;
396 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
397 &IID_nsIPrefBranch, (void**)&pref);
398 if(NS_FAILED(nsres)) {
399 ERR("Could not get preference service: %08x\n", nsres);
400 return;
403 set_lang(pref);
404 set_proxy(pref);
405 set_bool_pref(pref, "security.warn_entering_secure", FALSE);
406 set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
407 set_int_pref(pref, "layout.spellcheckDefault", 0);
409 nsIPrefBranch_Release(pref);
412 static BOOL init_xpcom(const PRUnichar *gre_path)
414 nsresult nsres;
415 nsIObserver *pStartNotif;
416 nsIComponentRegistrar *registrar = NULL;
417 nsAString path;
418 nsIFile *gre_dir;
420 nsAString_InitDepend(&path, gre_path);
421 nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
422 nsAString_Finish(&path);
423 if(NS_FAILED(nsres)) {
424 ERR("NS_NewLocalFile failed: %08x\n", nsres);
425 FreeLibrary(hXPCOM);
426 return FALSE;
429 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
430 if(NS_FAILED(nsres)) {
431 ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
432 FreeLibrary(hXPCOM);
433 return FALSE;
436 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
437 if(NS_FAILED(nsres))
438 ERR("Could not get nsIComponentManager: %08x\n", nsres);
440 nsres = NS_GetComponentRegistrar(&registrar);
441 if(NS_SUCCEEDED(nsres)) {
442 nsres = nsIComponentRegistrar_AutoRegister(registrar, NULL);
443 if(NS_FAILED(nsres))
444 ERR("AutoRegister(NULL) failed: %08x\n", nsres);
446 init_nsio(pCompMgr, registrar);
447 }else {
448 ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
451 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
452 NULL, &IID_nsIObserver, (void**)&pStartNotif);
453 if(NS_SUCCEEDED(nsres)) {
454 nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
455 if(NS_FAILED(nsres))
456 ERR("Observe failed: %08x\n", nsres);
458 nsIObserver_Release(pStartNotif);
459 }else {
460 ERR("could not get appstartup-notifier: %08x\n", nsres);
463 set_preferences();
465 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_MEMORY_CONTRACTID,
466 NULL, &IID_nsIMemory, (void**)&nsmem);
467 if(NS_FAILED(nsres))
468 ERR("Could not get nsIMemory: %08x\n", nsres);
470 if(registrar) {
471 register_nsservice(registrar, pServMgr);
472 nsIComponentRegistrar_Release(registrar);
475 return TRUE;
478 static CRITICAL_SECTION cs_load_gecko;
479 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
481 0, 0, &cs_load_gecko,
482 { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
483 0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
485 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
487 BOOL load_gecko(BOOL silent)
489 PRUnichar gre_path[MAX_PATH];
490 BOOL ret = FALSE;
492 static DWORD loading_thread;
494 TRACE("()\n");
496 /* load_gecko may be called recursively */
497 if(loading_thread == GetCurrentThreadId())
498 return pCompMgr != NULL;
500 EnterCriticalSection(&cs_load_gecko);
502 if(!loading_thread) {
503 loading_thread = GetCurrentThreadId();
505 if(load_wine_gecko(gre_path)
506 || (install_wine_gecko(silent) && load_wine_gecko(gre_path)))
507 ret = init_xpcom(gre_path);
508 else
509 MESSAGE("Could not load wine-gecko. HTML rendering will be disabled.\n");
510 }else {
511 ret = pCompMgr != NULL;
514 LeaveCriticalSection(&cs_load_gecko);
516 return ret;
519 void *nsalloc(size_t size)
521 return nsIMemory_Alloc(nsmem, size);
524 void nsfree(void *mem)
526 nsIMemory_Free(nsmem, mem);
529 static void nsACString_Init(nsACString *str, const char *data)
531 NS_CStringContainerInit(str);
532 if(data)
533 nsACString_SetData(str, data);
536 void nsACString_SetData(nsACString *str, const char *data)
538 NS_CStringSetData(str, data, PR_UINT32_MAX);
541 PRUint32 nsACString_GetData(const nsACString *str, const char **data)
543 return NS_CStringGetData(str, data, NULL);
546 static void nsACString_Finish(nsACString *str)
548 NS_CStringContainerFinish(str);
551 BOOL nsAString_Init(nsAString *str, const PRUnichar *data)
553 return NS_SUCCEEDED(NS_StringContainerInit2(str, data, PR_UINT32_MAX, 0));
557 * Initializes nsAString with data owned by caller.
558 * Caller must ensure that data is valid during lifetime of string object.
560 void nsAString_InitDepend(nsAString *str, const PRUnichar *data)
562 NS_StringContainerInit2(str, data, PR_UINT32_MAX, NS_STRING_CONTAINER_INIT_DEPEND);
565 void nsAString_SetData(nsAString *str, const PRUnichar *data)
567 NS_StringSetData(str, data, PR_UINT32_MAX);
570 PRUint32 nsAString_GetData(const nsAString *str, const PRUnichar **data)
572 return NS_StringGetData(str, data, NULL);
575 void nsAString_Finish(nsAString *str)
577 NS_StringContainerFinish(str);
580 nsICommandParams *create_nscommand_params(void)
582 nsICommandParams *ret = NULL;
583 nsresult nsres;
585 if(!pCompMgr)
586 return NULL;
588 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
589 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
590 (void**)&ret);
591 if(NS_FAILED(nsres))
592 ERR("Could not get nsICommandParams\n");
594 return ret;
597 nsresult get_nsinterface(nsISupports *iface, REFIID riid, void **ppv)
599 nsIInterfaceRequestor *iface_req;
600 nsresult nsres;
602 nsres = nsISupports_QueryInterface(iface, &IID_nsIInterfaceRequestor, (void**)&iface_req);
603 if(NS_FAILED(nsres))
604 return nsres;
606 nsres = nsIInterfaceRequestor_GetInterface(iface_req, riid, ppv);
607 nsIInterfaceRequestor_Release(iface_req);
609 return nsres;
612 static HRESULT nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
614 nsIDOMNodeList *node_list = NULL;
615 PRBool has_children = FALSE;
616 PRUint16 type;
617 nsresult nsres;
619 nsIDOMNode_HasChildNodes(nsnode, &has_children);
621 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
622 if(NS_FAILED(nsres)) {
623 ERR("GetType failed: %08x\n", nsres);
624 return E_FAIL;
627 switch(type) {
628 case ELEMENT_NODE: {
629 nsIDOMElement *nselem;
630 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMElement, (void**)&nselem);
631 nsIContentSerializer_AppendElementStart(serializer, nselem, nselem, str);
632 nsIDOMElement_Release(nselem);
633 break;
635 case TEXT_NODE: {
636 nsIDOMText *nstext;
637 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMText, (void**)&nstext);
638 nsIContentSerializer_AppendText(serializer, nstext, 0, -1, str);
639 nsIDOMText_Release(nstext);
640 break;
642 case COMMENT_NODE: {
643 nsIDOMComment *nscomment;
644 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMComment, (void**)&nscomment);
645 nsres = nsIContentSerializer_AppendComment(serializer, nscomment, 0, -1, str);
646 break;
648 case DOCUMENT_NODE: {
649 nsIDOMDocument *nsdoc;
650 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMDocument, (void**)&nsdoc);
651 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
652 nsIDOMDocument_Release(nsdoc);
653 break;
655 case DOCUMENT_TYPE_NODE:
656 WARN("Ignoring DOCUMENT_TYPE_NODE\n");
657 break;
658 case DOCUMENT_FRAGMENT_NODE:
659 break;
660 default:
661 FIXME("Unhandled type %u\n", type);
664 if(has_children) {
665 PRUint32 child_cnt, i;
666 nsIDOMNode *child_node;
668 nsIDOMNode_GetChildNodes(nsnode, &node_list);
669 nsIDOMNodeList_GetLength(node_list, &child_cnt);
671 for(i=0; i<child_cnt; i++) {
672 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
673 if(NS_SUCCEEDED(nsres)) {
674 nsnode_to_nsstring_rec(serializer, child_node, str);
675 nsIDOMNode_Release(child_node);
676 }else {
677 ERR("Item failed: %08x\n", nsres);
681 nsIDOMNodeList_Release(node_list);
684 if(type == ELEMENT_NODE) {
685 nsIDOMElement *nselem;
686 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMElement, (void**)&nselem);
687 nsIContentSerializer_AppendElementEnd(serializer, nselem, str);
688 nsIDOMElement_Release(nselem);
691 return S_OK;
694 HRESULT nsnode_to_nsstring(nsIDOMNode *nsnode, nsAString *str)
696 nsIContentSerializer *serializer;
697 nsresult nsres;
698 HRESULT hres;
700 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
701 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
702 (void**)&serializer);
703 if(NS_FAILED(nsres)) {
704 ERR("Could not get nsIContentSerializer: %08x\n", nsres);
705 return E_FAIL;
708 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE, FALSE /* FIXME */);
709 if(NS_FAILED(nsres))
710 ERR("Init failed: %08x\n", nsres);
712 hres = nsnode_to_nsstring_rec(serializer, nsnode, str);
713 if(SUCCEEDED(hres)) {
714 nsres = nsIContentSerializer_Flush(serializer, str);
715 if(NS_FAILED(nsres))
716 ERR("Flush failed: %08x\n", nsres);
719 nsIContentSerializer_Release(serializer);
720 return hres;
723 void get_editor_controller(NSContainer *This)
725 nsIEditingSession *editing_session = NULL;
726 nsIControllerContext *ctrlctx;
727 nsresult nsres;
729 if(This->editor) {
730 nsIEditor_Release(This->editor);
731 This->editor = NULL;
734 if(This->editor_controller) {
735 nsIController_Release(This->editor_controller);
736 This->editor_controller = NULL;
739 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
740 (void**)&editing_session);
741 if(NS_FAILED(nsres)) {
742 ERR("Could not get nsIEditingSession: %08x\n", nsres);
743 return;
746 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
747 This->doc->basedoc.window->nswindow, &This->editor);
748 nsIEditingSession_Release(editing_session);
749 if(NS_FAILED(nsres)) {
750 ERR("Could not get editor: %08x\n", nsres);
751 return;
754 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
755 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
756 if(NS_SUCCEEDED(nsres)) {
757 nsres = nsIControllerContext_SetCommandContext(ctrlctx, (nsISupports *)This->editor);
758 if(NS_FAILED(nsres))
759 ERR("SetCommandContext failed: %08x\n", nsres);
760 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
761 (void**)&This->editor_controller);
762 nsIControllerContext_Release(ctrlctx);
763 if(NS_FAILED(nsres))
764 ERR("Could not get nsIController interface: %08x\n", nsres);
765 }else {
766 ERR("Could not create edit controller: %08x\n", nsres);
770 void close_gecko(void)
772 TRACE("()\n");
774 release_nsio();
776 if(pCompMgr)
777 nsIComponentManager_Release(pCompMgr);
779 if(pServMgr)
780 nsIServiceManager_Release(pServMgr);
782 if(nsmem)
783 nsIMemory_Release(nsmem);
785 /* Gecko doesn't really support being unloaded */
786 /* if (hXPCOM) FreeLibrary(hXPCOM); */
789 /**********************************************************
790 * nsIWebBrowserChrome interface
793 #define NSWBCHROME_THIS(iface) DEFINE_THIS(NSContainer, WebBrowserChrome, iface)
795 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
796 nsIIDRef riid, nsQIResult result)
798 NSContainer *This = NSWBCHROME_THIS(iface);
800 *result = NULL;
801 if(IsEqualGUID(&IID_nsISupports, riid)) {
802 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
803 *result = NSWBCHROME(This);
804 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
805 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
806 *result = NSWBCHROME(This);
807 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
808 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
809 *result = NSCML(This);
810 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
811 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
812 *result = NSURICL(This);
813 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
814 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
815 *result = NSEMBWNDS(This);
816 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
817 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
818 *result = NSTOOLTIP(This);
819 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
820 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
821 *result = NSIFACEREQ(This);
822 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
823 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
824 *result = NSWEAKREF(This);
825 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
826 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
827 *result = NSSUPWEAKREF(This);
830 if(*result) {
831 nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
832 return NS_OK;
835 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
836 return NS_NOINTERFACE;
839 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
841 NSContainer *This = NSWBCHROME_THIS(iface);
842 LONG ref = InterlockedIncrement(&This->ref);
844 TRACE("(%p) ref=%d\n", This, ref);
846 return ref;
849 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
851 NSContainer *This = NSWBCHROME_THIS(iface);
852 LONG ref = InterlockedDecrement(&This->ref);
854 TRACE("(%p) ref=%d\n", This, ref);
856 if(!ref) {
857 if(This->parent)
858 nsIWebBrowserChrome_Release(NSWBCHROME(This->parent));
859 heap_free(This);
862 return ref;
865 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
866 PRUint32 statusType, const PRUnichar *status)
868 NSContainer *This = NSWBCHROME_THIS(iface);
869 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
870 return NS_OK;
873 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
874 nsIWebBrowser **aWebBrowser)
876 NSContainer *This = NSWBCHROME_THIS(iface);
878 TRACE("(%p)->(%p)\n", This, aWebBrowser);
880 if(!aWebBrowser)
881 return NS_ERROR_INVALID_ARG;
883 if(This->webbrowser)
884 nsIWebBrowser_AddRef(This->webbrowser);
885 *aWebBrowser = This->webbrowser;
886 return S_OK;
889 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
890 nsIWebBrowser *aWebBrowser)
892 NSContainer *This = NSWBCHROME_THIS(iface);
894 TRACE("(%p)->(%p)\n", This, aWebBrowser);
896 if(aWebBrowser != This->webbrowser)
897 ERR("Wrong nsWebBrowser!\n");
899 return NS_OK;
902 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
903 PRUint32 *aChromeFlags)
905 NSContainer *This = NSWBCHROME_THIS(iface);
906 WARN("(%p)->(%p)\n", This, aChromeFlags);
907 return NS_ERROR_NOT_IMPLEMENTED;
910 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
911 PRUint32 aChromeFlags)
913 NSContainer *This = NSWBCHROME_THIS(iface);
914 WARN("(%p)->(%08x)\n", This, aChromeFlags);
915 return NS_ERROR_NOT_IMPLEMENTED;
918 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
920 NSContainer *This = NSWBCHROME_THIS(iface);
921 TRACE("(%p)\n", This);
922 return NS_ERROR_NOT_IMPLEMENTED;
925 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
926 PRInt32 aCX, PRInt32 aCY)
928 NSContainer *This = NSWBCHROME_THIS(iface);
929 WARN("(%p)->(%d %d)\n", This, aCX, aCY);
930 return NS_ERROR_NOT_IMPLEMENTED;
933 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
935 NSContainer *This = NSWBCHROME_THIS(iface);
936 WARN("(%p)\n", This);
937 return NS_ERROR_NOT_IMPLEMENTED;
940 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
942 NSContainer *This = NSWBCHROME_THIS(iface);
943 WARN("(%p)->(%p)\n", This, _retval);
944 return NS_ERROR_NOT_IMPLEMENTED;
947 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
948 nsresult aStatus)
950 NSContainer *This = NSWBCHROME_THIS(iface);
951 WARN("(%p)->(%08x)\n", This, aStatus);
952 return NS_ERROR_NOT_IMPLEMENTED;
955 #undef NSWBCHROME_THIS
957 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
958 nsWebBrowserChrome_QueryInterface,
959 nsWebBrowserChrome_AddRef,
960 nsWebBrowserChrome_Release,
961 nsWebBrowserChrome_SetStatus,
962 nsWebBrowserChrome_GetWebBrowser,
963 nsWebBrowserChrome_SetWebBrowser,
964 nsWebBrowserChrome_GetChromeFlags,
965 nsWebBrowserChrome_SetChromeFlags,
966 nsWebBrowserChrome_DestroyBrowserWindow,
967 nsWebBrowserChrome_SizeBrowserTo,
968 nsWebBrowserChrome_ShowAsModal,
969 nsWebBrowserChrome_IsWindowModal,
970 nsWebBrowserChrome_ExitModalEventLoop
973 /**********************************************************
974 * nsIContextMenuListener interface
977 #define NSCML_THIS(iface) DEFINE_THIS(NSContainer, ContextMenuListener, iface)
979 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
980 nsIIDRef riid, nsQIResult result)
982 NSContainer *This = NSCML_THIS(iface);
983 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
986 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
988 NSContainer *This = NSCML_THIS(iface);
989 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
992 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
994 NSContainer *This = NSCML_THIS(iface);
995 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
998 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
999 PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1001 NSContainer *This = NSCML_THIS(iface);
1002 nsIDOMMouseEvent *event;
1003 POINT pt;
1004 DWORD dwID = CONTEXT_MENU_DEFAULT;
1005 nsresult nsres;
1007 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1009 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
1010 if(NS_FAILED(nsres)) {
1011 ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
1012 return nsres;
1015 nsIDOMMouseEvent_GetScreenX(event, &pt.x);
1016 nsIDOMMouseEvent_GetScreenY(event, &pt.y);
1017 nsIDOMMouseEvent_Release(event);
1019 switch(aContextFlags) {
1020 case CONTEXT_NONE:
1021 case CONTEXT_DOCUMENT:
1022 case CONTEXT_TEXT:
1023 dwID = CONTEXT_MENU_DEFAULT;
1024 break;
1025 case CONTEXT_IMAGE:
1026 case CONTEXT_IMAGE|CONTEXT_LINK:
1027 dwID = CONTEXT_MENU_IMAGE;
1028 break;
1029 case CONTEXT_LINK:
1030 dwID = CONTEXT_MENU_ANCHOR;
1031 break;
1032 case CONTEXT_INPUT:
1033 dwID = CONTEXT_MENU_CONTROL;
1034 break;
1035 default:
1036 FIXME("aContextFlags=%08x\n", aContextFlags);
1039 show_context_menu(This->doc, dwID, &pt, (IDispatch*)HTMLDOMNODE(get_node(This->doc->basedoc.doc_node, aNode, TRUE)));
1041 return NS_OK;
1044 #undef NSCML_THIS
1046 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1047 nsContextMenuListener_QueryInterface,
1048 nsContextMenuListener_AddRef,
1049 nsContextMenuListener_Release,
1050 nsContextMenuListener_OnShowContextMenu
1053 /**********************************************************
1054 * nsIURIContentListener interface
1057 #define NSURICL_THIS(iface) DEFINE_THIS(NSContainer, URIContentListener, iface)
1059 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1060 nsIIDRef riid, nsQIResult result)
1062 NSContainer *This = NSURICL_THIS(iface);
1063 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1066 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1068 NSContainer *This = NSURICL_THIS(iface);
1069 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1072 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1074 NSContainer *This = NSURICL_THIS(iface);
1075 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1078 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1079 nsIURI *aURI, PRBool *_retval)
1081 NSContainer *This = NSURICL_THIS(iface);
1082 nsACString spec_str;
1083 const char *spec;
1084 nsresult nsres;
1086 nsACString_Init(&spec_str, NULL);
1087 nsIURI_GetSpec(aURI, &spec_str);
1088 nsACString_GetData(&spec_str, &spec);
1090 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1092 nsACString_Finish(&spec_str);
1094 nsres = on_start_uri_open(This, aURI, _retval);
1095 if(NS_FAILED(nsres))
1096 return nsres;
1098 return !*_retval && This->content_listener
1099 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1100 : NS_OK;
1103 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1104 const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
1105 nsIStreamListener **aContentHandler, PRBool *_retval)
1107 NSContainer *This = NSURICL_THIS(iface);
1109 TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1110 aRequest, aContentHandler, _retval);
1112 return This->content_listener
1113 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1114 aIsContentPreferred, aRequest, aContentHandler, _retval)
1115 : NS_ERROR_NOT_IMPLEMENTED;
1118 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1119 const char *aContentType, char **aDesiredContentType, PRBool *_retval)
1121 NSContainer *This = NSURICL_THIS(iface);
1123 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1125 /* FIXME: Should we do something here? */
1126 *_retval = TRUE;
1128 return This->content_listener
1129 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1130 aDesiredContentType, _retval)
1131 : NS_OK;
1134 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1135 const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
1136 PRBool *_retval)
1138 NSContainer *This = NSURICL_THIS(iface);
1140 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1141 aDesiredContentType, _retval);
1143 return This->content_listener
1144 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1145 aIsContentPreferred, aDesiredContentType, _retval)
1146 : NS_ERROR_NOT_IMPLEMENTED;
1149 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1150 nsISupports **aLoadCookie)
1152 NSContainer *This = NSURICL_THIS(iface);
1154 WARN("(%p)->(%p)\n", This, aLoadCookie);
1156 return This->content_listener
1157 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1158 : NS_ERROR_NOT_IMPLEMENTED;
1161 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1162 nsISupports *aLoadCookie)
1164 NSContainer *This = NSURICL_THIS(iface);
1166 WARN("(%p)->(%p)\n", This, aLoadCookie);
1168 return This->content_listener
1169 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1170 : NS_ERROR_NOT_IMPLEMENTED;
1173 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1174 nsIURIContentListener **aParentContentListener)
1176 NSContainer *This = NSURICL_THIS(iface);
1178 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1180 if(This->content_listener)
1181 nsIURIContentListener_AddRef(This->content_listener);
1183 *aParentContentListener = This->content_listener;
1184 return NS_OK;
1187 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1188 nsIURIContentListener *aParentContentListener)
1190 NSContainer *This = NSURICL_THIS(iface);
1192 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1194 if(aParentContentListener == NSURICL(This))
1195 return NS_OK;
1197 if(This->content_listener)
1198 nsIURIContentListener_Release(This->content_listener);
1200 This->content_listener = aParentContentListener;
1201 if(This->content_listener)
1202 nsIURIContentListener_AddRef(This->content_listener);
1204 return NS_OK;
1207 #undef NSURICL_THIS
1209 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1210 nsURIContentListener_QueryInterface,
1211 nsURIContentListener_AddRef,
1212 nsURIContentListener_Release,
1213 nsURIContentListener_OnStartURIOpen,
1214 nsURIContentListener_DoContent,
1215 nsURIContentListener_IsPreferred,
1216 nsURIContentListener_CanHandleContent,
1217 nsURIContentListener_GetLoadCookie,
1218 nsURIContentListener_SetLoadCookie,
1219 nsURIContentListener_GetParentContentListener,
1220 nsURIContentListener_SetParentContentListener
1223 /**********************************************************
1224 * nsIEmbeddinSiteWindow interface
1227 #define NSEMBWNDS_THIS(iface) DEFINE_THIS(NSContainer, EmbeddingSiteWindow, iface)
1229 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1230 nsIIDRef riid, nsQIResult result)
1232 NSContainer *This = NSEMBWNDS_THIS(iface);
1233 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1236 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1238 NSContainer *This = NSEMBWNDS_THIS(iface);
1239 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1242 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1244 NSContainer *This = NSEMBWNDS_THIS(iface);
1245 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1248 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1249 PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
1251 NSContainer *This = NSEMBWNDS_THIS(iface);
1252 WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1253 return NS_ERROR_NOT_IMPLEMENTED;
1256 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1257 PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
1259 NSContainer *This = NSEMBWNDS_THIS(iface);
1260 WARN("(%p)->(%08x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1261 return NS_ERROR_NOT_IMPLEMENTED;
1264 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1266 NSContainer *This = NSEMBWNDS_THIS(iface);
1268 TRACE("(%p)\n", This);
1270 return nsIBaseWindow_SetFocus(This->window);
1273 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1274 PRBool *aVisibility)
1276 NSContainer *This = NSEMBWNDS_THIS(iface);
1278 TRACE("(%p)->(%p)\n", This, aVisibility);
1280 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1281 return NS_OK;
1284 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1285 PRBool aVisibility)
1287 NSContainer *This = NSEMBWNDS_THIS(iface);
1289 TRACE("(%p)->(%x)\n", This, aVisibility);
1291 return NS_OK;
1294 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1295 PRUnichar **aTitle)
1297 NSContainer *This = NSEMBWNDS_THIS(iface);
1298 WARN("(%p)->(%p)\n", This, aTitle);
1299 return NS_ERROR_NOT_IMPLEMENTED;
1302 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1303 const PRUnichar *aTitle)
1305 NSContainer *This = NSEMBWNDS_THIS(iface);
1306 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1307 return NS_ERROR_NOT_IMPLEMENTED;
1310 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1311 void **aSiteWindow)
1313 NSContainer *This = NSEMBWNDS_THIS(iface);
1315 TRACE("(%p)->(%p)\n", This, aSiteWindow);
1317 *aSiteWindow = This->hwnd;
1318 return NS_OK;
1321 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1322 nsEmbeddingSiteWindow_QueryInterface,
1323 nsEmbeddingSiteWindow_AddRef,
1324 nsEmbeddingSiteWindow_Release,
1325 nsEmbeddingSiteWindow_SetDimensions,
1326 nsEmbeddingSiteWindow_GetDimensions,
1327 nsEmbeddingSiteWindow_SetFocus,
1328 nsEmbeddingSiteWindow_GetVisibility,
1329 nsEmbeddingSiteWindow_SetVisibility,
1330 nsEmbeddingSiteWindow_GetTitle,
1331 nsEmbeddingSiteWindow_SetTitle,
1332 nsEmbeddingSiteWindow_GetSiteWindow
1335 #define NSTOOLTIP_THIS(iface) DEFINE_THIS(NSContainer, TooltipListener, iface)
1337 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1338 nsQIResult result)
1340 NSContainer *This = NSTOOLTIP_THIS(iface);
1341 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1344 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1346 NSContainer *This = NSTOOLTIP_THIS(iface);
1347 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1350 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1352 NSContainer *This = NSTOOLTIP_THIS(iface);
1353 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1356 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1357 PRInt32 aXCoord, PRInt32 aYCoord, const PRUnichar *aTipText)
1359 NSContainer *This = NSTOOLTIP_THIS(iface);
1361 if (This->doc)
1362 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1364 return NS_OK;
1367 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1369 NSContainer *This = NSTOOLTIP_THIS(iface);
1371 if (This->doc)
1372 hide_tooltip(This->doc);
1374 return NS_OK;
1377 #undef NSTOOLTIM_THIS
1379 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1380 nsTooltipListener_QueryInterface,
1381 nsTooltipListener_AddRef,
1382 nsTooltipListener_Release,
1383 nsTooltipListener_OnShowTooltip,
1384 nsTooltipListener_OnHideTooltip
1387 #define NSIFACEREQ_THIS(iface) DEFINE_THIS(NSContainer, InterfaceRequestor, iface)
1389 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1390 nsIIDRef riid, nsQIResult result)
1392 NSContainer *This = NSIFACEREQ_THIS(iface);
1393 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1396 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1398 NSContainer *This = NSIFACEREQ_THIS(iface);
1399 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1402 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1404 NSContainer *This = NSIFACEREQ_THIS(iface);
1405 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1408 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1409 nsIIDRef riid, nsQIResult result)
1411 NSContainer *This = NSIFACEREQ_THIS(iface);
1413 if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1414 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1415 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1418 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1421 #undef NSIFACEREQ_THIS
1423 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1424 nsInterfaceRequestor_QueryInterface,
1425 nsInterfaceRequestor_AddRef,
1426 nsInterfaceRequestor_Release,
1427 nsInterfaceRequestor_GetInterface
1430 #define NSWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, WeakReference, iface)
1432 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1433 nsIIDRef riid, nsQIResult result)
1435 NSContainer *This = NSWEAKREF_THIS(iface);
1436 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1439 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1441 NSContainer *This = NSWEAKREF_THIS(iface);
1442 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1445 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1447 NSContainer *This = NSWEAKREF_THIS(iface);
1448 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1451 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1452 const nsIID *riid, void **result)
1454 NSContainer *This = NSWEAKREF_THIS(iface);
1455 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1458 #undef NSWEAKREF_THIS
1460 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1461 nsWeakReference_QueryInterface,
1462 nsWeakReference_AddRef,
1463 nsWeakReference_Release,
1464 nsWeakReference_QueryReferent
1467 #define NSSUPWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, SupportsWeakReference, iface)
1469 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1470 nsIIDRef riid, nsQIResult result)
1472 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1473 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1476 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1478 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1479 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1482 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1484 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1485 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1488 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1489 nsIWeakReference **_retval)
1491 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1493 TRACE("(%p)->(%p)\n", This, _retval);
1495 nsIWeakReference_AddRef(NSWEAKREF(This));
1496 *_retval = NSWEAKREF(This);
1497 return NS_OK;
1500 #undef NSWEAKREF_THIS
1502 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1503 nsSupportsWeakReference_QueryInterface,
1504 nsSupportsWeakReference_AddRef,
1505 nsSupportsWeakReference_Release,
1506 nsSupportsWeakReference_GetWeakReference
1510 NSContainer *NSContainer_Create(HTMLDocumentObj *doc, NSContainer *parent)
1512 nsIWebBrowserSetup *wbsetup;
1513 nsIScrollable *scrollable;
1514 NSContainer *ret;
1515 nsresult nsres;
1517 if(!load_gecko(TRUE))
1518 return NULL;
1520 ret = heap_alloc_zero(sizeof(NSContainer));
1522 ret->lpWebBrowserChromeVtbl = &nsWebBrowserChromeVtbl;
1523 ret->lpContextMenuListenerVtbl = &nsContextMenuListenerVtbl;
1524 ret->lpURIContentListenerVtbl = &nsURIContentListenerVtbl;
1525 ret->lpEmbeddingSiteWindowVtbl = &nsEmbeddingSiteWindowVtbl;
1526 ret->lpTooltipListenerVtbl = &nsTooltipListenerVtbl;
1527 ret->lpInterfaceRequestorVtbl = &nsInterfaceRequestorVtbl;
1528 ret->lpWeakReferenceVtbl = &nsWeakReferenceVtbl;
1529 ret->lpSupportsWeakReferenceVtbl = &nsSupportsWeakReferenceVtbl;
1531 ret->doc = doc;
1532 ret->ref = 1;
1534 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1535 NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
1536 if(NS_FAILED(nsres)) {
1537 ERR("Creating WebBrowser failed: %08x\n", nsres);
1538 heap_free(ret);
1539 return NULL;
1542 if(parent)
1543 nsIWebBrowserChrome_AddRef(NSWBCHROME(parent));
1544 ret->parent = parent;
1546 nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, NSWBCHROME(ret));
1547 if(NS_FAILED(nsres))
1548 ERR("SetContainerWindow failed: %08x\n", nsres);
1550 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
1551 (void**)&ret->window);
1552 if(NS_FAILED(nsres))
1553 ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1555 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
1556 (void**)&wbsetup);
1557 if(NS_SUCCEEDED(nsres)) {
1558 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
1559 nsIWebBrowserSetup_Release(wbsetup);
1560 if(NS_FAILED(nsres))
1561 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
1562 }else {
1563 ERR("Could not get nsIWebBrowserSetup interface\n");
1566 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
1567 (void**)&ret->navigation);
1568 if(NS_FAILED(nsres))
1569 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1571 nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
1572 (void**)&ret->focus);
1573 if(NS_FAILED(nsres))
1574 ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
1576 if(!nscontainer_class)
1577 register_nscontainer_class();
1579 ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1580 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1581 GetDesktopWindow(), NULL, hInst, ret);
1583 nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
1584 if(NS_SUCCEEDED(nsres)) {
1585 nsres = nsIBaseWindow_Create(ret->window);
1586 if(NS_FAILED(nsres))
1587 WARN("Creating window failed: %08x\n", nsres);
1589 nsIBaseWindow_SetVisibility(ret->window, FALSE);
1590 nsIBaseWindow_SetEnabled(ret->window, FALSE);
1591 }else {
1592 ERR("InitWindow failed: %08x\n", nsres);
1595 nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser, NSURICL(ret));
1596 if(NS_FAILED(nsres))
1597 ERR("SetParentURIContentListener failed: %08x\n", nsres);
1599 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
1600 if(NS_SUCCEEDED(nsres)) {
1601 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1602 ScrollOrientation_Y, Scrollbar_Always);
1603 if(NS_FAILED(nsres))
1604 ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
1606 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1607 ScrollOrientation_X, Scrollbar_Auto);
1608 if(NS_FAILED(nsres))
1609 ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
1611 nsIScrollable_Release(scrollable);
1612 }else {
1613 ERR("Could not get nsIScrollable: %08x\n", nsres);
1616 return ret;
1619 void NSContainer_Release(NSContainer *This)
1621 TRACE("(%p)\n", This);
1623 This->doc = NULL;
1625 ShowWindow(This->hwnd, SW_HIDE);
1626 SetParent(This->hwnd, NULL);
1628 nsIBaseWindow_SetVisibility(This->window, FALSE);
1629 nsIBaseWindow_Destroy(This->window);
1631 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1633 nsIWebBrowser_Release(This->webbrowser);
1634 This->webbrowser = NULL;
1636 nsIWebNavigation_Release(This->navigation);
1637 This->navigation = NULL;
1639 nsIBaseWindow_Release(This->window);
1640 This->window = NULL;
1642 nsIWebBrowserFocus_Release(This->focus);
1643 This->focus = NULL;
1645 if(This->editor_controller) {
1646 nsIController_Release(This->editor_controller);
1647 This->editor_controller = NULL;
1650 if(This->editor) {
1651 nsIEditor_Release(This->editor);
1652 This->editor = NULL;
1655 if(This->content_listener) {
1656 nsIURIContentListener_Release(This->content_listener);
1657 This->content_listener = NULL;
1660 if(This->hwnd) {
1661 DestroyWindow(This->hwnd);
1662 This->hwnd = NULL;
1665 nsIWebBrowserChrome_Release(NSWBCHROME(This));