OSDN Git Service

DPIの設定を変えると変換窓がずれる現象を修正した
[fooeditengine/FooEditEngine.git] / WPF / FooEditEngine / FooTextBox.cs
index 697d6c0..f25a695 100644 (file)
@@ -73,7 +73,6 @@ namespace FooEditEngine.WPF
             this.textStore = new TextStore();\r
             this.textStore.IsLoading += textStore_IsLoading;\r
             this.textStore.IsReadOnly += textStore_IsReadOnly;\r
-            this.textStore.GetDpi += textStore_GetDpi;\r
             this.textStore.GetStringLength += () => this.Document.Length;\r
             this.textStore.GetString += _textStore_GetString;\r
             this.textStore.GetSelectionIndex += _textStore_GetSelectionIndex;\r
@@ -86,14 +85,15 @@ namespace FooEditEngine.WPF
             this.textStore.CompositionUpdated += textStore_CompositionUpdated;\r
             this.textStore.CompositionEnded += textStore_CompositionEnded;\r
 \r
-            this.Document = new Document();\r
-\r
             this.Render = new D2DRender(this, 200, 200,this.image);\r
             this.Render.ShowFullSpace = this.ShowFullSpace;\r
             this.Render.ShowHalfSpace = this.ShowHalfSpace;\r
             this.Render.ShowTab = this.ShowTab;\r
 \r
-            this.View = new EditView(this.Document, this.Render,new Padding(5,5,5,5));\r
+            this.Document = new Document();\r
+            this.Document.LayoutLines.Render = this.Render;\r
+\r
+            this.View = new EditView(this.Document, this.Render, new Padding(5, 5, 5, 5));\r
             this.View.SrcChanged += View_SrcChanged;\r
             this.View.InsertMode = this.InsertMode;\r
             this.View.DrawLineNumber = this.DrawLineNumber;\r
@@ -347,21 +347,52 @@ namespace FooEditEngine.WPF
         }\r
 \r
         /// <summary>\r
+        /// 指定行までスクロールする\r
+        /// </summary>\r
+        /// <param name="row">行</param>\r
+        /// <param name="alignTop">指定行を画面上に置くなら真。そうでないなら偽</param>\r
+        public void ScrollIntoView(int row, bool alignTop)\r
+        {\r
+            this.View.ScrollIntoView(row, alignTop);\r
+        }\r
+\r
+        /// <summary>\r
+        /// ストリームからドキュメントを構築する\r
+        /// </summary>\r
+        /// <param name="tr">TextReader</param>\r
+        /// <param name="token">キャンセル用トークン</param>\r
+        /// <returns>Taskオブジェクト</returns>\r
+        public async Task LoadAsync(System.IO.TextReader tr, System.Threading.CancellationTokenSource token)\r
+        {\r
+            WinFileReader fs = new WinFileReader(tr);\r
+            await this.LoadAsyncImpl(fs, token);\r
+        }\r
+\r
+        /// <summary>\r
         /// ファイルからドキュメントを構築する\r
         /// </summary>\r
         /// <param name="filepath">ファイルパス</param>\r
         /// <param name="enc">エンコード</param>\r
+        /// <param name="token">キャンセル用トークン</param>\r
         /// <returns>Taskオブジェクト</returns>\r
         public async Task LoadFileAsync(string filepath, Encoding enc,System.Threading.CancellationTokenSource token)\r
         {\r
-            this.View.LayoutLines.IsFrozneDirtyFlag = true;\r
             WinFileReader fs = new WinFileReader(filepath, enc);\r
+            await this.LoadAsyncImpl(fs, token);\r
+            fs.Close();\r
+        }\r
+\r
+        async Task LoadAsyncImpl(WinFileReader fs,System.Threading.CancellationTokenSource token)\r
+        {\r
+            this.IsEnabled = false;\r
+            this.View.LayoutLines.IsFrozneDirtyFlag = true;\r
             await this.Document.LoadAsync(fs, token);\r
             this.View.LayoutLines.IsFrozneDirtyFlag = false;\r
             TextStoreHelper.NotifyTextChanged(this.textStore, 0, 0, this.Document.Length);\r
             if (this.verticalScrollBar != null)\r
                 this.verticalScrollBar.Maximum = this.View.LayoutLines.Count;\r
             this.View.CalculateLineCountOnScreen();\r
+            this.IsEnabled = true;\r
         }\r
 \r
         /// <summary>\r
@@ -376,6 +407,7 @@ namespace FooEditEngine.WPF
             WinFileWriter fs = new WinFileWriter(filepath, enc);\r
             fs.NewLine = newLine;\r
             await this.Document.SaveAsync(fs, token);\r
+            fs.Close();\r
         }\r
 \r
         /// <summary>\r
@@ -408,19 +440,15 @@ namespace FooEditEngine.WPF
         \r
         void Refresh(Rectangle updateRect)\r
         {\r
-            if (this.disposed)\r
+            if (this.disposed || this.Visibility == Visibility.Collapsed)\r
                 return;\r
 \r
-            this.timer.Stop();\r
-\r
             this.Render.BegineDraw();\r
             if (this.IsEnabled)\r
                 this.View.Draw(updateRect);\r
             else\r
                 this.Render.FillBackground(updateRect);\r
             this.Render.EndDraw();\r
-\r
-            this.timer.Start();\r
         }\r
 \r
         #region Commands\r
