/// <summary>Creates a new instance of the <see cref="FileProperties" /> class, given its properties.</summary> /// <param name="path">The file's path.</param> /// <param name="encoding">The file's character coding.</param> /// <param name="subtitleType">The file's subtitle type.</param> /// <param name="timingMode">The file's timing mode. This is more useful for the subtitle types that support both the time and frame modes.</param> /// <param name="newlineType">The file's newline type.</param> public FileProperties (string path, Encoding encoding, SubtitleType subtitleType, TimingMode timingMode, NewlineType newlineType) { this.path = path; this.encoding = encoding; this.subtitleType = subtitleType; this.timingMode = timingMode; this.newlineType = newlineType; }
private void OnDefaultsFileSaveNewlineChanged(object o, EventArgs args) { int active = fileSaveNewline.ActiveSelection; ConfigFileSaveNewlineOption activeOption = (ConfigFileSaveNewlineOption)Enum.ToObject(typeof(ConfigFileSaveNewlineOption), active); if (((int)activeOption) > ((int)ConfigFileSaveNewlineOption.Specific)) //Positions higher than specific are always specific too { activeOption = ConfigFileSaveNewlineOption.Specific; } Base.Config.FileSaveNewlineOption = activeOption; /* If newline is specific, newlineOption=Specific and newline holds the newline type name */ if (activeOption == ConfigFileSaveNewlineOption.Specific) { NewlineType chosenNewlineType = fileSaveNewline.ChosenNewlineType; if (!chosenNewlineType.Equals(NewlineType.Unknown)) { Base.Config.FileSaveNewline = chosenNewlineType; } } else { /* If newline option is remember last, use the system default */ Base.Config.FileSaveNewline = Core.Util.GetSystemNewlineType(); } }
/// <summary>Creates a new instance of the <see cref="FileProperties" /> class, given its properties.</summary> /// <param name="path">The file's path.</param> /// <param name="encoding">The file's character coding.</param> /// <param name="subtitleType">The file's subtitle type.</param> /// <param name="timingMode">The file's timing mode. This is more useful for the subtitle types that support both the time and frame modes.</param> /// <param name="newlineType">The file's newline type.</param> public FileProperties(string path, Encoding encoding, SubtitleType subtitleType, TimingMode timingMode, NewlineType newlineType) { this.path = path; this.encoding = encoding; this.subtitleType = subtitleType; this.timingMode = timingMode; this.newlineType = newlineType; }
public NewlineTypeComboBox(NewlineType newlineTypeToSelect, string[] additionalActions) { this.comboBox = new ComboBoxText(); this.newlineTypeToSelect = newlineTypeToSelect; this.additionalActions = additionalActions; FillComboBox(); ConnectHandlers(); }
public NewlineTypeComboBox (ComboBox comboBox, NewlineType newlineTypeToSelect, string[] additionalActions) { this.comboBox = comboBox; this.newlineTypeToSelect = newlineTypeToSelect; this.additionalActions = additionalActions; InitComboBoxModel(); FillComboBox(); ConnectHandlers(); }
private void BuildNewlineComboBox() { NewlineType newlineTypeToSelect = Base.Config.FileSaveNewline; /* If no newline type set, or system default unknown, use Unix */ if (newlineTypeToSelect == NewlineType.Unknown) { newlineTypeToSelect = NewlineType.Unix; } newlineComboBox = new NewlineTypeComboBox(newlineTypeToSelect, null); }
private void ConvertNewlines(StringBuilder builder, FileProperties properties) { NewlineType type = properties.NewlineType; if ((type == NewlineType.Unknown) || (type == NewlineType.Unix)) { return; } string newline = (type == NewlineType.Windows ? "\r\n" : "\r"); //Windows : Macintosh builder.Replace("\n", newline); }
private void SetSystemNewlineSuffix(NewlineType newline, ref string mac, ref string unix, ref string windows, string suffix) { switch (newline) { case NewlineType.Macintosh: mac += suffix; break; case NewlineType.Unix: unix += suffix; break; case NewlineType.Windows: windows += suffix; break; } }
/// <summary>Displays a SaveAs dialog and gets the chosen options as <cref="FileProperties" />.</summary> /// <param name="dialog">The dialog to display.</param> /// <returns>The chosen file properties, or null in case SaveAs was canceled.</returns> private FileProperties ShowSaveAsDialog(FileSaveDialog dialog) { dialog.Show(); bool toSaveAs = dialog.WaitForResponse(); if (!toSaveAs) { return(null); } string path = dialog.Filename; Encoding encoding = Encodings.GetEncoding(dialog.Encoding.CodePage); SubtitleType subtitleType = dialog.SubtitleType; NewlineType newlineType = dialog.NewlineType; TimingMode timingMode = Base.TimingMode; return(new FileProperties(path, encoding, subtitleType, timingMode, newlineType)); }
private NewlineTypeComboBox BuildFileSaveNewlineComboBox() { string[] additionalActions = { Catalog.GetString("Remember the last used type") }; NewlineType newlineTypeToSelect = NewlineType.Unknown; ConfigFileSaveNewlineOption fileSaveNewlineOption = Base.Config.FileSaveNewlineOption; if (fileSaveNewlineOption == ConfigFileSaveNewlineOption.Specific) { newlineTypeToSelect = Base.Config.FileSaveNewline; } NewlineTypeComboBox comboBox = new NewlineTypeComboBox(newlineTypeToSelect, additionalActions); if (fileSaveNewlineOption != ConfigFileSaveNewlineOption.Specific) { comboBox.ActiveSelection = (int)fileSaveNewlineOption; } comboBox.SelectionChanged += OnDefaultsFileSaveNewlineChanged; return(comboBox); }
/* Private members */ private void FillComboBox() { DisconnectComboBoxChangedSignal(); (comboBox.Model as ListStore).Clear(); bool hasAdditionalActions = (additionalActions != null) && (additionalActions.Length > 0); /* Add additional actions */ if (hasAdditionalActions) { foreach (string additionalAction in additionalActions) { comboBox.AppendText(additionalAction); } comboBox.AppendText("-"); } /* Prepare newline types to add */ string mac = "Mac OS Classic"; string unix = "Unix/Linux"; string windows = "Windows"; string systemDefault = " (" + Catalog.GetString("System Default") + ")"; NewlineType systemNewline = Core.Util.GetSystemNewlineType(); SetSystemNewlineSuffix(systemNewline, ref mac, ref unix, ref windows, systemDefault); /* Add newline types */ comboBox.AppendText(mac); comboBox.AppendText(unix); comboBox.AppendText(windows); if (newlineTypeToSelect != NewlineType.Unknown) { int activeItem = (int)newlineTypeToSelect - 1 + (hasAdditionalActions ? additionalActions.Length + 1 : 0); SetActiveItem(activeItem, false); //Don't use silent change because the signal is already disabled } ConnectComboBoxChangedSignal(); }
/* Event members */ protected override bool ProcessResponse(ResponseType response) { if (response == ResponseType.Ok) { /* Check chosen encoding */ chosenEncoding = encodingComboBox.ChosenEncoding; if (Base.Config.FileSaveEncodingOption == ConfigFileSaveEncodingOption.RememberLastUsed) { int activeAction = encodingComboBox.ActiveSelection; ConfigFileSaveEncoding activeOption = (ConfigFileSaveEncoding)Enum.ToObject(typeof(ConfigFileSaveEncoding), activeAction); if (((int)activeOption) >= ((int)ConfigFileSaveEncoding.Fixed)) { Base.Config.FileSaveEncodingFixed = chosenEncoding.Code; } else { Base.Config.FileSaveEncoding = activeOption; } } /* Check chosen subtitle format */ chosenSubtitleType = formatComboBox.ChosenSubtitleType; if (Base.Config.FileSaveFormatOption == ConfigFileSaveFormatOption.RememberLastUsed) { Base.Config.FileSaveFormatFixed = chosenSubtitleType; } /* Check chosen newline type */ chosenNewlineType = newlineComboBox.ChosenNewlineType; if (Base.Config.FileSaveNewlineOption == ConfigFileSaveNewlineOption.RememberLastUsed) { Base.Config.FileSaveNewline = chosenNewlineType; } chosenFilename = (Dialog as FileChooserDialog).Filename; SetReturnValue(true); } return(false); }
public static string AsString(this NewlineType Type) => Representations.ContainsKey(Type) ? Representations[Type] : Representations[NewlineType.Undefined];
public static void ReplaceSelection(this IEnumerable <string> Selections, IServiceProvider ServiceProvider, EmptyLineAction Action, int[] EmptyLinePositions, NewlineType NewlineType, bool WasNewLine) { TextSelection.ServiceProvider = ServiceProvider; ReplaceSelection(Selections, Action, EmptyLinePositions, NewlineType, WasNewLine); }
public static void ReplaceSelection(this IEnumerable <string> Selections, EmptyLineAction Action, int[] EmptyLinePositions, NewlineType NewlineType, bool WasNewline) { ThreadHelper.ThrowIfNotOnUIThread(); string newlineStr = NewlineType.AsString(); if (Action == EmptyLineAction.DependsOnSettings) { Action = VSPackage.Loader.Settings.EmptyLineAction; } DTE dte = ServiceProvider?.GetService(typeof(DTE)) as DTE; if (dte is null) { return; } switch (Action) { case EmptyLineAction.AsMask: int i = 0; Selections = Selections.SelectMany(x => { List <string> result = new List <string>(); while (EmptyLinePositions.Contains(i)) { result.Add(string.Empty); ++i; } result.Add(x); ++i; return(result); }); break; default: break; } string text = string.Join(newlineStr, Selections) + (WasNewline ? newlineStr : string.Empty); try { // `((EnvDTE.TextSelection)dte.ActiveDocument.Selection).Text = value` is really slow! // So I use this small 'hack': // Saving current clipboard state: IDataObject obj = Clipboard.GetDataObject(); // Loading text to clipboard: Clipboard.SetDataObject(new DataObject(DataFormats.UnicodeText, text), true); //Clipboard.SetText(text); -- Throws when system clipboard's blocked by another process // Pasting text from clipboard (and formatting it): dte.ExecuteCommand("Edit.Paste"); // Now we return everything as it was) Clipboard.SetDataObject(obj); } catch { ((EnvDTE.TextSelection)dte.ActiveDocument.Selection).Text = text; } }
public static IEnumerable <string> GetSelection(IServiceProvider ServiceProvider, EmptyLineAction Action, out int[] EmptyLinePositions, out NewlineType NewlineType, out bool WasNewline) { TextSelection.ServiceProvider = ServiceProvider; return(GetSelection(Action, out EmptyLinePositions, out NewlineType, out WasNewline)); }
public static IEnumerable <string> GetSelection(EmptyLineAction Action, out int[] EmptyLinePositions, out NewlineType NewlineType, out bool WasNewline) { IVsTextManager2 textManager = ServiceProvider.GetService(typeof(SVsTextManager)) as IVsTextManager2; Assumes.Present(textManager); textManager.GetActiveView2(1, null, (uint)_VIEWFRAMETYPE.vftCodeWindow, out IVsTextView view); view.GetSelectedText(out string selectedText); NewlineType = selectedText.GetNewlineType(); string newlineStr = NewlineType.AsString(); WasNewline = selectedText.EndsWith(newlineStr); IEnumerable <string> result = selectedText.Split(new[] { newlineStr }, StringSplitOptions.None); int lastIndex = ((string[])result).Length - 1; EmptyLinePositions = result.Select((x, i) => new { Line = x, Index = i }).Where(x => string.IsNullOrWhiteSpace(x.Line)).Select(x => x.Index).ToArray(); if (Action == EmptyLineAction.DependsOnSettings) { Action = VSPackage.Loader.Settings.EmptyLineAction; } result = Action == EmptyLineAction.AsLine ? result.Where((x, i) => !(string.IsNullOrEmpty(x) && i == lastIndex)) : result.Where(x => !string.IsNullOrWhiteSpace(x)); return(result); }
public static void ReplaceSelection(this IEnumerable <string> Selections, int[] EmptyLinePositions, NewlineType NewlineType, bool WasNewLine) => ReplaceSelection(Selections, VSPackage.Loader.Settings.EmptyLineAction, EmptyLinePositions, NewlineType, WasNewLine);
protected override bool ProcessResponse (ResponseType response) { if (response == ResponseType.Ok) { /* Check chosen encoding */ chosenEncoding = encodingComboBoxComponent.ChosenEncoding; if (Base.Config.PrefsDefaultsFileSaveEncodingOption == ConfigFileSaveEncodingOption.RememberLastUsed) { int activeAction = encodingComboBoxComponent.ActiveSelection; ConfigFileSaveEncoding activeOption = (ConfigFileSaveEncoding)Enum.ToObject(typeof(ConfigFileSaveEncoding), activeAction); if (((int)activeOption) >= ((int)ConfigFileSaveEncoding.Fixed)) { Base.Config.PrefsDefaultsFileSaveEncodingFixed = chosenEncoding.Name; } else { Base.Config.PrefsDefaultsFileSaveEncoding = activeOption; } } /* Check chosen subtitle format */ chosenSubtitleType = formatComboBoxComponent.ChosenSubtitleType; if (Base.Config.PrefsDefaultsFileSaveFormatOption == ConfigFileSaveFormatOption.RememberLastUsed) { Base.Config.PrefsDefaultsFileSaveFormatFixed = chosenSubtitleType; } /* Check chosen newline type */ chosenNewlineType = newlineComboBoxComponent.ChosenNewlineType; if (Base.Config.PrefsDefaultsFileSaveNewlineOption == ConfigFileSaveNewlineOption.RememberLastUsed) { Base.Config.PrefsDefaultsFileSaveNewline = chosenNewlineType; } /* Check chosen filename */ chosenFilename = AddExtensionIfNeeded(chosenSubtitleType); SetReturnValue(true); } return false; }
private void SetSystemNewlineSuffix (NewlineType newline, ref string mac, ref string unix, ref string windows, string suffix) { switch (newline) { case NewlineType.Macintosh: mac += suffix; break; case NewlineType.Unix: unix += suffix; break; case NewlineType.Windows: windows += suffix; break; } }