2009-11-11 Chris Toshok <toshok@ximian.com>
[moon.git] / gtk / Moonlight.Gtk / MoonlightHost.cs
blobcb3244d25f830128eebaa764f4e262b0125dae1f
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, 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)System.Windows.Application.Current.RootVisual; }
89 set {
90 Deployment.Current.InitializeDeployment (null, null);
92 System.Windows.Application.Current.RootVisual = value;
96 /// <summary>
97 /// Helper property to get the current Application controlling the current Content
98 /// </summary>
99 /// <remarks>
100 /// This property simply proxies the value of System.Windows.Application.Current
101 /// </remarks>
102 public System.Windows.Application Application {
103 get { return System.Windows.Application.Current; }
106 /// <summary>
107 /// The transparent state for the widget. Used to drive
108 /// the compositing of unpainted regions against the
109 /// background.
110 /// </summary>
111 /// <remarks>
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.
115 /// </remarks>
116 public bool Transparent {
117 get { return NativeMethods.moon_window_get_transparent (window); }
118 set { NativeMethods.moon_window_set_transparent (window, value); }
121 /// <summary>
122 /// Initializes the Surface widget from the XAML contents in a string
123 /// </summary>
124 /// <param name="xaml">The contents of the string.</param>
125 /// <remarks>
126 /// This uses the XAML parser to load the given string and
127 /// display it on the Surface widget.
128 /// </remarks>
129 public void LoadXaml (string xaml)
131 if (xaml == null) {
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;
146 /// <summary>
147 /// Initializes the GtkSilver widget from the XAML contents in a file
148 /// </summary>
149 /// <param name="file">The name of a file in your file system.</param>
150 /// <remarks>
151 /// This uses the XAML parser to load the given file and
152 /// display it on the Surface widget.
153 /// </remarks>
154 public void LoadXamlFromFile (string file)
156 if (file == null) {
157 throw new ArgumentNullException ("file");
160 LoadXaml (System.IO.File.ReadAllText (file));
163 /// <summary>
164 /// Initializes the Surface widget from a XAP file
165 /// </summary>
166 /// <param name="xapPath">Path to the XAP file</param>
167 /// <remarks>
168 /// This uses the XAP loader to load the given XAP
169 /// display it on the Surface widget.
170 /// </remarks>
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.
182 /// <summary>
183 /// Loads XAML within the context of the current GtkSilver widget
184 /// </summary>
185 /// <param name="xaml">The contents of the string.</param>
186 /// <param name="createNamescope"></param>
187 public DependencyObject CreateElementFromString (string xaml, bool createNamescope)
189 if (xaml == null) {
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;
200 #endregion
202 private void OnSizeAllocated (object o, SizeAllocatedArgs args)
204 NativeMethods.surface_resize (surface, Allocation.Width, Allocation.Height);