~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt/Application.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// Application.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Reflection;
 
29
using Xwt.Backends;
 
30
using Xwt.Engine;
 
31
using System.Threading.Tasks;
 
32
 
 
33
namespace Xwt
 
34
{
 
35
        public static class Application
 
36
        {
 
37
                static readonly TaskScheduler taskScheduler = new XwtTaskScheduler ();
 
38
 
 
39
                public static TaskScheduler UITaskScheduler {
 
40
                        get { return taskScheduler; }
 
41
                }
 
42
 
 
43
                internal static System.Threading.Thread UIThread {
 
44
                        get;
 
45
                        private set;
 
46
                }
 
47
 
 
48
                static EngineBackend engine;
 
49
                
 
50
                internal static EngineBackend EngineBackend {
 
51
                        get { return engine; }
 
52
                }
 
53
                
 
54
                public static void Initialize ()
 
55
                {
 
56
                        if (engine != null)
 
57
                                return;
 
58
                        Initialize (null);
 
59
                }
 
60
                
 
61
                public static void Initialize (ToolkitType type)
 
62
                {
 
63
                        Initialize (GetBackendType (type));
 
64
                }
 
65
 
 
66
                public static void Initialize (string backendType)
 
67
                {
 
68
                        InitBackend (backendType);
 
69
                        engine.InitializeApplication ();
 
70
                        UIThread = System.Threading.Thread.CurrentThread;
 
71
                }
 
72
                
 
73
                public static void Run ()
 
74
                {
 
75
                        Toolkit.InvokePlatformCode (delegate {
 
76
                                engine.RunApplication ();
 
77
                        });
 
78
                }
 
79
                
 
80
                public static void Exit ()
 
81
                {
 
82
                        Toolkit.InvokePlatformCode (delegate {
 
83
                                engine.ExitApplication ();
 
84
                        });
 
85
                }
 
86
                        /// <summary>
 
87
                /// Invokes an action in the GUI thread
 
88
                /// </summary>
 
89
                /// <param name='action'>
 
90
                /// The action to execute.
 
91
                /// </param>
 
92
                public static void Invoke (Action action)
 
93
                {
 
94
                        engine.InvokeAsync (delegate {
 
95
                                try {
 
96
                                        Toolkit.EnterUserCode ();
 
97
                                        action ();
 
98
                                        Toolkit.ExitUserCode (null);
 
99
                                } catch (Exception ex) {
 
100
                                        Toolkit.ExitUserCode (ex);
 
101
                                }
 
102
                        });
 
103
                }
 
104
                
 
105
                /// <summary>
 
106
                /// Invokes an action in the GUI thread after the provided time span
 
107
                /// </summary>
 
108
                /// <returns>
 
109
                /// A timer object
 
110
                /// </returns>
 
111
                /// <param name='action'>
 
112
                /// The action to execute.
 
113
                /// </param>
 
114
                /// <remarks>
 
115
                /// This method schedules the execution of the provided function. The function
 
116
                /// must return 'true' if it has to be executed again after the time span, or 'false'
 
117
                /// if the timer can be discarded.
 
118
                /// The execution of the funciton can be canceled by disposing the returned object.
 
119
                /// </remarks>
 
120
                public static IDisposable TimeoutInvoke (int ms, Func<bool> action)
 
121
                {
 
122
                        return TimeoutInvoke (TimeSpan.FromMilliseconds (ms), action);
 
123
                }
 
124
                
 
125
                /// <summary>
 
126
                /// Invokes an action in the GUI thread after the provided time span
 
127
                /// </summary>
 
128
                /// <returns>
 
129
                /// A timer object
 
130
                /// </returns>
 
131
                /// <param name='action'>
 
132
                /// The action to execute.
 
133
                /// </param>
 
134
                /// <remarks>
 
135
                /// This method schedules the execution of the provided function. The function
 
136
                /// must return 'true' if it has to be executed again after the time span, or 'false'
 
137
                /// if the timer can be discarded.
 
138
                /// The execution of the funciton can be canceled by disposing the returned object.
 
139
                /// </remarks>
 
140
                public static IDisposable TimeoutInvoke (TimeSpan timeSpan, Func<bool> action)
 
141
                {
 
142
                        Timer t = new Timer ();
 
143
                        t.Id = engine.TimerInvoke (delegate {
 
144
                                bool res = false;
 
145
                                try {
 
146
                                        Toolkit.EnterUserCode ();
 
147
                                        res = action ();
 
148
                                        Toolkit.ExitUserCode (null);
 
149
                                } catch (Exception ex) {
 
150
                                        Toolkit.ExitUserCode (ex);
 
151
                                }
 
152
                                return res;
 
153
                        }, timeSpan);
 
154
                        return t;
 
155
                }
 
156
 
 
157
                public static void DispatchPendingEvents ()
 
158
                {
 
159
                        try {
 
160
                                Toolkit.ExitUserCode (null);
 
161
                                engine.DispatchPendingEvents ();
 
162
                        } finally {
 
163
                                Toolkit.EnterUserCode ();
 
164
                        }
 
165
                }
 
166
                
 
167
                public static StatusIcon CreateStatusIcon ()
 
168
                {
 
169
                        return new StatusIcon ();
 
170
                }
 
171
 
 
172
                public static event EventHandler<ExceptionEventArgs> UnhandledException;
 
173
                
 
174
                class Timer: IDisposable
 
175
                {
 
176
                        public object Id;
 
177
                        public void Dispose ()
 
178
                        {
 
179
                                Application.engine.CancelTimerInvoke (Id);
 
180
                        }
 
181
                }
 
182
                
 
183
                static bool LoadBackend (string type)
 
184
                {
 
185
                        int i = type.IndexOf (',');
 
186
                        string assembly = type.Substring (i+1).Trim ();
 
187
                        type = type.Substring (0, i).Trim ();
 
188
                        try {
 
189
                                Assembly asm = Assembly.Load (assembly);
 
190
                                if (asm != null) {
 
191
                                        Type t = asm.GetType (type);
 
192
                                        if (t != null) {
 
193
                                                engine = (EngineBackend) Activator.CreateInstance (t);
 
194
                                                return true;
 
195
                                        }
 
196
                                }
 
197
                        }
 
198
                        catch (Exception ex) {
 
199
                                Console.WriteLine (ex);
 
200
                        }
 
201
                        return false;
 
202
                }
 
203
                
 
204
                static void InitBackend (string type)
 
205
                {
 
206
                        Toolkit.EnterUserCode ();
 
207
                        if (type != null && LoadBackend (type))
 
208
                                return;
 
209
                        
 
210
                        throw new InvalidOperationException ("Xwt engine not found");
 
211
                }
 
212
 
 
213
                internal static string GetBackendType (ToolkitType type)
 
214
                {
 
215
                        string version = typeof(Application).Assembly.GetName ().Version.ToString ();
 
216
                        
 
217
                        switch (type) {
 
218
                        case ToolkitType.Gtk:
 
219
                                return "Xwt.GtkBackend.GtkEngine, Xwt.Gtk, Version=" + version;
 
220
                        case ToolkitType.Cocoa:
 
221
                                return "Xwt.Mac.MacEngine, Xwt.Mac, Version=" + version;
 
222
                        case ToolkitType.Wpf:
 
223
                                return "Xwt.WPFBackend.WPFEngine, Xwt.WPF, Version=" + version;
 
224
                        default:
 
225
                                throw new ArgumentException ("Invalid toolkit type");
 
226
                        }
 
227
                }
 
228
 
 
229
                internal static void NotifyException (Exception ex)
 
230
                {
 
231
                        var unhandledException = UnhandledException;
 
232
                        if (unhandledException != null)
 
233
                        {
 
234
                                unhandledException (null, new ExceptionEventArgs (ex));
 
235
                        }
 
236
                        else
 
237
                        {
 
238
                                Console.WriteLine (ex);
 
239
                        }
 
240
                }
 
241
        }
 
242
 
 
243
        public enum ToolkitType
 
244
        {
 
245
                Gtk,
 
246
                Cocoa,
 
247
                Wpf
 
248
        }
 
249
}
 
250