-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDocumentForm.cs
More file actions
654 lines (555 loc) · 21.9 KB
/
DocumentForm.cs
File metadata and controls
654 lines (555 loc) · 21.9 KB
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using ScintillaNET;
using System.IO;
using System.Data.OleDb;
using System.Configuration;
using System.Xml;
using System.Drawing.Text;
namespace WeCode1._0
{
public partial class DocumentForm : DockContent
{
#region Fields
private string _nodeId;
private string _type;
private bool _iniLexer;
private string _treeLocation;
private string _lastUpdateTime;
private int _isLock=0;
private youdao.YouDaoNode2 _data = null;
#endregion Fields
#region Properties
public string NodeId
{
get { return _nodeId; }
set { _nodeId = value; }
}
public string Type
{
get { return _type; }
set { _type = value; }
}
public bool IniLexer
{
get { return _iniLexer; }
set { _iniLexer = value; }
}
//树路径,状态栏显示
public string TreeLocation
{
get { return _treeLocation; }
set { _treeLocation = value; }
}
//最后更新时间
public string LastUpdateTime
{
get { return _lastUpdateTime; }
set { _lastUpdateTime = value; }
}
//是否加密
public int IsLock
{
get { return _isLock; }
set { _isLock = value; }
}
public Scintilla Scintilla
{
get
{
return scintilla1;
}
}
//public youdao.YouDaoNode2 Data
//{
// get { return _data; }
// set { _data = value; }
//}
#endregion Properties
public DocumentForm()
{
InitializeComponent();
}
//刷新状态栏
public void reFreshStatus()
{
if (Type == "local")
{
Attachment.frmMain.showFullPathTime("【本地笔记本】" + TreeLocation, LastUpdateTime);
}
else
{
Attachment.frmMain.showFullPathTime("【有道云】" + TreeLocation, LastUpdateTime);
}
}
private void DocumentForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (Scintilla.Modified&&Attachment.isDeleteClose=="0")
{
string Title = this.Text;
DialogResult dr = MessageBox.Show(this, "文档已修改,是否保存?", Title, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Cancel)
{
// Stop closing
e.Cancel = true;
return;
}
else if (dr == DialogResult.Yes)
{
this.Save();
e.Cancel = false;
return;
}
}
}
//保存当前文章
public void Save()
{
if (this.Type == "local")
{
string DocText = this.scintilla1.Text;
if (DocText.Length == 0)
return;
//如果是加密文章,先把文本加密再保存
if (IsLock == 1)
{
DocText = EncryptDecrptt.EncrptyByKey(DocText, Attachment.KeyD);
}
OleDbParameter p1 = new OleDbParameter("@Content", OleDbType.VarChar);
p1.Value = DocText;
OleDbParameter p3 = new OleDbParameter("@NodeId", OleDbType.Integer);
p3.Value = Convert.ToInt32(this.NodeId);
OleDbParameter p2 = new OleDbParameter("@UpdateTime", OleDbType.Integer);
p2.Value = PubFunc.time2TotalSeconds();
OleDbParameter[] ArrPara = new OleDbParameter[3];
ArrPara[0] = p1;
ArrPara[1] = p2;
ArrPara[2] = p3;
string SQL = "update tcontent set content=@Content,updatetime=@UpdateTime where NodeId=@NodeId";
AccessAdo.ExecuteNonQuery(SQL, ArrPara);
//更新修改时间
//int Seconds = PubFunc.time2TotalSeconds();
//SQL="update ttree set updatetime="+Seconds+" where nodeid="+NodeId;
//AccessAdo.ExecuteNonQuery(SQL);
LastUpdateTime = "最后更新时间:"+DateTime.Now.ToString();
Attachment.frmMain.showFullPathTime("【本地笔记本】"+TreeLocation, LastUpdateTime);
scintilla1.Modified = false;
}
else if (this.Type == "online")
{
string DocText = this.scintilla1.Text;
if (DocText.Length == 0)
return;
//如果是加密文章,先把文本加密再保存
if (IsLock == 1)
{
DocText = EncryptDecrptt.EncrptyByKey(DocText, Attachment.KeyDYouDao);
}
int updatetime=PubFunc.time2TotalSeconds();
//存入缓存数据库
OleDbConnection ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
OleDbParameter p1 = new OleDbParameter("@Content", OleDbType.VarChar);
p1.Value = DocText;
OleDbParameter p3 = new OleDbParameter("@path", OleDbType.VarChar);
p3.Value = this.NodeId;
OleDbParameter p2 = new OleDbParameter("@UpdateTime", OleDbType.Integer);
p2.Value = updatetime;
OleDbParameter[] ArrPara = new OleDbParameter[3];
ArrPara[0] = p1;
ArrPara[1] = p2;
ArrPara[2] = p3;
string SQL = "update tcontent set content=@Content,updatetime=@UpdateTime,needSync=1 where path=@path";
AccessAdo.ExecuteNonQuery(ExportConn,SQL, ArrPara);
LastUpdateTime = "最后更新时间:" + PubFunc.seconds2Time(updatetime).ToString();
Attachment.frmMain.showFullPathTime("【有道云】" + TreeLocation, LastUpdateTime);
scintilla1.Modified = false;
//通知主窗口唤醒扫描线程
Attachment.frmMain.InterRuptSleep();
}
}
private void scintilla1_ModifiedChanged(object sender, EventArgs e)
{
AddOrRemoveAsteric();
}
private void AddOrRemoveAsteric()
{
if (scintilla1.Modified)
{
if (!Text.EndsWith(" *"))
Text += " *";
}
else
{
if (Text.EndsWith(" *"))
Text = Text.Substring(0, Text.Length - 2);
}
}
//编辑窗体激活
private void DocumentForm_Activated(object sender, EventArgs e)
{
//设置附件
Attachment.ActiveNodeId = this.NodeId;
Attachment.ActiveDOCType = this.Type;
Attachment.AttForm.GridEnable();
Attachment.AttForm.ReFreshAttachGrid();
try
{
int x = Scintilla.Caret.LineNumber + 1;
int overAllPosition = Scintilla.Caret.Position;
int lineStartPosition = Scintilla.Lines.Current.StartPosition;
int y = overAllPosition - lineStartPosition;
Attachment.frmMain.showLinePosition(x, y);
//设置路径以及最后修改时间
if (Type == "local")
{
Attachment.frmMain.showFullPathTime("【本地笔记本】" + TreeLocation, LastUpdateTime);
}
else {
Attachment.frmMain.showFullPathTime("【有道云】"+TreeLocation, LastUpdateTime);
}
}
catch (Exception ex)
{
}
}
private void DocumentForm_Load(object sender, EventArgs e)
{
}
//关闭完了需要显示欢迎界面
private void chek2ShowWelcome()
{
if (Attachment.DocCount == 1)
{
Attachment.frmMain.openWelcomePage();
//清空状态栏信息
Attachment.frmMain.clearStatus();
}
}
//关闭当前页签
private void toolStripMenuItemTabClose_Click(object sender, EventArgs e)
{
this.Close();
}
//除此之外全部关闭
private void toolStripMenuItemTabCloseE_Click(object sender, EventArgs e)
{
IDockContent[] documents = DockPanel.DocumentsToArray();
foreach (IDockContent content in documents)
{
if (!content.Equals(this))
{
content.DockHandler.Close();
}
}
}
//全部关闭
private void toolStripMenuItemTabCloseAll_Click(object sender, EventArgs e)
{
IDockContent[] documents = DockPanel.DocumentsToArray();
foreach (IDockContent content in documents)
{
content.DockHandler.Close();
}
}
//保存
private void toolStripMenuItemTabSv_Click(object sender, EventArgs e)
{
this.Save();
}
//全部保存
private void toolStripMenuItemTabSvAll_Click(object sender, EventArgs e)
{
IDockContent[] documents = DockPanel.DocumentsToArray();
foreach (WeCode1._0.DocumentForm Doc in documents)
{
Doc.Save();
}
}
//只读
private void toolStripMenuItemTabRdOnly_Click(object sender, EventArgs e)
{
toolStripMenuItemTabRdOnly.Checked = !toolStripMenuItemTabRdOnly.Checked;
this.Scintilla.IsReadOnly = toolStripMenuItemTabRdOnly.Checked;
}
//加为书签
private void toolStripMenuItemTabAddMark_Click(object sender, EventArgs e)
{
if (this.Type == "local")
{
string NodeId = this.NodeId;
string SQL = "update Ttree set marktime=" + PubFunc.time2TotalSeconds().ToString() + " where Nodeid=" + NodeId;
AccessAdo.ExecuteNonQuery(SQL);
}
else if (this.Type == "online")
{
string NodeId = this.NodeId;
XmlDocument doc = new XmlDocument();
doc.Load("TreeNodeLocal.xml");
XmlNode preNode = doc.SelectSingleNode("//note[@path='" + NodeId + "']");
preNode.Attributes["isMark"].Value = "1";
doc.Save("TreeNodeLocal.xml");
XMLAPI.XML2Yun();
}
}
private void scintilla1_CharAdded(object sender, CharAddedEventArgs e)
{
return;
//string tmp = scintilla1.GetWordFromPosition(scintilla1.CurrentPos);
//string pretmp = scintilla1.GetWordFromPosition(scintilla1.CurrentPos - 1);
//if (string.IsNullOrEmpty(tmp) || tmp.Length < 1 || !tmp.Equals(pretmp))
// return;
//List<string> userList = scintilla1.AutoComplete.List.FindAll(
// delegate(string txt)
// {
// if (txt.StartsWith(tmp, StringComparison.OrdinalIgnoreCase))
// return true;
// else
// return false;
// });
////Debug.Print("Word: " + tmp);
//if (userList.Count > 0)
//{
// scintilla1.AutoComplete.ShowUserList(-1, userList);
// //scintilla.AutoComplete.Show();
//}
////scintilla.AutoComplete.Show();
}
private void DocumentForm_FormClosed(object sender, FormClosedEventArgs e)
{
Attachment.DocCount = DockPanel.DocumentsCount;
chek2ShowWelcome();
}
//导出为HTML
public bool ExportAsHtml()
{
using (SaveFileDialog dialog = new SaveFileDialog())
{
string fileName = (Text.EndsWith(" *") ? Text.Substring(0, Text.Length - 2) : Text);
dialog.Filter = "HTML Files (*.html;*.htm)|*.html;*.htm|All Files (*.*)|*.*";
dialog.FileName = fileName + ".html";
if (dialog.ShowDialog() == DialogResult.OK)
{
scintilla1.Lexing.Colorize(); // Make sure the document is current
using (StreamWriter sw = new StreamWriter(dialog.FileName))
scintilla1.ExportHtml(sw, fileName, false);
return true;
}
}
return false;
}
//撤销
private void toolStripMenuItemUndo_Click(object sender, EventArgs e)
{
this.Scintilla.UndoRedo.Undo();
}
//反撤销
private void toolStripMenuItemRedo_Click(object sender, EventArgs e)
{
this.Scintilla.UndoRedo.Redo();
}
//剪切
private void toolStripMenuItemCut_Click(object sender, EventArgs e)
{
this.Scintilla.Clipboard.Cut();
}
//复制
private void toolStripMenuItemCopy_Click(object sender, EventArgs e)
{
this.Scintilla.Clipboard.Copy();
}
//粘贴
private void toolStripMenuItemPaste_Click(object sender, EventArgs e)
{
this.Scintilla.Clipboard.Paste();
}
//查找
private void toolStripMenuItemSerch_Click(object sender, EventArgs e)
{
this.Scintilla.FindReplace.ShowFind();
}
//替换
private void toolStripMenuItemReplace_Click(object sender, EventArgs e)
{
this.Scintilla.FindReplace.ShowReplace();
}
//全选
private void toolStripMenuItemSeleAll_Click(object sender, EventArgs e)
{
this.Scintilla.Selection.SelectAll();
}
//右键菜单
private void scintilla1_MouseDown(object sender, MouseEventArgs e)
{
}
//右键菜单
private void contextMenuStripMouseClick_Opening(object sender, CancelEventArgs e)
{
this.toolStripMenuItemUndo.Enabled = this.Scintilla.UndoRedo.CanUndo;
this.toolStripMenuItemRedo.Enabled = this.Scintilla.UndoRedo.CanRedo;
this.toolStripMenuItemCut.Enabled = this.Scintilla.Clipboard.CanCut;
this.toolStripMenuItemCopy.Enabled = this.Scintilla.Clipboard.CanCopy;
this.toolStripMenuItemPaste.Enabled = this.Scintilla.Clipboard.CanPaste;
}
//光标改变
private void scintilla1_CursorChanged(object sender, EventArgs e)
{
}
//光标位置改变
private void scintilla1_SelectionChanged(object sender, EventArgs e)
{
try
{
int x = Scintilla.Caret.LineNumber + 1;
int overAllPosition = Scintilla.Caret.Position;
int lineStartPosition = Scintilla.Lines.Current.StartPosition;
int y = overAllPosition - lineStartPosition;
Attachment.frmMain.showLinePosition(x, y);
}
catch (Exception ex)
{
}
}
//设置字体以及样式
private void setFontStyle()
{
//设置自动换行
if (PubFunc.GetConfiguration("AutoLineWrap") == "1")
{
Scintilla.LineWrapping.Mode = LineWrappingMode.Word;
}
else
{
Scintilla.LineWrapping.Mode = LineWrappingMode.None;
}
bool isVerExi = false;
bool isCouExi = false;
bool isWeiExi = false;
try
{
FontConverter fc = new FontConverter();
string sfont;
if (ConfigurationManager.AppSettings["defaultFont"] != "")
{
sfont = ConfigurationManager.AppSettings["defaultFont"];
SetFont(this, (Font)fc.ConvertFromString(sfont));
}
else
{
InstalledFontCollection Flists = new InstalledFontCollection();
foreach (FontFamily font in Flists.Families)
{
if (font.Name == "Verdana")
{
isVerExi = true;
}
else if (font.Name == "Courier New")
{
isCouExi = true;
}
else if (font.Name == "微软雅黑")
{
isWeiExi = true;
}
}
if (isVerExi == true)
{
sfont = "Verdana, 12pt";
SetFont(this, (Font)fc.ConvertFromString(sfont));
}
else if (isCouExi == true)
{
sfont = "Courier New, 12pt";
SetFont(this, (Font)fc.ConvertFromString(sfont));
}
else if (isWeiExi == true)
{
sfont = "微软雅黑, 12pt";
SetFont(this, (Font)fc.ConvertFromString(sfont));
}
}
}
catch (Exception ex)
{
}
}
private void DocumentForm_Shown(object sender, EventArgs e)
{
setFontStyle();
}
public void SetFont(DocumentForm doc, Font xFont)
{
//ActiveDocument.Scintilla.Font = xFont;
doc.Scintilla.Styles.Default.Font = xFont;
doc.Scintilla.Styles[0].Font = xFont; //'white space
doc.Scintilla.Styles[1].Font = xFont; //'comments-block
doc.Scintilla.Styles[2].Font = xFont; // 'comments-singlechar
doc.Scintilla.Styles[3].Font = xFont; // 'half-formed comment
doc.Scintilla.Styles[4].Font = xFont; // 'numbers
doc.Scintilla.Styles[5].Font = xFont; // 'keyword after complete
doc.Scintilla.Styles[6].Font = xFont; // 'quoted text-double
doc.Scintilla.Styles[7].Font = xFont; // 'quoted text-single
doc.Scintilla.Styles[8].Font = xFont; //'"table" keyword l
doc.Scintilla.Styles[9].Font = xFont; //'? knows
doc.Scintilla.Styles[10].Font = xFont; //'symbol (<>);=-
doc.Scintilla.Styles[11].Font = xFont; //'half-formed words
doc.Scintilla.Styles[12].Font = xFont; //'mixed quoted text 'text" ?strange
doc.Scintilla.Styles[14].Font = xFont; //'sql-type keyword (still looks weird)
doc.Scintilla.Styles[15].Font = xFont; //'sql @symbol in a comment
doc.Scintilla.Styles[16].Font = xFont; //'sql function returning INT
doc.Scintilla.Styles[19].Font = xFont; //'in/out
doc.Scintilla.Styles[32].Font = xFont; //'plain ordinary whitespace, that exists everywhere
doc.Scintilla.Styles[StylesCommon.LineNumber].Font = xFont;
doc.Scintilla.Styles[StylesCommon.BraceBad].Font = xFont;
doc.Scintilla.Styles[StylesCommon.BraceLight].Font = xFont;
doc.Scintilla.Styles[StylesCommon.CallTip].Font = xFont;
doc.Scintilla.Styles[StylesCommon.ControlChar].Font = xFont;
doc.Scintilla.Styles[StylesCommon.Default].Font = xFont;
doc.Scintilla.Styles[StylesCommon.IndentGuide].Font = xFont;
doc.Scintilla.Styles[StylesCommon.LastPredefined].Font = xFont;
doc.Scintilla.Styles[StylesCommon.Max].Font = xFont;
//设置样式
doc.Scintilla.Styles[StylesCommon.LineNumber].BackColor = SystemColors.Window;
doc.Scintilla.Margins.FoldMarginColor = Color.FromArgb(250, 240, 230);
doc.Scintilla.Margins.Margin1.Width = 0;
doc.Scintilla.Styles[StylesCommon.LineNumber].ForeColor = Color.FromArgb(128, 128, 128);
}
//设置语言
public void SetLanguageByDoc(string language,string newTitle,string newTreeLocation)
{
this.TreeLocation = newTreeLocation;
reFreshStatus();
if (this.Scintilla.Modified == true)
{
this.Text = newTitle + " *";
}
else
{
this.Text = newTitle;
}
if ("ini".Equals(language, StringComparison.OrdinalIgnoreCase))
{
// Reset/set all styles and prepare _scintilla for custom lexing
this.IniLexer = true;
//WeCode1._0.IniLexer.Init(this.Scintilla);
}
else
{
// Use a built-in lexer and configuration
this.IniLexer = false;
this.Scintilla.ConfigurationManager.Language = language;
// Smart indenting...
if ("cs".Equals(language, StringComparison.OrdinalIgnoreCase))
this.Scintilla.Indentation.SmartIndentType = ScintillaNET.SmartIndent.CPP;
else
this.Scintilla.Indentation.SmartIndentType = ScintillaNET.SmartIndent.None;
}
setFontStyle();
}
}
}