2019-10-15 16:09:51 +00:00
using Crypto_Notepad.Properties ;
2019-10-02 16:24:52 +00:00
using System ;
2018-12-17 11:16:17 +00:00
using System.ComponentModel ;
using System.Diagnostics ;
2019-10-11 10:31:24 +00:00
using System.Drawing ;
2019-09-28 19:57:03 +00:00
using System.Drawing.Drawing2D ;
2018-12-17 11:16:17 +00:00
using System.IO ;
using System.Linq ;
using System.Net ;
using System.Reflection ;
using System.Security.Cryptography ;
2019-08-30 10:07:26 +00:00
using System.Threading.Tasks ;
2018-12-17 11:16:17 +00:00
using System.Windows.Forms ;
namespace Crypto_Notepad
{
public partial class MainForm : Form
{
2019-10-11 10:38:15 +00:00
Settings settings = Settings . Default ;
2019-09-28 19:57:03 +00:00
readonly string [ ] args = Environment . GetCommandLineArgs ( ) ;
2019-10-13 11:59:01 +00:00
bool cancelPressed = false ;
2018-12-17 11:16:17 +00:00
string filePath = "" ;
string argsPath = "" ;
2019-08-22 17:05:18 +00:00
int findPos = 0 ;
2019-10-11 10:38:15 +00:00
int caretPos ;
2018-12-25 15:29:29 +00:00
2018-12-17 11:16:17 +00:00
public MainForm ( )
{
InitializeComponent ( ) ;
2019-09-28 19:57:03 +00:00
richTextBox . DragDrop + = new DragEventHandler ( RichTextBox_DragDrop ) ;
richTextBox . AllowDrop = true ;
2018-12-17 11:16:17 +00:00
}
2019-10-02 16:35:34 +00:00
#region Methods
2018-12-25 15:29:29 +00:00
private void DecryptAES ( )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
EnterKeyForm enterKeyForm = new EnterKeyForm
{
Owner = this
} ;
enterKeyForm . ShowDialog ( ) ;
2018-12-25 15:29:29 +00:00
if ( ! PublicVar . okPressed )
2018-12-17 11:16:17 +00:00
{
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( filePath ) ;
2018-12-17 11:16:17 +00:00
return ;
}
2019-09-28 19:57:03 +00:00
if ( searchPanel . Visible )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
FindMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-17 11:16:17 +00:00
}
try
{
2019-09-28 19:57:03 +00:00
string opnfile = File . ReadAllText ( openFileDialog . FileName ) ;
string NameWithotPath = Path . GetFileName ( openFileDialog . FileName ) ;
2019-01-04 11:15:06 +00:00
string de ;
2019-09-28 19:57:03 +00:00
de = AES . Decrypt ( opnfile , TypedPassword . Value , null , settings . HashAlgorithm , Convert . ToInt32 ( settings . PasswordIterations ) , Convert . ToInt32 ( settings . KeySize ) ) ;
richTextBox . Text = de ;
2019-01-07 10:09:52 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2019-09-28 19:57:03 +00:00
filePath = openFileDialog . FileName ;
PublicVar . openFileName = Path . GetFileName ( openFileDialog . FileName ) ;
2018-12-17 11:16:17 +00:00
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
TypedPassword . Value = null ;
}
catch ( CryptographicException )
{
using ( new CenterWinDialog ( this ) )
{
TypedPassword . Value = null ;
2019-10-13 11:57:47 +00:00
DialogResult dialogResult = MessageBox . Show ( this , "Invalid key!" , PublicVar . appName , MessageBoxButtons . RetryCancel , MessageBoxIcon . Error ) ;
2018-12-17 11:16:17 +00:00
if ( dialogResult = = DialogResult . Retry )
{
DecryptAES ( ) ;
}
if ( dialogResult = = DialogResult . Cancel )
{
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( filePath ) ;
2018-12-17 11:16:17 +00:00
}
}
}
}
2018-12-25 15:29:29 +00:00
private void OpenAsotiations ( )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
EnterKeyForm enterKeyForm = new EnterKeyForm
{
Owner = this ,
StartPosition = FormStartPosition . CenterScreen
} ;
2018-12-17 11:16:17 +00:00
string fileExtension = Path . GetExtension ( args [ 1 ] ) ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( args [ 1 ] ) ;
2018-12-17 11:16:17 +00:00
if ( fileExtension = = ".cnp" )
{
try
{
string NameWithotPath = Path . GetFileName ( args [ 1 ] ) ;
string opnfile = File . ReadAllText ( args [ 1 ] ) ;
2019-09-28 19:57:03 +00:00
enterKeyForm . ShowDialog ( ) ;
string de = AES . Decrypt ( opnfile , TypedPassword . Value , null , settings . HashAlgorithm , Convert . ToInt32 ( settings . PasswordIterations ) , Convert . ToInt32 ( settings . KeySize ) ) ;
richTextBox . Text = de ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-17 11:16:17 +00:00
filePath = args [ 1 ] ;
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
TypedPassword . Value = null ;
}
catch ( CryptographicException )
{
TypedPassword . Value = null ;
2019-10-13 11:57:47 +00:00
DialogResult dialogResult = MessageBox . Show ( this , "Invalid key!" , PublicVar . appName , MessageBoxButtons . RetryCancel , MessageBoxIcon . Error ) ;
2018-12-17 11:16:17 +00:00
if ( dialogResult = = DialogResult . Retry )
{
2018-12-25 15:29:29 +00:00
OpenAsotiations ( ) ;
2018-12-17 11:16:17 +00:00
}
2019-10-15 16:09:51 +00:00
}
}
2018-12-17 11:16:17 +00:00
else
{
string opnfile = File . ReadAllText ( args [ 1 ] ) ;
string NameWithotPath = Path . GetFileName ( args [ 1 ] ) ;
2019-09-28 19:57:03 +00:00
richTextBox . Text = opnfile ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
private void SendTo ( )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
EnterKeyForm enterKeyForm = new EnterKeyForm
{
Owner = this ,
StartPosition = FormStartPosition . CenterScreen
} ;
2018-12-17 11:16:17 +00:00
string fileExtension = Path . GetExtension ( argsPath ) ;
if ( fileExtension = = ".cnp" )
{
try
{
string NameWithotPath = Path . GetFileName ( argsPath ) ;
string opnfile = File . ReadAllText ( argsPath ) ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( argsPath ) ;
2019-09-28 19:57:03 +00:00
enterKeyForm . ShowDialog ( ) ;
string de = AES . Decrypt ( opnfile , TypedPassword . Value , null , settings . HashAlgorithm , Convert . ToInt32 ( settings . PasswordIterations ) , Convert . ToInt32 ( settings . KeySize ) ) ;
richTextBox . Text = de ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-17 11:16:17 +00:00
filePath = argsPath ;
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
TypedPassword . Value = null ;
}
catch ( CryptographicException )
{
TypedPassword . Value = null ;
2019-10-13 11:57:47 +00:00
DialogResult dialogResult = MessageBox . Show ( this , "Invalid key!" , PublicVar . appName , MessageBoxButtons . RetryCancel , MessageBoxIcon . Error ) ;
2018-12-17 11:16:17 +00:00
if ( dialogResult = = DialogResult . Retry )
{
2018-12-25 15:29:29 +00:00
SendTo ( ) ;
2018-12-17 11:16:17 +00:00
}
2019-10-15 16:09:51 +00:00
}
}
2018-12-17 11:16:17 +00:00
else
{
string opnfile = File . ReadAllText ( argsPath ) ;
string NameWithotPath = Path . GetFileName ( argsPath ) ;
2019-09-28 19:57:03 +00:00
richTextBox . Text = opnfile ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
private void ContextMenuEncryptReplace ( )
2018-12-17 11:16:17 +00:00
{
if ( args [ 1 ] . Contains ( ".cnp" ) )
{
2019-10-13 11:57:47 +00:00
MessageBox . Show ( this , "Looks like this file is already encrypted" , PublicVar . appName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2018-12-17 11:16:17 +00:00
return ;
}
2019-10-13 11:57:47 +00:00
DialogResult res = MessageBox . Show ( this , "This action will delete the source file and replace it with encrypted version" , PublicVar . appName , MessageBoxButtons . OKCancel , MessageBoxIcon . Question ) ;
2018-12-17 11:16:17 +00:00
if ( res = = DialogResult . Cancel )
{
2018-12-17 13:03:08 +00:00
Environment . Exit ( 0 ) ;
2018-12-17 11:16:17 +00:00
}
if ( ! args [ 1 ] . Contains ( ".cnp" ) )
{
string opnfile = File . ReadAllText ( args [ 1 ] ) ;
2019-09-28 19:57:03 +00:00
richTextBox . Text = opnfile ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( args [ 1 ] ) ;
2018-12-17 11:16:17 +00:00
string newFile = Path . GetDirectoryName ( args [ 1 ] ) + @"\" + Path . GetFileNameWithoutExtension ( args [ 1 ] ) + ".cnp" ;
2019-09-28 19:57:03 +00:00
EnterKeyForm enterKeyForm = new EnterKeyForm
{
Owner = this
} ;
enterKeyForm . ShowDialog ( ) ;
2018-12-17 11:16:17 +00:00
File . Delete ( args [ 1 ] ) ;
2019-09-28 19:57:03 +00:00
string noenc = richTextBox . Text ;
2018-12-17 11:16:17 +00:00
string en ;
2019-09-28 19:57:03 +00:00
en = AES . Encrypt ( richTextBox . Text , TypedPassword . Value , null , settings . HashAlgorithm , Convert . ToInt32 ( settings . PasswordIterations ) , Convert . ToInt32 ( settings . KeySize ) ) ;
richTextBox . Text = en ;
2018-12-17 11:16:17 +00:00
StreamWriter sw = new StreamWriter ( newFile ) ;
2019-09-28 19:57:03 +00:00
int i = richTextBox . Lines . Count ( ) ;
2018-12-17 11:16:17 +00:00
int j = 0 ;
i = i - 1 ;
while ( j < = i )
{
2019-09-28 19:57:03 +00:00
sw . WriteLine ( richTextBox . Lines . GetValue ( j ) . ToString ( ) ) ;
2018-12-17 11:16:17 +00:00
j = j + 1 ;
}
sw . Close ( ) ;
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
TypedPassword . Value = null ;
filePath = newFile ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( newFile ) ;
Text = PublicVar . appName + " – " + PublicVar . openFileName ;
2019-09-28 19:57:03 +00:00
richTextBox . Text = noenc ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
richTextBox . Modified = false ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void ContextMenuEncrypt ( )
2018-12-17 11:16:17 +00:00
{
if ( ! args [ 1 ] . Contains ( ".cnp" ) )
{
string opnfile = File . ReadAllText ( args [ 1 ] ) ;
string NameWithotPath = Path . GetFileName ( args [ 1 ] ) ;
2019-09-28 19:57:03 +00:00
richTextBox . Text = opnfile ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + NameWithotPath ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( args [ 1 ] ) ;
2019-09-28 19:57:03 +00:00
filePath = openFileDialog . FileName ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
richTextBox . Modified = false ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void DeleteUpdateFiles ( )
2018-12-17 11:16:17 +00:00
{
string exePath = Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) . Location ) + @"\" ;
string UpdaterExe = exePath + "Updater.exe" ;
string UpdateZip = exePath + "Crypto-Notepad-Update.zip" ;
string ZipDll = exePath + "Ionic.Zip.dll" ;
if ( File . Exists ( UpdaterExe ) )
{
File . Delete ( UpdaterExe ) ;
}
if ( File . Exists ( UpdateZip ) )
{
File . Delete ( UpdateZip ) ;
}
if ( File . Exists ( ZipDll ) )
{
File . Delete ( ZipDll ) ;
}
}
2019-10-13 11:59:01 +00:00
private void SaveConfirm ( )
2018-12-17 11:16:17 +00:00
{
2019-10-13 11:59:01 +00:00
if ( richTextBox . Modified )
2019-10-11 10:38:15 +00:00
{
2019-10-15 16:48:17 +00:00
if ( string . IsNullOrEmpty ( PublicVar . openFileName ) )
2018-12-17 11:16:17 +00:00
{
2019-10-13 11:59:01 +00:00
PublicVar . openFileName = "Unnamed.cnp" ;
2018-12-17 11:16:17 +00:00
}
2019-10-13 11:59:01 +00:00
string messageBoxText ;
if ( ! PublicVar . keyChanged )
2018-12-25 15:29:29 +00:00
{
2019-10-13 11:59:01 +00:00
messageBoxText = "Save file: " + "\"" + PublicVar . openFileName + "\"" + " ? " ;
2018-12-25 15:29:29 +00:00
}
2019-10-13 11:59:01 +00:00
else
2019-08-22 19:31:10 +00:00
{
2019-10-13 11:59:01 +00:00
messageBoxText = "Save file: " + "\"" + PublicVar . openFileName + "\"" + " with a new key? " ;
2019-08-22 19:31:10 +00:00
}
2019-10-13 11:59:01 +00:00
using ( new CenterWinDialog ( this ) )
2018-12-25 15:29:29 +00:00
{
2019-10-13 11:57:47 +00:00
DialogResult res = MessageBox . Show ( this , messageBoxText , PublicVar . appName , MessageBoxButtons . YesNoCancel , MessageBoxIcon . Question ) ;
2019-10-13 11:59:01 +00:00
if ( res = = DialogResult . Yes )
{
trayIcon . Visible = false ;
2019-10-13 11:57:47 +00:00
SaveMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
2019-10-13 11:59:01 +00:00
if ( res = = DialogResult . Cancel )
{
cancelPressed = true ;
}
2018-12-25 15:29:29 +00:00
}
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
private void CheckForUpdates ( bool autoCheck )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
try
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
WebClient client = new WebClient ( ) ;
2019-01-02 19:10:41 +00:00
Stream stream = client . OpenRead ( "https://raw.githubusercontent.com/Crypto-Notepad/Crypto-Notepad/master/version.txt" ) ;
2018-12-25 15:29:29 +00:00
StreamReader reader = new StreamReader ( stream ) ;
string content = reader . ReadToEnd ( ) ;
string version = Application . ProductVersion ;
string exePath = Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) . Location ) + @"\" ;
int appVersion = Convert . ToInt32 ( version . Replace ( "." , "" ) ) , serverVersion = Convert . ToInt32 ( content . Replace ( "." , "" ) ) ;
if ( serverVersion > appVersion )
2018-12-17 11:16:17 +00:00
{
2019-10-04 18:48:55 +00:00
if ( statusPanel . Visible )
{
StatusPanelMessage ( "update-needed" ) ;
}
else
{
using ( new CenterWinDialog ( this ) )
2019-09-28 19:37:04 +00:00
{
2019-10-13 11:57:47 +00:00
DialogResult res = MessageBox . Show ( this , "New version is available. Install it now?" , PublicVar . appName , MessageBoxButtons . YesNo , MessageBoxIcon . Information ) ;
2019-10-04 18:48:55 +00:00
if ( res = = DialogResult . Yes )
2018-12-25 15:29:29 +00:00
{
2019-10-04 18:48:55 +00:00
File . WriteAllBytes ( exePath + "Ionic.Zip.dll" , Resources . Ionic_Zip ) ;
File . WriteAllBytes ( exePath + "Updater.exe" , Resources . Updater ) ;
var pr = new Process ( ) ;
pr . StartInfo . FileName = exePath + "Updater.exe" ;
pr . StartInfo . Arguments = "/u" ;
pr . Start ( ) ;
Application . Exit ( ) ;
2018-12-25 15:29:29 +00:00
}
}
2019-10-04 18:48:55 +00:00
}
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
if ( serverVersion < = appVersion & & autoCheck )
{
2019-10-04 18:48:55 +00:00
using ( new CenterWinDialog ( this ) )
{
if ( statusPanel . Visible )
2018-12-25 15:29:29 +00:00
{
2019-10-04 18:48:55 +00:00
StatusPanelMessage ( "update-missing" ) ;
}
else
{
2019-10-13 11:57:47 +00:00
MessageBox . Show ( this , "Crypto Notepad is up to date." , PublicVar . appName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
2018-12-25 15:29:29 +00:00
}
2019-10-04 18:48:55 +00:00
}
2018-12-25 15:29:29 +00:00
}
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
catch
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:14:24 +00:00
if ( autoCheck )
{
mainMenu . Invoke ( ( Action ) delegate
{
using ( new CenterWinDialog ( this ) )
{
if ( statusPanel . Visible )
{
StatusPanelMessage ( "update-failed" ) ;
}
else
{
2019-10-13 11:57:47 +00:00
MessageBox . Show ( this , "Checking for updates failed:\nConnection lost or the server is busy." , PublicVar . appName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2019-09-28 19:14:24 +00:00
}
}
} ) ;
}
}
}
2019-09-28 19:37:04 +00:00
private async void StatusPanelMessage ( string type )
{
string ready = "Ready" ;
2019-10-13 11:59:01 +00:00
if ( statusPanelLabel . Text = = "New version is available" )
2019-09-28 19:37:04 +00:00
{
ready = "New version is available" ;
}
switch ( type )
{
case "save" :
2019-10-13 11:54:45 +00:00
if ( statusPanelLabel . Text ! = "File Saved" )
2019-09-28 19:37:04 +00:00
{
2019-10-13 11:54:45 +00:00
statusPanelLabel . Text = "File Saved" ;
2019-09-28 19:37:04 +00:00
await Task . Delay ( 3000 ) ;
2019-10-13 11:54:45 +00:00
statusPanelLabel . Text = ready ;
2019-09-28 19:37:04 +00:00
}
break ;
case "update-missing" :
2019-10-13 11:54:45 +00:00
statusPanelLabel . Text = "Crypto Notepad is up to date" ;
2019-09-28 19:37:04 +00:00
await Task . Delay ( 3000 ) ;
2019-10-13 11:54:45 +00:00
statusPanelLabel . Text = ready ;
2019-09-28 19:37:04 +00:00
break ;
case "update-failed" :
2019-10-13 11:54:45 +00:00
statusPanelLabel . Text = "Checking for updates failed" ;
2019-09-28 19:37:04 +00:00
await Task . Delay ( 3000 ) ;
2019-10-13 11:54:45 +00:00
statusPanelLabel . Text = ready ;
2019-09-28 19:37:04 +00:00
break ;
case "update-needed" :
2019-10-13 11:54:45 +00:00
statusPanelLabel . Text = "New version is available" ;
2019-09-28 19:37:04 +00:00
break ;
2019-10-11 10:38:15 +00:00
case "always-top-on" :
2019-10-13 11:54:45 +00:00
if ( statusPanelLabel . Text ! = "Always on top ON" )
2019-10-11 10:38:15 +00:00
{
2019-10-13 11:54:45 +00:00
statusPanelLabel . Text = "Always on top ON" ;
2019-10-11 10:38:15 +00:00
await Task . Delay ( 3000 ) ;
2019-10-13 11:54:45 +00:00
statusPanelLabel . Text = ready ;
2019-10-11 10:38:15 +00:00
}
break ;
case "always-top-off" :
2019-10-13 11:54:45 +00:00
if ( statusPanelLabel . Text ! = "Always on top OFF" )
2019-10-11 10:38:15 +00:00
{
2019-10-13 11:54:45 +00:00
statusPanelLabel . Text = "Always on top OFF" ;
2019-10-11 10:38:15 +00:00
await Task . Delay ( 3000 ) ;
2019-10-13 11:54:45 +00:00
statusPanelLabel . Text = ready ;
2019-10-11 10:38:15 +00:00
}
break ;
2019-09-28 19:37:04 +00:00
}
}
private void StatusPanelTextInfo ( )
{
int currentColumn = 1 + richTextBox . SelectionStart - richTextBox . GetFirstCharIndexOfCurrentLine ( ) ;
int currentLine = 0 ;
using ( RichTextBox rtb = new RichTextBox ( ) { WordWrap = false , Text = richTextBox . Text } )
{
currentLine = 1 + rtb . GetLineFromCharIndex ( richTextBox . SelectionStart ) ;
}
int linesCount = richTextBox . Lines . Count ( ) ;
if ( linesCount = = 0 )
{
linesCount = 1 ;
}
2019-10-13 11:54:45 +00:00
statusPanelLengthLabel . Text = "Length: " + richTextBox . TextLength ;
statusPaneLinesLabel . Text = "Lines: " + linesCount ;
statusPaneLnLabel . Text = "Ln: " + currentLine ;
statusPaneColLabel . Text = "Col: " + currentColumn ;
2019-09-28 19:37:04 +00:00
}
2019-10-11 10:38:15 +00:00
private void LockFile ( )
{
2018-12-25 15:29:29 +00:00
try
2018-12-17 11:16:17 +00:00
{
2019-10-13 11:59:01 +00:00
TypedPassword . Value = fileLockedKeyTextBox . Text ;
2018-12-25 15:29:29 +00:00
string opnfile = File . ReadAllText ( filePath ) ;
2019-09-28 19:57:03 +00:00
string de = AES . Decrypt ( opnfile , TypedPassword . Value , null , settings . HashAlgorithm , Convert . ToInt32 ( settings . PasswordIterations ) , Convert . ToInt32 ( settings . KeySize ) ) ;
2019-10-13 11:59:01 +00:00
fileLockedPanel . Visible = false ;
2019-10-11 10:38:15 +00:00
richTextBox . Focus ( ) ;
2019-09-28 19:57:03 +00:00
richTextBox . Text = de ;
richTextBox . SelectionStart = caretPos ;
2018-12-25 15:29:29 +00:00
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
TypedPassword . Value = null ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
catch ( Exception ex )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
if ( ex is CryptographicException )
{
TypedPassword . Value = null ;
2019-10-11 10:38:15 +00:00
using ( new CenterWinDialog ( this ) )
2018-12-25 15:29:29 +00:00
{
2019-10-13 11:57:47 +00:00
DialogResult dialogResult = MessageBox . Show ( this , "Invalid key!" , PublicVar . appName , MessageBoxButtons . RetryCancel , MessageBoxIcon . Error ) ;
2019-10-11 10:38:15 +00:00
if ( dialogResult = = DialogResult . Retry )
{
2019-10-13 11:54:45 +00:00
fileLockedKeyTextBox . Text = "" ;
fileLockedKeyTextBox . Focus ( ) ;
2019-10-11 10:38:15 +00:00
}
if ( dialogResult = = DialogResult . Cancel )
{
2019-10-13 11:54:45 +00:00
fileLockedPanel . Visible = false ;
2019-10-11 10:38:15 +00:00
Text = PublicVar . appName ;
filePath = "" ;
2019-10-15 16:48:17 +00:00
PublicVar . openFileName = "" ;
2019-10-11 10:38:15 +00:00
}
2018-12-25 15:29:29 +00:00
}
}
2018-12-17 11:16:17 +00:00
}
}
2019-10-04 18:44:30 +00:00
private void LoadSettings ( )
2019-09-28 19:57:03 +00:00
{
2019-10-04 18:44:30 +00:00
if ( settings . editorRightToLeft )
{
richTextBox . RightToLeft = RightToLeft . Yes ;
2019-10-13 11:59:01 +00:00
lineNumbers . Dock = DockStyle . Right ;
2019-10-04 18:44:30 +00:00
rightToLeftContextMenu . Checked = true ;
}
else
{
richTextBox . RightToLeft = RightToLeft . No ;
2019-10-13 11:54:45 +00:00
lineNumbers . Dock = DockStyle . Left ;
2019-10-04 18:44:30 +00:00
rightToLeftContextMenu . Checked = false ;
}
2019-10-13 11:54:45 +00:00
if ( settings . insertKey = = "Disable" )
2019-10-04 18:44:30 +00:00
{
2019-10-13 11:54:45 +00:00
insertMainMenu . ShortcutKeys = Keys . Insert ;
2019-10-04 18:44:30 +00:00
}
else
{
2019-10-13 11:54:45 +00:00
insertMainMenu . ShortcutKeys = Keys . None ;
2019-10-04 18:44:30 +00:00
}
if ( settings . autoCheckUpdate )
{
CheckForUpdates ( false ) ;
}
if ( settings . windowLocation . ToString ( ) ! = "{X=0,Y=0}" )
{
Location = settings . windowLocation ;
}
Size = settings . windowSize ;
WindowState = settings . windowState ;
if ( settings . toolbarBorder )
{
toolbarPanel . BorderStyle = BorderStyle . FixedSingle ;
}
else
{
toolbarPanel . BorderStyle = BorderStyle . None ;
}
2019-10-11 10:38:15 +00:00
TopMost = settings . alwaysOnTop ;
alwaysOnTopMainMenu . Checked = settings . alwaysOnTop ;
2019-10-11 10:37:05 +00:00
if ( settings . closeToTray | settings . minimizeToTray )
{
trayIcon . Visible = true ;
}
2019-10-04 18:44:30 +00:00
wordWrapMainMenu . Checked = settings . editorWrap ;
toolbarPanel . BackColor = settings . toolbarBackColor ;
toolbarPanel . Visible = settings . toolbarVisible ;
mainMenu . Visible = settings . mainMenuVisible ;
rightToLeftContextMenu . Checked = settings . editorRightToLeft ;
2019-10-13 11:54:45 +00:00
lineNumbers . BackColor = settings . lineNumbersBackColor ;
lineNumbers . Font = settings . editorFont ;
lineNumbers . ForeColor = settings . lineNumbersForeColor ;
2019-10-04 18:44:30 +00:00
statusPanel . ForeColor = settings . statusPanelFontColor ;
statusPanel . BackColor = settings . statusPanelBackColor ;
statusPanel . Visible = settings . statusPanelVisible ;
richTextBox . WordWrap = settings . editorWrap ;
richTextBox . ForeColor = settings . editroForeColor ;
richTextBox . BackColor = settings . editorBackColor ;
richTextBox . Font = settings . editorFont ;
BackColor = settings . editorBackColor ;
searchPanel . BackColor = settings . searchPanelBackColor ;
searchPanel . ForeColor = settings . searchPanelForeColor ;
searchTextBox . BackColor = settings . searchPanelBackColor ;
searchTextBox . ForeColor = settings . searchPanelForeColor ;
2019-10-13 11:54:45 +00:00
searchCaseSensitiveCheckBox . ForeColor = settings . searchPanelForeColor ;
searchWholeWordCheckBox . ForeColor = settings . searchPanelForeColor ;
searchFindNextButton . ForeColor = settings . searchPanelForeColor ;
lineNumbers . Visible = bool . Parse ( settings . lineNumbersVisible ) ;
lineNumbers . Show_BorderLines = bool . Parse ( settings . borderLinesVisible ) ;
lineNumbers . Show_GridLines = bool . Parse ( settings . gridLinesVisible ) ;
lineNumbers . Show_MarginLines = bool . Parse ( settings . marginLinesVisible ) ;
lineNumbers . GridLines_Color = settings . gridLinesColor ;
lineNumbers . MarginLines_Color = settings . marginLinesColor ;
lineNumbers . BorderLines_Color = settings . borderLinesColor ;
lineNumbers . BorderLines_Style = ( DashStyle ) Enum . Parse ( typeof ( DashStyle ) , settings . borderLinesStyle ) ;
lineNumbers . GridLines_Style = ( DashStyle ) Enum . Parse ( typeof ( DashStyle ) , settings . gridLinesStyle ) ;
lineNumbers . MarginLines_Style = ( DashStyle ) Enum . Parse ( typeof ( DashStyle ) , settings . marginLinesStyle ) ;
lineNumbers . MarginLines_Side = ( LineNumbers . LineNumbers . LineNumberDockSide ) Enum . Parse ( typeof ( LineNumbers . LineNumbers . LineNumberDockSide ) , settings . marginLinesSide ) ;
2019-10-04 18:44:30 +00:00
}
public void MenuIcons ( bool menuIcons )
{
if ( menuIcons )
{
newMainMenu . Image = Resources . document_plus ;
openMainMenu . Image = Resources . folder_open_document ;
saveMainMenu . Image = Resources . disk_return_black ;
saveAsMainMenu . Image = Resources . disks_black ;
2019-10-13 12:55:13 +00:00
openFileLocationMainMenu . Image = Resources . folder_horizontal ;
2019-10-04 18:44:30 +00:00
deleteFileMainMenu . Image = Resources . document_minus ;
exitMainMenu . Image = Resources . cross_button ;
undoMainMenu . Image = Resources . arrow_left ;
redoMainMenu . Image = Resources . arrow_right ;
cutMainMenu . Image = Resources . scissors ;
copyMainMenu . Image = Resources . document_copy ;
pasteMainMenu . Image = Resources . clipboard ;
deleteMainMenu . Image = Resources . minus ;
findMainMenu . Image = Resources . magnifier ;
selectAllMainMenu . Image = Resources . selection_input ;
wordWrapMainMenu . Image = Resources . wrap_option ;
clearMainMenu . Image = Resources . document ;
changeKeyMainMenu . Image = Resources . key ;
lockMainMenu . Image = Resources . lock_warning ;
settingsMainMenu . Image = Resources . gear ;
2019-10-13 12:55:13 +00:00
documentationMainMenu . Image = Resources . document_text ;
checkForUpdatesMainMenu . Image = Resources . upload_cloud ;
2019-10-04 18:44:30 +00:00
aboutMainMenu . Image = Resources . information ;
2019-10-11 10:31:24 +00:00
alwaysOnTopMainMenu . Image = Resources . applications_blue ;
2018-12-29 16:26:51 +00:00
}
else
{
2019-09-28 19:57:03 +00:00
foreach ( ToolStripItem item in mainMenu . Items )
2018-12-29 16:26:51 +00:00
{
if ( item is ToolStripDropDownItem )
foreach ( ToolStripItem dropDownItem in ( ( ToolStripDropDownItem ) item ) . DropDownItems )
{
dropDownItem . Image = null ;
}
}
}
}
2019-10-02 16:35:34 +00:00
2019-10-04 18:48:55 +00:00
public void Toolbaricons ( bool oldIcons )
2019-10-02 16:24:52 +00:00
{
2019-10-04 18:48:55 +00:00
if ( oldIcons )
2019-10-02 16:24:52 +00:00
{
newToolbarButton . Image = Resources . old_page_white_add ;
openToolbarButton . Image = Resources . old_folder_vertical_document ;
saveToolbarButton . Image = Resources . old_diskette ;
fileLocationToolbarButton . Image = Resources . old_folder_stand ;
deleteFileToolbarButton . Image = Resources . old_page_white_delete ;
cutToolbarButton . Image = Resources . old_cut_red ;
copyToolbarButton . Image = Resources . old_page_white_copy ;
pasteToolbarButton . Image = Resources . old_paste_plain ;
changeKeyToolbarButton . Image = Resources . old_page_white_key ;
lockToolbarButton . Image = Resources . old_lock ;
settingsToolbarButton . Image = Resources . old_setting_tools ;
2019-10-11 10:31:24 +00:00
alwaysOnTopToolbarButton . Image = Resources . old_application_double ;
2019-10-02 16:24:52 +00:00
}
else
{
newToolbarButton . Image = Resources . document_plus ;
openToolbarButton . Image = Resources . folder_open_document ;
saveToolbarButton . Image = Resources . disk_return_black ;
fileLocationToolbarButton . Image = Resources . folder_horizontal ;
deleteFileToolbarButton . Image = Resources . document_minus ;
cutToolbarButton . Image = Resources . scissors ;
copyToolbarButton . Image = Resources . document_copy ;
pasteToolbarButton . Image = Resources . clipboard ;
changeKeyToolbarButton . Image = Resources . key ;
lockToolbarButton . Image = Resources . lock_warning ;
settingsToolbarButton . Image = Resources . gear ;
2019-10-11 10:31:24 +00:00
alwaysOnTopToolbarButton . Image = Resources . applications_blue ;
2019-10-02 16:24:52 +00:00
}
}
2019-09-28 19:57:03 +00:00
#endregion
2018-12-17 11:16:17 +00:00
2019-09-28 19:57:03 +00:00
#region Event Handlers
2019-10-11 10:37:05 +00:00
private void MainForm_Resize ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-10-11 10:37:05 +00:00
if ( settings . minimizeToTray )
{
if ( WindowState = = FormWindowState . Minimized )
{
Hide ( ) ;
}
}
2018-12-17 11:16:17 +00:00
2019-10-15 16:48:17 +00:00
if ( WindowState = = FormWindowState . Minimized & settings . autoLock & ! string . IsNullOrEmpty ( PublicVar . encryptionKey . Get ( ) ) )
2018-12-17 11:16:17 +00:00
{
2019-10-13 11:54:45 +00:00
fileLockedPanel . Visible = true ;
2018-12-17 11:16:17 +00:00
}
2019-10-11 10:37:05 +00:00
if ( WindowState = = FormWindowState . Normal )
2018-12-17 11:16:17 +00:00
{
2019-10-13 11:54:45 +00:00
if ( fileLockedPanel . Visible )
2019-10-11 10:37:05 +00:00
{
2019-10-13 11:54:45 +00:00
fileLockedKeyTextBox . Focus ( ) ;
2019-10-11 10:37:05 +00:00
}
2018-12-17 11:16:17 +00:00
}
2019-10-11 10:38:15 +00:00
}
private void MainWindow_Activated ( object sender , EventArgs e )
{
richTextBox . Focus ( ) ;
if ( PublicVar . keyChanged )
2018-12-17 11:16:17 +00:00
{
2019-10-11 10:38:15 +00:00
richTextBox . Modified = true ;
2018-12-17 11:16:17 +00:00
}
2019-10-13 11:54:45 +00:00
lineNumbers . Refresh ( ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void MainWindow_FormClosing ( object sender , FormClosingEventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-10-13 11:59:01 +00:00
if ( WindowState = = FormWindowState . Normal )
{
settings . windowSize = Size ;
settings . windowLocation = Location ;
settings . windowState = WindowState ;
}
if ( WindowState = = FormWindowState . Maximized )
{
settings . windowState = WindowState ;
}
settings . Save ( ) ;
2019-10-11 10:37:05 +00:00
if ( settings . closeToTray )
2018-12-25 15:29:29 +00:00
{
2019-10-13 11:59:01 +00:00
if ( e . CloseReason = = CloseReason . UserClosing )
2019-10-11 10:37:05 +00:00
{
2019-10-15 16:48:17 +00:00
if ( settings . autoLock & ! string . IsNullOrEmpty ( PublicVar . encryptionKey . Get ( ) ) )
2019-10-13 11:59:01 +00:00
{
fileLockedPanel . Visible = true ;
}
Hide ( ) ;
e . Cancel = true ;
return ;
2019-10-11 10:37:05 +00:00
}
2018-12-25 15:29:29 +00:00
}
2019-10-13 11:59:01 +00:00
if ( richTextBox . Modified )
2018-12-25 15:29:29 +00:00
{
2019-10-15 16:48:17 +00:00
if ( string . IsNullOrEmpty ( PublicVar . openFileName ) )
2019-10-13 11:59:01 +00:00
{
PublicVar . openFileName = "Unnamed.cnp" ;
}
2019-10-15 16:48:17 +00:00
if ( ! string . IsNullOrEmpty ( richTextBox . Text ) )
2019-10-13 11:59:01 +00:00
{
string messageBoxText ;
if ( ! PublicVar . keyChanged )
{
messageBoxText = "Save file: " + "\"" + PublicVar . openFileName + "\"" + " ? " ;
}
else
{
messageBoxText = "Save file: " + "\"" + PublicVar . openFileName + "\"" + " with a new key? " ;
}
using ( new CenterWinDialog ( this ) )
{
2019-10-13 11:57:47 +00:00
DialogResult res = MessageBox . Show ( this , messageBoxText , PublicVar . appName , MessageBoxButtons . YesNoCancel , MessageBoxIcon . Question ) ;
if ( res = = DialogResult . Yes )
{
trayIcon . Visible = false ;
SaveMainMenu_Click ( this , new EventArgs ( ) ) ;
2019-10-13 11:59:01 +00:00
}
if ( res = = DialogResult . Cancel )
{
e . Cancel = true ;
}
}
}
}
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
private void MainForm_Shown ( object sender , EventArgs e )
{
Visible = true ;
2019-09-28 19:22:53 +00:00
richTextBox . SetInnerMargins ( Convert . ToInt32 ( settings . editorPaddingLeft ) , 0 , 0 , 0 ) ;
2019-09-28 19:57:03 +00:00
richTextBox . Modified = false ;
}
2018-12-29 16:26:51 +00:00
2018-12-25 15:29:29 +00:00
private void MainWindow_Load ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
Visible = false ;
2019-10-11 10:38:15 +00:00
searchPanel . CellBorderStyle = TableLayoutPanelCellBorderStyle . Single ;
2019-10-04 18:44:30 +00:00
LoadSettings ( ) ;
2018-12-25 15:29:29 +00:00
DeleteUpdateFiles ( ) ;
2019-10-04 18:44:30 +00:00
MenuIcons ( settings . menuIcons ) ;
2019-10-02 16:24:52 +00:00
Toolbaricons ( settings . oldToolbarIcons ) ;
2019-08-21 13:19:10 +00:00
if ( args . Length = = 2 ) /*drag & drop to executable*/
2019-08-19 19:35:20 +00:00
{
OpenAsotiations ( ) ;
}
2018-12-25 15:29:29 +00:00
if ( args . Contains ( "/s" ) ) /*send to*/
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
foreach ( var arg in args )
{
argsPath = arg ;
}
SendTo ( ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
if ( args . Contains ( "/o" ) ) /*decrypt & open cnp*/
{
OpenAsotiations ( ) ;
}
if ( args . Contains ( "/e" ) ) /*encrypt*/
{
ContextMenuEncrypt ( ) ;
}
if ( args . Contains ( "/er" ) ) /*encrypt and replace*/
{
ContextMenuEncryptReplace ( ) ;
}
2019-10-04 19:00:37 +00:00
#if DEBUG
2019-09-28 19:57:03 +00:00
debugMainMenu . Visible = true ;
2019-10-04 19:00:37 +00:00
#endif
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
2019-08-22 17:55:56 +00:00
private void RichTextBox_SelectionChanged ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
if ( richTextBox . SelectionLength ! = 0 )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
cutToolbarButton . Enabled = true ;
copyToolbarButton . Enabled = true ;
2018-12-17 11:16:17 +00:00
}
else
{
2019-09-28 19:57:03 +00:00
cutToolbarButton . Enabled = false ;
copyToolbarButton . Enabled = false ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
StatusPanelTextInfo ( ) ;
2018-12-17 11:16:17 +00:00
}
2019-08-22 17:55:56 +00:00
private void RichTextBox_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
StatusPanelTextInfo ( ) ;
2018-12-17 11:16:17 +00:00
}
2019-08-22 17:55:56 +00:00
private void RichTextBox_KeyDown ( object sender , KeyEventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
if ( e . KeyCode = = Keys . Escape )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
searchTextBox . Text = "" ;
searchPanel . Visible = false ;
richTextBox . Focus ( ) ;
richTextBox . DeselectAll ( ) ;
e . Handled = e . SuppressKeyPress = true ;
findPos = 0 ;
2018-12-25 15:29:29 +00:00
}
2019-10-15 16:09:51 +00:00
if ( e . KeyCode = = Keys . Enter & searchPanel . Visible & ! string . IsNullOrEmpty ( searchTextBox . Text ) )
2019-08-22 17:05:18 +00:00
{
2019-10-13 11:54:45 +00:00
SearchFindNextButton_Click ( this , new EventArgs ( ) ) ;
2019-08-22 17:05:18 +00:00
e . Handled = e . SuppressKeyPress = true ;
}
2018-12-17 11:16:17 +00:00
}
2019-08-22 17:55:56 +00:00
private void RichTextBox_LinkClicked ( object sender , LinkClickedEventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
if ( settings . openLinks = = "LMB Click" )
2018-12-25 15:29:29 +00:00
{
Process . Start ( e . LinkText ) ;
}
2019-09-28 19:57:03 +00:00
if ( settings . openLinks = = "Shift+LMB" )
{
if ( ( ModifierKeys & Keys . Shift ) ! = 0 )
{
Process . Start ( e . LinkText ) ;
}
}
if ( settings . openLinks = = "Control+LMB" )
{
if ( ( ModifierKeys & Keys . Control ) ! = 0 )
{
Process . Start ( e . LinkText ) ;
}
}
2018-12-17 11:16:17 +00:00
}
2019-08-22 17:55:56 +00:00
private void RichTextBox_DragDrop ( object sender , DragEventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-10-13 11:59:01 +00:00
SaveConfirm ( ) ;
if ( cancelPressed )
{
cancelPressed = false ;
return ;
}
2018-12-25 15:29:29 +00:00
string [ ] FileList = ( string [ ] ) e . Data . GetData ( DataFormats . FileDrop , false ) ;
2019-09-28 19:57:03 +00:00
foreach ( string file in FileList ) openFileDialog . FileName = file ;
2018-12-25 15:29:29 +00:00
object fname = e . Data . GetData ( "FileDrop" ) ;
2019-09-28 19:57:03 +00:00
PublicVar . openFileName = Path . GetFileName ( openFileDialog . FileName ) ;
2018-12-25 15:29:29 +00:00
if ( fname ! = null )
{
var list = fname as string [ ] ;
if ( list ! = null & & ! string . IsNullOrWhiteSpace ( list [ 0 ] ) )
{
2019-09-28 19:57:03 +00:00
if ( ! openFileDialog . FileName . Contains ( ".cnp" ) )
{
string opnfile = File . ReadAllText ( openFileDialog . FileName ) ;
string NameWithotPath = Path . GetFileName ( openFileDialog . FileName ) ;
richTextBox . Text = opnfile ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2019-09-28 19:57:03 +00:00
filePath = openFileDialog . FileName ;
2018-12-25 15:29:29 +00:00
return ;
}
DecryptAES ( ) ;
}
}
2019-09-28 19:57:03 +00:00
}
2019-09-28 19:37:04 +00:00
private void RichTextBox_TextChanged ( object sender , EventArgs e )
{
StatusPanelTextInfo ( ) ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:37:04 +00:00
private void RichTextBox_CursorPositionChanged ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:37:04 +00:00
StatusPanelTextInfo ( ) ;
}
2019-10-13 11:59:01 +00:00
private void StatusPanelLabel_TextChanged ( object sender , EventArgs e )
2019-09-28 19:37:04 +00:00
{
2019-10-13 11:59:01 +00:00
if ( statusPanelLabel . Text = = "New version is available" )
2018-12-25 15:29:29 +00:00
{
2019-10-13 11:59:01 +00:00
statusPanelLabel . IsLink = true ;
2019-09-28 19:37:04 +00:00
}
else
{
2019-10-13 11:59:01 +00:00
statusPanelLabel . IsLink = false ;
2018-12-25 15:29:29 +00:00
}
2018-12-17 11:16:17 +00:00
}
2019-10-13 11:59:01 +00:00
private void StatusPanelLabel_Click ( object sender , EventArgs e )
2019-09-28 19:37:04 +00:00
{
2019-10-13 11:59:01 +00:00
if ( statusPanelLabel . Text = = "New version is available" )
2019-09-28 19:37:04 +00:00
{
string exePath = Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) . Location ) + @"\" ;
using ( new CenterWinDialog ( this ) )
{
2019-10-13 11:57:47 +00:00
DialogResult res = MessageBox . Show ( this , "New version is available. Install it now?" , PublicVar . appName , MessageBoxButtons . YesNo , MessageBoxIcon . Information ) ;
2019-09-28 19:37:04 +00:00
if ( res = = DialogResult . Yes )
{
2019-10-04 18:48:55 +00:00
File . WriteAllBytes ( exePath + "Ionic.Zip.dll" , Resources . Ionic_Zip ) ;
File . WriteAllBytes ( exePath + "Updater.exe" , Resources . Updater ) ;
2019-09-28 19:37:04 +00:00
var pr = new Process ( ) ;
pr . StartInfo . FileName = exePath + "Updater.exe" ;
pr . StartInfo . Arguments = "/u" ;
pr . Start ( ) ;
Application . Exit ( ) ;
}
}
}
}
#endregion
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
2019-09-28 19:57:03 +00:00
#region Main Menu
2018-12-25 15:29:29 +00:00
/*File*/
2019-09-28 19:57:03 +00:00
private void NewMainMenu_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-10-13 11:59:01 +00:00
SaveConfirm ( ) ;
if ( cancelPressed )
{
cancelPressed = false ;
return ;
}
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = "Unnamed.cnp" ;
2019-09-28 19:57:03 +00:00
EnterKeyForm enterKeyForm = new EnterKeyForm
{
Owner = this
} ;
enterKeyForm . ShowDialog ( ) ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
if ( ! PublicVar . okPressed )
{
2019-10-15 16:48:17 +00:00
if ( ! string . IsNullOrEmpty ( filePath ) )
2019-01-07 10:09:52 +00:00
{
PublicVar . openFileName = Path . GetFileName ( filePath ) ;
2019-08-21 11:15:16 +00:00
}
2018-12-25 15:29:29 +00:00
TypedPassword . Value = null ;
}
else
{
2019-09-28 19:57:03 +00:00
saveFileDialog . FileName = "Unnamed.cnp" ;
2019-01-07 10:09:52 +00:00
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
2018-12-25 15:29:29 +00:00
PublicVar . okPressed = false ;
2019-09-28 19:57:03 +00:00
if ( saveFileDialog . ShowDialog ( ) ! = DialogResult . OK )
2018-12-25 15:29:29 +00:00
{
TypedPassword . Value = null ;
return ;
}
2019-09-28 19:57:03 +00:00
richTextBox . Clear ( ) ;
StreamWriter sw = new StreamWriter ( saveFileDialog . FileName ) ;
string NameWithotPath = Path . GetFileName ( saveFileDialog . FileName ) ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2019-09-28 19:57:03 +00:00
filePath = saveFileDialog . FileName ;
PublicVar . openFileName = Path . GetFileName ( saveFileDialog . FileName ) ;
2018-12-25 15:29:29 +00:00
sw . Close ( ) ;
}
TypedPassword . Value = null ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
private void OpenMainMenu_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-10-13 11:59:01 +00:00
SaveConfirm ( ) ;
if ( cancelPressed )
2019-09-28 19:57:03 +00:00
{
2019-10-13 11:59:01 +00:00
cancelPressed = false ;
2019-09-28 19:57:03 +00:00
return ;
}
2019-10-13 11:59:01 +00:00
openFileDialog . FileName = "" ;
2019-09-28 19:57:03 +00:00
if ( openFileDialog . ShowDialog ( ) ! = DialogResult . OK ) return ;
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
PublicVar . openFileName = Path . GetFileName ( openFileDialog . FileName ) ;
if ( ! openFileDialog . FileName . Contains ( ".cnp" ) )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
string opnfile = File . ReadAllText ( openFileDialog . FileName ) ;
string NameWithotPath = Path . GetFileName ( openFileDialog . FileName ) ;
richTextBox . Text = opnfile ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-25 15:29:29 +00:00
return ;
}
DecryptAES ( ) ;
2019-09-28 19:57:03 +00:00
richTextBox . Modified = false ;
2018-12-17 11:16:17 +00:00
}
}
2019-09-28 19:57:03 +00:00
private void SaveMainMenu_Click ( object sender , EventArgs e )
2019-10-15 16:48:17 +00:00
{
if ( string . IsNullOrEmpty ( PublicVar . encryptionKey . Get ( ) ) )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
SaveAsMainMenu_Click ( this , new EventArgs ( ) ) ;
2019-10-15 16:48:17 +00:00
return ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
string enc = AES . Encrypt ( richTextBox . Text , PublicVar . encryptionKey . Get ( ) , null , settings . HashAlgorithm , Convert . ToInt32 ( settings . PasswordIterations ) , Convert . ToInt32 ( settings . KeySize ) ) ;
2019-08-30 10:05:29 +00:00
using ( StreamWriter writer = new StreamWriter ( filePath ) )
2018-12-17 11:16:17 +00:00
{
2019-08-30 10:05:29 +00:00
writer . Write ( enc ) ;
writer . Close ( ) ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
richTextBox . Modified = false ;
2018-12-25 15:29:29 +00:00
PublicVar . keyChanged = false ;
2019-09-28 19:37:04 +00:00
StatusPanelMessage ( "save" ) ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
private void SaveAsMainMenu_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-10-15 16:48:17 +00:00
if ( ! string . IsNullOrEmpty ( filePath ) )
2019-01-07 10:09:52 +00:00
{
PublicVar . openFileName = Path . GetFileName ( filePath ) ;
2019-09-28 19:57:03 +00:00
saveFileDialog . FileName = Path . GetFileName ( filePath ) ;
2019-01-07 10:09:52 +00:00
}
else
{
PublicVar . openFileName = "Unnamed.cnp" ;
2019-09-28 19:57:03 +00:00
saveFileDialog . FileName = "Unnamed.cnp" ;
2019-01-07 10:09:52 +00:00
}
2019-09-28 19:57:03 +00:00
EnterKeyForm enterKeyForm = new EnterKeyForm
{
Owner = this
} ;
2018-12-25 15:29:29 +00:00
if ( string . IsNullOrEmpty ( PublicVar . encryptionKey . Get ( ) ) )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
enterKeyForm . ShowDialog ( ) ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
if ( saveFileDialog . ShowDialog ( ) ! = DialogResult . OK )
2018-12-17 11:16:17 +00:00
{
return ;
}
2018-12-25 15:29:29 +00:00
if ( TypedPassword . Value = = null )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
TypedPassword . Value = PublicVar . encryptionKey . Get ( ) ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
filePath = saveFileDialog . FileName ;
string enc = AES . Encrypt ( richTextBox . Text , TypedPassword . Value , null , settings . HashAlgorithm , Convert . ToInt32 ( settings . PasswordIterations ) , Convert . ToInt32 ( settings . KeySize ) ) ;
2019-08-30 10:05:29 +00:00
using ( StreamWriter writer = new StreamWriter ( filePath ) )
{
writer . Write ( enc ) ;
writer . Close ( ) ;
}
2019-09-28 19:57:03 +00:00
richTextBox . Modified = false ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + Path . GetFileName ( filePath ) ;
2018-12-25 15:29:29 +00:00
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
TypedPassword . Value = null ;
2019-09-28 19:57:03 +00:00
PublicVar . openFileName = Path . GetFileName ( saveFileDialog . FileName ) ;
2019-09-28 19:37:04 +00:00
StatusPanelMessage ( "save" ) ;
2018-12-25 15:29:29 +00:00
}
2019-10-13 12:55:13 +00:00
private void OpenFileLocationMainMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
Process . Start ( "explorer.exe" , @"/select, " + filePath ) ;
}
private void DeleteFileToolStripMenuItem_Click ( object sender , EventArgs e )
{
2019-10-15 16:48:17 +00:00
if ( ! string . IsNullOrEmpty ( filePath ) )
2018-12-25 15:29:29 +00:00
{
using ( new CenterWinDialog ( this ) )
2018-12-17 11:16:17 +00:00
{
2019-10-13 11:57:47 +00:00
if ( MessageBox . Show ( this , "Delete file: " + "\"" + filePath + "\"" + " ?" , PublicVar . appName , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) = = DialogResult . Yes )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
File . Delete ( filePath ) ;
2019-09-28 19:57:03 +00:00
richTextBox . Clear ( ) ;
2018-12-17 11:16:17 +00:00
PublicVar . encryptionKey . Set ( null ) ;
2019-09-28 19:57:03 +00:00
fileLocationToolbarButton . Enabled = false ;
deleteFileToolbarButton . Enabled = false ;
changeKeyToolbarButton . Enabled = false ;
lockToolbarButton . Enabled = false ;
2018-12-19 22:56:59 +00:00
filePath = "" ;
2019-10-15 16:48:17 +00:00
PublicVar . openFileName = "" ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName ;
2018-12-17 11:16:17 +00:00
return ;
}
}
}
}
2019-09-28 19:57:03 +00:00
private void ExitMainMenu_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-10-13 11:59:01 +00:00
Application . Exit ( ) ;
2018-12-25 15:29:29 +00:00
}
2018-12-17 11:16:17 +00:00
2019-10-13 12:24:47 +00:00
private void FileMainMenu_DropDownOpened ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-10-15 16:48:17 +00:00
if ( string . IsNullOrEmpty ( filePath ) )
2018-12-17 11:16:17 +00:00
{
2019-10-13 12:55:13 +00:00
openFileLocationMainMenu . Enabled = false ;
2019-09-28 19:57:03 +00:00
deleteFileMainMenu . Enabled = false ;
2018-12-25 15:29:29 +00:00
}
else
{
2019-10-13 12:55:13 +00:00
openFileLocationMainMenu . Enabled = true ;
2019-09-28 19:57:03 +00:00
deleteFileMainMenu . Enabled = true ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
/*File*/
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
/*Edit*/
2019-09-28 19:57:03 +00:00
private void EditMainMenu_DropDownOpened ( object sender , EventArgs e )
{
if ( richTextBox . SelectionLength ! = 0 )
{
cutMainMenu . Enabled = true ;
copyMainMenu . Enabled = true ;
deleteMainMenu . Enabled = true ;
}
else
{
cutMainMenu . Enabled = false ;
copyMainMenu . Enabled = false ;
deleteMainMenu . Enabled = false ;
}
}
private void UndoMainMenu_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
richTextBox . Undo ( ) ;
richTextBox . DeselectAll ( ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
public void RedoMainMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
richTextBox . Redo ( ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
private void CutMainMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
richTextBox . Cut ( ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
private void CopyMainMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
richTextBox . Copy ( ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
private void PasteMainMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
if ( richTextBox . Focused )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
richTextBox . Paste ( DataFormats . GetFormat ( DataFormats . Text ) ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
if ( searchTextBox . Focused )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
searchTextBox . Paste ( ) ;
2018-12-25 15:29:29 +00:00
}
}
2019-09-28 19:57:03 +00:00
private void DeleteMainMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
richTextBox . SelectedText = "" ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
private void FindMainMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-10-11 10:38:15 +00:00
if ( ! richTextBox . Visible ) return ;
2019-09-28 19:57:03 +00:00
if ( searchPanel . Visible )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
searchTextBox . Text = "" ;
searchPanel . Visible = false ;
richTextBox . Focus ( ) ;
richTextBox . DeselectAll ( ) ;
2018-12-17 11:16:17 +00:00
}
else
{
2019-09-28 19:57:03 +00:00
searchPanel . Visible = true ;
searchTextBox . Focus ( ) ;
2018-12-17 11:16:17 +00:00
}
}
2019-09-28 19:57:03 +00:00
private void SelectAllMainMenu_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
if ( richTextBox . Focused )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
richTextBox . SelectAll ( ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
if ( searchTextBox . Focused )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
searchTextBox . SelectAll ( ) ;
2018-12-25 15:29:29 +00:00
}
}
2019-09-28 19:57:03 +00:00
private void WordWrapMainMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
if ( wordWrapMainMenu . Checked )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
richTextBox . WordWrap = true ;
2018-12-17 11:16:17 +00:00
}
else
{
2019-09-28 19:57:03 +00:00
richTextBox . WordWrap = false ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
settings . menuWrap = wordWrapMainMenu . Checked ;
settings . editorWrap = richTextBox . WordWrap ;
settings . Save ( ) ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
private void ClearMainMenu_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
richTextBox . SelectAll ( ) ;
richTextBox . SelectedText = " " ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
/*Edit*/
2018-12-17 11:16:17 +00:00
2019-09-28 19:57:03 +00:00
/*Tools*/
2019-10-11 10:31:24 +00:00
private void AlwaysOnTopMainMenu_Click ( object sender , EventArgs e )
{
if ( alwaysOnTopMainMenu . Checked )
{
settings . alwaysOnTop = true ;
TopMost = true ;
StatusPanelMessage ( "always-top-on" ) ;
}
else
{
settings . alwaysOnTop = false ;
TopMost = false ;
StatusPanelMessage ( "always-top-off" ) ;
}
}
2019-10-11 10:38:15 +00:00
2019-09-28 19:57:03 +00:00
private void ToolsMainMenu_DropDownOpened ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-10-15 16:48:17 +00:00
if ( string . IsNullOrEmpty ( PublicVar . encryptionKey . Get ( ) ) )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
changeKeyMainMenu . Enabled = false ;
lockMainMenu . Enabled = false ;
2018-12-25 15:29:29 +00:00
}
else
{
2019-09-28 19:57:03 +00:00
changeKeyMainMenu . Enabled = true ;
lockMainMenu . Enabled = true ;
2018-12-17 11:16:17 +00:00
}
}
2019-09-28 19:57:03 +00:00
private void ChangeKeyMainMenu_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
ChangeKeyForm changeKeyForm = new ChangeKeyForm ( ) ;
changeKeyForm . ShowDialog ( this ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
private void LockMainMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
SaveMainMenu_Click ( this , new EventArgs ( ) ) ;
2019-10-13 11:54:45 +00:00
fileLockedPanel . Visible = true ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
private void SettingsMainMenu_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
SettingsForm settingsForm = new SettingsForm
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
Owner = this
} ;
settingsForm . ShowDialog ( ) ;
2018-12-25 15:29:29 +00:00
}
/*Tools*/
/*Help*/
2019-10-13 12:55:13 +00:00
private void DocumentationMainMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-01-02 19:10:41 +00:00
Process . Start ( "https://github.com/Crypto-Notepad/Crypto-Notepad/wiki/Documentation" ) ;
2018-12-25 15:29:29 +00:00
}
2019-10-13 12:55:13 +00:00
private void CheckForUpdatesMainMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-10-02 16:35:34 +00:00
CheckForUpdates ( true ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
private void AboutMainMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
AboutFrom aboutFrom = new AboutFrom ( ) ;
aboutFrom . ShowDialog ( this ) ;
2018-12-25 15:29:29 +00:00
}
/*Help*/
2019-09-28 19:57:03 +00:00
#endregion
2018-12-25 15:29:29 +00:00
2019-09-28 19:57:03 +00:00
#region Editor Menu
private void ContextMenu_Opening ( object sender , CancelEventArgs e )
{
if ( richTextBox . SelectionLength ! = 0 )
{
cutContextMenu . Enabled = true ;
copyContextMenu . Enabled = true ;
deleteContextMenu . Enabled = true ;
}
else
{
cutContextMenu . Enabled = false ;
copyContextMenu . Enabled = false ;
deleteContextMenu . Enabled = false ;
}
}
2018-12-25 15:29:29 +00:00
2019-09-28 19:57:03 +00:00
private void UndoContextMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
UndoMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
private void RedoContextMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
RedoMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
private void CutContextMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-10-13 12:55:13 +00:00
CutMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
private void CopyContextMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
CopyMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
private void PasteContextMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
PasteMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
private void DeleteContextMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
DeleteMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
private void SelectAllContextMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
SelectAllMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
2018-12-17 11:16:17 +00:00
2019-09-28 19:29:24 +00:00
private void RightToLeftContextMenu_Click ( object sender , EventArgs e )
2018-12-25 15:22:59 +00:00
{
2019-09-28 19:29:24 +00:00
if ( rightToLeftContextMenu . Checked )
2018-12-25 15:22:59 +00:00
{
2019-09-28 19:57:03 +00:00
if ( ! richTextBox . WordWrap )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:57:03 +00:00
string rtbTxt = richTextBox . Text ;
richTextBox . Clear ( ) ;
2019-09-28 19:29:24 +00:00
richTextBox . RightToLeft = RightToLeft . Yes ;
2018-12-25 15:22:59 +00:00
Application . DoEvents ( ) ;
2019-09-28 19:57:03 +00:00
richTextBox . Text = rtbTxt ;
2018-12-25 15:29:29 +00:00
}
2018-12-25 15:22:59 +00:00
else
{
2019-09-28 19:57:03 +00:00
string rtbTxt = richTextBox . Text ;
richTextBox . Clear ( ) ;
2019-09-28 19:29:24 +00:00
richTextBox . RightToLeft = RightToLeft . Yes ;
2019-09-28 19:57:03 +00:00
Application . DoEvents ( ) ;
richTextBox . Text = rtbTxt ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:29:24 +00:00
settings . editorRightToLeft = true ;
2019-10-13 11:54:45 +00:00
lineNumbers . Dock = DockStyle . Right ;
2019-09-28 19:57:03 +00:00
richTextBox . Modified = false ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:22:59 +00:00
else
{
2019-09-28 19:29:24 +00:00
richTextBox . RightToLeft = RightToLeft . No ;
settings . editorRightToLeft = false ;
2019-10-13 11:54:45 +00:00
lineNumbers . Dock = DockStyle . Left ;
2019-09-28 19:57:03 +00:00
richTextBox . Modified = false ;
}
settings . Save ( ) ;
2018-12-17 11:16:17 +00:00
}
2019-09-28 19:57:03 +00:00
private void ClearContextMenu_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
ClearMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:57:03 +00:00
#endregion
2018-12-17 11:16:17 +00:00
2019-09-28 19:57:03 +00:00
#region Toolbar
2018-12-25 15:29:29 +00:00
private void NewToolbarButton_Click ( object sender , EventArgs e )
{
2019-09-28 19:57:03 +00:00
NewMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
private void OpenToolbarButton_Click ( object sender , EventArgs e )
{
2019-09-28 19:57:03 +00:00
OpenMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
private void SaveToolbarButton_Click ( object sender , EventArgs e )
{
2019-09-28 19:57:03 +00:00
SaveMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
private void FileLocationToolbarButton_Click ( object sender , EventArgs e )
{
2019-10-13 12:55:13 +00:00
OpenFileLocationMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
private void DeleteFileToolbarButton_Click ( object sender , EventArgs e )
{
DeleteFileToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
private void CutToolbarButton_Click ( object sender , EventArgs e )
{
2019-09-28 19:57:03 +00:00
CutMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
private void CopyToolbarButton_Click ( object sender , EventArgs e )
{
2019-09-28 19:57:03 +00:00
CopyMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
private void PasteToolbarButton_Click ( object sender , EventArgs e )
{
2019-09-28 19:57:03 +00:00
PasteMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
private void ChangeKeyToolbarButton_Click ( object sender , EventArgs e )
{
2019-09-28 19:57:03 +00:00
ChangeKeyMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
private void SettingsToolbarButton_Click ( object sender , EventArgs e )
{
2019-09-28 19:57:03 +00:00
SettingsMainMenu_Click ( this , new EventArgs ( ) ) ;
2018-12-25 15:29:29 +00:00
}
2019-01-04 12:14:12 +00:00
private void LockToolbarButton_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
LockMainMenu_Click ( this , new EventArgs ( ) ) ;
2019-01-04 12:17:03 +00:00
}
2018-12-25 15:29:29 +00:00
2019-09-28 19:57:03 +00:00
private void CloseToolbarButton_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:57:03 +00:00
toolbarPanel . Visible = false ;
2019-10-04 18:48:55 +00:00
settings . toolbarVisible = false ;
2018-12-25 15:29:29 +00:00
}
2018-12-17 11:16:17 +00:00
2019-09-28 19:57:03 +00:00
private void CloseToolbarButton_MouseEnter ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-10-04 18:48:55 +00:00
closeToolbarButton . Image = Resources . close_b ;
2018-12-25 15:29:29 +00:00
}
2018-12-17 11:16:17 +00:00
2019-09-28 19:57:03 +00:00
private void CloseToolbarButton_MouseLeave ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-10-04 18:48:55 +00:00
closeToolbarButton . Image = Resources . close_g ;
2018-12-17 11:16:17 +00:00
}
2019-10-11 10:31:24 +00:00
private void AlwaysOnTopToolbarButton_Click ( object sender , EventArgs e )
{
if ( settings . alwaysOnTop )
{
settings . alwaysOnTop = false ;
TopMost = settings . alwaysOnTop ;
alwaysOnTopMainMenu . Checked = settings . alwaysOnTop ;
StatusPanelMessage ( "always-top-off" ) ;
}
else
{
settings . alwaysOnTop = true ;
TopMost = settings . alwaysOnTop ;
alwaysOnTopMainMenu . Checked = settings . alwaysOnTop ;
StatusPanelMessage ( "always-top-on" ) ;
}
}
2019-10-11 10:38:15 +00:00
private void ToolbarPanel_MouseEnter ( object sender , EventArgs e )
{
if ( richTextBox . SelectionLength ! = 0 )
{
cutToolbarButton . Enabled = true ;
copyToolbarButton . Enabled = true ;
}
else
{
cutToolbarButton . Enabled = false ;
copyToolbarButton . Enabled = false ;
}
2019-10-15 16:48:17 +00:00
if ( string . IsNullOrEmpty ( PublicVar . encryptionKey . Get ( ) ) )
2019-10-11 10:38:15 +00:00
{
fileLocationToolbarButton . Enabled = false ;
deleteFileToolbarButton . Enabled = false ;
changeKeyToolbarButton . Enabled = false ;
lockToolbarButton . Enabled = false ;
}
else
{
fileLocationToolbarButton . Enabled = true ;
deleteFileToolbarButton . Enabled = true ;
changeKeyToolbarButton . Enabled = true ;
lockToolbarButton . Enabled = true ;
}
}
2019-09-28 19:57:03 +00:00
#endregion
2018-12-25 15:29:29 +00:00
2019-09-28 19:54:34 +00:00
#region Search Panel
2018-12-25 15:29:29 +00:00
private void SearchTextBox_TextChanged ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2019-08-22 17:05:18 +00:00
findPos = 0 ;
2018-12-25 15:29:29 +00:00
}
private void SearchTextBox_KeyDown ( object sender , KeyEventArgs e )
{
if ( e . KeyCode = = Keys . Escape )
2018-12-17 11:16:17 +00:00
{
2019-09-28 19:54:34 +00:00
searchTextBox . Text = "" ;
searchPanel . Visible = false ;
richTextBox . Focus ( ) ;
richTextBox . DeselectAll ( ) ;
2018-12-25 15:29:29 +00:00
e . Handled = e . SuppressKeyPress = true ;
2019-08-22 17:05:18 +00:00
findPos = 0 ;
2018-12-17 11:16:17 +00:00
}
2019-10-15 16:09:51 +00:00
if ( e . KeyCode = = Keys . Enter & searchPanel . Visible & ! string . IsNullOrEmpty ( searchTextBox . Text ) )
2019-09-28 19:54:34 +00:00
{
2019-10-13 11:54:45 +00:00
SearchFindNextButton_Click ( this , new EventArgs ( ) ) ;
2019-09-28 19:54:34 +00:00
e . Handled = e . SuppressKeyPress = true ;
}
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
2019-10-13 11:54:45 +00:00
private void SearchCloseButton_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-09-28 19:54:34 +00:00
FindMainMenu_Click ( this , new EventArgs ( ) ) ;
2019-08-21 11:15:16 +00:00
}
2018-12-25 15:29:29 +00:00
2019-10-13 11:54:45 +00:00
private void SearchCloseButton_MouseHover ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-10-13 11:54:45 +00:00
searchCloseButton . Image = Resources . close_b ;
2018-12-25 15:29:29 +00:00
}
2019-10-13 11:54:45 +00:00
private void SearchCloseButton_MouseLeave ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-10-13 11:54:45 +00:00
searchCloseButton . Image = Resources . close_g ;
2018-12-25 15:29:29 +00:00
}
2019-10-13 11:54:45 +00:00
private void SearchCaseSensitiveCheckBox_CheckedChanged ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-08-22 17:05:18 +00:00
findPos = 0 ;
2019-09-28 19:54:34 +00:00
richTextBox . DeselectAll ( ) ;
2018-12-25 15:29:29 +00:00
}
2019-10-13 11:54:45 +00:00
private void SearchWholeWordCheckBox_CheckedChanged ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-08-22 17:05:18 +00:00
findPos = 0 ;
2019-09-28 19:54:34 +00:00
richTextBox . DeselectAll ( ) ;
2019-08-22 17:05:18 +00:00
}
2019-08-27 07:04:44 +00:00
private void FindText ( string text , RichTextBoxFinds findOptions )
2019-08-22 17:05:18 +00:00
{
if ( text . Length > 0 )
{
try
{
2019-09-28 19:54:34 +00:00
findPos = richTextBox . Find ( searchTextBox . Text , findPos , findOptions ) ;
2019-08-22 17:05:18 +00:00
if ( findPos = = - 1 )
{
findPos = 0 ;
2019-09-28 19:54:34 +00:00
searchTextBox . Focus ( ) ;
2019-08-22 17:05:18 +00:00
return ;
}
2019-09-28 19:54:34 +00:00
richTextBox . Focus ( ) ;
richTextBox . Select ( findPos , searchTextBox . Text . Length ) ;
findPos + = searchTextBox . Text . Length + 1 ;
2019-08-22 17:05:18 +00:00
}
catch
{
findPos = 0 ;
}
}
}
2019-09-28 19:54:34 +00:00
2019-10-13 11:54:45 +00:00
private void SearchFindNextButton_Click ( object sender , EventArgs e )
2019-08-22 17:05:18 +00:00
{
2019-10-13 11:54:45 +00:00
if ( ( ! searchWholeWordCheckBox . Checked ) & ( ! searchCaseSensitiveCheckBox . Checked ) )
2019-08-22 17:05:18 +00:00
{
2019-09-28 19:54:34 +00:00
FindText ( searchTextBox . Text , RichTextBoxFinds . None ) ;
2019-08-22 17:05:18 +00:00
return ;
}
2019-10-13 11:54:45 +00:00
if ( searchWholeWordCheckBox . Checked & searchCaseSensitiveCheckBox . Checked )
2019-08-22 17:05:18 +00:00
{
2019-09-28 19:54:34 +00:00
FindText ( searchTextBox . Text , RichTextBoxFinds . MatchCase | RichTextBoxFinds . WholeWord ) ;
2019-08-22 17:05:18 +00:00
return ;
}
2019-10-13 11:54:45 +00:00
if ( searchCaseSensitiveCheckBox . Checked )
2019-08-22 17:05:18 +00:00
{
2019-09-28 19:54:34 +00:00
FindText ( searchTextBox . Text , RichTextBoxFinds . MatchCase ) ;
2019-08-22 17:05:18 +00:00
return ;
}
2019-10-13 11:54:45 +00:00
if ( searchWholeWordCheckBox . Checked )
2019-08-22 17:05:18 +00:00
{
2019-09-28 19:54:34 +00:00
FindText ( searchTextBox . Text , RichTextBoxFinds . WholeWord ) ;
2019-08-22 17:05:18 +00:00
return ;
}
2018-12-25 15:29:29 +00:00
}
2019-09-28 19:54:34 +00:00
#endregion
2018-12-25 15:29:29 +00:00
2019-10-11 10:38:15 +00:00
#region AutoLock
2019-10-13 11:54:45 +00:00
private void FileLockedKeyTextBox_TextChanged ( object sender , EventArgs e )
2019-10-11 10:38:15 +00:00
{
2019-10-13 11:54:45 +00:00
if ( fileLockedKeyTextBox . Text . Length > 0 )
2019-10-13 12:08:51 +00:00
{
2019-10-13 11:54:45 +00:00
fileLockedOkButton . Enabled = true ;
2019-10-13 12:08:51 +00:00
encryptionKeyPlaceholder . Visible = false ;
}
2019-10-11 10:38:15 +00:00
else
2019-10-13 12:08:51 +00:00
{
encryptionKeyPlaceholder . Visible = true ;
2019-10-13 11:54:45 +00:00
fileLockedOkButton . Enabled = false ;
2019-10-13 12:08:51 +00:00
}
2019-10-11 10:38:15 +00:00
}
2019-10-13 11:54:45 +00:00
private void FileLockedPanel_VisibleChanged ( object sender , EventArgs e )
2019-10-11 10:38:15 +00:00
{
2019-10-13 11:54:45 +00:00
if ( fileLockedPanel . Visible )
2019-10-11 10:38:15 +00:00
{
2019-10-13 12:25:32 +00:00
foreach ( ToolStripItem item in mainMenu . Items )
{
if ( item is ToolStripDropDownItem )
foreach ( ToolStripItem dropDownItem in ( ( ToolStripDropDownItem ) item ) . DropDownItems )
{
dropDownItem . Enabled = false ;
}
}
2019-10-11 10:38:15 +00:00
PublicVar . encryptionKey . Set ( null ) ;
caretPos = richTextBox . SelectionStart ;
richTextBox . Visible = false ;
toolbarPanel . Enabled = false ;
mainMenu . Enabled = false ;
richTextBox . Clear ( ) ;
2019-10-13 11:54:45 +00:00
fileLockedKeyTextBox . Focus ( ) ;
2019-10-11 10:38:15 +00:00
}
else
{
2019-10-13 12:25:32 +00:00
foreach ( ToolStripItem item in mainMenu . Items )
{
if ( item is ToolStripDropDownItem )
foreach ( ToolStripItem dropDownItem in ( ( ToolStripDropDownItem ) item ) . DropDownItems )
{
dropDownItem . Enabled = true ;
}
}
2019-10-11 10:38:15 +00:00
richTextBox . Visible = true ;
toolbarPanel . Enabled = true ;
searchPanel . Enabled = true ;
mainMenu . Enabled = true ;
2019-10-15 16:48:17 +00:00
fileLockedKeyTextBox . Text = "" ;
2019-10-11 10:38:15 +00:00
}
}
2019-10-13 11:54:45 +00:00
private void FileLockedOkButton_Click ( object sender , EventArgs e )
2019-10-11 10:38:15 +00:00
{
LockFile ( ) ;
}
2019-10-13 11:54:45 +00:00
private void FileLockedCloseButton_MouseClick ( object sender , MouseEventArgs e )
2019-10-11 10:38:15 +00:00
{
2019-10-13 11:54:45 +00:00
fileLockedPanel . Visible = false ;
2019-10-11 10:38:15 +00:00
Text = PublicVar . appName ;
2019-10-15 16:48:17 +00:00
filePath = "" ;
PublicVar . openFileName = "" ;
2019-10-11 10:38:15 +00:00
}
2019-10-13 11:54:45 +00:00
private void FileLockedShowKey_Click ( object sender , EventArgs e )
2019-10-11 10:38:15 +00:00
{
2019-10-13 11:54:45 +00:00
if ( fileLockedKeyTextBox . UseSystemPasswordChar )
2019-10-11 10:38:15 +00:00
{
2019-10-13 11:54:45 +00:00
fileLockedKeyTextBox . UseSystemPasswordChar = false ;
fileLockedShowKey . Image = Resources . eye ;
2019-10-11 10:38:15 +00:00
}
else
{
2019-10-13 11:54:45 +00:00
fileLockedKeyTextBox . UseSystemPasswordChar = true ;
fileLockedShowKey . Image = Resources . eye_half ;
2019-10-15 16:09:51 +00:00
}
2019-10-11 10:38:15 +00:00
}
2019-10-13 11:54:45 +00:00
private void FileLockedKeyTextBox_KeyDown ( object sender , KeyEventArgs e )
2019-10-11 10:38:15 +00:00
{
2019-10-15 16:13:08 +00:00
if ( e . KeyCode = = Keys . Enter )
{
e . SuppressKeyPress = true ;
}
2019-10-13 11:54:45 +00:00
if ( e . KeyCode = = Keys . Enter & fileLockedOkButton . Enabled )
2019-10-11 10:38:15 +00:00
{
2019-10-13 11:54:45 +00:00
FileLockedOkButton_Click ( sender , e ) ;
2019-10-11 10:38:15 +00:00
}
}
2019-10-13 11:54:45 +00:00
private void FileLockedCloseButton_MouseEnter ( object sender , EventArgs e )
2019-10-11 10:38:15 +00:00
{
2019-10-13 11:54:45 +00:00
fileLockedCloseButton . ForeColor = Color . Silver ;
2019-10-11 10:38:15 +00:00
}
2019-10-13 11:54:45 +00:00
private void FileLockedCloseButton_MouseLeave ( object sender , EventArgs e )
2019-10-11 10:38:15 +00:00
{
2019-10-13 11:54:45 +00:00
fileLockedCloseButton . ForeColor = Color . DimGray ;
2019-10-11 10:38:15 +00:00
}
#endregion
#region Tray
private void TrayIcon_MouseDoubleClick ( object sender , MouseEventArgs e )
{
2019-10-13 11:54:45 +00:00
if ( e . Button = = MouseButtons . Left ) {
Show ( ) ;
WindowState = FormWindowState . Normal ;
}
2019-10-11 10:38:15 +00:00
}
2019-10-13 12:55:13 +00:00
private void ShowTrayMenu_Click ( object sender , EventArgs e )
2019-10-11 10:38:15 +00:00
{
Show ( ) ;
WindowState = FormWindowState . Normal ;
}
2019-10-13 12:55:13 +00:00
private void ExitTrayMenu_Click ( object sender , EventArgs e )
2019-10-11 10:38:15 +00:00
{
2019-10-13 11:54:45 +00:00
Application . Exit ( ) ;
2019-10-11 10:38:15 +00:00
}
#endregion
2019-09-28 19:45:20 +00:00
#region Debug Menu
private void VariablesMainMenu_Click ( object sender , EventArgs e )
{
2019-10-04 18:48:55 +00:00
#if DEBUG
string formattedTime = DateTime . Now . ToString ( "yyyy.MM.dd hh:mm:ss" ) ;
Debug . WriteLine ( "\nTime: " + formattedTime ) ;
Debug . WriteLine ( "PublicVar.openFileName: " + PublicVar . openFileName ) ;
Debug . WriteLine ( "filePath: " + filePath ) ;
Debug . WriteLine ( "encryptionKey: " + PublicVar . encryptionKey . Get ( ) ) ;
Debug . WriteLine ( "TypedPassword: " + TypedPassword . Value ) ;
Debug . WriteLine ( "keyChanged: " + PublicVar . keyChanged ) ;
Debug . WriteLine ( "okPressed: " + PublicVar . okPressed ) ;
Debug . WriteLine ( "RichTextBox.Modified: " + richTextBox . Modified ) ;
Debug . WriteLine ( "EditorMenuStrip: " + contextMenu . Enabled ) ;
#endif
2019-08-23 18:41:21 +00:00
}
2019-10-13 11:54:45 +00:00
2019-09-28 19:45:20 +00:00
#endregion
2019-10-15 16:09:51 +00:00
2018-12-17 11:16:17 +00:00
}
2019-08-21 11:15:16 +00:00
}