~ubuntu-branches/ubuntu/karmic/moon/karmic

« back to all changes in this revision

Viewing changes to gtk/gtk.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// gtk.cs
 
3
//
 
4
// Contact:
 
5
//   Moonlight List (moonlight-list@lists.ximian.com)
 
6
//
 
7
// Copyright 2007 Novell, Inc.
 
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
//
 
28
 
 
29
using System;
 
30
using System.Runtime.InteropServices;
 
31
using Gtk;
 
32
using System.Windows;
 
33
using System.Windows.Controls;
 
34
using Mono;
 
35
using Mono.Xaml;
 
36
using System.Reflection;
 
37
 
 
38
namespace Gtk.Moonlight {
 
39
        
 
40
        /// <summary>
 
41
        ///    A Gtk# widget that can be used to embed
 
42
        ///    Moonlight/Silverlight(tm) content in a Gtk application
 
43
        /// </summary>
 
44
        /// <remarks>
 
45
        ///    See the namespace documentation for a sample on how to
 
46
        ///    use this widget with your Gtk# code.
 
47
        /// </remarks>
 
48
public class GtkSilver : EventBox {
 
49
        IntPtr surface;
 
50
        IntPtr window;
 
51
 
 
52
        //
 
53
        // The downloader callbacks
 
54
        //
 
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);
 
60
        
 
61
        [DllImport ("moon")]
 
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);
 
68
        
 
69
        static GtkSilver ()
 
70
        {
 
71
                NativeMethods.runtime_init_desktop ();
 
72
                downloader_set_functions (
 
73
                        ManagedDownloader.CreateDownloader,
 
74
                        ManagedDownloader.DestroyDownloader,
 
75
                        ManagedDownloader.Open,
 
76
                        ManagedDownloader.Send,
 
77
                        ManagedDownloader.Abort);
 
78
                
 
79
                DependencyObject.Initialize ();
 
80
        }
 
81
 
 
82
        /// <summary>
 
83
        ///    Initializes the GtkSilver widget, this must be called
 
84
        ///    before any calls are done to System.Windows.
 
85
        /// </summary>
 
86
        /// <remarks>
 
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>
 
95
        ///
 
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>
 
100
        /// </remarks>
 
101
        static public void Init ()
 
102
        {
 
103
                // Just to execute the constructor.
 
104
        }
 
105
        
 
106
        /// <summary>
 
107
        ///    Public constructor, creates a widget with the specified
 
108
        ///    width and height
 
109
        /// </summary>
 
110
        /// <param name="width">The initial width for the widget</param>
 
111
        /// <param name="height">The initial height for the widget</param>
 
112
        /// <remarks>
 
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>
 
117
        ///
 
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>
 
123
        /// </remarks>
 
124
        public GtkSilver (int width, int height)
 
125
        {
 
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);
 
130
        }
 
131
 
 
132
        public GtkSilver () : this (100, 100)
 
133
        {
 
134
        }
 
135
        
 
136
        /// <summary>
 
137
        ///    The transparent state for the widget.  Used to drive
 
138
        ///    the compositing of unpainted regions against the
 
139
        ///    background.
 
140
        /// </summary>
 
141
        /// <remarks>
 
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.
 
145
        /// </remarks>
 
146
        public bool Transparent {
 
147
                get {
 
148
                        return NativeMethods.moon_window_get_transparent (window);
 
149
                }
 
150
 
 
151
                set {
 
152
                        NativeMethods.moon_window_set_transparent (window, value);
 
153
                }
 
154
        }
 
155
        
 
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)
 
158
        {
 
159
                NativeMethods.surface_paint (surface, ctx, area.X, area.Y, area.Width, area.Height);
 
160
        }
 
161
 
 
162
        FrameworkElement content;
 
163
 
 
164
        /// <summary>
 
165
        ///    Makes the specified System.Windows.FrameworkElement
 
166
        ///    the content to be displayed on this widget
 
167
        /// </summary>
 
168
        /// <param name="toplevel">The System.Windows.FrameworkElement to attach.</param>
 
169
        /// <remarks>
 
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.
 
174
        /// </remarks>
 
175
        public void Attach (FrameworkElement toplevel)
 
176
        {
 
177
                if (toplevel == null)
 
178
                        throw new ArgumentNullException ("toplevel");
 
179
 
 
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 });
 
184
                content = toplevel;
 
185
        }
 
186
        
 
187
        /// <summary>
 
188
        ///    The currently attached FrameworkElement.
 
189
        /// </summary>
 
190
        /// <remarks>
 
