(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Application.cs
blobed17bd4c1a1f589b98d8ac6351afd169b41d4c2e
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.
20 // Copyright (c) 2004 Novell, Inc.
22 // Authors:
23 // Peter Bartok pbartok@novell.com
26 // $Revision: 1.8 $
27 // $Modtime: $
28 // $Log: Application.cs,v $
29 // Revision 1.8 2004/10/20 03:33:33 pbartok
30 // - Fixed to deal with new Form subclasses for menus
32 // Revision 1.7 2004/10/18 04:14:14 pbartok
33 // - Added code to simulate modal dialogs on Win32
35 // Revision 1.6 2004/09/22 20:05:41 pbartok
36 // - Added message loop for modal dialogs
38 // Revision 1.5 2004/09/21 00:54:15 jackson
39 // New message loop that uses poll so we don't get a busy loop
41 // Revision 1.4 2004/08/23 22:45:19 pbartok
42 // - Removed debug output
43 // - Simplifications
45 // Revision 1.3 2004/08/23 22:09:29 pbartok
46 // - Added handling of Idle event
47 // - Added handling of form closing
48 // - Fixed reporting of MessageLoop property
49 // - Removed some unneeded code, should provide a bit of a speedup
51 // Revision 1.2 2004/08/11 22:16:50 pbartok
52 // - Fixed Signature
53 // - Added .Net 1.1 method
55 // Revision 1.1 2004/07/09 05:21:25 pbartok
56 // - Initial check-in
60 // COMPLETE
62 using Microsoft.Win32;
63 using System;
64 using System.Drawing;
65 using System.ComponentModel;
66 using System.Collections;
67 using System.Diagnostics;
68 using System.Globalization;
69 using System.IO;
70 using System.Reflection;
71 using System.Runtime.InteropServices;
72 using System.Threading;
74 namespace System.Windows.Forms {
75 public sealed class Application {
76 private static bool browser_embedded;
77 private static bool exiting;
78 private static InputLanguage input_language;
79 private static bool messageloop_started;
80 private static string safe_caption_format;
81 private static ArrayList message_filters;
82 private static ApplicationContext app_context;
83 private static Form main_form;
85 private Application () {
86 input_language = InputLanguage.CurrentInputLanguage;
87 message_filters = new ArrayList();
88 app_context = null;
89 browser_embedded= false;
90 exiting = false;
91 messageloop_started = false;
92 safe_caption_format = "{1} - {0} - {2}";
95 #region Private and Internal Methods
96 internal static void ModalRun(Form form) {
97 MSG msg = new MSG();
98 Queue toplevels = new Queue();
99 IEnumerator control = Control.controls.GetEnumerator();
101 if (form == null) {
102 return;
105 while (control.MoveNext()) {
106 if ((((Control)control.Current).parent == null) && (((Control)control.Current).is_visible) && (((Control)control.Current).is_enabled)) {
107 if ((control.Current is Form.FormParentWindow) && (((Form.FormParentWindow)control.Current)!=form.form_parent_window)) {
108 XplatUI.EnableWindow(((Control)control.Current).window.Handle, false);
109 toplevels.Enqueue((Control)control.Current);
114 while (!exiting && XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
115 if (form.end_modal) {
116 break;
119 XplatUI.TranslateMessage(ref msg);
120 XplatUI.DispatchMessage(ref msg);
122 // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
123 if (form.closing) {
124 form.end_modal = true;
128 while (toplevels.Count>0) {
129 XplatUI.EnableWindow(((Control)toplevels.Dequeue()).window.Handle, true);
132 #endregion // Private and Internal Methods
134 #region Public Static Properties
135 public static bool AllowQuit {
136 get {
137 return browser_embedded;
141 public static string CommonAppDataPath {
142 get {
143 return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
147 public static RegistryKey CommonAppDataRegistry {
148 get {
149 RegistryKey key;
151 key = Registry.LocalMachine.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
153 return key;
157 public static string CompanyName {
158 get {
159 StackTrace st;
161 if (Environment.OSVersion.Platform != (PlatformID)128) {
162 RegistryKey key;
163 String ret;
165 key=Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion", false);
166 ret=(String)key.GetValue("RegisteredOrganization");
168 return ret;
172 st=new StackTrace();
173 return st.GetFrame(st.FrameCount-1).GetMethod().DeclaringType.Namespace;
177 public static CultureInfo CurrentCulture {
178 get {
179 return Thread.CurrentThread.CurrentUICulture;
182 set {
184 Thread.CurrentThread.CurrentUICulture=value;
188 public static InputLanguage CurrentInputLanguage {
189 get {
190 return input_language;
193 set {
194 input_language=value;
198 public static string ExecutablePath {
199 get {
200 return Assembly.GetEntryAssembly().Location;
204 public static string LocalUserAppDataPath {
205 get {
206 return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
210 public static bool MessageLoop {
211 get {
212 return messageloop_started;
216 public static string ProductName {
217 get {
218 AssemblyProductAttribute[] attrs = (AssemblyProductAttribute[]) Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), true);
220 if ((attrs != null) && attrs.Length>0) {
221 return attrs[0].Product;
224 return Assembly.GetEntryAssembly().GetName().Name;
228 public static string ProductVersion {
229 get {
230 String version;
232 version = Assembly.GetEntryAssembly().GetName().Version.ToString();
234 if (version.StartsWith("0.")) {
235 version="1." + version.Substring(2);
237 return version;
241 public static string SafeTopLevelCaptionFormat {
242 get {
243 return safe_caption_format;
246 set {
247 safe_caption_format=value;
251 public static string StartupPath {
252 get {
253 return Path.GetDirectoryName(Application.ExecutablePath);
257 public static string UserAppDataPath {
258 get {
259 return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
263 public static RegistryKey UserAppDataRegistry {
264 get {
265 RegistryKey key;
267 key = Registry.CurrentUser.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
269 return key;
272 #endregion
274 #region Public Static Methods
275 public static void AddMessageFilter(IMessageFilter value) {
276 message_filters.Add(value);
279 public static void DoEvents() {
280 XplatUI.DoEvents();
283 public static void EnableVisualStyles() {
284 XplatUI.EnableThemes();
287 public static void Exit() {
288 XplatUI.Exit();
291 public static void ExitThread() {
292 exiting=true;
295 private static void InternalExit(object sender, EventArgs e) {
296 Application.Exit();
299 public static ApartmentState OleRequired() {
300 //throw new NotImplementedException("OLE Not supported by this System.Windows.Forms implementation");
301 return ApartmentState.Unknown;
304 public static void OnThreadException(Exception t) {
305 if (Application.ThreadException != null) {
306 Application.ThreadException(null, new ThreadExceptionEventArgs(t));
307 } else {
308 XplatUI.HandleException(t);
312 public static void RemoveMessageFilter(IMessageFilter filter) {
313 message_filters.Remove(filter);
316 public static void Run() {
317 MSG msg = new MSG();
318 Form form = null;
320 if (app_context != null) {
321 form = app_context.MainForm;
324 messageloop_started = true;
326 while (!exiting && XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
327 XplatUI.TranslateMessage(ref msg);
328 XplatUI.DispatchMessage(ref msg);
330 // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
331 if ((form != null) && form.closing) {
332 exiting = true;
336 messageloop_started = false;
338 if (ApplicationExit != null) {
339 ApplicationExit(null, EventArgs.Empty);
343 public static void Run(Form mainForm) {
344 mainForm.CreateControl();
345 Run(new ApplicationContext(mainForm));
348 public static void Run(ApplicationContext context) {
349 app_context=context;
350 if (app_context.MainForm!=null) {
351 app_context.MainForm.Show();
352 app_context.ThreadExit += new EventHandler(InternalExit);
354 Run();
356 #endregion // Public Static Methods
358 #region Events
359 public static event EventHandler ApplicationExit;
361 public static event EventHandler Idle {
362 add {
363 XplatUI.Idle += value;
365 remove {
366 XplatUI.Idle -= value;
370 public static event EventHandler ThreadExit;
371 public static event ThreadExceptionEventHandler ThreadException;
372 #endregion // Events