in plugin/:
[moon.git] / gtk / Moonlight.Gtk / MoonlightHost.cs
blob3137b11eb23441b236138a41c1174191afcc1536
1 //
2 // MoonlightHost.cs
3 //
4 // Author:
5 // Aaron Bockover <abockover@novell.com>
6 //
7 // Copyright 2009 Aaron Bockover
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
29 using System;
30 using System.Runtime.InteropServices;
31 using System.Windows;
32 using Mono;
33 using Mono.Xaml;
34 using Gtk;
36 namespace Moonlight.Gtk
38 /// <summary>
39 /// A Gtk# widget that can be used to embed
40 /// Moonlight/Silverlight(tm) content in a Gtk application
41 /// </summary>
42 /// <remarks>
43 /// See the namespace documentation for a sample on how to
44 /// use this widget with your Gtk# code.
45 /// </remarks>
46 public class MoonlightHost : EventBox
48 private IntPtr surface;
49 private IntPtr window;
51 /// <summary>
52 /// Public constructor
53 /// </summary>
54 /// <remarks>
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>
58 ///
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>
66 /// </remarks>
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;
77 #region Public API
79 /// <summary>
80 /// Gets or sets the current top level FrameworkElement
81 /// </summary>
82 /// <remarks>
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.
86 /// </remarks>
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); }
93 /// <summary>
94 /// Helper property to get the current Application controlling the current Content
95 /// </summary>
96 /// <remarks>
97 /// This property simply proxies the value of System.Windows.Application.Current
98 /// </remarks>
99 public System.Windows.Application Application {
100 get { return System.Windows.Application.Current; }
103 /// <summary>
104 /// The transparent state for the widget. Used to drive
105 /// the compositing of unpainted regions against the
106 /// background.
107 /// </summary>
108 /// <remarks>
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.
112 /// </remarks>
113 public bool Transparent {
114 get { return NativeMethods.moon_window_get_transparent (window); }
115 set { NativeMethods.moon_window_set_transparent (window, value); }
118 /// <summary>
119 /// Initializes the Surface widget from the XAML contents in a string
120 /// </summary>
121 /// <param name="xaml">The contents of the string.</param>
122 /// <remarks>
123 /// This uses the XAML parser to load the given string and
124 /// display it on the Surface widget.
125 /// </remarks>
126 public void LoadXaml (string xaml)
128 if (xaml == null) {
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;
143 /// <summary>
144 /// Initializes the GtkSilver widget from the XAML contents in a file
145 /// </summary>
146 /// <param name="file">The name of a file in your file system.</param>
147 /// <remarks>
148 /// This uses the XAML parser to load the given file and
149 /// display it on the Surface widget.
150 /// </remarks>
151 public void LoadXamlFromFile (string file)
153 if (file == null) {
154 throw new ArgumentNullException ("file");
157 LoadXaml (System.IO.File.ReadAllText (file));
160 /// <summary>
161 /// Initializes the Surface widget from a XAP file
162 /// </summary>
163 /// <param name="xapPath">Path to the XAP file</param>
164 /// <remarks>
165 /// This uses the XAP loader to load the given XAP
166 /// display it on the Surface widget.
167 /// </remarks>
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.
179 /// <summary>
180 /// Loads XAML within the context of the current GtkSilver widget
181 /// </summary>
182 /// <param name="xaml">The contents of the string.</param>
183 /// <param name="createNamescope"></param>
184 public DependencyObject CreateElementFromString (string xaml, bool createNamescope)
186 if (xaml == null) {
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;
197 #endregion
199 private void OnSizeAllocated (object o, SizeAllocatedArgs args)
201 NativeMethods.surface_resize (surface, Allocation.Width, Allocation.Height);