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