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

« back to all changes in this revision

Viewing changes to external/monomac/samples/NSAlert/MainWindowController.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
// MainWindowController.cs
 
3
//
 
4
// Author:
 
5
//   Aaron Bockover <abock@xamarin.com>
 
6
//
 
7
// Copyright 2012 Xamarin Inc. (http://xamarin.com)
 
8
//
 
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:
 
16
//
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
//
 
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.
 
27
//
 
28
 
 
29
using System;
 
30
using System.Collections.Generic;
 
31
using System.Linq;
 
32
using System.Reflection;
 
33
 
 
34
using MonoMac.Foundation;
 
35
using MonoMac.AppKit;
 
36
 
 
37
namespace NSAlertSample
 
38
{
 
39
        public partial class MainWindowController : MonoMac.AppKit.NSWindowController
 
40
        {
 
41
                #region Constructors
 
42
                
 
43
                // Called when created from unmanaged code
 
44
                public MainWindowController (IntPtr handle) : base (handle)
 
45
                {
 
46
                        Initialize ();
 
47
                }
 
48
                
 
49
                // Called when created directly from a XIB file
 
50
                [Export ("initWithCoder:")]
 
51
                public MainWindowController (NSCoder coder) : base (coder)
 
52
                {
 
53
                        Initialize ();
 
54
                }
 
55
                
 
56
                // Call to load from the XIB/NIB file
 
57
                public MainWindowController () : base ("MainWindow")
 
58
                {
 
59
                        Initialize ();
 
60
                }
 
61
                
 
62
                // Shared initialization code
 
63
                void Initialize ()
 
64
                {
 
65
                        var timerCount = 0;
 
66
                        NSRunLoop.Current.AddTimer (NSTimer.CreateRepeatingTimer (TimeSpan.FromSeconds (0.1), () => {
 
67
                                ModalCounter.StringValue = (timerCount++).ToString ();
 
68
                        }), NSRunLoopMode.Default);
 
69
                }
 
70
                
 
71
                #endregion
 
72
                
 
73
                //strongly typed window accessor
 
74
                public new MainWindow Window {
 
75
                        get {
 
76
                                return (MainWindow)base.Window;
 
77
                        }
 
78
                }
 
79
 
 
80
                void ShowResponse (NSAlert alert, int response)
 
81
                {
 
82
                        string message;
 
83
 
 
84
                        if (response <= 1) {
 
85
                                switch (response) {
 
86
                                case -1:
 
87
                                        message = String.Format ("Non-custom response: -1 (other)");
 
88
                                        break;
 
89
                                case 0:
 
90
                                        message = String.Format ("Non-custom response: 0 (alternate)");
 
91
                                        break;
 
92
                                case 1:
 
93
                                        message = String.Format ("Non-custom response: 1 (default)");
 
94
                                        break;
 
95
                                default:
 
96
                                        message = String.Format ("Unknown Response: {0}", response);
 
97
                                        break;
 
98
                                }
 
99
                        } else {
 
100
                                var buttonIndex = response - (int)NSAlertButtonReturn.First;
 
101
                                if (buttonIndex >= alert.Buttons.Length)
 
102
                                        message = String.Format ("Unknown Response: {0}", response);
 
103
                                else
 
104
                                        message = String.Format (
 
105
                                                "\"{0}\"\n\nButton Index: {1}\nResult (NSAlertButtonReturn): {2}\nResult (int): {3}",
 
106
                                                alert.Buttons [buttonIndex].Title,
 
107
                                                buttonIndex,
 
108
                                                (NSAlertButtonReturn)response,
 
109
                                                response);
 
110
                        }
 
111
 
 
112
                        if (alert.ShowsSuppressionButton)
 
113
                                message += String.Format ("\nSuppression: {0}", alert.SuppressionButton.State);
 
114
 
 
115
                        ResultLabel.StringValue = message;
 
116
                }
 
117
 
 
118
                void Run (NSAlert alert)
 
119
                {
 
120
                        switch (AlertOptions.SelectedTag) {
 
121
                        case 0:
 
122
                                alert.BeginSheetForResponse (Window, response => ShowResponse (alert, response));
 
123
                                break;
 
124
                        case 1:
 
125
                                ShowResponse (alert, alert.RunSheetModal (Window));
 
126
                                break;
 
127
                        case 2:
 
128
                                ShowResponse (alert, alert.RunModal ());
 
129
                                break;
 
130
                        default:
 
131
                                ResultLabel.StringValue = "Unknown Alert Option";
 
132
                                break;
 
133
                        }
 
134
                }
 
135
 
 
136
                #region NSAlert Sample Implementations
 
137
 
 
138
                partial void NSAlertWithMessage (NSObject sender)
 
139
                {
 
140
                        Run (NSAlert.WithMessage ("Hello NSAlert", "Default", "Alternate", "Other", String.Empty));
 
141
                }
 
142
 
 
143
                partial void NSAlertWithError (NSObject sender)
 
144
                {
 
145
                        Run (NSAlert.WithError (new NSError (new NSString ("org.mono-project.NSAlertSample"), 3000, null)));
 
146
                }
 
147
 
 
148
                partial void CustomButtons (NSObject sender)
 
149
                {
 
150
                        var alert = new NSAlert {
 
151
                                MessageText = "Pick a Number!",
 
152
                                InformativeText = "Long description about why picking a number is important."
 
153
                        };
 
154
                        
 
155
                        alert.AddButton ("One");
 
156
                        alert.AddButton ("Two");
 
157
                        alert.AddButton ("Three");
 
158
                        alert.AddButton ("Four");
 
159
                        alert.AddButton ("Five");
 
160
                        alert.AddButton ("Six");
 
161
 
 
162
                        Run (alert);
 
163
                }
 
164
 
 
165
                partial void CustomImage (NSObject sender)
 
166
                {
 
167
                        var alert = new NSAlert {
 
168
                                MessageText = "The cat that started it all!"
 
169
                        };
 
170
                        
 
171
                        var asm = Assembly.GetExecutingAssembly ();
 
172
                        using (var stream = asm.GetManifestResourceStream ("NSAlertSample.i-can-has-cheezburger.jpg")) {
 
173
                                alert.Icon = NSImage.FromStream (stream);
 
174
                        }
 
175
 
 
176
                        alert.AddButton ("No Can Has");
 
177
 
 
178
                        Run (alert);
 
179
                }
 
180
 
 
181
                partial void DefaultSuppression (NSObject sender)
 
182
                {
 
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
 
187
                        };
 
188
 
 
189
                        alert.AddButton ("Yes Please!");
 
190
                        alert.AddButton ("Absolutely Not");
 
191
 
 
192
                        Run (alert);
 
193
                }
 
194
 
 
195
                partial void CustomSuppression (NSObject sender)
 
196
                {
 
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
 
201
                        };
 
202
 
 
203
                        alert.SuppressionButton.Title = "Go away forever, meow";
 
204
                        alert.SuppressionButton.Font = NSFont.ControlContentFontOfSize (NSFont.SmallSystemFontSize);
 
205
                        
 
206
                        alert.AddButton ("YES YES YES");
 
207
                        alert.AddButton ("Remind me later");
 
208
                        alert.AddButton ("I prefer DogOverflow.com");
 
209
                        
 
210
                        Run (alert);
 
211
                }
 
212
 
 
213
                #endregion
 
214
        }
 
215
}