2006-05-18 Mike Kestner <mkestner@novell.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ListView.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-2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Ravindra Kumar (rkumar@novell.com)
24 //      Jordi Mas i Hernandez, jordi@ximian.com
25 //      Mike Kestner (mkestner@novell.com)
26 //
27 // TODO:
28 //   - Feedback for item activation, change in cursor types as mouse moves.
29 //   - HideSelection
30 //   - LabelEdit
31 //   - Drag and drop
32
33
34 // NOT COMPLETE
35
36
37 using System.Collections;
38 using System.ComponentModel;
39 using System.ComponentModel.Design;
40 using System.Drawing;
41 using System.Runtime.InteropServices;
42 using System.Globalization;
43
44 namespace System.Windows.Forms
45 {
46         [DefaultEvent ("SelectedIndexChanged")]
47         [DefaultProperty ("Items")]
48         [Designer ("System.Windows.Forms.Design.ListViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
49         public class ListView : Control
50         {
51                 private ItemActivation activation = ItemActivation.Standard;
52                 private ListViewAlignment alignment = ListViewAlignment.Top;
53                 private bool allow_column_reorder = false;
54                 private bool auto_arrange = true;
55                 private bool check_boxes = false;
56                 private CheckedIndexCollection checked_indices;
57                 private CheckedListViewItemCollection checked_items;
58                 private ColumnHeaderCollection columns;
59                 internal ListViewItem focused_item;
60                 private bool full_row_select = false;
61                 private bool grid_lines = false;
62                 private ColumnHeaderStyle header_style = ColumnHeaderStyle.Clickable;
63                 private bool hide_selection = true;
64                 private bool hover_selection = false;
65                 private IComparer item_sorter;
66                 private ListViewItemCollection items;
67                 private bool label_edit = false;
68                 private bool label_wrap = true;
69                 private bool multiselect = true;
70                 private bool scrollable = true;
71                 private SelectedIndexCollection selected_indices;
72                 private SelectedListViewItemCollection selected_items;
73                 private SortOrder sort_order = SortOrder.None;
74                 private ImageList state_image_list;
75                 private bool updating = false;
76                 private View view = View.LargeIcon;
77                 private int layout_wd;    // We might draw more than our client area
78                 private int layout_ht;    // therefore we need to have these two.
79                 //private TextBox editor;   // Used for editing an item text
80                 HeaderControl header_control;
81                 internal ItemControl item_control;
82                 internal ScrollBar h_scroll; // used for scrolling horizontally
83                 internal ScrollBar v_scroll; // used for scrolling vertically
84                 internal int h_marker;          // Position markers for scrolling
85                 internal int v_marker;
86                 private int keysearch_tickcnt;
87                 private string keysearch_text;
88                 static private readonly int keysearch_keydelay = 1000;
89                 private int[] reordered_column_indices;
90
91                 // internal variables
92                 internal ImageList large_image_list;
93                 internal ImageList small_image_list;
94                 internal Size text_size = Size.Empty;
95
96                 #region Events
97                 public event LabelEditEventHandler AfterLabelEdit;
98
99                 [Browsable (false)]
100                 [EditorBrowsable (EditorBrowsableState.Never)]
101                 public new event EventHandler BackgroundImageChanged {
102                         add { base.BackgroundImageChanged += value; }
103                         remove { base.BackgroundImageChanged -= value; }
104                 }
105
106                 public event LabelEditEventHandler BeforeLabelEdit;
107                 public event ColumnClickEventHandler ColumnClick;
108                 public event EventHandler ItemActivate;
109                 public event ItemCheckEventHandler ItemCheck;
110                 public event ItemDragEventHandler ItemDrag;
111
112                 [Browsable (false)]
113                 [EditorBrowsable (EditorBrowsableState.Never)]
114                 public new event PaintEventHandler Paint {
115                         add { base.Paint += value; }
116                         remove { base.Paint -= value; }
117                 }
118
119                 public event EventHandler SelectedIndexChanged;
120
121                 [Browsable (false)]
122                 [EditorBrowsable (EditorBrowsableState.Never)]
123                 public new event EventHandler TextChanged {
124                         add { base.TextChanged += value; }
125                         remove { base.TextChanged -= value; }
126                 }
127
128                 #endregion // Events
129
130                 #region Public Constructors
131                 public ListView ()
132                 {
133                         background_color = ThemeEngine.Current.ColorWindow;
134                         checked_indices = new CheckedIndexCollection (this);
135                         checked_items = new CheckedListViewItemCollection (this);
136                         columns = new ColumnHeaderCollection (this);
137                         foreground_color = SystemColors.WindowText;
138                         items = new ListViewItemCollection (this);
139                         selected_indices = new SelectedIndexCollection (this);
140                         selected_items = new SelectedListViewItemCollection (this);
141
142                         border_style = BorderStyle.Fixed3D;
143
144                         header_control = new HeaderControl (this);
145                         header_control.Visible = false;
146                         Controls.AddImplicit (header_control);
147
148                         item_control = new ItemControl (this);
149                         Controls.AddImplicit (item_control);
150
151                         h_scroll = new HScrollBar ();
152                         Controls.AddImplicit (this.h_scroll);
153
154                         v_scroll = new VScrollBar ();
155                         Controls.AddImplicit (this.v_scroll);
156
157                         h_marker = v_marker = 0;
158                         keysearch_tickcnt = 0;
159
160                         // scroll bars are disabled initially
161                         h_scroll.Visible = false;
162                         h_scroll.ValueChanged += new EventHandler(HorizontalScroller);
163                         v_scroll.Visible = false;
164                         v_scroll.ValueChanged += new EventHandler(VerticalScroller);
165
166                         // event handlers
167                         base.KeyDown += new KeyEventHandler(ListView_KeyDown);
168                         SizeChanged += new EventHandler (ListView_SizeChanged);
169
170                         this.SetStyle (ControlStyles.UserPaint | ControlStyles.StandardClick, false);
171                 }
172                 #endregion      // Public Constructors
173
174                 #region Private Internal Properties
175                 internal Size CheckBoxSize {
176                         get {
177                                 if (this.check_boxes) {
178                                         if (this.state_image_list != null)
179                                                 return this.state_image_list.ImageSize;
180                                         else
181                                                 return ThemeEngine.Current.ListViewCheckBoxSize;
182                                 }
183                                 return Size.Empty;
184                         }
185                 }
186
187                 bool multiselecting;
188
189                 bool CanMultiselect {
190                         get {
191                                 if (multiselecting)
192                                         return true;
193                                 else if (multiselect && (XplatUI.State.ModifierKeys & (Keys.Control | Keys.Shift)) != 0)
194                                         return true;
195                                 else
196                                         return false;
197                         }
198                 }
199
200                 #endregion      // Private Internal Properties
201
202                 #region  Protected Properties
203                 protected override CreateParams CreateParams {
204                         get { return base.CreateParams; }
205                 }
206
207                 protected override Size DefaultSize {
208                         get { return ThemeEngine.Current.ListViewDefaultSize; }
209                 }
210                 #endregion      // Protected Properties
211
212                 #region Public Instance Properties
213                 [DefaultValue (ItemActivation.Standard)]
214                 public ItemActivation Activation {
215                         get { return activation; }
216                         set { 
217                                 if (value != ItemActivation.Standard && value != ItemActivation.OneClick && 
218                                         value != ItemActivation.TwoClick) {
219                                         throw new InvalidEnumArgumentException (string.Format
220                                                 ("Enum argument value '{0}' is not valid for Activation", value));
221                                 }
222                                   
223                                 activation = value;
224                         }
225                 }
226
227                 [DefaultValue (ListViewAlignment.Top)]
228                 [Localizable (true)]
229                 public ListViewAlignment Alignment {
230                         get { return alignment; }
231                         set {
232                                 if (value != ListViewAlignment.Default && value != ListViewAlignment.Left && 
233                                         value != ListViewAlignment.SnapToGrid && value != ListViewAlignment.Top) {
234                                         throw new InvalidEnumArgumentException (string.Format 
235                                                 ("Enum argument value '{0}' is not valid for Alignment", value));
236                                 }
237                                 
238                                 if (this.alignment != value) {
239                                         alignment = value;
240                                         // alignment does not matter in Details/List views
241                                         if (this.view == View.LargeIcon ||
242                                             this.View == View.SmallIcon)
243                                                 this.Redraw (true);
244                                 }
245                         }
246                 }
247
248                 [DefaultValue (false)]
249                 public bool AllowColumnReorder {
250                         get { return allow_column_reorder; }
251                         set { allow_column_reorder = value; }
252                 }
253
254                 [DefaultValue (true)]
255                 public bool AutoArrange {
256                         get { return auto_arrange; }
257                         set {
258                                 if (auto_arrange != value) {
259                                         auto_arrange = value;
260                                         // autoarrange does not matter in Details/List views
261                                         if (this.view == View.LargeIcon || this.View == View.SmallIcon)
262                                                 this.Redraw (true);
263                                 }
264                         }
265                 }
266
267                 public override Color BackColor {
268                         get {
269                                 if (background_color.IsEmpty)
270                                         return ThemeEngine.Current.ColorWindow;
271                                 else
272                                         return background_color;
273                         }
274                         set { background_color = value; }
275                 }
276
277                 [Browsable (false)]
278                 [EditorBrowsable (EditorBrowsableState.Never)]
279                 public override Image BackgroundImage {
280                         get { return background_image; }
281                         set {
282                                 if (value == background_image)
283                                         return;
284
285                                 background_image = value;
286                                 OnBackgroundImageChanged (EventArgs.Empty);
287                         }
288                 }
289
290                 [DefaultValue (BorderStyle.Fixed3D)]
291                 [DispId (-504)]
292                 public BorderStyle BorderStyle {
293                         get { return InternalBorderStyle; }
294                         set { InternalBorderStyle = value; }
295                 }
296
297                 [DefaultValue (false)]
298                 public bool CheckBoxes {
299                         get { return check_boxes; }
300                         set {
301                                 if (check_boxes != value) {
302                                         check_boxes = value;
303                                         this.Redraw (true);
304                                 }
305                         }
306                 }
307
308                 [Browsable (false)]
309                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
310                 public CheckedIndexCollection CheckedIndices {
311                         get { return checked_indices; }
312                 }
313
314                 [Browsable (false)]
315                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
316                 public CheckedListViewItemCollection CheckedItems {
317                         get { return checked_items; }
318                 }
319
320                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
321                 [Localizable (true)]
322                 [MergableProperty (false)]
323                 public ColumnHeaderCollection Columns {
324                         get { return columns; }
325                 }
326
327                 [Browsable (false)]
328                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
329                 public ListViewItem FocusedItem {
330                         get {
331                                 return focused_item;
332                         }
333                 }
334
335                 public override Color ForeColor {
336                         get {
337                                 if (foreground_color.IsEmpty)
338                                         return ThemeEngine.Current.ColorWindowText;
339                                 else
340                                         return foreground_color;
341                         }
342                         set { foreground_color = value; }
343                 }
344
345                 [DefaultValue (false)]
346                 public bool FullRowSelect {
347                         get { return full_row_select; }
348                         set { full_row_select = value; }
349                 }
350
351                 [DefaultValue (false)]
352                 public bool GridLines {
353                         get { return grid_lines; }
354                         set {
355                                 if (grid_lines != value) {
356                                         grid_lines = value;
357                                         this.Redraw (false);
358                                 }
359                         }
360                 }
361
362                 [DefaultValue (ColumnHeaderStyle.Clickable)]
363                 public ColumnHeaderStyle HeaderStyle {
364                         get { return header_style; }
365                         set {
366                                 if (header_style == value)
367                                         return;
368
369                                 switch (value) {
370                                 case ColumnHeaderStyle.Clickable:
371                                 case ColumnHeaderStyle.Nonclickable:
372                                 case ColumnHeaderStyle.None:
373                                         break;
374                                 default:
375                                         throw new InvalidEnumArgumentException (string.Format 
376                                                 ("Enum argument value '{0}' is not valid for ColumnHeaderStyle", value));
377                                 }
378                                 
379                                 header_style = value;
380                                 if (view == View.Details)
381                                         Redraw (true);
382                         }
383                 }
384
385                 [DefaultValue (true)]
386                 public bool HideSelection {
387                         get { return hide_selection; }
388                         set {
389                                 if (hide_selection != value) {
390                                         hide_selection = value;
391                                         this.Redraw (false);
392                                 }
393                         }
394                 }
395
396                 [DefaultValue (false)]
397                 public bool HoverSelection {
398                         get { return hover_selection; }
399                         set { hover_selection = value; }
400                 }
401
402                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
403                 [Localizable (true)]
404                 [MergableProperty (false)]              
405                 public ListViewItemCollection Items {
406                         get { return items; }
407                 }
408
409                 [DefaultValue (false)]
410                 public bool LabelEdit {
411                         get { return label_edit; }
412                         set { label_edit = value; }
413                 }
414
415                 [DefaultValue (true)]
416                 [Localizable (true)]
417                 public bool LabelWrap {
418                         get { return label_wrap; }
419                         set {
420                                 if (label_wrap != value) {
421                                         label_wrap = value;
422                                         this.Redraw (true);
423                                 }
424                         }
425                 }
426
427                 [DefaultValue (null)]
428                 public ImageList LargeImageList {
429                         get { return large_image_list; }
430                         set {
431                                 large_image_list = value;
432                                 this.Redraw (true);
433                         }
434                 }
435
436                 [Browsable (false)]
437                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
438                 public IComparer ListViewItemSorter {
439                         get { return item_sorter; }
440                         set { item_sorter = value; }
441                 }
442
443                 [DefaultValue (true)]
444                 public bool MultiSelect {
445                         get { return multiselect; }
446                         set { multiselect = value; }
447                 }
448
449                 [DefaultValue (true)]
450                 public bool Scrollable {
451                         get { return scrollable; }
452                         set {
453                                 if (scrollable != value) {
454                                         scrollable = value;
455                                         this.Redraw (true);
456                                 }
457                         }
458                 }
459
460                 [Browsable (false)]
461                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
462                 public SelectedIndexCollection SelectedIndices {
463                         get { return selected_indices; }
464                 }
465
466                 [Browsable (false)]
467                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
468                 public SelectedListViewItemCollection SelectedItems {
469                         get { return selected_items; }
470                 }
471
472 #if NET_2_0
473                 [MonoTODO("Implement")]
474                 public bool ShowGroups {
475                         get {
476                                 return false;
477                         }
478
479                         set {
480                         }
481                 }
482 #endif
483
484                 [DefaultValue (null)]
485                 public ImageList SmallImageList {
486                         get { return small_image_list; }
487                         set {
488                                 small_image_list = value;
489                                 this.Redraw (true);
490                         }
491                 }
492
493                 [DefaultValue (SortOrder.None)]
494                 public SortOrder Sorting {
495                         get { return sort_order; }
496                         set { 
497                                 if (value != SortOrder.Ascending && value != SortOrder.Descending  && 
498                                         value != SortOrder.None) {
499                                         throw new InvalidEnumArgumentException (string.Format
500                                                 ("Enum argument value '{0}' is not valid for Sorting", value));
501                                 }
502                                 
503                                 if (sort_order != value)  {                     
504                                         sort_order = value; 
505                                         this.Redraw (false);
506                                 }
507                         }
508                 }
509
510                 [DefaultValue (null)]
511                 public ImageList StateImageList {
512                         get { return state_image_list; }
513                         set {
514                                 state_image_list = value;
515                                 this.Redraw (true);
516                         }
517                 }
518
519                 [Bindable (false)]
520                 [Browsable (false)]
521                 [EditorBrowsable (EditorBrowsableState.Never)]
522                 public override string Text {
523                         get { return text; } 
524                         set {
525                                 if (value == text)
526                                         return;
527
528                                 text = value;
529                                 this.Redraw (true);
530
531                                 OnTextChanged (EventArgs.Empty);
532                         }
533                 }
534
535                 [Browsable (false)]
536                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
537                 public ListViewItem TopItem {
538                         get {
539                                 // there is no item
540                                 if (this.items.Count == 0)
541                                         return null;
542                                 // if contents are not scrolled
543                                 // it is the first item
544                                 else if (h_marker == 0 && v_marker == 0)
545                                         return this.items [0];
546                                 // do a hit test for the scrolled position
547                                 else {
548                                         foreach (ListViewItem item in this.items) {
549                                                 if (item.Bounds.X >= 0 && item.Bounds.Y >= 0)
550                                                         return item;
551                                         }
552                                         return null;
553                                 }
554                         }
555                 }
556
557 #if NET_2_0
558                 [MonoTODO("Implement")]
559                 public bool UseCompatibleStateImageBehavior {
560                         get {
561                                 return false;
562                         }
563
564                         set {
565                         }
566                 }
567 #endif
568
569                 [DefaultValue (View.LargeIcon)]
570                 public View View {
571                         get { return view; }
572                         set { 
573                                 if (value != View.Details && value != View.LargeIcon  && 
574                                         value != View.List  && value != View.SmallIcon  ) {
575                                         throw new InvalidEnumArgumentException (string.Format
576                                                 ("Enum argument value '{0}' is not valid for View", value));
577                                 }
578                                 
579                                 if (view != value) {
580                                         h_scroll.Value = v_scroll.Value = 0;
581                                         view = value; 
582                                         Redraw (true);
583                                 }
584                         }
585                 }
586                 #endregion      // Public Instance Properties
587
588                 #region Internal Methods Properties
589                 
590                 internal int FirstVisibleIndex {
591                         get {
592                                 // there is no item
593                                 if (this.items.Count == 0)
594                                         return 0;
595                                                                         
596                                 if (h_marker == 0 && v_marker == 0)
597                                         return 0;                                       
598                                 
599                                 foreach (ListViewItem item in this.items) {
600                                         if (item.Bounds.Right >= 0 && item.Bounds.Bottom >= 0)
601                                                 return item.Index;
602                                 }
603                                 return 0;
604
605                         }
606                 }
607
608                 
609                 internal int LastVisibleIndex {                 
610                         get {                                                   
611                                 for (int i = FirstVisibleIndex; i < Items.Count; i++) {
612                                         if (View == View.List || Alignment == ListViewAlignment.Left) {
613                                                 if (Items[i].Bounds.X > ClientRectangle.Right)
614                                                         return i - 1;                                   
615                                         } else {
616                                                 if (Items[i].Bounds.Y > ClientRectangle.Bottom)
617                                                         return i - 1;                                   
618                                         }
619                                 }
620                                 
621                                 return Items.Count - 1;
622                         }
623                 }
624                 
625                 internal int TotalWidth {
626                         get { return Math.Max (this.Width, this.layout_wd); }
627                 }
628
629                 internal int TotalHeight {
630                         get { return Math.Max (this.Height, this.layout_ht); }
631                 }
632
633                 internal void Redraw (bool recalculate)
634                 {
635                         // Avoid calculations when control is being updated
636                         if (this.updating)
637                                 return;
638
639                         if (recalculate)
640                                 CalculateListView (this.alignment);
641
642                         Refresh ();
643                 }
644
645                 internal Size GetChildColumnSize (int index)
646                 {
647                         Size ret_size = Size.Empty;
648                         ColumnHeader col = this.columns [index];
649
650                         if (col.Width == -2) { // autosize = max(items, columnheader)
651                                 Size size = Size.Ceiling (this.DeviceContext.MeasureString
652                                                           (col.Text, this.Font));
653                                 ret_size = BiggestItem (index);
654                                 if (size.Width > ret_size.Width)
655                                         ret_size = size;
656                         }
657                         else { // -1 and all the values < -2 are put under one category
658                                 ret_size = BiggestItem (index);
659                                 // fall back to empty columns' width if no subitem is available for a column
660                                 if (ret_size.IsEmpty) {
661                                         ret_size.Width = ThemeEngine.Current.ListViewEmptyColumnWidth;
662                                         if (col.Text.Length > 0)
663                                                 ret_size.Height = Size.Ceiling (this.DeviceContext.MeasureString
664                                                                                 (col.Text, this.Font)).Height;
665                                         else
666                                                 ret_size.Height = this.Font.Height;
667                                 }
668                         }
669
670                         // adjust the size for icon and checkbox for 0th column
671                         if (index == 0) {
672                                 ret_size.Width += (this.CheckBoxSize.Width + 4);
673                                 if (this.small_image_list != null)
674                                         ret_size.Width += this.small_image_list.ImageSize.Width;
675                         }
676                         return ret_size;
677                 }
678
679                 // Returns the size of biggest item text in a column.
680                 private Size BiggestItem (int col)
681                 {
682                         Size temp = Size.Empty;
683                         Size ret_size = Size.Empty;
684
685                         // 0th column holds the item text, we check the size of
686                         // the various subitems falling in that column and get
687                         // the biggest one's size.
688                         foreach (ListViewItem item in items) {
689                                 if (col >= item.SubItems.Count)
690                                         continue;
691
692                                 temp = Size.Ceiling (this.DeviceContext.MeasureString
693                                                      (item.SubItems [col].Text, this.Font));
694                                 if (temp.Width > ret_size.Width)
695                                         ret_size = temp;
696                         }
697
698                         // adjustment for space
699                         if (!ret_size.IsEmpty)
700                                 ret_size.Width += 4;
701
702                         return ret_size;
703                 }
704
705                 // Sets the size of the biggest item text as per the view
706                 private void CalcTextSize ()
707                 {                       
708                         // clear the old value
709                         text_size = Size.Empty;
710
711                         if (items.Count == 0)
712                                 return;
713
714                         text_size = BiggestItem (0);
715
716                         if (view == View.LargeIcon && this.label_wrap) {
717                                 Size temp = Size.Empty;
718                                 if (this.check_boxes)
719                                         temp.Width += 2 * this.CheckBoxSize.Width;
720                                 if (large_image_list != null)
721                                         temp.Width += large_image_list.ImageSize.Width;
722                                 if (temp.Width == 0)
723                                         temp.Width = 43;
724                                 // wrapping is done for two lines only
725                                 if (text_size.Width > temp.Width) {
726                                         text_size.Width = temp.Width;
727                                         text_size.Height *= 2;
728                                 }
729                         }
730                         else if (view == View.List) {
731                                 // in list view max text shown in determined by the
732                                 // control width, even if scolling is enabled.
733                                 int max_wd = this.Width - (this.CheckBoxSize.Width - 2);
734                                 if (this.small_image_list != null)
735                                         max_wd -= this.small_image_list.ImageSize.Width;
736
737                                 if (text_size.Width > max_wd)
738                                         text_size.Width = max_wd;
739                         }
740
741                         // we do the default settings, if we have got 0's
742                         if (text_size.Height <= 0)
743                                 text_size.Height = this.Font.Height;
744                         if (text_size.Width <= 0)
745                                 text_size.Width = this.Width;
746
747                         // little adjustment
748                         text_size.Width += 4;
749                         text_size.Height += 2;
750                 }
751
752                 private void Scroll (ScrollBar scrollbar, int delta)
753                 {
754                         if (delta == 0 || !scrollbar.Visible)
755                                 return;
756
757                         int max;
758                         if (scrollbar == h_scroll)
759                                 max = h_scroll.Maximum - item_control.Width;
760                         else
761                                 max = v_scroll.Maximum - item_control.Height;
762
763                         int val = scrollbar.Value + delta;
764                         if (val > max)
765                                 val = max;
766                         else if (val < scrollbar.Minimum)
767                                 val = scrollbar.Minimum;
768                         scrollbar.Value = val;
769                 }
770
771                 private void CalculateScrollBars ()
772                 {
773                         Rectangle client_area = ClientRectangle;
774                         
775                         if (!this.scrollable || this.items.Count <= 0) {
776                                 h_scroll.Visible = false;
777                                 v_scroll.Visible = false;
778                                 return;
779                         }
780
781                         // making a scroll bar visible might make
782                         // other scroll bar visible                     
783                         if (layout_wd > client_area.Right) {
784                                 h_scroll.Visible = true;
785                                 if ((layout_ht + h_scroll.Height) > client_area.Bottom)
786                                         v_scroll.Visible = true;                                        
787                                 else
788                                         v_scroll.Visible = false;
789                         } else if (layout_ht > client_area.Bottom) {                            
790                                 v_scroll.Visible = true;
791                                 if ((layout_wd + v_scroll.Width) > client_area.Right)
792                                         h_scroll.Visible = true;
793                                 else
794                                         h_scroll.Visible = false;
795                         } else {
796                                 h_scroll.Visible = false;
797                                 v_scroll.Visible = false;
798                         }                       
799
800                         item_control.Height = ClientRectangle.Height - header_control.Height;
801
802                         if (h_scroll.Visible) {
803                                 h_scroll.Location = new Point (client_area.X, client_area.Bottom - h_scroll.Height);
804                                 h_scroll.Minimum = 0;
805
806                                 // if v_scroll is visible, adjust the maximum of the
807                                 // h_scroll to account for the width of v_scroll
808                                 if (v_scroll.Visible) {
809                                         h_scroll.Maximum = layout_wd + v_scroll.Width;
810                                         h_scroll.Width = client_area.Width - v_scroll.Width;
811                                 }
812                                 else {
813                                         h_scroll.Maximum = layout_wd;
814                                         h_scroll.Width = client_area.Width;
815                                 }
816    
817                                 h_scroll.LargeChange = client_area.Width;
818                                 h_scroll.SmallChange = Font.Height;
819                                 item_control.Height -= h_scroll.Height;
820                         }
821
822                         if (header_control.Visible)
823                                 header_control.Width = ClientRectangle.Width;
824                         item_control.Width = ClientRectangle.Width;
825
826                         if (v_scroll.Visible) {
827                                 v_scroll.Location = new Point (client_area.Right - v_scroll.Width, client_area.Y);
828                                 v_scroll.Minimum = 0;
829
830                                 // if h_scroll is visible, adjust the maximum of the
831                                 // v_scroll to account for the height of h_scroll
832                                 if (h_scroll.Visible) {
833                                         v_scroll.Maximum = layout_ht + h_scroll.Height;
834                                         v_scroll.Height = client_area.Height; // - h_scroll.Height already done 
835                                 } else {
836                                         v_scroll.Maximum = layout_ht;
837                                         v_scroll.Height = client_area.Height;
838                                 }
839
840                                 v_scroll.LargeChange = client_area.Height;
841                                 v_scroll.SmallChange = Font.Height;
842                                 if (header_control.Visible)
843                                         header_control.Width -= v_scroll.Width;
844                                 item_control.Width -= v_scroll.Width;
845                         }
846                 }
847                 
848                 ColumnHeader GetReorderedColumn (int index)
849                 {
850                         if (reordered_column_indices == null)
851                                 return Columns [index];
852                         else
853                                 return Columns [reordered_column_indices [index]];
854                 }
855
856                 void ReorderColumn (ColumnHeader col, int index)
857                 {
858                         if (reordered_column_indices == null) {
859                                 reordered_column_indices = new int [Columns.Count];
860                                 for (int i = 0; i < Columns.Count; i++)
861                                         reordered_column_indices [i] = i;
862                         }
863
864                         if (reordered_column_indices [index] == col.Index)
865                                 return;
866
867                         int[] curr = reordered_column_indices;
868                         int[] result = new int [Columns.Count];
869                         int curr_idx = 0;
870                         for (int i = 0; i < Columns.Count; i++) {
871                                 if (curr_idx < Columns.Count && curr [curr_idx] == col.Index)
872                                         curr_idx++;
873
874                                 if (i == index)
875                                         result [i] = col.Index;
876                                 else
877                                         result [i] = curr [curr_idx++];
878                         }
879
880                         reordered_column_indices = result;
881                         LayoutDetails ();
882                         header_control.Invalidate ();
883                         item_control.Invalidate ();
884                 }
885
886                 Size LargeIconItemSize {
887                         get {
888                                 int image_w = LargeImageList == null ? 12 : LargeImageList.ImageSize.Width;
889                                 int image_h = LargeImageList == null ? 2 : LargeImageList.ImageSize.Height;
890                                 int w = CheckBoxSize.Width + 2 + Math.Max (text_size.Width, image_w);
891                                 int h = text_size.Height + 2 + Math.Max (CheckBoxSize.Height, image_h);
892                                 return new Size (w, h);
893                         }
894                 }
895
896                 Size SmallIconItemSize {
897                         get {
898                                 int image_w = SmallImageList == null ? 0 : SmallImageList.ImageSize.Width;
899                                 int image_h = SmallImageList == null ? 0 : SmallImageList.ImageSize.Height;
900                                 int w = text_size.Width + 2 + CheckBoxSize.Width + image_w;
901                                 int h = Math.Max (text_size.Height, Math.Max (CheckBoxSize.Height, image_h));
902                                 return new Size (w, h);
903                         }
904                 }
905
906                 int rows;
907                 int cols;
908                 ListViewItem[,] item_matrix;
909
910                 void LayoutIcons (bool large_icons, bool left_aligned, int x_spacing, int y_spacing)
911                 {
912                         header_control.Visible = false;
913                         header_control.Size = Size.Empty;
914                         item_control.Visible = true;
915                         item_control.Location = Point.Empty;
916
917                         if (items.Count == 0)
918                                 return;
919
920                         Size sz = large_icons ? LargeIconItemSize : SmallIconItemSize;
921
922                         Rectangle area = ClientRectangle;
923
924                         if (left_aligned) {
925                                 rows = (int) Math.Floor ((double)(area.Height - h_scroll.Height + y_spacing) / (double)(sz.Height + y_spacing));
926                                 if (rows <= 0)
927                                         rows = 1;
928                                 cols = (int) Math.Ceiling ((double)items.Count / (double)rows);
929                         } else {
930                                 cols = (int) Math.Floor ((double)(area.Width - v_scroll.Width + x_spacing) / (double)(sz.Width + x_spacing));
931                                 if (cols <= 0)
932                                         cols = 1;
933                                 rows = (int) Math.Ceiling ((double)items.Count / (double)cols);
934                         }
935
936                         layout_ht = rows * (sz.Height + y_spacing) - y_spacing;
937                         layout_wd = cols * (sz.Width + x_spacing) - x_spacing;
938                         item_matrix = new ListViewItem [rows, cols];
939                         int row = 0;
940                         int col = 0;
941                         foreach (ListViewItem item in items) {
942                                 int x = col * (sz.Width + x_spacing);
943                                 int y = row * (sz.Height + y_spacing);
944                                 item.Location = new Point (x, y);
945                                 item.Layout ();
946                                 item.row = row;
947                                 item.col = col;
948                                 item_matrix [row, col] = item;
949                                 if (left_aligned) {
950                                         if (++row == rows) {
951                                                 row = 0;
952                                                 col++;
953                                         }
954                                 } else {
955                                         if (++col == cols) {
956                                                 col = 0;
957                                                 row++;
958                                         }
959                                 }
960                         }
961
962                         item_control.Size = new Size (layout_wd, layout_ht);
963                 }
964
965                 void LayoutHeader ()
966                 {
967                         if (header_style == ColumnHeaderStyle.None) {
968                                 header_control.Visible = false;
969                                 header_control.Size = Size.Empty;
970                                 return;
971                         }
972
973                         int x = 0;
974                         for (int i = 0; i < Columns.Count; i++) {
975                                 ColumnHeader col = GetReorderedColumn (i);
976                                 col.X = x;
977                                 col.Y = 0;
978                                 col.CalcColumnHeader ();
979                                 x += col.Wd;
980                         }
981
982                         if (x < ClientRectangle.Width)
983                                 x = ClientRectangle.Width;
984
985                         header_control.Width = x;
986                         header_control.Height = columns [0].Ht;
987                         header_control.Visible = true;
988                 }
989
990                 void LayoutDetails ()
991                 {
992                         if (columns.Count == 0) {
993                                 header_control.Visible = false;
994                                 item_control.Visible = false;
995                                 return;
996                         }
997
998                         LayoutHeader ();
999
1000                         item_control.Visible = true;
1001                         item_control.Location = new Point (0, header_control.Height);
1002
1003                         int y = 0; 
1004                         if (items.Count > 0) {
1005                                 foreach (ListViewItem item in items) {
1006                                         item.Layout ();
1007                                         item.Location = new Point (0, y);
1008                                         y += item.Bounds.Height + 2;
1009                                 }
1010
1011                                 // some space for bottom gridline
1012                                 if (grid_lines)
1013                                         y += 2;
1014                         }
1015
1016                         layout_wd = Math.Max (header_control.Width, item_control.Width);
1017                         layout_ht = y + header_control.Height;
1018                 }
1019
1020                 private void CalculateListView (ListViewAlignment align)
1021                 {
1022                         CalcTextSize ();
1023
1024                         switch (view) {
1025                         case View.Details:
1026                                 LayoutDetails ();
1027                                 break;
1028
1029                         case View.SmallIcon:
1030                                 LayoutIcons (false, alignment == ListViewAlignment.Left, 4, 2);
1031                                 break;
1032
1033                         case View.LargeIcon:
1034                                 LayoutIcons (true, alignment == ListViewAlignment.Left,
1035                                              ThemeEngine.Current.ListViewHorizontalSpacing,
1036                                              ThemeEngine.Current.ListViewVerticalSpacing);
1037                                 break;
1038
1039                         case View.List:
1040                                 LayoutIcons (false, true, 4, 2);
1041                                 break;
1042                         }
1043
1044                         CalculateScrollBars ();
1045                 }
1046
1047                 internal void UpdateSelection (ListViewItem item)
1048                 {
1049                         if (item.Selected) {
1050
1051                                 if (!CanMultiselect && SelectedItems.Count > 0) {
1052                                         SelectedItems.Clear ();
1053                                         SelectedIndices.list.Clear ();
1054                                 }
1055
1056                                 if (!SelectedItems.Contains (item)) {
1057                                         SelectedItems.list.Add (item);
1058                                         SelectedIndices.list.Add (item.Index);
1059                                 }
1060                         } else {
1061                                 SelectedItems.list.Remove (item);
1062                                 SelectedIndices.list.Remove (item.Index);
1063                         }
1064                 }
1065
1066                 private bool KeySearchString (KeyEventArgs ke)
1067                 {
1068                         int current_tickcnt = Environment.TickCount;
1069                         if (keysearch_tickcnt > 0 && current_tickcnt - keysearch_tickcnt > keysearch_keydelay) {
1070                                 keysearch_text = string.Empty;
1071                         }
1072                         
1073                         keysearch_text += (char) ke.KeyData;
1074                         keysearch_tickcnt = current_tickcnt;
1075
1076                         int start = FocusedItem == null ? 0 : FocusedItem.Index;
1077                         int i = start;
1078                         while (true) {
1079                                 if (CultureInfo.CurrentCulture.CompareInfo.IsPrefix (Items[i].Text, keysearch_text,
1080                                         CompareOptions.IgnoreCase)) {
1081                                         SetFocusedItem (Items [i]);
1082                                         items [i].Selected = true;
1083                                         EnsureVisible (i);
1084                                         break;
1085                                 }
1086                                 i = (i + 1  < Items.Count) ? i+1 : 0;
1087
1088                                 if (i == start)
1089                                         break;
1090                         }
1091                         return true;
1092                 }
1093
1094                 int GetAdjustedIndex (Keys key)
1095                 {
1096                         int result = -1;
1097
1098                         if (View == View.Details) {
1099                                 if (key == Keys.Up)
1100                                         result = FocusedItem.Index - 1;
1101                                 else if (key == Keys.Down) {
1102                                         result = FocusedItem.Index + 1;
1103                                         if (result == items.Count)
1104                                                 result = -1;
1105                                 }
1106                                 return result;
1107                         }
1108
1109                         int row = FocusedItem.row;
1110                         int col = FocusedItem.col;
1111
1112                         switch (key) {
1113                         case Keys.Left:
1114                                 if (col == 0)
1115                                         return -1;
1116                                 return item_matrix [row, col - 1].Index;
1117
1118                         case Keys.Right:
1119                                 if (col == (cols - 1))
1120                                         return -1;
1121                                 while (item_matrix [row, col + 1] == null)
1122                                        row--;   
1123                                 return item_matrix [row, col + 1].Index;
1124
1125                         case Keys.Up:
1126                                 if (row == 0)
1127                                         return -1;
1128                                 return item_matrix [row - 1, col].Index;
1129
1130                         case Keys.Down:
1131                                 if (row == (rows - 1))
1132                                         return -1;
1133                                 while (item_matrix [row + 1, col] == null)
1134                                        col--;   
1135                                 return item_matrix [row + 1, col].Index;
1136
1137                         default:
1138                                 return -1;
1139                         }
1140                 }
1141
1142                 ListViewItem selection_start;
1143
1144                 private void SelectItems (ArrayList sel_items)
1145                 {
1146                         multiselecting = true;
1147                         ArrayList curr_items = (ArrayList) SelectedItems.list.Clone ();
1148                         foreach (ListViewItem item in curr_items)
1149                                 if (!sel_items.Contains (item))
1150                                         item.Selected = false;
1151                         foreach (ListViewItem item in sel_items)
1152                                 item.Selected = true;
1153                         multiselecting = false;
1154                 }
1155
1156                 private void UpdateMultiSelection (int index)
1157                 {
1158                         bool shift_pressed = (XplatUI.State.ModifierKeys & Keys.Shift) != 0;
1159                         bool ctrl_pressed = (XplatUI.State.ModifierKeys & Keys.Control) != 0;
1160                         ListViewItem item = items [index];
1161
1162                         if (shift_pressed && selection_start != null) {
1163                                 ArrayList list = new ArrayList ();
1164                                 int start = Math.Min (selection_start.Index, index);
1165                                 int end = Math.Max (selection_start.Index, index);
1166                                 if (View == View.Details) {
1167                                         for (int i = start; i <= end; i++)
1168                                                 list.Add (items [i]);
1169                                 } else {
1170                                         int left = Math.Min (items [start].col, items [end].col);
1171                                         int right = Math.Max (items [start].col, items [end].col);
1172                                         int top = Math.Min (items [start].row, items [end].row);
1173                                         int bottom = Math.Max (items [start].row, items [end].row);
1174                                         foreach (ListViewItem curr in items)
1175                                                 if (curr.row >= top && curr.row <= bottom && 
1176                                                     curr.col >= left && curr.col <= right)
1177                                                         list.Add (curr);
1178                                 }
1179                                 SelectItems (list);
1180                         } else if (!ctrl_pressed) {
1181                                 SelectedItems.Clear ();
1182                                 SelectedIndices.list.Clear ();
1183                                 item.Selected = true;
1184                                 selection_start = item;
1185                         }
1186                 }
1187
1188                 private void ListView_KeyDown (object sender, KeyEventArgs ke)
1189                 {                       
1190                         if (ke.Handled || Items.Count == 0 || !item_control.Visible)
1191                                 return;
1192
1193                         int index = -1;
1194                         ke.Handled = true;
1195
1196                         switch (ke.KeyCode) {
1197
1198                         case Keys.End:
1199                                 index = Items.Count - 1;
1200                                 break;
1201
1202                         case Keys.Home:                 
1203                                 index = 0;
1204                                 break;
1205
1206                         case Keys.Left:
1207                         case Keys.Right:
1208                         case Keys.Up:                           
1209                         case Keys.Down:
1210                                 index = GetAdjustedIndex (ke.KeyCode);
1211                                 break;
1212
1213                         default:
1214                                 ke.Handled = KeySearchString (ke);
1215                                 return;
1216                         }
1217                         
1218                         if (index == -1)
1219                                 return;
1220
1221                         if (MultiSelect)
1222                                 UpdateMultiSelection (index);
1223                         else
1224                                 items [index].Selected = true;
1225
1226                         SetFocusedItem (items [index]);                         
1227                         EnsureVisible (index);
1228                 }
1229
1230                                 
1231                 internal class ItemControl : Control {
1232
1233                         ListView owner;
1234                         ListViewItem clicked_item;
1235                         ListViewItem last_clicked_item;
1236                         bool hover_processed = false;
1237
1238                         public ItemControl (ListView owner)
1239                         {
1240                                 this.owner = owner;
1241                                 DoubleClick += new EventHandler(ItemsDoubleClick);
1242                                 KeyDown += new KeyEventHandler (ItemsKeyDown);
1243                                 KeyUp += new KeyEventHandler (ItemsKeyUp);
1244                                 MouseDown += new MouseEventHandler(ItemsMouseDown);
1245                                 MouseMove += new MouseEventHandler(ItemsMouseMove);
1246                                 MouseHover += new EventHandler(ItemsMouseHover);
1247                                 MouseUp += new MouseEventHandler(ItemsMouseUp);
1248                                 MouseWheel += new MouseEventHandler(ItemsMouseWheel);
1249                                 GotFocus += new EventHandler (FocusChanged);
1250                                 LostFocus += new EventHandler (FocusChanged);
1251                         }
1252
1253                         void ItemsDoubleClick (object sender, EventArgs e)
1254                         {
1255                                 if (owner.activation == ItemActivation.Standard && owner.ItemActivate != null)
1256                                         owner.ItemActivate (this, e);
1257                         }
1258
1259                         void ItemsKeyDown (object sender, KeyEventArgs args)
1260                         {
1261                                 owner.OnKeyDown (args);
1262                         }
1263
1264                         void ItemsKeyUp (object sender, KeyEventArgs args)
1265                         {
1266                                 owner.OnKeyUp (args);
1267                         }
1268
1269                         enum BoxSelect {
1270                                 None,
1271                                 Normal,
1272                                 Shift,
1273                                 Control
1274                         }
1275
1276                         BoxSelect box_select_mode = BoxSelect.None;
1277                         ArrayList prev_selection;
1278                         Point box_select_start;
1279
1280                         Rectangle box_select_rect;
1281                         internal Rectangle BoxSelectRectangle {
1282                                 get { return box_select_rect; }
1283                                 set {
1284                                         if (box_select_rect == value)
1285                                                 return;
1286
1287                                         InvalidateBoxSelectRect ();
1288                                         box_select_rect = value;
1289                                         InvalidateBoxSelectRect ();
1290                                 }
1291                         }
1292
1293                         void InvalidateBoxSelectRect ()
1294                         {
1295                                 if (BoxSelectRectangle.Size.IsEmpty)
1296                                         return;
1297
1298                                 Rectangle edge = BoxSelectRectangle;
1299                                 edge.X -= 1;
1300                                 edge.Y -= 1;
1301                                 edge.Width += 2;
1302                                 edge.Height = 2;
1303                                 Invalidate (edge);
1304                                 edge.Y = BoxSelectRectangle.Bottom - 1;
1305                                 Invalidate (edge);
1306                                 edge.Y = BoxSelectRectangle.Y - 1;
1307                                 edge.Width = 2;
1308                                 edge.Height = BoxSelectRectangle.Height + 2;
1309                                 Invalidate (edge);
1310                                 edge.X = BoxSelectRectangle.Right - 1;
1311                                 Invalidate (edge);
1312                         }
1313
1314                         private Rectangle CalculateBoxSelectRectangle (Point pt)
1315                         {
1316                                 int left = Math.Min (box_select_start.X, pt.X);
1317                                 int right = Math.Max (box_select_start.X, pt.X);
1318                                 int top = Math.Min (box_select_start.Y, pt.Y);
1319                                 int bottom = Math.Max (box_select_start.Y, pt.Y);
1320                                 return Rectangle.FromLTRB (left, top, right, bottom);
1321                         }
1322
1323                         ArrayList BoxSelectedItems {
1324                                 get {
1325                                         ArrayList result = new ArrayList ();
1326                                         foreach (ListViewItem item in owner.Items) {
1327                                                 Rectangle r = item.Bounds;
1328                                                 r.X += r.Width / 4;
1329                                                 r.Y += r.Height / 4;
1330                                                 r.Width /= 2;
1331                                                 r.Height /= 2;
1332                                                 if (BoxSelectRectangle.IntersectsWith (r))
1333                                                         result.Add (item);
1334                                         }
1335                                         return result;
1336                                 }
1337                         }
1338
1339                         private bool PerformBoxSelection (Point pt)
1340                         {
1341                                 if (box_select_mode == BoxSelect.None)
1342                                         return false;
1343
1344                                 BoxSelectRectangle = CalculateBoxSelectRectangle (pt);
1345                                 
1346                                 ArrayList box_items = BoxSelectedItems;
1347
1348                                 ArrayList items;
1349
1350                                 switch (box_select_mode) {
1351
1352                                 case BoxSelect.Normal:
1353                                         items = box_items;
1354                                         break;
1355
1356                                 case BoxSelect.Control:
1357                                         items = new ArrayList ();
1358                                         foreach (ListViewItem item in prev_selection)
1359                                                 if (!box_items.Contains (item))
1360                                                         items.Add (item);
1361                                         foreach (ListViewItem item in box_items)
1362                                                 if (!prev_selection.Contains (item))
1363                                                         items.Add (item);
1364                                         break;
1365
1366                                 case BoxSelect.Shift:
1367                                         items = box_items;
1368                                         foreach (ListViewItem item in box_items)
1369                                                 prev_selection.Remove (item);
1370                                         foreach (ListViewItem item in prev_selection)
1371                                                 items.Add (item);
1372                                         break;
1373
1374                                 default:
1375                                         throw new Exception ("Unexpected Selection mode: " + box_select_mode);
1376                                 }
1377
1378                                 SuspendLayout ();
1379                                 owner.SelectItems (items);
1380                                 ResumeLayout ();
1381
1382                                 return true;
1383                         }
1384
1385                         private void ItemsMouseDown (object sender, MouseEventArgs me)
1386                         {
1387                                 if (owner.items.Count == 0)
1388                                         return;
1389
1390                                 Point pt = new Point (me.X, me.Y);
1391                                 foreach (ListViewItem item in owner.items) {
1392                                         if (item.CheckRectReal.Contains (pt)) {
1393                                                 CheckState curr_state = item.Checked ?  CheckState.Checked : CheckState.Unchecked;
1394                                                 item.Checked = !item.Checked;
1395
1396                                                 CheckState new_state = item.Checked ?  CheckState.Checked : CheckState.Unchecked;
1397
1398                                                 // Raise the ItemCheck event
1399                                                 ItemCheckEventArgs ice = new ItemCheckEventArgs (item.Index, curr_state, new_state);
1400                                                 owner.OnItemCheck (ice);
1401                                                 break;
1402                                         }
1403
1404                                         if (owner.View == View.Details && !owner.FullRowSelect) {
1405                                                 if (item.GetBounds (ItemBoundsPortion.Label).Contains (pt)) {
1406                                                         clicked_item = item;
1407                                                         break;
1408                                                 }
1409                                         } else {
1410                                                 if (item.Bounds.Contains (pt)) {
1411                                                         clicked_item = item;
1412                                                         break;
1413                                                 }
1414                                         }
1415                                 }
1416
1417
1418                                 if (clicked_item != null) {
1419                                         owner.SetFocusedItem (clicked_item);
1420                                         bool changed = !clicked_item.Selected;
1421                                         if (owner.MultiSelect && (XplatUI.State.ModifierKeys & Keys.Control) == 0)
1422                                                 owner.UpdateMultiSelection (clicked_item.Index);
1423                                         else
1424                                                 clicked_item.Selected = true;
1425                                 
1426                                         if (changed)
1427                                                 owner.OnSelectedIndexChanged (EventArgs.Empty);
1428
1429                                         // Raise double click if the item was clicked. On MS the
1430                                         // double click is only raised if you double click an item
1431                                         if (me.Clicks > 1 && clicked_item != null)
1432                                                 owner.OnDoubleClick (EventArgs.Empty);
1433                                         else if (me.Clicks == 1 && clicked_item != null)
1434                                                 owner.OnClick (EventArgs.Empty);
1435                                 } else {
1436                                         if (owner.MultiSelect) {
1437                                                 Keys mods = XplatUI.State.ModifierKeys;
1438                                                 if ((mods & Keys.Shift) != 0)
1439                                                         box_select_mode = BoxSelect.Shift;
1440                                                 else if ((mods & Keys.Control) != 0)
1441                                                         box_select_mode = BoxSelect.Control;
1442                                                 else
1443                                                         box_select_mode = BoxSelect.Normal;
1444                                                 box_select_start = pt; 
1445                                                 prev_selection = (ArrayList) owner.SelectedItems.list.Clone ();
1446                                         } else if (owner.selected_indices.Count > 0) {
1447                                                 owner.SelectedItems.Clear ();
1448                                                 owner.SelectedIndices.list.Clear ();
1449                                                 owner.OnSelectedIndexChanged (EventArgs.Empty);
1450                                         }
1451                                 }
1452                         }
1453
1454                         private void ItemsMouseMove (object sender, MouseEventArgs me)
1455                         {
1456                                 if (PerformBoxSelection (new Point (me.X, me.Y)))
1457                                         return;
1458
1459                                 if (owner.HoverSelection && hover_processed) {
1460
1461                                         Point pt = PointToClient (Control.MousePosition);
1462                                         ListViewItem item = owner.GetItemAt (pt.X, pt.Y);
1463                                         if (item == null || item.Selected)
1464                                                 return;
1465
1466                                         hover_processed = false;
1467                                         XplatUI.ResetMouseHover (Handle);
1468                                 }
1469                         }
1470
1471
1472                         private void ItemsMouseHover (object sender, EventArgs e)
1473                         {
1474                                 if (Capture || !owner.HoverSelection)
1475                                         return;
1476
1477                                 hover_processed = true;
1478                                 Point pt = PointToClient (Control.MousePosition);
1479                                 ListViewItem item = owner.GetItemAt (pt.X, pt.Y);
1480
1481                                 if (item == null)
1482                                         return;
1483
1484                                 item.Selected = true;
1485                                 owner.OnSelectedIndexChanged (new EventArgs ());
1486                         }
1487
1488                         private void ItemsMouseUp (object sender, MouseEventArgs me)
1489                         {
1490                                 Capture = false;
1491                                 if (owner.Items.Count == 0)
1492                                         return;
1493
1494                                 Point pt = new Point (me.X, me.Y);
1495
1496                                 Rectangle rect = Rectangle.Empty;
1497                                 if (clicked_item != null) {
1498                                         if (owner.view == View.Details && !owner.full_row_select)
1499                                                 rect = clicked_item.GetBounds (ItemBoundsPortion.Label);
1500                                         else
1501                                                 rect = clicked_item.Bounds;
1502
1503                                         if (rect.Contains (pt)) {
1504                                                 switch (owner.activation) {
1505                                                 case ItemActivation.OneClick:
1506                                                         owner.OnItemActivate (EventArgs.Empty);
1507                                                         break;
1508
1509                                                 case ItemActivation.TwoClick:
1510                                                         if (last_clicked_item == clicked_item) {
1511                                                                 owner.OnItemActivate (EventArgs.Empty);
1512                                                                 last_clicked_item = null;
1513                                                         } else
1514                                                                 last_clicked_item = clicked_item;
1515                                                         break;
1516                                                 default:
1517                                                         // DoubleClick activation is handled in another handler
1518                                                         break;
1519                                                 }
1520                                         }
1521                                 } else if (owner.SelectedItems.Count > 0 && BoxSelectRectangle.Size.IsEmpty) {
1522                                         // Need this to clean up background clicks
1523                                         owner.SelectedItems.Clear ();
1524                                         owner.SelectedIndices.list.Clear ();
1525                                         owner.OnSelectedIndexChanged (EventArgs.Empty);
1526                                 }
1527
1528                                 clicked_item = null;
1529                                 box_select_start = Point.Empty;
1530                                 BoxSelectRectangle = Rectangle.Empty;
1531                                 prev_selection = null;
1532                                 box_select_mode = BoxSelect.None;
1533                         }
1534
1535                         private void ItemsMouseWheel (object sender, MouseEventArgs me)
1536                         {
1537                                 if (owner.Items.Count == 0)
1538                                         return;
1539
1540                                 int lines = me.Delta / 120;
1541
1542                                 if (lines == 0)
1543                                         return;
1544
1545                                 switch (owner.View) {
1546                                 case View.Details:
1547                                 case View.SmallIcon:
1548                                         owner.Scroll (owner.v_scroll, -owner.Items [0].Bounds.Height * SystemInformation.MouseWheelScrollLines * lines);
1549                                         break;
1550                                 case View.LargeIcon:
1551                                         owner.Scroll (owner.v_scroll, -(owner.Items [0].Bounds.Height + ThemeEngine.Current.ListViewVerticalSpacing)  * lines);
1552                                         break;
1553                                 case View.List:
1554                                         owner.Scroll (owner.h_scroll, -owner.Items [0].Bounds.Width * lines);
1555                                         break;
1556                                 }
1557                         }
1558
1559                         void FocusChanged (object o, EventArgs args)
1560                         {
1561                                 if (owner.Items.Count == 0)
1562                                         return;
1563
1564                                 if (owner.FocusedItem == null)
1565                                         owner.SetFocusedItem (owner.Items [0]);
1566
1567                                 Invalidate (owner.FocusedItem.Bounds);
1568                         }
1569
1570                         internal override void OnPaintInternal (PaintEventArgs pe)
1571                         {
1572                                 ThemeEngine.Current.DrawListViewItems (pe.Graphics, pe.ClipRectangle, owner);
1573                         }
1574                 }
1575
1576                 internal override void OnPaintInternal (PaintEventArgs pe)
1577                 {
1578                         if (updating)
1579                                 return; 
1580                                 
1581                         CalculateScrollBars ();
1582                 }
1583
1584                 private void ListView_SizeChanged (object sender, EventArgs e)
1585                 {
1586                         CalculateListView (alignment);
1587                 }
1588                 
1589                 private void SetFocusedItem (ListViewItem item)
1590                 {
1591                         if (focused_item != null)
1592                                 focused_item.Focused = false;
1593                         
1594                         if (item != null)
1595                                 item.Focused = true;
1596                                 
1597                         focused_item = item;
1598                 }
1599
1600                 private void HorizontalScroller (object sender, EventArgs e)
1601                 {
1602                         // Avoid unnecessary flickering, when button is
1603                         // kept pressed at the end
1604                         if (h_marker != h_scroll.Value) {
1605                                 
1606                                 int pixels =  h_marker - h_scroll.Value;
1607                                 
1608                                 h_marker = h_scroll.Value;
1609                                 if (header_control.Visible)
1610                                         XplatUI.ScrollWindow (header_control.Handle, pixels, 0, false);
1611
1612                                 XplatUI.ScrollWindow (item_control.Handle, pixels, 0, false);
1613                         }
1614                 }
1615
1616                 private void VerticalScroller (object sender, EventArgs e)
1617                 {
1618                         // Avoid unnecessary flickering, when button is
1619                         // kept pressed at the end
1620                         if (v_marker != v_scroll.Value) {
1621                                 int pixels =  v_marker - v_scroll.Value;
1622                                 Rectangle area = item_control.ClientRectangle;
1623                                 v_marker = v_scroll.Value;
1624                                 XplatUI.ScrollWindow (item_control.Handle, area, 0, pixels, false);
1625                         }
1626                 }
1627                 #endregion      // Internal Methods Properties
1628
1629                 #region Protected Methods
1630                 protected override void CreateHandle ()
1631                 {
1632                         base.CreateHandle ();
1633                 }
1634
1635                 protected override void Dispose (bool disposing)
1636                 {                       
1637                         if (disposing) {                        
1638                                 h_scroll.Dispose ();
1639                                 v_scroll.Dispose ();
1640                                 
1641                                 large_image_list = null;
1642                                 small_image_list = null;
1643                                 state_image_list = null;
1644                         }
1645                         
1646                         base.Dispose (disposing);
1647                 }
1648
1649                 protected override bool IsInputKey (Keys keyData)
1650                 {
1651                         switch (keyData) {
1652                         case Keys.Up:
1653                         case Keys.Down:
1654                         case Keys.PageUp:
1655                         case Keys.PageDown:
1656                         case Keys.Right:
1657                         case Keys.Left:
1658                         case Keys.End:
1659                         case Keys.Home:                         
1660                                 return true;
1661
1662                         default:
1663                                 break;
1664                         }
1665                         
1666                         return base.IsInputKey (keyData);
1667                 }
1668
1669                 protected virtual void OnAfterLabelEdit (LabelEditEventArgs e)
1670                 {
1671                         if (AfterLabelEdit != null)
1672                                 AfterLabelEdit (this, e);
1673                 }
1674
1675                 protected virtual void OnBeforeLabelEdit (LabelEditEventArgs e)
1676                 {
1677                         if (BeforeLabelEdit != null)
1678                                 BeforeLabelEdit (this, e);
1679                 }
1680
1681                 protected virtual void OnColumnClick (ColumnClickEventArgs e)
1682                 {
1683                         if (ColumnClick != null)
1684                                 ColumnClick (this, e);
1685                 }
1686
1687                 protected override void OnEnabledChanged (EventArgs e)
1688                 {
1689                         base.OnEnabledChanged (e);
1690                 }
1691
1692                 protected override void OnFontChanged (EventArgs e)
1693                 {
1694                         base.OnFontChanged (e);
1695                         Redraw (true);
1696                 }
1697
1698                 protected override void OnHandleCreated (EventArgs e)
1699                 {
1700                         base.OnHandleCreated (e);
1701                 }
1702
1703                 protected override void OnHandleDestroyed (EventArgs e)
1704                 {
1705                         base.OnHandleDestroyed (e);
1706                 }
1707
1708                 protected virtual void OnItemActivate (EventArgs e)
1709                 {
1710                         if (ItemActivate != null)
1711                                 ItemActivate (this, e);
1712                 }
1713
1714                 protected virtual void OnItemCheck (ItemCheckEventArgs ice)
1715                 {
1716                         if (ItemCheck != null)
1717                                 ItemCheck (this, ice);
1718                 }
1719
1720                 protected virtual void OnItemDrag (ItemDragEventArgs e)
1721                 {
1722                         if (ItemDrag != null)
1723                                 ItemDrag (this, e);
1724                 }
1725
1726                 protected virtual void OnSelectedIndexChanged (EventArgs e)
1727                 {
1728                         if (SelectedIndexChanged != null)
1729                                 SelectedIndexChanged (this, e);
1730                 }
1731
1732                 protected override void OnSystemColorsChanged (EventArgs e)
1733                 {
1734                         base.OnSystemColorsChanged (e);
1735                 }
1736
1737                 protected void RealizeProperties ()
1738                 {
1739                         // FIXME: TODO
1740                 }
1741
1742                 protected void UpdateExtendedStyles ()
1743                 {
1744                         // FIXME: TODO
1745                 }
1746
1747                 protected override void WndProc (ref Message m)
1748                 {
1749                         base.WndProc (ref m);
1750                 }
1751                 #endregion // Protected Methods
1752
1753                 #region Public Instance Methods
1754                 public void ArrangeIcons ()
1755                 {
1756                         ArrangeIcons (this.alignment);
1757                 }
1758
1759                 public void ArrangeIcons (ListViewAlignment alignment)
1760                 {
1761                         // Icons are arranged only if view is set to LargeIcon or SmallIcon
1762                         if (view == View.LargeIcon || view == View.SmallIcon) {
1763                                 this.CalculateListView (alignment);
1764                                 // we have done the calculations already
1765                                 this.Redraw (false);
1766                         }
1767                 }
1768
1769                 public void BeginUpdate ()
1770                 {
1771                         // flag to avoid painting
1772                         updating = true;
1773                 }
1774
1775                 public void Clear ()
1776                 {
1777                         columns.Clear ();
1778                         items.Clear (); // Redraw (true) called here                    
1779                 }
1780
1781                 public void EndUpdate ()
1782                 {
1783                         // flag to avoid painting
1784                         updating = false;
1785
1786                         // probably, now we need a redraw with recalculations
1787                         this.Redraw (true);
1788                 }
1789
1790                 public void EnsureVisible (int index)
1791                 {
1792                         if (index < 0 || index >= items.Count || scrollable == false)
1793                                 return;
1794
1795                         Rectangle view_rect = item_control.ClientRectangle;
1796                         Rectangle bounds = items [index].Bounds;
1797
1798                         if (view_rect.Contains (bounds))
1799                                 return;
1800
1801                         if (bounds.Left < 0)
1802                                 h_scroll.Value += bounds.Left;
1803                         else if (bounds.Right > view_rect.Right)
1804                                 h_scroll.Value += (bounds.Right - view_rect.Right);
1805
1806                         if (bounds.Top < 0)
1807                                 v_scroll.Value += bounds.Top;
1808                         else if (bounds.Bottom > view_rect.Bottom)
1809                                 v_scroll.Value += (bounds.Bottom - view_rect.Bottom);
1810                 }
1811                 
1812                 public ListViewItem GetItemAt (int x, int y)
1813                 {
1814                         foreach (ListViewItem item in items) {
1815                                 if (item.Bounds.Contains (x, y))
1816                                         return item;
1817                         }
1818                         return null;
1819                 }
1820
1821                 public Rectangle GetItemRect (int index)
1822                 {
1823                         return GetItemRect (index, ItemBoundsPortion.Entire);
1824                 }
1825
1826                 public Rectangle GetItemRect (int index, ItemBoundsPortion portion)
1827                 {
1828                         if (index < 0 || index >= items.Count)
1829                                 throw new IndexOutOfRangeException ("index");
1830
1831                         return items [index].GetBounds (portion);
1832                 }
1833
1834                 public void Sort ()
1835                 {
1836                         if (sort_order != SortOrder.None)
1837                                 items.list.Sort (item_sorter);
1838
1839                         if (sort_order == SortOrder.Descending)
1840                                 items.list.Reverse ();
1841
1842                         this.Redraw (true);
1843                 }
1844
1845                 public override string ToString ()
1846                 {
1847                         int count = this.Items.Count;
1848
1849                         if (count == 0)
1850                                 return string.Format ("System.Windows.Forms.ListView, Items.Count: 0");
1851                         else
1852                                 return string.Format ("System.Windows.Forms.ListView, Items.Count: {0}, Items[0]: {1}", count, this.Items [0].ToString ());
1853                 }
1854                 #endregion      // Public Instance Methods
1855
1856
1857                 #region Subclasses
1858
1859                 class HeaderControl : Control {
1860
1861                         ListView owner;
1862                         bool column_resize_active = false;
1863                         ColumnHeader resize_column;
1864                         ColumnHeader clicked_column;
1865                         ColumnHeader drag_column;
1866                         int drag_x;
1867                         int drag_to_index = -1;
1868
1869                         public HeaderControl (ListView owner)
1870                         {
1871                                 this.owner = owner;
1872                                 MouseDown += new MouseEventHandler (HeaderMouseDown);
1873                                 MouseMove += new MouseEventHandler (HeaderMouseMove);
1874                                 MouseUp += new MouseEventHandler (HeaderMouseUp);
1875                         }
1876
1877                         private ColumnHeader ColumnAtX (int x)
1878                         {
1879                                 Point pt = new Point (x, 0);
1880                                 ColumnHeader result = null;
1881                                 foreach (ColumnHeader col in owner.Columns) {
1882                                         if (col.Rect.Contains (pt)) {
1883                                                 result = col;
1884                                                 break;
1885                                         }
1886                                 }
1887                                 return result;
1888                         }
1889
1890                         private int GetReorderedIndex (ColumnHeader col)
1891                         {
1892                                 if (owner.reordered_column_indices == null)
1893                                         return col.Index;
1894                                 else
1895                                         for (int i = 0; i < owner.Columns.Count; i++)
1896                                                 if (owner.reordered_column_indices [i] == col.Index)
1897                                                         return i;
1898                                 throw new Exception ("Column index missing from reordered array");
1899                         }
1900
1901                         private void HeaderMouseDown (object sender, MouseEventArgs me)
1902                         {
1903                                 if (resize_column != null) {
1904                                         column_resize_active = true;
1905                                         Capture = true;
1906                                         return;
1907                                 }
1908
1909                                 clicked_column = ColumnAtX (me.X + owner.h_marker);
1910
1911                                 if (clicked_column != null) {
1912                                         Capture = true;
1913                                         if (owner.AllowColumnReorder) {
1914                                                 drag_x = me.X;
1915                                                 drag_column = (ColumnHeader) (clicked_column as ICloneable).Clone ();
1916                                                 drag_column.column_rect = clicked_column.Rect;
1917                                                 drag_to_index = GetReorderedIndex (clicked_column);
1918                                         }
1919                                         clicked_column.pressed = true;
1920                                         Rectangle bounds = clicked_column.Rect;
1921                                         bounds.X -= owner.h_marker;
1922                                         Invalidate (bounds);
1923                                         return;
1924                                 }
1925                         }
1926
1927                         private void HeaderMouseMove (object sender, MouseEventArgs me)
1928                         {
1929                                 Point pt = new Point (me.X + owner.h_marker, me.Y);
1930
1931                                 if (column_resize_active)  {
1932                                         resize_column.Width = pt.X - resize_column.X;
1933                                         if (resize_column.Width < 0)
1934                                                 resize_column.Width = 0;
1935                                         return;
1936                                 }
1937
1938                                 resize_column = null;
1939
1940                                 if (clicked_column != null) {
1941                                         if (owner.AllowColumnReorder) {
1942                                                 Rectangle r;
1943
1944                                                 r = drag_column.column_rect;
1945                                                 r.X = clicked_column.Rect.X + me.X - drag_x;
1946                                                 drag_column.column_rect = r;
1947
1948                                                 int x = me.X + owner.h_marker;
1949                                                 ColumnHeader over = ColumnAtX (x);
1950                                                 if (over == null)
1951                                                         drag_to_index = owner.Columns.Count;
1952                                                 else if (x < over.X + over.Width / 2)
1953                                                         drag_to_index = GetReorderedIndex (over);
1954                                                 else
1955                                                         drag_to_index = GetReorderedIndex (over) + 1;
1956                                                 Invalidate ();
1957                                         } else {
1958                                                 ColumnHeader over = ColumnAtX (me.X + owner.h_marker);
1959                                                 bool pressed = clicked_column.pressed;
1960                                                 clicked_column.pressed = over == clicked_column;
1961                                                 if (clicked_column.pressed ^ pressed) {
1962                                                         Rectangle bounds = clicked_column.Rect;
1963                                                         bounds.X -= owner.h_marker;
1964                                                         Invalidate (bounds);
1965                                                 }
1966                                         }
1967                                         return;
1968                                 }
1969
1970                                 for (int i = 0; i < owner.Columns.Count; i++) {
1971                                         Rectangle zone = owner.Columns [i].Rect;
1972                                         zone.X = zone.Right - 5;
1973                                         zone.Width = 10;
1974                                         if (zone.Contains (pt)) {
1975                                                 resize_column = owner.Columns [i];
1976                                                 break;
1977                                         }
1978                                 }
1979
1980                                 if (resize_column == null)
1981                                         Cursor = Cursors.Default;
1982                                 else
1983                                         Cursor = Cursors.VSplit;
1984                         }
1985
1986                         void HeaderMouseUp (object sender, MouseEventArgs me)
1987                         {
1988                                 Capture = false;
1989
1990                                 if (column_resize_active) {
1991                                         column_resize_active = false;
1992                                         resize_column = null;
1993                                         Cursor = Cursors.Default;
1994                                         return;
1995                                 }
1996
1997                                 if (clicked_column != null && clicked_column.pressed) {
1998                                         clicked_column.pressed = false;
1999                                         Rectangle bounds = clicked_column.Rect;
2000                                         bounds.X -= owner.h_marker;
2001                                         Invalidate (bounds);
2002                                         owner.OnColumnClick (new ColumnClickEventArgs (clicked_column.Index));
2003                                 }
2004
2005                                 if (drag_column != null && owner.AllowColumnReorder) {
2006                                         drag_column = null;
2007                                         if (drag_to_index > GetReorderedIndex (clicked_column))
2008                                                 drag_to_index--;
2009                                         if (owner.GetReorderedColumn (drag_to_index) != clicked_column)
2010                                                 owner.ReorderColumn (clicked_column, drag_to_index);
2011                                         drag_to_index = -1;
2012                                         Invalidate ();
2013                                 }
2014
2015                                 clicked_column = null;
2016                         }
2017
2018                         internal override void OnPaintInternal (PaintEventArgs pe)
2019                         {
2020                                 if (owner.updating)
2021                                         return; 
2022                                 
2023                                 Theme theme = ThemeEngine.Current;
2024                                 theme.DrawListViewHeader (pe.Graphics, pe.ClipRectangle, this.owner);
2025
2026                                 if (drag_column == null)
2027                                         return;
2028
2029                                 int target_x;
2030                                 if (drag_to_index == owner.Columns.Count)
2031                                         target_x = owner.GetReorderedColumn (drag_to_index - 1).Rect.Right - owner.h_marker;
2032                                 else
2033                                         target_x = owner.GetReorderedColumn (drag_to_index).Rect.X - owner.h_marker;
2034                                 theme.DrawListViewHeaderDragDetails (pe.Graphics, owner, drag_column, target_x);
2035                         }
2036                 }
2037
2038                 public class CheckedIndexCollection : IList, ICollection, IEnumerable
2039                 {
2040                         internal ArrayList list;
2041                         private ListView owner;
2042
2043                         #region Public Constructor
2044                         public CheckedIndexCollection (ListView owner)
2045                         {
2046                                 list = new ArrayList ();
2047                                 this.owner = owner;
2048                         }
2049                         #endregion      // Public Constructor
2050
2051                         #region Public Properties
2052                         [Browsable (false)]
2053                         public int Count {
2054                                 get { return list.Count; }
2055                         }
2056
2057                         public bool IsReadOnly {
2058                                 get { return true; }
2059                         }
2060
2061                         public int this [int index] {
2062                                 get {
2063                                         if (index < 0 || index >= list.Count)
2064                                                 throw new ArgumentOutOfRangeException ("index");
2065                                         return (int) list [index];
2066                                 }
2067                         }
2068
2069                         bool ICollection.IsSynchronized {
2070                                 get { return false; }
2071                         }
2072
2073                         object ICollection.SyncRoot {
2074                                 get { return this; }
2075                         }
2076
2077                         bool IList.IsFixedSize {
2078                                 get { return true; }
2079                         }
2080
2081                         object IList.this [int index] {
2082                                 get { return this [index]; }
2083                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
2084                         }
2085                         #endregion      // Public Properties
2086
2087                         #region Public Methods
2088                         public bool Contains (int checkedIndex)
2089                         {
2090                                 return list.Contains (checkedIndex);
2091                         }
2092
2093                         public IEnumerator GetEnumerator ()
2094                         {
2095                                 return list.GetEnumerator ();
2096                         }
2097
2098                         void ICollection.CopyTo (Array dest, int index)
2099                         {
2100                                 list.CopyTo (dest, index);
2101                         }
2102
2103                         int IList.Add (object value)
2104                         {
2105                                 throw new NotSupportedException ("Add operation is not supported.");
2106                         }
2107
2108                         void IList.Clear ()
2109                         {
2110                                 throw new NotSupportedException ("Clear operation is not supported.");
2111                         }
2112
2113                         bool IList.Contains (object checkedIndex)
2114                         {
2115                                 return list.Contains (checkedIndex);
2116                         }
2117
2118                         int IList.IndexOf (object checkedIndex)
2119                         {
2120                                 return list.IndexOf (checkedIndex);
2121                         }
2122
2123                         void IList.Insert (int index, object value)
2124                         {
2125                                 throw new NotSupportedException ("Insert operation is not supported.");
2126                         }
2127
2128                         void IList.Remove (object value)
2129                         {
2130                                 throw new NotSupportedException ("Remove operation is not supported.");
2131                         }
2132
2133                         void IList.RemoveAt (int index)
2134                         {
2135                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
2136                         }
2137
2138                         public int IndexOf (int checkedIndex)
2139                         {
2140                                 return list.IndexOf (checkedIndex);
2141                         }
2142                         #endregion      // Public Methods
2143
2144                 }       // CheckedIndexCollection
2145
2146                 public class CheckedListViewItemCollection : IList, ICollection, IEnumerable
2147                 {
2148                         internal ArrayList list;
2149                         private ListView owner;
2150
2151                         #region Public Constructor
2152                         public CheckedListViewItemCollection (ListView owner)
2153                         {
2154                                 list = new ArrayList ();
2155                                 this.owner = owner;
2156                         }
2157                         #endregion      // Public Constructor
2158
2159                         #region Public Properties
2160                         [Browsable (false)]
2161                         public int Count {
2162                                 get { return list.Count; }
2163                         }
2164
2165                         public bool IsReadOnly {
2166                                 get { return true; }
2167                         }
2168
2169                         public ListViewItem this [int index] {
2170                                 get {
2171                                         if (index < 0 || index >= list.Count)
2172                                                 throw new ArgumentOutOfRangeException ("index");
2173                                         return (ListViewItem) list [index];
2174                                 }
2175                         }
2176
2177                         bool ICollection.IsSynchronized {
2178                                 get { return list.IsSynchronized; }
2179                         }
2180
2181                         object ICollection.SyncRoot {
2182                                 get { return this; }
2183                         }
2184
2185                         bool IList.IsFixedSize {
2186                                 get { return true; }
2187                         }
2188
2189                         object IList.this [int index] {
2190                                 get { return this [index]; }
2191                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
2192                         }
2193                         #endregion      // Public Properties
2194
2195                         #region Public Methods
2196                         public bool Contains (ListViewItem item)
2197                         {
2198                                 return list.Contains (item);
2199                         }
2200
2201                         public void CopyTo (Array dest, int index)
2202                         {
2203                                 list.CopyTo (dest, index);
2204                         }
2205
2206                         public IEnumerator GetEnumerator ()
2207                         {
2208                                 return list.GetEnumerator ();
2209                         }
2210
2211                         int IList.Add (object value)
2212                         {
2213                                 throw new NotSupportedException ("Add operation is not supported.");
2214                         }
2215
2216                         void IList.Clear ()
2217                         {
2218                                 throw new NotSupportedException ("Clear operation is not supported.");
2219                         }
2220
2221                         bool IList.Contains (object item)
2222                         {
2223                                 return list.Contains (item);
2224                         }
2225
2226                         int IList.IndexOf (object item)
2227                         {
2228                                 return list.IndexOf (item);
2229                         }
2230
2231                         void IList.Insert (int index, object value)
2232                         {
2233                                 throw new NotSupportedException ("Insert operation is not supported.");
2234                         }
2235
2236                         void IList.Remove (object value)
2237                         {
2238                                 throw new NotSupportedException ("Remove operation is not supported.");
2239                         }
2240
2241                         void IList.RemoveAt (int index)
2242                         {
2243                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
2244                         }
2245
2246                         public int IndexOf (ListViewItem item)
2247                         {
2248                                 return list.IndexOf (item);
2249                         }
2250                         #endregion      // Public Methods
2251
2252                 }       // CheckedListViewItemCollection
2253
2254                 public class ColumnHeaderCollection : IList, ICollection, IEnumerable
2255                 {
2256                         internal ArrayList list;
2257                         private ListView owner;
2258
2259                         #region Public Constructor
2260                         public ColumnHeaderCollection (ListView owner)
2261                         {
2262                                 list = new ArrayList ();
2263                                 this.owner = owner;
2264                         }
2265                         #endregion      // Public Constructor
2266
2267                         #region Public Properties
2268                         [Browsable (false)]
2269                         public int Count {
2270                                 get { return list.Count; }
2271                         }
2272
2273                         public bool IsReadOnly {
2274                                 get { return false; }
2275                         }
2276
2277                         public virtual ColumnHeader this [int index] {
2278                                 get {
2279                                         if (index < 0 || index >= list.Count)
2280                                                 throw new ArgumentOutOfRangeException ("index");
2281                                         return (ColumnHeader) list [index];
2282                                 }
2283                         }
2284
2285                         bool ICollection.IsSynchronized {
2286                                 get { return true; }
2287                         }
2288
2289                         object ICollection.SyncRoot {
2290                                 get { return this; }
2291                         }
2292
2293                         bool IList.IsFixedSize {
2294                                 get { return list.IsFixedSize; }
2295                         }
2296
2297                         object IList.this [int index] {
2298                                 get { return this [index]; }
2299                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
2300                         }
2301                         #endregion      // Public Properties
2302
2303                         #region Public Methods
2304                         public virtual int Add (ColumnHeader value)
2305                         {
2306                                 int idx;
2307                                 value.owner = this.owner;
2308                                 idx = list.Add (value);
2309                                 if (owner.IsHandleCreated) {
2310                                         owner.Redraw (true); 
2311                                 }
2312                                 return idx;
2313                         }
2314
2315                         public virtual ColumnHeader Add (string str, int width, HorizontalAlignment textAlign)
2316                         {
2317                                 ColumnHeader colHeader = new ColumnHeader (this.owner, str, textAlign, width);
2318                                 this.Add (colHeader);                                                                   
2319                                 return colHeader;
2320                         }
2321
2322                         public virtual void AddRange (ColumnHeader [] values)
2323                         {
2324                                 foreach (ColumnHeader colHeader in values) {
2325                                         colHeader.owner = this.owner;
2326                                         Add (colHeader);
2327                                 }
2328                                 
2329                                 owner.Redraw (true); 
2330                         }
2331
2332                         public virtual void Clear ()
2333                         {
2334                                 list.Clear ();
2335                                 owner.Redraw (true);
2336                         }
2337
2338                         public bool Contains (ColumnHeader value)
2339                         {
2340                                 return list.Contains (value);
2341                         }
2342
2343                         public IEnumerator GetEnumerator ()
2344                         {
2345                                 return list.GetEnumerator ();
2346                         }
2347
2348                         void ICollection.CopyTo (Array dest, int index)
2349                         {
2350                                 list.CopyTo (dest, index);
2351                         }
2352
2353                         int IList.Add (object value)
2354                         {
2355                                 if (! (value is ColumnHeader)) {
2356                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
2357                                 }
2358
2359                                 return this.Add ((ColumnHeader) value);
2360                         }
2361
2362                         bool IList.Contains (object value)
2363                         {
2364                                 if (! (value is ColumnHeader)) {
2365                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
2366                                 }
2367
2368                                 return this.Contains ((ColumnHeader) value);
2369                         }
2370
2371                         int IList.IndexOf (object value)
2372                         {
2373                                 if (! (value is ColumnHeader)) {
2374                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
2375                                 }
2376
2377                                 return this.IndexOf ((ColumnHeader) value);
2378                         }
2379
2380                         void IList.Insert (int index, object value)
2381                         {
2382                                 if (! (value is ColumnHeader)) {
2383                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
2384                                 }
2385
2386                                 this.Insert (index, (ColumnHeader) value);
2387                         }
2388
2389                         void IList.Remove (object value)
2390                         {
2391                                 if (! (value is ColumnHeader)) {
2392                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
2393                                 }
2394
2395                                 this.Remove ((ColumnHeader) value);
2396                         }
2397
2398                         public int IndexOf (ColumnHeader value)
2399                         {
2400                                 return list.IndexOf (value);
2401                         }
2402
2403                         public void Insert (int index, ColumnHeader value)
2404                         {
2405                                 // LAMESPEC: MSDOCS say greater than or equal to the value of the Count property
2406                                 // but it's really only greater.
2407                                 if (index < 0 || index > list.Count)
2408                                         throw new ArgumentOutOfRangeException ("index");
2409
2410                                 value.owner = this.owner;
2411                                 list.Insert (index, value);
2412                                 owner.Redraw (true);
2413                         }
2414
2415                         public void Insert (int index, string str, int width, HorizontalAlignment textAlign)
2416                         {
2417                                 ColumnHeader colHeader = new ColumnHeader (this.owner, str, textAlign, width);
2418                                 this.Insert (index, colHeader);
2419                         }
2420
2421                         public virtual void Remove (ColumnHeader column)
2422                         {
2423                                 // TODO: Update Column internal index ?
2424                                 list.Remove (column);
2425                                 owner.Redraw (true);
2426                         }
2427
2428                         public virtual void RemoveAt (int index)
2429                         {
2430                                 if (index < 0 || index >= list.Count)
2431                                         throw new ArgumentOutOfRangeException ("index");
2432
2433                                 // TODO: Update Column internal index ?
2434                                 list.RemoveAt (index);
2435                                 owner.Redraw (true);
2436                         }
2437                         #endregion      // Public Methods
2438                         
2439
2440                 }       // ColumnHeaderCollection
2441
2442                 public class ListViewItemCollection : IList, ICollection, IEnumerable
2443                 {
2444                         internal ArrayList list;
2445                         private ListView owner;
2446
2447                         #region Public Constructor
2448                         public ListViewItemCollection (ListView owner)
2449                         {
2450                                 list = new ArrayList ();
2451                                 this.owner = owner;
2452                         }
2453                         #endregion      // Public Constructor
2454
2455                         #region Public Properties
2456                         [Browsable (false)]
2457                         public int Count {
2458                                 get { return list.Count; }
2459                         }
2460
2461                         public bool IsReadOnly {
2462                                 get { return false; }
2463                         }
2464
2465                         public virtual ListViewItem this [int displayIndex] {
2466                                 get {
2467                                         if (displayIndex < 0 || displayIndex >= list.Count)
2468                                                 throw new ArgumentOutOfRangeException ("displayIndex");
2469                                         return (ListViewItem) list [displayIndex];
2470                                 }
2471
2472                                 set {
2473                                         if (displayIndex < 0 || displayIndex >= list.Count)
2474                                                 throw new ArgumentOutOfRangeException ("displayIndex");
2475
2476                                         if (list.Contains (value))
2477                                                 throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "value");
2478
2479                                         value.Owner = owner;
2480                                         list [displayIndex] = value;
2481
2482                                         owner.Redraw (true);
2483                                 }
2484                         }
2485
2486                         bool ICollection.IsSynchronized {
2487                                 get { return true; }
2488                         }
2489
2490                         object ICollection.SyncRoot {
2491                                 get { return this; }
2492                         }
2493
2494                         bool IList.IsFixedSize {
2495                                 get { return list.IsFixedSize; }
2496                         }
2497
2498                         object IList.this [int index] {
2499                                 get { return this [index]; }
2500                                 set {
2501                                         if (value is ListViewItem)
2502                                                 this [index] = (ListViewItem) value;
2503                                         else
2504                                                 this [index] = new ListViewItem (value.ToString ());
2505                                 }
2506                         }
2507                         #endregion      // Public Properties
2508
2509                         #region Public Methods
2510                         public virtual ListViewItem Add (ListViewItem value)
2511                         {
2512                                 if (list.Contains (value))
2513                                         throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "value");
2514
2515                                 value.Owner = owner;
2516                                 list.Add (value);
2517
2518                                 if (owner.Sorting != SortOrder.None)
2519                                         owner.Sort ();
2520
2521                                 owner.Redraw (true);
2522
2523                                 return value;
2524                         }
2525
2526                         public virtual ListViewItem Add (string text)
2527                         {
2528                                 ListViewItem item = new ListViewItem (text);
2529                                 return this.Add (item);
2530                         }
2531
2532                         public virtual ListViewItem Add (string text, int imageIndex)
2533                         {
2534                                 ListViewItem item = new ListViewItem (text, imageIndex);
2535                                 return this.Add (item);
2536                         }
2537
2538                         public void AddRange (ListViewItem [] values)
2539                         {
2540                                 list.Clear ();
2541                                 owner.SelectedItems.list.Clear ();
2542                                 owner.SelectedIndices.list.Clear ();
2543                                 owner.CheckedItems.list.Clear ();
2544                                 owner.CheckedIndices.list.Clear ();
2545
2546                                 foreach (ListViewItem item in values) {
2547                                         item.Owner = owner;
2548                                         list.Add (item);
2549                                 }
2550
2551                                 if (owner.Sorting != SortOrder.None)
2552                                         owner.Sort ();
2553
2554                                 owner.Redraw (true);
2555                         }
2556
2557                         public virtual void Clear ()
2558                         {
2559                                 owner.SetFocusedItem (null);
2560                                 owner.h_scroll.Value = owner.v_scroll.Value = 0;
2561                                 list.Clear ();
2562                                 owner.SelectedItems.list.Clear ();
2563                                 owner.SelectedIndices.list.Clear ();
2564                                 owner.CheckedItems.list.Clear ();
2565                                 owner.CheckedIndices.list.Clear ();
2566                                 owner.Redraw (true);
2567                         }
2568
2569                         public bool Contains (ListViewItem item)
2570                         {
2571                                 return list.Contains (item);
2572                         }
2573
2574                         public void CopyTo (Array dest, int index)
2575                         {
2576                                 list.CopyTo (dest, index);
2577                         }
2578
2579                         public IEnumerator GetEnumerator ()
2580                         {
2581                                 return list.GetEnumerator ();
2582                         }
2583
2584                         int IList.Add (object item)
2585                         {
2586                                 int result;
2587                                 ListViewItem li;
2588
2589                                 if (item is ListViewItem) {
2590                                         li = (ListViewItem) item;
2591                                         if (list.Contains (li))
2592                                                 throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "item");
2593                                 }
2594                                 else
2595                                         li = new ListViewItem (item.ToString ());
2596
2597                                 li.Owner = owner;
2598                                 result = list.Add (li);
2599                                 owner.Redraw (true);
2600
2601                                 return result;
2602                         }
2603
2604                         bool IList.Contains (object item)
2605                         {
2606                                 return list.Contains (item);
2607                         }
2608
2609                         int IList.IndexOf (object item)
2610                         {
2611                                 return list.IndexOf (item);
2612                         }
2613
2614                         void IList.Insert (int index, object item)
2615                         {
2616                                 if (item is ListViewItem)
2617                                         this.Insert (index, (ListViewItem) item);
2618                                 else
2619                                         this.Insert (index, item.ToString ());
2620                         }
2621
2622                         void IList.Remove (object item)
2623                         {
2624                                 Remove ((ListViewItem) item);
2625                         }
2626
2627                         public int IndexOf (ListViewItem item)
2628                         {
2629                                 return list.IndexOf (item);
2630                         }
2631
2632                         public ListViewItem Insert (int index, ListViewItem item)
2633                         {
2634                                 // LAMESPEC: MSDOCS say greater than or equal to the value of the Count property
2635                                 // but it's really only greater.
2636                                 if (index < 0 || index > list.Count)
2637                                         throw new ArgumentOutOfRangeException ("index");
2638
2639                                 if (list.Contains (item))
2640                                         throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "item");
2641
2642                                 item.Owner = owner;
2643                                 list.Insert (index, item);
2644                                 owner.Redraw (true);
2645                                 return item;
2646                         }
2647
2648                         public ListViewItem Insert (int index, string text)
2649                         {
2650                                 return this.Insert (index, new ListViewItem (text));
2651                         }
2652
2653                         public ListViewItem Insert (int index, string text, int imageIndex)
2654                         {
2655                                 return this.Insert (index, new ListViewItem (text, imageIndex));
2656                         }
2657
2658                         public virtual void Remove (ListViewItem item)
2659                         {
2660                                 if (!list.Contains (item))
2661                                         return;
2662                                         
2663                                 owner.SelectedItems.list.Remove (item);
2664                                 owner.SelectedIndices.list.Remove (item.Index);
2665                                 owner.CheckedItems.list.Remove (item);
2666                                 owner.CheckedIndices.list.Remove (item.Index);
2667                                 list.Remove (item);
2668                                 owner.Redraw (true);                            
2669                         }
2670
2671                         public virtual void RemoveAt (int index)
2672                         {
2673                                 if (index < 0 || index >= list.Count)
2674                                         throw new ArgumentOutOfRangeException ("index");
2675
2676                                 list.RemoveAt (index);
2677                                 owner.SelectedItems.list.RemoveAt (index);
2678                                 owner.SelectedIndices.list.RemoveAt (index);
2679                                 owner.CheckedItems.list.RemoveAt (index);
2680                                 owner.CheckedIndices.list.RemoveAt (index);
2681                                 owner.Redraw (false);
2682                         }
2683                         #endregion      // Public Methods
2684
2685                 }       // ListViewItemCollection
2686
2687                 public class SelectedIndexCollection : IList, ICollection, IEnumerable
2688                 {
2689                         internal ArrayList list;
2690                         private ListView owner;
2691
2692                         #region Public Constructor
2693                         public SelectedIndexCollection (ListView owner)
2694                         {
2695                                 list = new ArrayList ();
2696                                 this.owner = owner;
2697                         }
2698                         #endregion      // Public Constructor
2699
2700                         #region Public Properties
2701                         [Browsable (false)]
2702                         public int Count {
2703                                 get { return list.Count; }
2704                         }
2705
2706                         public bool IsReadOnly {
2707                                 get { return true; }
2708                         }
2709
2710                         public int this [int index] {
2711                                 get {
2712                                         if (index < 0 || index >= list.Count)
2713                                                 throw new ArgumentOutOfRangeException ("index");
2714                                         return (int) list [index];
2715                                 }
2716                         }
2717
2718                         bool ICollection.IsSynchronized {
2719                                 get { return list.IsSynchronized; }
2720                         }
2721
2722                         object ICollection.SyncRoot {
2723                                 get { return this; }
2724                         }
2725
2726                         bool IList.IsFixedSize {
2727                                 get { return true; }
2728                         }
2729
2730                         object IList.this [int index] {
2731                                 get { return this [index]; }
2732                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
2733                         }
2734                         #endregion      // Public Properties
2735
2736                         #region Public Methods
2737                         public bool Contains (int selectedIndex)
2738                         {
2739                                 return list.Contains (selectedIndex);
2740                         }
2741
2742                         public void CopyTo (Array dest, int index)
2743                         {
2744                                 list.CopyTo (dest, index);
2745                         }
2746
2747                         public IEnumerator GetEnumerator ()
2748                         {
2749                                 return list.GetEnumerator ();
2750                         }
2751
2752                         int IList.Add (object value)
2753                         {
2754                                 throw new NotSupportedException ("Add operation is not supported.");
2755                         }
2756
2757                         void IList.Clear ()
2758                         {
2759                                 throw new NotSupportedException ("Clear operation is not supported.");
2760                         }
2761
2762                         bool IList.Contains (object selectedIndex)
2763                         {
2764                                 return list.Contains (selectedIndex);
2765                         }
2766
2767                         int IList.IndexOf (object selectedIndex)
2768                         {
2769                                 return list.IndexOf (selectedIndex);
2770                         }
2771
2772                         void IList.Insert (int index, object value)
2773                         {
2774                                 throw new NotSupportedException ("Insert operation is not supported.");
2775                         }
2776
2777                         void IList.Remove (object value)
2778                         {
2779                                 throw new NotSupportedException ("Remove operation is not supported.");
2780                         }
2781
2782                         void IList.RemoveAt (int index)
2783                         {
2784                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
2785                         }
2786
2787                         public int IndexOf (int selectedIndex)
2788                         {
2789                                 return list.IndexOf (selectedIndex);
2790                         }
2791                         #endregion      // Public Methods
2792
2793                 }       // SelectedIndexCollection
2794
2795                 public class SelectedListViewItemCollection : IList, ICollection, IEnumerable
2796                 {
2797                         internal ArrayList list;
2798                         private ListView owner;
2799
2800                         #region Public Constructor
2801                         public SelectedListViewItemCollection (ListView owner)
2802                         {
2803                                 list = new ArrayList ();
2804                                 this.owner = owner;
2805                         }
2806                         #endregion      // Public Constructor
2807
2808                         #region Public Properties
2809                         [Browsable (false)]
2810                         public int Count {
2811                                 get { return list.Count; }
2812                         }
2813
2814                         public bool IsReadOnly {
2815                                 get { return true; }
2816                         }
2817
2818                         public ListViewItem this [int index] {
2819                                 get {
2820                                         if (index < 0 || index >= list.Count)
2821                                                 throw new ArgumentOutOfRangeException ("index");
2822                                         return (ListViewItem) list [index];
2823                                 }
2824                         }
2825
2826                         bool ICollection.IsSynchronized {
2827                                 get { return list.IsSynchronized; }
2828                         }
2829
2830                         object ICollection.SyncRoot {
2831                                 get { return this; }
2832                         }
2833
2834                         bool IList.IsFixedSize {
2835                                 get { return true; }
2836                         }
2837
2838                         object IList.this [int index] {
2839                                 get { return this [index]; }
2840                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
2841                         }
2842                         #endregion      // Public Properties
2843
2844                         #region Public Methods
2845                         public void Clear ()
2846                         {
2847                                 ArrayList copy = (ArrayList) list.Clone ();
2848                                 for (int i = 0; i < copy.Count; i++)
2849                                         ((ListViewItem) copy [i]).Selected = false;
2850
2851                                 list.Clear ();
2852                         }
2853
2854                         public bool Contains (ListViewItem item)
2855                         {
2856                                 return list.Contains (item);
2857                         }
2858
2859                         public void CopyTo (Array dest, int index)
2860                         {
2861                                 list.CopyTo (dest, index);
2862                         }
2863
2864                         public IEnumerator GetEnumerator ()
2865                         {
2866                                 return list.GetEnumerator ();
2867                         }
2868
2869                         int IList.Add (object value)
2870                         {
2871                                 throw new NotSupportedException ("Add operation is not supported.");
2872                         }
2873
2874                         bool IList.Contains (object item)
2875                         {
2876                                 return list.Contains (item);
2877                         }
2878
2879                         int IList.IndexOf (object item)
2880                         {
2881                                 return list.IndexOf (item);
2882                         }
2883
2884                         void IList.Insert (int index, object value)
2885                         {
2886                                 throw new NotSupportedException ("Insert operation is not supported.");
2887                         }
2888
2889                         void IList.Remove (object value)
2890                         {
2891                                 throw new NotSupportedException ("Remove operation is not supported.");
2892                         }
2893
2894                         void IList.RemoveAt (int index)
2895                         {
2896                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
2897                         }
2898
2899                         public int IndexOf (ListViewItem item)
2900                         {
2901                                 return list.IndexOf (item);
2902                         }
2903                         #endregion      // Public Methods
2904
2905                 }       // SelectedListViewItemCollection
2906
2907                 #endregion // Subclasses
2908         }
2909 }