5
// Moonlight List (moonlight-list@lists.ximian.com)
7
// Copyright 2007 Novell, Inc.
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
using System.Runtime.InteropServices;
33
using System.Windows.Controls;
36
using System.Reflection;
38
namespace Gtk.Moonlight {
41
/// A Gtk# widget that can be used to embed
42
/// Moonlight/Silverlight(tm) content in a Gtk application
45
/// See the namespace documentation for a sample on how to
46
/// use this widget with your Gtk# code.
48
public class GtkSilver : EventBox {
53
// The downloader callbacks
55
internal delegate IntPtr downloader_create_state_func (IntPtr native);
56
internal delegate void downloader_destroy_state_func (IntPtr state);
57
internal delegate void downloader_open_func (string verb, string uri, bool streaming, IntPtr state);
58
internal delegate void downloader_send_func (IntPtr state);
59
internal delegate void downloader_abort_func (IntPtr state);
62
internal extern static void downloader_set_functions (
63
downloader_create_state_func create_state,
64
downloader_destroy_state_func destroy_state,
65
downloader_open_func open,
66
downloader_send_func send,
67
downloader_abort_func abort);
71
NativeMethods.runtime_init_desktop ();
72
downloader_set_functions (
73
ManagedDownloader.CreateDownloader,
74
ManagedDownloader.DestroyDownloader,
75
ManagedDownloader.Open,
76
ManagedDownloader.Send,
77
ManagedDownloader.Abort);
79
DependencyObject.Initialize ();
83
/// Initializes the GtkSilver widget, this must be called
84
/// before any calls are done to System.Windows.
87
/// <para>The System.Windows namespace for Silverlight
88
/// requires a downloader engine to be registered before it
89
/// can be used to satisfy assembly dependencies and
90
/// images. If your application will for some reason call
91
/// into System.Windows before they create an instance of
92
/// GtkSilver, they should call this method to ensure that
93
/// the proper downloader has been registered with the
94
/// agclr runtime.</para>
96
/// <para>Failure to call this method typically result in
97
/// errors from the XAML parsing code when it tries to
98
/// resolve assembly references, external classes or
99
/// loading of external media.</para>
101
static public void Init ()
103
// Just to execute the constructor.
107
/// Public constructor, creates a widget with the specified
110
/// <param name="width">The initial width for the widget</param>
111
/// <param name="height">The initial height for the widget</param>
113
/// <para>The initial width and height of the GtkSilver
114
/// control are given by the parameters. The size of the
115
/// widget can later be changed by using the standard Gtk#
116
/// APIs (SizeAllocate).</para>
118
/// <para>The widget is initially empty, you must call the
119
/// <see cref="Attach"/> method with a
120
/// System.Windows.FrameworkElement instance (you can
121
/// create those programatically, using XAML, or using the
122
/// <see cref="LoadFile(System.String)"/> method).</para>
124
public GtkSilver (int width, int height)
126
Mono.Xaml.XamlLoader.AllowMultipleSurfacesPerDomain = true;
127
window = NativeMethods.moon_window_gtk_new (false, width, height, IntPtr.Zero);
128
surface = NativeMethods.surface_new (window, true);
129
Raw = NativeMethods.moon_window_gtk_get_widget (window);
132
public GtkSilver () : this (100, 100)
137
/// The transparent state for the widget. Used to drive
138
/// the compositing of unpainted regions against the
142
/// By default the value is false which will produce a
143
/// solid white background, otherwise the background is
144
/// cleared with black and composited with the background.
146
public bool Transparent {
148
return NativeMethods.moon_window_get_transparent (window);
152
NativeMethods.moon_window_set_transparent (window, value);
156
// This is a quick hack for f-spot code it will be cleaned up soon
157
public void Print (IntPtr ctx, Gdk.Rectangle area)
159
NativeMethods.surface_paint (surface, ctx, area.X, area.Y, area.Width, area.Height);
162
FrameworkElement content;
165
/// Makes the specified System.Windows.FrameworkElement
166
/// the content to be displayed on this widget
168
/// <param name="toplevel">The System.Windows.FrameworkElement to attach.</param>
170
/// This will make the instance of canvas be the content
171
/// displayed by the widget. Calling this method with a
172
/// new canvas replaces the currently attached toplevel
173
/// with the new one.
175
public void Attach (FrameworkElement toplevel)
177
if (toplevel == null)
178
throw new ArgumentNullException ("toplevel");
180
// Dynamically invoke our get-the-handle-code
181
MethodInfo m = typeof (Canvas).Assembly.GetType ("Mono.Hosting").
182
GetMethod ("SurfaceAttach", BindingFlags.Static | BindingFlags.NonPublic);
183
m.Invoke (null, new object [] { surface, toplevel });
188
/// The currently attached FrameworkElement.
191
/// This returns the instance of the currently attached FrameworkElement.
193
public FrameworkElement Content {
200
/// Resizes the surface to the specified size.
202
/// <param name="width">The new width.</param>
203
/// <param name="height">The new height.</param>
204
public void Resize (int width, int height)
206
NativeMethods.surface_resize (surface, width, height);
210
/// Initializes the GtkSilver widget from the XAML contents
213
/// <param name="xaml">The contents of the string.</param>
214
/// <param name="toplevel">The created FrameworkElement, if the creation of the xaml string was successful.</param>
216
/// This uses the XAML parser to load the given string and
217
/// display it on the GtkSilver widget.
219
public bool LoadXaml (string xaml, out FrameworkElement toplevel)
224
throw new ArgumentNullException ("xaml");
226
DependencyObject top = CreateFromString (xaml, true);
231
toplevel = top as FrameworkElement;
232
if (toplevel == null)
240
public bool LoadXap (string xap, out System.Windows.Application application)
245
throw new ArgumentNullException ("xaml");
247
application = System.Windows.Application.CreateFromXap (IntPtr.Zero, surface, xap);
249
Console.WriteLine ("application = {0}", application);
250
Console.WriteLine ("application.RootVisual = {0}", application.RootVisual);
252
// we don't Attach() here because CreateFromXap has already done that for us.
257
/// Initializes the GtkSilver widget from the XAML contents
260
/// <param name="file">The name of a file in your file system.</param>
261
/// <param name="top">The created FrameworkElement, if the creation of the xaml string was successful.</param>
263
/// This uses the XAML parser to load the given file and
264
/// display it on the GtkSilver widget.
266
public bool LoadFile (string file, out FrameworkElement top)
269
throw new ArgumentNullException ("file");
271
return LoadXaml (System.IO.File.ReadAllText (file), out top);
275
/// Initializes the GtkSilver widget from the XAML contents
278
/// <param name="file">The name of a file in your file system.</param>
280
/// This uses the XAML parser to load the given file and
281
/// display it on the GtkSilver widget.
283
public bool LoadFile (string file)
285
FrameworkElement unused;
286
return LoadFile (file, out unused);
290
/// Loads xaml within the context of the current GtkSilver
293
/// <param name="xaml">The contents of the string.</param>
294
/// <param name="createNamescope"></param>
295
public DependencyObject CreateFromString (string xaml, bool createNamescope)
301
throw new ArgumentNullException ("xaml");
303
XamlLoader.AllowMultipleSurfacesPerDomain = true;
305
loader = XamlLoader.CreateManagedXamlLoader (surface, IntPtr.Zero);
306
result = loader.CreateDependencyObjectFromString (xaml, true);
308
return result as DependencyObject;