2 * This file is part of Gnome Subtitles.
3 * Copyright (C) 2006-2009 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
;
25 using System
.Collections
;
28 namespace GnomeSubtitles
.Dialog
{
30 public abstract class SubtitleFileChooserDialog
: GladeDialog
{
31 private int fixedEncoding
= -1;
32 private bool isEncodingsChangeSilent
= false; //used to indicate whether a change in the encodings list should be taken into account
33 private ComboBox encodingComboBox
= null;
35 /* Protected variables */
36 protected new FileChooserDialog dialog
= null;
38 protected string chosenFilename
= String
.Empty
;
39 protected EncodingDescription chosenEncoding
;
40 protected bool hasChosenEncoding
= false;
42 protected EncodingDescription
[] encodings
= null;
45 protected SubtitleFileChooserDialog (string filename
) : base(filename
) {
46 dialog
= GetDialog() as FileChooserDialog
;
48 fixedEncoding
= GetFixedEncoding();
49 SetEncodingComboBox();
53 /* Public properties */
55 public string Filename
{
56 get { return chosenFilename; }
59 public EncodingDescription ChosenEncoding
{
60 get { return chosenEncoding; }
63 public bool HasChosenEncoding
{
64 get { return hasChosenEncoding; }
67 /* Protected members */
69 protected void SetEncodingComboBox () {
70 encodingComboBox
= GetEncodingComboBox();
72 AddInitialEncodingComboBoxItems();
73 encodingComboBox
.RowSeparatorFunc
= SeparatorFunc
;
74 FillEncodingComboBox(Base
.Config
.PrefsEncodingsShownInMenu
);
77 protected bool SeparatorFunc (TreeModel model
, TreeIter iter
) {
78 string text
= (string)model
.GetValue(iter
, 0);
79 return ((text
!= null) && (text
.CompareTo("-") == 0));
82 protected int GetActiveEncodingComboBoxItem () {
83 return encodingComboBox
.Active
;
88 protected virtual int GetFixedEncoding () {
92 protected virtual void AddInitialEncodingComboBoxItems () {
95 /* Abstract members */
97 protected abstract ComboBox
GetEncodingComboBox ();
101 /* Private members */
103 private void LoadEncodings (string[] names
) {
104 ArrayList encodings
= new ArrayList();
105 bool fixedCodePageInserted
= false;
107 foreach (string name
in names
) {
108 EncodingDescription description
= new EncodingDescription();
109 if (Encodings
.Find(name
, ref description
)) {
110 encodings
.Add(description
);
111 if (description
.CodePage
== fixedEncoding
)
112 fixedCodePageInserted
= true;
116 /* Insert fixed encoding code page if not already inserted */
117 if ((fixedEncoding
!= -1) && (!fixedCodePageInserted
)) {
118 EncodingDescription description
= new EncodingDescription();
119 if (Encodings
.Find(fixedEncoding
, ref description
))
120 encodings
.Add(description
);
124 encodings
.Insert(0, Encodings
.SystemDefault
);
126 this.encodings
= (EncodingDescription
[])encodings
.ToArray(typeof(EncodingDescription
));
129 private void FillEncodingComboBox (string[] names
) {
130 LoadEncodings(names
);
134 foreach (EncodingDescription encoding
in encodings
) {
135 encodingComboBox
.AppendText(encoding
.Description
+ " (" + encoding
.Name
+ ")");
136 if (encoding
.CodePage
== fixedEncoding
) {
137 activeItem
= currentItem
;
142 encodingComboBox
.AppendText("-");
143 encodingComboBox
.AppendText(Catalog
.GetString("Add or Remove..."));
145 encodingComboBox
.Active
= activeItem
;
148 private void UpdateEncodingComboBox (ComboBox comboBox
, string[] names
) {
149 isEncodingsChangeSilent
= true;
151 /* Get the first elements that are not in the model */
152 int itemCount
= comboBox
.Model
.IterNChildren();
153 int knownCount
= encodings
.Length
+ 2; //Add 2 for horizontal bar and item at the end
154 int remainingCount
= itemCount
- knownCount
;
156 string[] remainingItems
= new string[remainingCount
];
158 ListStore store
= comboBox
.Model
as ListStore
;
159 foreach (object[] row
in store
) {
160 if (rowNumber
== remainingCount
)
163 remainingItems
[rowNumber
] = row
[0] as string;
169 foreach (string item
in remainingItems
)
170 comboBox
.AppendText(item
);
172 FillEncodingComboBox(names
);
174 isEncodingsChangeSilent
= false;
177 #pragma warning disable 169 //Disables warning about handlers not being used
179 private void OnEncodingComboBoxChanged (object o
, EventArgs args
) {
180 if (isEncodingsChangeSilent
)
183 ComboBox comboBox
= o
as ComboBox
;
184 int itemCount
= comboBox
.Model
.IterNChildren();
185 if (comboBox
.Active
== (itemCount
- 1)) {
186 EncodingsDialog dialog
= Base
.Dialogs
.Get(typeof(EncodingsDialog
)) as EncodingsDialog
;
188 dialog
.WaitForResponse();
189 UpdateEncodingComboBox(comboBox
, dialog
.ChosenNames
);