forked from sunminghong/MEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmMain_code.cs
579 lines (498 loc) · 16 KB
/
frmMain_code.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using MarkdownSharp;
using MEditor.Properties;
using MRU;
namespace MEditor
{
public partial class frmMain : Form, IMRUClient
{
private static string _myExtName = ".md";
private string myExtName = "Markdown文件|*" + _myExtName + "|所有文件|*.*";
private MarkdownEditorManager meditorManager=null;
FileMonitor _filemonitor = null;
private Color _bgColor = Color.FromArgb(0x4a, 0x52, 0x5a);//20,0x20,0x20;
private Color _foreColor = Color.FromArgb(0xff, 0xff, 0xff); //0xf2,0xf0,0xdf
private Font _font = new Font("微软雅黑",12);
private string _extfileother = "";
private Regex _regexExtMarkdowns = new Regex(@"\.md|\.markdown", RegexOptions.Compiled);
private Regex _regexExtOthertext = new Regex(@"\.txt|\.js|\.htm|\.xml|\.as|\.log|\.php|\.cs", RegexOptions.Compiled);
private Regex _regexExtHtml = new Regex(@"\.htm|\.xml", RegexOptions.Compiled);
#region defaultCss
private string _defcss = @"
body,td,th {font-family:""微软雅黑"", Verdana, ""Bitstream Vera Sans"", sans-serif; }
body {
margin-top: 0;
padding: 0;
line-height: 1.5em;
color: #{1};
background-color: #{0};
text-align: left;
}
a em {
padding-bottom: 2px;
}
sup {
line-height: 0px;
}
sup a:link {
padding: 3px;
}
sup a:visited {
padding: 3px;
}
p {
margin: 0 0 1.6em 0;
padding: 0;
}
h1 {
font-family: ""黑体"",""Gill Sans"", ""Gill Sans Std"", ""Gill Sans MT"", Georgia, serif;
font-size: 1.55em;
line-height: 1.5em;
text-align: left;
font-weight: bold;
margin: 0em 0px 1.25em 0px;
}
h2 {
font-family:""黑体"", ""Gill Sans"", ""Gill Sans Std"", ""Gill Sans MT"", Verdana, ""Bitstream Vera Sans"", sans-serif;
font-size: 1.1em; /* 1 */
text-align: left;
font-weight:bold;
line-height: 1.8em;
letter-spacing: .2em;
margin: 1em 0 1em 0;
text-transform: uppercase;
}
h1 + h2 {
margin-top: 2em;
}
h2 + h3 {
margin-top: 1.5em;
}
h3 {
font-family: ""黑体"",Gill Sans"", ""Gill Sans Std"", ""Gill Sans MT"", Verdana, ""Bitstream Vera Sans"", sans-serif;
font-size: .91em;
text-align: left;
font-weight: normal;
line-height: 1.8em;
letter-spacing: .2em;
margin-bottom: 0.4em;
margin-top: 0.5em;
text-transform: uppercase;
}
p + h3 {
margin-top: 4em;
}
pre + h3 {
margin-top: 4em;
}
h6 + h2 {
margin-top: 2em;
}
h4, h5, h6 {
font-family: Verdana, ""Bitstream Vera Sans"", sans-serif;
font-size: 1em;
text-align: left;
font-weight: bold;
line-height: 1.5em;
margin: 1em 0 0 0;
}
strong {
font-weight: bold;
font-size: .91em;
letter-spacing: .2em;
text-transform: uppercase;
}
em {
font-style: normal;
}
blockquote {
font-size: 1em;
margin: 2em 2em 2em 1em;
padding: 0 .75em 0 1.25em;
border-left: 1px solid #777;
border-right: 0px solid #777;
}
blockquote strong {
font-weight: bold;
font-size: 1em;
letter-spacing: normal;
text-transform: none;
}
img {
margin-top: 5px;
}
thead {
font-weight: bold;
}
ul, ol {
padding-left: 1.25em;
margin: 0 0 2em 1em;
}
pre {
font-family: ""Bitstream Vera Sans Mono"", Courier, Monaco, ProFont, ""American Typewriter"", ""Andale Mono"", monospace;
line-height: 1.45em;
color: #{1};
background-color: inherit;
margin: 0.5em 0 0.5em 0;
padding: 5px 0 5px 10px;
border-width: 1px 0 1px 0;
border-color: #6b6b6b;
border-style: dashed;
}
code {
font-family: Monaco, ProFont, ""Bitstream Vera Sans Mono"", ""American Typewriter"", ""Andale Mono"", monospace;
font-size: 0.91em; /* 1.09em for Courier */
}
ul {
list-style-type: square;
}
ul ul {
list-style-type: disc;
}
ul ul ul {
list-style-type: circle;
}
hr {
height: 1px;
margin: 1em 1em 1em 0;
text-align: center;
border-color: #777;
border-width: 0;
border-style: dotted;
}
dt {
font-family: Verdana, ""Bitstream Vera Sans"", sans-serif;
font-size: 1em;
text-align: left;
font-weight: normal;
margin: 0 0 .4em 0;
letter-spacing: normal;
text-transform: none;
}
dd {
margin: auto auto 1.5em 1em;
}
dd p {
margin: 0 0 1em 0;
}
table{
border:1px #CCC solid;
border-collapse: separate;border-spacing: 0;
}
th,td{padding:5px;border: 1px solid #CCC;}
";
#endregion
public void ReadCss()
{
_font = Settings.Default.font;
_foreColor = Settings.Default.color;
_bgColor = Settings.Default.bgcolor;
_defcss = Settings.Default.css;
_extfileother = Settings.Default.extfile;
_regexExtOthertext = new Regex(_regexExtOthertext.Replace(".", @"\."), RegexOptions.Compiled);
meditorManager.SetFont(_font);
meditorManager.SetForeColor(_foreColor);
meditorManager.SetBackColor(_bgColor );
meditorManager.SetCss(_defcss);
//MessageBox.Show(Settings.Default.appconfig.ToString());
}
public void OpenMRUFile(string fileName)
{
openfile(fileName);
}
public void openfiles(string[] fileNames)
{
foreach (string s in fileNames)
openfile(s);
}
private void openfile(string fileName)
{
if (meditorManager.Open(fileName))
{
Add(fileName);
}
else
mruManager.Remove(fileName); // when Open File operation failed
}
private SortedList<string, bool> _monitorList = new SortedList<string, bool>();
public void Add(string fullpath)
{
string dir = System.IO.Path.GetDirectoryName(fullpath);
if (_monitorList.ContainsKey(dir))
{
return;
}
_monitorList.Add(dir, true);
_filemonitor.Add(dir, "*.md");
}
delegate bool dirupdated(string filename);
void fsw_Changed(object sender, System.IO.FileSystemEventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new dirupdated(meditorManager.RefrushOpen), e.Name);
return;
}
meditorManager.RefrushOpen(e.Name);
}
private void openfile()
{
OpenFileDialog fileone = new OpenFileDialog();
fileone.Filter = myExtName ;
fileone.FilterIndex = 1;
if (fileone.ShowDialog() == DialogResult.OK)
{
try
{
openfile(fileone.FileName);
}
catch (Exception ex)
{
MessageBox.Show("出现了错误:" + ex.Message);
mruManager.Remove(fileone.FileName); // when Open File operation failed
}
}
}
private void PreviewHtml()
{
this.toolStripStatusLabel1.Text = " 正在转换。。。。 ";
MarkdownEditor meditor = meditorManager.SetStyle();
if (meditor == null)
return;
bool bhtml = filetypeConvert(meditor);
tabBrowser.Text = meditor.MarkdownPage.Text;
this.toolStripStatusLabel1.Text = "当前文档:" + meditor.FileName;
if (isLeft)
{
if (bhtml && splitContainer1.Panel2Collapsed)
splitContainer1.Panel2Collapsed = false;
}
else
{
if (bhtml && splitContainer1.Panel1Collapsed)
splitContainer1.Panel1Collapsed = false;
}
//tabControl1.Focus();
meditor.GetTextBox().Focus();
}
private bool filetypeConvert(MarkdownEditor meditor)
{
string marktext = meditor.GetMarkdown();
if (string.IsNullOrEmpty(marktext))
{
return false ;
}
string ext = Path.GetExtension(meditor.FileName);
if (_regexExtMarkdowns.IsMatch(ext))
{
string html="";
Markdown mark = new Markdown();
html = mark.Transform(marktext);
rtbHtml.Text = html;
webBrowser1.DocumentText = meditorManager.GetHTMLStyle(html);
return true;
}
if (_regexExtHtml.IsMatch(ext))
{
webBrowser1.DocumentText = marktext;
rtbHtml.Text = marktext;
return true;
}
if (_regexExtOthertext.IsMatch(ext))
{
webBrowser1.DocumentText = meditorManager.GetHTMLStyle("<pre><code>"+marktext+"</code></pre>");
rtbHtml.Text = marktext;
return true;
}
return false;
}
public void SelectForeColor()
{
ColorDialog color = new ColorDialog();
if (color.ShowDialog() == DialogResult.OK)
{
_foreColor = color.Color;
meditorManager.SetForeColor(_foreColor);
meditorManager.SetStyle(rtbHtml);
//Settings.Default.color = _foreColor;
//Settings.Default.Save();
}
}
public void SetOldStyle()
{
_bgColor = Color.FromArgb(0xff, 0xff, 0xff);//20,0x20,0x20;
_foreColor = Color.FromArgb(0x00, 0x00, 0x00); //0xf2,0xf0,0xdf
_font = new Font("微软雅黑",12);
setstyle();
//SaveSettings();
}
public void SetBlackWhiteStyle()
{
_bgColor = Color.FromArgb(0x4a, 0x52, 0x5a);//20,0x20,0x20;
_foreColor = Color.FromArgb(0xff, 0xff, 0xff); //0xf2,0xf0,0xdf
_font = new Font("微软雅黑", 12);
setstyle();
//SaveSettings();
}
private void setstyle()
{
meditorManager.SetFont(_font);
meditorManager.SetForeColor(_foreColor);
meditorManager.SetBackColor(_bgColor);
meditorManager.SetStyle(rtbHtml);
}
private string convertColor(Color co)
{
return co.R.ToString() +","+ co.G.ToString() +","+ co.B.ToString();
}
public void SaveSettings()
{
string appconfig = Settings.Default.appconfig;
appconfig=appconfig.Replace("{font}", _font.ToString());
appconfig = appconfig.Replace("{color}", convertColor(_foreColor));
appconfig = appconfig.Replace("{bgcolor}", convertColor(_bgColor));
appconfig = appconfig.Replace("{css}", _defcss);
appconfig = appconfig.Replace("{extfile}", _extfileother);
string conffile = Application.UserAppDataPath;
conffile = Application.ExecutablePath+".config";
using (StreamWriter sw = new StreamWriter(conffile, false, Encoding.UTF8))
{
sw.Write(appconfig);
sw.Close();
}
}
public void SelectBackColor()
{
ColorDialog color = new ColorDialog();
if (color.ShowDialog() == DialogResult.OK)
{
_bgColor = color.Color;
meditorManager.SetBackColor(_bgColor);
meditorManager.SetStyle(rtbHtml);
//Settings.Default.bgcolor = _bgColor;
//Settings.Default.Save();
}
}
public void SelectFont()
{
FontDialog font = new FontDialog();
if (font.ShowDialog() == DialogResult.OK)
{
_font = font.Font;
meditorManager.SetFont(_font);
meditorManager.SetStyle(rtbHtml);
//Settings.Default.font =_font;
//Settings.Default.Save();
}
}
public void SetCss(string css)
{
_defcss = css;
meditorManager.SetCss(_defcss);
PreviewHtml();
//Settings.Default.css = _defcss;
//Settings.Default.Save();
}
public void SetExt(string ext)
{
_extfileother = ext;
_regexExtOthertext = new Regex(_extfileother.Replace(".", @"\."), RegexOptions.Compiled);
}
private void editCss()
{
frmCss fcss = new frmCss(_defcss,this);
fcss.ShowDialog();
}
private void showSyntax(string url)
{
toolStripStatusLabel1.Text ="正在打开" +url+"..." ;
System.Diagnostics.Process.Start(url);
//webBrowser1.Navigate(url);
//webBrowser1.DocumentText = html;
if (splitContainer1.Panel2Collapsed)
splitContainer1.Panel2Collapsed = false;
}
private bool isLeft = true;
private void SwithFlaout()
{
if (isLeft)
{
this.splitContainer1.Panel2.Controls.Add(this.tabControl1);
this.splitContainer1.Panel1.Controls.Add(this.tabControl2);
if (splitContainer1.Panel2Collapsed)
{
splitContainer1.Panel2Collapsed = false;
splitContainer1.Panel1Collapsed = true;
}
isLeft = false;
return;
}
this.splitContainer1.Panel1.Controls.Add(this.tabControl1);
this.splitContainer1.Panel2.Controls.Add(this.tabControl2);
if (splitContainer1.Panel1Collapsed)
{
splitContainer1.Panel1Collapsed = false;
splitContainer1.Panel2Collapsed = true;
}
isLeft = true;
}
private void UnRegExtfile()
{
bool rel= FileTypeRegister.UnRegister(_myExtName);
if (!rel && IsWindowsVistaOrNewer)
{
MessageBox.Show("您是在windows7 下运行的此程序,如果执行完扩展名关联但没有成功,请您以“管理员权限方式再打开此程序再关联一次”!");
}
}
/// <summary>
/// Gets a value indicating if the operating system is a Windows Vista or a newer one.
/// </summary>
public static bool IsWindowsVistaOrNewer
{
get { return (Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version.Major >= 6); }
}
private void regExtFile()
{
Assembly asm = Assembly.GetExecutingAssembly();
string resourceName = "MEditor.logo.ico";
Stream stream = asm.GetManifestResourceStream(resourceName);
string icofile = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\markdown.ico";
FileStream fs = new FileStream(icofile, FileMode.Create, FileAccess.Write);
byte[] bs = new byte[stream.Length];
stream.Read(bs, 0, (int)stream.Length);
fs.Write(bs, 0, bs.Length);
fs.Flush();
fs.Close();
fs.Dispose();
//注册关联的文件扩展名
FileTypeRegInfo fr = new FileTypeRegInfo();
fr.Description = "markdown文档格式";
fr.ExePath = Application.ExecutablePath;
fr.ExtendName = _myExtName;
fr.IcoPath = icofile;
bool rel = true;
if (FileTypeRegister.CheckIfRegistered(fr.ExtendName))
{
rel=FileTypeRegister.UpdateRegInfo(fr);
}
else{
rel=FileTypeRegister.Register(fr);
}
if (!rel && IsWindowsVistaOrNewer)
{
MessageBox.Show("您是在windows7 下运行的此程序,如果执行完扩展名关联但没有成功,请您以“管理员权限方式再打开此程序再关联一次”!");
}
}
}
}