OSDN Git Service

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