OSDN Git Service

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