Mark tests as not working under TARGET_JVM
[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         // XXX [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.EllipsisWord;
151                         // text is wrappable only in LargeIcon and SmallIcon views
152                         format.FormatFlags = StringFormatFlags.NoWrap;
153
154                         if (owner != null)
155                                 column_rect.Height = owner.Font.Height + 8;
156                         else
157                                 column_rect.Height = ThemeEngine.Current.DefaultFont.Height + 8;
158
159                         if (width >= 0)
160                                 column_rect.Width = width;
161                         else if (Index != -1) {
162                                 column_rect.Width = owner.GetChildColumnSize (Index).Width;
163                                 width = column_rect.Width;
164                         } else
165                                 column_rect.Width = 0;
166                 }
167
168                 internal void SetListView (ListView list_view)
169                 {
170                         owner = list_view;
171                 }
172
173                 #endregion      // Private Internal Methods Properties
174
175                 #region Public Instance Properties
176
177 #if NET_2_0
178                 [Localizable (true)]
179                 [RefreshProperties (RefreshProperties.Repaint)]
180                 public int DisplayIndex {
181                         get {
182                                 if (owner == null)
183                                         return display_index;
184                                         
185                                 return owner.GetReorderedColumnIndex (this);
186                         }
187                         set {
188                                 if (owner == null) {
189                                         display_index = value;
190                                         return;
191                                 }
192                                 if (value < 0 || value >= owner.Columns.Count)
193                                         throw new ArgumentOutOfRangeException ("DisplayIndex");
194
195                                 owner.ReorderColumn (this, value, false);
196                         }
197                 }
198
199                 [DefaultValue (-1)]
200                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
201                 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
202                          "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
203                 [RefreshProperties (RefreshProperties.Repaint)]
204                 [TypeConverter (typeof (ImageIndexConverter))]
205                 public int ImageIndex {
206                         get {
207                                 return image_index;
208                         }
209                         set {
210                                 if (value < -1)
211                                         throw new ArgumentOutOfRangeException ("ImageIndex");
212
213                                 image_index = value;
214                                 image_key = String.Empty;
215                         }
216                 }
217
218                 [DefaultValue ("")]
219                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
220                 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
221                          "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
222                 [RefreshProperties (RefreshProperties.Repaint)]
223                 // XXX [TypeConverter (typeof (ImageKeyConverter))]
224                 public string ImageKey {
225                         get {
226                                 return image_key;
227                         }
228                         set {
229                                 image_key = value == null ? String.Empty : value;
230                                 image_index = -1;
231                         }
232                 }
233
234                 [Browsable (false)]
235                 public ImageList ImageList {
236                         get {
237                                 if (owner == null)
238                                         return null;
239
240                                 return owner.SmallImageList;
241                         }
242                 }
243 #endif
244
245                 [Browsable (false)]
246                 public int Index {
247                         get {
248                                 if (owner != null && owner.Columns != null
249                                     && owner.Columns.Contains (this)) {
250                                         return owner.Columns.IndexOf (this);
251                                 }
252                                 return -1;
253                         }
254                 }
255
256                 [Browsable (false)]
257                 public ListView ListView {
258                         get { return owner; }
259                 }
260
261 #if NET_2_0
262                 [Browsable (false)]
263                 public string Name {
264                         get {
265                                 return name;
266                         }
267                         set {
268                                 name = value == null ? String.Empty : value;
269                         }
270                 }
271
272                 [DefaultValue (null)]
273                 [BindableAttribute (true)]
274                 [LocalizableAttribute (false)]
275                 [TypeConverter (typeof (StringConverter))]
276                 public object Tag {
277                         get {
278                                 return tag;
279                         }
280                         set {
281                                 tag = value;
282                         }
283                 }
284 #endif
285
286                 [Localizable (true)]
287                 public string Text {
288                         get { return text; }
289                         set {
290                                 text = value;
291                                 if (owner != null)
292                                         owner.Redraw (true);
293                         }
294                 }
295
296                 [DefaultValue (HorizontalAlignment.Left)]
297                 [Localizable (true)]
298                 public HorizontalAlignment TextAlign {
299                         get { return text_alignment; }
300                         set {
301                                 text_alignment = value;
302                                 if (owner != null)
303                                         owner.Redraw (true);
304                         }
305                 }
306
307                 [DefaultValue (60)]
308                 [Localizable (true)]
309                 public int Width {
310                         get { return width; }
311                         set {
312                                 width = value;
313                                 if (owner != null)
314                                         owner.Redraw (true);
315                         }
316                 }
317                 #endregion // Public Instance Properties
318
319                 #region Public Methods
320 #if NET_2_0
321                 public void AutoResize (ColumnHeaderAutoResizeStyle headerAutoResize)
322                 {
323                         switch (headerAutoResize) {
324                                 case ColumnHeaderAutoResizeStyle.None:
325                                         break;
326                                 case ColumnHeaderAutoResizeStyle.ColumnContent:
327                                         Width = -1;
328                                         break;
329                                 case ColumnHeaderAutoResizeStyle.HeaderSize:
330                                         Width = -2;
331                                         break;
332                                 default:
333                                         throw new InvalidEnumArgumentException ("headerAutoResize", (int) headerAutoResize,
334                                                         typeof (ColumnHeaderAutoResizeStyle));
335                         }
336                 }
337 #endif
338
339                 public object Clone ()
340                 {
341                         ColumnHeader columnHeader = new ColumnHeader ();
342                         columnHeader.text = text;
343                         columnHeader.text_alignment = text_alignment;
344                         columnHeader.width = width;
345                         columnHeader.owner = owner;
346                         columnHeader.format = (StringFormat) Format.Clone ();
347                         columnHeader.column_rect = Rectangle.Empty;
348                         return columnHeader;
349                 }
350
351                 public override string ToString ()
352                 {
353                         return string.Format ("ColumnHeader: Text: {0}", text);
354                 }
355                 #endregion // Public Methods
356
357                 #region Protected Methods
358                 protected override void Dispose (bool disposing)
359                 {
360                         base.Dispose (disposing);
361                 }
362                 #endregion // Protected Methods
363         }
364 }