From 66af626e5ef6fcc2d955deb48372109dae81b83f Mon Sep 17 00:00:00 2001 From: Sigmanor Date: Thu, 9 Feb 2017 23:42:08 +0200 Subject: [PATCH] Rewritten save confirmation function --- Crypto Notepad/Form1.cs | 170 ++++++++++++++------------------------ Crypto Notepad/Form1.resx | 4 +- 2 files changed, 64 insertions(+), 110 deletions(-) diff --git a/Crypto Notepad/Form1.cs b/Crypto Notepad/Form1.cs index 8c3aa95..9a13591 100644 --- a/Crypto Notepad/Form1.cs +++ b/Crypto Notepad/Form1.cs @@ -21,8 +21,10 @@ public partial class MainWindow : Form string[] args = Environment.GetCommandLineArgs(); int caretPos = 0; string appName = Assembly.GetExecutingAssembly().GetName().Name + " – "; - string currentFilename; + string currentFilename = "Unnamed.cnp"; bool shiftPresed; + bool cancelPressed = false; + bool noExit = false; public MainWindow() { InitializeComponent(); @@ -78,7 +80,6 @@ void DecryptAES() OpenFile.FileName = ""; return; } - publicVar.okPressed = false; if (panel1.Visible == true) { findToolStripMenuItem2_Click(this, new EventArgs()); @@ -111,7 +112,12 @@ void DecryptAES() private void openToolStripMenuItem_Click(object sender, EventArgs e) { - currentFilename = this.Text.Substring(this.Text.LastIndexOf(" – ") + 2); + saveConfirm(false); + if (cancelPressed == true) + { + cancelPressed = false; + return; + } if (OpenFile.ShowDialog() != DialogResult.OK) return; { @@ -125,9 +131,12 @@ private void openToolStripMenuItem_Click(object sender, EventArgs e) customRTB.Select(Convert.ToInt32(cc2), 0); return; } - - saveConfirm(); - + DecryptAES(); + if (publicVar.okPressed == true) + { + publicVar.okPressed = false; + currentFilename = Path.GetFileName(OpenFile.FileName); + } } } @@ -167,6 +176,7 @@ private void openAsotiations() openAsotiations(); } } + currentFilename = Path.GetFileName(OpenFile.FileName); } private void newToolStripMenuItem_Click_1(object sender, EventArgs e) @@ -251,71 +261,11 @@ private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) ps.Save(); - if (customRTB.Modified == true) + saveConfirm(true); + + if (noExit == true) { - string noname = "Unnamed.cnp"; - string NameWithotPath; - - try - { - NameWithotPath = Path.GetFileName(args[1]); - } - catch (IndexOutOfRangeException) - { - NameWithotPath = Path.GetFileName(OpenFile.FileName); - } - - if (NameWithotPath == "") - { - NameWithotPath = noname; - } - - if (customRTB.Text != "") - { - DialogResult res = new DialogResult(); - string messageBoxText = ""; - - if (publicVar.keyChanged == false) - { - messageBoxText = "Save file: " + "\"" + NameWithotPath + "\"" + " ? "; - } - if (publicVar.keyChanged == true) - { - messageBoxText = "Save file: " + "\"" + NameWithotPath + "\"" + " with a new key? "; - } - - using (new CenterWinDialog(this)) - { - res = MessageBox.Show(messageBoxText, "Crypto Notepad", - MessageBoxButtons.YesNoCancel, - MessageBoxIcon.Question); - if (res == DialogResult.Yes) - { - if (filename == noname) - { - SaveFile.FileName = noname; - saveAsToolStripMenuItem_Click(this, new EventArgs()); - e.Cancel = true; - } - - if (filename != noname) - { - saveToolStripMenuItem1_Click_1(this, new EventArgs()); - Environment.Exit(0); - } - } - - if (res == DialogResult.No) - { - return; - } - - if (res == DialogResult.Cancel) - { - e.Cancel = true; - } - } - } + e.Cancel = true; } } @@ -687,9 +637,8 @@ private void MainWindow_Activated(object sender, EventArgs e) customRTB.BackColor = ps.RichBackColor; /*workaround, unhighlight URLs fix*/ - SendKeys.Send("^(z)"); - SendKeys.Send("^(z)"); - SendKeys.Send("{right}"); + customRTB.DetectUrls = false; + customRTB.DetectUrls = true; /*workaround, unhighlight URLs fix*/ if (ps.ShowToolbar == false && panel2.Visible == true) @@ -1073,49 +1022,57 @@ private void customRTB_LinkClicked(object sender, LinkClickedEventArgs e) private void customRTB_DragDrop(object sender, DragEventArgs e) { - currentFilename = this.Text.Substring(this.Text.LastIndexOf(" – ") + 2); + saveConfirm(false); + if (cancelPressed == true) + { + cancelPressed = false; + return; + } + string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false); foreach (string file in FileList) OpenFile.FileName = file; object fname = e.Data.GetData("FileDrop"); if (fname != null) { var list = fname as string[]; - if (list != null && !string.IsNullOrWhiteSpace(list[0])) { + string opnfile = File.ReadAllText(OpenFile.FileName); + string NameWithotPath = Path.GetFileName(OpenFile.FileName); + if (!OpenFile.FileName.Contains(".cnp")) { - string opnfile = File.ReadAllText(OpenFile.FileName); - string NameWithotPath = Path.GetFileName(OpenFile.FileName); customRTB.Text = opnfile; this.Text = appName + NameWithotPath; string cc2 = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture); customRTB.Select(Convert.ToInt32(cc2), 0); - saveConfirm(); return; } - saveConfirm(); + + DecryptAES(); + if (publicVar.okPressed == true) + { + publicVar.okPressed = false; + currentFilename = Path.GetFileName(OpenFile.FileName); + } } } } - void saveConfirm() + public void saveConfirm(bool exit) { + if (customRTB.Modified == false) + { + if (exit == true) + { + Environment.Exit(0); + } + } + if (customRTB.Modified == true) { string noname = "Unnamed.cnp"; - string NameWithotPath = "Unnamed.cnp"; - - if (!this.Text.Contains(".cnp")) - { - NameWithotPath = "Unnamed.cnp"; - } - - if (this.Text.Contains(".cnp")) - { - NameWithotPath = currentFilename; - } if (customRTB.Text != "") { @@ -1124,11 +1081,11 @@ void saveConfirm() if (publicVar.keyChanged == false) { - messageBoxText = "Save file: " + "\"" + NameWithotPath + "\"" + " ? "; + messageBoxText = "Save file: " + "\"" + currentFilename + "\"" + " ? "; } if (publicVar.keyChanged == true) { - messageBoxText = "Save file: " + "\"" + NameWithotPath + "\"" + " with a new key? "; + messageBoxText = "Save file: " + "\"" + currentFilename + "\"" + " with a new key? "; } using (new CenterWinDialog(this)) @@ -1148,36 +1105,33 @@ void saveConfirm() if (filename != noname) { saveToolStripMenuItem1_Click_1(this, new EventArgs()); + + if (exit == true) + { + Environment.Exit(0); + } } } if (res == DialogResult.No) { - DecryptAES(); - return; + if (exit == true) + { + Environment.Exit(0); + } } if (res == DialogResult.Cancel) { + noExit = true; + cancelPressed = true; return; } } } - - if (customRTB.Text == "") - { - DecryptAES(); - return; - } } - - if (customRTB.Modified == false) - { - DecryptAES(); - } - } - + private void customRTB_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.ShiftKey) diff --git a/Crypto Notepad/Form1.resx b/Crypto Notepad/Form1.resx index 38b9164..678e3bd 100644 --- a/Crypto Notepad/Form1.resx +++ b/Crypto Notepad/Form1.resx @@ -121,7 +121,7 @@ 17, 17 - 535, 17 + 419, 17 127, 17 @@ -130,7 +130,7 @@ 227, 17 - 438, 17 + 322, 17