mshtml: Store reference to document node in window object.
[wine.git] / dlls / mshtml / nsembed.c
blob61759e9cb9c586c1eae21f11afd6bdb2965302aa
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 void *d3;
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*);
71 static HINSTANCE hXPCOM = NULL;
73 static nsIServiceManager *pServMgr = NULL;
74 static nsIComponentManager *pCompMgr = NULL;
75 static nsIMemory *nsmem = NULL;
77 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
79 static ATOM nscontainer_class;
81 #define WM_RESETFOCUS_HACK WM_USER+600
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_RESETFOCUS_HACK:
109 * FIXME
110 * Gecko grabs focus in edit mode and some apps don't like it.
111 * We should somehow prevent grabbing focus.
114 TRACE("WM_RESETFOCUS_HACK\n");
116 if(This->reset_focus) {
117 SetFocus(This->reset_focus);
118 This->reset_focus = NULL;
119 if(This->doc)
120 This->doc->focus = FALSE;
124 return DefWindowProcW(hwnd, msg, wParam, lParam);
128 static void register_nscontainer_class(void)
130 static WNDCLASSEXW wndclass = {
131 sizeof(WNDCLASSEXW),
132 CS_DBLCLKS,
133 nsembed_proc,
134 0, 0, NULL, NULL, NULL, NULL, NULL,
135 wszNsContainer,
136 NULL,
138 wndclass.hInstance = hInst;
139 nscontainer_class = RegisterClassExW(&wndclass);
142 static void set_environment(LPCWSTR gre_path)
144 WCHAR path_env[MAX_PATH], buf[20];
145 int len, debug_level = 0;
147 static const WCHAR pathW[] = {'P','A','T','H',0};
148 static const WCHAR warnW[] = {'w','a','r','n',0};
149 static const WCHAR xpcom_debug_breakW[] =
150 {'X','P','C','O','M','_','D','E','B','U','G','_','B','R','E','A','K',0};
151 static const WCHAR nspr_log_modulesW[] =
152 {'N','S','P','R','_','L','O','G','_','M','O','D','U','L','E','S',0};
153 static const WCHAR debug_formatW[] = {'a','l','l',':','%','d',0};
155 /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
156 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
157 len = strlenW(path_env);
158 path_env[len++] = ';';
159 strcpyW(path_env+len, gre_path);
160 SetEnvironmentVariableW(pathW, path_env);
162 SetEnvironmentVariableW(xpcom_debug_breakW, warnW);
164 if(TRACE_ON(gecko))
165 debug_level = 5;
166 else if(WARN_ON(gecko))
167 debug_level = 3;
168 else if(ERR_ON(gecko))
169 debug_level = 2;
171 sprintfW(buf, debug_formatW, debug_level);
172 SetEnvironmentVariableW(nspr_log_modulesW, buf);
175 static BOOL load_xpcom(const PRUnichar *gre_path)
177 static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
179 TRACE("(%s)\n", debugstr_w(gre_path));
181 set_environment(gre_path);
183 hXPCOM = LoadLibraryW(strXPCOM);
184 if(!hXPCOM) {
185 WARN("Could not load XPCOM: %d\n", GetLastError());
186 return FALSE;
189 #define NS_DLSYM(func) \
190 func = (void *)GetProcAddress(hXPCOM, #func); \
191 if(!func) \
192 ERR("Could not GetProcAddress(" #func ") failed\n")
194 NS_DLSYM(NS_InitXPCOM2);
195 NS_DLSYM(NS_ShutdownXPCOM);
196 NS_DLSYM(NS_GetComponentRegistrar);
197 NS_DLSYM(NS_StringContainerInit);
198 NS_DLSYM(NS_CStringContainerInit);
199 NS_DLSYM(NS_StringContainerFinish);
200 NS_DLSYM(NS_CStringContainerFinish);
201 NS_DLSYM(NS_StringSetData);
202 NS_DLSYM(NS_CStringSetData);
203 NS_DLSYM(NS_NewLocalFile);
204 NS_DLSYM(NS_StringGetData);
205 NS_DLSYM(NS_CStringGetData);
207 #undef NS_DLSYM
209 return TRUE;
212 static BOOL check_version(LPCWSTR gre_path, const char *version_string)
214 WCHAR file_name[MAX_PATH];
215 char version[128];
216 DWORD read=0;
217 HANDLE hfile;
219 static const WCHAR wszVersion[] = {'\\','V','E','R','S','I','O','N',0};
221 strcpyW(file_name, gre_path);
222 strcatW(file_name, wszVersion);
224 hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
225 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
226 if(hfile == INVALID_HANDLE_VALUE) {
227 ERR("Could not open VERSION file\n");
228 return FALSE;
231 ReadFile(hfile, version, sizeof(version), &read, NULL);
232 version[read] = 0;
233 CloseHandle(hfile);
235 TRACE("%s\n", debugstr_a(version));
237 if(strcmp(version, version_string)) {
238 ERR("Unexpected version %s, expected %s\n", debugstr_a(version),
239 debugstr_a(version_string));
240 return FALSE;
243 return TRUE;
246 static BOOL load_wine_gecko_v(PRUnichar *gre_path, HKEY mshtml_key,
247 const char *version, const char *version_string)
249 DWORD res, type, size = MAX_PATH;
250 HKEY hkey = mshtml_key;
252 static const WCHAR wszGeckoPath[] =
253 {'G','e','c','k','o','P','a','t','h',0};
255 if(version) {
256 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
257 res = RegOpenKeyA(mshtml_key, version, &hkey);
258 if(res != ERROR_SUCCESS)
259 return FALSE;
262 res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
263 if(hkey != mshtml_key)
264 RegCloseKey(hkey);
265 if(res != ERROR_SUCCESS || type != REG_SZ)
266 return FALSE;
268 if(!check_version(gre_path, version_string))
269 return FALSE;
271 return load_xpcom(gre_path);
274 static BOOL load_wine_gecko(PRUnichar *gre_path)
276 HKEY hkey;
277 DWORD res;
278 BOOL ret;
280 static const WCHAR wszMshtmlKey[] = {
281 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
282 '\\','M','S','H','T','M','L',0};
284 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
285 res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
286 if(res != ERROR_SUCCESS)
287 return FALSE;
289 ret = load_wine_gecko_v(gre_path, hkey, GECKO_VERSION, GECKO_VERSION_STRING);
291 RegCloseKey(hkey);
292 return ret;
295 static void set_bool_pref(nsIPrefBranch *pref, const char *pref_name, BOOL val)
297 nsresult nsres;
299 nsres = nsIPrefBranch_SetBoolPref(pref, pref_name, val);
300 if(NS_FAILED(nsres))
301 ERR("Could not set pref %s\n", debugstr_a(pref_name));
304 static void set_int_pref(nsIPrefBranch *pref, const char *pref_name, int val)
306 nsresult nsres;
308 nsres = nsIPrefBranch_SetIntPref(pref, pref_name, val);
309 if(NS_FAILED(nsres))
310 ERR("Could not set pref %s\n", debugstr_a(pref_name));
313 static void set_string_pref(nsIPrefBranch *pref, const char *pref_name, const char *val)
315 nsresult nsres;
317 nsres = nsIPrefBranch_SetCharPref(pref, pref_name, val);
318 if(NS_FAILED(nsres))
319 ERR("Could not set pref %s\n", debugstr_a(pref_name));
322 static void set_lang(nsIPrefBranch *pref)
324 char langs[100];
325 DWORD res, size, type;
326 HKEY hkey;
328 static const WCHAR international_keyW[] =
329 {'S','o','f','t','w','a','r','e',
330 '\\','M','i','c','r','o','s','o','f','t',
331 '\\','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
332 '\\','I','n','t','e','r','n','a','t','i','o','n','a','l',0};
334 res = RegOpenKeyW(HKEY_CURRENT_USER, international_keyW, &hkey);
335 if(res != ERROR_SUCCESS)
336 return;
338 size = sizeof(langs);
339 res = RegQueryValueExA(hkey, "AcceptLanguage", 0, &type, (LPBYTE)langs, &size);
340 RegCloseKey(hkey);
341 if(res != ERROR_SUCCESS || type != REG_SZ)
342 return;
344 TRACE("Setting lang %s\n", debugstr_a(langs));
346 set_string_pref(pref, "intl.accept_languages", langs);
349 static void set_proxy(nsIPrefBranch *pref)
351 char proxy[512];
352 char * proxy_port;
353 int proxy_port_num;
354 DWORD enabled = 0, res, size, type;
355 HKEY hkey;
357 static const WCHAR proxy_keyW[] =
358 {'S','o','f','t','w','a','r','e',
359 '\\','M','i','c','r','o','s','o','f','t',
360 '\\','W','i','n','d','o','w','s',
361 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
362 '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
364 res = RegOpenKeyW(HKEY_CURRENT_USER, proxy_keyW, &hkey);
365 if(res != ERROR_SUCCESS)
366 return;
368 size = sizeof(enabled);
369 res = RegQueryValueExA(hkey, "ProxyEnable", 0, &type, (LPBYTE)&enabled, &size);
370 if(res != ERROR_SUCCESS || type != REG_DWORD || enabled == 0)
372 RegCloseKey(hkey);
373 return;
376 size = sizeof(proxy);
377 res = RegQueryValueExA(hkey, "ProxyServer", 0, &type, (LPBYTE)proxy, &size);
378 RegCloseKey(hkey);
379 if(res != ERROR_SUCCESS || type != REG_SZ)
380 return;
382 proxy_port = strchr(proxy, ':');
383 if (!proxy_port)
384 return;
386 *proxy_port = 0;
387 proxy_port_num = atoi(proxy_port + 1);
388 TRACE("Setting proxy to %s, port %d\n", debugstr_a(proxy), proxy_port_num);
390 set_string_pref(pref, "network.proxy.http", proxy);
391 set_string_pref(pref, "network.proxy.ssl", proxy);
393 set_int_pref(pref, "network.proxy.type", 1);
394 set_int_pref(pref, "network.proxy.http_port", proxy_port_num);
395 set_int_pref(pref, "network.proxy.ssl_port", proxy_port_num);
398 static void set_preferences(void)
400 nsIPrefBranch *pref;
401 nsresult nsres;
403 nsres = nsIServiceManager_GetServiceByContractID(pServMgr, NS_PREFERENCES_CONTRACTID,
404 &IID_nsIPrefBranch, (void**)&pref);
405 if(NS_FAILED(nsres)) {
406 ERR("Could not get preference service: %08x\n", nsres);
407 return;
410 set_lang(pref);
411 set_proxy(pref);
412 set_bool_pref(pref, "security.warn_entering_secure", FALSE);
413 set_bool_pref(pref, "security.warn_submit_insecure", FALSE);
414 set_int_pref(pref, "layout.spellcheckDefault", 0);
416 nsIPrefBranch_Release(pref);
419 static BOOL init_xpcom(const PRUnichar *gre_path)
421 nsresult nsres;
422 nsIObserver *pStartNotif;
423 nsIComponentRegistrar *registrar = NULL;
424 nsAString path;
425 nsIFile *gre_dir;
427 nsAString_Init(&path, gre_path);
428 nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
429 nsAString_Finish(&path);
430 if(NS_FAILED(nsres)) {
431 ERR("NS_NewLocalFile failed: %08x\n", nsres);
432 FreeLibrary(hXPCOM);
433 return FALSE;
436 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
437 if(NS_FAILED(nsres)) {
438 ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
439 FreeLibrary(hXPCOM);
440 return FALSE;
443 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
444 if(NS_FAILED(nsres))
445 ERR("Could not get nsIComponentManager: %08x\n", nsres);
447 nsres = NS_GetComponentRegistrar(&registrar);
448 if(NS_SUCCEEDED(nsres)) {
449 nsres = nsIComponentRegistrar_AutoRegister(registrar, NULL);
450 if(NS_FAILED(nsres))
451 ERR("AutoRegister(NULL) failed: %08x\n", nsres);
453 init_nsio(pCompMgr, registrar);
454 }else {
455 ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
458 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
459 NULL, &IID_nsIObserver, (void**)&pStartNotif);
460 if(NS_SUCCEEDED(nsres)) {
461 nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
462 if(NS_FAILED(nsres))
463 ERR("Observe failed: %08x\n", nsres);
465 nsIObserver_Release(pStartNotif);
466 }else {
467 ERR("could not get appstartup-notifier: %08x\n", nsres);
470 set_preferences();
472 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_MEMORY_CONTRACTID,
473 NULL, &IID_nsIMemory, (void**)&nsmem);
474 if(NS_FAILED(nsres))
475 ERR("Could not get nsIMemory: %08x\n", nsres);
477 if(registrar) {
478 register_nsservice(registrar, pServMgr);
479 nsIComponentRegistrar_Release(registrar);
482 return TRUE;
485 static CRITICAL_SECTION cs_load_gecko;
486 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
488 0, 0, &cs_load_gecko,
489 { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
490 0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
492 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
494 BOOL load_gecko(BOOL silent)
496 PRUnichar gre_path[MAX_PATH];
497 BOOL ret = FALSE;
499 static DWORD loading_thread;
501 TRACE("()\n");
503 /* load_gecko may be called recursively */
504 if(loading_thread == GetCurrentThreadId())
505 return pCompMgr != NULL;
507 EnterCriticalSection(&cs_load_gecko);
509 if(!loading_thread) {
510 loading_thread = GetCurrentThreadId();
512 if(load_wine_gecko(gre_path)
513 || (install_wine_gecko(silent) && load_wine_gecko(gre_path)))
514 ret = init_xpcom(gre_path);
515 else
516 MESSAGE("Could not load wine-gecko. HTML rendering will be disabled.\n");
517 }else {
518 ret = pCompMgr != NULL;
521 LeaveCriticalSection(&cs_load_gecko);
523 return ret;
526 void *nsalloc(size_t size)
528 return nsIMemory_Alloc(nsmem, size);
531 void nsfree(void *mem)
533 nsIMemory_Free(nsmem, mem);
536 void nsACString_Init(nsACString *str, const char *data)
538 NS_CStringContainerInit(str);
539 if(data)
540 nsACString_SetData(str, data);
543 void nsACString_SetData(nsACString *str, const char *data)
545 NS_CStringSetData(str, data, PR_UINT32_MAX);
548 PRUint32 nsACString_GetData(const nsACString *str, const char **data)
550 return NS_CStringGetData(str, data, NULL);
553 void nsACString_Finish(nsACString *str)
555 NS_CStringContainerFinish(str);
558 void nsAString_Init(nsAString *str, const PRUnichar *data)
560 NS_StringContainerInit(str);
561 if(data)
562 nsAString_SetData(str, data);
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->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 set_ns_editmode(NSContainer *This)
772 nsIEditingSession *editing_session = NULL;
773 nsIURIContentListener *listener = NULL;
774 nsIDOMWindow *dom_window = NULL;
775 nsresult nsres;
777 nsres = get_nsinterface((nsISupports*)This->webbrowser, &IID_nsIEditingSession,
778 (void**)&editing_session);
779 if(NS_FAILED(nsres)) {
780 ERR("Could not get nsIEditingSession: %08x\n", nsres);
781 return;
784 nsres = nsIWebBrowser_GetContentDOMWindow(This->webbrowser, &dom_window);
785 if(NS_FAILED(nsres)) {
786 ERR("Could not get content DOM window: %08x\n", nsres);
787 nsIEditingSession_Release(editing_session);
788 return;
791 nsres = nsIEditingSession_MakeWindowEditable(editing_session, dom_window,
792 NULL, FALSE, TRUE, TRUE);
793 nsIEditingSession_Release(editing_session);
794 nsIDOMWindow_Release(dom_window);
795 if(NS_FAILED(nsres)) {
796 ERR("MakeWindowEditable failed: %08x\n", nsres);
797 return;
800 /* MakeWindowEditable changes WebBrowser's parent URI content listener.
801 * It seams to be a bug in Gecko. To workaround it we set our content
802 * listener again and Gecko's one as its parent.
804 nsIWebBrowser_GetParentURIContentListener(This->webbrowser, &listener);
805 nsIURIContentListener_SetParentContentListener(NSURICL(This), listener);
806 nsIURIContentListener_Release(listener);
807 nsIWebBrowser_SetParentURIContentListener(This->webbrowser, NSURICL(This));
810 void update_nsdocument(HTMLDocumentObj *doc)
812 HTMLDocumentNode *doc_node;
813 nsIDOMHTMLDocument *nsdoc;
814 nsIDOMDocument *nsdomdoc;
815 nsresult nsres;
816 HRESULT hres;
818 if(!doc->basedoc.nscontainer || !doc->basedoc.nscontainer->navigation)
819 return;
821 nsres = nsIWebNavigation_GetDocument(doc->basedoc.nscontainer->navigation, &nsdomdoc);
822 if(NS_FAILED(nsres) || !nsdomdoc) {
823 ERR("GetDocument failed: %08x\n", nsres);
824 return;
827 nsres = nsIDOMDocument_QueryInterface(nsdomdoc, &IID_nsIDOMHTMLDocument, (void**)&nsdoc);
828 nsIDOMDocument_Release(nsdomdoc);
829 if(NS_FAILED(nsres)) {
830 ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
831 return;
834 if(nsdoc == doc->basedoc.nsdoc) {
835 nsIDOMHTMLDocument_Release(nsdoc);
836 return;
839 if(doc->basedoc.nsdoc) {
840 remove_mutation_observer(doc->basedoc.nscontainer, doc->basedoc.nsdoc);
841 nsIDOMHTMLDocument_Release(doc->basedoc.nsdoc);
843 doc_node = doc->basedoc.doc_node;
844 doc_node->basedoc.doc_obj = NULL;
845 IHTMLDocument2_Release(HTMLDOC(&doc_node->basedoc));
846 doc->basedoc.doc_node = NULL;
849 doc->basedoc.nsdoc = nsdoc;
850 if(!nsdoc) {
851 window_set_docnode(doc->basedoc.window, NULL);
852 return;
855 set_mutation_observer(doc->basedoc.nscontainer, nsdoc);
857 hres = create_doc_from_nsdoc(nsdoc, doc, doc->basedoc.window, &doc_node);
858 if(FAILED(hres)) {
859 ERR("Could not create document: %08x\n", hres);
860 return;
863 doc->basedoc.doc_node = doc_node;
864 window_set_docnode(doc->basedoc.window, doc_node);
867 void close_gecko(void)
869 TRACE("()\n");
871 release_nsio();
873 if(pCompMgr)
874 nsIComponentManager_Release(pCompMgr);
876 if(pServMgr)
877 nsIServiceManager_Release(pServMgr);
879 if(nsmem)
880 nsIMemory_Release(nsmem);
882 /* Gecko doesn't really support being unloaded */
883 /* if (hXPCOM) FreeLibrary(hXPCOM); */
886 /**********************************************************
887 * nsIWebBrowserChrome interface
890 #define NSWBCHROME_THIS(iface) DEFINE_THIS(NSContainer, WebBrowserChrome, iface)
892 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
893 nsIIDRef riid, nsQIResult result)
895 NSContainer *This = NSWBCHROME_THIS(iface);
897 *result = NULL;
898 if(IsEqualGUID(&IID_nsISupports, riid)) {
899 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
900 *result = NSWBCHROME(This);
901 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
902 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
903 *result = NSWBCHROME(This);
904 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
905 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
906 *result = NSCML(This);
907 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
908 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
909 *result = NSURICL(This);
910 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
911 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
912 *result = NSEMBWNDS(This);
913 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
914 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
915 *result = NSTOOLTIP(This);
916 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
917 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
918 *result = NSIFACEREQ(This);
919 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
920 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
921 *result = NSWEAKREF(This);
922 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
923 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
924 *result = NSSUPWEAKREF(This);
927 if(*result) {
928 nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
929 return NS_OK;
932 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
933 return NS_NOINTERFACE;
936 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
938 NSContainer *This = NSWBCHROME_THIS(iface);
939 LONG ref = InterlockedIncrement(&This->ref);
941 TRACE("(%p) ref=%d\n", This, ref);
943 return ref;
946 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
948 NSContainer *This = NSWBCHROME_THIS(iface);
949 LONG ref = InterlockedDecrement(&This->ref);
951 TRACE("(%p) ref=%d\n", This, ref);
953 if(!ref) {
954 if(This->parent)
955 nsIWebBrowserChrome_Release(NSWBCHROME(This->parent));
956 heap_free(This);
959 return ref;
962 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
963 PRUint32 statusType, const PRUnichar *status)
965 NSContainer *This = NSWBCHROME_THIS(iface);
967 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
969 /* FIXME: This hack should be removed when we'll load all pages by URLMoniker */
970 if(This->doc)
971 update_nsdocument(This->doc->doc_obj);
973 return NS_OK;
976 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
977 nsIWebBrowser **aWebBrowser)
979 NSContainer *This = NSWBCHROME_THIS(iface);
981 TRACE("(%p)->(%p)\n", This, aWebBrowser);
983 if(!aWebBrowser)
984 return NS_ERROR_INVALID_ARG;
986 if(This->webbrowser)
987 nsIWebBrowser_AddRef(This->webbrowser);
988 *aWebBrowser = This->webbrowser;
989 return S_OK;
992 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
993 nsIWebBrowser *aWebBrowser)
995 NSContainer *This = NSWBCHROME_THIS(iface);
997 TRACE("(%p)->(%p)\n", This, aWebBrowser);
999 if(aWebBrowser != This->webbrowser)
1000 ERR("Wrong nsWebBrowser!\n");
1002 return NS_OK;
1005 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
1006 PRUint32 *aChromeFlags)
1008 NSContainer *This = NSWBCHROME_THIS(iface);
1009 WARN("(%p)->(%p)\n", This, aChromeFlags);
1010 return NS_ERROR_NOT_IMPLEMENTED;
1013 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
1014 PRUint32 aChromeFlags)
1016 NSContainer *This = NSWBCHROME_THIS(iface);
1017 WARN("(%p)->(%08x)\n", This, aChromeFlags);
1018 return NS_ERROR_NOT_IMPLEMENTED;
1021 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
1023 NSContainer *This = NSWBCHROME_THIS(iface);
1024 TRACE("(%p)\n", This);
1025 return NS_ERROR_NOT_IMPLEMENTED;
1028 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
1029 PRInt32 aCX, PRInt32 aCY)
1031 NSContainer *This = NSWBCHROME_THIS(iface);
1032 WARN("(%p)->(%d %d)\n", This, aCX, aCY);
1033 return NS_ERROR_NOT_IMPLEMENTED;
1036 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
1038 NSContainer *This = NSWBCHROME_THIS(iface);
1039 WARN("(%p)\n", This);
1040 return NS_ERROR_NOT_IMPLEMENTED;
1043 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
1045 NSContainer *This = NSWBCHROME_THIS(iface);
1046 WARN("(%p)->(%p)\n", This, _retval);
1047 return NS_ERROR_NOT_IMPLEMENTED;
1050 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
1051 nsresult aStatus)
1053 NSContainer *This = NSWBCHROME_THIS(iface);
1054 WARN("(%p)->(%08x)\n", This, aStatus);
1055 return NS_ERROR_NOT_IMPLEMENTED;
1058 #undef NSWBCHROME_THIS
1060 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
1061 nsWebBrowserChrome_QueryInterface,
1062 nsWebBrowserChrome_AddRef,
1063 nsWebBrowserChrome_Release,
1064 nsWebBrowserChrome_SetStatus,
1065 nsWebBrowserChrome_GetWebBrowser,
1066 nsWebBrowserChrome_SetWebBrowser,
1067 nsWebBrowserChrome_GetChromeFlags,
1068 nsWebBrowserChrome_SetChromeFlags,
1069 nsWebBrowserChrome_DestroyBrowserWindow,
1070 nsWebBrowserChrome_SizeBrowserTo,
1071 nsWebBrowserChrome_ShowAsModal,
1072 nsWebBrowserChrome_IsWindowModal,
1073 nsWebBrowserChrome_ExitModalEventLoop
1076 /**********************************************************
1077 * nsIContextMenuListener interface
1080 #define NSCML_THIS(iface) DEFINE_THIS(NSContainer, ContextMenuListener, iface)
1082 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
1083 nsIIDRef riid, nsQIResult result)
1085 NSContainer *This = NSCML_THIS(iface);
1086 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1089 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
1091 NSContainer *This = NSCML_THIS(iface);
1092 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1095 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
1097 NSContainer *This = NSCML_THIS(iface);
1098 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1101 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
1102 PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
1104 NSContainer *This = NSCML_THIS(iface);
1105 nsIDOMMouseEvent *event;
1106 POINT pt;
1107 DWORD dwID = CONTEXT_MENU_DEFAULT;
1108 nsresult nsres;
1110 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
1112 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
1113 if(NS_FAILED(nsres)) {
1114 ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
1115 return nsres;
1118 nsIDOMMouseEvent_GetScreenX(event, &pt.x);
1119 nsIDOMMouseEvent_GetScreenY(event, &pt.y);
1120 nsIDOMMouseEvent_Release(event);
1122 switch(aContextFlags) {
1123 case CONTEXT_NONE:
1124 case CONTEXT_DOCUMENT:
1125 case CONTEXT_TEXT:
1126 dwID = CONTEXT_MENU_DEFAULT;
1127 break;
1128 case CONTEXT_IMAGE:
1129 case CONTEXT_IMAGE|CONTEXT_LINK:
1130 dwID = CONTEXT_MENU_IMAGE;
1131 break;
1132 case CONTEXT_LINK:
1133 dwID = CONTEXT_MENU_ANCHOR;
1134 break;
1135 case CONTEXT_INPUT:
1136 dwID = CONTEXT_MENU_CONTROL;
1137 break;
1138 default:
1139 FIXME("aContextFlags=%08x\n", aContextFlags);
1142 show_context_menu(This->doc, dwID, &pt, (IDispatch*)HTMLDOMNODE(get_node(This->doc, aNode, TRUE)));
1144 return NS_OK;
1147 #undef NSCML_THIS
1149 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1150 nsContextMenuListener_QueryInterface,
1151 nsContextMenuListener_AddRef,
1152 nsContextMenuListener_Release,
1153 nsContextMenuListener_OnShowContextMenu
1156 /**********************************************************
1157 * nsIURIContentListener interface
1160 #define NSURICL_THIS(iface) DEFINE_THIS(NSContainer, URIContentListener, iface)
1162 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1163 nsIIDRef riid, nsQIResult result)
1165 NSContainer *This = NSURICL_THIS(iface);
1166 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1169 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1171 NSContainer *This = NSURICL_THIS(iface);
1172 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1175 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1177 NSContainer *This = NSURICL_THIS(iface);
1178 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1181 static BOOL translate_url(HTMLDocument *doc, nsIWineURI *nsuri)
1183 OLECHAR *new_url = NULL, *url;
1184 BOOL ret = FALSE;
1185 LPCWSTR wine_url;
1186 HRESULT hres;
1188 if(!doc->hostui)
1189 return FALSE;
1191 nsIWineURI_GetWineURL(nsuri, &wine_url);
1193 url = heap_strdupW(wine_url);
1194 hres = IDocHostUIHandler_TranslateUrl(doc->hostui, 0, url, &new_url);
1195 heap_free(url);
1196 if(hres != S_OK || !new_url)
1197 return FALSE;
1199 if(strcmpW(url, new_url)) {
1200 FIXME("TranslateUrl returned new URL %s -> %s\n", debugstr_w(url), debugstr_w(new_url));
1201 ret = TRUE;
1204 CoTaskMemFree(new_url);
1205 return ret;
1208 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1209 nsIURI *aURI, PRBool *_retval)
1211 NSContainer *This = NSURICL_THIS(iface);
1212 nsIWineURI *wine_uri;
1213 nsACString spec_str;
1214 const char *spec;
1215 nsresult nsres;
1217 nsACString_Init(&spec_str, NULL);
1218 nsIURI_GetSpec(aURI, &spec_str);
1219 nsACString_GetData(&spec_str, &spec);
1221 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1223 nsACString_Finish(&spec_str);
1225 nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
1226 if(NS_FAILED(nsres)) {
1227 WARN("Could not get nsIWineURI interface: %08x\n", nsres);
1228 return NS_ERROR_NOT_IMPLEMENTED;
1231 nsIWineURI_SetNSContainer(wine_uri, This);
1232 nsIWineURI_SetIsDocumentURI(wine_uri, TRUE);
1234 if(This->bscallback) {
1235 IMoniker *mon = get_channelbsc_mon(This->bscallback);
1237 if(mon) {
1238 LPWSTR wine_url;
1239 HRESULT hres;
1241 hres = IMoniker_GetDisplayName(mon, NULL, 0, &wine_url);
1242 if(SUCCEEDED(hres)) {
1243 nsIWineURI_SetWineURL(wine_uri, wine_url);
1244 CoTaskMemFree(wine_url);
1245 }else {
1246 WARN("GetDisplayName failed: %08x\n", hres);
1249 IMoniker_Release(mon);
1252 *_retval = FALSE;
1253 }else if(This->doc) {
1254 *_retval = translate_url(This->doc, wine_uri);
1257 nsIWineURI_Release(wine_uri);
1259 return !*_retval && This->content_listener
1260 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1261 : NS_OK;
1264 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1265 const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
1266 nsIStreamListener **aContentHandler, PRBool *_retval)
1268 NSContainer *This = NSURICL_THIS(iface);
1270 TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1271 aRequest, aContentHandler, _retval);
1273 return This->content_listener
1274 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1275 aIsContentPreferred, aRequest, aContentHandler, _retval)
1276 : NS_ERROR_NOT_IMPLEMENTED;
1279 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1280 const char *aContentType, char **aDesiredContentType, PRBool *_retval)
1282 NSContainer *This = NSURICL_THIS(iface);
1284 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1286 /* FIXME: Should we do something here? */
1287 *_retval = TRUE;
1289 return This->content_listener
1290 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1291 aDesiredContentType, _retval)
1292 : NS_OK;
1295 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1296 const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
1297 PRBool *_retval)
1299 NSContainer *This = NSURICL_THIS(iface);
1301 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1302 aDesiredContentType, _retval);
1304 return This->content_listener
1305 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1306 aIsContentPreferred, aDesiredContentType, _retval)
1307 : NS_ERROR_NOT_IMPLEMENTED;
1310 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1311 nsISupports **aLoadCookie)
1313 NSContainer *This = NSURICL_THIS(iface);
1315 WARN("(%p)->(%p)\n", This, aLoadCookie);
1317 return This->content_listener
1318 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1319 : NS_ERROR_NOT_IMPLEMENTED;
1322 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1323 nsISupports *aLoadCookie)
1325 NSContainer *This = NSURICL_THIS(iface);
1327 WARN("(%p)->(%p)\n", This, aLoadCookie);
1329 return This->content_listener
1330 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1331 : NS_ERROR_NOT_IMPLEMENTED;
1334 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1335 nsIURIContentListener **aParentContentListener)
1337 NSContainer *This = NSURICL_THIS(iface);
1339 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1341 if(This->content_listener)
1342 nsIURIContentListener_AddRef(This->content_listener);
1344 *aParentContentListener = This->content_listener;
1345 return NS_OK;
1348 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1349 nsIURIContentListener *aParentContentListener)
1351 NSContainer *This = NSURICL_THIS(iface);
1353 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1355 if(aParentContentListener == NSURICL(This))
1356 return NS_OK;
1358 if(This->content_listener)
1359 nsIURIContentListener_Release(This->content_listener);
1361 This->content_listener = aParentContentListener;
1362 if(This->content_listener)
1363 nsIURIContentListener_AddRef(This->content_listener);
1365 return NS_OK;
1368 #undef NSURICL_THIS
1370 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1371 nsURIContentListener_QueryInterface,
1372 nsURIContentListener_AddRef,
1373 nsURIContentListener_Release,
1374 nsURIContentListener_OnStartURIOpen,
1375 nsURIContentListener_DoContent,
1376 nsURIContentListener_IsPreferred,
1377 nsURIContentListener_CanHandleContent,
1378 nsURIContentListener_GetLoadCookie,
1379 nsURIContentListener_SetLoadCookie,
1380 nsURIContentListener_GetParentContentListener,
1381 nsURIContentListener_SetParentContentListener
1384 /**********************************************************
1385 * nsIEmbeddinSiteWindow interface
1388 #define NSEMBWNDS_THIS(iface) DEFINE_THIS(NSContainer, EmbeddingSiteWindow, iface)
1390 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1391 nsIIDRef riid, nsQIResult result)
1393 NSContainer *This = NSEMBWNDS_THIS(iface);
1394 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1397 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1399 NSContainer *This = NSEMBWNDS_THIS(iface);
1400 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1403 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1405 NSContainer *This = NSEMBWNDS_THIS(iface);
1406 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1409 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1410 PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
1412 NSContainer *This = NSEMBWNDS_THIS(iface);
1413 WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1414 return NS_ERROR_NOT_IMPLEMENTED;
1417 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1418 PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
1420 NSContainer *This = NSEMBWNDS_THIS(iface);
1421 WARN("(%p)->(%08x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1422 return NS_ERROR_NOT_IMPLEMENTED;
1425 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1427 NSContainer *This = NSEMBWNDS_THIS(iface);
1429 TRACE("(%p)\n", This);
1431 if(This->reset_focus)
1432 PostMessageW(This->hwnd, WM_RESETFOCUS_HACK, 0, 0);
1434 return nsIBaseWindow_SetFocus(This->window);
1437 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1438 PRBool *aVisibility)
1440 NSContainer *This = NSEMBWNDS_THIS(iface);
1442 TRACE("(%p)->(%p)\n", This, aVisibility);
1444 *aVisibility = This->doc && This->doc->hwnd && IsWindowVisible(This->doc->hwnd);
1445 return NS_OK;
1448 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1449 PRBool aVisibility)
1451 NSContainer *This = NSEMBWNDS_THIS(iface);
1453 TRACE("(%p)->(%x)\n", This, aVisibility);
1455 return NS_OK;
1458 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1459 PRUnichar **aTitle)
1461 NSContainer *This = NSEMBWNDS_THIS(iface);
1462 WARN("(%p)->(%p)\n", This, aTitle);
1463 return NS_ERROR_NOT_IMPLEMENTED;
1466 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1467 const PRUnichar *aTitle)
1469 NSContainer *This = NSEMBWNDS_THIS(iface);
1470 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1471 return NS_ERROR_NOT_IMPLEMENTED;
1474 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1475 void **aSiteWindow)
1477 NSContainer *This = NSEMBWNDS_THIS(iface);
1479 TRACE("(%p)->(%p)\n", This, aSiteWindow);
1481 *aSiteWindow = This->hwnd;
1482 return NS_OK;
1485 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1486 nsEmbeddingSiteWindow_QueryInterface,
1487 nsEmbeddingSiteWindow_AddRef,
1488 nsEmbeddingSiteWindow_Release,
1489 nsEmbeddingSiteWindow_SetDimensions,
1490 nsEmbeddingSiteWindow_GetDimensions,
1491 nsEmbeddingSiteWindow_SetFocus,
1492 nsEmbeddingSiteWindow_GetVisibility,
1493 nsEmbeddingSiteWindow_SetVisibility,
1494 nsEmbeddingSiteWindow_GetTitle,
1495 nsEmbeddingSiteWindow_SetTitle,
1496 nsEmbeddingSiteWindow_GetSiteWindow
1499 #define NSTOOLTIP_THIS(iface) DEFINE_THIS(NSContainer, TooltipListener, iface)
1501 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1502 nsQIResult result)
1504 NSContainer *This = NSTOOLTIP_THIS(iface);
1505 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1508 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1510 NSContainer *This = NSTOOLTIP_THIS(iface);
1511 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1514 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1516 NSContainer *This = NSTOOLTIP_THIS(iface);
1517 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1520 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1521 PRInt32 aXCoord, PRInt32 aYCoord, const PRUnichar *aTipText)
1523 NSContainer *This = NSTOOLTIP_THIS(iface);
1525 if (This->doc)
1526 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1528 return NS_OK;
1531 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1533 NSContainer *This = NSTOOLTIP_THIS(iface);
1535 if (This->doc)
1536 hide_tooltip(This->doc);
1538 return NS_OK;
1541 #undef NSTOOLTIM_THIS
1543 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1544 nsTooltipListener_QueryInterface,
1545 nsTooltipListener_AddRef,
1546 nsTooltipListener_Release,
1547 nsTooltipListener_OnShowTooltip,
1548 nsTooltipListener_OnHideTooltip
1551 #define NSIFACEREQ_THIS(iface) DEFINE_THIS(NSContainer, InterfaceRequestor, iface)
1553 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1554 nsIIDRef riid, nsQIResult result)
1556 NSContainer *This = NSIFACEREQ_THIS(iface);
1557 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1560 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1562 NSContainer *This = NSIFACEREQ_THIS(iface);
1563 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1566 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1568 NSContainer *This = NSIFACEREQ_THIS(iface);
1569 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1572 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1573 nsIIDRef riid, nsQIResult result)
1575 NSContainer *This = NSIFACEREQ_THIS(iface);
1577 if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1578 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1579 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1582 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1585 #undef NSIFACEREQ_THIS
1587 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1588 nsInterfaceRequestor_QueryInterface,
1589 nsInterfaceRequestor_AddRef,
1590 nsInterfaceRequestor_Release,
1591 nsInterfaceRequestor_GetInterface
1594 #define NSWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, WeakReference, iface)
1596 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1597 nsIIDRef riid, nsQIResult result)
1599 NSContainer *This = NSWEAKREF_THIS(iface);
1600 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1603 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1605 NSContainer *This = NSWEAKREF_THIS(iface);
1606 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1609 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1611 NSContainer *This = NSWEAKREF_THIS(iface);
1612 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1615 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1616 const nsIID *riid, void **result)
1618 NSContainer *This = NSWEAKREF_THIS(iface);
1619 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1622 #undef NSWEAKREF_THIS
1624 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1625 nsWeakReference_QueryInterface,
1626 nsWeakReference_AddRef,
1627 nsWeakReference_Release,
1628 nsWeakReference_QueryReferent
1631 #define NSSUPWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, SupportsWeakReference, iface)
1633 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1634 nsIIDRef riid, nsQIResult result)
1636 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1637 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1640 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1642 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1643 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1646 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1648 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1649 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1652 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1653 nsIWeakReference **_retval)
1655 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1657 TRACE("(%p)->(%p)\n", This, _retval);
1659 nsIWeakReference_AddRef(NSWEAKREF(This));
1660 *_retval = NSWEAKREF(This);
1661 return NS_OK;
1664 #undef NSWEAKREF_THIS
1666 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1667 nsSupportsWeakReference_QueryInterface,
1668 nsSupportsWeakReference_AddRef,
1669 nsSupportsWeakReference_Release,
1670 nsSupportsWeakReference_GetWeakReference
1674 NSContainer *NSContainer_Create(HTMLDocument *doc, NSContainer *parent)
1676 nsIWebBrowserSetup *wbsetup;
1677 nsIScrollable *scrollable;
1678 NSContainer *ret;
1679 nsresult nsres;
1681 if(!load_gecko(FALSE))
1682 return NULL;
1684 ret = heap_alloc_zero(sizeof(NSContainer));
1686 ret->lpWebBrowserChromeVtbl = &nsWebBrowserChromeVtbl;
1687 ret->lpContextMenuListenerVtbl = &nsContextMenuListenerVtbl;
1688 ret->lpURIContentListenerVtbl = &nsURIContentListenerVtbl;
1689 ret->lpEmbeddingSiteWindowVtbl = &nsEmbeddingSiteWindowVtbl;
1690 ret->lpTooltipListenerVtbl = &nsTooltipListenerVtbl;
1691 ret->lpInterfaceRequestorVtbl = &nsInterfaceRequestorVtbl;
1692 ret->lpWeakReferenceVtbl = &nsWeakReferenceVtbl;
1693 ret->lpSupportsWeakReferenceVtbl = &nsSupportsWeakReferenceVtbl;
1695 ret->doc = doc;
1696 ret->ref = 1;
1697 init_mutation(ret);
1699 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1700 NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
1701 if(NS_FAILED(nsres)) {
1702 ERR("Creating WebBrowser failed: %08x\n", nsres);
1703 heap_free(ret);
1704 return NULL;
1707 if(parent)
1708 nsIWebBrowserChrome_AddRef(NSWBCHROME(parent));
1709 ret->parent = parent;
1711 nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, NSWBCHROME(ret));
1712 if(NS_FAILED(nsres))
1713 ERR("SetContainerWindow failed: %08x\n", nsres);
1715 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
1716 (void**)&ret->window);
1717 if(NS_FAILED(nsres))
1718 ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1720 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
1721 (void**)&wbsetup);
1722 if(NS_SUCCEEDED(nsres)) {
1723 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, FALSE);
1724 nsIWebBrowserSetup_Release(wbsetup);
1725 if(NS_FAILED(nsres))
1726 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
1727 }else {
1728 ERR("Could not get nsIWebBrowserSetup interface\n");
1731 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
1732 (void**)&ret->navigation);
1733 if(NS_FAILED(nsres))
1734 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1736 nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
1737 (void**)&ret->focus);
1738 if(NS_FAILED(nsres))
1739 ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
1741 if(!nscontainer_class)
1742 register_nscontainer_class();
1744 ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1745 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1746 GetDesktopWindow(), NULL, hInst, ret);
1748 nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
1749 if(NS_SUCCEEDED(nsres)) {
1750 nsres = nsIBaseWindow_Create(ret->window);
1751 if(NS_FAILED(nsres))
1752 WARN("Creating window failed: %08x\n", nsres);
1754 nsIBaseWindow_SetVisibility(ret->window, FALSE);
1755 nsIBaseWindow_SetEnabled(ret->window, FALSE);
1756 }else {
1757 ERR("InitWindow failed: %08x\n", nsres);
1760 nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser, NSURICL(ret));
1761 if(NS_FAILED(nsres))
1762 ERR("SetParentURIContentListener failed: %08x\n", nsres);
1764 init_nsevents(ret);
1766 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
1767 if(NS_SUCCEEDED(nsres)) {
1768 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1769 ScrollOrientation_Y, Scrollbar_Always);
1770 if(NS_FAILED(nsres))
1771 ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
1773 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1774 ScrollOrientation_X, Scrollbar_Auto);
1775 if(NS_FAILED(nsres))
1776 ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
1778 nsIScrollable_Release(scrollable);
1779 }else {
1780 ERR("Could not get nsIScrollable: %08x\n", nsres);
1783 return ret;
1786 void NSContainer_Release(NSContainer *This)
1788 TRACE("(%p)\n", This);
1790 This->doc = NULL;
1792 ShowWindow(This->hwnd, SW_HIDE);
1793 SetParent(This->hwnd, NULL);
1795 nsIBaseWindow_SetVisibility(This->window, FALSE);
1796 nsIBaseWindow_Destroy(This->window);
1798 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1800 nsIWebBrowser_Release(This->webbrowser);
1801 This->webbrowser = NULL;
1803 nsIWebNavigation_Release(This->navigation);
1804 This->navigation = NULL;
1806 nsIBaseWindow_Release(This->window);
1807 This->window = NULL;
1809 nsIWebBrowserFocus_Release(This->focus);
1810 This->focus = NULL;
1812 if(This->editor_controller) {
1813 nsIController_Release(This->editor_controller);
1814 This->editor_controller = NULL;
1817 if(This->editor) {
1818 nsIEditor_Release(This->editor);
1819 This->editor = NULL;
1822 if(This->content_listener) {
1823 nsIURIContentListener_Release(This->content_listener);
1824 This->content_listener = NULL;
1827 if(This->hwnd) {
1828 DestroyWindow(This->hwnd);
1829 This->hwnd = NULL;
1832 nsIWebBrowserChrome_Release(NSWBCHROME(This));