OSDN Git Service

SelectionChangedが同じパンドラー内で二回呼び出されることがあったので避けるようにした
[fooeditengine/FooEditEngine.git] / Core / Controller.cs
index eda619f..c02471b 100644 (file)
@@ -38,7 +38,13 @@ namespace FooEditEngine
     /// </summary>
     public enum IndentMode
     {
+        /// <summary>
+        /// タブ
+        /// </summary>
         Tab,
+        /// <summary>
+        /// スペース
+        /// </summary>
         Space,
     }
 
@@ -48,29 +54,69 @@ namespace FooEditEngine
     internal sealed class Controller
     {
         EditView View;
-        Document Document;
-        int AnchorIndex;
+        Document _Document;
         
         public Controller(Document doc, EditView view)
         {
             this.Document = doc;
-            this.Document.Update += new DocumentUpdateEventHandler(Document_Update);
-            this.Document.RightToLeftChanged += (s, e) =>
-            {
-                this.AdjustCaret();
-            };
             this.View = view;
             this.View.render.ChangedRenderResource += render_ChangedRenderResource;
-            this.View.PerformLayouted += View_LineBreakChanged;
             this.View.PageBoundChanged += View_PageBoundChanged;
-            this.SelectionChanged += new EventHandler((s, e) => { });
-            this.Document.Clear();
+            //this.Document.Clear();
         }
 
-        /// <summary>
-        /// 選択領域変更時に通知される
-        /// </summary>
-        public event EventHandler SelectionChanged;
+        public Document Document
+        {
+            get
+            {
+                return this._Document;
+            }
+            set
+            {
+                //メモリリークを防ぐためにパンドラーを除く
+                if (this._Document != null)
+                {
+                    this._Document.Update -= Document_Update;
+                    this._Document.StatusUpdate -= Document_StatusChanged;
+                    this._Document.SelectionChanged -= Document_SelectionChanged;
+                    this._Document.PerformLayouted -= View_LineBreakChanged;
+                    this._Document.CaretChanged -= _Document_CaretChanged;
+                }
+
+                this._Document = value;
+
+                this._Document.Update += new DocumentUpdateEventHandler(Document_Update);
+                this._Document.StatusUpdate += Document_StatusChanged;
+                this._Document.SelectionChanged += Document_SelectionChanged;
+                this._Document.PerformLayouted += View_LineBreakChanged;
+                this._Document.CaretChanged += _Document_CaretChanged;
+            }
+        }
+
+        private void _Document_CaretChanged(object sender, EventArgs e)
+        {
+            TextPoint pos = this.Document.CaretPostion;
+            this.JumpCaret(pos.row, pos.col);
+        }
+
+        private void Document_SelectionChanged(object sender, EventArgs e)
+        {
+            if (this.IsReverseSelect())
+            {
+                this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart);
+                this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
+            }
+            else
+            {
+                this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart);
+                this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
+            }
+        }
+
+        void Document_StatusChanged(object sender,EventArgs e)
+        {
+            this.AdjustCaret();
+        }
 
         /// <summary>
         /// 矩形選択モードなら真を返し、そうでない場合は偽を返す
@@ -99,7 +145,7 @@ namespace FooEditEngine
             get
             {
                 if (this.View.Selections.Count == 0)
-                    return this.AnchorIndex;
+                    return this.Document.AnchorIndex;
                 else
                     return this.View.Selections.First().start;
             }
