ColumnHeader.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. (http://www.novell.com)
  21. //
  22. // Author:
  23. // Ravindra ([email protected])
  24. //
  25. // COMPLETE
  26. using System.ComponentModel;
  27. using System.Drawing;
  28. namespace System.Windows.Forms
  29. {
  30. [DefaultProperty ("Text")]
  31. [DesignTimeVisible (false)]
  32. [ToolboxItem (false)]
  33. public class ColumnHeader : Component, ICloneable
  34. {
  35. #region Instance Variables
  36. private StringFormat format = new StringFormat ();
  37. private string text = "ColumnHeader";
  38. private HorizontalAlignment text_alignment = HorizontalAlignment.Left;
  39. private int width = ThemeEngine.Current.ListViewDefaultColumnWidth;
  40. #if NET_2_0
  41. private int image_index = -1;
  42. private string image_key = String.Empty;
  43. private string name = String.Empty;
  44. private object tag;
  45. #endif
  46. // internal variables
  47. internal Rectangle column_rect = Rectangle.Empty;
  48. internal bool pressed = false;
  49. internal ListView owner;
  50. #endregion // Instance Variables
  51. #region Internal Constructor
  52. internal ColumnHeader (ListView owner, string text,
  53. HorizontalAlignment alignment, int width)
  54. {
  55. this.owner = owner;
  56. this.text = text;
  57. this.width = width;
  58. this.text_alignment = alignment;
  59. CalcColumnHeader ();
  60. }
  61. #if NET_2_0
  62. internal ColumnHeader (string key, string text, int width, HorizontalAlignment textAlign)
  63. {
  64. Name = key;
  65. Text = text;
  66. this.width = width;
  67. this.text_alignment = textAlign;
  68. CalcColumnHeader ();
  69. }
  70. #endif
  71. #endregion // Internal Constructor
  72. #region Public Constructors
  73. public ColumnHeader () { }
  74. #if NET_2_0
  75. public ColumnHeader (int imageIndex)
  76. {
  77. ImageIndex = imageIndex;
  78. }
  79. public ColumnHeader (string imageKey)
  80. {
  81. ImageKey = imageKey;
  82. }
  83. #endif
  84. #endregion // Public Constructors
  85. #region Private Internal Methods Properties
  86. // Since this class inherits from MarshalByRef,
  87. // we can't do ColumnHeader.column_rect.XXX. Hence,
  88. // we have some of the following properties to work around CS0197.
  89. internal bool Pressed {
  90. get { return this.pressed; }
  91. }
  92. internal int X {
  93. get { return this.column_rect.X; }
  94. set { this.column_rect.X = value; }
  95. }
  96. internal int Y {
  97. get { return this.column_rect.Y; }
  98. set { this.column_rect.Y = value; }
  99. }
  100. internal int Wd {
  101. get { return this.column_rect.Width; }
  102. set { this.column_rect.Width = value; }
  103. }
  104. internal int Ht {
  105. get { return this.column_rect.Height; }
  106. set { this.column_rect.Height = value; }
  107. }
  108. internal Rectangle Rect {
  109. get { return this.column_rect; }
  110. }
  111. internal StringFormat Format {
  112. get { return this.format; }
  113. }
  114. internal void CalcColumnHeader ()
  115. {
  116. if (text_alignment == HorizontalAlignment.Center)
  117. format.Alignment = StringAlignment.Center;
  118. else if (text_alignment == HorizontalAlignment.Right)
  119. format.Alignment = StringAlignment.Far;
  120. else
  121. format.Alignment = StringAlignment.Near;
  122. format.LineAlignment = StringAlignment.Center;
  123. format.Trimming = StringTrimming.EllipsisWord;
  124. // text is wrappable only in LargeIcon and SmallIcon views
  125. format.FormatFlags = StringFormatFlags.NoWrap;
  126. if (width >= 0) {
  127. this.column_rect.Width = width;
  128. if (owner != null)
  129. this.column_rect.Height = owner.Font.Height + 5 ;
  130. else
  131. this.column_rect.Height = ThemeEngine.Current.DefaultFont.Height + 5;
  132. }
  133. else if (this.Index != -1)
  134. this.column_rect.Size = owner.GetChildColumnSize (this.Index);
  135. else
  136. this.column_rect.Size = Size.Empty;
  137. }
  138. #endregion // Private Internal Methods Properties
  139. #region Public Instance Properties
  140. #if NET_2_0
  141. public int ImageIndex {
  142. get {
  143. return image_index;
  144. }
  145. set {
  146. if (value < -1)
  147. throw new ArgumentOutOfRangeException ("value");
  148. image_index = value;
  149. image_key = String.Empty;
  150. }
  151. }
  152. public string ImageKey {
  153. get {
  154. return image_key;
  155. }
  156. set {
  157. image_key = value == null ? String.Empty : value;
  158. image_index = -1;
  159. }
  160. }
  161. public ImageList ImageList {
  162. get {
  163. if (owner == null)
  164. return null;
  165. return owner.SmallImageList;
  166. }
  167. }
  168. #endif
  169. [Browsable (false)]
  170. public int Index {
  171. get {
  172. if (owner != null && owner.Columns != null
  173. && owner.Columns.Contains (this)) {
  174. return owner.Columns.IndexOf (this);
  175. }
  176. return -1;
  177. }
  178. }
  179. [Browsable (false)]
  180. public ListView ListView {
  181. get { return owner; }
  182. }
  183. #if NET_2_0
  184. public string Name {
  185. get {
  186. return name;
  187. }
  188. set {
  189. name = value == null ? String.Empty : value;
  190. }
  191. }
  192. [LocalizableAttribute (false)]
  193. [BindableAttribute (true)]
  194. public object Tag {
  195. get {
  196. return tag;
  197. }
  198. set {
  199. tag = value;
  200. }
  201. }
  202. #endif
  203. [Localizable (true)]
  204. public string Text {
  205. get { return text; }
  206. set {
  207. text = value;
  208. if (owner != null)
  209. owner.Redraw (true);
  210. }
  211. }
  212. [DefaultValue (HorizontalAlignment.Left)]
  213. [Localizable (true)]
  214. public HorizontalAlignment TextAlign {
  215. get { return text_alignment; }
  216. set {
  217. text_alignment = value;
  218. if (owner != null)
  219. owner.Redraw (true);
  220. }
  221. }
  222. [DefaultValue (60)]
  223. [Localizable (true)]
  224. public int Width {
  225. get { return width; }
  226. set {
  227. width = value;
  228. if (owner != null)
  229. owner.Redraw (true);
  230. }
  231. }
  232. #endregion // Public Instance Properties
  233. #region Public Methods
  234. public object Clone ()
  235. {
  236. ColumnHeader columnHeader = new ColumnHeader ();
  237. columnHeader.text = text;
  238. columnHeader.text_alignment = text_alignment;
  239. columnHeader.width = width;
  240. columnHeader.owner = owner;
  241. columnHeader.format = (StringFormat) Format.Clone ();
  242. columnHeader.column_rect = Rectangle.Empty;
  243. return columnHeader;
  244. }
  245. public override string ToString ()
  246. {
  247. return string.Format ("ColumnHeader: Text: {0}", text);
  248. }
  249. #endregion // Public Methods
  250. #region Protected Methods
  251. protected override void Dispose (bool disposing)
  252. {
  253. base.Dispose (disposing);
  254. }
  255. #endregion // Protected Methods
  256. }
  257. }