~ubuntu-branches/ubuntu/saucy/monodevelop/saucy-proposed

« back to all changes in this revision

Viewing changes to src/addins/MacPlatform/Dialogs/MacExceptionDialogHandler.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-02-05 10:49:36 UTC
  • mto: (10.3.1)
  • mto: This revision was merged to the branch mainline in revision 25.
  • Revision ID: package-import@ubuntu.com-20120205104936-4ujoylapu24cquuo
Tags: upstream-2.8.6.3+dfsg
ImportĀ upstreamĀ versionĀ 2.8.6.3+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// MacErrorDialogHandler.cs
 
3
//  
 
4
// Author:
 
5
//       Alan McGovern <alan@xamarin.com>
 
6
// 
 
7
// Copyright 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
 
 
27
using System;
 
28
using System.Drawing;
 
29
using System.Linq;
 
30
using System.Collections.Generic;
 
31
using MonoMac.Foundation;
 
32
using MonoMac.AppKit;
 
33
 
 
34
using MonoDevelop.Core;
 
35
using MonoDevelop.Ide;
 
36
using MonoDevelop.Ide.Extensions;
 
37
using MonoDevelop.Components.Extensions;
 
38
using MonoDevelop.MacInterop;
 
39
        
 
40
namespace MonoDevelop.MacIntegration
 
