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
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
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
53 // - Added .Net 1.1 method
55 // Revision 1.1 2004/07/09 05:21:25 pbartok
62 using Microsoft
.Win32
;
65 using System
.ComponentModel
;
66 using System
.Collections
;
67 using System
.Diagnostics
;
68 using System
.Globalization
;
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();
89 browser_embedded
= 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
) {
98 Queue toplevels
= new Queue();
99 IEnumerator control
= Control
.controls
.GetEnumerator();
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
) {
119 XplatUI
.TranslateMessage(ref msg
);
120 XplatUI
.DispatchMessage(ref msg
);
122 // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
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
{
137 return browser_embedded
;
141 public static string CommonAppDataPath
{
143 return Environment
.GetFolderPath(Environment
.SpecialFolder
.CommonApplicationData
);
147 public static RegistryKey CommonAppDataRegistry
{
151 key
= Registry
.LocalMachine
.OpenSubKey("Software\\" + Application
.CompanyName
+ "\\" + Application
.ProductName
+ "\\" + Application
.ProductVersion
, true);
157 public static string CompanyName
{
161 if (Environment
.OSVersion
.Platform
!= (PlatformID
)128) {
165 key
=Registry
.LocalMachine
.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion", false);
166 ret
=(String
)key
.GetValue("RegisteredOrganization");
173 return st
.GetFrame(st
.FrameCount
-1).GetMethod().DeclaringType
.Namespace
;
177 public static CultureInfo CurrentCulture
{
179 return Thread
.CurrentThread
.CurrentUICulture
;
184 Thread
.CurrentThread
.CurrentUICulture
=value;
188 public static InputLanguage CurrentInputLanguage
{
190 return input_language
;
194 input_language
=value;
198 public static string ExecutablePath
{
200 return Assembly
.GetEntryAssembly().Location
;
204 public static string LocalUserAppDataPath
{
206 return Environment
.GetFolderPath(Environment
.SpecialFolder
.LocalApplicationData
);
210 public static bool MessageLoop
{
212 return messageloop_started
;
216 public static string ProductName
{
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
{
232 version
= Assembly
.GetEntryAssembly().GetName().Version
.ToString();
234 if (version
.StartsWith("0.")) {
235 version
="1." + version
.Substring(2);
241 public static string SafeTopLevelCaptionFormat
{
243 return safe_caption_format
;
247 safe_caption_format
=value;
251 public static string StartupPath
{
253 return Path
.GetDirectoryName(Application
.ExecutablePath
);
257 public static string UserAppDataPath
{
259 return Environment
.GetFolderPath(Environment
.SpecialFolder
.ApplicationData
);
263 public static RegistryKey UserAppDataRegistry
{
267 key
= Registry
.CurrentUser
.OpenSubKey("Software\\" + Application
.CompanyName
+ "\\" + Application
.ProductName
+ "\\" + Application
.ProductVersion
, true);
274 #region Public Static Methods
275 public static void AddMessageFilter(IMessageFilter
value) {
276 message_filters
.Add(value);
279 public static void DoEvents() {
283 public static void EnableVisualStyles() {
284 XplatUI
.EnableThemes();
287 public static void Exit() {
291 public static void ExitThread() {
295 private static void InternalExit(object sender
, EventArgs e
) {
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
));
308 XplatUI
.HandleException(t
);
312 public static void RemoveMessageFilter(IMessageFilter filter
) {
313 message_filters
.Remove(filter
);
316 public static void Run() {
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
) {
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
) {
350 if (app_context
.MainForm
!=null) {
351 app_context
.MainForm
.Show();
352 app_context
.ThreadExit
+= new EventHandler(InternalExit
);
356 #endregion // Public Static Methods
359 public static event EventHandler ApplicationExit
;
361 public static event EventHandler Idle
{
363 XplatUI
.Idle
+= value;
366 XplatUI
.Idle
-= value;
370 public static event EventHandler ThreadExit
;
371 public static event ThreadExceptionEventHandler ThreadException
;