OSDN Git Service

正常に選択できないバグを修正した
authorgdkhd812 <jbh03215@htmil.co.jp>
Sun, 6 Oct 2013 15:54:45 +0000 (00:54 +0900)
committergdkhd812 <jbh03215@htmil.co.jp>
Sun, 6 Oct 2013 15:54:45 +0000 (00:54 +0900)
Common/Controller.cs
Metro/FooEditEngnine/FooTextBox.cs
WPF/FooEditEngine/FooTextBox.cs
WPF/Test/MainWindow.xaml
WPF/Test/MainWindow.xaml.cs

index edb6a32..14d0fea 100644 (file)
@@ -51,14 +51,13 @@ namespace FooEditEngine
             this.View.PerformLayouted += View_LineBreakChanged;\r
             this.View.PageBoundChanged += View_PageBoundChanged;\r
             this.Document.Clear();\r
-            this.CaretMoved += new EventHandler((s, e) => { });\r
+            this.SelectionChanged += new EventHandler((s, e) => { });\r
         }\r
 \r
         /// <summary>\r
-        /// キャレット移動時に通知される\r
+        /// 選択領域変更時に通知される\r
         /// </summary>\r
-        public event EventHandler CaretMoved;\r
-\r
+        public event EventHandler SelectionChanged;\r
 \r
         /// <summary>\r
         /// 矩形選択モードなら真を返し、そうでない場合は偽を返す\r
@@ -166,11 +165,12 @@ namespace FooEditEngine
                 else\r
                     this.View.InsertPoint = null;\r
             }\r
-            else\r
+            else if(length != 0)\r
             {\r
                 this.View.Selections.Add(Selection.Create(start, length));\r
                 this.View.InsertPoint = null;\r
             }\r
+            this.SelectionChanged(this, null);\r
         }\r
 \r
         public void Select(TextPoint tp, int width, int height)\r
@@ -193,6 +193,8 @@ namespace FooEditEngine
                 this.View.InsertPoint = new SelectCollection(this.View.Selections);\r
             else\r
                 this.View.InsertPoint = null;\r
+\r
+            this.SelectionChanged(this, null);\r
         }\r
 \r
         private void SelectByRectangle(TextRectangle rect)\r
@@ -386,8 +388,6 @@ namespace FooEditEngine
             this.View.AdjustCaretAndSrc();\r
 \r
             this.SelectWithMoveCaret(false);\r
-\r
-            this.CaretMoved(this, null);\r
         }\r
 \r
         /// <summary>\r
@@ -401,7 +401,6 @@ namespace FooEditEngine
             this.View.JumpCaret(0, 0);\r
             this.View.AdjustCaretAndSrc();\r
             this.SelectWithMoveCaret(isSelected);\r
-            this.CaretMoved(this, null);\r
         }\r
 \r
         /// <summary>\r
@@ -418,7 +417,6 @@ namespace FooEditEngine
             this.View.JumpCaret(this.View.LayoutLines.Count - 1, 0);\r
             this.View.AdjustCaretAndSrc();\r
             this.SelectWithMoveCaret(isSelected);\r
-            this.CaretMoved(this, null);\r
         }\r
 \r
         /// <summary>\r
@@ -471,7 +469,6 @@ namespace FooEditEngine
                 this.View.JumpCaret(toRow, 0);\r
                 this.View.AdjustCaretAndSrc();\r
                 this.SelectWithMoveCaret(isSelected);\r
-                this.CaretMoved(this, null);\r
             }\r
             else\r
             {\r
@@ -501,7 +498,6 @@ namespace FooEditEngine
             }\r
             this.View.AdjustCaretAndSrc(AdjustFlow.Col);\r
             this.SelectWithMoveCaret(isSelected);\r
-            this.CaretMoved(this, null);\r
         }\r
 \r
         void AlignNearestWord(bool MoveFlow)\r
@@ -536,7 +532,6 @@ namespace FooEditEngine
                 this.MoveCaretVertical(deltarow > 0);\r
             this.View.AdjustCaretAndSrc(AdjustFlow.Both);\r
             this.SelectWithMoveCaret(isSelected);\r
