2
// MainWindowController.cs
5
// Aaron Bockover <abock@xamarin.com>
7
// Copyright 2012 Xamarin Inc. (http://xamarin.com)
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
using System.Collections.Generic;
32
using System.Reflection;
34
using MonoMac.Foundation;
37
namespace NSAlertSample
39
public partial class MainWindowController : MonoMac.AppKit.NSWindowController
43
// Called when created from unmanaged code
44
public MainWindowController (IntPtr handle) : base (handle)
49
// Called when created directly from a XIB file
50
[Export ("initWithCoder:")]
51
public MainWindowController (NSCoder coder) : base (coder)
56
// Call to load from the XIB/NIB file
57
public MainWindowController () : base ("MainWindow")
62
// Shared initialization code
66
NSRunLoop.Current.AddTimer (NSTimer.CreateRepeatingTimer (TimeSpan.FromSeconds (0.1), () => {
67
ModalCounter.StringValue = (timerCount++).ToString ();
68
}), NSRunLoopMode.Default);
73
//strongly typed window accessor
74
public new MainWindow Window {
76
return (MainWindow)base.Window;
80
void ShowResponse (NSAlert alert, int response)
87
message = String.Format ("Non-custom response: -1 (other)");
90
message = String.Format ("Non-custom response: 0 (alternate)");
93
message = String.Format ("Non-custom response: 1 (default)");
96
message = String.Format ("Unknown Response: {0}", response);
100
var buttonIndex = response - (int)NSAlertButtonReturn.First;
101
if (buttonIndex >= alert.Buttons.Length)
102
message = String.Format ("Unknown Response: {0}", response);
104
message = String.Format (
105
"\"{0}\"\n\nButton Index: {1}\nResult (NSAlertButtonReturn): {2}\nResult (int): {3}",
106
alert.Buttons [buttonIndex].Title,
108
(NSAlertButtonReturn)response,
112
if (alert.ShowsSuppressionButton)
113
message += String.Format ("\nSuppression: {0}", alert.SuppressionButton.State);
115
ResultLabel.StringValue = message;
118
void Run (NSAlert alert)
120
switch (AlertOptions.SelectedTag) {
122
alert.BeginSheetForResponse (Window, response => ShowResponse (alert, response));
125
ShowResponse (alert, alert.RunSheetModal (Window));
128
ShowResponse (alert, alert.RunModal ());
131
ResultLabel.StringValue = "Unknown Alert Option";
136
#region NSAlert Sample Implementations
138
partial void NSAlertWithMessage (NSObject sender)
140
Run (NSAlert.WithMessage ("Hello NSAlert", "Default", "Alternate", "Other", String.Empty));
143
partial void NSAlertWithError (NSObject sender)
145
Run (NSAlert.WithError (new NSError (new NSString ("org.mono-project.NSAlertSample"), 3000, null)));
148
partial void CustomButtons (NSObject sender)
150
var alert = new NSAlert {
151
MessageText = "Pick a Number!",
152
InformativeText = "Long description about why picking a number is important."
155
alert.AddButton ("One");
156
alert.AddButton ("Two");
157
alert.AddButton ("Three");
158
alert.AddButton ("Four");
159
alert.AddButton ("Five");
160
alert.AddButton ("Six");
165
partial void CustomImage (NSObject sender)
167
var alert = new NSAlert {
168
MessageText = "The cat that started it all!"
171
var asm = Assembly.GetExecutingAssembly ();
172
using (var stream = asm.GetManifestResourceStream ("NSAlertSample.i-can-has-cheezburger.jpg")) {
173
alert.Icon = NSImage.FromStream (stream);
176
alert.AddButton ("No Can Has");
181
partial void DefaultSuppression (NSObject sender)
183
var alert = new NSAlert {
184
MessageText = "Purchase More Gold!",
185
InformativeText = "Would you like to purchase 30 more pounds of gold?",
186
ShowsSuppressionButton = true
189
alert.AddButton ("Yes Please!");
190
alert.AddButton ("Absolutely Not");
195
partial void CustomSuppression (NSObject sender)
197
var alert = new NSAlert {
198
MessageText = "Subscribe to CatOverflow.com",
199
InformativeText = "CatOverflow.com features the best cats the Internet has to offer.\n\nUpdated regularly and curated by a professional cat analyist, CatOverflow.com cannot be missed. Make it part of your daily regimen now!\n",
200
ShowsSuppressionButton = true
203
alert.SuppressionButton.Title = "Go away forever, meow";
204
alert.SuppressionButton.Font = NSFont.ControlContentFontOfSize (NSFont.SmallSystemFontSize);
206
alert.AddButton ("YES YES YES");
207
alert.AddButton ("Remind me later");
208
alert.AddButton ("I prefer DogOverflow.com");