@@ -154,120 +200,7 @@ namespace FooEditEngine
         public bool IsReverseSelect()
         {
             int index = this.View.LayoutLines.GetIndexFromTextPoint(this.Document.CaretPostion);
-            return index < this.AnchorIndex;
-        }
-
-        /// <summary>
-        /// 指定された範囲を選択する
-        /// </summary>
-        /// <param name="start"></param>
-        /// <param name="length"></param>
-        /// <remarks>RectSelectionの値によって動作が変わります。真の場合は矩形選択モードに、そうでない場合は行ごとに選択されます</remarks>
-        public void Select(int start, int length)
-        {
-            if (this.Document.FireUpdateEvent == false)
-                throw new InvalidOperationException("");
-            if (start < 0 || start + length < 0 || start + length > this.Document.Length)
-                throw new ArgumentOutOfRangeException("startかendが指定できる範囲を超えてます");
-            this.View.Selections.Clear();
-            if (length < 0)
-            {
-                int oldStart = start;
-                start += length;
-                length = oldStart - start;
-            }
-            if (this.RectSelection && length != 0)
-            {
-                TextPoint startTextPoint = this.View.GetLayoutLineFromIndex(start);
-                TextPoint endTextPoint = this.View.GetLayoutLineFromIndex(start + length);
-                this.SelectByRectangle(new TextRectangle(startTextPoint, endTextPoint));
-            }
-            else if(length != 0)
-            {
-                this.View.Selections.Add(Selection.Create(start, length));
-            }
-            this.SelectionChanged(this, null);
-        }
-
-        public void Select(TextPoint tp, int width, int height)
-        {
-            if (this.Document.FireUpdateEvent == false || !this.RectSelection)
-                throw new InvalidOperationException("");
-            TextPoint end = tp;
-
-            end.row = tp.row + height;
-            end.col = tp.col + width;
-            
-            if (end.row > this.View.LayoutLines.Count - 1)
-                throw new ArgumentOutOfRangeException("");
-            
-            this.View.Selections.Clear();
-            
-            this.SelectByRectangle(new TextRectangle(tp,end));
-            
-            this.SelectionChanged(this, null);
-        }
-
-        private void SelectByRectangle(TextRectangle rect)
-        {
-            if (this.Document.FireUpdateEvent == false)
-                throw new InvalidOperationException("");
-            if (rect.TopLeft <= rect.BottomRight)
-            {
-                for (int i = rect.TopLeft.row; i <= rect.BottomLeft.row; i++)
-                {
-                    int length = this.View.LayoutLines.GetLengthFromLineNumber(i);
-                    int leftCol = rect.TopLeft.col, rightCol = rect.TopRight.col, lastCol = length;
-                    if(length > 0 && this.View.LayoutLines[i][length - 1] == Document.NewLine)
-                        lastCol =  length - 1;
-                    if (lastCol < 0)
-                        lastCol = 0;
-                    if (rect.TopLeft.col > lastCol)
-                        leftCol = lastCol;
-                    if (rect.TopRight.col > lastCol)
-                        rightCol = lastCol;
-
-                    int StartIndex = this.View.LayoutLines.GetIndexFromTextPoint(new TextPoint(i, leftCol));
-                    int EndIndex = this.View.LayoutLines.GetIndexFromTextPoint(new TextPoint(i, rightCol));
-
-                    Selection sel;
-                    sel = Selection.Create(StartIndex, EndIndex - StartIndex);
-
-                    this.View.Selections.Add(sel);
-                }
-            }
-        }
-
-        /// <summary>
-        /// 単語単位で選択する
-        /// </summary>
-        /// <param name="index">探索を開始するインデックス</param>
-        /// <param name="changeAnchor">選択の起点となるとインデックスを変更するなら真。そうでなければ偽</param>
-        public void SelectWord(int index, bool changeAnchor = false)
-        {
-            if (this.Document.FireUpdateEvent == false)
-                throw new InvalidOperationException("");
-
-            if (this.Document.Length <= 0 || index >= this.Document.Length)
-                return;
-
-            Document str = this.Document;
-
-            int start = index;
-            while (start > 0 && !Util.IsWordSeparator(str[start]))
-                start--;
-
-            if (Util.IsWordSeparator(str[start]))
-                start++;
-
-            int end = index;
-            while (end < this.Document.Length && !Util.IsWordSeparator(str[end]))
-                end++;
-
-            this.Select(start, end - start);
-
-            if(changeAnchor)
-                this.AnchorIndex = start;
+            return index < this.Document.AnchorIndex;
         }
 
         /// <summary>
@@ -291,9 +224,7 @@ namespace FooEditEngine
                 else
                     return false;
             }
-            this.Document.Lock();
             this.Document.Replace(this.SelectionStart, this.SelectionLength, result.ToString());
-            this.Document.UnLock();
             return true;
         }
 
@@ -317,9 +248,7 @@ namespace FooEditEngine
                 else
                     i++;
             }
-            this.Document.Lock();
             this.Document.Replace(this.SelectionStart, this.SelectionLength, result.ToString());
-            this.Document.UnLock();
         }
 
         /// <summary>
