5 // Aaron Bockover <abockover@novell.com>
7 // Copyright 2009 Aaron Bockover
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
;
36 namespace Moonlight
.Gtk
39 /// A Gtk# widget that can be used to embed
40 /// Moonlight/Silverlight(tm) content in a Gtk application
43 /// See the namespace documentation for a sample on how to
44 /// use this widget with your Gtk# code.
46 public class MoonlightHost
: EventBox
48 private IntPtr surface
;
49 private IntPtr window
;
52 /// Public constructor
55 /// <para>The size of the internal root canvas is determined by
56 // the size of the Surface widget, which can later be changed
57 /// by using the standard Gtk# APIs (SizeAllocate).</para>
59 /// <para>The widget is initially empty, you must set the
60 /// <see cref="Content"/> method with a
61 /// System.Windows.FrameworkElement instance (you can
62 /// create those programatically, or use
63 /// <see cref="LoadXaml(System.String)"/>,
64 /// <see cref="LoadXamlFromFile(System.String)"/>, or
65 /// <see cref="LoadXap(System.String)"/>).</para>
67 public MoonlightHost ()
69 Mono
.Xaml
.XamlLoader
.AllowMultipleSurfacesPerDomain
= true;
70 window
= NativeMethods
.moon_window_gtk_new (false, 0, 0, IntPtr
.Zero
);
71 surface
= NativeMethods
.surface_new (window
);
72 Raw
= NativeMethods
.moon_window_gtk_get_native_widget (window
);
74 SizeAllocated
+= OnSizeAllocated
;
80 /// Gets or sets the current top level FrameworkElement
83 /// When setting, this will make the instance of canvas be the content
84 /// displayed by the widget. Using this setter with a new canvas replaces
85 /// the currently set content with the new content.
87 public FrameworkElement Content
{
88 get { return (FrameworkElement
)NativeDependencyObjectHelper
.FromIntPtr (
89 NativeMethods
.surface_get_toplevel (surface
)); }
90 set { NativeMethods.surface_attach (surface, value == null ? IntPtr.Zero : value.native); }
94 /// Helper property to get the current Application controlling the current Content
97 /// This property simply proxies the value of System.Windows.Application.Current
99 public System
.Windows
.Application Application
{
100 get { return System.Windows.Application.Current; }
104 /// The transparent state for the widget. Used to drive
105 /// the compositing of unpainted regions against the
109 /// By default the value is false which will produce a
110 /// solid white background, otherwise the background is
111 /// cleared with black and composited with the background.
113 public bool Transparent
{
114 get { return NativeMethods.moon_window_get_transparent (window); }
115 set { NativeMethods.moon_window_set_transparent (window, value); }
119 /// Initializes the Surface widget from the XAML contents in a string
121 /// <param name="xaml">The contents of the string.</param>
123 /// This uses the XAML parser to load the given string and
124 /// display it on the Surface widget.
126 public void LoadXaml (string xaml
)
129 throw new ArgumentNullException ("xaml");
132 Deployment
.Current
.InitializeDeployment (null, null);
134 DependencyObject toplevel
= CreateElementFromString (xaml
, true);
136 if (toplevel
== null) {
137 throw new ArgumentException ("xaml");
140 Content
= (FrameworkElement
)toplevel
;
144 /// Initializes the GtkSilver widget from the XAML contents in a file
146 /// <param name="file">The name of a file in your file system.</param>
148 /// This uses the XAML parser to load the given file and
149 /// display it on the Surface widget.
151 public void LoadXamlFromFile (string file
)
154 throw new ArgumentNullException ("file");
157 LoadXaml (System
.IO
.File
.ReadAllText (file
));
161 /// Initializes the Surface widget from a XAP file
163 /// <param name="xapPath">Path to the XAP file</param>
165 /// This uses the XAP loader to load the given XAP
166 /// display it on the Surface widget.
168 public void LoadXap (string xapPath
)
170 if (xapPath
== null) {
171 throw new ArgumentNullException ("xapPath");
174 Deployment
.Current
.InitializeDeployment (IntPtr
.Zero
, xapPath
, null, null);
176 // we don't Attach() here because CreateFromXap has already done that for us.
180 /// Loads XAML within the context of the current GtkSilver widget
182 /// <param name="xaml">The contents of the string.</param>
183 /// <param name="createNamescope"></param>
184 public DependencyObject
CreateElementFromString (string xaml
, bool createNamescope
)
187 throw new ArgumentNullException ("xaml");
190 XamlLoader
.AllowMultipleSurfacesPerDomain
= true;
192 return (DependencyObject
)XamlLoader
193 .CreateManagedXamlLoader (null, surface
, IntPtr
.Zero
)
194 .CreateObjectFromString (xaml
, true) as DependencyObject
;
199 private void OnSizeAllocated (object o
, SizeAllocatedArgs args
)
201 NativeMethods
.surface_resize (surface
, Allocation
.Width
, Allocation
.Height
);