ImageList.cs 12 KB

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