OSDN Git Service

InsertAtSelectionHandlerでo_startIndexとo_endIndexを指定できるようにした
[fooeditengine/FooEditEngine.git] / Common / DotNetTextStore / TextStoreBase.cs
index 3bee9f7..90e8c6d 100644 (file)
@@ -1,5 +1,6 @@
 // TSF のデバッグ表示を行うかどうか?\r
 //#define TSF_DEBUG_OUTPUT\r
+//#define TSF_DEBUG_OUTPUT_DISPLAY_ATTR\r
 using System;\r
 using System.Collections.Generic;\r
 using System.Linq;\r
@@ -30,6 +31,11 @@ namespace DotNetTextStore
             Debug.WriteLine(string.Format("{0, 4} : ↓↓↓ ", s_callCount) + _text);\r
         }\r
 \r
+        public static void Print(string i_string, params object[] i_params)\r
+        {\r
+            Debug.WriteLine(i_string, i_params);\r
+        }\r
+\r
         public void Dispose()\r
         {\r
             s_callCount++;\r
@@ -88,6 +94,9 @@ namespace DotNetTextStore
 \r
     public abstract class TextStoreBase\r
     {\r
+        public delegate double GetDpiHandeler();\r
+        public event GetDpiHandeler GetDpi;\r
+\r
         public delegate bool IsReadOnlyHandler();\r
         public event IsReadOnlyHandler IsReadOnly;\r
 \r
@@ -106,7 +115,7 @@ namespace DotNetTextStore
         public delegate string GetStringHandler(int start, int length);\r
         public event GetStringHandler GetString;\r
 \r
-        public delegate void InsertAtSelectionHandler(string i_value);\r
+        public delegate void InsertAtSelectionHandler(string i_value,ref int o_startIndex,ref int o_endIndex);\r
         public event InsertAtSelectionHandler InsertAtSelection;\r
 \r
         public delegate void GetScreenExtentHandler(\r
@@ -352,7 +361,10 @@ namespace DotNetTextStore
         {\r
             if( _sink != null )\r
             {\r
-                if( (_adviseFlags & AdviseFlags.TS_AS_SEL_CHANGE) != 0 )\r
+#if TSF_DEBUG_OUTPUT\r
+                DebugOut.Print(DebugOut.GetCaller());\r
+#endif\r
+                if ((_adviseFlags & AdviseFlags.TS_AS_SEL_CHANGE) != 0)\r
                     _sink.OnSelectionChange();\r
             }\r
         }\r
@@ -364,11 +376,14 @@ namespace DotNetTextStore
         /// <summary>\r
         /// テキストが変更されたことをTSFに伝える。各種ハンドラ内からコールしてはいけない。\r
         /// </summary>\r
-        public void NotifyTextChanged(TS_TEXTCHANGE textChange)\r
+        void NotifyTextChanged(TS_TEXTCHANGE textChange)\r
         {\r
             if( _sink != null )\r
             {\r
-                if( (_adviseFlags & AdviseFlags.TS_AS_TEXT_CHANGE) != 0 )\r
+#if TSF_DEBUG_OUTPUT\r
+                DebugOut.Print(DebugOut.GetCaller());\r
+#endif\r
+                if ((_adviseFlags & AdviseFlags.TS_AS_TEXT_CHANGE) != 0)\r
                     _sink.OnTextChange(0, ref textChange);\r
                 _sink.OnLayoutChange(TsLayoutCode.TS_LC_CHANGE, 1);\r
             }\r
@@ -390,6 +405,9 @@ namespace DotNetTextStore
         {\r
             if (_sink != null)\r
             {\r
+#if TSF_DEBUG_OUTPUT\r
+                DebugOut.Print(DebugOut.GetCaller());\r
+#endif\r
                 if ((_adviseFlags & AdviseFlags.TS_AS_TEXT_CHANGE) != 0)\r
                 {\r
                     var textChange = new TS_TEXTCHANGE();\r
@@ -412,7 +430,10 @@ namespace DotNetTextStore
         {\r
             if( _sink != null )\r
             {\r
-                if( (_adviseFlags & AdviseFlags.TS_AS_TEXT_CHANGE) != 0 )\r
+#if TSF_DEBUG_OUTPUT\r
+                DebugOut.Print(DebugOut.GetCaller());\r
+#endif\r
+                if ((_adviseFlags & AdviseFlags.TS_AS_TEXT_CHANGE) != 0)\r
                 {\r
                     var textChange = new TS_TEXTCHANGE();\r
                     textChange.start = 0;\r
@@ -433,6 +454,9 @@ namespace DotNetTextStore
         /// <summary>コントロールがフォーカスを取得した時に呼び出さなければいけない。</summary>\r
         public void SetFocus()\r
         {\r
+#if TSF_DEBUG_OUTPUT\r
+            DebugOut.Print(DebugOut.GetCaller());\r
+#endif\r
             if (_threadMgr != null)\r
                 _threadMgr.SetFocus(_documentMgr);\r
         }\r
@@ -792,18 +816,23 @@ namespace DotNetTextStore
                 if (GetStringLength == null)\r
                     throw new NotImplementedException();\r
 \r
+                int documentLength = GetStringLength();\r
+\r
                 if (i_startIndex < 0\r
                 || i_startIndex > i_endIndex\r
-                || i_endIndex > GetStringLength())\r
+                || i_endIndex > documentLength)\r
                 {\r
                     throw new COMException(\r
                         "インデックスが無効です。",\r
                         UnmanagedAPI.WinError.HRESULT.E_INVALIDARG\r
                     );\r
                 }\r
-\r
                 o_startIndex = i_startIndex;\r
                 o_endIndex = i_endIndex;\r
+\r
+#if TSF_DEBUG_OUTPUT\r
+                DebugOut.Print("o_startIndex:{0} o_endIndex:{1}", i_startIndex, i_endIndex);\r
+#endif\r
             }\r
         }\r
 \r
@@ -861,6 +890,10 @@ namespace DotNetTextStore
                     }\r
 \r
                     o_fetchedLength = 1;\r
+\r
+#if TSF_DEBUG_OUTPUT\r
+                    DebugOut.Print("sel start:{0} end:{1}", start, end);\r
+#endif\r
                 }\r
             }\r
         }\r
@@ -894,6 +927,10 @@ namespace DotNetTextStore
                 }\r
 \r
                 SetSelectionIndex(i_selections[0].start, i_selections[0].end);\r
+\r
+#if TSF_DEBUG_OUTPUT\r
+                DebugOut.Print("set selection startIndex:{0} endIndex:{1}", i_selections[0].start, i_selections[0].end);\r
+#endif\r
             }\r
         }\r
 \r
