1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.ComponentModel;
6
using System.Windows.Forms;
8
using ICSharpCode.Core.Services;
10
namespace ICSharpCode.Core.WinForms
13
/// Class with static methods to show message boxes.
14
/// All text displayed using the MessageService is passed to the
15
/// <see cref="StringParser"/> to replace ${res} markers.
17
public class WinFormsMessageService : IDialogMessageService
20
/// Gets/Sets the form used as owner for shown message boxes.
22
public IWin32Window DialogOwner { get; set; }
25
/// Gets/Sets the object used to synchronize message boxes shown on other threads.
27
public ISynchronizeInvoke DialogSynchronizeInvoke { get; set; }
29
void BeginInvoke(MethodInvoker method)
31
ISynchronizeInvoke si = DialogSynchronizeInvoke;
32
if (si == null || !si.InvokeRequired)
35
si.BeginInvoke(method, null);
38
void Invoke(MethodInvoker method)
40
ISynchronizeInvoke si = DialogSynchronizeInvoke;
41
if (si == null || !si.InvokeRequired)
44
si.Invoke(method, null);
47
public virtual void ShowException(Exception ex, string message)
50
message += "\n\nException occurred: " + ex.ToString();
55
public void ShowError(string message)
57
message = StringParser.Parse(message);
59
string caption = StringParser.Parse("${res:Global.ErrorText}");
62
MessageBox.Show(DialogOwner,
64
MessageBoxButtons.OK, MessageBoxIcon.Warning,
65
MessageBoxDefaultButton.Button1, GetOptions(message, caption));
69
public void ShowWarning(string message)
71
message = StringParser.Parse(message);
73
string caption = StringParser.Parse("${res:Global.WarningText}");
76
MessageBox.Show(DialogOwner,
78
MessageBoxButtons.OK, MessageBoxIcon.Warning,
79
MessageBoxDefaultButton.Button1, GetOptions(message, caption));
83
public bool AskQuestion(string question, string caption)
85
DialogResult result = DialogResult.None;
88
result = MessageBox.Show(DialogOwner,
89
StringParser.Parse(question),
90
StringParser.Parse(caption),
91
MessageBoxButtons.YesNo,
92
MessageBoxIcon.Question,
93
MessageBoxDefaultButton.Button1,
94
GetOptions(question, caption));
96
return result == DialogResult.Yes;
99
static MessageBoxOptions GetOptions(string text, string caption)
101
return IsRtlText(text) ? MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign : 0;
104
static bool IsRtlText(string text)
106
if (!RightToLeftConverter.IsRightToLeft)
108
foreach (char c in StringParser.Parse(text)) {
109
if (char.GetUnicodeCategory(c) == System.Globalization.UnicodeCategory.OtherLetter)
115
public int ShowCustomDialog(string caption, string dialogText, int acceptButtonIndex, int cancelButtonIndex, params string[] buttontexts)
120
using (CustomDialog messageBox = new CustomDialog(caption, dialogText, acceptButtonIndex, cancelButtonIndex, buttontexts)) {
121
messageBox.ShowDialog(DialogOwner);
122
result = messageBox.Result;
128
public string ShowInputBox(string caption, string dialogText, string defaultValue)
130
string result = null;
133
using (InputBox inputBox = new InputBox(dialogText, caption, defaultValue)) {
134
inputBox.ShowDialog(DialogOwner);
135
result = inputBox.Result;
141
public void ShowMessage(string message, string caption)
143
message = StringParser.Parse(message);
146
MessageBox.Show(DialogOwner,
148
StringParser.Parse(caption),
149
MessageBoxButtons.OK,
150
MessageBoxIcon.Information,
151
MessageBoxDefaultButton.Button1,
152
GetOptions(message, caption));
156
public void InformSaveError(string fileName, string message, string dialogName, Exception exceptionGot)
160
using (SaveErrorInformDialog dlg = new SaveErrorInformDialog(fileName, message, dialogName, exceptionGot)) {
161
dlg.ShowDialog(DialogOwner);
166
public ChooseSaveErrorResult ChooseSaveError(string fileName, string message, string dialogName, Exception exceptionGot, bool chooseLocationEnabled)
168
ChooseSaveErrorResult r = ChooseSaveErrorResult.Ignore;
172
using (SaveErrorChooseDialog dlg = new SaveErrorChooseDialog(fileName, message, dialogName, exceptionGot, chooseLocationEnabled)) {
173
switch (dlg.ShowDialog(DialogOwner)) {
174
case DialogResult.OK:
176
using (SaveFileDialog fdiag = new SaveFileDialog()) {
177
fdiag.OverwritePrompt = true;
178
fdiag.AddExtension = true;
179
fdiag.CheckFileExists = false;
180
fdiag.CheckPathExists = true;
181
fdiag.Title = "Choose alternate file name";
182
fdiag.FileName = fileName;
183
if (fdiag.ShowDialog() == DialogResult.OK) {
184
r = ChooseSaveErrorResult.SaveAlternative(fdiag.FileName);
190
case DialogResult.Retry:
191
r = ChooseSaveErrorResult.Retry;
194
r = ChooseSaveErrorResult.Ignore;