This repository was archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 805
Expand file tree
/
Copy pathWinFormsResourceService.cs
More file actions
248 lines (231 loc) · 8.42 KB
/
WinFormsResourceService.cs
File metadata and controls
248 lines (231 loc) · 8.42 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace ICSharpCode.Core.WinForms
{
/// <summary>
/// This Class contains two ResourceManagers, which handle string and image resources
/// for the application. It do handle localization strings on this level.
/// </summary>
public static class WinFormsResourceService
{
static Dictionary<string, Icon> iconCache = new Dictionary<string, Icon>();
static Dictionary<string, Bitmap> bitmapCache = new Dictionary<string, Bitmap>();
static readonly IResourceService resourceService;
static WinFormsResourceService()
{
resourceService = ServiceSingleton.GetRequiredService<IResourceService>();
resourceService.LanguageChanged += OnLanguageChanged;
}
static void OnLanguageChanged(object sender, EventArgs e)
{
lock (iconCache) {
iconCache.Clear();
}
lock (bitmapCache) {
bitmapCache.Clear();
}
}
#region Font loading
static Font defaultMonospacedFont;
public static Font DefaultMonospacedFont {
get {
if (defaultMonospacedFont == null) {
defaultMonospacedFont = LoadDefaultMonospacedFont(FontStyle.Regular);
}
return defaultMonospacedFont;
}
}
/// <summary>
/// Loads the default monospaced font (Consolas or Courier New).
/// </summary>
public static Font LoadDefaultMonospacedFont(FontStyle style)
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT
&& Environment.OSVersion.Version.Major >= 6)
{
return LoadFont("Consolas", 10, style);
} else {
return LoadFont("Courier New", 10, style);
}
}
/// <summary>
/// The LoadFont routines provide a safe way to load fonts.
/// </summary>
/// <param name="fontName">The name of the font to load.</param>
/// <param name="size">The size of the font to load.</param>
/// <returns>
/// The font to load or the menu font, if the requested font couldn't be loaded.
/// </returns>
public static Font LoadFont(string fontName, int size)
{
return LoadFont(fontName, size, FontStyle.Regular);
}
/// <summary>
/// The LoadFont routines provide a safe way to load fonts.
/// </summary>
/// <param name="fontName">The name of the font to load.</param>
/// <param name="size">The size of the font to load.</param>
/// <param name="style">The <see cref="System.Drawing.FontStyle"/> of the font</param>
/// <returns>
/// The font to load or the menu font, if the requested font couldn't be loaded.
/// </returns>
public static Font LoadFont(string fontName, int size, FontStyle style)
{
try {
return new Font(fontName, size, style);
} catch (Exception ex) {
LoggingService.Warn(ex);
return SystemInformation.MenuFont;
}
}
/// <summary>
/// The LoadFont routines provide a safe way to load fonts.
/// </summary>
/// <param name="fontName">The name of the font to load.</param>
/// <param name="size">The size of the font to load.</param>
/// <param name="unit">The <see cref="System.Drawing.GraphicsUnit"/> of the font</param>
/// <returns>
/// The font to load or the menu font, if the requested font couldn't be loaded.
/// </returns>
public static Font LoadFont(string fontName, int size, GraphicsUnit unit)
{
return LoadFont(fontName, size, FontStyle.Regular, unit);
}
/// <summary>
/// The LoadFont routines provide a safe way to load fonts.
/// </summary>
/// <param name="fontName">The name of the font to load.</param>
/// <param name="size">The size of the font to load.</param>
/// <param name="style">The <see cref="System.Drawing.FontStyle"/> of the font</param>
/// <param name="unit">The <see cref="System.Drawing.GraphicsUnit"/> of the font</param>
/// <returns>
/// The font to load or the menu font, if the requested font couldn't be loaded.
/// </returns>
public static Font LoadFont(string fontName, int size, FontStyle style, GraphicsUnit unit)
{
try {
return new Font(fontName, size, style, unit);
} catch (Exception ex) {
LoggingService.Warn(ex);
return SystemInformation.MenuFont;
}
}
/// <summary>
/// The LoadFont routines provide a safe way to load fonts.
/// </summary>
/// <param name="baseFont">The existing font from which to create the new font.</param>
/// <param name="newStyle">The new style of the font.</param>
/// <returns>
/// The font to load or the baseFont (if the requested font couldn't be loaded).
/// </returns>
public static Font LoadFont(Font baseFont, FontStyle newStyle)
{
try {
return new Font(baseFont.FontFamily.Name, (int) Math.Ceiling(baseFont.Size), newStyle, baseFont.Unit);
} catch (Exception ex) {
LoggingService.Warn(ex);
return baseFont;
}
}
#endregion
/// <summary>
/// Returns a icon from the resource database, it handles localization
/// transparent for the user. In the resource database can be a bitmap
/// instead of an icon in the dabase. It is converted automatically.
/// </summary>
/// <returns>
/// The icon in the (localized) resource database, or null, if the icon cannot
/// be found.
/// </returns>
/// <param name="name">
/// The name of the requested icon.
/// </param>
public static Icon GetIcon(string name)
{
lock (iconCache) {
Icon ico;
if (iconCache.TryGetValue(name, out ico))
return ico;
object iconobj = resourceService.GetImageResource(name);
if (iconobj == null) {
return null;
}
if (iconobj is Icon) {
ico = (Icon)iconobj;
} else {
ico = BitmapToIcon((Bitmap)iconobj);
}
iconCache[name] = ico;
return ico;
}
}
/// <summary>
/// Converts a bitmap into an icon.
/// </summary>
public static Icon BitmapToIcon(Bitmap bmp)
{
IntPtr hIcon = bmp.GetHicon();
try {
using (Icon tempIco = Icon.FromHandle(hIcon)) {
// Icon.FromHandle creates a Icon object that uses the HIcon but does
// not own it. We could leak HIcons on language changes.
// We have no idea when we may dispose the icons after a language change
// (they could still be used), so we'll have to create an owned icon.
// Unfortunately, there's no Icon.FromHandle(IntPtr,bool takeOwnership) method.
// We could use reflection to set the ownHandle field; or we create a copy of the icon
// and immediately destroy the original
return new Icon(tempIco, tempIco.Width, tempIco.Height);
} // dispose tempico, doesn't do much because the icon isn't owned
} finally {
NativeMethods.DestroyIcon(hIcon);
}
}
/// <summary>
/// Returns a bitmap from the resource database, it handles localization
/// transparent for the user.
/// The bitmaps are reused, you must not dispose the Bitmap!
/// </summary>
/// <returns>
/// The bitmap in the (localized) resource database.
/// </returns>
/// <param name="name">
/// The name of the requested bitmap.
/// </param>
/// <exception cref="ResourceNotFoundException">
/// Is thrown when the GlobalResource manager can't find a requested resource.
/// </exception>
public static Bitmap GetBitmap(string name)
{
lock (bitmapCache) {
Bitmap bmp;
if (bitmapCache.TryGetValue(name, out bmp))
return bmp;
bmp = (Bitmap)resourceService.GetImageResource(name);
if (bmp == null) {
throw new ResourceNotFoundException(name);
}
bitmapCache[name] = bmp;
return bmp;
}
}
}
}