2 * Copyright (C) 2013 FooProject
3 * * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
6 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
12 using System.Globalization;
15 using System.Diagnostics;
16 using System.Text.RegularExpressions;
21 namespace FooEditEngine
23 internal enum MoveFlow
28 internal enum ScrollDirection
39 public enum IndentMode
46 /// ユーザー側からの処理を担当するクラス。一部を除き、こちらで行われた操作はアンドゥの対象になります
48 internal sealed class Controller
54 public Controller(Document doc, EditView view)
57 this.Document.Update += new DocumentUpdateEventHandler(Document_Update);
59 this.View.render.ChangedRightToLeft += render_ChangedRightToLeft;
60 this.View.render.ChangedRenderResource += render_ChangedRenderResource;
61 this.View.PerformLayouted += View_LineBreakChanged;
62 this.View.PageBoundChanged += View_PageBoundChanged;
63 this.SelectionChanged += new EventHandler((s, e) => { });
64 this.Document.Clear();
70 public event EventHandler SelectionChanged;
73 /// 矩形選択モードなら真を返し、そうでない場合は偽を返す
75 public bool RectSelection
84 public IndentMode IndentMode
93 /// <remarks>SelectionLengthが0の場合、キャレット位置を表します</remarks>
94 public int SelectionStart
98 if (this.View.Selections.Count == 0)
99 return this.AnchorIndex;
101 return this.View.Selections.First().start;
108 /// <remarks>矩形選択モードの場合、選択範囲の文字数ではなく、開始位置から終了位置までの長さとなります</remarks>
109 public int SelectionLength
113 if (this.View.Selections.Count == 0)
115 Selection last = this.View.Selections.Last();
116 return last.start + last.length - this.SelectionStart;
124 /// 未選択状態で代入したときは追加され、そうでない場合は選択範囲の文字列と置き換えられます。
126 public string SelectedText
130 if (this.View.LayoutLines.Count == 0 || this.View.Selections.Count == 0)
132 if (this.RectSelection)
133 return GetTextFromRectangleSelectArea(this.View.Selections);
135 return GetTextFromLineSelectArea(this.View.Selections).Replace(Document.NewLine.ToString(), Environment.NewLine);
139 if (this.Document.FireUpdateEvent == false)
140 throw new InvalidOperationException("");
143 this.RepleaceSelectionArea(this.View.Selections, value.Replace(Environment.NewLine,Document.NewLine.ToString()));
148 /// 選択範囲が逆転しているかどうかを判定する
150 /// <returns>逆転しているなら真を返す</returns>
151 public bool IsReverseSelect()
153 int index = this.View.LayoutLines.GetIndexFromTextPoint(this.View.CaretPostion);
154 return index < this.AnchorIndex;
160 /// <param name="start"></param>
161 /// <param name="length"></param>
162 /// <remarks>RectSelectionの値によって動作が変わります。真の場合は矩形選択モードに、そうでない場合は行ごとに選択されます</remarks>
163 public void Select(int start, int length)
165 if (this.Document.FireUpdateEvent == false)
166 throw new InvalidOperationException("");
167 if (start < 0 || start + length < 0 || start + length > this.Document.Length)
168 throw new ArgumentOutOfRangeException("startかendが指定できる範囲を超えてます");
169 this.View.Selections.Clear();
172 int oldStart = start;
174 length = oldStart - start;
176 if (this.RectSelection && length != 0)
178 TextPoint startTextPoint = this.View.GetLayoutLineFromIndex(start);
179 TextPoint endTextPoint = this.View.GetLayoutLineFromIndex(start + length);
180 this.SelectByRectangle(new TextRectangle(startTextPoint, endTextPoint));
181 if (startTextPoint.col == endTextPoint.col)
182 this.View.InsertPoint = new SelectCollection(this.View.Selections);
184 this.View.InsertPoint = null;
188 this.View.Selections.Add(Selection.Create(start, length));
189 this.View.InsertPoint = null;
191 this.SelectionChanged(this, null);
194 public void Select(TextPoint tp, int width, int height)
196 if (this.Document.FireUpdateEvent == false || !this.RectSelection)
197 throw new InvalidOperationException("");
200 end.row = tp.row + height;
201 end.col = tp.col + width;
203 if (end.row > this.View.LayoutLines.Count - 1)
204 throw new ArgumentOutOfRangeException("");
206 this.View.Selections.Clear();
208 this.SelectByRectangle(new TextRectangle(tp,end));
211 this.View.InsertPoint = new SelectCollection(this.View.Selections);
213 this.View.InsertPoint = null;
215 this.SelectionChanged(this, null);
218 private void SelectByRectangle(TextRectangle rect)
220 if (this.Document.FireUpdateEvent == false)
221 throw new InvalidOperationException("");
222 if (rect.TopLeft <= rect.BottomRight)
224 for (int i = rect.TopLeft.row; i <= rect.BottomLeft.row; i++)
226 int length = this.View.LayoutLines.GetLengthFromLineNumber(i);
227 int leftCol = rect.TopLeft.col, rightCol = rect.TopRight.col, lastCol = length;
228 if(length > 0 && this.View.LayoutLines[i][length - 1] == Document.NewLine)
229 lastCol = length - 1;
232 if (rect.TopLeft.col > lastCol)
234 if (rect.TopRight.col > lastCol)
237 int StartIndex = this.View.LayoutLines.GetIndexFromTextPoint(new TextPoint(i, leftCol));
238 int EndIndex = this.View.LayoutLines.GetIndexFromTextPoint(new TextPoint(i, rightCol));
241 sel = Selection.Create(StartIndex, EndIndex - StartIndex);
243 this.View.Selections.Add(sel);
251 /// <param name="index">探索を開始するインデックス</param>
252 public void SelectWord(int index)
254 if (this.Document.FireUpdateEvent == false)
255 throw new InvalidOperationException("");
257 if (this.Document.Length <= 0 || index >= this.Document.Length)
260 Document str = this.Document;
263 while (start > 0 && !Util.IsWordSeparator(str[start]))
266 if (Util.IsWordSeparator(str[start]))
270 while (end < this.Document.Length && !Util.IsWordSeparator(str[end]))
273 this.Select(start, end - start);
277 /// 選択範囲内のUTF32コードポイントを文字列に変換します
279 /// <returns>成功した場合は真。そうでない場合は偽を返す</returns>
280 public bool ConvertToChar()
282 if (this.SelectionLength == 0 || this.RectSelection)
284 string str = this.Document.ToString(this.SelectionStart, this.SelectionLength);
285 string[] codes = str.Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries);
286 StringBuilder result = new StringBuilder();
287 foreach (string code in codes)
292 if (Int32.TryParse(code.TrimStart('U'),NumberStyles.HexNumber,null, out utf32_code))
293 result.Append(Char.ConvertFromUtf32(utf32_code));
297 this.Document.Lock();
298 this.Document.Replace(this.SelectionStart, this.SelectionLength, result.ToString());
299 this.Document.UnLock();
304 /// 選択文字列をUTF32のコードポイントに変換します
306 public void ConvertToCodePoint()
308 if (this.SelectionLength == 0 || this.RectSelection)
310 string str = this.Document.ToString(this.SelectionStart, this.SelectionLength);
311 StringInfo info = new StringInfo(str);
312 StringBuilder result = new StringBuilder();
313 for (int i = 0; i < str.Length;)
315 int utf32_code = Char.ConvertToUtf32(str, i);
316 result.Append("U" + Convert.ToString(utf32_code,16));
318 if(Char.IsHighSurrogate(str[i]))
323 this.Document.Lock();
324 this.Document.Replace(this.SelectionStart, this.SelectionLength, result.ToString());
325 this.Document.UnLock();
331 public void DeSelectAll()
333 if (this.Document.FireUpdateEvent == false)
334 throw new InvalidOperationException("");
336 this.View.Selections.Clear();
342 /// <param name="tp"></param>
343 /// <param name="type"></param>
344 /// <returns>真ならマーカーがある</returns>
345 public bool IsMarker(TextPoint tp,HilightType type)
347 if (this.Document.FireUpdateEvent == false)
348 throw new InvalidOperationException("");
349 int index = this.View.LayoutLines.GetIndexFromTextPoint(tp);
350 return this.IsMarker(index, type);
356 /// <param name="index"></param>
357 /// <param name="type"></param>
358 /// <returns>真ならマーカーがある</returns>
359 public bool IsMarker(int index, HilightType type)
361 foreach(int id in this.Document.Markers.IDs)
363 foreach (Marker m in this.Document.GetMarkers(index, id))
365 if (m.hilight == type)
375 public void AdjustCaret()
377 int row = this.View.CaretPostion.row;
378 if (row > this.View.LayoutLines.Count - 1)
379 row = this.View.LayoutLines.Count - 1;
380 int col = this.View.CaretPostion.col;
381 if (col > 0 && col > this.View.LayoutLines[row].Length)
382 col = this.View.LayoutLines[row].Length;
383 this.JumpCaret(row, col);
387 /// キャレットを指定した位置に移動させる
389 /// <param name="index"></param>
390 /// <param name="autoExpand">折り畳みを展開するなら真</param>
391 public void JumpCaret(int index,bool autoExpand = true)
393 if (index < 0 || index > this.Document.Length)
394 throw new ArgumentOutOfRangeException("indexが設定できる範囲を超えています");
395 TextPoint tp = this.View.GetLayoutLineFromIndex(index);
397 this.JumpCaret(tp.row, tp.col,autoExpand);
401 /// キャレットを指定した位置に移動させる
403 /// <param name="row"></param>
404 /// <param name="col"></param>
405 /// <param name="autoExpand">折り畳みを展開するなら真</param>
406 public void JumpCaret(int row, int col, bool autoExpand = true)
408 if (this.Document.FireUpdateEvent == false)
409 throw new InvalidOperationException("");
411 this.View.JumpCaret(row, col,autoExpand);
413 this.View.AdjustCaretAndSrc();
415 this.SelectWithMoveCaret(false);
421 /// <param name="row">行</param>
422 /// <param name="isSelected">選択状態にするかどうか</param>
423 public void JumpToLineHead(int row,bool isSelected)
425 this.View.JumpCaret(row, 0);
426 this.View.AdjustCaretAndSrc();
427 this.SelectWithMoveCaret(isSelected);
433 /// <param name="row">行</param>
434 /// <param name="isSelected">選択状態にするかどうか</param>
435 public void JumpToLineEnd(int row, bool isSelected)
437 this.View.JumpCaret(row, this.View.LayoutLines[row].Length - 1);
438 this.View.AdjustCaretAndSrc();
439 this.SelectWithMoveCaret(isSelected);
445 /// <param name="isSelected"></param>
446 public void JumpToHead(bool isSelected)
448 if (this.View.TryScroll(0, 0))
450 this.View.JumpCaret(0, 0);
451 this.View.AdjustCaretAndSrc();
452 this.SelectWithMoveCaret(isSelected);
458 /// <param name="isSelected"></param>
459 public void JumpToEnd(bool isSelected)
461 int srcRow = this.View.LayoutLines.Count - this.View.LineCountOnScreen - 1;
464 if (this.View.TryScroll(0, srcRow))
466 this.View.JumpCaret(this.View.LayoutLines.Count - 1, 0);
467 this.View.AdjustCaretAndSrc();
468 this.SelectWithMoveCaret(isSelected);
474 /// <param name="dir">方向を指定する</param>
475 /// <param name="delta">スクロールする量。ScrollDirectionの値がUpやDownなら行数。LeftやRightならピクセル単位の値となる</param>
476 /// <param name="isSelected">選択状態にするなら真</param>
477 /// <param name="withCaret">同時にキャレットを移動させるなら真</param>
478 public void Scroll(ScrollDirection dir, int delta, bool isSelected,bool withCaret)
480 if (this.Document.FireUpdateEvent == false)
481 throw new InvalidOperationException("");
482 int toRow = this.View.Src.Row;
483 double toX = this.View.Src.X;
486 case ScrollDirection.Up:
487 toRow = Math.Max(0, this.View.Src.Row - delta);
488 toRow = this.View.AdjustRow(toRow, false);
490 case ScrollDirection.Down:
491 toRow = Math.Min(this.View.Src.Row + delta, this.View.LayoutLines.Count - 1);
492 toRow = this.View.AdjustRow(toRow, true);
494 case ScrollDirection.Left:
497 case ScrollDirection.Right:
501 throw new ArgumentOutOfRangeException();
503 this.Scroll(toX, toRow, isSelected, withCaret);
509 /// <param name="toX">スクロール先の座標</param>
510 /// <param name="toRow">スクロール先の行</param>
511 /// <param name="isSelected">選択状態にするなら真</param>
512 /// <param name="withCaret">同時にキャレットを移動させるなら真</param>
513 public void Scroll(double toX, int toRow, bool isSelected, bool withCaret)
517 this.View.Scroll(toX, toRow);
518 this.View.JumpCaret(toRow, 0);
519 this.View.AdjustCaretAndSrc();
520 this.SelectWithMoveCaret(isSelected);
524 this.View.Scroll(toX, toRow);
525 this.View.IsFocused = false;
532 /// <returns>移動できない場合は真を返す</returns>
533 /// <param name="realLength">負の値なら左側へ、そうでないなら右側へ移動する</param>
534 /// <param name="isSelected">選択範囲とするなら真。そうでないなら偽</param>
535 /// <param name="alignWord">単語単位で移動するなら真。そうでないなら偽</param>
536 public void MoveCaretHorizontical(int realLength, bool isSelected,bool alignWord = false)
538 for (int i = Math.Abs(realLength); i > 0; i--)
540 bool MoveFlow = realLength > 0;
541 if (this.View.render.RightToLeft)
542 MoveFlow = !MoveFlow;
543 this.MoveCaretHorizontical(MoveFlow);
546 this.AlignNearestWord(MoveFlow);
548 this.View.AdjustCaretAndSrc(AdjustFlow.Col);
549 this.SelectWithMoveCaret(isSelected);
552 void AlignNearestWord(bool MoveFlow)
554 string str = this.View.LayoutLines[this.View.CaretPostion.row];
555 while (this.View.CaretPostion.col > 0 &&
556 this.View.CaretPostion.col < str.Length &&
557 str[this.View.CaretPostion.col] != Document.NewLine)
559 if (!Util.IsWordSeparator(str[this.View.CaretPostion.col]))
561 this.MoveCaretHorizontical(MoveFlow);
566 this.MoveCaretHorizontical(MoveFlow);
575 /// <returns>再描写する必要があるなら真を返す</returns>
576 /// <param name="deltarow">移動量</param>
577 /// <param name="isSelected"></param>
578 public void MoveCaretVertical(int deltarow,bool isSelected)
580 for (int i = Math.Abs(deltarow); i > 0; i--)
581 this.MoveCaretVertical(deltarow > 0);
582 this.View.AdjustCaretAndSrc(AdjustFlow.Both);
583 this.SelectWithMoveCaret(isSelected);
587 /// キャレット位置の文字を一文字削除する
589 public void DoDeleteAction()
591 if (this.SelectionLength != 0)
593 this.SelectedText = "";
597 if (this.Document.FireUpdateEvent == false)
598 throw new InvalidOperationException("");
600 TextPoint CaretPostion = this.View.CaretPostion;
601 int index = this.View.GetIndexFromLayoutLine(CaretPostion);
603 if (index == this.Document.Length)
606 int lineHeadIndex = this.View.LayoutLines.GetIndexFromLineNumber(CaretPostion.row);
607 int next = this.View.LayoutLines.GetLayout(CaretPostion.row).AlignIndexToNearestCluster(CaretPostion.col, AlignDirection.Forward) + lineHeadIndex;
609 if (this.Document[index] == Document.NewLine)
612 this.Document.Lock();
613 this.Document.Replace(index, next - index, "");
614 this.Document.UnLock();
618 /// キャレット位置の文字を一文字削除し、キャレット位置を後ろにずらす
620 public void DoBackSpaceAction()
622 if (this.View.InsertPoint != null)
624 this.ReplaceBeforeSelectionArea(this.View.Selections, 1, "");
627 else if (this.SelectionLength > 0)
629 this.SelectedText = "";
633 if (this.Document.FireUpdateEvent == false)
634 throw new InvalidOperationException("");
636 TextPoint CurrentPostion = this.View.CaretPostion;
638 if (CurrentPostion.row == 0 && CurrentPostion.col == 0)
641 int oldIndex = this.View.GetIndexFromLayoutLine(CurrentPostion);
643 int newCol, newIndex;
644 if (CurrentPostion.col > 0)
646 newCol = this.View.LayoutLines.GetLayout(CurrentPostion.row).AlignIndexToNearestCluster(CurrentPostion.col - 1, AlignDirection.Back);
647 newIndex = this.View.GetIndexFromLayoutLine(new TextPoint(CurrentPostion.row, newCol));
651 newIndex = this.View.GetIndexFromLayoutLine(CurrentPostion);
655 this.Document.Lock();
656 this.Document.Replace(newIndex, oldIndex - newIndex, "");
657 this.Document.UnLock();
663 public void DoEnterAction()
665 this.DoInputChar('\n');
669 /// キャレット位置に文字を入力し、その分だけキャレットを進める。isInsertModeの値により動作が変わります
671 /// <param name="ch"></param>
672 public void DoInputChar(char ch)
674 this.DoInputString(ch.ToString());
677 string GetIndentSpace(int col_index)
679 int space_count = this.View.TabStops - (col_index % this.View.TabStops);
680 return new string(Enumerable.Repeat(' ',space_count).ToArray());
684 /// キャレット位置に文字列を挿入し、その分だけキャレットを進める。isInsertModeの値により動作が変わります
686 /// <param name="str"></param>
687 /// <param name="fromTip"></param>
688 public void DoInputString(string str,bool fromTip = false)
690 TextPoint CaretPos = this.View.CaretPostion;
692 if (str == "\t" && this.IndentMode == IndentMode.Space)
693 str = this.GetIndentSpace(CaretPos.col);
695 if (this.View.InsertPoint != null)
697 this.ReplaceBeforeSelectionArea(this.View.Selections, 0, str);
700 else if (this.SelectionLength != 0)
702 this.RepleaceSelectionArea(this.View.Selections, str, fromTip);
706 if (this.Document.FireUpdateEvent == false)
707 throw new InvalidOperationException("");
709 int index = this.View.GetIndexFromLayoutLine(this.View.CaretPostion);
711 if (this.View.InsertMode == false && index < this.Document.Length && this.Document[index] != Document.NewLine)
713 string lineString = this.View.LayoutLines[CaretPos.row];
714 int end = this.View.LayoutLines.GetLayout(CaretPos.row).AlignIndexToNearestCluster(CaretPos.col + str.Length - 1, AlignDirection.Forward);
715 if (end > lineString.Length - 1)
716 end = lineString.Length - 1;
717 end += this.View.LayoutLines.GetIndexFromLineNumber(CaretPos.row);
718 length = end - index;
720 if (str == Document.NewLine.ToString())
722 int lineHeadIndex = this.View.LayoutLines.GetIndexFromLineNumber(CaretPos.row);
723 int lineLength = this.View.LayoutLines.GetLengthFromLineNumber(CaretPos.row);
724 FoldingItem foldingData = this.View.LayoutLines.FoldingCollection.GetFarestHiddenFoldingData(lineHeadIndex, lineLength);
725 if (foldingData != null && !foldingData.Expand && index > foldingData.Start && index <= foldingData.End)
726 index = foldingData.End + 1;
728 this.Document.Lock();
729 this.Document.Replace(index, length, str);
730 this.Document.UnLock();
734 /// キャレットの移動に合わせて選択する
736 /// <param name="isSelected">選択状態にするかどうか</param>
738 /// キャレットを移動後、このメソッドを呼び出さない場合、Select()メソッドは正常に機能しません
740 void SelectWithMoveCaret(bool isSelected)
742 if (this.View.CaretPostion.col < 0 || this.View.CaretPostion.row < 0)
745 if (this.Document.FireUpdateEvent == false)
746 throw new InvalidOperationException("");
748 int CaretPostion = this.View.GetIndexFromLayoutLine(this.View.CaretPostion);
750 SelectCollection Selections = this.View.Selections;
753 this.Select(this.AnchorIndex, CaretPostion - this.AnchorIndex);
755 this.AnchorIndex = CaretPostion;
756 this.View.InsertPoint = null;
757 this.Select(CaretPostion, 0);
762 /// JumpCaretで移動した位置からキャレットを移動し、選択状態にする
764 /// <param name="tp"></param>
765 public void MoveCaretAndSelect(TextPoint tp)
767 int CaretPostion = this.View.GetIndexFromLayoutLine(tp);
768 this.Select(this.AnchorIndex, CaretPostion - this.AnchorIndex);
769 this.View.JumpCaret(tp.row, tp.col);
770 this.View.AdjustCaretAndSrc();
773 public void MoveSelectBefore(TextPoint tp)
777 if (this.IsReverseSelect())
779 NewAnchorIndex = this.View.GetIndexFromLayoutLine(tp);
780 SelectionLength = this.SelectionLength + NewAnchorIndex - this.AnchorIndex;
781 this.Select(this.SelectionStart, SelectionLength);
785 NewAnchorIndex = this.View.GetIndexFromLayoutLine(tp);
786 SelectionLength = this.SelectionLength + this.AnchorIndex - NewAnchorIndex;
787 this.Select(NewAnchorIndex, SelectionLength);
789 this.AnchorIndex = NewAnchorIndex;
795 public void ResetCaretPostion()
801 /// 行単位で移動後のキャレット位置を取得する
803 /// <param name="count">移動量</param>
804 /// <param name="current">現在のキャレット位置</param>
805 /// <returns>移動後のキャレット位置</returns>
806 public TextPoint GetTextPointAfterMoveLine(int count, TextPoint current)
808 int row = current.row + count;
812 else if (row >= this.View.LayoutLines.Count)
813 row = this.View.LayoutLines.Count - 1;
815 row = this.View.AdjustRow(row, count > 0);
817 double colpos = this.View.GetColPostionFromIndex(current.row, current.col);
818 int col = this.View.GetIndexFromColPostion(row, colpos);
820 return new TextPoint(row, col);
824 /// 選択文字列のインデントを一つ増やす
826 public void UpIndent()
828 if (this.RectSelection || this.SelectionLength == 0)
830 int selectionStart = this.SelectionStart;
831 string insertStr = this.IndentMode == IndentMode.Space ? this.GetIndentSpace(0) : "\t";
832 string text = this.InsertLineHead(GetTextFromLineSelectArea(this.View.Selections), insertStr);
833 this.RepleaceSelectionArea(this.View.Selections,text);
834 this.Select(selectionStart, text.Length);
838 /// 選択文字列のインデントを一つ減らす
840 public void DownIndent()
842 if (this.RectSelection || this.SelectionLength == 0)
844 int selectionStart = this.SelectionStart;
845 string insertStr = this.IndentMode == IndentMode.Space ? this.GetIndentSpace(0) : "\t";
846 string text = this.RemoveLineHead(GetTextFromLineSelectArea(this.View.Selections), insertStr);
847 this.RepleaceSelectionArea(this.View.Selections, text);
848 this.Select(selectionStart, text.Length);
851 string InsertLineHead(string s, string str)
853 string[] lines = s.Split(new string[] { Document.NewLine.ToString() }, StringSplitOptions.None);
854 StringBuilder output = new StringBuilder();
855 for (int i = 0; i < lines.Length; i++)
857 if(lines[i].Length > 0)
858 output.Append(str + lines[i] + Document.NewLine);
859 else if(i < lines.Length - 1)
860 output.Append(lines[i] + Document.NewLine);
862 return output.ToString();
865 public string RemoveLineHead(string s, string str)
867 string[] lines = s.Split(new string[] { Document.NewLine.ToString() }, StringSplitOptions.None);
868 StringBuilder output = new StringBuilder();
869 for (int i = 0; i < lines.Length; i++)
871 if (lines[i].StartsWith(str))
872 output.Append(lines[i].Substring(1) + Document.NewLine);
873 else if (i < lines.Length - 1)
874 output.Append(lines[i] + Document.NewLine);
876 return output.ToString();
882 /// <param name="isMoveNext">真なら1文字すすめ、そうでなければ戻す</param>
883 /// <remarks>このメソッドを呼び出した後でScrollToCaretメソッドとSelectWithMoveCaretメソッドを呼び出す必要があります</remarks>
884 void MoveCaretHorizontical(bool isMoveNext)
886 if (this.Document.FireUpdateEvent == false)
887 throw new InvalidOperationException("");
888 int delta = isMoveNext ? 0 : -1;
889 int prevcol = this.View.CaretPostion.col;
890 int col = this.View.CaretPostion.col + delta;
891 string lineString = this.View.LayoutLines[this.View.CaretPostion.row];
892 if (col < 0 || this.View.CaretPostion.row >= this.View.LayoutLines.Count)
894 if (this.View.CaretPostion.row == 0)
899 this.MoveCaretVertical(false);
900 this.View.AdjustCaretAndSrc(AdjustFlow.Row); //この段階で調整しないとスクロールされない
901 col = this.View.LayoutLines.GetLengthFromLineNumber(this.View.CaretPostion.row) - 1; //最終行以外はすべて改行コードが付くはず
903 else if (col >= lineString.Length || lineString[col] == Document.NewLine)
905 if (this.View.CaretPostion.row < this.View.LayoutLines.Count - 1)
907 this.MoveCaretVertical(true);
908 this.View.AdjustCaretAndSrc(AdjustFlow.Row); //この段階で調整しないとスクロールされない
914 AlignDirection direction = isMoveNext ? AlignDirection.Forward : AlignDirection.Back;
915 col = this.View.LayoutLines.GetLayout(this.View.CaretPostion.row).AlignIndexToNearestCluster(col, direction);
918 this.View.JumpCaret(this.View.CaretPostion.row, col,false);
924 /// <param name="isMoveNext">プラス方向に移動するなら真</param>
925 /// <remarks>このメソッドを呼び出した後でScrollToCaretメソッドとSelectWithMoveCaretメソッドを呼び出す必要があります</remarks>
926 void MoveCaretVertical(bool isMoveNext)
928 if (this.Document.FireUpdateEvent == false)
929 throw new InvalidOperationException("");
931 TextPoint nextPoint = this.GetTextPointAfterMoveLine(isMoveNext ? 1 : -1, this.View.CaretPostion);
933 this.View.JumpCaret(nextPoint.row, nextPoint.col,false);
936 private void ReplaceBeforeSelectionArea(SelectCollection Selections, int removeLength, string insertStr)
938 if (removeLength == 0 && insertStr.Length == 0)
941 if (this.RectSelection == false || this.Document.FireUpdateEvent == false)
942 throw new InvalidOperationException();
944 SelectCollection temp = this.View.InsertPoint;
945 int selectStart = temp.First().start;
946 int selectEnd = temp.Last().start + temp.Last().length;
948 //ドキュメント操作後に行うとうまくいかないので、あらかじめ取得しておく
949 TextPoint start = this.View.LayoutLines.GetTextPointFromIndex(selectStart);
950 TextPoint end = this.View.LayoutLines.GetTextPointFromIndex(selectEnd);
952 bool reverse = temp.First().start > temp.Last().start;
954 int lineHeadIndex = this.View.LayoutLines.GetIndexFromLineNumber(this.View.LayoutLines.GetLineNumberFromIndex(selectStart));
955 if (selectStart - removeLength < lineHeadIndex)
958 this.Document.UndoManager.BeginUndoGroup();
959 this.Document.FireUpdateEvent = false;
963 for (int i = 0; i < temp.Count; i++)
965 this.ReplaceBeforeSelection(temp[i], removeLength, insertStr);
970 for (int i = temp.Count - 1; i >= 0; i--)
972 this.ReplaceBeforeSelection(temp[i], removeLength, insertStr);
976 this.Document.FireUpdateEvent = true;
977 this.Document.UndoManager.EndUndoGroup();
979 int delta = insertStr.Length - removeLength;
984 this.JumpCaret(start.row, start.col);
986 this.JumpCaret(end.row, end.col);
988 this.Select(start, 0, end.row - start.row);
991 private void ReplaceBeforeSelection(Selection sel, int removeLength, string insertStr)
993 sel = Util.NormalizeIMaker<Selection>(sel);
994 this.Document.Lock();
995 this.Document.Replace(sel.start - removeLength, removeLength, insertStr);
996 this.Document.UnLock();
999 private void RepleaceSelectionArea(SelectCollection Selections, string value,bool updateInsertPoint = false)
1004 if (this.RectSelection == false)
1006 Selection sel = Selection.Create(this.AnchorIndex, 0);
1007 if (Selections.Count > 0)
1008 sel = Util.NormalizeIMaker<Selection>(this.View.Selections.First());
1010 this.Document.Lock();
1011 this.Document.Replace(sel.start, sel.length, value);
1012 this.Document.UnLock();
1016 if (this.Document.FireUpdateEvent == false)
1017 throw new InvalidOperationException("");
1019 int StartIndex = this.SelectionStart;
1021 SelectCollection newInsertPoint = new SelectCollection();
1023 if (this.SelectionLength == 0)
1027 this.Document.Lock();
1029 this.Document.UndoManager.BeginUndoGroup();
1031 this.Document.FireUpdateEvent = false;
1033 string[] line = value.Split(new string[] { Document.NewLine.ToString() }, StringSplitOptions.RemoveEmptyEntries);
1035 TextPoint Current = this.View.GetLayoutLineFromIndex(this.SelectionStart);
1037 for (i = 0; i < line.Length && Current.row < this.View.LayoutLines.Count; i++, Current.row++)
1039 if (Current.col > this.View.LayoutLines[Current.row].Length)
1040 Current.col = this.View.LayoutLines[Current.row].Length;
1041 StartIndex = this.View.GetIndexFromLayoutLine(Current);
1042 this.Document.Replace(StartIndex, 0, line[i]);
1043 StartIndex += line[i].Length;
1046 for (; i < line.Length; i++)
1048 StartIndex = this.Document.Length;
1049 string str = Document.NewLine + line[i];
1050 this.Document.Replace(StartIndex, 0, str);
1051 StartIndex += str.Length;
1054 this.Document.FireUpdateEvent = true;
1056 this.Document.UndoManager.EndUndoGroup();
1058 this.Document.UnLock();
1062 SelectCollection temp = new SelectCollection(this.View.Selections); //コピーしないとReplaceCommandを呼び出した段階で書き換えられてしまう
1064 this.Document.Lock();
1066 this.Document.UndoManager.BeginUndoGroup();
1068 this.Document.FireUpdateEvent = false;
1070 if (temp.First().start < temp.Last().start)
1072 for (int i = temp.Count - 1; i >= 0; i--)
1074 Selection sel = Util.NormalizeIMaker<Selection>(temp[i]);
1076 StartIndex = sel.start;
1078 this.Document.Replace(sel.start, sel.length, value);
1080 newInsertPoint.Add(Selection.Create(sel.start + (value.Length - sel.length) * i,0));
1085 for (int i = 0; i < temp.Count; i++)
1087 Selection sel = Util.NormalizeIMaker<Selection>(temp[i]);
1089 StartIndex = sel.start;
1091 this.Document.Replace(sel.start, sel.length, value);
1093 newInsertPoint.Add(Selection.Create(sel.start + (value.Length - sel.length) * i, 0));
1097 this.Document.FireUpdateEvent = true;
1099 this.Document.UndoManager.EndUndoGroup();
1101 this.Document.UnLock();
1103 this.JumpCaret(StartIndex);
1104 if (updateInsertPoint && newInsertPoint.Count > 0)
1105 this.View.InsertPoint = newInsertPoint;
1108 private string GetTextFromLineSelectArea(SelectCollection Selections)
1110 Selection sel = Util.NormalizeIMaker<Selection>(Selections.First());
1112 string str = this.Document.ToString(sel.start, sel.length);
1117 string GetTextFromRectangleSelectArea(SelectCollection Selections)
1119 StringBuilder temp = new StringBuilder();
1120 if (Selections.First().start < Selections.Last().start)
1122 for (int i = 0; i < this.View.Selections.Count; i++)
1124 Selection sel = Util.NormalizeIMaker<Selection>(Selections[i]);
1126 string str = this.Document.ToString(sel.start, sel.length);
1127 if (str.IndexOf(Environment.NewLine) == -1)
1128 temp.AppendLine(str);
1135 for (int i = this.View.Selections.Count - 1; i >= 0; i--)
1137 Selection sel = Util.NormalizeIMaker<Selection>(Selections[i]);
1139 string str = this.Document.ToString(sel.start, sel.length).Replace(Document.NewLine.ToString(), Environment.NewLine);
1140 if (str.IndexOf(Environment.NewLine) == -1)
1141 temp.AppendLine(str);
1146 return temp.ToString();
1149 void View_LineBreakChanged(object sender, EventArgs e)
1155 void View_PageBoundChanged(object sender, EventArgs e)
1157 if (this.View.LineBreak == LineBreakMethod.PageBound && this.View.PageBound.Width - this.View.LineBreakingMarginWidth > 0)
1158 this.View.PerfomLayouts();
1162 void render_ChangedRenderResource(object sender, ChangedRenderRsourceEventArgs e)
1164 if (e.type == ResourceType.Font)
1166 if (this.View.LineBreak == LineBreakMethod.PageBound)
1167 this.View.PerfomLayouts();
1170 if (e.type == ResourceType.InlineChar)
1172 int oldLineCountOnScreen = this.View.LineCountOnScreen;
1173 this.View.CalculateLineCountOnScreen();
1174 if(this.View.LineCountOnScreen != oldLineCountOnScreen)
1179 void render_ChangedRightToLeft(object sender, EventArgs e)
1184 void Document_Update(object sender, DocumentUpdateEventArgs e)
1188 case UpdateType.Replace:
1189 if(e.startIndex < this.Document.Length && this.Document[e.startIndex] == Document.NewLine)
1190 this.View.CalculateLineCountOnScreen();
1191 this.JumpCaret(e.startIndex + e.insertLength,true);
1193 case UpdateType.Clear:
1194 this.JumpCaret(0,0, false);