~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// MacOpenFileDialogHandler.cs
 
3
//  
 
4
// Author:
 
5
//       Michael Hutchinson <mhutchinson@novell.com>
 
6
// 
 
7
// Copyright (c) 2010 Novell, 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.Collections.Generic;
 
29
using System.Drawing;
 
30
using System.Linq;
 
31
using MonoDevelop.Core;
 
32
using MonoDevelop.Projects.Text;
 
33
using MonoMac.AppKit;
 
34
using MonoMac.Foundation;
 
35
 
 
36
namespace MonoDevelop.Platform.Mac
 
37
{
 
38
        class SelectEncodingPanel : NSPanel
 
39
        {
 
40
                NSTableView allTable;
 
41
                NSTableView selectedTable;
 
42
                EncodingSource allSource;
 
43
                EncodingSource selectedSource;
 
44
                NSButton addButton, removeButton, upButton, downButton;
 
45
                
 
46
                public SelectEncodingPanel () : base () 
 
47
                {
 
48
                        var size = new SizeF (600, 400);
 
49
                        float padding = 12;
 
50
                        this.SetContentSize (size);
 
51
                        
 
52
                        var view = new NSView (new RectangleF (0, 0, size.Width, size.Height));
 
53
                        var okButton = new NSButton () {
 
54
                                Title = GettextCatalog.GetString ("OK"),
 
55
                                Bordered = true,
 
56
                                BezelStyle = NSBezelStyle.Rounded,
 
57
                        };
 
58
                        okButton.SetButtonType (NSButtonType.MomentaryPushIn);
 
59
                        okButton.Activated += delegate {
 
60
                                Dismiss (1);
 
61
                        };
 
62
                        this.DefaultButtonCell = okButton.Cell;
 
63
                        
 
64
                        var cancelButton = new NSButton () {
 
65
                                Title = GettextCatalog.GetString ("Cancel"),
 
66
                                Bordered = true,
 
67
                                BezelStyle = NSBezelStyle.Rounded,
 
68
                        };
 
69
                        cancelButton.Activated += delegate {
 
70
                                Dismiss (0);
 
71
                        };
 
72
                        var buttonBox = new MDBox (LayoutDirection.Horizontal, padding, 0) {
 
73
                                new MDAlignment (cancelButton, true) { MinWidth = 96, MinHeight = 32 },
 
74
                                new MDAlignment (okButton, true) { MinWidth = 96, MinHeight = 32 },
 
75
                        };
 
76
                        buttonBox.Layout ();
 
77
                        var buttonView = buttonBox.View;
 
78
                        var buttonRect = buttonView.Frame;
 
79
                        buttonRect.Y = 12;
 
80
                        buttonRect.X = size.Width - buttonRect.Width - padding;
 
81
                        buttonView.Frame = buttonRect;
 
82
                        view.AddSubview (buttonView);
 
83
                        
 
84
                        float buttonAreaTop = buttonRect.Height + padding * 2;
 
85
                        
 
86
                        var label = CreateLabel (GettextCatalog.GetString ("Available encodings:"));
 
87
                        var labelSize = label.Frame.Size;
 
88
                        float labelBottom = size.Height - 12 - labelSize.Height;
 
89
                        label.Frame = new RectangleF (12, labelBottom, labelSize.Width, labelSize.Height);
 
90
                        view.AddSubview (label);
 
91
                        
 
92
                        var moveButtonWidth = 32;
 
93
                        var tableHeight = labelBottom - buttonAreaTop - padding;
 
94
                        var tableWidth = size.Width / 2 - padding * 3 - moveButtonWidth + padding / 2;
 
95
                        
 
96
                        allTable = new NSTableView (new RectangleF (padding, buttonAreaTop, tableWidth, tableHeight));
 
97
                        allTable.HeaderView = null;
 
98
                        var allScroll = new NSScrollView (allTable.Frame) {
 
99
                                BorderType = NSBorderType.BezelBorder,
 
100
                                AutohidesScrollers = true,
 
101
                                HasVerticalScroller = true,
 
102
                                DocumentView = allTable,
 
103
                        };
 
104
                        view.AddSubview (allScroll);
 
105
                        
 
106
                        float center = (size.Width + padding) / 2;
 
107
                        
 
108
                        var selectedLabel = CreateLabel (GettextCatalog.GetString ("Encodings shown in menu:"));
 
109
                        var selectedLabelSize = selectedLabel.Frame.Size;
 
110
                        selectedLabel.Frame = new RectangleF (center, labelBottom, selectedLabelSize.Width, selectedLabelSize.Height);
 
111
                        view.AddSubview (selectedLabel);
 
112
                        
 
113
                        selectedTable = new NSTableView (new RectangleF (center, buttonAreaTop, tableWidth, tableHeight));
 
114
                        selectedTable.HeaderView = null;
 
115
                        var selectedScroll = new NSScrollView (selectedTable.Frame) {
 
116
                                BorderType = NSBorderType.BezelBorder,
 
117
                                AutohidesScrollers = true,
 
118
                                HasVerticalScroller = true,
 
119
                                DocumentView = selectedTable,
 
120
                        };
 
121
                        view.AddSubview (selectedScroll);
 
122
                        
 
123
                        float buttonLevel = tableHeight / 2 + buttonAreaTop;
 
124
                        
 
125
                        var goRightImage = NSImage.ImageNamed ("NSGoRightTemplate");
 
126
                        
 
127
                        addButton = new NSButton (
 
128
                                new RectangleF (tableWidth + padding * 2, buttonLevel + padding / 2,
 
129
                                        moveButtonWidth, moveButtonWidth)) {
 
130
                                //Title = "\u2192",
 
131
                                BezelStyle = NSBezelStyle.SmallSquare,
 
132
                                Image = goRightImage
 
133
                        };
 
134
                        addButton.Activated += Add;
 
135
                        view.AddSubview (addButton);
 
136
                        
 
137
                        removeButton = new NSButton (
 
138
                                new RectangleF (tableWidth + padding * 2, buttonLevel - padding / 2 - moveButtonWidth,
 
139
                                        moveButtonWidth, moveButtonWidth)) {
 
140
                                //Title = "\u2190",
 
141
                                BezelStyle = NSBezelStyle.SmallSquare,
 
142
                                Image = NSImage.ImageNamed ("NSGoLeftTemplate"),
 
143
                        };
 
144
                        removeButton.Activated += Remove;
 
145
                        view.AddSubview (removeButton);
 
146
                        
 
147
                        upButton = new NSButton (
 
148
                                new RectangleF (center + tableWidth + padding, buttonLevel + padding / 2,
 
149
                                        moveButtonWidth, moveButtonWidth)) {
 
150
                                //Title = "\u2191",
 
151
                                BezelStyle = NSBezelStyle.SmallSquare,
 
152
                                Image = MakeRotatedCopy (goRightImage, 90),
 
153
                        };
 
154
                        upButton.Activated += MoveUp;
 
155
                        view.AddSubview (upButton);
 
156
                        
 
157
                        downButton = new NSButton (
 
158
                                new RectangleF (center + tableWidth + padding, buttonLevel - padding / 2 - moveButtonWidth,
 
159
                                        moveButtonWidth, moveButtonWidth)) {
 
160
                                //Title = "\u2193",
 
161
                                BezelStyle = NSBezelStyle.SmallSquare,
 
162
                                Image = MakeRotatedCopy (goRightImage, -90),
 
163
                        };
 
164
                        downButton.Activated += MoveDown;
 
165
                        view.AddSubview (downButton);
 
166
                        
 
167
                        var allColumn = new NSTableColumn () {
 
168
                                DataCell = new NSTextFieldCell () { Wraps = true },
 
169
                                Width = tableWidth
 
170
                        };
 
171
                        allTable.AddColumn (allColumn);
 
172
                        allTable.DataSource = allSource = new EncodingSource (TextEncoding.SupportedEncodings);
 
173
                        allTable.Delegate = new EncodingAllDelegate (this);
 
174
                        
 
175
                        var selectedColumn = new NSTableColumn () {
 
176
                                DataCell = new NSTextFieldCell () { Wraps = true },
 
177
                                Width = tableWidth
 
178
                        };
 
179
                        selectedTable.AddColumn (selectedColumn);
 
180
                        selectedTable.DataSource = selectedSource = new EncodingSource (TextEncoding.ConversionEncodings);
 
181
                        selectedTable.Delegate = new EncodingSelectedDelegate (this);
 
182
                        
 
183
                        UpdateButtons ();
 
184
                        
 
185
                        this.ContentView = view;
 
186
                }
 
187
                
 
188
                NSImage MakeRotatedCopy (NSImage original, float degrees)
 
189
                {
 
190
                        var copy = new NSImage (original.Size);
 
191
                        copy.LockFocus ();
 
192
                        try {
 
193
                                var rot = new NSAffineTransform ();
 
194
                                rot.Translate (original.Size.Width / 2, original.Size.Height / 2);
 
195
                                rot.RotateByDegrees (degrees);
 
196
                                rot.Translate (-original.Size.Width / 2, -original.Size.Height / 2);
 
197
                                rot.Concat ();
 
198
                                original.Draw (PointF.Empty, RectangleF.Empty, NSCompositingOperation.Copy, 1);
 
199
                        } finally {
 
200
                                copy.UnlockFocus ();
 
201
                        }
 
202
                        return copy;
 
203
                }
 
204
 
 
205
                void Add (object sender, EventArgs e)
 
206
                {
 
207
                        var fromIndex = allTable.SelectedRow;
 
208
                        var encoding = allSource.encodings[fromIndex];
 
209
                        var toIndex = selectedTable.SelectedRow + 1;
 
210
                        if (toIndex <= 0)
 
211
                                toIndex = selectedSource.encodings.Count;
 
212
                        selectedSource.encodings.Insert (toIndex, encoding);
 
213
                        selectedTable.ReloadData ();
 
214
                        selectedTable.SelectRows (new NSIndexSet ((uint)(toIndex)), false);
 
215
                        UpdateButtons ();
 
216
                }
 
217
                
 
218
                void Remove (object sender, EventArgs e)
 
219
                {
 
220
                        var index = selectedTable.SelectedRow;
 
221
                        selectedSource.encodings.RemoveAt (index);
 
222
                        selectedTable.ReloadData ();
 
223
                        if (index >= selectedSource.encodings.Count)
 
224
                                index--;
 
225
                        selectedTable.SelectRows (new NSIndexSet ((uint)(index)), false);
 
226
                        UpdateButtons ();
 
227
                }
 
228
 
 
229
                void MoveUp (object sender, EventArgs e)
 
230
                {
 
231
                        var index = selectedTable.SelectedRow;
 
232
                        var selected = selectedSource.encodings[index];
 
233
                        selectedSource.encodings[index] = selectedSource.encodings[index - 1];
 
234
                        selectedSource.encodings[index - 1] = selected;
 
235
                        selectedTable.ReloadData ();
 
236
                        selectedTable.SelectRows (new NSIndexSet ((uint)(index - 1)), false);
 
237
                        UpdateButtons ();
 
238
                }
 
239
                
 
240
                void MoveDown (object sender, EventArgs e)
 
241
                {
 
242
                        var index = selectedTable.SelectedRow;
 
243
                        var selected = selectedSource.encodings[index];
 
244
                        selectedSource.encodings[index] = selectedSource.encodings[index + 1];
 
245
                        selectedSource.encodings[index + 1] = selected;
 
246
                        selectedTable.ReloadData ();
 
247
                        selectedTable.SelectRows (new NSIndexSet ((uint)(index + 1)), false);
 
248
                        UpdateButtons ();
 
249
                }
 
250
                
 
251
                void UpdateButtons ()
 
252
                {
 
253
                        var allIndex = allTable.SelectedRow;
 
254
                        var allEncoding = allIndex >= 0? allSource.encodings[allIndex] : null;
 
255
                        addButton.Enabled = allEncoding != null && !selectedSource.encodings.Any (e => e.Id == allEncoding.Id);
 
256
                        
 
257
                        var selectedIndex = selectedTable.SelectedRow;
 
258
                        removeButton.Enabled = selectedIndex >= 0 && selectedSource.encodings.Count > 0;
 
259
                        upButton.Enabled = selectedIndex > 0;
 
260
                        downButton.Enabled = selectedIndex >= 0 && selectedIndex < selectedSource.encodings.Count - 1;
 
261
                }
 
262
                
 
263
                static NSTextField CreateLabel (string text)
 
264
                {
 
265
                        var label = new NSTextField () {
 
266
                                StringValue = text,
 
267
                                DrawsBackground = false,
 
268
                                Bordered = false,
 
269
                                Editable = false,
 
270
                                Selectable = false,
 
271
                        };
 
272
                        label.SizeToFit ();
 
273
                        return label;
 
274
                }
 
275
                
 
276
                public int RunModal ()
 
277
                {
 
278
                        this.DidResignKey += StopSharedAppModal;
 
279
                        try {
 
280
                                return SaveIfOk (NSApplication.SharedApplication.RunModalForWindow (this));
 
281
                        } finally {
 
282
                                this.DidResignKey -= StopSharedAppModal;
 
283
                        }
 
284
                }
 
285
                
 
286
                [Export ("sheetSel")]
 
287
                void SheetSel ()
 
288
                {
 
289
                }
 
290
                
 
291
                bool sheet;
 
292
                
 
293
                public int RunModalSheet (NSWindow parent)
 
294
                {
 
295
                        var sel = new MonoMac.ObjCRuntime.Selector ("sheetSel");
 
296
                        NSApplication.SharedApplication.BeginSheet (this, parent, this, sel, IntPtr.Zero);
 
297
                        this.DidResignKey += StopSharedAppModal;
 
298
                        try {
 
299
                                sheet = true;
 
300
                                return SaveIfOk (NSApplication.SharedApplication.RunModalForWindow (this));
 
301
                        } finally {
 
302
                                sheet = false;
 
303
                                this.DidResignKey -= StopSharedAppModal;
 
304
                        }
 
305
                }
 
306
                
 
307
                int SaveIfOk (int ret)
 
308
                {
 
309
                        if (ret != 0)
 
310
                                TextEncoding.ConversionEncodings = selectedSource.encodings.ToArray ();
 
311
                        return ret;
 
312
                }
 
313
 
 
314
                static void StopSharedAppModal (object sender, EventArgs e)
 
315
                {
 
316
                        NSApplication.SharedApplication.StopModal ();
 
317
                }
 
318
                
 
319
                void Dismiss (int code)
 
320
                {
 
321
                        if (sheet) {
 
322
                                NSApplication.SharedApplication.EndSheet (this, code);
 
323
                                OrderOut (this);
 
324
                        } else {
 
325
                                NSApplication.SharedApplication.StopModal ();
 
326
                                OrderOut (this);
 
327
                        }
 
328
                }
 
329
                
 
330
                class EncodingSource : NSTableViewDataSource
 
331
                {
 
332
                        public List<TextEncoding> encodings;
 
333
                        
 
334
                        public EncodingSource (IEnumerable<TextEncoding> encodings)
 
335
                        {
 
336
                                this.encodings = new List<TextEncoding> (encodings);
 
337
                        }
 
338
                        
 
339
                        public override int GetRowCount (NSTableView tableView)
 
340
                        {
 
341
                                return encodings.Count;
 
342
                        }
 
343
                        
 
344
                        public override NSObject GetObjectValue (NSTableView tableView, NSTableColumn tableColumn, int row)
 
345
                        {
 
346
                                var encoding = encodings[row];
 
347
                                return new NSString (string.Format ("{0} ({1})", encoding.Name, encoding.Id));
 
348
                        }
 
349
                }
 
350
                
 
351
                class EncodingAllDelegate : NSTableViewDelegate
 
352
                {
 
353
                        SelectEncodingPanel parent;
 
354
                        
 
355
                        public EncodingAllDelegate (SelectEncodingPanel parent)
 
356
                        {
 
357
                                this.parent = parent;
 
358
                        }
 
359
                        
 
360
                        public override void SelectionDidChange (NSNotification notification)
 
361
                        {
 
362
                                parent.UpdateButtons ();
 
363
                        }
 
364
                }
 
365
                
 
366
                class EncodingSelectedDelegate : NSTableViewDelegate
 
367
                {
 
368
                        SelectEncodingPanel parent;
 
369
                        
 
370
                        public EncodingSelectedDelegate (SelectEncodingPanel parent)
 
371
                        {
 
372
                                this.parent = parent;
 
373
                        }
 
374
                        
 
375
                        public override void SelectionDidChange (NSNotification notification)
 
376
                        {
 
377
                                parent.UpdateButtons ();
 
378
                        }
 
379
                }
 
380
        }
 
381
}