* UnsafeNativeMethods.cs: added IEnumVariant interface,
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / Application.cs
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
11 //
12
13 using System;
14 using System.Drawing;
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 
23 {
24
25         /// <summary>
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.
30         /// </summary>
31
32         [MonoTODO]
33         public sealed class Application 
34         {
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;
41
42
43                 private Application(){//For signiture compatablity. Prevents the auto creation of public constructor
44                 }
45
46                 // --- (public) Properties ---
47                 public static bool AllowQuit 
48                 {
49                         // according to docs return false if embbedded in a
50                         // browser, not (yet?) embedded in a browser
51                         get { return true; }
52                 }
53
54                 [MonoTODO]
55                 public static string CommonAppDataPath 
56                 {
57                         get 
58                         {
59                                 //FIXME:
60                                 return "";
61                         }
62                 }
63
64                 [MonoTODO]
65                 public static RegistryKey CommonAppDataRegistry 
66                 {
67                         get 
68                         {
69                                 throw new NotImplementedException ();
70                         }
71                 }
72         
73                 [MonoTODO]
74                 public static string CompanyName 
75                 {
76                         get 
77                         {
78                                 AssemblyCompanyAttribute[] attrs =(AssemblyCompanyAttribute[]) Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute),true);
79                                 if (attrs != null && attrs[0] != null)
80                                         return attrs[0].Company;
81                                 return "";
82                         }
83                 }
84         
85                 [MonoTODO]
86                 public static CultureInfo CurrentCulture 
87                 {
88                         get 
89                         {
90                                 return CultureInfo.CurrentCulture;
91                         }
92                         set 
93                         {
94                                 Thread.CurrentThread.CurrentCulture = value;
95                         }
96                 }
97         
98                 [MonoTODO]
99                 public static InputLanguage CurrentInputLanguage 
100                 {
101                         get { throw new NotImplementedException (); }
102                         set {return;}
103                 }
104         
105                 [MonoTODO]
106                 public static string ExecutablePath 
107                 {
108                         get 
109                         {
110                                 return Assembly.GetExecutingAssembly().Location;                        
111                         }
112                 }
113         
114                 [MonoTODO]
115                 public static string LocalUserAppDataPath 
116                 {
117                         get 
118                         {
119                                 //FIXME:
120                                 return "";
121                         }
122                 }
123         
124                 public static bool MessageLoop 
125                 {
126                         get 
127                         {
128                                 return messageLoopStarted;
129                         }
130                 }
131
132                 [MonoTODO]
133                         //.NET version 1.1
134                 public static void EnableVisualStyles () 
135                 {
136                         return;
137                 }
138                         
139                 [MonoTODO]
140                 public static string ProductName 
141                 {
142                         get 
143                         {
144                                 AssemblyProductAttribute[] attrs =(AssemblyProductAttribute[]) Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute),true);
145                                 if (attrs != null && attrs[0] != null)
146                                         return attrs[0].Product;
147                                 return "";
148                         }
149                 }
150         
151                 [MonoTODO]
152                 public static string ProductVersion 
153                 {
154                         get 
155                         {
156                                 AssemblyVersionAttribute[] attrs =(AssemblyVersionAttribute[]) Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyVersionAttribute),true);
157                                 if (attrs != null && attrs[0] != null)
158                                         return attrs[0].Version;
159                                 return "";
160                         }
161                 }
162         
163                 [MonoTODO]
164                 public static string SafeTopLevelCaptionFormat 
165                 {
166                         get 
167                         {
168                                 return safeTopLevelCaptionFormat;
169                         }
170                         set 
171                         {
172                                 safeTopLevelCaptionFormat = value;
173                         }
174                 }
175         
176                 [MonoTODO]
177                 public static string StartupPath 
178                 {
179                         get 
180                         {
181                                 //FIXME:
182                                 return "";
183                         }
184                 }
185         
186                 [MonoTODO]
187                 public static string UserAppDataPath 
188                 {
189                         get 
190                         {
191                                 //FIXME:
192                                 return "";
193                         }
194                 }
195         
196                 [MonoTODO]
197                         // Registry key not yet defined
198                 public static RegistryKey UserAppDataRegistry 
199                 {
200                         get { throw new NotImplementedException (); }
201                 }
202         
203                 // --- Methods ---
204                 public static void AddMessageFilter (IMessageFilter value) 
205                 {
206                         messageFilters.Add (value);
207                 }
208
209                 //Compact Framework     
210                 public static void DoEvents () 
211                 {
212                         MSG msg = new MSG();
213
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);
218                                 }
219                         }
220                 }
221
222                 //Compact Framework     
223                 public static void Exit () 
224                 {
225                         Win32.PostQuitMessage (0);
226                 }
227         
228                 public static void ExitThread () 
229                 {
230                         messageLoopStopRequest = true;
231                 }
232         
233                 [MonoTODO]
234                 public static ApartmentState OleRequired () 
235                 {
236                         throw new NotImplementedException ();
237                 }
238         
239                 [MonoTODO]
240                 public static void OnThreadException (Exception t) 
241                 {                       
242                         
243                         if(Application.ThreadException != null) 
244                                 Application.ThreadException(null, new ThreadExceptionEventArgs(t));
245                         else{                                                   
246                                 
247                                 if (!showingException)  {
248                                         
249                                         showingException = true;
250                                 
251                                         Form    excepForm = new Form();
252                                         excepForm.ClientSize = new System.Drawing.Size(400, 250);                               
253                                         
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);                                       
261                                         
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);
269                                         
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); 
275                                         
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);                                                                                                                            
281                                         
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);                                                                                                                        
287                                         
288                                         excepForm.ShowDialog();                                                 
289                                         showingException = false;
290                                 }                                                       
291                                 
292                         }
293                         
294                 }
295                 
296                 
297                 public static void RemoveMessageFilter (IMessageFilter value)
298                 {
299                         messageFilters.Remove (value);
300                 }
301
302                 static private void ApplicationFormClosed (object o, EventArgs args)
303                 {
304                         Win32.PostQuitMessage (0);
305                 }
306
307                 //Compact Framework
308                 static public void Run ()
309                 {
310                         MSG msg = new MSG();
311
312                         messageLoopStarted = true;
313
314
315                         while (!messageLoopStopRequest && 
316                                 Win32.GetMessageA (ref msg, 0, 0, 0) != 0) 
317                         {
318
319                                 bool dispatchMessage = true;
320
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;
326
327                                 IEnumerator e = messageFilters.GetEnumerator();
328
329                                 while (e.MoveNext()) 
330                                 {
331                                         IMessageFilter filter = 
332                                                 (IMessageFilter) e.Current;
333
334                                         // if PreFilterMessage returns true
335                                         // the message should not be dispatched
336                                         if (filter.PreFilterMessage (ref message))
337                                                 dispatchMessage = false;
338                                 }
339
340                                 Control receiver = Control.FromChildHandle ( message.HWnd );
341                                 if ( receiver != null ) 
342                                 {
343                                         dispatchMessage = ! receiver.PreProcessMessage ( ref message );
344                                 }
345
346                                 if (dispatchMessage) 
347                                 {
348                                         Win32.TranslateMessage (ref msg);
349                                         Win32.DispatchMessageA (ref msg);
350                                 }
351                                 //if (Idle != null)
352                                 //Idle (null, new EventArgs());
353                         }
354
355                         //if (ApplicationExit != null)
356                         //ApplicationExit (null, new EventArgs());
357                 }
358
359                 public static void Run (ApplicationContext context) 
360                 {
361                         applicationContext = context;
362                         applicationContext.MainForm.Show ();
363                         applicationContext.ThreadExit += new EventHandler( ApplicationFormClosed );
364                         //                      applicationContext.MainForm.Closed += //
365                         //                          new EventHandler (ApplicationFormClosed);
366                         Run();
367                 }
368
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.
373                 {
374                         mainForm.CreateControl ();
375                         ApplicationContext context = new ApplicationContext (
376                                 mainForm);
377                         Run (context);
378                 }
379                 
380                 internal static void enterModalLoop ( Form mainForm )
381                 {
382                         mainForm.ExitModalLoop = false;
383
384                         MSG msg = new MSG();
385                         while( Win32.GetMessageA( ref msg, 0, 0, 0 ) != 0 ) 
386                         {
387
388                                 if ( mainForm.ExitModalLoop )
389                                         break;
390
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;
396
397                                 Control receiver = Control.FromChildHandle ( message.HWnd );
398                                 if ( receiver != null )
399                                         if ( receiver.PreProcessMessage ( ref message ) )
400                                                 continue;
401
402                                 Win32.TranslateMessage (ref msg);
403                                 Win32.DispatchMessageA (ref msg);
404                         }
405                         
406                 }
407                 
408                 internal static void exitModalLoop ( Form mainForm )
409                 {
410                         mainForm.ExitModalLoop = true;
411
412                         Win32.PostMessage( IntPtr.Zero, Msg.WM_NULL, 0, 0 ); 
413                 }
414
415                 // --- Events ---
416                 public static event EventHandler ApplicationExit;
417                 public static event EventHandler Idle;
418                 public static event ThreadExceptionEventHandler ThreadException;
419                 public static event EventHandler ThreadExit;
420                 
421                 
422                 // StackButton
423                 internal class StackButton : System.Windows.Forms.Button{
424                                 
425                                 private Exception excep = null;                 
426                                 
427                                 public StackButton(Exception t) : base(){
428                                         excep = t;                                      
429                                 }                               
430                                 
431                                 protected override void OnClick(EventArgs e)    {       
432                                         MessageBox.Show(excep.StackTrace, "Stack Trace");
433                                 }
434                 }
435                 
436                 // QuitButton
437                 internal class QuitButton : System.Windows.Forms.Button{                                                                
438                                 
439                                 public QuitButton() : base(){}                          
440                                 
441                                 protected override void OnClick(EventArgs e)    {       
442                                         Application.ExitThread();
443                                         Application.Exit();
444                                 }
445                 }
446                 
447                 // ContinueButton
448                 internal class ContinueButton : System.Windows.Forms.Button{                                                            
449                 
450                                 private Form form = null;
451                                 
452                                 public ContinueButton(Form frm) : base(){form=frm;}                             
453                                 
454                                 protected override void OnClick(EventArgs e)    {       
455                                         form.Close();
456                                 }
457                 }
458
459
460         }
461 }
462