ImageList.cs 12 KB

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