ColumnHeader.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // $Revision: 1.5 $
  26. // $Modtime: $
  27. // $Log: ColumnHeader.cs,v $
  28. // Revision 1.5 2004/11/04 11:29:38 ravindra
  29. // - Changed default value signatures (prefixed all with ListView).
  30. // - Fixed/implemented layout LargeIcon, SmallIcon and List views for ListView.
  31. // - Fixed calculations for ListViewItem and implemented Clone() method.
  32. //
  33. // Revision 1.4 2004/10/26 09:55:48 ravindra
  34. // Some formatting for my last checkins.
  35. //
  36. // Revision 1.3 2004/10/26 09:33:00 ravindra
  37. // Added some internal members and calculations for ColumnHeader.
  38. //
  39. // Revision 1.2 2004/10/15 15:06:44 ravindra
  40. // Flushing some formatting changes.
  41. //
  42. // Revision 1.1 2004/09/30 13:25:33 ravindra
  43. // Supporting class for ListView control.
  44. //
  45. //
  46. // COMPLETE
  47. //
  48. using System.ComponentModel;
  49. using System.Drawing;
  50. namespace System.Windows.Forms
  51. {
  52. [DefaultProperty ("Text")]
  53. [DesignTimeVisible (false)]
  54. [ToolboxItem (false)]
  55. public class ColumnHeader : Component, ICloneable
  56. {
  57. #region Instance Variables
  58. private StringFormat format;
  59. private string text = "ColumnHeader";
  60. private HorizontalAlignment text_alignment = HorizontalAlignment.Left;
  61. private int width = ThemeEngine.Current.ListViewDefaultColumnWidth;
  62. // internal variables
  63. internal Rectangle column_rect = Rectangle.Empty;
  64. internal bool pressed = false;
  65. internal ListView owner;
  66. #endregion // Instance Variables
  67. #region Internal Constructor
  68. internal ColumnHeader (ListView owner, string text,
  69. HorizontalAlignment alignment, int width)
  70. {
  71. this.owner = owner;
  72. this.text = text;
  73. this.width = width;
  74. this.text_alignment = alignment;
  75. CalcColumnHeader ();
  76. }
  77. #endregion // Internal Constructor
  78. #region Public Constructors
  79. public ColumnHeader () { }
  80. #endregion // Public Constructors
  81. #region Private Internal Methods Properties
  82. // Since this class inherits from MarshalByRef,
  83. // we can't do ColumnHeader.column_rect.XXX. Hence,
  84. // we have some of the following properties to work around CS0197.
  85. internal bool Pressed {
  86. get { return this.pressed; }
  87. }
  88. internal int X {
  89. get { return this.column_rect.X; }
  90. set { this.column_rect.X = value; }
  91. }
  92. internal int Y {
  93. get { return this.column_rect.Y; }
  94. set { this.column_rect.Y = value; }
  95. }
  96. internal int Wd {
  97. get { return this.column_rect.Width; }
  98. set { this.column_rect.Width = value; }
  99. }
  100. internal int Ht {
  101. get { return this.column_rect.Height; }
  102. set { this.column_rect.Height = value; }
  103. }
  104. internal Rectangle Rect {
  105. get { return this.column_rect; }
  106. }
  107. internal StringFormat Format {
  108. get { return this.format; }
  109. }
  110. internal void CalcColumnHeader ()
  111. {
  112. format = new StringFormat ();
  113. if (text_alignment == HorizontalAlignment.Center)
  114. format.Alignment = StringAlignment.Center;
  115. else if (text_alignment == HorizontalAlignment.Right)
  116. format.Alignment = StringAlignment.Far;
  117. else
  118. format.Alignment = StringAlignment.Near;
  119. format.LineAlignment = StringAlignment.Center;
  120. format.Trimming = StringTrimming.EllipsisWord;
  121. // text is wrappable only in LargeIcon and SmallIcon views
  122. format.FormatFlags = StringFormatFlags.NoWrap;
  123. if (width >= 0) {
  124. this.column_rect.Width = width;
  125. if (owner != null)
  126. this.column_rect.Height = owner.Font.Height;
  127. else
  128. this.column_rect.Height = ThemeEngine.Current.DefaultFont.Height;
  129. }
  130. else if (this.Index != -1)
  131. this.column_rect.Size = owner.GetChildColumnSize (this.Index);
  132. else
  133. this.column_rect.Size = Size.Empty;
  134. }
  135. #endregion // Private Internal Methods Properties
  136. #region Public Instance Properties
  137. [Browsable (false)]
  138. public int Index {
  139. get {
  140. if (owner != null && owner.Columns != null
  141. && owner.Columns.Contains (this)) {
  142. return owner.Columns.IndexOf (this);
  143. }
  144. return -1;
  145. }
  146. }
  147. [Browsable (false)]
  148. public ListView ListView {
  149. get { return owner; }
  150. }
  151. [Localizable (true)]
  152. public string Text {
  153. get { return text; }
  154. set {
  155. text = value;
  156. if (owner != null)
  157. owner.Redraw (true);
  158. }
  159. }
  160. [DefaultValue (HorizontalAlignment.Left)]
  161. [Localizable (true)]
  162. public HorizontalAlignment TextAlign {
  163. get { return text_alignment; }
  164. set {
  165. text_alignment = value;
  166. if (owner != null)
  167. owner.Redraw (true);
  168. }
  169. }
  170. [DefaultValue (60)]
  171. [Localizable (true)]
  172. public int Width {
  173. get { return width; }
  174. set {
  175. width = value;
  176. if (owner != null)
  177. owner.Redraw (true);
  178. }
  179. }
  180. #endregion // Public Instance Properties
  181. #region Public Methods
  182. public virtual object Clone ()
  183. {
  184. ColumnHeader columnHeader = new ColumnHeader ();
  185. columnHeader.text = text;
  186. columnHeader.text_alignment = text_alignment;
  187. columnHeader.width = width;
  188. columnHeader.owner = owner;
  189. columnHeader.column_rect = Rectangle.Empty;
  190. return columnHeader;
  191. }
  192. public override string ToString ()
  193. {
  194. return string.Format ("ColumnHeader: Text: {0}", text);
  195. }
  196. #endregion // Public Methods
  197. #region Protected Methods
  198. protected override void Dispose (bool disposing)
  199. {
  200. base.Dispose (disposing);
  201. }
  202. #endregion // Protected Methods
  203. }
  204. }