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