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