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