~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Main/ICSharpCode.Core.WinForms/MessageService/WinFormsMessageService.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.ComponentModel;
 
6
using System.Windows.Forms;
 
7
 
 
8
using ICSharpCode.Core.Services;
 
9
 
 
10
namespace ICSharpCode.Core.WinForms
 
11
{
 
12
        /// <summary>
 
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.
 
16
        /// </summary>
 
17
        public class WinFormsMessageService : IDialogMessageService
 
18
        {
 
19
                /// <summary>
 
20
                /// Gets/Sets the form used as owner for shown message boxes.
 
21
                /// </summary>
 
22
                public IWin32Window DialogOwner { get; set; }
 
23
                
 
24
                /// <summary>
 
25
                /// Gets/Sets the object used to synchronize message boxes shown on other threads.
 
26
                /// </summary>
 
27
                public ISynchronizeInvoke DialogSynchronizeInvoke { get; set; }
 
28
                
 
29
                void BeginInvoke(MethodInvoker method)
 
30
                {
 
31
                        ISynchronizeInvoke si = DialogSynchronizeInvoke;
 
32
                        if (si == null || !si.InvokeRequired)
 
33
                                method();
 
34
                        else
 
35
                                si.BeginInvoke(method, null);
 
36
                }
 
37
                
 
38
                void Invoke(MethodInvoker method)
 
39
                {
 
40
                        ISynchronizeInvoke si = DialogSynchronizeInvoke;
 
41
                        if (si == null || !si.InvokeRequired)
 
42
                                method();
 
43
                        else
 
44
                                si.Invoke(method, null);
 
45
                }
 
46
                
 
47
                public virtual void ShowException(Exception ex, string message)
 
48
                {
 
49
                        if (ex != null) {
 
50
                                message += "\n\nException occurred: " + ex.ToString();
 
51
                        }
 
52
                        ShowError(message);
 
53
                }
 
54
                
 
55
                public void ShowError(string message)
 
56
                {
 
57
                        message = StringParser.Parse(message);
 
58
                        
 
59
                        string caption = StringParser.Parse("${res:Global.ErrorText}");
 
60
                        BeginInvoke(
 
61
                                delegate {
 
62
                                        MessageBox.Show(DialogOwner,
 
63
                                                        message, caption,
 
64
                                                        MessageBoxButtons.OK, MessageBoxIcon.Warning,
 
65
                                                        MessageBoxDefaultButton.Button1, GetOptions(message, caption));
 
66
                                });
 
67
                }
 
68
                
 
69
                public void ShowWarning(string message)
 
70
                {
 
71
                        message = StringParser.Parse(message);
 
72
                        
 
73
                        string caption = StringParser.Parse("${res:Global.WarningText}");
 
74
                        BeginInvoke(
 
75
                                delegate {
 
76
                                        MessageBox.Show(DialogOwner,
 
77
                                                        message, caption,
 
78
                                                        MessageBoxButtons.OK, MessageBoxIcon.Warning,
 
79
                                                        MessageBoxDefaultButton.Button1, GetOptions(message, caption));
 
80
                                });
 
81
                }
 
82
                
 
83
                public bool AskQuestion(string question, string caption)
 
84
                {
 
85
                        DialogResult result = DialogResult.None;
 
86
                        Invoke(
 
87
                                delegate {
 
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));
 
95
                                });
 
96
                        return result == DialogResult.Yes;
 
97
                }
 
98
                
 
99
                static MessageBoxOptions GetOptions(string text, string caption)
 
100
                {
 
101
                        return IsRtlText(text) ? MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign : 0;
 
102
                }
 
103
                
 
104
                static bool IsRtlText(string text)
 
105
                {
 
106
                        if (!RightToLeftConverter.IsRightToLeft)
 
107
                                return false;
 
108
                        foreach (char c in StringParser.Parse(text)) {
 
109
                                if (char.GetUnicodeCategory(c) == System.Globalization.UnicodeCategory.OtherLetter)
 
110
                                        return true;
 
111
                        }
 
112
                        return false;
 
113
                }
 
114
                
 
115
                public int ShowCustomDialog(string caption, string dialogText, int acceptButtonIndex, int cancelButtonIndex, params string[] buttontexts)
 
116
                {
 
117
                        int result = 0;
 
118
                        Invoke(
 
119
                                delegate {
 
120
                                        using (CustomDialog messageBox = new CustomDialog(caption, dialogText, acceptButtonIndex, cancelButtonIndex, buttontexts)) {
 
121
                                                messageBox.ShowDialog(DialogOwner);
 
122
                                                result = messageBox.Result;
 
123
                                        }
 
124
                                });
 
125
                        return result;
 
126
                }
 
127
                
 
128
                public string ShowInputBox(string caption, string dialogText, string defaultValue)
 
129
                {
 
130
                        string result = null;
 
131
                        Invoke(
 
132
                                delegate {
 
133
                                        using (InputBox inputBox = new InputBox(dialogText, caption, defaultValue)) {
 
134
                                                inputBox.ShowDialog(DialogOwner);
 
135
                                                result = inputBox.Result;
 
136
                                        }
 
137
                                });
 
138
                        return result;
 
139
                }
 
140
                
 
141
                public void ShowMessage(string message, string caption)
 
142
                {
 
143
                        message = StringParser.Parse(message);
 
144
                        BeginInvoke(
 
145
                                delegate {
 
146
                                        MessageBox.Show(DialogOwner,
 
147
                                                        message,
 
148
                                                        StringParser.Parse(caption),
 
149
                                                        MessageBoxButtons.OK,
 
150
                                                        MessageBoxIcon.Information,
 
151
                                                        MessageBoxDefaultButton.Button1,
 
152
                                                        GetOptions(message, caption));
 
153
                                });
 
154
                }
 
155
                
 
156
                public void InformSaveError(string fileName, string message, string dialogName, Exception exceptionGot)
 
157
                {
 
158
                        BeginInvoke(
 
159
                                delegate {
 
160
                                        using (SaveErrorInformDialog dlg = new SaveErrorInformDialog(fileName, message, dialogName, exceptionGot)) {
 
161
                                                dlg.ShowDialog(DialogOwner);
 
162
                                        }
 
163
                                });
 
164
                }
 
165
                
 
166
                public ChooseSaveErrorResult ChooseSaveError(string fileName, string message, string dialogName, Exception exceptionGot, bool chooseLocationEnabled)
 
167
                {
 
168
                        ChooseSaveErrorResult r = ChooseSaveErrorResult.Ignore;
 
169
                        Invoke(
 
170
                                delegate {
 
171
                                restartlabel:
 
172
                                        using (SaveErrorChooseDialog dlg = new SaveErrorChooseDialog(fileName, message, dialogName, exceptionGot, chooseLocationEnabled)) {
 
173
                                                switch (dlg.ShowDialog(DialogOwner)) {
 
174
                                                        case DialogResult.OK:
 
175
                                                                // choose location:
 
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);
 
185
                                                                                break;
 
186
                                                                        } else {
 
187
                                                                                goto restartlabel;
 
188
                                                                        }
 
189
                                                                }
 
190
                                                        case DialogResult.Retry:
 
191
                                                                r = ChooseSaveErrorResult.Retry;
 
192
                                                                break;
 
193
                                                        default:
 
194
                                                                r = ChooseSaveErrorResult.Ignore;
 
195
                                                                break;
 
196
                                                }
 
197
                                        }
 
198
                                });
 
199
                        return r;
 
200
                }
 
201
        }
 
202
}