191
        ///   This returns the instance of the currently attached FrameworkElement.
 
192
        /// </remarks>
 
193
        public FrameworkElement Content {
 
194
                get {
 
195
                        return content;
 
196
                }
 
197
        }
 
198
 
 
199
        /// <summary>
 
200
        ///    Resizes the surface to the specified size.
 
201
        /// </summary>
 
202
        /// <param name="width">The new width.</param>
 
203
        /// <param name="height">The new height.</param>
 
204
        public void Resize (int width, int height)
 
205
        {
 
206
                NativeMethods.surface_resize (surface, width, height);
 
207
        }
 
208
 
 
209
        /// <summary>
 
210
        ///    Initializes the GtkSilver widget from the XAML contents
 
211
        ///    in a string
 
212
        /// </summary>
 
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>
 
215
        /// <remarks>
 
216
        ///   This uses the XAML parser to load the given string and
 
217
        ///   display it on the GtkSilver widget.
 
218
        /// </remarks>
 
219
        public bool LoadXaml (string xaml, out FrameworkElement toplevel)
 
220
        {
 
221
                toplevel = null;
 
222
 
 
223
                if (xaml == null)
 
224
                        throw new ArgumentNullException ("xaml");
 
225
 
 
226
                DependencyObject top = CreateFromString (xaml, true);
 
227
 
 
228
                if (top == null)
 
229
                        return false;
 
230
 
 
231
                toplevel = top as FrameworkElement;
 
232
                if (toplevel == null)
 
233
                        return false;
 
234
        
 
235
                Attach (toplevel);
 
236
 
 
237
                return true;
 
238
        }
 
239
 
 
240
        public bool LoadXap (string xap, out System.Windows.Application application)
 
241
        {
 
242
                application = null;
 
243
 
 
244
                if (xap == null)
 
245
                        throw new ArgumentNullException ("xaml");
 
246
 
 
247
                application = System.Windows.Application.CreateFromXap (IntPtr.Zero, surface, xap);
 
248
 
 
249
                Console.WriteLine ("application = {0}", application);
 
250
                Console.WriteLine ("application.RootVisual = {0}", application.RootVisual);
 
251
 
 
252
                // we don't Attach() here because CreateFromXap has already done that for us.
 
253
                return true;
 
254
        }
 
255
 
 
256
        /// <summary>
 
257
        ///    Initializes the GtkSilver widget from the XAML contents
 
258
        ///    in a file
 
259
        /// </summary>
 
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>
 
262
        /// <remarks>
 
263
        ///   This uses the XAML parser to load the given file and
 
264
        ///   display it on the GtkSilver widget.
 
265
        /// </remarks>
 
266
        public bool LoadFile (string file, out FrameworkElement top)
 
267
        {
 
268
                if (file == null)
 
269
                        throw new ArgumentNullException ("file");
 
270
 
 
271
                return LoadXaml (System.IO.File.ReadAllText (file), out top);
 
272
        }
 
273
 
 
274
        /// <summary>
 
275
        ///    Initializes the GtkSilver widget from the XAML contents
 
276
        ///    in a file
 
277
        /// </summary>
 
278
        /// <param name="file">The name of a file in your file system.</param>
 
279
        /// <remarks>
 
280
        ///   This uses the XAML parser to load the given file and
 
281
        ///   display it on the GtkSilver widget.
 
282
        /// </remarks>
 
283
        public bool LoadFile (string file)
 
284
        {
 
285
                FrameworkElement unused;
 
286
                return LoadFile (file, out unused);
 
287
        }
 
288
 
 
289
        /// <summary>
 
290
        ///    Loads xaml within the context of the current GtkSilver
 
291
        ///    widget
 
292
        /// </summary>
 
293
        /// <param name="xaml">The contents of the string.</param>
 
294
        /// <param name="createNamescope"></param>
 
295
        public DependencyObject CreateFromString (string xaml, bool createNamescope)
 
296
        {
 
297
                object result;
 
298
                XamlLoader loader;
 
299
 
 
300
                if (xaml == null)
 
301
                        throw new ArgumentNullException ("xaml");
 
302
 
 
303
                XamlLoader.AllowMultipleSurfacesPerDomain = true;
 
304
 
 
305
                loader = XamlLoader.CreateManagedXamlLoader (surface, IntPtr.Zero);
 
306
                result = loader.CreateDependencyObjectFromString (xaml, true);
 
307
 
 
308
                return result as DependencyObject;
 
309
        }
 
310
}
 
311
}
 
312
 
 
313