OSDN Git Service

ナレーター周りをリファクタリングした&UWP版でナレーターがうまく動かないバグを修正した
[fooeditengine/FooEditEngine.git] / Core / Automaion / FooTextBoxAutomationPeer.cs
index 71c765e..fe4403f 100644 (file)
@@ -32,6 +32,7 @@ using System.Windows.Automation;
 using System.Windows.Automation.Peers;
 using System.Windows.Automation.Provider;
 using System.Windows.Automation.Text;
+using FooEditEngine;
 using FooEditEngine.WPF;
 #endif
 
@@ -45,7 +46,11 @@ namespace FooEditEngine
     /// Text Pattern (ITextProvider) and Value Pattern (IValuePattern) interfaces.  So Touch keyboard shows 
     /// automatically when user taps on the control with Touch or Pen.
     /// </summary>
+#if METRO || WINDOWS_UWP
+    sealed class FooTextBoxAutomationPeer : FrameworkElementAutomationPeer, ITextProvider, ITextProvider2, IValueProvider
+#elif WPF
     sealed class FooTextBoxAutomationPeer : FrameworkElementAutomationPeer, ITextProvider, IValueProvider
+#endif
     {
         private FooTextBox fooTextBox;
         private string accClass = "FooTextBox";
@@ -87,10 +92,13 @@ namespace FooEditEngine
             {
                 return this;
             }
+            else if (patternInterface == PatternInterface.Text2)
+            {
+                return this;
+            }
             return base.GetPatternCore(patternInterface);
         }
-#endif
-#if WPF
+#elif WPF
         public override object GetPattern(PatternInterface patternInterface)
         {
             if (patternInterface == PatternInterface.Value)
@@ -138,24 +146,23 @@ namespace FooEditEngine
         protected override Windows.Foundation.Rect GetBoundingRectangleCore()
         {
             double scale = Util.GetScale();
-            return new Windows.Foundation.Rect(0, 0, this.fooTextBox.ActualWidth * scale, this.fooTextBox.ActualHeight * scale);
-        }
-#endif
-#if WPF
+            Point left = Util.GetPointInWindow(new Point(0, 0),this.fooTextBox).Scale(scale);
+            Point bottom = Util.GetPointInWindow(new Point(this.fooTextBox.ActualWidth, this.fooTextBox.ActualHeight),this.fooTextBox).Scale(scale);
+#elif WPF
         protected override System.Windows.Rect GetBoundingRectangleCore()
         {
-            System.Windows.Point left = this.fooTextBox.PointToScreen(new System.Windows.Point(0, 0));
-            System.Windows.Point bottom = this.fooTextBox.PointToScreen(new System.Windows.Point(this.fooTextBox.ActualWidth, this.fooTextBox.ActualHeight));
-            return new System.Windows.Rect(left, bottom);
-        }
+            Point left = Util.GetScreentPoint(new Point(0, 0), this.fooTextBox);
+            Point bottom = Util.GetScreentPoint(new Point(this.fooTextBox.ActualWidth, this.fooTextBox.ActualHeight), this.fooTextBox);
 #endif
+            return new Rectangle(left,bottom);
+        }
 
-    #region Implementation for ITextPattern interface
+        #region Implementation for ITextPattern and ITextPattern2 interface
         // Complete implementation of the ITextPattern is beyond the scope of this sample.  The implementation provided
         // is specific to this sample's custom control, so it is unlikely that they are directly transferable to other 
         // custom control.
 
-        ITextRangeProvider ITextProvider.DocumentRange
+        public ITextRangeProvider DocumentRange
         {
             // A real implementation of this method is beyond the scope of this sample.
             // If your custom control has complex text involving both readonly and non-readonly ranges, 
@@ -166,7 +173,7 @@ namespace FooEditEngine
             }
         }
 
-        ITextRangeProvider[] ITextProvider.GetSelection()
+        public ITextRangeProvider[] GetSelection()
         {
             ITextRangeProvider[] ret = new ITextRangeProvider[1];
             int selStart = this.fooTextBox.Selection.Index;
@@ -175,7 +182,7 @@ namespace FooEditEngine
             return ret;
         }
 