@@ -357,7 +286,7 @@ namespace FooEditEngine
         {
             foreach(int id in this.Document.Markers.IDs)
             {
-                foreach (Marker m in this.Document.GetMarkers(index, id))
+                foreach (Marker m in this.Document.GetMarkers(id, index))
                 {
                     if (m.hilight == type)
                         return true;
@@ -377,7 +306,14 @@ namespace FooEditEngine
             int col = this.Document.CaretPostion.col;
             if (col > 0 && col > this.View.LayoutLines[row].Length)
                 col = this.View.LayoutLines[row].Length;
+
+            //選択領域が消えてしまうので覚えておく
+            int sel_start = this.SelectionStart;
+            int sel_length = this.SelectionLength;
+
             this.JumpCaret(row, col);
+
+            this.Document.Select(sel_start, sel_length);
         }
 
         /// <summary>
@@ -465,6 +401,33 @@ namespace FooEditEngine
             this.SelectWithMoveCaret(isSelected);
         }
 
+        double noti;
+        public void ScrollByPixel(ScrollDirection dir,int delta, bool isSelected, bool withCaret)
+        {
+            if (this.Document.FireUpdateEvent == false)
+                throw new InvalidOperationException("");
+
+            if (dir == ScrollDirection.Left || dir == ScrollDirection.Right)
+            {
+                this.Scroll(dir, delta, isSelected, withCaret);
+                return;
+            }
+
+            if(dir == ScrollDirection.Up || dir == ScrollDirection.Down)
+            {
+                noti += delta;
+
+                if (noti < this.View.render.emSize.Height)
+                    return;
+
+                int delta_row = (int)(noti / this.View.render.emSize.Height + 1.0);
+
+                noti = 0;
+
+                this.Scroll(dir, delta_row, isSelected, withCaret);
+            }
+        }
+
         /// <summary>
         /// スクロールする
         /// </summary>
@@ -519,8 +482,10 @@ namespace FooEditEngine
             else
             {
                 this.View.Scroll(toX, toRow);
-                this.View.IsFocused = false;
             }
+
+            this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart);
+            this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
         }
 
         /// <summary>
@@ -606,9 +571,7 @@ namespace FooEditEngine
             if (this.Document[index] == Document.NewLine)
                 next = index + 1;
 
-            this.Document.Lock();
-            this.Document.Replace(index, next - index, "");
-            this.Document.UnLock();
+            this.Document.Replace(index, next - index, "", true);
         }
 
         public bool IsRectInsertMode()
@@ -661,9 +624,7 @@ namespace FooEditEngine
                 newIndex--;
             }
 
-            this.Document.Lock();
-            this.Document.Replace(newIndex, oldIndex - newIndex, "");
-            this.Document.UnLock();
+            this.Document.Replace(newIndex, oldIndex - newIndex, "", true);
         }
 
         /// <summary>
@@ -692,8 +653,8 @@ namespace FooEditEngine
         /// <summary>
         /// キャレット位置に文字列を挿入し、その分だけキャレットを進める。isInsertModeの値により動作が変わります
         /// </summary>
-        /// <param name="str"></param>
-        /// <param name="fromTip"></param>
+        /// <param name="str">挿入したい文字列</param>
+        /// <param name="fromTip">真の場合、矩形選択の幅にかかわらず矩形編集モードとして動作します。そうでない場合は選択領域を文字列で置き換えます</param>
         public void DoInputString(string str,bool fromTip = false)
         {
             TextPoint CaretPos = this.Document.CaretPostion;
@@ -734,9 +695,7 @@ namespace FooEditEngine
                 if (foldingData != null && !foldingData.Expand && index > foldingData.Start && index <= foldingData.End)
                     index = foldingData.End + 1;
             }
-            this.Document.Lock();
-            this.Document.Replace(index, length, str);
-            this.Document.UnLock();
+            this.Document.Replace(index, length, str, true);
         }
 
         /// <summary>
@@ -759,10 +718,10 @@ namespace FooEditEngine
             SelectCollection Selections = this.View.Selections;
             if (isSelected)
             {
-                this.Select(this.AnchorIndex, CaretPostion - this.AnchorIndex);
+                this.Document.Select(this.Document.AnchorIndex, CaretPostion - this.Document.AnchorIndex);
             }else{
-                this.AnchorIndex = CaretPostion;
-                this.Select(CaretPostion, 0);
+                this.Document.AnchorIndex = CaretPostion;
+                this.Document.Select(CaretPostion, 0);
             }
         }
 
@@ -773,7 +732,7 @@ namespace FooEditEngine
         public void MoveCaretAndSelect(TextPoint tp)
         {
             int CaretPostion = this.View.GetIndexFromLayoutLine(tp);
-            this.Select(this.AnchorIndex, CaretPostion - this.AnchorIndex);
+            this.Document.Select(this.Document.AnchorIndex, CaretPostion - this.Document.AnchorIndex);
             this.View.JumpCaret(tp.row, tp.col);
             this.View.AdjustCaretAndSrc();
         }
