ImageList.cs 11 KB

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