* ColumnHeader.cs: Remove the double linear search in the Index
[mcs.git] / class / Managed.Windows.Forms / System.Windows.Forms / ColumnHeader.cs
blob5b7f9ea8b6bf4ac48b0dbde2417b71a1f8bce7c0
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.
20 // Copyright (c) 2004 Novell, Inc. (http://www.novell.com)
22 // Author:
23 // Ravindra (rkumar@novell.com)
27 // COMPLETE
30 using System.ComponentModel;
31 using System.Drawing;
33 namespace System.Windows.Forms
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
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;
56 // internal variables
57 Rectangle column_rect = Rectangle.Empty;
58 bool pressed = false;
59 ListView owner;
60 #endregion // Instance Variables
62 #region Internal Constructor
63 internal ColumnHeader (ListView owner, string text,
64 HorizontalAlignment alignment, int width)
66 this.owner = owner;
67 this.text = text;
68 this.width = width;
69 this.text_alignment = alignment;
70 CalcColumnHeader ();
73 #if NET_2_0
74 internal ColumnHeader (string key, string text, int width, HorizontalAlignment textAlign)
76 Name = key;
77 Text = text;
78 this.width = width;
79 this.text_alignment = textAlign;
80 CalcColumnHeader ();
82 #endif
83 #endregion // Internal Constructor
85 #region Public Constructors
86 public ColumnHeader () { }
88 #if NET_2_0
89 public ColumnHeader (int imageIndex)
91 ImageIndex = imageIndex;
94 public ColumnHeader (string imageKey)
96 ImageKey = imageKey;
98 #endif
99 #endregion // Public Constructors
101 #region Private Internal Methods Properties
102 internal bool Pressed {
103 get { return pressed; }
104 set { pressed = value; }
107 internal int X {
108 get { return column_rect.X; }
109 set { column_rect.X = value; }
112 internal int Y {
113 get { return column_rect.Y; }
114 set { column_rect.Y = value; }
117 internal int Wd {
118 get { return column_rect.Width; }
119 set { column_rect.Width = value; }
122 internal int Ht {
123 get { return column_rect.Height; }
124 set { column_rect.Height = value; }
127 internal Rectangle Rect {
128 get { return column_rect; }
129 set { column_rect = value; }
132 internal StringFormat Format {
133 get { return format; }
136 internal int InternalDisplayIndex {
137 get { return display_index; }
138 set { display_index = value; }
141 internal void CalcColumnHeader ()
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;
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);
159 column_rect.Width = 0;
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;
168 column_rect.Width = owner.GetChildColumnSize (Index).Width;
169 width = column_rect.Width;
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;
177 column_rect.Width = width;
182 internal void SetListView (ListView list_view)
184 owner = list_view;
187 #endregion // Private Internal Methods Properties
189 #region Public Instance Properties
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;
199 return owner.GetReorderedColumnIndex (this);
201 set {
202 if (owner == null) {
203 display_index = value;
204 return;
206 if (value < 0 || value >= owner.Columns.Count)
207 throw new ArgumentOutOfRangeException ("DisplayIndex");
209 owner.ReorderColumn (this, value, false);
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;
223 set {
224 if (value < -1)
225 throw new ArgumentOutOfRangeException ("ImageIndex");
227 image_index = value;
228 image_key = String.Empty;
230 if (owner != null)
231 owner.header_control.Invalidate ();
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;
245 set {
246 image_key = value == null ? String.Empty : value;
247 image_index = -1;
249 if (owner != null)
250 owner.header_control.Invalidate ();
254 [Browsable (false)]
255 public ImageList ImageList {
256 get {
257 if (owner == null)
258 return null;
260 return owner.SmallImageList;
263 #endif
265 [Browsable (false)]
266 public int Index {
267 get {
268 if (owner != null)
269 return owner.Columns.IndexOf (this);
271 return -1;
275 [Browsable (false)]
276 public ListView ListView {
277 get { return owner; }
280 #if NET_2_0
281 [Browsable (false)]
282 public string Name {
283 get {
284 return name;
286 set {
287 name = value == null ? String.Empty : value;
291 [DefaultValue (null)]
292 [BindableAttribute (true)]
293 [LocalizableAttribute (false)]
294 [TypeConverter (typeof (StringConverter))]
295 public object Tag {
296 get {
297 return tag;
299 set {
300 tag = value;
303 #endif
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);
314 #if NET_2_0
315 // UIA Framework: Raising Value changed event
316 OnUIATextChanged ();
317 #endif
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);
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);
347 #endregion // Public Instance Properties
349 #region Public Methods
350 #if NET_2_0
351 public void AutoResize (ColumnHeaderAutoResizeStyle headerAutoResize)
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));
367 #endif
369 public object Clone ()
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;
381 public override string ToString ()
383 return string.Format ("ColumnHeader: Text: {0}", text);
385 #endregion // Public Methods
387 #region Protected Methods
388 protected override void Dispose (bool disposing)
390 base.Dispose (disposing);
392 #endregion // Protected Methods
394 #if NET_2_0
396 #region UIA Framework: Methods, Properties and Events
398 static object UIATextChangedEvent = new object ();
400 internal event EventHandler UIATextChanged {
401 add { Events.AddHandler (UIATextChangedEvent, value); }
402 remove { Events.RemoveHandler (UIATextChangedEvent, value); }
405 private void OnUIATextChanged ()
407 EventHandler eh = (EventHandler) Events [UIATextChangedEvent];
408 if (eh != null)
409 eh (this, EventArgs.Empty);
412 #endregion
414 #endif