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
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
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
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.
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
76 public static string CommonAppDataPath
86 public static RegistryKey CommonAppDataRegistry
90 throw new NotImplementedException ();
95 public static string CompanyName
99 AssemblyCompanyAttribute
[] attrs
=(AssemblyCompanyAttribute
[]) Assembly
.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute
),true);
100 if (attrs
!= null && attrs
[0] != null)
101 return attrs
[0].Company
;
107 public static CultureInfo CurrentCulture
111 return CultureInfo
.CurrentCulture
;
115 Thread
.CurrentThread
.CurrentCulture
= value;
120 public static InputLanguage CurrentInputLanguage
122 get { throw new NotImplementedException (); }
127 public static string ExecutablePath
131 return Assembly
.GetExecutingAssembly().Location
;
136 public static string LocalUserAppDataPath
145 public static bool MessageLoop
149 return messageLoopStarted
;
155 public static void EnableVisualStyles ()
161 public static string ProductName
165 AssemblyProductAttribute
[] attrs
=(AssemblyProductAttribute
[]) Assembly
.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute
),true);
166 if (attrs
!= null && attrs
[0] != null)
167 return attrs
[0].Product
;
173 public static string ProductVersion
177 AssemblyVersionAttribute
[] attrs
=(AssemblyVersionAttribute
[]) Assembly
.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyVersionAttribute
),true);
178 if (attrs
!= null && attrs
[0] != null)
179 return attrs
[0].Version
;
185 public static string SafeTopLevelCaptionFormat
189 return safeTopLevelCaptionFormat
;
193 safeTopLevelCaptionFormat
= value;
198 public static string StartupPath
208 public static string UserAppDataPath
218 // Registry key not yet defined
219 public static RegistryKey UserAppDataRegistry
221 get { throw new NotImplementedException (); }
225 public static void AddMessageFilter (IMessageFilter
value)
227 messageFilters
.Add (value);
231 public static void DoEvents ()
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
);
244 public static void Exit ()
246 Win32
.PostQuitMessage (0);
249 public static void ExitThread ()
251 messageLoopStopRequest
= true;
255 public static ApartmentState
OleRequired ()
257 throw new NotImplementedException ();
261 public static void OnThreadException (Exception t
)
264 if(Application
.ThreadException
!= null)
265 Application
.ThreadException(null, new ThreadExceptionEventArgs(t
));
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);
329 static public void Run ()
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();
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
);
369 Win32
.TranslateMessage (ref msg
);
370 Win32
.DispatchMessageA (ref msg
);
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);
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 (
401 internal static void enterModalLoop ( Form mainForm
)
403 mainForm
.ExitModalLoop
= false;
406 while( Win32
.GetMessageA( ref msg
, 0, 0, 0 ) != 0 )
409 if ( mainForm
.ExitModalLoop
)
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
) )
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 );
437 public static event EventHandler ApplicationExit
;
438 public static event EventHandler Idle
;
439 public static event ThreadExceptionEventHandler ThreadException
;
440 public static event EventHandler ThreadExit
;
444 internal class StackButton
: System
.Windows
.Forms
.Button
{
446 private Exception excep
= null;
448 public StackButton(Exception t
) : base(){
452 protected override void OnClick(EventArgs e
) {
453 MessageBox
.Show(excep
.StackTrace
, "Stack Trace");
458 internal class QuitButton
: System
.Windows
.Forms
.Button
{
460 public QuitButton() : base(){}
462 protected override void OnClick(EventArgs e
) {
463 Application
.ExitThread();
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
) {