OSDN Git Service

互換性のないメソッドをいつか廃止した
[fooeditengine/FooEditEngine.git] / WPF / FooEditEngine / FooTextBox.cs
index c03183e..feec422 100644 (file)
@@ -35,7 +35,7 @@ namespace FooEditEngine.WPF
         const double MaxFontSize = 72.0f;
         const double MinFontSize = 1;
 
-        EditView View;
+        EditView _View;
         Controller _Controller;
         D2DRender Render;
         Image image;
@@ -47,7 +47,10 @@ namespace FooEditEngine.WPF
         bool isNotifyChanged = false;
         Document _Document;
         Popup popup;
-        
+
+        const int Interval = 96;
+        const int IntervalWhenLostFocuse = 160;
+
         static FooTextBox()
         {
             DefaultStyleKeyProperty.OverrideMetadata(typeof(FooTextBox), new FrameworkPropertyMetadata(typeof(FooTextBox)));
@@ -83,23 +86,23 @@ namespace FooEditEngine.WPF
             this.textStore.CompositionEnded += textStore_CompositionEnded;
 
             this.Render = new D2DRender(this, 200, 200,this.image);
-            this.Render.ShowFullSpace = this.ShowFullSpace;
-            this.Render.ShowHalfSpace = this.ShowHalfSpace;
-            this.Render.ShowTab = this.ShowTab;
 
             this.Document = new Document();
 
-            this.View = new EditView(this.Document, this.Render, new Padding(5, 5, 5, 5));
-            this.View.SrcChanged += View_SrcChanged;
-            this.View.InsertMode = this.InsertMode;
+            this._View = new EditView(this.Document, this.Render, new Padding(5, 5, 5, 5));
+            this._View.SrcChanged += View_SrcChanged;
+            this._View.InsertMode = this.InsertMode;
             this.Document.DrawLineNumber = this.DrawLineNumber;
-            this.View.HideCaret = !this.DrawCaret;
-            this.View.HideLineMarker = !this.DrawCaretLine;
+            this._View.HideCaret = !this.DrawCaret;
+            this._View.HideLineMarker = !this.DrawCaretLine;
             this.Document.HideRuler = !this.DrawRuler;
             this.Document.UrlMark = this.MarkURL;
             this.Document.TabStops = this.TabChars;
+            this.Document.ShowFullSpace = this.ShowFullSpace;
+            this.Document.ShowHalfSpace = this.ShowHalfSpace;
+            this.Document.ShowTab = this.ShowTab;
 
-            this._Controller = new Controller(this.Document, this.View);
+            this._Controller = new Controller(this.Document, this._View);
             this._Document.SelectionChanged += new EventHandler(Controller_SelectionChanged);
 
             this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, CopyCommand, CanExecute));
@@ -125,13 +128,11 @@ namespace FooEditEngine.WPF
             this.InputBindings.Add(new InputBinding(FooTextBoxCommands.ToggleCodePoint, new KeyGesture(Key.X, ModifierKeys.Alt)));
 
             this.timer = new DispatcherTimer();
-            this.timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
+            this.timer.Interval = new TimeSpan(0, 0, 0, 0, Interval);
             this.timer.Tick += new EventHandler(timer_Tick);
 
             this.Loaded += new RoutedEventHandler(FooTextBox_Loaded);
 
-            this.AutoIndentHooker = (s,e)=>{};
-
             SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
 
             this.SystemEvents_UserPreferenceChanged(null, new UserPreferenceChangedEventArgs(UserPreferenceCategory.Keyboard));
@@ -151,12 +152,6 @@ namespace FooEditEngine.WPF
         }
 
         /// <summary>
-        /// オートインデントを行うためのイベント
-        /// </summary>
-        [Obsolete]
-        public AutoIndentHookerHandler AutoIndentHooker;
-
-        /// <summary>
         /// テンプレートを適用します
         /// </summary>
         public override void OnApplyTemplate()
