OSDN Git Service

テキストサービスマネージャーが呼び出されないときにフォーカスが失われるとクラッシュすることがあった
[fooeditengine/FooEditEngine.git] / UWP / FooEditEngine.UWP / FooTextBox.cs
1 /*
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.
5
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.
8
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/>.
10  */
11
12 using System;
13 using System.Text;
14 using System.ComponentModel;
15 using System.Threading.Tasks;
16 using Windows.ApplicationModel.Resources.Core;
17 using Windows.Devices.Input;
18 using Windows.System;
19 using Windows.ApplicationModel.DataTransfer;
20 using Windows.Foundation;
21 using Windows.UI;
22 using Windows.UI.Input;
23 using Windows.UI.Core;
24 using Windows.UI.Popups;
25 using Windows.UI.Text;
26 using Windows.UI.Xaml.Media;
27 using Windows.UI.Xaml;
28 using Windows.UI.Xaml.Controls;
29 using Windows.UI.Xaml.Controls.Primitives;
30 using Windows.UI.Xaml.Input;
31 using Windows.UI.Text.Core;
32
33 // テンプレート コントロールのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=234235 を参照してください
34 namespace FooEditEngine.UWP
35 {
36     /// <summary>
37     /// テキストボックスコントロール
38     /// </summary>
39     public sealed class FooTextBox : Control,IDisposable
40     {
41         EditView View;
42         Controller _Controller;
43 #if !DUMMY_RENDER
44         D2DRender Render;
45 #else
46         DummyRender Render;
47 #endif
48         ScrollBar horizontalScrollBar, verticalScrollBar;
49         Windows.UI.Xaml.Shapes.Rectangle rectangle;
50         GestureRecognizer gestureRecongnizer = new GestureRecognizer();
51         CoreTextEditContext textEditContext;
52         CoreTextServicesManager textServiceManager;
53 #if ENABLE_AUTMATION
54         FooTextBoxAutomationPeer peer;
55 #endif
56         bool nowCaretMove = false;
57         bool nowCompstion = false;
58         Document _Document;
59         DispatcherTimer timer = new DispatcherTimer();
60
61         const int Interval = 96;
62         const int IntervalWhenLostFocus = 160;
63
64         /// <summary>
65         /// コンストラクター
66         /// </summary>
67         public FooTextBox()
68         {
69             this.DefaultStyleKey = typeof(FooTextBox);
70             
71             this.rectangle = new Windows.UI.Xaml.Shapes.Rectangle();
72             this.rectangle.Margin = this.Padding;
73 #if !DUMMY_RENDER
74             this.Render = new D2DRender(this,this.rectangle);
75 #else
76             this.Render = new DummyRender();
77 #endif
78
79             this.Document = new Document();
80
81             this.View = new EditView(this.Document, this.Render, new Padding(5, 5, Gripper.HitAreaWidth / 2, Gripper.HitAreaWidth));
82             this.View.SrcChanged += View_SrcChanged;
83             this.View.InsertMode = this.InsertMode;
84             this.Document.DrawLineNumber = this.DrawLineNumber;
85             this.View.HideCaret = !this.DrawCaret;
86             this.View.HideLineMarker = !this.DrawCaretLine;
87             this.Document.HideRuler = !this.DrawRuler;
88             this.Document.UrlMark = this.MarkURL;
89             this.Document.TabStops = this.TabChars;
90
91             this._Controller = new Controller(this.Document, this.View);
92
93             this.gestureRecongnizer.GestureSettings = GestureSettings.Drag | 
94                 GestureSettings.RightTap | 
95                 GestureSettings.Tap | 
96                 GestureSettings.DoubleTap |
97                 GestureSettings.ManipulationTranslateX | 
98                 GestureSettings.ManipulationTranslateY |
99                 GestureSettings.ManipulationScale |
100                 GestureSettings.ManipulationTranslateInertia |
101                 GestureSettings.ManipulationScaleInertia;
102             this.gestureRecongnizer.RightTapped += gestureRecongnizer_RightTapped;
103             this.gestureRecongnizer.Tapped += gestureRecongnizer_Tapped;
104             this.gestureRecongnizer.Dragging += gestureRecongnizer_Dragging;
105             this.gestureRecongnizer.ManipulationInertiaStarting += GestureRecongnizer_ManipulationInertiaStarting; ;
106             this.gestureRecongnizer.ManipulationStarted += gestureRecongnizer_ManipulationStarted;
107             this.gestureRecongnizer.ManipulationUpdated += gestureRecongnizer_ManipulationUpdated;
108             this.gestureRecongnizer.ManipulationCompleted += gestureRecongnizer_ManipulationCompleted;
109
110             this.timer.Interval = new TimeSpan(0, 0, 0, 0, Interval);
111             this.timer.Tick += this.timer_Tick;
112             this.timer.Start();
113
114             this.GettingFocus += FooTextBox_GettingFocus;
115             this.LosingFocus += FooTextBox_LosingFocus;
116
117             this.SizeChanged += FooTextBox_SizeChanged;
118
119             this.Loaded += FooTextBox_Loaded;
120         }
121
122         /// <summary>
123         /// ファイナライザー
124         /// </summary>
125         ~FooTextBox()
126         {
127             this.Dispose(false);
128         }
129
130         /// <summary>
131         /// アンマネージドリソースを解放する
132         /// </summary>
133         public void Dispose()
134         {
135             this.Dispose(true);
136             GC.SuppressFinalize(this);
137         }
138
139         bool Disposed = false;
140         private void Dispose(bool disposing)
141         {
142             if (this.Disposed)
143                 return;
144             if (disposing)
145             {
146                 this.View.Dispose();
147                 this.Render.Dispose();
148             }
149         }
150
151         /// <summary>
152         /// ドキュメントを選択する
153         /// </summary>
154         /// <param name="start">開始インデックス</param>
155         /// <param name="length">長さ</param>
156         public void Select(int start, int length)
157         {
158             this.Document.Select(start, length);
159         }
160
161         /// <summary>
162         /// キャレットを指定した行に移動させます
163         /// </summary>
164         /// <param name="index">インデックス</param>
165         /// <remarks>このメソッドを呼び出すと選択状態は解除されます</remarks>
166         public void JumpCaret(int index)
167         {
168             this._Controller.JumpCaret(index);
169         }
170         /// <summary>
171         /// キャレットを指定した行と桁に移動させます
172         /// </summary>
173         /// <param name="row">行番号</param>
174         /// <param name="col">桁</param>
175         /// <remarks>このメソッドを呼び出すと選択状態は解除されます</remarks>
176         public void JumpCaret(int row, int col)
177         {
178             this._Controller.JumpCaret(row, col);
179         }
180
181         /// <summary>
182         /// 選択中のテキストをクリップボードにコピーします
183         /// </summary>
184         public void Copy()
185         {
186             string text = this._Controller.SelectedText;
187             if (text != null && text != string.Empty)
188             {
189                 DataPackage dataPackage = new DataPackage();
190                 dataPackage.RequestedOperation = DataPackageOperation.Copy;
191                 dataPackage.SetText(text);
192
193                 Clipboard.SetContent(dataPackage); 
194             }
195         }
196
197         /// <summary>
198         /// 選択中のテキストをクリップボードに切り取ります
199         /// </summary>
200         public void Cut()
201         {
202             string text = this._Controller.SelectedText;
203             if (text != null && text != string.Empty)
204             {
205                 DataPackage dataPackage = new DataPackage();
206                 dataPackage.RequestedOperation = DataPackageOperation.Copy;
207                 dataPackage.SetText(text);
208
209                 Clipboard.SetContent(dataPackage);
210                 
211                 this._Controller.SelectedText = "";
212             }
213         }
214
215         /// <summary>
216         /// 選択中のテキストを貼り付けます
217         /// </summary>
218         public async Task PasteAsync()
219         {
220             var dataPackageView = Clipboard.GetContent();
221             if (dataPackageView.Contains(StandardDataFormats.Text))
222             {
223                 try
224                 {
225                     this._Controller.SelectedText = await dataPackageView.GetTextAsync();
226                 }catch(Exception e)
227                 {
228                     System.Diagnostics.Debug.WriteLine("past error:" + e.Message);
229                 }
230             }
231         }
232
233         /// <summary>
234         /// すべて選択する
235         /// </summary>
236         public void SelectAll()
237         {
238             this.Document.Select(0, this.Document.Length);
239         }
240
241         /// <summary>
242         /// 選択を解除する
243         /// </summary>
244         public void DeSelectAll()
245         {
246             this._Controller.DeSelectAll();
247         }
248
249         /// <summary>
250         /// 対応する座標を返します
251         /// </summary>
252         /// <param name="tp">テキストポイント</param>
253         /// <returns>座標</returns>
254         /// <remarks>テキストポイントがクライアント領域の原点より外にある場合、返される値は原点に丸められます</remarks>
255         public Windows.Foundation.Point GetPostionFromTextPoint(TextPoint tp)
256         {
257             if (this.Document.FireUpdateEvent == false)
258                 throw new InvalidOperationException("");
259             return this.View.GetPostionFromTextPoint(tp);
260         }
261
262         /// <summary>
263         /// 対応するテキストポイントを返します
264         /// </summary>
265         /// <param name="p">クライアント領域の原点を左上とする座標</param>
266         /// <returns>テキストポイント</returns>
267         public TextPoint GetTextPointFromPostion(Windows.Foundation.Point p)
268         {
269             if (this.Document.FireUpdateEvent == false)
270                 throw new InvalidOperationException("");
271             return this.View.GetTextPointFromPostion(p);
272         }
273
274         /// <summary>
275         /// 行の高さを取得します
276         /// </summary>
277         /// <param name="row">レイアウト行</param>
278         /// <returns>行の高さ</returns>
279         public double GetLineHeight(int row)
280         {
281             if (this.Document.FireUpdateEvent == false)
282                 throw new InvalidOperationException("");
283             return this.View.LayoutLines.GetLayout(row).Height; ;
284         }
285
286         /// <summary>
287         /// インデックスに対応する座標を得ます
288         /// </summary>
289         /// <param name="index">インデックス</param>
290         /// <returns>座標を返す</returns>
291         public Windows.Foundation.Point GetPostionFromIndex(int index)
292         {
293             if (this.Document.FireUpdateEvent == false)
294                 throw new InvalidOperationException("");
295             TextPoint tp = this.View.GetLayoutLineFromIndex(index);
296             return this.View.GetPostionFromTextPoint(tp);
297         }
298
299         /// <summary>
300         /// 座標からインデックスに変換します
301         /// </summary>
302         /// <param name="p">座標</param>
303         /// <returns>インデックスを返す</returns>
304         public int GetIndexFromPostion(Windows.Foundation.Point p)
305         {
306             if (this.Document.FireUpdateEvent == false)
307                 throw new InvalidOperationException("");
308             TextPoint tp = this.View.GetTextPointFromPostion(p);
309             return this.View.GetIndexFromLayoutLine(tp);
310         }
311         
312
313         /// <summary>
314         /// 再描写する
315         /// </summary>
316         public void Refresh()
317         {
318             this.Refresh(this.View.PageBound);
319         }
320
321         /// <summary>
322         /// レイアウト行をすべて破棄し、再度レイアウトを行う
323         /// </summary>
324         public void PerfomLayouts()
325         {
326             this.Document.PerformLayout();
327         }
328
329         /// <summary>
330         /// 指定行までスクロールする
331         /// </summary>
332         /// <param name="row">行</param>
333         /// <param name="alignTop">指定行を画面上に置くなら真。そうでないなら偽</param>
334         public void ScrollIntoView(int row, bool alignTop)
335         {
336             this.View.ScrollIntoView(row, alignTop);
337         }
338
339         /// <summary>
340         /// ファイルからドキュメントを構築する
341         /// </summary>
342         /// <param name="sr">StremReader</param>
343         /// <returns>Taskオブジェクト</returns>
344         public async Task LoadFileAsync(System.IO.StreamReader sr, System.Threading.CancellationTokenSource token)
345         {
346             await this.Document.LoadAsync(sr, token);
347         }
348
349         private void Document_LoadProgress(object sender, ProgressEventArgs e)
350         {
351             if(e.state == ProgressState.Start)
352             {
353                 this.IsEnabled = false;
354             }
355             else if(e.state == ProgressState.Complete)
356             {
357                 CoreTextRange modified_range = new CoreTextRange();
358                 modified_range.StartCaretPosition = 0;
359                 modified_range.EndCaretPosition = 0;
360                 //キャレット位置はロード前と同じにしないと違和感を感じる
361                 if (this.textEditContext != null)
362                     this.textEditContext.NotifyTextChanged(modified_range, this.Document.Length, modified_range);
363
364                 if (this.verticalScrollBar != null)
365                     this.verticalScrollBar.Maximum = this.View.LayoutLines.Count;
366                 this.IsEnabled = true;
367                 this.Refresh(this.View.PageBound);
368             }
369         }
370
371         /// <summary>
372         /// ドキュメントの内容をファイルに保存する
373         /// </summary>
374         /// <param name="sw">StreamWriter</param>
375         /// <param name="enc">エンコード</param>
376         /// <returns>Taskオブジェクト</returns>
377         public async Task SaveFile(System.IO.StreamWriter sw, System.Threading.CancellationTokenSource token)
378         {
379             await this.Document.SaveAsync(sw, token);
380         }
381
382 #region command
383         void CopyCommand()
384         {
385             this.Copy();
386         }
387
388         void CutCommand()
389         {
390             this.Cut();
391             this.Refresh();
392         }
393
394         async Task PasteCommand()
395         {
396             await this.PasteAsync();
397             this.Refresh();
398         }
399
400 #endregion
401
402 #region event
403
404 #if ENABLE_AUTMATION
405         /// <inheritdoc/>
406         protected override Windows.UI.Xaml.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
407         {
408             this.peer = new FooTextBoxAutomationPeer(this);
409             return this.peer;
410         }
411 #endif
412         /// <inheritdoc/>
413         protected override void OnApplyTemplate()
414         {
415             base.OnApplyTemplate();
416
417             Grid grid = this.GetTemplateChild("PART_Grid") as Grid;
418             if (grid != null)
419             {
420                 Grid.SetRow(this.rectangle, 0);
421                 Grid.SetColumn(this.rectangle, 0);
422                 grid.Children.Add(this.rectangle);
423             }
424
425             this.horizontalScrollBar = this.GetTemplateChild("PART_HorizontalScrollBar") as ScrollBar;
426             if (this.horizontalScrollBar != null)
427             {
428                 this.horizontalScrollBar.SmallChange = 10;
429                 this.horizontalScrollBar.LargeChange = 100;
430                 this.horizontalScrollBar.Maximum = this.horizontalScrollBar.LargeChange + 1;
431                 this.horizontalScrollBar.Scroll += new ScrollEventHandler(horizontalScrollBar_Scroll);
432             }
433             this.verticalScrollBar = this.GetTemplateChild("PART_VerticalScrollBar") as ScrollBar;
434             if (this.verticalScrollBar != null)
435             {
436                 this.verticalScrollBar.SmallChange = 1;
437                 this.verticalScrollBar.LargeChange = 10;
438                 this.verticalScrollBar.Maximum = this.View.LayoutLines.Count;
439                 this.verticalScrollBar.Scroll += new ScrollEventHandler(verticalScrollBar_Scroll);
440             }
441         }
442
443         private void FooTextBox_LosingFocus(UIElement sender, LosingFocusEventArgs args)
444         {
445             this.RemoveTextContext();
446             
447             if (this.textServiceManager != null)
448             {
449                 this.textServiceManager.InputLanguageChanged -= TextServiceManager_InputLanguageChanged;
450                 this.textServiceManager = null;
451             }
452
453             System.Diagnostics.Debug.WriteLine("losing focus");
454         }
455
456         private async void FooTextBox_GettingFocus(UIElement sender, GettingFocusEventArgs args)
457         {
458             System.Diagnostics.Debug.WriteLine("getting focus");
459             if (this.textServiceManager == null)
460             {
461                 await Task.Delay(500);
462                 this.textServiceManager = CoreTextServicesManager.GetForCurrentView();
463                 this.textServiceManager.InputLanguageChanged += TextServiceManager_InputLanguageChanged;
464             }
465
466             this.CreateTextContext();
467         }
468
469         /// <inheritdoc/>
470         protected override void OnGotFocus(RoutedEventArgs e)
471         {
472             base.OnGotFocus(e);
473
474             System.Diagnostics.Debug.WriteLine("got focus");
475
476             this.View.IsFocused = true;
477             this.timer.Interval = new TimeSpan(0, 0, 0, 0, Interval);
478             this.Refresh();
479         }
480
481         private void TextServiceManager_InputLanguageChanged(CoreTextServicesManager sender, object args)
482         {
483             System.Diagnostics.Debug.WriteLine("input language changed input script:"+  this.textServiceManager.InputLanguage.Script);
484             this.RemoveTextContext();
485             this.CreateTextContext();
486         }
487
488        /// <inheritdoc/>
489         protected override void OnLostFocus(RoutedEventArgs e)
490         {
491             base.OnLostFocus(e);
492
493             System.Diagnostics.Debug.WriteLine("lost focus");
494             
495             this.View.IsFocused = false;
496             this.Refresh(this.View.PageBound);
497             this.timer.Interval = new TimeSpan(0, 0, 0, 0, IntervalWhenLostFocus);
498         }
499
500         private void CreateTextContext()
501         {
502             if(this.textServiceManager != null)
503             {
504                 this.textEditContext = this.textServiceManager.CreateEditContext();
505                 this.textEditContext.InputScope = CoreTextInputScope.Text;
506                 this.textEditContext.CompositionStarted += TextEditContext_CompositionStarted;
507                 this.textEditContext.CompositionCompleted += TextEditContext_CompositionCompleted;
508                 this.textEditContext.LayoutRequested += TextEditContext_LayoutRequested;
509                 this.textEditContext.TextUpdating += TextEditContext_TextUpdating;
510                 this.textEditContext.TextRequested += TextEditContext_TextRequested;
511                 this.textEditContext.SelectionRequested += TextEditContext_SelectionRequested;
512                 this.textEditContext.SelectionUpdating += TextEditContext_SelectionUpdating;
513                 this.textEditContext.FormatUpdating += TextEditContext_FormatUpdating;
514                 this.textEditContext.FocusRemoved += TextEditContext_FocusRemoved;
515                 this.textEditContext.NotifyFocusLeaveCompleted += TextEditContext_NotifyFocusLeaveCompleted;
516                 this.textEditContext.NotifyFocusEnter();
517             }
518         }
519
520         private void RemoveTextContext()
521         {
522             if(this.textEditContext != null)
523             {
524                 this.textEditContext.NotifyFocusLeave();
525                 this.textEditContext.CompositionStarted -= TextEditContext_CompositionStarted;
526                 this.textEditContext.CompositionCompleted -= TextEditContext_CompositionCompleted;
527                 this.textEditContext.LayoutRequested -= TextEditContext_LayoutRequested;
528                 this.textEditContext.TextUpdating -= TextEditContext_TextUpdating;
529                 this.textEditContext.TextRequested -= TextEditContext_TextRequested;
530                 this.textEditContext.SelectionRequested -= TextEditContext_SelectionRequested;
531                 this.textEditContext.SelectionUpdating -= TextEditContext_SelectionUpdating;
532                 this.textEditContext.FormatUpdating -= TextEditContext_FormatUpdating;
533                 this.textEditContext.FocusRemoved -= TextEditContext_FocusRemoved;
534                 this.textEditContext.NotifyFocusLeaveCompleted -= TextEditContext_NotifyFocusLeaveCompleted;
535                 this.textEditContext = null;
536             }
537         }
538
539         /// <inheritdoc/>
540         protected override async void OnKeyDown(KeyRoutedEventArgs e)
541         {
542             bool isControlPressed = this.IsModiferKeyPressed(VirtualKey.Control);
543             bool isShiftPressed = this.IsModiferKeyPressed(VirtualKey.Shift);
544             bool isMovedCaret = false;
545
546             var autocomplete = this.Document.AutoComplete as AutoCompleteBox;
547             if (autocomplete != null && autocomplete.ProcessKeyDown(this, e, isControlPressed, isShiftPressed))
548                 return;
549
550             switch (e.Key)
551             {
552                 case VirtualKey.Up:
553                     this._Controller.MoveCaretVertical(-1, isShiftPressed);
554                     this.Refresh();
555                     e.Handled = true;
556                     isMovedCaret = true;
557                     break;
558                 case VirtualKey.Down:
559                     this._Controller.MoveCaretVertical(+1, isShiftPressed);
560                     this.Refresh();
561                     e.Handled = true;
562                     isMovedCaret = true;
563                     break;
564                 case VirtualKey.Left:
565                     this._Controller.MoveCaretHorizontical(-1, isShiftPressed, isControlPressed);
566                     this.Refresh();
567                     e.Handled = true;
568                     isMovedCaret = true;
569                     break;
570                 case VirtualKey.Right:
571                     this._Controller.MoveCaretHorizontical(1, isShiftPressed, isControlPressed);
572                     this.Refresh();
573                     e.Handled = true;
574                     isMovedCaret = true;
575                     break;
576                 case VirtualKey.PageUp:
577                     this._Controller.Scroll(ScrollDirection.Up, this.View.LineCountOnScreen, isShiftPressed, true);
578                     this.Refresh();
579                     isMovedCaret = true;
580                     break;
581                 case VirtualKey.PageDown:
582                     this._Controller.Scroll(ScrollDirection.Down, this.View.LineCountOnScreen, isShiftPressed, true);
583                     this.Refresh();
584                     isMovedCaret = true;
585                     break;
586                 case VirtualKey.Home:
587                     if (isControlPressed)
588                         this._Controller.JumpToHead(isShiftPressed);
589                     else
590                         this.Controller.JumpToLineHead(this.Document.CaretPostion.row,isShiftPressed);
591                     this.Refresh();
592                     isMovedCaret = true;
593                     break;
594                 case VirtualKey.End:
595                     if (isControlPressed)
596                         this._Controller.JumpToEnd(isShiftPressed);
597                     else
598                         this.Controller.JumpToLineEnd(this.Document.CaretPostion.row,isShiftPressed);
599                     this.Refresh();
600                     isMovedCaret = true;
601                     break;
602                 case VirtualKey.Tab:
603                     if (!isControlPressed)
604                     {
605                         if (this._Controller.SelectionLength == 0)
606                             this._Controller.DoInputChar('\t');
607                         else if (isShiftPressed)
608                             this._Controller.DownIndent();
609                         else
610                             this._Controller.UpIndent();
611                         this.Refresh();
612                         e.Handled = true;
613                     }
614                     break;
615                 case VirtualKey.Enter:
616                     this._Controller.DoEnterAction();
617                     this.Refresh();
618                     e.Handled = true;
619                     break;
620                 case VirtualKey.Insert:
621                     if(this.View.InsertMode)
622                         this.View.InsertMode = false;
623                     else
624                         this.View.InsertMode = true;
625                     this.Refresh();
626                     e.Handled = true;
627                     break;
628                 case VirtualKey.A:
629                     if (isControlPressed)
630                     {
631                         this.SelectAll();
632                         this.Refresh();
633                         e.Handled = true;
634                     }
635                     break;
636                 case VirtualKey.B:
637                     if (isControlPressed)
638                     {
639                         if (this._Controller.RectSelection)
640                             this._Controller.RectSelection = false;
641                         else
642                             this._Controller.RectSelection = true;
643                         this.Refresh();
644                         e.Handled = true;
645                     }
646                     break;
647                 case VirtualKey.C:
648                     if (isControlPressed)
649                     {
650                         this.CopyCommand();
651                         e.Handled = true;
652                     }
653                     break;
654                 case VirtualKey.X:
655                     if (isControlPressed)
656                     {
657                         this.CutCommand();
658                         e.Handled = true;
659                     }
660                     break;
661                 case VirtualKey.V:
662                     if (isControlPressed)
663                     {
664                         await this.PasteCommand();
665                         e.Handled = true;
666                     }
667                     break;
668                 case VirtualKey.Y:
669                     if (isControlPressed)
670                     {
671                         this.Document.UndoManager.redo();
672                         this.Refresh();
673                         e.Handled = true;
674                     }
675                     break;
676                 case VirtualKey.Z:
677                     if (isControlPressed)
678                     {
679                         this.Document.UndoManager.undo();
680                         this.Refresh();
681                         e.Handled = true;
682                     }
683                     break;
684                 case VirtualKey.Back:
685                     this._Controller.DoBackSpaceAction();
686                     this.Refresh();
687                     e.Handled = true;
688                     break;
689                 case VirtualKey.Delete:
690                     this._Controller.DoDeleteAction();
691                     this.Refresh();
692                     e.Handled = true;
693                     break;
694             }
695 #if ENABLE_AUTMATION
696             if (isMovedCaret && this.peer != null)
697                 this.peer.OnNotifyCaretChanged();
698 #endif
699             base.OnKeyDown(e);
700         }
701
702         /// <inheritdoc/>
703         protected override void OnPointerPressed(PointerRoutedEventArgs e)
704         {
705             System.Diagnostics.Debug.WriteLine("pointer pressed");
706             this.CapturePointer(e.Pointer);
707             this.gestureRecongnizer.ProcessDownEvent(e.GetCurrentPoint(this));
708             e.Handled = true;
709         }
710
711         /// <inheritdoc/>
712         protected override void OnPointerMoved(PointerRoutedEventArgs e)
713         {
714             System.Diagnostics.Debug.WriteLine("pointer moved");
715             try
716             {
717                 this.gestureRecongnizer.ProcessMoveEvents(e.GetIntermediatePoints(this));
718             }catch(System.Runtime.InteropServices.COMException ex)
719             {
720                 //ピンチズームでこの例外が発生するが、回避できない
721                 System.Diagnostics.Debug.WriteLine("expection:" + ex);
722             }
723             e.Handled = true;
724
725             if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
726             {
727                 Point p = e.GetCurrentPoint(this).Position;
728                 if (this.View.HitTextArea(p.X, p.Y))
729                 {
730                     TextPoint tp = this.View.GetTextPointFromPostion(p);
731                     if (this._Controller.IsMarker(tp, HilightType.Url))
732                         Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Hand, 101);
733                     else
734                         Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.IBeam, 101);
735                 }
736                 else
737                 {
738                     Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Arrow, 101);
739                 }
740             }
741         }
742
743         /// <inheritdoc/>
744         protected override void OnPointerReleased(PointerRoutedEventArgs e)
745         {
746             System.Diagnostics.Debug.WriteLine("pointer released");
747             this.gestureRecongnizer.ProcessUpEvent(e.GetCurrentPoint(this));
748             e.Handled = true;
749         }
750
751         /// <inheritdoc/>
752         protected override void OnPointerCanceled(PointerRoutedEventArgs e)
753         {
754             System.Diagnostics.Debug.WriteLine("pointer canceled");
755             this.gestureRecongnizer.CompleteGesture();
756             e.Handled = true;
757         }
758
759         /// <inheritdoc/>
760         protected override void OnPointerWheelChanged(PointerRoutedEventArgs e)
761         {
762             System.Diagnostics.Debug.WriteLine("pointer wheelchanged");
763             bool shift = (e.KeyModifiers & Windows.System.VirtualKeyModifiers.Shift) ==
764                 Windows.System.VirtualKeyModifiers.Shift;
765             bool ctrl = (e.KeyModifiers & Windows.System.VirtualKeyModifiers.Control) ==
766                 Windows.System.VirtualKeyModifiers.Control;
767             this.gestureRecongnizer.ProcessMouseWheelEvent(e.GetCurrentPoint(this), shift, ctrl);
768             e.Handled = true;
769         }
770
771         private void TextEditContext_FormatUpdating(CoreTextEditContext sender, CoreTextFormatUpdatingEventArgs args)
772         {
773             if (this.Document.Length == 0 || args.IsCanceled)
774             {
775                 args.Result = CoreTextFormatUpdatingResult.Failed;
776                 return;
777             }
778             System.Diagnostics.Debug.WriteLine("core text format updating range({0}-{1}) underline type:{2} underline color:{3} reason:{4} textcolor:{5} background:{6}",
779                 args.Range.StartCaretPosition,
780                 args.Range.EndCaretPosition,
781                 args.UnderlineType,
782                 args.UnderlineColor,
783                 args.Reason,
784                 args.TextColor,
785                 args.BackgroundColor
786                 );
787             HilightType type = HilightType.None;
788             Color color = new Color(this.Foreground.A,this.Foreground.R,this.Foreground.G,this.Foreground.R);
789             bool isBoldLine = false;
790             switch (args.UnderlineType)
791             {
792                 case UnderlineType.Dotted:
793                     type = HilightType.Dot;
794                     break;
795                 case UnderlineType.Single:
796                     type = HilightType.Sold;
797                     break;
798                 case UnderlineType.Dash:
799                     type = HilightType.Dash;
800                     break;
801                 case UnderlineType.Wave:
802                     type = HilightType.Squiggle;
803                     break;
804                 case UnderlineType.Thin:
805                     type = HilightType.Sold;
806                     break;
807                 case UnderlineType.Thick:
808                     type = HilightType.Sold;
809                     isBoldLine = true;
810                     break;
811             }
812             int start = args.Range.StartCaretPosition;
813             int lengt = args.Range.EndCaretPosition - args.Range.StartCaretPosition;
814             this.Document.SetMarker(MarkerIDs.IME, Marker.Create(start, lengt, type, color, isBoldLine));
815
816             if (args.Reason == CoreTextFormatUpdatingReason.CompositionTargetConverted)
817             {
818                 this.View.AdjustSrc(args.Range.StartCaretPosition);
819             }
820             this.Refresh();
821
822             args.Result = CoreTextFormatUpdatingResult.Succeeded;
823         }
824
825         private void TextEditContext_TextRequested(CoreTextEditContext sender, CoreTextTextRequestedEventArgs args)
826         {
827             CoreTextTextRequest req = args.Request;
828
829             if (this.Document.Length == 0 || req.IsCanceled)
830             {
831                 return;
832             }
833
834             int start = req.Range.StartCaretPosition;
835             int end = req.Range.EndCaretPosition;
836             if (end > this.Document.Length)
837                 end = this.Document.Length;
838
839             int length = end - start;
840
841             System.Diagnostics.Debug.WriteLine("req text start:{0} length:{1}", start, length);
842
843             //キャレット位置も含むので+1する必要はない
844             req.Text = this.Document.ToString(start,length);
845         }
846
847         private void TextEditContext_LayoutRequested(CoreTextEditContext sender, CoreTextLayoutRequestedEventArgs args)
848         {
849             //変換候補の範囲を取得する
850             Point startPos, endPos;
851             int i_startIndex = args.Request.Range.StartCaretPosition;
852             int i_endIndex = args.Request.Range.EndCaretPosition;
853
854             if(args.Request.IsCanceled)
855             {
856                 return;
857             }
858
859             System.Diagnostics.Debug.WriteLine("core text layoutreq range({0}-{1})",i_startIndex,i_endIndex);
860
861             if (i_startIndex != -1 && i_endIndex != -1)
862             {
863                 TextStoreHelper.GetStringExtent(this.Document, this.View, i_startIndex, i_endIndex, out startPos, out endPos);
864
865                 double scale = Util.GetScale();
866
867                 //Core.Textはスクリーン座標に変換してくれないので自前で変換する(しかも、デバイス依存の座標で返さないといけない)
868                 var screenStartPos = Util.GetScreentPoint(startPos, this).Scale(scale);
869                 var screenEndPos = Util.GetScreentPoint(endPos, this).Scale(scale);
870                 args.Request.LayoutBounds.TextBounds = new Rect(
871                     screenStartPos.X,
872                     screenStartPos.Y,
873                     Math.Max(0,screenEndPos.X - screenStartPos.X),  //折り返されている場合、負になることがある
874                     screenEndPos.Y - screenStartPos.Y);
875             }
876
877             //コントロールの範囲を取得する
878             var controlTopLeft = new Point(0, 0);
879             var controlBottomRight = new Point(this.RenderSize.Width, this.RenderSize.Height);
880
881             var gt = this.TransformToVisual(Window.Current.Content);
882             controlTopLeft = gt.TransformPoint(controlTopLeft);
883             controlBottomRight = gt.TransformPoint(controlBottomRight);
884
885             args.Request.LayoutBounds.ControlBounds = new Rect(
886                 controlTopLeft.X,
887                 controlTopLeft.Y,
888                 controlBottomRight.X - controlTopLeft.X,
889                 controlBottomRight.Y - controlTopLeft.Y
890                 );
891         }
892
893         private void TextEditContext_SelectionRequested(CoreTextEditContext sender, CoreTextSelectionRequestedEventArgs args)
894         {
895             if(args.Request.IsCanceled || this.Document.Length == 0)
896             {
897                 return;
898             }
899             TextRange currentSelection = new TextRange();
900             TextStoreHelper.GetSelection(this._Controller, this.View.Selections, out currentSelection);
901
902             CoreTextRange currentSelectionRange = new CoreTextRange();
903             currentSelectionRange.StartCaretPosition = currentSelection.Index;
904             currentSelectionRange.EndCaretPosition = currentSelection.Index + currentSelection.Length;
905             args.Request.Selection = currentSelectionRange;
906             System.Diagnostics.Debug.WriteLine("req selection start:{0} end:{1}", currentSelectionRange.StartCaretPosition, currentSelectionRange.EndCaretPosition);
907         }
908
909         private void TextEditContext_SelectionUpdating(CoreTextEditContext sender, CoreTextSelectionUpdatingEventArgs args)
910         {
911             if(this.Document.Length == 0 || args.IsCanceled)
912             {
913                 args.Result = CoreTextSelectionUpdatingResult.Failed;
914                 return;
915             }
916             CoreTextRange sel = args.Selection;
917             System.Diagnostics.Debug.WriteLine("update selection start:{0} end:{1}", sel.StartCaretPosition, sel.EndCaretPosition);
918             TextStoreHelper.SetSelectionIndex(this.Controller, this.View, sel.StartCaretPosition, sel.EndCaretPosition);
919             args.Result = CoreTextSelectionUpdatingResult.Succeeded;
920             this.Refresh();
921         }
922
923         private void TextEditContext_TextUpdating(CoreTextEditContext sender, CoreTextTextUpdatingEventArgs args)
924         {
925             this.nowCompstion = true;
926
927             System.Diagnostics.Debug.WriteLine("update text (modify start:{0} end:{1}) text:{2} (new sel start:{0} end:{1})",
928                 args.Range.StartCaretPosition, 
929                 args.Range.EndCaretPosition, 
930                 args.Text, 
931                 args.NewSelection.StartCaretPosition, 
932                 args.NewSelection.EndCaretPosition);
933             bool isTip = args.InputLanguage.Script == "Latan";
934             CoreTextRange sel = args.Range;
935             TextStoreHelper.SetSelectionIndex(this.Controller, this.View, sel.StartCaretPosition, sel.EndCaretPosition);
936             TextStoreHelper.InsertTextAtSelection(this._Controller, args.Text, isTip);
937             this.Refresh();
938             args.Result = CoreTextTextUpdatingResult.Succeeded;
939
940             this.nowCompstion = false;
941         }
942
943         private void TextEditContext_CompositionCompleted(CoreTextEditContext sender, CoreTextCompositionCompletedEventArgs args)
944         {
945             System.Diagnostics.Debug.WriteLine("end compostion");
946             TextStoreHelper.EndCompostion(this.Document);
947             this.Document.RemoveAllMarker(MarkerIDs.IME);
948             this.Refresh();
949         }
950
951         private void TextEditContext_CompositionStarted(CoreTextEditContext sender, CoreTextCompositionStartedEventArgs args)
952         {
953             System.Diagnostics.Debug.WriteLine("start compstion");
954             TextStoreHelper.StartCompstion(this.Document);
955         }
956
957         private void TextEditContext_NotifyFocusLeaveCompleted(CoreTextEditContext sender, object args)
958         {
959             System.Diagnostics.Debug.WriteLine("notify focus leaved");
960         }
961
962         private void TextEditContext_FocusRemoved(CoreTextEditContext sender, object args)
963         {
964             System.Diagnostics.Debug.WriteLine("focus leaved");
965         }
966
967         void Controller_SelectionChanged(object sender, EventArgs e)
968         {
969             if (this._Controller == null)
970                 return;
971
972             //こうしないと選択できなくなってしまう
973             this.nowCaretMove = true;
974             SetValue(SelectedTextProperty, this._Controller.SelectedText);
975             SetValue(CaretPostionPropertyKey, this.Document.CaretPostion);
976             this.nowCaretMove = false;
977
978             if(!this.nowCompstion)
979             {
980                 TextRange currentSelection = new TextRange();
981                 TextStoreHelper.GetSelection(this._Controller, this.View.Selections, out currentSelection);
982
983                 CoreTextRange currentSelectionRange = new CoreTextRange();
984                 currentSelectionRange.StartCaretPosition = currentSelection.Index;
985                 currentSelectionRange.EndCaretPosition = currentSelection.Index + currentSelection.Length;
986
987                 System.Diagnostics.Debug.WriteLine("notify selection start:{0} end:{1}", currentSelectionRange.StartCaretPosition, currentSelectionRange.EndCaretPosition);
988                 //変換中に呼び出してはいけない
989                 if (this.textEditContext != null)
990                     this.textEditContext.NotifySelectionChanged(currentSelectionRange);
991             }
992         }
993
994         Gripper hittedGripper;
995         void gestureRecongnizer_ManipulationStarted(GestureRecognizer sender, ManipulationStartedEventArgs e)
996         {
997             //Updateedの段階でヒットテストしてしまうとグリッパーを触ってもヒットしないことがある
998             this.hittedGripper = this.View.HitGripperFromPoint(e.Position);
999         }
1000
1001         private void GestureRecongnizer_ManipulationInertiaStarting(GestureRecognizer sender, ManipulationInertiaStartingEventArgs args)
1002         {
1003             //sender.InertiaTranslationDeceleration = 0.001f;
1004             //sender.InertiaExpansionDeceleration = 100.0f * 96.0f / 1000.0f;
1005             //sender.InertiaRotationDeceleration = 720.0f / (1000.0f * 1000.0f);
1006         }
1007
1008         void gestureRecongnizer_ManipulationUpdated(GestureRecognizer sender, ManipulationUpdatedEventArgs e)
1009         {
1010             if (e.Delta.Scale < 1)
1011             {
1012                 double newSize = this.Render.FontSize - 1;
1013                 if (newSize < 1)
1014                     newSize = 1;
1015                 this.Render.FontSize = newSize;
1016                 this.Refresh();
1017                 SetValue(MagnificationPowerPropertyKey, this.Render.FontSize / this.FontSize);
1018                 return;
1019             }
1020
1021             if (e.Delta.Scale > 1)
1022             {
1023                 double newSize = this.Render.FontSize + 1;
1024                 if (newSize > 72)
1025                     newSize = 72;
1026                 this.Render.FontSize = newSize;
1027                 this.Refresh();
1028                 SetValue(MagnificationPowerPropertyKey, this.Render.FontSize / this.FontSize);
1029                 return;
1030             }
1031
1032             if (this._Controller.MoveCaretAndGripper(e.Position, this.hittedGripper))
1033             {
1034 #if ENABLE_AUTMATION
1035                 if (this.peer != null)
1036                     this.peer.OnNotifyCaretChanged();
1037 #endif
1038                 this.Refresh();                
1039                 return;
1040             }
1041             
1042             Point translation = e.Delta.Translation;
1043
1044             //Xの絶対値が大きければ横方向のスクロールで、そうでなければ縦方向らしい
1045             if (Math.Abs(e.Cumulative.Translation.X) < Math.Abs(e.Cumulative.Translation.Y))
1046             {
1047                 int deltay = (int)Math.Abs(Math.Ceiling(translation.Y));
1048                 if (translation.Y < 0)
1049                     this._Controller.ScrollByPixel(ScrollDirection.Down, deltay, false, false);
1050                 else
1051                     this._Controller.ScrollByPixel(ScrollDirection.Up, deltay, false, false);
1052                 this.Refresh();
1053                 return;
1054             }
1055
1056             int deltax = (int)Math.Abs(Math.Ceiling(translation.X));
1057             if (deltax != 0)
1058             {
1059                 if (translation.X < 0)
1060                     this._Controller.Scroll(ScrollDirection.Left, deltax, false, false);
1061                 else
1062                     this._Controller.Scroll(ScrollDirection.Right, deltax, false, false);
1063                 this.Refresh();
1064             }
1065         }
1066
1067         void gestureRecongnizer_ManipulationCompleted(GestureRecognizer sender, ManipulationCompletedEventArgs e)
1068         {
1069         }
1070
1071         async void gestureRecongnizer_RightTapped(GestureRecognizer sender, RightTappedEventArgs e)
1072         {
1073             ResourceMap map = ResourceManager.Current.MainResourceMap.GetSubtree("FooEditEngine.UWP/Resources");
1074             ResourceContext context = ResourceContext.GetForCurrentView();
1075             if (this.View.HitTextArea(e.Position.X, e.Position.Y))
1076             {
1077                 FooContextMenuEventArgs args = new FooContextMenuEventArgs(e.Position);
1078                 if (this.ContextMenuOpening != null)
1079                     this.ContextMenuOpening(this, args);
1080                 if (!args.Handled)
1081                 {
1082                     PopupMenu ContextMenu = new PopupMenu();
1083                     ContextMenu.Commands.Add(new UICommand(map.GetValue("CopyMenuName", context).ValueAsString, (command) =>
1084                     {
1085                         this.CopyCommand();
1086                     }));
1087                     ContextMenu.Commands.Add(new UICommand(map.GetValue("CutMenuName", context).ValueAsString, (command) =>
1088                     {
1089                         this.CutCommand();
1090                     }));
1091                     ContextMenu.Commands.Add(new UICommand(map.GetValue("PasteMenuName", context).ValueAsString, async (command) =>
1092                     {
1093                         await this.PasteCommand();
1094                     }));
1095                     if (this._Controller.RectSelection)
1096                     {
1097                         ContextMenu.Commands.Add(new UICommand(map.GetValue("LineSelectMenuName", context).ValueAsString, (command) =>
1098                         {
1099                             this._Controller.RectSelection = false;
1100                         }));
1101                     }
1102                     else
1103                     {
1104                         ContextMenu.Commands.Add(new UICommand(map.GetValue("RectSelectMenuName", context).ValueAsString, (command) =>
1105                         {
1106                             this._Controller.RectSelection = true;
1107                         }));
1108                     }
1109                     var windowStartPos = Util.GetPointInWindow(e.Position, this);
1110                     await ContextMenu.ShowAsync(windowStartPos);
1111                 }
1112             }
1113         }
1114
1115         long lastDouleTapTick;
1116         const long allowTripleTapTimeSpan = 500;
1117         void gestureRecongnizer_Tapped(GestureRecognizer sender, TappedEventArgs e)
1118         {
1119             bool touched = e.PointerDeviceType == PointerDeviceType.Touch;
1120             this.Document.SelectGrippers.BottomLeft.Enabled = false;
1121             this.Document.SelectGrippers.BottomRight.Enabled = touched;
1122             this.JumpCaret(e.Position);
1123             if(e.TapCount == 1 && System.DateTime.Now.Ticks - lastDouleTapTick < allowTripleTapTimeSpan * 10000)    //トリプルタップ
1124             {
1125                 //タッチスクリーンで行選択した場合、アンカーインデックスを単語の先頭にしないとバグる
1126                 this.Document.SelectGrippers.BottomLeft.Enabled = touched;
1127                 this.Document.SelectLine(this.Controller.SelectionStart, touched);
1128                 this.Refresh();
1129             }
1130             else  if(e.TapCount == 2)   //ダブルタップ
1131             {
1132                 //タッチスクリーンで単語選択した場合、アンカーインデックスを単語の先頭にしないとバグる
1133                 this.Document.SelectGrippers.BottomLeft.Enabled = touched;
1134                 if (e.Position.X < this.Render.TextArea.X)
1135                     this.Document.SelectLine(this.Controller.SelectionStart, touched);
1136                 else
1137                     this.Document.SelectWord(this.Controller.SelectionStart, touched);
1138                 this.lastDouleTapTick = System.DateTime.Now.Ticks;
1139                 this.Refresh();
1140             }
1141         }
1142
1143         void JumpCaret(Point p)
1144         {
1145             TextPoint tp = this.View.GetTextPointFromPostion(p);
1146             if (tp == TextPoint.Null)
1147                 return;
1148
1149             int index = this.View.LayoutLines.GetIndexFromTextPoint(tp);
1150
1151             FoldingItem foldingData = this.View.HitFoldingData(p.X, tp.row);
1152             if (foldingData != null)
1153             {
1154                 if (foldingData.Expand)
1155                     this.View.LayoutLines.FoldingCollection.Collapse(foldingData);
1156                 else
1157                     this.View.LayoutLines.FoldingCollection.Expand(foldingData);
1158                 this._Controller.JumpCaret(foldingData.Start, false);
1159             }
1160             else
1161             {
1162                 this._Controller.JumpCaret(tp.row, tp.col, false);
1163             }
1164 #if ENABLE_AUTMATION
1165             if (this.peer != null)
1166                 this.peer.OnNotifyCaretChanged();
1167 #endif
1168             this.View.IsFocused = true;
1169             this.Focus(FocusState.Programmatic);
1170             this.Refresh();
1171         }
1172
1173         void gestureRecongnizer_Dragging(GestureRecognizer sender, DraggingEventArgs e)
1174         {
1175             Point p = e.Position;
1176             TextPointSearchRange searchRange;
1177             if (this.View.HitTextArea(p.X, p.Y))
1178                 searchRange = TextPointSearchRange.TextAreaOnly;
1179             else if (this._Controller.SelectionLength > 0)
1180                 searchRange = TextPointSearchRange.Full;
1181             else
1182                 return;
1183             TextPoint tp = this.View.GetTextPointFromPostion(p, searchRange);
1184             this._Controller.MoveCaretAndSelect(tp, this.IsModiferKeyPressed(VirtualKey.LeftControl));
1185 #if ENABLE_AUTMATION
1186             if (this.peer != null)
1187                 this.peer.OnNotifyCaretChanged();
1188 #endif
1189             this.Refresh();
1190         }
1191
1192         bool IsModiferKeyPressed(VirtualKey key)
1193         {
1194             CoreVirtualKeyStates state = Window.Current.CoreWindow.GetKeyState(key);
1195             return (state & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down;
1196         }
1197         void Refresh(Rectangle updateRect)
1198         {
1199             if (this.rectangle.ActualWidth == 0 || this.rectangle.ActualHeight == 0 || this.Visibility == Windows.UI.Xaml.Visibility.Collapsed)
1200                 return;
1201
1202             this.Render.DrawContent(this.View, this.IsEnabled, updateRect);
1203
1204             this.Document.IsRequestRedraw = false;
1205         }
1206
1207
1208         bool Resize(double width, double height)
1209         {
1210             if (width == 0 || height == 0)
1211                 throw new ArgumentOutOfRangeException();
1212             if (this.Render.Resize(this.rectangle, width, height))
1213             {
1214                 this.View.PageBound = new Rectangle(0, 0, width, height);
1215
1216                 if (this.horizontalScrollBar != null)
1217                 {
1218                     this.horizontalScrollBar.LargeChange = this.View.PageBound.Width;
1219                     this.horizontalScrollBar.Maximum = this.View.LongestWidth + this.horizontalScrollBar.LargeChange + 1;
1220                 }
1221                 if (this.verticalScrollBar != null)
1222                 {
1223                     this.verticalScrollBar.LargeChange = this.View.LineCountOnScreen;
1224                     this.verticalScrollBar.Maximum = this.View.LayoutLines.Count + this.verticalScrollBar.LargeChange + 1;
1225                 }
1226                 return true;
1227             }
1228             return false;
1229         }
1230
1231         void View_SrcChanged(object sender, EventArgs e)
1232         {
1233             if (this.horizontalScrollBar == null || this.verticalScrollBar == null)
1234                 return;
1235             EditView view = this.View;
1236             if (view.Src.Row > this.verticalScrollBar.Maximum)
1237                 this.verticalScrollBar.Maximum = view.Src.Row + view.LineCountOnScreen + 1;
1238             double absoulteX = Math.Abs(view.Src.X);
1239             if (absoulteX > this.horizontalScrollBar.Maximum)
1240                 this.horizontalScrollBar.Maximum = absoulteX + view.PageBound.Width + 1;
1241             if (view.Src.Row != this.verticalScrollBar.Value)
1242                 this.verticalScrollBar.Value = view.Src.Row;
1243             if (view.Src.X != this.horizontalScrollBar.Value)
1244                 this.horizontalScrollBar.Value = Math.Abs(view.Src.X);
1245         }
1246
1247         void FooTextBox_SizeChanged(object sender, SizeChangedEventArgs e)
1248         {
1249             if (this.Resize(this.rectangle.ActualWidth, this.rectangle.ActualHeight))
1250             {
1251                 //普通に再描写するとちらつく
1252                 this.Refresh(this.View.PageBound);
1253             }
1254         }
1255
1256         void horizontalScrollBar_Scroll(object sender, ScrollEventArgs e)
1257         {
1258             if (this.horizontalScrollBar == null)
1259                 return;
1260             double toX;
1261             if (this.FlowDirection == FlowDirection.LeftToRight)
1262                 toX = this.horizontalScrollBar.Value;
1263             else
1264                 toX = -this.horizontalScrollBar.Value;
1265             this._Controller.Scroll(toX, this.View.Src.Row, false, false);
1266             this.Refresh();
1267         }
1268
1269         void verticalScrollBar_Scroll(object sender, ScrollEventArgs e)
1270         {
1271             if (this.verticalScrollBar == null)
1272                 return;
1273             int newRow = (int)this.verticalScrollBar.Value;
1274             if (newRow >= this.View.LayoutLines.Count)
1275                 return;
1276             this._Controller.Scroll(this.View.Src.X, newRow, false, false);
1277             this.Refresh();
1278         }
1279
1280         void Document_Update(object sender, DocumentUpdateEventArgs e)
1281         {
1282             if (e.type == UpdateType.Replace && !this.nowCompstion)
1283             {
1284                 CoreTextRange oldTextRange = new CoreTextRange();
1285                 oldTextRange.StartCaretPosition = e.startIndex;
1286                 oldTextRange.EndCaretPosition = e.startIndex;
1287                 //削除する範囲が1以上の場合、ドキュメントを飛び越えることはできない
1288                 //https://msdn.microsoft.com/en-us/windows/uwp/input-and-devices/custom-text-input
1289                 if (e.removeLength > 0)
1290                     oldTextRange.EndCaretPosition += e.removeLength;
1291
1292                 TextRange currentSelection = new TextRange();
1293                 TextStoreHelper.GetSelection(this._Controller, this.View.Selections, out currentSelection);
1294
1295                 CoreTextRange newSelection = new CoreTextRange();
1296                 newSelection.StartCaretPosition = e.startIndex;
1297                 newSelection.EndCaretPosition = e.startIndex;
1298
1299                 //置き換え後の長さを指定する
1300                 //(注意:削除された文字数のほうが多い場合は0を指定しないいけない)
1301                 int newTextLength = e.insertLength;
1302
1303                 System.Diagnostics.Debug.WriteLine("notify text change (modify start:{0} end:{1}) newlength:{2} (new sel start:{3} end:{4})",
1304                     oldTextRange.StartCaretPosition, oldTextRange.EndCaretPosition, newTextLength, newSelection.StartCaretPosition, newSelection.EndCaretPosition);
1305                 //変換中に呼び出してはいけない
1306                 if(this.textEditContext != null)
1307                     this.textEditContext.NotifyTextChanged(oldTextRange, newTextLength, newSelection);
1308             }
1309 #if ENABLE_AUTMATION
1310             if (this.peer != null)
1311                 this.peer.OnNotifyTextChanged();
1312 #endif
1313         }
1314
1315         void FooTextBox_Loaded(object sender, RoutedEventArgs e)
1316         {
1317             this.Focus(FocusState.Programmatic);
1318         }
1319
1320         void timer_Tick(object sender, object e)
1321         {
1322             if (this.View.LayoutLines.HilightAll() || this.View.LayoutLines.GenerateFolding() || this.Document.IsRequestRedraw)
1323             {
1324                 this.Refresh(this.View.PageBound);
1325             }
1326         }
1327
1328         private void SetDocument(Document value)
1329         {
1330             if (value == null)
1331                 return;
1332
1333             Document old_doc = this._Document;
1334
1335             if (this._Document != null)
1336             {
1337                 old_doc.Update -= new DocumentUpdateEventHandler(Document_Update);
1338                 this._Document.SelectionChanged -= Controller_SelectionChanged;
1339                 this._Document.LoadProgress -= Document_LoadProgress;
1340                 this._Document.AutoCompleteChanged -= _Document_AutoCompleteChanged;
1341                 if (this._Document.AutoComplete != null)
1342                 {
1343                     this._Document.AutoComplete.GetPostion = null;
1344                     this._Document.AutoComplete = null;
1345                 }
1346
1347                 //NotifyTextChanged()を呼び出すと落ちるのでTextConextをごっそり作り替える
1348                 this.RemoveTextContext();
1349             }
1350
1351             System.Diagnostics.Debug.WriteLine("document switched");
1352
1353             this._Document = value;
1354             this._Document.LayoutLines.Render = this.Render;
1355             this._Document.Update += new DocumentUpdateEventHandler(Document_Update);
1356             this._Document.LoadProgress += Document_LoadProgress;
1357             this._Document.AutoCompleteChanged += _Document_AutoCompleteChanged;
1358             if (this._Document.AutoComplete != null && this._Document.AutoComplete.GetPostion == null)
1359                 this._Document_AutoCompleteChanged(this._Document, null);
1360             //初期化が終わっていればすべて存在する
1361             if (this.Controller != null && this.View != null)
1362             {
1363                 this.Controller.Document = value;
1364                 this.View.Document = value;
1365
1366                 this.Controller.AdjustCaret();
1367
1368                 this.CreateTextContext();
1369
1370                 //依存プロパティとドキュメント内容が食い違っているので再設定する
1371                 this.ShowFullSpace = value.ShowFullSpace;
1372                 this.ShowHalfSpace = value.ShowHalfSpace;
1373                 this.ShowLineBreak = value.ShowLineBreak;
1374                 this.ShowTab = value.ShowTab;
1375                 this.FlowDirection = value.RightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
1376                 this.IndentMode = value.IndentMode;
1377                 this.DrawCaretLine = !value.HideLineMarker;
1378                 this.InsertMode = value.InsertMode;
1379                 this.DrawRuler = !value.HideRuler;
1380                 this.DrawLineNumber = value.DrawLineNumber;
1381                 this.MarkURL = value.UrlMark;
1382                 this.LineBreakMethod = value.LineBreak;
1383                 this.LineBreakCharCount = value.LineBreakCharCount;
1384                 this.TabChars = value.TabStops;
1385
1386                 this.Refresh();
1387             }
1388             //TextEditContext作成後に設定しないと落ちることがある
1389             this._Document.SelectionChanged += Controller_SelectionChanged;
1390         }
1391
1392         private void _Document_AutoCompleteChanged(object sender, EventArgs e)
1393         {
1394             Document doc = (Document)sender;
1395             doc.AutoComplete.GetPostion = (tp, e_doc) =>
1396             {
1397                 var p = this.View.GetPostionFromTextPoint(tp);
1398                 int height = (int)e_doc.LayoutLines.GetLayout(e_doc.CaretPostion.row).Height;
1399
1400                 if (p.Y + AutoCompleteBox.CompleteListBoxHeight + height > e_doc.LayoutLines.Render.TextArea.Height)
1401                     p.Y -= AutoCompleteBox.CompleteListBoxHeight;
1402                 else
1403                     p.Y += height;
1404                 //AutoCompleteBox内ではCanvasで位置を指定しているので変換する必要がある
1405                 var pointInWindow = Util.GetPointInWindow(p, this);
1406                 return pointInWindow;
1407             };
1408         }
1409
1410         /// <inheritdoc/>
1411         public static void OnPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
1412         {
1413             FooTextBox source = (FooTextBox)sender;
1414             if(e.Property.Equals(SelectedTextProperty) && !source.nowCaretMove)
1415                 source._Controller.SelectedText = source.SelectedText;
1416             if (e.Property.Equals(DocumentProperty))
1417                 source.SetDocument(source.Document);
1418             if(e.Property.Equals(HilighterProperty))
1419                 source.View.Hilighter = source.Hilighter;
1420             if (e.Property.Equals(FoldingStrategyProperty))
1421                 source.View.LayoutLines.FoldingStrategy = source.FoldingStrategy;
1422             if (e.Property.Equals(IndentModeProperty))
1423                 source.Controller.IndentMode = source.IndentMode;
1424             if (e.Property.Equals(SelectionProperty) && !source.nowCaretMove)
1425                 source.Document.Select(source.Selection.Index,source.Selection.Length);
1426             if (e.Property.Equals(CaretPostionPropertyKey) && !source.nowCaretMove)
1427                 source.JumpCaret(source.CaretPostion.row, source.CaretPostion.col);
1428             if (e.Property.Equals(InsertModeProperty))
1429                 source.View.InsertMode = source.InsertMode;
1430             if (e.Property.Equals(TabCharsProperty))
1431                 source.Document.TabStops = source.TabChars;
1432             if (e.Property.Equals(RectSelectModeProperty))
1433                 source._Controller.RectSelection = source.RectSelectMode;
1434             if (e.Property.Equals(DrawCaretProperty))
1435                 source.View.HideCaret = !source.DrawCaret;
1436             if (e.Property.Equals(DrawCaretLineProperty))
1437                 source.View.HideLineMarker = !source.DrawCaretLine;
1438             if (e.Property.Equals(DrawLineNumberProperty))
1439                 source.Document.DrawLineNumber = source.DrawLineNumber;
1440             if (e.Property.Equals(MarkURLProperty))
1441                 source.Document.UrlMark = source.MarkURL;
1442             if (e.Property.Equals(LineBreakProperty))
1443                 source.Document.LineBreak = source.LineBreakMethod;
1444             if (e.Property.Equals(LineBreakCharCountProperty))
1445                 source.Document.LineBreakCharCount = source.LineBreakCharCount;
1446             if (e.Property.Equals(FlowDirectionProperty))
1447             {
1448                 source.Document.RightToLeft = source.FlowDirection == Windows.UI.Xaml.FlowDirection.RightToLeft;
1449                 if (source.horizontalScrollBar != null)
1450                     source.horizontalScrollBar.FlowDirection = source.FlowDirection;
1451             }
1452             if (e.Property.Equals(DrawRulerProperty))
1453             {
1454                 source.Document.HideRuler = !source.DrawRuler;
1455                 source._Controller.JumpCaret(source.Document.CaretPostion.row, source.Document.CaretPostion.col);
1456             }
1457 #if !DUMMY_RENDER
1458             if (e.Property.Equals(TextAntialiasModeProperty))
1459                 source.Render.TextAntialiasMode = source.TextAntialiasMode;
1460             if(e.Property.Equals(MagnificationPowerPropertyKey))
1461                 source.Render.FontSize = source.FontSize * source.MagnificationPower;
1462             if (e.Property.Equals(FontFamilyProperty))
1463                 source.Render.FontFamily = source.FontFamily;
1464             if (e.Property.Equals(FontStyleProperty))
1465                 source.Render.FontStyle = source.FontStyle;
1466             if (e.Property.Equals(FontWeightProperty))
1467                 source.Render.FontWeigth = source.FontWeight;
1468             if (e.Property.Equals(FontSizeProperty))
1469                 source.Render.FontSize = source.FontSize;
1470             if (e.Property.Equals(ForegroundProperty))
1471                 source.Render.Foreground = D2DRenderBase.ToColor4(source.Foreground);
1472             if (e.Property.Equals(HilightForegroundProperty))
1473                 source.Render.HilightForeground = D2DRenderBase.ToColor4(source.HilightForeground);
1474             if (e.Property.Equals(BackgroundProperty))
1475                 source.Render.Background = D2DRenderBase.ToColor4(source.Background);
1476             if (e.Property.Equals(ControlCharProperty))
1477                 source.Render.ControlChar = D2DRenderBase.ToColor4(source.ControlChar);
1478             if (e.Property.Equals(HilightProperty))
1479                 source.Render.Hilight = D2DRenderBase.ToColor4(source.Hilight);
1480             if (e.Property.Equals(Keyword1Property))
1481                 source.Render.Keyword1 = D2DRenderBase.ToColor4(source.Keyword1);
1482             if (e.Property.Equals(Keyword2Property))
1483                 source.Render.Keyword2 = D2DRenderBase.ToColor4(source.Keyword2);
1484             if (e.Property.Equals(CommentProperty))
1485                 source.Render.Comment = D2DRenderBase.ToColor4(source.Comment);
1486             if (e.Property.Equals(LiteralProperty))
1487                 source.Render.Literal = D2DRenderBase.ToColor4(source.Literal);
1488             if (e.Property.Equals(URLProperty))
1489                 source.Render.Url = D2DRenderBase.ToColor4(source.URL);
1490             if (e.Property.Equals(InsertCaretProperty))
1491                 source.Render.InsertCaret = D2DRenderBase.ToColor4(source.InsertCaret);
1492             if (e.Property.Equals(OverwriteCaretProperty))
1493                 source.Render.OverwriteCaret = D2DRenderBase.ToColor4(source.OverwriteCaret);
1494             if (e.Property.Equals(PaddingProperty))
1495                 source.View.Padding = new Padding((int)source.Padding.Left, (int)source.Padding.Top, (int)source.Padding.Right, (int)source.Padding.Bottom);
1496             if (e.Property.Equals(LineMarkerProperty))
1497                 source.Render.LineMarker = D2DRenderBase.ToColor4(source.LineMarker);
1498             if (e.Property.Equals(ShowFullSpaceProperty))
1499                 source.Render.ShowFullSpace = source.ShowFullSpace;
1500             if (e.Property.Equals(ShowHalfSpaceProperty))
1501                 source.Render.ShowHalfSpace = source.ShowHalfSpace;
1502             if (e.Property.Equals(ShowTabProperty))
1503                 source.Render.ShowTab = source.ShowTab;
1504             if (e.Property.Equals(ShowLineBreakProperty))
1505                 source.Render.ShowLineBreak = source.ShowLineBreak;
1506             if (e.Property.Equals(UpdateAreaProperty))
1507                 source.Render.UpdateArea = D2DRenderBase.ToColor4(source.UpdateArea);
1508             if (e.Property.Equals(LineNumberProperty))
1509                 source.Render.LineNumber = D2DRenderBase.ToColor4(source.LineNumber);
1510 #endif
1511         }
1512         #endregion
1513
1514         #region event
1515
1516         /// <summary>
1517         /// コンテキストメニューが表示されるときに呼び出されます
1518         /// </summary>
1519         public event EventHandler<FooContextMenuEventArgs> ContextMenuOpening;
1520
1521 #endregion
1522
1523 #region property
1524
1525         internal Controller Controller
1526         {
1527             get
1528             {
1529                 return this._Controller;
1530             }
1531         }
1532
1533         /// <summary>
1534         /// 文字列の描写に使用されるアンチエイリアシング モードを表します
1535         /// </summary>
1536         public TextAntialiasMode TextAntialiasMode
1537         {
1538             get { return (TextAntialiasMode)GetValue(TextAntialiasModeProperty); }
1539             set { SetValue(TextAntialiasModeProperty, value); }
1540         }
1541
1542         /// <summary>
1543         /// TextAntialiasModeの依存プロパティを表す
1544         /// </summary>
1545         public static readonly DependencyProperty TextAntialiasModeProperty =
1546             DependencyProperty.Register("TextAntialiasMode", typeof(TextAntialiasMode), typeof(FooTextBox), new PropertyMetadata(TextAntialiasMode.Default, OnPropertyChanged));
1547
1548         /// <summary>
1549         /// シンタックスハイライターを表す
1550         /// </summary>
1551         public IHilighter Hilighter
1552         {
1553             get { return (IHilighter)GetValue(HilighterProperty); }
1554             set { SetValue(HilighterProperty, value); }
1555         }
1556
1557         /// <summary>
1558         /// Hilighterの依存プロパティを表す
1559         /// </summary>
1560         public static readonly DependencyProperty HilighterProperty =
1561             DependencyProperty.Register("Hilighter", typeof(IHilighter), typeof(FooTextBox), new PropertyMetadata(null, OnPropertyChanged));
1562
1563         /// <summary>
1564         /// フォールティングを作成するインターフェイスを表す
1565         /// </summary>
1566         public IFoldingStrategy FoldingStrategy
1567         {
1568             get { return (IFoldingStrategy)GetValue(FoldingStrategyProperty); }
1569             set { SetValue(FoldingStrategyProperty, value); }
1570         }
1571
1572         /// <summary>
1573         /// FoldingStrategyの依存プロパティ
1574         /// </summary>
1575         public static readonly DependencyProperty FoldingStrategyProperty =
1576             DependencyProperty.Register("FoldingStrategy", typeof(IFoldingStrategy), typeof(FooTextBox), new PropertyMetadata(null,OnPropertyChanged));
1577
1578         /// <summary>
1579         /// マーカーパターンセットを表す
1580         /// </summary>
1581         public MarkerPatternSet MarkerPatternSet
1582         {
1583             get
1584             {
1585                 return this.Document.MarkerPatternSet;
1586             }
1587         }
1588
1589         /// <summary>
1590         /// ドキュメントを表す
1591         /// </summary>
1592         /// <remarks>切り替え後に再描写が行われます</remarks>
1593         public Document Document
1594         {
1595             get { return (Document)GetValue(DocumentProperty); }
1596             set { SetValue(DocumentProperty, value); }
1597         }
1598
1599         // Using a DependencyProperty as the backing store for Document.  This enables animation, styling, binding, etc...
1600         public static readonly DependencyProperty DocumentProperty =
1601             DependencyProperty.Register("Document", typeof(Document), typeof(FooTextBox), new PropertyMetadata(null, OnPropertyChanged));
1602
1603
1604
1605         /// <summary>
1606         /// レイアウト行を表す
1607         /// </summary>
1608         public LineToIndexTable LayoutLineCollection
1609         {
1610             get { return this.View.LayoutLines; }
1611         }
1612
1613         /// <summary>
1614         /// 選択中の文字列を表す
1615         /// </summary>
1616         public string SelectedText
1617         {
1618             get { return (string)GetValue(SelectedTextProperty); }
1619             set { SetValue(SelectedTextProperty, value); }
1620         }
1621
1622         /// <summary>
1623         /// SelectedTextの依存プロパティを表す
1624         /// </summary>
1625         public static readonly DependencyProperty SelectedTextProperty =
1626             DependencyProperty.Register("SelectedText", typeof(string), typeof(FooTextBox), new PropertyMetadata(null, OnPropertyChanged));
1627
1628         /// <summary>
1629         /// インデントの方法を表す
1630         /// </summary>
1631         public IndentMode IndentMode
1632         {
1633             get { return (IndentMode)GetValue(IndentModeProperty); }
1634             set { SetValue(IndentModeProperty, value); }
1635         }
1636
1637         /// <summary>
1638         /// IndentModeの依存プロパティを表す
1639         /// </summary>
1640         public static readonly DependencyProperty IndentModeProperty =
1641             DependencyProperty.Register("IndentMode", typeof(IndentMode), typeof(FooTextBox), new PropertyMetadata(IndentMode.Tab,OnPropertyChanged));
1642
1643         /// <summary>
1644         /// 選択範囲を表す
1645         /// </summary>
1646         /// <remarks>
1647         /// Lengthが0の場合はキャレット位置を表します。
1648         /// 矩形選択モードの場合、選択範囲の文字数ではなく、開始位置から終了位置までの長さとなります
1649         /// </remarks>
1650         public TextRange Selection
1651         {
1652             get { return (TextRange)GetValue(SelectionProperty); }
1653             set { SetValue(SelectionProperty, value); }
1654         }
1655
1656         /// <summary>
1657         /// Selectionの依存プロパティを表す
1658         /// </summary>
1659         public static readonly DependencyProperty SelectionProperty =
1660             DependencyProperty.Register("Selection", typeof(TextRange), typeof(FooTextBox), new PropertyMetadata(TextRange.Null, OnPropertyChanged));
1661
1662         /// <summary>
1663         /// 拡大率を表す
1664         /// </summary>
1665         public double MagnificationPower
1666         {
1667             get { return (double)GetValue(MagnificationPowerPropertyKey); }
1668             set { SetValue(MagnificationPowerPropertyKey, value); }
1669         }
1670
1671         /// <summary>
1672         /// 拡大率を表す依存プロパティ
1673         /// </summary>
1674         public static readonly DependencyProperty MagnificationPowerPropertyKey =
1675             DependencyProperty.Register("MagnificationPower", typeof(double), typeof(FooTextBox), new PropertyMetadata(1.0, OnPropertyChanged));
1676
1677         /// <summary>
1678         /// キャレット位置を表す
1679         /// </summary>
1680         public TextPoint CaretPostion
1681         {
1682             get { return (TextPoint)GetValue(CaretPostionPropertyKey); }
1683             set { SetValue(CaretPostionPropertyKey, value); }
1684         }
1685
1686         static readonly DependencyProperty CaretPostionPropertyKey =
1687             DependencyProperty.Register("CaretPostion", typeof(TextPoint), typeof(FooTextBox), new PropertyMetadata(new TextPoint(), OnPropertyChanged));
1688
1689         /// <summary>
1690         /// レタリング方向を表す
1691         /// </summary>
1692         public new FlowDirection FlowDirection
1693         {
1694             get { return (FlowDirection)GetValue(FlowDirectionProperty); }
1695             set { SetValue(FlowDirectionProperty, value); }
1696         }
1697
1698         /// <summary>
1699         /// レタリング方向を表す。これは依存プロパティです
1700         /// </summary>
1701         public new static readonly DependencyProperty FlowDirectionProperty =
1702             DependencyProperty.Register("FlowDirection", typeof(FlowDirection), typeof(FooTextBox), new PropertyMetadata(FlowDirection.LeftToRight,OnPropertyChanged));
1703
1704         /// <summary>
1705         /// フォントファミリーを表す
1706         /// </summary>
1707         public new FontFamily FontFamily
1708         {
1709             get { return (FontFamily)GetValue(FontFamilyProperty); }
1710             set { SetValue(FontFamilyProperty, value); }
1711         }
1712
1713         /// <summary>
1714         /// FontFamilyの依存プロパティを表す
1715         /// </summary>
1716         public new static readonly DependencyProperty FontFamilyProperty =
1717             DependencyProperty.Register("FontFamily", typeof(FontFamily), typeof(FooTextBox), new PropertyMetadata(new FontFamily("Cambria"), OnPropertyChanged));
1718
1719         /// <summary>
1720         /// フォントサイズを表す
1721         /// </summary>
1722         public new double FontSize
1723         {
1724             get { return (double)GetValue(FontSizeProperty); }
1725             set { SetValue(FontSizeProperty, value); }
1726         }
1727
1728         /// <summary>
1729         /// FontSizeの依存プロパティを表す
1730         /// </summary>
1731         public new static readonly DependencyProperty FontSizeProperty =
1732             DependencyProperty.Register("FontSize", typeof(double), typeof(FooTextBox), new PropertyMetadata(12.0,OnPropertyChanged));
1733
1734         /// <summary>
1735         /// フォントスタイルを表す
1736         /// </summary>
1737         public new FontStyle FontStyle
1738         {
1739             get { return (FontStyle)GetValue(FontStyleProperty); }
1740             set { SetValue(FontStyleProperty, value); }
1741         }
1742
1743         /// <summary>
1744         /// FontStyleの依存プロパティを表す
1745         /// </summary>
1746         public new static readonly DependencyProperty FontStyleProperty =
1747             DependencyProperty.Register("FontStyle", typeof(FontStyle), typeof(FooTextBox), new PropertyMetadata(FontStyle.Normal,OnPropertyChanged));
1748
1749         /// <summary>
1750         /// フォントの幅を表す
1751         /// </summary>
1752         public new FontWeight FontWeight
1753         {
1754             get { return (FontWeight)GetValue(FontWeightProperty); }
1755             set { SetValue(FontWeightProperty, value); }
1756         }
1757
1758         /// <summary>
1759         /// FontWeigthの依存プロパティを表す
1760         /// </summary>
1761         public new static readonly DependencyProperty FontWeightProperty =
1762             DependencyProperty.Register("FontWeigth", typeof(FontWeight), typeof(FooTextBox), new PropertyMetadata(FontWeights.Normal,OnPropertyChanged));
1763
1764         /// <summary>
1765         /// デフォルトの文字色を表す。これは依存プロパティです
1766         /// </summary>
1767         public new Windows.UI.Color Foreground
1768         {
1769             get { return (Windows.UI.Color)GetValue(ForegroundProperty); }
1770             set { SetValue(ForegroundProperty, value); }
1771         }
1772
1773         /// <summary>
1774         /// Foregroundの依存プロパティを表す
1775         /// </summary>
1776         public new static readonly DependencyProperty ForegroundProperty =
1777             DependencyProperty.Register("Foreground", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.Black, OnPropertyChanged));
1778
1779         /// <summary>
1780         /// 選択時の文字色を表す。これは依存プロパティです
1781         /// </summary>
1782         public Windows.UI.Color HilightForeground
1783         {
1784             get { return (Windows.UI.Color)GetValue(HilightForegroundProperty); }
1785             set { SetValue(HilightForegroundProperty, value); }
1786         }
1787
1788         /// <summary>
1789         /// HilightForegroundForegroundの依存プロパティを表す
1790         /// </summary>
1791         public static readonly DependencyProperty HilightForegroundProperty =
1792             DependencyProperty.Register("HilightForeground", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.White, OnPropertyChanged));
1793
1794         /// <summary>
1795         /// 背景色を表す。これは依存プロパティです
1796         /// </summary>
1797         public new Windows.UI.Color Background
1798         {
1799             get { return (Windows.UI.Color)GetValue(BackgroundProperty); }
1800             set { SetValue(BackgroundProperty, value); }
1801         }
1802
1803         /// <summary>
1804         /// Backgroundの依存プロパティを表す
1805         /// </summary>
1806         public new static readonly DependencyProperty BackgroundProperty =
1807             DependencyProperty.Register("Background", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.White, OnPropertyChanged));
1808
1809         /// <summary>
1810         /// コントロールコードの文字色を表す。これは依存プロパティです
1811         /// </summary>
1812         public Windows.UI.Color ControlChar
1813         {
1814             get { return (Windows.UI.Color)GetValue(ControlCharProperty); }
1815             set { SetValue(ControlCharProperty, value); }
1816         }
1817
1818         /// <summary>
1819         /// ControlCharの依存プロパティを表す
1820         /// </summary>
1821         public static readonly DependencyProperty ControlCharProperty =
1822             DependencyProperty.Register("ControlChar", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.Gray, OnPropertyChanged));
1823
1824         /// <summary>
1825         /// 選択時の背景色を表す。これは依存プロパティです
1826         /// </summary>
1827         public Windows.UI.Color Hilight
1828         {
1829             get { return (Windows.UI.Color)GetValue(HilightProperty); }
1830             set { SetValue(HilightProperty, value); }
1831         }
1832
1833         /// <summary>
1834         /// Hilightの依存プロパティを表す
1835         /// </summary>
1836         public static readonly DependencyProperty HilightProperty =
1837             DependencyProperty.Register("Hilight", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.DodgerBlue, OnPropertyChanged));
1838
1839         /// <summary>
1840         /// キーワード1の文字色を表す。これは依存プロパティです
1841         /// </summary>
1842         public Windows.UI.Color Keyword1
1843         {
1844             get { return (Windows.UI.Color)GetValue(Keyword1Property); }
1845             set { SetValue(Keyword1Property, value); }
1846         }
1847
1848         /// <summary>
1849         /// Keyword1の依存プロパティを表す
1850         /// </summary>
1851         public static readonly DependencyProperty Keyword1Property =
1852             DependencyProperty.Register("Keyword1", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.Blue, OnPropertyChanged));
1853
1854         /// <summary>
1855         /// キーワード2の文字色を表す。これは依存プロパティです
1856         /// </summary>
1857         public Windows.UI.Color Keyword2
1858         {
1859             get { return (Windows.UI.Color)GetValue(Keyword2Property); }
1860             set { SetValue(Keyword2Property, value); }
1861         }
1862
1863         /// <summary>
1864         /// Keyword2の依存プロパティを表す
1865         /// </summary>
1866         public static readonly DependencyProperty Keyword2Property =
1867             DependencyProperty.Register("Keyword2", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.DarkCyan, OnPropertyChanged));
1868
1869         /// <summary>
1870         /// コメントの文字色を表す。これは依存プロパティです
1871         /// </summary>
1872         public Windows.UI.Color Comment
1873         {
1874             get { return (Windows.UI.Color)GetValue(CommentProperty); }
1875             set { SetValue(CommentProperty, value); }
1876         }
1877
1878         /// <summary>
1879         /// Commentの依存プロパティを表す
1880         /// </summary>
1881         public static readonly DependencyProperty CommentProperty =
1882             DependencyProperty.Register("Comment", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.Green, OnPropertyChanged));
1883
1884         /// <summary>
1885         /// 文字リテラルの文字色を表す。これは依存プロパティです
1886         /// </summary>
1887         public Windows.UI.Color Literal
1888         {
1889             get { return (Windows.UI.Color)GetValue(LiteralProperty); }
1890             set { SetValue(LiteralProperty, value); }
1891         }
1892
1893         /// <summary>
1894         /// Literalの依存プロパティを表す
1895         /// </summary>
1896         public static readonly DependencyProperty LiteralProperty =
1897             DependencyProperty.Register("Literal", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.Brown, OnPropertyChanged));
1898
1899         /// <summary>
1900         /// URLの文字色を表す。これは依存プロパティです
1901         /// </summary>
1902         public Windows.UI.Color URL
1903         {
1904             get { return (Windows.UI.Color)GetValue(URLProperty); }
1905             set { SetValue(URLProperty, value); }
1906         }
1907
1908         /// <summary>
1909         /// URLの依存プロパティを表す
1910         /// </summary>
1911         public static readonly DependencyProperty URLProperty =
1912             DependencyProperty.Register("URL", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.Blue, OnPropertyChanged));
1913
1914         /// <summary>
1915         /// 行更新フラグの色を表す
1916         /// </summary>
1917         public Windows.UI.Color UpdateArea
1918         {
1919             get { return (Windows.UI.Color)GetValue(UpdateAreaProperty); }
1920             set { SetValue(UpdateAreaProperty, value); }
1921         }
1922
1923         /// <summary>
1924         /// UpdateAreaの依存プロパティを表す
1925         /// </summary>
1926         public static readonly DependencyProperty UpdateAreaProperty =
1927             DependencyProperty.Register("UpdateArea", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.MediumSeaGreen, OnPropertyChanged));
1928
1929         /// <summary>
1930         /// ラインマーカーの色を表す
1931         /// </summary>
1932         public Windows.UI.Color LineMarker
1933         {
1934             get { return (Windows.UI.Color)GetValue(LineMarkerProperty); }
1935             set { SetValue(LineMarkerProperty, value); }
1936         }
1937
1938         /// <summary>
1939         /// LineMarkerの依存プロパティを表す
1940         /// </summary>
1941         public static readonly DependencyProperty LineMarkerProperty =
1942             DependencyProperty.Register("LineMarker", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.Gray, OnPropertyChanged));
1943
1944         /// <summary>
1945         /// 挿入モード時のキャレットの色を表す
1946         /// </summary>
1947         public Windows.UI.Color InsertCaret
1948         {
1949             get { return (Windows.UI.Color)GetValue(InsertCaretProperty); }
1950             set { SetValue(InsertCaretProperty, value); }
1951         }
1952
1953         /// <summary>
1954         /// InsertCaretの依存プロパティを表す
1955         /// </summary>
1956         public static readonly DependencyProperty InsertCaretProperty =
1957             DependencyProperty.Register("InsertCaret", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.Black, OnPropertyChanged));
1958
1959         /// <summary>
1960         /// 上書きモード時のキャレット職を表す
1961         /// </summary>
1962         public Windows.UI.Color OverwriteCaret
1963         {
1964             get { return (Windows.UI.Color)GetValue(OverwriteCaretProperty); }
1965             set { SetValue(OverwriteCaretProperty, value); }
1966         }
1967
1968         /// <summary>
1969         /// OverwriteCaretの依存プロパティを表す
1970         /// </summary>
1971         public static readonly DependencyProperty OverwriteCaretProperty =
1972             DependencyProperty.Register("OverwriteCaret", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.Black, OnPropertyChanged));
1973
1974         /// <summary>
1975         /// 行番号の色を表す
1976         /// </summary>
1977         public Windows.UI.Color LineNumber
1978         {
1979             get { return (Windows.UI.Color)GetValue(LineNumberProperty); }
1980             set { SetValue(LineNumberProperty, value); }
1981         }
1982
1983         /// <summary>
1984         /// Using a DependencyProperty as the backing store for LineNumber.  This enables animation, styling, binding, etc...
1985         /// </summary>
1986         public static readonly DependencyProperty LineNumberProperty =
1987             DependencyProperty.Register("LineNumber", typeof(Windows.UI.Color), typeof(FooTextBox), new PropertyMetadata(Colors.DimGray,OnPropertyChanged));
1988
1989         /// <summary>
1990         /// 余白を表す
1991         /// </summary>
1992         public new Thickness Padding
1993         {
1994             get { return (Thickness)GetValue(PaddingProperty); }
1995             set { SetValue(PaddingProperty, value); }
1996         }
1997
1998         /// <summary>
1999         /// Paddingの依存プロパティを表す
2000         /// </summary>
2001         public new static readonly DependencyProperty PaddingProperty =
2002             DependencyProperty.Register("Padding", typeof(Thickness), typeof(FooTextBox), new PropertyMetadata(new Thickness(),OnPropertyChanged));        
2003
2004         /// <summary>
2005         /// 挿入モードなら真を返し、そうでないなら、偽を返す。これは依存プロパティです
2006         /// </summary>
2007         public bool InsertMode
2008         {
2009             get { return (bool)GetValue(InsertModeProperty); }
2010             set { SetValue(InsertModeProperty, value); }
2011         }
2012
2013         /// <summary>
2014         /// InsertModeの依存プロパティを表す
2015         /// </summary>
2016         public static readonly DependencyProperty InsertModeProperty =
2017             DependencyProperty.Register("InsertMode",
2018             typeof(bool),
2019             typeof(FooTextBox),
2020             new PropertyMetadata(true, OnPropertyChanged));
2021
2022         /// <summary>
2023         /// タブの文字数を表す。これは依存プロパティです
2024         /// </summary>
2025         public int TabChars
2026         {
2027             get { return (int)GetValue(TabCharsProperty); }
2028             set { SetValue(TabCharsProperty, value); }
2029         }
2030
2031         /// <summary>
2032         /// TabCharsの依存プロパティを表す
2033         /// </summary>
2034         public static readonly DependencyProperty TabCharsProperty =
2035             DependencyProperty.Register("TabChars",
2036             typeof(int),
2037             typeof(FooTextBox),
2038             new PropertyMetadata(4, OnPropertyChanged));
2039
2040         /// <summary>
2041         /// 矩形選択モードなら真を返し、そうでないなら偽を返す。これは依存プロパティです
2042         /// </summary>
2043         public bool RectSelectMode
2044         {
2045             get { return (bool)GetValue(RectSelectModeProperty); }
2046             set { SetValue(RectSelectModeProperty, value); }
2047         }
2048
2049         /// <summary>
2050         /// RectSelectModeの依存プロパティを表す
2051         /// </summary>
2052         public static readonly DependencyProperty RectSelectModeProperty =
2053             DependencyProperty.Register("RectSelectMode", typeof(bool), typeof(FooTextBox), new PropertyMetadata(false, OnPropertyChanged));
2054
2055         /// <summary>
2056         /// 折り返しの方法を指定する
2057         /// </summary>
2058         /// <remarks>
2059         /// 変更した場合、レイアウトの再構築を行う必要があります
2060         /// </remarks>
2061         public LineBreakMethod LineBreakMethod
2062         {
2063             get { return (LineBreakMethod)GetValue(LineBreakProperty); }
2064             set { SetValue(LineBreakProperty, value); }
2065         }
2066
2067         /// <summary>
2068         /// LineBreakMethodの依存プロパティを表す
2069         /// </summary>
2070         public static readonly DependencyProperty LineBreakProperty =
2071             DependencyProperty.Register("LineBreakMethod", typeof(LineBreakMethod), typeof(FooTextBox), new PropertyMetadata(LineBreakMethod.None, OnPropertyChanged));
2072
2073
2074         /// <summary>
2075         /// 折り返しの幅を指定する。LineBreakMethod.CharUnit以外の時は無視されます
2076         /// </summary>
2077         /// <remarks>
2078         /// 変更した場合、レイアウトの再構築を行う必要があります
2079         /// </remarks>
2080         public int LineBreakCharCount
2081         {
2082             get { return (int)GetValue(LineBreakCharCountProperty); }
2083             set { SetValue(LineBreakCharCountProperty, value); }
2084         }
2085
2086         /// <summary>
2087         /// LineBreakCharCountの依存プロパティを表す
2088         /// </summary>
2089         public static readonly DependencyProperty LineBreakCharCountProperty =
2090             DependencyProperty.Register("LineBreakCharCount", typeof(int), typeof(FooTextBox), new PropertyMetadata(80, OnPropertyChanged));        
2091
2092         /// <summary>
2093         /// キャレットを描くなら真。そうでないなら偽を返す。これは依存プロパティです
2094         /// </summary>
2095         public bool DrawCaret
2096         {
2097             get { return (bool)GetValue(DrawCaretProperty); }
2098             set { SetValue(DrawCaretProperty, value); }
2099         }
2100
2101         /// <summary>
2102         /// DrawCaretの依存プロパティを表す
2103         /// </summary>
2104         public static readonly DependencyProperty DrawCaretProperty =
2105             DependencyProperty.Register("DrawCaret", typeof(bool), typeof(FooTextBox), new PropertyMetadata(true, OnPropertyChanged));
2106
2107
2108         /// <summary>
2109         /// キャレットラインを描くなら真。そうでないなら偽を返す。これは依存プロパティです
2110         /// </summary>
2111         public bool DrawCaretLine
2112         {
2113             get { return (bool)GetValue(DrawCaretLineProperty); }
2114             set { SetValue(DrawCaretLineProperty, value); }
2115         }
2116
2117         /// <summary>
2118         /// DrawCaretLineの依存プロパティを表す
2119         /// </summary>
2120         public static readonly DependencyProperty DrawCaretLineProperty =
2121             DependencyProperty.Register("DrawCaretLine", typeof(bool), typeof(FooTextBox), new PropertyMetadata(false, OnPropertyChanged));
2122
2123         /// <summary>
2124         /// 行番号を描くなら真。そうでなければ偽。これは依存プロパティです
2125         /// </summary>
2126         public bool DrawLineNumber
2127         {
2128             get { return (bool)GetValue(DrawLineNumberProperty); }
2129             set { SetValue(DrawLineNumberProperty, value); }
2130         }
2131
2132         /// <summary>
2133         /// ルーラーを描くなら真。そうでなければ偽。これは依存プロパティです
2134         /// </summary>
2135         public bool DrawRuler
2136         {
2137             get { return (bool)GetValue(DrawRulerProperty); }
2138             set { SetValue(DrawRulerProperty, value); }
2139         }
2140
2141         /// <summary>
2142         /// DrawRulerの依存プロパティを表す
2143         /// </summary>
2144         public static readonly DependencyProperty DrawRulerProperty =
2145             DependencyProperty.Register("DrawRuler", typeof(bool), typeof(FooTextBox), new PropertyMetadata(false, OnPropertyChanged));
2146
2147
2148         /// <summary>
2149         /// DrawLineNumberの依存プロパティを表す
2150         /// </summary>
2151         public static readonly DependencyProperty DrawLineNumberProperty =
2152             DependencyProperty.Register("DrawLineNumber", typeof(bool), typeof(FooTextBox), new PropertyMetadata(false, OnPropertyChanged));
2153
2154         /// <summary>
2155         /// URLに下線を引くなら真。そうでないなら偽を表す。これは依存プロパティです
2156         /// </summary>
2157         public bool MarkURL
2158         {
2159             get { return (bool)GetValue(MarkURLProperty); }
2160             set { SetValue(MarkURLProperty, value); }
2161         }
2162
2163         /// <summary>
2164         /// MarkURLの依存プロパティを表す
2165         /// </summary>
2166         public static readonly DependencyProperty MarkURLProperty =
2167             DependencyProperty.Register("MarkURL", typeof(bool), typeof(FooTextBox), new PropertyMetadata(false, OnPropertyChanged));
2168
2169         /// <summary>
2170         /// 全角スペースを表示するなら真。そうでないなら偽
2171         /// </summary>
2172         public bool ShowFullSpace
2173         {
2174             get { return (bool)GetValue(ShowFullSpaceProperty); }
2175             set { SetValue(ShowFullSpaceProperty, value); }
2176         }
2177
2178         /// <summary>
2179         /// ShowFullSpaceの依存プロパティを表す
2180         /// </summary>
2181         public static readonly DependencyProperty ShowFullSpaceProperty =
2182             DependencyProperty.Register("ShowFullSpace", typeof(bool), typeof(FooTextBox), new PropertyMetadata(false, OnPropertyChanged));
2183
2184         /// <summary>
2185         /// 半角スペースを表示するなら真。そうでないなら偽
2186         /// </summary>
2187         public bool ShowHalfSpace
2188         {
2189             get { return (bool)GetValue(ShowHalfSpaceProperty); }
2190             set { SetValue(ShowHalfSpaceProperty, value); }
2191         }
2192
2193         /// <summary>
2194         /// ShowHalfSpaceの依存プロパティを表す
2195         /// </summary>
2196         public static readonly DependencyProperty ShowHalfSpaceProperty =
2197             DependencyProperty.Register("ShowHalfSpace", typeof(bool), typeof(FooTextBox), new PropertyMetadata(false, OnPropertyChanged));
2198
2199         /// <summary>
2200         /// タブを表示するなら真。そうでないなら偽
2201         /// </summary>
2202         public bool ShowTab
2203         {
2204             get { return (bool)GetValue(ShowTabProperty); }
2205             set { SetValue(ShowTabProperty, value); }
2206         }
2207
2208         /// <summary>
2209         /// ShowTabの依存プロパティを表す
2210         /// </summary>
2211         public static readonly DependencyProperty ShowTabProperty =
2212             DependencyProperty.Register("ShowTab", typeof(bool), typeof(FooTextBox), new PropertyMetadata(false, OnPropertyChanged));
2213
2214         /// <summary>
2215         /// 改行マークを表示するなら真。そうでないなら偽
2216         /// </summary>
2217         public bool ShowLineBreak
2218         {
2219             get { return (bool)GetValue(ShowLineBreakProperty); }
2220             set { SetValue(ShowLineBreakProperty, value); }
2221         }
2222
2223         /// <summary>
2224         /// ShowLineBreakの依存プロパティを表す
2225         /// </summary>
2226         public static readonly DependencyProperty ShowLineBreakProperty =
2227             DependencyProperty.Register("ShowLineBreak", typeof(bool), typeof(FooTextBox), new PropertyMetadata(false,OnPropertyChanged));
2228
2229         
2230 #endregion
2231     }
2232     /// <summary>
2233     /// コンテキストメニューのイベントデーターを表す
2234     /// </summary>
2235     public class FooContextMenuEventArgs
2236     {
2237         /// <summary>
2238         /// 処理済みなら真。そうでないなら偽
2239         /// </summary>
2240         public bool Handled = false;
2241         /// <summary>
2242         /// コンテキストメニューを表示すべき座標を表す
2243         /// </summary>
2244         public Windows.Foundation.Point Postion;
2245         /// <summary>
2246         /// コンストラクター
2247         /// </summary>
2248         /// <param name="pos"></param>
2249         public FooContextMenuEventArgs(Windows.Foundation.Point pos)
2250         {
2251             this.Postion = pos;
2252         }
2253     }
2254 }