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
52 /// ユーザー側からの処理を担当するクラス。一部を除き、こちらで行われた操作はアンドゥの対象になります
54 internal sealed class Controller
58 bool keepAnchor = false;
60 public Controller(Document doc, EditView view)
64 this.View.render.ChangedRenderResource += render_ChangedRenderResource;
65 this.View.PageBoundChanged += View_PageBoundChanged;
66 //this.Document.Clear();
69 public Document Document
73 return this._Document;
77 //メモリリークを防ぐためにパンドラーを除く
78 if (this._Document != null)
80 this._Document.Update -= Document_Update;
81 this._Document.StatusUpdate -= Document_StatusChanged;
82 this._Document.SelectionChanged -= Document_SelectionChanged;
83 this._Document.PerformLayouted -= View_LineBreakChanged;
84 this._Document.CaretChanged -= _Document_CaretChanged;
87 this._Document = value;
89 this._Document.Update += new DocumentUpdateEventHandler(Document_Update);
90 this._Document.StatusUpdate += Document_StatusChanged;
91 this._Document.SelectionChanged += Document_SelectionChanged;
92 this._Document.PerformLayouted += View_LineBreakChanged;
93 this._Document.CaretChanged += _Document_CaretChanged;
97 private void _Document_CaretChanged(object sender, EventArgs e)
101 TextPoint pos = this.Document.CaretPostion;
102 this.JumpCaret(pos.row, pos.col);
105 private void Document_SelectionChanged(object sender, EventArgs e)
107 if (this.IsReverseSelect())
109 this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart);
110 this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
114 this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart);
115 this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
119 void Document_StatusChanged(object sender,EventArgs e)
125 /// 矩形選択モードなら真を返し、そうでない場合は偽を返す
127 public bool RectSelection
129 get { return this.Document.RectSelection; }
130 set { this.Document.RectSelection = value; }
136 public IndentMode IndentMode
138 get { return this.Document.IndentMode; }
139 set { this.Document.IndentMode = value; }
145 /// <remarks>SelectionLengthが0の場合、キャレット位置を表します</remarks>
146 public int SelectionStart
150 if (this.View.Selections.Count == 0)
151 return this.Document.AnchorIndex;
153 return this.View.Selections.First().start;
160 /// <remarks>矩形選択モードの場合、選択範囲の文字数ではなく、開始位置から終了位置までの長さとなります</remarks>
161 public int SelectionLength
165 if (this.View.Selections.Count == 0)
167 Selection last = this.View.Selections.Last();
168 return last.start + last.length - this.SelectionStart;
176 /// 未選択状態で代入したときは追加され、そうでない場合は選択範囲の文字列と置き換えられます。
178 public string SelectedText
182 if (this.View.LayoutLines.Count == 0 || this.View.Selections.Count == 0)
184 if (this.RectSelection)
185 return GetTextFromRectangleSelectArea(this.View.Selections);
187 return GetTextFromLineSelectArea(this.View.Selections).Replace(Document.NewLine.ToString(), Environment.NewLine);
191 if (this.Document.FireUpdateEvent == false)
192 throw new InvalidOperationException("");
195 this.RepleaceSelectionArea(this.View.Selections, value.Replace(Environment.NewLine,Document.NewLine.ToString()));
200 /// 選択範囲が逆転しているかどうかを判定する
202 /// <returns>逆転しているなら真を返す</returns>
203 public bool IsReverseSelect()
205 int index = this.View.LayoutLines.GetIndexFromTextPoint(this.Document.CaretPostion);
206 return index < this.Document.AnchorIndex;
210 /// 選択範囲内のUTF32コードポイントを文字列に変換します
212 /// <returns>成功した場合は真。そうでない場合は偽を返す</returns>
213 public bool ConvertToChar()
215 if (this.SelectionLength == 0 || this.RectSelection)
217 string str = this.Document.ToString(this.SelectionStart, this.SelectionLength);
218 string[] codes = str.Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries);
219 StringBuilder result = new StringBuilder();
220 foreach (string code in codes)
225 if (Int32.TryParse(code.TrimStart('U'),NumberStyles.HexNumber,null, out utf32_code))
226 result.Append(Char.ConvertFromUtf32(utf32_code));
230 this.Document.Replace(this.SelectionStart, this.SelectionLength, result.ToString());
235 /// 選択文字列をUTF32のコードポイントに変換します
237 public void ConvertToCodePoint()
239 if (this.SelectionLength == 0 || this.RectSelection)
241 string str = this.Document.ToString(this.SelectionStart, this.SelectionLength);
242 StringInfo info = new StringInfo(str);
243 StringBuilder result = new StringBuilder();
244 for (int i = 0; i < str.Length;)
246 int utf32_code = Char.ConvertToUtf32(str, i);
247 result.Append("U" + Convert.ToString(utf32_code,16));
249 if(Char.IsHighSurrogate(str[i]))
254 this.Document.Replace(this.SelectionStart, this.SelectionLength, result.ToString());
260 public void DeSelectAll()
262 if (this.Document.FireUpdateEvent == false)
263 throw new InvalidOperationException("");
265 this.View.Selections.Clear();
271 /// <param name="tp"></param>
272 /// <param name="type"></param>
273 /// <returns>真ならマーカーがある</returns>
274 public bool IsMarker(TextPoint tp,HilightType type)
276 if (this.Document.FireUpdateEvent == false)
277 throw new InvalidOperationException("");
278 int index = this.View.LayoutLines.GetIndexFromTextPoint(tp);
279 return this.IsMarker(index, type);
285 /// <param name="index"></param>
286 /// <param name="type"></param>
287 /// <returns>真ならマーカーがある</returns>
288 public bool IsMarker(int index, HilightType type)
290 foreach(int id in this.Document.Markers.IDs)
292 foreach (Marker m in this.Document.GetMarkers(id, index))
294 if (m.hilight == type)
304 public void AdjustCaret()
306 int row = this.Document.CaretPostion.row;
307 if (row > this.View.LayoutLines.Count - 1)
308 row = this.View.LayoutLines.Count - 1;
309 int col = this.Document.CaretPostion.col;
310 if (col > 0 && col > this.View.LayoutLines[row].Length)
311 col = this.View.LayoutLines[row].Length;
314 int sel_start = this.SelectionStart;
315 int sel_length = this.SelectionLength;
317 this.JumpCaret(row, col);
319 this.Document.Select(sel_start, sel_length);
323 /// キャレットを指定した位置に移動させる
325 /// <param name="index"></param>
326 /// <param name="autoExpand">折り畳みを展開するなら真</param>
327 public void JumpCaret(int index,bool autoExpand = true)
329 if (index < 0 || index > this.Document.Length)
330 throw new ArgumentOutOfRangeException("indexが設定できる範囲を超えています");
331 TextPoint tp = this.View.GetLayoutLineFromIndex(index);
333 this.JumpCaret(tp.row, tp.col,autoExpand);
337 /// キャレットを指定した位置に移動させる
339 /// <param name="row"></param>
340 /// <param name="col"></param>
341 /// <param name="autoExpand">折り畳みを展開するなら真</param>
342 public void JumpCaret(int row, int col, bool autoExpand = true)
344 if (this.Document.FireUpdateEvent == false)
345 throw new InvalidOperationException("");
347 this.View.JumpCaret(row, col,autoExpand);
349 this.View.AdjustCaretAndSrc();
351 this.SelectWithMoveCaret(false);
357 /// <param name="row">行</param>
358 /// <param name="isSelected">選択状態にするかどうか</param>
359 public void JumpToLineHead(int row,bool isSelected)
361 this.View.JumpCaret(row, 0);
362 this.View.AdjustCaretAndSrc();
363 this.SelectWithMoveCaret(isSelected);
369 /// <param name="row">行</param>
370 /// <param name="isSelected">選択状態にするかどうか</param>
371 public void JumpToLineEnd(int row, bool isSelected)
373 this.View.JumpCaret(row, this.View.LayoutLines[row].Length - 1);
374 this.View.AdjustCaretAndSrc();
375 this.SelectWithMoveCaret(isSelected);
381 /// <param name="isSelected"></param>
382 public void JumpToHead(bool isSelected)
384 if (this.View.TryScroll(0, 0))
386 this.View.JumpCaret(0, 0);
387 this.View.AdjustCaretAndSrc();
388 this.SelectWithMoveCaret(isSelected);
394 /// <param name="isSelected"></param>
395 public void JumpToEnd(bool isSelected)
397 int srcRow = this.View.LayoutLines.Count - this.View.LineCountOnScreen - 1;
400 if (this.View.TryScroll(0, srcRow))
402 this.View.JumpCaret(this.View.LayoutLines.Count - 1, 0);
403 this.View.AdjustCaretAndSrc();
404 this.SelectWithMoveCaret(isSelected);
408 public void ScrollByPixel(ScrollDirection dir,int delta, bool isSelected, bool withCaret)
410 if (this.Document.FireUpdateEvent == false)
411 throw new InvalidOperationException("");
413 if (dir == ScrollDirection.Left || dir == ScrollDirection.Right)
415 this.Scroll(dir, delta, isSelected, withCaret);
419 if(dir == ScrollDirection.Up || dir == ScrollDirection.Down)
423 if (noti < this.View.render.emSize.Height)
426 int delta_row = (int)(noti / this.View.render.emSize.Height + 1.0);
430 this.Scroll(dir, delta_row, isSelected, withCaret);
437 /// <param name="dir">方向を指定する</param>
438 /// <param name="delta">スクロールする量。ScrollDirectionの値がUpやDownなら行数。LeftやRightならピクセル単位の値となる</param>
439 /// <param name="isSelected">選択状態にするなら真</param>
440 /// <param name="withCaret">同時にキャレットを移動させるなら真</param>
441 public void Scroll(ScrollDirection dir, int delta, bool isSelected,bool withCaret)
443 if (this.Document.FireUpdateEvent == false)
444 throw new InvalidOperationException("");
445 int toRow = this.View.Src.Row;
446 double toX = this.View.Src.X;
449 case ScrollDirection.Up:
450 toRow = Math.Max(0, this.View.Src.Row - delta);
451 toRow = this.View.AdjustRow(toRow, false);
453 case ScrollDirection.Down:
454 toRow = Math.Min(this.View.Src.Row + delta, this.View.LayoutLines.Count - 1);
455 toRow = this.View.AdjustRow(toRow, true);
457 case ScrollDirection.Left:
460 case ScrollDirection.Right:
464 throw new ArgumentOutOfRangeException();
466 this.Scroll(toX, toRow, isSelected, withCaret);
472 /// <param name="toX">スクロール先の座標</param>
473 /// <param name="toRow">スクロール先の行</param>
474 /// <param name="isSelected">選択状態にするなら真</param>
475 /// <param name="withCaret">同時にキャレットを移動させるなら真</param>
476 public void Scroll(double toX, int toRow, bool isSelected, bool withCaret)
480 this.View.Scroll(toX, toRow);
481 this.View.JumpCaret(toRow, 0);
482 this.View.AdjustCaretAndSrc();
483 this.SelectWithMoveCaret(isSelected);
487 this.View.Scroll(toX, toRow);
490 this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart);
491 this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
497 /// <returns>移動できない場合は真を返す</returns>
498 /// <param name="realLength">負の値なら左側へ、そうでないなら右側へ移動する</param>
499 /// <param name="isSelected">選択範囲とするなら真。そうでないなら偽</param>
500 /// <param name="alignWord">単語単位で移動するなら真。そうでないなら偽</param>
501 public void MoveCaretHorizontical(int realLength, bool isSelected,bool alignWord = false)
504 for (int i = Math.Abs(realLength); i > 0; i--)
506 bool MoveFlow = realLength > 0;
507 if (this.Document.RightToLeft)
508 MoveFlow = !MoveFlow;
509 this.MoveCaretHorizontical(MoveFlow);
512 this.AlignNearestWord(MoveFlow);
514 this.View.AdjustCaretAndSrc(AdjustFlow.Col);
515 this.SelectWithMoveCaret(isSelected);
519 void AlignNearestWord(bool MoveFlow)
521 string str = this.View.LayoutLines[this.Document.CaretPostion.row];
522 while (this.Document.CaretPostion.col > 0 &&
523 this.Document.CaretPostion.col < str.Length &&
524 str[this.Document.CaretPostion.col] != Document.NewLine)
526 if (!Util.IsWordSeparator(str[this.Document.CaretPostion.col]))
528 this.MoveCaretHorizontical(MoveFlow);
533 this.MoveCaretHorizontical(MoveFlow);
542 /// <returns>再描写する必要があるなら真を返す</returns>
543 /// <param name="deltarow">移動量</param>
544 /// <param name="isSelected"></param>
545 public void MoveCaretVertical(int deltarow,bool isSelected)
548 for (int i = Math.Abs(deltarow); i > 0; i--)
549 this.MoveCaretVertical(deltarow > 0);
550 this.View.AdjustCaretAndSrc(AdjustFlow.Both);
551 this.SelectWithMoveCaret(isSelected);
556 /// キャレット位置の文字を一文字削除する
558 public void DoDeleteAction()
560 if (this.SelectionLength != 0)
562 this.SelectedText = "";
566 if (this.Document.FireUpdateEvent == false)
567 throw new InvalidOperationException("");
569 TextPoint CaretPostion = this.Document.CaretPostion;
570 int index = this.View.GetIndexFromLayoutLine(CaretPostion);
572 if (index == this.Document.Length)
575 int lineHeadIndex = this.View.LayoutLines.GetIndexFromLineNumber(CaretPostion.row);
576 int next = this.View.LayoutLines.GetLayout(CaretPostion.row).AlignIndexToNearestCluster(CaretPostion.col, AlignDirection.Forward) + lineHeadIndex;
578 if (this.Document[index] == Document.NewLine)
581 this.Document.Replace(index, next - index, "");
584 public bool IsRectInsertMode()
586 if (!this.RectSelection || this.View.Selections.Count == 0)
588 foreach(Selection sel in this.View.Selections)
597 /// キャレット位置の文字を一文字削除し、キャレット位置を後ろにずらす
599 public void DoBackSpaceAction()
601 if (this.IsRectInsertMode())
603 this.ReplaceBeforeSelectionArea(this.View.Selections, 1, "");
606 else if (this.SelectionLength > 0)
608 this.SelectedText = "";
612 if (this.Document.FireUpdateEvent == false)
613 throw new InvalidOperationException("");
615 TextPoint CurrentPostion = this.Document.CaretPostion;
617 if (CurrentPostion.row == 0 && CurrentPostion.col == 0)
620 int oldIndex = this.View.GetIndexFromLayoutLine(CurrentPostion);
622 int newCol, newIndex;
623 if (CurrentPostion.col > 0)
625 newCol = this.View.LayoutLines.GetLayout(CurrentPostion.row).AlignIndexToNearestCluster(CurrentPostion.col - 1, AlignDirection.Back);
626 newIndex = this.View.GetIndexFromLayoutLine(new TextPoint(CurrentPostion.row, newCol));
630 newIndex = this.View.GetIndexFromLayoutLine(CurrentPostion);
634 this.Document.Replace(newIndex, oldIndex - newIndex, "");
640 public void DoEnterAction()
642 this.DoInputChar('\n');
646 /// キャレット位置に文字を入力し、その分だけキャレットを進める。isInsertModeの値により動作が変わります
648 /// <param name="ch"></param>
649 public void DoInputChar(char ch)
651 this.DoInputString(ch.ToString());
654 string GetIndentSpace(int col_index)
656 int space_count = this.Document.TabStops - (col_index % this.Document.TabStops);
657 return new string(Enumerable.Repeat(' ',space_count).ToArray());
661 /// キャレット位置に文字列を挿入し、その分だけキャレットを進める。isInsertModeの値により動作が変わります
663 /// <param name="str">挿入したい文字列</param>
664 /// <param name="fromTip">真の場合、矩形選択の幅にかかわらず矩形編集モードとして動作します。そうでない場合は選択領域を文字列で置き換えます</param>
665 public void DoInputString(string str,bool fromTip = false)
667 TextPoint CaretPos = this.Document.CaretPostion;
669 if (str == "\t" && this.IndentMode == IndentMode.Space)
670 str = this.GetIndentSpace(CaretPos.col);
672 if (this.IsRectInsertMode())
674 this.ReplaceBeforeSelectionArea(this.View.Selections, 0, str);
677 else if (this.SelectionLength != 0)
679 this.RepleaceSelectionArea(this.View.Selections, str, fromTip);
683 if (this.Document.FireUpdateEvent == false)
684 throw new InvalidOperationException("");
686 int index = this.View.GetIndexFromLayoutLine(this.Document.CaretPostion);
688 if (this.View.InsertMode == false && index < this.Document.Length && this.Document[index] != Document.NewLine)
690 string lineString = this.View.LayoutLines[CaretPos.row];
691 int end = this.View.LayoutLines.GetLayout(CaretPos.row).AlignIndexToNearestCluster(CaretPos.col + str.Length - 1, AlignDirection.Forward);
692 if (end > lineString.Length - 1)
693 end = lineString.Length - 1;
694 end += this.View.LayoutLines.GetIndexFromLineNumber(CaretPos.row);
695 length = end - index;
697 if (str == Document.NewLine.ToString())
699 int lineHeadIndex = this.View.LayoutLines.GetIndexFromLineNumber(CaretPos.row);
700 int lineLength = this.View.LayoutLines.GetLengthFromLineNumber(CaretPos.row);
701 FoldingItem foldingData = this.View.LayoutLines.FoldingCollection.GetFarestHiddenFoldingData(lineHeadIndex, lineLength);
702 if (foldingData != null && !foldingData.Expand && index > foldingData.Start && index <= foldingData.End)
703 index = foldingData.End + 1;
705 this.Document.Replace(index, length, str);
709 /// キャレットの移動に合わせて選択する
711 /// <param name="isSelected">選択状態にするかどうか</param>
713 /// キャレットを移動後、このメソッドを呼び出さない場合、Select()メソッドは正常に機能しません
715 void SelectWithMoveCaret(bool isSelected)
717 if (this.Document.CaretPostion.col < 0 || this.Document.CaretPostion.row < 0)
720 if (this.Document.FireUpdateEvent == false)
721 throw new InvalidOperationException("");
723 int CaretPostion = this.View.GetIndexFromLayoutLine(this.Document.CaretPostion);
725 SelectCollection Selections = this.View.Selections;
728 this.Document.Select(this.Document.AnchorIndex, CaretPostion - this.Document.AnchorIndex);
730 this.Document.AnchorIndex = CaretPostion;
731 this.Document.Select(CaretPostion, 0);
736 /// JumpCaretで移動した位置からキャレットを移動し、選択状態にする
738 /// <param name="tp"></param>
739 public void MoveCaretAndSelect(TextPoint tp)
742 int CaretPostion = this.View.GetIndexFromLayoutLine(tp);
743 this.Document.Select(this.Document.AnchorIndex, CaretPostion - this.Document.AnchorIndex);
744 this.View.JumpCaret(tp.row, tp.col);
745 this.View.AdjustCaretAndSrc();
750 /// グリッパーとキャレットを同時に移動する
752 /// <param name="p">ポインターの座標</param>
753 /// <param name="hittedGripper">動かす対象となるグリッパー</param>
754 /// <returns>移動できた場合は真を返す。そうでなければ偽を返す</returns>
755 /// <remarks>グリッパー内にポインターが存在しない場合、グリッパーはポインターの座標近くの行に移動する</remarks>
756 public bool MoveCaretAndGripper(Point p, Gripper hittedGripper)
758 bool HittedCaret = false;
759 TextPoint tp = this.View.GetTextPointFromPostion(p);
760 if (tp == this.Document.CaretPostion)
765 if (HittedCaret || hittedGripper != null)
767 TextPointSearchRange searchRange;
768 if (this.View.HitTextArea(p.X, p.Y))
769 searchRange = TextPointSearchRange.TextAreaOnly;
770 else if (this.SelectionLength > 0)
771 searchRange = TextPointSearchRange.Full;
775 if (hittedGripper != null)
777 tp = this.View.GetTextPointFromPostion(hittedGripper.AdjustPoint(p), searchRange);
778 if (tp == TextPoint.Null)
780 if (Object.ReferenceEquals(hittedGripper, this.Document.SelectGrippers.BottomRight))
781 this.MoveCaretAndSelect(tp);
782 else if(Object.ReferenceEquals(hittedGripper, this.Document.SelectGrippers.BottomLeft))
783 this.MoveSelectBefore(tp);
787 tp = this.View.GetTextPointFromPostion(p, searchRange);
788 if (tp != TextPoint.Null)
790 this.MoveCaretAndSelect(tp);
797 this.Document.SelectGrippers.BottomLeft.Enabled = this.SelectionLength != 0;
803 public void MoveSelectBefore(TextPoint tp)
807 if (this.IsReverseSelect())
809 NewAnchorIndex = this.View.GetIndexFromLayoutLine(tp);
810 SelectionLength = this.SelectionLength + NewAnchorIndex - this.Document.AnchorIndex;
811 this.Document.Select(this.SelectionStart, SelectionLength);
815 NewAnchorIndex = this.View.GetIndexFromLayoutLine(tp);
816 SelectionLength = this.SelectionLength + this.Document.AnchorIndex - NewAnchorIndex;
817 this.Document.Select(NewAnchorIndex, SelectionLength);
819 this.Document.AnchorIndex = NewAnchorIndex;
825 public void ResetCaretPostion()
831 /// 行単位で移動後のキャレット位置を取得する
833 /// <param name="count">移動量</param>
834 /// <param name="current">現在のキャレット位置</param>
835 /// <returns>移動後のキャレット位置</returns>
836 public TextPoint GetTextPointAfterMoveLine(int count, TextPoint current)
838 int row = current.row + count;
842 else if (row >= this.View.LayoutLines.Count)
843 row = this.View.LayoutLines.Count - 1;
845 row = this.View.AdjustRow(row, count > 0);
847 double colpos = this.View.GetColPostionFromIndex(current.row, current.col);
848 int col = this.View.GetIndexFromColPostion(row, colpos);
850 return new TextPoint(row, col);
854 /// 選択文字列のインデントを一つ増やす
856 public void UpIndent()
858 if (this.RectSelection || this.SelectionLength == 0)
860 int selectionStart = this.SelectionStart;
861 string insertStr = this.IndentMode == IndentMode.Space ? this.GetIndentSpace(0) : "\t";
862 string text = this.InsertLineHead(GetTextFromLineSelectArea(this.View.Selections), insertStr);
863 this.RepleaceSelectionArea(this.View.Selections,text);
864 this.Document.Select(selectionStart, text.Length);
868 /// 選択文字列のインデントを一つ減らす
870 public void DownIndent()
872 if (this.RectSelection || this.SelectionLength == 0)
874 int selectionStart = this.SelectionStart;
875 string insertStr = this.IndentMode == IndentMode.Space ? this.GetIndentSpace(0) : "\t";
876 string text = this.RemoveLineHead(GetTextFromLineSelectArea(this.View.Selections), insertStr);
877 this.RepleaceSelectionArea(this.View.Selections, text);
878 this.Document.Select(selectionStart, text.Length);
881 string InsertLineHead(string s, string str)
883 string[] lines = s.Split(new string[] { Document.NewLine.ToString() }, StringSplitOptions.None);
884 StringBuilder output = new StringBuilder();
885 for (int i = 0; i < lines.Length; i++)
887 if(lines[i].Length > 0)
888 output.Append(str + lines[i] + Document.NewLine);
889 else if(i < lines.Length - 1)
890 output.Append(lines[i] + Document.NewLine);
892 return output.ToString();
895 public string RemoveLineHead(string s, string str)
897 string[] lines = s.Split(new string[] { Document.NewLine.ToString() }, StringSplitOptions.None);
898 StringBuilder output = new StringBuilder();
899 for (int i = 0; i < lines.Length; i++)
901 if (lines[i].StartsWith(str))
902 output.Append(lines[i].Substring(1) + Document.NewLine);
903 else if (i < lines.Length - 1)
904 output.Append(lines[i] + Document.NewLine);
906 return output.ToString();
912 /// <param name="isMoveNext">真なら1文字すすめ、そうでなければ戻す</param>
913 /// <remarks>このメソッドを呼び出した後でScrollToCaretメソッドとSelectWithMoveCaretメソッドを呼び出す必要があります</remarks>
914 void MoveCaretHorizontical(bool isMoveNext)
916 if (this.Document.FireUpdateEvent == false)
917 throw new InvalidOperationException("");
918 int delta = isMoveNext ? 0 : -1;
919 int prevcol = this.Document.CaretPostion.col;
920 int col = this.Document.CaretPostion.col + delta;
921 string lineString = this.View.LayoutLines[this.Document.CaretPostion.row];
922 if (col < 0 || this.Document.CaretPostion.row >= this.View.LayoutLines.Count)
924 if (this.Document.CaretPostion.row == 0)
929 this.MoveCaretVertical(false);
930 this.View.AdjustCaretAndSrc(AdjustFlow.Row); //この段階で調整しないとスクロールされない
931 col = this.View.LayoutLines.GetLengthFromLineNumber(this.Document.CaretPostion.row) - 1; //最終行以外はすべて改行コードが付くはず
933 else if (col >= lineString.Length || lineString[col] == Document.NewLine)
935 if (this.Document.CaretPostion.row < this.View.LayoutLines.Count - 1)
937 this.MoveCaretVertical(true);
938 this.View.AdjustCaretAndSrc(AdjustFlow.Row); //この段階で調整しないとスクロールされない
944 AlignDirection direction = isMoveNext ? AlignDirection.Forward : AlignDirection.Back;
945 col = this.View.LayoutLines.GetLayout(this.Document.CaretPostion.row).AlignIndexToNearestCluster(col, direction);
948 this.View.JumpCaret(this.Document.CaretPostion.row, col,false);
954 /// <param name="isMoveNext">プラス方向に移動するなら真</param>
955 /// <remarks>このメソッドを呼び出した後でScrollToCaretメソッドとSelectWithMoveCaretメソッドを呼び出す必要があります</remarks>
956 void MoveCaretVertical(bool isMoveNext)
958 if (this.Document.FireUpdateEvent == false)
959 throw new InvalidOperationException("");
961 TextPoint nextPoint = this.GetTextPointAfterMoveLine(isMoveNext ? 1 : -1, this.Document.CaretPostion);
963 this.View.JumpCaret(nextPoint.row, nextPoint.col,false);
966 private void ReplaceBeforeSelectionArea(SelectCollection Selections, int removeLength, string insertStr)
968 if (removeLength == 0 && insertStr.Length == 0)
971 if (this.RectSelection == false || this.Document.FireUpdateEvent == false)
972 throw new InvalidOperationException();
974 SelectCollection temp = this.View.Selections;
975 int selectStart = temp.First().start;
976 int selectEnd = temp.Last().start + temp.Last().length;
978 //ドキュメント操作後に行うとうまくいかないので、あらかじめ取得しておく
979 TextPoint start = this.View.LayoutLines.GetTextPointFromIndex(selectStart);
980 TextPoint end = this.View.LayoutLines.GetTextPointFromIndex(selectEnd);
982 bool reverse = temp.First().start > temp.Last().start;
984 int lineHeadIndex = this.View.LayoutLines.GetIndexFromLineNumber(this.View.LayoutLines.GetLineNumberFromIndex(selectStart));
985 if (selectStart - removeLength < lineHeadIndex)
988 this.Document.UndoManager.BeginUndoGroup();
989 this.Document.FireUpdateEvent = false;
993 for (int i = 0; i < temp.Count; i++)
995 this.ReplaceBeforeSelection(temp[i], removeLength, insertStr);
1000 for (int i = temp.Count - 1; i >= 0; i--)
1002 this.ReplaceBeforeSelection(temp[i], removeLength, insertStr);
1006 this.Document.FireUpdateEvent = true;
1007 this.Document.UndoManager.EndUndoGroup();
1009 int delta = insertStr.Length - removeLength;
1014 this.JumpCaret(start.row, start.col);
1016 this.JumpCaret(end.row, end.col);
1018 this.Document.Select(start, 0, end.row - start.row);
1021 private void ReplaceBeforeSelection(Selection sel, int removeLength, string insertStr)
1023 sel = Util.NormalizeIMaker<Selection>(sel);
1024 this.Document.Replace(sel.start - removeLength, removeLength, insertStr);
1027 private void RepleaceSelectionArea(SelectCollection Selections, string value,bool updateInsertPoint = false)
1032 if (this.RectSelection == false)
1034 Selection sel = Selection.Create(this.Document.AnchorIndex, 0);
1035 if (Selections.Count > 0)
1036 sel = Util.NormalizeIMaker<Selection>(this.View.Selections.First());
1038 this.Document.Replace(sel.start, sel.length, value);
1042 if (this.Document.FireUpdateEvent == false)
1043 throw new InvalidOperationException("");
1045 int StartIndex = this.SelectionStart;
1047 SelectCollection newInsertPoint = new SelectCollection();
1049 if (this.SelectionLength == 0)
1053 this.Document.UndoManager.BeginUndoGroup();
1055 this.Document.FireUpdateEvent = false;
1057 string[] line = value.Split(new string[] { Document.NewLine.ToString() }, StringSplitOptions.RemoveEmptyEntries);
1059 TextPoint Current = this.View.GetLayoutLineFromIndex(this.SelectionStart);
1061 for (i = 0; i < line.Length && Current.row < this.View.LayoutLines.Count; i++, Current.row++)
1063 if (Current.col > this.View.LayoutLines[Current.row].Length)
1064 Current.col = this.View.LayoutLines[Current.row].Length;
1065 StartIndex = this.View.GetIndexFromLayoutLine(Current);
1066 this.Document.Replace(StartIndex, 0, line[i]);
1067 StartIndex += line[i].Length;
1070 for (; i < line.Length; i++)
1072 StartIndex = this.Document.Length;
1073 string str = Document.NewLine + line[i];
1074 this.Document.Replace(StartIndex, 0, str);
1075 StartIndex += str.Length;
1078 this.Document.FireUpdateEvent = true;
1080 this.Document.UndoManager.EndUndoGroup();
1084 SelectCollection temp = new SelectCollection(this.View.Selections); //コピーしないとReplaceCommandを呼び出した段階で書き換えられてしまう
1086 this.Document.UndoManager.BeginUndoGroup();
1088 this.Document.FireUpdateEvent = false;
1090 if (temp.First().start < temp.Last().start)
1092 for (int i = temp.Count - 1; i >= 0; i--)
1094 Selection sel = Util.NormalizeIMaker<Selection>(temp[i]);
1096 StartIndex = sel.start;
1098 this.Document.Replace(sel.start, sel.length, value);
1100 newInsertPoint.Add(Selection.Create(sel.start + (value.Length - sel.length) * i,0));
1105 for (int i = 0; i < temp.Count; i++)
1107 Selection sel = Util.NormalizeIMaker<Selection>(temp[i]);
1109 StartIndex = sel.start;
1111 this.Document.Replace(sel.start, sel.length, value);
1113 newInsertPoint.Add(Selection.Create(sel.start + (value.Length - sel.length) * i, 0));
1117 this.Document.FireUpdateEvent = true;
1119 this.Document.UndoManager.EndUndoGroup();
1121 this.JumpCaret(StartIndex);
1122 if (updateInsertPoint && newInsertPoint.Count > 0)
1123 this.View.Selections = newInsertPoint;
1126 private string GetTextFromLineSelectArea(SelectCollection Selections)
1128 Selection sel = Util.NormalizeIMaker<Selection>(Selections.First());
1130 string str = this.Document.ToString(sel.start, sel.length);
1135 string GetTextFromRectangleSelectArea(SelectCollection Selections)
1137 StringBuilder temp = new StringBuilder();
1138 if (Selections.First().start < Selections.Last().start)
1140 for (int i = 0; i < this.View.Selections.Count; i++)
1142 Selection sel = Util.NormalizeIMaker<Selection>(Selections[i]);
1144 string str = this.Document.ToString(sel.start, sel.length);
1145 if (str.IndexOf(Environment.NewLine) == -1)
1146 temp.AppendLine(str);
1153 for (int i = this.View.Selections.Count - 1; i >= 0; i--)
1155 Selection sel = Util.NormalizeIMaker<Selection>(Selections[i]);
1157 string str = this.Document.ToString(sel.start, sel.length).Replace(Document.NewLine.ToString(), Environment.NewLine);
1158 if (str.IndexOf(Environment.NewLine) == -1)
1159 temp.AppendLine(str);
1164 return temp.ToString();
1167 void View_LineBreakChanged(object sender, EventArgs e)
1173 void View_PageBoundChanged(object sender, EventArgs e)
1175 if (this.Document.LineBreak == LineBreakMethod.PageBound && this.View.PageBound.Width - this.View.LineBreakingMarginWidth > 0)
1176 this.Document.PerformLayout();
1180 void render_ChangedRenderResource(object sender, ChangedRenderRsourceEventArgs e)
1182 if (e.type == ResourceType.Font)
1184 if (this.Document.LineBreak == LineBreakMethod.PageBound)
1185 this.Document.PerformLayout();
1188 if (e.type == ResourceType.InlineChar)
1190 int oldLineCountOnScreen = this.View.LineCountOnScreen;
1191 this.View.CalculateLineCountOnScreen();
1192 if(this.View.LineCountOnScreen != oldLineCountOnScreen)
1197 void Document_Update(object sender, DocumentUpdateEventArgs e)
1201 case UpdateType.Replace:
1202 this.JumpCaret(e.startIndex + e.insertLength,true);
1204 case UpdateType.Clear:
1205 this.JumpCaret(0,0, false);