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/>.
13 using System.Collections.Generic;
15 namespace FooEditEngine
24 enum TextPointSearchRange
31 /// キャレットとドキュメントの表示を担当します。レイアウト関連もこちらで行います
33 sealed class EditView : ViewBase
36 internal const float LineMarkerThickness = 2;
39 internal const int LineNumberLength = 6;
40 const int UpdateAreaPaddingWidth = 2;
41 const int UpdateAreaWidth = 4;
42 const int UpdateAreaTotalWidth = UpdateAreaWidth + UpdateAreaPaddingWidth;
47 public EditView(Document doc, IEditorRender r, int MarginLeftAndRight = 5)
48 : this(doc, r, new Padding(MarginLeftAndRight, 0, MarginLeftAndRight, 0))
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)
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;
72 internal SelectCollection Selections
74 get { return this.Document.Selections; }
75 set { this.Document.Selections = value; }
79 /// ラインマーカーを描くなら偽。そうでなければ真
81 public bool HideLineMarker
83 get { return this.Document.HideLineMarker; }
84 set { this.Document.HideLineMarker = value; }
88 /// キャレットを描くなら偽。そうでなければ真
92 get { return this.Document.HideCaret; }
93 set { this.Document.HideCaret = value; }
97 /// 挿入モードなら真を返し、上書きモードなら偽を返す
99 public bool InsertMode
101 get { return this.Document.InsertMode; }
102 set { this.Document.InsertMode = value; }
108 public int CaretBlinkTime
117 public double CaretWidthOnInsertMode
124 /// フォーカスがあるなら真をセットする
126 public bool IsFocused
133 /// キャレットを点滅させるなら真。そうでないなら偽
135 /// <remarks>キャレット点滅タイマーもリセットされます</remarks>
136 public bool CaretBlink
138 get { return this._CaretBlink; }
141 this._CaretBlink = value;
143 this.tickCount = DateTime.Now.Ticks + this.To100nsTime(this.CaretBlinkTime);
148 /// 一ページの高さに収まる行数を返す(こちらは表示されていない行も含みます)
150 public int LineCountOnScreenWithInVisible
159 public double ScrollMarginWidth
161 get { return this.PageBound.Width * 20 / 100; }
167 public Point CaretLocation
176 /// <param name="x">x座標</param>
177 /// <param name="y">y座標</param>
178 /// <returns>テキストエリア内にあれば真。そうでなければ偽</returns>
179 public bool HitTextArea(double x, double y)
181 if (x >= this.render.TextArea.X && x <= this.render.TextArea.Right &&
182 y >= this.render.TextArea.Y && y <= this.render.TextArea.Bottom)
188 public bool IsUpperTextArea(double x, double y)
190 if (x >= this.render.TextArea.X && x <= this.render.TextArea.Right && y < this.render.TextArea.Y)
196 public bool IsUnderTextArea(double x,double y)
198 if (x >= this.render.TextArea.X && x <= this.render.TextArea.Right && y > this.render.TextArea.Bottom)
207 /// <param name="x">x座標</param>
208 /// <param name="row">行</param>
209 /// <returns>ヒットした場合はFoldingDataオブジェクトが返され、そうでない場合はnullが返る</returns>
210 public FoldingItem HitFoldingData(double x, int row)
212 IEditorRender render = (IEditorRender)base.render;
214 if (x >= this.GetRealtiveX(AreaType.FoldingArea) && x <= this.GetRealtiveX(AreaType.FoldingArea) + render.FoldingWidth)
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))
226 /// Rectで指定された範囲にドキュメントを描く
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)
233 if (this.LayoutLines.Count == 0)
236 IEditorRender render = (IEditorRender)base.render;
238 if ((updateRect.Height < this.PageBound.Height ||
239 updateRect.Width < this.PageBound.Width) &&
240 render.IsVaildCache() &&
243 render.DrawCachedBitmap(updateRect);
247 Rectangle background = this.PageBound;
248 render.FillBackground(background);
250 if (this.Document.HideRuler == false)
253 this.DrawLineMarker(this.Document.CaretPostion.row);
255 Point pos = this.render.TextArea.TopLeft;
256 //画面上では(X,Y)が開始位置になるが、開始する行が決まっているのでオフセットを求める
257 pos.Y -= this.Src.GetOffsetY(this.render.emSize.Height);
259 double endposy = this.render.TextArea.Bottom;
260 Size lineNumberSize = new Size(this.render.LineNemberWidth, this.render.TextArea.Height);
262 this.render.BeginClipRect(new Rectangle(this.PageBound.X, this.render.TextArea.Y, this.PageBound.Width, this.render.TextArea.Height));
264 //パフォーマンス向上のため行番号などを先に描く
265 for (int i = this.Src.Row; i < this.LayoutLines.Count; i++)
267 int lineIndex = this.LayoutLines.GetIndexFromLineNumber(i);
268 int lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
269 ITextLayout layout = this.LayoutLines.GetLayout(i);
274 FoldingItem foldingData = this.LayoutLines.FoldingCollection.Get(lineIndex, lineLength);
276 if (foldingData != null)
278 if (this.LayoutLines.FoldingCollection.IsHidden(lineIndex))
280 if (foldingData.IsFirstLine(this.LayoutLines, i))
281 render.DrawFoldingMark(foldingData.Expand, this.PageBound.X + this.GetRealtiveX(AreaType.FoldingArea), pos.Y);
284 if (this.Document.DrawLineNumber)
286 this.render.DrawString((i + 1).ToString(), this.PageBound.X + this.GetRealtiveX(AreaType.LineNumberArea), pos.Y, StringAlignment.Right, lineNumberSize,StringColorType.LineNumber);
289 DrawUpdateArea(i, pos.Y);
291 pos.Y += layout.Height;
292 //pos.Y += this.render.emSize.Height;
295 this.render.EndClipRect();
298 pos = this.render.TextArea.TopLeft;
300 pos.Y -= this.Src.GetOffsetY(this.render.emSize.Height);
302 this.render.BeginClipRect(this.render.TextArea);
304 for (int i = this.Src.Row; i < this.LayoutLines.Count; i++)
306 int lineIndex = this.LayoutLines.GetIndexFromLineNumber(i);
307 int lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
308 ITextLayout layout = this.LayoutLines.GetLayout(i);
313 FoldingItem foldingData = this.LayoutLines.FoldingCollection.Get(lineIndex, lineLength);
315 if (foldingData != null)
317 if (this.LayoutLines.FoldingCollection.IsHidden(lineIndex))
321 this.render.DrawOneLine(this.Document, this.LayoutLines, i, pos.X, pos.Y);
323 pos.Y += layout.Height;
324 //pos.Y += this.render.emSize.Height;
327 this.render.EndClipRect();
329 this.DrawInsertPoint();
331 this.Document.SelectGrippers.BottomLeft.Draw(this.render);
332 this.Document.SelectGrippers.BottomRight.Draw(this.render);
334 render.CacheContent();
340 void DrawUpdateArea(int row,double ypos)
342 IEditorRender render = (IEditorRender)base.render;
343 if(this.LayoutLines.GetDirtyFlag(row))
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);
353 IEditorRender render = (IEditorRender)base.render;
356 Size emSize = render.emSize;
357 Rectangle clipRect = this.render.TextArea;
359 double markerHeight = emSize.Height / 2;
360 if (this.Document.RightToLeft)
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++)
366 to = new Point(pos.X, pos.Y + emSize.Height);
367 int mod = count % 10;
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));
375 from.Y = from.Y + emSize.Height / 2;
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);
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++)
389 to = new Point(pos.X, pos.Y + emSize.Height);
390 int mod = count % 10;
392 this.render.DrawString((count / 10).ToString(), pos.X, pos.Y, StringAlignment.Left, new Size(double.MaxValue, double.MaxValue));
394 from.Y = from.Y + emSize.Height / 2;
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);
402 from = clipRect.TopLeft;
403 from.Y -= LineMarkerThickness;
404 to = clipRect.TopRight;
405 to.Y -= LineMarkerThickness;
406 render.DrawLine(from, to);
409 void DrawInsertPoint()
411 //一つしかない場合は行選択の可能性がある
412 if (this.Selections.Count <= 1)
414 IEditorRender render = (IEditorRender)base.render;
415 foreach (Selection sel in this.Selections)
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,
424 CaretWidthOnInsertMode,
426 render.FillRectangle(InsertRect, FillRectType.InsertPoint);
433 if (this.HideCaret || !this.IsFocused)
436 long diff = DateTime.Now.Ticks - this.tickCount;
437 long blinkTime = this.To100nsTime(this.CaretBlinkTime);
439 if (this.CaretBlink && diff % blinkTime >= blinkTime / 2)
442 Rectangle CaretRect = new Rectangle();
444 IEditorRender render = (IEditorRender)base.render;
446 int row = this.Document.CaretPostion.row;
447 ITextLayout layout = this.LayoutLines.GetLayout(row);
448 double lineHeight = layout.Height;
449 double charWidth = layout.GetWidthFromIndex(this.Document.CaretPostion.col);
451 if (this.InsertMode || charWidth == 0)
453 CaretRect.Size = new Size(CaretWidthOnInsertMode, lineHeight);
454 CaretRect.Location = new Point(this.CaretLocation.X, this.CaretLocation.Y);
455 render.FillRectangle(CaretRect, FillRectType.InsertCaret);
459 double height = lineHeight / 3;
460 CaretRect.Size = new Size(charWidth, height);
461 CaretRect.Location = new Point(this.CaretLocation.X, this.CaretLocation.Y + lineHeight - height);
462 render.FillRectangle(CaretRect, FillRectType.OverwriteCaret);
467 long To100nsTime(int ms)
472 public void DrawLineMarker(int row)
474 if (this.HideLineMarker || !this.IsFocused)
476 IEditorRender render = (IEditorRender)base.render;
477 Point p = this.CaretLocation;
478 double height = this.LayoutLines.GetLayout(this.Document.CaretPostion.row).Height;
479 double width = this.render.TextArea.Width;
480 render.FillRectangle(new Rectangle(this.PageBound.X + this.render.TextArea.X, this.CaretLocation.Y, width, height), FillRectType.LineMarker);
486 /// <returns>矩形領域を表すRectangle</returns>
487 public Rectangle GetCurrentCaretRect()
489 ITextLayout layout = this.LayoutLines.GetLayout(this.Document.CaretPostion.row);
490 double width = layout.GetWidthFromIndex(this.Document.CaretPostion.col);
492 width = this.CaretWidthOnInsertMode;
493 double height = layout.Height;
494 Rectangle updateRect = new Rectangle(
495 this.CaretLocation.X,
496 this.CaretLocation.Y,
503 /// 指定した座標の一番近くにあるTextPointを取得する
505 /// <param name="p">テキストエリアを左上とする相対位置</param>
506 /// <param name="searchRange">探索範囲</param>
507 /// <returns>レイアウトラインを指し示すTextPoint</returns>
508 public TextPoint GetTextPointFromPostion(Point p,TextPointSearchRange searchRange = TextPointSearchRange.TextAreaOnly)
510 if(searchRange == TextPointSearchRange.TextAreaOnly)
512 if (p.Y < this.render.TextArea.TopLeft.Y ||
513 p.Y > this.render.TextArea.BottomRight.Y)
514 return TextPoint.Null;
517 TextPoint tp = new TextPoint();
519 if (this.LayoutLines.Count == 0)
522 //表示領域から探索を始めるのでパディングの分だけ引く
523 p.Y -= this.render.TextArea.Y;
525 int lineHeadIndex, lineLength;
530 tp.row = this.LayoutLines.Count - 1;
531 for (int i = this.Src.Row; i < this.LayoutLines.Count; i++)
533 double height = this.LayoutLines.GetLayout(i).Height;
535 lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(i);
536 lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
538 if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex))
541 if (y + height > p.Y)
551 for (int i = this.Src.Row; i >= 0; i--)
553 double height = this.LayoutLines.GetLayout(i).Height;
555 lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(i);
556 lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
558 if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex))
561 if (y - height < p.Y)
570 if (searchRange == TextPointSearchRange.TextAreaOnly)
572 if (p.X < this.render.TextArea.X)
576 tp.col = GetIndexFromColPostion(tp.row, p.X);
578 lineLength = this.LayoutLines.GetLengthFromLineNumber(tp.row);
579 if (tp.col > lineLength)
586 /// 桁方向の座標に対応するインデックスを取得する
588 /// <param name="row">対象となる行</param>
589 /// <param name="x">テキストエリアからの相対位置</param>
590 /// <returns></returns>
591 public int GetIndexFromColPostion(int row, double x)
593 x -= this.render.TextArea.X;
594 int lineLength = this.LayoutLines.GetLengthFromLineNumber(row);
597 int index = this.LayoutLines.GetLayout(row).GetIndexFromColPostion(this.Src.X + x);
602 /// インデックスに対応する桁方向の座標を得る
604 /// <param name="row">対象となる行</param>
605 /// <param name="index">インデックス</param>
606 /// <returns>テキストエリアからの相対位置を返す</returns>
607 public double GetColPostionFromIndex(int row, int index)
609 double x = this.LayoutLines.GetLayout(row).GetColPostionFromIndex(index);
610 return x - Src.X + this.render.TextArea.X;
614 /// TextPointに対応する座標を得る
616 /// <param name="tp">レイアウトライン上の位置</param>
617 /// <returns>テキストエリアを左上とする相対位置</returns>
618 public Point GetPostionFromTextPoint(TextPoint tp)
620 Point p = new Point();
621 for (int i = this.Src.Row; i < tp.row; i++)
623 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(i);
624 int lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
625 if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex))
627 p.Y += this.LayoutLines.GetLayout(i).Height;
629 p.X = this.GetColPostionFromIndex(tp.row, tp.col);
630 p.Y += this.render.TextArea.Y;
634 public Gripper HitGripperFromPoint(Point p)
636 if (this.Document.SelectGrippers.BottomLeft.IsHit(p))
637 return this.Document.SelectGrippers.BottomLeft;
638 if (this.Document.SelectGrippers.BottomRight.IsHit(p))
639 return this.Document.SelectGrippers.BottomRight;
643 public Rectangle GetRectFromIndex(int index,int width,int height)
645 TextPoint tp = this.LayoutLines.GetTextPointFromIndex(index);
646 return this.GetRectFromTextPoint(tp, width, height);
649 public Rectangle GetRectFromTextPoint(TextPoint tp, int width, int height)
651 if (tp.row < this.Src.Row)
652 return Rectangle.Empty;
653 double radius = width / 2;
654 Point point = this.GetPostionFromTextPoint(tp);
655 double lineHeight = this.LayoutLines.GetLayout(tp.row).Height;
656 double srcOffsetY = this.Src.GetOffsetY(this.render.emSize.Height); //画面上ではずれているので引く必要がある
658 Rectangle rect = new Rectangle(point.X - radius, point.Y + lineHeight - srcOffsetY, width, height);
660 if (rect.BottomLeft.Y >= this.render.TextArea.BottomLeft.Y ||
661 rect.BottomRight.X < this.render.TextArea.BottomLeft.X ||
662 rect.BottomLeft.X > this.render.TextArea.BottomRight.X)
663 return Rectangle.Empty;
668 /// キャレットを指定した位置に移動させる
670 /// <param name="row"></param>
671 /// <param name="col"></param>
672 /// <param name="autoExpand">折り畳みを展開するなら真</param>
673 public void JumpCaret(int row, int col, bool autoExpand = true)
677 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(row);
678 int lineLength = this.LayoutLines.GetLengthFromLineNumber(row);
679 FoldingItem foldingData = this.LayoutLines.FoldingCollection.Get(lineHeadIndex, lineLength);
680 if(foldingData != null)
682 if (this.LayoutLines.FoldingCollection.IsParentHidden(foldingData) || !foldingData.IsFirstLine(this.LayoutLines, row))
684 this.LayoutLines.FoldingCollection.Expand(foldingData);
690 this.Document.SetCaretPostionWithoutEvent(new TextPoint(row, col));
694 /// index上の文字が表示されるようにSrcを調整する
696 /// <param name="index">インデックス</param>
697 /// <returns>調整されたら真。そうでなければ偽</returns>
698 public bool AdjustSrc(int index)
700 TextPoint startTextPoint = this.GetLayoutLineFromIndex(index);
701 double x = this.LayoutLines.GetLayout(startTextPoint.row).GetColPostionFromIndex(startTextPoint.col);
702 if (x < this.Src.X ||
703 x > this.Src.X + this.PageBound.Width)
705 this.TryScroll(x, this.Src.Row);
708 if (startTextPoint.row < this.Src.Row ||
709 startTextPoint.row > this.Src.Row + this.LineCountOnScreenWithInVisible)
711 this.TryScroll(this.Src.X, startTextPoint.row);
718 /// キャレットがあるところまでスクロールする
720 /// <return>再描写する必要があるなら真を返す</return>
721 /// <remarks>Document.Update(type == UpdateType.Clear)イベント時に呼び出した場合、例外が発生します</remarks>
722 public bool AdjustCaretAndSrc(AdjustFlow flow = AdjustFlow.Both)
724 IEditorRender render = (IEditorRender)base.render;
726 if (this.PageBound.Width == 0 || this.PageBound.Height == 0)
728 this.SetCaretPostion(this.Padding.Left + render.FoldingWidth, 0);
733 TextPoint tp = this.Document.CaretPostion;
734 double x = this.CaretLocation.X;
735 double y = this.CaretLocation.Y;
737 if (flow == AdjustFlow.Col || flow == AdjustFlow.Both)
739 x = this.LayoutLines.GetLayout(tp.row).GetColPostionFromIndex(tp.col);
741 double left = this.Src.X;
742 double right = this.Src.X + this.render.TextArea.Width;
744 if (x >= left && x <= right) //xは表示領域にないにある
748 else if (x > right) //xは表示領域の右側にある
750 this.Document.Src = new SrcPoint(x - this.render.TextArea.Width + this.ScrollMarginWidth,this.Document.Src.Row,this.Document.Src.Y);
751 if (this.Document.RightToLeft && this.Document.Src.X > 0)
753 System.Diagnostics.Debug.Assert(x > 0);
754 this.Document.Src = new SrcPoint(0, this.Document.Src.Row, this.Document.Src.Y);
758 x = this.render.TextArea.Width - this.ScrollMarginWidth;
762 else if (x < left) //xは表示領域の左側にある
764 this.Document.Src = new SrcPoint(x - this.ScrollMarginWidth, this.Document.Src.Row, this.Document.Src.Y);
765 if (!this.Document.RightToLeft && this.Document.Src.X < this.render.TextArea.X)
767 this.Document.Src = new SrcPoint(0, this.Document.Src.Row, this.Document.Src.Y);
771 x = this.ScrollMarginWidth;
775 x += this.render.TextArea.X;
778 if (flow == AdjustFlow.Row || flow == AdjustFlow.Both)
781 int lineCount = this.LineCountOnScreenWithInVisible;
782 if (tp.row >= this.Src.Row && tp.row < this.Src.Row + lineCount)
784 caretRow = tp.row - this.Src.Row;
785 y = -this.Src.GetOffsetY(this.render.emSize.Height); //画面上ではずれているので引く必要がある
787 else if (tp.row >= this.Src.Row + lineCount)
789 int srcRow = this.GetSrcRow(tp.row, this.LineCountOnScreen);
790 this.Document.Src = new SrcPoint(this.Document.Src.X, srcRow, srcRow * this.render.emSize.Height);
791 caretRow = tp.row - this.Document.Src.Row;
794 CalculateLineCountOnScreen();
796 else if (tp.row < this.Src.Row)
798 this.Document.Src = new SrcPoint(this.Document.Src.X, tp.row, tp.row * this.render.emSize.Height);
801 CalculateLineCountOnScreen();
806 for (int i = 0; i < caretRow; i++)
808 int currentRow = this.Src.Row + i;
809 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(currentRow);
810 int lineLength = this.LayoutLines.GetLengthFromLineNumber(currentRow);
812 if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex))
815 y += this.LayoutLines.GetLayout(currentRow).Height;
818 y += this.render.TextArea.Y;
821 this.SetCaretPostion(x, y);
825 this.OnSrcChanged(null);
831 int GetSrcRow(int row,int count)
833 if (this.LayoutLines.FoldingStrategy == null)
835 for (int i = row; i >= 0; i--)
837 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(i);
838 int lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
839 if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex))
849 /// レイアウト行をテキストポイントからインデックスに変換する
851 /// <param name="tp">テキストポイント表す</param>
852 /// <returns>インデックスを返す</returns>
853 public int GetIndexFromLayoutLine(TextPoint tp)
855 return this.LayoutLines.GetIndexFromTextPoint(tp);
859 /// インデックスからレイアウト行を指し示すテキストポイントに変換する
861 /// <param name="index">インデックスを表す</param>
862 /// <returns>テキストポイント返す</returns>
863 public TextPoint GetLayoutLineFromIndex(int index)
865 return this.LayoutLines.GetTextPointFromIndex(index);
871 /// <param name="x"></param>
872 /// <param name="row"></param>
874 /// 範囲外の座標を指定した場合、範囲内に収まるように調整されます
876 public void Scroll(double x, int row)
882 int endRow = this.LayoutLines.Count - 1 - this.LineCountOnScreen;
887 base.TryScroll(x, row);
893 /// <param name="row">行</param>
894 /// <param name="alignTop">指定行を画面上に置くなら真。そうでないなら偽</param>
895 public void ScrollIntoView(int row, bool alignTop)
900 double y = this.render.TextArea.Height;
901 for (int i = row; i >= 0; i--)
903 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(i);
904 int lineLength = this.LayoutLines.GetLengthFromLineNumber(i);
905 double height = this.LayoutLines.GetLayout(i).Height;
910 if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex))
916 public int AdjustRow(int row, bool isMoveNext)
918 if (this.LayoutLines.FoldingStrategy == null)
920 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(row);
921 int lineLength = this.LayoutLines.GetLengthFromLineNumber(row);
922 FoldingItem foldingData = this.LayoutLines.FoldingCollection.GetFarestHiddenFoldingData(lineHeadIndex, lineLength);
923 if (foldingData != null && !foldingData.Expand)
925 if (foldingData.End == this.Document.Length)
927 if (isMoveNext && lineHeadIndex > foldingData.Start)
928 row = this.LayoutLines.GetLineNumberFromIndex(foldingData.End) + 1;
930 row = this.LayoutLines.GetLineNumberFromIndex(foldingData.Start);
931 if(row > this.LayoutLines.Count - 1)
932 row = this.LayoutLines.GetLineNumberFromIndex(foldingData.Start);
937 protected override void CalculateClipRect()
939 IEditorRender render = (IEditorRender)base.render;
940 double x, y, width, height;
942 if (this.Document.DrawLineNumber)
944 if (this.Document.RightToLeft)
945 x = this.Padding.Left;
947 x = this.Padding.Left + UpdateAreaTotalWidth + this.render.LineNemberWidth + this.LineNumberMargin + render.FoldingWidth;
948 width = this.PageBound.Width - this.render.LineNemberWidth - this.LineNumberMargin - this.Padding.Left - this.Padding.Right - render.FoldingWidth - UpdateAreaTotalWidth;
952 if (this.Document.RightToLeft)
953 x = this.Padding.Left;
955 x = this.Padding.Left + UpdateAreaTotalWidth + render.FoldingWidth;
956 width = this.PageBound.Width - this.Padding.Left - this.Padding.Right - render.FoldingWidth - UpdateAreaTotalWidth;
959 y = this.Padding.Top;
960 height = this.PageBound.Height - this.Padding.Top - this.Padding.Bottom;
962 if (this.Document.HideRuler == false)
964 double rulerHeight = this.render.emSize.Height + LineMarkerThickness;
966 height -= rulerHeight;
975 this.render.TextArea = new Rectangle(x, y, width, height);
977 this.LineBreakingMarginWidth = width * 5 / 100;
980 public override void CalculateLineCountOnScreen()
982 if (this.LayoutLines.Count == 0 || this.PageBound.Height == 0)
986 int i = this.Src.Row;
987 int visualCount = this.Src.Row;
990 int row = i < this.LayoutLines.Count ? i : this.LayoutLines.Count - 1;
992 int lineHeadIndex = this.LayoutLines.GetIndexFromLineNumber(row);
993 int lineLength = this.LayoutLines.GetLengthFromLineNumber(row);
995 if (this.LayoutLines.FoldingCollection.IsHidden(lineHeadIndex) && row < this.LayoutLines.Count - 1)
998 ITextLayout layout = this.LayoutLines.GetLayout(row);
1000 double width = layout.Width;
1002 if (width > this._LongestWidth)
1003 this._LongestWidth = width;
1005 double lineHeight = layout.Height;
1009 if (y >= this.render.TextArea.Height)
1013 this.LineCountOnScreen = Math.Max(visualCount - this.Src.Row - 1, 0);
1014 this.LineCountOnScreenWithInVisible = Math.Max(i - this.Src.Row - 1, 0);
1017 void SetCaretPostion(double x, double y)
1019 this.CaretLocation = new Point(x + this.PageBound.X, y + this.PageBound.Y);
1022 void FoldingCollection_StatusChanged(object sender, FoldingItemStatusChangedEventArgs e)
1024 this.CalculateLineCountOnScreen();
1035 double GetRealtiveX(AreaType type)
1037 IEditorRender render = (IEditorRender)base.render;
1040 case AreaType.UpdateArea:
1041 if (this.Document.RightToLeft)
1042 return this.PageBound.TopRight.X - UpdateAreaTotalWidth;
1043 if (this.Document.DrawLineNumber)
1044 return this.render.TextArea.X - this.render.LineNemberWidth - this.LineNumberMargin - render.FoldingWidth - UpdateAreaTotalWidth;
1046 return this.render.TextArea.X - render.FoldingWidth - UpdateAreaTotalWidth;
1047 case AreaType.FoldingArea:
1048 if (this.Document.RightToLeft)
1049 return this.PageBound.TopRight.X - render.FoldingWidth;
1050 if (this.Document.DrawLineNumber)
1051 return this.render.TextArea.X - this.render.LineNemberWidth - this.LineNumberMargin - render.FoldingWidth;
1053 return this.render.TextArea.X - render.FoldingWidth;
1054 case AreaType.LineNumberArea:
1055 if (this.Document.DrawLineNumber == false)
1056 throw new InvalidOperationException();
1057 if (this.Document.RightToLeft)
1058 return this.PageBound.TopRight.X - UpdateAreaTotalWidth - render.FoldingWidth - this.render.LineNemberWidth;
1060 return this.render.TextArea.X - this.render.LineNemberWidth - this.LineNumberMargin;
1061 case AreaType.TextArea:
1062 return this.render.TextArea.X;
1064 throw new ArgumentOutOfRangeException();