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

« back to all changes in this revision

Viewing changes to src/addins/MacPlatform/Dialogs/SelectEncodingPopUpButton.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.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
using MonoDevelop.Projects.Text;
 
40
 
 
41
namespace MonoDevelop.Platform.Mac
 
42
{
 
43
        class SelectEncodingPopUpButton : NSPopUpButton
 
44
        {
 
45
                MonoMac.ObjCRuntime.Selector itemActivationSel = new MonoMac.ObjCRuntime.Selector ("itemActivated:");
 
46
                MonoMac.ObjCRuntime.Selector addRemoveActivationSel = new MonoMac.ObjCRuntime.Selector ("addRemoveActivated:");
 
47
                
 
48
                NSMenuItem autoDetectedItem, addRemoveItem;
 
49
                TextEncoding[] encodings;
 
50
                
 
51
                public SelectEncodingPopUpButton (bool showAutoDetected)
 
52
                {
 
53
                        Cell.UsesItemFromMenu = false;
 
54
                        
 
55
                        if (showAutoDetected) {
 
56
                                autoDetectedItem = new NSMenuItem () {
 
57
                                        Title = GettextCatalog.GetString ("Auto Detected"),
 
58
                                        Tag = -1,
 
59
                                        Target = this,
 
60
                                        Action = itemActivationSel,
 
61
                                };
 
62
                        }
 
63
                        
 
64
                        addRemoveItem = new NSMenuItem () {
 
65
                                Title = GettextCatalog.GetString ("Add or Remove..."),
 
66
                                Tag = -20,
 
67
                                Target = this,
 
68
                                Action = addRemoveActivationSel,
 
69
                        };
 
70
                        
 
71
                        Populate (false);
 
72
                        SelectedEncodingId = null;
 
73
                }
 
74
                
 
75
                public string SelectedEncodingId {
 
76
                        get {
 
77
                                var idx = Cell.MenuItem.Tag;
 
78
                                if (idx <= 0)
 
79
                                        return null;
 
80
                                return encodings[idx - 1].Id;
 
81
                        }
 
82
                        set {
 
83
                                NSMenuItem item = null;
 
84
                                if (!string.IsNullOrEmpty (value)) {
 
85
                                        int i = 1;
 
86
                                        foreach (var e in encodings) {
 
87
                                                if (e.Id == value) {
 
88
                                                        item = Menu.ItemWithTag (i);
 
89
                                                        break;
 
90
                                                }
 
91
                                                i++;
 
92
                                        }
 
93
                                }
 
94
                                Cell.SelectItem (Cell.MenuItem = item ?? Cell.Menu.ItemAt (0));
 
95
                        }
 
96
                }
 
97
                
 
98
                void Populate (bool clear)
 
99
                {
 
100
                        if (clear)
 
101
                                Menu.RemoveAllItems ();
 
102
                                
 
103
                        encodings = TextEncoding.ConversionEncodings;
 
104
                        if (encodings == null || encodings.Length == 0)
 
105
                                encodings = new TextEncoding [] { TextEncoding.GetEncoding (TextEncoding.DefaultEncoding) };
 
106
                        
 
107
                        if (autoDetectedItem != null) {
 
108
                                Menu.AddItem (autoDetectedItem);
 
109
                                Cell.MenuItem = autoDetectedItem;
 
110
                                Menu.AddItem (NSMenuItem.SeparatorItem);
 
111
                        }
 
112
                        
 
113
                        int i = 1;
 
114
                        foreach (var e in MonoDevelop.Projects.Text.TextEncoding.ConversionEncodings) {
 
115
                                Menu.AddItem (new NSMenuItem () {
 
116
                                        Title = string.Format ("{0} ({1})", e.Name, e.Id),
 
117
                                        Tag = i++,
 
118
                                        Target = this,
 
119
                                        Action = itemActivationSel,
 
120
                                });
 
121
                        }
 
122
                        
 
123
                        Menu.AddItem (NSMenuItem.SeparatorItem);
 
124
                        Menu.AddItem (addRemoveItem);
 
125
                }
 
126
                
 
127
                [Export ("addRemoveActivated:")]
 
128
                void HandleAddRemoveItemActivated (NSObject sender)
 
129
                {
 
130
                        Cell.SelectItem (Cell.MenuItem);
 
131
                        
 
132
                        var selection = SelectedEncodingId;
 
133
                        var dlg = new SelectEncodingPanel ();
 
134
                        if (dlg.RunModalSheet (this.Window) != 0) {
 
135
                                Populate (true);
 
136
                                SelectedEncodingId = selection;
 
137
                        }
 
138
                }
 
139
                
 
140
                [Export ("itemActivated:")]
 
141
                void HandleItemActivated (NSObject sender)
 
142
                {
 
143
                        Cell.MenuItem = (NSMenuItem)sender;
 
144
                }
 
145
        }
 
146
}