@@ -785,7 +744,7 @@ namespace FooEditEngine
         /// <param name="hittedGripper">動かす対象となるグリッパー</param>
         /// <returns>移動できた場合は真を返す。そうでなければ偽を返す</returns>
         /// <remarks>グリッパー内にポインターが存在しない場合、グリッパーはポインターの座標近くの行に移動する</remarks>
-        public bool MoveCaretAndGripper(Point p, GripperView hittedGripper)
+        public bool MoveCaretAndGripper(Point p, Gripper hittedGripper)
         {
             bool HittedCaret = false;
             TextPoint tp = this.View.GetTextPointFromPostion(p);
@@ -796,32 +755,37 @@ namespace FooEditEngine
 
             if (HittedCaret || hittedGripper != null)
             {
+                TextPointSearchRange searchRange;
+                if (this.View.HitTextArea(p.X, p.Y))
+                    searchRange = TextPointSearchRange.TextAreaOnly;
+                else if (this.SelectionLength > 0)
+                    searchRange = TextPointSearchRange.Full;
+                else
+                    return false;
+
                 if (hittedGripper != null)
                 {
-                    tp = this.View.GetTextPointFromPostion(hittedGripper.AdjustPoint(p));
-                    if (this.IsReverseSelect())
-                    {
-                        if (Object.ReferenceEquals(hittedGripper, this.View.SelectGrippers.BottomRight))
-                            this.MoveSelectBefore(tp);
-                        else
-                            this.MoveCaretAndSelect(tp);
-                    }
-                    else
-                    {
-                        if (Object.ReferenceEquals(hittedGripper, this.View.SelectGrippers.BottomLeft))
-                            this.MoveSelectBefore(tp);
-                        else
-                            this.MoveCaretAndSelect(tp);
-                    }
-                    hittedGripper.Move(this.View, tp);
+                    tp = this.View.GetTextPointFromPostion(hittedGripper.AdjustPoint(p), searchRange);
+                    if (tp == TextPoint.Null)
+                        return false;
+                    if (Object.ReferenceEquals(hittedGripper, this.Document.SelectGrippers.BottomRight))
+                        this.MoveCaretAndSelect(tp);
+                    else if(Object.ReferenceEquals(hittedGripper, this.Document.SelectGrippers.BottomLeft))
+                        this.MoveSelectBefore(tp);
                 }
                 else
                 {
-                    tp = this.View.GetTextPointFromPostion(p);
+                    tp = this.View.GetTextPointFromPostion(p, searchRange);
                     if (tp != TextPoint.Null)
+                    {
                         this.MoveCaretAndSelect(tp);
+                    }
+                    else
+                    {
+                        return false;
+                    }
                 }
-                this.View.SelectGrippers.BottomLeft.Enabled = this.SelectionLength != 0;
+                this.Document.SelectGrippers.BottomLeft.Enabled = this.SelectionLength != 0;
                 return true;
             }
             return false;
@@ -834,16 +798,16 @@ namespace FooEditEngine
             if (this.IsReverseSelect())
             {
                 NewAnchorIndex = this.View.GetIndexFromLayoutLine(tp);
-                SelectionLength = this.SelectionLength + NewAnchorIndex - this.AnchorIndex;
-                this.Select(this.SelectionStart, SelectionLength);
+                SelectionLength = this.SelectionLength + NewAnchorIndex - this.Document.AnchorIndex;
+                this.Document.Select(this.SelectionStart, SelectionLength);
             }
             else
             {
                 NewAnchorIndex = this.View.GetIndexFromLayoutLine(tp);
-                SelectionLength = this.SelectionLength + this.AnchorIndex - NewAnchorIndex;
-                this.Select(NewAnchorIndex, SelectionLength);
+                SelectionLength = this.SelectionLength + this.Document.AnchorIndex - NewAnchorIndex;
+                this.Document.Select(NewAnchorIndex, SelectionLength);
             }
-            this.AnchorIndex = NewAnchorIndex;
+            this.Document.AnchorIndex = NewAnchorIndex;
         }
 
         /// <summary>
@@ -888,7 +852,7 @@ namespace FooEditEngine
             string insertStr = this.IndentMode == IndentMode.Space ? this.GetIndentSpace(0) : "\t";
             string text = this.InsertLineHead(GetTextFromLineSelectArea(this.View.Selections), insertStr);
             this.RepleaceSelectionArea(this.View.Selections,text);
-            this.Select(selectionStart, text.Length);
+            this.Document.Select(selectionStart, text.Length);
         }
 
         /// <summary>
