-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
220 lines (198 loc) · 6.58 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Recurse, an ultra-lightweight and simplistic text editor
* Copyright (C) 2022 REMCodes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
* To contact REMCodes, please email "ContactREMCodes@gmail.com"
*/
namespace Recurse
{
public partial class Recurse : Form
{
private string FileContent
{
get
{
return _fileContent;
}
set
{
_fileContent = value;
TextArea.Text = _fileContent;
}
}
private string _fileContent = "";
private string prefix = "";
private string path = Properties.Settings.Default.LastFile;
public Recurse()
{
InitializeComponent();
OpenFileDialog.RestoreDirectory = true;
SaveFileDialog.RestoreDirectory = true;
KeyPreview = true;
}
private void TextArea_TextChanged(object sender, EventArgs e)
{
FileContent = TextArea.Text;
UpdateFileName();
}
private void Open()
{
OpenFileDialog.ShowDialog(this);
}
private void Save()
{
if (path != "Untitled")
{
File.WriteAllText(path, FileContent);
UpdateFileName();
}
else
{
SaveFileDialog.ShowDialog(this);
}
}
private void UpdateFileName()
{
try
{
if (path == "Untitled")
{
if (FileContent == String.Empty)
{
prefix = "";
}
else
{
prefix = "*";
}
}
else
{
if (FileContent == File.ReadAllText(path))
{
prefix = "";
}
else
{
prefix = "*";
}
}
}
catch
{
prefix = "*";
}
Text = "Recurse | " + path + prefix;
string[] name = path.Split(new char[] { '\\', '/' });
FileName.Text = prefix + name[name.Length-1];
}
private void Recurse_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control && e.KeyCode == Keys.O)
{
Open();
}
else if (e.Control && e.Shift && e.KeyCode == Keys.S)
{
SaveFileDialog.ShowDialog(this);
}
else if(e.Control && e.KeyCode == Keys.S)
{
Save();
} else if(e.Control && e.KeyCode == Keys.Q)
{
if (MessageBox.Show("Are you sure you want to exit?", "Confirm Exit", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Environment.Exit(0);
}
} else if (e.Control && e.KeyCode == Keys.H)
{
TextArea.ReadOnly = true;
About about = new About();
about.ShowDialog();
TextArea.ReadOnly = false;
} else if (e.Control && e.Shift && e.KeyCode == Keys.X)
{
CloseFile();
}
}
private void OpenFileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
var filePath = OpenFileDialog.FileName;
var fileStream = OpenFileDialog.OpenFile();
path = filePath;
using (StreamReader reader = new StreamReader(fileStream))
{
FileContent = reader.ReadToEnd();
UpdateFileName();
}
}
private void SaveFileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
path = SaveFileDialog.FileName;
File.WriteAllText(SaveFileDialog.FileName, FileContent);
UpdateFileName();
}
private void Recurse_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.LastFile = path;
Properties.Settings.Default.Save();
}
private void Recurse_Load(object sender, EventArgs e)
{
if (path != "Untitled")
{
StreamReader reader = new StreamReader(path);
FileContent = reader.ReadToEnd();
UpdateFileName();
}
}
private void CloseFile()
{
if ((path == "Untitled" && FileContent != "") | (path != "Untitled" && FileContent != File.ReadAllText(path)))
{
DialogResult result = MessageBox.Show("Do you want to save before closing?", "Save before closing?", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Cancel)
{
return;
}
else if (result == DialogResult.Yes)
{
Save();
}
}
path = "Untitled";
UpdateFileName();
FileContent = "";
}
private void Recurse_FormClosing(object sender, FormClosingEventArgs e)
{
if ((path == "Untitled" && FileContent != "") | (path != "Untitled" && FileContent != File.ReadAllText(path)))
{
DialogResult result = MessageBox.Show("Do you want to save before closing?", "Save before closing?", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Cancel)
{
e.Cancel = true;
}
else if (result == DialogResult.Yes)
{
Save();
}
}
}
}
}