2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.WebBrowser / Mono.Mozilla / Base.cs
blobf6221a7d19eef162ecfc8822aabf19c843a72a75
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2007, 2008 Novell, Inc.
22 // Authors:
23 // Andreia Gaita (avidigal@novell.com)
26 using System;
27 using System.Text;
28 using System.Collections;
29 using System.Runtime.InteropServices;
30 using System.Diagnostics;
31 using Mono.WebBrowser;
33 namespace Mono.Mozilla
35 internal class Base
37 private static Hashtable boundControls;
38 private static bool initialized;
39 private static object initLock = new object ();
40 private static string monoMozDir;
42 private class BindingInfo
44 public CallbackBinder callback;
45 public IntPtr gluezilla;
48 private static bool isInitialized ()
50 if (!initialized)
51 return false;
52 return true;
55 private static BindingInfo getBinding (IWebBrowser control)
57 if (!boundControls.ContainsKey (control))
58 return null;
59 BindingInfo info = boundControls[control] as BindingInfo;
60 return info;
63 static Base ()
65 boundControls = new Hashtable ();
68 public Base () { }
70 public static void Debug (int signal)
72 gluezilla_debug (signal);
75 public static bool Init (WebBrowser control, Platform platform)
77 lock (initLock) {
78 if (!initialized) {
80 Platform mozPlatform;
81 try {
82 short version = gluezilla_init (platform, out mozPlatform);
84 monoMozDir = System.IO.Path.Combine (
85 System.IO.Path.Combine (
86 Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData),
87 ".mono"), "mozilla-" + version);
89 if (!System.IO.Directory.Exists (monoMozDir))
90 System.IO.Directory.CreateDirectory (monoMozDir);
93 catch (DllNotFoundException) {
94 Console.WriteLine ("libgluezilla not found. To have webbrowser support, you need libgluezilla installed");
95 initialized = false;
96 return false;
98 control.enginePlatform = mozPlatform;
99 initialized = true;
102 return initialized;
105 public static bool Bind (WebBrowser control, IntPtr handle, int width, int height)
107 if (!isInitialized ())
108 return false;
110 BindingInfo info = new BindingInfo ();
111 info.callback = new CallbackBinder (control.callbacks);
112 IntPtr ptrCallback = Marshal.AllocHGlobal (Marshal.SizeOf (info.callback));
113 Marshal.StructureToPtr (info.callback, ptrCallback, true);
115 info.gluezilla = gluezilla_bind (ptrCallback, handle, width, height, Environment.CurrentDirectory, monoMozDir, control.platform);
116 lock (initLock) {
117 if (info.gluezilla == IntPtr.Zero) {
118 Marshal.FreeHGlobal (ptrCallback);
119 info = null;
120 initialized = false;
121 return false;
124 boundControls.Add (control as IWebBrowser, info);
125 return true;
128 public static bool Create (IWebBrowser control) {
129 if (!isInitialized ())
130 return false;
131 BindingInfo info = getBinding (control);
133 gluezilla_createBrowserWindow (info.gluezilla);
134 return true;
137 public static void Shutdown (IWebBrowser control)
139 lock (initLock) {
140 if (!initialized)
141 return;
143 BindingInfo info = getBinding (control);
145 gluezilla_shutdown (info.gluezilla);
146 boundControls.Remove (control);
147 if (boundControls.Count == 0) {
148 info = null;
149 initialized = false;
154 // layout
155 public static void Focus (IWebBrowser control, FocusOption focus)
157 if (!isInitialized ())
158 return;
159 BindingInfo info = getBinding (control);
161 gluezilla_focus (info.gluezilla, focus);
165 public static void Blur (IWebBrowser control)
167 if (!isInitialized ())
168 return;
169 BindingInfo info = getBinding (control);
171 gluezilla_blur (info.gluezilla);
174 public static void Activate (IWebBrowser control)
176 if (!isInitialized ())
177 return;
178 BindingInfo info = getBinding (control);
180 gluezilla_activate (info.gluezilla);
183 public static void Deactivate (IWebBrowser control)
185 if (!isInitialized ())
186 return;
187 BindingInfo info = getBinding (control);
189 gluezilla_deactivate (info.gluezilla);
192 public static void Resize (IWebBrowser control, int width, int height)
194 if (!isInitialized ())
195 return;
196 BindingInfo info = getBinding (control);
198 gluezilla_resize (info.gluezilla, width, height);
201 // navigation
202 public static void Home (IWebBrowser control)
204 if (!isInitialized ())
205 return;
206 BindingInfo info = getBinding (control);
208 gluezilla_home (info.gluezilla);
211 public static nsIWebNavigation GetWebNavigation (IWebBrowser control)
213 if (!isInitialized ())
214 return null;
215 BindingInfo info = getBinding (control);
217 return gluezilla_getWebNavigation (info.gluezilla);
220 public static IntPtr StringInit ()
222 if (!isInitialized ())
223 return IntPtr.Zero;
224 return gluezilla_stringInit ();
227 public static void StringFinish (HandleRef str)
229 if (!isInitialized ())
230 return;
231 gluezilla_stringFinish (str);
234 public static string StringGet (HandleRef str)
236 if (!isInitialized ())
237 return String.Empty;
238 IntPtr p = gluezilla_stringGet (str);
239 return Marshal.PtrToStringUni (p);
242 public static void StringSet (HandleRef str, string text)
244 if (!isInitialized ())
245 return;
246 gluezilla_stringSet (str, text);
250 public static object GetProxyForObject (IWebBrowser control, Guid iid, object obj)
252 if (!isInitialized ())
253 return null;
254 BindingInfo info = getBinding (control);
256 IntPtr ret;
257 gluezilla_getProxyForObject (info.gluezilla, iid, obj, out ret);
259 object o = Marshal.GetObjectForIUnknown (ret);
260 return o;
263 public static nsIServiceManager GetServiceManager (IWebBrowser control)
265 if (!isInitialized ())
266 return null;
267 BindingInfo info = getBinding (control);
269 return gluezilla_getServiceManager2 (info.gluezilla);
272 public static string EvalScript (IWebBrowser control, string script)
274 if (!isInitialized ())
275 return null;
276 BindingInfo info = getBinding (control);
277 IntPtr p = gluezilla_evalScript (info.gluezilla, script);
278 return Marshal.PtrToStringAuto (p);
282 #region pinvokes
283 [DllImport("gluezilla")]
284 private static extern void gluezilla_debug(int signal);
286 [DllImport("gluezilla")]
287 private static extern short gluezilla_init (Platform platform, out Platform mozPlatform);
289 [DllImport ("gluezilla")]
290 private static extern IntPtr gluezilla_bind (IntPtr events, IntPtr hwnd,
291 Int32 width, Int32 height,
292 string startDir, string dataDir,
293 Platform platform);
295 [DllImport ("gluezilla")]
296 private static extern int gluezilla_createBrowserWindow (IntPtr instance);
298 [DllImport ("gluezilla")]
299 private static extern IntPtr gluezilla_shutdown (IntPtr instance);
301 // layout
302 [DllImport ("gluezilla")]
303 private static extern int gluezilla_focus (IntPtr instance, FocusOption focus);
304 [DllImport ("gluezilla")]
305 private static extern int gluezilla_blur (IntPtr instance);
306 [DllImport ("gluezilla")]
307 private static extern int gluezilla_activate (IntPtr instance);
308 [DllImport ("gluezilla")]
309 private static extern int gluezilla_deactivate (IntPtr instance);
310 [DllImport ("gluezilla")]
311 private static extern int gluezilla_resize (IntPtr instance, Int32 width, Int32 height);
313 // navigation
314 [DllImport ("gluezilla")]
315 private static extern int gluezilla_home (IntPtr instance);
317 // dom
318 [DllImport ("gluezilla")]
319 [return:MarshalAs(UnmanagedType.Interface)]
320 private static extern nsIWebNavigation gluezilla_getWebNavigation (IntPtr instance);
322 [DllImport ("gluezilla")]
323 private static extern IntPtr gluezilla_stringInit ();
324 [DllImport ("gluezilla")]
325 private static extern int gluezilla_stringFinish (HandleRef str);
326 [DllImport ("gluezilla")]
327 private static extern IntPtr gluezilla_stringGet (HandleRef str);
328 [DllImport ("gluezilla")]
329 private static extern void gluezilla_stringSet (HandleRef str, [MarshalAs (UnmanagedType.LPWStr)] string text);
331 [DllImport ("gluezilla")]
332 private static extern void gluezilla_getProxyForObject (
333 IntPtr instance,
334 [MarshalAs (UnmanagedType.LPStruct)] Guid iid,
335 [MarshalAs (UnmanagedType.Interface)] object obj,
336 out IntPtr ret);
339 [DllImport ("gluezilla")]
340 public static extern uint gluezilla_StringContainerInit (HandleRef /*nsStringContainer & */aStr);
342 [DllImport ("gluezilla")]
343 public static extern void gluezilla_StringContainerFinish (HandleRef /*nsStringContainer & */aStr);
345 [DllImport ("gluezilla")]
346 public static extern uint gluezilla_StringGetData (HandleRef /*const nsAString &*/ aStr,
347 out IntPtr /*const PRUnichar ** */aBuf,
348 out bool /*PRBool * */aTerm);
350 [DllImport ("gluezilla")]
351 public static extern uint gluezilla_StringSetData (HandleRef /*nsAString &*/ aStr,
352 [MarshalAs (UnmanagedType.LPWStr)] string /*const PRUnichar * */ aBuf, uint aCount);
354 [DllImport ("gluezilla")]
355 public static extern uint gluezilla_CStringContainerInit (HandleRef /*nsCStringContainer &*/ aStr);
357 [DllImport ("gluezilla")]
358 public static extern void gluezilla_CStringContainerFinish (HandleRef /*nsCStringContainer &*/ aStr);
360 [DllImport ("gluezilla")]
361 public static extern uint gluezilla_CStringGetData (HandleRef /*const nsACString &*/ aStr,
362 out IntPtr /*const PRUnichar ** */aBuf,
363 out bool /*PRBool **/ aTerm);
365 [DllImport ("gluezilla")]
366 public static extern uint gluezilla_CStringSetData (HandleRef /*nsACString &*/ aStr,
367 string aBuf,
368 uint aCount);
370 [DllImport ("gluezilla")]
371 [return: MarshalAs (UnmanagedType.Interface)]
372 public static extern nsIServiceManager gluezilla_getServiceManager2 (IntPtr instance);
374 [DllImport ("gluezilla")]
375 private static extern IntPtr gluezilla_evalScript (IntPtr instance, string script);
377 #endregion