-            this.CaretMoved(this, null);\r
         }\r
 \r
         /// <summary>\r
@@ -689,10 +684,10 @@ namespace FooEditEngine
             \r
             SelectCollection Selections = this.View.Selections;\r
             if (isSelected)\r
-                this.Select(this.AnchorIndex, CaretPostion - this.AnchorIndex);\r
-            else\r
             {\r
-                this.View.Selections.Clear();\r
+                this.Select(this.AnchorIndex, CaretPostion - this.AnchorIndex);\r
+            }else{\r
+                this.Select(CaretPostion, 0);\r
                 this.AnchorIndex = CaretPostion;\r
                 this.View.InsertPoint = null;\r
             }\r
index 846849e..72ee373 100644 (file)
@@ -47,6 +47,7 @@ namespace FooEditEngine.Metro
         GestureRecognizer gestureRecongnizer = new GestureRecognizer();\r
         TextStore2 textStore;\r
         FooTextBoxAutomationPeer peer;\r
+        bool nowCaretMove = false;\r
 \r
         /// <summary>\r
         /// コンストラクター\r
@@ -87,7 +88,7 @@ namespace FooEditEngine.Metro
             this.View.TabStops = this.TabChars;\r
 \r
             this._Controller = new Controller(this.Document, this.View);\r
-            this._Controller.CaretMoved += Controller_CaretMoved;\r
+            this._Controller.SelectionChanged += Controller_SelectionChanged;\r
 \r
             this.FirstGripper = new Gripper(this._Controller, this.View, this.Render, GripperType.First);\r
             this.SecondGripper = new Gripper(this._Controller, this.View, this.Render, GripperType.Second);\r
@@ -736,10 +737,13 @@ namespace FooEditEngine.Metro
             this.Refresh();\r
         }\r
 \r
-        void Controller_CaretMoved(object sender, EventArgs e)\r
+        void Controller_SelectionChanged(object sender, EventArgs e)\r
         {\r
+            //こうしないと選択できなくなってしまう\r
+            this.nowCaretMove = true;\r
             SetValue(SelectionProperty, new TextRange(this._Controller.SelectionStart, this._Controller.SelectionLength));\r
             SetValue(CaretPostionPropertyKey, this.View.CaretPostion);\r
+            this.nowCaretMove = false;\r
             if (this.textStore.IsLocked() == false)\r
                 this.textStore.NotifySelectionChanged();\r
         }\r