@@ -902,7 +866,7 @@ namespace FooEditEngine
             string insertStr = this.IndentMode == IndentMode.Space ? this.GetIndentSpace(0) : "\t";
             string text = this.RemoveLineHead(GetTextFromLineSelectArea(this.View.Selections), insertStr);
             this.RepleaceSelectionArea(this.View.Selections, text);
-            this.Select(selectionStart, text.Length);
+            this.Document.Select(selectionStart, text.Length);
         }
 
         string InsertLineHead(string s, string str)
@@ -986,7 +950,7 @@ namespace FooEditEngine
                 throw new InvalidOperationException("");
 
             TextPoint nextPoint = this.GetTextPointAfterMoveLine(isMoveNext ? 1 : -1, this.Document.CaretPostion);
-            
+
             this.View.JumpCaret(nextPoint.row, nextPoint.col,false);
         }
 
@@ -1042,15 +1006,13 @@ namespace FooEditEngine
             else
                 this.JumpCaret(end.row, end.col);
             
-            this.Select(start, 0, end.row - start.row);
+            this.Document.Select(start, 0, end.row - start.row);
         }
 
         private void ReplaceBeforeSelection(Selection sel, int removeLength, string insertStr)
         {
             sel = Util.NormalizeIMaker<Selection>(sel);
-            this.Document.Lock();
             this.Document.Replace(sel.start - removeLength, removeLength, insertStr);
-            this.Document.UnLock();
         }
 
         private void RepleaceSelectionArea(SelectCollection Selections, string value,bool updateInsertPoint = false)
@@ -1060,13 +1022,11 @@ namespace FooEditEngine
 
             if (this.RectSelection == false)
             {
-                Selection sel = Selection.Create(this.AnchorIndex, 0);
+                Selection sel = Selection.Create(this.Document.AnchorIndex, 0);
                 if (Selections.Count > 0)
                     sel = Util.NormalizeIMaker<Selection>(this.View.Selections.First());
 
-                this.Document.Lock();
                 this.Document.Replace(sel.start, sel.length, value);
-                this.Document.UnLock();
                 return;
             }
 
@@ -1081,8 +1041,6 @@ namespace FooEditEngine
             {
                 int i;
 
-                this.Document.Lock();
-
                 this.Document.UndoManager.BeginUndoGroup();
 
                 this.Document.FireUpdateEvent = false;
@@ -1111,15 +1069,11 @@ namespace FooEditEngine
                 this.Document.FireUpdateEvent = true;
 
                 this.Document.UndoManager.EndUndoGroup();
-
-                this.Document.UnLock();
             }
             else
             {
                 SelectCollection temp = new SelectCollection(this.View.Selections); //コピーしないとReplaceCommandを呼び出した段階で書き換えられてしまう
 
-                this.Document.Lock();
-
                 this.Document.UndoManager.BeginUndoGroup();
 
                 this.Document.FireUpdateEvent = false;
@@ -1154,8 +1108,6 @@ namespace FooEditEngine
                 this.Document.FireUpdateEvent = true;
 
                 this.Document.UndoManager.EndUndoGroup();
-
-                this.Document.UnLock();
             }
             this.JumpCaret(StartIndex);
             if (updateInsertPoint && newInsertPoint.Count > 0)
@@ -1212,7 +1164,7 @@ namespace FooEditEngine
         void View_PageBoundChanged(object sender, EventArgs e)
         {
             if (this.Document.LineBreak == LineBreakMethod.PageBound && this.View.PageBound.Width - this.View.LineBreakingMarginWidth > 0)
-                this.View.PerfomLayouts();
+                this.Document.PerformLayout();
             this.AdjustCaret();
         }
 
@@ -1221,7 +1173,7 @@ namespace FooEditEngine
             if (e.type == ResourceType.Font)
             {
                 if (this.Document.LineBreak == LineBreakMethod.PageBound)
-                    this.View.PerfomLayouts();
+                    this.Document.PerformLayout();
                 this.AdjustCaret();
             }
             if (e.type == ResourceType.InlineChar)
@@ -1238,8 +1190,6 @@ namespace FooEditEngine
             switch (e.type)
             {
                 case UpdateType.Replace:
-                    if(e.startIndex < this.Document.Length && this.Document[e.startIndex] == Document.NewLine)
-                        this.View.CalculateLineCountOnScreen();
                     this.JumpCaret(e.startIndex + e.insertLength,true);
                     break;
                 case UpdateType.Clear: