ColumnHeader.cs 5.9 KB

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