2
// EncodingSelectionForm.cs
5
// Carlos Alberto Cortez <calberto.cortez@gmail.com>
7
// Copyright (c) 2011 Carlos Alberto Cortez
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;
29
using System.ComponentModel;
34
using System.Windows.Forms;
36
using MonoDevelop.Core;
37
using MonoDevelop.Projects.Text;
39
using CustomControls.OS;
41
namespace MonoDevelop.Platform
43
public partial class EncodingSelectionForm : Form
45
public EncodingSelectionForm ()
47
InitializeComponent ();
54
var availableEncodings = new Dictionary<string,TextEncoding> ();
55
foreach (var encoding in TextEncoding.SupportedEncodings)
56
availableEncodings [encoding.Id] = encoding;
58
var shownEncodings = TextEncoding.ConversionEncodings;
60
shownListView.BeginUpdate ();
61
foreach (var encoding in shownEncodings) {
62
var item = new ListViewItem (new string [] { encoding.Id, encoding.Name }) {
65
shownListView.Items.Add (item);
67
// Don't show on the available list the encodings
68
// that are already being shown
69
availableEncodings.Remove (encoding.Id);
71
shownListView.AutoResizeColumns (ColumnHeaderAutoResizeStyle.HeaderSize);
72
shownListView.EndUpdate ();
74
availableListView.BeginUpdate ();
75
foreach (var encoding in availableEncodings) {
76
var item = new ListViewItem (new string [] { encoding.Value.Id, encoding.Value.Name }) {
79
availableListView.Items.Add (item);
81
availableListView.AutoResizeColumns (ColumnHeaderAutoResizeStyle.HeaderSize);
82
availableListView.EndUpdate ();
85
void LoadStockItems ()
87
SetStockImage (addButton, Gtk.Stock.GoForward);
88
SetStockImage (removeButton, Gtk.Stock.GoBack);
89
SetStockImage (upButton, Gtk.Stock.GoUp);
90
SetStockImage (downButton, Gtk.Stock.GoDown);
92
Icon = WinFormsRoot.MonoDevelopIcon;
95
void SetStockImage (Button b, string stockId)
97
var pixbuf = MonoDevelop.Ide.ImageService.GetPixbuf (stockId, Gtk.IconSize.Button);
98
var stockImage = Image.FromStream (new System.IO.MemoryStream (pixbuf.SaveToBuffer ("png")));
100
b.Image = stockImage;
101
b.Text = String.Empty;
104
public TextEncoding [] SelectedEncodings {
106
var encodings = shownListView.Items.Cast<ListViewItem> ().Select (item => (TextEncoding)item.Tag);
107
return encodings.ToArray ();
111
void MoveItem (ListView srcView, ListView destView)
113
if (srcView.SelectedIndices.Count == 0)
116
int selectedIndex = srcView.SelectedIndices [0];
117
var item = srcView.Items [selectedIndex];
119
srcView.Items.RemoveAt (selectedIndex);
120
destView.Items.Add (item);
121
destView.AutoResizeColumns (ColumnHeaderAutoResizeStyle.HeaderSize);
124
private void addButtonClick (object sender, EventArgs e)
126
MoveItem (availableListView, shownListView);
129
private void removeButtonClick (object sender, EventArgs e)
131
MoveItem (shownListView, availableListView);
134
void ShiftItem (ListView listView, int shift)
136
if (listView.SelectedIndices.Count == 0)
139
int selectedIndex = listView.SelectedIndices[0];
140
int newIndex = selectedIndex + shift;
141
if (newIndex < 0 || newIndex >= listView.Items.Count)
144
listView.BeginUpdate ();
146
var item = listView.Items[selectedIndex];
147
listView.Items.RemoveAt (selectedIndex);
148
listView.Items.Insert (newIndex, item);
149
item.Selected = true;
151
listView.EndUpdate ();
154
private void upButtonClick (object sender, EventArgs e)
156
ShiftItem (shownListView, -1);
159
private void downButtonClick (object sender, EventArgs e)
161
ShiftItem (shownListView, 1);
164
private void okButtonClick (object sender, EventArgs e)
166
DialogResult = DialogResult.OK;
169
private void cancelButtonClick (object sender, EventArgs e)
171
DialogResult = DialogResult.Cancel;
174
protected override void WndProc (ref Message m)
176
base.WndProc (ref m);
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 ();
188
public class EncodingListView : ListView
190
public EncodingListView ()
196
FullRowSelect = true;
197
HideSelection = false;
199
Columns.Add ("Name");
200
Columns.Add ("Encoding");