Update class library csproj files
[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_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;
122                                 }
123                         }
124                         boundControls.Add (control as IWebBrowser, info);
125                         return true;
126                 }
127
128                 public static bool Create (IWebBrowser control) {
129                         if (!isInitialized ())
130                                 return false;
131                         BindingInfo info = getBinding (control);
132
133                         gluezilla_createBrowserWindow (info.gluezilla);
134                         return true;
135                 }
136
137                 public static void Shutdown (IWebBrowser control)
138                 {
139                         lock (initLock) {
140                                 if (!initialized)
141                                         return;
142                                         
143                                 BindingInfo info = getBinding (control);
144                                 
145                                 gluezilla_shutdown (info.gluezilla);
146                                 boundControls.Remove (control);
147                                 if (boundControls.Count == 0) {
148                                         info = null;
149                                         initialized = false;
150                                 }
151                         }
152                 }
153
154                 // layout
155                 public static void Focus (IWebBrowser control, FocusOption focus)
156                 {
157                         if (!isInitialized ())
158                                 return;
159                         BindingInfo info = getBinding (control);
160
161                         gluezilla_focus (info.gluezilla, focus);
162                 }
163
164
165                 public static void Blur (IWebBrowser control)
166                 {
167                         if (!isInitialized ())
168                                 return;
169                         BindingInfo info = getBinding (control);
170
171                         gluezilla_blur (info.gluezilla);
172                 }
173
174                 public static void Activate (IWebBrowser control)
175                 {
176                         if (!isInitialized ())
177                                 return;
178                         BindingInfo info = getBinding (control);
179
180                         gluezilla_activate (info.gluezilla);
181                 }
182
183                 public static void Deactivate (IWebBrowser control)
184                 {
185                         if (!isInitialized ())
186                                 return;
187                         BindingInfo info = getBinding (control);
188
189                         gluezilla_deactivate (info.gluezilla);
190                 }
191
192                 public static void Resize (IWebBrowser control, int width, int height)
193                 {
194                         if (!isInitialized ())
195                                 return;
196                         BindingInfo info = getBinding (control);
197
198                         gluezilla_resize (info.gluezilla, width, height);
199                 }
200
201                 // navigation
202                 public static void Home (IWebBrowser control)
203                 {
204                         if (!isInitialized ())
205                                 return;
206                         BindingInfo info = getBinding (control);
207
208                         gluezilla_home (info.gluezilla);
209                 }
210
211                 public static nsIWebNavigation GetWebNavigation (IWebBrowser control)
212                 {
213                         if (!isInitialized ())
214                                 return null;
215                         BindingInfo info = getBinding (control);
216
217                         return gluezilla_getWebNavigation (info.gluezilla);
218                 }
219
220                 public static IntPtr StringInit ()
221                 {
222                         if (!isInitialized ())
223                                 return IntPtr.Zero;
224                         return gluezilla_stringInit ();
225                 }
226
227                 public static void StringFinish (HandleRef str)
228                 {
229                         if (!isInitialized ())
230                                 return;
231                         gluezilla_stringFinish (str);
232                 }
233
234                 public static string StringGet (HandleRef str)
235                 {
236                         if (!isInitialized ())
237                                 return String.Empty;
238                         IntPtr p = gluezilla_stringGet (str);
239                         return Marshal.PtrToStringUni (p);
240                 }
241
242                 public static void StringSet (HandleRef str, string text)
243                 {
244                         if (!isInitialized ())
245                                 return;
246                         gluezilla_stringSet (str, text);
247                 }
248
249
250                 public static object GetProxyForObject (IWebBrowser control, Guid iid, object obj)
251                 {
252                         if (!isInitialized ())
253                                 return null;
254                         BindingInfo info = getBinding (control);
255                         
256                         IntPtr ret;
257                         gluezilla_getProxyForObject (info.gluezilla, iid, obj, out ret);
258                         
259                         object o = Marshal.GetObjectForIUnknown (ret);
260                         return o;
261                 }
262
263                 public static nsIServiceManager GetServiceManager (IWebBrowser control)
264                 {
265                         if (!isInitialized ())
266                                 return null;
267                         BindingInfo info = getBinding (control);
268                                         
269                         return gluezilla_getServiceManager2 (info.gluezilla);
270                 }               
271
272                 public static string EvalScript (IWebBrowser control, string script)
273                 {
274                         if (!isInitialized ())
275                                 return null;
276                         BindingInfo info = getBinding (control);
277                         IntPtr p = gluezilla_evalScript (info.gluezilla, script);
278                         return Marshal.PtrToStringAuto (p);
279                 }
280
281
282                 #region pinvokes
283                 [DllImport("gluezilla")]
284                 private static extern void gluezilla_debug(int signal);
285
286                 [DllImport("gluezilla")]
287                 private static extern short gluezilla_init (Platform platform, out Platform mozPlatform);
288
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);
294
295                 [DllImport ("gluezilla")]
296                 private static extern int gluezilla_createBrowserWindow (IntPtr instance);
297
298                 [DllImport ("gluezilla")]
299                 private static extern IntPtr gluezilla_shutdown (IntPtr instance);
300
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);
312
313                 // navigation
314                 [DllImport ("gluezilla")]
315                 private static extern int gluezilla_home (IntPtr instance);
316
317                 // dom
318                 [DllImport ("gluezilla")]
319                 [return:MarshalAs(UnmanagedType.Interface)]
320                 private static extern nsIWebNavigation gluezilla_getWebNavigation (IntPtr instance);
321
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);
330
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);
337
338
339                 [DllImport ("gluezilla")]
340                 public static extern uint gluezilla_StringContainerInit (HandleRef /*nsStringContainer & */aStr);
341
342                 [DllImport ("gluezilla")]
343                 public static extern void gluezilla_StringContainerFinish (HandleRef /*nsStringContainer & */aStr);
344
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);
349
350                 [DllImport ("gluezilla")]
351                 public static extern uint gluezilla_StringSetData (HandleRef /*nsAString &*/ aStr, 
352                         [MarshalAs (UnmanagedType.LPWStr)] string /*const PRUnichar * */ aBuf, uint aCount);
353
354                 [DllImport ("gluezilla")]
355                 public static extern uint gluezilla_CStringContainerInit (HandleRef /*nsCStringContainer &*/ aStr);
356
357                 [DllImport ("gluezilla")]
358                 public static extern void gluezilla_CStringContainerFinish (HandleRef /*nsCStringContainer &*/ aStr);
359
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);
364
365                 [DllImport ("gluezilla")]
366                 public static extern uint gluezilla_CStringSetData (HandleRef /*nsACString &*/ aStr, 
367                         string aBuf, 
368                         uint aCount);
369
370                 [DllImport ("gluezilla")]
371                 [return: MarshalAs (UnmanagedType.Interface)]
372                 public static extern nsIServiceManager  gluezilla_getServiceManager2 (IntPtr instance);
373
374                 [DllImport ("gluezilla")]
375                 private static extern IntPtr gluezilla_evalScript (IntPtr instance, string script);
376
377                 #endregion
378         }
379 }