**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / Application.cs
blob5625d7c40480b3d2e81d14d0330c5a447843ac08
1 //
2 // System.Windows.Forms.Application.cs
3 //
4 // Author:
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)
9 //
10 // (C) Ximian, Inc 2002/3
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System;
35 using System.Drawing;
36 using Microsoft.Win32;
37 using System.ComponentModel;
38 using System.Threading;
39 using System.Globalization;
40 using System.Reflection;
41 using System.Collections;
42 using System.Runtime.CompilerServices;
43 namespace System.Windows.Forms
46 /// <summary>
47 /// Provides static methods and properties to manage an application,
48 /// such as methods to start and stop an application, to process
49 /// Windows messages, and properties to get information about an
50 /// application. This class cannot be inherited.
51 /// </summary>
53 [MonoTODO]
54 public sealed class Application
56 static private ApplicationContext applicationContext = null;
57 static private bool messageLoopStarted = false;
58 static private bool messageLoopStopRequest = false;
59 static private ArrayList messageFilters = new ArrayList ();
60 static private string safeTopLevelCaptionFormat;
61 static private bool showingException = false;
64 private Application(){//For signiture compatablity. Prevents the auto creation of public constructor
67 // --- (public) Properties ---
68 public static bool AllowQuit
70 // according to docs return false if embbedded in a
71 // browser, not (yet?) embedded in a browser
72 get { return true; }
75 [MonoTODO]
76 public static string CommonAppDataPath
78 get
80 //FIXME:
81 return "";
85 [MonoTODO]
86 public static RegistryKey CommonAppDataRegistry
88 get
90 throw new NotImplementedException ();
94 [MonoTODO]
95 public static string CompanyName
97 get
99 AssemblyCompanyAttribute[] attrs =(AssemblyCompanyAttribute[]) Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute),true);
100 if (attrs != null && attrs[0] != null)
101 return attrs[0].Company;
102 return "";
106 [MonoTODO]
107 public static CultureInfo CurrentCulture
109 get
111 return CultureInfo.CurrentCulture;
113 set
115 Thread.CurrentThread.CurrentCulture = value;
119 [MonoTODO]
120 public static InputLanguage CurrentInputLanguage
122 get { throw new NotImplementedException (); }
123 set {return;}
126 [MonoTODO]
127 public static string ExecutablePath
129 get
131 return Assembly.GetExecutingAssembly().Location;
135 [MonoTODO]
136 public static string LocalUserAppDataPath
138 get
140 //FIXME:
141 return "";
145 public static bool MessageLoop
147 get
149 return messageLoopStarted;
153 [MonoTODO]
154 //.NET version 1.1
155 public static void EnableVisualStyles ()
157 return;
160 [MonoTODO]
161 public static string ProductName
163 get
165 AssemblyProductAttribute[] attrs =(AssemblyProductAttribute[]) Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute),true);
166 if (attrs != null && attrs[0] != null)
167 return attrs[0].Product;
168 return "";
172 [MonoTODO]
173 public static string ProductVersion
175 get
177 AssemblyVersionAttribute[] attrs =(AssemblyVersionAttribute[]) Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyVersionAttribute),true);
178 if (attrs != null && attrs[0] != null)
179 return attrs[0].Version;
180 return "";
184 [MonoTODO]
185 public static string SafeTopLevelCaptionFormat
187 get
189 return safeTopLevelCaptionFormat;
191 set
193 safeTopLevelCaptionFormat = value;
197 [MonoTODO]
198 public static string StartupPath
200 get
202 //FIXME:
203 return "";
207 [MonoTODO]
208 public static string UserAppDataPath
210 get
212 //FIXME:
213 return "";
217 [MonoTODO]
218 // Registry key not yet defined
219 public static RegistryKey UserAppDataRegistry
221 get { throw new NotImplementedException (); }
224 // --- Methods ---
225 public static void AddMessageFilter (IMessageFilter value)
227 messageFilters.Add (value);
230 //Compact Framework
231 public static void DoEvents ()
233 MSG msg = new MSG();
235 while (Win32.PeekMessageA (ref msg, (IntPtr) 0, 0, 0, (uint)PeekMessageFlags.PM_REMOVE) != 0) {
236 if (msg.message==Msg.WM_PAINT) {
237 Win32.TranslateMessage(ref msg);
238 Win32.DispatchMessageA(ref msg);
243 //Compact Framework
244 public static void Exit ()
246 Win32.PostQuitMessage (0);
249 public static void ExitThread ()
251 messageLoopStopRequest = true;
254 [MonoTODO]
255 public static ApartmentState OleRequired ()
257 throw new NotImplementedException ();
260 [MonoTODO]
261 public static void OnThreadException (Exception t)
264 if(Application.ThreadException != null)
265 Application.ThreadException(null, new ThreadExceptionEventArgs(t));
266 else{
268 if (!showingException) {
270 showingException = true;
272 Form excepForm = new Form();
273 excepForm.ClientSize = new System.Drawing.Size(400, 250);
275 TextBox txtLabel = new TextBox();
276 txtLabel.Location = new System.Drawing.Point(30, 30);
277 txtLabel.ReadOnly = true;
278 txtLabel.Multiline = true;
279 txtLabel.Size = new System.Drawing.Size(310, 50);
280 txtLabel.Text = "The application has produced an exception. Press 'Continue' if you want the application to try to continue its execution";
281 excepForm.Controls.Add(txtLabel);
283 TextBox txtError = new TextBox();
284 txtError.Location = new System.Drawing.Point(30, 110);
285 txtError.ReadOnly = true;
286 txtLabel.Multiline = true;
287 txtError.Size = new System.Drawing.Size(310, 50);
288 txtError.Text = t.Message;
289 excepForm.Controls.Add(txtError);
291 StackButton stackbtn = new StackButton(t);
292 stackbtn.Location = new System.Drawing.Point(30, 200);
293 stackbtn.Size = new System.Drawing.Size(100, 30);
294 stackbtn.Text = "Stack Trace";
295 excepForm.Controls.Add(stackbtn);
297 ContinueButton continuebtn = new ContinueButton(excepForm);
298 continuebtn.Location = new System.Drawing.Point(160, 200);
299 continuebtn.Size = new System.Drawing.Size(100, 30);
300 continuebtn.Text = "Continue";
301 excepForm.Controls.Add(continuebtn);
303 QuitButton quitbtn = new QuitButton();
304 quitbtn.Location = new System.Drawing.Point(290, 200);
305 quitbtn.Size = new System.Drawing.Size(100, 30);
306 quitbtn.Text = "Quit";
307 excepForm.Controls.Add(quitbtn);
309 excepForm.ShowDialog();
310 showingException = false;
318 public static void RemoveMessageFilter (IMessageFilter value)
320 messageFilters.Remove (value);
323 static private void ApplicationFormClosed (object o, EventArgs args)
325 Win32.PostQuitMessage (0);
328 //Compact Framework
329 static public void Run ()
331 MSG msg = new MSG();
333 messageLoopStarted = true;
336 while (!messageLoopStopRequest &&
337 Win32.GetMessageA (ref msg, 0, 0, 0) != 0)
340 bool dispatchMessage = true;
342 Message message = new Message ();
343 message.HWnd = msg.hwnd;
344 message.Msg = (int) msg.message;//
345 message.WParam = msg.wParam;
346 message.LParam = msg.lParam;
348 IEnumerator e = messageFilters.GetEnumerator();
350 while (e.MoveNext())
352 IMessageFilter filter =
353 (IMessageFilter) e.Current;
355 // if PreFilterMessage returns true
356 // the message should not be dispatched
357 if (filter.PreFilterMessage (ref message))
358 dispatchMessage = false;
361 Control receiver = Control.FromChildHandle ( message.HWnd );
362 if ( receiver != null )
364 dispatchMessage = ! receiver.PreProcessMessage ( ref message );
367 if (dispatchMessage)
369 Win32.TranslateMessage (ref msg);
370 Win32.DispatchMessageA (ref msg);
372 //if (Idle != null)
373 //Idle (null, new EventArgs());
376 //if (ApplicationExit != null)
377 //ApplicationExit (null, new EventArgs());
380 public static void Run (ApplicationContext context)
382 applicationContext = context;
383 applicationContext.MainForm.Show ();
384 applicationContext.ThreadExit += new EventHandler( ApplicationFormClosed );
385 // applicationContext.MainForm.Closed += //
386 // new EventHandler (ApplicationFormClosed);
387 Run();
390 //[TypeAttributes.BeforeFieldInit]
391 public static void Run (Form mainForm)
392 // Documents say this parameter name should be mainform,
393 // but the verifier says context.
395 mainForm.CreateControl ();
396 ApplicationContext context = new ApplicationContext (
397 mainForm);
398 Run (context);
401 internal static void enterModalLoop ( Form mainForm )
403 mainForm.ExitModalLoop = false;
405 MSG msg = new MSG();
406 while( Win32.GetMessageA( ref msg, 0, 0, 0 ) != 0 )
409 if ( mainForm.ExitModalLoop )
410 break;
412 Message message = new Message ();
413 message.HWnd = msg.hwnd;
414 message.Msg = (int) msg.message;
415 message.WParam = msg.wParam;
416 message.LParam = msg.lParam;
418 Control receiver = Control.FromChildHandle ( message.HWnd );
419 if ( receiver != null )
420 if ( receiver.PreProcessMessage ( ref message ) )
421 continue;
423 Win32.TranslateMessage (ref msg);
424 Win32.DispatchMessageA (ref msg);
429 internal static void exitModalLoop ( Form mainForm )
431 mainForm.ExitModalLoop = true;
433 Win32.PostMessage( IntPtr.Zero, Msg.WM_NULL, 0, 0 );
436 // --- Events ---
437 public static event EventHandler ApplicationExit;
438 public static event EventHandler Idle;
439 public static event ThreadExceptionEventHandler ThreadException;
440 public static event EventHandler ThreadExit;
443 // StackButton
444 internal class StackButton : System.Windows.Forms.Button{
446 private Exception excep = null;
448 public StackButton(Exception t) : base(){
449 excep = t;
452 protected override void OnClick(EventArgs e) {
453 MessageBox.Show(excep.StackTrace, "Stack Trace");
457 // QuitButton
458 internal class QuitButton : System.Windows.Forms.Button{
460 public QuitButton() : base(){}
462 protected override void OnClick(EventArgs e) {
463 Application.ExitThread();
464 Application.Exit();
468 // ContinueButton
469 internal class ContinueButton : System.Windows.Forms.Button{
471 private Form form = null;
473 public ContinueButton(Form frm) : base(){form=frm;}
475 protected override void OnClick(EventArgs e) {
476 form.Close();