2007-08-28 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ThreadExceptionDialog.cs
1 //
2 // System.Windows.Forms.ThreadExceptionDialog.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Authors:
24 //              Marek Safar     marek.safar@seznam.cz
25 //
26 // Copyright (C) Novell Inc., 2004
27
28 // COMPLETE - BUT DISABLED TEXTBOX
29
30 using System;
31 using System.Text;
32 using System.Reflection;
33 using System.ComponentModel;
34 using System.Runtime.InteropServices;
35
36 namespace System.Windows.Forms
37 {
38 #if NET_2_0
39         [ComVisible (true)]
40         [ClassInterface (ClassInterfaceType.AutoDispatch)]
41 #endif
42         public class ThreadExceptionDialog: Form
43         {
44                 Exception e;
45                 bool details;
46
47                 private System.Windows.Forms.Button buttonIgnore;
48                 private System.Windows.Forms.Button buttonAbort;
49                 private System.Windows.Forms.Button buttonDetails;
50                 private System.Windows.Forms.Label labelException;
51                 private System.Windows.Forms.Label label1;
52                 private System.Windows.Forms.TextBox textBoxDetails;
53                 private System.Windows.Forms.Label helpText;
54
55                 private void InitializeComponent()
56                 {
57                         this.helpText = new System.Windows.Forms.Label();
58                         this.buttonAbort = new System.Windows.Forms.Button();
59                         this.buttonIgnore = new System.Windows.Forms.Button();
60                         this.buttonDetails = new System.Windows.Forms.Button();
61                         this.labelException = new System.Windows.Forms.Label();
62                         this.textBoxDetails = new System.Windows.Forms.TextBox();
63                         this.label1 = new System.Windows.Forms.Label();
64                         this.SuspendLayout();
65                         // 
66                         // helpText
67                         // 
68                         this.helpText.Location = new System.Drawing.Point(60, 8);
69                         this.helpText.Name = "helpText";
70                         this.helpText.Size = new System.Drawing.Size(356, 40);
71                         this.helpText.TabIndex = 0;
72                         this.helpText.Text = "An unhandled exception has occurred in you application. If you click Ignore the a" +
73                                 "pplication will ignore this error and attempt to continue. If you click Abort, t" +
74                                 "he application will quit immediately.";
75                         // 
76                         // buttonAbort
77                         // 
78                         this.buttonAbort.DialogResult = System.Windows.Forms.DialogResult.Abort;
79                         this.buttonAbort.Location = new System.Drawing.Point(332, 112);
80                         this.buttonAbort.Name = "buttonAbort";
81                         this.buttonAbort.Size = new System.Drawing.Size(85, 23);
82                         this.buttonAbort.TabIndex = 4;
83                         this.buttonAbort.Text = "&Abort";
84                         this.buttonAbort.Click += new System.EventHandler(this.buttonAbort_Click);
85                         // 
86                         // buttonIgnore
87                         // 
88                         this.buttonIgnore.DialogResult = System.Windows.Forms.DialogResult.Ignore;
89                         this.buttonIgnore.Location = new System.Drawing.Point(236, 112);
90                         this.buttonIgnore.Name = "buttonIgnore";
91                         this.buttonIgnore.Size = new System.Drawing.Size(85, 23);
92                         this.buttonIgnore.TabIndex = 3;
93                         this.buttonIgnore.Text = "&Ignore";
94                         // 
95                         // buttonDetails
96                         // 
97                         this.buttonDetails.Location = new System.Drawing.Point(140, 112);
98                         this.buttonDetails.Name = "buttonDetails";
99                         this.buttonDetails.Size = new System.Drawing.Size(85, 23);
100                         this.buttonDetails.TabIndex = 2;
101                         this.buttonDetails.Text = "Show &Details";
102                         this.buttonDetails.Click += new System.EventHandler(this.buttonDetails_Click);
103                         // 
104                         // labelException
105                         // 
106                         this.labelException.Location = new System.Drawing.Point(60, 64);
107                         this.labelException.Name = "labelException";
108                         this.labelException.Size = new System.Drawing.Size(356, 32);
109                         this.labelException.TabIndex = 1;
110                         // 
111                         // textBoxDetails
112                         // 
113                         this.textBoxDetails.Location = new System.Drawing.Point(8, 168);
114                         this.textBoxDetails.Multiline = true;
115                         this.textBoxDetails.Name = "textBoxDetails";
116                         this.textBoxDetails.ReadOnly = true;
117                         this.textBoxDetails.ScrollBars = System.Windows.Forms.ScrollBars.Both;
118                         this.textBoxDetails.Size = new System.Drawing.Size(408, 196);
119                         this.textBoxDetails.TabIndex = 5;
120                         this.textBoxDetails.TabStop = false;
121                         this.textBoxDetails.Text = "";
122                         this.textBoxDetails.WordWrap = false;
123                         // 
124                         // label1
125                         // 
126                         this.label1.Location = new System.Drawing.Point(8, 148);
127                         this.label1.Name = "label1";
128                         this.label1.Size = new System.Drawing.Size(100, 16);
129                         this.label1.TabIndex = 0;
130                         this.label1.Text = "Exception details";
131                         // 
132                         // ThreadExceptionDialog
133                         // 
134                         this.AcceptButton = this.buttonIgnore;
135                         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
136                         this.CancelButton = this.buttonAbort;
137                         this.ClientSize = new System.Drawing.Size(428, 374);
138                         this.Controls.Add(this.label1);
139                         this.Controls.Add(this.textBoxDetails);
140                         this.Controls.Add(this.labelException);
141                         this.Controls.Add(this.buttonDetails);
142                         this.Controls.Add(this.buttonIgnore);
143                         this.Controls.Add(this.buttonAbort);
144                         this.Controls.Add(this.helpText);
145                         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
146                         this.MaximizeBox = false;
147                         this.MinimizeBox = false;
148                         this.Name = "ThreadExceptionDialog";
149                         this.ShowInTaskbar = false;
150                         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
151                         this.TopMost = true;
152                         this.ResumeLayout(false);
153                 }
154         
155                 public ThreadExceptionDialog (Exception e)
156                 {
157                         this.e = e;
158                         InitializeComponent ();
159
160                         this.labelException.Text = e.Message;
161                         if (Form.ActiveForm != null)
162                                 this.Text = Form.ActiveForm.Text;
163                         else
164                                 this.Text = "Mono";
165                         this.buttonAbort.Enabled = Application.AllowQuit;
166                         RefreshDetails ();
167                         FillExceptionDetails ();
168                 }
169
170                 void buttonDetails_Click(object sender, System.EventArgs e)
171                 {
172                         details = !details;
173                         RefreshDetails ();
174                 }
175
176                 void FillExceptionDetails ()
177                 {
178                         StringBuilder sb = new StringBuilder ();
179                         sb.Append (e.ToString ());
180                         sb.Append (Environment.NewLine + Environment.NewLine);
181                         sb.Append ("Loaded assemblies:" + Environment.NewLine + Environment.NewLine);
182
183                         foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies ()) {
184                                 AssemblyName an = a.GetName ();
185                                 sb.AppendFormat ("Name:\t{0}" + Environment.NewLine, an.Name);
186                                 sb.AppendFormat ("Version:\t{0}" + Environment.NewLine, an.Version);
187                                 sb.AppendFormat ("Location:\t{0}" + Environment.NewLine, an.CodeBase);
188                                 sb.Append (Environment.NewLine);
189                         }
190                         textBoxDetails.Text = sb.ToString ();
191                 }
192
193                 void RefreshDetails ()
194                 {
195                         if (details) {
196                                 buttonDetails.Text = "Hide &Details";
197                                 Height = 410;
198                                 label1.Visible = true;
199                                 textBoxDetails.Visible = true;
200                                 return;
201                         }
202                         buttonDetails.Text = "Show &Details";
203                         label1.Visible = false;
204                         textBoxDetails.Visible = false;
205                         Height = 180;
206                 }
207
208                 void buttonAbort_Click(object sender, System.EventArgs e)
209                 {
210                         Application.Exit ();
211                 }
212
213 #if NET_2_0
214                 [Browsable (false)]
215                 [EditorBrowsable (EditorBrowsableState.Never)]
216                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
217                 public override bool AutoSize {
218                         get { return base.AutoSize; }
219                         set { base.AutoSize = value; }
220                 }
221
222                 [Browsable (false)]
223                 [EditorBrowsable (EditorBrowsableState.Never)]
224                 public new event EventHandler AutoSizeChanged {
225                         add { base.AutoSizeChanged += value; }
226                         remove { base.AutoSizeChanged -= value; }
227                 }
228 #endif
229         }
230 }