* X11Keyboard.cs: Detect and use the num lock mask.
[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.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
49 using System.ComponentModel;
50 using System.Drawing;
51
52 namespace System.Windows.Forms
53 {
54         [DefaultProperty ("Text")]
55         [DesignTimeVisible (false)]
56         [ToolboxItem (false)]
57         public class ColumnHeader : Component, ICloneable
58         {
59                 #region Instance Variables
60                 private StringFormat format;
61                 private string text = "ColumnHeader";
62                 private HorizontalAlignment text_alignment = HorizontalAlignment.Left;
63                 private int width = ThemeEngine.Current.ListViewDefaultColumnWidth;
64
65                 // internal variables
66                 internal Rectangle column_rect = Rectangle.Empty;
67                 internal bool pressed = false;
68                 internal ListView owner;
69                 #endregion      // Instance Variables
70
71                 #region Internal Constructor
72                 internal ColumnHeader (ListView owner, string text,
73                                        HorizontalAlignment alignment, int width)
74                 {
75                         this.owner = owner;
76                         this.text = text;
77                         this.width = width;
78                         this.text_alignment = alignment;
79                         CalcColumnHeader ();
80                 }
81                 #endregion      // Internal Constructor
82
83                 #region Public Constructors
84                 public ColumnHeader () { }
85                 #endregion      // Public Constructors
86
87                 #region Private Internal Methods Properties
88                 // Since this class inherits from MarshalByRef,
89                 // we can't do ColumnHeader.column_rect.XXX. Hence,
90                 // we have some of the following properties to work around CS0197.
91                 internal bool Pressed {
92                         get { return this.pressed; }
93                 }
94
95                 internal int X {
96                         get { return this.column_rect.X; }
97                         set { this.column_rect.X = value; }
98                 }
99
100                 internal int Y {
101                         get { return this.column_rect.Y; }
102                         set { this.column_rect.Y = value; }
103                 }
104
105                 internal int Wd {
106                         get { return this.column_rect.Width; }
107                         set { this.column_rect.Width = value; }
108                 }
109
110                 internal int Ht {
111                         get { return this.column_rect.Height; }
112                         set { this.column_rect.Height = value; }
113                 }
114
115                 internal Rectangle Rect {
116                         get { return this.column_rect; }
117                 }
118
119                 internal StringFormat Format {
120                         get { return this.format; }
121                 }
122
123                 internal void CalcColumnHeader ()
124                 {
125                         format = new StringFormat ();
126                         if (text_alignment == HorizontalAlignment.Center)
127                                 format.Alignment = StringAlignment.Center;
128                         else if (text_alignment == HorizontalAlignment.Right)
129                                 format.Alignment = StringAlignment.Far;
130                         else
131                                 format.Alignment = StringAlignment.Near;
132                         format.LineAlignment = StringAlignment.Center;
133                         format.Trimming = StringTrimming.EllipsisWord;
134                         // text is wrappable only in LargeIcon and SmallIcon views
135                         format.FormatFlags = StringFormatFlags.NoWrap;
136
137                         if (width >= 0) {
138                                 this.column_rect.Width = width;
139                                 if (owner != null)
140                                         this.column_rect.Height = owner.Font.Height;
141                                 else
142                                         this.column_rect.Height = ThemeEngine.Current.DefaultFont.Height;
143                         }
144                         else if (this.Index != -1)
145                                 this.column_rect.Size = owner.GetChildColumnSize (this.Index);
146                         else
147                                 this.column_rect.Size = Size.Empty;
148                 }
149                 #endregion      // Private Internal Methods Properties
150
151                 #region Public Instance Properties
152                 [Browsable (false)]
153                 public int Index {
154                         get {
155                                 if (owner != null && owner.Columns != null
156                                     && owner.Columns.Contains (this)) {
157                                         return owner.Columns.IndexOf (this);
158                                 }
159                                 return -1;
160                         }
161                 }
162
163                 [Browsable (false)]
164                 public ListView ListView {
165                         get { return owner; }
166                 }
167
168                 [Localizable (true)]
169                 public string Text {
170                         get { return text; }
171                         set {
172                                 text = value;
173                                 if (owner != null)
174                                         owner.Redraw (true);
175                         }
176                 }
177
178                 [DefaultValue (HorizontalAlignment.Left)]
179                 [Localizable (true)]
180                 public HorizontalAlignment TextAlign {
181                         get { return text_alignment; }
182                         set {
183                                 text_alignment = value;
184                                 if (owner != null)
185                                         owner.Redraw (true);
186                         }
187                 }
188
189                 [DefaultValue (60)]
190                 [Localizable (true)]
191                 public int Width {
192                         get { return width; }
193                         set {
194                                 width = value;
195                                 if (owner != null)
196                                         owner.Redraw (true);
197                         }
198                 }
199                 #endregion // Public Instance Properties
200
201                 #region Public Methods
202                 public virtual object Clone ()
203                 {
204                         ColumnHeader columnHeader = new ColumnHeader ();
205                         columnHeader.text = text;
206                         columnHeader.text_alignment = text_alignment;
207                         columnHeader.width = width;
208                         columnHeader.owner = owner;
209                         columnHeader.column_rect = Rectangle.Empty;
210                         return columnHeader;
211                 }
212
213                 public override string ToString ()
214                 {
215                         return string.Format ("ColumnHeader: Text: {0}", text);
216                 }
217                 #endregion // Public Methods
218
219                 #region Protected Methods
220                 protected override void Dispose (bool disposing)
221                 {
222                         base.Dispose (disposing);
223                 }
224                 #endregion // Protected Methods
225         }
226 }