@@ -190,7 +185,7 @@ namespace FooEditEngine.WPF
             {
                 this.verticalScrollBar.SmallChange = 1;
                 this.verticalScrollBar.LargeChange = 10;
-                this.verticalScrollBar.Maximum = this.View.LayoutLines.Count;
+                this.verticalScrollBar.Maximum = this.Document.LayoutLines.Count - 1;
                 this.verticalScrollBar.Scroll += new ScrollEventHandler(verticalScrollBar_Scroll);
             }
         }
@@ -279,7 +274,7 @@ namespace FooEditEngine.WPF
         {
             if (this.Document.FireUpdateEvent == false)
                 throw new InvalidOperationException("");
-            return this.View.GetPostionFromTextPoint(tp);
+            return this.image.TranslatePoint(this._View.GetPostionFromTextPoint(tp),this);
         }
 
         /// <summary>
@@ -291,7 +286,8 @@ namespace FooEditEngine.WPF
         {
             if (this.Document.FireUpdateEvent == false)
                 throw new InvalidOperationException("");
-            return this.View.GetTextPointFromPostion(p);
+            System.Windows.Point relP = this.TranslatePoint(p, this.image);
+            return this._View.GetTextPointFromPostion(p);
         }
 
         /// <summary>
@@ -303,7 +299,7 @@ namespace FooEditEngine.WPF
         {
             if (this.Document.FireUpdateEvent == false)
                 throw new InvalidOperationException("");
-            return this.View.LayoutLines.GetLayout(row).Height;;
+            return this._View.LayoutLines.GetLayout(row).Height;;
         }
 
         /// <summary>
@@ -315,8 +311,8 @@ namespace FooEditEngine.WPF
         {
             if (this.Document.FireUpdateEvent == false)
                 throw new InvalidOperationException("");
-            TextPoint tp = this.View.GetLayoutLineFromIndex(index);
-            return this.View.GetPostionFromTextPoint(tp);
+            TextPoint tp = this._View.GetLayoutLineFromIndex(index);
+            return this._View.GetPostionFromTextPoint(tp);
         }
 
         /// <summary>
@@ -328,8 +324,8 @@ namespace FooEditEngine.WPF
         {
             if (this.Document.FireUpdateEvent == false)
                 throw new InvalidOperationException("");
-            TextPoint tp = this.View.GetTextPointFromPostion(p);
-            return this.View.GetIndexFromLayoutLine(tp);
+            TextPoint tp = this._View.GetTextPointFromPostion(p);
+            return this._View.GetIndexFromLayoutLine(tp);
         }
 
         /// <summary>
@@ -337,7 +333,7 @@ namespace FooEditEngine.WPF
         /// </summary>
         public void Refresh()
         {
-            this.Refresh(this.View.PageBound);
+            this.Refresh(this._View.PageBound);
         }
 
         /// <summary>
@@ -345,7 +341,7 @@ namespace FooEditEngine.WPF
         /// </summary>
         public void PerfomLayouts()
         {
-            this.View.PerfomLayouts();
+            this.Document.PerformLayout();
         }
 
         /// <summary>
@@ -355,7 +351,7 @@ namespace FooEditEngine.WPF
         /// <param name="alignTop">指定行を画面上に置くなら真。そうでないなら偽</param>
         public void ScrollIntoView(int row, bool alignTop)
         {
-            this.View.ScrollIntoView(row, alignTop);
+            this._View.ScrollIntoView(row, alignTop);
         }
 
         /// <summary>
@@ -393,9 +389,11 @@ namespace FooEditEngine.WPF
             {
                 TextStoreHelper.NotifyTextChanged(this.textStore, 0, 0, this.Document.Length);
                 if (this.verticalScrollBar != null)
-                    this.verticalScrollBar.Maximum = this.View.LayoutLines.Count;
-                this.View.CalculateLineCountOnScreen();
+                    this.verticalScrollBar.Maximum = this._View.LayoutLines.Count;
+                this._View.CalculateWhloeViewPort();
+                this._View.CalculateLineCountOnScreen();
                 this.IsEnabled = true;
+                this.Refresh(this._View.PageBound);
             }
         }
 