@@ -945,6 +982,9 @@ namespace DotNetTextStore
 \r
                 // 文字列を格納。\r
                 var text = GetString(i_startIndex, copyLength);\r
+#if TSF_DEBUG_OUTPUT\r
+                DebugOut.Print("got text:{0} from {1} length {2}", text,i_startIndex,copyLength);\r
+#endif\r
                 for (int i = 0; i < copyLength; i++)\r
                 {\r
                     o_plainText[i] = text[i];\r
@@ -1097,21 +1137,17 @@ namespace DotNetTextStore
                     var end = Math.Max(selectionStart, selectionEnd);\r
 \r
 #if TSF_DEBUG_OUTPUT\r
-                        Debug.WriteLine(string.Format(\r
-                            "start: {0}, end: {1}, text: {2}",\r
-                            start, end, new string(i_text)\r
-                        ));\r
+                    DebugOut.Print("start: {0}, end: {1}, text: {2}", start, end, new string(i_text));\r
 #endif\r
 \r
-                    InsertAtSelection(new string(i_text));\r
-\r
                     o_startIndex = start;\r
                     o_endIndex = start + i_length;\r
 \r
+                    InsertAtSelection(new string(i_text), ref o_startIndex, ref o_endIndex);\r
+\r
                     o_textChange.start = start;\r
                     o_textChange.oldEnd = end;\r
-                    o_textChange.newEnd = start + i_length;\r
-\r
+                    o_textChange.newEnd = o_endIndex;\r
                     // InsertAtSelection() 内でカーソル位置を更新しているため、ここでは不要。\r
                     // 改行した時に位置が狂う。\r
                     // SetSelectionIndex(start, start + i_length);\r
@@ -1151,14 +1187,14 @@ namespace DotNetTextStore
         }\r
 \r
         public void GetACPFromPoint(\r
-    int i_viewCookie,\r
-    ref POINT i_point,\r
-    GetPositionFromPointFlags i_flags,\r
-    out int o_index\r
-)\r
+            int i_viewCookie,\r
+            ref POINT i_point,\r
+            GetPositionFromPointFlags i_flags,\r
+            out int o_index\r
+        )\r
         {\r
 #if TSF_DEBUG_OUTPUT\r
-            using(var dbgout = new DebugOut("{0}()", DebugOut.GetCaller()) )\r
+            using (var dbgout = new DebugOut("{0}()", DebugOut.GetCaller()))\r
 #endif\r
             {\r
                 throw new NotImplementedException();\r
@@ -1206,11 +1242,19 @@ namespace DotNetTextStore
                 var pointBotttomRight = new POINT();\r
                 GetStringExtent(i_startIndex, i_endIndex, out pointTopLeft, out pointBotttomRight);\r
 \r
-                o_rect.left = (int)pointTopLeft.x;\r
-                o_rect.top = (int)pointTopLeft.y;\r
-                o_rect.bottom = (int)pointBotttomRight.y;//startFormattedText.Height);\r
-                o_rect.right = (int)pointBotttomRight.x;\r
+                if(this.GetDpi == null)\r
+                    throw new NotImplementedException();\r
+\r
+                double dpi = this.GetDpi();\r
+\r
+                o_rect.left = (int)(pointTopLeft.x * dpi / 96.0);\r
+                o_rect.top = (int)(pointTopLeft.y * dpi / 96.0);\r
+                o_rect.bottom = (int)(pointBotttomRight.y * dpi / 96.0);\r
+                o_rect.right = (int)(pointBotttomRight.x * dpi / 96.0);\r
                 o_isClipped = false;\r
+#if TSF_DEBUG_OUTPUT\r
+                DebugOut.Print("rect left:{0} top:{1} bottom:{2} right:{3}", o_rect.left, o_rect.top, o_rect.bottom, o_rect.right);\r
+#endif\r
             }\r
         }\r
 \r
