UnitsComboBoxHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Windows.Forms;
  5. namespace PaintDotNet
  6. {
  7. public sealed class UnitsComboBoxHandler
  8. : IUnitsComboBox
  9. {
  10. private ComboBox comboBox;
  11. [Browsable(false)]
  12. public ComboBox ComboBox
  13. {
  14. get
  15. {
  16. return this.comboBox;
  17. }
  18. }
  19. public UnitsComboBoxHandler(ComboBox comboBox)
  20. {
  21. this.comboBox = comboBox;
  22. this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
  23. this.comboBox.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
  24. ReloadItems();
  25. }
  26. private bool lowercase = true;
  27. private Hashtable unitsToString;
  28. private Hashtable stringToUnits;
  29. // maps from MeasurementUnit->bool for whether that item should be in the list or not
  30. private Hashtable measurementItems;
  31. private UnitsDisplayType unitsDisplayType = UnitsDisplayType.Plural;
  32. [DefaultValue(UnitsDisplayType.Plural)]
  33. public UnitsDisplayType UnitsDisplayType
  34. {
  35. get
  36. {
  37. return this.unitsDisplayType;
  38. }
  39. set
  40. {
  41. if (this.unitsDisplayType != value)
  42. {
  43. this.unitsDisplayType = value;
  44. ReloadItems();
  45. }
  46. }
  47. }
  48. [DefaultValue(true)]
  49. public bool LowercaseStrings
  50. {
  51. get
  52. {
  53. return this.lowercase;
  54. }
  55. set
  56. {
  57. if (this.lowercase != value)
  58. {
  59. this.lowercase = value;
  60. ReloadItems();
  61. }
  62. }
  63. }
  64. [DefaultValue(MeasurementUnit.Pixel)]
  65. public MeasurementUnit Units
  66. {
  67. get
  68. {
  69. object selected = this.stringToUnits[ComboBox.SelectedItem];
  70. return (MeasurementUnit)selected;
  71. }
  72. set
  73. {
  74. object selectMe = this.unitsToString[value];
  75. ComboBox.SelectedItem = selectMe;
  76. }
  77. }
  78. [Browsable(false)]
  79. public string UnitsText
  80. {
  81. get
  82. {
  83. if (ComboBox.SelectedItem == null)
  84. {
  85. return string.Empty;
  86. }
  87. else
  88. {
  89. return (string)ComboBox.SelectedItem;
  90. }
  91. }
  92. }
  93. [DefaultValue(true)]
  94. public bool PixelsAvailable
  95. {
  96. get
  97. {
  98. return (bool)this.measurementItems[MeasurementUnit.Pixel];
  99. }
  100. set
  101. {
  102. if (value != this.PixelsAvailable)
  103. {
  104. if (value)
  105. {
  106. AddUnit(MeasurementUnit.Pixel);
  107. }
  108. else
  109. {
  110. if (this.Units == MeasurementUnit.Pixel)
  111. {
  112. if (this.InchesAvailable)
  113. {
  114. this.Units = MeasurementUnit.Inch;
  115. }
  116. else if (this.MilsAvailable)
  117. {
  118. this.Units = MeasurementUnit.Mil;
  119. }
  120. else if (this.CentimetersAvailable)
  121. {
  122. this.Units = MeasurementUnit.Centimeter;
  123. }
  124. }
  125. RemoveUnit(MeasurementUnit.Pixel);
  126. }
  127. }
  128. }
  129. }
  130. [DefaultValue(true)]
  131. public bool InchesAvailable
  132. {
  133. get
  134. {
  135. return (bool)this.measurementItems[MeasurementUnit.Inch];
  136. }
  137. }
  138. [DefaultValue(true)]
  139. public bool MilsAvailable
  140. {
  141. get
  142. {
  143. return (bool)this.measurementItems[MeasurementUnit.Mil];
  144. }
  145. }
  146. [DefaultValue(true)]
  147. public bool CentimetersAvailable
  148. {
  149. get
  150. {
  151. return (bool)this.measurementItems[MeasurementUnit.Centimeter];
  152. }
  153. }
  154. public void RemoveUnit(MeasurementUnit removeMe)
  155. {
  156. InitMeasurementItems();
  157. this.measurementItems[removeMe] = false;
  158. ReloadItems();
  159. }
  160. public void AddUnit(MeasurementUnit addMe)
  161. {
  162. InitMeasurementItems();
  163. this.measurementItems[addMe] = true;
  164. ReloadItems();
  165. }
  166. private void InitMeasurementItems()
  167. {
  168. if (this.measurementItems == null)
  169. {
  170. this.measurementItems = new Hashtable();
  171. this.measurementItems.Add(MeasurementUnit.Pixel, true);
  172. this.measurementItems.Add(MeasurementUnit.Inch, true);
  173. this.measurementItems.Add(MeasurementUnit.Mil, true);
  174. this.measurementItems.Add(MeasurementUnit.Centimeter, true);
  175. this.measurementItems.Add(MeasurementUnit.Millimeter, true);
  176. this.measurementItems.Add(MeasurementUnit.Micron, true);
  177. this.measurementItems.Add(MeasurementUnit.Nano, true);
  178. }
  179. }
  180. private void ReloadItems()
  181. {
  182. string suffix;
  183. switch (this.unitsDisplayType)
  184. {
  185. case UnitsDisplayType.Plural:
  186. suffix = ".Plural";
  187. break;
  188. case UnitsDisplayType.Singular:
  189. suffix = string.Empty;
  190. break;
  191. case UnitsDisplayType.Ratio:
  192. suffix = ".Ratio";
  193. break;
  194. default:
  195. throw new InvalidEnumArgumentException("UnitsDisplayType");
  196. }
  197. InitMeasurementItems();
  198. MeasurementUnit oldUnits;
  199. if (this.unitsToString == null)
  200. {
  201. oldUnits = MeasurementUnit.Pixel;
  202. }
  203. else
  204. {
  205. oldUnits = this.Units;
  206. }
  207. ComboBox.Items.Clear();
  208. string pixelsString = PdnResources.GetString("MeasurementUnit.Pixel" + suffix);
  209. string inchesString = PdnResources.GetString("MeasurementUnit.Inch" + suffix);
  210. string milsString = PdnResources.GetString("MeasurementUnit.Mil" + suffix);
  211. string centimetersString = PdnResources.GetString("MeasurementUnit.Centimeter" + suffix);
  212. string millimeterString = PdnResources.GetString("Menu.Mm.text") + suffix;
  213. string micronString = PdnResources.GetString("Menu.Micron.text") + suffix;
  214. string nanoString = PdnResources.GetString("Menu.nanometer.text") + suffix;
  215. if (lowercase)
  216. {
  217. pixelsString = pixelsString.ToLower();
  218. inchesString = inchesString.ToLower();
  219. milsString = milsString.ToLower();
  220. centimetersString = centimetersString.ToLower();
  221. millimeterString = millimeterString.ToLower();
  222. micronString = micronString.ToLower();
  223. nanoString = nanoString.ToLower();
  224. }
  225. this.unitsToString = new Hashtable();
  226. this.unitsToString.Add(MeasurementUnit.Pixel, pixelsString);
  227. this.unitsToString.Add(MeasurementUnit.Inch, inchesString);
  228. this.unitsToString.Add(MeasurementUnit.Mil, milsString);
  229. this.unitsToString.Add(MeasurementUnit.Centimeter, centimetersString);
  230. this.unitsToString.Add(MeasurementUnit.Millimeter, millimeterString);
  231. this.unitsToString.Add(MeasurementUnit.Micron, micronString);
  232. this.unitsToString.Add(MeasurementUnit.Nano, nanoString);
  233. this.stringToUnits = new Hashtable();
  234. if ((bool)this.measurementItems[MeasurementUnit.Pixel])
  235. {
  236. this.stringToUnits.Add(pixelsString, MeasurementUnit.Pixel);
  237. ComboBox.Items.Add(pixelsString);
  238. }
  239. if ((bool)this.measurementItems[MeasurementUnit.Inch])
  240. {
  241. this.stringToUnits.Add(inchesString, MeasurementUnit.Inch);
  242. ComboBox.Items.Add(inchesString);
  243. }
  244. if ((bool)this.measurementItems[MeasurementUnit.Mil])
  245. {
  246. this.stringToUnits.Add(milsString, MeasurementUnit.Mil);
  247. ComboBox.Items.Add(milsString);
  248. }
  249. if ((bool)this.measurementItems[MeasurementUnit.Centimeter])
  250. {
  251. this.stringToUnits.Add(centimetersString, MeasurementUnit.Centimeter);
  252. ComboBox.Items.Add(centimetersString);
  253. }
  254. if ((bool)this.measurementItems[MeasurementUnit.Millimeter])
  255. {
  256. this.stringToUnits.Add(millimeterString, MeasurementUnit.Millimeter);
  257. ComboBox.Items.Add(millimeterString);
  258. }
  259. if ((bool)this.measurementItems[MeasurementUnit.Micron])
  260. {
  261. this.stringToUnits.Add(micronString, MeasurementUnit.Micron);
  262. ComboBox.Items.Add(micronString);
  263. }
  264. if ((bool)this.measurementItems[MeasurementUnit.Nano])
  265. {
  266. this.stringToUnits.Add(nanoString, MeasurementUnit.Nano);
  267. ComboBox.Items.Add(nanoString);
  268. }
  269. if (!(bool)this.measurementItems[oldUnits])
  270. {
  271. if (ComboBox.Items.Count == 0)
  272. {
  273. ComboBox.SelectedItem = null;
  274. }
  275. else
  276. {
  277. ComboBox.SelectedIndex = 0;
  278. }
  279. }
  280. else
  281. {
  282. this.Units = oldUnits;
  283. }
  284. }
  285. public event EventHandler UnitsChanged;
  286. private void OnUnitsChanged()
  287. {
  288. if (UnitsChanged != null)
  289. {
  290. UnitsChanged(this, EventArgs.Empty);
  291. }
  292. }
  293. private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
  294. {
  295. this.OnUnitsChanged();
  296. }
  297. }
  298. }