/// <summary> /// Load a file via the chosen handler without invoking the dialog to choose /// </summary> /// <param name="Handler">handler to user (format)</param> /// <param name="filename">target file</param> /// <param name="ContextId">Set the title bar to this format</param> public static void FileLoad(IFormatHandler Handler, string filename, SupportedFileHandleFormats ContextId) { if (TargetWindow == null) { throw new ArgumentNullException("Forgot to set TargetWindow befor call"); } if (Handler != null) { var NewContext = new ContextFile { CurrentFile = filename }; try { Handler.Load(TargetWindow.mainWindowRichText, NewContext); } catch (IOException e) { MessageBox.Show(e.Message, "Error loading file. Message is " + e.Message); NewContext = null; } if (NewContext != null) { TargetWindow.CurrentFile = NewContext; NewContext.Format = ContextId; TargetWindow.mainWindowRichText.Modified = false; NewContext.UpdateContextTitle(TargetWindow); } return; } throw new ArgumentNullException("handler=null"); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); HandleArgs(); if (!BadCommand) { using (var MainForm = new mainWindow()) { MainForm.DisableConfigLoad = DisableConfigLoad; FileHandling.TargetWindow = MainForm; MainForm.Text = "Hunter's Notebook"; if (string.IsNullOrWhiteSpace(TargetFile) == false) { if (Format == SupportedFileHandleFormats.Unknown) { var BESTGUESS = FileHandling.IsFileUnicode(TargetFile); if (BESTGUESS == false) { Format = SupportedFileHandleFormats.TextAnsiPlain; } else { Format = SupportedFileHandleFormats.TextUnicodePlain; } } if (Format != SupportedFileHandleFormats.Unknown) { FileHandling.FileLoad(FileHandling.GetHandlerClassInstance(Format), TargetFile, Format); } else { MessageBox.Show("Unable to detect " + TargetFile + "'s encoding. Please Specific a format when loaded (use -load FORMAT \"Target\")"); } } Application.Run(MainForm); } } else { StringBuilder err = new StringBuilder(); foreach (string s in Environment.GetCommandLineArgs()) { err.AppendLine(s); } MessageBox.Show(err.ToString(), "Invalid Command Arg", MessageBoxButtons.OK, MessageBoxIcon.Error); } //Application.Run(); }
/// <summary> /// Display a save dialog setup for the chosen format. Saves if the user presses ok /// </summary> /// <param name="format"></param> /// <returns></returns> public static SaveFileDialog GetSaveDialogForHandler(SupportedFileHandleFormats format) { SaveFileDialog Ret = new SaveFileDialog(); GenericDialogSetup(Ret); switch (format) { case SupportedFileHandleFormats.ZippedTrackPlain: Ret.Title = "Save a Change Tracked Text File"; Ret.Filter = "Change Text File (*.TBD)| *.TBD"; Ret.FileOk += SaveTBD_FileOk; break; case SupportedFileHandleFormats.TextUnicodeRTF: Ret.Title = "Save a Plain Unicdode Rich Text File"; Ret.Filter = "Text Files (*.rtf)|*.rtf"; Ret.FileOk += SaveUnicodeRichText_FileOk; break; case SupportedFileHandleFormats.TextUnicodePlain: Ret.Title = "Save a Plain Unicode Text File"; Ret.Filter = "Text Files (*.txt)|*.txt"; Ret.FileOk += SaveUnicodePlainText_FileOK; break; case SupportedFileHandleFormats.TextAnsiRTF: Ret.Title = "Save a Plain Ansi Rich Text File"; Ret.Filter = "Text Files (*.rtf)|*.rtf"; Ret.FileOk += SaveAnsiRTF_FileOK; break; case SupportedFileHandleFormats.TextAnsiPlain: Ret.Title = "Save a Plain Ansi Text File"; Ret.Filter = "Text Files (*.txt)|*.txt| Log File (*.log)|*.log"; Ret.FileOk += SaveAniPlainText_FileOK; break; case SupportedFileHandleFormats.CompressedTextFile: Ret.Title = "Save Compressed Text"; Ret.Filter = "Compressed Text (*.tx_)|*.tx_"; Ret.FileOk += SaveCompressedText_FileOK; break; default: Ret.Title = "Save a file"; throw new NotImplementedException(Enum.GetName(typeof(SupportedFileHandleFormats), format)); } Ret.Filter += "|All files (*.*)|*.*"; return(Ret); }
public static IFormatHandler GetHandlerClassInstance(SupportedFileHandleFormats format) { switch (format) { case SupportedFileHandleFormats.TextAnsiPlain: return(new AnsiPlainText()); case SupportedFileHandleFormats.TextUnicodePlain: return(new UnicodePlainText()); case SupportedFileHandleFormats.CompressedTextFile: return(new NoteformatLinker()); case SupportedFileHandleFormats.ZippedTrackPlain: return(new ZippedChangeTracker()); default: throw new NotImplementedException(Enum.GetName(typeof(SupportedFileHandleFormats), format)); } }
static void HandleArgs() { var cmds = Environment.GetCommandLineArgs(); for (int step = 1; step < cmds.Length; step++) { switch (cmds[step].ToLower()) { case "-noconfig": DisableConfigLoad = true; break; case "-shortcut": { // remote the shortcut FileHandling.DeleteAutoShortcut(); break; } case "-load": if (cmds.Length - step >= 3) { step++; switch (cmds[step].ToLower()) { case "ansi": Format = SupportedFileHandleFormats.TextAnsiPlain; TargetFile = cmds[step]; break; case "unicode": Format = SupportedFileHandleFormats.TextUnicodePlain; TargetFile = cmds[step]; break; case "rtf": Format = SupportedFileHandleFormats.TextUnicodeRTF; TargetFile = cmds[step]; break; case "tdb": Format = SupportedFileHandleFormats.ZippedTrackPlain; TargetFile = cmds[step]; break; default: TargetFile = cmds[step]; break; } step++; } else { TargetFile = cmds[step]; return; } break; default: TargetFile = cmds[step]; Format = SupportedFileHandleFormats.Unknown; return; } } }