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

« back to all changes in this revision

Viewing changes to src/addins/WindowsPlatform/Dialogs/EncodingSelectionForm.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
// EncodingSelectionForm.cs
 
3
//  
 
4
// Author:
 
5
//       Carlos Alberto Cortez <calberto.cortez@gmail.com>
 
6
// 
 
7
// Copyright (c) 2011 Carlos Alberto Cortez
 
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.ComponentModel;
 
30
using System.Data;
 
31
using System.Drawing;
 
32
using System.Linq;
 
33
using System.Text;
 
34
using System.Windows.Forms;
 
35
 
 
36
using MonoDevelop.Core;
 
37
using MonoDevelop.Projects.Text;
 
38
 
 
39
using CustomControls.OS;
 
40
 
 
41
namespace MonoDevelop.Platform
 
42
{
 
43
    public partial class EncodingSelectionForm : Form
 
44
    {
 
45
        public EncodingSelectionForm ()
 
46
        {
 
47
            InitializeComponent ();
 
48
                        LoadStockItems ();
 
49
                        Populate ();
 
50
        }
 
51
                
 
52
                void Populate ()
 
53
                {                       
 
54
                        var availableEncodings = new Dictionary<string,TextEncoding> ();
 
55
                        foreach (var encoding in TextEncoding.SupportedEncodings)
 
56
                                availableEncodings [encoding.Id] = encoding;
 
57
                        
 
58
                        var shownEncodings = TextEncoding.ConversionEncodings;
 
59
                        
 
60
                        shownListView.BeginUpdate ();
 
61
                        foreach (var encoding in shownEncodings) {
 
62
                                var item = new ListViewItem (new string [] { encoding.Id, encoding.Name }) {
 
63
                                        Tag = encoding
 
64
                                };
 
65
                                shownListView.Items.Add (item);
 
66
                                
 
67
                                // Don't show on the available list the encodings
 
68
                                // that are already being shown
 
69
                                availableEncodings.Remove (encoding.Id);
 
70
                        }
 
71
                        shownListView.AutoResizeColumns (ColumnHeaderAutoResizeStyle.HeaderSize);
 
72
                        shownListView.EndUpdate ();
 
73
                        
 
74
                        availableListView.BeginUpdate ();
 
75
                        foreach (var encoding in availableEncodings) {
 
76
                                var item = new ListViewItem (new string [] { encoding.Value.Id, encoding.Value.Name }) {
 
77
                                        Tag = encoding.Value
 
78
                                };
 
79
                                availableListView.Items.Add (item);
 
80
                        }
 
81
                        availableListView.AutoResizeColumns (ColumnHeaderAutoResizeStyle.HeaderSize);
 
82
                        availableListView.EndUpdate ();
 
83
                }
 
84
 
 
85
                void LoadStockItems ()
 
86
                {
 
87
                        SetStockImage (addButton, Gtk.Stock.GoForward);
 
88
                        SetStockImage (removeButton, Gtk.Stock.GoBack);
 
89
                        SetStockImage (upButton, Gtk.Stock.GoUp);
 
90
                        SetStockImage (downButton, Gtk.Stock.GoDown);
 
91
 
 
92
                        Icon = WinFormsRoot.MonoDevelopIcon;
 
93
                }
 
94
 
 
95
                void SetStockImage (Button b, string stockId)
 
96
                {
 
97
                        var pixbuf = MonoDevelop.Ide.ImageService.GetPixbuf (stockId, Gtk.IconSize.Button);
 
98
                        var stockImage = Image.FromStream (new System.IO.MemoryStream (pixbuf.SaveToBuffer ("png")));
 
99
 
 
100
                        b.Image = stockImage;
 
101
                        b.Text = String.Empty;
 
102
                }
 
103
                
 
104
                public TextEncoding [] SelectedEncodings {
 
105
                        get {
 
106
                                var encodings = shownListView.Items.Cast<ListViewItem> ().Select (item => (TextEncoding)item.Tag);
 
107
                                return encodings.ToArray ();
 
108
                        }
 
109
                }
 
110
 
 
111
        void MoveItem (ListView srcView, ListView destView)
 
112
        {
 
113
            if (srcView.SelectedIndices.Count == 0)
 
114
                return;
 
115
 
 
116
            int selectedIndex = srcView.SelectedIndices [0];
 
117
            var item = srcView.Items [selectedIndex];
 
118
 
 
119
            srcView.Items.RemoveAt (selectedIndex);
 
120
            destView.Items.Add (item);
 
121
                        destView.AutoResizeColumns (ColumnHeaderAutoResizeStyle.HeaderSize);
 
122
        }
 
123
 
 
124
        private void addButtonClick (object sender, EventArgs e)
 
125
        {
 
126
            MoveItem (availableListView, shownListView);
 
127
        }
 
128
 
 
129
        private void removeButtonClick (object sender, EventArgs e)
 
130
        {
 
131
            MoveItem (shownListView, availableListView);
 
132
        }
 
133
 
 
134
        void ShiftItem (ListView listView, int shift)
 
135
        {
 
136
            if (listView.SelectedIndices.Count == 0)
 
137
                return;
 
138
 
 
139
            int selectedIndex = listView.SelectedIndices[0];
 
140
            int newIndex = selectedIndex + shift;
 
141
            if (newIndex < 0 || newIndex >= listView.Items.Count)
 
142
                return;
 
143
 
 
144
            listView.BeginUpdate ();
 
145
 
 
146
            var item = listView.Items[selectedIndex];
 
147
            listView.Items.RemoveAt (selectedIndex);
 
148
            listView.Items.Insert (newIndex, item);
 
149
            item.Selected = true;
 
150
 
 
151
            listView.EndUpdate ();
 
152
        }
 
153
 
 
154
        private void upButtonClick (object sender, EventArgs e)
 
155
        {
 
156
            ShiftItem (shownListView, -1);
 
157
        }
 
158
 
 
159
        private void downButtonClick (object sender, EventArgs e)
 
160
        {
 
161
            ShiftItem (shownListView, 1);
 
162
        }
 
163
 
 
164
        private void okButtonClick (object sender, EventArgs e)
 
165
        {
 
166
            DialogResult = DialogResult.OK;
 
167
        }
 
168
 
 
169
        private void cancelButtonClick (object sender, EventArgs e)
 
170
        {
 
171
            DialogResult = DialogResult.Cancel;
 
172
        }
 
173
                
 
174
                protected override void WndProc (ref Message m)
 
175
                {
 
176
                        base.WndProc (ref m);
 
177
                        
 
178
                        switch (m.Msg) {
 
179
                                // No need to take into account the idle event, as
 
180
                                // we are handling it already from WinFormsRoot
 
181
                                case (int) Msg.WM_WINDOWPOSCHANGED:
 
182
                                        MonoDevelop.Ide.DispatchService.RunPendingEvents ();
 
183
                                        break;
 
184
                        }
 
185
                }
 
186
    }
 
187
 
 
188
    public class EncodingListView : ListView
 
189
    {
 
190
        public EncodingListView ()
 
191
        {
 
192
            SuspendLayout ();
 
193
 
 
194
            View = View.Details;
 
195
            MultiSelect = false;
 
196
                        FullRowSelect = true;
 
197
            HideSelection = false;
 
198
 
 
199
            Columns.Add ("Name");
 
200
            Columns.Add ("Encoding");
 
201
 
 
202
            ResumeLayout ();
 
203
        }
 
204
    }
 
205
}