90 lines
3.3 KiB
C#
90 lines
3.3 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using Microsoft.Win32;
|
|
|
|
namespace HuffmanEditor;
|
|
/// <summary>
|
|
/// Interaction logic for MainWindow.xaml
|
|
/// </summary>
|
|
public partial class MainWindow : Window
|
|
{
|
|
private string filePath = string.Empty;
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void Button_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
File.WriteAllBytes("C:\\Users\\retrd\\Documents\\АК.rt0", Huffman.EncodeString(File.ReadAllText("C:\\Users\\retrd\\Documents\\АК.txt")));
|
|
//label.Content = Huffman.DecodeString(File.ReadAllBytes("C:\\Users\\retrd\\Documents\\АК.rt0"));
|
|
}
|
|
|
|
private void OpenFileClick(object sender, RoutedEventArgs e)
|
|
{
|
|
OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Text files (*.txt;*.rt0)|*.txt;*.rt0", InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) };
|
|
if (openFileDialog.ShowDialog()!.Value)
|
|
{
|
|
if (openFileDialog.FileName.EndsWith(".rt0"))
|
|
{
|
|
textType.Content = "Zipped text (rt0)";
|
|
textBox.Text = Huffman.DecodeString(File.ReadAllBytes(openFileDialog.FileName));
|
|
}
|
|
else
|
|
{
|
|
textType.Content = "Plain text (txt)";
|
|
textBox.Text = File.ReadAllText(openFileDialog.FileName);
|
|
}
|
|
filePath = openFileDialog.FileName;
|
|
}
|
|
}
|
|
|
|
private void SaveFileClick(object sender, RoutedEventArgs e)
|
|
{
|
|
if (filePath == string.Empty) SaveAsClick(sender, e);
|
|
else
|
|
{
|
|
File.WriteAllBytes(filePath, Huffman.EncodeString(textBox.Text));
|
|
}
|
|
}
|
|
|
|
private void SaveAsClick(object sender, RoutedEventArgs e)
|
|
{
|
|
SaveFileDialog saveFileDialog = new SaveFileDialog() { Filter = "Text file (*.txt)|*.txt|Zipped file (*.rt0)|*.rt0", InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) };
|
|
if (saveFileDialog.ShowDialog()!.Value)
|
|
{
|
|
if (saveFileDialog.FileName.EndsWith(".rt0"))
|
|
{
|
|
textType.Content = "Zipped text (rt0)";
|
|
File.WriteAllBytes(saveFileDialog.FileName, Huffman.EncodeString(textBox.Text));
|
|
}
|
|
else
|
|
{
|
|
textType.Content = "Plain text (txt)";
|
|
File.WriteAllText(saveFileDialog.FileName, textBox.Text);
|
|
}
|
|
filePath = saveFileDialog.FileName;
|
|
}
|
|
}
|
|
|
|
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
|
|
{
|
|
characterCount.Content = $"Characters: {textBox.Text.Length}";
|
|
var lineIndex = textBox.GetLineIndexFromCharacterIndex(textBox.CaretIndex);
|
|
var columnIndex = textBox.CaretIndex - textBox.GetCharacterIndexFromLineIndex(lineIndex);
|
|
cursorAddress.Content = $"Ln {lineIndex}, Col {columnIndex}";
|
|
}
|
|
|
|
private void WordWrapClick(object sender, RoutedEventArgs e)
|
|
{
|
|
textBox.TextWrapping = (sender as CheckBox)!.IsChecked!.Value ? TextWrapping.Wrap : TextWrapping.NoWrap;
|
|
}
|
|
|
|
private void StatusStripClick(object sender, RoutedEventArgs e)
|
|
{
|
|
statusStrip.Height = (sender as CheckBox)!.IsChecked!.Value ? new GridLength(25, GridUnitType.Pixel) : new GridLength(0, GridUnitType.Pixel);
|
|
}
|
|
} |