2 // System.Windows.Forms.Application.cs
5 // stubbed out by Jaak Simm (jaaksimm@firm.ee)
6 // Miguel de Icaza (miguel@ximian.com)
7 // Dennis hayes (dennish@raytek.com)
8 // WINELib implementation started by John Sohn (jsohn@columbus.rr.com)
10 // (C) Ximian, Inc 2002/3
15 using Microsoft.Win32;
16 using System.ComponentModel;
17 using System.Threading;
18 using System.Globalization;
19 using System.Reflection;
20 using System.Collections;
21 using System.Runtime.CompilerServices;
22 namespace System.Windows.Forms
26 /// Provides static methods and properties to manage an application,
27 /// such as methods to start and stop an application, to process
28 /// Windows messages, and properties to get information about an
29 /// application. This class cannot be inherited.
33 public sealed class Application
35 static private ApplicationContext applicationContext = null;
36 static private bool messageLoopStarted = false;
37 static private bool messageLoopStopRequest = false;
38 static private ArrayList messageFilters = new ArrayList ();
39 static private string safeTopLevelCaptionFormat;
40 static private bool showingException = false;
43 private Application(){//For signiture compatablity. Prevents the auto creation of public constructor
46 // --- (public) Properties ---
47 public static bool AllowQuit
49 // according to docs return false if embbedded in a
50 // browser, not (yet?) embedded in a browser
55 public static string CommonAppDataPath
65 public static RegistryKey CommonAppDataRegistry
69 throw new NotImplementedException ();
74 public static string CompanyName
78 AssemblyCompanyAttribute[] attrs =(AssemblyCompanyAttribute[]) Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute),true);
79 if (attrs != null && attrs[0] != null)
80 return attrs[0].Company;
86 public static CultureInfo CurrentCulture
90 return CultureInfo.CurrentCulture;
94 Thread.CurrentThread.CurrentCulture = value;
99 public static InputLanguage CurrentInputLanguage
101 get { throw new NotImplementedException (); }
106 public static string ExecutablePath
110 return Assembly.GetExecutingAssembly().Location;
115 public static string LocalUserAppDataPath
124 public static bool MessageLoop
128 return messageLoopStarted;
134 public static void EnableVisualStyles ()
140 public static string ProductName
144 AssemblyProductAttribute[] attrs =(AssemblyProductAttribute[]) Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute),true);
145 if (attrs != null && attrs[0] != null)
146 return attrs[0].Product;
152 public static string ProductVersion
156 AssemblyVersionAttribute[] attrs =(AssemblyVersionAttribute[]) Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyVersionAttribute),true);
157 if (attrs != null && attrs[0] != null)
158 return attrs[0].Version;
164 public static string SafeTopLevelCaptionFormat
168 return safeTopLevelCaptionFormat;
172 safeTopLevelCaptionFormat = value;
177 public static string StartupPath
187 public static string UserAppDataPath
197 // Registry key not yet defined
198 public static RegistryKey UserAppDataRegistry
200 get { throw new NotImplementedException (); }
204 public static void AddMessageFilter (IMessageFilter value)
206 messageFilters.Add (value);
210 public static void DoEvents ()
214 while (Win32.PeekMessageA (ref msg, (IntPtr) 0, 0, 0, (uint)PeekMessageFlags.PM_REMOVE) != 0) {
215 if (msg.message==Msg.WM_PAINT) {
216 Win32.TranslateMessage(ref msg);
217 Win32.DispatchMessageA(ref msg);
223 public static void Exit ()
225 Win32.PostQuitMessage (0);
228 public static void ExitThread ()
230 messageLoopStopRequest = true;
234 public static ApartmentState OleRequired ()
236 throw new NotImplementedException ();
240 public static void OnThreadException (Exception t)
243 if(Application.ThreadException != null)
244 Application.ThreadException(null, new ThreadExceptionEventArgs(t));
247 if (!showingException) {
249 showingException = true;
251 Form excepForm = new Form();
252 excepForm.ClientSize = new System.Drawing.Size(400, 250);
254 TextBox txtLabel = new TextBox();
255 txtLabel.Location = new System.Drawing.Point(30, 30);
256 txtLabel.ReadOnly = true;
257 txtLabel.Multiline = true;
258 txtLabel.Size = new System.Drawing.Size(310, 50);
259 txtLabel.Text = "The application has produced an exception. Press 'Continue' if you want the application to try to continue its execution";
260 excepForm.Controls.Add(txtLabel);
262 TextBox txtError = new TextBox();
263 txtError.Location = new System.Drawing.Point(30, 110);
264 txtError.ReadOnly = true;
265 txtLabel.Multiline = true;
266 txtError.Size = new System.Drawing.Size(310, 50);
267 txtError.Text = t.Message;
268 excepForm.Controls.Add(txtError);
270 StackButton stackbtn = new StackButton(t);
271 stackbtn.Location = new System.Drawing.Point(30, 200);
272 stackbtn.Size = new System.Drawing.Size(100, 30);
273 stackbtn.Text = "Stack Trace";
274 excepForm.Controls.Add(stackbtn);
276 ContinueButton continuebtn = new ContinueButton(excepForm);
277 continuebtn.Location = new System.Drawing.Point(160, 200);
278 continuebtn.Size = new System.Drawing.Size(100, 30);
279 continuebtn.Text = "Continue";
280 excepForm.Controls.Add(continuebtn);
282 QuitButton quitbtn = new QuitButton();
283 quitbtn.Location = new System.Drawing.Point(290, 200);
284 quitbtn.Size = new System.Drawing.Size(100, 30);
285 quitbtn.Text = "Quit";
286 excepForm.Controls.Add(quitbtn);
288 excepForm.ShowDialog();
289 showingException = false;
297 public static void RemoveMessageFilter (IMessageFilter value)
299 messageFilters.Remove (value);
302 static private void ApplicationFormClosed (object o, EventArgs args)
304 Win32.PostQuitMessage (0);
308 static public void Run ()
312 messageLoopStarted = true;
315 while (!messageLoopStopRequest &&
316 Win32.GetMessageA (ref msg, 0, 0, 0) != 0)
319 bool dispatchMessage = true;
321 Message message = new Message ();
322 message.HWnd = msg.hwnd;
323 message.Msg = (int) msg.message;//
324 message.WParam = msg.wParam;
325 message.LParam = msg.lParam;
327 IEnumerator e = messageFilters.GetEnumerator();
331 IMessageFilter filter =
332 (IMessageFilter) e.Current;
334 // if PreFilterMessage returns true
335 // the message should not be dispatched
336 if (filter.PreFilterMessage (ref message))
337 dispatchMessage = false;
340 Control receiver = Control.FromChildHandle ( message.HWnd );
341 if ( receiver != null )
343 dispatchMessage = ! receiver.PreProcessMessage ( ref message );
348 Win32.TranslateMessage (ref msg);
349 Win32.DispatchMessageA (ref msg);
352 //Idle (null, new EventArgs());
355 //if (ApplicationExit != null)
356 //ApplicationExit (null, new EventArgs());
359 public static void Run (ApplicationContext context)
361 applicationContext = context;
362 applicationContext.MainForm.Show ();
363 applicationContext.ThreadExit += new EventHandler( ApplicationFormClosed );
364 // applicationContext.MainForm.Closed += //
365 // new EventHandler (ApplicationFormClosed);
369 //[TypeAttributes.BeforeFieldInit]
370 public static void Run (Form mainForm)
371 // Documents say this parameter name should be mainform,
372 // but the verifier says context.
374 mainForm.CreateControl ();
375 ApplicationContext context = new ApplicationContext (
380 internal static void enterModalLoop ( Form mainForm )
382 mainForm.ExitModalLoop = false;
385 while( Win32.GetMessageA( ref msg, 0, 0, 0 ) != 0 )
388 if ( mainForm.ExitModalLoop )
391 Message message = new Message ();
392 message.HWnd = msg.hwnd;
393 message.Msg = (int) msg.message;
394 message.WParam = msg.wParam;
395 message.LParam = msg.lParam;
397 Control receiver = Control.FromChildHandle ( message.HWnd );
398 if ( receiver != null )
399 if ( receiver.PreProcessMessage ( ref message ) )
402 Win32.TranslateMessage (ref msg);
403 Win32.DispatchMessageA (ref msg);
408 internal static void exitModalLoop ( Form mainForm )
410 mainForm.ExitModalLoop = true;
412 Win32.PostMessage( IntPtr.Zero, Msg.WM_NULL, 0, 0 );
416 public static event EventHandler ApplicationExit;
417 public static event EventHandler Idle;
418 public static event ThreadExceptionEventHandler ThreadException;
419 public static event EventHandler ThreadExit;
423 internal class StackButton : System.Windows.Forms.Button{
425 private Exception excep = null;
427 public StackButton(Exception t) : base(){
431 protected override void OnClick(EventArgs e) {
432 MessageBox.Show(excep.StackTrace, "Stack Trace");
437 internal class QuitButton : System.Windows.Forms.Button{
439 public QuitButton() : base(){}
441 protected override void OnClick(EventArgs e) {
442 Application.ExitThread();
448 internal class ContinueButton : System.Windows.Forms.Button{
450 private Form form = null;
452 public ContinueButton(Form frm) : base(){form=frm;}
454 protected override void OnClick(EventArgs e) {