OSDN Git Service

インストーラーのパスを変更した
[fooeditengine/FooEditEngine.git] / Core / Util.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 using System;
12 using System.Linq;
13 using System.Collections.Generic;
14 using System.Globalization;
15 using System.Text;
16
17 namespace FooEditEngine
18 {
19     class Util
20     {
21 #if METRO
22         public static Windows.Foundation.Point GetClientPoint(Windows.Foundation.Point screen, Windows.UI.Xaml.UIElement element)
23         {
24             var gt = element.TransformToVisual(element);
25             return gt.TransformPoint(screen);
26         }
27         public static Windows.Foundation.Point GetScreentPoint(Windows.Foundation.Point client, Windows.UI.Xaml.UIElement element)
28         {
29             var gt = element.TransformToVisual(Windows.UI.Xaml.Window.Current.Content);
30             return gt.TransformPoint(client);
31         }
32         public static Windows.Foundation.Rect GetClientRect(Windows.Foundation.Rect screen, Windows.UI.Xaml.UIElement element)
33         {
34             var gt = element.TransformToVisual(element);
35             return gt.TransformBounds(screen);
36         }
37         public static Windows.Foundation.Rect GetScreentRect(Windows.Foundation.Rect client, Windows.UI.Xaml.UIElement element)
38         {
39             var gt = element.TransformToVisual(Windows.UI.Xaml.Window.Current.Content);
40             return gt.TransformBounds(client);
41         }
42         public static IEnumerable<char> GetEnumrator(string s)
43         {
44             char[] chars = s.ToCharArray();
45             return chars;
46         }
47 #else
48         public static IEnumerable<char> GetEnumrator(string s)
49         {
50             return s;
51         }
52 #endif
53
54         public static T ConvertAbsIndexToRelIndex<T>(T n, int StartIndex, int Length) where T : IRange
55         {
56             n = Util.NormalizeIMaker<T>(n);
57
58             int markerEnd = n.start + n.length - 1;
59
60             int EndIndex = StartIndex + Length;
61
62             if (n.start >= StartIndex && markerEnd <= EndIndex)
63                 n.start -= StartIndex;
64             else if (n.start >= StartIndex && n.start <= EndIndex)
65             {
66                 n.start -= StartIndex;
67                 n.length = EndIndex - StartIndex + 1;
68             }
69             else if (markerEnd >= StartIndex && markerEnd <= EndIndex)
70             {
71                 n.start = 0;
72                 n.length = markerEnd - StartIndex + 1;
73             }
74             else if (n.start >= StartIndex && markerEnd <= EndIndex)
75                 n.start -= StartIndex;
76             else if (n.start <= StartIndex && markerEnd > EndIndex)
77             {
78                 n.start = 0;
79                 n.length = EndIndex - StartIndex + 1;
80             }
81             else
82             {
83                 n.start = -1;
84                 n.length = 0;
85             }
86             return n;
87         }
88
89         public static bool IsWordSeparator(char c)
90         {
91             if (c == Document.NewLine || char.IsSeparator(c) || char.IsPunctuation(c) || CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.MathSymbol)
92                 return true;
93             else
94                 return false;
95         }
96         public static void Swap<T>(ref T a, ref T b)
97         {
98             T c = b;
99             b = a;
100             a = c;
101         }
102
103         public static string Generate(char c, int count)
104         {
105             StringBuilder tabstr = new StringBuilder();
106             for (int j = count; j > 0; j--)
107                 tabstr.Append(c);
108             return tabstr.ToString();
109         }
110
111         public static Rectangle OffsetAndDeflate(Rectangle r, Size s)
112         {
113             return new Rectangle(r.X + s.Width,r.Y + s.Height, r.Width - s.Width, r.Height - s.Height);
114         }
115
116         public static T NormalizeIMaker<T>(T m) where T : IRange
117         {
118             if (m.length > 0)
119                 return m;
120             m.start = m.start + m.length;
121             m.length = Math.Abs(m.length);
122             return m;
123         }
124
125         public static int RoundUp(double x)
126         {
127             return (int)(x + 0.5);
128         }
129     }
130 }