/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewCellStyle.cs

https://github.com/iainlane/mono · C# · 388 lines · 326 code · 36 blank · 26 comment · 78 complexity · dc8821a44ee4bdbe9c4d6c20b4f3d93b MD5 · raw file

  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) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Author:
  23. // Pedro Martínez Juliá <pedromj@gmail.com>
  24. //
  25. using System.Drawing;
  26. using System.ComponentModel;
  27. using System.Globalization;
  28. namespace System.Windows.Forms {
  29. [Editor ("System.Windows.Forms.Design.DataGridViewCellStyleEditor, " + Consts.AssemblySystem_Design,
  30. "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  31. [TypeConverter (typeof (DataGridViewCellStyleConverter))]
  32. public class DataGridViewCellStyle : ICloneable {
  33. private DataGridViewContentAlignment alignment;
  34. private Color backColor;
  35. private object dataSourceNullValue;
  36. private Font font;
  37. private Color foreColor;
  38. private string format;
  39. private IFormatProvider formatProvider;
  40. private object nullValue;
  41. private Padding padding;
  42. private Color selectionBackColor;
  43. private Color selectionForeColor;
  44. private object tag;
  45. private DataGridViewTriState wrapMode;
  46. public DataGridViewCellStyle ()
  47. {
  48. alignment = DataGridViewContentAlignment.NotSet;
  49. backColor = Color.Empty;
  50. dataSourceNullValue = DBNull.Value;
  51. font = null;
  52. foreColor = Color.Empty;
  53. format = String.Empty;
  54. nullValue = string.Empty;
  55. padding = Padding.Empty;
  56. selectionBackColor = Color.Empty;
  57. selectionForeColor = Color.Empty;
  58. tag = null;
  59. wrapMode = DataGridViewTriState.NotSet;
  60. }
  61. public DataGridViewCellStyle (DataGridViewCellStyle dataGridViewCellStyle)
  62. {
  63. this.alignment = dataGridViewCellStyle.alignment;
  64. this.backColor = dataGridViewCellStyle.backColor;
  65. this.dataSourceNullValue = dataGridViewCellStyle.dataSourceNullValue;
  66. this.font = dataGridViewCellStyle.font;
  67. this.foreColor = dataGridViewCellStyle.foreColor;
  68. this.format = dataGridViewCellStyle.format;
  69. this.formatProvider = dataGridViewCellStyle.formatProvider;
  70. this.nullValue = dataGridViewCellStyle.nullValue;
  71. this.padding = dataGridViewCellStyle.padding;
  72. this.selectionBackColor = dataGridViewCellStyle.selectionBackColor;
  73. this.selectionForeColor = dataGridViewCellStyle.selectionForeColor;
  74. this.tag = dataGridViewCellStyle.tag;
  75. this.wrapMode = dataGridViewCellStyle.wrapMode;
  76. }
  77. [DefaultValue (DataGridViewContentAlignment.NotSet)]
  78. public DataGridViewContentAlignment Alignment {
  79. get { return alignment; }
  80. set {
  81. if (!Enum.IsDefined(typeof(DataGridViewContentAlignment), value)) {
  82. throw new InvalidEnumArgumentException("Value is not valid DataGridViewContentAlignment.");
  83. }
  84. if (alignment != value) {
  85. alignment = value;
  86. OnStyleChanged();
  87. }
  88. }
  89. }
  90. public Color BackColor {
  91. get { return backColor; }
  92. set {
  93. if (backColor != value) {
  94. backColor = value;
  95. OnStyleChanged();
  96. }
  97. }
  98. }
  99. [Browsable (false)]
  100. [EditorBrowsable (EditorBrowsableState.Advanced)]
  101. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  102. public object DataSourceNullValue {
  103. get { return dataSourceNullValue; }
  104. set {
  105. if (dataSourceNullValue != value) {
  106. dataSourceNullValue = value;
  107. OnStyleChanged();
  108. }
  109. }
  110. }
  111. public Font Font {
  112. get { return font; }
  113. set {
  114. if (font != value) {
  115. font = value;
  116. OnStyleChanged();
  117. }
  118. }
  119. }
  120. public Color ForeColor {
  121. get { return foreColor; }
  122. set {
  123. if (foreColor != value) {
  124. foreColor = value;
  125. OnStyleChanged();
  126. }
  127. }
  128. }
  129. [DefaultValue ("")]
  130. [Editor ("System.Windows.Forms.Design.FormatStringEditor, " + Consts.AssemblySystem_Design,
  131. "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  132. [EditorBrowsable (EditorBrowsableState.Advanced)]
  133. public string Format {
  134. get { return format; }
  135. set {
  136. if (format != value) {
  137. format = value;
  138. OnStyleChanged();
  139. }
  140. }
  141. }
  142. [Browsable (false)]
  143. [EditorBrowsable (EditorBrowsableState.Advanced)]
  144. public IFormatProvider FormatProvider {
  145. get {
  146. if (formatProvider == null)
  147. return CultureInfo.CurrentCulture;
  148. return formatProvider;
  149. }
  150. set {
  151. if (formatProvider != value) {
  152. formatProvider = value;
  153. OnStyleChanged();
  154. }
  155. }
  156. }
  157. [Browsable (false)]
  158. [EditorBrowsable (EditorBrowsableState.Advanced)]
  159. public bool IsDataSourceNullValueDefault {
  160. get { return dataSourceNullValue != null; }
  161. }
  162. [Browsable (false)]
  163. [EditorBrowsable (EditorBrowsableState.Advanced)]
  164. public bool IsFormatProviderDefault {
  165. get { return formatProvider == null; }
  166. }
  167. [Browsable (false)]
  168. [EditorBrowsable (EditorBrowsableState.Advanced)]
  169. public bool IsNullValueDefault {
  170. get {
  171. if (nullValue is string) {
  172. return (string) nullValue == string.Empty;
  173. }
  174. return false;
  175. }
  176. }
  177. [DefaultValue ("")]
  178. [TypeConverter (typeof (StringConverter))]
  179. public object NullValue {
  180. get { return nullValue; }
  181. set {
  182. if (nullValue != value) {
  183. nullValue = value;
  184. OnStyleChanged();
  185. }
  186. }
  187. }
  188. public Padding Padding {
  189. get { return padding; }
  190. set {
  191. if (padding != value) {
  192. padding = value;
  193. OnStyleChanged();
  194. }
  195. }
  196. }
  197. public Color SelectionBackColor {
  198. get { return selectionBackColor; }
  199. set {
  200. if (selectionBackColor != value) {
  201. selectionBackColor = value;
  202. OnStyleChanged();
  203. }
  204. }
  205. }
  206. public Color SelectionForeColor {
  207. get { return selectionForeColor; }
  208. set {
  209. if (selectionForeColor != value) {
  210. selectionForeColor = value;
  211. OnStyleChanged();
  212. }
  213. }
  214. }
  215. [Browsable (false)]
  216. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  217. public object Tag {
  218. get { return tag; }
  219. set {
  220. if (tag != value) {
  221. tag = value;
  222. OnStyleChanged();
  223. }
  224. }
  225. }
  226. [DefaultValue (DataGridViewTriState.NotSet)]
  227. public DataGridViewTriState WrapMode {
  228. get { return wrapMode; }
  229. set {
  230. if (!Enum.IsDefined(typeof(DataGridViewTriState), value)) {
  231. throw new InvalidEnumArgumentException("Value is not valid DataGridViewTriState.");
  232. }
  233. if (wrapMode != value) {
  234. wrapMode = value;
  235. OnStyleChanged();
  236. }
  237. }
  238. }
  239. public virtual void ApplyStyle (DataGridViewCellStyle dataGridViewCellStyle)
  240. {
  241. // We should only apply the new style if it is 'set'.
  242. if (dataGridViewCellStyle.alignment != DataGridViewContentAlignment.NotSet)
  243. this.alignment = dataGridViewCellStyle.alignment;
  244. if (dataGridViewCellStyle.backColor != Color.Empty)
  245. this.backColor = dataGridViewCellStyle.backColor;
  246. if (dataGridViewCellStyle.dataSourceNullValue != DBNull.Value)
  247. this.dataSourceNullValue = dataGridViewCellStyle.dataSourceNullValue;
  248. if (dataGridViewCellStyle.font != null)
  249. this.font = dataGridViewCellStyle.font;
  250. if (dataGridViewCellStyle.foreColor != Color.Empty)
  251. this.foreColor = dataGridViewCellStyle.foreColor;
  252. if (dataGridViewCellStyle.format != string.Empty)
  253. this.format = dataGridViewCellStyle.format;
  254. if (dataGridViewCellStyle.formatProvider != null)
  255. this.formatProvider = dataGridViewCellStyle.formatProvider;
  256. if (dataGridViewCellStyle.nullValue != null)
  257. this.nullValue = dataGridViewCellStyle.nullValue;
  258. if (dataGridViewCellStyle.padding != Padding.Empty)
  259. this.padding = dataGridViewCellStyle.padding;
  260. if (dataGridViewCellStyle.selectionBackColor != Color.Empty)
  261. this.selectionBackColor = dataGridViewCellStyle.selectionBackColor;
  262. if (dataGridViewCellStyle.selectionForeColor != Color.Empty)
  263. this.selectionForeColor = dataGridViewCellStyle.selectionForeColor;
  264. if (dataGridViewCellStyle.tag != null)
  265. this.tag = dataGridViewCellStyle.tag;
  266. if (dataGridViewCellStyle.wrapMode != DataGridViewTriState.NotSet)
  267. this.wrapMode = dataGridViewCellStyle.wrapMode;
  268. }
  269. object ICloneable.Clone ()
  270. {
  271. return Clone ();
  272. }
  273. public virtual DataGridViewCellStyle Clone ()
  274. {
  275. return new DataGridViewCellStyle(this);
  276. }
  277. public override bool Equals (object o)
  278. {
  279. if (o is DataGridViewCellStyle) {
  280. DataGridViewCellStyle o_aux = (DataGridViewCellStyle) o;
  281. return this.alignment == o_aux.alignment &&
  282. this.backColor == o_aux.backColor &&
  283. this.dataSourceNullValue == o_aux.dataSourceNullValue &&
  284. this.font == o_aux.font &&
  285. this.foreColor == o_aux.foreColor &&
  286. this.format == o_aux.format &&
  287. this.formatProvider == o_aux.formatProvider &&
  288. this.nullValue == o_aux.nullValue &&
  289. this.padding == o_aux.padding &&
  290. this.selectionBackColor == o_aux.selectionBackColor &&
  291. this.selectionForeColor == o_aux.selectionForeColor &&
  292. this.tag == o_aux.tag &&
  293. this.wrapMode == o_aux.wrapMode;
  294. }
  295. return false;
  296. }
  297. public override int GetHashCode ()
  298. {
  299. return base.GetHashCode();
  300. }
  301. public override string ToString ()
  302. {
  303. /////////////////////////////////////// COMPROBAR EN Windows ////////////////////////////////
  304. return "";
  305. }
  306. internal event EventHandler StyleChanged;
  307. internal void OnStyleChanged ()
  308. {
  309. if (StyleChanged != null) {
  310. StyleChanged(this, EventArgs.Empty);
  311. }
  312. }
  313. internal StringFormat SetAlignment (StringFormat format)
  314. {
  315. switch (Alignment) {
  316. case DataGridViewContentAlignment.BottomCenter:
  317. case DataGridViewContentAlignment.BottomLeft:
  318. case DataGridViewContentAlignment.BottomRight:
  319. format.LineAlignment = StringAlignment.Near;
  320. break;
  321. case DataGridViewContentAlignment.MiddleCenter:
  322. case DataGridViewContentAlignment.MiddleLeft:
  323. case DataGridViewContentAlignment.MiddleRight:
  324. format.LineAlignment = StringAlignment.Center;
  325. break;
  326. case DataGridViewContentAlignment.TopCenter:
  327. case DataGridViewContentAlignment.TopLeft:
  328. case DataGridViewContentAlignment.TopRight:
  329. format.LineAlignment = StringAlignment.Far;
  330. break;
  331. }
  332. switch (Alignment) {
  333. case DataGridViewContentAlignment.BottomCenter:
  334. case DataGridViewContentAlignment.MiddleCenter:
  335. case DataGridViewContentAlignment.TopCenter:
  336. format.Alignment = StringAlignment.Center;
  337. break;
  338. case DataGridViewContentAlignment.BottomLeft:
  339. case DataGridViewContentAlignment.MiddleLeft:
  340. case DataGridViewContentAlignment.TopLeft:
  341. format.Alignment = StringAlignment.Near;
  342. break;
  343. case DataGridViewContentAlignment.BottomRight:
  344. case DataGridViewContentAlignment.MiddleRight:
  345. case DataGridViewContentAlignment.TopRight:
  346. format.Alignment = StringAlignment.Far;
  347. break;
  348. }
  349. return format;
  350. }
  351. }
  352. }