* gluezilla/src/gluezilla.cpp, gluezilla/src/gluezilla.h: Return the
[mono.git] / mcs / class / Mono.WebBrowser / Mono.Mozilla / Base.cs
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.
19 //
20 // Copyright (c) 2007, 2008 Novell, Inc.
21 //
22 // Authors:
23 //      Andreia Gaita (avidigal@novell.com)
24 //
25
26 using System;
27 using System.Text;
28 using System.Collections;
29 using System.Runtime.InteropServices;
30 using System.Diagnostics;
31 using Mono.WebBrowser;
32
33 namespace Mono.Mozilla
34 {
35         internal class Base
36         {
37                 private static Hashtable boundControls;
38                 private static bool initialized;
39                 private static object initLock = new object ();
40                 private static string monoMozDir;
41
42                 private class BindingInfo
43                 {
44                         public CallbackBinder callback;
45                         public IntPtr gluezilla;
46                 }
47
48                 private static bool isInitialized ()
49                 {
50                         if (!initialized)
51                                 return false;
52                         return true;
53                 }
54
55                 private static BindingInfo getBinding (IWebBrowser control)
56                 {
57                         if (!boundControls.ContainsKey (control))
58                                 return null;
59                         BindingInfo info = boundControls[control] as BindingInfo;
60                         return info;
61                 }
62
63                 static Base ()
64                 {
65                         boundControls = new Hashtable ();
66                 }
67
68                 public Base () { }
69
70                 public static void Debug (int signal)
71                 {
72                         gluezilla_debug (signal);
73                 }
74                 
75                 public static bool Init (WebBrowser control, Platform platform)
76                 {
77                         lock (initLock) {
78                                 if (!initialized) {
79                                 
80                                         Platform mozPlatform;
81                                         try {
82                                                 short version = gluezilla_init (platform, out mozPlatform);
83
84                                                 monoMozDir = System.IO.Path.Combine (
85                                                 System.IO.Path.Combine (
86                                                 Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData),
87                                                 ".mono"), "mozilla-" + version);
88
89                                         if (!System.IO.Directory.Exists (monoMozDir))
90                                                 System.IO.Directory.CreateDirectory (monoMozDir);
91                                                 
92                                         }
93                                         catch (DllNotFoundException) {
94                                                 Console.WriteLine ("libgluezilla not found. To have webbrowser support, you need libgluezilla installed");
95                                                 initialized = false;
96                                                 return false;
97                                         }
98                                         control.enginePlatform = mozPlatform;
99                                         initialized = true;
100                                 }
101                         }
102                         return initialized;
103                 }
104
105                 public static bool Bind (WebBrowser control, IntPtr handle, int width, int height)
106                 {
107                         if (!isInitialized ())
108                                 return false;
109
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);
114                         
115                         info.gluezilla = gluezilla_createBrowserWindow (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;
122                                 }
123                         }
124                         boundControls.Add (control as IWebBrowser, info);
125                         return true;
126                 }
127
128                 public static void Shutdown (IWebBrowser control)
129                 {
130                         lock (initLock) {
131                                 if (!initialized)
132                                         return;
133                                         
134                                 BindingInfo info = getBinding (control);
135                                 
136                                 gluezilla_shutdown (info.gluezilla);
137                                 boundControls.Remove (control);
138                                 if (boundControls.Count == 0) {
139                                         info = null;
140                                         initialized = false;
141                                 }
142                         }
143                 }
144
145                 // layout
146                 public static void Focus (IWebBrowser control, FocusOption focus)
147                 {
148                         if (!isInitialized ())
149                                 return;
150                         BindingInfo info = getBinding (control);
151
152                         gluezilla_focus (info.gluezilla, focus);
153                 }
154
155
156                 public static void Blur (IWebBrowser control)
157                 {
158                         if (!isInitialized ())
159                                 return;
160                         BindingInfo info = getBinding (control);
161
162                         gluezilla_blur (info.gluezilla);
163                 }
164
165                 public static void Activate (IWebBrowser control)
166                 {
167                         if (!isInitialized ())
168                                 return;
169                         BindingInfo info = getBinding (control);
170
171                         gluezilla_activate (info.gluezilla);
172                 }
173
174                 public static void Deactivate (IWebBrowser control)
175                 {
176                         if (!isInitialized ())
177                                 return;
178                         BindingInfo info = getBinding (control);
179
180                         gluezilla_deactivate (info.gluezilla);
181                 }
182
183                 public static void Resize (IWebBrowser control, int width, int height)
184                 {
185                         if (!isInitialized ())
186                                 return;
187                         BindingInfo info = getBinding (control);
188
189                         gluezilla_resize (info.gluezilla, width, height);
190                 }
191
192                 // navigation
193                 public static void Home (IWebBrowser control)
194                 {
195                         if (!isInitialized ())
196                                 return;
197                         BindingInfo info = getBinding (control);
198
199                         gluezilla_home (info.gluezilla);
200                 }
201
202                 public static nsIWebNavigation GetWebNavigation (IWebBrowser control)
203                 {
204                         if (!isInitialized ())
205                                 return null;
206                         BindingInfo info = getBinding (control);
207
208                         return gluezilla_getWebNavigation (info.gluezilla);
209                 }
210
211                 public static IntPtr StringInit ()
212                 {
213                         if (!isInitialized ())
214                                 return IntPtr.Zero;
215                         return gluezilla_stringInit ();
216                 }
217
218                 public static void StringFinish (HandleRef str)
219                 {
220                         if (!isInitialized ())
221                                 return;
222                         gluezilla_stringFinish (str);
223                 }
224
225                 public static string StringGet (HandleRef str)
226                 {
227                         if (!isInitialized ())
228                                 return String.Empty;
229                         IntPtr p = gluezilla_stringGet (str);
230                         return Marshal.PtrToStringUni (p);
231                 }
232
233                 public static void StringSet (HandleRef str, string text)
234                 {
235                         if (!isInitialized ())
236                                 return;
237                         gluezilla_stringSet (str, text);
238                 }
239
240
241                 public static object GetProxyForObject (IWebBrowser control, Guid iid, object obj)
242                 {
243                         if (!isInitialized ())
244                                 return null;
245                         BindingInfo info = getBinding (control);
246                         
247                         IntPtr ret;
248                         gluezilla_getProxyForObject (info.gluezilla, iid, obj, out ret);
249                         
250                         object o = Marshal.GetObjectForIUnknown (ret);
251                         return o;
252                 }
253
254                 public static string EvalScript (IWebBrowser control, string script)
255                 {
256                         if (!isInitialized ())
257                                 return null;
258                         BindingInfo info = getBinding (control);
259                         IntPtr p = gluezilla_evalScript (info.gluezilla, script);
260                         return Marshal.PtrToStringAuto (p);
261                 }
262
263
264                 #region pinvokes
265                 [DllImport("gluezilla")]
266                 private static extern void gluezilla_debug(int signal);
267
268                 [DllImport("gluezilla")]
269                 private static extern short gluezilla_init (Platform platform, out Platform mozPlatform);
270
271                 [DllImport ("gluezilla")]
272                 private static extern IntPtr gluezilla_shutdown (IntPtr instance);
273
274                 [DllImport ("gluezilla")]
275                 private static extern IntPtr gluezilla_createBrowserWindow (/*IntPtr instance, */IntPtr events, IntPtr hwnd, Int32 width, Int32 height, string startDir, string dataDir, Platform platform);
276                 
277                 // layout
278                 [DllImport ("gluezilla")]
279                 private static extern int gluezilla_focus (IntPtr instance, FocusOption focus);
280                 [DllImport ("gluezilla")]
281                 private static extern int gluezilla_blur (IntPtr instance);
282                 [DllImport ("gluezilla")]
283                 private static extern int gluezilla_activate (IntPtr instance);
284                 [DllImport ("gluezilla")]
285                 private static extern int gluezilla_deactivate (IntPtr instance);
286                 [DllImport ("gluezilla")]
287                 private static extern int gluezilla_resize (IntPtr instance, Int32 width, Int32 height);
288
289                 // navigation
290                 [DllImport ("gluezilla")]
291                 private static extern int gluezilla_home (IntPtr instance);
292
293                 // dom
294                 [DllImport ("gluezilla")]
295                 [return:MarshalAs(UnmanagedType.Interface)]
296                 private static extern nsIWebNavigation gluezilla_getWebNavigation (IntPtr instance);
297
298                 [DllImport ("gluezilla")]
299                 private static extern IntPtr gluezilla_stringInit ();
300                 [DllImport ("gluezilla")]
301                 private static extern int gluezilla_stringFinish (HandleRef str);
302                 [DllImport ("gluezilla")]
303                 private static extern IntPtr gluezilla_stringGet (HandleRef str);
304                 [DllImport ("gluezilla")]
305                 private static extern void gluezilla_stringSet (HandleRef str, [MarshalAs (UnmanagedType.LPWStr)] string text);
306
307                 [DllImport ("gluezilla")]
308                 private static extern void gluezilla_getProxyForObject (
309                         IntPtr instance, 
310                         [MarshalAs (UnmanagedType.LPStruct)] Guid iid, 
311                         [MarshalAs (UnmanagedType.Interface)] object obj,
312                         out IntPtr ret);
313
314
315                 [DllImport ("gluezilla")]
316                 public static extern uint gluezilla_StringContainerInit (HandleRef /*nsStringContainer & */aStr);
317
318                 [DllImport ("gluezilla")]
319                 public static extern void gluezilla_StringContainerFinish (HandleRef /*nsStringContainer & */aStr);
320
321                 [DllImport ("gluezilla")]
322                 public static extern uint gluezilla_StringGetData (HandleRef /*const nsAString &*/ aStr, 
323                         out IntPtr /*const PRUnichar ** */aBuf, 
324                         out bool /*PRBool * */aTerm);
325
326                 [DllImport ("gluezilla")]
327                 public static extern uint gluezilla_StringSetData (HandleRef /*nsAString &*/ aStr, 
328                         [MarshalAs (UnmanagedType.LPWStr)] string /*const PRUnichar * */ aBuf, uint aCount);
329
330                 [DllImport ("gluezilla")]
331                 public static extern uint gluezilla_CStringContainerInit (HandleRef /*nsCStringContainer &*/ aStr);
332
333                 [DllImport ("gluezilla")]
334                 public static extern void gluezilla_CStringContainerFinish (HandleRef /*nsCStringContainer &*/ aStr);
335
336                 [DllImport ("gluezilla")]
337                 public static extern uint gluezilla_CStringGetData (HandleRef /*const nsACString &*/ aStr, 
338                         out IntPtr /*const PRUnichar ** */aBuf, 
339                         out bool /*PRBool **/ aTerm);
340
341                 [DllImport ("gluezilla")]
342                 public static extern uint gluezilla_CStringSetData (HandleRef /*nsACString &*/ aStr, 
343                         string aBuf, 
344                         uint aCount);
345
346                 [DllImport ("gluezilla")]
347                 [return: MarshalAs (UnmanagedType.Interface)]
348                 public static extern nsIServiceManager  gluezilla_getServiceManager ();
349
350                 [DllImport ("gluezilla")]
351                 private static extern IntPtr gluezilla_evalScript (IntPtr instance, string script);
352
353                 #endregion
354         }
355 }