-        ITextRangeProvider[] ITextProvider.GetVisibleRanges()
+        public ITextRangeProvider[] GetVisibleRanges()
         {
             ITextRangeProvider[] ret = new ITextRangeProvider[1];
             if (this.fooTextBox.LayoutLineCollection.Count == 0)
@@ -184,59 +191,63 @@ namespace FooEditEngine
             }
             else
             {
-#if METRO || WINDOWS_UWP
-                int startIndex = this.fooTextBox.GetIndexFromPostion(new Windows.Foundation.Point(0,0));
-                int endIndex = this.fooTextBox.GetIndexFromPostion(new Windows.Foundation.Point(this.fooTextBox.ActualWidth, this.fooTextBox.ActualHeight));
-#endif
-#if WPF
-                int startIndex = this.fooTextBox.GetIndexFromPostion(new System.Windows.Point(0, 0));
-                int endIndex = this.fooTextBox.GetIndexFromPostion(new System.Windows.Point(this.fooTextBox.ActualWidth, this.fooTextBox.ActualHeight));
-#endif
+                EditView view = this.fooTextBox.View;
+                
+                int startIndex = view.GetIndexFromLayoutLine(new TextPoint(view.Src.Row,0));
+                int endIndex = view.GetIndexFromLayoutLine(new TextPoint(view.Src.Row + view.LineCountOnScreen, 0));
                 ret[0] = new FooTextBoxRangeProvider(this.fooTextBox, startIndex, endIndex - startIndex, this);
             }
             return ret;
         }
 
-        ITextRangeProvider ITextProvider.RangeFromChild(IRawElementProviderSimple childElement)
+        public ITextRangeProvider RangeFromChild(IRawElementProviderSimple childElement)
         {
             return new FooTextBoxRangeProvider(this.fooTextBox,0,0, this);
         }
 
 #if METRO || WINDOWS_UWP
-        ITextRangeProvider ITextProvider.RangeFromPoint(Windows.Foundation.Point screenLocation)
+        public ITextRangeProvider RangeFromPoint(Windows.Foundation.Point screenLocation)
+#elif WPF
+        public ITextRangeProvider RangeFromPoint(System.Windows.Point screenLocation)
+#endif
         {
-            Point pt = Util.GetClientPoint(screenLocation, this.fooTextBox);
+            Point pt = Util.GetClientPoint(screenLocation, fooTextBox);
+            EditView view = this.fooTextBox.View;
 
-            int index = this.fooTextBox.GetIndexFromPostion(pt);
+            int index = view.GetIndexFromLayoutLine(view.GetTextPointFromPostion(pt));
             int length = 1;
             if (index == this.fooTextBox.Document.Length)
                 length = 0;
-            
+
             return new FooTextBoxRangeProvider(this.fooTextBox, index, length, this);
         }
-#endif
-#if WPF
-        ITextRangeProvider ITextProvider.RangeFromPoint(System.Windows.Point screenLocation)
+
+        public SupportedTextSelection SupportedTextSelection
         {
-            System.Windows.Point pt = this.fooTextBox.PointFromScreen(screenLocation);
+            get { return SupportedTextSelection.Single; }
+        }
 
-            int index = this.fooTextBox.GetIndexFromPostion(pt);
+        public ITextRangeProvider RangeFromAnnotation(IRawElementProviderSimple annotationElement)
+        {
+            throw new NotImplementedException();
+        }
+
+        public ITextRangeProvider GetCaretRange(out bool isActive)
+        {
+
+            EditView view = this.fooTextBox.View;
+            Document doc = this.fooTextBox.Document;
+            isActive = true;
+            int index = view.GetIndexFromLayoutLine(doc.CaretPostion);
             int length = 1;
             if (index == this.fooTextBox.Document.Length)
                 length = 0;
 
             return new FooTextBoxRangeProvider(this.fooTextBox, index, length, this);
         }
-#endif
-
-        SupportedTextSelection ITextProvider.SupportedTextSelection
-        {
-            get { return SupportedTextSelection.Single; }
-        }
+        #endregion
 
-    #endregion
-
-    #region Implementation for IValueProvider interface
+        #region Implementation for IValueProvider interface
         // Complete implementation of the IValueProvider is beyond the scope of this sample.  The implementation provided
         // is specific to this sample's custom control, so it is unlikely that they are directly transferable to other 
         // custom control.
@@ -245,19 +256,19 @@ namespace FooEditEngine
         /// The value needs to be false for the Touch keyboard to be launched automatically because Touch keyboard
         /// does not appear when the input focus is in a readonly UI control.
         /// </summary>
-        bool IValueProvider.IsReadOnly
+        public bool IsReadOnly
         {
             get { return false; }
         }
 
-        void IValueProvider.SetValue(string value)
+        public void SetValue(string value)
         {
             string oldText = this.fooTextBox.Document.ToString(0);
             this.fooTextBox.Document.Replace(0,this.fooTextBox.Document.Length,value);
             this.RaisePropertyChangedEvent(ValuePatternIdentifiers.ValueProperty, oldText, this.fooTextBox.Document.ToString(0));
         }
 
-        string IValueProvider.Value
+        public string Value
         {
             get
             {
@@ -271,6 +282,7 @@ namespace FooEditEngine
         {
             return ProviderFromPeer(this);
         }
+
     }
 
 #endif