From 88b018607bdc27c8691c76ff1521908d8648e865 Mon Sep 17 00:00:00 2001 From: Nick Vella Date: Sat, 3 Aug 2019 22:44:46 +1000 Subject: [PATCH] LocEdit: finish implementation --- src/managed/LocEdit/MainForm.Designer.cs | 24 ++++- src/managed/LocEdit/MainForm.cs | 116 +++++++++++++++++++++-- src/managed/LocEdit/MainForm.resx | 22 ++--- 3 files changed, 139 insertions(+), 23 deletions(-) diff --git a/src/managed/LocEdit/MainForm.Designer.cs b/src/managed/LocEdit/MainForm.Designer.cs index 113ff7b4..4df5c26f 100644 --- a/src/managed/LocEdit/MainForm.Designer.cs +++ b/src/managed/LocEdit/MainForm.Designer.cs @@ -29,6 +29,7 @@ private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); this.toolStrip = new System.Windows.Forms.ToolStrip(); this.toolStripButtonLoad = new System.Windows.Forms.ToolStripButton(); this.toolStripButtonSave = new System.Windows.Forms.ToolStripButton(); @@ -66,8 +67,8 @@ this.toolStripButtonLoad.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; this.toolStripButtonLoad.ImageTransparentColor = System.Drawing.Color.Magenta; this.toolStripButtonLoad.Name = "toolStripButtonLoad"; - this.toolStripButtonLoad.Size = new System.Drawing.Size(53, 22); - this.toolStripButtonLoad.Text = "Load"; + this.toolStripButtonLoad.Size = new System.Drawing.Size(56, 22); + this.toolStripButtonLoad.Text = "Open"; this.toolStripButtonLoad.Click += new System.EventHandler(this.ToolStripButtonLoad_Click); // // toolStripButtonSave @@ -78,6 +79,7 @@ this.toolStripButtonSave.Name = "toolStripButtonSave"; this.toolStripButtonSave.Size = new System.Drawing.Size(51, 22); this.toolStripButtonSave.Text = "Save"; + this.toolStripButtonSave.Click += new System.EventHandler(this.ToolStripButtonSave_Click); // // tableLayoutPanel1 // @@ -127,7 +129,7 @@ this.buttonFind.Name = "buttonFind"; this.buttonFind.Size = new System.Drawing.Size(75, 23); this.buttonFind.TabIndex = 1; - this.buttonFind.Text = "Find Next"; + this.buttonFind.Text = "Next (F3)"; this.buttonFind.UseVisualStyleBackColor = true; this.buttonFind.Click += new System.EventHandler(this.ButtonFind_Click); // @@ -149,12 +151,22 @@ this.dataGridColKey, this.dataGridColValue, this.dataGridColComment}); + dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Window; + dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.ControlText; + dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight; + dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText; + dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True; + this.dataGridView.DefaultCellStyle = dataGridViewCellStyle1; this.dataGridView.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGridView.Location = new System.Drawing.Point(3, 3); + this.dataGridView.MultiSelect = false; this.dataGridView.Name = "dataGridView"; this.dataGridView.RowHeadersWidth = 72; this.dataGridView.Size = new System.Drawing.Size(794, 383); this.dataGridView.TabIndex = 1; + this.dataGridView.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.DataGridView_CellValueChanged); // // dataGridColKey // @@ -162,6 +174,7 @@ this.dataGridColKey.HeaderText = "Key"; this.dataGridColKey.MinimumWidth = 9; this.dataGridColKey.Name = "dataGridColKey"; + this.dataGridColKey.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // dataGridColValue // @@ -169,6 +182,7 @@ this.dataGridColValue.HeaderText = "Value"; this.dataGridColValue.MinimumWidth = 9; this.dataGridColValue.Name = "dataGridColValue"; + this.dataGridColValue.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // dataGridColComment // @@ -176,6 +190,7 @@ this.dataGridColComment.HeaderText = "Comment"; this.dataGridColComment.MinimumWidth = 9; this.dataGridColComment.Name = "dataGridColComment"; + this.dataGridColComment.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // MainForm // @@ -187,6 +202,7 @@ this.KeyPreview = true; this.Name = "MainForm"; this.Text = "LocEdit"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); this.toolStrip.ResumeLayout(false); this.toolStrip.PerformLayout(); this.tableLayoutPanel1.ResumeLayout(false); @@ -208,10 +224,10 @@ private System.Windows.Forms.TextBox textBoxFind; private System.Windows.Forms.Button buttonFind; private System.Windows.Forms.DataGridView dataGridView; + private System.Windows.Forms.Label label1; private System.Windows.Forms.DataGridViewTextBoxColumn dataGridColKey; private System.Windows.Forms.DataGridViewTextBoxColumn dataGridColValue; private System.Windows.Forms.DataGridViewTextBoxColumn dataGridColComment; - private System.Windows.Forms.Label label1; } } diff --git a/src/managed/LocEdit/MainForm.cs b/src/managed/LocEdit/MainForm.cs index ae5c6425..4b50995a 100644 --- a/src/managed/LocEdit/MainForm.cs +++ b/src/managed/LocEdit/MainForm.cs @@ -22,13 +22,24 @@ namespace LocEdit InitializeComponent(); } + private bool _modified = false; + public bool Modified + { + get => _modified; + set + { + _modified = value; + Text = $"LocEdit: {_loadedFile}{(_modified ? "*" : "")}"; + } + } + public void LoadFile(string filePath) { _loadedFile = filePath; - Text = $"LocEdit: {filePath}"; + Modified = false; dataGridView.Rows.Clear(); - using (CsvParser csvParser = new CsvParser(new StreamReader(new FileStream(Path.GetFullPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.Default), true)) + using (CsvParser csvParser = new CsvParser(new StreamReader(new FileStream(Path.GetFullPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8), true)) { foreach (string[] line in csvParser) { @@ -39,9 +50,37 @@ namespace LocEdit dataGridView.Rows.Add(new LocDataGridViewRow(line[0], value, comment)); } } + dataGridView.AutoResizeRows(); } + public void SaveFile(string filePath) + { + _loadedFile = filePath; + Modified = false; + + var sb = new StringBuilder(); + sb.AppendLine("Name,Value,Comment"); + + foreach(DataGridViewRow row in dataGridView.Rows) + { + if (row.Cells.Count < 3) continue; + + var key = (string)row.Cells[0].Value; + var value = (string)row.Cells[1].Value; + var comment = (string)row.Cells[2].Value; + + if (key == null || value == null || comment == null) continue; + + sb.Append($"{Helpers.CsvizeString(key)},"); + sb.Append($"{Helpers.CsvizeString(value)},"); + sb.Append($"{Helpers.CsvizeString(comment)}"); + sb.AppendLine(); + } + + File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8); + } + public void FindNext(string query) { int y = dataGridView.CurrentCell != null ? dataGridView.CurrentCell.RowIndex + 1 : 0; @@ -49,7 +88,7 @@ namespace LocEdit while(y < dataGridView.Rows.Count - 1) { - if (((string)dataGridView.Rows[y].Cells[0].Value).Contains(query)) + if (((string)dataGridView.Rows[y].Cells[0].Value).ToLower().Contains(query.ToLower())) { dataGridView.CurrentCell = dataGridView.Rows[y].Cells[0]; return; @@ -71,6 +110,12 @@ namespace LocEdit return true; } + if (keyData == (Keys.Control | Keys.S)) + { + ShowSaveDialog(); + return true; + } + if (keyData == (Keys.Control | Keys.F)) { textBoxFind.Focus(); @@ -78,6 +123,12 @@ namespace LocEdit } + if (keyData == Keys.F3) + { + FindNext(textBoxFind.Text); + return true; + } + return base.ProcessCmdKey(ref msg, keyData); } @@ -87,10 +138,18 @@ namespace LocEdit { openFileDialog1.Filter = "Localization CSV File (*.csv)|*.csv"; - if (openFileDialog1.ShowDialog() == DialogResult.OK) - { - LoadFile(openFileDialog1.FileName); - } + if (openFileDialog1.ShowDialog() == DialogResult.OK) LoadFile(openFileDialog1.FileName); + } + } + + private void ShowSaveDialog() + { + using (SaveFileDialog saveFileDialog1 = new SaveFileDialog()) + { + saveFileDialog1.FileName = _loadedFile; + saveFileDialog1.Filter = "Localization CSV File (*.csv)|*.csv"; + + if (saveFileDialog1.ShowDialog() == DialogResult.OK) SaveFile(saveFileDialog1.FileName); } } @@ -107,7 +166,48 @@ namespace LocEdit private void TextBoxFind_KeyUp(object sender, KeyEventArgs e) { - if(textBoxFind.Focused && e.KeyCode == Keys.Enter) FindNext(textBoxFind.Text); + if (textBoxFind.Focused && e.KeyCode == Keys.Enter) + { + FindNext(textBoxFind.Text); + e.Handled = true; + } + } + + private void ToolStripButtonSave_Click(object sender, EventArgs e) + => ShowSaveDialog(); + + private void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) + => Modified = true; + + private void MainForm_FormClosing(object sender, FormClosingEventArgs e) + { + if (!Modified) return; + + var result = MessageBox.Show(this, "There are unsaved changes. Are you sure you want to exit?", "LocEdit", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); + if (result == DialogResult.No) e.Cancel = true; + } + } + + internal static class Helpers + { + public static string CsvizeString(string input) + { + var sb = new StringBuilder(); + bool shouldQuote = input.Contains(",") || input.Contains("\n") || (input != string.Empty && input[0] == '"'); + + if (shouldQuote) + { + sb.Append('"'); + sb.Append(input.Replace("\"", "\"\"")); // Replace double-quotes with double-double-quotes + sb.Append('"'); + } + else + { + sb.Append(input); + } + + + return sb.ToString(); } } diff --git a/src/managed/LocEdit/MainForm.resx b/src/managed/LocEdit/MainForm.resx index c102cad7..565047d3 100644 --- a/src/managed/LocEdit/MainForm.resx +++ b/src/managed/LocEdit/MainForm.resx @@ -124,17 +124,17 @@ iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8 - YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAIwSURBVDhPzZBdSFNhGMdfhK7Cm+oquu2yr8voKi/KsiIk - SCgjWOrm3HLp1haxGVnBloNktmYrMpooRCtcBmI0pWjLNaeLyFYy7Sp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