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
, 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)System.Windows.Application.Current.RootVisual; }
90 Deployment
.Current
.InitializeDeployment (null, null);
92 System
.Windows
.Application
.Current
.RootVisual
= value;
97 /// Helper property to get the current Application controlling the current Content
100 /// This property simply proxies the value of System.Windows.Application.Current
102 public System
.Windows
.Application Application
{
103 get { return System.Windows.Application.Current; }
107 /// The transparent state for the widget. Used to drive
108 /// the compositing of unpainted regions against the
112 /// By default the value is false which will produce a
113 /// solid white background, otherwise the background is
114 /// cleared with black and composited with the background.
116 public bool Transparent
{
117 get { return NativeMethods.moon_window_get_transparent (window); }
118 set { NativeMethods.moon_window_set_transparent (window, value); }
122 /// Initializes the Surface widget from the XAML contents in a string
124 /// <param name="xaml">The contents of the string.</param>
126 /// This uses the XAML parser to load the given string and
127 /// display it on the Surface widget.
129 public void LoadXaml (string xaml
)
132 throw new ArgumentNullException ("xaml");
135 Deployment
.Current
.InitializeDeployment (null, null);
137 DependencyObject toplevel
= CreateElementFromString (xaml
, true);
139 if (toplevel
== null) {
140 throw new ArgumentException ("xaml");
143 Content
= (FrameworkElement
)toplevel
;
147 /// Initializes the GtkSilver widget from the XAML contents in a file
149 /// <param name="file">The name of a file in your file system.</param>
151 /// This uses the XAML parser to load the given file and
152 /// display it on the Surface widget.
154 public void LoadXamlFromFile (string file
)
157 throw new ArgumentNullException ("file");
160 LoadXaml (System
.IO
.File
.ReadAllText (file
));
164 /// Initializes the Surface widget from a XAP file
166 /// <param name="xapPath">Path to the XAP file</param>
168 /// This uses the XAP loader to load the given XAP
169 /// display it on the Surface widget.
171 public void LoadXap (string xapPath
)
173 if (xapPath
== null) {
174 throw new ArgumentNullException ("xapPath");
177 Deployment
.Current
.InitializeDeployment (IntPtr
.Zero
, xapPath
, null, null);
179 // we don't Attach() here because CreateFromXap has already done that for us.
183 /// Loads XAML within the context of the current GtkSilver widget
185 /// <param name="xaml">The contents of the string.</param>
186 /// <param name="createNamescope"></param>
187 public DependencyObject
CreateElementFromString (string xaml
, bool createNamescope
)
190 throw new ArgumentNullException ("xaml");
193 XamlLoader
.AllowMultipleSurfacesPerDomain
= true;
195 return (DependencyObject
)XamlLoader
196 .CreateManagedXamlLoader (null, surface
, IntPtr
.Zero
)
197 .CreateObjectFromString (xaml
, true) as DependencyObject
;
202 private void OnSizeAllocated (object o
, SizeAllocatedArgs args
)
204 NativeMethods
.surface_resize (surface
, Allocation
.Width
, Allocation
.Height
);