- Added new keyword where required
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Application.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25
26
27 // COMPLETE
28
29 using Microsoft.Win32;
30 using System;
31 using System.Drawing;
32 using System.ComponentModel;
33 using System.Collections;
34 using System.Diagnostics;
35 using System.Globalization;
36 using System.IO;
37 using System.Reflection;
38 using System.Runtime.InteropServices;
39 using System.Threading;
40
41 namespace System.Windows.Forms {
42         public sealed class Application {
43                 private static bool                     browser_embedded;
44                 private static bool                     exiting;
45                 private static InputLanguage            input_language;
46                 private static bool                     messageloop_started;
47                 private static string                   safe_caption_format;
48                 private static ArrayList                message_filters;
49                 private static ApplicationContext       app_context;
50
51                 private Application () {
52                         input_language  = InputLanguage.CurrentInputLanguage;
53                         message_filters = new ArrayList();
54                         app_context     = null;
55                         browser_embedded= false;
56                         exiting         = false;
57                         messageloop_started = false;
58                         safe_caption_format = "{1} - {0} - {2}";
59                 }
60
61                 #region Private and Internal Methods
62                 internal static void ModalRun(Form form) {
63                         MSG     msg = new MSG();
64                         Queue   toplevels = new Queue();
65                         IEnumerator control = Control.controls.GetEnumerator();
66
67                         if (form == null) {
68                                 return;
69                         }
70
71                         while (control.MoveNext()) {
72                                 if ((((Control)control.Current).parent == null) && (((Control)control.Current).is_visible) && (((Control)control.Current).is_enabled)) {
73                                         if ((control.Current is Form.FormParentWindow)  && (((Form.FormParentWindow)control.Current)!=form.form_parent_window)) {
74                                                 XplatUI.EnableWindow(((Control)control.Current).window.Handle, false);
75                                                 toplevels.Enqueue((Control)control.Current);
76                                         }
77                                 }
78                         }
79
80                         while (!exiting && !form.end_modal && XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
81                                 XplatUI.TranslateMessage(ref msg);
82                                 XplatUI.DispatchMessage(ref msg);
83
84                                 // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
85                                 if (form.closing) {
86                                         form.end_modal = true;
87                                 }
88                         }
89
90                         while (toplevels.Count>0) {
91                                 XplatUI.EnableWindow(((Control)toplevels.Dequeue()).window.Handle, true);
92                         }
93                 }
94                 #endregion      // Private and Internal Methods
95
96                 #region Public Static Properties
97                 public static bool AllowQuit {
98                         get {
99                                 return browser_embedded;
100                         }
101                 }
102
103                 public static string CommonAppDataPath {
104                         get {
105                                 return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
106                         }
107                 }
108
109                 public static RegistryKey CommonAppDataRegistry {
110                         get {
111                                 RegistryKey     key;
112
113                                 key = Registry.LocalMachine.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
114
115                                 return key;
116                         }
117                 }
118
119                 public static string CompanyName {
120                         get {
121                                 StackTrace      st;
122
123                                 if (Environment.OSVersion.Platform != (PlatformID)128) {
124                                         RegistryKey     key;
125                                         String          ret;
126
127                                         key=Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion", false);
128                                         ret=(String)key.GetValue("RegisteredOrganization");
129
130                                         return ret;
131                                         
132                                 }
133
134                                 st=new StackTrace();
135                                 return st.GetFrame(st.FrameCount-1).GetMethod().DeclaringType.Namespace;
136                         }
137                 }
138
139                 public static CultureInfo CurrentCulture {
140                         get {
141                                 return Thread.CurrentThread.CurrentUICulture;
142                         }
143
144                         set {
145                                 
146                                 Thread.CurrentThread.CurrentUICulture=value;
147                         }
148                 }
149
150                 public static InputLanguage CurrentInputLanguage {
151                         get {
152                                 return input_language;
153                         }
154
155                         set {
156                                 input_language=value;
157                         }
158                 }
159
160                 public static string ExecutablePath {
161                         get {
162                                 return Assembly.GetEntryAssembly().Location;
163                         }
164                 }
165
166                 public static string LocalUserAppDataPath {
167                         get {
168                                 return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
169                         }
170                 }
171
172                 public static bool MessageLoop {
173                         get {
174                                 return messageloop_started;
175                         }
176                 }
177
178                 public static string ProductName {
179                         get {
180                                 AssemblyProductAttribute[] attrs = (AssemblyProductAttribute[]) Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), true);
181                                 
182                                 if ((attrs != null) && attrs.Length>0) {
183                                         return attrs[0].Product;
184                                 }
185
186                                 return Assembly.GetEntryAssembly().GetName().Name;
187                         }
188                 }
189
190                 public static string ProductVersion {
191                         get {
192                                 String version;
193
194                                 version = Assembly.GetEntryAssembly().GetName().Version.ToString();
195
196                                 if (version.StartsWith("0.")) {
197                                         version="1." + version.Substring(2);
198                                 }
199                                 return version;
200                         }
201                 }
202
203                 public static string SafeTopLevelCaptionFormat {
204                         get {
205                                 return safe_caption_format;
206                         }
207
208                         set {
209                                 safe_caption_format=value;
210                         }
211                 }
212
213                 public static string StartupPath {
214                         get {
215                                 return Path.GetDirectoryName(Application.ExecutablePath);
216                         }
217                 }
218
219                 public static string UserAppDataPath {
220                         get {
221                                 return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
222                         }
223                 }
224
225                 public static RegistryKey UserAppDataRegistry {
226                         get {
227                                 RegistryKey     key;
228
229                                 key = Registry.CurrentUser.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
230
231                                 return key;
232                         }
233                 }
234                 #endregion
235
236                 #region Public Static Methods
237                 public static void AddMessageFilter(IMessageFilter value) {
238                         message_filters.Add(value);
239                 }
240
241                 public static void DoEvents() {
242                         XplatUI.DoEvents();
243                 }
244
245                 public static void EnableVisualStyles() {
246                         XplatUI.EnableThemes();
247                 }
248
249                 public static void Exit() {
250                         XplatUI.Exit();
251                 }
252
253                 public static void ExitThread() {
254                         exiting=true;
255                 }
256
257                 private static void InternalExit(object sender, EventArgs e) {
258                         Application.Exit();
259                 }
260
261                 public static ApartmentState OleRequired() {
262                         //throw new NotImplementedException("OLE Not supported by this System.Windows.Forms implementation");
263                         return ApartmentState.Unknown;
264                 }
265
266                 public static void OnThreadException(Exception t) {
267                         if (Application.ThreadException != null) {
268                                 Application.ThreadException(null, new ThreadExceptionEventArgs(t));
269                                 return;
270                         }
271 #if !later
272                          else {
273                                 XplatUI.HandleException(t);
274                         }
275 #else
276                         // TODO: Missing implementation
277                         //if (SystemInformation.UserInteractive)
278                         {
279                                 Form form = new ThreadExceptionDialog (t);
280                                 form.ShowDialog ();
281                         }
282                         //else
283                                 Console.WriteLine (t.ToString ());
284 #endif
285                 }
286
287                 public static void RemoveMessageFilter(IMessageFilter filter) {
288                         message_filters.Remove(filter);
289                 }
290
291                 public static void Run() {
292                         MSG     msg = new MSG();
293                         Form    form = null;
294
295                         if (app_context != null) {
296                                 form = app_context.MainForm;
297                         }
298
299                         messageloop_started = true;
300
301                         while (!exiting && XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
302                                 XplatUI.TranslateMessage(ref msg);
303                                 XplatUI.DispatchMessage(ref msg);
304
305                                 // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
306                                 if ((form != null) && form.closing) {
307                                         exiting = true;
308                                 }
309                         }
310
311                         messageloop_started = false;
312
313                         if (ThreadExit != null) {
314                                 ThreadExit(null, EventArgs.Empty);
315                         }
316
317                         if (ApplicationExit != null) {
318                                 ApplicationExit(null, EventArgs.Empty);
319                         }
320                 }
321
322                 public static void Run(Form mainForm) {
323                         mainForm.CreateControl();
324                         Run(new ApplicationContext(mainForm));
325                 }
326
327                 public static void Run(ApplicationContext context) {
328                         app_context=context;
329                         if (app_context.MainForm!=null) {
330                                 app_context.MainForm.Show();
331                                 app_context.ThreadExit += new EventHandler(InternalExit);
332                         }
333                         Run();
334                 }
335                 #endregion      // Public Static Methods
336
337                 #region Events
338                 public static event EventHandler        ApplicationExit;
339
340                 public static event EventHandler        Idle {
341                         add {
342                                 XplatUI.Idle += value;
343                         }
344                         remove {
345                                 XplatUI.Idle -= value;
346                         }
347                 }
348
349                 public static event EventHandler        ThreadExit;
350                 public static event ThreadExceptionEventHandler ThreadException;
351                 #endregion      // Events
352         }
353 }