OSDN Git Service

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