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             this.CaptureMouse();
831
832             var p = this.GetDipFromPoint(e.GetPosition(this));
833             TextPoint tp = this.View.GetTextPointFromPostion(p);
834             if (tp == TextPoint.Null)
835                 return;
836             int index = this.View.LayoutLines.GetIndexFromTextPoint(tp);
837
838             FooMouseButtonEventArgs newEventArgs = new FooMouseButtonEventArgs(e.MouseDevice,
839                 e.Timestamp,
840                 e.ChangedButton,
841                 e.StylusDevice,
842                 index);
843             newEventArgs.RoutedEvent = e.RoutedEvent;
844             base.OnMouseDown(newEventArgs);
845
846             if (newEventArgs.Handled)
847                 return;
848
849             if (e.LeftButton == MouseButtonState.Pressed)
850             {
851                 FoldingItem foldingData = this.View.HitFoldingData(p.X,tp.row);
852                 if (foldingData != null)
853                 {
854                     if (foldingData.Expand)
855                         this.View.LayoutLines.FoldingCollection.Collapse(foldingData);
856                     else
857                         this.View.LayoutLines.FoldingCollection.Expand(foldingData);
858                     this._Controller.JumpCaret(foldingData.Start,false);
859                 }
860                 else
861                 {
862                     this._Controller.JumpCaret(tp.row, tp.col, false);
863                 }
864                 if (this.peer != null)
865                     this.peer.OnNotifyCaretChanged();
866                 this.View.IsFocused = true;
867                 this.Focus();
868                 this.Document.SelectGrippers.BottomLeft.Enabled = false;
869                 this.Document.SelectGrippers.BottomRight.Enabled = false;
870                 this.Refresh();
871             }
872         }
873
874         /// <summary>
875         /// マウスのボタンが離されたときに呼ばれます
876         /// </summary>
877         /// <param name="e"></param>
878         protected override void OnMouseUp(MouseButtonEventArgs e)
879         {
880             this.ReleaseMouseCapture();
881             base.OnMouseUp(e);
882         }
883
884         /// <summary>
885         /// マウスが移動したときに呼ばれます
886         /// </summary>
887         /// <param name="e">イベントパラメーター</param>
888         /// <remarks>
889         /// イベントパラメーターはFooMouseEventArgsにキャスト可能です。
890         /// e.Handledを真にした場合、選択処理と状況に応じたカーソルの変化が行われなくなります
891         /// </remarks>
892         protected override void  OnMouseMove(MouseEventArgs e)
893         {
894             bool leftPressed = e.LeftButton == MouseButtonState.Pressed;
895
896             var p = this.GetDipFromPoint(e.GetPosition(this));
897
898             TextPointSearchRange searchRange;
899             if (this.View.HitTextArea(p.X, p.Y))
900                 searchRange = TextPointSearchRange.TextAreaOnly;
901             else if (leftPressed)
902                 searchRange = TextPointSearchRange.Full;
903             else
904                 searchRange = TextPointSearchRange.TextAreaOnly;
905
906             TextPoint tp = this.View.GetTextPointFromPostion(p, searchRange);
907
908             if (tp == TextPoint.Null)
909             {
910                 base.OnMouseMove(e);
911                 return;
912             }
913
914             int index = this.View.GetIndexFromLayoutLine(tp);
915
916             FooMouseEventArgs newEventArgs = new FooMouseEventArgs(e.MouseDevice, e.Timestamp, e.StylusDevice, index);
917             newEventArgs.RoutedEvent = e.RoutedEvent;
918             base.OnMouseMove(newEventArgs);
919
920             if (newEventArgs.Handled)
921                 return;
922
923             //この状態のときはカーソルがテキストエリア内にある
924             if (searchRange == TextPointSearchRange.TextAreaOnly)
925             {
926                 if (this._Controller.IsMarker(tp, HilightType.Url))
927                     this.Cursor = Cursors.Hand;
928                 else
929                     this.Cursor = Cursors.IBeam;
930             }
931             else
932             {
933                 this.Cursor = Cursors.Arrow;
934             }
935
936             if (leftPressed)
937             {
938                 this._Controller.MoveCaretAndSelect(tp);
939                 if (this.peer != null)
940                     this.peer.OnNotifyCaretChanged();
941                 this.Refresh();
942             }
943         }
944
945         Gripper hittedGripper;
946         bool touchScrolled = false;
947
948         /// <inheritdoc/>
949         protected override void OnTouchDown(TouchEventArgs e)
950         {
951             var p = this.GetDipFromPoint(e.GetTouchPoint(this).Position);
952             this.hittedGripper = this.View.HitGripperFromPoint(p);
953             this.CaptureTouch(e.TouchDevice);
954         }
955
956         /// <inheritdoc/>
957         protected override void OnTouchUp(TouchEventArgs e)
958         {
959             this.ReleaseTouchCapture(e.TouchDevice);
960             if(this.hittedGripper != null || this.touchScrolled)
961             {
962                 this.hittedGripper = null;
963                 this.touchScrolled = false;
964                 return;
965             }
966
967             var p = this.GetDipFromPoint(e.GetTouchPoint(this).Position);
968             TextPoint tp = this.View.GetTextPointFromPostion(p);
969             if (tp == TextPoint.Null)
970                 return;
971             int index = this.View.LayoutLines.GetIndexFromTextPoint(tp);
972
973             FoldingItem foldingData = this.View.HitFoldingData(p.X, tp.row);
974             if (foldingData != null)
975             {
976                 if (foldingData.Expand)
977                     this.View.LayoutLines.FoldingCollection.Collapse(foldingData);
978                 else
979                     this.View.LayoutLines.FoldingCollection.Expand(foldingData);
980                 this._Controller.JumpCaret(foldingData.Start, false);
981             }
982             else
983             {
984                 this._Controller.JumpCaret(tp.row, tp.col, false);
985             }
986             if (this.peer != null)
987                 this.peer.OnNotifyCaretChanged();
988             this.View.IsFocused = true;
989             this.Focus();
990             this.Document.SelectGrippers.BottomRight.Enabled = true;
991             this.Refresh();
992         }
993
994         /// <inheritdoc/>
995         protected override void OnTouchMove(TouchEventArgs e)
996         {
997             var p = this.GetDipFromPoint(e.GetTouchPoint(this).Position);
998             if (this.Controller.MoveCaretAndGripper(p, this.hittedGripper))
999             {
1000                 if (this.peer != null)
1001                     this.peer.OnNotifyCaretChanged();
1002                 this.Refresh();
1003             }
1004         }
1005
1006         /// <inheritdoc/>
1007         protected override void OnManipulationInertiaStarting(ManipulationInertiaStartingEventArgs e)
1008         {
1009         }
1010
1011         /// <inheritdoc/>
1012         protected override void OnManipulationDelta(ManipulationDeltaEventArgs e)
1013         {
1014             if (this.hittedGripper != null)
1015                 return;
1016
1017             Point translation = new Point(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);
1018
1019             //Xの絶対値が大きければ横方向のスクロールで、そうでなければ縦方向らしい
1020             if (Math.Abs(e.CumulativeManipulation.Translation.X) < Math.Abs(e.CumulativeManipulation.Translation.Y))
1021             {
1022                 int deltay = (int)Math.Abs(Math.Ceiling(translation.Y));
1023                 if (translation.Y < 0)
1024                     this._Controller.ScrollByPixel(ScrollDirection.Down, deltay, false, false);
1025                 else
1026                     this._Controller.ScrollByPixel(ScrollDirection.Up, deltay, false, false);
1027                 this.touchScrolled = true;
1028                 this.Refresh();
1029                 return;
1030             }
1031
1032             int deltax = (int)Math.Abs(Math.Ceiling(translation.X));
1033             if (deltax != 0)
1034             {
1035                 if (translation.X < 0)
1036                     this._Controller.Scroll(ScrollDirection.Left, deltax, false, false);
1037                 else
1038                     this._Controller.Scroll(ScrollDirection.Right, deltax, false, false);
1039                 this.touchScrolled = true;
1040                 this.Refresh();
1041             }
1042         }
1043
1044         private Point GetDipFromPoint(Point p)
1045         {
1046             float dpi;
1047             this.Render.GetDpi(out dpi,out dpi);
1048             double scale = dpi / 96.0;
1049             return p.Scale(1 / scale);
1050         }
1051
1052         /// <inheritdoc/>
1053         protected override void OnMouseWheel(MouseWheelEventArgs e)
1054         {
1055             if(Keyboard.Modifiers == ModifierKeys.None)
1056             {
1057                 if (e.Delta > 0)
1058                     this._Controller.Scroll(ScrollDirection.Up, SystemParameters.WheelScrollLines, false, false);
1059                 else
1060                     this._Controller.Scroll(ScrollDirection.Down, SystemParameters.WheelScrollLines, false, false);
1061             }
1062             else if (Keyboard.Modifiers == ModifierKeys.Control)
1063             {
1064                 double newFontSize = this.Render.FontSize;
1065                 if (e.Delta > 0)
1066                     newFontSize++;
1067                 else
1068                     newFontSize--;
1069                 if (newFontSize > MaxFontSize)
1070                     newFontSize = 72;
1071                 else if (newFontSize < MinFontSize)
1072                     newFontSize = 1;
1073                 this.Render.FontSize = newFontSize;
1074                 SetValue(MagnificationPowerPropertyKey, this.Render.FontSize / this.FontSize);
1075             }
1076             this.Refresh();
1077             base.OnMouseWheel(e);
1078         }
1079
1080         void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
1081         {
1082             if (e.Category == UserPreferenceCategory.Keyboard)
1083             {
1084                 int blinkTime = (int)NativeMethods.GetCaretBlinkTime();
1085                 this.View.CaretBlink = blinkTime >= 0;
1086                 this.View.CaretBlinkTime = blinkTime * 2;
1087             }
1088             if (e.Category == UserPreferenceCategory.General)
1089             {
1090                 this.View.CaretWidthOnInsertMode = SystemParameters.CaretWidth;
1091             }
1092         }
1093
1094         void Document_Update(object sender, DocumentUpdateEventArgs e)
1095         {
1096             if (this.textStore.IsLocked())
1097                 return;
1098             if(e.type == UpdateType.Replace)
1099                 TextStoreHelper.NotifyTextChanged(this.textStore, e.startIndex, e.removeLength, e.insertLength);
1100             if(this.peer != null)
1101                 this.peer.OnNotifyTextChanged();
1102         }
1103
1104         void timer_Tick(object sender, EventArgs e)
1105         {
1106             if (this.image.ActualWidth == 0 || this.image.ActualHeight == 0)
1107                 return;
1108             if (this.Resize(this.image.ActualWidth, this.image.ActualHeight))
1109             {
1110                 this.Refresh();
1111                 return;
1112             }
1113
1114             bool updateAll = this.View.LayoutLines.HilightAll() || this.View.LayoutLines.GenerateFolding();
1115
1116             if (updateAll)
1117                 this.Refresh();
1118             else
1119                 this.Refresh(this.View.GetCurrentCaretRect());
1120         }
1121
1122         void horizontalScrollBar_Scroll(object sender, ScrollEventArgs e)
1123         {
1124             if (this.horizontalScrollBar == null)
1125                 return;
1126             double toX;
1127             if (this.FlowDirection == System.Windows.FlowDirection.LeftToRight)
1128                 toX = this.horizontalScrollBar.Value;
1129             else
1130                 toX = -this.horizontalScrollBar.Value;
1131             this._Controller.Scroll(toX, this.View.Src.Row, false, false);
1132             this.Refresh();
1133         }
1134
1135         void verticalScrollBar_Scroll(object sender, ScrollEventArgs e)
1136         {
1137             if (this.verticalScrollBar == null)
1138                 return;
1139             int newRow = (int)this.verticalScrollBar.Value;
1140             if (newRow >= this.View.LayoutLines.Count)
1141                 return;
1142             this._Controller.Scroll(this.View.Src.X,newRow, false, false);
1143             this.Refresh();
1144         }
1145
1146         void View_SrcChanged(object sender, EventArgs e)
1147         {
1148             if (this.horizontalScrollBar == null || this.verticalScrollBar == null)
1149                 return;
1150             EditView view = this.View;
1151             if (view.Src.Row > this.verticalScrollBar.Maximum)
1152                 this.verticalScrollBar.Maximum = view.Src.Row + view.LineCountOnScreen + 1;
1153             double absoulteX = Math.Abs(view.Src.X);
1154             if(absoulteX > this.horizontalScrollBar.Maximum)
1155                 this.horizontalScrollBar.Maximum = absoulteX + view.PageBound.Width + 1;
1156             if(view.Src.Row != this.verticalScrollBar.Value)
1157                 this.verticalScrollBar.Value = view.Src.Row;
1158             if (view.Src.X != this.horizontalScrollBar.Value)
1159                 this.horizontalScrollBar.Value = Math.Abs(view.Src.X);
1160         }
1161
1162         void Controller_SelectionChanged(object sender, EventArgs e)
1163         {
1164             this.View.CaretBlink = this.View.CaretBlink;
1165             this.CaretMoved(this, null);
1166             //こうしないと選択できなくなってしまう
1167             this.isNotifyChanged = true;
1168             SetValue(SelectedTextProperty, this._Controller.SelectedText);
1169             SetValue(SelectionProperty, new TextRange(this._Controller.SelectionStart, this._Controller.SelectionLength));
1170             SetValue(CaretPostionProperty, this.Document.CaretPostion);
1171             this.isNotifyChanged = false;
1172             if (this.textStore.IsLocked() == false)
1173                 this.textStore.NotifySelectionChanged();
1174         }
1175
1176         void FooTextBox_Loaded(object sender, RoutedEventArgs e)
1177         {
1178             this.Resize(this.image.ActualWidth, this.image.ActualHeight);
1179             this.Focus();
1180             this.timer.Start();
1181         }
1182
1183         bool Resize(double width, double height)
1184         {
1185             if (width == 0 || height == 0)
1186                 throw new ArgumentOutOfRangeException();
1187             if (this.Render.Resize(width, height))
1188             {
1189                 double scale = this.Render.GetScale();
1190                 // RenderはレタリングはDIPだが、widthとheightの値はDPI依存なのでDIPに変換する
1191                 this.View.PageBound = new Rectangle(0, 0, width / scale, height / scale);
1192
1193                 if (this.horizontalScrollBar != null)
1194                 {
1195                     this.horizontalScrollBar.LargeChange = this.View.PageBound.Width;
1196                     this.horizontalScrollBar.Maximum = this.View.LongestWidth + this.horizontalScrollBar.LargeChange + 1;
1197                 }
1198                 if (this.verticalScrollBar != null)
1199                 {
1200                     this.verticalScrollBar.LargeChange = this.View.LineCountOnScreen;
1201                     this.verticalScrollBar.Maximum = this.View.LayoutLines.Count + this.verticalScrollBar.LargeChange + 1;
1202                 }
1203                 return true;
1204             }
1205             return false;
1206         }
1207
1208         /// <summary>
1209         /// プロパティーが変更されたときに呼ばれます
1210         /// </summary>
1211         /// <param name="e">イベントパラメーター</param>
1212         protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
1213         {
1214             switch (e.Property.Name)
1215             {
1216                 case "Hilighter":
1217                     this.View.Hilighter = this.Hilighter;
1218                     break;
1219                 case "TextAntialiasMode":
1220                     this.Render.TextAntialiasMode = this.TextAntialiasMode;
1221                     break;
1222                 case "FoldingStrategy":
1223                     this.View.LayoutLines.FoldingStrategy = this.FoldingStrategy;
1224                     break;
1225                 case "SelectedText":
1226                     if (!this.isNotifyChanged)
1227                         this._Controller.SelectedText = this.SelectedText;
1228                     break;
1229                 case "IndentMode":
1230                     this._Controller.IndentMode = this.IndentMode;
1231                     break;
1232                 case "Selection":
1233                     if(!this.isNotifyChanged)
1234                         this.Select(this.Selection.Index, this.Selection.Length);
1235                     break;
1236                 case "CaretPostion":
1237                     if (!this.isNotifyChanged)
1238                         this.JumpCaret(this.CaretPostion.row, this.CaretPostion.col);
1239                     break;
1240                 case "LineBreakMethod":
1241                     this.Document.LineBreak = this.LineBreakMethod;
1242                     break;
1243                 case "LineBreakCharCount":
1244                     this.Document.LineBreakCharCount = this.LineBreakCharCount;
1245                     break;
1246                 case "InsertMode":
1247                     this.View.InsertMode = this.InsertMode;
1248                     break;
1249                 case "TabChars":
1250                     this.Document.TabStops = this.TabChars;
1251                     break;
1252                 case "RectSelectMode":
1253                     this._Controller.RectSelection = this.RectSelectMode;
1254                     break;
1255                 case "DrawCaret":
1256                     this.View.HideCaret = !this.DrawCaret;
1257                     break;
1258                 case "DrawCaretLine":
1259                     this.View.HideLineMarker = !this.DrawCaretLine;
1260                     break;
1261                 case "DrawLineNumber":
1262                     this.Document.DrawLineNumber = this.DrawLineNumber;
1263                     break;
1264                 case "FontFamily":
1265                     this.Render.FontFamily = this.FontFamily;
1266                     break;
1267                 case "FontSize":
1268                     this.Render.FontSize = this.FontSize;
1269                     break;
1270                 case "FontStyle":
1271                     this.Render.FontStyle = this.FontStyle;
1272                     break;
1273                 case "FontWeight":
1274                     this.Render.FontWeigth = this.FontWeight;
1275                     break;
1276                 case "Foreground":
1277                     this.Render.Foreground = D2DRender.ToColor4(this.Foreground);
1278                     break;
1279                 case "Background":
1280                     this.Render.Background = D2DRender.ToColor4(this.Background);
1281                     break;
1282                 case "ControlChar":
1283                     this.Render.ControlChar =D2DRender.ToColor4( this.ControlChar);
1284                     break;
1285                 case "Hilight":
1286                     this.Render.Hilight = D2DRender.ToColor4(this.Hilight);
1287                     break;
1288                 case "Keyword1":
1289                     this.Render.Keyword1 = D2DRender.ToColor4(this.Keyword1);
1290                     break;
1291                 case "Keyword2":
1292                     this.Render.Keyword2 = D2DRender.ToColor4(this.Keyword2);
1293                     break;
1294                 case "Comment":
1295                     this.Render.Comment = D2DRender.ToColor4(this.Comment);
1296                     break;
1297                 case "Literal":
1298                     this.Render.Literal = D2DRender.ToColor4(this.Literal);
1299                     break;
1300                 case "URL":
1301                     this.Render.Url = D2DRender.ToColor4(this.URL);
1302                     break;
1303                 case "InsertCaret":
1304                     this.Render.InsertCaret = D2DRender.ToColor4(this.InsertCaret);
1305                     break;
1306                 case "OverwriteCaret":
1307                     this.Render.OverwriteCaret = D2DRender.ToColor4(this.OverwriteCaret);
1308                     break;
1309                 case "Padding":
1310                     this.View.Padding = new Padding((int)this.Padding.Left, (int)this.Padding.Top, (int)this.Padding.Right, (int)this.Padding.Bottom);
1311                     break;
1312                 case "LineMarker":
1313                     this.Render.LineMarker = D2DRender.ToColor4(this.LineMarker);
1314                     break;
1315                 case "MarkURL":
1316                     this.Document.UrlMark = this.MarkURL;
1317                     break;
1318                 case "ShowFullSpace":
1319                     this.Document.ShowFullSpace = this.ShowFullSpace;
1320                     break;
1321                 case "ShowHalfSpace":
1322                     this.Document.ShowHalfSpace = this.ShowHalfSpace;
1323                     break;
1324                 case "ShowTab":
1325                     this.Document.ShowTab = this.ShowTab;
1326                     break;
1327                 case "ShowLineBreak":
1328                     this.Document.ShowLineBreak = this.ShowLineBreak;
1329                     break;
1330                 case "FlowDirection":
1331                     this.Document.RightToLeft = this.FlowDirection == System.Windows.FlowDirection.RightToLeft;
1332                     this.horizontalScrollBar.FlowDirection = this.FlowDirection;
1333                     break;
1334                 case "DrawRuler":
1335                     this.Document.HideRuler = !this.DrawRuler;
1336                     this._Controller.JumpCaret(this.Document.CaretPostion.row, this.Document.CaretPostion.col);
1337                     break;
1338                 case "UpdateArea":
1339                     this.Render.UpdateArea = D2DRender.ToColor4(this.UpdateArea);
1340                     break;
1341                 case "LineNumber":
1342                     this.Render.LineNumber = D2DRender.ToColor4(this.LineNumber);
1343                     break;
1344             }
1345             base.OnPropertyChanged(e);
1346         }
1347         #endregion
1348         #region property
1349
1350         internal Controller Controller
1351         {
1352             get
1353             {
1354                 return this._Controller;
1355             }
1356         }
1357
1358         /// <summary>
1359         /// 文字列の描写に使用されるアンチエイリアシング モードを表します
1360         /// </summary>
1361         public TextAntialiasMode TextAntialiasMode
1362         {
1363             get { return (TextAntialiasMode)GetValue(TextAntialiasModeProperty); }
1364             set { SetValue(TextAntialiasModeProperty, value); }
1365         }
1366
1367         /// <summary>
1368         /// TextAntialiasModeの依存プロパティを表す
1369         /// </summary>
1370         public static readonly DependencyProperty TextAntialiasModeProperty =
1371             DependencyProperty.Register("TextAntialiasMode", typeof(TextAntialiasMode), typeof(FooTextBox), new PropertyMetadata(TextAntialiasMode.Default));
1372
1373         /// <summary>
1374         /// シンタックスハイライターを表す
1375         /// </summary>
1376         public IHilighter Hilighter
1377         {
1378             get { return (IHilighter)GetValue(HilighterProperty); }
1379             set { SetValue(HilighterProperty, value); }
1380         }
1381
1382         /// <summary>
1383         /// Hilighterの依存プロパティを表す
1384         /// </summary>
1385         public static readonly DependencyProperty HilighterProperty =
1386             DependencyProperty.Register("Hilighter", typeof(IHilighter), typeof(FooTextBox), new PropertyMetadata(null));
1387
1388         /// <summary>
1389         /// フォールティングを作成するインターフェイスを表す
1390         /// </summary>
1391         public IFoldingStrategy FoldingStrategy
1392         {
1393             get { return (IFoldingStrategy)GetValue(FoldingStrategyProperty); }
1394             set { SetValue(FoldingStrategyProperty, value); }
1395         }
1396
1397         /// <summary>
1398         /// FoldingStrategyの依存プロパティ
1399         /// </summary>
1400         public static readonly DependencyProperty FoldingStrategyProperty =
1401             DependencyProperty.Register("FoldingStrategy", typeof(IFoldingStrategy), typeof(FooTextBox), new PropertyMetadata(null));
1402
1403
1404         /// <summary>
1405         /// マーカーパターンセット
1406         /// </summary>
1407         public MarkerPatternSet MarkerPatternSet
1408         {
1409             get
1410             {
1411                 return this.Document.MarkerPatternSet;
1412             }
1413         }
1414
1415         /// <summary>
1416         /// ドキュメントを表す
1417         /// </summary>
1418         public Document Document
1419         {
1420             get
1421             {
1422                 return this._Document;
1423             }
1424             set
1425             {
1426                 Document old_doc = this._Document;
1427                 int oldLength = 0;
1428                 if(this._Document != null)
1429                 {
1430                     old_doc.Update -= new DocumentUpdateEventHandler(Document_Update);
1431                     old_doc.SelectionChanged += new EventHandler(Controller_SelectionChanged);
1432                     oldLength = old_doc.Length;
1433                 }
1434
1435                 this._Document = value;
1436                 this._Document.LayoutLines.Render = this.Render;
1437                 this._Document.Update += new DocumentUpdateEventHandler(Document_Update);
1438                 //初期化が終わっていればすべて存在する
1439                 if(this.Controller != null && this.View != null && this.textStore != null)
1440                 {
1441                     this._Document.SelectionChanged += new EventHandler(Controller_SelectionChanged);
1442
1443                     this.Controller.Document = value;
1444                     this.View.Document = value;
1445                     this.Controller.AdjustCaret();
1446                     this.textStore.NotifyTextChanged(oldLength, value.Length);
1447
1448                     //依存プロパティとドキュメント内容が食い違っているので再設定する
1449                     this.ShowFullSpace = value.ShowFullSpace;
1450                     this.ShowHalfSpace = value.ShowHalfSpace;
1451                     this.ShowLineBreak = value.ShowLineBreak;
1452                     this.ShowTab = value.ShowTab;
1453                     this.FlowDirection = value.RightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
1454                     this.IndentMode = value.IndentMode;
1455                     this.DrawCaretLine = !value.HideLineMarker;
1456                     this.InsertMode = value.InsertMode;
1457                     this.DrawRuler = !value.HideRuler;
1458                     this.DrawLineNumber = value.DrawLineNumber;
1459                     this.MarkURL = value.UrlMark;
1460                     this.LineBreakMethod = value.LineBreak;
1461                     this.LineBreakCharCount = value.LineBreakCharCount;
1462                     this.TabChars = value.TabStops;
1463
1464                     this.Refresh();
1465                 }
1466             }
1467         }
1468
1469         /// <summary>
1470         /// レイアウト行を表す
1471         /// </summary>
1472         public LineToIndexTable LayoutLineCollection
1473         {
1474             get { return this.View.LayoutLines; }
1475         }
1476
1477         /// <summary>
1478         /// 選択中の文字列を表す
1479         /// </summary>
1480         public string SelectedText
1481         {
1482             get { return (string)GetValue(SelectedTextProperty); }
1483             set { SetValue(SelectedTextProperty, value); }
1484         }
1485
1486         /// <summary>
1487         /// SelectedTextの依存プロパティを表す
1488         /// </summary>
1489         public static readonly DependencyProperty SelectedTextProperty =
1490             DependencyProperty.Register("SelectedText", typeof(string), typeof(FooTextBox), new PropertyMetadata(null));
1491
1492         /// <summary>
1493         /// インデントの方法を表す
1494         /// </summary>
1495         public IndentMode IndentMode
1496         {
1497             get { return (IndentMode)GetValue(IndentModeProperty); }
1498             set { SetValue(IndentModeProperty, value); }
1499         }
1500
1501         /// <summary>
1502         /// IndentModeの依存プロパティを表す
1503         /// </summary>
1504         public static readonly DependencyProperty IndentModeProperty =
1505             DependencyProperty.Register("IndentMode", typeof(IndentMode), typeof(FooTextBox), new PropertyMetadata(IndentMode.Tab));
1506
1507         /// <summary>
1508         /// 選択範囲を表す
1509         /// </summary>
1510         /// <remarks>
1511         /// Lengthが0の場合はキャレット位置を表します。
1512         /// 矩形選択モードの場合、選択範囲の文字数ではなく、開始位置から終了位置までの長さとなります
1513         /// </remarks>
1514         public TextRange Selection
1515         {
1516             get { return (TextRange)GetValue(SelectionProperty); }
1517             set { SetValue(SelectionProperty, value); }
1518         }
1519
1520         /// <summary>
1521         /// Selectionの依存プロパティを表す
1522         /// </summary>
1523         public static readonly DependencyProperty SelectionProperty =
1524             DependencyProperty.Register("Selection", typeof(TextRange), typeof(FooTextBox), new PropertyMetadata(TextRange.Null));
1525
1526         /// <summary>
1527         /// 拡大率を表す
1528         /// </summary>
1529         public double MagnificationPower
1530         {
1531             get { return (double)GetValue(MagnificationPowerPropertyKey.DependencyProperty); }
1532         }
1533
1534         /// <summary>
1535         /// 拡大率を表す依存プロパティ
1536         /// </summary>
1537         public static readonly DependencyPropertyKey MagnificationPowerPropertyKey =
1538             DependencyProperty.RegisterReadOnly("MagnificationPower", typeof(double), typeof(FooTextBox), new PropertyMetadata(1.0));
1539
1540         /// <summary>
1541         /// レタリング方向を表す
1542         /// </summary>
1543         public new FlowDirection FlowDirection
1544         {
1545             get { return (FlowDirection)GetValue(FlowDirectionProperty); }
1546             set { SetValue(FlowDirectionProperty, value); }
1547         }
1548
1549         /// <summary>
1550         /// レタリング方向を表す。これは依存プロパティです
1551         /// </summary>
1552         public new static readonly DependencyProperty FlowDirectionProperty =
1553             DependencyProperty.Register("FlowDirection", typeof(FlowDirection), typeof(FooTextBox), new PropertyMetadata(FlowDirection.LeftToRight));        
1554
1555         /// <summary>
1556         /// キャレット位置を表す。これは依存プロパティです
1557         /// </summary>
1558         public TextPoint CaretPostion
1559         {
1560             get { return (TextPoint)GetValue(CaretPostionProperty); }
1561             set { SetValue(CaretPostionProperty, value); }
1562         }
1563
1564         /// <summary>
1565         /// CaretPostionの依存プロパティを表す
1566         /// </summary>
1567         public static readonly DependencyProperty CaretPostionProperty =
1568             DependencyProperty.Register("CaretPostion", typeof(TextPoint), typeof(FooTextBox), new PropertyMetadata(TextPoint.Null));
1569         
1570         /// <summary>
1571         /// デフォルトの文字色を表す。これは依存プロパティです
1572         /// </summary>
1573         public new System.Windows.Media.Color Foreground
1574         {
1575             get { return (System.Windows.Media.Color)GetValue(ForegroundProperty); }
1576             set { SetValue(ForegroundProperty, value); }
1577         }
1578
1579         /// <summary>
1580         /// Foregroundの依存プロパティを表す
1581         /// </summary>
1582         public new static readonly DependencyProperty ForegroundProperty =
1583             DependencyProperty.Register("Foreground", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(SystemColors.WindowTextColor));
1584
1585         /// <summary>
1586         /// 背景色を表す。これは依存プロパティです
1587         /// </summary>
1588         public new System.Windows.Media.Color Background
1589         {
1590             get { return (System.Windows.Media.Color)GetValue(BackgroundProperty); }
1591             set { SetValue(BackgroundProperty, value); }
1592         }
1593
1594         /// <summary>
1595         /// Backgroundの依存プロパティを表す
1596         /// </summary>
1597         public new static readonly DependencyProperty BackgroundProperty =
1598             DependencyProperty.Register("Background", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(SystemColors.WindowColor));
1599         
1600         /// <summary>
1601         /// コントロールコードの文字色を表す。これは依存プロパティです
1602         /// </summary>
1603         public System.Windows.Media.Color ControlChar
1604         {
1605             get { return (System.Windows.Media.Color)GetValue(ControlCharProperty); }
1606             set { SetValue(ControlCharProperty, value); }
1607         }
1608
1609         /// <summary>
1610         /// ControlCharの依存プロパティを表す
1611         /// </summary>
1612         public static readonly DependencyProperty ControlCharProperty =
1613             DependencyProperty.Register("ControlChar", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(Colors.Gray));
1614         
1615         /// <summary>
1616         /// 選択時の背景色を表す。これは依存プロパティです
1617         /// </summary>
1618         public System.Windows.Media.Color Hilight
1619         {
1620             get { return (System.Windows.Media.Color)GetValue(HilightProperty); }
1621             set { SetValue(HilightProperty, value); }
1622         }
1623
1624         /// <summary>
1625         /// Hilightの依存プロパティを表す
1626         /// </summary>
1627         public static readonly DependencyProperty HilightProperty =
1628             DependencyProperty.Register("Hilight", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(Colors.DeepSkyBlue));
1629         
1630         /// <summary>
1631         /// キーワード1の文字色を表す。これは依存プロパティです
1632         /// </summary>
1633         public System.Windows.Media.Color Keyword1
1634         {
1635             get { return (System.Windows.Media.Color)GetValue(Keyword1Property); }
1636             set { SetValue(Keyword1Property, value); }
1637         }
1638
1639         /// <summary>
1640         /// Keyword1の依存プロパティを表す
1641         /// </summary>
1642         public static readonly DependencyProperty Keyword1Property =
1643             DependencyProperty.Register("Keyword1", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(Colors.Blue));
1644
1645         /// <summary>
1646         /// キーワード2の文字色を表す。これは依存プロパティです
1647         /// </summary>
1648         public System.Windows.Media.Color Keyword2
1649         {
1650             get { return (System.Windows.Media.Color)GetValue(Keyword2Property); }
1651             set { SetValue(Keyword2Property, value); }
1652         }
1653
1654         /// <summary>
1655         /// Keyword2の依存プロパティを表す
1656         /// </summary>
1657         public static readonly DependencyProperty Keyword2Property =
1658             DependencyProperty.Register("Keyword2", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(Colors.DarkCyan));
1659
1660         /// <summary>
1661         /// コメントの文字色を表す。これは依存プロパティです
1662         /// </summary>
1663         public System.Windows.Media.Color Comment
1664         {
1665             get { return (System.Windows.Media.Color)GetValue(CommentProperty); }
1666             set { SetValue(CommentProperty, value); }
1667         }
1668
1669         /// <summary>
1670         /// Commentの依存プロパティを表す
1671         /// </summary>
1672         public static readonly DependencyProperty CommentProperty =
1673             DependencyProperty.Register("Comment", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(Colors.Green));
1674
1675         /// <summary>
1676         /// 文字リテラルの文字色を表す。これは依存プロパティです
1677         /// </summary>
1678         public System.Windows.Media.Color Literal
1679         {
1680             get { return (System.Windows.Media.Color)GetValue(LiteralProperty); }
1681             set { SetValue(LiteralProperty, value); }
1682         }
1683
1684         /// <summary>
1685         /// Literalの依存プロパティを表す
1686         /// </summary>
1687         public static readonly DependencyProperty LiteralProperty =
1688             DependencyProperty.Register("Literal", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(Colors.Brown));
1689
1690         /// <summary>
1691         /// URLの文字色を表す。これは依存プロパティです
1692         /// </summary>
1693         public System.Windows.Media.Color URL
1694         {
1695             get { return (System.Windows.Media.Color)GetValue(URLProperty); }
1696             set { SetValue(URLProperty, value); }
1697         }
1698
1699         /// <summary>
1700         /// URLの依存プロパティを表す
1701         /// </summary>
1702         public static readonly DependencyProperty URLProperty =
1703             DependencyProperty.Register("URL", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(Colors.Blue));
1704
1705
1706         /// <summary>
1707         /// ラインマーカーの色を表す
1708         /// </summary>
1709         public System.Windows.Media.Color LineMarker
1710         {
1711             get { return (System.Windows.Media.Color)GetValue(LineMarkerProperty); }
1712             set { SetValue(LineMarkerProperty, value); }
1713         }
1714
1715         /// <summary>
1716         /// LineMarkerの依存プロパティを表す
1717         /// </summary>
1718         public static readonly DependencyProperty LineMarkerProperty =
1719             DependencyProperty.Register("LineMarker", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(Colors.Silver));
1720
1721         /// <summary>
1722         /// 挿入モード時のキャレットの色を表す
1723         /// </summary>
1724         public System.Windows.Media.Color InsertCaret
1725         {
1726             get { return (System.Windows.Media.Color)GetValue(InsertCaretProperty); }
1727             set { SetValue(InsertCaretProperty, value); }
1728         }
1729
1730         /// <summary>
1731         /// InsertCaretの依存プロパティを表す
1732         /// </summary>
1733         public static readonly DependencyProperty InsertCaretProperty =
1734             DependencyProperty.Register("InsertCaret", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(SystemColors.WindowTextColor));
1735
1736         /// <summary>
1737         /// 行更新フラグの色を表す
1738         /// </summary>
1739         public System.Windows.Media.Color UpdateArea
1740         {
1741             get { return (System.Windows.Media.Color)GetValue(UpdateAreaProperty); }
1742             set { SetValue(UpdateAreaProperty, value); }
1743         }
1744
1745         /// <summary>
1746         /// UpdateAreaの依存プロパティを表す
1747         /// </summary>
1748         public static readonly DependencyProperty UpdateAreaProperty =
1749             DependencyProperty.Register("UpdateArea", typeof(System.Windows.Media.Color), typeof(FooTextBox), new PropertyMetadata(Colors.MediumSeaGreen));        
1750
1751         /// <summary>
1752         /// 上書きモード時のキャレット職を表す
1753         /// </summary>
1754         public System.Windows.Media.Color OverwriteCaret
1755         {
1756             get { return (System.Windows.Media.Color)GetValue(OverwriteCaretProperty); }
1757             set { SetValue(OverwriteCaretProperty, value); }
1758         }
1759         
1760         /// <summary>
1761         /// OverwriteCaretの依存プロパティを表す
1762         /// </summary>
1763         public static readonly DependencyProperty OverwriteCaretProperty =
1764             DependencyProperty.Register("OverwriteCaret", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(SystemColors.WindowTextColor));
1765
1766         /// <summary>
1767         /// 行番号の色を表す
1768         /// </summary>
1769         public System.Windows.Media.Color LineNumber
1770         {
1771             get { return (System.Windows.Media.Color)GetValue(LineNumberProperty); }
1772             set { SetValue(LineNumberProperty, value); }
1773         }
1774
1775         /// <summary>
1776         /// Using a DependencyProperty as the backing store for LineNumber.  This enables animation, styling, binding, etc...
1777         /// </summary>
1778         public static readonly DependencyProperty LineNumberProperty =
1779             DependencyProperty.Register("LineNumber", typeof(System.Windows.Media.Color), typeof(FooTextBox), new PropertyMetadata(Colors.DimGray));
1780
1781         /// <summary>
1782         /// 挿入モードなら真を返し、そうでないなら、偽を返す。これは依存プロパティです
1783         /// </summary>
1784         public bool InsertMode
1785         {
1786             get { return (bool)GetValue(InsertModeProperty); }
1787             set { SetValue(InsertModeProperty, value); }
1788         }
1789
1790         /// <summary>
1791         /// InsertModeの依存プロパティを表す
1792         /// </summary>
1793         public static readonly DependencyProperty InsertModeProperty =
1794             DependencyProperty.Register("InsertMode",
1795             typeof(bool),
1796             typeof(FooTextBox),
1797             new FrameworkPropertyMetadata(true));
1798
1799         /// <summary>
1800         /// タブの文字数を表す。これは依存プロパティです
1801         /// </summary>
1802         public int TabChars
1803         {
1804             get { return (int)GetValue(TabCharsProperty); }
1805             set { SetValue(TabCharsProperty, value); }
1806         }
1807
1808         /// <summary>
1809         /// TabCharsの依存プロパティを表す
1810         /// </summary>
1811         public static readonly DependencyProperty TabCharsProperty =
1812             DependencyProperty.Register("TabChars",
1813             typeof(int),
1814             typeof(FooTextBox),
1815             new FrameworkPropertyMetadata(4));
1816
1817         /// <summary>
1818         /// 矩形選択モードなら真を返し、そうでないなら偽を返す。これは依存プロパティです
1819         /// </summary>
1820         public bool RectSelectMode
1821         {
1822             get { return (bool)GetValue(RectSelectModeProperty); }
1823             set { SetValue(RectSelectModeProperty, value); }
1824         }
1825
1826         /// <summary>
1827         /// RectSelectModeの依存プロパティを表す
1828         /// </summary>
1829         public static readonly DependencyProperty RectSelectModeProperty =
1830             DependencyProperty.Register("RectSelectMode", typeof(bool), typeof(FooTextBox), new FrameworkPropertyMetadata(false));
1831
1832         /// <summary>
1833         /// 折り返しの方法を指定する
1834         /// </summary>
1835         /// <remarks>
1836         /// 変更した場合、レイアウトの再構築を行う必要があります
1837         /// </remarks>
1838         public LineBreakMethod LineBreakMethod
1839         {
1840             get { return (LineBreakMethod)GetValue(LineBreakProperty); }
1841             set { SetValue(LineBreakProperty, value); }
1842         }
1843
1844         /// <summary>
1845         /// LineBreakMethodの依存プロパティを表す
1846         /// </summary>
1847         public static readonly DependencyProperty LineBreakProperty =
1848             DependencyProperty.Register("LineBreakMethod", typeof(LineBreakMethod), typeof(FooTextBox), new PropertyMetadata(LineBreakMethod.None));
1849
1850
1851         /// <summary>
1852         /// 折り返しの幅を指定する。LineBreakMethod.CharUnit以外の時は無視されます
1853         /// </summary>
1854         /// <remarks>
1855         /// 変更した場合、レイアウトの再構築を行う必要があります
1856         /// </remarks>
1857         public int LineBreakCharCount
1858         {
1859             get { return (int)GetValue(LineBreakCharCountProperty); }
1860             set { SetValue(LineBreakCharCountProperty, value); }
1861         }
1862
1863         /// <summary>
1864         /// LineBreakCharCountの依存プロパティを表す
1865         /// </summary>
1866         public static readonly DependencyProperty LineBreakCharCountProperty =
1867             DependencyProperty.Register("LineBreakCharCount", typeof(int), typeof(FooTextBox), new PropertyMetadata(80));
1868
1869         /// <summary>
1870         /// キャレットを描くなら真。そうでないなら偽を返す。これは依存プロパティです
1871         /// </summary>
1872         public bool DrawCaret
1873         {
1874             get { return (bool)GetValue(DrawCaretProperty); }
1875             set { SetValue(DrawCaretProperty, value); }
1876         }
1877
1878         /// <summary>
1879         /// DrawCaretの依存プロパティを表す
1880         /// </summary>
1881         public static readonly DependencyProperty DrawCaretProperty =
1882             DependencyProperty.Register("DrawCaret", typeof(bool), typeof(FooTextBox), new FrameworkPropertyMetadata(true));
1883
1884         
1885         /// <summary>
1886         /// キャレットラインを描くなら真。そうでないなら偽を返す。これは依存プロパティです
1887         /// </summary>
1888         public bool DrawCaretLine
1889         {
1890             get { return (bool)GetValue(DrawCaretLineProperty); }
1891             set { SetValue(DrawCaretLineProperty, value); }
1892         }
1893
1894         /// <summary>
1895         /// DrawCaretLineの依存プロパティを表す
1896         /// </summary>
1897         public static readonly DependencyProperty DrawCaretLineProperty =
1898             DependencyProperty.Register("DrawCaretLine", typeof(bool), typeof(FooTextBox), new FrameworkPropertyMetadata(false));
1899
1900         /// <summary>
1901         /// 行番号を描くなら真。そうでなければ偽。これは依存プロパティです
1902         /// </summary>
1903         public bool DrawLineNumber
1904         {
1905             get { return (bool)GetValue(DrawLineNumberProperty); }
1906             set { SetValue(DrawLineNumberProperty, value); }
1907         }
1908
1909         /// <summary>
1910         /// ルーラーを描くなら真。そうでなければ偽。これは依存プロパティです
1911         /// </summary>
1912         public bool DrawRuler
1913         {
1914             get { return (bool)GetValue(DrawRulerProperty); }
1915             set { SetValue(DrawRulerProperty, value); }
1916         }
1917
1918         /// <summary>
1919         /// DrawRulerの依存プロパティを表す
1920         /// </summary>
1921         public static readonly DependencyProperty DrawRulerProperty =
1922             DependencyProperty.Register("DrawRuler", typeof(bool), typeof(FooTextBox), new PropertyMetadata(false));
1923
1924         
1925         /// <summary>
1926         /// DrawLineNumberの依存プロパティを表す
1927         /// </summary>
1928         public static readonly DependencyProperty DrawLineNumberProperty =
1929             DependencyProperty.Register("DrawLineNumber", typeof(bool), typeof(FooTextBox), new FrameworkPropertyMetadata(false));
1930
1931         /// <summary>
1932         /// URLに下線を引くなら真。そうでないなら偽を表す。これは依存プロパティです
1933         /// </summary>
1934         public bool MarkURL
1935         {
1936             get { return (bool)GetValue(MarkURLProperty); }
1937             set { SetValue(MarkURLProperty, value); }
1938         }
1939
1940         /// <summary>
1941         /// MarkURLの依存プロパティを表す
1942         /// </summary>
1943         public static readonly DependencyProperty MarkURLProperty =
1944             DependencyProperty.Register("MarkURL", typeof(bool), typeof(FooTextBox), new FrameworkPropertyMetadata(false));
1945
1946         /// <summary>
1947         /// 全角スペースを表示するなら真。そうでないなら偽
1948         /// </summary>
1949         public bool ShowFullSpace
1950         {
1951             get { return (bool)GetValue(ShowFullSpaceProperty); }
1952             set { SetValue(ShowFullSpaceProperty, value); }
1953         }
1954
1955         /// <summary>
1956         /// ShowFullSpaceの依存プロパティを表す
1957         /// </summary>
1958         public static readonly DependencyProperty ShowFullSpaceProperty =
1959             DependencyProperty.Register("ShowFullSpace", typeof(bool), typeof(FooTextBox), new UIPropertyMetadata(false));
1960
1961         /// <summary>
1962         /// 半角スペースを表示するなら真。そうでないなら偽
1963         /// </summary>
1964         public bool ShowHalfSpace
1965         {
1966             get { return (bool)GetValue(ShowHalfSpaceProperty); }
1967             set { SetValue(ShowHalfSpaceProperty, value); }
1968         }
1969
1970         /// <summary>
1971         /// ShowHalfSpaceの依存プロパティを表す
1972         /// </summary>
1973         public static readonly DependencyProperty ShowHalfSpaceProperty =
1974             DependencyProperty.Register("ShowHalfSpace", typeof(bool), typeof(FooTextBox), new UIPropertyMetadata(false));
1975
1976         /// <summary>
1977         /// タブを表示するなら真。そうでないなら偽
1978         /// </summary>
1979         public bool ShowTab
1980         {
1981             get { return (bool)GetValue(ShowTabProperty); }
1982             set { SetValue(ShowTabProperty, value); }
1983         }
1984
1985         /// <summary>
1986         /// ShowTabの依存プロパティを表す
1987         /// </summary>
1988         public static readonly DependencyProperty ShowTabProperty =
1989             DependencyProperty.Register("ShowTab", typeof(bool), typeof(FooTextBox), new UIPropertyMetadata(false));
1990
1991         /// <summary>
1992         /// 改行マークを表示するなら真。そうでないなら偽
1993         /// </summary>
1994         public bool ShowLineBreak
1995         {
1996             get { return (bool)GetValue(ShowLineBreakProperty); }
1997             set { SetValue(ShowLineBreakProperty, value); }
1998         }
1999
2000         /// <summary>
2001         /// ShowLineBreakの依存プロパティを表す
2002         /// </summary>
2003         public static readonly DependencyProperty ShowLineBreakProperty =
2004             DependencyProperty.Register("ShowLineBreak", typeof(bool), typeof(FooTextBox), new PropertyMetadata(false));
2005         
2006         #endregion
2007     }
2008     /// <summary>
2009     /// マウスボタン関連のイベントクラス
2010     /// </summary>
2011     public sealed class FooMouseButtonEventArgs : MouseButtonEventArgs
2012     {
2013         /// <summary>
2014         /// イベントが発生したドキュメントのインデックス
2015         /// </summary>
2016         public int Index
2017         {
2018             get;
2019             private set;
2020         }
2021
2022         /// <summary>
2023         /// コンストラクター
2024         /// </summary>
2025         /// <param name="mouse">マウスデバイス</param>
2026         /// <param name="timestamp">タイムスタンプ</param>
2027         /// <param name="button">ボタン</param>
2028         /// <param name="stylusDevice">スタイラスデバイス</param>
2029         /// <param name="index">インデックス</param>
2030         public FooMouseButtonEventArgs(MouseDevice mouse, int timestamp, MouseButton button, StylusDevice stylusDevice, int index)
2031             : base(mouse, timestamp, button, stylusDevice)
2032         {
2033             this.Index = index;
2034         }
2035     }
2036     /// <summary>
2037     /// マウス関連のイベントクラス
2038     /// </summary>
2039     public sealed class FooMouseEventArgs : MouseEventArgs
2040     {
2041         /// <summary>
2042         /// イベントが発生したドキュメントのインデックス
2043         /// </summary>
2044         public int Index
2045         {
2046             get;
2047             private set;
2048         }
2049
2050         /// <summary>
2051         /// コンストラクター
2052         /// </summary>
2053         /// <param name="mouse">マウスデバイス</param>
2054         /// <param name="timestamp">タイムスタンプ</param>
2055         /// <param name="stylusDevice">スタイラスデバイス</param>
2056         /// <param name="index">インデックス</param>
2057         public FooMouseEventArgs(MouseDevice mouse,
2058             int timestamp,
2059             StylusDevice stylusDevice,
2060             int index)
2061             : base(mouse, timestamp, stylusDevice)
2062         {
2063             this.Index = index;
2064         }
2065     }
2066 }