[winforms] Style
[mono.git] / mcs / class / Mono.WebBrowser / Mono.Mozilla / Base.cs
index 8f207c562eb578c06d92fa5d1c3be61de2f183fe..f6221a7d19eef162ecfc8822aabf19c843a72a75 100644 (file)
-// Permission is hereby granted, free of charge, to any person obtaining\r
-// a copy of this software and associated documentation files (the\r
-// "Software"), to deal in the Software without restriction, including\r
-// without limitation the rights to use, copy, modify, merge, publish,\r
-// distribute, sublicense, and/or sell copies of the Software, and to\r
-// permit persons to whom the Software is furnished to do so, subject to\r
-// the following conditions:\r
-// \r
-// The above copyright notice and this permission notice shall be\r
-// included in all copies or substantial portions of the Software.\r
-// \r
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
-//\r
-// Copyright (c) 2007, 2008 Novell, Inc.\r
-//\r
-// Authors:\r
-//     Andreia Gaita (avidigal@novell.com)\r
-//\r
-\r
-using System;\r
-using System.Text;\r
-using System.Collections;\r
-using System.Runtime.InteropServices;\r
-using System.Diagnostics;\r
-using Mono.WebBrowser;\r
-\r
-namespace Mono.Mozilla\r
-{\r
-       internal class Base\r
-       {\r
-               private static Hashtable boundControls;\r
-               internal static bool gluezillaInstalled;\r
-               internal static bool initialized;\r
-\r
-               private class BindingInfo\r
-               {\r
-                       public CallbackBinder callback;\r
-                       public IntPtr gluezilla;\r
-               }\r
-\r
-               private static bool isInitialized ()\r
-               {\r
-                       if (!gluezillaInstalled)\r
-                               return false;\r
-                       return true;\r
-               }\r
-\r
-               private static BindingInfo getBinding (IWebBrowser control)\r
-               {\r
-                       if (!boundControls.ContainsKey (control))\r
-                               return null;\r
-                       BindingInfo info = boundControls[control] as BindingInfo;\r
-                       return info;\r
-               }\r
-\r
-               static Base ()\r
-               {\r
-                       boundControls = new Hashtable ();\r
-               }\r
-\r
-               public Base () { }\r
-\r
-               public static void Debug (int signal)\r
-               {\r
-                       gluezilla_debug (signal);\r
-               }\r
-               \r
-               public static bool Init (WebBrowser control, Platform platform)\r
-               {\r
-                       if (!initialized) {\r
-       \r
-                               string monoMozDir = System.IO.Path.Combine (\r
-                                       System.IO.Path.Combine (\r
-                                       Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData),\r
-                                       ".mono"), "mozilla");\r
-       \r
-                               if (!System.IO.Directory.Exists (monoMozDir))\r
-                                       System.IO.Directory.CreateDirectory (monoMozDir);\r
-       \r
-                               Platform mozPlatform;\r
-                               try {\r
-                                       gluezilla_init (platform, out mozPlatform);\r
-                               }\r
-                               catch (DllNotFoundException) {\r
-                                       Console.WriteLine ("libgluezilla not found. To have webbrowser support, you need libgluezilla installed");\r
-                                       gluezillaInstalled = false;\r
-                                       initialized = false;\r
-                                       return false;\r
-                               }\r
-                               control.enginePlatform = mozPlatform;\r
-                               gluezillaInstalled = true;\r
-                               initialized = true;\r
-                       }\r
-                       return initialized;\r
-               }\r
-\r
-               public static void Bind (WebBrowser control, IntPtr handle, int width, int height)\r
-               {\r
-                       if (!isInitialized ())\r
-                               return;\r
-\r
-                       BindingInfo info = new BindingInfo ();\r
-                       info.callback = new CallbackBinder (control.callbacks);\r
-                       IntPtr ptrCallback = Marshal.AllocHGlobal (Marshal.SizeOf (info.callback));\r
-                       Marshal.StructureToPtr (info.callback, ptrCallback, true);\r
-                       \r
-                       string monoMozDir = System.IO.Path.Combine (\r
-                               System.IO.Path.Combine (\r
-                               Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData),\r
-                               ".mono"), "mozilla");\r
-                       info.gluezilla = gluezilla_createBrowserWindow (ptrCallback, handle, width, height, Environment.CurrentDirectory, monoMozDir, control.platform);\r
-                       \r
-                       boundControls.Add (control as IWebBrowser, info);\r
-                       \r
-               }\r
-\r
-               public static void Shutdown (IWebBrowser control)\r
-               {\r
-                       if (!isInitialized ())\r
-                               return;\r
-                       BindingInfo info = getBinding (control);\r
-\r
-                       gluezilla_shutdown (info.gluezilla);\r
-               }\r
-\r
-               // layout\r
-               public static void Focus (IWebBrowser control, FocusOption focus)\r
-               {\r
-                       if (!isInitialized ())\r
-                               return;\r
-                       BindingInfo info = getBinding (control);\r
-\r
-                       gluezilla_focus (info.gluezilla, focus);\r
-               }\r
-\r
-\r
-               public static void Blur (IWebBrowser control)\r
-               {\r
-                       if (!isInitialized ())\r
-                               return;\r
-                       BindingInfo info = getBinding (control);\r
-\r
-                       gluezilla_blur (info.gluezilla);\r
-               }\r
-\r
-               public static void Activate (IWebBrowser control)\r
-               {\r
-                       if (!isInitialized ())\r
-                               return;\r
-                       BindingInfo info = getBinding (control);\r
-\r
-                       gluezilla_activate (info.gluezilla);\r
-               }\r
-\r
-               public static void Deactivate (IWebBrowser control)\r
-               {\r
-                       if (!isInitialized ())\r
-                               return;\r
-                       BindingInfo info = getBinding (control);\r
-\r
-                       gluezilla_deactivate (info.gluezilla);\r
-               }\r
-\r
-               public static void Resize (IWebBrowser control, int width, int height)\r
-               {\r
-                       if (!isInitialized ())\r
-                               return;\r
-                       BindingInfo info = getBinding (control);\r
-\r
-                       gluezilla_resize (info.gluezilla, width, height);\r
-               }\r
-\r
-               // navigation\r
-               public static void Home (IWebBrowser control)\r
-               {\r
-                       if (!isInitialized ())\r
-                               return;\r
-                       BindingInfo info = getBinding (control);\r
-\r
-                       gluezilla_home (info.gluezilla);\r
-               }\r
-\r
-               public static nsIWebNavigation GetWebNavigation (IWebBrowser control)\r
-               {\r
-                       if (!isInitialized ())\r
-                               return null;\r
-                       BindingInfo info = getBinding (control);\r
-\r
-                       return gluezilla_getWebNavigation (info.gluezilla);\r
-               }\r
-\r
-               public static IntPtr StringInit ()\r
-               {\r
-                       return gluezilla_stringInit ();\r
-               }\r
-\r
-               public static void StringFinish (HandleRef str)\r
-               {\r
-                       gluezilla_stringFinish (str);\r
-               }\r
-\r
-               public static string StringGet (HandleRef str)\r
-               {\r
-                       IntPtr p = gluezilla_stringGet (str);\r
-                       return Marshal.PtrToStringUni (p);\r
-               }\r
-\r
-               public static void StringSet (HandleRef str, string text)\r
-               {\r
-                       gluezilla_stringSet (str, text);\r
-               }\r
-\r
-\r
-               public static object GetProxyForObject (IWebBrowser control, Guid iid, object obj)\r
-               {\r
-                       if (!isInitialized ())\r
-                               return null;\r
-                       BindingInfo info = getBinding (control);\r
-                       \r
-                       IntPtr ret;\r
-                       gluezilla_getProxyForObject (info.gluezilla, iid, obj, out ret);\r
-                       \r
-                       object o = Marshal.GetObjectForIUnknown (ret);\r
-                       return o;\r
-               }\r
-\r
-               public static string EvalScript (IWebBrowser control, string script)\r
-               {\r
-                       if (!isInitialized ())\r
-                               return null;\r
-                       BindingInfo info = getBinding (control);\r
-                       IntPtr p = gluezilla_evalScript (info.gluezilla, script);\r
-                       return Marshal.PtrToStringAuto (p);\r
-               }\r
-
-
-               #region pinvokes\r
-               [DllImport("gluezilla")]\r
-               private static extern void gluezilla_debug(int signal);\r
-\r
-               [DllImport("gluezilla")]\r
-               private static extern IntPtr gluezilla_init (Platform platform, out Platform mozPlatform);\r
-\r
-               [DllImport ("gluezilla")]\r
-               private static extern IntPtr gluezilla_shutdown (IntPtr instance);\r
-\r
-               [DllImport ("gluezilla")]\r
-               private static extern IntPtr gluezilla_createBrowserWindow (/*IntPtr instance, */IntPtr events, IntPtr hwnd, Int32 width, Int32 height, string startDir, string dataDir, Platform platform);\r
-               \r
-               // layout\r
-               [DllImport ("gluezilla")]\r
-               private static extern int gluezilla_focus (IntPtr instance, FocusOption focus);\r
-               [DllImport ("gluezilla")]\r
-               private static extern int gluezilla_blur (IntPtr instance);\r
-               [DllImport ("gluezilla")]\r
-               private static extern int gluezilla_activate (IntPtr instance);\r
-               [DllImport ("gluezilla")]\r
-               private static extern int gluezilla_deactivate (IntPtr instance);\r
-               [DllImport ("gluezilla")]\r
-               private static extern int gluezilla_resize (IntPtr instance, Int32 width, Int32 height);\r
-\r
-               // navigation\r
-               [DllImport ("gluezilla")]\r
-               private static extern int gluezilla_home (IntPtr instance);\r
-\r
-               // dom\r
-               [DllImport ("gluezilla")]\r
-               [return:MarshalAs(UnmanagedType.Interface)]\r
-               private static extern nsIWebNavigation gluezilla_getWebNavigation (IntPtr instance);\r
-\r
-               [DllImport ("gluezilla")]\r
-               private static extern IntPtr gluezilla_stringInit ();\r
-               [DllImport ("gluezilla")]\r
-               private static extern int gluezilla_stringFinish (HandleRef str);\r
-               [DllImport ("gluezilla")]\r
-               private static extern IntPtr gluezilla_stringGet (HandleRef str);\r
-               [DllImport ("gluezilla")]\r
-               private static extern void gluezilla_stringSet (HandleRef str, [MarshalAs (UnmanagedType.LPWStr)] string text);\r
-\r
-               [DllImport ("gluezilla")]\r
-               private static extern void gluezilla_getProxyForObject (\r
-                       IntPtr instance, \r
-                       [MarshalAs (UnmanagedType.LPStruct)] Guid iid, \r
-                       [MarshalAs (UnmanagedType.Interface)] object obj,\r
-                       out IntPtr ret);\r
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+// Copyright (c) 2007, 2008 Novell, Inc.
+//
+// Authors:
+//     Andreia Gaita (avidigal@novell.com)
+//
+
+using System;
+using System.Text;
+using System.Collections;
+using System.Runtime.InteropServices;
+using System.Diagnostics;
+using Mono.WebBrowser;
+
+namespace Mono.Mozilla
+{
+       internal class Base
+       {
+               private static Hashtable boundControls;
+               private static bool initialized;
+               private static object initLock = new object ();
+               private static string monoMozDir;
+
+               private class BindingInfo
+               {
+                       public CallbackBinder callback;
+                       public IntPtr gluezilla;
+               }
+
+               private static bool isInitialized ()
+               {
+                       if (!initialized)
+                               return false;
+                       return true;
+               }
+
+               private static BindingInfo getBinding (IWebBrowser control)
+               {
+                       if (!boundControls.ContainsKey (control))
+                               return null;
+                       BindingInfo info = boundControls[control] as BindingInfo;
+                       return info;
+               }
+
+               static Base ()
+               {
+                       boundControls = new Hashtable ();
+               }
+
+               public Base () { }
+
+               public static void Debug (int signal)
+               {
+                       gluezilla_debug (signal);
+               }
+               
+               public static bool Init (WebBrowser control, Platform platform)
+               {
+                       lock (initLock) {
+                               if (!initialized) {
+                               
+                                       Platform mozPlatform;
+                                       try {
+                                               short version = gluezilla_init (platform, out mozPlatform);
+
+                                               monoMozDir = System.IO.Path.Combine (
+                                               System.IO.Path.Combine (
+                                               Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData),
+                                               ".mono"), "mozilla-" + version);
+
+                                       if (!System.IO.Directory.Exists (monoMozDir))
+                                               System.IO.Directory.CreateDirectory (monoMozDir);
+                                               
+                                       }
+                                       catch (DllNotFoundException) {
+                                               Console.WriteLine ("libgluezilla not found. To have webbrowser support, you need libgluezilla installed");
+                                               initialized = false;
+                                               return false;
+                                       }
+                                       control.enginePlatform = mozPlatform;
+                                       initialized = true;
+                               }
+                       }
+                       return initialized;
+               }
+
+               public static bool Bind (WebBrowser control, IntPtr handle, int width, int height)
+               {
+                       if (!isInitialized ())
+                               return false;
+
+                       BindingInfo info = new BindingInfo ();
+                       info.callback = new CallbackBinder (control.callbacks);
+                       IntPtr ptrCallback = Marshal.AllocHGlobal (Marshal.SizeOf (info.callback));
+                       Marshal.StructureToPtr (info.callback, ptrCallback, true);
+                       
+                       info.gluezilla = gluezilla_bind (ptrCallback, handle, width, height, Environment.CurrentDirectory, monoMozDir, control.platform);
+                       lock (initLock) {
+                               if (info.gluezilla == IntPtr.Zero) {
+                                       Marshal.FreeHGlobal (ptrCallback);
+                                       info = null;
+                                       initialized = false;
+                                       return false;
+                               }
+                       }
+                       boundControls.Add (control as IWebBrowser, info);
+                       return true;
+               }
+
+               public static bool Create (IWebBrowser control) {
+                       if (!isInitialized ())
+                               return false;
+                       BindingInfo info = getBinding (control);
+
+                       gluezilla_createBrowserWindow (info.gluezilla);
+                       return true;
+               }
+
+               public static void Shutdown (IWebBrowser control)
+               {
+                       lock (initLock) {
+                               if (!initialized)
+                                       return;
+                                       
+                               BindingInfo info = getBinding (control);
+                               
+                               gluezilla_shutdown (info.gluezilla);
+                               boundControls.Remove (control);
+                               if (boundControls.Count == 0) {
+                                       info = null;
+                                       initialized = false;
+                               }
+                       }
+               }
+
+               // layout
+               public static void Focus (IWebBrowser control, FocusOption focus)
+               {
+                       if (!isInitialized ())
+                               return;
+                       BindingInfo info = getBinding (control);
+
+                       gluezilla_focus (info.gluezilla, focus);
+               }
+
+
+               public static void Blur (IWebBrowser control)
+               {
+                       if (!isInitialized ())
+                               return;
+                       BindingInfo info = getBinding (control);
+
+                       gluezilla_blur (info.gluezilla);
+               }
+
+               public static void Activate (IWebBrowser control)
+               {
+                       if (!isInitialized ())
+                               return;
+                       BindingInfo info = getBinding (control);
+
+                       gluezilla_activate (info.gluezilla);
+               }
+
+               public static void Deactivate (IWebBrowser control)
+               {
+                       if (!isInitialized ())
+                               return;
+                       BindingInfo info = getBinding (control);
+
+                       gluezilla_deactivate (info.gluezilla);
+               }
+
+               public static void Resize (IWebBrowser control, int width, int height)
+               {
+                       if (!isInitialized ())
+                               return;
+                       BindingInfo info = getBinding (control);
+
+                       gluezilla_resize (info.gluezilla, width, height);
+               }
+
+               // navigation
+               public static void Home (IWebBrowser control)
+               {
+                       if (!isInitialized ())
+                               return;
+                       BindingInfo info = getBinding (control);
+
+                       gluezilla_home (info.gluezilla);
+               }
+
+               public static nsIWebNavigation GetWebNavigation (IWebBrowser control)
+               {
+                       if (!isInitialized ())
+                               return null;
+                       BindingInfo info = getBinding (control);
+
+                       return gluezilla_getWebNavigation (info.gluezilla);
+               }
+
+               public static IntPtr StringInit ()
+               {
+                       if (!isInitialized ())
+                               return IntPtr.Zero;
+                       return gluezilla_stringInit ();
+               }
+
+               public static void StringFinish (HandleRef str)
+               {
+                       if (!isInitialized ())
+                               return;
+                       gluezilla_stringFinish (str);
+               }
+
+               public static string StringGet (HandleRef str)
+               {
+                       if (!isInitialized ())
+                               return String.Empty;
+                       IntPtr p = gluezilla_stringGet (str);
+                       return Marshal.PtrToStringUni (p);
+               }
+
+               public static void StringSet (HandleRef str, string text)
+               {
+                       if (!isInitialized ())
+                               return;
+                       gluezilla_stringSet (str, text);
+               }
+
+
+               public static object GetProxyForObject (IWebBrowser control, Guid iid, object obj)
+               {
+                       if (!isInitialized ())
+                               return null;
+                       BindingInfo info = getBinding (control);
+                       
+                       IntPtr ret;
+                       gluezilla_getProxyForObject (info.gluezilla, iid, obj, out ret);
+                       
+                       object o = Marshal.GetObjectForIUnknown (ret);
+                       return o;
+               }
+
+               public static nsIServiceManager GetServiceManager (IWebBrowser control)
+               {
+                       if (!isInitialized ())
+                               return null;
+                       BindingInfo info = getBinding (control);
+                                       
+                       return gluezilla_getServiceManager2 (info.gluezilla);
+               }               
+
+               public static string EvalScript (IWebBrowser control, string script)
+               {
+                       if (!isInitialized ())
+                               return null;
+                       BindingInfo info = getBinding (control);
+                       IntPtr p = gluezilla_evalScript (info.gluezilla, script);
+                       return Marshal.PtrToStringAuto (p);
+               }
+
+
+               #region pinvokes
+               [DllImport("gluezilla")]
+               private static extern void gluezilla_debug(int signal);
+
+               [DllImport("gluezilla")]
+               private static extern short gluezilla_init (Platform platform, out Platform mozPlatform);
+
+               [DllImport ("gluezilla")]
+               private static extern IntPtr gluezilla_bind (IntPtr events, IntPtr hwnd,
+                                       Int32 width, Int32 height,
+                                       string startDir, string dataDir,
+                                       Platform platform);
+
+               [DllImport ("gluezilla")]
+               private static extern int gluezilla_createBrowserWindow (IntPtr instance);
+
+               [DllImport ("gluezilla")]
+               private static extern IntPtr gluezilla_shutdown (IntPtr instance);
+
+               // layout
+               [DllImport ("gluezilla")]
+               private static extern int gluezilla_focus (IntPtr instance, FocusOption focus);
+               [DllImport ("gluezilla")]
+               private static extern int gluezilla_blur (IntPtr instance);
+               [DllImport ("gluezilla")]
+               private static extern int gluezilla_activate (IntPtr instance);
+               [DllImport ("gluezilla")]
+               private static extern int gluezilla_deactivate (IntPtr instance);
+               [DllImport ("gluezilla")]
+               private static extern int gluezilla_resize (IntPtr instance, Int32 width, Int32 height);
+
+               // navigation
+               [DllImport ("gluezilla")]
+               private static extern int gluezilla_home (IntPtr instance);
+
+               // dom
+               [DllImport ("gluezilla")]
+               [return:MarshalAs(UnmanagedType.Interface)]
+               private static extern nsIWebNavigation gluezilla_getWebNavigation (IntPtr instance);
+
+               [DllImport ("gluezilla")]
+               private static extern IntPtr gluezilla_stringInit ();
+               [DllImport ("gluezilla")]
+               private static extern int gluezilla_stringFinish (HandleRef str);
+               [DllImport ("gluezilla")]
+               private static extern IntPtr gluezilla_stringGet (HandleRef str);
+               [DllImport ("gluezilla")]
+               private static extern void gluezilla_stringSet (HandleRef str, [MarshalAs (UnmanagedType.LPWStr)] string text);
+
+               [DllImport ("gluezilla")]
+               private static extern void gluezilla_getProxyForObject (
+                       IntPtr instance, 
+                       [MarshalAs (UnmanagedType.LPStruct)] Guid iid, 
+                       [MarshalAs (UnmanagedType.Interface)] object obj,
+                       out IntPtr ret);
 
 
                [DllImport ("gluezilla")]
@@ -322,13 +367,13 @@ namespace Mono.Mozilla
                        string aBuf, 
                        uint aCount);
 
-               [DllImport ("gluezilla")]\r
+               [DllImport ("gluezilla")]
                [return: MarshalAs (UnmanagedType.Interface)]
-               public static extern nsIServiceManager  gluezilla_getServiceManager ();\r
-\r
-               [DllImport ("gluezilla")]\r
+               public static extern nsIServiceManager  gluezilla_getServiceManager2 (IntPtr instance);
+
+               [DllImport ("gluezilla")]
                private static extern IntPtr gluezilla_evalScript (IntPtr instance, string script);
-\r
-               #endregion\r
-       }\r
-}\r
+
+               #endregion
+       }
+}