FileTypeCollection.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace PaintDotNet
  6. {
  7. /// <summary>
  8. /// Represents a collection of FileType instances.
  9. /// </summary>
  10. [Serializable]
  11. public class FileTypeCollection
  12. {
  13. private FileType[] fileTypes;
  14. public FileType[] FileTypes
  15. {
  16. get
  17. {
  18. return (FileType[])fileTypes.Clone();
  19. }
  20. }
  21. public int Length
  22. {
  23. get
  24. {
  25. return fileTypes.Length;
  26. }
  27. }
  28. public FileType this[int index]
  29. {
  30. get
  31. {
  32. return fileTypes[index];
  33. }
  34. }
  35. public string[] AllExtensions
  36. {
  37. get
  38. {
  39. List<string> exts = new List<string>();
  40. foreach (FileType fileType in this.fileTypes)
  41. {
  42. foreach (string ext in fileType.Extensions)
  43. {
  44. exts.Add(ext);
  45. }
  46. }
  47. return exts.ToArray();
  48. }
  49. }
  50. internal FileTypeCollection(FileType[] fileTypes)
  51. {
  52. this.fileTypes = fileTypes;
  53. }
  54. public FileTypeCollection(ICollection fileTypes)
  55. {
  56. this.fileTypes = new FileType[fileTypes.Count];
  57. int dstIndex = 0;
  58. foreach (FileType ft in fileTypes)
  59. {
  60. this.fileTypes[dstIndex] = ft;
  61. ++dstIndex;
  62. }
  63. }
  64. public static FileType[] FilterFileTypeList(FileType[] input, bool excludeCantSave, bool excludeCantLoad)
  65. {
  66. List<FileType> filtered = new List<FileType>();
  67. foreach (FileType fileType in input)
  68. {
  69. if (excludeCantSave && !fileType.SupportsSaving)
  70. {
  71. continue;
  72. }
  73. if (excludeCantLoad && !fileType.SupportsLoading)
  74. {
  75. continue;
  76. }
  77. filtered.Add(fileType);
  78. }
  79. return filtered.ToArray();
  80. }
  81. public string ToString(bool excludeCantSave, bool excludeCantLoad)
  82. {
  83. FileType[] filtered = FilterFileTypeList(this.fileTypes, excludeCantSave, excludeCantLoad);
  84. StringBuilder sb = new StringBuilder();
  85. for (int i = 0; i < filtered.Length; ++i)
  86. {
  87. FileType fileType = filtered[i];
  88. sb.Append(fileType.ToString());
  89. if (i != filtered.Length - 1)
  90. {
  91. sb.Append("|");
  92. }
  93. }
  94. return sb.ToString();
  95. }
  96. /// <summary>
  97. /// Allows you to include an "All" type at the top that includes all the filetypes
  98. /// "All images (*.bmp, *.gif, ...)" for instance.
  99. /// </summary>
  100. /// <param name="includeAll">Whether or not to include the 'all' file type at the top</param>
  101. /// <param name="allName">The name of the 'all' type (example: "All images"). If this is null
  102. /// but includeAll is true, then this defaults to the string "All image types"</param>
  103. public string ToString(bool includeAll, string allName, bool excludeCantSave, bool excludeCantLoad)
  104. {
  105. if (allName == null)
  106. {
  107. allName = PdnResources.GetString("FileTypeCollection.AllImageTypes");
  108. }
  109. if (includeAll)
  110. {
  111. StringBuilder description = new StringBuilder(allName);
  112. StringBuilder formats = new StringBuilder();
  113. bool didFirst = false;
  114. FileType[] filtered = FilterFileTypeList(this.fileTypes, excludeCantSave, excludeCantLoad);
  115. for (int i = 0; i < filtered.Length; ++i)
  116. {
  117. if (!didFirst)
  118. {
  119. didFirst = true;
  120. description.Append(" (");
  121. }
  122. string[] extensions = (filtered[i]).Extensions;
  123. for (int j = 0; j < extensions.Length; ++j)
  124. {
  125. description.Append("*");
  126. description.Append(extensions[j]);
  127. formats.Append("*");
  128. formats.Append(extensions[j]);
  129. // if this is NOT the last extension in the whole list ...
  130. if (!(j == extensions.Length - 1 && i == filtered.Length - 1))
  131. {
  132. description.Append(", ");
  133. formats.Append(";");
  134. }
  135. }
  136. }
  137. if (didFirst)
  138. {
  139. description.Append(")");
  140. }
  141. string ret = description.ToString() + "|" + formats.ToString();
  142. if (filtered.Length != 0)
  143. {
  144. ret += "|" + ToString(excludeCantSave, excludeCantLoad);
  145. }
  146. return ret;
  147. }
  148. else
  149. {
  150. return ToString(excludeCantSave, excludeCantLoad);
  151. }
  152. }
  153. public int IndexOfFileType(FileType fileType)
  154. {
  155. if (fileType == null)
  156. {
  157. return -1;
  158. }
  159. for (int i = 0; i < fileTypes.Length; ++i)
  160. {
  161. if (fileTypes[i].Name == fileType.Name)
  162. {
  163. return i;
  164. }
  165. }
  166. return -1;
  167. }
  168. public int IndexOfExtension(string findMeExt)
  169. {
  170. if (findMeExt == null)
  171. {
  172. return -1;
  173. }
  174. for (int i = 0; i < fileTypes.Length; ++i)
  175. {
  176. foreach (string ext in fileTypes[i].Extensions)
  177. {
  178. if (ext.ToLower() == findMeExt.ToLower())
  179. {
  180. return i;
  181. }
  182. }
  183. }
  184. return -1;
  185. }
  186. public int IndexOfName(string name)
  187. {
  188. for (int i = 0; i < fileTypes.Length; ++i)
  189. {
  190. if (fileTypes[i].Name == name)
  191. {
  192. return i;
  193. }
  194. }
  195. return -1;
  196. }
  197. }
  198. }