using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace Foundations
{
///
/// Shows the report of a failure that ends the program, for a person to copy. It uses nothing of the game's own
/// drawing, which may be what failed.
///
public class CrashDialog : Form
{
public const string CopyCaption = "Copy report";
public const string CopiedCaption = "Copied";
public const string CopyFailedCaption = "Couldn't copy";
private readonly Action setClipboard;
public readonly TextBox ReportBox;
public readonly Button CopyButton;
public readonly Button QuitButton;
/// Puts text on the clipboard, or null to use the system's.
public CrashDialog(string report, Action setClipboard = null)
{
if (report == null)
{
throw new ArgumentNullException(nameof(report));
}
this.setClipboard = setClipboard ?? Clipboard.SetText;
Text = "Something went wrong";
StartPosition = FormStartPosition.CenterScreen;
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(760, 480);
MinimizeBox = false;
// A text box breaks lines only at the system's own line ending.
ReportBox = new TextBox
{
Multiline = true,
ReadOnly = true,
WordWrap = false,
ScrollBars = ScrollBars.Both,
Dock = DockStyle.Fill,
Font = new Font(FontFamily.GenericMonospace, 9),
MaxLength = 0,
Text = report.Replace("\r\n", "\n").Replace("\n", Environment.NewLine),
TabIndex = 2,
};
var explanation = new Label
{
Text = "The program hit an error it can't carry on from. Please copy the report below and send it " +
"to the developer.",
Dock = DockStyle.Top,
Height = 40,
TextAlign = ContentAlignment.MiddleLeft,
};
CopyButton = new Button { Text = CopyCaption, AutoSize = true, TabIndex = 0 };
CopyButton.Click += (sender, clicked) => Copy();
QuitButton = new Button { Text = "Quit", AutoSize = true, TabIndex = 1 };
QuitButton.Click += (sender, clicked) => Close();
var buttons = new FlowLayoutPanel { Dock = DockStyle.Bottom, AutoSize = true };
buttons.Controls.Add(CopyButton);
buttons.Controls.Add(QuitButton);
// Docking goes from the last added to the first, so the one that fills goes in first.
Controls.Add(ReportBox);
Controls.Add(explanation);
Controls.Add(buttons);
}
///
/// Puts the report on the clipboard, and says on the copy button whether that worked.
///
public void Copy()
{
try
{
setClipboard(ReportBox.Text);
CopyButton.Text = CopiedCaption;
}
catch (ExternalException)
{
// Another program is holding the clipboard.
CopyButton.Text = CopyFailedCaption;
}
}
///
/// Runs on a thread that can use the clipboard, which is this one or a new one, and
/// returns once it has run.
///
public static void RunOnStaThread(Action action)
{
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
{
action();
return;
}
var thread = new Thread(() => action());
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
///
/// Shows from any thread, and returns once the dialog has been closed.
///
public static void ShowOnAnyThread(string report)
{
RunOnStaThread(() =>
{
using (var dialog = new CrashDialog(report))
{
dialog.ShowDialog();
}
});
}
}
}