OSDN Git Service

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