PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Core/UnitsComboBoxHandler.cs

https://bitbucket.org/tuldok89/openpdn
C# | 289 lines | 236 code | 43 blank | 10 comment | 27 complexity | c4638a61e8f89ee33ff241e35d93fe70 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. using System;
  10. using System.Collections;
  11. using System.ComponentModel;
  12. using System.Windows.Forms;
  13. namespace PaintDotNet
  14. {
  15. public sealed class UnitsComboBoxHandler
  16. : IUnitsComboBox
  17. {
  18. private readonly ComboBox _comboBox;
  19. [Browsable(false)]
  20. public ComboBox ComboBox
  21. {
  22. get
  23. {
  24. return _comboBox;
  25. }
  26. }
  27. public UnitsComboBoxHandler(ComboBox comboBox)
  28. {
  29. _comboBox = comboBox;
  30. _comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
  31. _comboBox.SelectedIndexChanged += ComboBoxSelectedIndexChanged;
  32. ReloadItems();
  33. }
  34. private bool _lowercase = true;
  35. private Hashtable _unitsToString;
  36. private Hashtable _stringToUnits;
  37. // maps from MeasurementUnit->bool for whether that item should be in the list or not
  38. private Hashtable _measurementItems;
  39. private UnitsDisplayType _unitsDisplayType = UnitsDisplayType.Plural;
  40. [DefaultValue(UnitsDisplayType.Plural)]
  41. public UnitsDisplayType UnitsDisplayType
  42. {
  43. get
  44. {
  45. return _unitsDisplayType;
  46. }
  47. set
  48. {
  49. if (_unitsDisplayType == value) return;
  50. _unitsDisplayType = value;
  51. ReloadItems();
  52. }
  53. }
  54. [DefaultValue(true)]
  55. public bool LowercaseStrings
  56. {
  57. get
  58. {
  59. return _lowercase;
  60. }
  61. set
  62. {
  63. if (_lowercase == value) return;
  64. _lowercase = value;
  65. ReloadItems();
  66. }
  67. }
  68. [DefaultValue(MeasurementUnit.Pixel)]
  69. public MeasurementUnit Units
  70. {
  71. get
  72. {
  73. object selected = _stringToUnits[ComboBox.SelectedItem];
  74. return (MeasurementUnit)selected;
  75. }
  76. set
  77. {
  78. object selectMe = _unitsToString[value];
  79. ComboBox.SelectedItem = selectMe;
  80. }
  81. }
  82. [Browsable(false)]
  83. public string UnitsText
  84. {
  85. get
  86. {
  87. if (ComboBox.SelectedItem == null)
  88. {
  89. return string.Empty;
  90. }
  91. return (string)ComboBox.SelectedItem;
  92. }
  93. }
  94. [DefaultValue(true)]
  95. public bool PixelsAvailable
  96. {
  97. get
  98. {
  99. return (bool)_measurementItems[MeasurementUnit.Pixel];
  100. }
  101. set
  102. {
  103. if (value == PixelsAvailable) return;
  104. if (value)
  105. {
  106. AddUnit(MeasurementUnit.Pixel);
  107. }
  108. else
  109. {
  110. if (Units == MeasurementUnit.Pixel)
  111. {
  112. if (InchesAvailable)
  113. {
  114. Units = MeasurementUnit.Inch;
  115. }
  116. else if (CentimetersAvailable)
  117. {
  118. Units = MeasurementUnit.Centimeter;
  119. }
  120. }
  121. RemoveUnit(MeasurementUnit.Pixel);
  122. }
  123. }
  124. }
  125. [DefaultValue(true)]
  126. public bool InchesAvailable
  127. {
  128. get
  129. {
  130. return (bool)_measurementItems[MeasurementUnit.Inch];
  131. }
  132. }
  133. [DefaultValue(true)]
  134. public bool CentimetersAvailable
  135. {
  136. get
  137. {
  138. return (bool)_measurementItems[MeasurementUnit.Centimeter];
  139. }
  140. }
  141. public void RemoveUnit(MeasurementUnit removeMe)
  142. {
  143. InitMeasurementItems();
  144. _measurementItems[removeMe] = false;
  145. ReloadItems();
  146. }
  147. public void AddUnit(MeasurementUnit addMe)
  148. {
  149. InitMeasurementItems();
  150. _measurementItems[addMe] = true;
  151. ReloadItems();
  152. }
  153. private void InitMeasurementItems()
  154. {
  155. if (_measurementItems == null)
  156. {
  157. _measurementItems = new Hashtable
  158. {
  159. {MeasurementUnit.Pixel, true},
  160. {MeasurementUnit.Centimeter, true},
  161. {MeasurementUnit.Inch, true}
  162. };
  163. }
  164. }
  165. private void ReloadItems()
  166. {
  167. string suffix;
  168. switch (_unitsDisplayType)
  169. {
  170. case UnitsDisplayType.Plural:
  171. suffix = ".Plural";
  172. break;
  173. case UnitsDisplayType.Singular:
  174. suffix = string.Empty;
  175. break;
  176. case UnitsDisplayType.Ratio:
  177. suffix = ".Ratio";
  178. break;
  179. default:
  180. throw new InvalidEnumArgumentException("UnitsDisplayType");
  181. }
  182. InitMeasurementItems();
  183. MeasurementUnit oldUnits = _unitsToString == null ? MeasurementUnit.Pixel : Units;
  184. ComboBox.Items.Clear();
  185. string pixelsString = PdnResources.GetString("MeasurementUnit.Pixel" + suffix);
  186. string inchesString = PdnResources.GetString("MeasurementUnit.Inch" + suffix);
  187. string centimetersString = PdnResources.GetString("MeasurementUnit.Centimeter" + suffix);
  188. if (_lowercase)
  189. {
  190. // TODO: we shouldn't really be using ToLower() here, these should be separately localizable strings
  191. pixelsString = pixelsString.ToLower();
  192. inchesString = inchesString.ToLower();
  193. centimetersString = centimetersString.ToLower();
  194. }
  195. _unitsToString = new Hashtable
  196. {
  197. {MeasurementUnit.Pixel, pixelsString},
  198. {MeasurementUnit.Inch, inchesString},
  199. {MeasurementUnit.Centimeter, centimetersString}
  200. };
  201. _stringToUnits = new Hashtable();
  202. if ((bool)_measurementItems[MeasurementUnit.Pixel])
  203. {
  204. _stringToUnits.Add(pixelsString, MeasurementUnit.Pixel);
  205. ComboBox.Items.Add(pixelsString);
  206. }
  207. if ((bool)_measurementItems[MeasurementUnit.Inch])
  208. {
  209. _stringToUnits.Add(inchesString, MeasurementUnit.Inch);
  210. ComboBox.Items.Add(inchesString);
  211. }
  212. if ((bool)_measurementItems[MeasurementUnit.Centimeter])
  213. {
  214. _stringToUnits.Add(centimetersString, MeasurementUnit.Centimeter);
  215. ComboBox.Items.Add(centimetersString);
  216. }
  217. if (!(bool)_measurementItems[oldUnits])
  218. {
  219. if (ComboBox.Items.Count == 0)
  220. {
  221. ComboBox.SelectedItem = null;
  222. }
  223. else
  224. {
  225. ComboBox.SelectedIndex = 0;
  226. }
  227. }
  228. else
  229. {
  230. Units = oldUnits;
  231. }
  232. }
  233. public event EventHandler UnitsChanged;
  234. private void OnUnitsChanged()
  235. {
  236. if (UnitsChanged != null)
  237. {
  238. UnitsChanged(this, EventArgs.Empty);
  239. }
  240. }
  241. private void ComboBoxSelectedIndexChanged(object sender, EventArgs e)
  242. {
  243. OnUnitsChanged();
  244. }
  245. }
  246. }