Skip to content

Commit 1cb4e87

Browse files
committed
FileIconService added to allow custom template icons to be loaded from the file system.
1 parent 604696c commit 1cb4e87

File tree

3 files changed

+77
-12
lines changed

3 files changed

+77
-12
lines changed

src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@
474474
<Compile Include="Src\Services\DisplayBinding\ExternalProcessDisplayBinding.cs" />
475475
<Compile Include="Src\Services\DisplayBinding\ISecondaryDisplayBinding.cs" />
476476
<Compile Include="Src\Services\DisplayBinding\ShellExecuteDisplayBinding.cs" />
477+
<Compile Include="Src\Services\FileIconService.cs" />
477478
<Compile Include="Src\Services\File\FileChangeWatcher.cs" />
478479
<Compile Include="Src\Services\File\OpenedFile.cs" />
479480
<Compile Include="Src\Services\File\RecentOpen.cs" />
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2+
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
3+
4+
using ICSharpCode.Core;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Drawing;
8+
9+
namespace ICSharpCode.SharpDevelop
10+
{
11+
/// <summary>
12+
/// This Class contains a ResourceManager, which handles bitmap resources
13+
/// for the application which were loaded from the filesystem.
14+
/// </summary>
15+
public static class FileIconService
16+
{
17+
static Dictionary<string, Bitmap> bitmapCache = new Dictionary<string, Bitmap>();
18+
19+
static FileIconService()
20+
{
21+
ResourceService.ClearCaches += ResourceService_ClearCaches;
22+
}
23+
24+
static void ResourceService_ClearCaches(object sender, EventArgs e)
25+
{
26+
lock (bitmapCache) {
27+
bitmapCache.Clear();
28+
}
29+
}
30+
31+
/// <summary>
32+
/// Returns a bitmap from the file system. Paceholders like ${SharpDevelopBinPath}
33+
/// and AddinPath (e. g. ${AddInPath:ICSharpCode.FiletypeRegisterer}) are resolved
34+
/// through the StringParser.
35+
/// The bitmaps are reused, you must not dispose the Bitmap!
36+
/// </summary>
37+
/// <returns>
38+
/// The bitmap loaded from the file system.
39+
/// </returns>
40+
/// <param name="name">
41+
/// The name of the requested bitmap (prefix, path and filename).
42+
/// </param>
43+
/// <exception cref="FileNotFoundException">
44+
/// Is thrown when the bitmap was not found in the file system.
45+
/// </exception>
46+
public static Bitmap GetBitmap(string name)
47+
{
48+
Bitmap bmp = null;
49+
if (name.ToUpper().StartsWith("FILE:")) {
50+
lock (bitmapCache) {
51+
if (bitmapCache.TryGetValue(name, out bmp))
52+
return bmp;
53+
bmp = (Bitmap)Image.FromFile(StringParser.Parse(name.Substring(5,name.Length-5)));
54+
bitmapCache[name] = bmp;
55+
}
56+
}
57+
return bmp;
58+
}
59+
}
60+
}
61+

src/Main/Base/Project/Src/Services/IconService.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,21 @@ public static Bitmap GetGhostBitmap(Bitmap bitmap)
6060

6161
public static Bitmap GetBitmap(string name)
6262
{
63-
Bitmap bmp;
64-
try {
65-
bmp = WinFormsResourceService.GetBitmap(name);
66-
} catch (ResourceNotFoundException ex) {
67-
LoggingService.Warn(ex);
68-
bmp = null;
69-
}
70-
if (bmp != null) {
71-
return bmp;
72-
}
73-
74-
return WinFormsResourceService.GetBitmap("Icons.16x16.MiscFiles");
63+
Bitmap bmp = null;
64+
try {
65+
bmp = FileIconService.GetBitmap(name);
66+
if (bmp == null) {
67+
bmp = WinFormsResourceService.GetBitmap(name);
68+
}
69+
} catch (ResourceNotFoundException ex) {
70+
LoggingService.Warn(ex);
71+
} catch (FileNotFoundException ex) {
72+
LoggingService.Warn(ex);
73+
}
74+
if (bmp != null) {
75+
return bmp;
76+
}
77+
return WinFormsResourceService.GetBitmap("Icons.16x16.MiscFiles");
7578
}
7679

7780
public static Icon GetIcon(string name)

0 commit comments

Comments
 (0)