~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt/MessageDialog.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// MessageDialog.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.Collections.Generic;
 
28
using Xwt.Backends;
 
29
using Xwt.Engine;
 
30
 
 
31
namespace Xwt
 
32
{
 
33
        public static class MessageDialog
 
34
        {
 
35
                public static WindowFrame RootWindow { get; set; }
 
36
                
 
37
                #region ShowError
 
38
                public static void ShowError (string primaryText)
 
39
                {
 
40
                        ShowError (RootWindow, primaryText);
 
41
                }
 
42
                public static void ShowError (WindowFrame parent, string primaryText)
 
43
                {
 
44
                        ShowError (parent, primaryText, null);
 
45
                }
 
46
                public static void ShowError (string primaryText, string secondaryText)
 
47
                {
 
48
                        ShowError (RootWindow, primaryText, secondaryText);
 
49
                }
 
50
                public static void ShowError (WindowFrame parent, string primaryText, string secondaryText)
 
51
                {
 
52
                        GenericAlert (parent, StockIcons.Error, primaryText, secondaryText, Command.Ok);
 
53
                }
 
54
                #endregion
 
55
                
 
56
                #region ShowWarning
 
57
                public static void ShowWarning (string primaryText)
 
58
                {
 
59
                        ShowWarning (RootWindow, primaryText);
 
60
                }
 
61
                public static void ShowWarning (WindowFrame parent, string primaryText)
 
62
                {
 
63
                        ShowWarning (parent, primaryText, null);
 
64
                }
 
65
                public static void ShowWarning (string primaryText, string secondaryText)
 
66
                {
 
67
                        ShowWarning (RootWindow, primaryText, secondaryText);
 
68
                }
 
69
                public static void ShowWarning (WindowFrame parent, string primaryText, string secondaryText)
 
70
                {
 
71
                        GenericAlert (parent, StockIcons.Warning, primaryText, secondaryText, Command.Ok);
 
72
                }
 
73
                #endregion
 
74
                
 
75
                
 
76
                #region ShowMessage
 
77
                public static void ShowMessage (string primaryText)
 
78
                {
 
79
                        ShowMessage (RootWindow, primaryText);
 
80
                }
 
81
                public static void ShowMessage (WindowFrame parent, string primaryText)
 
82
                {
 
83
                        ShowMessage (parent, primaryText, null);
 
84
                }
 
85
                public static void ShowMessage (string primaryText, string secondaryText)
 
86
                {
 
87
                        ShowMessage (RootWindow, primaryText, secondaryText);
 
88
                }
 
89
                public static void ShowMessage (WindowFrame parent, string primaryText, string secondaryText)
 
90
                {
 
91
                        GenericAlert (parent, StockIcons.Information, primaryText, secondaryText, Command.Ok);
 
92
                }
 
93
                #endregion
 
94
                
 
95
                #region Confirm
 
96
                public static bool Confirm (string primaryText, Command button)
 
97
                {
 
98
                        return Confirm (primaryText, null, button);
 
99
                }
 
100
                
 
101
                public static bool Confirm (string primaryText, string secondaryText, Command button)
 
102
                {
 
103
                        return GenericAlert (RootWindow, StockIcons.Question, primaryText, secondaryText, Command.Cancel, button) == button;
 
104
                }
 
105
                public static bool Confirm (string primaryText, Command button, bool confirmIsDefault)
 
106
                {
 
107
                        return Confirm (primaryText, null, button, confirmIsDefault);
 
108
                }
 
109
                
 
110
                public static bool Confirm (string primaryText, string secondaryText, Command button, bool confirmIsDefault)
 
111
                {
 
112
                        return GenericAlert (RootWindow, StockIcons.Question, primaryText, secondaryText, confirmIsDefault ? 0 : 1, Command.Cancel, button) == button;
 
113
                }
 
114
                
 
115
                public static bool Confirm (ConfirmationMessage message)
 
116
                {
 
117
                        return GenericAlert (RootWindow, message) == message.ConfirmButton;
 
118
                }
 
119
                #endregion
 
120
                
 
121
                #region AskQuestion
 
122
                public static Command AskQuestion (string primaryText, params Command[] buttons)
 
123
                {
 
124
                        return AskQuestion (primaryText, null, buttons);
 
125
                }
 
126
                
 
127
                public static Command AskQuestion (string primaryText, string secondaryText, params Command[] buttons)
 
128
                {
 
129
                        return GenericAlert (RootWindow, StockIcons.Question, primaryText, secondaryText, buttons);
 
130
                }
 
131
                public static Command AskQuestion (string primaryText, int defaultButton, params Command[] buttons)
 
132
                {
 
133
                        return AskQuestion (primaryText, null, defaultButton, buttons);
 
134
                }
 
135
                
 
136
                public static Command AskQuestion (string primaryText, string secondaryText, int defaultButton, params Command[] buttons)
 
137
                {
 
138
                        return GenericAlert (RootWindow, StockIcons.Question, primaryText, secondaryText, defaultButton, buttons);
 
139
                }
 
140
                
 
141
                public static Command AskQuestion (QuestionMessage message)
 
142
                {
 
143
                        return GenericAlert (RootWindow, message);
 
144
                }
 
145
                #endregion
 
146
                
 
147
                static Command GenericAlert (WindowFrame parent, string icon, string primaryText, string secondaryText, params Command[] buttons)
 
148
                {
 
149
                        return GenericAlert (parent, icon, primaryText, secondaryText, buttons.Length - 1, buttons);
 
150
                }
 
151
                
 
152
                static Command GenericAlert (WindowFrame parent, string icon, string primaryText, string secondaryText, int defaultButton, params Command[] buttons)
 
153
                {
 
154
                        GenericMessage message = new GenericMessage () {
 
155
                                Icon = icon,
 
156
                                Text = primaryText,
 
157
                                SecondaryText = secondaryText,
 
158
                                DefaultButton = defaultButton
 
159
                        };
 
160
                        foreach (Command but in buttons)
 
161
                                message.Buttons.Add (but);
 
162
                        
 
163
                        return GenericAlert (parent, message);
 
164
                }
 
165
                
 
166
                static Command GenericAlert (WindowFrame parent, MessageDescription message)
 
167
                {
 
168
                        if (message.ApplyToAllButton != null)
 
169
                                return message.ApplyToAllButton;
 
170
                        
 
171
                        IAlertDialogBackend backend = WidgetRegistry.CreateBackend<IAlertDialogBackend> (typeof(IAlertDialogBackend));
 
172
                        
 
173
                        using (backend) {
 
174
                                var res = backend.Run (parent ?? RootWindow, message);
 
175
                                
 
176
                                if (backend.ApplyToAll)
 
177
                                        message.ApplyToAllButton = res;
 
178
                                
 
179
                                return res;
 
180
                        }
 
181
                }
 
182
        }
 
183
        
 
184
        public class MessageDescription
 
185
        {
 
186
                internal MessageDescription ()
 
187
                {
 
188
                        DefaultButton = -1;
 
189
                        Buttons = new List<Command> ();
 
190
                        Options = new List<AlertOption> ();
 
191
                }
 
192
                
 
193
                public IList<Command> Buttons { get; private set; }
 
194
                public IList<AlertOption> Options { get; private set; }
 
195
                
 
196
                internal Command ApplyToAllButton { get; set; }
 
197
                
 
198
                public string Icon { get; set; }
 
199
                
 
200
                public string Text { get; set; }
 
201
                public string SecondaryText { get; set; }
 
202
                public bool AllowApplyToAll { get; set; }
 
203
                public int DefaultButton { get; set; }
 
204
                
 
205
                public void AddOption (string id, string text, bool setByDefault)
 
206
                {
 
207
                        Options.Add (new AlertOption (id, text) { Value = setByDefault });
 
208
                }
 
209
                
 
210
                public bool GetOptionValue (string id)
 
211
                {
 
212
                        foreach (var op in Options)
 
213
                                if (op.Id == id)
 
214
                                        return op.Value;
 
215
                        throw new ArgumentException ("Invalid option id");
 
216
                }
 
217
                
 
218
                public void SetOptionValue (string id, bool value)
 
219
                {
 
220
                        foreach (var op in Options) {
 
221
                                if (op.Id == id) {
 
222
                                        op.Value = value;
 
223
                                        return;
 
224
                                }
 
225
                        }
 
226
                        throw new ArgumentException ("Invalid option id");
 
227
                }
 
228
        }
 
229
        
 
230
        public class AlertOption
 
231
        {
 
232
                internal AlertOption (string id, string text)
 
233
                {
 
234
                        this.Id = id;
 
235
                        this.Text = text;
 
236
                }
 
237
 
 
238
                public string Id { get; private set; }
 
239
                public string Text { get; private set; }
 
240
                public bool Value { get; set; }
 
241
        }
 
242
        
 
243
        public sealed class GenericMessage: MessageDescription
 
244
        {
 
245
                public GenericMessage ()
 
246
                {
 
247
                }
 
248
                
 
249
                public GenericMessage (string text)
 
250
                {
 
251
                        Text = text;
 
252
                }
 
253
                
 
254
                public GenericMessage (string text, string secondaryText): this (text)
 
255
                {
 
256
                        SecondaryText = secondaryText;
 
257
                }
 
258
                
 
259
                public new IList<Command> Buttons {
 
260
                        get { return base.Buttons; }
 
261
                }
 
262
        }
 
263
        
 
264
        
 
265
        public sealed class QuestionMessage: MessageDescription
 
266
        {
 
267
                public QuestionMessage ()
 
268
                {
 
269
                        Icon = StockIcons.Question;
 
270
                }
 
271
                
 
272
                public QuestionMessage (string text): this ()
 
273
                {
 
274
                        Text = text;
 
275
                }
 
276
                
 
277
                public QuestionMessage (string text, string secondaryText): this (text)
 
278
                {
 
279
                        SecondaryText = secondaryText;
 
280
                }
 
281
                
 
282
                public new IList<Command> Buttons {
 
283
                        get { return base.Buttons; }
 
284
                }
 
285
        }
 
286
        
 
287
        public sealed class ConfirmationMessage: MessageDescription
 
288
        {
 
289
                Command confirmButton;
 
290
                
 
291
                public ConfirmationMessage ()
 
292
                {
 
293
                        Icon = StockIcons.Question;
 
294
                        Buttons.Add (Command.Cancel);
 
295
                }
 
296
                
 
297
                public ConfirmationMessage (Command button): this ()
 
298
                {
 
299
                        ConfirmButton = button;
 
300
                }
 
301
                
 
302
                public ConfirmationMessage (string primaryText, Command button): this (button)
 
303
                {
 
304
                        Text = primaryText;
 
305
                }
 
306
                
 
307
                public ConfirmationMessage (string primaryText, string secondaryText, Command button): this (primaryText, button)
 
308
                {
 
309
                        SecondaryText = secondaryText;
 
310
                }
 
311
                
 
312
                public Command ConfirmButton {
 
313
                        get { return confirmButton; }
 
314
                        set {
 
315
                                if (Buttons.Count == 2)
 
316
                                        Buttons.RemoveAt (1);
 
317
                                Buttons.Add (value);
 
318
                                confirmButton = value;
 
319
                        }
 
320
                }
 
321
                
 
322
                public bool ConfirmIsDefault {
 
323
                        get {
 
324
                                return DefaultButton == 1;
 
325
                        }
 
326
                        set {
 
327
                                if (value)
 
328
                                        DefaultButton = 1;
 
329
                                else
 
330
                                        DefaultButton = 0;
 
331
                        }
 
332
                }
 
333
        }
 
334
}
 
335