/
CocoaHelper.cs
178 lines (161 loc) · 7.05 KB
/
CocoaHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright © 2011-2013 Yanick Castonguay
//
// This file is part of MPfm.
//
// MPfm is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// MPfm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MPfm. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using Mono.Unix;
using MonoMac.AppKit;
using MonoMac.CoreGraphics;
using MonoMac.Foundation;
namespace MPfm.Mac.Classes.Helpers
{
/// <summary>
/// Helper static class for miscellaneous helper methods.
/// </summary>
public static class CocoaHelper
{
public static void ShowCriticalAlert(string title, string text)
{
using(NSAlert alert = new NSAlert())
{
alert.MessageText = title;
alert.InformativeText = text;
alert.AlertStyle = NSAlertStyle.Critical;
alert.RunModal();
}
}
public static RectangleF MeasureString(SizeF sizeConstraint, string text, string fontName, float fontSize)
{
NSMutableDictionary dict = new NSMutableDictionary();
dict.Add(NSAttributedString.FontAttributeName, NSFont.FromFontName(fontName, fontSize));
NSString nsstr = new NSString(text);
RectangleF rect = nsstr.BoundingRectWithSize(sizeConstraint, NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesLineFragmentOrigin, dict);
return rect;
}
public static float MeasureStringWidth(CGContext context, string text, string fontName, float fontSize)
{
context.SaveState();
PointF pos = context.TextPosition;
context.SelectFont(fontName, fontSize, CGTextEncoding.MacRoman);
context.TextMatrix = CGAffineTransform.MakeScale(1.0f, -1.0f);
//context.TranslateCTM(0, 20);
context.ScaleCTM(1, -1);
context.SetTextDrawingMode(CGTextDrawingMode.Invisible);
context.ShowTextAtPoint(pos.X, pos.Y, text);
PointF pos2 = context.TextPosition;
context.RestoreState();
return pos2.X - pos.X;
}
public static void DrawRect(CGContext context, RectangleF rect, CGColor color)
{
context.SaveState();
context.AddRect(rect);
context.Clip();
context.SetLineWidth(2);
context.SetStrokeColor(color);
context.StrokeRect(rect);
context.RestoreState();
}
public static void FillRect(CGContext context, RectangleF rect, CGColor color)
{
context.SaveState();
context.AddRect(rect);
context.Clip();
context.SetFillColor(color);
context.FillRect(rect);
context.RestoreState();
}
public static void DrawEllipsis(CGContext context, RectangleF rect, CGColor color, float lineWidth)
{
context.SaveState();
context.SetStrokeColor(color);
context.SetLineWidth(lineWidth);
context.AddEllipseInRect(rect);
context.StrokePath();
context.RestoreState();
}
public static void FillEllipsis(CGContext context, RectangleF rect, CGColor color)
{
context.SaveState();
context.SetFillColor(color);
context.FillEllipseInRect(rect);
context.FillPath();
context.RestoreState();
}
public static void FillPath(CGContext context, CGPath path, CGColor color)
{
context.SaveState();
context.SetFillColor(color);
context.AddPath(path);
context.FillPath();
context.RestoreState();
}
public static void EOFillPath(CGContext context, CGPath path, CGColor color)
{
context.SaveState();
context.SetFillColor(color);
context.AddPath(path);
context.EOFillPath();
context.RestoreState();
}
public static void FillGradient(CGContext context, RectangleF rect, CGColor color1, CGColor color2)
{
CGGradient gradientBackground;
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
float[] locationListBackground = new float[] { 1.0f, 0.0f };
List<float> colorListBackground = new List<float>();
colorListBackground.AddRange(color1.Components);
colorListBackground.AddRange(color2.Components);
gradientBackground = new CGGradient(colorSpace, colorListBackground.ToArray(), locationListBackground);
context.SaveState();
context.AddRect(rect);
context.Clip();
//context.ScaleCTM(1, -1);
context.DrawLinearGradient(gradientBackground, new PointF(0, 0), new PointF(0, rect.Height), CGGradientDrawingOptions.DrawsBeforeStartLocation);
context.RestoreState();
}
public static void DrawText(CGContext context, string text, string fontName, float fontSize, float translateHeight, float x, float y)
{
context.SaveState();
context.SelectFont(fontName, fontSize, CGTextEncoding.MacRoman);
context.SetTextDrawingMode(CGTextDrawingMode.Fill);
context.SetFillColor(new CGColor(1, 1));
context.SetStrokeColor(new CGColor(1.0f, 1.0f));
//context.AddRect(rectText);
//context.Clip();
context.TextMatrix = CGAffineTransform.MakeScale(1.0f, -1.0f);
context.TranslateCTM(0, translateHeight);
context.ScaleCTM(1, -1);
context.ShowTextAtPoint(x, y, text);
context.RestoreState();
}
public static void DrawText(RectangleF rect, float x, float y, string text, string fontName, float fontSize, NSColor fontColor)
{
NSMutableDictionary dict = new NSMutableDictionary();
dict.Add(NSAttributedString.FontAttributeName, NSFont.FromFontName(fontName, fontSize));
dict.Add(NSAttributedString.ForegroundColorAttributeName, fontColor);
NSString nsstr = new NSString(text);
RectangleF rectBounds = nsstr.BoundingRectWithSize(new SizeF(rect.Width, rect.Height), NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesLineFragmentOrigin, dict);
rectBounds.X = rect.X + x;
rectBounds.Y = rect.Y + y;
nsstr.DrawString(rectBounds, NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesLineFragmentOrigin, dict);
}
}
}