* TabControl.cs: Show the tooltip depending on the value
[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
26
27 // COMPLETE
28
29
30 using System.ComponentModel;
31 using System.Drawing;
32
33 namespace System.Windows.Forms
34 {
35         [DefaultProperty ("Text")]
36         [DesignTimeVisible (false)]
37         [ToolboxItem (false)]
38 #if NET_2_0
39         [TypeConverter (typeof (ColumnHeaderConverter))]
40 #endif
41         public class ColumnHeader : Component, ICloneable
42         {
43                 #region Instance Variables
44                 private StringFormat format = new StringFormat ();
45                 private string text = "ColumnHeader";
46                 private HorizontalAlignment text_alignment = HorizontalAlignment.Left;
47                 private int width = ThemeEngine.Current.ListViewDefaultColumnWidth;
48 #if NET_2_0
49                 private int image_index = -1;
50                 private string image_key = String.Empty;
51                 private string name = String.Empty;
52                 private object tag;
53 #endif
54                 private int display_index = -1;
55
56                 // internal variables
57                 Rectangle column_rect = Rectangle.Empty;
58                 bool pressed = false;
59                 ListView owner;
60                 #endregion      // Instance Variables
61
62                 #region Internal Constructor
63                 internal ColumnHeader (ListView owner, string text,
64                                        HorizontalAlignment alignment, int width)
65                 {
66                         this.owner = owner;
67                         this.text = text;
68                         this.width = width;
69                         this.text_alignment = alignment;
70                         CalcColumnHeader ();
71                 }
72
73 #if NET_2_0
74                 internal ColumnHeader (string key, string text, int width, HorizontalAlignment textAlign)
75                 {
76                         Name = key;
77                         Text = text;
78                         this.width = width;
79                         this.text_alignment = textAlign;
80                         CalcColumnHeader ();
81                 }
82 #endif
83                 #endregion      // Internal Constructor
84
85                 #region Public Constructors
86                 public ColumnHeader () { }
87
88 #if NET_2_0
89                 public ColumnHeader (int imageIndex)
90                 {
91                         ImageIndex = imageIndex;
92                 }
93
94                 public ColumnHeader (string imageKey)
95                 {
96                         ImageKey = imageKey;
97                 }
98 #endif
99                 #endregion      // Public Constructors
100
101                 #region Private Internal Methods Properties
102                 internal bool Pressed {
103                         get { return pressed; }
104                         set { pressed = value; }
105                 }
106
107                 internal int X {
108                         get { return column_rect.X; }
109                         set { column_rect.X = value; }
110                 }
111
112                 internal int Y {
113                         get { return column_rect.Y; }
114                         set { column_rect.Y = value; }
115                 }
116
117                 internal int Wd {
118                         get { return column_rect.Width; }
119                         set { column_rect.Width = value; }
120                 }
121
122                 internal int Ht {
123                         get { return column_rect.Height; }
124                         set { column_rect.Height = value; }
125                 }
126
127                 internal Rectangle Rect {
128                         get { return column_rect; }
129                         set { column_rect = value; }
130                 }
131
132                 internal StringFormat Format {
133                         get { return format; }
134                 }
135
136                 internal int InternalDisplayIndex {
137                         get { return display_index; }
138                         set { display_index = value; }
139                 }
140
141                 internal void CalcColumnHeader ()
142                 {
143                         if (text_alignment == HorizontalAlignment.Center)
144                                 format.Alignment = StringAlignment.Center;
145                         else if (text_alignment == HorizontalAlignment.Right)
146                                 format.Alignment = StringAlignment.Far;
147                         else
148                                 format.Alignment = StringAlignment.Near;
149                         format.LineAlignment = StringAlignment.Center;
150                         format.Trimming = StringTrimming.EllipsisCharacter;
151                         // text is wrappable only in LargeIcon and SmallIcon views
152                         format.FormatFlags = StringFormatFlags.NoWrap;
153
154                         if (owner != null)
155                                 column_rect.Height = ThemeEngine.Current.ListViewGetHeaderHeight (owner, owner.Font);
156                         else
157                                 column_rect.Height = ThemeEngine.Current.ListViewGetHeaderHeight (null, ThemeEngine.Current.DefaultFont);
158
159                         column_rect.Width = 0;
160
161                         if (width >= 0) // manual width
162                                 column_rect.Width = width;
163                         else if (Index != -1) { // automatic width, either -1 or -2
164                                 // try to expand if we are the last column
165                                 bool expand_to_right = Index == owner.Columns.Count - 1 && width == -2;
166                                 Rectangle visible_area = owner.ClientRectangle;
167
168                                 column_rect.Width = owner.GetChildColumnSize (Index).Width;
169                                 width = column_rect.Width;
170
171                                 // expand only if we have free space to the right
172                                 if (expand_to_right && column_rect.X + column_rect.Width < visible_area.Width) {
173                                         width = visible_area.Width - column_rect.X;
174                                         if (owner.v_scroll.Visible)
175                                                 width -= owner.v_scroll.Width;
176
177                                         column_rect.Width = width;
178                                 }
179                         }
180                 }
181
182                 internal void SetListView (ListView list_view)
183                 {
184                         owner = list_view;
185                 }
186
187                 #endregion      // Private Internal Methods Properties
188
189                 #region Public Instance Properties
190
191 #if NET_2_0
192                 [Localizable (true)]
193                 [RefreshProperties (RefreshProperties.Repaint)]
194                 public int DisplayIndex {
195                         get {
196                                 if (owner == null)
197                                         return display_index;
198                                         
199                                 return owner.GetReorderedColumnIndex (this);
200                         }
201                         set {
202                                 if (owner == null) {
203                                         display_index = value;
204                                         return;
205                                 }
206                                 if (value < 0 || value >= owner.Columns.Count)
207                                         throw new ArgumentOutOfRangeException ("DisplayIndex");
208
209                                 owner.ReorderColumn (this, value, false);
210                         }
211                 }
212
213                 [DefaultValue (-1)]
214                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
215                 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
216                          "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
217                 [RefreshProperties (RefreshProperties.Repaint)]
218                 [TypeConverter (typeof (ImageIndexConverter))]
219                 public int ImageIndex {
220                         get {
221                                 return image_index;
222                         }
223                         set {
224                                 if (value < -1)
225                                         throw new ArgumentOutOfRangeException ("ImageIndex");
226
227                                 image_index = value;
228                                 image_key = String.Empty;
229
230                                 if (owner != null)
231                                         owner.header_control.Invalidate ();
232                         }
233                 }
234
235                 [DefaultValue ("")]
236                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
237                 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
238                          "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
239                 [RefreshProperties (RefreshProperties.Repaint)]
240                 [TypeConverter (typeof (ImageKeyConverter))]
241                 public string ImageKey {
242                         get {
243                                 return image_key;
244                         }
245                         set {
246                                 image_key = value == null ? String.Empty : value;
247                                 image_index = -1;
248
249                                 if (owner != null)
250                                         owner.header_control.Invalidate ();
251                         }
252                 }
253
254                 [Browsable (false)]
255                 public ImageList ImageList {
256                         get {
257                                 if (owner == null)
258                                         return null;
259
260                                 return owner.SmallImageList;
261                         }
262                 }
263 #endif
264
265                 [Browsable (false)]
266                 public int Index {
267                         get {
268                                 if (owner != null)
269                                         return owner.Columns.IndexOf (this);
270
271                                 return -1;
272                         }
273                 }
274
275                 [Browsable (false)]
276                 public ListView ListView {
277                         get { return owner; }
278                 }
279
280 #if NET_2_0
281                 [Browsable (false)]
282                 public string Name {
283                         get {
284                                 return name;
285                         }
286                         set {
287                                 name = value == null ? String.Empty : value;
288                         }
289                 }
290
291                 [DefaultValue (null)]
292                 [BindableAttribute (true)]
293                 [LocalizableAttribute (false)]
294                 [TypeConverter (typeof (StringConverter))]
295                 public object Tag {
296                         get {
297                                 return tag;
298                         }
299                         set {
300                                 tag = value;
301                         }
302                 }
303 #endif
304
305                 [Localizable (true)]
306                 public string Text {
307                         get { return text; }
308                         set {
309                                 if (text != value) {
310                                         text = value;
311                                         if (owner != null)
312                                                 owner.Redraw (true);
313
314 #if NET_2_0
315                                         // UIA Framework: Raising Value changed event
316                                         OnUIATextChanged ();
317 #endif
318                                 }
319                         }
320                 }
321
322                 [DefaultValue (HorizontalAlignment.Left)]
323                 [Localizable (true)]
324                 public HorizontalAlignment TextAlign {
325                         get { return text_alignment; }
326                         set {
327                                 text_alignment = value;
328                                 if (owner != null)
329                                         owner.Redraw (true);
330                         }
331                 }
332
333                 [DefaultValue (60)]
334                 [Localizable (true)]
335                 public int Width {
336                         get { return width; }
337                         set {
338                                 if (width != value) {
339                                         width = value;
340                                         if (owner != null) {
341                                                 owner.Redraw (true);
342                                                 owner.RaiseColumnWidthChanged (this);
343                                         }
344                                 }
345                         }
346                 }
347                 #endregion // Public Instance Properties
348
349                 #region Public Methods
350 #if NET_2_0
351                 public void AutoResize (ColumnHeaderAutoResizeStyle headerAutoResize)
352                 {
353                         switch (headerAutoResize) {
354                                 case ColumnHeaderAutoResizeStyle.None:
355                                         break;
356                                 case ColumnHeaderAutoResizeStyle.ColumnContent:
357                                         Width = -1;
358                                         break;
359                                 case ColumnHeaderAutoResizeStyle.HeaderSize:
360                                         Width = -2;
361                                         break;
362                                 default:
363                                         throw new InvalidEnumArgumentException ("headerAutoResize", (int) headerAutoResize,
364                                                         typeof (ColumnHeaderAutoResizeStyle));
365                         }
366                 }
367 #endif
368
369                 public object Clone ()
370                 {
371                         ColumnHeader columnHeader = new ColumnHeader ();
372                         columnHeader.text = text;
373                         columnHeader.text_alignment = text_alignment;
374                         columnHeader.width = width;
375                         columnHeader.owner = owner;
376                         columnHeader.format = (StringFormat) Format.Clone ();
377                         columnHeader.column_rect = Rectangle.Empty;
378                         return columnHeader;
379                 }
380
381                 public override string ToString ()
382                 {
383                         return string.Format ("ColumnHeader: Text: {0}", text);
384                 }
385                 #endregion // Public Methods
386
387                 #region Protected Methods
388                 protected override void Dispose (bool disposing)
389                 {
390                         base.Dispose (disposing);
391                 }
392                 #endregion // Protected Methods
393
394 #if NET_2_0
395                 
396                 #region UIA Framework: Methods, Properties and Events
397
398                 static object UIATextChangedEvent = new object ();
399
400                 internal event EventHandler UIATextChanged {
401                         add { Events.AddHandler (UIATextChangedEvent, value); }
402                         remove { Events.RemoveHandler (UIATextChangedEvent, value); }
403                 }
404
405                 private void OnUIATextChanged ()
406                 {
407                         EventHandler eh = (EventHandler) Events [UIATextChangedEvent];
408                         if (eh != null)
409                                 eh (this, EventArgs.Empty);
410                 }
411
412                 #endregion
413
414 #endif
415         }
416 }