source: branches/2839_HiveProjectManagement/HeuristicLab.CodeEditor/3.4/LanguageFeatures/CodeCompletion/CSharp/CompletionData/Images/CompletionImage.cs @ 16057

Last change on this file since 16057 was 16057, checked in by jkarder, 16 months ago

#2839:

File size: 14.6 KB
Line 
1#region License Information
2// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
3// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
4
5/* HeuristicLab
6 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
7 *
8 * This file is part of HeuristicLab.
9 *
10 * HeuristicLab is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * HeuristicLab is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
22 */
23#endregion
24
25using System;
26using System.Threading;
27using System.Windows;
28using System.Windows.Media;
29using System.Windows.Media.Imaging;
30using ICSharpCode.NRefactory.TypeSystem;
31
32namespace ICSharpCode.AvalonEdit.CodeCompletion {
33  /// <summary>
34  /// Provides icons for code-completion.
35  /// </summary>
36  public class CompletionImage {
37    #region Non-Entity Images
38    static readonly BitmapImage namespaceImage = LoadBitmap("NameSpace");
39
40    /// <summary>
41    /// Gets the image for namespaces.
42    /// </summary>
43    public static ImageSource NamespaceImage {
44      get { return namespaceImage; }
45    }
46
47    static BitmapImage LoadBitmap(string name) {
48      BitmapImage image = new BitmapImage(new Uri("pack://application:,,,/HeuristicLab.CodeEditor-3.4;component/LanguageFeatures/CodeCompletion/CSharp/CompletionData/Images/" + name + ".png"));
49      image.Freeze();
50      return image;
51    }
52    #endregion
53
54    #region Entity Images
55    static readonly CompletionImage imageClass = new CompletionImage("Class", false);
56    static readonly CompletionImage imageStruct = new CompletionImage("Struct", false);
57    static readonly CompletionImage imageInterface = new CompletionImage("Interface", false);
58    static readonly CompletionImage imageDelegate = new CompletionImage("Delegate", false);
59    static readonly CompletionImage imageEnum = new CompletionImage("Enum", false);
60    static readonly CompletionImage imageStaticClass = new CompletionImage("StaticClass", false);
61
62    /// <summary>Gets the image used for non-static classes.</summary>
63    public static CompletionImage Class { get { return imageClass; } }
64
65    /// <summary>Gets the image used for structs.</summary>
66    public static CompletionImage Struct { get { return imageStruct; } }
67
68    /// <summary>Gets the image used for interfaces.</summary>
69    public static CompletionImage Interface { get { return imageInterface; } }
70
71    /// <summary>Gets the image used for delegates.</summary>
72    public static CompletionImage Delegate { get { return imageDelegate; } }
73
74    /// <summary>Gets the image used for enums.</summary>
75    public static CompletionImage Enum { get { return imageEnum; } }
76
77    /// <summary>Gets the image used for modules/static classes.</summary>
78    public static CompletionImage StaticClass { get { return imageStaticClass; } }
79
80    static readonly CompletionImage imageField = new CompletionImage("Field", true);
81    static readonly CompletionImage imageFieldReadOnly = new CompletionImage("FieldReadOnly", true);
82    static readonly CompletionImage imageLiteral = new CompletionImage("Literal", false);
83    static readonly CompletionImage imageEnumValue = new CompletionImage("EnumValue", false);
84    static readonly CompletionImage imageTemplate = new CompletionImage("Template", false);
85
86    /// <summary>Gets the image used for non-static classes.</summary>
87    public static CompletionImage Field { get { return imageField; } }
88
89    /// <summary>Gets the image used for structs.</summary>
90    public static CompletionImage ReadOnlyField { get { return imageFieldReadOnly; } }
91
92    /// <summary>Gets the image used for constants.</summary>
93    public static CompletionImage Literal { get { return imageLiteral; } }
94
95    /// <summary>Gets the image used for enum values.</summary>
96    public static CompletionImage EnumValue { get { return imageEnumValue; } }
97
98    /// <summary>Gets the image used for templates.</summary>
99    public static CompletionImage Template { get { return imageTemplate; } }
100
101    static readonly CompletionImage imageMethod = new CompletionImage("Method", true);
102    static readonly CompletionImage imageConstructor = new CompletionImage("Constructor", true);
103    static readonly CompletionImage imageVirtualMethod = new CompletionImage("VirtualMethod", true);
104    static readonly CompletionImage imageOperator = new CompletionImage("Operator", false);
105    static readonly CompletionImage imageExtensionMethod = new CompletionImage("ExtensionMethod", true);
106    static readonly CompletionImage imagePInvokeMethod = new CompletionImage("PInvokeMethod", true);
107    static readonly CompletionImage imageProperty = new CompletionImage("Property", true);
108    static readonly CompletionImage imageIndexer = new CompletionImage("Indexer", true);
109    static readonly CompletionImage imageEvent = new CompletionImage("Event", true);
110
111    /// <summary>Gets the image used for methods.</summary>
112    public static CompletionImage Method { get { return imageMethod; } }
113
114    /// <summary>Gets the image used for constructos.</summary>
115    public static CompletionImage Constructor { get { return imageConstructor; } }
116
117    /// <summary>Gets the image used for virtual methods.</summary>
118    public static CompletionImage VirtualMethod { get { return imageVirtualMethod; } }
119
120    /// <summary>Gets the image used for operators.</summary>
121    public static CompletionImage Operator { get { return imageOperator; } }
122
123    /// <summary>Gets the image used for extension methods.</summary>
124    public static CompletionImage ExtensionMethod { get { return imageExtensionMethod; } }
125
126    /// <summary>Gets the image used for P/Invoke methods.</summary>
127    public static CompletionImage PInvokeMethod { get { return imagePInvokeMethod; } }
128
129    /// <summary>Gets the image used for properties.</summary>
130    public static CompletionImage Property { get { return imageProperty; } }
131
132    /// <summary>Gets the image used for indexers.</summary>
133    public static CompletionImage Indexer { get { return imageIndexer; } }
134
135    /// <summary>Gets the image used for events.</summary>
136    public static CompletionImage Event { get { return imageEvent; } }
137
138    /// <summary>
139    /// Gets the CompletionImage instance for the specified entity.
140    /// Returns null when no image is available for the entity type.
141    /// </summary>
142    public static CompletionImage GetCompletionImage(IEntity entity) {
143      if (entity == null)
144        throw new ArgumentNullException("entity");
145      switch (entity.SymbolKind) {
146        case SymbolKind.TypeDefinition:
147          return GetCompletionImageForType(((ITypeDefinition)entity).Kind, entity.IsStatic);
148        case SymbolKind.Field:
149          IField field = (IField)entity;
150          if (field.IsConst) {
151            if (field.DeclaringTypeDefinition != null && field.DeclaringTypeDefinition.Kind == TypeKind.Enum)
152              return imageEnumValue;
153            else
154              return imageLiteral;
155          }
156          return field.IsReadOnly ? imageFieldReadOnly : imageField;
157        case SymbolKind.Method:
158          IMethod method = (IMethod)entity;
159          if (method.IsExtensionMethod)
160            return imageExtensionMethod;
161          else
162            return method.IsOverridable ? imageVirtualMethod : imageMethod;
163        case SymbolKind.Property:
164          return imageProperty;
165        case SymbolKind.Indexer:
166          return imageIndexer;
167        case SymbolKind.Event:
168          return imageEvent;
169        case SymbolKind.Operator:
170        case SymbolKind.Destructor:
171          return imageOperator;
172        case SymbolKind.Constructor:
173          return imageConstructor;
174        default:
175          return null;
176      }
177    }
178
179    /// <summary>
180    /// Gets the CompletionImage instance for the specified entity.
181    /// Returns null when no image is available for the entity type.
182    /// </summary>
183    public static CompletionImage GetCompletionImage(IUnresolvedEntity entity) {
184      if (entity == null)
185        throw new ArgumentNullException("entity");
186      switch (entity.SymbolKind) {
187        case SymbolKind.TypeDefinition:
188          return GetCompletionImageForType(((IUnresolvedTypeDefinition)entity).Kind, entity.IsStatic);
189        case SymbolKind.Field:
190          IUnresolvedField field = (IUnresolvedField)entity;
191          if (field.IsConst) {
192            if (field.DeclaringTypeDefinition != null && field.DeclaringTypeDefinition.Kind == TypeKind.Enum)
193              return imageEnumValue;
194            else
195              return imageLiteral;
196          }
197          return field.IsReadOnly ? imageFieldReadOnly : imageField;
198        case SymbolKind.Method:
199          IUnresolvedMethod method = (IUnresolvedMethod)entity;
200          return method.IsOverridable ? imageVirtualMethod : imageMethod;
201        case SymbolKind.Property:
202          return imageProperty;
203        case SymbolKind.Indexer:
204          return imageIndexer;
205        case SymbolKind.Event:
206          return imageEvent;
207        case SymbolKind.Operator:
208        case SymbolKind.Destructor:
209          return imageOperator;
210        case SymbolKind.Constructor:
211          return imageConstructor;
212        default:
213          return null;
214      }
215    }
216
217    static CompletionImage GetCompletionImageForType(TypeKind typeKind, bool isStatic) {
218      switch (typeKind) {
219        case TypeKind.Interface:
220          return imageInterface;
221        case TypeKind.Struct:
222        case TypeKind.Void:
223          return imageStruct;
224        case TypeKind.Delegate:
225          return imageDelegate;
226        case TypeKind.Enum:
227          return imageEnum;
228        case TypeKind.Class:
229          return isStatic ? imageStaticClass : imageClass;
230        case TypeKind.Module:
231          return imageStaticClass;
232        default:
233          return null;
234      }
235    }
236
237    /// <summary>
238    /// Gets the image for the specified entity.
239    /// Returns null when no image is available for the entity type.
240    /// </summary>
241    public static ImageSource GetImage(IEntity entity) {
242      CompletionImage image = GetCompletionImage(entity);
243      if (image != null)
244        return image.GetImage(entity.Accessibility, entity.IsStatic);
245      else
246        return null;
247    }
248
249    /// <summary>
250    /// Gets the image for the specified entity.
251    /// Returns null when no image is available for the entity type.
252    /// </summary>
253    public static ImageSource GetImage(IUnresolvedEntity entity) {
254      CompletionImage image = GetCompletionImage(entity);
255      if (image != null)
256        return image.GetImage(entity.Accessibility, entity.IsStatic);
257      else
258        return null;
259    }
260    #endregion
261
262    #region Overlays
263    static readonly BitmapImage overlayStatic = LoadBitmap("OverlayStatic");
264
265    /// <summary>
266    /// Gets the overlay image for the static modifier.
267    /// </summary>
268    public ImageSource StaticOverlay { get { return overlayStatic; } }
269
270    const int AccessibilityOverlaysLength = 5;
271
272    static readonly BitmapImage[] accessibilityOverlays = new BitmapImage[AccessibilityOverlaysLength] {
273      null,
274      LoadBitmap("OverlayPrivate"),
275      LoadBitmap("OverlayProtected"),
276      LoadBitmap("OverlayInternal"),
277      LoadBitmap("OverlayProtectedInternal")
278    };
279
280    /// <summary>
281    /// Gets an overlay image for the specified accessibility.
282    /// Returns null if no overlay exists (for example, public members don't use overlays).
283    /// </summary>
284    public static ImageSource GetAccessibilityOverlay(Accessibility accessibility) {
285      return accessibilityOverlays[GetAccessibilityOverlayIndex(accessibility)];
286    }
287
288    static int GetAccessibilityOverlayIndex(Accessibility accessibility) {
289      switch (accessibility) {
290        case Accessibility.Private:
291          return 1;
292        case Accessibility.Protected:
293          return 2;
294        case Accessibility.Internal:
295          return 3;
296        case Accessibility.ProtectedOrInternal:
297        case Accessibility.ProtectedAndInternal:
298          return 4;
299        default:
300          return 0;
301      }
302    }
303    #endregion
304
305    #region Instance Members (add overlay to entity image)
306    readonly string imageName;
307    readonly bool showStaticOverlay;
308
309    private CompletionImage(string imageName, bool showStaticOverlay) {
310      this.imageName = imageName;
311      this.showStaticOverlay = showStaticOverlay;
312    }
313
314    ImageSource[] images = new ImageSource[2 * AccessibilityOverlaysLength];
315    // 0..N-1  = base image + accessibility overlay
316    // N..2N-1 = base image + static overlay + accessibility overlay
317
318    /// <summary>
319    /// Gets the image without any overlays.
320    /// </summary>
321    public ImageSource BaseImage {
322      get {
323        ImageSource image = images[0];
324        if (image == null) {
325          image = LoadBitmap(imageName);
326          Thread.MemoryBarrier();
327          images[0] = image;
328        }
329        return image;
330      }
331    }
332
333    /// <summary>
334    /// Gets this image combined with the specified accessibility overlay.
335    /// </summary>
336    public ImageSource GetImage(Accessibility accessibility, bool isStatic = false) {
337      int accessibilityIndex = GetAccessibilityOverlayIndex(accessibility);
338      int index;
339      if (isStatic && showStaticOverlay)
340        index = accessibilityOverlays.Length + accessibilityIndex;
341      else
342        index = accessibilityIndex;
343
344      if (index == 0)
345        return this.BaseImage;
346
347      ImageSource image = images[index];
348      if (image == null) {
349        DrawingGroup g = new DrawingGroup();
350        Rect iconRect = new Rect(0, 0, 16, 16);
351        g.Children.Add(new ImageDrawing(this.BaseImage, iconRect));
352
353        if (accessibilityOverlays[accessibilityIndex] != null)
354          g.Children.Add(new ImageDrawing(accessibilityOverlays[accessibilityIndex], iconRect));
355
356        image = new DrawingImage(g);
357        image.Freeze();
358        Thread.MemoryBarrier();
359        images[index] = image;
360      }
361      return image;
362    }
363
364    /// <inheritdoc/>
365    public override string ToString() {
366      return "[CompletionImage " + imageName + "]";
367    }
368    #endregion
369  }
370}
Note: See TracBrowser for help on using the repository browser.