@@ -1227,10 +1271,18 @@ namespace DotNetTextStore
 \r
                 GetScreenExtent(out pointTopLeft, out pointBottomRight);\r
 \r
-                o_rect.left = (int)pointTopLeft.x;\r
-                o_rect.top = (int)pointTopLeft.y;\r
-                o_rect.right = (int)pointBottomRight.x;\r
-                o_rect.bottom = (int)pointBottomRight.y;\r
+                if (this.GetDpi == null)\r
+                    throw new NotImplementedException();\r
+\r
+                double dpi = this.GetDpi();\r
+\r
+                o_rect.left = (int)(pointTopLeft.x * dpi / 96.0);\r
+                o_rect.top = (int)(pointTopLeft.y * dpi / 96.0);\r
+                o_rect.bottom = (int)(pointBottomRight.y * dpi / 96.0);\r
+                o_rect.right = (int)(pointBottomRight.x * dpi / 96.0);\r
+#if TSF_DEBUG_OUTPUT\r
+                DebugOut.Print("rect left:{0} top:{1} bottom:{2} right:{3}", o_rect.left, o_rect.top, o_rect.bottom, o_rect.right);\r
+#endif\r
             }\r
         }\r
 \r
@@ -1488,7 +1540,7 @@ namespace DotNetTextStore
                             attribute = attribute\r
                         };\r
 \r
-#if TSF_DEBUG_OUTPUT\r
+#if TSF_DEBUG_OUTPUT_DISPLAY_ATTR\r
                             Debug.WriteLine(\r
                                 "*******:::: DisplayAttribute: {0} ~ {1} :::::: *********",\r
                                 start, start + count\r