OSDN Git Service

b892490e428f0c5732de5dcb8c3b6fc6a6173814
[fooeditengine/FooEditEngine.git] / Core / EditView.cs
1 /*
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.
5
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.
8
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/>.
10  */
11 using System;
12 using System.Linq;
13 using System.Collections.Generic;
14
15 namespace FooEditEngine
16 {
17     enum AdjustFlow
18     {
19         Row,
20         Col,
21         Both,
22     }
23
24     /// <summary>
25     /// キャレットとドキュメントの表示を担当します。レイアウト関連もこちらで行います
26     /// </summary>
27     sealed class EditView : ViewBase
28
29     {
30         internal const float LineMarkerThickness = 2;
31         long tickCount;
32         bool _CaretBlink, _HideRuler = true;
33         internal const int LineNumberLength = 6;
34         const int UpdateAreaPaddingWidth = 2;
35         const int UpdateAreaWidth = 4;
36         const int UpdateAreaTotalWidth = UpdateAreaWidth + UpdateAreaPaddingWidth;
37
38         /// <summary>
39         /// コンストラクター
40         /// </summary>
41         public EditView(Document doc, IEditorRender r, int MarginLeftAndRight = 5)
42             : this(doc, r, new Padding(MarginLeftAndRight, 0, MarginLeftAndRight, 0))
43         {
44         }
45
46         /// <summary>
47         /// コンストラクター
48         /// </summary>
49         /// <param name="doc">ドキュメント</param>
50         /// <param name="r">レンダー</param>
51         /// <param name="margin">マージン(1番目:左、2番目:上、3番目:右、4番目:下)</param>
52         public EditView(Document doc, IEditorRender r, Padding margin)
53             : base(doc, r, margin)
54         {
55             this.CaretBlinkTime = 500;
56             this.CaretWidthOnInsertMode = 1;
57             this.CalculateClipRect();
58             this.CaretLocation = new Point(this.render.TextArea.X, this.render.TextArea.Y);
59             this.LayoutLines.FoldingCollection.StatusChanged += FoldingCollection_StatusChanged;
60             this.IsFocused = false;
61         }
62
63         /// <summary>
64         /// 選択範囲コレクション
65         /// </summary>
66         internal SelectCollection Selections
67         {
68             get { return this.Document.Selections; }
69             set { this.Document.Selections = value; }
70         }
71
72         /// <summary>
73         /// ラインマーカーを描くなら偽。そうでなければ真
74         /// </summary>
75         public bool HideLineMarker
76         {
77             get { return this.Document.HideLineMarker; }
78             set { this.Document.HideLineMarker = value; }
79         }
80
81         /// <summary>
82         /// キャレットを描くなら偽。そうでなければ真
83         /// </summary>
84         public bool HideCaret
85         {
86             get { return this.Document.HideCaret; }
87             set { this.Document.HideCaret = value; }
88         }
89
90         /// <summary>
91         /// 挿入モードなら真を返し、上書きモードなら偽を返す
92         /// </summary>
93         public bool InsertMode
94         {
95             get { return this.Document.InsertMode; }
96             set { this.Document.InsertMode = value; }
97         }
98
99         /// <summary>
100         /// キャレットの点滅間隔
101         /// </summary>
102         public int CaretBlinkTime
103         {
104             get;
105             set;
106         }
107
108         /// <summary>
109         /// 挿入モード時のキャレットの幅
110         /// </summary>
111         public double CaretWidthOnInsertMode
112         {
113             get;
114             set;
115         }
116
117         /// <summary>
118         /// フォーカスがあるなら真をセットする
119         /// </summary>
120         public bool IsFocused
121         {
122             get;
123             set;
124         }
125
126         /// <summary>
127         /// キャレットを点滅させるなら真。そうでないなら偽
128         /// </summary>
129         /// <remarks>キャレット点滅タイマーもリセットされます</remarks>
130         public bool CaretBlink
131         {
132             get { return this._CaretBlink; }
133             set
134             {
135                 this._CaretBlink = value;
136                 if (value)
137                     this.tickCount = DateTime.Now.Ticks + this.To100nsTime(this.CaretBlinkTime);
138             }
139         }
140
141         /// <summary>
142         /// 一ページの高さに収まる行数を返す(こちらは表示されていない行も含みます)
143         /// </summary>
144         public int LineCountOnScreenWithInVisible
145         {
146             get;
147             private set;
148         }
149
150         /// <summary>
151         /// スクロール時に確保するマージン幅
152         /// </summary>
153         public double ScrollMarginWidth
154         {
155             get { return this.PageBound.Width * 20 / 100; }
156         }
157
158         /// <summary>
159         /// キャレットがある領域を示す
160         /// </summary>
161         public Point CaretLocation
162         {
163             get;
164             private set;
165         }
166
167         /// <summary>
168         /// ヒットテストを行う
169         /// </summary>
170         /// <param name="x">x座標</param>
171         /// <param name="y">y座標</param>
172         /// <returns>テキストエリア内にあれば真。そうでなければ偽</returns>
173         public bool HitTextArea(double x, double y)
174         {
175             if (x >= this.render.TextArea.X && x <= this.render.TextArea.Right &&
176                 y >= this.render.TextArea.Y && y <= this.render.TextArea.Bottom)
177                 return true;
178             else
179                 return false;
180         }
181
182         /// <summary>
183         /// ヒットテストを行う
184         /// </summary>
185         /// <param name="x">x座標</param>
186         /// <param name="row">行</param>
187         /// <returns>ヒットした場合はFoldingDataオブジェクトが返され、そうでない場合はnullが返る</returns>
188         public FoldingItem HitFoldingData(double x, int row)
189         {
190             IEditorRender render = (IEditorRender)base.render;
191
192             if (x >= this.GetRealtiveX(AreaType.FoldingArea) && x <= this.GetRealtiveX(AreaType.FoldingArea) + render.FoldingWidth)
193             {
194                 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(row);
195                 int lineLength = this.LayoutLines.GetLengthFromLineNumber(row);
196                 FoldingItem foldingData = this.LayoutLines.FoldingCollection.Get(lineHeadIndex, lineLength);
197                 if (foldingData != null && foldingData.IsFirstLine(this.LayoutLines,row))
198                     return foldingData;
199             }
200             return null;
201         }
202
203         /// <summary>
204         /// Rectで指定された範囲にドキュメントを描く
205         /// </summary>
206         /// <param name="updateRect">描写する範囲</param>
207         /// <returns>キャッシュしてはならない場合は真を返し、そうでない場合は偽を返します</returns>
208         /// <remarks>描写する範囲がPageBoundより小さい場合、キャッシュされた内容を使用することがあります。なお、レタリング後にrender.CacheContent()を呼び出さなかった場合は更新範囲にかかわらずキャッシュを使用しません</remarks>
209         public override bool Draw(Rectangle updateRect)
210         {
211             if (this.LayoutLines.Count == 0)
212                 return false;
213
214             IEditorRender render = (IEditorRender)base.render;
215
216             if ((updateRect.Height < this.PageBound.Height ||
217                 updateRect.Width < this.PageBound.Width) && 
218                 render.IsVaildCache())
219             {
220                 render.DrawCachedBitmap(updateRect);
221             }
222             else
223             {
224                 Rectangle background = this.PageBound;
225                 render.FillBackground(background);
226
227                 if (this.Document.HideRuler == false)
228                     this.DrawRuler();
229
230                 this.DrawLineMarker(this.Document.CaretPostion.row);
231
232                 Point pos = this.render.TextArea.TopLeft;
233                 //画面上では(X,Y)が開始位置になるが、開始する行が決まっているのでオフセットを求める
234                 pos.Y -= this.Src.GetOffsetY(this.render.emSize.Height);
235
236                 double endposy = this.render.TextArea.Bottom;
237                 Size lineNumberSize = new Size(this.render.LineNemberWidth, this.render.TextArea.Height);
238
239                 this.render.BeginClipRect(new Rectangle(this.PageBound.X, this.render.TextArea.Y, this.PageBound.Width, this.render.TextArea.Height));
240
241                 //パフォーマンス向上のため行番号などを先に描く
242                 for (int i = this.Src.Row; i < this.LayoutLines.Count; i++)
243                 {
244                     int lineIndex = this.LayoutLines.GetIndexFromLineNumber(i);
245                     int lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
246                     ITextLayout layout = this.LayoutLines.GetLayout(i);
247
248                     if (pos.Y > endposy)
249                         break;
250
251                     FoldingItem foldingData = this.LayoutLines.FoldingCollection.Get(lineIndex, lineLength);
252
253                     if (foldingData != null)
254                     {
255                         if ((!this.LayoutLines.FoldingCollection.IsHasParent(foldingData) || 
256                              !this.LayoutLines.FoldingCollection.IsParentHidden(foldingData))
257                             && foldingData.IsFirstLine(this.LayoutLines, i))
258                             render.DrawFoldingMark(foldingData.Expand, this.PageBound.X + this.GetRealtiveX(AreaType.FoldingArea), pos.Y);
259                         if (this.LayoutLines.FoldingCollection.IsHidden(lineIndex))
260                             continue;
261                     }
262
263                     if (this.Document.DrawLineNumber)
264                     {
265                         this.render.DrawString((i + 1).ToString(), this.PageBound.X + this.GetRealtiveX(AreaType.LineNumberArea), pos.Y, StringAlignment.Right, lineNumberSize,StringColorType.LineNumber);
266                     }
267
268                     DrawUpdateArea(i, pos.Y);
269
270                     pos.Y += this.LayoutLines.GetLayout(i).Height;
271                 }
272
273                 this.render.EndClipRect();
274
275                 //リセットしないと行が正しく描けない
276                 pos = this.render.TextArea.TopLeft;
277                 pos.X -= this.Src.X;
278                 pos.Y -= this.Src.GetOffsetY(this.render.emSize.Height);
279
280                 this.render.BeginClipRect(this.render.TextArea);
281
282                 for (int i = this.Src.Row; i < this.LayoutLines.Count; i++)
283                 {
284                     int lineIndex = this.LayoutLines.GetIndexFromLineNumber(i);
285                     int lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
286                     ITextLayout layout = this.LayoutLines.GetLayout(i);
287
288                     if (pos.Y > endposy)
289                         break;
290
291                     var selectRange = from s in this.Selections.Get(lineIndex, lineLength)
292                                       let n = Util.ConvertAbsIndexToRelIndex(s, lineIndex, lineLength)
293                                       select n;
294
295                     this.render.DrawOneLine(this.LayoutLines, i, pos.X, pos.Y, selectRange);
296
297                     pos.Y += this.LayoutLines.GetLayout(i).Height;
298                 }
299
300                 this.render.EndClipRect();
301
302                 this.DrawInsertPoint();
303
304                 this.Document.SelectGrippers.BottomLeft.Draw(this.render);
305                 this.Document.SelectGrippers.BottomRight.Draw(this.render);
306             }
307             return this.DrawCaret();    //キャレットを描いた場合はキャッシュしてはならない
308         }
309
310         void DrawUpdateArea(int row,double ypos)
311         {
312             IEditorRender render = (IEditorRender)base.render;
313             if(this.LayoutLines.GetDirtyFlag(row))
314             {
315                 Point pos = new Point(this.PageBound.X + this.GetRealtiveX(AreaType.UpdateArea), ypos);
316                 Rectangle rect = new Rectangle(pos.X, pos.Y, UpdateAreaWidth, this.LayoutLines.GetLayout(row).Height);
317                 render.FillRectangle(rect, FillRectType.UpdateArea);
318             }
319         }
320
321         void DrawRuler()
322         {
323             IEditorRender render = (IEditorRender)base.render;
324
325             Point pos, from, to;
326             Size emSize = render.emSize;
327             Rectangle clipRect = this.render.TextArea;
328             int count = 0;
329             double markerHeight = emSize.Height / 2;
330             if (this.Document.RightToLeft)
331             {
332                 pos = new Point(clipRect.TopRight.X, clipRect.TopRight.Y - emSize.Height - LineMarkerThickness);
333                 for (; pos.X >= clipRect.TopLeft.X; pos.X -= emSize.Width, count++)
334                 {
335                     from = pos;
336                     to = new Point(pos.X, pos.Y + emSize.Height);
337                     int mod = count % 10;
338                     if (mod == 0)
339                     {
340                         string countStr = (count / 10).ToString();
341                         double counterWidth = emSize.Width * countStr.Length;
342                         this.render.DrawString(countStr, pos.X - counterWidth, pos.Y, StringAlignment.Right, new Size(counterWidth, double.MaxValue));
343                     }
344                     else if (mod == 5)
345                         from.Y = from.Y + emSize.Height / 2;
346                     else
347                         from.Y = from.Y + emSize.Height * 3 / 4;
348                     render.DrawLine(from, to);
349                     if (this.CaretLocation.X >= pos.X && this.CaretLocation.X < pos.X + emSize.Width)
350                         render.FillRectangle(new Rectangle(pos.X, pos.Y + markerHeight, emSize.Width, markerHeight), FillRectType.OverwriteCaret);
351                 }
352             }
353             else
354             {
355                 pos = new Point(clipRect.TopLeft.X, clipRect.TopLeft.Y - emSize.Height - LineMarkerThickness);
356                 for (; pos.X < clipRect.TopRight.X; pos.X += emSize.Width, count++)
357                 {
358                     from = pos;
359                     to = new Point(pos.X, pos.Y + emSize.Height);
360                     int mod = count % 10;
361                     if (mod == 0)
362                         this.render.DrawString((count / 10).ToString(), pos.X, pos.Y, StringAlignment.Left, new Size(double.MaxValue, double.MaxValue));
363                     else if (mod == 5)
364                         from.Y = from.Y + emSize.Height / 2;
365                     else
366                         from.Y = from.Y + emSize.Height * 3 / 4;
367                     render.DrawLine(from, to);
368                     if (this.CaretLocation.X >= pos.X && this.CaretLocation.X < pos.X + emSize.Width)
369                         render.FillRectangle(new Rectangle(pos.X, pos.Y + markerHeight, emSize.Width, markerHeight), FillRectType.OverwriteCaret);
370                 }
371             }
372             from = clipRect.TopLeft;
373             from.Y -= LineMarkerThickness;
374             to = clipRect.TopRight;
375             to.Y -= LineMarkerThickness;
376             render.DrawLine(from, to);
377         }
378
379         void DrawInsertPoint()
380         {
381             //一つしかない場合は行選択の可能性がある
382             if (this.Selections.Count <= 1)
383                 return;
384             IEditorRender render = (IEditorRender)base.render;
385             foreach (Selection sel in this.Selections)
386             {
387                 if (sel.length == 0)
388                 {
389                     TextPoint tp = this.GetLayoutLineFromIndex(sel.start);
390                     Point left = this.GetPostionFromTextPoint(tp);
391                     double lineHeight = this.LayoutLines.GetLayout(tp.row).Height;
392                     Rectangle InsertRect = new Rectangle(left.X,
393                         left.Y,
394                         CaretWidthOnInsertMode,
395                         lineHeight);
396                     render.FillRectangle(InsertRect, FillRectType.InsertPoint);
397                 }
398             }
399         }
400
401         bool DrawCaret()
402         {
403             if (this.HideCaret || !this.IsFocused)
404                 return false;
405
406             long diff = DateTime.Now.Ticks - this.tickCount;
407             long blinkTime = this.To100nsTime(this.CaretBlinkTime);
408
409             if (this.CaretBlink && diff % blinkTime >= blinkTime / 2)
410                 return false;
411
412             Rectangle CaretRect = new Rectangle();
413
414             IEditorRender render = (IEditorRender)base.render;
415
416             int row = this.Document.CaretPostion.row;
417             double lineHeight = this.LayoutLines.GetLayout(row).Height;
418             double charWidth = this.LayoutLines.GetLayout(row).GetWidthFromIndex(this.Document.CaretPostion.col);
419
420             if (this.InsertMode || charWidth == 0)
421             {
422                 CaretRect.Size = new Size(CaretWidthOnInsertMode, lineHeight);
423                 CaretRect.Location = new Point(this.CaretLocation.X, this.CaretLocation.Y);
424                 render.FillRectangle(CaretRect, FillRectType.InsertCaret);
425             }
426             else
427             {
428                 double height = lineHeight / 3;
429                 CaretRect.Size = new Size(charWidth, height);
430                 CaretRect.Location = new Point(this.CaretLocation.X, this.CaretLocation.Y + lineHeight - height);
431                 render.FillRectangle(CaretRect, FillRectType.OverwriteCaret);
432             }
433             return true;
434         }
435
436         long To100nsTime(int ms)
437         {
438             return ms * 10000;
439         }
440
441         public void DrawLineMarker(int row)
442         {
443             if (this.HideLineMarker || !this.IsFocused)
444                 return;
445             IEditorRender render = (IEditorRender)base.render;
446             Point p = this.CaretLocation;
447             double height = this.LayoutLines.GetLayout(this.Document.CaretPostion.row).Height;
448             double width = this.render.TextArea.Width;
449             render.FillRectangle(new Rectangle(this.PageBound.X + this.render.TextArea.X, this.CaretLocation.Y, width, height), FillRectType.LineMarker);
450         }
451
452         /// <summary>
453         /// 現在のキャレット位置の領域を返す
454         /// </summary>
455         /// <returns>矩形領域を表すRectangle</returns>
456         public Rectangle GetCurrentCaretRect()
457         {
458             ITextLayout layout = this.LayoutLines.GetLayout(this.Document.CaretPostion.row);
459             double width = layout.GetWidthFromIndex(this.Document.CaretPostion.col);
460             if (width == 0.0)
461                 width = this.CaretWidthOnInsertMode;
462             double height = layout.Height;
463             Rectangle updateRect = new Rectangle(
464                 this.CaretLocation.X,
465                 this.CaretLocation.Y,
466                 width,
467                 height);
468             return updateRect;
469         }
470
471         /// <summary>
472         /// 指定した座標の一番近くにあるTextPointを取得する
473         /// </summary>
474         /// <param name="p">テキストエリアを左上とする相対位置</param>
475         /// <returns>レイアウトラインを指し示すTextPoint</returns>
476         public TextPoint GetTextPointFromPostion(Point p)
477         {
478             if (p.Y < this.render.TextArea.TopLeft.Y || 
479                 p.Y > this.render.TextArea.BottomRight.Y)
480                 return TextPoint.Null;
481             TextPoint tp = new TextPoint();
482
483             if (this.LayoutLines.Count == 0)
484                 return tp;
485
486             p.Y -= this.render.TextArea.Y;
487
488             int lineHeadIndex, lineLength;
489             double y = 0;
490             tp.row = this.LayoutLines.Count - 1;
491             for (int i = this.Src.Row; i < this.LayoutLines.Count; i++)
492             {
493                 double height = this.LayoutLines.GetLayout(i).Height;
494
495                 lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(i);
496                 lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
497
498                 if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex))
499                     continue;
500
501                 if (y + height > p.Y)
502                 {
503                     tp.row = i;
504                     break;
505                 }
506                 y += height;
507             }
508
509             if (p.X < this.render.TextArea.X)
510                 return tp;
511
512             tp.col = GetIndexFromColPostion(tp.row, p.X);
513
514             lineLength = this.LayoutLines.GetLengthFromLineNumber(tp.row);
515             if (tp.col > lineLength)
516                 tp.col = lineLength;
517
518             return tp;
519         }
520
521         /// <summary>
522         /// 桁方向の座標に対応するインデックスを取得する
523         /// </summary>
524         /// <param name="row">対象となる行</param>
525         /// <param name="x">テキストエリアからの相対位置</param>
526         /// <returns></returns>
527         public int GetIndexFromColPostion(int row, double x)
528         {
529             x -= this.render.TextArea.X;
530             int lineLength = this.LayoutLines.GetLengthFromLineNumber(row);
531             if (lineLength == 0)
532                 return 0;
533             int index = this.LayoutLines.GetLayout(row).GetIndexFromColPostion(this.Src.X + x);
534             return index;
535         }
536
537         /// <summary>
538         /// インデックスに対応する桁方向の座標を得る
539         /// </summary>
540         /// <param name="row">対象となる行</param>
541         /// <param name="index">インデックス</param>
542         /// <returns>テキストエリアからの相対位置を返す</returns>
543         public double GetColPostionFromIndex(int row, int index)
544         {
545             double x = this.LayoutLines.GetLayout(row).GetColPostionFromIndex(index);
546             return x - Src.X + this.render.TextArea.X;
547         }
548
549         /// <summary>
550         /// TextPointに対応する座標を得る
551         /// </summary>
552         /// <param name="tp">レイアウトライン上の位置</param>
553         /// <returns>テキストエリアを左上とする相対位置</returns>
554         public Point GetPostionFromTextPoint(TextPoint tp)
555         {
556             Point p = new Point();
557             for (int i = this.Src.Row; i < tp.row; i++)
558             {
559                 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(i);
560                 int lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
561                 if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex))
562                     continue;
563                 p.Y += this.LayoutLines.GetLayout(i).Height;
564             }
565             p.X = this.GetColPostionFromIndex(tp.row, tp.col);
566             p.Y += this.render.TextArea.Y;
567             return p;
568         }
569
570         public Gripper HitGripperFromPoint(Point p)
571         {
572             if (this.Document.SelectGrippers.BottomLeft.IsHit(p))
573                 return this.Document.SelectGrippers.BottomLeft;
574             if (this.Document.SelectGrippers.BottomRight.IsHit(p))
575                 return this.Document.SelectGrippers.BottomRight;
576             return null;
577         }
578
579         public Rectangle GetRectFromIndex(int index,int width,int height)
580         {
581             TextPoint tp = this.LayoutLines.GetTextPointFromIndex(index);
582             return this.GetRectFromTextPoint(tp, width, height);
583         }
584
585         public Rectangle GetRectFromTextPoint(TextPoint tp, int width, int height)
586         {
587             double radius = width / 2;
588             Point point = this.GetPostionFromTextPoint(tp);
589             double lineHeight = this.LayoutLines.GetLayout(tp.row).Height;
590             double srcOffsetY = this.Src.GetOffsetY(this.render.emSize.Height); //画面上ではずれているので引く必要がある
591
592             return new Rectangle(point.X - radius, point.Y + lineHeight - srcOffsetY, width, height);
593         }
594
595         /// <summary>
596         /// キャレットを指定した位置に移動させる
597         /// </summary>
598         /// <param name="row"></param>
599         /// <param name="col"></param>
600         /// <param name="autoExpand">折り畳みを展開するなら真</param>
601         public void JumpCaret(int row, int col, bool autoExpand = true)
602         {
603             if (autoExpand)
604             {
605                 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(row);
606                 int lineLength = this.LayoutLines.GetLengthFromLineNumber(row);
607                 FoldingItem foldingData = this.LayoutLines.FoldingCollection.Get(lineHeadIndex, lineLength);
608                 if(foldingData != null)
609                 {
610                     if (this.LayoutLines.FoldingCollection.IsParentHidden(foldingData) || !foldingData.IsFirstLine(this.LayoutLines, row))
611                     {
612                         this.LayoutLines.FoldingCollection.Expand(foldingData);
613                     }
614                 }
615             }
616
617             this.Document.CaretPostion = new TextPoint(row, col);
618         }
619
620         /// <summary>
621         /// index上の文字が表示されるようにSrcを調整する
622         /// </summary>
623         /// <param name="index">インデックス</param>
624         /// <returns>調整されたら真。そうでなければ偽</returns>
625         public bool AdjustSrc(int index)
626         {
627             TextPoint startTextPoint = this.GetLayoutLineFromIndex(index);
628             double x = this.LayoutLines.GetLayout(startTextPoint.row).GetColPostionFromIndex(startTextPoint.col);
629             if (x < this.Src.X ||
630                 x > this.Src.X + this.PageBound.Width)
631             {
632                 this.TryScroll(x, this.Src.Row);
633                 return true;
634             }
635             if (startTextPoint.row < this.Src.Row ||
636                 startTextPoint.row > this.Src.Row + this.LineCountOnScreenWithInVisible)
637             {
638                 this.TryScroll(this.Src.X, startTextPoint.row);
639                 return true;
640             }
641             return false;
642         }
643
644         /// <summary>
645         /// キャレットがあるところまでスクロールする
646         /// </summary>
647         /// <return>再描写する必要があるなら真を返す</return>
648         /// <remarks>Document.Update(type == UpdateType.Clear)イベント時に呼び出した場合、例外が発生します</remarks>
649         public bool AdjustCaretAndSrc(AdjustFlow flow = AdjustFlow.Both)
650         {
651             IEditorRender render = (IEditorRender)base.render;
652
653             if (this.PageBound.Width == 0 || this.PageBound.Height == 0)
654             {
655                 this.SetCaretPostion(this.Padding.Left + render.FoldingWidth, 0);
656                 return false;
657             }
658
659             bool result = false;
660             TextPoint tp = this.Document.CaretPostion;
661             double x = this.CaretLocation.X;
662             double y = this.CaretLocation.Y;
663
664             if (flow == AdjustFlow.Col || flow == AdjustFlow.Both)
665             {
666                 x = this.LayoutLines.GetLayout(tp.row).GetColPostionFromIndex(tp.col);
667
668                 double left = this.Src.X;
669                 double right = this.Src.X + this.render.TextArea.Width;
670
671                 if (x >= left && x <= right)    //xは表示領域にないにある
672                 {
673                     x -= left;
674                 }
675                 else if (x > right) //xは表示領域の右側にある
676                 {
677                     this.Document.Src = new SrcPoint(x - this.render.TextArea.Width + this.ScrollMarginWidth,this.Document.Src.Row,this.Document.Src.Y);
678                     if (this.Document.RightToLeft && this.Document.Src.X > 0)
679                     {
680                         System.Diagnostics.Debug.Assert(x > 0);
681                         this.Document.Src = new SrcPoint(0, this.Document.Src.Row, this.Document.Src.Y);
682                     }
683                     else
684                     {
685                         x = this.render.TextArea.Width - this.ScrollMarginWidth;
686                     }
687                     result = true;
688                 }
689                 else if (x < left)    //xは表示領域の左側にある
690                 {
691                     this.Document.Src = new SrcPoint(x - this.ScrollMarginWidth, this.Document.Src.Row, this.Document.Src.Y);
692                     if (!this.Document.RightToLeft && this.Document.Src.X < this.render.TextArea.X)
693                     {
694                         this.Document.Src = new SrcPoint(0, this.Document.Src.Row, this.Document.Src.Y);
695                     }
696                     else
697                     {
698                         x = this.ScrollMarginWidth;
699                     }
700                     result = true;
701                 }
702                 x += this.render.TextArea.X;
703             }
704
705             if (flow == AdjustFlow.Row || flow == AdjustFlow.Both)
706             {
707                 int caretRow = 0;
708                 int lineCount = this.LineCountOnScreenWithInVisible;
709                 if (tp.row >= this.Src.Row && tp.row < this.Src.Row + lineCount)
710                 {
711                     caretRow = tp.row - this.Src.Row;
712                     y = -this.Src.GetOffsetY(this.render.emSize.Height);    //画面上ではずれているので引く必要がある
713                 }
714                 else if (tp.row >= this.Src.Row + lineCount)
715                 {
716                     int srcRow = this.GetSrcRow(tp.row, this.LineCountOnScreen);
717                     this.Document.Src = new SrcPoint(this.Document.Src.X, srcRow, srcRow * this.render.emSize.Height);
718                     caretRow = tp.row - this.Document.Src.Row;
719                     y = 0;
720                     result = true;
721                     CalculateLineCountOnScreen();
722                 }
723                 else if (tp.row < this.Src.Row)
724                 {
725                     this.Document.Src = new SrcPoint(this.Document.Src.X, tp.row, tp.row * this.render.emSize.Height);
726                     y = 0;
727                     result = true;
728                     CalculateLineCountOnScreen();
729                 }
730
731                 if (caretRow > 0)
732                 {
733                     for (int i = 0; i < caretRow; i++)
734                     {
735                         int currentRow = this.Src.Row + i;
736                         int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(currentRow);
737                         int lineLength = this.LayoutLines.GetLengthFromLineNumber(currentRow);
738
739                         if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex))
740                             continue;
741
742                         y += this.LayoutLines.GetLayout(currentRow).Height;
743                     }
744                 }
745                 y += this.render.TextArea.Y;
746             }
747
748             this.SetCaretPostion(x, y);
749
750             if (result)
751             {
752                 this.OnSrcChanged(null);
753             }
754
755             return result;
756         }
757
758         int GetSrcRow(int row,int count)
759         {
760             if (this.LayoutLines.FoldingStrategy == null)
761                 return row - count;
762             for (int i = row; i >= 0; i--)
763             {
764                 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(i);
765                 int lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
766                 if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex))
767                     continue;
768                 if (count <= 0)
769                     return i;
770                 count--;
771             }
772             return 0;
773         }
774
775         /// <summary>
776         /// レイアウト行をテキストポイントからインデックスに変換する
777         /// </summary>
778         /// <param name="tp">テキストポイント表す</param>
779         /// <returns>インデックスを返す</returns>
780         public int GetIndexFromLayoutLine(TextPoint tp)
781         {
782             return this.LayoutLines.GetIndexFromTextPoint(tp);
783         }
784
785         /// <summary>
786         /// インデックスからレイアウト行を指し示すテキストポイントに変換する
787         /// </summary>
788         /// <param name="index">インデックスを表す</param>
789         /// <returns>テキストポイント返す</returns>
790         public TextPoint GetLayoutLineFromIndex(int index)
791         {
792             return this.LayoutLines.GetTextPointFromIndex(index);
793         }
794
795         /// <summary>
796         /// 指定した座標までスクロールする
797         /// </summary>
798         /// <param name="x"></param>
799         /// <param name="row"></param>
800         /// <remarks>
801         /// 範囲外の座標を指定した場合、範囲内に収まるように調整されます
802         /// </remarks>
803         public void Scroll(double x, int row)
804         {
805             if (x < 0)
806                 x = 0;
807             if(row < 0)
808                 row = 0;
809             int endRow = this.LayoutLines.Count - 1 - this.LineCountOnScreen;
810             if (endRow < 0)
811                 endRow = 0;
812             if (row > endRow)
813                 row = endRow;
814             base.TryScroll(x, row);
815         }
816
817         /// <summary>
818         /// 指定した座標までスクロールする
819         /// </summary>
820         /// <param name="x"></param>
821         /// <param name="row"></param>
822         /// <remarks>
823         /// 範囲外の座標を指定した場合、範囲内に収まるように調整されます
824         /// </remarks>
825         public void Scroll(double x, double y)
826         {
827             if (x < 0)
828                 x = 0;
829             if (y < 0)
830                 y = 0;
831             double totalHeight = this.LayoutLines.Count * this.render.emSize.Height;
832             if (y > totalHeight)
833                 y = totalHeight - this.render.TextArea.Height;
834             base.TryScroll(x, y);
835         }
836
837         /// <summary>
838         /// 指定行までスクロールする
839         /// </summary>
840         /// <param name="row">行</param>
841         /// <param name="alignTop">指定行を画面上に置くなら真。そうでないなら偽</param>
842         public void ScrollIntoView(int row, bool alignTop)
843         {
844             this.Scroll(0, row);
845             if (alignTop)
846                 return;
847             double y = this.render.TextArea.Height;
848             for (int i = row; i >= 0; i--)
849             {
850                 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(i);
851                 int lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
852                 double height = this.LayoutLines.GetLayout(i).Height;
853                 if (y - height <= 0)
854                 {
855                     this.Scroll(0, i);
856                 }
857                 if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex))
858                     continue;
859                 y -= height;
860             }
861         }
862
863         public int AdjustRow(int row, bool isMoveNext)
864         {
865             if (this.LayoutLines.FoldingStrategy == null)
866                 return row;
867             int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(row);
868             int lineLength = this.LayoutLines.GetLengthFromLineNumber(row);
869             FoldingItem foldingData = this.LayoutLines.FoldingCollection.GetFarestHiddenFoldingData(lineHeadIndex, lineLength);
870             if (foldingData != null && !foldingData.Expand)
871             {
872                 if (foldingData.End == this.Document.Length)
873                     return row;
874                 if (isMoveNext && lineHeadIndex > foldingData.Start)
875                     row = this.LayoutLines.GetLineNumberFromIndex(foldingData.End) + 1;
876                 else
877                     row = this.LayoutLines.GetLineNumberFromIndex(foldingData.Start);
878                 if(row > this.LayoutLines.Count - 1)
879                     row = this.LayoutLines.GetLineNumberFromIndex(foldingData.Start);
880             }
881             return row;
882         }
883
884         protected override void CalculateClipRect()
885         {
886             IEditorRender render = (IEditorRender)base.render;
887             double x, y, width, height;
888
889             if (this.Document.DrawLineNumber)
890             {
891                 if (this.Document.RightToLeft)
892                     x = this.Padding.Left;
893                 else
894                     x = this.Padding.Left + UpdateAreaTotalWidth + this.render.LineNemberWidth + this.LineNumberMargin + render.FoldingWidth;
895                 width = this.PageBound.Width - this.render.LineNemberWidth - this.LineNumberMargin - this.Padding.Left - this.Padding.Right - render.FoldingWidth - UpdateAreaTotalWidth;
896             }
897             else
898             {
899                 if (this.Document.RightToLeft)
900                     x = this.Padding.Left;
901                 else
902                     x = this.Padding.Left + UpdateAreaTotalWidth + render.FoldingWidth;
903                 width = this.PageBound.Width - this.Padding.Left - this.Padding.Right - render.FoldingWidth - UpdateAreaTotalWidth;
904             }
905
906             y = this.Padding.Top;
907             height = this.PageBound.Height - this.Padding.Top - this.Padding.Bottom;
908
909             if (this.Document.HideRuler == false)
910             {
911                 double rulerHeight = this.render.emSize.Height + LineMarkerThickness;
912                 y += rulerHeight;
913                 height -= rulerHeight;
914             }
915
916             if (width < 0)
917                 width = 0;
918
919             if (height < 0)
920                 height = 0;
921
922             this.render.TextArea = new Rectangle(x, y, width, height);
923
924             this.LineBreakingMarginWidth = width * 5 / 100;
925         }
926
927         public override void CalculateLineCountOnScreen()
928         {
929             if (this.LayoutLines.Count == 0 || this.PageBound.Height == 0)
930                 return;
931
932             double y = 0;
933             int i = this.Src.Row;
934             int visualCount = this.Src.Row;
935             for (; true; i++)
936             {
937                 int row = i < this.LayoutLines.Count ? i : this.LayoutLines.Count - 1;
938
939                 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(row);
940                 int lineLength = this.LayoutLines.GetLengthFromLineNumber(row);
941
942                 if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex) && row < this.LayoutLines.Count - 1)
943                     continue;
944
945                 ITextLayout layout = this.LayoutLines.GetLayout(row);
946
947                 double width = layout.Width;
948
949                 if (width > this._LongestWidth)
950                     this._LongestWidth = width;
951
952                 double lineHeight = layout.Height;
953
954                 y += lineHeight;
955
956                 if (y >= this.render.TextArea.Height)
957                     break;
958                 visualCount++;
959             }
960             this.LineCountOnScreen = Math.Max(visualCount - this.Src.Row - 1, 0);
961             this.LineCountOnScreenWithInVisible = Math.Max(i - this.Src.Row - 1, 0);
962         }
963
964         void SetCaretPostion(double x, double y)
965         {
966             this.CaretLocation = new Point(x + this.PageBound.X, y + this.PageBound.Y);
967         }
968
969         void FoldingCollection_StatusChanged(object sender, FoldingItemStatusChangedEventArgs e)
970         {
971             this.CalculateLineCountOnScreen();
972         }
973
974         enum AreaType
975         {
976             UpdateArea,
977             FoldingArea,
978             LineNumberArea,
979             TextArea
980         }
981
982         double GetRealtiveX(AreaType type)
983         {
984             IEditorRender render = (IEditorRender)base.render;
985             switch (type)
986             {
987                 case AreaType.UpdateArea:
988                     if (this.Document.RightToLeft)
989                         return this.PageBound.TopRight.X - UpdateAreaTotalWidth;
990                     if (this.Document.DrawLineNumber)
991                         return this.render.TextArea.X - this.render.LineNemberWidth - this.LineNumberMargin - render.FoldingWidth - UpdateAreaTotalWidth;
992                     else
993                         return this.render.TextArea.X - render.FoldingWidth - UpdateAreaTotalWidth;
994                 case AreaType.FoldingArea:
995                     if (this.Document.RightToLeft)
996                         return this.PageBound.TopRight.X - render.FoldingWidth;
997                     if (this.Document.DrawLineNumber)
998                         return this.render.TextArea.X - this.render.LineNemberWidth - this.LineNumberMargin - render.FoldingWidth;
999                     else
1000                         return this.render.TextArea.X - render.FoldingWidth;
1001                 case AreaType.LineNumberArea:
1002                     if (this.Document.DrawLineNumber == false)
1003                         throw new InvalidOperationException();
1004                     if (this.Document.RightToLeft)
1005                         return this.PageBound.TopRight.X - UpdateAreaTotalWidth - render.FoldingWidth - this.render.LineNemberWidth;
1006                     else
1007                         return this.render.TextArea.X - this.render.LineNemberWidth - this.LineNumberMargin;
1008                 case AreaType.TextArea:
1009                     return this.render.TextArea.X;
1010             }
1011             throw new ArgumentOutOfRangeException();
1012         }
1013     }
1014 }