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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
23 // Peter Bartok pbartok@novell.com
29 using Microsoft.Win32;
32 using System.ComponentModel;
33 using System.Collections;
34 using System.Diagnostics;
35 using System.Globalization;
37 using System.Reflection;
38 using System.Runtime.InteropServices;
39 using System.Threading;
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;
51 private Application () {
52 input_language = InputLanguage.CurrentInputLanguage;
53 message_filters = new ArrayList();
55 browser_embedded= false;
57 messageloop_started = false;
58 safe_caption_format = "{1} - {0} - {2}";
61 #region Private and Internal Methods
62 internal static void ModalRun(Form form) {
64 Queue toplevels = new Queue();
65 IEnumerator control = Control.controls.GetEnumerator();
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);
80 while (!exiting && !form.end_modal && XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
81 XplatUI.TranslateMessage(ref msg);
82 XplatUI.DispatchMessage(ref msg);
84 // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
86 form.end_modal = true;
90 while (toplevels.Count>0) {
91 XplatUI.EnableWindow(((Control)toplevels.Dequeue()).window.Handle, true);
94 #endregion // Private and Internal Methods
96 #region Public Static Properties
97 public static bool AllowQuit {
99 return browser_embedded;
103 public static string CommonAppDataPath {
105 return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
109 public static RegistryKey CommonAppDataRegistry {
113 key = Registry.LocalMachine.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
119 public static string CompanyName {
123 if (Environment.OSVersion.Platform != (PlatformID)128) {
127 key=Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion", false);
128 ret=(String)key.GetValue("RegisteredOrganization");
135 return st.GetFrame(st.FrameCount-1).GetMethod().DeclaringType.Namespace;
139 public static CultureInfo CurrentCulture {
141 return Thread.CurrentThread.CurrentUICulture;
146 Thread.CurrentThread.CurrentUICulture=value;
150 public static InputLanguage CurrentInputLanguage {
152 return input_language;
156 input_language=value;
160 public static string ExecutablePath {
162 return Assembly.GetEntryAssembly().Location;
166 public static string LocalUserAppDataPath {
168 return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
172 public static bool MessageLoop {
174 return messageloop_started;
178 public static string ProductName {
180 AssemblyProductAttribute[] attrs = (AssemblyProductAttribute[]) Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), true);
182 if ((attrs != null) && attrs.Length>0) {
183 return attrs[0].Product;
186 return Assembly.GetEntryAssembly().GetName().Name;
190 public static string ProductVersion {
194 version = Assembly.GetEntryAssembly().GetName().Version.ToString();
196 if (version.StartsWith("0.")) {
197 version="1." + version.Substring(2);
203 public static string SafeTopLevelCaptionFormat {
205 return safe_caption_format;
209 safe_caption_format=value;
213 public static string StartupPath {
215 return Path.GetDirectoryName(Application.ExecutablePath);
219 public static string UserAppDataPath {
221 return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
225 public static RegistryKey UserAppDataRegistry {
229 key = Registry.CurrentUser.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
236 #region Public Static Methods
237 public static void AddMessageFilter(IMessageFilter value) {
238 message_filters.Add(value);
241 public static void DoEvents() {
245 public static void EnableVisualStyles() {
246 XplatUI.EnableThemes();
249 public static void Exit() {
253 public static void ExitThread() {
257 private static void InternalExit(object sender, EventArgs e) {
261 public static ApartmentState OleRequired() {
262 //throw new NotImplementedException("OLE Not supported by this System.Windows.Forms implementation");
263 return ApartmentState.Unknown;
266 public static void OnThreadException(Exception t) {
267 if (Application.ThreadException != null) {
268 Application.ThreadException(null, new ThreadExceptionEventArgs(t));
273 XplatUI.HandleException(t);
276 // TODO: Missing implementation
277 //if (SystemInformation.UserInteractive)
279 Form form = new ThreadExceptionDialog (t);
283 Console.WriteLine (t.ToString ());
287 public static void RemoveMessageFilter(IMessageFilter filter) {
288 message_filters.Remove(filter);
291 public static void Run() {
295 if (app_context != null) {
296 form = app_context.MainForm;
299 messageloop_started = true;
301 while (!exiting && XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
302 XplatUI.TranslateMessage(ref msg);
303 XplatUI.DispatchMessage(ref msg);
305 // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
306 if ((form != null) && form.closing) {
311 messageloop_started = false;
313 if (ThreadExit != null) {
314 ThreadExit(null, EventArgs.Empty);
317 if (ApplicationExit != null) {
318 ApplicationExit(null, EventArgs.Empty);
322 public static void Run(Form mainForm) {
323 mainForm.CreateControl();
324 Run(new ApplicationContext(mainForm));
327 public static void Run(ApplicationContext context) {
329 if (app_context.MainForm!=null) {
330 app_context.MainForm.Show();
331 app_context.ThreadExit += new EventHandler(InternalExit);
335 #endregion // Public Static Methods
338 public static event EventHandler ApplicationExit;
340 public static event EventHandler Idle {
342 XplatUI.Idle += value;
345 XplatUI.Idle -= value;
349 public static event EventHandler ThreadExit;
350 public static event ThreadExceptionEventHandler ThreadException;