ImageList.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // $Log: ImageList.cs,v $
  27. // Revision 1.4 2004/08/11 17:43:08 pbartok
  28. // - Removed disposing of the actual images when the list is disposed
  29. //
  30. // Revision 1.3 2004/08/11 13:43:13 pbartok
  31. // - Added missing creation of the collection list
  32. //
  33. // Revision 1.2 2004/08/09 23:12:13 pbartok
  34. // - Fixed several bugs Ravindra pointed out
  35. //
  36. // Revision 1.1 2004/07/15 20:05:28 pbartok
  37. // - Implemented ImageList and ImageList.ImageCollection classes
  38. // - Added ColorDepth enumeration
  39. // - Updated SWF VS.Net project
  40. //
  41. //
  42. // COMPLETE
  43. using System.Collections;
  44. using System.ComponentModel;
  45. using System.Drawing;
  46. using System.Drawing.Imaging;
  47. namespace System.Windows.Forms {
  48. public sealed class ImageList : System.ComponentModel.Component {
  49. #region Local Variables
  50. private ColorDepth color_depth;
  51. private ImageCollection image_collection;
  52. private Size size;
  53. private Color transparency_color;
  54. private Delegate handler;
  55. private ImageListStreamer image_stream;
  56. #endregion // Local Variables
  57. #region Sub-classes
  58. public sealed class ImageCollection : IList, ICollection, IEnumerable {
  59. #region ImageCollection Local Variables
  60. private ImageList owner;
  61. private ArrayList list;
  62. #endregion // ImageCollection Local Variables
  63. #region ImageCollection Private Constructors
  64. internal ImageCollection(ImageList owner) {
  65. this.owner=owner;
  66. this.list=new ArrayList();
  67. }
  68. #endregion // ImageCollection Private Constructor
  69. #region ImageCollection Public Instance Properties
  70. public int Count {
  71. get {
  72. return list.Count;
  73. }
  74. }
  75. public bool Empty {
  76. get {
  77. return list.Count==0;
  78. }
  79. }
  80. public bool IsReadOnly {
  81. get {
  82. return list.IsReadOnly;
  83. }
  84. }
  85. public Image this[int index] {
  86. get {
  87. if (index<0 || index>=list.Count) {
  88. throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
  89. }
  90. return (Image)list[index];
  91. }
  92. set {
  93. if (index<0 || index>=list.Count) {
  94. throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
  95. }
  96. if (value==null) {
  97. throw new ArgumentOutOfRangeException("value", value, "Image cannot be null");
  98. }
  99. list[index]=value;
  100. // What happens if the bitmap had a previous 'MakeTransparent' done to it?
  101. ((Bitmap)list[index]).MakeTransparent(owner.transparency_color);
  102. }
  103. }
  104. #endregion // ImageCollection Public Instance Properties
  105. #region ImageCollection Private Instance Methods
  106. private int AddInternal(Image image) {
  107. int width;
  108. int height;
  109. PixelFormat format;
  110. width=owner.ImageSize.Width;
  111. height=owner.ImageSize.Height;
  112. switch(owner.color_depth) {
  113. case ColorDepth.Depth4Bit: format=PixelFormat.Format4bppIndexed; break;
  114. case ColorDepth.Depth8Bit: format=PixelFormat.Format8bppIndexed; break;
  115. case ColorDepth.Depth16Bit: format=PixelFormat.Format16bppRgb555; break;
  116. case ColorDepth.Depth24Bit: format=PixelFormat.Format24bppRgb; break;
  117. case ColorDepth.Depth32Bit: format=PixelFormat.Format32bppRgb; break;
  118. default: format=PixelFormat.Format32bppRgb; break;
  119. }
  120. // Check if we can add straight or if we have to resize
  121. if (image.Width!=width || image.Height!=height || image.PixelFormat!=format) {
  122. Graphics g;
  123. Bitmap reformatted_image;
  124. reformatted_image = new Bitmap(width, height, format);
  125. g=Graphics.FromImage(reformatted_image);
  126. g.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
  127. g.Dispose();
  128. return list.Add(reformatted_image);
  129. } else {
  130. return list.Add(image);
  131. }
  132. }
  133. internal void Dispose() {
  134. #if dontwantthis
  135. if (list!=null) {
  136. for (int i=0; i<list.Count; i++) {
  137. ((Image)list[i]).Dispose();
  138. }
  139. }
  140. #endif
  141. }
  142. #endregion // ImageCollection Private Instance Methods
  143. #region ImageCollection Public Instance Methods
  144. public int Add(Image value, Color transparentColor) {
  145. if (value==null) {
  146. throw new ArgumentNullException("value", "Cannot add null image");
  147. }
  148. ((Bitmap)value).MakeTransparent(owner.transparency_color);
  149. return AddInternal(value);
  150. }
  151. public void Add(Icon value) {
  152. Image image;
  153. image = value.ToBitmap();
  154. if (value==null || image==null) {
  155. throw new ArgumentNullException("value", "Cannot add null icon");
  156. }
  157. ((Bitmap)image).MakeTransparent(owner.transparency_color);
  158. AddInternal(image);
  159. }
  160. public void Add(Image value) {
  161. if (value==null) {
  162. throw new ArgumentNullException("value", "Cannot add null image");
  163. }
  164. ((Bitmap)value).MakeTransparent(owner.transparency_color);
  165. AddInternal(value);
  166. }
  167. public int AddStrip(Image value) {
  168. int image_count;
  169. int width;
  170. int height;
  171. Bitmap image;
  172. Graphics g;
  173. if (value==null) {
  174. throw new ArgumentNullException("value", "Cannot add null images");
  175. }
  176. if ((value.Width % owner.ImageSize.Width) != 0) {
  177. throw new ArgumentException("Strip is not a multiple of the ImageList with", "value");
  178. }
  179. // MSDN: The number of images is inferred from the width. A strip is multiple images side-by-side
  180. width=owner.ImageSize.Width;
  181. height=owner.ImageSize.Height;
  182. image_count=value.Width/width;
  183. for (int i=0; i<image_count; i++) {
  184. image = new Bitmap(value, width, height);
  185. g = Graphics.FromImage(image);
  186. g.DrawImage(value, new Rectangle(0, 0, width, height), i*width, 0, width, height, GraphicsUnit.Pixel);
  187. AddInternal(image);
  188. g.Dispose();
  189. image.Dispose();
  190. }
  191. // FIXME - is this right? MSDN says to return the index, but we might have multiple...
  192. return image_count;
  193. }
  194. public void Clear() {
  195. list.Clear();
  196. }
  197. public bool Contains(Image image) {
  198. return list.Contains(image);
  199. }
  200. public IEnumerator GetEnumerator() {
  201. return list.GetEnumerator();
  202. }
  203. public int IndexOf(Image image) {
  204. return list.IndexOf(image);
  205. }
  206. public void Remove(Image image) {
  207. list.Remove(image);
  208. }
  209. public void RemoveAt(int index) {
  210. if (index<0 || index>=list.Count) {
  211. throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
  212. }
  213. list.RemoveAt(index);
  214. }
  215. #endregion // ImageCollection Public Instance Methods
  216. #region ImageCollection Interface Properties
  217. object IList.this[int index] {
  218. get {
  219. if (index<0 || index>=list.Count) {
  220. throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
  221. }
  222. return this[index];
  223. }
  224. set {
  225. if (!(value is Bitmap)) {
  226. throw new ArgumentException("Object of type Image required", "value");
  227. }
  228. this[index]=(Image)value;
  229. }
  230. }
  231. bool IList.IsFixedSize {
  232. get {
  233. return false;
  234. }
  235. }
  236. bool IList.IsReadOnly {
  237. get {
  238. return list.IsReadOnly;
  239. }
  240. }
  241. bool ICollection.IsSynchronized {
  242. get {
  243. return list.IsSynchronized;
  244. }
  245. }
  246. object ICollection.SyncRoot {
  247. get {
  248. return list.SyncRoot;
  249. }
  250. }
  251. #endregion // ImageCollection Interface Properties
  252. #region ImageCollection Interface Methods
  253. int IList.Add(object value) {
  254. if (value == null) {
  255. throw new ArgumentNullException("value", "Cannot add null images");
  256. }
  257. if (!(value is Bitmap)) {
  258. throw new ArgumentException("Object of type Image required", "value");
  259. }
  260. return list.Add(value);
  261. }
  262. bool IList.Contains(object value) {
  263. if (!(value is Bitmap)) {
  264. throw new ArgumentException("Object of type Image required", "value");
  265. }
  266. return this.Contains((Image) value);
  267. }
  268. int IList.IndexOf(object value) {
  269. if (!(value is Bitmap)) {
  270. throw new ArgumentException("Object of type Image required", "value");
  271. }
  272. return this.IndexOf((Image) value);
  273. }
  274. void IList.Insert(int index, object value) {
  275. if (!(value is Bitmap)) {
  276. throw new ArgumentException("Object of type Image required", "value");
  277. }
  278. list.Insert(index, value);
  279. }
  280. void IList.Remove(object value) {
  281. if (!(value is Bitmap)) {
  282. throw new ArgumentException("Object of type Image required", "value");
  283. }
  284. list.Remove(value);
  285. }
  286. void ICollection.CopyTo(Array array, int index) {
  287. if (list.Count>0) {
  288. list.CopyTo(array, index);
  289. }
  290. }
  291. #endregion // ImageCollection Interface Methods
  292. }
  293. #endregion // Sub-classes
  294. #region Public Constructors
  295. public ImageList() {
  296. color_depth = ColorDepth.Depth8Bit;
  297. transparency_color = Color.Transparent;
  298. size = new Size(16, 16);
  299. image_collection = new ImageCollection(this);
  300. }
  301. #endregion // Public Constructors
  302. #region Public Instance Properties
  303. public ColorDepth ColorDepth {
  304. get {
  305. return this.color_depth;
  306. }
  307. set {
  308. this.color_depth=value;
  309. }
  310. }
  311. [MonoTODO("Determine if we support HBITMAP handles, this would involve XplatUI")]
  312. public IntPtr Handle {
  313. get {
  314. return IntPtr.Zero;
  315. }
  316. }
  317. [MonoTODO("Determine if we support HBITMAP handles, this would involve XplatUI")]
  318. public bool HandleCreated {
  319. get {
  320. return false;
  321. }
  322. }
  323. public ImageCollection Images {
  324. get {
  325. return this.image_collection;
  326. }
  327. }
  328. public Size ImageSize {
  329. get {
  330. return this.size;
  331. }
  332. set {
  333. if (value.Width<1 || value.Width>256 || value.Height<1 || value.Height>256) {
  334. throw new ArgumentException("ImageSize width and height must be between 1 and 255", "value");
  335. }
  336. this.size=value;
  337. }
  338. }
  339. public ImageListStreamer ImageStream {
  340. get {
  341. return image_stream;
  342. }
  343. set {
  344. image_stream = value;
  345. }
  346. }
  347. public Color TransparentColor {
  348. get {
  349. return this.transparency_color;
  350. }
  351. set {
  352. this.transparency_color=value;
  353. }
  354. }
  355. #endregion // Public Instance Properties
  356. #region Public Instance Methods
  357. public void Draw(Graphics g, Point pt, int index) {
  358. this.Draw(g, pt.X, pt.Y, this.size.Width, this.size.Height, index);
  359. }
  360. public void Draw(Graphics g, int x, int y, int width, int height, int index) {
  361. }
  362. public override string ToString() {
  363. return "ImageList Size "+this.size.Width.ToString()+"x"+this.size.Height.ToString()+", Depth "+this.color_depth.ToString()+", Transparency color "+this.transparency_color.ToString();
  364. }
  365. #endregion // Public Instance Methods
  366. #region Protected Instance Methods
  367. protected override void Dispose(bool disposing) {
  368. if (image_collection!=null) {
  369. image_collection.Dispose();
  370. }
  371. }
  372. #endregion // Protected Instance Methods
  373. }
  374. }