@@ -512,13 +540,6 @@ namespace FooEditEngine.WPF
             get { return this.textStore; }\r
         }\r
 \r
-        double textStore_GetDpi()\r
-        {\r
-            float dpi;\r
-            this.Render.GetDpi(out dpi, out dpi);\r
-            return dpi;\r
-        }\r
-\r
         bool textStore_IsReadOnly()\r
         {\r
             return false;\r
@@ -572,8 +593,12 @@ namespace FooEditEngine.WPF
             Point startPos, endPos;\r
             TextStoreHelper.GetStringExtent(this.Document, this.View, i_startIndex, i_endIndex, out startPos, out endPos);\r
 \r
-            startPos = PointToScreen(this.TranslatePoint(startPos, this));\r
-            endPos = PointToScreen(this.TranslatePoint(endPos, this));\r
+            float dpi;\r
+            this.Render.GetDpi(out dpi, out dpi);\r
+            double scale = dpi / 96.0;\r
+            \r
+            startPos = PointToScreen(this.TranslatePoint(startPos.Scale(scale), this));\r
+            endPos = PointToScreen(this.TranslatePoint(endPos.Scale(scale), this));\r
             \r
             o_topLeft = new POINT((int)startPos.X, (int)startPos.Y);\r
             o_bottomRight = new POINT((int)endPos.X, (int)endPos.Y);\r
@@ -602,7 +627,7 @@ namespace FooEditEngine.WPF
             this.Refresh();\r
         }\r
 \r
-        void _textStore_InsertAtSelection(string i_value)\r
+        void _textStore_InsertAtSelection(string i_value, ref int o_startIndex, ref int o_endIndex)\r
         {\r
             TextStoreHelper.InsertTextAtSelection(this._Controller, i_value);\r
             this.Refresh();\r
@@ -1028,6 +1053,8 @@ namespace FooEditEngine.WPF
         void FooTextBox_Loaded(object sender, RoutedEventArgs e)\r
         {\r
             this.Resize(this.image.ActualWidth, this.image.ActualHeight);\r
+            this.Focus();\r
+            this.timer.Start();\r
         }\r
 \r
         bool Resize(double width, double height)\r
@@ -1061,6 +1088,9 @@ namespace FooEditEngine.WPF
         {\r
             switch (e.Property.Name)\r
             {\r
+                case "IndentMode":\r
+                    this._Controller.IndentMode = this.IndentMode;\r
+                    break;\r
                 case "Selection":\r
                     if(!this.nowCaretMove)\r
                         this.Select(this.Selection.Index, this.Selection.Length);\r
@@ -1171,6 +1201,9 @@ namespace FooEditEngine.WPF
                 case "UpdateArea":\r
                     this.Render.UpdateArea = D2DRender.ToColor4(this.UpdateArea);\r
                     break;\r
+                case "LineNumber":\r
+                    this.Render.LineNumber = D2DRender.ToColor4(this.LineNumber);\r
+                    break;\r
             }\r
             base.OnPropertyChanged(e);\r
         }\r
@@ -1278,6 +1311,21 @@ namespace FooEditEngine.WPF
         }\r
 \r
         /// <summary>\r
+        /// インデントの方法を表す\r
+        /// </summary>\r
+        public IndentMode IndentMode\r
+        {\r
+            get { return (IndentMode)GetValue(IndentModeProperty); }\r
+            set { SetValue(IndentModeProperty, value); }\r
+        }\r
+\r
+        /// <summary>\r
+        /// IndentModeの依存プロパティを表す\r
+        /// </summary>\r
+        public static readonly DependencyProperty IndentModeProperty =\r
+            DependencyProperty.Register("IndentMode", typeof(IndentMode), typeof(FooTextBox), new PropertyMetadata(IndentMode.Tab));\r
+\r
+        /// <summary>\r
         /// 選択範囲を表す\r
         /// </summary>\r
         /// <remarks>\r
@@ -1529,13 +1577,28 @@ namespace FooEditEngine.WPF
             get { return (System.Windows.Media.Color)GetValue(OverwriteCaretProperty); }\r
             set { SetValue(OverwriteCaretProperty, value); }\r
         }\r
-\r
+        \r
         /// <summary>\r
         /// OverwriteCaretの依存プロパティを表す\r
         /// </summary>\r
         public static readonly DependencyProperty OverwriteCaretProperty =\r
             DependencyProperty.Register("OverwriteCaret", typeof(System.Windows.Media.Color), typeof(FooTextBox), new FrameworkPropertyMetadata(SystemColors.WindowTextColor));\r
-        \r
+\r
+        /// <summary>\r
+        /// 行番号の色を表す\r
+        /// </summary>\r
+        public System.Windows.Media.Color LineNumber\r
+        {\r
+            get { return (System.Windows.Media.Color)GetValue(LineNumberProperty); }\r
+            set { SetValue(LineNumberProperty, value); }\r
+        }\r
+\r
+        /// <summary>\r
+        /// Using a DependencyProperty as the backing store for LineNumber.  This enables animation, styling, binding, etc...\r
+        /// </summary>\r
+        public static readonly DependencyProperty LineNumberProperty =\r
+            DependencyProperty.Register("LineNumber", typeof(System.Windows.Media.Color), typeof(FooTextBox), new PropertyMetadata(Colors.DimGray));\r
+\r
         /// <summary>\r
         /// 挿入モードなら真を返し、そうでないなら、偽を返す。これは依存プロパティです\r
         /// </summary>\r