OSDN Git Service

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