3
// Copyright (C) 2008 Chris Szikszoy
5
// This program 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 3 of the License, or
8
// (at your option) any later version.
10
// This program 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, see <http://www.gnu.org/licenses/>.
21
using System.Reflection;
22
using System.Collections.Generic;
23
using System.Threading;
31
public partial class ConfigUI : Gtk.Bin
33
static IPreferences TranslatePluginPrefs;
43
TranslatePluginPrefs = Services.Preferences.Get<Translate.ConfigUI> ();
46
public void SetupColumns()
49
Gtk.TreeViewColumn FlagCol = new Gtk.TreeViewColumn();
50
Gtk.CellRendererPixbuf pixbuf = new Gtk.CellRendererPixbuf ();
51
FlagCol.PackStart(pixbuf, false);
52
treeEnableLang.AppendColumn(FlagCol);
53
FlagCol.AddAttribute(pixbuf, "pixbuf", 0);
55
Gtk.TreeViewColumn LanguageCol = new Gtk.TreeViewColumn();
56
Gtk.CellRendererText text = new Gtk.CellRendererText();
57
LanguageCol.PackStart(text, true);
58
treeEnableLang.AppendColumn(LanguageCol);
59
LanguageCol.AddAttribute(text, "text", 1);
61
Gtk.TreeViewColumn IsEnabled = new Gtk.TreeViewColumn();
62
Gtk.CellRendererToggle EnabledToggle = new Gtk.CellRendererToggle();
63
EnabledToggle.Activatable = true;
64
EnabledToggle.Toggled += CheckEnable;
65
IsEnabled.PackStart(EnabledToggle, false);
66
treeEnableLang.AppendColumn(IsEnabled);
67
IsEnabled.AddAttribute(EnabledToggle, "active", 2);
69
//add a dummy column to fill the rest
70
Gtk.TreeViewColumn DummyCol = new Gtk.TreeViewColumn();
71
Gtk.CellRendererText DummyText = new Gtk.CellRendererText();
72
DummyCol.PackStart(DummyText, true);
73
treeEnableLang.AppendColumn(DummyCol);
74
DummyCol.AddAttribute(DummyText, "text", 3);
76
public void FillProviderCmb()
80
Gtk.CellRendererText cell = new Gtk.CellRendererText();
81
Gtk.CellRendererPixbuf pixbuf = new Gtk.CellRendererPixbuf ();
82
cmbProvider.PackStart(pixbuf, true);
83
cmbProvider.PackStart(cell, true);
84
cmbProvider.AddAttribute(pixbuf, "pixbuf" , 0);
85
cmbProvider.AddAttribute(cell, "text", 1);
87
Gtk.ListStore ProvidersList = new Gtk.ListStore(typeof (Gdk.Pixbuf), typeof (string));
89
// Get an instance of each pastebin provider in this assembly.
90
var providers = from type in Assembly.GetExecutingAssembly().GetTypes()
91
where type.GetInterface("Translate.ITranslateProvider") != null
92
select Activator.CreateInstance(type);
96
foreach (ITranslateProvider provider in providers)
98
Icon = provider.Icon.Split(new char[] {'@'});
99
using (Gdk.Pixbuf temp = Gdk.Pixbuf.LoadFromResource(Icon[0]))
101
pb = temp.ScaleSimple((20 * temp.Width)/temp.Height, 20, Gdk.InterpType.Bilinear);
103
ProvidersList.AppendValues (pb, provider.Name);
105
cmbProvider.Model = ProvidersList;
106
//set selection to what's in GConf, if we can
108
if (SearchCombobox(out ti, cmbProvider, SelectedProvider,1))
109
cmbProvider.SetActiveIter(ti);
112
public void FillSourceCmb(ITranslateProvider Translator)
114
cmbDefaultSource.Clear();
116
Gtk.CellRendererText cell = new Gtk.CellRendererText();
117
Gtk.CellRendererPixbuf pixbuf = new Gtk.CellRendererPixbuf ();
118
cmbDefaultSource.PackStart(pixbuf, true);
119
cmbDefaultSource.PackStart(cell, true);
120
cmbDefaultSource.AddAttribute(pixbuf, "pixbuf" , 0);
121
cmbDefaultSource.AddAttribute(cell, "text", 1);
124
Gtk.ListStore SourceList = new Gtk.ListStore(typeof (Gdk.Pixbuf), typeof (string), typeof (string));
126
Gdk.Pixbuf LanguageFlag = null;
127
const int scale_height = 20;
128
string[] Icon = null;
129
if (Translator.SupportsAutoDetect)
130
SourceList.AppendValues(null, Catalog.GetString("Auto Detect (Recommended)"), Translator.AutoDetectCode);
131
foreach (LanguageItem Lang in Translator.SupportedLanguages)
133
Icon = Lang.Icon.Split(new char[] {'@'});
134
using (Gdk.Pixbuf temp = Gdk.Pixbuf.LoadFromResource(Icon[0]))
136
LanguageFlag = temp.ScaleSimple((scale_height * temp.Width) / temp.Height, scale_height, Gdk.InterpType.Bilinear);
138
SourceList.AppendValues(LanguageFlag, Lang.Name, Lang.Code);
141
cmbDefaultSource.Model = SourceList;
142
//set default source to auto if it's enabled, else try what's in gconf, else, just the first in the list
144
if ((Translator.SupportsAutoDetect) && (SearchCombobox(out ti, cmbDefaultSource, Translator.AutoDetectCode, 2)))
145
cmbDefaultSource.SetActiveIter(ti);
146
else if (SearchCombobox(out ti, cmbDefaultSource, SelectedSourceLang, 2))
147
cmbDefaultSource.SetActiveIter(ti);
150
cmbDefaultSource.Model.GetIterFirst(out ti);
151
cmbDefaultSource.SetActiveIter(ti);
155
private void FillIfaceCmb(ITranslateProvider Translator)
157
cmbDefaultIface.Clear();
159
Gtk.CellRendererText cell = new Gtk.CellRendererText();
160
Gtk.CellRendererPixbuf pixbuf = new Gtk.CellRendererPixbuf ();
161
cmbDefaultIface.PackStart(pixbuf, true);
162
cmbDefaultIface.PackStart(cell, true);
163
cmbDefaultIface.AddAttribute(pixbuf, "pixbuf" , 0);
164
cmbDefaultIface.AddAttribute(cell, "text", 1);
166
Gtk.ListStore IfaceList = new Gtk.ListStore(typeof (Gdk.Pixbuf), typeof (string), typeof (string));
168
Gdk.Pixbuf LanguageFlag = null;
169
const int scale_height = 20;
170
string[] Icon = null;
171
foreach (LanguageItem Lang in Translator.SupportedLanguages)
173
Icon = Lang.Icon.Split(new char[] {'@'});
174
using (Gdk.Pixbuf temp = Gdk.Pixbuf.LoadFromResource(Icon[0]))
176
LanguageFlag = temp.ScaleSimple((scale_height * temp.Width) / temp.Height, scale_height, Gdk.InterpType.Bilinear);
178
IfaceList.AppendValues(LanguageFlag, Lang.Name, Lang.Code);
181
cmbDefaultIface.Model = IfaceList;
182
//set selection to what's in GConf, if we can
184
if (SearchCombobox(out ti, cmbDefaultIface, SelectedIfaceLang, 2))
185
cmbDefaultIface.SetActiveIter(ti);
188
cmbDefaultIface.Model.GetIterFirst(out ti);
189
cmbDefaultIface.SetActiveIter(ti);
193
private void FillEnableBox(ITranslateProvider Translator)
195
Gtk.ListStore LanguageList = new Gtk.ListStore(typeof (Gdk.Pixbuf), typeof (string), typeof (bool), typeof (string), typeof (LanguageNode));
196
const int scale_height = 25;
197
Gdk.Pixbuf LanguageFlag = null;
198
string[] Icon = null;
199
LanguageNode LanguageTreeNode = null;
201
foreach (LanguageItem Lang in Translator.SupportedLanguages)
203
Icon = Lang.Icon.Split(new char[] {'@'});
204
using (Gdk.Pixbuf temp = Gdk.Pixbuf.LoadFromResource(Icon[0]))
206
LanguageFlag = temp.ScaleSimple((scale_height * temp.Width) / temp.Height, scale_height, Gdk.InterpType.Bilinear);
208
LanguageTreeNode = new LanguageNode(LanguageFlag, Lang.Name, Lang.Code);
209
LanguageList.AppendValues(LanguageTreeNode.Icon, LanguageTreeNode.Name, LanguageTreeNode.IsEnabled, string.Empty, LanguageTreeNode);
212
treeEnableLang.HeadersVisible = false;
213
treeEnableLang.Model = LanguageList;
216
private void CheckEnable(object sender, Gtk.ToggledArgs e)
219
Gtk.TreeModel model = treeEnableLang.Model;
221
if(model.GetIter(out iter, new Gtk.TreePath(e.Path)))
223
LanguageNode SelectedLanguage = (LanguageNode)model.GetValue(iter, 4);
224
if (SelectedLanguage.Name != "Auto Detect Language")
226
bool currentState = SelectedLanguage.IsEnabled;
227
SelectedLanguage.IsEnabled = !currentState;
228
model.SetValue(iter, 2, SelectedLanguage.IsEnabled);
233
public bool SearchCombobox(out Gtk.TreeIter ti, Gtk.ComboBox box, string val, int col)
235
box.Model.GetIterFirst(out ti);
238
if ((string)box.Model.GetValue(ti,col) == val)
240
} while (box.Model.IterNext(ref ti));
245
protected virtual void ProviderSelectionChanged (object sender, System.EventArgs e)
247
Gtk.ComboBox ProviderBox = (Gtk.ComboBox)sender;
249
ProviderBox.GetActiveIter(out ti);
250
SelectedProvider = (string)ProviderBox.Model.GetValue(ti,1);
251
ITranslateProvider Translator = TranslateProviderFactory.GetProviderFromPreferences();
254
FillSourceCmb(Translator);
255
if (Translator.SupportsIfaceLang)
256
FillIfaceCmb(Translator);
259
cmbDefaultIface.Clear();
260
cmbDefaultIface.Model = null;
262
FillEnableBox(Translator);
266
protected virtual void SourceSelectionChanged (object sender, System.EventArgs e)
268
Gtk.ComboBox SourceBox = (Gtk.ComboBox)sender;
270
SourceBox.GetActiveIter(out ti);
271
//column 2 holds the language "code"
272
SelectedSourceLang = (string)SourceBox.Model.GetValue(ti,2);
275
protected virtual void IfaceSelectionChanged (object sender, System.EventArgs e)
277
Gtk.ComboBox IfaceBox = (Gtk.ComboBox)sender;
279
IfaceBox.GetActiveIter(out ti);
280
//column 2 holds the language "code"
281
SelectedIfaceLang = (string)IfaceBox.Model.GetValue(ti,2);
284
public static string SelectedProvider
288
ITranslateProvider DefaultProvider = new Google();
289
return TranslatePluginPrefs.Get<string> ("SelectedProvider", DefaultProvider.Name);
291
set { TranslatePluginPrefs.Set<string> ("SelectedProvider", value); }
293
public static string SelectedSourceLang {
296
ITranslateProvider Translator = TranslateProviderFactory.GetProviderFromPreferences();
297
return TranslatePluginPrefs.Get<string> ("SelectedSourceLanguage", Translator.DefaultSourceCode);
299
set { TranslatePluginPrefs.Set<string> ("SelectedSourceLanguage", value); }
301
public static string SelectedIfaceLang {
304
ITranslateProvider Translator = TranslateProviderFactory.GetProviderFromPreferences();
305
return TranslatePluginPrefs.Get<string> ("SelectedIfaceLanguage", Translator.DefaultIfaceCode);
307
set { TranslatePluginPrefs.Set<string> ("SelectedIfaceLanguage", value); }