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_StatusUpdate(this, null);
124 private void _Document_PerformLayouted(object sender, EventArgs e)
126 CalculateLineCountOnScreen();
127 if(this.PerformLayouted != null)
128 this.PerformLayouted(this, e);
131 private void Document_StatusUpdate(object sender, EventArgs e)
133 if (this.render == null)
135 if (this.render.TabWidthChar != this.Document.TabStops)
136 this.render.TabWidthChar = this.Document.TabStops;
137 if (this.render.RightToLeft != this.Document.RightToLeft)
138 this.render.RightToLeft = this.Document.RightToLeft;
139 if (this.render.ShowFullSpace != this.Document.ShowFullSpace)
140 this.render.ShowFullSpace = this.Document.ShowFullSpace;
141 if (this.render.ShowHalfSpace != this.Document.ShowHalfSpace)
142 this.render.ShowHalfSpace = this.Document.ShowHalfSpace;
143 if (this.render.ShowTab != this.Document.ShowTab)
144 this.render.ShowTab = this.Document.ShowTab;
145 if (this.render.ShowLineBreak != this.Document.ShowLineBreak)
146 this.render.ShowLineBreak = this.Document.ShowLineBreak;
148 CalculateLineCountOnScreen();
149 this._LayoutLines.ClearLayoutCache();
152 private void Document_LineBreakChanged(object sender, EventArgs e)
154 if (this.Document.LineBreak != LineBreakMethod.None)
155 this._LayoutLines.SpilitString = new SpilitStringEventHandler(LayoutLines_SpilitStringByPixelbase);
157 this._LayoutLines.SpilitString = new SpilitStringEventHandler(LayoutLines_SpilitStringByChar);
160 protected LineToIndexTable _LayoutLines
164 return this.Document.LayoutLines;
168 public event EventHandler SrcChanged;
171 public event EventHandler PerformLayouted;
173 public event EventHandler PageBoundChanged;
178 public ITextRender render
187 public int LineCountOnScreen
196 public double LineBreakingMarginWidth
205 public LineToIndexTable LayoutLines
207 get { return this._LayoutLines; }
213 public double LongestWidth
215 get { return this._LongestWidth; }
218 public double LineNumberMargin
222 return this.render.emSize.Width;
229 /// <remarks>差し替えた場合、再構築する必要があります</remarks>
230 public IHilighter Hilighter
232 get { return this._LayoutLines.Hilighter; }
233 set { this._LayoutLines.Hilighter = value; this._LayoutLines.ClearLayoutCache(); }
237 /// すべてのレイアウト行を破棄し、再度レイアウトをやり直す
240 public virtual void PerfomLayouts()
243 this.Document.PerformLayout();
249 public Padding Padding
252 return this._Padding;
255 this._Padding = value;
257 CalculateLineCountOnScreen();
258 if (this.Document.RightToLeft)
259 this._LayoutLines.ClearLayoutCache();
260 this.PageBoundChanged(this, null);
267 public Rectangle PageBound
269 get { return this._Rect; }
272 if (value.Width < 0 || value.Height < 0)
273 throw new ArgumentOutOfRangeException("");
276 CalculateLineCountOnScreen();
277 if (this.Document.RightToLeft)
278 this._LayoutLines.ClearLayoutCache();
279 this.PageBoundChanged(this, null);
284 /// Draw()の対象となる領域の左上を表す
288 get { return this.Document.Src; }
289 set { this.Document.Src = value; }
292 public virtual void Draw(Rectangle updateRect, bool force = false)
297 public virtual bool TryScroll(double x, int row)
301 if (row > this.LayoutLines.Count - 1)
303 this.Document.Src = new SrcPoint(x, row, row * this.render.emSize.Height);
304 this.SrcChanged(this,null);
308 public void Dispose()
311 GC.SuppressFinalize(this);
314 public virtual void CalculateLineCountOnScreen()
318 protected virtual void Dispose(bool disposing)
322 this.Document.UpdateCalledAlways -= new DocumentUpdateEventHandler(this.doc_Update); //これをしないと複数のビューを作成した時に妙なエラーが発生する
324 this._LayoutLines.Clear();
327 protected virtual void CalculateClipRect()
332 protected virtual void OnSrcChanged(EventArgs e)
334 EventHandler handler = this.SrcChanged;
336 this.SrcChanged(this, e);
339 protected virtual void OnPerformLayoutedChanged(EventArgs e)
341 EventHandler handler = this.PerformLayouted;
343 this.PerformLayouted(this, e);
346 protected virtual void OnPageBoundChanged(EventArgs e)
348 EventHandler handler = this.PageBoundChanged;
350 this.PageBoundChanged(this, e);
353 void render_ChangedRightToLeft(object sender, EventArgs e)
355 this.Document.Src = new SrcPoint(0, this.Document.Src.Row, this.Src.Y);
358 void render_ChangedRenderResource(object sender, ChangedRenderRsourceEventArgs e)
360 this._LayoutLines.ClearLayoutCache();
361 if (e.type == ResourceType.Font)
363 this.CalculateClipRect();
364 this.CalculateLineCountOnScreen();
368 void doc_Update(object sender, DocumentUpdateEventArgs e)
372 case UpdateType.Clear:
373 this._LongestWidth = 0;
378 IList<LineToIndexTableData> LayoutLines_SpilitStringByPixelbase(object sender, SpilitStringEventArgs e)
381 if (this.Document.LineBreak == LineBreakMethod.PageBound)
382 WrapWidth = this.render.TextArea.Width - LineBreakingMarginWidth; //余白を残さないと欠ける
384 WrapWidth = this.render.emSize.Width * this.Document.LineBreakCharCount;
386 if (WrapWidth < 0 && this.Document.LineBreak != LineBreakMethod.None)
387 throw new InvalidOperationException();
389 int startIndex = e.index;
390 int endIndex = e.index + e.length - 1;
392 LineToIndexTable layoutLineCollection = (LineToIndexTable)sender;
394 return this.render.BreakLine(e.buffer,layoutLineCollection, startIndex, endIndex, WrapWidth);
397 IList<LineToIndexTableData> LayoutLines_SpilitStringByChar(object sender, SpilitStringEventArgs e)
399 return this.Document.CreateLineList(e.index, e.length, Document.MaximumLineLength);