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