OSDN Git Service

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