OSDN Git Service

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