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 using System.Reflection;
17 using System.Collections;
18
19 namespace FooEditEngine
20 {
21     class Util
22     {
23 #if METRO || WINDOWS_UWP
24         static float? _LogicalDpi;
25         public static void GetDpi(out float dpix, out float dpiy)
26         {
27             if(_LogicalDpi == null)
28                 _LogicalDpi = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;
29             dpix = _LogicalDpi.Value;
30             dpiy = _LogicalDpi.Value;
31         }
32
33         public static double GetScale()
34         {
35             float dpi;
36             Util.GetDpi(out dpi, out dpi);
37             return dpi / 96.0;
38         }
39
40         public static Point GetClientPoint(Point screen, Windows.UI.Xaml.UIElement element)
41         {
42             //Windows10以降では補正する必要がある
43             Windows.Foundation.Rect win_rect = Windows.UI.Xaml.Window.Current.CoreWindow.Bounds;
44             screen = screen.Offset(-win_rect.X, -win_rect.Y);
45
46             var gt = Windows.UI.Xaml.Window.Current.Content.TransformToVisual(element);
47             return gt.TransformPoint(screen);
48         }
49
50         public static Point GetPointInWindow(Point client, Windows.UI.Xaml.UIElement element)
51         {
52             //ウィンドウ内での絶対座標を取得する
53             var gt = element.TransformToVisual(Windows.UI.Xaml.Window.Current.Content);
54             return gt.TransformPoint(client);
55         }
56
57         public static Point GetScreentPoint(Point client, Windows.UI.Xaml.UIElement element)
58         {
59             var gt = element.TransformToVisual(Windows.UI.Xaml.Window.Current.Content);
60             Point p = gt.TransformPoint(client);
61
62             //Windows10以降では補正する必要がある
63             Windows.Foundation.Rect win_rect = Windows.UI.Xaml.Window.Current.CoreWindow.Bounds;
64             var screenPoint = p.Offset(win_rect.X, win_rect.Y);
65             return screenPoint;
66         }
67         public static Windows.Foundation.Rect GetClientRect(Windows.Foundation.Rect screen, Windows.UI.Xaml.UIElement element)
68         {
69             //Windows10以降では補正する必要がある
70             Windows.Foundation.Rect win_rect = Windows.UI.Xaml.Window.Current.CoreWindow.Bounds;
71             screen.X -= win_rect.X;
72             screen.Y -= win_rect.Y;
73
74             var gt = Windows.UI.Xaml.Window.Current.Content.TransformToVisual(element);
75             return gt.TransformBounds(screen);
76         }
77         public static Windows.Foundation.Rect GetScreentRect(Windows.Foundation.Rect client, Windows.UI.Xaml.UIElement element)
78         {
79             //ウィンドウ内での絶対座標を取得する
80             var gt = element.TransformToVisual(Windows.UI.Xaml.Window.Current.Content);
81             Windows.Foundation.Rect screenRect = gt.TransformBounds(client);
82
83             //Windows10以降では補正する必要がある
84             Windows.Foundation.Rect win_rect = Windows.UI.Xaml.Window.Current.CoreWindow.Bounds;
85             screenRect.X += win_rect.X;
86             screenRect.Y += win_rect.Y;
87
88             return screenRect;
89         }
90         public static IEnumerable<char> GetEnumrator(string s)
91         {
92             char[] chars = s.ToCharArray();
93             return chars;
94         }
95 #elif WPF
96         public static void GetDpi(out float dpix, out float dpiy)
97         {
98             var dpiXProperty = typeof(System.Windows.SystemParameters).GetProperty("DpiX", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
99             var dpiYProperty = typeof(System.Windows.SystemParameters).GetProperty("Dpi", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
100
101             dpix = (int)dpiXProperty.GetValue(null, null);
102             dpiy = (int)dpiYProperty.GetValue(null, null);
103         }
104
105         public static double GetScale()
106         {
107             float dpi;
108             Util.GetDpi(out dpi, out dpi);
109             return dpi / 96.0;
110         }
111
112         public static Point GetClientPoint(Point screen, System.Windows.FrameworkElement element)
113         {
114             return element.PointFromScreen(screen);
115         }
116
117         public static Point GetScreentPoint(Point client, System.Windows.FrameworkElement element)
118         {
119             return element.PointFromScreen(client);
120         }
121
122         public static IEnumerable<char> GetEnumrator(string s)
123         {
124             return s;
125         }
126 #else
127         public static IEnumerable<char> GetEnumrator(string s)
128         {
129             return s;
130         }
131 #endif
132
133         public static T ConvertAbsIndexToRelIndex<T>(T n, int StartIndex, int Length) where T : IRange
134         {
135             n = Util.NormalizeIMaker<T>(n);
136
137             int markerEnd = n.start + n.length - 1;
138
139             int EndIndex = StartIndex + Length;
140
141             if (n.start >= StartIndex && markerEnd <= EndIndex)
142                 n.start -= StartIndex;
143             else if (n.start >= StartIndex && n.start <= EndIndex)
144             {
145                 n.start -= StartIndex;
146                 n.length = EndIndex - StartIndex + 1;
147             }
148             else if (markerEnd >= StartIndex && markerEnd <= EndIndex)
149             {
150                 n.start = 0;
151                 n.length = markerEnd - StartIndex + 1;
152             }
153             else if (n.start >= StartIndex && markerEnd <= EndIndex)
154                 n.start -= StartIndex;
155             else if (n.start <= StartIndex && markerEnd > EndIndex)
156             {
157                 n.start = 0;
158                 n.length = EndIndex - StartIndex + 1;
159             }
160             else
161             {
162                 n.start = -1;
163                 n.length = 0;
164             }
165             return n;
166         }
167
168         public static bool IsWordSeparator(char c)
169         {
170             if (c == Document.NewLine || char.IsSeparator(c) || char.IsPunctuation(c) || CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.MathSymbol)
171                 return true;
172             else
173                 return false;
174         }
175         public static void Swap<T>(ref T a, ref T b)
176         {
177             T c = b;
178             b = a;
179             a = c;
180         }
181
182         public static string Generate(char c, int count)
183         {
184             StringBuilder tabstr = new StringBuilder();
185             for (int j = count; j > 0; j--)
186                 tabstr.Append(c);
187             return tabstr.ToString();
188         }
189
190         public static Rectangle OffsetAndDeflate(Rectangle r, Size s)
191         {
192             return new Rectangle(r.X + s.Width,r.Y + s.Height, r.Width - s.Width, r.Height - s.Height);
193         }
194
195         public static T NormalizeIMaker<T>(T m) where T : IRange
196         {
197             if (m.length > 0)
198                 return m;
199             m.start = m.start + m.length;
200             m.length = Math.Abs(m.length);
201             return m;
202         }
203
204         public static int RoundUp(double x)
205         {
206             return (int)(x + 0.5);
207         }
208
209     }
210 }