OSDN Git Service

e9649aae04486294fa49be4e2950a705b38d9c2b
[fooeditengine/FooEditEngine.git] / Core / TextServiceFramework / TextStoreHelper.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 #if METRO || WPF
12 using System.Linq;
13 using DotNetTextStore.UnmanagedAPI.WinDef;
14 using DotNetTextStore.UnmanagedAPI.TSF;
15 using DotNetTextStore;
16
17 namespace FooEditEngine
18 {
19     static class TextStoreHelper
20     {
21         public static bool StartCompstion(Document document)
22         {
23             document.UndoManager.BeginUndoGroup();
24             return true;
25         }
26
27         public static void EndCompostion(Document document)
28         {
29             document.UndoManager.EndUndoGroup();
30         }
31
32         public static bool ScrollToCompstionUpdated(TextStoreBase textStore,EditView view,int start, int end)
33         {
34             if (textStore.IsLocked() == false)
35                 return false;
36             using (Unlocker locker = textStore.LockDocument(false))
37             {
38                 foreach (TextDisplayAttribute attr in textStore.EnumAttributes(start, end))
39                 {
40                     if (attr.attribute.bAttr == TF_DA_ATTR_INFO.TF_ATTR_TARGET_CONVERTED)
41                     {
42                         if (view.AdjustSrc(attr.startIndex))
43                         {
44                             return true;
45                         }
46                     }
47                 }
48             }
49             return false;   
50         }
51
52         public static void GetStringExtent(Document document,EditView view,int i_startIndex,int i_endIndex,out Point startPos,out Point endPos)
53         {
54             var endIndex = i_endIndex < 0 ? document.Length - 1 : i_endIndex;
55             TextPoint endTextPoint;
56
57             startPos = view.GetPostionFromTextPoint(view.LayoutLines.GetTextPointFromIndex(i_startIndex));
58             endTextPoint = view.GetLayoutLineFromIndex(endIndex);
59             endPos = view.GetPostionFromTextPoint(endTextPoint);
60             endPos.Y += view.LayoutLines.GetLayout(endTextPoint.row).Height;
61         }
62
63         public static void GetSelection(Controller controller, SelectCollection selectons, out int o_startIndex, out int o_endIndex)
64         {
65             if (controller.RectSelection && selectons.Count > 0)
66             {
67                 o_startIndex = selectons[0].start;
68                 o_endIndex = o_startIndex + selectons[0].length;
69             }
70             else
71             {
72                 o_startIndex = controller.SelectionStart;
73                 o_endIndex = o_startIndex + controller.SelectionLength;
74             }
75         }
76
77         public static void SetSelectionIndex(Controller controller,EditView view,int i_startIndex, int i_endIndex)
78         {
79             if (controller.IsRectInsertMode())
80             {
81                 TextPoint start = view.LayoutLines.GetTextPointFromIndex(i_startIndex);
82                 TextPoint end = view.LayoutLines.GetTextPointFromIndex(view.Selections.Last().start);
83                 controller.JumpCaret(i_endIndex);
84                 controller.Select(start, i_endIndex - i_startIndex, end.row - start.row);
85             }
86             else if (i_startIndex == i_endIndex)
87             {
88                 controller.JumpCaret(i_startIndex);
89             }
90             else
91             {
92                 controller.Select(i_startIndex, i_endIndex - i_startIndex);
93             }
94         }
95
96         public static void InsertTextAtSelection(Controller controller,string i_value)
97         {
98             controller.DoInputString(i_value, true);
99         }
100
101         public static void NotifyTextChanged(TextStoreBase textStore, int startIndex,int removeLength,int insertLength)
102         {
103 #if METRO
104             //Windows8.1では同じ値にしないと日本語入力ができなくなってしまう
105             int oldend = startIndex,
106             newend = startIndex;
107 #else
108             //TS_TEXTCHANGE structure's remark
109             //1文字削除した場合はoldendに削除前の位置を設定し、newendとstartIndexに現在位置を設定し、
110             //1文字追加した場合はoldendに追加前の位置を設定し、newendとstartIndexに現在位置を設定する
111             int oldend = startIndex + removeLength,
112                 newend = startIndex + insertLength;
113 #endif
114             textStore.NotifyTextChanged(startIndex, oldend, newend);
115         }
116     }
117 }
118 #endif