From b5fa9cc1058ec3471a2fc4b844da1d27bf19ff33 Mon Sep 17 00:00:00 2001 From: morkt Date: Tue, 12 May 2015 13:31:27 +0400 Subject: [PATCH] added text files preview. --- App.config | 2 +- GARbro.GUI.csproj | 7 + ImagePreview.cs | 166 ++++++++++++++++++---- MainWindow.xaml | 22 ++- MainWindow.xaml.cs | 33 +++-- Properties/AssemblyInfo.cs | 6 +- Properties/Settings.Designer.cs | 8 +- Properties/Settings.settings | 4 +- Strings/guiStrings.Designer.cs | 11 +- Strings/guiStrings.resx | 3 + Strings/guiStrings.ru-RU.resx | 3 + TextViewer.xaml | 5 + TextViewer.xaml.cs | 234 ++++++++++++++++++++++++++++++++ 13 files changed, 451 insertions(+), 53 deletions(-) create mode 100644 TextViewer.xaml create mode 100644 TextViewer.xaml.cs diff --git a/App.config b/App.config index ddb4269b..922da4fb 100644 --- a/App.config +++ b/App.config @@ -56,7 +56,7 @@ - UTF-8 + 65001 diff --git a/GARbro.GUI.csproj b/GARbro.GUI.csproj index b134a86b..5c23d738 100644 --- a/GARbro.GUI.csproj +++ b/GARbro.GUI.csproj @@ -155,6 +155,9 @@ True guiStrings.resx + + TextViewer.xaml + @@ -193,6 +196,10 @@ MainWindow.xaml Code + + Designer + MSBuild:Compile + diff --git a/ImagePreview.cs b/ImagePreview.cs index 5eb2b6c5..b9e40184 100644 --- a/ImagePreview.cs +++ b/ImagePreview.cs @@ -2,7 +2,7 @@ //! \date Sun Jul 06 06:34:56 2014 //! \brief preview images. // -// Copyright (C) 2014 by morkt +// Copyright (C) 2014-2015 by morkt // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to @@ -37,6 +37,10 @@ using System.Windows.Threading; using GARbro.GUI.Strings; using GARbro.GUI.Properties; using GameRes; +using System.Text; +using System.Windows.Documents; +using System.Windows.Media; +using System.Globalization; namespace GARbro.GUI { @@ -46,6 +50,29 @@ namespace GARbro.GUI private PreviewFile m_current_preview = new PreviewFile(); private bool m_preview_pending = false; + private UIElement m_active_viewer; + public UIElement ActiveViewer + { + get { return m_active_viewer; } + set + { + if (value == m_active_viewer) + return; + m_active_viewer = value; + m_active_viewer.Visibility = Visibility.Visible; + bool exists = false; + foreach (var c in PreviewPane.Children.Cast()) + { + if (c != m_active_viewer) + c.Visibility = Visibility.Collapsed; + else + exists = true; + } + if (!exists) + PreviewPane.Children.Add (m_active_viewer); + } + } + class PreviewFile { public string Path { get; set; } @@ -66,6 +93,31 @@ namespace GARbro.GUI if (m_preview_pending) RefreshPreviewPane(); }; + ActiveViewer = ImageView; + } + + private IEnumerable m_encoding_list = GetEncodingList(); + public IEnumerable TextEncodings { get { return m_encoding_list; } } + + private static IEnumerable GetEncodingList () + { + var list = new HashSet(); + list.Add (Encoding.Default); + var oem = CultureInfo.CurrentCulture.TextInfo.OEMCodePage; + list.Add (Encoding.GetEncoding (oem)); + list.Add (Encoding.GetEncoding (932)); + list.Add (Encoding.UTF8); + list.Add (Encoding.Unicode); + list.Add (Encoding.BigEndianUnicode); + return list; + } + + private void OnEncodingSelect (object sender, SelectionChangedEventArgs e) + { + var enc = this.EncodingChoice.SelectedItem as Encoding; + if (null == enc || null == CurrentTextInput) + return; + TextView.CurrentEncoding = enc; } /// @@ -85,43 +137,110 @@ namespace GARbro.GUI if (null != current) UpdatePreviewPane (current.Source); else - PreviewPane.Source = null; + ResetPreviewPane(); + } + + void ResetPreviewPane () + { + ActiveViewer = ImageView; + ImageCanvas.Source = null; + TextView.Clear(); + CurrentTextInput = null; + } + + bool IsPreviewPossible (Entry entry) + { + return "image" == entry.Type || "script" == entry.Type + || (string.IsNullOrEmpty (entry.Type) && entry.Size < 0x100000); } void UpdatePreviewPane (Entry entry) { SetStatusText (""); var vm = ViewModel; - m_current_preview = new PreviewFile { Path = vm.Path, Name = entry.Name }; - PreviewPane.Source = null; - if (entry.Type != "image") - return; - if (vm.IsArchive) + m_current_preview = new PreviewFile { Path = vm.Path, Name = entry.Name, Entry = entry }; + ImageCanvas.Source = null; + TextView.Clear(); + if (!IsPreviewPossible (entry)) { - m_current_preview.Archive = m_app.CurrentArchive; - m_current_preview.Entry = entry; + ActiveViewer = ImageView; + return; } - if (!m_preview_worker.IsBusy) + if (vm.IsArchive) + m_current_preview.Archive = m_app.CurrentArchive; + if ("image" != entry.Type) + LoadPreviewText (m_current_preview); + else if (!m_preview_worker.IsBusy) m_preview_worker.RunWorkerAsync (m_current_preview); else m_preview_pending = true; } + private Stream m_current_text; + private Stream CurrentTextInput + { + get { return m_current_text; } + set + { + if (value == m_current_text) + return; + if (null != m_current_text) + m_current_text.Dispose(); + m_current_text = value; + } + } + + Stream OpenPreviewStream (PreviewFile preview) + { + if (null == preview.Archive) + { + string filename = Path.Combine (preview.Path, preview.Name); + return File.OpenRead (filename); + } + else + { + return preview.Archive.OpenEntry (preview.Entry); + } + } + + void LoadPreviewText (PreviewFile preview) + { + Stream file = null; + try + { + file = OpenPreviewStream (preview); + if (!TextView.IsTextFile (file)) + { + ActiveViewer = ImageView; + return; + } + var enc = EncodingChoice.SelectedItem as Encoding; + if (null == enc) + { + enc = TextView.GuessEncoding (file); + EncodingChoice.SelectedItem = enc; + } + TextView.DisplayStream (file, enc); + ActiveViewer = TextView; + CurrentTextInput = file; + file = null; + } + catch (Exception X) + { + SetStatusText (X.Message); + } + finally + { + if (file != null) + file.Dispose(); + } + } + void LoadPreviewImage (PreviewFile preview) { try { - Stream file; - if (null == preview.Archive) - { - string filename = Path.Combine (preview.Path, preview.Name); - file = new FileStream (filename, FileMode.Open, FileAccess.Read); - } - else - { - file = preview.Archive.OpenEntry (preview.Entry); - } - using (file) + using (var file = OpenPreviewStream (preview)) { var data = ImageFormat.Read (file); if (null != data) @@ -153,7 +272,8 @@ namespace GARbro.GUI { if (m_current_preview == preview) // compare by reference { - PreviewPane.Source = bitmap; + ActiveViewer = ImageView; + ImageCanvas.Source = bitmap; SetStatusText (string.Format (guiStrings.MsgImageSize, bitmap.PixelWidth, bitmap.PixelHeight, bitmap.Format.BitsPerPixel)); } @@ -165,7 +285,7 @@ namespace GARbro.GUI /// private void FitWindowExec (object sender, ExecutedRoutedEventArgs e) { - var image = PreviewPane.Source; + var image = ImageCanvas.Source; if (null == image) return; var width = image.Width + Settings.Default.lvPanelWidth.Value + 1; diff --git a/MainWindow.xaml b/MainWindow.xaml index 36402d6f..a0a095ee 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -2,6 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:GARbro.GUI" + xmlns:jv="clr-namespace:JustView" xmlns:s="clr-namespace:GARbro.GUI.Strings" xmlns:p="clr-namespace:GARbro.GUI.Properties" Title="GARbro" MinHeight="250" ResizeMode="CanResizeWithGrip" @@ -212,6 +213,15 @@ + + + + + +