* mcs/class/Makefile, mcs/class/Mono.Mozilla,
[mono.git] / mcs / class / Mono.WebBrowser / Mono.Mozilla / Base.cs
1 // Permission is hereby granted, free of charge, to any person obtaining\r
2 // a copy of this software and associated documentation files (the\r
3 // "Software"), to deal in the Software without restriction, including\r
4 // without limitation the rights to use, copy, modify, merge, publish,\r
5 // distribute, sublicense, and/or sell copies of the Software, and to\r
6 // permit persons to whom the Software is furnished to do so, subject to\r
7 // the following conditions:\r
8 // \r
9 // The above copyright notice and this permission notice shall be\r
10 // included in all copies or substantial portions of the Software.\r
11 // \r
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
19 //\r
20 // Copyright (c) 2007, 2008 Novell, Inc.\r
21 //\r
22 // Authors:\r
23 //      Andreia Gaita (avidigal@novell.com)\r
24 //\r
25 \r
26 using System;\r
27 using System.Text;\r
28 using System.Collections;\r
29 using System.Runtime.InteropServices;\r
30 using System.Diagnostics;\r
31 using Mono.WebBrowser;\r
32 \r
33 namespace Mono.Mozilla\r
34 {\r
35         internal class Base\r
36         {\r
37                 private static Hashtable boundControls;\r
38                 internal static bool gluezillaInstalled;\r
39                 internal static bool initialized;\r
40 \r
41                 private class BindingInfo\r
42                 {\r
43                         public CallbackBinder callback;\r
44                         public IntPtr gluezilla;\r
45                 }\r
46 \r
47                 private static bool isInitialized ()\r
48                 {\r
49                         if (!gluezillaInstalled)\r
50                                 return false;\r
51                         return true;\r
52                 }\r
53 \r
54                 private static BindingInfo getBinding (IWebBrowser control)\r
55                 {\r
56                         if (!boundControls.ContainsKey (control))\r
57                                 return null;\r
58                         BindingInfo info = boundControls[control] as BindingInfo;\r
59                         return info;\r
60                 }\r
61 \r
62                 static Base ()\r
63                 {\r
64                         boundControls = new Hashtable ();\r
65                 }\r
66 \r
67                 public Base () { }\r
68 \r
69                 public static void Debug (int signal)\r
70                 {\r
71                         gluezilla_debug (signal);\r
72                 }\r
73                 \r
74                 public static bool Init (WebBrowser control, Platform platform)\r
75                 {\r
76                         if (!initialized) {\r
77         \r
78                                 string monoMozDir = System.IO.Path.Combine (\r
79                                         System.IO.Path.Combine (\r
80                                         Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData),\r
81                                         ".mono"), "mozilla");\r
82         \r
83                                 if (!System.IO.Directory.Exists (monoMozDir))\r
84                                         System.IO.Directory.CreateDirectory (monoMozDir);\r
85         \r
86                                 Platform mozPlatform;\r
87                                 try {\r
88                                         gluezilla_init (platform, out mozPlatform);\r
89                                 }\r
90                                 catch (DllNotFoundException) {\r
91                                         Console.WriteLine ("libgluezilla not found. To have webbrowser support, you need libgluezilla installed");\r
92                                         gluezillaInstalled = false;\r
93                                         initialized = false;\r
94                                         return false;\r
95                                 }\r
96                                 control.enginePlatform = mozPlatform;\r
97                                 gluezillaInstalled = true;\r
98                                 initialized = true;\r
99                         }\r
100                         return initialized;\r
101                 }\r
102 \r
103                 public static void Bind (WebBrowser control, IntPtr handle, int width, int height)\r
104                 {\r
105                         if (!isInitialized ())\r
106                                 return;\r
107 \r
108                         BindingInfo info = new BindingInfo ();\r
109                         info.callback = new CallbackBinder (control.callbacks);\r
110                         IntPtr ptrCallback = Marshal.AllocHGlobal (Marshal.SizeOf (info.callback));\r
111                         Marshal.StructureToPtr (info.callback, ptrCallback, true);\r
112                         \r
113                         string monoMozDir = System.IO.Path.Combine (\r
114                                 System.IO.Path.Combine (\r
115                                 Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData),\r
116                                 ".mono"), "mozilla");\r
117                         info.gluezilla = gluezilla_createBrowserWindow (ptrCallback, handle, width, height, Environment.CurrentDirectory, monoMozDir, control.platform);\r
118                         \r
119                         boundControls.Add (control as IWebBrowser, info);\r
120                         \r
121                 }\r
122 \r
123                 public static void Shutdown (IWebBrowser control)\r
124                 {\r
125                         if (!isInitialized ())\r
126                                 return;\r
127                         BindingInfo info = getBinding (control);\r
128 \r
129                         gluezilla_shutdown (info.gluezilla);\r
130                 }\r
131 \r
132                 // layout\r
133                 public static void Focus (IWebBrowser control, FocusOption focus)\r
134                 {\r
135                         if (!isInitialized ())\r
136                                 return;\r
137                         BindingInfo info = getBinding (control);\r
138 \r
139                         gluezilla_focus (info.gluezilla, focus);\r
140                 }\r
141 \r
142 \r
143                 public static void Blur (IWebBrowser control)\r
144                 {\r
145                         if (!isInitialized ())\r
146                                 return;\r
147                         BindingInfo info = getBinding (control);\r
148 \r
149                         gluezilla_blur (info.gluezilla);\r
150                 }\r
151 \r
152                 public static void Activate (IWebBrowser control)\r
153                 {\r
154                         if (!isInitialized ())\r
155                                 return;\r
156                         BindingInfo info = getBinding (control);\r
157 \r
158                         gluezilla_activate (info.gluezilla);\r
159                 }\r
160 \r
161                 public static void Deactivate (IWebBrowser control)\r
162                 {\r
163                         if (!isInitialized ())\r
164                                 return;\r
165                         BindingInfo info = getBinding (control);\r
166 \r
167                         gluezilla_deactivate (info.gluezilla);\r
168                 }\r
169 \r
170                 public static void Resize (IWebBrowser control, int width, int height)\r
171                 {\r
172                         if (!isInitialized ())\r
173                                 return;\r
174                         BindingInfo info = getBinding (control);\r
175 \r
176                         gluezilla_resize (info.gluezilla, width, height);\r
177                 }\r
178 \r
179                 // navigation\r
180                 public static void Home (IWebBrowser control)\r
181                 {\r
182                         if (!isInitialized ())\r
183                                 return;\r
184                         BindingInfo info = getBinding (control);\r
185 \r
186                         gluezilla_home (info.gluezilla);\r
187                 }\r
188 \r
189                 public static nsIWebNavigation GetWebNavigation (IWebBrowser control)\r
190                 {\r
191                         if (!isInitialized ())\r
192                                 return null;\r
193                         BindingInfo info = getBinding (control);\r
194 \r
195                         return gluezilla_getWebNavigation (info.gluezilla);\r
196                 }\r
197 \r
198                 public static IntPtr StringInit ()\r
199                 {\r
200                         return gluezilla_stringInit ();\r
201                 }\r
202 \r
203                 public static void StringFinish (HandleRef str)\r
204                 {\r
205                         gluezilla_stringFinish (str);\r
206                 }\r
207 \r
208                 public static string StringGet (HandleRef str)\r
209                 {\r
210                         IntPtr p = gluezilla_stringGet (str);\r
211                         return Marshal.PtrToStringUni (p);\r
212                 }\r
213 \r
214                 public static void StringSet (HandleRef str, string text)\r
215                 {\r
216                         gluezilla_stringSet (str, text);\r
217                 }\r
218 \r
219 \r
220                 public static object GetProxyForObject (IWebBrowser control, Guid iid, object obj)\r
221                 {\r
222                         if (!isInitialized ())\r
223                                 return null;\r
224                         BindingInfo info = getBinding (control);\r
225                         \r
226                         IntPtr ret;\r
227                         gluezilla_getProxyForObject (info.gluezilla, iid, obj, out ret);\r
228                         \r
229                         object o = Marshal.GetObjectForIUnknown (ret);\r
230                         return o;\r
231                 }\r
232 \r
233                 public static string EvalScript (IWebBrowser control, string script)\r
234                 {\r
235                         if (!isInitialized ())\r
236                                 return null;\r
237                         BindingInfo info = getBinding (control);\r
238                         IntPtr p = gluezilla_evalScript (info.gluezilla, script);\r
239                         return Marshal.PtrToStringAuto (p);\r
240                 }\r
241
242
243                 #region pinvokes\r
244                 [DllImport("gluezilla")]\r
245                 private static extern void gluezilla_debug(int signal);\r
246 \r
247                 [DllImport("gluezilla")]\r
248                 private static extern IntPtr gluezilla_init (Platform platform, out Platform mozPlatform);\r
249 \r
250                 [DllImport ("gluezilla")]\r
251                 private static extern IntPtr gluezilla_shutdown (IntPtr instance);\r
252 \r
253                 [DllImport ("gluezilla")]\r
254                 private static extern IntPtr gluezilla_createBrowserWindow (/*IntPtr instance, */IntPtr events, IntPtr hwnd, Int32 width, Int32 height, string startDir, string dataDir, Platform platform);\r
255                 \r
256                 // layout\r
257                 [DllImport ("gluezilla")]\r
258                 private static extern int gluezilla_focus (IntPtr instance, FocusOption focus);\r
259                 [DllImport ("gluezilla")]\r
260                 private static extern int gluezilla_blur (IntPtr instance);\r
261                 [DllImport ("gluezilla")]\r
262                 private static extern int gluezilla_activate (IntPtr instance);\r
263                 [DllImport ("gluezilla")]\r
264                 private static extern int gluezilla_deactivate (IntPtr instance);\r
265                 [DllImport ("gluezilla")]\r
266                 private static extern int gluezilla_resize (IntPtr instance, Int32 width, Int32 height);\r
267 \r
268                 // navigation\r
269                 [DllImport ("gluezilla")]\r
270                 private static extern int gluezilla_home (IntPtr instance);\r
271 \r
272                 // dom\r
273                 [DllImport ("gluezilla")]\r
274                 [return:MarshalAs(UnmanagedType.Interface)]\r
275                 private static extern nsIWebNavigation gluezilla_getWebNavigation (IntPtr instance);\r
276 \r
277                 [DllImport ("gluezilla")]\r
278                 private static extern IntPtr gluezilla_stringInit ();\r
279                 [DllImport ("gluezilla")]\r
280                 private static extern int gluezilla_stringFinish (HandleRef str);\r
281                 [DllImport ("gluezilla")]\r
282                 private static extern IntPtr gluezilla_stringGet (HandleRef str);\r
283                 [DllImport ("gluezilla")]\r
284                 private static extern void gluezilla_stringSet (HandleRef str, [MarshalAs (UnmanagedType.LPWStr)] string text);\r
285 \r
286                 [DllImport ("gluezilla")]\r
287                 private static extern void gluezilla_getProxyForObject (\r
288                         IntPtr instance, \r
289                         [MarshalAs (UnmanagedType.LPStruct)] Guid iid, \r
290                         [MarshalAs (UnmanagedType.Interface)] object obj,\r
291                         out IntPtr ret);\r
292
293
294                 [DllImport ("gluezilla")]
295                 public static extern uint gluezilla_StringContainerInit (HandleRef /*nsStringContainer & */aStr);
296
297                 [DllImport ("gluezilla")]
298                 public static extern void gluezilla_StringContainerFinish (HandleRef /*nsStringContainer & */aStr);
299
300                 [DllImport ("gluezilla")]
301                 public static extern uint gluezilla_StringGetData (HandleRef /*const nsAString &*/ aStr, 
302                         out IntPtr /*const PRUnichar ** */aBuf, 
303                         out bool /*PRBool * */aTerm);
304
305                 [DllImport ("gluezilla")]
306                 public static extern uint gluezilla_StringSetData (HandleRef /*nsAString &*/ aStr, 
307                         [MarshalAs (UnmanagedType.LPWStr)] string /*const PRUnichar * */ aBuf, uint aCount);
308
309                 [DllImport ("gluezilla")]
310                 public static extern uint gluezilla_CStringContainerInit (HandleRef /*nsCStringContainer &*/ aStr);
311
312                 [DllImport ("gluezilla")]
313                 public static extern void gluezilla_CStringContainerFinish (HandleRef /*nsCStringContainer &*/ aStr);
314
315                 [DllImport ("gluezilla")]
316                 public static extern uint gluezilla_CStringGetData (HandleRef /*const nsACString &*/ aStr, 
317                         out IntPtr /*const PRUnichar ** */aBuf, 
318                         out bool /*PRBool **/ aTerm);
319
320                 [DllImport ("gluezilla")]
321                 public static extern uint gluezilla_CStringSetData (HandleRef /*nsACString &*/ aStr, 
322                         string aBuf, 
323                         uint aCount);
324
325                 [DllImport ("gluezilla")]\r
326                 [return: MarshalAs (UnmanagedType.Interface)]
327                 public static extern nsIServiceManager  gluezilla_getServiceManager ();\r
328 \r
329                 [DllImport ("gluezilla")]\r
330                 private static extern IntPtr gluezilla_evalScript (IntPtr instance, string script);
331 \r
332                 #endregion\r
333         }\r
334 }\r