(GUI): implemented encoding setting.

This commit is contained in:
morkt 2018-02-18 04:07:17 +04:00
parent 816e392d03
commit f2ae663ae8
2 changed files with 82 additions and 20 deletions

View File

@ -99,7 +99,7 @@ namespace GARbro.GUI
private IEnumerable<Encoding> m_encoding_list = GetEncodingList();
public IEnumerable<Encoding> TextEncodings { get { return m_encoding_list; } }
private static IEnumerable<Encoding> GetEncodingList ()
internal static IEnumerable<Encoding> GetEncodingList (bool exclude_utf16 = false)
{
var list = new HashSet<Encoding>();
list.Add (Encoding.Default);
@ -108,8 +108,11 @@ namespace GARbro.GUI
list.Add (Encoding.GetEncoding (932));
list.Add (Encoding.GetEncoding (936));
list.Add (Encoding.UTF8);
if (!exclude_utf16)
{
list.Add (Encoding.Unicode);
list.Add (Encoding.BigEndianUnicode);
}
return list;
}

View File

@ -24,8 +24,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
@ -45,6 +47,11 @@ namespace GARbro.GUI
InitializeComponent();
this.DataContext = this.ViewModel = CreateSettingsTree();
this.Closing += (s, e) => {
var section = SectionsPane.SelectedItem as SettingsSectionView;
if (section != null)
LastSelectedSection = section.Label;
};
}
SettingsViewModel ViewModel;
@ -68,9 +75,6 @@ namespace GARbro.GUI
{
ApplyChanges();
DialogResult = true;
var section = SectionsPane.SelectedItem as SettingsSectionView;
if (section != null)
LastSelectedSection = section.Label;
}
private void ApplyChanges ()
@ -103,23 +107,15 @@ namespace GARbro.GUI
IEnumerable<SettingsSectionView> EnumerateFormatsSettings ()
{
var list = new List<SettingsSectionView>();
foreach (var format in FormatCatalog.Instance.Formats.Where (f => f.Settings != null && f.Settings.Any()))
var formats = FormatCatalog.Instance.Formats.Where (f => f.Settings != null && f.Settings.Any());
foreach (var format in formats.OrderBy (f => f.Tag))
{
var pane = new WrapPanel();
foreach (var setting in format.Settings)
{
if (setting.Value is bool)
{
var view = new ResourceSettingView<bool> (setting);
view.ValueChanged += (s, e) => ViewModel.HasChanges = true;
this.OnApplyChanges += (s, e) => view.Apply();
var check_box = new CheckBox {
Template = (ControlTemplate)this.Resources["BoundCheckBox"],
DataContext = view,
};
pane.Children.Add (check_box);
}
var widget = CreateSettingWidget (setting, setting.Value as dynamic);
if (widget != null)
pane.Children.Add (widget);
}
if (pane.Children.Count > 0)
{
@ -134,6 +130,57 @@ namespace GARbro.GUI
return list;
}
UIElement CreateSettingWidget (IResourceSetting setting, bool value)
{
return new CheckBox {
Template = (ControlTemplate)this.Resources["BoundCheckBox"],
DataContext = CreateSettingView<bool> (setting),
};
}
UIElement CreateSettingWidget (IResourceSetting setting, Encoding value)
{
var view = CreateSettingView<Encoding> (setting);
// XXX make a control template in XAML instead
var container = new StackPanel {
Orientation = Orientation.Vertical,
Margin = new Thickness (2.0),
DataContext = view,
};
var caption = new TextBlock {
Text = view.Text,
ToolTip = view.Description,
};
var combo_box = new ComboBox {
ItemsSource = MainWindow.GetEncodingList (true),
Margin = new Thickness (0,4,0,0),
DisplayMemberPath = "EncodingName",
ToolTip = view.Description,
};
var binding = new Binding ("Value") {
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
};
BindingOperations.SetBinding (combo_box, ComboBox.SelectedItemProperty, binding);
container.Children.Add (caption);
container.Children.Add (combo_box);
return container;
}
UIElement CreateSettingWidget<TUnknown> (IResourceSetting setting, TUnknown value)
{
Trace.WriteLine (string.Format ("Unknown setting type {0}", value.GetType()), "[GUI]");
return null;
}
ISettingView CreateSettingView<TValue> (IResourceSetting setting)
{
var view = new ResourceSettingView<TValue> (setting);
view.ValueChanged += (s, e) => ViewModel.HasChanges = true;
this.OnApplyChanges += (s, e) => view.Apply();
return view;
}
static IEnumerable<SettingsSectionView> EnumerateSections (IEnumerable<SettingsSectionView> list)
{
foreach (var section in list)
@ -205,7 +252,19 @@ namespace GARbro.GUI
public IEnumerable<SettingsSectionView> Children { get; set; }
}
public class ResourceSettingView<TValue>
public interface ISettingView
{
IResourceSetting Source { get; }
bool IsChanged { get; }
string Text { get; }
string Description { get; }
void Apply ();
event PropertyChangedEventHandler ValueChanged;
}
public class ResourceSettingView<TValue> : ISettingView
{
public IResourceSetting Source { get; private set; }
public bool IsChanged { get; private set; }