2 * This file is part of Gnome Subtitles.
3 * Copyright (C) 2006-2010 Pedro Castro
5 * Gnome Subtitles is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Gnome Subtitles is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 using GnomeSubtitles
.Core
;
21 using GnomeSubtitles
.Dialog
;
25 using System
.Collections
;
27 namespace GnomeSubtitles
.Ui
.Component
{
29 public class EncodingComboBox
{
31 private ComboBox comboBox
= null;
32 private int comboBoxActiveItem
= 0; //Stores the last active item so we can get back to it after using Add/Remove
34 private ArrayList encodings
= null; //Encodings present in the combo box
35 private int actionCount
= 0; //Actions correspond to the initial items (including auto detection)
36 private bool hasAutoDetect
= false; //Whether to add Auto Detect to the top of the item list
37 private string[] additionalActions
= null; //
38 private int fixedEncoding
= -1; //Codepage of an encoding that must be present (-1 if not)
39 private ArrayList configShownEncodings
= null; //Encodings shown in menu from config
41 public EncodingComboBox (ComboBox comboBox
, bool hasAutoDetect
, string[] additionalActions
, int fixedEncoding
) {
42 this.comboBox
= comboBox
;
43 this.additionalActions
= additionalActions
;
44 this.hasAutoDetect
= hasAutoDetect
;
45 this.fixedEncoding
= fixedEncoding
;
49 SetComboBox(Base
.Config
.PrefsEncodingsShownInMenu
);
53 public EncodingComboBox (ComboBox comboBox
, bool hasAutoDetect
) : this(comboBox
, hasAutoDetect
, null, -1) {
57 /* Public properties */
59 public bool HasChosenAction
{
60 get { return comboBox.Active < actionCount; }
63 public int ChosenAction
{
64 get { return (HasChosenAction ? comboBox.Active : -1); }
67 public EncodingDescription ChosenEncoding
{
69 int active
= comboBox
.Active
;
70 if (active
< actionCount
) //An action is active
71 return EncodingDescription
.Empty
;
73 return (EncodingDescription
)encodings
[active
- (actionCount
> 0 ? actionCount
+ 1 : 0)]; //1 for break line
77 public bool IsChosenCurrentLocale
{
78 get { return comboBox.Active == actionCount + 1; }
81 public int ActiveSelection
{
82 get { return comboBox.Active; }
83 set { SetActiveItem(value, false); }
88 public event EventHandler SelectionChanged
;
93 private void InitComboBoxModel () {
94 ComboBoxUtil
.InitComboBox(comboBox
);
97 private void SetActionCount () {
98 this.actionCount
= (hasAutoDetect
? 1 : 0) + (additionalActions
!= null ? additionalActions
.Length
: 0);
101 private void SetComboBox (string[] names
) {
102 configShownEncodings
= new ArrayList(names
);
107 private void LoadEncodings () {
108 bool toAddFixedEncoding
= (fixedEncoding
!= -1);
109 ArrayList encodings
= new ArrayList();
111 foreach (string name
in configShownEncodings
) {
112 EncodingDescription description
= EncodingDescription
.Empty
;
113 if (Encodings
.Find(name
, ref description
)) {
114 encodings
.Add(description
);
115 if (toAddFixedEncoding
&& (description
.CodePage
== fixedEncoding
))
116 toAddFixedEncoding
= false;
120 if (toAddFixedEncoding
) {
121 EncodingDescription description
= EncodingDescription
.Empty
;
122 if (Encodings
.Find(fixedEncoding
, ref description
))
123 encodings
.Add(description
);
127 encodings
.Insert(0, Encodings
.SystemDefault
);
129 this.encodings
= encodings
;
132 private void FillComboBox () {
133 DisconnectComboBoxChangedSignal();
135 (comboBox
.Model
as ListStore
).Clear();
137 int activeItem
= comboBoxActiveItem
;
140 /* Add auto detect */
146 /* Add additional actions */
147 if (additionalActions
!= null) {
148 foreach (string additionalAction
in additionalActions
) {
149 comboBox
.AppendText(additionalAction
);
154 if (currentItem
!= 0) {
155 comboBox
.AppendText("-");
160 foreach (EncodingDescription encoding
in encodings
) {
161 comboBox
.AppendText(encoding
.Description
+ " (" + encoding
.Name
+ ")");
162 if (encoding
.CodePage
== fixedEncoding
) {
163 activeItem
= currentItem
;
168 /* Add add/remove action */
169 comboBox
.AppendText("-");
170 comboBox
.AppendText(Catalog
.GetString("Add or Remove..."));
172 SetActiveItem(activeItem
, false); //Don't use silent change because the signal is already disabled
174 ConnectComboBoxChangedSignal();
177 private void AddAutoDetect () {
178 comboBox
.AppendText(Catalog
.GetString("Auto Detected"));
181 private void SetActiveItem (int item
, bool silent
) {
182 int itemCount
= comboBox
.Model
.IterNChildren();
187 DisconnectComboBoxChangedSignal();
189 comboBoxActiveItem
= (item
< itemCount
- 2 ? item
: 0);
190 comboBox
.Active
= comboBoxActiveItem
;
193 ConnectComboBoxChangedSignal();
198 #pragma warning disable 169 //Disables warning about handlers not being used
200 private void ConnectHandlers () {
201 comboBox
.RowSeparatorFunc
= ComboBoxUtil
.SeparatorFunc
;
204 private void ConnectComboBoxChangedSignal () {
205 comboBox
.Changed
+= OnComboBoxChanged
;
208 private void DisconnectComboBoxChangedSignal () {
209 comboBox
.Changed
-= OnComboBoxChanged
;
212 private void OnComboBoxChanged (object o
, EventArgs args
) {
213 ComboBox comboBox
= o
as ComboBox
;
214 int itemCount
= comboBox
.Model
.IterNChildren();
215 int selectedItem
= comboBox
.Active
;
217 if (selectedItem
== (itemCount
- 1)) {
218 EncodingsDialog dialog
= Base
.Dialogs
.Get(typeof(EncodingsDialog
)) as EncodingsDialog
;
220 dialog
.WaitForResponse();
221 SetComboBox(dialog
.ChosenNames
);
222 //SetActiveItem(comboBoxActiveItem, true);
225 comboBoxActiveItem
= selectedItem
;
226 if (SelectionChanged
!= null)
227 SelectionChanged(o
, args
);