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