PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Tools/IronStudio/IronStudio/VisualStudio/Project/ImageHandler.cs

http://github.com/IronLanguages/main
C# | 207 lines | 138 code | 20 blank | 49 comment | 29 complexity | 6731e910804d6894491c9fc5ab3d6447 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. * ***************************************************************************/
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Drawing;
  17. using System.IO;
  18. using System.Windows.Forms;
  19. using Microsoft.VisualStudio;
  20. namespace Microsoft.VisualStudio.Project
  21. {
  22. public class ImageHandler : IDisposable
  23. {
  24. private ImageList imageList;
  25. private List<IntPtr> iconHandles;
  26. private static volatile object Mutex;
  27. private bool isDisposed;
  28. /// <summary>
  29. /// Initializes the <see cref="RDTListener"/> class.
  30. /// </summary>
  31. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")]
  32. static ImageHandler()
  33. {
  34. Mutex = new object();
  35. }
  36. /// <summary>
  37. /// Builds an empty ImageHandler object.
  38. /// </summary>
  39. public ImageHandler()
  40. {
  41. }
  42. /// <summary>
  43. /// Builds an ImageHandler object from a Stream providing the bitmap that
  44. /// stores the images for the image list.
  45. /// </summary>
  46. public ImageHandler(Stream resourceStream)
  47. {
  48. if(null == resourceStream)
  49. {
  50. throw new ArgumentNullException("resourceStream");
  51. }
  52. imageList = Utilities.GetImageList(resourceStream);
  53. }
  54. /// <summary>
  55. /// Builds an ImageHandler object from an ImageList object.
  56. /// </summary>
  57. public ImageHandler(ImageList list)
  58. {
  59. if(null == list)
  60. {
  61. throw new ArgumentNullException("list");
  62. }
  63. imageList = list;
  64. }
  65. /// <summary>
  66. /// Closes the ImageHandler object freeing its resources.
  67. /// </summary>
  68. public void Close()
  69. {
  70. if(null != iconHandles)
  71. {
  72. foreach(IntPtr hnd in iconHandles)
  73. {
  74. if(hnd != IntPtr.Zero)
  75. {
  76. NativeMethods.DestroyIcon(hnd);
  77. }
  78. }
  79. iconHandles = null;
  80. }
  81. if(null != imageList)
  82. {
  83. imageList.Dispose();
  84. imageList = null;
  85. }
  86. }
  87. /// <summary>
  88. /// Add an image to the ImageHandler.
  89. /// </summary>
  90. /// <param name="image">the image object to be added.</param>
  91. public void AddImage(Image image)
  92. {
  93. if(null == image)
  94. {
  95. throw new ArgumentNullException("image");
  96. }
  97. if(null == imageList)
  98. {
  99. imageList = new ImageList();
  100. }
  101. imageList.Images.Add(image);
  102. if(null != iconHandles)
  103. {
  104. iconHandles.Add(IntPtr.Zero);
  105. }
  106. }
  107. /// <summary>
  108. /// Get or set the ImageList object for this ImageHandler.
  109. /// </summary>
  110. public ImageList ImageList
  111. {
  112. get { return imageList; }
  113. set
  114. {
  115. Close();
  116. imageList = value;
  117. }
  118. }
  119. /// <summary>
  120. /// Returns the handle to an icon build from the image of index
  121. /// iconIndex in the image list.
  122. /// </summary>
  123. public IntPtr GetIconHandle(int iconIndex)
  124. {
  125. // Verify that the object is in a consistent state.
  126. if((null == imageList))
  127. {
  128. throw new InvalidOperationException();
  129. }
  130. // Make sure that the list of handles is initialized.
  131. if(null == iconHandles)
  132. {
  133. InitHandlesList();
  134. }
  135. // Verify that the index is inside the expected range.
  136. if((iconIndex < 0) || (iconIndex >= iconHandles.Count))
  137. {
  138. throw new ArgumentOutOfRangeException("iconIndex");
  139. }
  140. // Check if the icon is in the cache.
  141. if(IntPtr.Zero == iconHandles[iconIndex])
  142. {
  143. Bitmap bitmap = imageList.Images[iconIndex] as Bitmap;
  144. // If the image is not a bitmap, then we can not build the icon,
  145. // so we have to return a null handle.
  146. if(null == bitmap)
  147. {
  148. return IntPtr.Zero;
  149. }
  150. iconHandles[iconIndex] = bitmap.GetHicon();
  151. }
  152. return iconHandles[iconIndex];
  153. }
  154. private void InitHandlesList()
  155. {
  156. iconHandles = new List<IntPtr>(imageList.Images.Count);
  157. for(int i = 0; i < imageList.Images.Count; ++i)
  158. {
  159. iconHandles.Add(IntPtr.Zero);
  160. }
  161. }
  162. #region IDisposable Members
  163. /// <summary>
  164. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  165. /// </summary>
  166. public void Dispose()
  167. {
  168. this.Dispose(true);
  169. GC.SuppressFinalize(this);
  170. }
  171. #endregion
  172. private void Dispose(bool disposing)
  173. {
  174. if(!this.isDisposed)
  175. {
  176. lock(Mutex)
  177. {
  178. if(disposing)
  179. {
  180. this.imageList.Dispose();
  181. }
  182. this.isDisposed = true;
  183. }
  184. }
  185. }
  186. }
  187. }