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