OSDN Git Service

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