@@ -437,7 +435,7 @@ namespace FooEditEngine.WPF
             {
                 this.textStore.Dispose();
                 this.timer.Stop();
-                this.View.Dispose();
+                this._View.Dispose();
                 this.Render.Dispose();
             }
             SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
@@ -448,7 +446,7 @@ namespace FooEditEngine.WPF
             if (this.disposed || this.Visibility == Visibility.Collapsed)
                 return;
 
-            this.Render.DrawContent(this.View, this.IsEnabled, updateRect);
+            this.Render.DrawContent(this._View, this.IsEnabled, updateRect);
             this.Document.IsRequestRedraw = false;
         }
 
@@ -559,7 +557,7 @@ namespace FooEditEngine.WPF
 
         void textStore_CompositionUpdated(int start, int end)
         {
-            if (TextStoreHelper.ScrollToCompstionUpdated(this.textStore, this.View, start, end))
+            if (TextStoreHelper.ScrollToCompstionUpdated(this.textStore, this._View, start, end))
                 this.Refresh();
         }
         bool textStore_CompositionStarted()
@@ -592,7 +590,7 @@ namespace FooEditEngine.WPF
         )
         {
             Point startPos, endPos;
-            TextStoreHelper.GetStringExtent(this.Document, this.View, i_startIndex, i_endIndex, out startPos, out endPos);
+            TextStoreHelper.GetStringExtent(this.Document, this._View, i_startIndex, i_endIndex, out startPos, out endPos);
 
             double scale = this.Render.GetScale();
             
@@ -618,7 +616,7 @@ namespace FooEditEngine.WPF
         void _textStore_GetSelectionIndex(int start_index, int max_count, out DotNetTextStore.TextSelection[] sels)
         {
             TextRange selRange;
-            TextStoreHelper.GetSelection(this._Controller, this.View.Selections, out selRange);
+            TextStoreHelper.GetSelection(this._Controller, this._View.Selections, out selRange);
 
             sels = new DotNetTextStore.TextSelection[1];
             sels[0] = new DotNetTextStore.TextSelection();
@@ -628,7 +626,7 @@ namespace FooEditEngine.WPF
 
         void _textStore_SetSelectionIndex(DotNetTextStore.TextSelection[] sels)
         {
-            TextStoreHelper.SetSelectionIndex(this._Controller, this.View, sels[0].start, sels[0].end);
+            TextStoreHelper.SetSelectionIndex(this._Controller, this._View, sels[0].start, sels[0].end);
             this.Refresh();
         }
 
@@ -646,7 +644,8 @@ namespace FooEditEngine.WPF
         {
             base.OnGotKeyboardFocus(e);
             this.textStore.SetFocus();
-            this.View.IsFocused = true;
+            this._View.IsFocused = true;
+            this.timer.Interval = new TimeSpan(0,0,0,0,Interval);
             this.Refresh();
         }
 
@@ -657,7 +656,8 @@ namespace FooEditEngine.WPF
         protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
         {
             base.OnLostKeyboardFocus(e);
-            this.View.IsFocused = false;
+            this._View.IsFocused = false;
+            this.timer.Interval = new TimeSpan(0, 0, 0, 0, IntervalWhenLostFocuse);
             this.Refresh();
         }
         #endregion
@@ -681,7 +681,6 @@ namespace FooEditEngine.WPF
             if (e.Text == "\r")
             {
                 this._Controller.DoEnterAction();
-                this.AutoIndentHooker(this, null);
             }
             else if (e.Text == "\b")
             {
@@ -752,12 +751,12 @@ namespace FooEditEngine.WPF
                     movedCaret = true;
                     break;
                 case Key.PageUp:
-                    this._Controller.Scroll(ScrollDirection.Up,this.View.LineCountOnScreen, this.IsPressedModifierKey(modiferKeys, ModifierKeys.Shift),true);
+                    this._Controller.Scroll(ScrollDirection.Up,this._View.LineCountOnScreen, this.IsPressedModifierKey(modiferKeys, ModifierKeys.Shift),true);
                     this.Refresh();
                     movedCaret = true;
                     break;
                 case Key.PageDown:
-                    this._Controller.Scroll(ScrollDirection.Down,this.View.LineCountOnScreen, this.IsPressedModifierKey(modiferKeys, ModifierKeys.Shift),true);
+                    this._Controller.Scroll(ScrollDirection.Down,this._View.LineCountOnScreen, this.IsPressedModifierKey(modiferKeys, ModifierKeys.Shift),true);
                     this.Refresh();
                     movedCaret = true;
                     break;
@@ -814,10 +813,10 @@ namespace FooEditEngine.WPF
         protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
         {
             var p = this.GetDipFromPoint(e.GetPosition(this));
-            TextPoint tp = this.View.GetTextPointFromPostion(p);
+            TextPoint tp = this._View.GetTextPointFromPostion(p);
             if (tp == TextPoint.Null)
                 return;
-            int index = this.View.LayoutLines.GetIndexFromTextPoint(tp);
+            int index = this._View.LayoutLines.GetIndexFromTextPoint(tp);
 
             FooMouseButtonEventArgs newEventArgs = new FooMouseButtonEventArgs(e.MouseDevice,
                 e.Timestamp,
@@ -832,8 +831,11 @@ namespace FooEditEngine.WPF
 
             if (e.LeftButton == MouseButtonState.Pressed)
             {
+                if (p.X < this.Render.TextArea.X)
+                    this.Document.SelectLine((int)index);
+                else
+                    this.Document.SelectWord((int)index);
 
-                this.Document.SelectWord((int)index);
                 this.textStore.NotifySelectionChanged();
                 if(this.peer != null)
                     this.peer.OnNotifyCaretChanged();
@@ -854,10 +856,10 @@ namespace FooEditEngine.WPF
             this.CaptureMouse();
 
             var p = this.GetDipFromPoint(e.GetPosition(this));
-            TextPoint tp = this.View.GetTextPointFromPostion(p);
+            TextPoint tp = this._View.GetTextPointFromPostion(p);
             if (tp == TextPoint.Null)
                 return;
-            int index = this.View.LayoutLines.GetIndexFromTextPoint(tp);
+            int index = this._View.LayoutLines.GetIndexFromTextPoint(tp);
 
             FooMouseButtonEventArgs newEventArgs = new FooMouseButtonEventArgs(e.MouseDevice,
                 e.Timestamp,
@@ -872,13 +874,13 @@ namespace FooEditEngine.WPF
 
             if (e.LeftButton == MouseButtonState.Pressed)
             {
-                FoldingItem foldingData = this.View.HitFoldingData(p.X,tp.row);
+                FoldingItem foldingData = this._View.HitFoldingData(p.X,tp.row);
                 if (foldingData != null)
                 {
                     if (foldingData.Expand)
-                        this.View.LayoutLines.FoldingCollection.Collapse(foldingData);
+                        this._View.LayoutLines.FoldingCollection.Collapse(foldingData);
                     else
-                        this.View.LayoutLines.FoldingCollection.Expand(foldingData);
+                        this._View.LayoutLines.FoldingCollection.Expand(foldingData);
                     this._Controller.JumpCaret(foldingData.Start,false);
                 }
                 else
@@ -887,7 +889,7 @@ namespace FooEditEngine.WPF
                 }
                 if (this.peer != null)
                     this.peer.OnNotifyCaretChanged();
-                this.View.IsFocused = true;
+                this._View.IsFocused = true;
                 this.Focus();
                 this.Document.SelectGrippers.BottomLeft.Enabled = false;
                 this.Document.SelectGrippers.BottomRight.Enabled = false;
@@ -920,22 +922,31 @@ namespace FooEditEngine.WPF
             var p = this.GetDipFromPoint(e.GetPosition(this));
 
             TextPointSearchRange searchRange;
-            if (this.View.HitTextArea(p.X, p.Y))
+            if (this._View.HitTextArea(p.X, p.Y))
+            {
                 searchRange = TextPointSearchRange.TextAreaOnly;
+            }
             else if (leftPressed)
+            {
                 searchRange = TextPointSearchRange.Full;
+            }
             else
-                searchRange = TextPointSearchRange.TextAreaOnly;
+            {
+                this.Cursor = Cursors.Arrow;
+                base.OnMouseMove(e);
+                return;
+            }
 
-            TextPoint tp = this.View.GetTextPointFromPostion(p, searchRange);
+            TextPoint tp = this._View.GetTextPointFromPostion(p, searchRange);
 
             if (tp == TextPoint.Null)
             {
+                this.Cursor = Cursors.Arrow;
                 base.OnMouseMove(e);
                 return;
             }
 
-            int index = this.View.GetIndexFromLayoutLine(tp);
+            int index = this._View.GetIndexFromLayoutLine(tp);
 
             FooMouseEventArgs newEventArgs = new FooMouseEventArgs(e.MouseDevice, e.Timestamp, e.StylusDevice, index);
             newEventArgs.RoutedEvent = e.RoutedEvent;
@@ -957,9 +968,11 @@ namespace FooEditEngine.WPF
                 this.Cursor = Cursors.Arrow;
             }
 
-            if (leftPressed)
+            //スクロールバーを押した場合はキャレットを移動させる必要がない
+            if (leftPressed && e.OriginalSource.GetType() == typeof(FooTextBox))
             {
-                this._Controller.MoveCaretAndSelect(tp);
+                bool controlPressed = (Keyboard.GetKeyStates(Key.LeftCtrl) & KeyStates.Down) == KeyStates.Down;
+                this._Controller.MoveCaretAndSelect(tp, controlPressed);
                 if (this.peer != null)
                     this.peer.OnNotifyCaretChanged();
                 this.Refresh();
@@ -973,7 +986,7 @@ namespace FooEditEngine.WPF
         protected override void OnTouchDown(TouchEventArgs e)
         {
             var p = this.GetDipFromPoint(e.GetTouchPoint(this).Position);
-            this.hittedGripper = this.View.HitGripperFromPoint(p);
+            this.hittedGripper = this._View.HitGripperFromPoint(p);
             this.CaptureTouch(e.TouchDevice);
         }
 
@@ -989,18 +1002,18 @@ namespace FooEditEngine.WPF
             }
 
             var p = this.GetDipFromPoint(e.GetTouchPoint(this).Position);
-            TextPoint tp = this.View.GetTextPointFromPostion(p);
+            TextPoint tp = this._View.GetTextPointFromPostion(p);
             if (tp == TextPoint.Null)
                 return;
-            int index = this.View.LayoutLines.GetIndexFromTextPoint(tp);
+            int index = this._View.LayoutLines.GetIndexFromTextPoint(tp);
 
-            FoldingItem foldingData = this.View.HitFoldingData(p.X, tp.row);
+            FoldingItem foldingData = this._View.HitFoldingData(p.X, tp.row);
             if (foldingData != null)
             {
                 if (foldingData.Expand)
-                    this.View.LayoutLines.FoldingCollection.Collapse(foldingData);
+                    this._View.LayoutLines.FoldingCollection.Collapse(foldingData);
                 else
-                    this.View.LayoutLines.FoldingCollection.Expand(foldingData);
+                    this._View.LayoutLines.FoldingCollection.Expand(foldingData);
                 this._Controller.JumpCaret(foldingData.Start, false);
             }
             else
@@ -1009,7 +1022,7 @@ namespace FooEditEngine.WPF
             }
             if (this.peer != null)
                 this.peer.OnNotifyCaretChanged();
-            this.View.IsFocused = true;
+            this._View.IsFocused = true;
             this.Focus();
             this.Document.SelectGrippers.BottomLeft.Enabled = false;
             this.Document.SelectGrippers.BottomRight.Enabled = true;
@@ -1107,12 +1120,12 @@ namespace FooEditEngine.WPF
             if (e.Category == UserPreferenceCategory.Keyboard)
             {
                 int blinkTime = (int)NativeMethods.GetCaretBlinkTime();
-                this.View.CaretBlink = blinkTime >= 0;
-                this.View.CaretBlinkTime = blinkTime * 2;
+                this._View.CaretBlink = blinkTime >= 0;
+                this._View.CaretBlinkTime = blinkTime * 2;
             }
             if (e.Category == UserPreferenceCategory.General)
             {
-                this.View.CaretWidthOnInsertMode = SystemParameters.CaretWidth;
+                this._View.CaretWidthOnInsertMode = SystemParameters.CaretWidth;
             }
         }
 
@@ -1132,16 +1145,16 @@ namespace FooEditEngine.WPF
                 return;
             if (this.Resize(this.image.ActualWidth, this.image.ActualHeight))
             {
-                this.Refresh();
+                this.Refresh(this._View.PageBound);
                 return;
             }
 
-            bool updateAll = this.View.LayoutLines.HilightAll() || this.View.LayoutLines.GenerateFolding() || this.Document.IsRequestRedraw;
+            bool updateAll = this._View.LayoutLines.HilightAll() || this._View.LayoutLines.GenerateFolding() || this.Document.IsRequestRedraw;
 
             if (updateAll)
-                this.Refresh();
+                this.Refresh(this._View.PageBound);
             else
-                this.Refresh(this.View.GetCurrentCaretRect());
+                this.Refresh(this._View.GetCurrentCaretRect());
         }
 
         void horizontalScrollBar_Scroll(object sender, ScrollEventArgs e)
@@ -1153,7 +1166,7 @@ namespace FooEditEngine.WPF
                 toX = this.horizontalScrollBar.Value;
             else
                 toX = -this.horizontalScrollBar.Value;
-            this._Controller.Scroll(toX, this.View.Src.Row, false, false);
+            this.Controller.ScrollByPixel(ScrollDirection.Left, (int)toX, false, false);
             this.Refresh();
         }
 
@@ -1161,10 +1174,7 @@ namespace FooEditEngine.WPF
         {
             if (this.verticalScrollBar == null)
                 return;
-            int newRow = (int)this.verticalScrollBar.Value;
-            if (newRow >= this.View.LayoutLines.Count)
-                return;
-            this._Controller.Scroll(this.View.Src.X,newRow, false, false);
+            this.Controller.Scroll(this.Document.Src.X, (int)this.verticalScrollBar.Value, false, false);
             this.Refresh();
         }
 
@@ -1172,9 +1182,9 @@ namespace FooEditEngine.WPF
         {
             if (this.horizontalScrollBar == null || this.verticalScrollBar == null)
                 return;
-            EditView view = this.View;
-            if (view.Src.Row > this.verticalScrollBar.Maximum)
-                this.verticalScrollBar.Maximum = view.Src.Row + view.LineCountOnScreen + 1;
+            EditView view = this._View;
+            if (view.Src.Row > this.Document.LayoutLines.Count)
+                this.verticalScrollBar.Maximum = this.Document.LayoutLines.Count - 1;
             double absoulteX = Math.Abs(view.Src.X);
             if(absoulteX > this.horizontalScrollBar.Maximum)
                 this.horizontalScrollBar.Maximum = absoulteX + view.PageBound.Width + 1;
@@ -1186,7 +1196,7 @@ namespace FooEditEngine.WPF
 
         void Controller_SelectionChanged(object sender, EventArgs e)
         {
-            this.View.CaretBlink = this.View.CaretBlink;
+            this._View.CaretBlink = this._View.CaretBlink;
             this.CaretMoved(this, null);
             //こうしないと選択できなくなってしまう
             this.isNotifyChanged = true;
@@ -1213,17 +1223,17 @@ namespace FooEditEngine.WPF
             {
                 double scale = this.Render.GetScale();
                 // RenderはレタリングはDIPだが、widthとheightの値はDPI依存なのでDIPに変換する
-                this.View.PageBound = new Rectangle(0, 0, width / scale, height / scale);
+                this._View.PageBound = new Rectangle(0, 0, width / scale, height / scale);
 
                 if (this.horizontalScrollBar != null)
                 {
-                    this.horizontalScrollBar.LargeChange = this.View.PageBound.Width;
-                    this.horizontalScrollBar.Maximum = this.View.LongestWidth + this.horizontalScrollBar.LargeChange + 1;
+                    this.horizontalScrollBar.LargeChange = this._View.PageBound.Width;
+                    this.horizontalScrollBar.Maximum = this._View.LongestWidth + this.horizontalScrollBar.LargeChange + 1;
                 }
                 if (this.verticalScrollBar != null)
                 {
-                    this.verticalScrollBar.LargeChange = this.View.LineCountOnScreen;
-                    this.verticalScrollBar.Maximum = this.View.LayoutLines.Count + this.verticalScrollBar.LargeChange + 1;
+                    this.verticalScrollBar.LargeChange = this._View.LineCountOnScreen;
+                    this.verticalScrollBar.Maximum = this._View.LayoutLines.Count + this.verticalScrollBar.LargeChange + 1;
                 }
                 return true;
             }
@@ -1241,7 +1251,8 @@ namespace FooEditEngine.WPF
             {
                 old_doc.Update -= new DocumentUpdateEventHandler(Document_Update);
                 old_doc.LoadProgress -= Document_LoadProgress;
-                old_doc.SelectionChanged += new EventHandler(Controller_SelectionChanged);
+                old_doc.SelectionChanged -= new EventHandler(Controller_SelectionChanged);
+                old_doc.AutoCompleteChanged -= _Document_AutoCompleteChanged;
                 oldLength = old_doc.Length;
                 if (this._Document.AutoComplete != null)
                 {
@@ -1255,24 +1266,16 @@ namespace FooEditEngine.WPF
             this._Document.LayoutLines.Render = this.Render;
             this._Document.Update += new DocumentUpdateEventHandler(Document_Update);
             this._Document.LoadProgress += Document_LoadProgress;
-            if (this._Document.AutoComplete != null)
-            {
-                ((AutoCompleteBox)this._Document.AutoComplete).TargetPopup = this.popup;
-                this._Document.AutoComplete.GetPostion = (tp, doc) =>
-                {
-                    var p = this.View.GetPostionFromTextPoint(tp);
-                    int height = (int)doc.LayoutLines.GetLayout(doc.CaretPostion.row).Height;
-                    p.Y += height;
-                    return PointToScreen(this.TranslatePoint(p.Scale(Util.GetScale()), this));
-                };
-            }
+            this._Document.AutoCompleteChanged += _Document_AutoCompleteChanged;
+            if (this._Document.AutoComplete != null && this.Document.AutoComplete.GetPostion == null)
+                this._Document_AutoCompleteChanged(this.Document, null);
             //初期化が終わっていればすべて存在する
-            if (this.Controller != null && this.View != null && this.textStore != null)
+            if (this.Controller != null && this._View != null && this.textStore != null)
             {
                 this._Document.SelectionChanged += new EventHandler(Controller_SelectionChanged);
 
                 this.Controller.Document = value;
-                this.View.Document = value;
+                this._View.Document = value;
                 this.Controller.AdjustCaret();
                 this.textStore.NotifyTextChanged(oldLength, value.Length);
 
@@ -1295,6 +1298,20 @@ namespace FooEditEngine.WPF
                 this.Refresh();
             }
         }
+
+        private void _Document_AutoCompleteChanged(object sender, EventArgs e)
+        {
+            Document doc = (Document)sender;
+            ((AutoCompleteBox)this._Document.AutoComplete).TargetPopup = this.popup;
+            this._Document.AutoComplete.GetPostion = (tp, edoc) =>
+            {
+                var p = this._View.GetPostionFromTextPoint(tp);
+                int height = (int)this.Render.emSize.Height;
+                p.Y += height;
+                return PointToScreen(this.TranslatePoint(p.Scale(Util.GetScale()), this));
+            };
+        }
+
         /// <summary>
         /// プロパティーが変更されたときに呼ばれます
         /// </summary>
@@ -1307,13 +1324,13 @@ namespace FooEditEngine.WPF
                     this.SetDocument(this.Document);
                     break;
                 case "Hilighter":
-                    this.View.Hilighter = this.Hilighter;
+                    this._View.Hilighter = this.Hilighter;
                     break;
                 case "TextAntialiasMode":
                     this.Render.TextAntialiasMode = this.TextAntialiasMode;
                     break;
                 case "FoldingStrategy":
-                    this.View.LayoutLines.FoldingStrategy = this.FoldingStrategy;
+                    this._View.LayoutLines.FoldingStrategy = this.FoldingStrategy;
                     break;
                 case "SelectedText":
                     if (!this.isNotifyChanged)
@@ -1337,7 +1354,7 @@ namespace FooEditEngine.WPF
                     this.Document.LineBreakCharCount = this.LineBreakCharCount;
                     break;
                 case "InsertMode":
-                    this.View.InsertMode = this.InsertMode;
+                    this._View.InsertMode = this.InsertMode;
                     break;
                 case "TabChars":
                     this.Document.TabStops = this.TabChars;
@@ -1346,10 +1363,10 @@ namespace FooEditEngine.WPF
                     this._Controller.RectSelection = this.RectSelectMode;
                     break;
                 case "DrawCaret":
-                    this.View.HideCaret = !this.DrawCaret;
+                    this._View.HideCaret = !this.DrawCaret;
                     break;
                 case "DrawCaretLine":
-                    this.View.HideLineMarker = !this.DrawCaretLine;
+                    this._View.HideLineMarker = !this.DrawCaretLine;
                     break;
                 case "DrawLineNumber":
                     this.Document.DrawLineNumber = this.DrawLineNumber;
@@ -1369,6 +1386,9 @@ namespace FooEditEngine.WPF
                 case "Foreground":
                     this.Render.Foreground = D2DRender.ToColor4(this.Foreground);
                     break;
+                case "HilightForeground":
+                    this.Render.HilightForeground = D2DRender.ToColor4(this.HilightForeground);
+                    break;
                 case "Background":
                     this.Render.Background = D2DRender.ToColor4(this.Background);
                     break;
@@ -1400,7 +1420,7 @@ namespace FooEditEngine.WPF
                     this.Render.OverwriteCaret = D2DRender.ToColor4(this.OverwriteCaret);
                     break;
                 case "Padding":
-                    this.View.Padding = new Padding((int)this.Padding.Left, (int)this.Padding.Top, (int)this.Padding.Right, (int)this.Padding.Bottom);
+                    this._View.Padding = new Padding((int)this.Padding.Left, (int)this.Padding.Top, (int)this.Padding.Right, (int)this.Padding.Bottom);
                     break;
                 case "LineMarker":
                     this.Render.LineMarker = D2DRender.ToColor4(this.LineMarker);
@@ -1440,6 +1460,14 @@ namespace FooEditEngine.WPF
         #endregion
         #region property
 
+        internal EditView View
+        {
+            get
+            {
+                return this._View;
+            }
+        }
+
         internal Controller Controller
         {
             get
@@ -1526,7 +1554,7 @@ namespace FooEditEngine.WPF
         /// </summary>
         public LineToIndexTable LayoutLineCollection
         {
-            get { return this.View.LayoutLines; }
+            get { return this._View.LayoutLines; }
         }
 
         /// <summary>
@@ -1651,7 +1679,22 @@ namespace FooEditEngine.WPF
         /// </summary>
         public new static readonly DependencyProperty BackgroundProperty =
             DependencyProperty.Register("Background", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(SystemColors.WindowColor));
-        
+
+        /// <summary>
+        /// 選択時の文字色を表す。これは依存プロパティです
+        /// </summary>
+        public System.Windows.Media.Color HilightForeground
+        {
+            get { return (System.Windows.Media.Color)GetValue(HilightForegroundProperty); }
+            set { SetValue(HilightForegroundProperty, value); }
+        }
+
+        /// <summary>
+        /// ControlCharの依存プロパティを表す
+        /// </summary>
+        public static readonly DependencyProperty HilightForegroundProperty =
+            DependencyProperty.Register("HilightForeground", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(Colors.White));
+
         /// <summary>
         /// コントロールコードの文字色を表す。これは依存プロパティです
         /// </summary>