Release 0.9.39.
[wine/gsoc-2012-control.git] / dlls / mshtml / nsembed.c
blob40225d5fe6230d613e4358076f30ace1922da995
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);
38 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
39 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
40 #define NS_PROFILE_CONTRACTID "@mozilla.org/profile/manager;1"
41 #define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
42 #define NS_STRINGSTREAM_CONTRACTID "@mozilla.org/io/string-input-stream;1"
43 #define NS_COMMANDPARAMS_CONTRACTID "@mozilla.org/embedcomp/command-params;1"
44 #define NS_HTMLSERIALIZER_CONTRACTID "@mozilla.org/layout/contentserializer;1?mimetype=text/html"
45 #define NS_EDITORCONTROLLER_CONTRACTID "@mozilla.org/editor/editorcontroller;1"
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 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
83 NSContainer *This;
84 nsresult nsres;
86 static const WCHAR wszTHIS[] = {'T','H','I','S',0};
88 if(msg == WM_CREATE) {
89 This = *(NSContainer**)lParam;
90 SetPropW(hwnd, wszTHIS, This);
91 }else {
92 This = (NSContainer*)GetPropW(hwnd, wszTHIS);
95 switch(msg) {
96 case WM_SIZE:
97 TRACE("(%p)->(WM_SIZE)\n", This);
99 nsres = nsIBaseWindow_SetSize(This->window,
100 LOWORD(lParam), HIWORD(lParam), TRUE);
101 if(NS_FAILED(nsres))
102 WARN("SetSize failed: %08x\n", nsres);
105 return DefWindowProcW(hwnd, msg, wParam, lParam);
109 static void register_nscontainer_class(void)
111 static WNDCLASSEXW wndclass = {
112 sizeof(WNDCLASSEXW),
113 CS_DBLCLKS,
114 nsembed_proc,
115 0, 0, NULL, NULL, NULL, NULL, NULL,
116 wszNsContainer,
117 NULL,
119 wndclass.hInstance = hInst;
120 nscontainer_class = RegisterClassExW(&wndclass);
123 static BOOL load_xpcom(const PRUnichar *gre_path)
125 WCHAR path_env[MAX_PATH];
126 int len;
128 static const WCHAR wszPATH[] = {'P','A','T','H',0};
129 static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
131 TRACE("(%s)\n", debugstr_w(gre_path));
133 /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
134 GetEnvironmentVariableW(wszPATH, path_env, sizeof(path_env)/sizeof(WCHAR));
135 len = strlenW(path_env);
136 path_env[len++] = ';';
137 strcpyW(path_env+len, gre_path);
138 SetEnvironmentVariableW(wszPATH, path_env);
140 hXPCOM = LoadLibraryW(strXPCOM);
141 if(!hXPCOM) {
142 WARN("Could not load XPCOM: %d\n", GetLastError());
143 return FALSE;
146 #define NS_DLSYM(func) \
147 func = (typeof(func))GetProcAddress(hXPCOM, #func); \
148 if(!func) \
149 ERR("Could not GetProcAddress(" #func ") failed\n")
151 NS_DLSYM(NS_InitXPCOM2);
152 NS_DLSYM(NS_ShutdownXPCOM);
153 NS_DLSYM(NS_GetComponentRegistrar);
154 NS_DLSYM(NS_StringContainerInit);
155 NS_DLSYM(NS_CStringContainerInit);
156 NS_DLSYM(NS_StringContainerFinish);
157 NS_DLSYM(NS_CStringContainerFinish);
158 NS_DLSYM(NS_StringSetData);
159 NS_DLSYM(NS_CStringSetData);
160 NS_DLSYM(NS_NewLocalFile);
161 NS_DLSYM(NS_StringGetData);
162 NS_DLSYM(NS_CStringGetData);
164 #undef NS_DLSYM
166 return TRUE;
169 static BOOL check_version(LPCWSTR gre_path, const char *version_string)
171 WCHAR file_name[MAX_PATH];
172 char version[128];
173 DWORD read=0;
174 HANDLE hfile;
176 static const WCHAR wszVersion[] = {'\\','V','E','R','S','I','O','N',0};
178 strcpyW(file_name, gre_path);
179 strcatW(file_name, wszVersion);
181 hfile = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
182 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
183 if(hfile == INVALID_HANDLE_VALUE) {
184 ERR("Could not open VERSION file\n");
185 return FALSE;
188 ReadFile(hfile, version, sizeof(version), &read, NULL);
189 version[read] = 0;
190 CloseHandle(hfile);
192 TRACE("%s\n", debugstr_a(version));
194 if(strcmp(version, version_string)) {
195 ERR("Unexpected version %s, expected %s\n", debugstr_a(version),
196 debugstr_a(version_string));
197 return FALSE;
200 return TRUE;
203 static BOOL load_wine_gecko_v(PRUnichar *gre_path, HKEY mshtml_key,
204 const char *version, const char *version_string)
206 DWORD res, type, size = MAX_PATH;
207 HKEY hkey = mshtml_key;
209 static const WCHAR wszGeckoPath[] =
210 {'G','e','c','k','o','P','a','t','h',0};
212 if(version) {
213 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
214 res = RegOpenKeyA(mshtml_key, version, &hkey);
215 if(res != ERROR_SUCCESS)
216 return FALSE;
219 res = RegQueryValueExW(hkey, wszGeckoPath, NULL, &type, (LPBYTE)gre_path, &size);
220 if(hkey != mshtml_key)
221 RegCloseKey(hkey);
222 if(res != ERROR_SUCCESS || type != REG_SZ)
223 return FALSE;
225 if(!check_version(gre_path, version_string))
226 return FALSE;
228 return load_xpcom(gre_path);
231 static BOOL load_wine_gecko(PRUnichar *gre_path)
233 HKEY hkey;
234 DWORD res;
235 BOOL ret;
237 static const WCHAR wszMshtmlKey[] = {
238 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
239 '\\','M','S','H','T','M','L',0};
241 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
242 res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
243 if(res != ERROR_SUCCESS)
244 return FALSE;
246 ret = load_wine_gecko_v(gre_path, hkey, GECKO_VERSION, GECKO_VERSION_STRING)
247 || load_wine_gecko_v(gre_path, hkey, "0.0.1", "Wine Gecko 0.0.1\n")
248 || load_wine_gecko_v(gre_path, hkey, NULL, "Wine Gecko 0.0.1\n");
250 RegCloseKey(hkey);
251 return ret;
254 static void set_profile(void)
256 nsIProfile *profile;
257 PRBool exists = FALSE;
258 nsresult nsres;
260 static const WCHAR wszMSHTML[] = {'M','S','H','T','M','L',0};
262 nsres = nsIServiceManager_GetServiceByContactID(pServMgr, NS_PROFILE_CONTRACTID,
263 &IID_nsIProfile, (void**)&profile);
264 if(NS_FAILED(nsres)) {
265 ERR("Could not get profile service: %08x\n", nsres);
266 return;
269 nsres = nsIProfile_ProfileExists(profile, wszMSHTML, &exists);
270 if(!exists) {
271 nsres = nsIProfile_CreateNewProfile(profile, wszMSHTML, NULL, NULL, FALSE);
272 if(NS_FAILED(nsres))
273 ERR("CreateNewProfile failed: %08x\n", nsres);
276 nsres = nsIProfile_SetCurrentProfile(profile, wszMSHTML);
277 if(NS_FAILED(nsres))
278 ERR("SetCurrentProfile failed: %08x\n", nsres);
280 nsIProfile_Release(profile);
283 static BOOL init_xpcom(const PRUnichar *gre_path)
285 nsresult nsres;
286 nsIObserver *pStartNotif;
287 nsIComponentRegistrar *registrar = NULL;
288 nsAString path;
289 nsIFile *gre_dir;
291 nsAString_Init(&path, gre_path);
292 nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
293 nsAString_Finish(&path);
294 if(NS_FAILED(nsres)) {
295 ERR("NS_NewLocalFile failed: %08x\n", nsres);
296 FreeLibrary(hXPCOM);
297 return FALSE;
300 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
301 if(NS_FAILED(nsres)) {
302 ERR("NS_InitXPCOM2 failed: %08x\n", nsres);
303 FreeLibrary(hXPCOM);
304 return FALSE;
307 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
308 if(NS_FAILED(nsres))
309 ERR("Could not get nsIComponentManager: %08x\n", nsres);
311 nsres = NS_GetComponentRegistrar(&registrar);
312 if(NS_SUCCEEDED(nsres)) {
313 nsres = nsIComponentRegistrar_AutoRegister(registrar, NULL);
314 if(NS_FAILED(nsres))
315 ERR("AutoRegister(NULL) failed: %08x\n", nsres);
317 nsres = nsIComponentRegistrar_AutoRegister(registrar, gre_dir);
318 if(NS_FAILED(nsres))
319 ERR("AutoRegister(gre_dir) failed: %08x\n", nsres);
321 init_nsio(pCompMgr, registrar);
322 }else {
323 ERR("NS_GetComponentRegistrar failed: %08x\n", nsres);
326 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
327 NULL, &IID_nsIObserver, (void**)&pStartNotif);
328 if(NS_SUCCEEDED(nsres)) {
329 nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
330 if(NS_FAILED(nsres))
331 ERR("Observe failed: %08x\n", nsres);
333 nsIObserver_Release(pStartNotif);
334 }else {
335 ERR("could not get appstartup-notifier: %08x\n", nsres);
338 set_profile();
340 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_MEMORY_CONTRACTID,
341 NULL, &IID_nsIMemory, (void**)&nsmem);
342 if(NS_FAILED(nsres))
343 ERR("Could not get nsIMemory: %08x\n", nsres);
345 if(registrar) {
346 register_nsservice(registrar, pServMgr);
347 nsIComponentRegistrar_Release(registrar);
350 return TRUE;
353 static CRITICAL_SECTION cs_load_gecko;
354 static CRITICAL_SECTION_DEBUG cs_load_gecko_dbg =
356 0, 0, &cs_load_gecko,
357 { &cs_load_gecko_dbg.ProcessLocksList, &cs_load_gecko_dbg.ProcessLocksList },
358 0, 0, { (DWORD_PTR)(__FILE__ ": load_gecko") }
360 static CRITICAL_SECTION cs_load_gecko = { &cs_load_gecko_dbg, -1, 0, 0, 0, 0 };
362 static BOOL load_gecko(void)
364 PRUnichar gre_path[MAX_PATH];
365 BOOL ret = FALSE;
367 static LONG loading_thread;
369 TRACE("()\n");
371 /* load_gecko may be called recursively */
372 if(loading_thread == GetCurrentThreadId())
373 return pCompMgr != NULL;
375 EnterCriticalSection(&cs_load_gecko);
377 if(!loading_thread) {
378 loading_thread = GetCurrentThreadId();
380 if(load_wine_gecko(gre_path)
381 || (install_wine_gecko() && load_wine_gecko(gre_path)))
382 ret = init_xpcom(gre_path);
383 else
384 MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
385 }else {
386 ret = pCompMgr != NULL;
389 LeaveCriticalSection(&cs_load_gecko);
391 return ret;
394 void *nsalloc(size_t size)
396 return nsIMemory_Alloc(nsmem, size);
399 void nsfree(void *mem)
401 nsIMemory_Free(nsmem, mem);
404 void nsACString_Init(nsACString *str, const char *data)
406 NS_CStringContainerInit(str);
407 if(data)
408 nsACString_SetData(str, data);
411 void nsACString_SetData(nsACString *str, const char *data)
413 NS_CStringSetData(str, data, PR_UINT32_MAX);
416 PRUint32 nsACString_GetData(const nsACString *str, const char **data, PRBool *termited)
418 return NS_CStringGetData(str, data, termited);
421 void nsACString_Finish(nsACString *str)
423 NS_CStringContainerFinish(str);
426 void nsAString_Init(nsAString *str, const PRUnichar *data)
428 NS_StringContainerInit(str);
429 if(data)
430 NS_StringSetData(str, data, PR_UINT32_MAX);
433 PRUint32 nsAString_GetData(const nsAString *str, const PRUnichar **data, PRBool *termited)
435 return NS_StringGetData(str, data, termited);
438 void nsAString_Finish(nsAString *str)
440 NS_StringContainerFinish(str);
443 nsIInputStream *create_nsstream(const char *data, PRInt32 data_len)
445 nsIStringInputStream *ret;
446 nsresult nsres;
448 if(!pCompMgr)
449 return NULL;
451 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
452 NS_STRINGSTREAM_CONTRACTID, NULL, &IID_nsIStringInputStream,
453 (void**)&ret);
454 if(NS_FAILED(nsres)) {
455 ERR("Could not get nsIStringInputStream\n");
456 return NULL;
459 nsres = nsIStringInputStream_SetData(ret, data, data_len);
460 if(NS_FAILED(nsres)) {
461 ERR("AdoptData failed: %08x\n", nsres);
462 nsIStringInputStream_Release(ret);
463 return NULL;
466 return (nsIInputStream*)ret;
469 nsICommandParams *create_nscommand_params(void)
471 nsICommandParams *ret = NULL;
472 nsresult nsres;
474 if(!pCompMgr)
475 return NULL;
477 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
478 NS_COMMANDPARAMS_CONTRACTID, NULL, &IID_nsICommandParams,
479 (void**)&ret);
480 if(NS_FAILED(nsres))
481 ERR("Could not get nsICommandParams\n");
483 return ret;
486 static void nsnode_to_nsstring_rec(nsIContentSerializer *serializer, nsIDOMNode *nsnode, nsAString *str)
488 nsIDOMNodeList *node_list = NULL;
489 PRBool has_children = FALSE;
490 PRUint16 type;
491 nsresult nsres;
493 nsIDOMNode_HasChildNodes(nsnode, &has_children);
495 nsres = nsIDOMNode_GetNodeType(nsnode, &type);
496 if(NS_FAILED(nsres)) {
497 ERR("GetType failed: %08x\n", nsres);
498 return;
501 switch(type) {
502 case ELEMENT_NODE: {
503 nsIDOMElement *nselem;
504 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMElement, (void**)&nselem);
505 nsIContentSerializer_AppendElementStart(serializer, nselem, has_children, str);
506 nsIDOMElement_Release(nselem);
507 break;
509 case TEXT_NODE: {
510 nsIDOMText *nstext;
511 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMText, (void**)&nstext);
512 nsIContentSerializer_AppendText(serializer, nstext, 0, -1, str);
513 nsIDOMText_Release(nstext);
514 break;
516 case COMMENT_NODE: {
517 nsIDOMComment *nscomment;
518 nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMComment, (void**)&nscomment);
519 nsres = nsIContentSerializer_AppendComment(serializer, nscomment, 0, -1, str);
520 break;
522 case DOCUMENT_NODE: {
523 nsIDOMDocument *nsdoc;
524 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMDocument, (void**)&nsdoc);
525 nsIContentSerializer_AppendDocumentStart(serializer, nsdoc, str);
526 nsIDOMDocument_Release(nsdoc);
527 break;
529 case DOCUMENT_FRAGMENT_NODE:
530 break;
531 default:
532 FIXME("Unhandled type %u\n", type);
535 if(has_children) {
536 PRUint32 child_cnt, i;
537 nsIDOMNode *child_node;
539 nsIDOMNode_GetChildNodes(nsnode, &node_list);
540 nsIDOMNodeList_GetLength(node_list, &child_cnt);
542 for(i=0; i<child_cnt; i++) {
543 nsres = nsIDOMNodeList_Item(node_list, i, &child_node);
544 if(NS_SUCCEEDED(nsres)) {
545 nsnode_to_nsstring_rec(serializer, child_node, str);
546 nsIDOMNode_Release(child_node);
547 }else {
548 ERR("Item failed: %08x\n", nsres);
552 nsIDOMNodeList_Release(node_list);
555 if(type == ELEMENT_NODE) {
556 nsIDOMElement *nselem;
557 nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMElement, (void**)&nselem);
558 nsIContentSerializer_AppendElementEnd(serializer, nselem, str);
559 nsIDOMElement_Release(nselem);
563 void nsnode_to_nsstring(nsIDOMNode *nsdoc, nsAString *str)
565 nsIContentSerializer *serializer;
566 nsIDOMNode *nsnode;
567 nsresult nsres;
569 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
570 NS_HTMLSERIALIZER_CONTRACTID, NULL, &IID_nsIContentSerializer,
571 (void**)&serializer);
572 if(NS_FAILED(nsres)) {
573 ERR("Could not get nsIContentSerializer: %08x\n", nsres);
574 return;
577 nsres = nsIContentSerializer_Init(serializer, 0, 100, NULL, FALSE);
578 if(NS_FAILED(nsres))
579 ERR("Init failed: %08x\n", nsres);
581 nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
582 nsnode_to_nsstring_rec(serializer, nsnode, str);
583 nsIDOMNode_Release(nsnode);
585 nsres = nsIContentSerializer_Flush(serializer, str);
586 if(NS_FAILED(nsres))
587 ERR("Flush failed: %08x\n", nsres);
589 nsIContentSerializer_Release(serializer);
592 static nsIController *get_editor_controller(NSContainer *This)
594 nsIController *ret = NULL;
595 nsIEditingSession *editing_session = NULL;
596 nsIInterfaceRequestor *iface_req;
597 nsIControllerContext *ctrlctx;
598 nsIEditor *editor = NULL;
599 nsresult nsres;
601 nsres = nsIWebBrowser_QueryInterface(This->webbrowser,
602 &IID_nsIInterfaceRequestor, (void**)&iface_req);
603 if(NS_FAILED(nsres)) {
604 ERR("Could not get nsIInterfaceRequestor: %08x\n", nsres);
605 return NULL;
608 nsres = nsIInterfaceRequestor_GetInterface(iface_req, &IID_nsIEditingSession,
609 (void**)&editing_session);
610 nsIInterfaceRequestor_Release(iface_req);
611 if(NS_FAILED(nsres)) {
612 ERR("Could not get nsIEditingSession: %08x\n", nsres);
613 return NULL;
616 nsres = nsIEditingSession_GetEditorForWindow(editing_session,
617 This->doc->window->nswindow, &editor);
618 nsIEditingSession_Release(editing_session);
619 if(NS_FAILED(nsres)) {
620 ERR("Could not get editor: %08x\n", nsres);
621 return NULL;
624 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr,
625 NS_EDITORCONTROLLER_CONTRACTID, NULL, &IID_nsIControllerContext, (void**)&ctrlctx);
626 if(NS_SUCCEEDED(nsres)) {
627 nsres = nsIControllerContext_SetCommandContext(ctrlctx, editor);
628 if(NS_FAILED(nsres))
629 ERR("SetCommandContext failed: %08x\n", nsres);
630 nsres = nsIControllerContext_QueryInterface(ctrlctx, &IID_nsIController,
631 (void**)&ret);
632 nsIControllerContext_Release(ctrlctx);
633 if(NS_FAILED(nsres))
634 ERR("Could not get nsIController interface: %08x\n", nsres);
635 }else {
636 ERR("Could not create edit controller: %08x\n", nsres);
639 nsISupports_Release(editor);
641 return ret;
644 void set_ns_editmode(NSContainer *This)
646 nsIInterfaceRequestor *iface_req;
647 nsIEditingSession *editing_session = NULL;
648 nsIURIContentListener *listener = NULL;
649 nsIDOMWindow *dom_window = NULL;
650 nsresult nsres;
652 nsres = nsIWebBrowser_QueryInterface(This->webbrowser,
653 &IID_nsIInterfaceRequestor, (void**)&iface_req);
654 if(NS_FAILED(nsres)) {
655 ERR("Could not get nsIInterfaceRequestor: %08x\n", nsres);
656 return;
659 nsres = nsIInterfaceRequestor_GetInterface(iface_req, &IID_nsIEditingSession,
660 (void**)&editing_session);
661 nsIInterfaceRequestor_Release(iface_req);
662 if(NS_FAILED(nsres)) {
663 ERR("Could not get nsIEditingSession: %08x\n", nsres);
664 return;
667 nsres = nsIWebBrowser_GetContentDOMWindow(This->webbrowser, &dom_window);
668 if(NS_FAILED(nsres)) {
669 ERR("Could not get content DOM window: %08x\n", nsres);
670 nsIEditingSession_Release(editing_session);
671 return;
674 nsres = nsIEditingSession_MakeWindowEditable(editing_session, dom_window, NULL, FALSE);
675 nsIEditingSession_Release(editing_session);
676 nsIDOMWindow_Release(dom_window);
677 if(NS_FAILED(nsres)) {
678 ERR("MakeWindowEditable failed: %08x\n", nsres);
679 return;
682 /* MakeWindowEditable changes WebBrowser's parent URI content listener.
683 * It seams to be a bug in Gecko. To workaround it we set our content
684 * listener again and Gecko's one as its parent.
686 nsIWebBrowser_GetParentURIContentListener(This->webbrowser, &listener);
687 nsIURIContentListener_SetParentContentListener(NSURICL(This), listener);
688 nsIURIContentListener_Release(listener);
689 nsIWebBrowser_SetParentURIContentListener(This->webbrowser, NSURICL(This));
692 static void handle_load_event(NSContainer *This, nsIDOMEvent *event)
694 task_t *task;
696 TRACE("(%p)\n", This);
698 if(!This->doc)
699 return;
701 if(This->editor_controller) {
702 nsIController_Release(This->editor_controller);
703 This->editor_controller = NULL;
706 if(This->doc->usermode == EDITMODE)
707 This->editor_controller = get_editor_controller(This);
709 task = mshtml_alloc(sizeof(task_t));
711 task->doc = This->doc;
712 task->task_id = TASK_PARSECOMPLETE;
713 task->next = NULL;
716 * This should be done in the worker thread that parses HTML,
717 * but we don't have such thread (Gecko parses HTML for us).
719 push_task(task);
722 void close_gecko(void)
724 TRACE("()\n");
726 if(pCompMgr)
727 nsIComponentManager_Release(pCompMgr);
729 if(pServMgr)
730 nsIServiceManager_Release(pServMgr);
732 if(nsmem)
733 nsIMemory_Release(nsmem);
735 if(hXPCOM)
736 FreeLibrary(hXPCOM);
739 /**********************************************************
740 * nsIWebBrowserChrome interface
743 #define NSWBCHROME_THIS(iface) DEFINE_THIS(NSContainer, WebBrowserChrome, iface)
745 static nsresult NSAPI nsWebBrowserChrome_QueryInterface(nsIWebBrowserChrome *iface,
746 nsIIDRef riid, nsQIResult result)
748 NSContainer *This = NSWBCHROME_THIS(iface);
750 *result = NULL;
751 if(IsEqualGUID(&IID_nsISupports, riid)) {
752 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
753 *result = NSWBCHROME(This);
754 }else if(IsEqualGUID(&IID_nsIWebBrowserChrome, riid)) {
755 TRACE("(%p)->(IID_nsIWebBrowserChrome, %p)\n", This, result);
756 *result = NSWBCHROME(This);
757 }else if(IsEqualGUID(&IID_nsIContextMenuListener, riid)) {
758 TRACE("(%p)->(IID_nsIContextMenuListener, %p)\n", This, result);
759 *result = NSCML(This);
760 }else if(IsEqualGUID(&IID_nsIURIContentListener, riid)) {
761 TRACE("(%p)->(IID_nsIURIContentListener %p)\n", This, result);
762 *result = NSURICL(This);
763 }else if(IsEqualGUID(&IID_nsIEmbeddingSiteWindow, riid)) {
764 TRACE("(%p)->(IID_nsIEmbeddingSiteWindow %p)\n", This, result);
765 *result = NSEMBWNDS(This);
766 }else if(IsEqualGUID(&IID_nsITooltipListener, riid)) {
767 TRACE("(%p)->(IID_nsITooltipListener %p)\n", This, result);
768 *result = NSTOOLTIP(This);
769 }else if(IsEqualGUID(&IID_nsIDOMEventListener, riid)) {
770 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This, result);
771 *result = NSEVENTLIST(This);
772 }else if(IsEqualGUID(&IID_nsIInterfaceRequestor, riid)) {
773 TRACE("(%p)->(IID_nsIInterfaceRequestor %p)\n", This, result);
774 *result = NSIFACEREQ(This);
775 }else if(IsEqualGUID(&IID_nsIWeakReference, riid)) {
776 TRACE("(%p)->(IID_nsIWeakReference %p)\n", This, result);
777 *result = NSWEAKREF(This);
778 }else if(IsEqualGUID(&IID_nsISupportsWeakReference, riid)) {
779 TRACE("(%p)->(IID_nsISupportsWeakReference %p)\n", This, result);
780 *result = NSSUPWEAKREF(This);
783 if(*result) {
784 nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
785 return NS_OK;
788 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
789 return NS_NOINTERFACE;
792 static nsrefcnt NSAPI nsWebBrowserChrome_AddRef(nsIWebBrowserChrome *iface)
794 NSContainer *This = NSWBCHROME_THIS(iface);
795 LONG ref = InterlockedIncrement(&This->ref);
797 TRACE("(%p) ref=%d\n", This, ref);
799 return ref;
802 static nsrefcnt NSAPI nsWebBrowserChrome_Release(nsIWebBrowserChrome *iface)
804 NSContainer *This = NSWBCHROME_THIS(iface);
805 LONG ref = InterlockedDecrement(&This->ref);
807 TRACE("(%p) ref=%d\n", This, ref);
809 if(!ref) {
810 if(This->parent)
811 nsIWebBrowserChrome_Release(NSWBCHROME(This->parent));
812 mshtml_free(This);
815 return ref;
818 static nsresult NSAPI nsWebBrowserChrome_SetStatus(nsIWebBrowserChrome *iface,
819 PRUint32 statusType, const PRUnichar *status)
821 NSContainer *This = NSWBCHROME_THIS(iface);
822 TRACE("(%p)->(%d %s)\n", This, statusType, debugstr_w(status));
823 return NS_ERROR_NOT_IMPLEMENTED;
826 static nsresult NSAPI nsWebBrowserChrome_GetWebBrowser(nsIWebBrowserChrome *iface,
827 nsIWebBrowser **aWebBrowser)
829 NSContainer *This = NSWBCHROME_THIS(iface);
831 TRACE("(%p)->(%p)\n", This, aWebBrowser);
833 if(!aWebBrowser)
834 return NS_ERROR_INVALID_ARG;
836 if(This->webbrowser)
837 nsIWebBrowser_AddRef(This->webbrowser);
838 *aWebBrowser = This->webbrowser;
839 return S_OK;
842 static nsresult NSAPI nsWebBrowserChrome_SetWebBrowser(nsIWebBrowserChrome *iface,
843 nsIWebBrowser *aWebBrowser)
845 NSContainer *This = NSWBCHROME_THIS(iface);
847 TRACE("(%p)->(%p)\n", This, aWebBrowser);
849 if(aWebBrowser != This->webbrowser)
850 ERR("Wrong nsWebBrowser!\n");
852 return NS_OK;
855 static nsresult NSAPI nsWebBrowserChrome_GetChromeFlags(nsIWebBrowserChrome *iface,
856 PRUint32 *aChromeFlags)
858 NSContainer *This = NSWBCHROME_THIS(iface);
859 WARN("(%p)->(%p)\n", This, aChromeFlags);
860 return NS_ERROR_NOT_IMPLEMENTED;
863 static nsresult NSAPI nsWebBrowserChrome_SetChromeFlags(nsIWebBrowserChrome *iface,
864 PRUint32 aChromeFlags)
866 NSContainer *This = NSWBCHROME_THIS(iface);
867 WARN("(%p)->(%08x)\n", This, aChromeFlags);
868 return NS_ERROR_NOT_IMPLEMENTED;
871 static nsresult NSAPI nsWebBrowserChrome_DestroyBrowserWindow(nsIWebBrowserChrome *iface)
873 NSContainer *This = NSWBCHROME_THIS(iface);
874 TRACE("(%p)\n", This);
875 return NS_ERROR_NOT_IMPLEMENTED;
878 static nsresult NSAPI nsWebBrowserChrome_SizeBrowserTo(nsIWebBrowserChrome *iface,
879 PRInt32 aCX, PRInt32 aCY)
881 NSContainer *This = NSWBCHROME_THIS(iface);
882 WARN("(%p)->(%d %d)\n", This, aCX, aCY);
883 return NS_ERROR_NOT_IMPLEMENTED;
886 static nsresult NSAPI nsWebBrowserChrome_ShowAsModal(nsIWebBrowserChrome *iface)
888 NSContainer *This = NSWBCHROME_THIS(iface);
889 WARN("(%p)\n", This);
890 return NS_ERROR_NOT_IMPLEMENTED;
893 static nsresult NSAPI nsWebBrowserChrome_IsWindowModal(nsIWebBrowserChrome *iface, PRBool *_retval)
895 NSContainer *This = NSWBCHROME_THIS(iface);
896 WARN("(%p)->(%p)\n", This, _retval);
897 return NS_ERROR_NOT_IMPLEMENTED;
900 static nsresult NSAPI nsWebBrowserChrome_ExitModalEventLoop(nsIWebBrowserChrome *iface,
901 nsresult aStatus)
903 NSContainer *This = NSWBCHROME_THIS(iface);
904 WARN("(%p)->(%08x)\n", This, aStatus);
905 return NS_ERROR_NOT_IMPLEMENTED;
908 #undef NSWBCHROME_THIS
910 static const nsIWebBrowserChromeVtbl nsWebBrowserChromeVtbl = {
911 nsWebBrowserChrome_QueryInterface,
912 nsWebBrowserChrome_AddRef,
913 nsWebBrowserChrome_Release,
914 nsWebBrowserChrome_SetStatus,
915 nsWebBrowserChrome_GetWebBrowser,
916 nsWebBrowserChrome_SetWebBrowser,
917 nsWebBrowserChrome_GetChromeFlags,
918 nsWebBrowserChrome_SetChromeFlags,
919 nsWebBrowserChrome_DestroyBrowserWindow,
920 nsWebBrowserChrome_SizeBrowserTo,
921 nsWebBrowserChrome_ShowAsModal,
922 nsWebBrowserChrome_IsWindowModal,
923 nsWebBrowserChrome_ExitModalEventLoop
926 /**********************************************************
927 * nsIContextMenuListener interface
930 #define NSCML_THIS(iface) DEFINE_THIS(NSContainer, ContextMenuListener, iface)
932 static nsresult NSAPI nsContextMenuListener_QueryInterface(nsIContextMenuListener *iface,
933 nsIIDRef riid, nsQIResult result)
935 NSContainer *This = NSCML_THIS(iface);
936 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
939 static nsrefcnt NSAPI nsContextMenuListener_AddRef(nsIContextMenuListener *iface)
941 NSContainer *This = NSCML_THIS(iface);
942 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
945 static nsrefcnt NSAPI nsContextMenuListener_Release(nsIContextMenuListener *iface)
947 NSContainer *This = NSCML_THIS(iface);
948 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
951 static nsresult NSAPI nsContextMenuListener_OnShowContextMenu(nsIContextMenuListener *iface,
952 PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
954 NSContainer *This = NSCML_THIS(iface);
955 nsIDOMMouseEvent *event;
956 POINT pt;
957 DWORD dwID = CONTEXT_MENU_DEFAULT;
958 nsresult nsres;
960 TRACE("(%p)->(%08x %p %p)\n", This, aContextFlags, aEvent, aNode);
962 nsres = nsIDOMEvent_QueryInterface(aEvent, &IID_nsIDOMMouseEvent, (void**)&event);
963 if(NS_FAILED(nsres)) {
964 ERR("Could not get nsIDOMMouseEvent interface: %08x\n", nsres);
965 return nsres;
968 nsIDOMMouseEvent_GetScreenX(event, &pt.x);
969 nsIDOMMouseEvent_GetScreenY(event, &pt.y);
970 nsIDOMMouseEvent_Release(event);
972 switch(aContextFlags) {
973 case CONTEXT_NONE:
974 case CONTEXT_DOCUMENT:
975 case CONTEXT_TEXT:
976 dwID = CONTEXT_MENU_DEFAULT;
977 break;
978 case CONTEXT_IMAGE:
979 case CONTEXT_IMAGE|CONTEXT_LINK:
980 dwID = CONTEXT_MENU_IMAGE;
981 break;
982 case CONTEXT_LINK:
983 dwID = CONTEXT_MENU_ANCHOR;
984 break;
985 case CONTEXT_INPUT:
986 dwID = CONTEXT_MENU_CONTROL;
987 break;
988 default:
989 FIXME("aContextFlags=%08x\n", aContextFlags);
992 show_context_menu(This->doc, dwID, &pt);
994 return NS_OK;
997 #undef NSCML_THIS
999 static const nsIContextMenuListenerVtbl nsContextMenuListenerVtbl = {
1000 nsContextMenuListener_QueryInterface,
1001 nsContextMenuListener_AddRef,
1002 nsContextMenuListener_Release,
1003 nsContextMenuListener_OnShowContextMenu
1006 /**********************************************************
1007 * nsIURIContentListener interface
1010 #define NSURICL_THIS(iface) DEFINE_THIS(NSContainer, URIContentListener, iface)
1012 static nsresult NSAPI nsURIContentListener_QueryInterface(nsIURIContentListener *iface,
1013 nsIIDRef riid, nsQIResult result)
1015 NSContainer *This = NSURICL_THIS(iface);
1016 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1019 static nsrefcnt NSAPI nsURIContentListener_AddRef(nsIURIContentListener *iface)
1021 NSContainer *This = NSURICL_THIS(iface);
1022 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1025 static nsrefcnt NSAPI nsURIContentListener_Release(nsIURIContentListener *iface)
1027 NSContainer *This = NSURICL_THIS(iface);
1028 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1031 static nsresult NSAPI nsURIContentListener_OnStartURIOpen(nsIURIContentListener *iface,
1032 nsIURI *aURI, PRBool *_retval)
1034 NSContainer *This = NSURICL_THIS(iface);
1035 nsIWineURI *wine_uri;
1036 nsACString spec_str;
1037 const char *spec;
1038 nsresult nsres;
1040 nsACString_Init(&spec_str, NULL);
1041 nsIURI_GetSpec(aURI, &spec_str);
1042 nsACString_GetData(&spec_str, &spec, NULL);
1044 TRACE("(%p)->(%p(%s) %p)\n", This, aURI, debugstr_a(spec), _retval);
1046 nsACString_Finish(&spec_str);
1048 nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
1049 if(NS_FAILED(nsres)) {
1050 WARN("Could not get nsIWineURI interface: %08x\n", nsres);
1051 return NS_ERROR_NOT_IMPLEMENTED;
1054 nsIWineURI_SetNSContainer(wine_uri, This);
1055 nsIWineURI_SetIsDocumentURI(wine_uri, TRUE);
1057 if(This->bscallback && This->bscallback->mon) {
1058 LPWSTR wine_url;
1059 HRESULT hres;
1061 hres = IMoniker_GetDisplayName(This->bscallback->mon, NULL, 0, &wine_url);
1062 if(SUCCEEDED(hres)) {
1063 nsIWineURI_SetWineURL(wine_uri, wine_url);
1064 CoTaskMemFree(wine_url);
1065 }else {
1066 WARN("GetDisplayName failed: %08x\n", hres);
1070 nsIWineURI_Release(wine_uri);
1072 *_retval = FALSE;
1073 return This->content_listener
1074 ? nsIURIContentListener_OnStartURIOpen(This->content_listener, aURI, _retval)
1075 : NS_OK;
1078 static nsresult NSAPI nsURIContentListener_DoContent(nsIURIContentListener *iface,
1079 const char *aContentType, PRBool aIsContentPreferred, nsIRequest *aRequest,
1080 nsIStreamListener **aContentHandler, PRBool *_retval)
1082 NSContainer *This = NSURICL_THIS(iface);
1084 TRACE("(%p)->(%s %x %p %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1085 aRequest, aContentHandler, _retval);
1087 return This->content_listener
1088 ? nsIURIContentListener_DoContent(This->content_listener, aContentType,
1089 aIsContentPreferred, aRequest, aContentHandler, _retval)
1090 : NS_ERROR_NOT_IMPLEMENTED;
1093 static nsresult NSAPI nsURIContentListener_IsPreferred(nsIURIContentListener *iface,
1094 const char *aContentType, char **aDesiredContentType, PRBool *_retval)
1096 NSContainer *This = NSURICL_THIS(iface);
1098 TRACE("(%p)->(%s %p %p)\n", This, debugstr_a(aContentType), aDesiredContentType, _retval);
1100 /* FIXME: Should we do something here? */
1101 *_retval = TRUE;
1103 return This->content_listener
1104 ? nsIURIContentListener_IsPreferred(This->content_listener, aContentType,
1105 aDesiredContentType, _retval)
1106 : NS_OK;
1109 static nsresult NSAPI nsURIContentListener_CanHandleContent(nsIURIContentListener *iface,
1110 const char *aContentType, PRBool aIsContentPreferred, char **aDesiredContentType,
1111 PRBool *_retval)
1113 NSContainer *This = NSURICL_THIS(iface);
1115 TRACE("(%p)->(%s %x %p %p)\n", This, debugstr_a(aContentType), aIsContentPreferred,
1116 aDesiredContentType, _retval);
1118 return This->content_listener
1119 ? nsIURIContentListener_CanHandleContent(This->content_listener, aContentType,
1120 aIsContentPreferred, aDesiredContentType, _retval)
1121 : NS_ERROR_NOT_IMPLEMENTED;
1124 static nsresult NSAPI nsURIContentListener_GetLoadCookie(nsIURIContentListener *iface,
1125 nsISupports **aLoadCookie)
1127 NSContainer *This = NSURICL_THIS(iface);
1129 WARN("(%p)->(%p)\n", This, aLoadCookie);
1131 return This->content_listener
1132 ? nsIURIContentListener_GetLoadCookie(This->content_listener, aLoadCookie)
1133 : NS_ERROR_NOT_IMPLEMENTED;
1136 static nsresult NSAPI nsURIContentListener_SetLoadCookie(nsIURIContentListener *iface,
1137 nsISupports *aLoadCookie)
1139 NSContainer *This = NSURICL_THIS(iface);
1141 WARN("(%p)->(%p)\n", This, aLoadCookie);
1143 return This->content_listener
1144 ? nsIURIContentListener_SetLoadCookie(This->content_listener, aLoadCookie)
1145 : NS_ERROR_NOT_IMPLEMENTED;
1148 static nsresult NSAPI nsURIContentListener_GetParentContentListener(nsIURIContentListener *iface,
1149 nsIURIContentListener **aParentContentListener)
1151 NSContainer *This = NSURICL_THIS(iface);
1153 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1155 if(This->content_listener)
1156 nsIURIContentListener_AddRef(This->content_listener);
1158 *aParentContentListener = This->content_listener;
1159 return NS_OK;
1162 static nsresult NSAPI nsURIContentListener_SetParentContentListener(nsIURIContentListener *iface,
1163 nsIURIContentListener *aParentContentListener)
1165 NSContainer *This = NSURICL_THIS(iface);
1167 TRACE("(%p)->(%p)\n", This, aParentContentListener);
1169 if(aParentContentListener == NSURICL(This))
1170 return NS_OK;
1172 if(This->content_listener)
1173 nsIURIContentListener_Release(This->content_listener);
1175 This->content_listener = aParentContentListener;
1176 if(This->content_listener)
1177 nsIURIContentListener_AddRef(This->content_listener);
1179 return NS_OK;
1182 #undef NSURICL_THIS
1184 static const nsIURIContentListenerVtbl nsURIContentListenerVtbl = {
1185 nsURIContentListener_QueryInterface,
1186 nsURIContentListener_AddRef,
1187 nsURIContentListener_Release,
1188 nsURIContentListener_OnStartURIOpen,
1189 nsURIContentListener_DoContent,
1190 nsURIContentListener_IsPreferred,
1191 nsURIContentListener_CanHandleContent,
1192 nsURIContentListener_GetLoadCookie,
1193 nsURIContentListener_SetLoadCookie,
1194 nsURIContentListener_GetParentContentListener,
1195 nsURIContentListener_SetParentContentListener
1198 /**********************************************************
1199 * nsIEmbeddinSiteWindow interface
1202 #define NSEMBWNDS_THIS(iface) DEFINE_THIS(NSContainer, EmbeddingSiteWindow, iface)
1204 static nsresult NSAPI nsEmbeddingSiteWindow_QueryInterface(nsIEmbeddingSiteWindow *iface,
1205 nsIIDRef riid, nsQIResult result)
1207 NSContainer *This = NSEMBWNDS_THIS(iface);
1208 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1211 static nsrefcnt NSAPI nsEmbeddingSiteWindow_AddRef(nsIEmbeddingSiteWindow *iface)
1213 NSContainer *This = NSEMBWNDS_THIS(iface);
1214 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1217 static nsrefcnt NSAPI nsEmbeddingSiteWindow_Release(nsIEmbeddingSiteWindow *iface)
1219 NSContainer *This = NSEMBWNDS_THIS(iface);
1220 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1223 static nsresult NSAPI nsEmbeddingSiteWindow_SetDimensions(nsIEmbeddingSiteWindow *iface,
1224 PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
1226 NSContainer *This = NSEMBWNDS_THIS(iface);
1227 WARN("(%p)->(%08x %d %d %d %d)\n", This, flags, x, y, cx, cy);
1228 return NS_ERROR_NOT_IMPLEMENTED;
1231 static nsresult NSAPI nsEmbeddingSiteWindow_GetDimensions(nsIEmbeddingSiteWindow *iface,
1232 PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
1234 NSContainer *This = NSEMBWNDS_THIS(iface);
1235 WARN("(%p)->(%08x %p %p %p %p)\n", This, flags, x, y, cx, cy);
1236 return NS_ERROR_NOT_IMPLEMENTED;
1239 static nsresult NSAPI nsEmbeddingSiteWindow_SetFocus(nsIEmbeddingSiteWindow *iface)
1241 NSContainer *This = NSEMBWNDS_THIS(iface);
1242 WARN("(%p)\n", This);
1243 return NS_ERROR_NOT_IMPLEMENTED;
1246 static nsresult NSAPI nsEmbeddingSiteWindow_GetVisibility(nsIEmbeddingSiteWindow *iface,
1247 PRBool *aVisibility)
1249 NSContainer *This = NSEMBWNDS_THIS(iface);
1250 WARN("(%p)->(%p)\n", This, aVisibility);
1251 return NS_ERROR_NOT_IMPLEMENTED;
1254 static nsresult NSAPI nsEmbeddingSiteWindow_SetVisibility(nsIEmbeddingSiteWindow *iface,
1255 PRBool aVisibility)
1257 NSContainer *This = NSEMBWNDS_THIS(iface);
1258 WARN("(%p)->(%x)\n", This, aVisibility);
1259 return NS_ERROR_NOT_IMPLEMENTED;
1262 static nsresult NSAPI nsEmbeddingSiteWindow_GetTitle(nsIEmbeddingSiteWindow *iface,
1263 PRUnichar **aTitle)
1265 NSContainer *This = NSEMBWNDS_THIS(iface);
1266 WARN("(%p)->(%p)\n", This, aTitle);
1267 return NS_ERROR_NOT_IMPLEMENTED;
1270 static nsresult NSAPI nsEmbeddingSiteWindow_SetTitle(nsIEmbeddingSiteWindow *iface,
1271 const PRUnichar *aTitle)
1273 NSContainer *This = NSEMBWNDS_THIS(iface);
1274 WARN("(%p)->(%s)\n", This, debugstr_w(aTitle));
1275 return NS_ERROR_NOT_IMPLEMENTED;
1278 static nsresult NSAPI nsEmbeddingSiteWindow_GetSiteWindow(nsIEmbeddingSiteWindow *iface,
1279 void **aSiteWindow)
1281 NSContainer *This = NSEMBWNDS_THIS(iface);
1283 TRACE("(%p)->(%p)\n", This, aSiteWindow);
1285 *aSiteWindow = This->hwnd;
1286 return NS_OK;
1289 static const nsIEmbeddingSiteWindowVtbl nsEmbeddingSiteWindowVtbl = {
1290 nsEmbeddingSiteWindow_QueryInterface,
1291 nsEmbeddingSiteWindow_AddRef,
1292 nsEmbeddingSiteWindow_Release,
1293 nsEmbeddingSiteWindow_SetDimensions,
1294 nsEmbeddingSiteWindow_GetDimensions,
1295 nsEmbeddingSiteWindow_SetFocus,
1296 nsEmbeddingSiteWindow_GetVisibility,
1297 nsEmbeddingSiteWindow_SetVisibility,
1298 nsEmbeddingSiteWindow_GetTitle,
1299 nsEmbeddingSiteWindow_SetTitle,
1300 nsEmbeddingSiteWindow_GetSiteWindow
1303 #define NSTOOLTIP_THIS(iface) DEFINE_THIS(NSContainer, TooltipListener, iface)
1305 static nsresult NSAPI nsTooltipListener_QueryInterface(nsITooltipListener *iface, nsIIDRef riid,
1306 nsQIResult result)
1308 NSContainer *This = NSTOOLTIP_THIS(iface);
1309 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1312 static nsrefcnt NSAPI nsTooltipListener_AddRef(nsITooltipListener *iface)
1314 NSContainer *This = NSTOOLTIP_THIS(iface);
1315 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1318 static nsrefcnt NSAPI nsTooltipListener_Release(nsITooltipListener *iface)
1320 NSContainer *This = NSTOOLTIP_THIS(iface);
1321 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1324 static nsresult NSAPI nsTooltipListener_OnShowTooltip(nsITooltipListener *iface,
1325 PRInt32 aXCoord, PRInt32 aYCoord, const PRUnichar *aTipText)
1327 NSContainer *This = NSTOOLTIP_THIS(iface);
1329 show_tooltip(This->doc, aXCoord, aYCoord, aTipText);
1331 return NS_OK;
1334 static nsresult NSAPI nsTooltipListener_OnHideTooltip(nsITooltipListener *iface)
1336 NSContainer *This = NSTOOLTIP_THIS(iface);
1338 hide_tooltip(This->doc);
1340 return NS_OK;
1343 #undef NSTOOLTIM_THIS
1345 static const nsITooltipListenerVtbl nsTooltipListenerVtbl = {
1346 nsTooltipListener_QueryInterface,
1347 nsTooltipListener_AddRef,
1348 nsTooltipListener_Release,
1349 nsTooltipListener_OnShowTooltip,
1350 nsTooltipListener_OnHideTooltip
1353 #define NSEVENTLIST_THIS(iface) DEFINE_THIS(NSContainer, DOMEventListener, iface)
1355 static nsresult NSAPI nsDOMEventListener_QueryInterface(nsIDOMEventListener *iface,
1356 nsIIDRef riid, nsQIResult result)
1358 NSContainer *This = NSEVENTLIST_THIS(iface);
1359 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1362 static nsrefcnt NSAPI nsDOMEventListener_AddRef(nsIDOMEventListener *iface)
1364 NSContainer *This = NSEVENTLIST_THIS(iface);
1365 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1368 static nsrefcnt NSAPI nsDOMEventListener_Release(nsIDOMEventListener *iface)
1370 NSContainer *This = NSEVENTLIST_THIS(iface);
1371 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1374 static nsresult NSAPI nsDOMEventListener_HandleEvent(nsIDOMEventListener *iface, nsIDOMEvent *event)
1376 NSContainer *This = NSEVENTLIST_THIS(iface);
1377 nsAString type_str;
1378 const PRUnichar *type;
1380 static const PRUnichar loadW[] = {'l','o','a','d',0};
1382 nsAString_Init(&type_str, NULL);
1383 nsIDOMEvent_GetType(event, &type_str);
1384 nsAString_GetData(&type_str, &type, NULL);
1386 TRACE("(%p)->(%p) %s\n", This, event, debugstr_w(type));
1388 if(!strcmpW(loadW, type)) {
1389 handle_load_event(This, event);
1390 }else if(This->doc) {
1391 update_doc(This->doc, UPDATE_UI);
1392 if(This->doc->usermode == EDITMODE)
1393 handle_edit_event(This->doc, event);
1396 nsAString_Finish(&type_str);
1398 return NS_OK;
1401 #undef NSEVENTLIST_THIS
1403 static const nsIDOMEventListenerVtbl nsDOMEventListenerVtbl = {
1404 nsDOMEventListener_QueryInterface,
1405 nsDOMEventListener_AddRef,
1406 nsDOMEventListener_Release,
1407 nsDOMEventListener_HandleEvent
1410 #define NSIFACEREQ_THIS(iface) DEFINE_THIS(NSContainer, InterfaceRequestor, iface)
1412 static nsresult NSAPI nsInterfaceRequestor_QueryInterface(nsIInterfaceRequestor *iface,
1413 nsIIDRef riid, nsQIResult result)
1415 NSContainer *This = NSIFACEREQ_THIS(iface);
1416 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1419 static nsrefcnt NSAPI nsInterfaceRequestor_AddRef(nsIInterfaceRequestor *iface)
1421 NSContainer *This = NSIFACEREQ_THIS(iface);
1422 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1425 static nsrefcnt NSAPI nsInterfaceRequestor_Release(nsIInterfaceRequestor *iface)
1427 NSContainer *This = NSIFACEREQ_THIS(iface);
1428 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1431 static nsresult NSAPI nsInterfaceRequestor_GetInterface(nsIInterfaceRequestor *iface,
1432 nsIIDRef riid, nsQIResult result)
1434 NSContainer *This = NSIFACEREQ_THIS(iface);
1436 if(IsEqualGUID(&IID_nsIDOMWindow, riid)) {
1437 TRACE("(%p)->(IID_nsIDOMWindow %p)\n", This, result);
1438 return nsIWebBrowser_GetContentDOMWindow(This->webbrowser, (nsIDOMWindow**)result);
1441 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1444 #undef NSIFACEREQ_THIS
1446 static const nsIInterfaceRequestorVtbl nsInterfaceRequestorVtbl = {
1447 nsInterfaceRequestor_QueryInterface,
1448 nsInterfaceRequestor_AddRef,
1449 nsInterfaceRequestor_Release,
1450 nsInterfaceRequestor_GetInterface
1453 #define NSWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, WeakReference, iface)
1455 static nsresult NSAPI nsWeakReference_QueryInterface(nsIWeakReference *iface,
1456 nsIIDRef riid, nsQIResult result)
1458 NSContainer *This = NSWEAKREF_THIS(iface);
1459 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1462 static nsrefcnt NSAPI nsWeakReference_AddRef(nsIWeakReference *iface)
1464 NSContainer *This = NSWEAKREF_THIS(iface);
1465 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1468 static nsrefcnt NSAPI nsWeakReference_Release(nsIWeakReference *iface)
1470 NSContainer *This = NSWEAKREF_THIS(iface);
1471 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1474 static nsresult NSAPI nsWeakReference_QueryReferent(nsIWeakReference *iface,
1475 const nsIID *riid, void **result)
1477 NSContainer *This = NSWEAKREF_THIS(iface);
1478 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1481 #undef NSWEAKREF_THIS
1483 static const nsIWeakReferenceVtbl nsWeakReferenceVtbl = {
1484 nsWeakReference_QueryInterface,
1485 nsWeakReference_AddRef,
1486 nsWeakReference_Release,
1487 nsWeakReference_QueryReferent
1490 #define NSSUPWEAKREF_THIS(iface) DEFINE_THIS(NSContainer, SupportsWeakReference, iface)
1492 static nsresult NSAPI nsSupportsWeakReference_QueryInterface(nsISupportsWeakReference *iface,
1493 nsIIDRef riid, nsQIResult result)
1495 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1496 return nsIWebBrowserChrome_QueryInterface(NSWBCHROME(This), riid, result);
1499 static nsrefcnt NSAPI nsSupportsWeakReference_AddRef(nsISupportsWeakReference *iface)
1501 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1502 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
1505 static nsrefcnt NSAPI nsSupportsWeakReference_Release(nsISupportsWeakReference *iface)
1507 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1508 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
1511 static nsresult NSAPI nsSupportsWeakReference_GetWeakReference(nsISupportsWeakReference *iface,
1512 nsIWeakReference **_retval)
1514 NSContainer *This = NSSUPWEAKREF_THIS(iface);
1516 TRACE("(%p)->(%p)\n", This, _retval);
1518 nsIWeakReference_AddRef(NSWEAKREF(This));
1519 *_retval = NSWEAKREF(This);
1520 return NS_OK;
1523 #undef NSWEAKREF_THIS
1525 static const nsISupportsWeakReferenceVtbl nsSupportsWeakReferenceVtbl = {
1526 nsSupportsWeakReference_QueryInterface,
1527 nsSupportsWeakReference_AddRef,
1528 nsSupportsWeakReference_Release,
1529 nsSupportsWeakReference_GetWeakReference
1533 NSContainer *NSContainer_Create(HTMLDocument *doc, NSContainer *parent)
1535 nsIDOMWindow *dom_window;
1536 nsIWebBrowserSetup *wbsetup;
1537 nsIScrollable *scrollable;
1538 NSContainer *ret;
1539 nsresult nsres;
1541 if(!load_gecko())
1542 return NULL;
1544 ret = mshtml_alloc(sizeof(NSContainer));
1546 ret->lpWebBrowserChromeVtbl = &nsWebBrowserChromeVtbl;
1547 ret->lpContextMenuListenerVtbl = &nsContextMenuListenerVtbl;
1548 ret->lpURIContentListenerVtbl = &nsURIContentListenerVtbl;
1549 ret->lpEmbeddingSiteWindowVtbl = &nsEmbeddingSiteWindowVtbl;
1550 ret->lpTooltipListenerVtbl = &nsTooltipListenerVtbl;
1551 ret->lpInterfaceRequestorVtbl = &nsInterfaceRequestorVtbl;
1552 ret->lpWeakReferenceVtbl = &nsWeakReferenceVtbl;
1553 ret->lpSupportsWeakReferenceVtbl = &nsSupportsWeakReferenceVtbl;
1554 ret->lpDOMEventListenerVtbl = &nsDOMEventListenerVtbl;
1556 ret->doc = doc;
1557 ret->ref = 1;
1558 ret->bscallback = NULL;
1559 ret->content_listener = NULL;
1560 ret->editor_controller = NULL;
1562 if(parent)
1563 nsIWebBrowserChrome_AddRef(NSWBCHROME(parent));
1564 ret->parent = parent;
1566 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
1567 NULL, &IID_nsIWebBrowser, (void**)&ret->webbrowser);
1568 if(NS_FAILED(nsres))
1569 ERR("Creating WebBrowser failed: %08x\n", nsres);
1571 nsres = nsIWebBrowser_SetContainerWindow(ret->webbrowser, NSWBCHROME(ret));
1572 if(NS_FAILED(nsres))
1573 ERR("SetContainerWindow failed: %08x\n", nsres);
1575 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIBaseWindow,
1576 (void**)&ret->window);
1577 if(NS_FAILED(nsres))
1578 ERR("Could not get nsIBaseWindow interface: %08x\n", nsres);
1580 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserSetup,
1581 (void**)&wbsetup);
1582 if(NS_SUCCEEDED(nsres)) {
1583 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, TRUE);
1584 nsIWebBrowserSetup_Release(wbsetup);
1585 if(NS_FAILED(nsres))
1586 ERR("SetProperty(SETUP_IS_CHROME_WRAPPER) failed: %08x\n", nsres);
1587 }else {
1588 ERR("Could not get nsIWebBrowserSetup interface\n");
1591 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIWebNavigation,
1592 (void**)&ret->navigation);
1593 if(NS_FAILED(nsres))
1594 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1596 nsres = nsIWebBrowserFocus_QueryInterface(ret->webbrowser, &IID_nsIWebBrowserFocus,
1597 (void**)&ret->focus);
1598 if(NS_FAILED(nsres))
1599 ERR("Could not get nsIWebBrowserFocus interface: %08x\n", nsres);
1601 if(!nscontainer_class)
1602 register_nscontainer_class();
1604 ret->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
1605 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
1606 GetDesktopWindow(), NULL, hInst, ret);
1608 nsres = nsIBaseWindow_InitWindow(ret->window, ret->hwnd, NULL, 0, 0, 100, 100);
1609 if(NS_SUCCEEDED(nsres)) {
1610 nsres = nsIBaseWindow_Create(ret->window);
1611 if(NS_FAILED(nsres))
1612 WARN("Creating window failed: %08x\n", nsres);
1614 nsIBaseWindow_SetVisibility(ret->window, FALSE);
1615 nsIBaseWindow_SetEnabled(ret->window, FALSE);
1616 }else {
1617 ERR("InitWindow failed: %08x\n", nsres);
1620 nsres = nsIWebBrowser_SetParentURIContentListener(ret->webbrowser, NSURICL(ret));
1621 if(NS_FAILED(nsres))
1622 ERR("SetParentURIContentListener failed: %08x\n", nsres);
1624 nsres = nsIWebBrowser_GetContentDOMWindow(ret->webbrowser, &dom_window);
1625 if(NS_SUCCEEDED(nsres)) {
1626 nsIDOMEventTarget *target;
1627 nsres = nsIDOMWindow_QueryInterface(dom_window, &IID_nsIDOMEventTarget, (void**)&target);
1628 nsIDOMWindow_Release(dom_window);
1629 if(NS_SUCCEEDED(nsres)) {
1630 nsAString keypress_str, load_str;
1631 static const PRUnichar wsz_keypress[] = {'k','e','y','p','r','e','s','s',0};
1632 static const PRUnichar wsz_load[] = {'l','o','a','d',0};
1634 nsAString_Init(&keypress_str, wsz_keypress);
1635 nsres = nsIDOMEventTarget_AddEventListener(target, &keypress_str, NSEVENTLIST(ret), FALSE);
1636 nsAString_Finish(&keypress_str);
1637 if(NS_FAILED(nsres))
1638 ERR("AddEventTarget failed: %08x\n", nsres);
1640 nsAString_Init(&load_str, wsz_load);
1641 nsres = nsIDOMEventTarget_AddEventListener(target, &load_str, NSEVENTLIST(ret), TRUE);
1642 nsAString_Finish(&load_str);
1643 if(NS_FAILED(nsres))
1644 ERR("AddEventTarget failed: %08x\n", nsres);
1646 nsIDOMEventTarget_Release(target);
1647 }else {
1648 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres);
1650 }else {
1651 ERR("GetContentDOMWindow failed: %08x\n", nsres);
1654 nsres = nsIWebBrowser_QueryInterface(ret->webbrowser, &IID_nsIScrollable, (void**)&scrollable);
1655 if(NS_SUCCEEDED(nsres)) {
1656 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1657 ScrollOrientation_Y, Scrollbar_Always);
1658 if(NS_FAILED(nsres))
1659 ERR("Could not set default Y scrollbar prefs: %08x\n", nsres);
1661 nsres = nsIScrollable_SetDefaultScrollbarPreferences(scrollable,
1662 ScrollOrientation_X, Scrollbar_Auto);
1663 if(NS_FAILED(nsres))
1664 ERR("Could not set default X scrollbar prefs: %08x\n", nsres);
1666 nsIScrollable_Release(scrollable);
1667 }else {
1668 ERR("Could not get nsIScrollable: %08x\n", nsres);
1671 return ret;
1674 void NSContainer_Release(NSContainer *This)
1676 TRACE("(%p)\n", This);
1678 ShowWindow(This->hwnd, SW_HIDE);
1679 SetParent(This->hwnd, NULL);
1681 nsIBaseWindow_SetVisibility(This->window, FALSE);
1682 nsIBaseWindow_Destroy(This->window);
1684 nsIWebBrowser_SetContainerWindow(This->webbrowser, NULL);
1686 nsIWebBrowser_Release(This->webbrowser);
1687 This->webbrowser = NULL;
1689 nsIWebNavigation_Release(This->navigation);
1690 This->navigation = NULL;
1692 nsIBaseWindow_Release(This->window);
1693 This->window = NULL;
1695 nsIWebBrowserFocus_Release(This->focus);
1696 This->focus = NULL;
1698 if(This->editor_controller) {
1699 nsIController_Release(This->editor_controller);
1700 This->editor_controller = NULL;
1703 if(This->content_listener) {
1704 nsIURIContentListener_Release(This->content_listener);
1705 This->content_listener = NULL;
1708 if(This->hwnd) {
1709 DestroyWindow(This->hwnd);
1710 This->hwnd = NULL;
1713 nsIWebBrowserChrome_Release(NSWBCHROME(This));