OSDN Git Service

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