2 * Copyright (C) 2013 FooProject
3 * * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
6 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
12 using System.Collections.Generic;
15 using System.Text.RegularExpressions;
16 using System.Threading.Tasks;
18 namespace FooEditEngine
21 /// LineBreakMethod列挙体
23 public enum LineBreakMethod
63 /// <param name="left">左</param>
64 /// <param name="top">上</param>
65 /// <param name="right">右</param>
66 /// <param name="bottom">下</param>
67 public Padding(int left, int top, int right, int bottom)
76 abstract class ViewBase : IDisposable
78 const int SpiltCharCount = 1024;
81 protected Rectangle _Rect;
82 protected double _LongestWidth;
85 public ViewBase(Document doc, ITextRender r,Padding padding)
87 this._Padding = padding;
89 this._LayoutLines.SpilitString = new SpilitStringEventHandler(LayoutLines_SpilitStringByChar);
91 this.render.ChangedRenderResource += new ChangedRenderResourceEventHandler(render_ChangedRenderResource);
92 this.render.ChangedRightToLeft += render_ChangedRightToLeft;
93 this.SrcChanged += new EventHandler((s, e) => { });
94 this.PageBoundChanged += new EventHandler((s, e) => { });
97 public Document Document
101 return this._Document;
105 if(this._Document != null)
107 this._Document.UpdateCalledAlways -= new DocumentUpdateEventHandler(doc_Update);
108 this._Document.LineBreakChanged -= Document_LineBreakChanged;
109 this._Document.StatusUpdate -= Document_StatusUpdate;
110 this._Document.PerformLayouted -= _Document_PerformLayouted;
113 this._Document = value;
115 this._Document.UpdateCalledAlways += new DocumentUpdateEventHandler(doc_Update);
116 this._Document.LineBreakChanged += Document_LineBreakChanged;
117 this._Document.StatusUpdate += Document_StatusUpdate;
118 this._Document.PerformLayouted += _Document_PerformLayouted;
120 this.Document_LineBreakChanged(this, null);
122 this.Document_StatusUpdate(this, null);
126 private void _Document_PerformLayouted(object sender, EventArgs e)
128 CalculateLineCountOnScreen();
129 if(this.PerformLayouted != null)
130 this.PerformLayouted(this, e);
133 private void Document_StatusUpdate(object sender, EventArgs e)
135 if (this.render == null)
137 if (this.render.TabWidthChar != this.Document.TabStops)
138 this.render.TabWidthChar = this.Document.TabStops;
139 if (this.render.RightToLeft != this.Document.RightToLeft)
140 this.render.RightToLeft = this.Document.RightToLeft;
141 if (this.render.ShowFullSpace != this.Document.ShowFullSpace)
142 this.render.ShowFullSpace = this.Document.ShowFullSpace;
143 if (this.render.ShowHalfSpace != this.Document.ShowHalfSpace)
144 this.render.ShowHalfSpace = this.Document.ShowHalfSpace;
145 if (this.render.ShowTab != this.Document.ShowTab)
146 this.render.ShowTab = this.Document.ShowTab;
147 if (this.render.ShowLineBreak != this.Document.ShowLineBreak)
148 this.render.ShowLineBreak = this.Document.ShowLineBreak;
150 CalculateLineCountOnScreen();
151 this._LayoutLines.ClearLayoutCache();
154 private void Document_LineBreakChanged(object sender, EventArgs e)
156 if (this.Document.LineBreak != LineBreakMethod.None)
157 this._LayoutLines.SpilitString = new SpilitStringEventHandler(LayoutLines_SpilitStringByPixelbase);
159 this._LayoutLines.SpilitString = new SpilitStringEventHandler(LayoutLines_SpilitStringByChar);
162 protected LineToIndexTable _LayoutLines
166 return this.Document.LayoutLines;
170 public event EventHandler SrcChanged;
173 public event EventHandler PerformLayouted;
175 public event EventHandler PageBoundChanged;
180 public ITextRender render
189 public int LineCountOnScreen
198 public double LineBreakingMarginWidth
207 public LineToIndexTable LayoutLines
209 get { return this._LayoutLines; }
215 public double LongestWidth
217 get { return this._LongestWidth; }
220 public double LineNumberMargin
224 return this.render.emSize.Width;
231 /// <remarks>差し替えた場合、再構築する必要があります</remarks>
232 public IHilighter Hilighter
234 get { return this._LayoutLines.Hilighter; }
235 set { this._LayoutLines.Hilighter = value; this._LayoutLines.ClearLayoutCache(); }
239 /// すべてのレイアウト行を破棄し、再度レイアウトをやり直す
242 public virtual void PerfomLayouts()
245 this.Document.PerformLayout();
251 public Padding Padding
254 return this._Padding;
257 this._Padding = value;
259 CalculateLineCountOnScreen();
260 if (this.Document.RightToLeft)
261 this._LayoutLines.ClearLayoutCache();
262 this.PageBoundChanged(this, null);
269 public Rectangle PageBound
271 get { return this._Rect; }
274 if (value.Width < 0 || value.Height < 0)
275 throw new ArgumentOutOfRangeException("");
278 CalculateLineCountOnScreen();
279 if (this.Document.RightToLeft)
280 this._LayoutLines.ClearLayoutCache();
281 this.PageBoundChanged(this, null);
286 /// Draw()の対象となる領域の左上を表す
290 get { return this.Document.Src; }
291 set { this.Document.Src = value; }
294 public virtual void Draw(Rectangle updateRect, bool force = false)
299 public virtual bool TryScroll(double x, int row)
303 if (row > this.LayoutLines.Count - 1)
305 this.Document.Src = new SrcPoint(x, row, row * this.render.emSize.Height);
306 this.SrcChanged(this,null);
310 public void Dispose()
313 GC.SuppressFinalize(this);
316 public virtual void CalculateLineCountOnScreen()
320 protected virtual void Dispose(bool disposing)
324 this._Document.UpdateCalledAlways -= new DocumentUpdateEventHandler(this.doc_Update); //これをしないと複数のビューを作成した時に妙なエラーが発生する
325 this._Document.LineBreakChanged -= Document_LineBreakChanged;
326 this._Document.StatusUpdate -= Document_StatusUpdate;
327 this._Document.PerformLayouted -= _Document_PerformLayouted;
329 this._LayoutLines.Clear();
332 protected virtual void CalculateClipRect()
337 protected virtual void OnSrcChanged(EventArgs e)
339 EventHandler handler = this.SrcChanged;
341 this.SrcChanged(this, e);
344 protected virtual void OnPerformLayoutedChanged(EventArgs e)
346 EventHandler handler = this.PerformLayouted;
348 this.PerformLayouted(this, e);
351 protected virtual void OnPageBoundChanged(EventArgs e)
353 EventHandler handler = this.PageBoundChanged;
355 this.PageBoundChanged(this, e);
358 void render_ChangedRightToLeft(object sender, EventArgs e)
360 this.Document.Src = new SrcPoint(0, this.Document.Src.Row, this.Src.Y);
363 void render_ChangedRenderResource(object sender, ChangedRenderRsourceEventArgs e)
365 this._LayoutLines.ClearLayoutCache();
366 if (e.type == ResourceType.Font)
368 this.CalculateClipRect();
369 this.CalculateLineCountOnScreen();
373 void doc_Update(object sender, DocumentUpdateEventArgs e)
377 case UpdateType.Clear:
378 this._LongestWidth = 0;
383 IList<LineToIndexTableData> LayoutLines_SpilitStringByPixelbase(object sender, SpilitStringEventArgs e)
386 if (this.Document.LineBreak == LineBreakMethod.PageBound)
387 WrapWidth = this.render.TextArea.Width - LineBreakingMarginWidth; //余白を残さないと欠ける
389 WrapWidth = this.render.emSize.Width * this.Document.LineBreakCharCount;
391 if (WrapWidth < 0 && this.Document.LineBreak != LineBreakMethod.None)
392 throw new InvalidOperationException();
394 int startIndex = e.index;
395 int endIndex = e.index + e.length - 1;
397 LineToIndexTable layoutLineCollection = (LineToIndexTable)sender;
399 return this.render.BreakLine(e.buffer,layoutLineCollection, startIndex, endIndex, WrapWidth);
402 IList<LineToIndexTableData> LayoutLines_SpilitStringByChar(object sender, SpilitStringEventArgs e)
404 return this.Document.CreateLineList(e.index, e.length, Document.MaximumLineLength);