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.Collections.Generic;
15 using System.Text.RegularExpressions;
16 using System.Threading.Tasks;
18 using Windows.UI.Xaml;
19 using Windows.UI.Xaml.Media;
20 using Windows.UI.Xaml.Automation;
21 using Windows.UI.Xaml.Automation.Peers;
22 using Windows.UI.Xaml.Automation.Provider;
23 using Windows.UI.Xaml.Automation.Text;
24 using FooEditEngine.Metro;
27 using System.Windows.Automation;
28 using System.Windows.Automation.Peers;
29 using System.Windows.Automation.Provider;
30 using System.Windows.Automation.Text;
31 using FooEditEngine.WPF;
34 namespace FooEditEngine
38 /// Automation Peer class for CustomInputBox2.
40 /// Note: The difference between this and CustomControl1AutomationPeer is that this one implements
41 /// Text Pattern (ITextProvider) and Value Pattern (IValuePattern) interfaces. So Touch keyboard shows
42 /// automatically when user taps on the control with Touch or Pen.
44 sealed class FooTextBoxAutomationPeer : FrameworkElementAutomationPeer, ITextProvider, IValueProvider
46 private FooTextBox fooTextBox;
47 private string accClass = "FooTextBox";
52 /// <param name="owner"></param>
53 public FooTextBoxAutomationPeer(FooTextBox owner)
56 this.fooTextBox = owner;
57 this.fooTextBox.Document.Update += Document_Update;
60 void Document_Update(object sender, DocumentUpdateEventArgs e)
62 this.RaiseAutomationEvent(AutomationEvents.TextPatternOnTextChanged);
65 public void OnNotifyCaretChanged()
67 this.RaiseAutomationEvent(AutomationEvents.TextPatternOnTextSelectionChanged);
72 /// Override GetPatternCore to return the object that supports the specified pattern. In this case the Value pattern, Text
73 /// patter and any base class patterns.
75 /// <param name="patternInterface"></param>
76 /// <returns>the object that supports the specified pattern</returns>
77 protected override object GetPatternCore(PatternInterface patternInterface)
79 if (patternInterface == PatternInterface.Value)
83 else if (patternInterface == PatternInterface.Text)
87 return base.GetPatternCore(patternInterface);
91 public override object GetPattern(PatternInterface patternInterface)
93 if (patternInterface == PatternInterface.Value)
97 else if (patternInterface == PatternInterface.Text)
101 return base.GetPattern(patternInterface);
106 /// Override GetClassNameCore and set the name of the class that defines the type associated with this control.
108 /// <returns>The name of the control class</returns>
109 protected override string GetClassNameCore()
111 return this.accClass;
114 protected override AutomationControlType GetAutomationControlTypeCore()
116 return AutomationControlType.Edit;
119 protected override bool IsContentElementCore()
125 protected override Windows.Foundation.Rect GetBoundingRectangleCore()
127 return Util.GetScreentRect(new Windows.Foundation.Rect(0, 0, this.fooTextBox.ActualWidth, this.fooTextBox.ActualHeight),this.fooTextBox);
131 protected override System.Windows.Rect GetBoundingRectangleCore()
133 System.Windows.Point left = this.fooTextBox.PointToScreen(new System.Windows.Point(0, 0));
134 System.Windows.Point bottom = this.fooTextBox.PointToScreen(new System.Windows.Point(this.fooTextBox.ActualWidth, this.fooTextBox.ActualHeight));
135 return new System.Windows.Rect(left, bottom);
139 #region Implementation for ITextPattern interface
140 // Complete implementation of the ITextPattern is beyond the scope of this sample. The implementation provided
141 // is specific to this sample's custom control, so it is unlikely that they are directly transferable to other
144 ITextRangeProvider ITextProvider.DocumentRange
146 // A real implementation of this method is beyond the scope of this sample.
147 // If your custom control has complex text involving both readonly and non-readonly ranges,
148 // it will need a smarter implementation than just returning a fixed range
151 return new FooTextBoxRangeProvider(this.fooTextBox, this);
155 ITextRangeProvider[] ITextProvider.GetSelection()
157 ITextRangeProvider[] ret = new ITextRangeProvider[1];
158 int selStart = this.fooTextBox.Selection.Index;
159 int selLength = this.fooTextBox.Selection.Length;
160 ret[0] = new FooTextBoxRangeProvider(this.fooTextBox, selStart, selLength, this);
164 ITextRangeProvider[] ITextProvider.GetVisibleRanges()
166 ITextRangeProvider[] ret = new ITextRangeProvider[1];
167 if (this.fooTextBox.LayoutLineCollection.Count == 0)
169 ret[0] = new FooTextBoxRangeProvider(this.fooTextBox, 0, 0, this);
174 int startIndex = this.fooTextBox.GetIndexFromPostion(new Windows.Foundation.Point(0,0));
175 int endIndex = this.fooTextBox.GetIndexFromPostion(new Windows.Foundation.Point(this.fooTextBox.ActualWidth, this.fooTextBox.ActualHeight));
178 int startIndex = this.fooTextBox.GetIndexFromPostion(new System.Windows.Point(0, 0));
179 int endIndex = this.fooTextBox.GetIndexFromPostion(new System.Windows.Point(this.fooTextBox.ActualWidth, this.fooTextBox.ActualHeight));
181 ret[0] = new FooTextBoxRangeProvider(this.fooTextBox, startIndex, endIndex - startIndex, this);
186 ITextRangeProvider ITextProvider.RangeFromChild(IRawElementProviderSimple childElement)
188 return new FooTextBoxRangeProvider(this.fooTextBox,0,0, this);
192 ITextRangeProvider ITextProvider.RangeFromPoint(Windows.Foundation.Point screenLocation)
194 Windows.Foundation.Point pt = Util.GetClientPoint(screenLocation, this.fooTextBox);
196 int index = this.fooTextBox.GetIndexFromPostion(pt);
198 if (index == this.fooTextBox.Document.Length)
201 return new FooTextBoxRangeProvider(this.fooTextBox, index, length, this);
205 ITextRangeProvider ITextProvider.RangeFromPoint(System.Windows.Point screenLocation)
207 System.Windows.Point pt = this.fooTextBox.PointFromScreen(screenLocation);
209 int index = this.fooTextBox.GetIndexFromPostion(pt);
211 if (index == this.fooTextBox.Document.Length)
214 return new FooTextBoxRangeProvider(this.fooTextBox, index, length, this);
218 SupportedTextSelection ITextProvider.SupportedTextSelection
220 get { return SupportedTextSelection.Single; }
225 #region Implementation for IValueProvider interface
226 // Complete implementation of the IValueProvider is beyond the scope of this sample. The implementation provided
227 // is specific to this sample's custom control, so it is unlikely that they are directly transferable to other
231 /// The value needs to be false for the Touch keyboard to be launched automatically because Touch keyboard
232 /// does not appear when the input focus is in a readonly UI control.
234 bool IValueProvider.IsReadOnly
236 get { return false; }
239 void IValueProvider.SetValue(string value)
241 string oldText = this.fooTextBox.Document.ToString(0);
242 this.fooTextBox.Document.Replace(0,this.fooTextBox.Document.Length,value);
243 this.RaisePropertyChangedEvent(ValuePatternIdentifiers.ValueProperty, oldText, this.fooTextBox.Document.ToString(0));
246 string IValueProvider.Value
250 return this.fooTextBox.Document.ToString(0,this.fooTextBox.Document.Length);
254 #endregion //Implementation for IValueProvider interface
256 public IRawElementProviderSimple GetRawElementProviderSimple()
258 return ProviderFromPeer(this);
263 /// A minimal implementation of ITextRangeProvider, used by CustomControl2AutomationPeer
264 /// A real implementation is beyond the scope of this sample
266 sealed class FooTextBoxRangeProvider : ITextRangeProvider
268 private FooTextBox textbox;
269 private Document document;
270 private FooTextBoxAutomationPeer _peer;
271 private int start, end;
273 public FooTextBoxRangeProvider(FooTextBox textbox, FooTextBoxAutomationPeer peer)
274 : this(textbox,0,textbox.Document.Length,peer)
277 public FooTextBoxRangeProvider(FooTextBox textbox, int start, int length, FooTextBoxAutomationPeer peer)
279 this.textbox = textbox;
280 this.document = textbox.Document;
282 this.end = start + length;
286 public void AddToSelection()
288 throw new InvalidOperationException();
291 public ITextRangeProvider Clone()
293 return new FooTextBoxRangeProvider(this.textbox,this.start,this.end - this.start, _peer);
296 public bool Compare(ITextRangeProvider o)
298 FooTextBoxRangeProvider other = o as FooTextBoxRangeProvider;
300 throw new ArgumentNullException("null以外の値を指定してください");
301 if (this.start == other.start && this.end == other.end)
307 public int CompareEndpoints(TextPatternRangeEndpoint endpoint, ITextRangeProvider targetRange, TextPatternRangeEndpoint targetEndpoint)
309 FooTextBoxRangeProvider other = targetRange as FooTextBoxRangeProvider;
312 throw new ArgumentException("");
314 if (endpoint == TextPatternRangeEndpoint.Start)
316 if (targetEndpoint == TextPatternRangeEndpoint.Start)
317 return this.Compare(this.start,other.start);
318 if(targetEndpoint == TextPatternRangeEndpoint.End)
319 return this.Compare(this.start,other.end);
321 if (endpoint == TextPatternRangeEndpoint.End)
323 if (targetEndpoint == TextPatternRangeEndpoint.Start)
324 return this.Compare(this.start, other.end);
325 if (targetEndpoint == TextPatternRangeEndpoint.End)
326 return this.Compare(this.end, other.end);
328 throw new ArgumentException("endpointに未知の値が指定されました");
331 int Compare(int self, int other)
335 else if (self > other)
341 public void ExpandToEnclosingUnit(TextUnit unit)
343 if (unit == TextUnit.Character)
345 if (this.start == this.end)
347 if (this.start < this.document.Length)
352 if (unit == TextUnit.Format || unit == TextUnit.Word || unit == TextUnit.Line)
354 LineToIndexTable layoutLineCollection = this.textbox.LayoutLineCollection;
355 int row = layoutLineCollection.GetLineNumberFromIndex(this.start);
356 this.start = layoutLineCollection.GetIndexFromLineNumber(row);
357 this.end = this.start + layoutLineCollection.GetLengthFromLineNumber(row);
360 if (unit == TextUnit.Paragraph || unit == TextUnit.Page || unit == TextUnit.Document)
363 this.end = this.document.Length;
366 throw new NotImplementedException();
369 public ITextRangeProvider FindAttribute(int attribute, Object value, bool backward)
374 public ITextRangeProvider FindText(String text, bool backward, bool ignoreCase)
377 throw new NotImplementedException();
378 document.SetFindParam(text, false, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None);
379 IEnumerator<SearchResult> it = document.Find();
382 SearchResult sr = it.Current;
383 return new FooTextBoxRangeProvider(this.textbox, sr.Start, sr.End - sr.Start + 1, _peer);
388 public Object GetAttributeValue(int attribute)
394 public void GetBoundingRectangles(out double[] rectangles)
397 public double[] GetBoundingRectangles()
400 LineToIndexTable layoutLineCollection = this.textbox.LayoutLineCollection;
401 TextPoint topLeft = layoutLineCollection.GetTextPointFromIndex(this.start);
402 TextPoint bottomRight = this.textbox.LayoutLineCollection.GetTextPointFromIndex(IsNewLine(this.end) ? this.end - 1 : this.end);
406 Windows.Foundation.Point topLeftPos = this.textbox.GetPostionFromTextPoint(topLeft);
407 Windows.Foundation.Point bottomRightPos = this.textbox.GetPostionFromTextPoint(bottomRight);
408 topLeftPos = Util.GetScreentPoint(topLeftPos, this.textbox);
409 bottomRightPos = Util.GetScreentPoint(bottomRightPos, this.textbox);
412 System.Windows.Point topLeftPos = this.textbox.GetPostionFromTextPoint(topLeft);
413 System.Windows.Point bottomRightPos = this.textbox.GetPostionFromTextPoint(bottomRight);
414 topLeftPos = this.textbox.PointToScreen(topLeftPos);
415 bottomRightPos = this.textbox.PointToScreen(bottomRightPos);
418 double width = bottomRightPos.X - topLeftPos.X;
421 Rectangle rect = new Rectangle(topLeftPos.X, topLeftPos.Y,
423 bottomRightPos.Y - topLeftPos.Y + layoutLineCollection.GetLayout(bottomRight.row).Height);
426 rectangles = new double[4]{
434 return new double[4]{
443 bool IsNewLine(int index)
445 if (this.document.Length > 0)
446 return this.document[index == 0 ? 0 : index - 1] == Document.NewLine;
451 public IRawElementProviderSimple[] GetChildren()
453 return new IRawElementProviderSimple[0];
456 public IRawElementProviderSimple GetEnclosingElement()
458 return _peer.GetRawElementProviderSimple();
461 public String GetText(int maxLength)
463 if (this.document.Length == 0)
465 int length = this.end - this.start;
467 return this.document.ToString(this.start, length);
469 return this.document.ToString(this.start, (int)Math.Min(length, maxLength));
472 public int Move(TextUnit unit, int count)
478 case TextUnit.Character:
481 this.start = this.MoveEndpointByCharacter(this.start,count,out moved);
482 this.end = this.start + 1;
485 case TextUnit.Format:
489 return this.MoveEndPointByLine(this.start, count, out this.start, out this.end);
491 case TextUnit.Paragraph:
493 case TextUnit.Document:
496 this.end = this.document.Length;
500 throw new NotImplementedException();
503 public void MoveEndpointByRange(TextPatternRangeEndpoint endpoint, ITextRangeProvider targetRange, TextPatternRangeEndpoint targetEndpoint)
505 FooTextBoxRangeProvider other = targetRange as FooTextBoxRangeProvider;
508 throw new ArgumentException("");
510 if (endpoint == TextPatternRangeEndpoint.Start)
512 if (targetEndpoint == TextPatternRangeEndpoint.Start)
513 this.start = other.start;
514 if (targetEndpoint == TextPatternRangeEndpoint.End)
515 this.start = other.end;
516 if (this.start > this.end)
517 this.end = this.start;
520 if (endpoint == TextPatternRangeEndpoint.End)
522 if (targetEndpoint == TextPatternRangeEndpoint.Start)
523 this.end = other.start;
524 if (targetEndpoint == TextPatternRangeEndpoint.End)
525 this.end = other.end;
528 throw new ArgumentException("endpointに未知の値が指定されました");
531 public int MoveEndpointByUnit(TextPatternRangeEndpoint endpoint, TextUnit unit, int count)
536 if (unit == TextUnit.Character)
538 if (endpoint == TextPatternRangeEndpoint.Start)
540 this.start = this.MoveEndpointByCharacter(this.start, count, out moved);
541 if(this.start > this.end)
542 this.end = this.start;
544 else if (endpoint == TextPatternRangeEndpoint.End)
546 this.end = this.MoveEndpointByCharacter(this.end, count, out moved);
547 if (this.end < this.start)
548 this.start = this.end;
551 if (unit == TextUnit.Format || unit == TextUnit.Word || unit == TextUnit.Line)
553 if (endpoint == TextPatternRangeEndpoint.Start)
554 return this.MoveEndPointByLine(this.start, count, out this.start, out this.end);
555 else if (endpoint == TextPatternRangeEndpoint.End)
556 return this.MoveEndPointByLine(this.end, count, out this.start, out this.end);
558 if (unit == TextUnit.Paragraph || unit == TextUnit.Page || unit == TextUnit.Document)
561 this.end = this.document.Length - 1;
567 int MoveEndpointByCharacter(int index, int count,out int moved)
569 int newIndex = index + count - 1;
570 if (newIndex > this.document.Length)
571 newIndex = this.document.Length;
574 moved = newIndex - index;
578 int MoveEndPointByLine(int index, int count,out int newStartInex,out int newEndInex)
580 LineToIndexTable layoutLineCollection = this.textbox.LayoutLineCollection;
581 Controller controller = this.textbox.Controller;
582 TextPoint currentPoint = layoutLineCollection.GetTextPointFromIndex(index);
583 TextPoint newPoint = controller.GetTextPointAfterMoveLine(count, currentPoint);
584 int lineLength = layoutLineCollection.GetLengthFromLineNumber(newPoint.row);
585 newStartInex = layoutLineCollection.GetIndexFromLineNumber(newPoint.row);
586 newEndInex = newStartInex + lineLength;
587 return newPoint.row - currentPoint.row;
590 public void RemoveFromSelection()
592 throw new InvalidOperationException();
595 public void ScrollIntoView(bool alignToTop)
597 int row = this.textbox.LayoutLineCollection.GetLineNumberFromIndex(alignToTop ? this.start : this.end);
598 this.textbox.ScrollIntoView(row, alignToTop);
603 this.textbox.Select(this.start, this.end - this.start + 1);