@@ -1091,9 +1095,9 @@ namespace FooEditEngine.Metro
         public static void OnPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)\r
         {\r
             FooTextBox source = (FooTextBox)sender;\r
-            if (e.Property.Equals(SelectionProperty))\r
+            if (e.Property.Equals(SelectionProperty) && !source.nowCaretMove)\r
                 source._Controller.Select(source.Selection.Index,source.Selection.Length);\r
-            if (e.Property.Equals(CaretPostionPropertyKey))\r
+            if (e.Property.Equals(CaretPostionPropertyKey) && !source.nowCaretMove)\r
                 source.JumpCaret(source.CaretPostion.row, source.CaretPostion.col);\r
             if (e.Property.Equals(InsertModeProperty))\r
                 source.View.InsertMode = source.InsertMode;\r
index 71d050f..37b5713 100644 (file)
@@ -49,6 +49,7 @@ namespace FooEditEngine.WPF
         DispatcherTimer timer;\r
         bool disposed = false;\r
         FooTextBoxAutomationPeer peer;\r
+        bool nowCaretMove = false;\r
         \r
         static FooTextBox()\r
         {\r
@@ -101,7 +102,7 @@ namespace FooEditEngine.WPF
             this.View.TabStops = this.TabChars;\r
 \r
             this._Controller = new Controller(this.Document, this.View);\r
-            this._Controller.CaretMoved += new EventHandler(Controller_CaretMoved);\r
+            this._Controller.SelectionChanged += new EventHandler(Controller_SelectionChanged);\r
 \r
             //Viewを作成した後に追加しないと例外が発生する\r
             this.Document.Update += new DocumentUpdateEventHandler(Document_Update);\r
@@ -1027,12 +1028,15 @@ namespace FooEditEngine.WPF
                 this.horizontalScrollBar.Value = Math.Abs(view.Src.X);\r
         }\r
 \r
-        void Controller_CaretMoved(object sender, EventArgs e)\r
+        void Controller_SelectionChanged(object sender, EventArgs e)\r
         {\r
             this.View.CaretBlink = this.View.CaretBlink;\r
             this.CaretMoved(this, null);\r
+            //こうしないと選択できなくなってしまう\r
+            this.nowCaretMove = true;\r
             SetValue(SelectionProperty, new TextRange(this._Controller.SelectionStart, this._Controller.SelectionLength));\r
             SetValue(CaretPostionProperty, this.View.CaretPostion);\r
+            this.nowCaretMove = false;            \r
             if(this.textStore.IsLocked() == false)\r
                 this.textStore.NotifySelectionChanged();\r
         }\r
@@ -1074,10 +1078,12 @@ namespace FooEditEngine.WPF
             switch (e.Property.Name)\r
             {\r
                 case "Selection":\r
-                    this.Select(this.Selection.Index, this.Selection.Length);\r
+                    if(!this.nowCaretMove)\r
+                        this.Select(this.Selection.Index, this.Selection.Length);\r
                     break;\r
                 case "CaretPostion":\r
-                    this.JumpCaret(this.CaretPostion.row, this.CaretPostion.col);\r
+                    if (!this.nowCaretMove)\r
+                        this.JumpCaret(this.CaretPostion.row, this.CaretPostion.col);\r
                     break;\r
                 case "LineBreakMethod":\r
                     this.View.LineBreak = this.LineBreakMethod;\r
@@ -1287,7 +1293,7 @@ namespace FooEditEngine.WPF
         /// Selectionの依存プロパティを表す\r
         /// </summary>\r
         public static readonly DependencyProperty SelectionProperty =\r
-            DependencyProperty.Register("Selection", typeof(TextRange), typeof(FooTextBox), new PropertyMetadata(0));\r
+            DependencyProperty.Register("Selection", typeof(TextRange), typeof(FooTextBox), new PropertyMetadata(TextRange.Null));\r
 \r
         /// <summary>\r
         /// 拡大率を表す\r
index 6a00578..24d52e3 100644 (file)
@@ -2,7 +2,11 @@
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:MyNamespace="clr-namespace:FooEditEngine.WPF;assembly=FooEditEngine.WPF"
+        xmlns:local="clr-namespace:Test"
         Title="MainWindow" Height="350" Width="525">
+    <Window.Resources>
+        <local:TextRangeConveter x:Key="TextRangeConverter"/>
+    </Window.Resources>
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="24"/>
@@ -28,8 +32,7 @@
         </Menu>
         <MyNamespace:FooTextBox x:Name="fooTextBox" Grid.Row="1"/>
         <StackPanel Grid.Row="2" Orientation="Horizontal">
-            <TextBlock Text="{Binding Selection.Index,ElementName=fooTextBox}"/>
-            <TextBlock Text="{Binding Selection.Length,ElementName=fooTextBox}" Margin="10,0,0,0"/>
+            <TextBlock Text="{Binding Selection,ElementName=fooTextBox,Converter={StaticResource TextRangeConverter}}" Margin="10,0,0,0"/>
             <TextBlock Text="FindPattern" Margin="10,0,0,0"/>
             <TextBox Name="FindPattern" Width="100"/>
             <TextBlock Text="ReplaceAll" Margin="10,0,0,0"/>
index b744416..58464a7 100644 (file)
@@ -243,4 +243,17 @@ namespace Test
             this.fooTextBox.Refresh();
         }
     }
+    public class TextRangeConveter : System.Windows.Data.IValueConverter
+    {
+        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
+        {
+            FooEditEngine.TextRange range = (FooEditEngine.TextRange)value;
+            return string.Format("Index:{0} Length:{1}", range.Index, range.Length);
+        }
+
+        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
+        {
+            throw new System.NotImplementedException();
+        }
+    }
 }