2
// MacOpenFileDialogHandler.cs
5
// Michael Hutchinson <mhutchinson@novell.com>
7
// Copyright (c) 2010 Novell, 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
28
using System.Collections.Generic;
31
using MonoDevelop.Core;
32
using MonoDevelop.Projects.Text;
34
using MonoMac.Foundation;
36
namespace MonoDevelop.Platform.Mac
38
class SelectEncodingPanel : NSPanel
41
NSTableView selectedTable;
42
EncodingSource allSource;
43
EncodingSource selectedSource;
44
NSButton addButton, removeButton, upButton, downButton;
46
public SelectEncodingPanel () : base ()
48
var size = new SizeF (600, 400);
50
this.SetContentSize (size);
52
var view = new NSView (new RectangleF (0, 0, size.Width, size.Height));
53
var okButton = new NSButton () {
54
Title = GettextCatalog.GetString ("OK"),
56
BezelStyle = NSBezelStyle.Rounded,
58
okButton.SetButtonType (NSButtonType.MomentaryPushIn);
59
okButton.Activated += delegate {
62
this.DefaultButtonCell = okButton.Cell;
64
var cancelButton = new NSButton () {
65
Title = GettextCatalog.GetString ("Cancel"),
67
BezelStyle = NSBezelStyle.Rounded,
69
cancelButton.Activated += delegate {
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 },
77
var buttonView = buttonBox.View;
78
var buttonRect = buttonView.Frame;
80
buttonRect.X = size.Width - buttonRect.Width - padding;
81
buttonView.Frame = buttonRect;
82
view.AddSubview (buttonView);
84
float buttonAreaTop = buttonRect.Height + padding * 2;
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);
92
var moveButtonWidth = 32;
93
var tableHeight = labelBottom - buttonAreaTop - padding;
94
var tableWidth = size.Width / 2 - padding * 3 - moveButtonWidth + padding / 2;
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,
104
view.AddSubview (allScroll);
106
float center = (size.Width + padding) / 2;
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);
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,
121
view.AddSubview (selectedScroll);
123
float buttonLevel = tableHeight / 2 + buttonAreaTop;
125
var goRightImage = NSImage.ImageNamed ("NSGoRightTemplate");
127
addButton = new NSButton (
128
new RectangleF (tableWidth + padding * 2, buttonLevel + padding / 2,
129
moveButtonWidth, moveButtonWidth)) {
131
BezelStyle = NSBezelStyle.SmallSquare,
134
addButton.Activated += Add;
135
view.AddSubview (addButton);
137
removeButton = new NSButton (
138
new RectangleF (tableWidth + padding * 2, buttonLevel - padding / 2 - moveButtonWidth,
139
moveButtonWidth, moveButtonWidth)) {
141
BezelStyle = NSBezelStyle.SmallSquare,
142
Image = NSImage.ImageNamed ("NSGoLeftTemplate"),
144
removeButton.Activated += Remove;
145
view.AddSubview (removeButton);
147
upButton = new NSButton (
148
new RectangleF (center + tableWidth + padding, buttonLevel + padding / 2,
149
moveButtonWidth, moveButtonWidth)) {
151
BezelStyle = NSBezelStyle.SmallSquare,
152
Image = MakeRotatedCopy (goRightImage, 90),
154
upButton.Activated += MoveUp;
155
view.AddSubview (upButton);
157
downButton = new NSButton (
158
new RectangleF (center + tableWidth + padding, buttonLevel - padding / 2 - moveButtonWidth,
159
moveButtonWidth, moveButtonWidth)) {
161
BezelStyle = NSBezelStyle.SmallSquare,
162
Image = MakeRotatedCopy (goRightImage, -90),
164
downButton.Activated += MoveDown;
165
view.AddSubview (downButton);
167
var allColumn = new NSTableColumn () {
168
DataCell = new NSTextFieldCell () { Wraps = true },
171
allTable.AddColumn (allColumn);
172
allTable.DataSource = allSource = new EncodingSource (TextEncoding.SupportedEncodings);
173
allTable.Delegate = new EncodingAllDelegate (this);
175
var selectedColumn = new NSTableColumn () {
176
DataCell = new NSTextFieldCell () { Wraps = true },
179
selectedTable.AddColumn (selectedColumn);
180
selectedTable.DataSource = selectedSource = new EncodingSource (TextEncoding.ConversionEncodings);
181
selectedTable.Delegate = new EncodingSelectedDelegate (this);
185
this.ContentView = view;
188
NSImage MakeRotatedCopy (NSImage original, float degrees)
190
var copy = new NSImage (original.Size);
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);
198
original.Draw (PointF.Empty, RectangleF.Empty, NSCompositingOperation.Copy, 1);
205
void Add (object sender, EventArgs e)
207
var fromIndex = allTable.SelectedRow;
208
var encoding = allSource.encodings[fromIndex];
209
var toIndex = selectedTable.SelectedRow + 1;
211
toIndex = selectedSource.encodings.Count;
212
selectedSource.encodings.Insert (toIndex, encoding);
213
selectedTable.ReloadData ();
214
selectedTable.SelectRows (new NSIndexSet ((uint)(toIndex)), false);
218
void Remove (object sender, EventArgs e)
220
var index = selectedTable.SelectedRow;
221
selectedSource.encodings.RemoveAt (index);
222
selectedTable.ReloadData ();
223
if (index >= selectedSource.encodings.Count)
225
selectedTable.SelectRows (new NSIndexSet ((uint)(index)), false);
229
void MoveUp (object sender, EventArgs e)
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);
240
void MoveDown (object sender, EventArgs e)
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);
251
void UpdateButtons ()
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);
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;
263
static NSTextField CreateLabel (string text)
265
var label = new NSTextField () {
267
DrawsBackground = false,
276
public int RunModal ()
278
this.DidResignKey += StopSharedAppModal;
280
return SaveIfOk (NSApplication.SharedApplication.RunModalForWindow (this));
282
this.DidResignKey -= StopSharedAppModal;
286
[Export ("sheetSel")]
293
public int RunModalSheet (NSWindow parent)
295
var sel = new MonoMac.ObjCRuntime.Selector ("sheetSel");
296
NSApplication.SharedApplication.BeginSheet (this, parent, this, sel, IntPtr.Zero);
297
this.DidResignKey += StopSharedAppModal;
300
return SaveIfOk (NSApplication.SharedApplication.RunModalForWindow (this));
303
this.DidResignKey -= StopSharedAppModal;
307
int SaveIfOk (int ret)
310
TextEncoding.ConversionEncodings = selectedSource.encodings.ToArray ();
314
static void StopSharedAppModal (object sender, EventArgs e)
316
NSApplication.SharedApplication.StopModal ();
319
void Dismiss (int code)
322
NSApplication.SharedApplication.EndSheet (this, code);
325
NSApplication.SharedApplication.StopModal ();
330
class EncodingSource : NSTableViewDataSource
332
public List<TextEncoding> encodings;
334
public EncodingSource (IEnumerable<TextEncoding> encodings)
336
this.encodings = new List<TextEncoding> (encodings);
339
public override int GetRowCount (NSTableView tableView)
341
return encodings.Count;
344
public override NSObject GetObjectValue (NSTableView tableView, NSTableColumn tableColumn, int row)
346
var encoding = encodings[row];
347
return new NSString (string.Format ("{0} ({1})", encoding.Name, encoding.Id));
351
class EncodingAllDelegate : NSTableViewDelegate
353
SelectEncodingPanel parent;
355
public EncodingAllDelegate (SelectEncodingPanel parent)
357
this.parent = parent;
360
public override void SelectionDidChange (NSNotification notification)
362
parent.UpdateButtons ();
366
class EncodingSelectedDelegate : NSTableViewDelegate
368
SelectEncodingPanel parent;
370
public EncodingSelectedDelegate (SelectEncodingPanel parent)
372
this.parent = parent;
375
public override void SelectionDidChange (NSNotification notification)
377
parent.UpdateButtons ();