5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
27
using System.Collections.Generic;
33
public static class MessageDialog
35
public static WindowFrame RootWindow { get; set; }
38
public static void ShowError (string primaryText)
40
ShowError (RootWindow, primaryText);
42
public static void ShowError (WindowFrame parent, string primaryText)
44
ShowError (parent, primaryText, null);
46
public static void ShowError (string primaryText, string secondaryText)
48
ShowError (RootWindow, primaryText, secondaryText);
50
public static void ShowError (WindowFrame parent, string primaryText, string secondaryText)
52
GenericAlert (parent, StockIcons.Error, primaryText, secondaryText, Command.Ok);
57
public static void ShowWarning (string primaryText)
59
ShowWarning (RootWindow, primaryText);
61
public static void ShowWarning (WindowFrame parent, string primaryText)
63
ShowWarning (parent, primaryText, null);
65
public static void ShowWarning (string primaryText, string secondaryText)
67
ShowWarning (RootWindow, primaryText, secondaryText);
69
public static void ShowWarning (WindowFrame parent, string primaryText, string secondaryText)
71
GenericAlert (parent, StockIcons.Warning, primaryText, secondaryText, Command.Ok);
77
public static void ShowMessage (string primaryText)
79
ShowMessage (RootWindow, primaryText);
81
public static void ShowMessage (WindowFrame parent, string primaryText)
83
ShowMessage (parent, primaryText, null);
85
public static void ShowMessage (string primaryText, string secondaryText)
87
ShowMessage (RootWindow, primaryText, secondaryText);
89
public static void ShowMessage (WindowFrame parent, string primaryText, string secondaryText)
91
GenericAlert (parent, StockIcons.Information, primaryText, secondaryText, Command.Ok);
96
public static bool Confirm (string primaryText, Command button)
98
return Confirm (primaryText, null, button);
101
public static bool Confirm (string primaryText, string secondaryText, Command button)
103
return GenericAlert (RootWindow, StockIcons.Question, primaryText, secondaryText, Command.Cancel, button) == button;
105
public static bool Confirm (string primaryText, Command button, bool confirmIsDefault)
107
return Confirm (primaryText, null, button, confirmIsDefault);
110
public static bool Confirm (string primaryText, string secondaryText, Command button, bool confirmIsDefault)
112
return GenericAlert (RootWindow, StockIcons.Question, primaryText, secondaryText, confirmIsDefault ? 0 : 1, Command.Cancel, button) == button;
115
public static bool Confirm (ConfirmationMessage message)
117
return GenericAlert (RootWindow, message) == message.ConfirmButton;
122
public static Command AskQuestion (string primaryText, params Command[] buttons)
124
return AskQuestion (primaryText, null, buttons);
127
public static Command AskQuestion (string primaryText, string secondaryText, params Command[] buttons)
129
return GenericAlert (RootWindow, StockIcons.Question, primaryText, secondaryText, buttons);
131
public static Command AskQuestion (string primaryText, int defaultButton, params Command[] buttons)
133
return AskQuestion (primaryText, null, defaultButton, buttons);
136
public static Command AskQuestion (string primaryText, string secondaryText, int defaultButton, params Command[] buttons)
138
return GenericAlert (RootWindow, StockIcons.Question, primaryText, secondaryText, defaultButton, buttons);
141
public static Command AskQuestion (QuestionMessage message)
143
return GenericAlert (RootWindow, message);
147
static Command GenericAlert (WindowFrame parent, string icon, string primaryText, string secondaryText, params Command[] buttons)
149
return GenericAlert (parent, icon, primaryText, secondaryText, buttons.Length - 1, buttons);
152
static Command GenericAlert (WindowFrame parent, string icon, string primaryText, string secondaryText, int defaultButton, params Command[] buttons)
154
GenericMessage message = new GenericMessage () {
157
SecondaryText = secondaryText,
158
DefaultButton = defaultButton
160
foreach (Command but in buttons)
161
message.Buttons.Add (but);
163
return GenericAlert (parent, message);
166
static Command GenericAlert (WindowFrame parent, MessageDescription message)
168
if (message.ApplyToAllButton != null)
169
return message.ApplyToAllButton;
171
IAlertDialogBackend backend = WidgetRegistry.CreateBackend<IAlertDialogBackend> (typeof(IAlertDialogBackend));
174
var res = backend.Run (parent ?? RootWindow, message);
176
if (backend.ApplyToAll)
177
message.ApplyToAllButton = res;
184
public class MessageDescription
186
internal MessageDescription ()
189
Buttons = new List<Command> ();
190
Options = new List<AlertOption> ();
193
public IList<Command> Buttons { get; private set; }
194
public IList<AlertOption> Options { get; private set; }
196
internal Command ApplyToAllButton { get; set; }
198
public string Icon { get; set; }
200
public string Text { get; set; }
201
public string SecondaryText { get; set; }
202
public bool AllowApplyToAll { get; set; }
203
public int DefaultButton { get; set; }
205
public void AddOption (string id, string text, bool setByDefault)
207
Options.Add (new AlertOption (id, text) { Value = setByDefault });
210
public bool GetOptionValue (string id)
212
foreach (var op in Options)
215
throw new ArgumentException ("Invalid option id");
218
public void SetOptionValue (string id, bool value)
220
foreach (var op in Options) {
226
throw new ArgumentException ("Invalid option id");
230
public class AlertOption
232
internal AlertOption (string id, string text)
238
public string Id { get; private set; }
239
public string Text { get; private set; }
240
public bool Value { get; set; }
243
public sealed class GenericMessage: MessageDescription
245
public GenericMessage ()
249
public GenericMessage (string text)
254
public GenericMessage (string text, string secondaryText): this (text)
256
SecondaryText = secondaryText;
259
public new IList<Command> Buttons {
260
get { return base.Buttons; }
265
public sealed class QuestionMessage: MessageDescription
267
public QuestionMessage ()
269
Icon = StockIcons.Question;
272
public QuestionMessage (string text): this ()
277
public QuestionMessage (string text, string secondaryText): this (text)
279
SecondaryText = secondaryText;
282
public new IList<Command> Buttons {
283
get { return base.Buttons; }
287
public sealed class ConfirmationMessage: MessageDescription
289
Command confirmButton;
291
public ConfirmationMessage ()
293
Icon = StockIcons.Question;
294
Buttons.Add (Command.Cancel);
297
public ConfirmationMessage (Command button): this ()
299
ConfirmButton = button;
302
public ConfirmationMessage (string primaryText, Command button): this (button)
307
public ConfirmationMessage (string primaryText, string secondaryText, Command button): this (primaryText, button)
309
SecondaryText = secondaryText;
312
public Command ConfirmButton {
313
get { return confirmButton; }
315
if (Buttons.Count == 2)
316
Buttons.RemoveAt (1);
318
confirmButton = value;
322
public bool ConfirmIsDefault {
324
return DefaultButton == 1;