OSDN Git Service

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