ImageList.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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.6 2004/08/21 01:50:52 ravindra
  28. // Changed draw method to use the arguments passed in to draw the image.
  29. //
  30. // Revision 1.5 2004/08/19 21:39:09 pbartok
  31. // - Added missing Draw() method
  32. // - Added missing RecreateHandle event
  33. //
  34. // Revision 1.4 2004/08/11 17:43:08 pbartok
  35. // - Removed disposing of the actual images when the list is disposed
  36. //
  37. // Revision 1.3 2004/08/11 13:43:13 pbartok
  38. // - Added missing creation of the collection list
  39. //
  40. // Revision 1.2 2004/08/09 23:12:13 pbartok
  41. // - Fixed several bugs Ravindra pointed out
  42. //
  43. // Revision 1.1 2004/07/15 20:05:28 pbartok
  44. // - Implemented ImageList and ImageList.ImageCollection classes
  45. // - Added ColorDepth enumeration
  46. // - Updated SWF VS.Net project
  47. //
  48. //
  49. // COMPLETE
  50. using System.Collections;
  51. using System.ComponentModel;
  52. using System.Drawing;
  53. using System.Drawing.Imaging;
  54. namespace System.Windows.Forms {
  55. public sealed class ImageList : System.ComponentModel.Component {
  56. #region Local Variables
  57. private ColorDepth color_depth;
  58. private ImageCollection image_collection;
  59. private Size size;
  60. private Color transparency_color;
  61. private Delegate handler;
  62. private ImageListStreamer image_stream;
  63. #endregion // Local Variables
  64. #region Sub-classes
  65. public sealed class ImageCollection : IList, ICollection, IEnumerable {
  66. #region ImageCollection Local Variables
  67. private ImageList owner;
  68. private ArrayList list;
  69. #endregion // ImageCollection Local Variables
  70. #region ImageCollection Private Constructors
  71. internal ImageCollection(ImageList owner) {
  72. this.owner=owner;
  73. this.list=new ArrayList();
  74. }
  75. #endregion // ImageCollection Private Constructor
  76. #region ImageCollection Public Instance Properties
  77. public int Count {
  78. get {
  79. return list.Count;
  80. }
  81. }
  82. public bool Empty {
  83. get {
  84. return list.Count==0;
  85. }
  86. }
  87. public bool IsReadOnly {
  88. get {
  89. return list.IsReadOnly;
  90. }
  91. }
  92. public Image this[int index] {
  93. get {
  94. if (index<0 || index>=list.Count) {
  95. throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
  96. }
  97. return (Image)list[index];
  98. }
  99. set {
  100. if (index<0 || index>=list.Count) {
  101. throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
  102. }
  103. if (value==null) {
  104. throw new ArgumentOutOfRangeException("value", value, "Image cannot be null");
  105. }
  106. list[index]=value;
  107. // What happens if the bitmap had a previous 'MakeTransparent' done to it?
  108. ((Bitmap)list[index]).MakeTransparent(owner.transparency_color);
  109. }
  110. }
  111. #endregion // ImageCollection Public Instance Properties
  112. #region ImageCollection Private Instance Methods
  113. private int AddInternal(Image image) {
  114. int width;
  115. int height;
  116. PixelFormat format;
  117. width=owner.ImageSize.Width;
  118. height=owner.ImageSize.Height;
  119. switch(owner.color_depth) {
  120. case ColorDepth.Depth4Bit: format=PixelFormat.Format4bppIndexed; break;
  121. case ColorDepth.Depth8Bit: format=PixelFormat.Format8bppIndexed; break;
  122. case ColorDepth.Depth16Bit: format=PixelFormat.Format16bppRgb555; break;
  123. case ColorDepth.Depth24Bit: format=PixelFormat.Format24bppRgb; break;
  124. case ColorDepth.Depth32Bit: format=PixelFormat.Format32bppRgb; break;
  125. default: format=PixelFormat.Format32bppRgb; break;
  126. }
  127. // Check if we can add straight or if we have to resize
  128. if (image.Width!=width || image.Height!=height || image.PixelFormat!=format) {
  129. Graphics g;
  130. Bitmap reformatted_image;
  131. reformatted_image = new Bitmap(width, height, format);
  132. g=Graphics.FromImage(reformatted_image);
  133. g.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
  134. g.Dispose();
  135. return list.Add(reformatted_image);
  136. } else {
  137. return list.Add(image);
  138. }
  139. }
  140. internal void Dispose() {
  141. #if dontwantthis
  142. if (list!=null) {
  143. for (int i=0; i<list.Count; i++) {
  144. ((Image)list[i]).Dispose();
  145. }
  146. }
  147. #endif
  148. }
  149. #endregion // ImageCollection Private Instance Methods
  150. #region ImageCollection Public Instance Methods
  151. public int Add(Image value, Color transparentColor) {
  152. if (value==null) {
  153. throw new ArgumentNullException("value", "Cannot add null image");
  154. }
  155. ((Bitmap)value).MakeTransparent(owner.transparency_color);
  156. return AddInternal(value);
  157. }
  158. public void Add(Icon value) {
  159. Image image;
  160. image = value.ToBitmap();
  161. if (value==null || image==null) {
  162. throw new ArgumentNullException("value", "Cannot add null icon");
  163. }
  164. ((Bitmap)image).MakeTransparent(owner.transparency_color);
  165. AddInternal(image);
  166. }
  167. public void Add(Image value) {
  168. if (value==null) {
  169. throw new ArgumentNullException("value", "Cannot add null image");
  170. }
  171. ((Bitmap)value).MakeTransparent(owner.transparency_color);
  172. AddInternal(value);
  173. }
  174. public int AddStrip(Image value) {
  175. int image_count;
  176. int width;
  177. int height;
  178. Bitmap image;
  179. Graphics g;
  180. if (value==null) {
  181. throw new ArgumentNullException("value", "Cannot add null images");
  182. }
  183. if ((value.Width % owner.ImageSize.Width) != 0) {
  184. throw new ArgumentException("Strip is not a multiple of the ImageList with", "value");
  185. }
  186. // MSDN: The number of images is inferred from the width. A strip is multiple images side-by-side
  187. width=owner.ImageSize.Width;
  188. height=owner.ImageSize.Height;
  189. image_count=value.Width/width;
  190. for (int i=0; i<image_count; i++) {
  191. image = new Bitmap(value, width, height);
  192. g = Graphics.FromImage(image);
  193. g.DrawImage(value, new Rectangle(0, 0, width, height), i*width, 0, width, height, GraphicsUnit.Pixel);
  194. AddInternal(image);
  195. g.Dispose();
  196. image.Dispose();
  197. }
  198. // FIXME - is this right? MSDN says to return the index, but we might have multiple...
  199. return image_count;
  200. }
  201. public void Clear() {
  202. list.Clear();
  203. }
  204. public bool Contains(Image image) {
  205. return list.Contains(image);
  206. }
  207. public IEnumerator GetEnumerator() {
  208. return list.GetEnumerator();
  209. }
  210. public int IndexOf(Image image) {
  211. return list.IndexOf(image);
  212. }
  213. public void Remove(Image image) {
  214. list.Remove(image);
  215. }
  216. public void RemoveAt(int index) {
  217. if (index<0 || index>=list.Count) {
  218. throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
  219. }
  220. list.RemoveAt(index);
  221. }
  222. #endregion // ImageCollection Public Instance Methods
  223. #region ImageCollection Interface Properties
  224. object IList.this[int index] {
  225. get {
  226. if (index<0 || index>=list.Count) {
  227. throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
  228. }
  229. return this[index];
  230. }
  231. set {
  232. if (!(value is Bitmap)) {
  233. throw new ArgumentException("Object of type Image required", "value");
  234. }
  235. this[index]=(Image)value;
  236. }
  237. }
  238. bool IList.IsFixedSize {
  239. get {
  240. return false;
  241. }
  242. }
  243. bool IList.IsReadOnly {
  244. get {
  245. return list.IsReadOnly;
  246. }
  247. }
  248. bool ICollection.IsSynchronized {
  249. get {
  250. return list.IsSynchronized;
  251. }
  252. }
  253. object ICollection.SyncRoot {
  254. get {
  255. return list.SyncRoot;
  256. }
  257. }
  258. #endregion // ImageCollection Interface Properties
  259. #region ImageCollection Interface Methods
  260. int IList.Add(object value) {
  261. if (value == null) {
  262. throw new ArgumentNullException("value", "Cannot add null images");
  263. }
  264. if (!(value is Bitmap)) {
  265. throw new ArgumentException("Object of type Image required", "value");
  266. }
  267. return list.Add(value);
  268. }
  269. bool IList.Contains(object value) {
  270. if (!(value is Bitmap)) {
  271. throw new ArgumentException("Object of type Image required", "value");
  272. }
  273. return this.Contains((Image) value);
  274. }
  275. int IList.IndexOf(object value) {
  276. if (!(value is Bitmap)) {
  277. throw new ArgumentException("Object of type Image required", "value");
  278. }
  279. return this.IndexOf((Image) value);
  280. }
  281. void IList.Insert(int index, object value) {
  282. if (!(value is Bitmap)) {
  283. throw new ArgumentException("Object of type Image required", "value");
  284. }
  285. list.Insert(index, value);
  286. }
  287. void IList.Remove(object value) {
  288. if (!(value is Bitmap)) {
  289. throw new ArgumentException("Object of type Image required", "value");
  290. }
  291. list.Remove(value);
  292. }
  293. void ICollection.CopyTo(Array array, int index) {
  294. if (list.Count>0) {
  295. list.CopyTo(array, index);
  296. }
  297. }
  298. #endregion // ImageCollection Interface Methods
  299. }
  300. #endregion // Sub-classes
  301. #region Public Constructors
  302. public ImageList() {
  303. color_depth = ColorDepth.Depth8Bit;
  304. transparency_color = Color.Transparent;
  305. size = new Size(16, 16);
  306. image_collection = new ImageCollection(this);
  307. }
  308. [MonoTODO]
  309. public ImageList(System.ComponentModel.IContainer container) {
  310. throw new NotImplementedException();
  311. }
  312. #endregion // Public Constructors
  313. #region Public Instance Properties
  314. public ColorDepth ColorDepth {
  315. get {
  316. return this.color_depth;
  317. }
  318. set {
  319. this.color_depth=value;
  320. }
  321. }
  322. [MonoTODO("Determine if we support HBITMAP handles, this would involve XplatUI")]
  323. public IntPtr Handle {
  324. get {
  325. if (RecreateHandle!=null) RecreateHandle(this, EventArgs.Empty);
  326. return IntPtr.Zero;
  327. }
  328. }
  329. [MonoTODO("Determine if we support HBITMAP handles, this would involve XplatUI")]
  330. public bool HandleCreated {
  331. get {
  332. return false;
  333. }
  334. }
  335. public ImageCollection Images {
  336. get {
  337. return this.image_collection;
  338. }
  339. }
  340. public Size ImageSize {
  341. get {
  342. return this.size;
  343. }
  344. set {
  345. if (value.Width<1 || value.Width>256 || value.Height<1 || value.Height>256) {
  346. throw new ArgumentException("ImageSize width and height must be between 1 and 255", "value");
  347. }
  348. this.size=value;
  349. }
  350. }
  351. public ImageListStreamer ImageStream {
  352. get {
  353. return image_stream;
  354. }
  355. set {
  356. image_stream = value;
  357. }
  358. }
  359. public Color TransparentColor {
  360. get {
  361. return this.transparency_color;
  362. }
  363. set {
  364. this.transparency_color=value;
  365. }
  366. }
  367. #endregion // Public Instance Properties
  368. #region Public Instance Methods
  369. public void Draw(Graphics g, Point pt, int index) {
  370. this.Draw(g, pt.X, pt.Y, this.size.Width, this.size.Height, index);
  371. }
  372. public void Draw(Graphics g, int x, int y, int width, int height, int index) {
  373. Image i;
  374. if ((index < 0) || (index >= this.Images.Count)) {
  375. throw new ArgumentOutOfRangeException("index", index, "ImageList does not contain that many images");
  376. }
  377. i = this.Images[index];
  378. g.DrawImage(i, x, y, width, height);
  379. }
  380. public override string ToString() {
  381. return "ImageList Size "+this.size.Width.ToString()+"x"+this.size.Height.ToString()+", Depth "+this.color_depth.ToString()+", Transparency color "+this.transparency_color.ToString();
  382. }
  383. #endregion // Public Instance Methods
  384. #region Protected Instance Methods
  385. protected override void Dispose(bool disposing) {
  386. if (image_collection!=null) {
  387. image_collection.Dispose();
  388. }
  389. }
  390. #endregion // Protected Instance Methods
  391. #region Events
  392. public event EventHandler RecreateHandle;
  393. #endregion // Events
  394. }
  395. }