From 9b5840a1f1e0ae32817afa47dd760273ad3c46c3 Mon Sep 17 00:00:00 2001 From: niksedk Date: Tue, 28 Dec 2010 10:13:32 +0000 Subject: [PATCH] Remember English language in spell check (if using several like ca, gb, us, za) + a little cleanup git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@205 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Controls/SubtitleListView.cs | 24 ++--- src/Controls/VideoPlayerContainer.cs | 26 +----- src/Forms/Main.cs | 16 ++-- .../Networking/NikseWebServiceSession.cs | 26 +++--- src/Logic/Utilities.cs | 87 +++++++++++++++---- src/Logic/WavePeak.cs | 19 ++-- 6 files changed, 116 insertions(+), 82 deletions(-) diff --git a/src/Controls/SubtitleListView.cs b/src/Controls/SubtitleListView.cs index 15fca775a..65c8c4004 100644 --- a/src/Controls/SubtitleListView.cs +++ b/src/Controls/SubtitleListView.cs @@ -6,7 +6,7 @@ using Nikse.SubtitleEdit.Logic; namespace Nikse.SubtitleEdit.Controls { - public class SubtitleListView : ListView + public sealed class SubtitleListView : ListView { public const int ColumnIndexNumber = 0; public const int ColumnIndexStart = 1; @@ -19,7 +19,7 @@ namespace Nikse.SubtitleEdit.Controls private int _firstVisibleIndex = -1; private string _lineSeparatorString = " || "; public string SubtitleFontName = "Tahoma"; - public bool SubtitleFontBold = false; + public bool SubtitleFontBold; public int SubtitleFontSize = 8; public bool IsAlternateTextColumnVisible { get; private set; } public bool IsExtraColumnVisible { get; private set; } @@ -50,7 +50,7 @@ namespace Nikse.SubtitleEdit.Controls public SubtitleListView() { - this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + Font = new Font("Tahoma", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0); Columns.AddRange(new[] { new ColumnHeader { Text="#", Width=55 }, @@ -82,7 +82,7 @@ namespace Nikse.SubtitleEdit.Controls } int length = Columns[ColumnIndexNumber].Width + Columns[ColumnIndexStart].Width + Columns[ColumnIndexEnd].Width + Columns[ColumnIndexDuration].Width; - int lengthAvailable = this.Width - length; + int lengthAvailable = Width - length; Columns[ColumnIndexText].Width = (lengthAvailable / 2) - 15; Columns[ColumnIndexTextAlternate].Width = -2; @@ -198,7 +198,7 @@ namespace Nikse.SubtitleEdit.Controls FirstVisibleIndex = -1; } - private string GetOriginalSubtitle(int index, Paragraph paragraph, List originalParagraphs) + private static string GetOriginalSubtitle(int index, Paragraph paragraph, List originalParagraphs) { if (index < originalParagraphs.Count && Math.Abs(originalParagraphs[index].StartTime.TotalMilliseconds - paragraph.StartTime.TotalMilliseconds) < 50) return originalParagraphs[index].Text; @@ -336,7 +336,7 @@ namespace Nikse.SubtitleEdit.Controls Columns.Add(new ColumnHeader { Text = title, Width = 80 }); int length = Columns[ColumnIndexNumber].Width + Columns[ColumnIndexStart].Width + Columns[ColumnIndexEnd].Width + Columns[ColumnIndexDuration].Width; - int lengthAvailable = this.Width - length; + int lengthAvailable = Width - length; if (IsAlternateTextColumnVisible) { @@ -470,12 +470,12 @@ namespace Nikse.SubtitleEdit.Controls { if (index >= 0 && index < Items.Count) { - ListViewItem item = Items[index]; - Items[index].Text = string.Empty; - Items[index].SubItems[ColumnIndexStart].Text = string.Empty; - Items[index].SubItems[ColumnIndexEnd].Text = string.Empty; - Items[index].SubItems[ColumnIndexDuration].Text = string.Empty; - Items[index].SubItems[ColumnIndexText].Text = string.Empty; + ListViewItem item = Items[index]; + item.Text = string.Empty; + item.SubItems[ColumnIndexStart].Text = string.Empty; + item.SubItems[ColumnIndexEnd].Text = string.Empty; + item.SubItems[ColumnIndexDuration].Text = string.Empty; + item.SubItems[ColumnIndexText].Text = string.Empty; SetBackgroundColor(index, color); } diff --git a/src/Controls/VideoPlayerContainer.cs b/src/Controls/VideoPlayerContainer.cs index 400a45442..080c771b4 100644 --- a/src/Controls/VideoPlayerContainer.cs +++ b/src/Controls/VideoPlayerContainer.cs @@ -305,7 +305,7 @@ namespace Nikse.SubtitleEdit.Controls _labelTimeCode.Location = new Point(280, 29); _labelTimeCode.ForeColor = Color.FromArgb(100, 200, 200); - _labelTimeCode.Font = new System.Drawing.Font(_labelTimeCode.Font.FontFamily, 7); + _labelTimeCode.Font = new Font(_labelTimeCode.Font.FontFamily, 7); _labelTimeCode.AutoSize = true; _panelcontrols.Controls.Add(_labelTimeCode); @@ -321,29 +321,6 @@ namespace Nikse.SubtitleEdit.Controls public void VideoPlayerContainerResize(object sender, EventArgs e) { - //_pictureBoxPlay.Left = 22; - //_pictureBoxPlay.Top = 126 - 113; - //_pictureBoxPlayDown.Location = new Point(22, 127 - 113); - //_pictureBoxPlayOver.Location = new Point(23, 126 - 113); - - //_pictureBoxPause.Location = new Point(23, 126 - 113); - //_pictureBoxPauseDown.Location = new Point(22, 127 - 113); - //_pictureBoxPauseOver.Location = new Point(22, 127 - 113); - - //_pictureBoxStop.Location = new Point(60, 130 - 113); - //_pictureBoxStopDown.Location = new Point(60, 130 - 113); - //_pictureBoxStopOver.Location = new Point(60, 130 - 113); - - //_pictureBoxMute.Location = new Point(91, 131 - 113); - //_pictureBoxMuteDown.Location = new Point(91, 131 - 113); - //_pictureBoxMuteOver.Location = new Point(91, 131 - 113); - - //_pictureBoxProgressBar.Location = new Point(47, 118 - 113); - //_pictureBoxVolumeBarBackground.Location = new Point(111, 135 - 113); - //_pictureBoxVolumeBar.Location = new Point(120, 139 - 113); - //_pictureBoxProgressbarBackground.Location = new Point(43, 114 - 113); - - PanelPlayer.Height = Height - ControlsHeight; PanelPlayer.Width = Width; @@ -354,7 +331,6 @@ namespace Nikse.SubtitleEdit.Controls _pictureBoxFastForward.Left = Width - 49; _labelTimeCode.Left = Width - 170; - } #region PlayPauseButtons diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index c42751be6..68f5fd8f8 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -7268,7 +7268,7 @@ namespace Nikse.SubtitleEdit.Forms networkNew.Initialize(_networkSession, _fileName); if (networkNew.ShowDialog(this) == DialogResult.OK) { - _networkSession.Log(_networkSession.CurrentUser.UserName + ": Started session " + _networkSession.SessionId + " at " + DateTime.Now.ToLongTimeString()); + _networkSession.AppendToLog(_networkSession.CurrentUser.UserName + ": Started session " + _networkSession.SessionId + " at " + DateTime.Now.ToLongTimeString()); toolStripStatusNetworking.Visible = true; toolStripStatusNetworking.Text = "Network mode"; EnableDisableControlsNotWorkingInNetworkMode(false); @@ -7298,7 +7298,7 @@ namespace Nikse.SubtitleEdit.Forms toolStripStatusNetworking.Visible = true; toolStripStatusNetworking.Text = "Network mode"; EnableDisableControlsNotWorkingInNetworkMode(false); - _networkSession.Log(_networkSession.CurrentUser.UserName + ": Joined session " + _networkSession.SessionId + " at " + DateTime.Now.ToLongTimeString()); + _networkSession.AppendToLog(_networkSession.CurrentUser.UserName + ": Joined session " + _networkSession.SessionId + " at " + DateTime.Now.ToLongTimeString()); SubtitleListview1.ShowExtraColumn("User/action"); _subtitleListViewIndex = -1; _oldSelectedParagraph = null; @@ -7410,7 +7410,7 @@ namespace Nikse.SubtitleEdit.Forms { _networkChat.AddUser(update.User); } - _networkSession.Log("New user: " + update.User.UserName + " (" + update.User.Ip + ")"); + _networkSession.AppendToLog("New user: " + update.User.UserName + " (" + update.User.Ip + ")"); } else if (update.Action == "MSG") { @@ -7425,7 +7425,7 @@ namespace Nikse.SubtitleEdit.Forms { _networkChat.AddChatMessage(update.User, update.Text); } - _networkSession.Log("Message: " + update.User.UserName + " (" + update.User.Ip + "): " + update.Text); + _networkSession.AppendToLog("Message: " + update.User.UserName + " (" + update.User.Ip + "): " + update.Text); } else if (update.Action == "DEL") { @@ -7433,7 +7433,7 @@ namespace Nikse.SubtitleEdit.Forms _subtitle.Paragraphs.RemoveAt(update.Index); if (_networkSession.LastSubtitle != null) _networkSession.LastSubtitle.Paragraphs.RemoveAt(update.Index); - _networkSession.Log("Delete: " + update.User.UserName + " (" + update.User.Ip + "): Index=" + update.Index.ToString()); + _networkSession.AppendToLog("Delete: " + update.User.UserName + " (" + update.User.Ip + "): Index=" + update.Index.ToString()); _networkSession.AdjustUpdateLogToDelete(update.Index); _change = true; @@ -7461,7 +7461,7 @@ namespace Nikse.SubtitleEdit.Forms _subtitle.Paragraphs.Insert(update.Index, p); if (_networkSession.LastSubtitle != null) _networkSession.LastSubtitle.Paragraphs.Insert(update.Index, new Paragraph(p)); - _networkSession.Log("Insert: " + update.User.UserName + " (" + update.User.Ip + "): Index=" + update.Index.ToString() + ", Text=" + update.Text.Replace(Environment.NewLine, Configuration.Settings.General.ListViewLineSeparatorString)); + _networkSession.AppendToLog("Insert: " + update.User.UserName + " (" + update.User.Ip + "): Index=" + update.Index.ToString() + ", Text=" + update.Text.Replace(Environment.NewLine, Configuration.Settings.General.ListViewLineSeparatorString)); _networkSession.AddToWsUserLog(update.User, update.Index, update.Action, false); updateListViewStatus = true; _networkSession.AdjustUpdateLogToInsert(update.Index); @@ -7523,11 +7523,11 @@ namespace Nikse.SubtitleEdit.Forms if (removeUser != null) _networkSession.Users.Remove(removeUser); - _networkSession.Log("Bye: " + update.User.UserName + " (" + update.User.Ip + ")"); + _networkSession.AppendToLog("Bye: " + update.User.UserName + " (" + update.User.Ip + ")"); } else { - _networkSession.Log("UNKNOWN ACTION: " + update.Action + " by " + update.User.UserName + " (" + update.User.Ip + ")"); + _networkSession.AppendToLog("UNKNOWN ACTION: " + update.Action + " by " + update.User.UserName + " (" + update.User.Ip + ")"); } } } diff --git a/src/Logic/Networking/NikseWebServiceSession.cs b/src/Logic/Networking/NikseWebServiceSession.cs index 623ad9e47..496ecf28a 100644 --- a/src/Logic/Networking/NikseWebServiceSession.cs +++ b/src/Logic/Networking/NikseWebServiceSession.cs @@ -22,13 +22,13 @@ namespace Nikse.SubtitleEdit.Logic.Networking SeNetworkService.SeService _seWs; DateTime _seWsLastUpdate = DateTime.Now.AddYears(-1); public SeNetworkService.SeUser CurrentUser { get; set; } - public Subtitle LastSubtitle = null; + public Subtitle LastSubtitle; public Subtitle Subtitle; public Subtitle OriginalSubtitle; public string SessionId; public string FileName; public List Users; - public StringBuilder _log; + public StringBuilder Log; public string WebServiceUrl { @@ -44,8 +44,8 @@ namespace Nikse.SubtitleEdit.Logic.Networking OriginalSubtitle = originalSubtitle; _timerWebService = new System.Windows.Forms.Timer(); _timerWebService.Interval = 5000; - _timerWebService.Tick += new EventHandler(TimerWebServiceTick); - _log = new StringBuilder(); + _timerWebService.Tick += TimerWebServiceTick; + Log = new StringBuilder(); OnUpdateTimerTick = onUpdateTimerTick; OnUpdateUserLogEntries = onUpdateUserLogEntries; } @@ -56,7 +56,7 @@ namespace Nikse.SubtitleEdit.Logic.Networking var list = new List(); foreach (Paragraph p in Subtitle.Paragraphs) { - list.Add(new SeNetworkService.SeSequence() + list.Add(new SeNetworkService.SeSequence { StartMilliseconds = (int)p.StartTime.TotalMilliseconds, EndMilliseconds = (int)p.EndTime.TotalMilliseconds, @@ -69,7 +69,7 @@ namespace Nikse.SubtitleEdit.Logic.Networking { foreach (Paragraph p in OriginalSubtitle.Paragraphs) { - originalSubtitle.Add(new SeNetworkService.SeSequence() + originalSubtitle.Add(new SeNetworkService.SeSequence { StartMilliseconds = (int)p.StartTime.TotalMilliseconds, EndMilliseconds = (int)p.EndTime.TotalMilliseconds, @@ -152,7 +152,7 @@ namespace Nikse.SubtitleEdit.Logic.Networking { if (_seWs != null) { - Subtitle = new Logic.Subtitle(); + Subtitle = new Subtitle(); var sequences = _seWs.GetSubtitle(SessionId, out FileName, out _seWsLastUpdate); foreach (var sequence in sequences) { @@ -164,15 +164,15 @@ namespace Nikse.SubtitleEdit.Logic.Networking } } - public void Log(string text) + public void AppendToLog(string text) { string timestamp = DateTime.Now.ToLongTimeString(); - _log.AppendLine(timestamp + ": " + text.TrimEnd().Replace(Environment.NewLine, Configuration.Settings.General.ListViewLineSeparatorString)); + Log.AppendLine(timestamp + ": " + text.TrimEnd().Replace(Environment.NewLine, Configuration.Settings.General.ListViewLineSeparatorString)); } public string GetLog() { - return _log.ToString(); + return Log.ToString(); } public void SendChatMessage(string message) @@ -182,7 +182,7 @@ namespace Nikse.SubtitleEdit.Logic.Networking internal void UpdateLine(int index, Paragraph paragraph) { - _seWs.UpdateLine(SessionId, index, new SeNetworkService.SeSequence() + _seWs.UpdateLine(SessionId, index, new SeNetworkService.SeSequence { StartMilliseconds = (int)paragraph.StartTime.TotalMilliseconds, EndMilliseconds = (int)paragraph.EndTime.TotalMilliseconds, @@ -250,13 +250,13 @@ namespace Nikse.SubtitleEdit.Logic.Networking sb.Append(index + ", "); AdjustUpdateLogToDelete(index); } - Log("Delete line: " + sb.ToString().Trim().TrimEnd(',')); + AppendToLog("Delete line: " + sb.ToString().Trim().TrimEnd(',')); } internal void InsertLine(int index, Paragraph newParagraph) { _seWs.InsertLine(SessionId, index, (int)newParagraph.StartTime.TotalMilliseconds, (int)newParagraph.EndTime.TotalMilliseconds, newParagraph.Text, CurrentUser); - Log("Insert line at " + index.ToString() + ": " + newParagraph.ToString()); + AppendToLog("Insert line at " + index + ": " + newParagraph); } internal void AdjustUpdateLogToInsert(int index) diff --git a/src/Logic/Utilities.cs b/src/Logic/Utilities.cs index 0516a9996..e1655d524 100644 --- a/src/Logic/Utilities.cs +++ b/src/Logic/Utilities.cs @@ -411,9 +411,9 @@ namespace Nikse.SubtitleEdit.Logic gs.SubtitleFontName = "Tahoma"; if (gs.SubtitleFontBold) - control.Font = new System.Drawing.Font(gs.SubtitleFontName, gs.SubtitleFontSize, System.Drawing.FontStyle.Bold); + control.Font = new Font(gs.SubtitleFontName, gs.SubtitleFontSize, FontStyle.Bold); else - control.Font = new System.Drawing.Font(gs.SubtitleFontName, gs.SubtitleFontSize); + control.Font = new Font(gs.SubtitleFontName, gs.SubtitleFontSize); control.BackColor = gs.SubtitleBackgroundColor; control.ForeColor = gs.SubtitleFontColor; @@ -506,8 +506,7 @@ namespace Nikse.SubtitleEdit.Logic { if (GetCount(hewbrewEncoding.GetString(buffer), "אולי", "אולי", "אולי", "אולי", "טוב", "טוב") > 10) return hewbrewEncoding; - else - return arabicEncoding; + return arabicEncoding; } else if (GetCount(hewbrewEncoding.GetString(buffer), "אתה", "אולי", "הוא", "בסדר", "יודע", "טוב") > 5) return hewbrewEncoding; @@ -593,8 +592,7 @@ namespace Nikse.SubtitleEdit.Logic { if (GetCount(hewbrewEncoding.GetString(buffer), "אולי", "אולי", "אולי", "אולי", "טוב", "טוב") > 10) return hewbrewEncoding; - else - return arabicEncoding; + return arabicEncoding; } else if (GetCount(hewbrewEncoding.GetString(buffer), "אתה", "אולי", "הוא", "בסדר", "יודע", "טוב") > 5) return hewbrewEncoding; @@ -826,7 +824,19 @@ namespace Nikse.SubtitleEdit.Logic sb.AppendLine(p.Text); string text = sb.ToString(); - foreach (string name in GetDictionaryLanguages()) + List dictionaryNames = GetDictionaryLanguages(); + + bool containsEnGB = false; + bool containsEnUS = false; + foreach (string name in dictionaryNames) + { + if (name.Contains("[en_GB]")) + containsEnGB = true; + if (name.Contains("[en_US]")) + containsEnUS = true; + } + + foreach (string name in dictionaryNames) { string shortName = string.Empty; int start = name.IndexOf("["); @@ -861,7 +871,54 @@ namespace Nikse.SubtitleEdit.Logic case "en_US": count = GetCount(text, "we", "are", "and", "you", "your", "what"); if (count > bestCount) - languageName = shortName; + { + if (!string.IsNullOrEmpty(languageName) && languageName.StartsWith("en_")) + { + //keep existing english language + } + else + { + if (containsEnGB) + { + int usCount = GetCount("color", "flavor", "ass", "humor", "neighbor", "honor", "airplane"); + int gbCount = GetCount("colour", "flavour", "arse", "humour", "neighbour", "honour", "aeroplane"); + if (usCount >= gbCount) + languageName = shortName; + else + languageName = "en_GB"; + } + else + { + languageName = shortName; + } + } + } + break; + case "en_GB": + count = GetCount(text, "we", "are", "and", "you", "your", "what"); + if (count > bestCount) + { + if (!string.IsNullOrEmpty(languageName) && languageName.StartsWith("en_")) + { + //keep existing english language + } + else + { + if (containsEnUS) + { + int usCount = GetCount("color", "flavor", "ass", "humor", "neighbor", "honor", "airplane"); + int gbCount = GetCount("colour", "flavour", "arse", "humour", "neighbour", "honour", "aeroplane"); + if (usCount >= gbCount) + languageName = "en_US"; + else + languageName = "en_GB"; + } + else + { + languageName = shortName; + } + } + } break; case "sv_SE": count = GetCount(text, "vi", "är", "och", "Jag", "inte", "för"); @@ -949,7 +1006,7 @@ namespace Nikse.SubtitleEdit.Logic return languageName; } - public static string ColorToHex(System.Drawing.Color c) + public static string ColorToHex(Color c) { string result = string.Format("#{0:x2}{1:x2}{2:x2}", c.R, c.G, c.B); return result; @@ -1143,9 +1200,9 @@ namespace Nikse.SubtitleEdit.Logic labelLength.Left = position; position += labelLength.Width - 4; if (line.Length > Configuration.Settings.General.SubtitleLineMaximumLength) - labelLength.ForeColor = System.Drawing.Color.Red; + labelLength.ForeColor = Color.Red; else if (line.Length > Configuration.Settings.General.SubtitleLineMaximumLength - 5) - labelLength.ForeColor = System.Drawing.Color.Orange; + labelLength.ForeColor = Color.Orange; } } @@ -1373,9 +1430,9 @@ namespace Nikse.SubtitleEdit.Logic internal static Color GetColorFromUserName(string userName) { if (string.IsNullOrEmpty(userName)) - return System.Drawing.Color.Pink; + return Color.Pink; - byte[] buffer = System.Text.Encoding.UTF8.GetBytes(userName); + byte[] buffer = Encoding.UTF8.GetBytes(userName); long number = 0; foreach (byte b in buffer) number += b; @@ -1413,7 +1470,7 @@ namespace Nikse.SubtitleEdit.Logic if (string.IsNullOrEmpty(userName)) return 0; - byte[] buffer = System.Text.Encoding.UTF8.GetBytes(userName); + byte[] buffer = Encoding.UTF8.GetBytes(userName); long number = 0; foreach (byte b in buffer) number += b; @@ -1444,7 +1501,7 @@ namespace Nikse.SubtitleEdit.Logic StringBuilder encodedValue = new StringBuilder(); foreach (char c in chars) { - if ((int)c > 127) // above normal ASCII + if (c > 127) // above normal ASCII encodedValue.Append("&#" + (int)c + ";"); else encodedValue.Append(c); diff --git a/src/Logic/WavePeak.cs b/src/Logic/WavePeak.cs index 50f4d7404..56b72144d 100644 --- a/src/Logic/WavePeak.cs +++ b/src/Logic/WavePeak.cs @@ -79,7 +79,7 @@ namespace Nikse.SubtitleEdit.Logic stream.Read(buffer, 0, buffer.Length); DataId = Encoding.UTF8.GetString(buffer, 0, 4); DataChunkSize = BitConverter.ToInt32(buffer, 4); - DataStartPosition = (int)(ConstantHeaderSize + FmtChunkSize + 8); + DataStartPosition = ConstantHeaderSize + FmtChunkSize + 8; _headerData = new byte[DataStartPosition]; stream.Position = 0; @@ -98,7 +98,7 @@ namespace Nikse.SubtitleEdit.Logic { get { - return DataChunkSize / BytesPerSecond; + return (double)DataChunkSize / BytesPerSecond; } } @@ -115,24 +115,25 @@ namespace Nikse.SubtitleEdit.Logic toStream.Write(_headerData, 0, _headerData.Length); } - private void WriteInt16ToByteArray(byte[] _headerData, int index, int value) + private static void WriteInt16ToByteArray(byte[] headerData, int index, int value) { byte[] buffer = BitConverter.GetBytes((short)value); for (int i = 0; i < buffer.Length; i++) - _headerData[index + i] = buffer[i]; + headerData[index + i] = buffer[i]; } - private void WriteInt32ToByteArray(byte[] _headerData, int index, int value) + + private static void WriteInt32ToByteArray(byte[] headerData, int index, int value) { byte[] buffer = BitConverter.GetBytes(value); for (int i = 0; i < buffer.Length; i++) - _headerData[index + i] = buffer[i]; + headerData[index + i] = buffer[i]; } } public class WavePeakGenerator { - private Stream _stream = null; - private byte[] _data = null; + private Stream _stream; + private byte[] _data; private delegate int ReadSampleDataValueDelegate(ref int index); @@ -317,7 +318,7 @@ namespace Nikse.SubtitleEdit.Logic /// Sample data reader that matches bits per sample private ReadSampleDataValueDelegate GetSampleDataRerader() { - ReadSampleDataValueDelegate readSampleDataValue = null; + ReadSampleDataValueDelegate readSampleDataValue; switch (Header.BitsPerSample) { case 8: