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
59 public Controller(Document doc, EditView view)
63 this.View.render.ChangedRenderResource += render_ChangedRenderResource;
64 this.View.PageBoundChanged += View_PageBoundChanged;
65 //this.Document.Clear();
68 public Document Document
72 return this._Document;
76 //メモリリークを防ぐためにパンドラーを除く
77 if (this._Document != null)
79 this._Document.Update -= Document_Update;
80 this._Document.StatusUpdate -= Document_StatusChanged;
81 this._Document.SelectionChanged -= Document_SelectionChanged;
82 this._Document.PerformLayouted -= View_LineBreakChanged;
83 this._Document.CaretChanged -= _Document_CaretChanged;
86 this._Document = value;
88 this._Document.Update += new DocumentUpdateEventHandler(Document_Update);
89 this._Document.StatusUpdate += Document_StatusChanged;
90 this._Document.SelectionChanged += Document_SelectionChanged;
91 this._Document.PerformLayouted += View_LineBreakChanged;
92 this._Document.CaretChanged += _Document_CaretChanged;
96 private void _Document_CaretChanged(object sender, EventArgs e)
98 TextPoint pos = this.Document.CaretPostion;
99 this.JumpCaret(pos.row, pos.col);
102 private void Document_SelectionChanged(object sender, EventArgs e)
104 if (this.IsReverseSelect())
106 this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart);
107 this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
111 this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart);
112 this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
116 void Document_StatusChanged(object sender,EventArgs e)
122 /// 矩形選択モードなら真を返し、そうでない場合は偽を返す
124 public bool RectSelection
126 get { return this.Document.RectSelection; }
127 set { this.Document.RectSelection = value; }
133 public IndentMode IndentMode
135 get { return this.Document.IndentMode; }
136 set { this.Document.IndentMode = value; }
142 /// <remarks>SelectionLengthが0の場合、キャレット位置を表します</remarks>
143 public int SelectionStart
147 if (this.View.Selections.Count == 0)
148 return this.Document.AnchorIndex;
150 return this.View.Selections.First().start;
157 /// <remarks>矩形選択モードの場合、選択範囲の文字数ではなく、開始位置から終了位置までの長さとなります</remarks>
158 public int SelectionLength
162 if (this.View.Selections.Count == 0)
164 Selection last = this.View.Selections.Last();
165 return last.start + last.length - this.SelectionStart;
173 /// 未選択状態で代入したときは追加され、そうでない場合は選択範囲の文字列と置き換えられます。
175 public string SelectedText
179 if (this.View.LayoutLines.Count == 0 || this.View.Selections.Count == 0)
181 if (this.RectSelection)
182 return GetTextFromRectangleSelectArea(this.View.Selections);
184 return GetTextFromLineSelectArea(this.View.Selections).Replace(Document.NewLine.ToString(), Environment.NewLine);
188 if (this.Document.FireUpdateEvent == false)
189 throw new InvalidOperationException("");
192 this.RepleaceSelectionArea(this.View.Selections, value.Replace(Environment.NewLine,Document.NewLine.ToString()));
197 /// 選択範囲が逆転しているかどうかを判定する
199 /// <returns>逆転しているなら真を返す</returns>
200 public bool IsReverseSelect()
202 int index = this.View.LayoutLines.GetIndexFromTextPoint(this.Document.CaretPostion);
203 return index < this.Document.AnchorIndex;
207 /// 選択範囲内のUTF32コードポイントを文字列に変換します
209 /// <returns>成功した場合は真。そうでない場合は偽を返す</returns>
210 public bool ConvertToChar()
212 if (this.SelectionLength == 0 || this.RectSelection)
214 string str = this.Document.ToString(this.SelectionStart, this.SelectionLength);
215 string[] codes = str.Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries);
216 StringBuilder result = new StringBuilder();
217 foreach (string code in codes)
222 if (Int32.TryParse(code.TrimStart('U'),NumberStyles.HexNumber,null, out utf32_code))
223 result.Append(Char.ConvertFromUtf32(utf32_code));
227 this.Document.Replace(this.SelectionStart, this.SelectionLength, result.ToString());
232 /// 選択文字列をUTF32のコードポイントに変換します
234 public void ConvertToCodePoint()
236 if (this.SelectionLength == 0 || this.RectSelection)
238 string str = this.Document.ToString(this.SelectionStart, this.SelectionLength);
239 StringInfo info = new StringInfo(str);
240 StringBuilder result = new StringBuilder();
241 for (int i = 0; i < str.Length;)
243 int utf32_code = Char.ConvertToUtf32(str, i);
244 result.Append("U" + Convert.ToString(utf32_code,16));
246 if(Char.IsHighSurrogate(str[i]))
251 this.Document.Replace(this.SelectionStart, this.SelectionLength, result.ToString());
257 public void DeSelectAll()
259 if (this.Document.FireUpdateEvent == false)
260 throw new InvalidOperationException("");
262 this.View.Selections.Clear();
268 /// <param name="tp"></param>
269 /// <param name="type"></param>
270 /// <returns>真ならマーカーがある</returns>
271 public bool IsMarker(TextPoint tp,HilightType type)
273 if (this.Document.FireUpdateEvent == false)
274 throw new InvalidOperationException("");
275 int index = this.View.LayoutLines.GetIndexFromTextPoint(tp);
276 return this.IsMarker(index, type);
282 /// <param name="index"></param>
283 /// <param name="type"></param>
284 /// <returns>真ならマーカーがある</returns>
285 public bool IsMarker(int index, HilightType type)
287 foreach(int id in this.Document.Markers.IDs)
289 foreach (Marker m in this.Document.GetMarkers(id, index))
291 if (m.hilight == type)
301 public void AdjustCaret()
303 int row = this.Document.CaretPostion.row;
304 if (row > this.View.LayoutLines.Count - 1)
305 row = this.View.LayoutLines.Count - 1;
306 int col = this.Document.CaretPostion.col;
307 if (col > 0 && col > this.View.LayoutLines[row].Length)
308 col = this.View.LayoutLines[row].Length;
311 int sel_start = this.SelectionStart;
312 int sel_length = this.SelectionLength;
314 this.JumpCaret(row, col);
316 this.Document.Select(sel_start, sel_length);
320 /// キャレットを指定した位置に移動させる
322 /// <param name="index"></param>
323 /// <param name="autoExpand">折り畳みを展開するなら真</param>
324 public void JumpCaret(int index,bool autoExpand = true)
326 if (index < 0 || index > this.Document.Length)
327 throw new ArgumentOutOfRangeException("indexが設定できる範囲を超えています");
328 TextPoint tp = this.View.GetLayoutLineFromIndex(index);
330 this.JumpCaret(tp.row, tp.col,autoExpand);
334 /// キャレットを指定した位置に移動させる
336 /// <param name="row"></param>
337 /// <param name="col"></param>
338 /// <param name="autoExpand">折り畳みを展開するなら真</param>
339 public void JumpCaret(int row, int col, bool autoExpand = true)
341 if (this.Document.FireUpdateEvent == false)
342 throw new InvalidOperationException("");
344 this.View.JumpCaret(row, col,autoExpand);
346 this.View.AdjustCaretAndSrc();
348 this.SelectWithMoveCaret(false);
354 /// <param name="row">行</param>
355 /// <param name="isSelected">選択状態にするかどうか</param>
356 public void JumpToLineHead(int row,bool isSelected)
358 this.View.JumpCaret(row, 0);
359 this.View.AdjustCaretAndSrc();
360 this.SelectWithMoveCaret(isSelected);
366 /// <param name="row">行</param>
367 /// <param name="isSelected">選択状態にするかどうか</param>
368 public void JumpToLineEnd(int row, bool isSelected)
370 this.View.JumpCaret(row, this.View.LayoutLines[row].Length - 1);
371 this.View.AdjustCaretAndSrc();
372 this.SelectWithMoveCaret(isSelected);
378 /// <param name="isSelected"></param>
379 public void JumpToHead(bool isSelected)
381 if (this.View.TryScroll(0, 0))
383 this.View.JumpCaret(0, 0);
384 this.View.AdjustCaretAndSrc();
385 this.SelectWithMoveCaret(isSelected);
391 /// <param name="isSelected"></param>
392 public void JumpToEnd(bool isSelected)
394 int srcRow = this.View.LayoutLines.Count - this.View.LineCountOnScreen - 1;
397 if (this.View.TryScroll(0, srcRow))
399 this.View.JumpCaret(this.View.LayoutLines.Count - 1, 0);
400 this.View.AdjustCaretAndSrc();
401 this.SelectWithMoveCaret(isSelected);
405 public void ScrollByPixel(ScrollDirection dir,int delta, bool isSelected, bool withCaret)
407 if (this.Document.FireUpdateEvent == false)
408 throw new InvalidOperationException("");
410 if (dir == ScrollDirection.Left || dir == ScrollDirection.Right)
412 this.Scroll(dir, delta, isSelected, withCaret);
416 if(dir == ScrollDirection.Up || dir == ScrollDirection.Down)
420 if (noti < this.View.render.emSize.Height)
423 int delta_row = (int)(noti / this.View.render.emSize.Height + 1.0);
427 this.Scroll(dir, delta_row, isSelected, withCaret);
434 /// <param name="dir">方向を指定する</param>
435 /// <param name="delta">スクロールする量。ScrollDirectionの値がUpやDownなら行数。LeftやRightならピクセル単位の値となる</param>
436 /// <param name="isSelected">選択状態にするなら真</param>
437 /// <param name="withCaret">同時にキャレットを移動させるなら真</param>
438 public void Scroll(ScrollDirection dir, int delta, bool isSelected,bool withCaret)
440 if (this.Document.FireUpdateEvent == false)
441 throw new InvalidOperationException("");
442 int toRow = this.View.Src.Row;
443 double toX = this.View.Src.X;
446 case ScrollDirection.Up:
447 toRow = Math.Max(0, this.View.Src.Row - delta);
448 toRow = this.View.AdjustRow(toRow, false);
450 case ScrollDirection.Down:
451 toRow = Math.Min(this.View.Src.Row + delta, this.View.LayoutLines.Count - 1);
452 toRow = this.View.AdjustRow(toRow, true);
454 case ScrollDirection.Left:
457 case ScrollDirection.Right:
461 throw new ArgumentOutOfRangeException();
463 this.Scroll(toX, toRow, isSelected, withCaret);
469 /// <param name="toX">スクロール先の座標</param>
470 /// <param name="toRow">スクロール先の行</param>
471 /// <param name="isSelected">選択状態にするなら真</param>
472 /// <param name="withCaret">同時にキャレットを移動させるなら真</param>
473 public void Scroll(double toX, int toRow, bool isSelected, bool withCaret)
477 this.View.Scroll(toX, toRow);
478 this.View.JumpCaret(toRow, 0);
479 this.View.AdjustCaretAndSrc();
480 this.SelectWithMoveCaret(isSelected);
484 this.View.Scroll(toX, toRow);
487 this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart);
488 this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
494 /// <returns>移動できない場合は真を返す</returns>
495 /// <param name="realLength">負の値なら左側へ、そうでないなら右側へ移動する</param>
496 /// <param name="isSelected">選択範囲とするなら真。そうでないなら偽</param>
497 /// <param name="alignWord">単語単位で移動するなら真。そうでないなら偽</param>
498 public void MoveCaretHorizontical(int realLength, bool isSelected,bool alignWord = false)
500 for (int i = Math.Abs(realLength); i > 0; i--)
502 bool MoveFlow = realLength > 0;
503 if (this.Document.RightToLeft)
504 MoveFlow = !MoveFlow;
505 this.MoveCaretHorizontical(MoveFlow);
508 this.AlignNearestWord(MoveFlow);
510 this.View.AdjustCaretAndSrc(AdjustFlow.Col);
511 this.SelectWithMoveCaret(isSelected);
514 void AlignNearestWord(bool MoveFlow)
516 string str = this.View.LayoutLines[this.Document.CaretPostion.row];
517 while (this.Document.CaretPostion.col > 0 &&
518 this.Document.CaretPostion.col < str.Length &&
519 str[this.Document.CaretPostion.col] != Document.NewLine)
521 if (!Util.IsWordSeparator(str[this.Document.CaretPostion.col]))
523 this.MoveCaretHorizontical(MoveFlow);
528 this.MoveCaretHorizontical(MoveFlow);
537 /// <returns>再描写する必要があるなら真を返す</returns>
538 /// <param name="deltarow">移動量</param>
539 /// <param name="isSelected"></param>
540 public void MoveCaretVertical(int deltarow,bool isSelected)
542 for (int i = Math.Abs(deltarow); i > 0; i--)
543 this.MoveCaretVertical(deltarow > 0);
544 this.View.AdjustCaretAndSrc(AdjustFlow.Both);
545 this.SelectWithMoveCaret(isSelected);
549 /// キャレット位置の文字を一文字削除する
551 public void DoDeleteAction()
553 if (this.SelectionLength != 0)
555 this.SelectedText = "";
559 if (this.Document.FireUpdateEvent == false)
560 throw new InvalidOperationException("");
562 TextPoint CaretPostion = this.Document.CaretPostion;
563 int index = this.View.GetIndexFromLayoutLine(CaretPostion);
565 if (index == this.Document.Length)
568 int lineHeadIndex = this.View.LayoutLines.GetIndexFromLineNumber(CaretPostion.row);
569 int next = this.View.LayoutLines.GetLayout(CaretPostion.row).AlignIndexToNearestCluster(CaretPostion.col, AlignDirection.Forward) + lineHeadIndex;
571 if (this.Document[index] == Document.NewLine)
574 this.Document.Replace(index, next - index, "", true);
577 public bool IsRectInsertMode()
579 if (!this.RectSelection || this.View.Selections.Count == 0)
581 foreach(Selection sel in this.View.Selections)
590 /// キャレット位置の文字を一文字削除し、キャレット位置を後ろにずらす
592 public void DoBackSpaceAction()
594 if (this.IsRectInsertMode())
596 this.ReplaceBeforeSelectionArea(this.View.Selections, 1, "");
599 else if (this.SelectionLength > 0)
601 this.SelectedText = "";
605 if (this.Document.FireUpdateEvent == false)
606 throw new InvalidOperationException("");
608 TextPoint CurrentPostion = this.Document.CaretPostion;
610 if (CurrentPostion.row == 0 && CurrentPostion.col == 0)
613 int oldIndex = this.View.GetIndexFromLayoutLine(CurrentPostion);
615 int newCol, newIndex;
616 if (CurrentPostion.col > 0)
618 newCol = this.View.LayoutLines.GetLayout(CurrentPostion.row).AlignIndexToNearestCluster(CurrentPostion.col - 1, AlignDirection.Back);
619 newIndex = this.View.GetIndexFromLayoutLine(new TextPoint(CurrentPostion.row, newCol));
623 newIndex = this.View.GetIndexFromLayoutLine(CurrentPostion);
627 this.Document.Replace(newIndex, oldIndex - newIndex, "", true);
633 public void DoEnterAction()
635 this.DoInputChar('\n');
639 /// キャレット位置に文字を入力し、その分だけキャレットを進める。isInsertModeの値により動作が変わります
641 /// <param name="ch"></param>
642 public void DoInputChar(char ch)
644 this.DoInputString(ch.ToString());
647 string GetIndentSpace(int col_index)
649 int space_count = this.Document.TabStops - (col_index % this.Document.TabStops);
650 return new string(Enumerable.Repeat(' ',space_count).ToArray());
654 /// キャレット位置に文字列を挿入し、その分だけキャレットを進める。isInsertModeの値により動作が変わります
656 /// <param name="str">挿入したい文字列</param>
657 /// <param name="fromTip">真の場合、矩形選択の幅にかかわらず矩形編集モードとして動作します。そうでない場合は選択領域を文字列で置き換えます</param>
658 public void DoInputString(string str,bool fromTip = false)
660 TextPoint CaretPos = this.Document.CaretPostion;
662 if (str == "\t" && this.IndentMode == IndentMode.Space)
663 str = this.GetIndentSpace(CaretPos.col);
665 if (this.IsRectInsertMode())
667 this.ReplaceBeforeSelectionArea(this.View.Selections, 0, str);
670 else if (this.SelectionLength != 0)
672 this.RepleaceSelectionArea(this.View.Selections, str, fromTip);
676 if (this.Document.FireUpdateEvent == false)
677 throw new InvalidOperationException("");
679 int index = this.View.GetIndexFromLayoutLine(this.Document.CaretPostion);
681 if (this.View.InsertMode == false && index < this.Document.Length && this.Document[index] != Document.NewLine)
683 string lineString = this.View.LayoutLines[CaretPos.row];
684 int end = this.View.LayoutLines.GetLayout(CaretPos.row).AlignIndexToNearestCluster(CaretPos.col + str.Length - 1, AlignDirection.Forward);
685 if (end > lineString.Length - 1)
686 end = lineString.Length - 1;
687 end += this.View.LayoutLines.GetIndexFromLineNumber(CaretPos.row);
688 length = end - index;
690 if (str == Document.NewLine.ToString())
692 int lineHeadIndex = this.View.LayoutLines.GetIndexFromLineNumber(CaretPos.row);
693 int lineLength = this.View.LayoutLines.GetLengthFromLineNumber(CaretPos.row);
694 FoldingItem foldingData = this.View.LayoutLines.FoldingCollection.GetFarestHiddenFoldingData(lineHeadIndex, lineLength);
695 if (foldingData != null && !foldingData.Expand && index > foldingData.Start && index <= foldingData.End)
696 index = foldingData.End + 1;
698 this.Document.Replace(index, length, str, true);
702 /// キャレットの移動に合わせて選択する
704 /// <param name="isSelected">選択状態にするかどうか</param>
706 /// キャレットを移動後、このメソッドを呼び出さない場合、Select()メソッドは正常に機能しません
708 void SelectWithMoveCaret(bool isSelected)
710 if (this.Document.CaretPostion.col < 0 || this.Document.CaretPostion.row < 0)
713 if (this.Document.FireUpdateEvent == false)
714 throw new InvalidOperationException("");
716 int CaretPostion = this.View.GetIndexFromLayoutLine(this.Document.CaretPostion);
718 SelectCollection Selections = this.View.Selections;
721 this.Document.Select(this.Document.AnchorIndex, CaretPostion - this.Document.AnchorIndex);
723 this.Document.AnchorIndex = CaretPostion;
724 this.Document.Select(CaretPostion, 0);
729 /// JumpCaretで移動した位置からキャレットを移動し、選択状態にする
731 /// <param name="tp"></param>
732 public void MoveCaretAndSelect(TextPoint tp)
734 int CaretPostion = this.View.GetIndexFromLayoutLine(tp);
735 this.Document.Select(this.Document.AnchorIndex, CaretPostion - this.Document.AnchorIndex);
736 this.View.JumpCaret(tp.row, tp.col);
737 this.View.AdjustCaretAndSrc();
741 /// グリッパーとキャレットを同時に移動する
743 /// <param name="p">ポインターの座標</param>
744 /// <param name="hittedGripper">動かす対象となるグリッパー</param>
745 /// <returns>移動できた場合は真を返す。そうでなければ偽を返す</returns>
746 /// <remarks>グリッパー内にポインターが存在しない場合、グリッパーはポインターの座標近くの行に移動する</remarks>
747 public bool MoveCaretAndGripper(Point p, Gripper hittedGripper)
749 bool HittedCaret = false;
750 TextPoint tp = this.View.GetTextPointFromPostion(p);
751 if (tp == this.Document.CaretPostion)
756 if (HittedCaret || hittedGripper != null)
758 TextPointSearchRange searchRange;
759 if (this.View.HitTextArea(p.X, p.Y))
760 searchRange = TextPointSearchRange.TextAreaOnly;
761 else if (this.SelectionLength > 0)
762 searchRange = TextPointSearchRange.Full;
766 if (hittedGripper != null)
768 tp = this.View.GetTextPointFromPostion(hittedGripper.AdjustPoint(p), searchRange);
769 if (tp == TextPoint.Null)
771 if (Object.ReferenceEquals(hittedGripper, this.Document.SelectGrippers.BottomRight))
772 this.MoveCaretAndSelect(tp);
773 else if(Object.ReferenceEquals(hittedGripper, this.Document.SelectGrippers.BottomLeft))
774 this.MoveSelectBefore(tp);
778 tp = this.View.GetTextPointFromPostion(p, searchRange);
779 if (tp != TextPoint.Null)
781 this.MoveCaretAndSelect(tp);
788 this.Document.SelectGrippers.BottomLeft.Enabled = this.SelectionLength != 0;
794 public void MoveSelectBefore(TextPoint tp)
798 if (this.IsReverseSelect())
800 NewAnchorIndex = this.View.GetIndexFromLayoutLine(tp);
801 SelectionLength = this.SelectionLength + NewAnchorIndex - this.Document.AnchorIndex;
802 this.Document.Select(this.SelectionStart, SelectionLength);
806 NewAnchorIndex = this.View.GetIndexFromLayoutLine(tp);
807 SelectionLength = this.SelectionLength + this.Document.AnchorIndex - NewAnchorIndex;
808 this.Document.Select(NewAnchorIndex, SelectionLength);
810 this.Document.AnchorIndex = NewAnchorIndex;
816 public void ResetCaretPostion()
822 /// 行単位で移動後のキャレット位置を取得する
824 /// <param name="count">移動量</param>
825 /// <param name="current">現在のキャレット位置</param>
826 /// <returns>移動後のキャレット位置</returns>
827 public TextPoint GetTextPointAfterMoveLine(int count, TextPoint current)
829 int row = current.row + count;
833 else if (row >= this.View.LayoutLines.Count)
834 row = this.View.LayoutLines.Count - 1;
836 row = this.View.AdjustRow(row, count > 0);
838 double colpos = this.View.GetColPostionFromIndex(current.row, current.col);
839 int col = this.View.GetIndexFromColPostion(row, colpos);
841 return new TextPoint(row, col);
845 /// 選択文字列のインデントを一つ増やす
847 public void UpIndent()
849 if (this.RectSelection || this.SelectionLength == 0)
851 int selectionStart = this.SelectionStart;
852 string insertStr = this.IndentMode == IndentMode.Space ? this.GetIndentSpace(0) : "\t";
853 string text = this.InsertLineHead(GetTextFromLineSelectArea(this.View.Selections), insertStr);
854 this.RepleaceSelectionArea(this.View.Selections,text);
855 this.Document.Select(selectionStart, text.Length);
859 /// 選択文字列のインデントを一つ減らす
861 public void DownIndent()
863 if (this.RectSelection || this.SelectionLength == 0)
865 int selectionStart = this.SelectionStart;
866 string insertStr = this.IndentMode == IndentMode.Space ? this.GetIndentSpace(0) : "\t";
867 string text = this.RemoveLineHead(GetTextFromLineSelectArea(this.View.Selections), insertStr);
868 this.RepleaceSelectionArea(this.View.Selections, text);
869 this.Document.Select(selectionStart, text.Length);
872 string InsertLineHead(string s, string str)
874 string[] lines = s.Split(new string[] { Document.NewLine.ToString() }, StringSplitOptions.None);
875 StringBuilder output = new StringBuilder();
876 for (int i = 0; i < lines.Length; i++)
878 if(lines[i].Length > 0)
879 output.Append(str + lines[i] + Document.NewLine);
880 else if(i < lines.Length - 1)
881 output.Append(lines[i] + Document.NewLine);
883 return output.ToString();
886 public string RemoveLineHead(string s, string str)
888 string[] lines = s.Split(new string[] { Document.NewLine.ToString() }, StringSplitOptions.None);
889 StringBuilder output = new StringBuilder();
890 for (int i = 0; i < lines.Length; i++)
892 if (lines[i].StartsWith(str))
893 output.Append(lines[i].Substring(1) + Document.NewLine);
894 else if (i < lines.Length - 1)
895 output.Append(lines[i] + Document.NewLine);
897 return output.ToString();
903 /// <param name="isMoveNext">真なら1文字すすめ、そうでなければ戻す</param>
904 /// <remarks>このメソッドを呼び出した後でScrollToCaretメソッドとSelectWithMoveCaretメソッドを呼び出す必要があります</remarks>
905 void MoveCaretHorizontical(bool isMoveNext)
907 if (this.Document.FireUpdateEvent == false)
908 throw new InvalidOperationException("");
909 int delta = isMoveNext ? 0 : -1;
910 int prevcol = this.Document.CaretPostion.col;
911 int col = this.Document.CaretPostion.col + delta;
912 string lineString = this.View.LayoutLines[this.Document.CaretPostion.row];
913 if (col < 0 || this.Document.CaretPostion.row >= this.View.LayoutLines.Count)
915 if (this.Document.CaretPostion.row == 0)
920 this.MoveCaretVertical(false);
921 this.View.AdjustCaretAndSrc(AdjustFlow.Row); //この段階で調整しないとスクロールされない
922 col = this.View.LayoutLines.GetLengthFromLineNumber(this.Document.CaretPostion.row) - 1; //最終行以外はすべて改行コードが付くはず
924 else if (col >= lineString.Length || lineString[col] == Document.NewLine)
926 if (this.Document.CaretPostion.row < this.View.LayoutLines.Count - 1)
928 this.MoveCaretVertical(true);
929 this.View.AdjustCaretAndSrc(AdjustFlow.Row); //この段階で調整しないとスクロールされない
935 AlignDirection direction = isMoveNext ? AlignDirection.Forward : AlignDirection.Back;
936 col = this.View.LayoutLines.GetLayout(this.Document.CaretPostion.row).AlignIndexToNearestCluster(col, direction);
939 this.View.JumpCaret(this.Document.CaretPostion.row, col,false);
945 /// <param name="isMoveNext">プラス方向に移動するなら真</param>
946 /// <remarks>このメソッドを呼び出した後でScrollToCaretメソッドとSelectWithMoveCaretメソッドを呼び出す必要があります</remarks>
947 void MoveCaretVertical(bool isMoveNext)
949 if (this.Document.FireUpdateEvent == false)
950 throw new InvalidOperationException("");
952 TextPoint nextPoint = this.GetTextPointAfterMoveLine(isMoveNext ? 1 : -1, this.Document.CaretPostion);
954 this.View.JumpCaret(nextPoint.row, nextPoint.col,false);
957 private void ReplaceBeforeSelectionArea(SelectCollection Selections, int removeLength, string insertStr)
959 if (removeLength == 0 && insertStr.Length == 0)
962 if (this.RectSelection == false || this.Document.FireUpdateEvent == false)
963 throw new InvalidOperationException();
965 SelectCollection temp = this.View.Selections;
966 int selectStart = temp.First().start;
967 int selectEnd = temp.Last().start + temp.Last().length;
969 //ドキュメント操作後に行うとうまくいかないので、あらかじめ取得しておく
970 TextPoint start = this.View.LayoutLines.GetTextPointFromIndex(selectStart);
971 TextPoint end = this.View.LayoutLines.GetTextPointFromIndex(selectEnd);
973 bool reverse = temp.First().start > temp.Last().start;
975 int lineHeadIndex = this.View.LayoutLines.GetIndexFromLineNumber(this.View.LayoutLines.GetLineNumberFromIndex(selectStart));
976 if (selectStart - removeLength < lineHeadIndex)
979 this.Document.UndoManager.BeginUndoGroup();
980 this.Document.FireUpdateEvent = false;
984 for (int i = 0; i < temp.Count; i++)
986 this.ReplaceBeforeSelection(temp[i], removeLength, insertStr);
991 for (int i = temp.Count - 1; i >= 0; i--)
993 this.ReplaceBeforeSelection(temp[i], removeLength, insertStr);
997 this.Document.FireUpdateEvent = true;
998 this.Document.UndoManager.EndUndoGroup();
1000 int delta = insertStr.Length - removeLength;
1005 this.JumpCaret(start.row, start.col);
1007 this.JumpCaret(end.row, end.col);
1009 this.Document.Select(start, 0, end.row - start.row);
1012 private void ReplaceBeforeSelection(Selection sel, int removeLength, string insertStr)
1014 sel = Util.NormalizeIMaker<Selection>(sel);
1015 this.Document.Replace(sel.start - removeLength, removeLength, insertStr);
1018 private void RepleaceSelectionArea(SelectCollection Selections, string value,bool updateInsertPoint = false)
1023 if (this.RectSelection == false)
1025 Selection sel = Selection.Create(this.Document.AnchorIndex, 0);
1026 if (Selections.Count > 0)
1027 sel = Util.NormalizeIMaker<Selection>(this.View.Selections.First());
1029 this.Document.Replace(sel.start, sel.length, value);
1033 if (this.Document.FireUpdateEvent == false)
1034 throw new InvalidOperationException("");
1036 int StartIndex = this.SelectionStart;
1038 SelectCollection newInsertPoint = new SelectCollection();
1040 if (this.SelectionLength == 0)
1044 this.Document.UndoManager.BeginUndoGroup();
1046 this.Document.FireUpdateEvent = false;
1048 string[] line = value.Split(new string[] { Document.NewLine.ToString() }, StringSplitOptions.RemoveEmptyEntries);
1050 TextPoint Current = this.View.GetLayoutLineFromIndex(this.SelectionStart);
1052 for (i = 0; i < line.Length && Current.row < this.View.LayoutLines.Count; i++, Current.row++)
1054 if (Current.col > this.View.LayoutLines[Current.row].Length)
1055 Current.col = this.View.LayoutLines[Current.row].Length;
1056 StartIndex = this.View.GetIndexFromLayoutLine(Current);
1057 this.Document.Replace(StartIndex, 0, line[i]);
1058 StartIndex += line[i].Length;
1061 for (; i < line.Length; i++)
1063 StartIndex = this.Document.Length;
1064 string str = Document.NewLine + line[i];
1065 this.Document.Replace(StartIndex, 0, str);
1066 StartIndex += str.Length;
1069 this.Document.FireUpdateEvent = true;
1071 this.Document.UndoManager.EndUndoGroup();
1075 SelectCollection temp = new SelectCollection(this.View.Selections); //コピーしないとReplaceCommandを呼び出した段階で書き換えられてしまう
1077 this.Document.UndoManager.BeginUndoGroup();
1079 this.Document.FireUpdateEvent = false;
1081 if (temp.First().start < temp.Last().start)
1083 for (int i = temp.Count - 1; i >= 0; i--)
1085 Selection sel = Util.NormalizeIMaker<Selection>(temp[i]);
1087 StartIndex = sel.start;
1089 this.Document.Replace(sel.start, sel.length, value);
1091 newInsertPoint.Add(Selection.Create(sel.start + (value.Length - sel.length) * i,0));
1096 for (int i = 0; i < temp.Count; i++)
1098 Selection sel = Util.NormalizeIMaker<Selection>(temp[i]);
1100 StartIndex = sel.start;
1102 this.Document.Replace(sel.start, sel.length, value);
1104 newInsertPoint.Add(Selection.Create(sel.start + (value.Length - sel.length) * i, 0));
1108 this.Document.FireUpdateEvent = true;
1110 this.Document.UndoManager.EndUndoGroup();
1112 this.JumpCaret(StartIndex);
1113 if (updateInsertPoint && newInsertPoint.Count > 0)
1114 this.View.Selections = newInsertPoint;
1117 private string GetTextFromLineSelectArea(SelectCollection Selections)
1119 Selection sel = Util.NormalizeIMaker<Selection>(Selections.First());
1121 string str = this.Document.ToString(sel.start, sel.length);
1126 string GetTextFromRectangleSelectArea(SelectCollection Selections)
1128 StringBuilder temp = new StringBuilder();
1129 if (Selections.First().start < Selections.Last().start)
1131 for (int i = 0; i < this.View.Selections.Count; i++)
1133 Selection sel = Util.NormalizeIMaker<Selection>(Selections[i]);
1135 string str = this.Document.ToString(sel.start, sel.length);
1136 if (str.IndexOf(Environment.NewLine) == -1)
1137 temp.AppendLine(str);
1144 for (int i = this.View.Selections.Count - 1; i >= 0; i--)
1146 Selection sel = Util.NormalizeIMaker<Selection>(Selections[i]);
1148 string str = this.Document.ToString(sel.start, sel.length).Replace(Document.NewLine.ToString(), Environment.NewLine);
1149 if (str.IndexOf(Environment.NewLine) == -1)
1150 temp.AppendLine(str);
1155 return temp.ToString();
1158 void View_LineBreakChanged(object sender, EventArgs e)
1164 void View_PageBoundChanged(object sender, EventArgs e)
1166 if (this.Document.LineBreak == LineBreakMethod.PageBound && this.View.PageBound.Width - this.View.LineBreakingMarginWidth > 0)
1167 this.Document.PerformLayout();
1171 void render_ChangedRenderResource(object sender, ChangedRenderRsourceEventArgs e)
1173 if (e.type == ResourceType.Font)
1175 if (this.Document.LineBreak == LineBreakMethod.PageBound)
1176 this.Document.PerformLayout();
1179 if (e.type == ResourceType.InlineChar)
1181 int oldLineCountOnScreen = this.View.LineCountOnScreen;
1182 this.View.CalculateLineCountOnScreen();
1183 if(this.View.LineCountOnScreen != oldLineCountOnScreen)
1188 void Document_Update(object sender, DocumentUpdateEventArgs e)
1192 case UpdateType.Replace:
1193 this.JumpCaret(e.startIndex + e.insertLength,true);
1195 case UpdateType.Clear:
1196 this.JumpCaret(0,0, false);