41
{
 
42
        class MacExceptionDialogHandler : IExceptionDialogHandler
 
43
        {
 
44
                class MyTextView : NSTextView
 
45
                {
 
46
                        public MyTextView (RectangleF frame)
 
47
                                : base (frame)
 
48
                        {
 
49
 
 
50
                        }
 
51
 
 
52
                        public override void KeyDown (NSEvent theEvent)
 
53
                        {
 
54
                                if (theEvent.ModifierFlags.HasFlag (NSEventModifierMask.CommandKeyMask)) {
 
55
                                        switch (theEvent.Characters) {
 
56
                                        case "x":
 
57
                                                Cut (this);
 
58
                                                break;
 
59
                                        case "c":
 
60
                                                Copy (this);
 
61
                                                break;
 
62
                                        case "a":
 
63
                                                SelectAll (this);
 
64
                                                break;
 
65
                                        }
 
66
                                }
 
67
                                
 
68
                                base.KeyDown (theEvent);
 
69
                        }
 
70
                }
 
71
                
 
72
                public bool Run (ExceptionDialogData data)
 
73
                {
 
74
                        using (var alert = new NSAlert { AlertStyle = NSAlertStyle.Critical }) {
 
75
                                alert.Icon = NSApplication.SharedApplication.ApplicationIconImage;
 
76
                                
 
77
                                alert.MessageText = data.Title ?? GettextCatalog.GetString ("Error");
 
78
                                
 
79
                                if (!string.IsNullOrEmpty (data.Message)) {
 
80
                                        alert.InformativeText = data.Message;
 
81
                                }
 
82
 
 
83
                                List<AlertButton> buttons = null;
 
84
                                if (data.Buttons != null && data.Buttons.Length > 0)
 
85
                                        buttons = data.Buttons.Reverse ().ToList ();
 
86
 
 
87
                                if (buttons != null) {
 
88
                                        foreach (var button in buttons) {
 
89
                                                var label = button.Label;
 
90
                                                if (button.IsStockButton)
 
91
                                                        label = Gtk.Stock.Lookup (label).Label;
 
92
                                                label = label.Replace ("_", "");
 
93
 
 
94
                                                //this message seems to be a standard Mac message since alert handles it specially
 
95
                                                if (button == AlertButton.CloseWithoutSave)
 
96
                                                        label = GettextCatalog.GetString ("Don't Save");
 
97
 
 
98
                                                alert.AddButton (label);
 
99
                                        }
 
100
                                }
 
101
 
 
102
                                if (data.Exception != null) {
 
103
                                        var scrollSize = new SizeF (400, 130);
 
104
                                        float spacing = 4;
 
105
                                        
 
106
                                        string title = GettextCatalog.GetString ("View details");
 
107
                                        string altTitle = GettextCatalog.GetString ("Hide details");
 
108
                                        
 
109
                                        var buttonFrame = new RectangleF (0, 0, 0, 0);
 
110
                                        var button = new NSButton (buttonFrame) {
 
111
                                                BezelStyle = NSBezelStyle.Disclosure,
 
112
                                                Title = "",
 
113
                                                AlternateTitle = "",
 
114
                                        };
 
115
                                        button.SetButtonType (NSButtonType.OnOff);
 
116
                                        button.SizeToFit ();
 
117
                                        
 
118
                                        var label = new MDClickableLabel (title) {
 
119
                                                Alignment = NSTextAlignment.Left,
 
120
                                        };
 
121
                                        label.SizeToFit ();
 
122
                                        
 
123
                                        button.SetFrameSize (new SizeF (button.Frame.Width, Math.Max (button.Frame.Height, label.Frame.Height)));
 
124
                                        label.SetFrameOrigin (new PointF (button.Frame.Width + 5, button.Frame.Y));
 
125
                                        
 
126
                                        var text = new MyTextView (new RectangleF (0, 0, float.MaxValue, float.MaxValue)) {
 
127
                                                HorizontallyResizable = true,
 
128
                                        };
 
129
                                        text.TextContainer.ContainerSize = new SizeF (float.MaxValue, float.MaxValue);
 
130
                                        text.TextContainer.WidthTracksTextView = true;
 
131
                                        text.InsertText (new NSString (data.Exception.ToString ()));
 
132
                                        text.Editable = false;
 
133
 
 
134
                                        var scrollView = new NSScrollView (new RectangleF (PointF.Empty, SizeF.Empty)) {
 
135
                                                HasHorizontalScroller = true,
 
136
                                                HasVerticalScroller = true,
 
137
                                        };
 
138
                                        
 
139
                                        var accessory = new NSView (new RectangleF (0, 0, scrollSize.Width, button.Frame.Height));
 
140
                                        accessory.AddSubview (scrollView);
 
141
                                        accessory.AddSubview (button);
 
142
                                        accessory.AddSubview (label);
 
143
                                        
 
144
                                        alert.AccessoryView = accessory;
 
145
                                        
 
146
                                        button.Activated += delegate {
 
147
                                                float change;
 
148
                                                if (button.State == NSCellStateValue.On) {
 
149
                                                        change = scrollSize.Height + spacing;
 
150
                                                        label.StringValue = altTitle;
 
151
                                                        scrollView.Hidden = false;
 
152
                                                        scrollView.Frame = new RectangleF (PointF.Empty, scrollSize);
 
153
                                                        scrollView.DocumentView = text;
 
154
                                                } else {
 
155
                                                        change = -(scrollSize.Height + spacing);
 
156
                                                        label.StringValue = title;
 
157
                                                        scrollView.Hidden = true;
 
158
                                                        scrollView.Frame = new RectangleF (PointF.Empty, SizeF.Empty);
 
159
                                                }
 
160
                                                var f = accessory.Frame;
 
161
                                                f.Height += change;
 
162
                                                accessory.Frame = f;
 
163
                                                var lf = label.Frame;
 
164
                                                lf.Y += change;
 
165
                                                label.Frame = lf;
 
166
                                                var bf = button.Frame;
 
167
                                                bf.Y += change;
 
168
                                                button.Frame = bf;
 
169
                                                label.SizeToFit ();
 
170
                                                var panel = (NSPanel) alert.Window;
 
171
                                                var pf = panel.Frame;
 
172
                                                pf.Height += change;
 
173
                                                pf.Y -= change;
 
174
                                                panel.SetFrame (pf, true, true);
 
175
                                                //unless we assign the icon again, it starts nesting old icon into the warning icon
 
176
                                                alert.Icon = NSApplication.SharedApplication.ApplicationIconImage;
 
177
                                                alert.Layout ();
 
178
                                        };
 
179
                                        label.OnMouseUp += (sender, e) => button.PerformClick (e.Event);
 
180
                                }
 
181
 
 
182
                                int result = alert.RunModal () - (int)NSAlertButtonReturn.First;
 
183
                                data.ResultButton = buttons != null ? buttons [result] : null;
 
184
                                GtkQuartz.FocusWindow (data.TransientFor ?? MessageService.RootWindow);
 
185
                        }
 
186
                        
 
187
                        return true;
 
188
                }
 
189
                
 
190
                class MDClickableLabel: MDLabel
 
191
                {
 
192
                        public MDClickableLabel (string text) : base (text)
 
193
                        {
 
194
                        }
 
195
                        
 
196
                        public override void MouseDown (NSEvent theEvent)
 
197
                        {
 
198
                                if (OnMouseDown != null)
 
199
                                        OnMouseDown (this, new NSEventArgs (theEvent));
 
200
                                else
 
201
                                        base.MouseDown (theEvent);
 
202
                        }
 
203
                        
 
204
                        public event EventHandler<NSEventArgs> OnMouseDown;
 
205
                        
 
206
                        public override void MouseUp (NSEvent theEvent)
 
207
                        {
 
208
                                if (OnMouseUp != null)
 
209
                                        OnMouseUp (this, new NSEventArgs (theEvent));
 
210
                                else
 
211
                                        base.MouseUp (theEvent);
 
212
                        }
 
213
                        
 
214
                        public event EventHandler<NSEventArgs> OnMouseUp;
 
215
                        
 
216
                        public override void MouseEntered (NSEvent theEvent)
 
217
                        {
 
218
                                if (OnMouseEntered != null)
 
219
                                        OnMouseEntered (this, new NSEventArgs (theEvent));
 
220
                                else
 
221
                                        base.MouseEntered (theEvent);
 
222
                        }
 
223
                        
 
224
                        public event EventHandler<NSEventArgs> OnMouseEntered;
 
225
                        
 
226
                        public override void MouseExited (NSEvent theEvent)
 
227
                        {
 
228
                                if (OnMouseExited != null)
 
229
                                        OnMouseExited (this, new NSEventArgs (theEvent));
 
230
                                else
 
231
                                        base.MouseExited (theEvent);
 
232
                        }
 
233
                        
 
234
                        public event EventHandler<NSEventArgs> OnMouseExited;
 
235
                        
 
236
                        public override void MouseMoved (NSEvent theEvent)
 
237
                        {
 
238
                                if (OnMouseMoved != null)
 
239
                                        OnMouseMoved (this, new NSEventArgs (theEvent));
 
240
                                else
 
241
                                        base.MouseMoved (theEvent);
 
242
                        }
 
243
                        
 
244
                        public event EventHandler<NSEventArgs> OnMouseMoved;
 
245
                }
 
246
                
 
247
                class NSEventArgs : EventArgs
 
248
                {
 
249
                        public NSEventArgs (NSEvent evt)
 
250
                        {
 
251
                                this.Event = evt;
 
252
                        }
 
253
                        
 
254
                        public NSEvent Event { get; private set; }
 
255
                }
 
256
        }
 
257
}
 
 
b'\\ No newline at end of file'