From e6a4e0c79706218f56f0b0d3305df4f5d3d3bca8 Mon Sep 17 00:00:00 2001 From: gdkhd812 Date: Fri, 20 Nov 2015 18:50:36 +0530 Subject: [PATCH] =?utf8?q?View=E3=81=A8Controller=E3=81=A7Document?= =?utf8?q?=E3=82=92=E5=B7=AE=E3=81=97=E6=9B=BF=E3=81=88=E3=82=89=E3=82=8C?= =?utf8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F=EF=BC=86?= =?utf8?q?=E3=83=89=E3=82=AD=E3=83=A5=E3=83=A1=E3=83=B3=E3=83=88=E3=81=8C?= =?utf8?q?=E5=B7=AE=E3=81=97=E6=9B=BF=E3=81=88=E3=82=89=E3=82=8C=E3=82=8B?= =?utf8?q?=E5=8F=AF=E8=83=BD=E6=80=A7=E3=81=8C=E3=81=82=E3=82=8B=E5=A0=B4?= =?utf8?q?=E6=89=80=E3=82=92=E4=BF=AE=E6=AD=A3=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Core/Automaion/FooTextBoxAutomationPeer.cs | 31 +++++------ Core/Controller.cs | 34 ++++++++++-- Core/Document.cs | 9 +++ Core/EditView.cs | 20 ++++--- Core/ViewBase.cs | 89 +++++++++++++++++++----------- Metro/FooEditEngine/FooTextBox.cs | 1 + WPF/FooEditEngine/FooTextBox.cs | 1 + 7 files changed, 122 insertions(+), 63 deletions(-) diff --git a/Core/Automaion/FooTextBoxAutomationPeer.cs b/Core/Automaion/FooTextBoxAutomationPeer.cs index ad08d10..d5119b6 100644 --- a/Core/Automaion/FooTextBoxAutomationPeer.cs +++ b/Core/Automaion/FooTextBoxAutomationPeer.cs @@ -54,10 +54,9 @@ namespace FooEditEngine : base(owner) { this.fooTextBox = owner; - this.fooTextBox.Document.Update += Document_Update; } - void Document_Update(object sender, DocumentUpdateEventArgs e) + public void OnNotifyTextChanged() { this.RaiseAutomationEvent(AutomationEvents.TextPatternOnTextChanged); } @@ -266,7 +265,6 @@ namespace FooEditEngine sealed class FooTextBoxRangeProvider : ITextRangeProvider { private FooTextBox textbox; - private Document document; private FooTextBoxAutomationPeer _peer; private int start, end; @@ -277,7 +275,6 @@ namespace FooEditEngine public FooTextBoxRangeProvider(FooTextBox textbox, int start, int length, FooTextBoxAutomationPeer peer) { this.textbox = textbox; - this.document = textbox.Document; this.start = start; this.end = start + length; _peer = peer; @@ -344,7 +341,7 @@ namespace FooEditEngine { if (this.start == this.end) { - if (this.start < this.document.Length) + if (this.start < this.textbox.Document.Length) this.end += 1; } return; @@ -360,7 +357,7 @@ namespace FooEditEngine if (unit == TextUnit.Paragraph || unit == TextUnit.Page || unit == TextUnit.Document) { this.start = 0; - this.end = this.document.Length; + this.end = this.textbox.Document.Length; return; } throw new NotImplementedException(); @@ -375,8 +372,8 @@ namespace FooEditEngine { if (backward) throw new NotImplementedException(); - document.SetFindParam(text, false, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None); - IEnumerator it = document.Find(); + textbox.Document.SetFindParam(text, false, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None); + IEnumerator it = textbox.Document.Find(); if (it.MoveNext()) { SearchResult sr = it.Current; @@ -442,8 +439,8 @@ namespace FooEditEngine bool IsNewLine(int index) { - if (this.document.Length > 0) - return this.document[index == 0 ? 0 : index - 1] == Document.NewLine; + if (this.textbox.Document.Length > 0) + return this.textbox.Document[index == 0 ? 0 : index - 1] == Document.NewLine; else return false; } @@ -460,13 +457,13 @@ namespace FooEditEngine public String GetText(int maxLength) { - if (this.document.Length == 0) + if (this.textbox.Document.Length == 0) return ""; int length = this.end - this.start; if (maxLength < 0) - return this.document.ToString(this.start, length); + return this.textbox.Document.ToString(this.start, length); else - return this.document.ToString(this.start, (int)Math.Min(length, maxLength)); + return this.textbox.Document.ToString(this.start, (int)Math.Min(length, maxLength)); } public int Move(TextUnit unit, int count) @@ -493,7 +490,7 @@ namespace FooEditEngine case TextUnit.Document: { this.start = 0; - this.end = this.document.Length; + this.end = this.textbox.Document.Length; return 1; } } @@ -558,7 +555,7 @@ namespace FooEditEngine if (unit == TextUnit.Paragraph || unit == TextUnit.Page || unit == TextUnit.Document) { this.start = 0; - this.end = this.document.Length - 1; + this.end = this.textbox.Document.Length - 1; moved = 1; } return moved; @@ -567,8 +564,8 @@ namespace FooEditEngine int MoveEndpointByCharacter(int index, int count,out int moved) { int newIndex = index + count - 1; - if (newIndex > this.document.Length) - newIndex = this.document.Length; + if (newIndex > this.textbox.Document.Length) + newIndex = this.textbox.Document.Length; if (newIndex < 0) newIndex = 0; moved = newIndex - index; diff --git a/Core/Controller.cs b/Core/Controller.cs index 65860ea..7be5049 100644 --- a/Core/Controller.cs +++ b/Core/Controller.cs @@ -48,17 +48,12 @@ namespace FooEditEngine internal sealed class Controller { EditView View; - Document Document; + Document _Document; int AnchorIndex; public Controller(Document doc, EditView view) { this.Document = doc; - this.Document.Update += new DocumentUpdateEventHandler(Document_Update); - this.Document.StatusUpdate += (s, e) => - { - this.AdjustCaret(); - }; this.View = view; this.View.render.ChangedRenderResource += render_ChangedRenderResource; this.View.PerformLayouted += View_LineBreakChanged; @@ -67,6 +62,33 @@ namespace FooEditEngine this.Document.Clear(); } + public Document Document + { + get + { + return this._Document; + } + set + { + //メモリリークを防ぐためにパンドラーを除く + if (this._Document != null) + { + this._Document.Update -= Document_Update; + this._Document.StatusUpdate -= Document_StatusChanged; + } + + this._Document = value; + + this._Document.Update += new DocumentUpdateEventHandler(Document_Update); + this._Document.StatusUpdate += Document_StatusChanged; + } + } + + void Document_StatusChanged(object sender,EventArgs e) + { + this.AdjustCaret(); + } + /// /// 選択領域変更時に通知される /// diff --git a/Core/Document.cs b/Core/Document.cs index ca56d58..954b4ca 100644 --- a/Core/Document.cs +++ b/Core/Document.cs @@ -197,6 +197,15 @@ namespace FooEditEngine } /// + /// レタリングの開始位置を表す + /// + internal Point2 Src + { + get; + set; + } + + /// /// ルーラーやキャレット・行番号などの表示すべきものが変化した場合に呼び出される。ドキュメントの内容が変化した通知を受け取り場合はUpdateを使用してください /// public event EventHandler StatusUpdate; diff --git a/Core/EditView.cs b/Core/EditView.cs index 472d90f..6bd2a70 100644 --- a/Core/EditView.cs +++ b/Core/EditView.cs @@ -645,11 +645,11 @@ namespace FooEditEngine } else if (x > right) //xは表示領域の右側にある { - this._Src.X = x - this.render.TextArea.Width + this.ScrollMarginWidth; - if (this.Document.RightToLeft && this._Src.X > 0) + this.Document.Src = new Point2(x - this.render.TextArea.Width + this.ScrollMarginWidth,this.Document.Src.Row); + if (this.Document.RightToLeft && this.Document.Src.X > 0) { System.Diagnostics.Debug.Assert(x > 0); - this._Src.X = 0; + this.Document.Src = new Point2(0, this.Document.Src.Row); } else { @@ -659,10 +659,10 @@ namespace FooEditEngine } else if (x < left) //xは表示領域の左側にある { - this._Src.X = x - this.ScrollMarginWidth; - if (!this.Document.RightToLeft && this._Src.X < this.render.TextArea.X) + this.Document.Src = new Point2(x - this.ScrollMarginWidth, this.Document.Src.Row); + if (!this.Document.RightToLeft && this.Document.Src.X < this.render.TextArea.X) { - this._Src.X = 0; + this.Document.Src = new Point2(0, this.Document.Src.Row); } else { @@ -681,14 +681,14 @@ namespace FooEditEngine caretRow = tp.row - this.Src.Row; else if (tp.row >= this.Src.Row + lineCount) { - this._Src.Row = this.GetSrcRow(tp.row, this.LineCountOnScreen); - caretRow = tp.row - this._Src.Row; + this.Document.Src = new Point2(this.Document.Src.X, this.GetSrcRow(tp.row, this.LineCountOnScreen)); + caretRow = tp.row - this.Document.Src.Row; result = true; CalculateLineCountOnScreen(); } else if (tp.row < this.Src.Row) { - this._Src.Row = tp.row; + this.Document.Src = new Point2(this.Document.Src.X, tp.row); result = true; CalculateLineCountOnScreen(); } @@ -715,7 +715,9 @@ namespace FooEditEngine this.SetCaretPostion(x, y); if (result) + { this.OnSrcChanged(null); + } return result; } diff --git a/Core/ViewBase.cs b/Core/ViewBase.cs index a1356a9..d2b8932 100644 --- a/Core/ViewBase.cs +++ b/Core/ViewBase.cs @@ -77,8 +77,7 @@ namespace FooEditEngine { const int SpiltCharCount = 1024; - protected Document Document; - protected Point2 _Src = new Point2(); + Document _Document; protected Rectangle _Rect; protected double _LongestWidth; Padding _Padding; @@ -87,7 +86,6 @@ namespace FooEditEngine { this._Padding = padding; this.Document = doc; - this.Document.UpdateCalledAlways += new DocumentUpdateEventHandler(doc_Update); this._LayoutLines.SpilitString = new SpilitStringEventHandler(LayoutLines_SpilitStringByChar); this.render = r; this.render.ChangedRenderResource += new ChangedRenderResourceEventHandler(render_ChangedRenderResource); @@ -95,30 +93,60 @@ namespace FooEditEngine this.SrcChanged += new EventHandler((s, e) => { }); this.PerformLayouted += new EventHandler((s, e) => { }); this.PageBoundChanged += new EventHandler((s, e) => { }); - this.Document.LineBreakChanged += (s, e) => { - if (this.Document.LineBreak != LineBreakMethod.None) - this._LayoutLines.SpilitString = new SpilitStringEventHandler(LayoutLines_SpilitStringByPixelbase); - else - this._LayoutLines.SpilitString = new SpilitStringEventHandler(LayoutLines_SpilitStringByChar); - }; - this.Document.StatusUpdate += (s,e) => + } + + protected Document Document + { + get { - if(this.render.TabWidthChar != this.Document.TabStops) - this.render.TabWidthChar = this.Document.TabStops; - if (this.render.RightToLeft != this.Document.RightToLeft) - this.render.RightToLeft = this.Document.RightToLeft; - if (this.render.ShowFullSpace != this.Document.ShowFullSpace) - this.render.ShowFullSpace = this.Document.ShowFullSpace; - if (this.render.ShowHalfSpace != this.Document.ShowHalfSpace) - this.render.ShowHalfSpace = this.Document.ShowHalfSpace; - if (this.render.ShowTab != this.Document.ShowTab) - this.render.ShowTab = this.Document.ShowTab; - if (this.render.ShowLineBreak != this.Document.ShowLineBreak) - this.render.ShowLineBreak = this.Document.ShowLineBreak; - CalculateClipRect(); - CalculateLineCountOnScreen(); - this._LayoutLines.ClearLayoutCache(); - }; + return this._Document; + } + set + { + if(this._Document != null) + { + this._Document.UpdateCalledAlways -= new DocumentUpdateEventHandler(doc_Update); + this._Document.LineBreakChanged -= Document_LineBreakChanged; + this._Document.StatusUpdate -= Document_StatusUpdate; + } + + this._Document = value; + + this._Document.UpdateCalledAlways += new DocumentUpdateEventHandler(doc_Update); + this._Document.LineBreakChanged += Document_LineBreakChanged; + this._Document.StatusUpdate += Document_StatusUpdate; + + this.Document_StatusUpdate(this, null); + } + } + + private void Document_StatusUpdate(object sender, EventArgs e) + { + if (this.render == null) + return; + if (this.render.TabWidthChar != this.Document.TabStops) + this.render.TabWidthChar = this.Document.TabStops; + if (this.render.RightToLeft != this.Document.RightToLeft) + this.render.RightToLeft = this.Document.RightToLeft; + if (this.render.ShowFullSpace != this.Document.ShowFullSpace) + this.render.ShowFullSpace = this.Document.ShowFullSpace; + if (this.render.ShowHalfSpace != this.Document.ShowHalfSpace) + this.render.ShowHalfSpace = this.Document.ShowHalfSpace; + if (this.render.ShowTab != this.Document.ShowTab) + this.render.ShowTab = this.Document.ShowTab; + if (this.render.ShowLineBreak != this.Document.ShowLineBreak) + this.render.ShowLineBreak = this.Document.ShowLineBreak; + CalculateClipRect(); + CalculateLineCountOnScreen(); + this._LayoutLines.ClearLayoutCache(); + } + + private void Document_LineBreakChanged(object sender, EventArgs e) + { + if (this.Document.LineBreak != LineBreakMethod.None) + this._LayoutLines.SpilitString = new SpilitStringEventHandler(LayoutLines_SpilitStringByPixelbase); + else + this._LayoutLines.SpilitString = new SpilitStringEventHandler(LayoutLines_SpilitStringByChar); } protected LineToIndexTable _LayoutLines @@ -249,8 +277,8 @@ namespace FooEditEngine /// public Point2 Src { - get { return this._Src; } - set { this._Src = value; } + get { return this.Document.Src; } + set { this.Document.Src = value; } } public virtual void Draw(Rectangle updateRect) @@ -263,8 +291,7 @@ namespace FooEditEngine return true; if (row > this.LayoutLines.Count - 1) return true; - this._Src.X = x; - this._Src.Row = row; + this.Document.Src = new Point2(x, row); CalculateLineCountOnScreen(); this.SrcChanged(this,null); return false; @@ -317,7 +344,7 @@ namespace FooEditEngine void render_ChangedRightToLeft(object sender, EventArgs e) { - this._Src.X = 0; + this.Document.Src = new Point2(0, this.Document.Src.Row); } void render_ChangedRenderResource(object sender, ChangedRenderRsourceEventArgs e) diff --git a/Metro/FooEditEngine/FooTextBox.cs b/Metro/FooEditEngine/FooTextBox.cs index ebb25ea..b7c1a20 100644 --- a/Metro/FooEditEngine/FooTextBox.cs +++ b/Metro/FooEditEngine/FooTextBox.cs @@ -1079,6 +1079,7 @@ namespace FooEditEngine.Metro return; if (e.type == UpdateType.Replace) TextStoreHelper.NotifyTextChanged(this.textStore, e.startIndex, e.removeLength, e.insertLength); + this.peer.OnNotifyTextChanged(); } void FooTextBox_Loaded(object sender, RoutedEventArgs e) diff --git a/WPF/FooEditEngine/FooTextBox.cs b/WPF/FooEditEngine/FooTextBox.cs index 707e58b..6205f8f 100644 --- a/WPF/FooEditEngine/FooTextBox.cs +++ b/WPF/FooEditEngine/FooTextBox.cs @@ -978,6 +978,7 @@ namespace FooEditEngine.WPF return; if(e.type == UpdateType.Replace) TextStoreHelper.NotifyTextChanged(this.textStore, e.startIndex, e.removeLength, e.insertLength); + this.peer.OnNotifyTextChanged(); } void timer_Tick(object sender, EventArgs e) -- 2.11.0