OSDN Git Service

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