2007-03-13 Jonathan Chambers <joncham@gmail.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 //      Daniel Nauck (dna(at)mono-project(dot)de)
27 //
28 // TODO:
29 //   - Feedback for item activation, change in cursor types as mouse moves.
30 //   - Drag and drop
31
32
33 // NOT COMPLETE
34
35
36 using System.Collections;
37 using System.ComponentModel;
38 using System.ComponentModel.Design;
39 using System.Drawing;
40 using System.Runtime.InteropServices;
41 using System.Globalization;
42 #if NET_2_0
43 using System.Collections.Generic;
44 #endif
45
46 namespace System.Windows.Forms
47 {
48         [DefaultEvent ("SelectedIndexChanged")]
49         [DefaultProperty ("Items")]
50         [Designer ("System.Windows.Forms.Design.ListViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
51 #if NET_2_0
52         [ClassInterface (ClassInterfaceType.AutoDispatch)]
53         [ComVisible (true)]
54         [Docking (DockingBehavior.Ask)]
55 #endif
56         public class ListView : Control
57         {
58                 private ItemActivation activation = ItemActivation.Standard;
59                 private ListViewAlignment alignment = ListViewAlignment.Top;
60                 private bool allow_column_reorder;
61                 private bool auto_arrange = true;
62                 private bool check_boxes;
63                 private readonly CheckedIndexCollection checked_indices;
64                 private readonly CheckedListViewItemCollection checked_items;
65                 private readonly ColumnHeaderCollection columns;
66                 internal ListViewItem focused_item;
67                 private bool full_row_select;
68                 private bool grid_lines;
69                 private ColumnHeaderStyle header_style = ColumnHeaderStyle.Clickable;
70                 private bool hide_selection = true;
71                 private bool hover_selection;
72                 private IComparer item_sorter;
73                 private readonly ListViewItemCollection items;
74 #if NET_2_0
75                 private readonly ListViewGroupCollection groups;
76                 private bool show_groups = true;
77 #endif
78                 private bool label_edit;
79                 private bool label_wrap = true;
80                 private bool multiselect = true;
81                 private bool scrollable = true;
82                 private readonly SelectedIndexCollection selected_indices;
83                 private readonly SelectedListViewItemCollection selected_items;
84                 private SortOrder sort_order = SortOrder.None;
85                 private ImageList state_image_list;
86                 private bool updating;
87                 private View view = View.LargeIcon;
88                 private int layout_wd;    // We might draw more than our client area
89                 private int layout_ht;    // therefore we need to have these two.
90                 HeaderControl header_control;
91                 internal ItemControl item_control;
92                 internal ScrollBar h_scroll; // used for scrolling horizontally
93                 internal ScrollBar v_scroll; // used for scrolling vertically
94                 internal int h_marker;          // Position markers for scrolling
95                 internal int v_marker;
96                 private int keysearch_tickcnt;
97                 private string keysearch_text;
98                 static private readonly int keysearch_keydelay = 1000;
99                 private int[] reordered_column_indices;
100                 private Point [] items_location;
101                 private ItemMatrixLocation [] items_matrix_location;
102                 private Size item_size; // used for caching item size
103 #if NET_2_0
104                 private Size tile_size;
105                 private bool virtual_mode;
106                 private int virtual_list_size;
107 #endif
108
109                 // internal variables
110                 internal ImageList large_image_list;
111                 internal ImageList small_image_list;
112                 internal Size text_size = Size.Empty;
113
114                 #region Events
115                 static object AfterLabelEditEvent = new object ();
116                 static object BeforeLabelEditEvent = new object ();
117                 static object ColumnClickEvent = new object ();
118                 static object ItemActivateEvent = new object ();
119                 static object ItemCheckEvent = new object ();
120                 static object ItemDragEvent = new object ();
121                 static object SelectedIndexChangedEvent = new object ();
122 #if NET_2_0
123                 static object ItemCheckedEvent = new object ();
124                 static object CacheVirtualItemsEvent = new object ();
125                 static object RetrieveVirtualItemEvent = new object ();
126 #endif
127
128                 public event LabelEditEventHandler AfterLabelEdit {
129                         add { Events.AddHandler (AfterLabelEditEvent, value); }
130                         remove { Events.RemoveHandler (AfterLabelEditEvent, value); }
131                 }
132
133                 [Browsable (false)]
134                 [EditorBrowsable (EditorBrowsableState.Never)]
135                 public new event EventHandler BackgroundImageChanged {
136                         add { base.BackgroundImageChanged += value; }
137                         remove { base.BackgroundImageChanged -= value; }
138                 }
139
140                 public event LabelEditEventHandler BeforeLabelEdit {
141                         add { Events.AddHandler (BeforeLabelEditEvent, value); }
142                         remove { Events.RemoveHandler (BeforeLabelEditEvent, value); }
143                 }
144
145                 public event ColumnClickEventHandler ColumnClick {
146                         add { Events.AddHandler (ColumnClickEvent, value); }
147                         remove { Events.RemoveHandler (ColumnClickEvent, value); }
148                 }
149
150                 public event EventHandler ItemActivate {
151                         add { Events.AddHandler (ItemActivateEvent, value); }
152                         remove { Events.RemoveHandler (ItemActivateEvent, value); }
153                 }
154
155                 public event ItemCheckEventHandler ItemCheck {
156                         add { Events.AddHandler (ItemCheckEvent, value); }
157                         remove { Events.RemoveHandler (ItemCheckEvent, value); }
158                 }
159
160 #if NET_2_0
161                 public event ItemCheckedEventHandler ItemChecked {
162                         add { Events.AddHandler (ItemCheckedEvent, value); }
163                         remove { Events.RemoveHandler (ItemCheckedEvent, value); }
164                 }
165 #endif
166
167                 public event ItemDragEventHandler ItemDrag {
168                         add { Events.AddHandler (ItemDragEvent, value); }
169                         remove { Events.RemoveHandler (ItemDragEvent, value); }
170                 }
171
172                 [Browsable (false)]
173                 [EditorBrowsable (EditorBrowsableState.Never)]
174                 public new event PaintEventHandler Paint {
175                         add { base.Paint += value; }
176                         remove { base.Paint -= value; }
177                 }
178
179                 public event EventHandler SelectedIndexChanged {
180                         add { Events.AddHandler (SelectedIndexChangedEvent, value); }
181                         remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
182                 }
183
184                 [Browsable (false)]
185                 [EditorBrowsable (EditorBrowsableState.Never)]
186                 public new event EventHandler TextChanged {
187                         add { base.TextChanged += value; }
188                         remove { base.TextChanged -= value; }
189                 }
190
191 #if NET_2_0
192                 public event CacheVirtualItemsEventHandler CacheVirtualItems {
193                         add { Events.AddHandler (CacheVirtualItemsEvent, value); }
194                         remove { Events.RemoveHandler (CacheVirtualItemsEvent, value); }
195                 }
196
197                 public event RetrieveVirtualItemEventHandler RetrieveVirtualItem {
198                         add { Events.AddHandler (RetrieveVirtualItemEvent, value); }
199                         remove { Events.RemoveHandler (RetrieveVirtualItemEvent, value); }
200                 }
201 #endif
202
203                 #endregion // Events
204
205                 #region Public Constructors
206                 public ListView ()
207                 {
208                         background_color = ThemeEngine.Current.ColorWindow;
209                         items = new ListViewItemCollection (this);
210 #if NET_2_0
211                         groups = new ListViewGroupCollection (this);
212 #endif
213                         checked_indices = new CheckedIndexCollection (this);
214                         checked_items = new CheckedListViewItemCollection (this);
215                         columns = new ColumnHeaderCollection (this);
216                         foreground_color = SystemColors.WindowText;
217                         selected_indices = new SelectedIndexCollection (this);
218                         selected_items = new SelectedListViewItemCollection (this);
219                         items_location = new Point [16];
220                         items_matrix_location = new ItemMatrixLocation [16];
221
222                         border_style = BorderStyle.Fixed3D;
223
224                         header_control = new HeaderControl (this);
225                         header_control.Visible = false;
226                         Controls.AddImplicit (header_control);
227
228                         item_control = new ItemControl (this);
229                         Controls.AddImplicit (item_control);
230
231                         h_scroll = new ImplicitHScrollBar ();
232                         Controls.AddImplicit (this.h_scroll);
233
234                         v_scroll = new ImplicitVScrollBar ();
235                         Controls.AddImplicit (this.v_scroll);
236
237                         h_marker = v_marker = 0;
238                         keysearch_tickcnt = 0;
239
240                         // scroll bars are disabled initially
241                         h_scroll.Visible = false;
242                         h_scroll.ValueChanged += new EventHandler(HorizontalScroller);
243                         v_scroll.Visible = false;
244                         v_scroll.ValueChanged += new EventHandler(VerticalScroller);
245
246                         // event handlers
247                         base.KeyDown += new KeyEventHandler(ListView_KeyDown);
248                         SizeChanged += new EventHandler (ListView_SizeChanged);
249                         GotFocus += new EventHandler (FocusChanged);
250                         LostFocus += new EventHandler (FocusChanged);
251                         MouseWheel += new MouseEventHandler(ListView_MouseWheel);
252
253                         this.SetStyle (ControlStyles.UserPaint | ControlStyles.StandardClick
254 #if NET_2_0
255                                 | ControlStyles.UseTextForAccessibility
256 #endif
257                                 , false);
258                 }
259                 #endregion      // Public Constructors
260
261                 #region Private Internal Properties
262                 internal Size CheckBoxSize {
263                         get {
264                                 if (this.check_boxes) {
265                                         if (this.state_image_list != null)
266                                                 return this.state_image_list.ImageSize;
267                                         else
268                                                 return ThemeEngine.Current.ListViewCheckBoxSize;
269                                 }
270                                 return Size.Empty;
271                         }
272                 }
273
274                 internal Size ItemSize {
275                         get {
276                                 if (view != View.Details)
277                                         return item_size;
278
279                                 Size size = new Size ();
280                                 size.Height = item_size.Height;
281                                 for (int i = 0; i < columns.Count; i++)
282                                         size.Width += columns [i].Wd;
283
284                                 return size;
285                         }
286                         set {
287                                 item_size = value;
288                         }
289                 }
290
291                 #endregion      // Private Internal Properties
292
293                 #region  Protected Properties
294                 protected override CreateParams CreateParams {
295                         get { return base.CreateParams; }
296                 }
297
298                 protected override Size DefaultSize {
299                         get { return ThemeEngine.Current.ListViewDefaultSize; }
300                 }
301                 #endregion      // Protected Properties
302
303                 #region Public Instance Properties
304                 [DefaultValue (ItemActivation.Standard)]
305                 public ItemActivation Activation {
306                         get { return activation; }
307                         set { 
308                                 if (value != ItemActivation.Standard && value != ItemActivation.OneClick && 
309                                         value != ItemActivation.TwoClick) {
310                                         throw new InvalidEnumArgumentException (string.Format
311                                                 ("Enum argument value '{0}' is not valid for Activation", value));
312                                 }
313                                 
314                                 activation = value;
315                         }
316                 }
317
318                 [DefaultValue (ListViewAlignment.Top)]
319                 [Localizable (true)]
320                 public ListViewAlignment Alignment {
321                         get { return alignment; }
322                         set {
323                                 if (value != ListViewAlignment.Default && value != ListViewAlignment.Left && 
324                                         value != ListViewAlignment.SnapToGrid && value != ListViewAlignment.Top) {
325                                         throw new InvalidEnumArgumentException (string.Format 
326                                                 ("Enum argument value '{0}' is not valid for Alignment", value));
327                                 }
328                                 
329                                 if (this.alignment != value) {
330                                         alignment = value;
331                                         // alignment does not matter in Details/List views
332                                         if (this.view == View.LargeIcon || this.View == View.SmallIcon)
333                                                 this.Redraw (true);
334                                 }
335                         }
336                 }
337
338                 [DefaultValue (false)]
339                 public bool AllowColumnReorder {
340                         get { return allow_column_reorder; }
341                         set { allow_column_reorder = value; }
342                 }
343
344                 [DefaultValue (true)]
345                 public bool AutoArrange {
346                         get { return auto_arrange; }
347                         set {
348                                 if (auto_arrange != value) {
349                                         auto_arrange = value;
350                                         // autoarrange does not matter in Details/List views
351                                         if (this.view == View.LargeIcon || this.View == View.SmallIcon)
352                                                 this.Redraw (true);
353                                 }
354                         }
355                 }
356
357                 public override Color BackColor {
358                         get {
359                                 if (background_color.IsEmpty)
360                                         return ThemeEngine.Current.ColorWindow;
361                                 else
362                                         return background_color;
363                         }
364                         set { background_color = value; }
365                 }
366
367                 [Browsable (false)]
368                 [EditorBrowsable (EditorBrowsableState.Never)]
369                 public override Image BackgroundImage {
370                         get { return base.BackgroundImage; }
371                         set { base.BackgroundImage = value; }
372                 }
373
374                 [DefaultValue (BorderStyle.Fixed3D)]
375                 [DispId (-504)]
376                 public BorderStyle BorderStyle {
377                         get { return InternalBorderStyle; }
378                         set { InternalBorderStyle = value; }
379                 }
380
381                 [DefaultValue (false)]
382                 public bool CheckBoxes {
383                         get { return check_boxes; }
384                         set {
385                                 if (check_boxes != value) {
386 #if NET_2_0
387                                         if (value && View == View.Tile)
388                                                 throw new NotSupportedException ("CheckBoxes are not"
389                                                         + " supported in Tile view. Choose a different"
390                                                         + " view or set CheckBoxes to false.");
391 #endif
392
393                                         check_boxes = value;
394                                         this.Redraw (true);
395                                 }
396                         }
397                 }
398
399                 [Browsable (false)]
400                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
401                 public CheckedIndexCollection CheckedIndices {
402                         get { return checked_indices; }
403                 }
404
405                 [Browsable (false)]
406                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
407                 public CheckedListViewItemCollection CheckedItems {
408                         get { return checked_items; }
409                 }
410
411                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
412                 [Localizable (true)]
413                 [MergableProperty (false)]
414                 public ColumnHeaderCollection Columns {
415                         get { return columns; }
416                 }
417
418                 [Browsable (false)]
419                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
420                 public ListViewItem FocusedItem {
421                         get {
422                                 return focused_item;
423                         }
424 #if NET_2_0
425                         set {
426                                 throw new NotImplementedException ();
427                         }
428 #endif
429                 }
430
431                 public override Color ForeColor {
432                         get {
433                                 if (foreground_color.IsEmpty)
434                                         return ThemeEngine.Current.ColorWindowText;
435                                 else
436                                         return foreground_color;
437                         }
438                         set { foreground_color = value; }
439                 }
440
441                 [DefaultValue (false)]
442                 public bool FullRowSelect {
443                         get { return full_row_select; }
444                         set { full_row_select = value; }
445                 }
446
447                 [DefaultValue (false)]
448                 public bool GridLines {
449                         get { return grid_lines; }
450                         set {
451                                 if (grid_lines != value) {
452                                         grid_lines = value;
453                                         this.Redraw (false);
454                                 }
455                         }
456                 }
457
458                 [DefaultValue (ColumnHeaderStyle.Clickable)]
459                 public ColumnHeaderStyle HeaderStyle {
460                         get { return header_style; }
461                         set {
462                                 if (header_style == value)
463                                         return;
464
465                                 switch (value) {
466                                 case ColumnHeaderStyle.Clickable:
467                                 case ColumnHeaderStyle.Nonclickable:
468                                 case ColumnHeaderStyle.None:
469                                         break;
470                                 default:
471                                         throw new InvalidEnumArgumentException (string.Format 
472                                                 ("Enum argument value '{0}' is not valid for ColumnHeaderStyle", value));
473                                 }
474                                 
475                                 header_style = value;
476                                 if (view == View.Details)
477                                         Redraw (true);
478                         }
479                 }
480
481                 [DefaultValue (true)]
482                 public bool HideSelection {
483                         get { return hide_selection; }
484                         set {
485                                 if (hide_selection != value) {
486                                         hide_selection = value;
487                                         this.Redraw (false);
488                                 }
489                         }
490                 }
491
492                 [DefaultValue (false)]
493                 public bool HoverSelection {
494                         get { return hover_selection; }
495                         set { hover_selection = value; }
496                 }
497
498                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
499                 [Localizable (true)]
500                 [MergableProperty (false)]
501                 public ListViewItemCollection Items {
502                         get { return items; }
503                 }
504
505                 [DefaultValue (false)]
506                 public bool LabelEdit {
507                         get { return label_edit; }
508                         set { label_edit = value; }
509                 }
510
511                 [DefaultValue (true)]
512                 [Localizable (true)]
513                 public bool LabelWrap {
514                         get { return label_wrap; }
515                         set {
516                                 if (label_wrap != value) {
517                                         label_wrap = value;
518                                         this.Redraw (true);
519                                 }
520                         }
521                 }
522
523                 [DefaultValue (null)]
524                 public ImageList LargeImageList {
525                         get { return large_image_list; }
526                         set {
527                                 large_image_list = value;
528                                 this.Redraw (true);
529                         }
530                 }
531
532                 [Browsable (false)]
533                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
534                 public IComparer ListViewItemSorter {
535                         get {
536                                 if (View != View.SmallIcon && View != View.LargeIcon && item_sorter is ItemComparer)
537                                         return null;
538                                 return item_sorter;
539                         }
540                         set {
541                                 if (item_sorter != value) {
542                                         item_sorter = value;
543                                         Sort ();
544                                 }
545                         }
546                 }
547
548                 [DefaultValue (true)]
549                 public bool MultiSelect {
550                         get { return multiselect; }
551                         set { multiselect = value; }
552                 }
553
554                 [DefaultValue (true)]
555                 public bool Scrollable {
556                         get { return scrollable; }
557                         set {
558                                 if (scrollable != value) {
559                                         scrollable = value;
560                                         this.Redraw (true);
561                                 }
562                         }
563                 }
564
565                 [Browsable (false)]
566                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
567                 public SelectedIndexCollection SelectedIndices {
568                         get { return selected_indices; }
569                 }
570
571                 [Browsable (false)]
572                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
573                 public SelectedListViewItemCollection SelectedItems {
574                         get { return selected_items; }
575                 }
576
577 #if NET_2_0
578                 [DefaultValue(true)]
579                 public bool ShowGroups {
580                         get { return show_groups; }
581                         set {
582                                 if (show_groups != value) {
583                                         show_groups = value;
584                                         Redraw(true);
585                                 }
586                         }
587                 }
588
589                 [LocalizableAttribute (true)]
590                 [MergableProperty (false)]
591                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
592                 public ListViewGroupCollection Groups {
593                         get { return groups; }
594                 }
595 #endif
596
597                 [DefaultValue (null)]
598                 public ImageList SmallImageList {
599                         get { return small_image_list; }
600                         set {
601                                 small_image_list = value;
602                                 this.Redraw (true);
603                         }
604                 }
605
606                 [DefaultValue (SortOrder.None)]
607                 public SortOrder Sorting {
608                         get { return sort_order; }
609                         set { 
610                                 if (!Enum.IsDefined (typeof (SortOrder), value)) {
611                                         throw new InvalidEnumArgumentException ("value", (int) value,
612                                                 typeof (SortOrder));
613                                 }
614                                 
615                                 if (sort_order == value)
616                                         return;
617
618                                 sort_order = value;
619
620 #if NET_2_0
621                                 if (virtual_mode) // Sorting is not allowed in virtual mode
622                                         return;
623 #endif
624
625                                 if (value == SortOrder.None) {
626                                         if (item_sorter != null) {
627                                                 // ListViewItemSorter should never be reset for SmallIcon
628                                                 // and LargeIcon view
629                                                 if (View != View.SmallIcon && View != View.LargeIcon)
630 #if NET_2_0
631                                                         item_sorter = null;
632 #else
633                                                         // in .NET 1.1, only internal IComparer would be
634                                                         // set to null
635                                                         if (item_sorter is ItemComparer)
636                                                                 item_sorter = null;
637 #endif
638                                         }
639                                         this.Redraw (false);
640                                 } else {
641                                         if (item_sorter == null)
642                                                 item_sorter = new ItemComparer (value);
643                                         if (item_sorter is ItemComparer) {
644 #if NET_2_0
645                                                 item_sorter = new ItemComparer (value);
646 #else
647                                                 // in .NET 1.1, the sort order is not updated for
648                                                 // SmallIcon and LargeIcon views if no custom IComparer
649                                                 // is set
650                                                 if (View != View.SmallIcon && View != View.LargeIcon)
651                                                         item_sorter = new ItemComparer (value);
652 #endif
653                                         }
654                                         Sort ();
655                                 }
656                         }
657                 }
658
659                 [DefaultValue (null)]
660                 public ImageList StateImageList {
661                         get { return state_image_list; }
662                         set {
663                                 state_image_list = value;
664                                 this.Redraw (true);
665                         }
666                 }
667
668                 [Bindable (false)]
669                 [Browsable (false)]
670                 [EditorBrowsable (EditorBrowsableState.Never)]
671                 public override string Text {
672                         get { return base.Text; } 
673                         set {
674                                 if (value == base.Text)
675                                         return;
676
677                                 base.Text = value;
678                                 this.Redraw (true);
679                         }
680                 }
681
682 #if NET_2_0
683                 [Browsable (true)]
684                 public Size TileSize {
685                         get {
686                                 return tile_size;
687                         }
688                         set {
689                                 if (value.Width <= 0 || value.Height <= 0)
690                                         throw new ArgumentOutOfRangeException ("value");
691
692                                 tile_size = value;
693                                 Redraw (true);
694                         }
695                 }
696 #endif
697
698                 [Browsable (false)]
699                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
700                 public ListViewItem TopItem {
701                         get {
702                                 // there is no item
703                                 if (this.items.Count == 0)
704                                         return null;
705                                 // if contents are not scrolled
706                                 // it is the first item
707                                 else if (h_marker == 0 && v_marker == 0)
708                                         return this.items [0];
709                                 // do a hit test for the scrolled position
710                                 else {
711                                         for (int i = 0; i < items.Count; i++) {
712                                                 Point item_loc = GetItemLocation (i);
713                                                 if (item_loc.X >= 0 && item_loc.Y >= 0)
714                                                         return items [i];
715                                         }
716                                         return null;
717                                 }
718                         }
719 #if NET_2_0
720                         set {
721                                 throw new NotImplementedException ();
722                         }
723 #endif
724                 }
725
726 #if NET_2_0
727                 [EditorBrowsable (EditorBrowsableState.Advanced)]
728                 [DefaultValue (true)]
729                 [Browsable (false)]
730                 [MonoInternalNote ("Stub, not implemented")]
731                 public bool UseCompatibleStateImageBehavior {
732                         get {
733                                 return false;
734                         }
735                         set {
736                         }
737                 }
738 #endif
739
740                 [DefaultValue (View.LargeIcon)]
741                 public View View {
742                         get { return view; }
743                         set { 
744                                 if (!Enum.IsDefined (typeof (View), value))
745                                         throw new InvalidEnumArgumentException ("value", (int) value,
746                                                 typeof (View));
747
748                                 if (view != value) {
749 #if NET_2_0
750                                         if (CheckBoxes && value == View.Tile)
751                                                 throw new NotSupportedException ("CheckBoxes are not"
752                                                         + " supported in Tile view. Choose a different"
753                                                         + " view or set CheckBoxes to false.");
754 #endif
755
756                                         h_scroll.Value = v_scroll.Value = 0;
757                                         view = value; 
758                                         Redraw (true);
759                                 }
760                         }
761                 }
762
763 #if NET_2_0
764                 public bool VirtualMode {
765                         get {
766                                 return virtual_mode;
767                         }
768                         set {
769                                 if (virtual_mode == value)
770                                         return;
771
772                                 if (!virtual_mode && items.Count > 0)
773                                         throw new InvalidOperationException ();
774
775                                 virtual_mode = value;
776                                 Redraw (true);
777                         }
778                 }
779
780                 public int VirtualListSize {
781                         get {
782                                 return virtual_list_size;
783                         }
784                         set {
785                                 if (value < 0)
786                                         throw new ArgumentException ("value");
787
788                                 if (virtual_list_size == value)
789                                         return;
790
791                                 virtual_list_size = value;
792                                 if (virtual_mode)
793                                         Redraw (true);
794                         }
795                 }
796 #endif
797                 #endregion      // Public Instance Properties
798
799                 #region Internal Methods Properties
800                 
801                 internal int FirstVisibleIndex {
802                         get {
803                                 // there is no item
804                                 if (this.items.Count == 0)
805                                         return 0;
806                                 
807                                 if (h_marker == 0 && v_marker == 0)
808                                         return 0;
809                                 
810                                 Size item_size = ItemSize;
811                                 for (int i = 0; i < items.Count; i++) {
812                                         Rectangle item_rect = new Rectangle (GetItemLocation (i), item_size);
813                                         if (item_rect.Right >= 0 && item_rect.Bottom >= 0)
814                                                 return i;
815                                 }
816
817                                 return 0;
818                         }
819                 }
820
821                 
822                 internal int LastVisibleIndex {
823                         get {
824                                 for (int i = FirstVisibleIndex; i < Items.Count; i++) {
825                                         if (View == View.List || Alignment == ListViewAlignment.Left) {
826                                                 if (GetItemLocation (i).X > item_control.ClientRectangle.Right)
827                                                         return i - 1;
828                                         } else {
829                                                 if (GetItemLocation (i).Y > item_control.ClientRectangle.Bottom)
830                                                         return i - 1;
831                                         }
832                                 }
833                                 
834                                 return Items.Count - 1;
835                         }
836                 }
837                 
838                 internal void OnSelectedIndexChanged ()
839                 {
840                         if (IsHandleCreated)
841                                 OnSelectedIndexChanged (EventArgs.Empty);
842                 }
843
844                 internal int TotalWidth {
845                         get { return Math.Max (this.Width, this.layout_wd); }
846                 }
847
848                 internal int TotalHeight {
849                         get { return Math.Max (this.Height, this.layout_ht); }
850                 }
851
852                 internal void Redraw (bool recalculate)
853                 {
854                         // Avoid calculations when control is being updated
855                         if (updating)
856                                 return;
857 #if NET_2_0
858                         // VirtualMode doesn't do any calculations until handle is created
859                         if (virtual_mode && !IsHandleCreated)
860                                 return;
861 #endif
862
863
864                         if (recalculate)
865                                 CalculateListView (this.alignment);
866
867                         Refresh ();
868                 }
869
870                 const int text_padding = 15;
871
872                 internal Size GetChildColumnSize (int index)
873                 {
874                         Size ret_size = Size.Empty;
875                         ColumnHeader col = this.columns [index];
876
877                         if (col.Width == -2) { // autosize = max(items, columnheader)
878                                 Size size = Size.Ceiling (this.DeviceContext.MeasureString
879                                         (col.Text, this.Font));
880                                 size.Width += text_padding;
881                                 ret_size = BiggestItem (index);
882                                 if (size.Width > ret_size.Width)
883                                         ret_size = size;
884                         }
885                         else { // -1 and all the values < -2 are put under one category
886                                 ret_size = BiggestItem (index);
887                                 // fall back to empty columns' width if no subitem is available for a column
888                                 if (ret_size.IsEmpty) {
889                                         ret_size.Width = ThemeEngine.Current.ListViewEmptyColumnWidth;
890                                         if (col.Text.Length > 0)
891                                                 ret_size.Height = Size.Ceiling (this.DeviceContext.MeasureString
892                                                                                 (col.Text, this.Font)).Height;
893                                         else
894                                                 ret_size.Height = this.Font.Height;
895                                 }
896                         }
897
898                         ret_size.Height += text_padding;
899
900                         // adjust the size for icon and checkbox for 0th column
901                         if (index == 0) {
902                                 ret_size.Width += (this.CheckBoxSize.Width + 4);
903                                 if (this.small_image_list != null)
904                                         ret_size.Width += this.small_image_list.ImageSize.Width;
905                         }
906                         return ret_size;
907                 }
908
909                 // Returns the size of biggest item text in a column.
910                 private Size BiggestItem (int col)
911                 {
912                         Size temp = Size.Empty;
913                         Size ret_size = Size.Empty;
914
915 #if NET_2_0
916                         // VirtualMode uses the first item text size
917                         if (virtual_mode && items.Count > 0) {
918                                 ListViewItem item = items [0];
919                                 ret_size = Size.Ceiling (DeviceContext.MeasureString (item.SubItems [col].Text,
920                                                         Font));
921                         } else {
922 #endif
923                                 // 0th column holds the item text, we check the size of
924                                 // the various subitems falling in that column and get
925                                 // the biggest one's size.
926                                 foreach (ListViewItem item in items) {
927                                         if (col >= item.SubItems.Count)
928                                                 continue;
929
930                                         temp = Size.Ceiling (DeviceContext.MeasureString
931                                                                 (item.SubItems [col].Text, Font));
932                                         if (temp.Width > ret_size.Width)
933                                                 ret_size = temp;
934                                 }
935 #if NET_2_0
936                         }
937 #endif
938
939                         // adjustment for space
940                         if (!ret_size.IsEmpty)
941                                 ret_size.Width += 4;
942
943                         return ret_size;
944                 }
945
946                 const int max_wrap_padding = 38;
947
948                 // Sets the size of the biggest item text as per the view
949                 private void CalcTextSize ()
950                 {
951                         // clear the old value
952                         text_size = Size.Empty;
953
954                         if (items.Count == 0)
955                                 return;
956
957                         text_size = BiggestItem (0);
958
959                         if (view == View.LargeIcon && this.label_wrap) {
960                                 Size temp = Size.Empty;
961                                 if (this.check_boxes)
962                                         temp.Width += 2 * this.CheckBoxSize.Width;
963                                 int icon_w = LargeImageList == null ? 12 : LargeImageList.ImageSize.Width;
964                                 temp.Width += icon_w + max_wrap_padding;
965                                 // wrapping is done for two lines only
966                                 if (text_size.Width > temp.Width) {
967                                         text_size.Width = temp.Width;
968                                         text_size.Height *= 2;
969                                 }
970                         }
971                         else if (view == View.List) {
972                                 // in list view max text shown in determined by the
973                                 // control width, even if scolling is enabled.
974                                 int max_wd = this.Width - (this.CheckBoxSize.Width - 2);
975                                 if (this.small_image_list != null)
976                                         max_wd -= this.small_image_list.ImageSize.Width;
977
978                                 if (text_size.Width > max_wd)
979                                         text_size.Width = max_wd;
980                         }
981
982                         // we do the default settings, if we have got 0's
983                         if (text_size.Height <= 0)
984                                 text_size.Height = this.Font.Height;
985                         if (text_size.Width <= 0)
986                                 text_size.Width = this.Width;
987
988                         // little adjustment
989                         text_size.Width += 4;
990                         text_size.Height += 2;
991                 }
992
993                 private void Scroll (ScrollBar scrollbar, int delta)
994                 {
995                         if (delta == 0 || !scrollbar.Visible)
996                                 return;
997
998                         int max;
999                         if (scrollbar == h_scroll)
1000                                 max = h_scroll.Maximum - item_control.Width;
1001                         else
1002                                 max = v_scroll.Maximum - item_control.Height;
1003
1004                         int val = scrollbar.Value + delta;
1005                         if (val > max)
1006                                 val = max;
1007                         else if (val < scrollbar.Minimum)
1008                                 val = scrollbar.Minimum;
1009                         scrollbar.Value = val;
1010                 }
1011
1012                 private void CalculateScrollBars ()
1013                 {
1014                         Rectangle client_area = ClientRectangle;
1015                         
1016                         if (!this.scrollable || this.items.Count <= 0) {
1017                                 h_scroll.Visible = false;
1018                                 v_scroll.Visible = false;
1019                                 item_control.Location = new Point (0, header_control.Height);
1020                                 item_control.Height = ClientRectangle.Width - header_control.Height;
1021                                 item_control.Width = ClientRectangle.Width;
1022                                 header_control.Width = ClientRectangle.Width;
1023                                 return;
1024                         }
1025
1026                         // Don't calculate if the view is not displayable
1027                         if (client_area.Height < 0 || client_area.Width < 0)
1028                                 return;
1029
1030                         // making a scroll bar visible might make
1031                         // other scroll bar visible
1032                         if (layout_wd > client_area.Right) {
1033                                 h_scroll.Visible = true;
1034                                 if ((layout_ht + h_scroll.Height) > client_area.Bottom)
1035                                         v_scroll.Visible = true;
1036                                 else
1037                                         v_scroll.Visible = false;
1038                         } else if (layout_ht > client_area.Bottom) {
1039                                 v_scroll.Visible = true;
1040                                 if ((layout_wd + v_scroll.Width) > client_area.Right)
1041                                         h_scroll.Visible = true;
1042                                 else
1043                                         h_scroll.Visible = false;
1044                         } else {
1045                                 h_scroll.Visible = false;
1046                                 v_scroll.Visible = false;
1047                         }
1048
1049                         item_control.Height = ClientRectangle.Height - header_control.Height;
1050
1051                         if (h_scroll.is_visible) {
1052                                 h_scroll.Location = new Point (client_area.X, client_area.Bottom - h_scroll.Height);
1053                                 h_scroll.Minimum = 0;
1054
1055                                 // if v_scroll is visible, adjust the maximum of the
1056                                 // h_scroll to account for the width of v_scroll
1057                                 if (v_scroll.Visible) {
1058                                         h_scroll.Maximum = layout_wd + v_scroll.Width;
1059                                         h_scroll.Width = client_area.Width - v_scroll.Width;
1060                                 }
1061                                 else {
1062                                         h_scroll.Maximum = layout_wd;
1063                                         h_scroll.Width = client_area.Width;
1064                                 }
1065
1066                                 h_scroll.LargeChange = client_area.Width;
1067                                 h_scroll.SmallChange = Font.Height;
1068                                 item_control.Height -= h_scroll.Height;
1069                         }
1070
1071                         if (header_control.is_visible)
1072                                 header_control.Width = ClientRectangle.Width;
1073                         item_control.Width = ClientRectangle.Width;
1074
1075                         if (v_scroll.is_visible) {
1076                                 v_scroll.Location = new Point (client_area.Right - v_scroll.Width, client_area.Y);
1077                                 v_scroll.Minimum = 0;
1078
1079                                 // if h_scroll is visible, adjust the maximum of the
1080                                 // v_scroll to account for the height of h_scroll
1081                                 if (h_scroll.Visible) {
1082                                         v_scroll.Maximum = layout_ht + h_scroll.Height;
1083                                         v_scroll.Height = client_area.Height; // - h_scroll.Height already done 
1084                                 } else {
1085                                         v_scroll.Maximum = layout_ht;
1086                                         v_scroll.Height = client_area.Height;
1087                                 }
1088
1089                                 v_scroll.LargeChange = client_area.Height;
1090                                 v_scroll.SmallChange = Font.Height;
1091                                 if (header_control.Visible)
1092                                         header_control.Width -= v_scroll.Width;
1093                                 item_control.Width -= v_scroll.Width;
1094                         }
1095                 }
1096
1097 #if NET_2_0
1098                 internal int GetReorderedColumnIndex (ColumnHeader column)
1099                 {
1100                         if (reordered_column_indices == null)
1101                                 return column.Index;
1102
1103                         for (int i = 0; i < Columns.Count; i++)
1104                                 if (reordered_column_indices [i] == column.Index)
1105                                         return i;
1106
1107                         return -1;
1108                 }
1109 #endif
1110
1111                 internal ColumnHeader GetReorderedColumn (int index)
1112                 {
1113                         if (reordered_column_indices == null)
1114                                 return Columns [index];
1115                         else
1116                                 return Columns [reordered_column_indices [index]];
1117                 }
1118
1119                 internal void ReorderColumn (ColumnHeader col, int index, bool fireEvent)
1120                 {
1121 #if NET_2_0
1122                         if (fireEvent) {
1123                                 ColumnReorderedEventHandler eh = (ColumnReorderedEventHandler) (Events [ColumnReorderedEvent]);
1124                                 if (eh != null){
1125                                         ColumnReorderedEventArgs args = new ColumnReorderedEventArgs (col.Index, index, col);
1126
1127                                         eh (this, args);
1128                                         if (args.Cancel) {
1129                                                 header_control.Invalidate ();
1130                                                 item_control.Invalidate ();
1131                                                 return;
1132                                         }
1133                                 }
1134                         }
1135 #endif
1136                         int column_count = Columns.Count;
1137
1138                         if (reordered_column_indices == null) {
1139                                 reordered_column_indices = new int [column_count];
1140                                 for (int i = 0; i < column_count; i++)
1141                                         reordered_column_indices [i] = i;
1142                         }
1143
1144                         if (reordered_column_indices [index] == col.Index)
1145                                 return;
1146
1147                         int[] curr = reordered_column_indices;
1148                         int [] result = new int [column_count];
1149                         int curr_idx = 0;
1150                         for (int i = 0; i < column_count; i++) {
1151                                 if (curr_idx < column_count && curr [curr_idx] == col.Index)
1152                                         curr_idx++;
1153
1154                                 if (i == index)
1155                                         result [i] = col.Index;
1156                                 else
1157                                         result [i] = curr [curr_idx++];
1158                         }
1159
1160                         ReorderColumns (result, true);
1161                 }
1162
1163                 internal void ReorderColumns (int [] display_indices, bool redraw)
1164                 {
1165                         reordered_column_indices = display_indices;
1166                         for (int i = 0; i < Columns.Count; i++) {
1167                                 ColumnHeader col = Columns [i];
1168                                 col.InternalDisplayIndex = reordered_column_indices [i];
1169                         }
1170                         if (redraw && view == View.Details && IsHandleCreated) {
1171                                 LayoutDetails ();
1172                                 header_control.Invalidate ();
1173                                 item_control.Invalidate ();
1174                         }
1175                 }
1176
1177                 internal void AddColumn (ColumnHeader newCol, int index, bool redraw)
1178                 {
1179                         int column_count = Columns.Count;
1180                         newCol.SetListView (this);
1181
1182                         int [] display_indices = new int [column_count];
1183                         for (int i = 0; i < column_count; i++) {
1184                                 ColumnHeader col = Columns [i];
1185                                 if (i == index) {
1186                                         display_indices [i] = index;
1187                                 } else {
1188                                         int display_index = col.InternalDisplayIndex;
1189                                         if (display_index < index) {
1190                                                 display_indices [i] = display_index;
1191                                         } else {
1192                                                 display_indices [i] = (display_index + 1);
1193                                         }
1194                                 }
1195                         }
1196
1197                         ReorderColumns (display_indices, redraw);
1198                         Invalidate ();
1199                 }
1200
1201                 Size LargeIconItemSize
1202                 {
1203                         get {
1204                                 int image_w = LargeImageList == null ? 12 : LargeImageList.ImageSize.Width;
1205                                 int image_h = LargeImageList == null ? 2 : LargeImageList.ImageSize.Height;
1206                                 int w = CheckBoxSize.Width + 2 + Math.Max (text_size.Width, image_w);
1207                                 int h = text_size.Height + 2 + Math.Max (CheckBoxSize.Height, image_h);
1208                                 return new Size (w, h);
1209                         }
1210                 }
1211
1212                 Size SmallIconItemSize {
1213                         get {
1214                                 int image_w = SmallImageList == null ? 0 : SmallImageList.ImageSize.Width;
1215                                 int image_h = SmallImageList == null ? 0 : SmallImageList.ImageSize.Height;
1216                                 int w = text_size.Width + 2 + CheckBoxSize.Width + image_w;
1217                                 int h = Math.Max (text_size.Height, Math.Max (CheckBoxSize.Height, image_h));
1218                                 return new Size (w, h);
1219                         }
1220                 }
1221
1222 #if NET_2_0
1223                 Size TileItemSize {
1224                         get {
1225                                 // Calculate tile size if needed
1226                                 // It appears that using Font.Size instead of a SizeF value can give us
1227                                 // a slightly better approach to the proportions defined in .Net
1228                                 if (tile_size == Size.Empty) {
1229                                         int image_w = LargeImageList == null ? 0 : LargeImageList.ImageSize.Width;
1230                                         int image_h = LargeImageList == null ? 0 : LargeImageList.ImageSize.Height;
1231                                         int w = (int)Font.Size * ThemeEngine.Current.ListViewTileWidthFactor + image_w + 4;
1232                                         int h = Math.Max ((int)Font.Size * ThemeEngine.Current.ListViewTileHeightFactor, image_h);
1233                                 
1234                                         tile_size = new Size (w, h);
1235                                 }
1236                         
1237                                 return tile_size;
1238                         }
1239                 }
1240 #endif
1241
1242                 int GetDetailsItemHeight ()
1243                 {
1244                         int item_height;
1245                         int checkbox_height = CheckBoxes ? CheckBoxSize.Height : 0;
1246                         int small_image_height = SmallImageList == null ? 0 : SmallImageList.ImageSize.Height;
1247                         item_height = Math.Max (checkbox_height, text_size.Height);
1248                         item_height = Math.Max (item_height, small_image_height);
1249                         return item_height;
1250                 }
1251
1252
1253                 void SetItemLocation (int index, int x, int y, int row, int col)
1254                 {
1255                         Point old_location = items_location [index];
1256                         if (old_location.X == x && old_location.Y == y)
1257                                 return;
1258
1259                         Size item_size = ItemSize;
1260                         Rectangle old_rect = new Rectangle (GetItemLocation (index), item_size);
1261
1262                         items_location [index] = new Point (x, y);
1263                         items_matrix_location [index] = new ItemMatrixLocation (row, col);
1264
1265                         // Invalidate both previous and new bounds
1266                         item_control.Invalidate (old_rect);
1267                         item_control.Invalidate (new Rectangle (GetItemLocation (index), item_size));
1268                 }
1269
1270                 int rows;
1271                 int cols;
1272                 int[,] item_index_matrix;
1273
1274                 void LayoutIcons (Size item_size, bool left_aligned, int x_spacing, int y_spacing)
1275                 {
1276                         header_control.Visible = false;
1277                         header_control.Size = Size.Empty;
1278                         item_control.Visible = true;
1279                         item_control.Location = Point.Empty;
1280                         ItemSize = item_size; // Cache item size
1281
1282                         if (items.Count == 0)
1283                                 return;
1284
1285                         Size sz = item_size;
1286                         Rectangle area = ClientRectangle;
1287
1288                         if (left_aligned) {
1289                                 rows = (int) Math.Floor ((double)(area.Height - h_scroll.Height + y_spacing) / (double)(sz.Height + y_spacing));
1290                                 if (rows <= 0)
1291                                         rows = 1;
1292                                 cols = (int) Math.Ceiling ((double)items.Count / (double)rows);
1293                         } else {
1294                                 cols = (int) Math.Floor ((double)(area.Width - v_scroll.Width + x_spacing) / (double)(sz.Width + x_spacing));
1295                                 if (cols <= 0)
1296                                         cols = 1;
1297                                 rows = (int) Math.Ceiling ((double)items.Count / (double)cols);
1298                         }
1299
1300                         layout_ht = rows * (sz.Height + y_spacing) - y_spacing;
1301                         layout_wd = cols * (sz.Width + x_spacing) - x_spacing;
1302                         item_index_matrix = new int [rows, cols];
1303                         int row = 0;
1304                         int col = 0;
1305                         for (int i = 0; i < items.Count; i++) {
1306                                 int x = col * (sz.Width + x_spacing);
1307                                 int y = row * (sz.Height + y_spacing);
1308                                 SetItemLocation (i, x, y, row, col);
1309                                 item_index_matrix [row, col] = i;
1310 #if NET_2_0
1311                                 if (!virtual_mode) // Virtual mode sets Layout until draw time
1312 #endif
1313                                         items [i].Layout ();
1314
1315                                 if (left_aligned) {
1316                                         if (++row == rows) {
1317                                                 row = 0;
1318                                                 col++;
1319                                         }
1320                                 } else {
1321                                         if (++col == cols) {
1322                                                 col = 0;
1323                                                 row++;
1324                                         }
1325                                 }
1326                         }
1327
1328                         item_control.Size = new Size (layout_wd, layout_ht);
1329                 }
1330
1331                 void LayoutHeader ()
1332                 {
1333                         int x = 0;
1334                         for (int i = 0; i < Columns.Count; i++) {
1335                                 ColumnHeader col = GetReorderedColumn (i);
1336                                 col.X = x;
1337                                 col.Y = 0;
1338                                 col.CalcColumnHeader ();
1339                                 x += col.Wd;
1340                         }
1341
1342                         if (x < ClientRectangle.Width)
1343                                 x = ClientRectangle.Width;
1344
1345                         if (header_style == ColumnHeaderStyle.None) {
1346                                 header_control.Visible = false;
1347                                 header_control.Size = Size.Empty;
1348                         } else {
1349                                 header_control.Width = x;
1350                                 header_control.Height = columns [0].Ht;
1351                                 header_control.Visible = true;
1352                         }
1353                 }
1354
1355                 void LayoutDetails ()
1356                 {
1357                         if (columns.Count == 0) {
1358                                 header_control.Visible = false;
1359                                 item_control.Visible = false;
1360                                 return;
1361                         }
1362
1363                         LayoutHeader ();
1364
1365                         item_control.Visible = true;
1366                         item_control.Location = new Point (0, header_control.Height);
1367
1368                         int item_height = GetDetailsItemHeight ();
1369                         ItemSize = new Size (0, item_height); // We only cache Height for details view
1370
1371                         int y = 0; 
1372                         if (items.Count > 0) {
1373                                 for (int i = 0; i < items.Count; i++) {
1374                                         SetItemLocation (i, 0, y, 0, 0);
1375 #if NET_2_0
1376                                         if (!virtual_mode) // Virtual mode sets Layout until draw time
1377 #endif
1378                                                 items [i].Layout ();
1379
1380                                         y += item_height + 2;
1381                                 }
1382
1383                                 // some space for bottom gridline
1384                                 if (grid_lines)
1385                                         y += 2;
1386                         }
1387
1388                         layout_wd = Math.Max (header_control.Width, item_control.Width);
1389                         layout_ht = y + header_control.Height;
1390                 }
1391
1392                 private void AdjustItemsPositionArray (int count)
1393                 {
1394                         if (items_location.Length >= count)
1395                                 return;
1396
1397                         // items_location and items_matrix_location must keep the same length
1398                         count = Math.Max (count, items_location.Length * 2);
1399                         items_location = new Point [count];
1400                         items_matrix_location = new ItemMatrixLocation [count];
1401                 }
1402
1403                 private void CalculateListView (ListViewAlignment align)
1404                 {
1405                         CalcTextSize ();
1406
1407                         AdjustItemsPositionArray (items.Count);
1408
1409                         switch (view) {
1410                         case View.Details:
1411                                 LayoutDetails ();
1412                                 break;
1413
1414                         case View.SmallIcon:
1415                                 LayoutIcons (SmallIconItemSize, alignment == ListViewAlignment.Left, 4, 2);
1416                                 break;
1417
1418                         case View.LargeIcon:
1419                                 LayoutIcons (LargeIconItemSize, alignment == ListViewAlignment.Left,
1420                                         ThemeEngine.Current.ListViewHorizontalSpacing,
1421                                         ThemeEngine.Current.ListViewVerticalSpacing);
1422                                 break;
1423
1424                         case View.List:
1425                                 LayoutIcons (SmallIconItemSize, true, 4, 2);
1426                                 break;
1427 #if NET_2_0
1428                         case View.Tile:
1429                                 LayoutIcons (TileItemSize, alignment == ListViewAlignment.Left, 
1430                                                 ThemeEngine.Current.ListViewHorizontalSpacing,
1431                                                 ThemeEngine.Current.ListViewVerticalSpacing);
1432                                 break;
1433 #endif
1434                         }
1435
1436                         CalculateScrollBars ();
1437                 }
1438
1439                 internal Point GetItemLocation (int index)
1440                 {
1441                         Point loc = items_location [index];
1442                         loc.X -= h_marker; // Adjust to scroll
1443                         loc.Y -= v_marker;
1444
1445                         return loc;
1446                 }
1447
1448                 private bool KeySearchString (KeyEventArgs ke)
1449                 {
1450                         int current_tickcnt = Environment.TickCount;
1451                         if (keysearch_tickcnt > 0 && current_tickcnt - keysearch_tickcnt > keysearch_keydelay) {
1452                                 keysearch_text = string.Empty;
1453                         }
1454                         
1455                         keysearch_text += (char) ke.KeyData;
1456                         keysearch_tickcnt = current_tickcnt;
1457
1458                         int start = FocusedItem == null ? 0 : FocusedItem.Index;
1459                         int i = start;
1460                         while (true) {
1461                                 if (CultureInfo.CurrentCulture.CompareInfo.IsPrefix (Items[i].Text, keysearch_text,
1462                                         CompareOptions.IgnoreCase)) {
1463                                         SetFocusedItem (Items [i]);
1464                                         items [i].Selected = true;
1465                                         EnsureVisible (i);
1466                                         break;
1467                                 }
1468                                 i = (i + 1 < Items.Count) ? i+1 : 0;
1469
1470                                 if (i == start)
1471                                         break;
1472                         }
1473                         return true;
1474                 }
1475
1476                 int GetAdjustedIndex (Keys key)
1477                 {
1478                         int result = -1;
1479
1480                         if (View == View.Details) {
1481                                 switch (key) {
1482                                 case Keys.Up:
1483                                         result = FocusedItem.Index - 1;
1484                                         break;
1485                                 case Keys.Down:
1486                                         result = FocusedItem.Index + 1;
1487                                         if (result == items.Count)
1488                                                 result = -1;
1489                                         break;
1490                                 case Keys.PageDown:
1491                                         int last_index = LastVisibleIndex;
1492                                         Rectangle item_rect = new Rectangle (GetItemLocation (last_index), ItemSize);
1493                                         if (item_rect.Bottom > item_control.ClientRectangle.Bottom)
1494                                                 last_index--;
1495                                         if (FocusedItem.Index == last_index) {
1496                                                 if (FocusedItem.Index < Items.Count - 1) {
1497                                                         int page_size = item_control.Height / ItemSize.Height - 1;
1498                                                         result = FocusedItem.Index + page_size - 1;
1499                                                         if (result >= Items.Count)
1500                                                                 result = Items.Count - 1;
1501                                                 }
1502                                         } else
1503                                                 result = last_index;
1504                                         break;
1505                                 case Keys.PageUp:
1506                                         int first_index = FirstVisibleIndex;
1507                                         if (GetItemLocation (first_index).Y < 0)
1508                                                 first_index++;
1509                                         if (FocusedItem.Index == first_index) {
1510                                                 if (first_index > 0) {
1511                                                         int page_size = item_control.Height / ItemSize.Height - 1;
1512                                                         result = first_index - page_size + 1;
1513                                                         if (result < 0)
1514                                                                 result = 0;
1515                                                 }
1516                                         } else
1517                                                 result = first_index;
1518                                         break;
1519                                 }
1520                                 return result;
1521                         }
1522
1523                         ItemMatrixLocation item_matrix_location = items_matrix_location [FocusedItem.Index];
1524                         int row = item_matrix_location.Row;
1525                         int col = item_matrix_location.Col;
1526
1527                         switch (key) {
1528                         case Keys.Left:
1529                                 if (col == 0)
1530                                         return -1;
1531                                 return item_index_matrix [row, col - 1];
1532
1533                         case Keys.Right:
1534                                 if (col == (cols - 1))
1535                                         return -1;
1536                                 while (item_index_matrix [row, col + 1] == 0) {
1537                                         row--;
1538                                         if (row < 0)
1539                                                 return -1;
1540                                 }
1541                                 return item_index_matrix [row, col + 1];
1542
1543                         case Keys.Up:
1544                                 if (row == 0)
1545                                         return -1;
1546                                 return item_index_matrix [row - 1, col];
1547
1548                         case Keys.Down:
1549                                 if (row == (rows - 1) || row == Items.Count - 1)
1550                                         return -1;
1551                                 while (item_index_matrix [row + 1, col] == 0) {
1552                                         col--;
1553                                         if (col < 0)
1554                                                 return -1;
1555                                 }
1556                                 return item_index_matrix [row + 1, col];
1557
1558                         default:
1559                                 return -1;
1560                         }
1561                 }
1562
1563                 ListViewItem selection_start;
1564
1565                 private bool SelectItems (ArrayList sel_items)
1566                 {
1567                         bool changed = false;
1568                         ArrayList curr_items = SelectedItems.List;
1569                         foreach (ListViewItem item in curr_items)
1570                                 if (!sel_items.Contains (item)) {
1571                                         item.Selected = false;
1572                                         changed = true;
1573                                 }
1574                         foreach (ListViewItem item in sel_items)
1575                                 if (!item.Selected) {
1576                                         item.Selected = true;
1577                                         changed = true;
1578                                 }
1579                         return changed;
1580                 }
1581
1582                 private void UpdateMultiSelection (int index, bool reselect)
1583                 {
1584                         bool shift_pressed = (XplatUI.State.ModifierKeys & Keys.Shift) != 0;
1585                         bool ctrl_pressed = (XplatUI.State.ModifierKeys & Keys.Control) != 0;
1586                         ListViewItem item = items [index];
1587
1588                         if (shift_pressed && selection_start != null) {
1589                                 ArrayList list = new ArrayList ();
1590                                 int start_index = selection_start.Index;
1591                                 int start = Math.Min (start_index, index);
1592                                 int end = Math.Max (start_index, index);
1593                                 if (View == View.Details) {
1594                                         for (int i = start; i <= end; i++)
1595                                                 list.Add (items [i]);
1596                                 } else {
1597                                         ItemMatrixLocation start_item_matrix_location = items_matrix_location [start];
1598                                         ItemMatrixLocation end_item_matrix_location = items_matrix_location [end];
1599                                         int left = Math.Min (start_item_matrix_location.Col, end_item_matrix_location.Col);
1600                                         int right = Math.Max (start_item_matrix_location.Col, end_item_matrix_location.Col);
1601                                         int top = Math.Min (start_item_matrix_location.Row, end_item_matrix_location.Row);
1602                                         int bottom = Math.Max (start_item_matrix_location.Row, end_item_matrix_location.Row);
1603
1604                                         for (int i = 0; i < items.Count; i++) {
1605                                                 ItemMatrixLocation item_matrix_loc = items_matrix_location [i];
1606
1607                                                 if (item_matrix_loc.Row >= top && item_matrix_loc.Row <= bottom &&
1608                                                                 item_matrix_loc.Col >= left && item_matrix_loc.Col <= right)
1609                                                         list.Add (items [i]);
1610                                         }
1611                                 }
1612                                 SelectItems (list);
1613                         } else if (ctrl_pressed) {
1614                                 item.Selected = !item.Selected;
1615                                 selection_start = item;
1616                         } else {
1617                                 if (!reselect) {
1618                                         // do not unselect, and reselect the item
1619                                         foreach (int itemIndex in SelectedIndices) {
1620                                                 if (index == itemIndex)
1621                                                         continue;
1622                                                 items [itemIndex].Selected = false;
1623                                         }
1624                                 } else {
1625                                         SelectedItems.Clear ();
1626                                         item.Selected = true;
1627                                 }
1628                                 selection_start = item;
1629                         }
1630                 }
1631
1632                 internal override bool InternalPreProcessMessage (ref Message msg)
1633                 {
1634                         if (msg.Msg == (int)Msg.WM_KEYDOWN) {
1635                                 Keys key_data = (Keys)msg.WParam.ToInt32();
1636                                 if (HandleNavKeys (key_data))
1637                                         return true;
1638                         } 
1639                         return base.InternalPreProcessMessage (ref msg);
1640                 }
1641
1642                 bool HandleNavKeys (Keys key_data)
1643                 {
1644                         if (Items.Count == 0 || !item_control.Visible)
1645                                 return false;
1646
1647                         if (FocusedItem == null)
1648                                 SetFocusedItem (Items [0]);
1649
1650                         switch (key_data) {
1651                         case Keys.End:
1652                                 SelectIndex (Items.Count - 1);
1653                                 break;
1654
1655                         case Keys.Home:
1656                                 SelectIndex (0);
1657                                 break;
1658
1659                         case Keys.Left:
1660                         case Keys.Right:
1661                         case Keys.Up:
1662                         case Keys.Down:
1663                         case Keys.PageUp:
1664                         case Keys.PageDown:
1665                                 SelectIndex (GetAdjustedIndex (key_data));
1666                                 break;
1667
1668                         default:
1669                                 return false;
1670                         }
1671
1672                         return true;
1673                 }
1674
1675                 void SelectIndex (int index)
1676                 {
1677                         if (index == -1)
1678                                 return;
1679
1680                         if (MultiSelect)
1681                                 UpdateMultiSelection (index, true);
1682                         else if (!items [index].Selected)
1683                                 items [index].Selected = true;
1684
1685                         SetFocusedItem (items [index]);
1686                         EnsureVisible (index);
1687                 }
1688
1689                 private void ListView_KeyDown (object sender, KeyEventArgs ke)
1690                 {
1691                         if (ke.Handled || Items.Count == 0 || !item_control.Visible)
1692                                 return;
1693
1694                         ke.Handled = KeySearchString (ke);
1695                 }
1696
1697                 private MouseEventArgs TranslateMouseEventArgs (MouseEventArgs args)
1698                 {
1699                         Point loc = PointToClient (Control.MousePosition);
1700                         return new MouseEventArgs (args.Button, args.Clicks, loc.X, loc.Y, args.Delta);
1701                 }
1702
1703                 internal class ItemControl : Control {
1704
1705                         ListView owner;
1706                         ListViewItem clicked_item;
1707                         ListViewItem last_clicked_item;
1708                         bool hover_processed = false;
1709                         bool checking = false;
1710                         
1711                         ListViewLabelEditTextBox edit_text_box;
1712                         internal ListViewItem edit_item;
1713                         LabelEditEventArgs edit_args;
1714
1715                         public ItemControl (ListView owner)
1716                         {
1717                                 this.owner = owner;
1718                                 DoubleClick += new EventHandler(ItemsDoubleClick);
1719                                 MouseDown += new MouseEventHandler(ItemsMouseDown);
1720                                 MouseMove += new MouseEventHandler(ItemsMouseMove);
1721                                 MouseHover += new EventHandler(ItemsMouseHover);
1722                                 MouseUp += new MouseEventHandler(ItemsMouseUp);
1723                         }
1724
1725                         void ItemsDoubleClick (object sender, EventArgs e)
1726                         {
1727                                 if (owner.activation == ItemActivation.Standard)
1728                                         owner.OnItemActivate (EventArgs.Empty);
1729                         }
1730
1731                         enum BoxSelect {
1732                                 None,
1733                                 Normal,
1734                                 Shift,
1735                                 Control
1736                         }
1737
1738                         BoxSelect box_select_mode = BoxSelect.None;
1739                         ArrayList prev_selection;
1740                         Point box_select_start;
1741
1742                         Rectangle box_select_rect;
1743                         internal Rectangle BoxSelectRectangle {
1744                                 get { return box_select_rect; }
1745                                 set {
1746                                         if (box_select_rect == value)
1747                                                 return;
1748
1749                                         InvalidateBoxSelectRect ();
1750                                         box_select_rect = value;
1751                                         InvalidateBoxSelectRect ();
1752                                 }
1753                         }
1754
1755                         void InvalidateBoxSelectRect ()
1756                         {
1757                                 if (BoxSelectRectangle.Size.IsEmpty)
1758                                         return;
1759
1760                                 Rectangle edge = BoxSelectRectangle;
1761                                 edge.X -= 1;
1762                                 edge.Y -= 1;
1763                                 edge.Width += 2;
1764                                 edge.Height = 2;
1765                                 Invalidate (edge);
1766                                 edge.Y = BoxSelectRectangle.Bottom - 1;
1767                                 Invalidate (edge);
1768                                 edge.Y = BoxSelectRectangle.Y - 1;
1769                                 edge.Width = 2;
1770                                 edge.Height = BoxSelectRectangle.Height + 2;
1771                                 Invalidate (edge);
1772                                 edge.X = BoxSelectRectangle.Right - 1;
1773                                 Invalidate (edge);
1774                         }
1775
1776                         private Rectangle CalculateBoxSelectRectangle (Point pt)
1777                         {
1778                                 int left = Math.Min (box_select_start.X, pt.X);
1779                                 int right = Math.Max (box_select_start.X, pt.X);
1780                                 int top = Math.Min (box_select_start.Y, pt.Y);
1781                                 int bottom = Math.Max (box_select_start.Y, pt.Y);
1782                                 return Rectangle.FromLTRB (left, top, right, bottom);
1783                         }
1784
1785                         ArrayList BoxSelectedItems {
1786                                 get {
1787                                         ArrayList result = new ArrayList ();
1788                                         Size item_size = owner.ItemSize;
1789                                         for (int i = 0; i < owner.Items.Count; i++) {
1790                                                 Rectangle r = new Rectangle (owner.GetItemLocation (i), item_size);
1791                                                 r.X += r.Width / 4;
1792                                                 r.Y += r.Height / 4;
1793                                                 r.Width /= 2;
1794                                                 r.Height /= 2;
1795                                                 if (BoxSelectRectangle.IntersectsWith (r))
1796                                                         result.Add (owner.Items [i]);
1797                                         }
1798                                         return result;
1799                                 }
1800                         }
1801
1802                         private bool PerformBoxSelection (Point pt)
1803                         {
1804                                 if (box_select_mode == BoxSelect.None)
1805                                         return false;
1806
1807                                 BoxSelectRectangle = CalculateBoxSelectRectangle (pt);
1808                                 
1809                                 ArrayList box_items = BoxSelectedItems;
1810
1811                                 ArrayList items;
1812
1813                                 switch (box_select_mode) {
1814
1815                                 case BoxSelect.Normal:
1816                                         items = box_items;
1817                                         break;
1818
1819                                 case BoxSelect.Control:
1820                                         items = new ArrayList ();
1821                                         foreach (ListViewItem item in prev_selection)
1822                                                 if (!box_items.Contains (item))
1823                                                         items.Add (item);
1824                                         foreach (ListViewItem item in box_items)
1825                                                 if (!prev_selection.Contains (item))
1826                                                         items.Add (item);
1827                                         break;
1828
1829                                 case BoxSelect.Shift:
1830                                         items = box_items;
1831                                         foreach (ListViewItem item in box_items)
1832                                                 prev_selection.Remove (item);
1833                                         foreach (ListViewItem item in prev_selection)
1834                                                 items.Add (item);
1835                                         break;
1836
1837                                 default:
1838                                         throw new Exception ("Unexpected Selection mode: " + box_select_mode);
1839                                 }
1840
1841                                 SuspendLayout ();
1842                                 owner.SelectItems (items);
1843                                 ResumeLayout ();
1844
1845                                 return true;
1846                         }
1847
1848                         private void ToggleCheckState (ListViewItem item)
1849                         {
1850                                 CheckState curr_state = item.Checked ? CheckState.Checked : CheckState.Unchecked;
1851                                 item.Checked = !item.Checked;
1852                                 CheckState new_state = item.Checked ? CheckState.Checked : CheckState.Unchecked;
1853
1854                                 ItemCheckEventArgs ice = new ItemCheckEventArgs (item.Index, curr_state, new_state);
1855                                 owner.OnItemCheck (ice);
1856
1857 #if NET_2_0
1858                                 // ItemCheckedEvent goes after the checked state has changed
1859                                 ItemCheckedEventArgs icea = new ItemCheckedEventArgs (item);
1860                                 owner.OnItemChecked (icea);
1861 #endif
1862                         }
1863
1864                         private void ItemsMouseDown (object sender, MouseEventArgs me)
1865                         {
1866                                 if (owner.items.Count == 0) {
1867                                         owner.OnMouseDown (owner.TranslateMouseEventArgs (me));
1868                                         return;
1869                                 }
1870
1871                                 Size item_size = owner.ItemSize;
1872                                 Point pt = new Point (me.X, me.Y);
1873                                 for (int i = 0; i < owner.items.Count; i++) {
1874                                         Rectangle item_rect = new Rectangle (owner.GetItemLocation (i), item_size);
1875                                         if (!item_rect.Contains (pt))
1876                                                 continue;
1877
1878                                         if (me.Clicks == 1 && owner.items [i].CheckRectReal.Contains (pt)) {
1879                                                 checking = true;
1880                                                 ToggleCheckState (owner.items [i]);
1881                                                 owner.OnMouseDown (owner.TranslateMouseEventArgs (me));
1882                                                 return;
1883                                         }
1884
1885                                         if (owner.View == View.Details && !owner.FullRowSelect) {
1886                                                 if (owner.items [i].GetBounds (ItemBoundsPortion.Label).Contains (pt)) {
1887                                                         clicked_item = owner.items [i];
1888                                                         break;
1889                                                 }
1890                                         } else
1891                                                 clicked_item = owner.items [i];
1892                                 }
1893
1894
1895                                 if (clicked_item != null) {
1896                                         owner.SetFocusedItem (clicked_item);
1897                                         bool changed = !clicked_item.Selected;
1898
1899                                         if (owner.MultiSelect) {
1900                                                 bool reselect = (!owner.LabelEdit || changed);
1901                                                 owner.UpdateMultiSelection (clicked_item.Index, reselect);
1902                                         } else {
1903                                                 clicked_item.Selected = true;
1904                                         }
1905
1906                                         // Raise double click if the item was clicked. On MS the
1907                                         // double click is only raised if you double click an item
1908                                         if (me.Clicks > 1) {
1909                                                 owner.OnDoubleClick (EventArgs.Empty);
1910                                                 if (owner.CheckBoxes)
1911                                                         ToggleCheckState (clicked_item);
1912                                         } else if (me.Clicks == 1) {
1913                                                 owner.OnClick (EventArgs.Empty);
1914                                                 if (owner.LabelEdit && !changed)
1915                                                         BeginEdit (clicked_item); // this is probably not the correct place to execute BeginEdit
1916                                         }
1917                                 } else {
1918                                         if (owner.MultiSelect) {
1919                                                 Keys mods = XplatUI.State.ModifierKeys;
1920                                                 if ((mods & Keys.Shift) != 0)
1921                                                         box_select_mode = BoxSelect.Shift;
1922                                                 else if ((mods & Keys.Control) != 0)
1923                                                         box_select_mode = BoxSelect.Control;
1924                                                 else
1925                                                         box_select_mode = BoxSelect.Normal;
1926                                                 box_select_start = pt; 
1927                                                 prev_selection = owner.SelectedItems.List;
1928                                         } else if (owner.SelectedItems.Count > 0) {
1929                                                 owner.SelectedItems.Clear ();
1930                                         }
1931                                 }
1932
1933                                 owner.OnMouseDown (owner.TranslateMouseEventArgs (me));
1934                         }
1935
1936                         private void ItemsMouseMove (object sender, MouseEventArgs me)
1937                         {
1938                                 bool done = PerformBoxSelection (new Point (me.X, me.Y));
1939
1940                                 if (!done && owner.HoverSelection && hover_processed) {
1941
1942                                         Point pt = PointToClient (Control.MousePosition);
1943                                         ListViewItem item = owner.GetItemAt (pt.X, pt.Y);
1944                                         if (item != null && !item.Selected) {
1945                                                 hover_processed = false;
1946                                                 XplatUI.ResetMouseHover (Handle);
1947                                         }
1948                                 }
1949
1950                                 owner.OnMouseMove (owner.TranslateMouseEventArgs (me));
1951                         }
1952
1953
1954                         private void ItemsMouseHover (object sender, EventArgs e)
1955                         {
1956                                 owner.OnMouseHover(e);
1957
1958                                 if (Capture || !owner.HoverSelection)
1959                                         return;
1960
1961                                 hover_processed = true;
1962                                 Point pt = PointToClient (Control.MousePosition);
1963                                 ListViewItem item = owner.GetItemAt (pt.X, pt.Y);
1964
1965                                 if (item == null)
1966                                         return;
1967
1968                                 item.Selected = true;
1969                         }
1970
1971                         private void ItemsMouseUp (object sender, MouseEventArgs me)
1972                         {
1973                                 Capture = false;
1974                                 if (owner.Items.Count == 0) {
1975                                         owner.OnMouseUp (owner.TranslateMouseEventArgs (me));
1976                                         return;
1977                                 }
1978
1979                                 Point pt = new Point (me.X, me.Y);
1980
1981                                 Rectangle rect = Rectangle.Empty;
1982                                 if (clicked_item != null) {
1983                                         if (owner.view == View.Details && !owner.full_row_select)
1984                                                 rect = clicked_item.GetBounds (ItemBoundsPortion.Label);
1985                                         else
1986                                                 rect = clicked_item.Bounds;
1987
1988                                         if (rect.Contains (pt)) {
1989                                                 switch (owner.activation) {
1990                                                 case ItemActivation.OneClick:
1991                                                         owner.OnItemActivate (EventArgs.Empty);
1992                                                         break;
1993
1994                                                 case ItemActivation.TwoClick:
1995                                                         if (last_clicked_item == clicked_item) {
1996                                                                 owner.OnItemActivate (EventArgs.Empty);
1997                                                                 last_clicked_item = null;
1998                                                         } else
1999                                                                 last_clicked_item = clicked_item;
2000                                                         break;
2001                                                 default:
2002                                                         // DoubleClick activation is handled in another handler
2003                                                         break;
2004                                                 }
2005                                         }
2006                                 } else if (!checking && owner.SelectedItems.Count > 0 && BoxSelectRectangle.Size.IsEmpty) {
2007                                         // Need this to clean up background clicks
2008                                         owner.SelectedItems.Clear ();
2009                                 }
2010
2011                                 clicked_item = null;
2012                                 box_select_start = Point.Empty;
2013                                 BoxSelectRectangle = Rectangle.Empty;
2014                                 prev_selection = null;
2015                                 box_select_mode = BoxSelect.None;
2016                                 checking = false;
2017                                 owner.OnMouseUp (owner.TranslateMouseEventArgs (me));
2018                         }
2019                         
2020                         private void LabelEditFinished (object sender, EventArgs e)
2021                         {
2022                                 EndEdit (edit_item);
2023                         }
2024
2025                         private void LabelEditCancelled (object sender, EventArgs e)
2026                         {
2027                                 edit_args.SetLabel (null);
2028                                 EndEdit (edit_item);
2029                         }
2030
2031                         private void LabelTextChanged (object sender, EventArgs e)
2032                         {
2033                                 if (edit_args != null)
2034                                         edit_args.SetLabel (edit_text_box.Text);
2035                         }
2036
2037                         internal void BeginEdit (ListViewItem item)
2038                         {
2039                                 if (edit_item != null)
2040                                         EndEdit (edit_item);
2041                                 
2042                                 if (edit_text_box == null) {
2043                                         edit_text_box = new ListViewLabelEditTextBox ();
2044                                         edit_text_box.BorderStyle = BorderStyle.FixedSingle;
2045                                         edit_text_box.EditingCancelled += new EventHandler (LabelEditCancelled);
2046                                         edit_text_box.EditingFinished += new EventHandler (LabelEditFinished);
2047                                         edit_text_box.TextChanged += new EventHandler (LabelTextChanged);
2048                                         edit_text_box.Visible = false;
2049                                         Controls.Add (edit_text_box);
2050                                 }
2051                                 
2052                                 item.EnsureVisible();
2053                                 
2054                                 edit_text_box.Reset ();
2055                                 
2056                                 switch (owner.view) {
2057                                         case View.List:
2058                                         case View.SmallIcon:
2059                                         case View.Details:
2060                                                 edit_text_box.TextAlign = HorizontalAlignment.Left;
2061                                                 edit_text_box.Bounds = item.GetBounds (ItemBoundsPortion.Label);
2062                                                 SizeF sizef = DeviceContext.MeasureString (item.Text, item.Font);
2063                                                 edit_text_box.Width = (int)sizef.Width + 4;
2064                                                 edit_text_box.MaxWidth = owner.ClientRectangle.Width - edit_text_box.Bounds.X;
2065                                                 edit_text_box.WordWrap = false;
2066                                                 edit_text_box.Multiline = false;
2067                                                 break;
2068                                         case View.LargeIcon:
2069                                                 edit_text_box.TextAlign = HorizontalAlignment.Center;
2070                                                 edit_text_box.Bounds = item.GetBounds (ItemBoundsPortion.Label);
2071                                                 sizef = DeviceContext.MeasureString (item.Text, item.Font);
2072                                                 edit_text_box.Width = (int)sizef.Width + 4;
2073                                                 edit_text_box.MaxWidth = item.GetBounds(ItemBoundsPortion.Entire).Width;
2074                                                 edit_text_box.MaxHeight = owner.ClientRectangle.Height - edit_text_box.Bounds.Y;
2075                                                 edit_text_box.WordWrap = true;
2076                                                 edit_text_box.Multiline = true;
2077                                                 break;
2078                                 }
2079
2080                                 edit_item = item;
2081
2082                                 edit_text_box.Text = item.Text;
2083                                 edit_text_box.Font = item.Font;
2084                                 edit_text_box.Visible = true;
2085                                 edit_text_box.Focus ();
2086                                 edit_text_box.SelectAll ();
2087
2088                                 edit_args = new LabelEditEventArgs (owner.Items.IndexOf (edit_item));
2089                                 owner.OnBeforeLabelEdit (edit_args);
2090
2091                                 if (edit_args.CancelEdit)
2092                                         EndEdit (item);
2093                         }
2094
2095                         internal void CancelEdit (ListViewItem item)
2096                         {
2097                                 // do nothing if there's no item being edited, or if the
2098                                 // item being edited is not the one passed in
2099                                 if (edit_item == null || edit_item != item)
2100                                         return;
2101
2102                                 edit_args.SetLabel (null);
2103                                 EndEdit (item);
2104                         }
2105
2106                         internal void EndEdit (ListViewItem item)
2107                         {
2108                                 // do nothing if there's no item being edited, or if the
2109                                 // item being edited is not the one passed in
2110                                 if (edit_item == null || edit_item != item)
2111                                         return;
2112
2113                                 owner.OnAfterLabelEdit (edit_args);
2114                                 if (!edit_args.CancelEdit && edit_args.Label != null)
2115                                         edit_item.Text = edit_text_box.Text;
2116
2117                                 if (edit_text_box != null) {
2118                                         if (edit_text_box.Visible)
2119                                                 edit_text_box.Visible = false;
2120                                         // ensure listview gets focus
2121                                         owner.Focus ();
2122                                 }
2123
2124                                 edit_item = null;
2125                         }
2126
2127                         internal override void OnPaintInternal (PaintEventArgs pe)
2128                         {
2129                                 ThemeEngine.Current.DrawListViewItems (pe.Graphics, pe.ClipRectangle, owner);
2130                         }
2131
2132                         protected override void WndProc (ref Message m)
2133                         {
2134                                 switch ((Msg)m.Msg) {
2135                                 case Msg.WM_KILLFOCUS:
2136                                         owner.Select (false, true);
2137                                         break;
2138                                 case Msg.WM_SETFOCUS:
2139                                         owner.Select (false, true);
2140                                         break;
2141                                 case Msg.WM_RBUTTONDOWN:
2142                                         owner.Select (false, true);
2143                                         break;
2144                                 default:
2145                                         break;
2146                                 }
2147                                 base.WndProc (ref m);
2148                         }
2149                 }
2150                 
2151                 internal class ListViewLabelEditTextBox : TextBox
2152                 {
2153                         int max_width = -1;
2154                         int min_width = -1;
2155                         
2156                         int max_height = -1;
2157                         int min_height = -1;
2158                         
2159                         int old_number_lines = 1;
2160                         
2161                         SizeF text_size_one_char;
2162                         
2163                         public ListViewLabelEditTextBox ()
2164                         {
2165                                 min_height = DefaultSize.Height;
2166                                 text_size_one_char = DeviceContext.MeasureString ("B", Font);
2167                         }
2168                         
2169                         public int MaxWidth {
2170                                 set {
2171                                         if (value < min_width)
2172                                                 max_width = min_width;
2173                                         else
2174                                                 max_width = value;
2175                                 }
2176                         }
2177                         
2178                         public int MaxHeight {
2179                                 set {
2180                                         if (value < min_height)
2181                                                 max_height = min_height;
2182                                         else
2183                                                 max_height = value;
2184                                 }
2185                         }
2186                         
2187                         public new int Width {
2188                                 get {
2189                                         return base.Width;
2190                                 }
2191                                 set {
2192                                         min_width = value;
2193                                         base.Width = value;
2194                                 }
2195                         }
2196                         
2197                         public override Font Font {
2198                                 get {
2199                                         return base.Font;
2200                                 }
2201                                 set {
2202                                         base.Font = value;
2203                                         text_size_one_char = DeviceContext.MeasureString ("B", Font);
2204                                 }
2205                         }
2206                         
2207                         protected override void OnTextChanged (EventArgs e)
2208                         {
2209                                 SizeF text_size = DeviceContext.MeasureString (Text, Font);
2210                                 
2211                                 int new_width = (int)text_size.Width + 8;
2212                                 
2213                                 if (!Multiline)
2214                                         ResizeTextBoxWidth (new_width);
2215                                 else {
2216                                         if (Width != max_width)
2217                                                 ResizeTextBoxWidth (new_width);
2218                                         
2219                                         int number_lines = Lines.Length;
2220                                         
2221                                         if (number_lines != old_number_lines) {
2222                                                 int new_height = number_lines * (int)text_size_one_char.Height + 4;
2223                                                 old_number_lines = number_lines;
2224                                                 
2225                                                 ResizeTextBoxHeight (new_height);
2226                                         }
2227                                 }
2228                                 
2229                                 base.OnTextChanged (e);
2230                         }
2231                         
2232                         protected override bool IsInputKey (Keys key_data)
2233                         {
2234                                 if ((key_data & Keys.Alt) == 0) {
2235                                         switch (key_data & Keys.KeyCode) {
2236                                                 case Keys.Enter:
2237                                                         return true;
2238                                                 case Keys.Escape:
2239                                                         return true;
2240                                         }
2241                                 }
2242                                 return base.IsInputKey (key_data);
2243                         }
2244                         
2245                         protected override void OnKeyDown (KeyEventArgs e)
2246                         {
2247                                 if (!Visible)
2248                                         return;
2249
2250                                 switch (e.KeyCode) {
2251                                 case Keys.Return:
2252                                         Visible = false;
2253                                         e.Handled = true;
2254                                         OnEditingFinished (e);
2255                                         break;
2256                                 case Keys.Escape:
2257                                         Visible = false;
2258                                         e.Handled = true;
2259                                         OnEditingCancelled (e);
2260                                         break;
2261                                 }
2262                         }
2263                         
2264                         protected override void OnLostFocus (EventArgs e)
2265                         {
2266                                 if (Visible) {
2267                                         OnEditingFinished (e);
2268                                 }
2269                         }
2270
2271                         protected void OnEditingCancelled (EventArgs e)
2272                         {
2273                                 EventHandler eh = (EventHandler)(Events [EditingCancelledEvent]);
2274                                 if (eh != null)
2275                                         eh (this, e);
2276                         }
2277                         
2278                         protected void OnEditingFinished (EventArgs e)
2279                         {
2280                                 EventHandler eh = (EventHandler)(Events [EditingFinishedEvent]);
2281                                 if (eh != null)
2282                                         eh (this, e);
2283                         }
2284                         
2285                         private void ResizeTextBoxWidth (int new_width)
2286                         {
2287                                 if (new_width > max_width)
2288                                         base.Width = max_width;
2289                                 else 
2290                                 if (new_width >= min_width)
2291                                         base.Width = new_width;
2292                                 else
2293                                         base.Width = min_width;
2294                         }
2295                         
2296                         private void ResizeTextBoxHeight (int new_height)
2297                         {
2298                                 if (new_height > max_height)
2299                                         base.Height = max_height;
2300                                 else 
2301                                 if (new_height >= min_height)
2302                                         base.Height = new_height;
2303                                 else
2304                                         base.Height = min_height;
2305                         }
2306                         
2307                         public void Reset ()
2308                         {
2309                                 max_width = -1;
2310                                 min_width = -1;
2311                                 
2312                                 max_height = -1;
2313                                 
2314                                 old_number_lines = 1;
2315                                 
2316                                 Text = String.Empty;
2317                                 
2318                                 Size = DefaultSize;
2319                         }
2320
2321                         static object EditingCancelledEvent = new object ();
2322                         public event EventHandler EditingCancelled {
2323                                 add { Events.AddHandler (EditingCancelledEvent, value); }
2324                                 remove { Events.RemoveHandler (EditingCancelledEvent, value); }
2325                         }
2326
2327                         static object EditingFinishedEvent = new object ();
2328                         public event EventHandler EditingFinished {
2329                                 add { Events.AddHandler (EditingFinishedEvent, value); }
2330                                 remove { Events.RemoveHandler (EditingFinishedEvent, value); }
2331                         }
2332                 }
2333
2334                 internal override void OnPaintInternal (PaintEventArgs pe)
2335                 {
2336                         if (updating)
2337                                 return;
2338                                 
2339                         CalculateScrollBars ();
2340                 }
2341
2342                 void FocusChanged (object o, EventArgs args)
2343                 {
2344                         if (Items.Count == 0)
2345                                 return;
2346
2347                         if (FocusedItem == null)
2348                                 SetFocusedItem (Items [0]);
2349
2350                         item_control.Invalidate (FocusedItem.Bounds);
2351                 }
2352
2353                 private void ListView_MouseWheel (object sender, MouseEventArgs me)
2354                 {
2355                         if (Items.Count == 0)
2356                                 return;
2357
2358                         int lines = me.Delta / 120;
2359
2360                         if (lines == 0)
2361                                 return;
2362
2363                         switch (View) {
2364                         case View.Details:
2365                         case View.SmallIcon:
2366                                 Scroll (v_scroll, -ItemSize.Height * SystemInformation.MouseWheelScrollLines * lines);
2367                                 break;
2368                         case View.LargeIcon:
2369                                 Scroll (v_scroll, -(ItemSize.Height + ThemeEngine.Current.ListViewVerticalSpacing)  * lines);
2370                                 break;
2371                         case View.List:
2372                                 Scroll (h_scroll, -ItemSize.Width * lines);
2373                                 break;
2374                         }
2375                 }
2376
2377                 private void ListView_SizeChanged (object sender, EventArgs e)
2378                 {
2379                         CalculateListView (alignment);
2380                 }
2381                 
2382                 private void SetFocusedItem (ListViewItem item)
2383                 {
2384                         if (focused_item != null)
2385                                 focused_item.Focused = false;
2386                         
2387                         if (item != null)
2388                                 item.Focused = true;
2389                                 
2390                         focused_item = item;
2391                 }
2392
2393                 private void HorizontalScroller (object sender, EventArgs e)
2394                 {
2395                         item_control.EndEdit (item_control.edit_item);
2396                         
2397                         // Avoid unnecessary flickering, when button is
2398                         // kept pressed at the end
2399                         if (h_marker != h_scroll.Value) {
2400                                 
2401                                 int pixels = h_marker - h_scroll.Value;
2402                                 
2403                                 h_marker = h_scroll.Value;
2404                                 if (header_control.Visible)
2405                                         XplatUI.ScrollWindow (header_control.Handle, pixels, 0, false);
2406
2407                                 XplatUI.ScrollWindow (item_control.Handle, pixels, 0, false);
2408                         }
2409                 }
2410
2411                 private void VerticalScroller (object sender, EventArgs e)
2412                 {
2413                         item_control.EndEdit (item_control.edit_item);
2414                         
2415                         // Avoid unnecessary flickering, when button is
2416                         // kept pressed at the end
2417                         if (v_marker != v_scroll.Value) {
2418                                 int pixels = v_marker - v_scroll.Value;
2419                                 Rectangle area = item_control.ClientRectangle;
2420                                 v_marker = v_scroll.Value;
2421                                 XplatUI.ScrollWindow (item_control.Handle, area, 0, pixels, false);
2422                         }
2423                 }
2424                 #endregion      // Internal Methods Properties
2425
2426                 #region Protected Methods
2427                 protected override void CreateHandle ()
2428                 {
2429                         base.CreateHandle ();
2430                         for (int i = 0; i < SelectedItems.Count; i++)
2431                                 OnSelectedIndexChanged (EventArgs.Empty);
2432                 }
2433
2434                 protected override void Dispose (bool disposing)
2435                 {
2436                         if (disposing) {
2437                                 h_scroll.Dispose ();
2438                                 v_scroll.Dispose ();
2439                                 
2440                                 large_image_list = null;
2441                                 small_image_list = null;
2442                                 state_image_list = null;
2443
2444                                 foreach (ColumnHeader col in columns)
2445                                         col.SetListView (null);
2446
2447 #if NET_2_0
2448                                 if (!virtual_mode) // In virtual mode we don't save the items
2449 #endif
2450                                         foreach (ListViewItem item in items)
2451                                                 item.Owner = null;
2452                         }
2453                         
2454                         base.Dispose (disposing);
2455                 }
2456
2457                 protected override bool IsInputKey (Keys keyData)
2458                 {
2459                         switch (keyData) {
2460                         case Keys.Up:
2461                         case Keys.Down:
2462                         case Keys.PageUp:
2463                         case Keys.PageDown:
2464                         case Keys.Right:
2465                         case Keys.Left:
2466                         case Keys.End:
2467                         case Keys.Home:
2468                                 return true;
2469
2470                         default:
2471                                 break;
2472                         }
2473                         
2474                         return base.IsInputKey (keyData);
2475                 }
2476
2477                 protected virtual void OnAfterLabelEdit (LabelEditEventArgs e)
2478                 {
2479                         LabelEditEventHandler eh = (LabelEditEventHandler)(Events [AfterLabelEditEvent]);
2480                         if (eh != null)
2481                                 eh (this, e);
2482                 }
2483
2484                 protected virtual void OnBeforeLabelEdit (LabelEditEventArgs e)
2485                 {
2486                         LabelEditEventHandler eh = (LabelEditEventHandler)(Events [BeforeLabelEditEvent]);
2487                         if (eh != null)
2488                                 eh (this, e);
2489                 }
2490
2491                 protected virtual void OnColumnClick (ColumnClickEventArgs e)
2492                 {
2493                         ColumnClickEventHandler eh = (ColumnClickEventHandler)(Events [ColumnClickEvent]);
2494                         if (eh != null)
2495                                 eh (this, e);
2496                 }
2497
2498                 protected override void OnEnabledChanged (EventArgs e)
2499                 {
2500                         base.OnEnabledChanged (e);
2501                 }
2502
2503                 protected override void OnFontChanged (EventArgs e)
2504                 {
2505                         base.OnFontChanged (e);
2506                         Redraw (true);
2507                 }
2508
2509                 protected override void OnHandleCreated (EventArgs e)
2510                 {
2511                         base.OnHandleCreated (e);
2512                         CalculateListView (alignment);
2513 #if NET_2_0
2514                         if (!virtual_mode) // Sorting is not allowed in virtual mode
2515 #endif
2516                                 Sort ();
2517                 }
2518
2519                 protected override void OnHandleDestroyed (EventArgs e)
2520                 {
2521                         base.OnHandleDestroyed (e);
2522                 }
2523
2524                 protected virtual void OnItemActivate (EventArgs e)
2525                 {
2526                         EventHandler eh = (EventHandler)(Events [ItemActivateEvent]);
2527                         if (eh != null)
2528                                 eh (this, e);
2529                 }
2530
2531                 protected virtual void OnItemCheck (ItemCheckEventArgs ice)
2532                 {
2533                         ItemCheckEventHandler eh = (ItemCheckEventHandler)(Events [ItemCheckEvent]);
2534                         if (eh != null)
2535                                 eh (this, ice);
2536                 }
2537
2538 #if NET_2_0
2539                 protected virtual void OnItemChecked (ItemCheckedEventArgs icea)
2540                 {
2541                         ItemCheckedEventHandler eh = (ItemCheckedEventHandler)(Events [ItemCheckedEvent]);
2542                         if (eh != null)
2543                                 eh (this, icea);
2544                 }
2545 #endif
2546
2547                 protected virtual void OnItemDrag (ItemDragEventArgs e)
2548                 {
2549                         EventHandler eh = (EventHandler)(Events [ItemDragEvent]);
2550                         if (eh != null)
2551                                 eh (this, e);
2552                 }
2553
2554                 protected virtual void OnSelectedIndexChanged (EventArgs e)
2555                 {
2556                         EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
2557                         if (eh != null)
2558                                 eh (this, e);
2559                 }
2560
2561                 protected override void OnSystemColorsChanged (EventArgs e)
2562                 {
2563                         base.OnSystemColorsChanged (e);
2564                 }
2565
2566 #if NET_2_0
2567                 protected virtual void OnCacheVirtualItems (CacheVirtualItemsEventArgs args)
2568                 {
2569                         EventHandler eh = (EventHandler)Events [CacheVirtualItemsEvent];
2570                         if (eh != null)
2571                                 eh (this, args);
2572                 }
2573
2574                 protected virtual void OnRetrieveVirtualItem (RetrieveVirtualItemEventArgs args)
2575                 {
2576                         RetrieveVirtualItemEventHandler eh = (RetrieveVirtualItemEventHandler)Events [RetrieveVirtualItemEvent];
2577                         if (eh != null)
2578                                 eh (this, args);
2579                 }
2580 #endif
2581
2582                 protected void RealizeProperties ()
2583                 {
2584                         // FIXME: TODO
2585                 }
2586
2587                 protected void UpdateExtendedStyles ()
2588                 {
2589                         // FIXME: TODO
2590                 }
2591
2592                 bool refocusing = false;
2593
2594                 protected override void WndProc (ref Message m)
2595                 {
2596                         switch ((Msg)m.Msg) {
2597                         case Msg.WM_KILLFOCUS:
2598                                 Control receiver = Control.FromHandle (m.WParam);
2599                                 if (receiver == item_control) {
2600                                         has_focus = false;
2601                                         refocusing = true;
2602                                         return;
2603                                 }
2604                                 break;
2605                         case Msg.WM_SETFOCUS:
2606                                 if (refocusing) {
2607                                         has_focus = true;
2608                                         refocusing = false;
2609                                         return;
2610                                 }
2611                                 break;
2612                         default:
2613                                 break;
2614                         }
2615                         base.WndProc (ref m);
2616                 }
2617                 #endregion // Protected Methods
2618
2619                 #region Public Instance Methods
2620                 public void ArrangeIcons ()
2621                 {
2622                         ArrangeIcons (this.alignment);
2623                 }
2624
2625                 public void ArrangeIcons (ListViewAlignment alignment)
2626                 {
2627                         // Icons are arranged only if view is set to LargeIcon or SmallIcon
2628                         if (view == View.LargeIcon || view == View.SmallIcon) {
2629                                 this.CalculateListView (alignment);
2630                                 // we have done the calculations already
2631                                 this.Redraw (false);
2632                         }
2633                 }
2634
2635 #if NET_2_0
2636                 public void AutoResizeColumn (int columnIndex, ColumnHeaderAutoResizeStyle headerAutoResize)
2637                 {
2638                         if (columnIndex < 0 || columnIndex >= columns.Count)
2639                                 throw new ArgumentOutOfRangeException ("columnIndex");
2640
2641                         columns [columnIndex].AutoResize (headerAutoResize);
2642                 }
2643
2644                 public void AutoResizeColumns (ColumnHeaderAutoResizeStyle headerAutoResize)
2645                 {
2646                         BeginUpdate ();
2647                         foreach (ColumnHeader col in columns) 
2648                                 col.AutoResize (headerAutoResize);
2649                         EndUpdate ();
2650                 }
2651 #endif
2652
2653                 public void BeginUpdate ()
2654                 {
2655                         // flag to avoid painting
2656                         updating = true;
2657                 }
2658
2659                 public void Clear ()
2660                 {
2661                         columns.Clear ();
2662                         items.Clear (); // Redraw (true) called here
2663                 }
2664
2665                 public void EndUpdate ()
2666                 {
2667                         // flag to avoid painting
2668                         updating = false;
2669
2670                         // probably, now we need a redraw with recalculations
2671                         this.Redraw (true);
2672                 }
2673
2674                 public void EnsureVisible (int index)
2675                 {
2676                         if (index < 0 || index >= items.Count || scrollable == false)
2677                                 return;
2678
2679                         Rectangle view_rect = item_control.ClientRectangle;
2680                         Rectangle bounds = new Rectangle (GetItemLocation (index), ItemSize);
2681
2682                         if (view_rect.Contains (bounds))
2683                                 return;
2684
2685                         if (View != View.Details) {
2686                                 if (bounds.Left < 0)
2687                                         h_scroll.Value += bounds.Left;
2688                                 else if (bounds.Right > view_rect.Right)
2689                                         h_scroll.Value += (bounds.Right - view_rect.Right);
2690                         }
2691
2692                         if (bounds.Top < 0)
2693                                 v_scroll.Value += bounds.Top;
2694                         else if (bounds.Bottom > view_rect.Bottom)
2695                                 v_scroll.Value += (bounds.Bottom - view_rect.Bottom);
2696                 }
2697
2698 #if NET_2_0
2699                 public ListViewItem FindItemWithText (string text)
2700                 {
2701                         if (items.Count == 0)
2702                                 return null;
2703
2704                         return FindItemWithText (text, true, 0, true);
2705                 }
2706
2707                 public ListViewItem FindItemWithText (string text, bool includeSubItems, int startIndex)
2708                 {
2709                         return FindItemWithText (text, includeSubItems, startIndex, true);
2710                 }
2711
2712                 public ListViewItem FindItemWithText (string text, bool includeSubItems, int startIndex, bool prefixSearch)
2713                 {
2714                         if (startIndex < 0 || startIndex >= items.Count)
2715                                 throw new ArgumentOutOfRangeException ("startIndex");
2716
2717                         if (text == null)
2718                                 throw new ArgumentNullException ("text");
2719
2720                         for (int i = startIndex; i < items.Count; i++) {
2721                                 ListViewItem lvi = items [i];
2722
2723                                 if ((prefixSearch && lvi.Text.StartsWith (text, true, CultureInfo.CurrentCulture)) // prefix search
2724                                                 || String.Compare (lvi.Text, text, true) == 0) // match
2725                                         return lvi;
2726                         }
2727
2728                         if (includeSubItems) {
2729                                 for (int i = startIndex; i < items.Count; i++) {
2730                                         ListViewItem lvi = items [i];
2731                                         foreach (ListViewItem.ListViewSubItem sub_item in lvi.SubItems)
2732                                                 if ((prefixSearch && sub_item.Text.StartsWith (text, true, CultureInfo.CurrentCulture))
2733                                                                 || String.Compare (sub_item.Text, text, true) == 0)
2734                                                         return lvi;
2735                                 }
2736                         }
2737
2738                         return null;
2739                 }
2740 #endif
2741                 
2742                 public ListViewItem GetItemAt (int x, int y)
2743                 {
2744                         Size item_size = ItemSize;
2745                         for (int i = 0; i < items.Count; i++) {
2746                                 Point item_location = GetItemLocation (i);
2747                                 Rectangle item_rect = new Rectangle (item_location, item_size);
2748                                 if (item_rect.Contains (x, y))
2749                                         return items [i];
2750                         }
2751
2752                         return null;
2753                 }
2754
2755                 public Rectangle GetItemRect (int index)
2756                 {
2757                         return GetItemRect (index, ItemBoundsPortion.Entire);
2758                 }
2759
2760                 public Rectangle GetItemRect (int index, ItemBoundsPortion portion)
2761                 {
2762                         if (index < 0 || index >= items.Count)
2763                                 throw new IndexOutOfRangeException ("index");
2764
2765                         return items [index].GetBounds (portion);
2766                 }
2767
2768                 public void Sort ()
2769                 {
2770 #if NET_2_0
2771                         if (virtual_mode)
2772                                 throw new InvalidOperationException ();
2773 #endif
2774
2775                         Sort (true);
2776                 }
2777
2778                 // we need this overload to reuse the logic for sorting, while allowing
2779                 // redrawing to be done by caller or have it done by this method when
2780                 // sorting is really performed
2781                 //
2782                 // ListViewItemCollection's Add and AddRange methods call this overload
2783                 // with redraw set to false, as they take care of redrawing themselves
2784                 // (they even want to redraw the listview if no sort is performed, as 
2785                 // an item was added), while ListView.Sort () only wants to redraw if 
2786                 // sorting was actually performed
2787                 private void Sort (bool redraw)
2788                 {
2789                         if (!IsHandleCreated || item_sorter == null) {
2790                                 return;
2791                         }
2792                         
2793                         items.Sort (item_sorter);
2794                         if (redraw)
2795                                 this.Redraw (true);
2796                 }
2797
2798                 public override string ToString ()
2799                 {
2800                         int count = this.Items.Count;
2801
2802                         if (count == 0)
2803                                 return string.Format ("System.Windows.Forms.ListView, Items.Count: 0");
2804                         else
2805                                 return string.Format ("System.Windows.Forms.ListView, Items.Count: {0}, Items[0]: {1}", count, this.Items [0].ToString ());
2806                 }
2807                 #endregion      // Public Instance Methods
2808
2809
2810                 #region Subclasses
2811
2812                 class HeaderControl : Control {
2813
2814                         ListView owner;
2815                         bool column_resize_active = false;
2816                         ColumnHeader resize_column;
2817                         ColumnHeader clicked_column;
2818                         ColumnHeader drag_column;
2819                         int drag_x;
2820                         int drag_to_index = -1;
2821
2822                         public HeaderControl (ListView owner)
2823                         {
2824                                 this.owner = owner;
2825                                 MouseDown += new MouseEventHandler (HeaderMouseDown);
2826                                 MouseMove += new MouseEventHandler (HeaderMouseMove);
2827                                 MouseUp += new MouseEventHandler (HeaderMouseUp);
2828                         }
2829
2830                         private ColumnHeader ColumnAtX (int x)
2831                         {
2832                                 Point pt = new Point (x, 0);
2833                                 ColumnHeader result = null;
2834                                 foreach (ColumnHeader col in owner.Columns) {
2835                                         if (col.Rect.Contains (pt)) {
2836                                                 result = col;
2837                                                 break;
2838                                         }
2839                                 }
2840                                 return result;
2841                         }
2842
2843                         private int GetReorderedIndex (ColumnHeader col)
2844                         {
2845                                 if (owner.reordered_column_indices == null)
2846                                         return col.Index;
2847                                 else
2848                                         for (int i = 0; i < owner.Columns.Count; i++)
2849                                                 if (owner.reordered_column_indices [i] == col.Index)
2850                                                         return i;
2851                                 throw new Exception ("Column index missing from reordered array");
2852                         }
2853
2854                         private void HeaderMouseDown (object sender, MouseEventArgs me)
2855                         {
2856                                 if (resize_column != null) {
2857                                         column_resize_active = true;
2858                                         Capture = true;
2859                                         return;
2860                                 }
2861
2862                                 clicked_column = ColumnAtX (me.X + owner.h_marker);
2863
2864                                 if (clicked_column != null) {
2865                                         Capture = true;
2866                                         if (owner.AllowColumnReorder) {
2867                                                 drag_x = me.X;
2868                                                 drag_column = (ColumnHeader) (clicked_column as ICloneable).Clone ();
2869                                                 drag_column.Rect = clicked_column.Rect;
2870                                                 drag_to_index = GetReorderedIndex (clicked_column);
2871                                         }
2872                                         clicked_column.Pressed = true;
2873                                         Rectangle bounds = clicked_column.Rect;
2874                                         bounds.X -= owner.h_marker;
2875                                         Invalidate (bounds);
2876                                         return;
2877                                 }
2878                         }
2879
2880                         void StopResize ()
2881                         {
2882                                 column_resize_active = false;
2883                                 resize_column = null;
2884                                 Capture = false;
2885                                 Cursor = Cursors.Default;
2886                         }
2887                         
2888                         private void HeaderMouseMove (object sender, MouseEventArgs me)
2889                         {
2890                                 Point pt = new Point (me.X + owner.h_marker, me.Y);
2891
2892                                 if (column_resize_active) {
2893                                         int width = pt.X - resize_column.X;
2894                                         if (width < 0)
2895                                                 width = 0;
2896
2897                                         if (!owner.CanProceedWithResize (resize_column, width)){
2898                                                 StopResize ();
2899                                                 return;
2900                                         }
2901                                         resize_column.Width = width;
2902                                         return;
2903                                 }
2904
2905                                 resize_column = null;
2906
2907                                 if (clicked_column != null) {
2908                                         if (owner.AllowColumnReorder) {
2909                                                 Rectangle r;
2910
2911                                                 r = drag_column.Rect;
2912                                                 r.X = clicked_column.Rect.X + me.X - drag_x;
2913                                                 drag_column.Rect = r;
2914
2915                                                 int x = me.X + owner.h_marker;
2916                                                 ColumnHeader over = ColumnAtX (x);
2917                                                 if (over == null)
2918                                                         drag_to_index = owner.Columns.Count;
2919                                                 else if (x < over.X + over.Width / 2)
2920                                                         drag_to_index = GetReorderedIndex (over);
2921                                                 else
2922                                                         drag_to_index = GetReorderedIndex (over) + 1;
2923                                                 Invalidate ();
2924                                         } else {
2925                                                 ColumnHeader over = ColumnAtX (me.X + owner.h_marker);
2926                                                 bool pressed = clicked_column.Pressed;
2927                                                 clicked_column.Pressed = over == clicked_column;
2928                                                 if (clicked_column.Pressed ^ pressed) {
2929                                                         Rectangle bounds = clicked_column.Rect;
2930                                                         bounds.X -= owner.h_marker;
2931                                                         Invalidate (bounds);
2932                                                 }
2933                                         }
2934                                         return;
2935                                 }
2936
2937                                 for (int i = 0; i < owner.Columns.Count; i++) {
2938                                         Rectangle zone = owner.Columns [i].Rect;
2939                                         zone.X = zone.Right - 5;
2940                                         zone.Width = 10;
2941                                         if (zone.Contains (pt)) {
2942                                                 if (i < owner.Columns.Count - 1 && owner.Columns [i + 1].Width == 0)
2943                                                         i++;
2944                                                 resize_column = owner.Columns [i];
2945                                                 break;
2946                                         }
2947                                 }
2948
2949                                 if (resize_column == null)
2950                                         Cursor = Cursors.Default;
2951                                 else
2952                                         Cursor = Cursors.VSplit;
2953                         }
2954
2955                         void HeaderMouseUp (object sender, MouseEventArgs me)
2956                         {
2957                                 Capture = false;
2958
2959                                 if (column_resize_active) {
2960                                         int column_idx = resize_column.Index;
2961                                         StopResize ();
2962                                         owner.RaiseColumnWidthChanged (column_idx);
2963                                         return;
2964                                 }
2965
2966                                 if (clicked_column != null && clicked_column.Pressed) {
2967                                         clicked_column.Pressed = false;
2968                                         Rectangle bounds = clicked_column.Rect;
2969                                         bounds.X -= owner.h_marker;
2970                                         Invalidate (bounds);
2971                                         owner.OnColumnClick (new ColumnClickEventArgs (clicked_column.Index));
2972                                 }
2973
2974                                 if (drag_column != null && owner.AllowColumnReorder) {
2975                                         drag_column = null;
2976                                         if (drag_to_index > GetReorderedIndex (clicked_column))
2977                                                 drag_to_index--;
2978                                         if (owner.GetReorderedColumn (drag_to_index) != clicked_column)
2979                                                 owner.ReorderColumn (clicked_column, drag_to_index, true);
2980                                         drag_to_index = -1;
2981                                         Invalidate ();
2982                                 }
2983
2984                                 clicked_column = null;
2985                         }
2986
2987                         internal override void OnPaintInternal (PaintEventArgs pe)
2988                         {
2989                                 if (owner.updating)
2990                                         return;
2991                                 
2992                                 Theme theme = ThemeEngine.Current;
2993                                 theme.DrawListViewHeader (pe.Graphics, pe.ClipRectangle, this.owner);
2994
2995                                 if (drag_column == null)
2996                                         return;
2997
2998                                 int target_x;
2999                                 if (drag_to_index == owner.Columns.Count)
3000                                         target_x = owner.GetReorderedColumn (drag_to_index - 1).Rect.Right - owner.h_marker;
3001                                 else
3002                                         target_x = owner.GetReorderedColumn (drag_to_index).Rect.X - owner.h_marker;
3003                                 theme.DrawListViewHeaderDragDetails (pe.Graphics, owner, drag_column, target_x);
3004                         }
3005
3006                         protected override void WndProc (ref Message m)
3007                         {
3008                                 switch ((Msg)m.Msg) {
3009                                 case Msg.WM_SETFOCUS:
3010                                         owner.Focus ();
3011                                         break;
3012                                 default:
3013                                         base.WndProc (ref m);
3014                                         break;
3015                                 }
3016                         }
3017                 }
3018
3019                 private class ItemComparer : IComparer {
3020                         readonly SortOrder sort_order;
3021
3022                         public ItemComparer (SortOrder sortOrder)
3023                         {
3024                                 sort_order = sortOrder;
3025                         }
3026
3027                         public int Compare (object x, object y)
3028                         {
3029                                 ListViewItem item_x = x as ListViewItem;
3030                                 ListViewItem item_y = y as ListViewItem;
3031                                 if (sort_order == SortOrder.Ascending)
3032                                         return String.Compare (item_x.Text, item_y.Text);
3033                                 else
3034                                         return String.Compare (item_y.Text, item_x.Text);
3035                         }
3036                 }
3037
3038                 public class CheckedIndexCollection : IList, ICollection, IEnumerable
3039                 {
3040                         private readonly ListView owner;
3041
3042                         #region Public Constructor
3043                         public CheckedIndexCollection (ListView owner)
3044                         {
3045                                 this.owner = owner;
3046                         }
3047                         #endregion      // Public Constructor
3048
3049                         #region Public Properties
3050                         [Browsable (false)]
3051                         public int Count {
3052                                 get { return owner.CheckedItems.Count; }
3053                         }
3054
3055                         public bool IsReadOnly {
3056                                 get { return true; }
3057                         }
3058
3059                         public int this [int index] {
3060                                 get {
3061                                         int [] indices = GetIndices ();
3062                                         if (index < 0 || index >= indices.Length)
3063                                                 throw new ArgumentOutOfRangeException ("index");
3064                                         return indices [index];
3065                                 }
3066                         }
3067
3068                         bool ICollection.IsSynchronized {
3069                                 get { return false; }
3070                         }
3071
3072                         object ICollection.SyncRoot {
3073                                 get { return this; }
3074                         }
3075
3076                         bool IList.IsFixedSize {
3077                                 get { return true; }
3078                         }
3079
3080                         object IList.this [int index] {
3081                                 get { return this [index]; }
3082                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
3083                         }
3084                         #endregion      // Public Properties
3085
3086                         #region Public Methods
3087                         public bool Contains (int checkedIndex)
3088                         {
3089                                 int [] indices = GetIndices ();
3090                                 for (int i = 0; i < indices.Length; i++) {
3091                                         if (indices [i] == checkedIndex)
3092                                                 return true;
3093                                 }
3094                                 return false;
3095                         }
3096
3097                         public IEnumerator GetEnumerator ()
3098                         {
3099                                 int [] indices = GetIndices ();
3100                                 return indices.GetEnumerator ();
3101                         }
3102
3103                         void ICollection.CopyTo (Array dest, int index)
3104                         {
3105                                 int [] indices = GetIndices ();
3106                                 Array.Copy (indices, 0, dest, index, indices.Length);
3107                         }
3108
3109                         int IList.Add (object value)
3110                         {
3111                                 throw new NotSupportedException ("Add operation is not supported.");
3112                         }
3113
3114                         void IList.Clear ()
3115                         {
3116                                 throw new NotSupportedException ("Clear operation is not supported.");
3117                         }
3118
3119                         bool IList.Contains (object checkedIndex)
3120                         {
3121                                 if (!(checkedIndex is int))
3122                                         return false;
3123                                 return Contains ((int) checkedIndex);
3124                         }
3125
3126                         int IList.IndexOf (object checkedIndex)
3127                         {
3128                                 if (!(checkedIndex is int))
3129                                         return -1;
3130                                 return IndexOf ((int) checkedIndex);
3131                         }
3132
3133                         void IList.Insert (int index, object value)
3134                         {
3135                                 throw new NotSupportedException ("Insert operation is not supported.");
3136                         }
3137
3138                         void IList.Remove (object value)
3139                         {
3140                                 throw new NotSupportedException ("Remove operation is not supported.");
3141                         }
3142
3143                         void IList.RemoveAt (int index)
3144                         {
3145                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
3146                         }
3147
3148                         public int IndexOf (int checkedIndex)
3149                         {
3150                                 int [] indices = GetIndices ();
3151                                 for (int i = 0; i < indices.Length; i++) {
3152                                         if (indices [i] == checkedIndex)
3153                                                 return i;
3154                                 }
3155                                 return -1;
3156                         }
3157                         #endregion      // Public Methods
3158
3159                         private int [] GetIndices ()
3160                         {
3161                                 ArrayList checked_items = owner.CheckedItems.List;
3162                                 int [] indices = new int [checked_items.Count];
3163                                 for (int i = 0; i < checked_items.Count; i++) {
3164                                         ListViewItem item = (ListViewItem) checked_items [i];
3165                                         indices [i] = item.Index;
3166                                 }
3167                                 return indices;
3168                         }
3169                 }       // CheckedIndexCollection
3170
3171                 public class CheckedListViewItemCollection : IList, ICollection, IEnumerable
3172                 {
3173                         private readonly ListView owner;
3174                         private ArrayList list;
3175
3176                         #region Public Constructor
3177                         public CheckedListViewItemCollection (ListView owner)
3178                         {
3179                                 this.owner = owner;
3180                                 this.owner.Items.Changed += new CollectionChangedHandler (
3181                                         ItemsCollection_Changed);
3182                         }
3183                         #endregion      // Public Constructor
3184
3185                         #region Public Properties
3186                         [Browsable (false)]
3187                         public int Count {
3188                                 get {
3189                                         if (!owner.CheckBoxes)
3190                                                 return 0;
3191                                         return List.Count;
3192                                 }
3193                         }
3194
3195                         public bool IsReadOnly {
3196                                 get { return true; }
3197                         }
3198
3199                         public ListViewItem this [int index] {
3200                                 get {
3201                                         ArrayList checked_items = List;
3202                                         if (index < 0 || index >= checked_items.Count)
3203                                                 throw new ArgumentOutOfRangeException ("index");
3204                                         return (ListViewItem) checked_items [index];
3205                                 }
3206                         }
3207
3208 #if NET_2_0
3209                         public virtual ListViewItem this [string key] {
3210                                 get {
3211                                         int idx = IndexOfKey (key);
3212                                         return idx == -1 ? null : (ListViewItem) List [idx];
3213                                 }
3214                         }
3215 #endif
3216
3217                         bool ICollection.IsSynchronized {
3218                                 get { return false; }
3219                         }
3220
3221                         object ICollection.SyncRoot {
3222                                 get { return this; }
3223                         }
3224
3225                         bool IList.IsFixedSize {
3226                                 get { return true; }
3227                         }
3228
3229                         object IList.this [int index] {
3230                                 get { return this [index]; }
3231                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
3232                         }
3233                         #endregion      // Public Properties
3234
3235                         #region Public Methods
3236                         public bool Contains (ListViewItem item)
3237                         {
3238                                 if (!owner.CheckBoxes)
3239                                         return false;
3240                                 return List.Contains (item);
3241                         }
3242
3243 #if NET_2_0
3244                         public virtual bool ContainsKey (string key)
3245                         {
3246                                 return IndexOfKey (key) != -1;
3247                         }
3248 #endif
3249
3250                         public void CopyTo (Array dest, int index)
3251                         {
3252                                 if (!owner.CheckBoxes)
3253                                         return;
3254                                 List.CopyTo (dest, index);
3255                         }
3256
3257                         public IEnumerator GetEnumerator ()
3258                         {
3259                                 if (!owner.CheckBoxes)
3260                                         return (new ListViewItem [0]).GetEnumerator ();
3261                                 return List.GetEnumerator ();
3262                         }
3263
3264                         int IList.Add (object value)
3265                         {
3266                                 throw new NotSupportedException ("Add operation is not supported.");
3267                         }
3268
3269                         void IList.Clear ()
3270                         {
3271                                 throw new NotSupportedException ("Clear operation is not supported.");
3272                         }
3273
3274                         bool IList.Contains (object item)
3275                         {
3276                                 if (!(item is ListViewItem))
3277                                         return false;
3278                                 return Contains ((ListViewItem) item);
3279                         }
3280
3281                         int IList.IndexOf (object item)
3282                         {
3283                                 if (!(item is ListViewItem))
3284                                         return -1;
3285                                 return IndexOf ((ListViewItem) item);
3286                         }
3287
3288                         void IList.Insert (int index, object value)
3289                         {
3290                                 throw new NotSupportedException ("Insert operation is not supported.");
3291                         }
3292
3293                         void IList.Remove (object value)
3294                         {
3295                                 throw new NotSupportedException ("Remove operation is not supported.");
3296                         }
3297
3298                         void IList.RemoveAt (int index)
3299                         {
3300                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
3301                         }
3302
3303                         public int IndexOf (ListViewItem item)
3304                         {
3305                                 if (!owner.CheckBoxes)
3306                                         return -1;
3307                                 return List.IndexOf (item);
3308                         }
3309
3310 #if NET_2_0
3311                         public virtual int IndexOfKey (string key)
3312                         {
3313                                 if (key == null || key.Length == 0)
3314                                         return -1;
3315
3316                                 ArrayList checked_items = List;
3317                                 for (int i = 0; i < checked_items.Count; i++) {
3318                                         ListViewItem item = (ListViewItem) checked_items [i];
3319                                         if (String.Compare (key, item.Name, true) == 0)
3320                                                 return i;
3321                                 }
3322
3323                                 return -1;
3324                         }
3325 #endif
3326                         #endregion      // Public Methods
3327
3328                         internal ArrayList List {
3329                                 get {
3330                                         if (list == null) {
3331                                                 list = new ArrayList ();
3332                                                 foreach (ListViewItem item in owner.Items) {
3333                                                         if (item.Checked)
3334                                                                 list.Add (item);
3335                                                 }
3336                                         }
3337                                         return list;
3338                                 }
3339                         }
3340
3341                         internal void Reset ()
3342                         {
3343                                 // force re-population of list
3344                                 list = null;
3345                         }
3346
3347                         private void ItemsCollection_Changed ()
3348                         {
3349                                 Reset ();
3350                         }
3351                 }       // CheckedListViewItemCollection
3352
3353                 public class ColumnHeaderCollection : IList, ICollection, IEnumerable
3354                 {
3355                         internal ArrayList list;
3356                         private ListView owner;
3357
3358                         #region Public Constructor
3359                         public ColumnHeaderCollection (ListView owner)
3360                         {
3361                                 list = new ArrayList ();
3362                                 this.owner = owner;
3363                         }
3364                         #endregion      // Public Constructor
3365
3366                         #region Public Properties
3367                         [Browsable (false)]
3368                         public int Count {
3369                                 get { return list.Count; }
3370                         }
3371
3372                         public bool IsReadOnly {
3373                                 get { return false; }
3374                         }
3375
3376                         public virtual ColumnHeader this [int index] {
3377                                 get {
3378                                         if (index < 0 || index >= list.Count)
3379                                                 throw new ArgumentOutOfRangeException ("index");
3380                                         return (ColumnHeader) list [index];
3381                                 }
3382                         }
3383
3384 #if NET_2_0
3385                         public virtual ColumnHeader this [string key] {
3386                                 get {
3387                                         int idx = IndexOfKey (key);
3388                                         if (idx == -1)
3389                                                 return null;
3390
3391                                         return (ColumnHeader) list [idx];
3392                                 }
3393                         }
3394 #endif
3395
3396                         bool ICollection.IsSynchronized {
3397                                 get { return true; }
3398                         }
3399
3400                         object ICollection.SyncRoot {
3401                                 get { return this; }
3402                         }
3403
3404                         bool IList.IsFixedSize {
3405                                 get { return list.IsFixedSize; }
3406                         }
3407
3408                         object IList.this [int index] {
3409                                 get { return this [index]; }
3410                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
3411                         }
3412                         #endregion      // Public Properties
3413
3414                         #region Public Methods
3415                         public virtual int Add (ColumnHeader value)
3416                         {
3417                                 int idx = list.Add (value);
3418                                 owner.AddColumn (value, idx, true);
3419                                 return idx;
3420                         }
3421
3422                         public virtual ColumnHeader Add (string str, int width, HorizontalAlignment textAlign)
3423                         {
3424                                 ColumnHeader colHeader = new ColumnHeader (this.owner, str, textAlign, width);
3425                                 this.Add (colHeader);
3426                                 return colHeader;
3427                         }
3428
3429 #if NET_2_0
3430                         public virtual ColumnHeader Add (string text)
3431                         {
3432                                 return Add (String.Empty, text);
3433                         }
3434
3435                         public virtual ColumnHeader Add (string text, int iwidth)
3436                         {
3437                                 return Add (String.Empty, text, iwidth);
3438                         }
3439
3440                         public virtual ColumnHeader Add (string key, string text)
3441                         {
3442                                 ColumnHeader colHeader = new ColumnHeader ();
3443                                 colHeader.Name = key;
3444                                 colHeader.Text = text;
3445                                 Add (colHeader);
3446                                 return colHeader;
3447                         }
3448
3449                         public virtual ColumnHeader Add (string key, string text, int iwidth)
3450                         {
3451                                 return Add (key, text, iwidth, HorizontalAlignment.Left, -1);
3452                         }
3453
3454                         public virtual ColumnHeader Add (string key, string text, int iwidth, HorizontalAlignment textAlign, int imageIndex)
3455                         {
3456                                 ColumnHeader colHeader = new ColumnHeader (key, text, iwidth, textAlign);
3457                                 colHeader.ImageIndex = imageIndex;
3458                                 Add (colHeader);
3459                                 return colHeader;
3460                         }
3461
3462                         public virtual ColumnHeader Add (string key, string text, int iwidth, HorizontalAlignment textAlign, string imageKey)
3463                         {
3464                                 ColumnHeader colHeader = new ColumnHeader (key, text, iwidth, textAlign);
3465                                 colHeader.ImageKey = imageKey;
3466                                 Add (colHeader);
3467                                 return colHeader;
3468                         }
3469 #endif
3470
3471                         public virtual void AddRange (ColumnHeader [] values)
3472                         {
3473                                 foreach (ColumnHeader colHeader in values) {
3474                                         int idx = list.Add (colHeader);
3475                                         owner.AddColumn (colHeader, idx, false);
3476                                 }
3477                                 
3478                                 owner.Redraw (true);
3479                         }
3480
3481                         public virtual void Clear ()
3482                         {
3483                                 foreach (ColumnHeader col in list)
3484                                         col.SetListView (null);
3485                                 list.Clear ();
3486                                 owner.ReorderColumns (new int [0], true);
3487                         }
3488
3489                         public bool Contains (ColumnHeader value)
3490                         {
3491                                 return list.Contains (value);
3492                         }
3493
3494 #if NET_2_0
3495                         public virtual bool ContainsKey (string key)
3496                         {
3497                                 return IndexOfKey (key) != -1;
3498                         }
3499 #endif
3500
3501                         public IEnumerator GetEnumerator ()
3502                         {
3503                                 return list.GetEnumerator ();
3504                         }
3505
3506                         void ICollection.CopyTo (Array dest, int index)
3507                         {
3508                                 list.CopyTo (dest, index);
3509                         }
3510
3511                         int IList.Add (object value)
3512                         {
3513                                 if (! (value is ColumnHeader)) {
3514                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
3515                                 }
3516
3517                                 return this.Add ((ColumnHeader) value);
3518                         }
3519
3520                         bool IList.Contains (object value)
3521                         {
3522                                 if (! (value is ColumnHeader)) {
3523                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
3524                                 }
3525
3526                                 return this.Contains ((ColumnHeader) value);
3527                         }
3528
3529                         int IList.IndexOf (object value)
3530                         {
3531                                 if (! (value is ColumnHeader)) {
3532                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
3533                                 }
3534
3535                                 return this.IndexOf ((ColumnHeader) value);
3536                         }
3537
3538                         void IList.Insert (int index, object value)
3539                         {
3540                                 if (! (value is ColumnHeader)) {
3541                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
3542                                 }
3543
3544                                 this.Insert (index, (ColumnHeader) value);
3545                         }
3546
3547                         void IList.Remove (object value)
3548                         {
3549                                 if (! (value is ColumnHeader)) {
3550                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
3551                                 }
3552
3553                                 this.Remove ((ColumnHeader) value);
3554                         }
3555
3556                         public int IndexOf (ColumnHeader value)
3557                         {
3558                                 return list.IndexOf (value);
3559                         }
3560
3561 #if NET_2_0
3562                         public virtual int IndexOfKey (string key)
3563                         {
3564                                 if (key == null || key.Length == 0)
3565                                         return -1;
3566
3567                                 for (int i = 0; i < list.Count; i++) {
3568                                         ColumnHeader col = (ColumnHeader) list [i];
3569                                         if (String.Compare (key, col.Name, true) == 0)
3570                                                 return i;
3571                                 }
3572
3573                                 return -1;
3574                         }
3575 #endif
3576
3577                         public void Insert (int index, ColumnHeader value)
3578                         {
3579                                 // LAMESPEC: MSDOCS say greater than or equal to the value of the Count property
3580                                 // but it's really only greater.
3581                                 if (index < 0 || index > list.Count)
3582                                         throw new ArgumentOutOfRangeException ("index");
3583
3584                                 list.Insert (index, value);
3585                                 owner.AddColumn (value, index, true);
3586                         }
3587
3588 #if NET_2_0
3589                         public void Insert (int index, string text)
3590                         {
3591                                 Insert (index, String.Empty, text);
3592                         }
3593
3594                         public void Insert (int index, string text, int width)
3595                         {
3596                                 Insert (index, String.Empty, text, width);
3597                         }
3598
3599                         public void Insert (int index, string key, string text)
3600                         {
3601                                 ColumnHeader colHeader = new ColumnHeader ();
3602                                 colHeader.Name = key;
3603                                 colHeader.Text = text;
3604                                 Insert (index, colHeader);
3605                         }
3606
3607                         public void Insert (int index, string key, string text, int width)
3608                         {
3609                                 ColumnHeader colHeader = new ColumnHeader (key, text, width, HorizontalAlignment.Left);
3610                                 Insert (index, colHeader);
3611                         }
3612
3613                         public void Insert (int index, string key, string text, int width, HorizontalAlignment textAlign, int imageIndex)
3614                         {
3615                                 ColumnHeader colHeader = new ColumnHeader (key, text, width, textAlign);
3616                                 colHeader.ImageIndex = imageIndex;
3617                                 Insert (index, colHeader);
3618                         }
3619
3620                         public void Insert (int index, string key, string text, int width, HorizontalAlignment textAlign, string imageKey)
3621                         {
3622                                 ColumnHeader colHeader = new ColumnHeader (key, text, width, textAlign);
3623                                 colHeader.ImageKey = imageKey;
3624                                 Insert (index, colHeader);
3625                         }
3626 #endif
3627
3628                         public void Insert (int index, string str, int width, HorizontalAlignment textAlign)
3629                         {
3630                                 ColumnHeader colHeader = new ColumnHeader (this.owner, str, textAlign, width);
3631                                 this.Insert (index, colHeader);
3632                         }
3633
3634                         public virtual void Remove (ColumnHeader column)
3635                         {
3636                                 if (!Contains (column))
3637                                         return;
3638
3639                                 list.Remove (column);
3640                                 column.SetListView (null);
3641
3642                                 int rem_display_index = column.InternalDisplayIndex;
3643                                 int [] display_indices = new int [list.Count];
3644                                 for (int i = 0; i < display_indices.Length; i++) {
3645                                         ColumnHeader col = (ColumnHeader) list [i];
3646                                         int display_index = col.InternalDisplayIndex;
3647                                         if (display_index < rem_display_index) {
3648                                                 display_indices [i] = display_index;
3649                                         } else {
3650                                                 display_indices [i] = (display_index - 1);
3651                                         }
3652                                 }
3653
3654                                 column.InternalDisplayIndex = -1;
3655                                 owner.ReorderColumns (display_indices, true);
3656                         }
3657
3658 #if NET_2_0
3659                         public virtual void RemoveByKey (string key)
3660                         {
3661                                 int idx = IndexOfKey (key);
3662                                 if (idx != -1)
3663                                         RemoveAt (idx);
3664                         }
3665 #endif
3666
3667                         public virtual void RemoveAt (int index)
3668                         {
3669                                 if (index < 0 || index >= list.Count)
3670                                         throw new ArgumentOutOfRangeException ("index");
3671
3672                                 ColumnHeader col = (ColumnHeader) list [index];
3673                                 Remove (col);
3674                         }
3675                         #endregion      // Public Methods
3676                         
3677
3678                 }       // ColumnHeaderCollection
3679
3680                 public class ListViewItemCollection : IList, ICollection, IEnumerable
3681                 {
3682                         private readonly ArrayList list;
3683                         private readonly ListView owner;
3684
3685                         #region Public Constructor
3686                         public ListViewItemCollection (ListView owner)
3687                         {
3688                                 list = new ArrayList (0);
3689                                 this.owner = owner;
3690                         }
3691                         #endregion      // Public Constructor
3692
3693                         #region Public Properties
3694                         [Browsable (false)]
3695                         public int Count {
3696                                 get {
3697 #if NET_2_0
3698                                         if (owner != null && owner.VirtualMode)
3699                                                 return owner.VirtualListSize;
3700 #endif
3701
3702                                         return list.Count; 
3703                                 }
3704                         }
3705
3706                         public bool IsReadOnly {
3707                                 get { return false; }
3708                         }
3709
3710                         public virtual ListViewItem this [int displayIndex] {
3711                                 get {
3712                                         if (displayIndex < 0 || displayIndex >= Count)
3713                                                 throw new ArgumentOutOfRangeException ("displayIndex");
3714
3715 #if NET_2_0
3716                                         if (owner != null && owner.VirtualMode)
3717                                                 return RetrieveVirtualItemFromOwner (displayIndex);
3718 #endif
3719                                         return (ListViewItem) list [displayIndex];
3720                                 }
3721
3722                                 set {
3723                                         if (displayIndex < 0 || displayIndex >= Count)
3724                                                 throw new ArgumentOutOfRangeException ("displayIndex");
3725
3726 #if NET_2_0
3727                                         if (owner != null && owner.VirtualMode)
3728                                                 throw new InvalidOperationException ();
3729 #endif
3730
3731                                         if (list.Contains (value))
3732                                                 throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "value");
3733
3734                                         if (value.ListView != null && value.ListView != owner)
3735                                                 throw new ArgumentException ("Cannot add or insert the item '" + value.Text + "' in more than one place. You must first remove it from its current location or clone it.", "value");
3736
3737                                         value.Owner = owner;
3738                                         list [displayIndex] = value;
3739                                         OnChange ();
3740
3741                                         owner.Redraw (true);
3742                                 }
3743                         }
3744
3745 #if NET_2_0
3746                         public virtual ListViewItem this [string key] {
3747                                 get {
3748                                         int idx = IndexOfKey (key);
3749                                         if (idx == -1)
3750                                                 return null;
3751
3752                                         return this [idx];
3753                                 }
3754                         }
3755 #endif
3756
3757                         bool ICollection.IsSynchronized {
3758                                 get { return true; }
3759                         }
3760
3761                         object ICollection.SyncRoot {
3762                                 get { return this; }
3763                         }
3764
3765                         bool IList.IsFixedSize {
3766                                 get { return list.IsFixedSize; }
3767                         }
3768
3769                         object IList.this [int index] {
3770                                 get { return this [index]; }
3771                                 set {
3772                                         if (value is ListViewItem)
3773                                                 this [index] = (ListViewItem) value;
3774                                         else
3775                                                 this [index] = new ListViewItem (value.ToString ());
3776                                         OnChange ();
3777                                 }
3778                         }
3779                         #endregion      // Public Properties
3780
3781                         #region Public Methods
3782                         public virtual ListViewItem Add (ListViewItem value)
3783                         {
3784 #if NET_2_0
3785                                 if (owner != null && owner.VirtualMode)
3786                                         throw new InvalidOperationException ();
3787 #endif
3788
3789                                 AddItem (value);
3790                                 CollectionChanged (true);
3791
3792                                 return value;
3793                         }
3794
3795                         public virtual ListViewItem Add (string text)
3796                         {
3797                                 ListViewItem item = new ListViewItem (text);
3798                                 return this.Add (item);
3799                         }
3800
3801                         public virtual ListViewItem Add (string text, int imageIndex)
3802                         {
3803                                 ListViewItem item = new ListViewItem (text, imageIndex);
3804                                 return this.Add (item);
3805                         }
3806
3807 #if NET_2_0
3808                         public virtual ListViewItem Add (string text, string imageKey)
3809                         {
3810                                 ListViewItem item = new ListViewItem (text, imageKey);
3811                                 return this.Add (item);
3812                         }
3813
3814                         public virtual ListViewItem Add (string key, string text, int imageIndex)
3815                         {
3816                                 ListViewItem item = new ListViewItem (text, imageIndex);
3817                                 item.Name = key;
3818                                 return this.Add (item);
3819                         }
3820
3821                         public virtual ListViewItem Add (string key, string text, string imageKey)
3822                         {
3823                                 ListViewItem item = new ListViewItem (text, imageKey);
3824                                 item.Name = key;
3825                                 return this.Add (item);
3826                         }
3827 #endif
3828
3829                         public void AddRange (ListViewItem [] values)
3830                         {
3831                                 if (values == null)
3832                                         throw new ArgumentNullException ("Argument cannot be null!", "values");
3833 #if NET_2_0
3834                                 if (owner != null && owner.VirtualMode)
3835                                         throw new InvalidOperationException ();
3836 #endif
3837
3838                                 foreach (ListViewItem item in values)
3839                                         AddItem (item);
3840
3841                                 CollectionChanged (true);
3842                         }
3843
3844 #if NET_2_0
3845                         public void AddRange (ListViewItemCollection items)
3846                         {
3847                                 if (items == null)
3848                                         throw new ArgumentNullException ("Argument cannot be null!", "items");
3849
3850                                 ListViewItem[] itemArray = new ListViewItem[items.Count];
3851                                 items.CopyTo (itemArray,0);
3852                                 this.AddRange (itemArray);
3853                         }
3854 #endif
3855
3856                         public virtual void Clear ()
3857                         {
3858 #if NET_2_0
3859                                 if (owner != null && owner.VirtualMode)
3860                                         throw new InvalidOperationException ();
3861 #endif
3862                                 owner.SetFocusedItem (null);
3863                                 owner.h_scroll.Value = owner.v_scroll.Value = 0;
3864                                 foreach (ListViewItem item in list) {
3865                                         owner.item_control.CancelEdit (item);
3866                                         item.Owner = null;
3867                                 }
3868                                 list.Clear ();
3869                                 CollectionChanged (false);
3870                         }
3871
3872                         public bool Contains (ListViewItem item)
3873                         {
3874                                 return IndexOf (item) != -1;
3875                         }
3876
3877 #if NET_2_0
3878                         public virtual bool ContainsKey (string key)
3879                         {
3880                                 return IndexOfKey (key) != -1;
3881                         }
3882 #endif
3883
3884                         public void CopyTo (Array dest, int index)
3885                         {
3886                                 list.CopyTo (dest, index);
3887                         }
3888
3889 #if NET_2_0
3890                         public ListViewItem [] Find (string key, bool searchAllSubitems)
3891                         {
3892                                 if (key == null)
3893                                         return new ListViewItem [0];
3894
3895                                 List<ListViewItem> temp_list = new List<ListViewItem> ();
3896                                 
3897                                 for (int i = 0; i < list.Count; i++) {
3898                                         ListViewItem lvi = (ListViewItem) list [i];
3899                                         if (String.Compare (key, lvi.Name, true) == 0)
3900                                                 temp_list.Add (lvi);
3901                                 }
3902
3903                                 ListViewItem [] retval = new ListViewItem [temp_list.Count];
3904                                 temp_list.CopyTo (retval);
3905
3906                                 return retval;
3907                         }
3908 #endif
3909
3910                         public IEnumerator GetEnumerator ()
3911                         {
3912 #if NET_2_0
3913                                 if (owner != null && owner.VirtualMode)
3914                                         throw new InvalidOperationException ();
3915 #endif
3916                                 
3917                                 return list.GetEnumerator ();
3918                         }
3919
3920                         int IList.Add (object item)
3921                         {
3922                                 int result;
3923                                 ListViewItem li;
3924
3925 #if NET_2_0
3926                                 if (owner != null && owner.VirtualMode)
3927                                         throw new InvalidOperationException ();
3928 #endif
3929
3930                                 if (item is ListViewItem) {
3931                                         li = (ListViewItem) item;
3932                                         if (list.Contains (li))
3933                                                 throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "item");
3934
3935                                         if (li.ListView != null && li.ListView != owner)
3936                                                 throw new ArgumentException ("Cannot add or insert the item '" + li.Text + "' in more than one place. You must first remove it from its current location or clone it.", "item");
3937                                 }
3938                                 else
3939                                         li = new ListViewItem (item.ToString ());
3940
3941                                 li.Owner = owner;
3942                                 result = list.Add (li);
3943                                 CollectionChanged (true);
3944
3945                                 return result;
3946                         }
3947
3948                         bool IList.Contains (object item)
3949                         {
3950                                 return Contains ((ListViewItem) item);
3951                         }
3952
3953                         int IList.IndexOf (object item)
3954                         {
3955                                 return IndexOf ((ListViewItem) item);
3956                         }
3957
3958                         void IList.Insert (int index, object item)
3959                         {
3960                                 if (item is ListViewItem)
3961                                         this.Insert (index, (ListViewItem) item);
3962                                 else
3963                                         this.Insert (index, item.ToString ());
3964                         }
3965
3966                         void IList.Remove (object item)
3967                         {
3968                                 Remove ((ListViewItem) item);
3969                         }
3970
3971                         public int IndexOf (ListViewItem item)
3972                         {
3973 #if NET_2_0
3974                                 if (owner != null && owner.VirtualMode) {
3975                                         for (int i = 0; i < Count; i++)
3976                                                 if (RetrieveVirtualItemFromOwner (i) == item)
3977                                                         return i;
3978
3979                                         return -1;
3980                                 }
3981 #endif
3982                                 
3983                                 return list.IndexOf (item);
3984                         }
3985
3986 #if NET_2_0
3987                         public virtual int IndexOfKey (string key)
3988                         {
3989                                 if (key == null || key.Length == 0)
3990                                         return -1;
3991
3992                                 for (int i = 0; i < Count; i++) {
3993                                         ListViewItem lvi = this [i];
3994                                         if (String.Compare (key, lvi.Name, true) == 0)
3995                                                 return i;
3996                                 }
3997
3998                                 return -1;
3999                         }
4000 #endif
4001
4002                         public ListViewItem Insert (int index, ListViewItem item)
4003                         {
4004                                 if (index < 0 || index > list.Count)
4005                                         throw new ArgumentOutOfRangeException ("index");
4006
4007 #if NET_2_0
4008                                 if (owner != null && owner.VirtualMode)
4009                                         throw new InvalidOperationException ();
4010 #endif
4011
4012                                 if (list.Contains (item))
4013                                         throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "item");
4014
4015                                 if (item.ListView != null && item.ListView != owner)
4016                                         throw new ArgumentException ("Cannot add or insert the item '" + item.Text + "' in more than one place. You must first remove it from its current location or clone it.", "item");
4017
4018                                 item.Owner = owner;
4019                                 list.Insert (index, item);
4020                                 CollectionChanged (true);
4021                                 return item;
4022                         }
4023
4024                         public ListViewItem Insert (int index, string text)
4025                         {
4026                                 return this.Insert (index, new ListViewItem (text));
4027                         }
4028
4029                         public ListViewItem Insert (int index, string text, int imageIndex)
4030                         {
4031                                 return this.Insert (index, new ListViewItem (text, imageIndex));
4032                         }
4033
4034 #if NET_2_0
4035                         public ListViewItem Insert (int index, string key, string text, int imageIndex)
4036                         {
4037                                 ListViewItem lvi = new ListViewItem (text, imageIndex);
4038                                 lvi.Name = key;
4039                                 return Insert (index, lvi);
4040                         }
4041 #endif
4042
4043                         public virtual void Remove (ListViewItem item)
4044                         {
4045 #if NET_2_0
4046                                 if (owner != null && owner.VirtualMode)
4047                                         throw new InvalidOperationException ();
4048 #endif
4049                                 if (!list.Contains (item))
4050                                         return;
4051                                         
4052                                 bool selection_changed = owner.SelectedItems.Contains (item);
4053                                 owner.item_control.CancelEdit (item);
4054                                 list.Remove (item);
4055                                 item.Owner = null;
4056                                 CollectionChanged (false);
4057                                 if (selection_changed)
4058                                         owner.OnSelectedIndexChanged (EventArgs.Empty);
4059                         }
4060
4061                         public virtual void RemoveAt (int index)
4062                         {
4063                                 if (index < 0 || index >= Count)
4064                                         throw new ArgumentOutOfRangeException ("index");
4065
4066 #if NET_2_0
4067                                 if (owner != null && owner.VirtualMode)
4068                                         throw new InvalidOperationException ();
4069 #endif
4070
4071                                 ListViewItem item = (ListViewItem) list [index];
4072                                 Remove (item);
4073                         }
4074
4075 #if NET_2_0
4076                         public virtual void RemoveByKey (string key)
4077                         {
4078                                 int idx = IndexOfKey (key);
4079                                 if (idx != -1)
4080                                         RemoveAt (idx);
4081                         }
4082 #endif
4083
4084                         #endregion      // Public Methods
4085
4086                         void AddItem (ListViewItem value)
4087                         {
4088                                 if (list.Contains (value))
4089                                         throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "value");
4090
4091                                 if (value.ListView != null && value.ListView != owner)
4092                                         throw new ArgumentException ("Cannot add or insert the item '" + value.Text + "' in more than one place. You must first remove it from its current location or clone it.", "value");
4093                                 value.Owner = owner;
4094                                 list.Add (value);
4095                         }
4096
4097                         void CollectionChanged (bool sort)
4098                         {
4099                                 if (owner != null) {
4100                                         if (sort)
4101                                                 owner.Sort (false);
4102
4103                                         OnChange ();
4104                                         owner.Redraw (true);
4105                                 }
4106                         }
4107
4108 #if NET_2_0
4109                         ListViewItem RetrieveVirtualItemFromOwner (int displayIndex)
4110                         {
4111                                 RetrieveVirtualItemEventArgs args = new RetrieveVirtualItemEventArgs (displayIndex);
4112
4113                                 owner.OnRetrieveVirtualItem (args);
4114                                 ListViewItem retval = args.Item;
4115                                 retval.Owner = owner;
4116                                 retval.SetIndex (displayIndex);
4117
4118                                 return retval;
4119                         }
4120 #endif
4121
4122                         internal event CollectionChangedHandler Changed;
4123
4124                         internal void Sort (IComparer comparer)
4125                         {
4126                                 list.Sort (comparer);
4127                                 OnChange ();
4128                         }
4129
4130                         internal void OnChange ()
4131                         {
4132                                 if (Changed != null)
4133                                         Changed ();
4134                         }
4135                 }       // ListViewItemCollection
4136
4137                 public class SelectedIndexCollection : IList, ICollection, IEnumerable
4138                 {
4139                         private readonly ListView owner;
4140
4141                         #region Public Constructor
4142                         public SelectedIndexCollection (ListView owner)
4143                         {
4144                                 this.owner = owner;
4145                         }
4146                         #endregion      // Public Constructor
4147
4148                         #region Public Properties
4149                         [Browsable (false)]
4150                         public int Count {
4151                                 get {
4152                                         return owner.SelectedItems.Count;
4153                                 }
4154                         }
4155
4156                         public bool IsReadOnly {
4157                                 get { 
4158 #if NET_2_0
4159                                         return false;
4160 #else
4161                                         return true; 
4162 #endif
4163                                 }
4164                         }
4165
4166                         public int this [int index] {
4167                                 get {
4168                                         int [] indices = GetIndices ();
4169                                         if (index < 0 || index >= indices.Length)
4170                                                 throw new ArgumentOutOfRangeException ("index");
4171                                         return indices [index];
4172                                 }
4173                         }
4174
4175                         bool ICollection.IsSynchronized {
4176                                 get { return false; }
4177                         }
4178
4179                         object ICollection.SyncRoot {
4180                                 get { return this; }
4181                         }
4182
4183                         bool IList.IsFixedSize {
4184                                 get { 
4185 #if NET_2_0
4186                                         return false;
4187 #else
4188                                         return true;
4189 #endif
4190                                 }
4191                         }
4192
4193                         object IList.this [int index] {
4194                                 get { return this [index]; }
4195                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
4196                         }
4197                         #endregion      // Public Properties
4198
4199                         #region Public Methods
4200 #if NET_2_0
4201                         public int Add (int itemIndex)
4202                         {
4203                                 if (itemIndex < 0 || itemIndex >= owner.Items.Count)
4204                                         throw new ArgumentOutOfRangeException ("index");
4205
4206                                 owner.Items [itemIndex].Selected = true;
4207                                 if (!owner.IsHandleCreated)
4208                                         return 0;
4209
4210                                 return owner.SelectedItems.Count;
4211                         }
4212
4213                         public void Clear ()
4214                         {
4215                                 owner.SelectedItems.Clear ();
4216                         }
4217 #endif
4218                         public bool Contains (int selectedIndex)
4219                         {
4220                                 int [] indices = GetIndices ();
4221                                 for (int i = 0; i < indices.Length; i++) {
4222                                         if (indices [i] == selectedIndex)
4223                                                 return true;
4224                                 }
4225                                 return false;
4226                         }
4227
4228                         public void CopyTo (Array dest, int index)
4229                         {
4230                                 int [] indices = GetIndices ();
4231                                 Array.Copy (indices, 0, dest, index, indices.Length);
4232                         }
4233
4234                         public IEnumerator GetEnumerator ()
4235                         {
4236                                 int [] indices = GetIndices ();
4237                                 return indices.GetEnumerator ();
4238                         }
4239
4240                         int IList.Add (object value)
4241                         {
4242                                 throw new NotSupportedException ("Add operation is not supported.");
4243                         }
4244
4245                         void IList.Clear ()
4246                         {
4247                                 throw new NotSupportedException ("Clear operation is not supported.");
4248                         }
4249
4250                         bool IList.Contains (object selectedIndex)
4251                         {
4252                                 if (!(selectedIndex is int))
4253                                         return false;
4254                                 return Contains ((int) selectedIndex);
4255                         }
4256
4257                         int IList.IndexOf (object selectedIndex)
4258                         {
4259                                 if (!(selectedIndex is int))
4260                                         return -1;
4261                                 return IndexOf ((int) selectedIndex);
4262                         }
4263
4264                         void IList.Insert (int index, object value)
4265                         {
4266                                 throw new NotSupportedException ("Insert operation is not supported.");
4267                         }
4268
4269                         void IList.Remove (object value)
4270                         {
4271                                 throw new NotSupportedException ("Remove operation is not supported.");
4272                         }
4273
4274                         void IList.RemoveAt (int index)
4275                         {
4276                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
4277                         }
4278
4279                         public int IndexOf (int selectedIndex)
4280                         {
4281                                 int [] indices = GetIndices ();
4282                                 for (int i = 0; i < indices.Length; i++) {
4283                                         if (indices [i] == selectedIndex)
4284                                                 return i;
4285                                 }
4286                                 return -1;
4287                         }
4288
4289 #if NET_2_0
4290                         public void Remove (int itemIndex)
4291                         {
4292                                 if (itemIndex < 0 || itemIndex >= owner.Items.Count)
4293                                         throw new ArgumentOutOfRangeException ("itemIndex");
4294
4295                                 owner.Items [itemIndex].Selected = false;
4296                         }
4297 #endif
4298                         #endregion      // Public Methods
4299
4300                         private int [] GetIndices ()
4301                         {
4302                                 ArrayList selected_items = owner.SelectedItems.List;
4303                                 int [] indices = new int [selected_items.Count];
4304                                 for (int i = 0; i < selected_items.Count; i++) {
4305                                         ListViewItem item = (ListViewItem) selected_items [i];
4306                                         indices [i] = item.Index;
4307                                 }
4308                                 return indices;
4309                         }
4310                 }       // SelectedIndexCollection
4311
4312                 public class SelectedListViewItemCollection : IList, ICollection, IEnumerable
4313                 {
4314                         private readonly ListView owner;
4315                         private ArrayList list;
4316
4317                         #region Public Constructor
4318                         public SelectedListViewItemCollection (ListView owner)
4319                         {
4320                                 this.owner = owner;
4321                                 this.owner.Items.Changed += new CollectionChangedHandler (
4322                                         ItemsCollection_Changed);
4323                         }
4324                         #endregion      // Public Constructor
4325
4326                         #region Public Properties
4327                         [Browsable (false)]
4328                         public int Count {
4329                                 get {
4330                                         if (!owner.IsHandleCreated)
4331                                                 return 0;
4332                                         return List.Count;
4333                                 }
4334                         }
4335
4336                         public bool IsReadOnly {
4337                                 get { return true; }
4338                         }
4339
4340                         public ListViewItem this [int index] {
4341                                 get {
4342                                         ArrayList selected_items = List;
4343                                         if (!owner.IsHandleCreated || index < 0 || index >= selected_items.Count)
4344                                                 throw new ArgumentOutOfRangeException ("index");
4345                                         return (ListViewItem) selected_items [index];
4346                                 }
4347                         }
4348
4349 #if NET_2_0
4350                         public virtual ListViewItem this [string key] {
4351                                 get {
4352                                         int idx = IndexOfKey (key);
4353                                         if (idx == -1)
4354                                                 return null;
4355
4356                                         return (ListViewItem) List [idx];
4357                                 }
4358                         }
4359 #endif
4360
4361                         bool ICollection.IsSynchronized {
4362                                 get { return false; }
4363                         }
4364
4365                         object ICollection.SyncRoot {
4366                                 get { return this; }
4367                         }
4368
4369                         bool IList.IsFixedSize {
4370                                 get { return true; }
4371                         }
4372
4373                         object IList.this [int index] {
4374                                 get { return this [index]; }
4375                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
4376                         }
4377                         #endregion      // Public Properties
4378
4379                         #region Public Methods
4380                         public void Clear ()
4381                         {
4382                                 if (!owner.IsHandleCreated)
4383                                         return;
4384
4385                                 foreach (ListViewItem item in List)
4386                                         item.Selected = false;
4387                         }
4388
4389                         public bool Contains (ListViewItem item)
4390                         {
4391                                 if (!owner.IsHandleCreated)
4392                                         return false;
4393                                 return List.Contains (item);
4394                         }
4395
4396 #if NET_2_0
4397                         public virtual bool ContainsKey (string key)
4398                         {
4399                                 return IndexOfKey (key) != -1;
4400                         }
4401 #endif
4402
4403                         public void CopyTo (Array dest, int index)
4404                         {
4405                                 if (!owner.IsHandleCreated)
4406                                         return;
4407                                 List.CopyTo (dest, index);
4408                         }
4409
4410                         public IEnumerator GetEnumerator ()
4411                         {
4412                                 if (!owner.IsHandleCreated)
4413                                         return (new ListViewItem [0]).GetEnumerator ();
4414                                 return List.GetEnumerator ();
4415                         }
4416
4417                         int IList.Add (object value)
4418                         {
4419                                 throw new NotSupportedException ("Add operation is not supported.");
4420                         }
4421
4422                         bool IList.Contains (object item)
4423                         {
4424                                 if (!(item is ListViewItem))
4425                                         return false;
4426                                 return Contains ((ListViewItem) item);
4427                         }
4428
4429                         int IList.IndexOf (object item)
4430                         {
4431                                 if (!(item is ListViewItem))
4432                                         return -1;
4433                                 return IndexOf ((ListViewItem) item);
4434                         }
4435
4436                         void IList.Insert (int index, object value)
4437                         {
4438                                 throw new NotSupportedException ("Insert operation is not supported.");
4439                         }
4440
4441                         void IList.Remove (object value)
4442                         {
4443                                 throw new NotSupportedException ("Remove operation is not supported.");
4444                         }
4445
4446                         void IList.RemoveAt (int index)
4447                         {
4448                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
4449                         }
4450
4451                         public int IndexOf (ListViewItem item)
4452                         {
4453                                 if (!owner.IsHandleCreated)
4454                                         return -1;
4455                                 return List.IndexOf (item);
4456                         }
4457
4458 #if NET_2_0
4459                         public virtual int IndexOfKey (string key)
4460                         {
4461                                 if (!owner.IsHandleCreated || key == null || key.Length == 0)
4462                                         return -1;
4463
4464                                 ArrayList selected_items = List;
4465                                 for (int i = 0; i < selected_items.Count; i++) {
4466                                         ListViewItem item = (ListViewItem) selected_items [i];
4467                                         if (String.Compare (item.Name, key, true) == 0)
4468                                                 return i;
4469                                 }
4470
4471                                 return -1;
4472                         }
4473 #endif
4474                         #endregion      // Public Methods
4475
4476                         internal ArrayList List {
4477                                 get {
4478                                         if (list == null) {
4479                                                 list = new ArrayList ();
4480                                                 for (int i = 0; i < owner.Items.Count; i++) {
4481                                                         ListViewItem item = owner.Items [i];
4482                                                         if (item.Selected)
4483                                                                 list.Add (item);
4484                                                 }
4485                                         }
4486                                         return list;
4487                                 }
4488                         }
4489
4490                         internal void Reset ()
4491                         {
4492                                 // force re-population of list
4493                                 list = null;
4494                         }
4495
4496                         private void ItemsCollection_Changed ()
4497                         {
4498                                 Reset ();
4499                         }
4500                 }       // SelectedListViewItemCollection
4501
4502                 internal delegate void CollectionChangedHandler ();
4503
4504                 struct ItemMatrixLocation
4505                 {
4506                         int row;
4507                         int col;
4508
4509                         public ItemMatrixLocation (int row, int col)
4510                         {
4511                                 this.row = row;
4512                                 this.col = col;
4513                 
4514                         }
4515                 
4516                         public int Col {
4517                                 get {
4518                                         return col;
4519                                 }
4520                                 set {
4521                                         col = value;
4522                                 }
4523                         }
4524
4525                         public int Row {
4526                                 get {
4527                                         return row;
4528                                 }
4529                                 set {
4530                                         row = value;
4531                                 }
4532                         }
4533         
4534                 }
4535
4536                 #endregion // Subclasses
4537 #if NET_2_0
4538                 protected override void OnResize (EventArgs e)
4539                 {
4540                         base.OnResize (e);
4541                 }
4542
4543                 protected override void OnMouseLeave (EventArgs e)
4544                 {
4545                         base.OnMouseLeave (e);
4546                 }
4547
4548                 //
4549                 // ColumnReorder event
4550                 //
4551                 static object ColumnReorderedEvent = new object ();
4552                 public event ColumnReorderedEventHandler ColumnReordered {
4553                         add { Events.AddHandler (ColumnReorderedEvent, value); }
4554                         remove { Events.RemoveHandler (ColumnReorderedEvent, value); }
4555                 }
4556
4557                 protected virtual void OnColumnReordered (ColumnReorderedEventArgs e)
4558                 {
4559                         ColumnReorderedEventHandler creh = (ColumnReorderedEventHandler) (Events [ColumnReorderedEvent]);
4560
4561                         if (creh != null)
4562                                 creh (this, e);
4563                 }
4564
4565                 //
4566                 // ColumnWidthChanged
4567                 //
4568                 static object ColumnWidthChangedEvent = new object ();
4569                 public event ColumnWidthChangedEventHandler ColumnWidthChanged {
4570                         add { Events.AddHandler (ColumnWidthChangedEvent, value); }
4571                         remove { Events.RemoveHandler (ColumnWidthChangedEvent, value); }
4572                 }
4573
4574                 protected virtual void OnColumnWidthChanged (ColumnWidthChangedEventArgs e)
4575                 {
4576                         ColumnWidthChangedEventHandler eh = (ColumnWidthChangedEventHandler) (Events[ColumnWidthChangedEvent]);
4577                         if (eh != null)
4578                                 eh (this, e);
4579                 }
4580                 
4581                 void RaiseColumnWidthChanged (int resize_column)
4582                 {
4583                         ColumnWidthChangedEventArgs n = new ColumnWidthChangedEventArgs (resize_column);
4584
4585                         OnColumnWidthChanged (n);
4586                 }
4587                 
4588                 //
4589                 // ColumnWidthChanging
4590                 //
4591                 static object ColumnWidthChangingEvent = new object ();
4592                 public event ColumnWidthChangingEventHandler ColumnWidthChanging {
4593                         add { Events.AddHandler (ColumnWidthChangingEvent, value); }
4594                         remove { Events.RemoveHandler (ColumnWidthChangingEvent, value); }
4595                 }
4596
4597                 protected virtual void OnColumnWidthChanging (ColumnWidthChangingEventArgs e)
4598                 {
4599                         ColumnWidthChangingEventHandler cwceh = (ColumnWidthChangingEventHandler) (Events[ColumnWidthChangingEvent]);
4600                         if (cwceh != null)
4601                                 cwceh (this, e);
4602                 }
4603                 
4604                 //
4605                 // 2.0 profile based implementation
4606                 //
4607                 bool CanProceedWithResize (ColumnHeader col, int width)
4608                 {
4609                         ColumnWidthChangingEventHandler cwceh = (ColumnWidthChangingEventHandler) (Events[ColumnWidthChangingEvent]);
4610                         if (cwceh == null)
4611                                 return true;
4612                         
4613                         ColumnWidthChangingEventArgs changing = new ColumnWidthChangingEventArgs (col.Index, width);
4614                         cwceh (this, changing);
4615                         return !changing.Cancel;
4616                 }
4617 #else
4618                 //
4619                 // 1.0 profile based implementation
4620                 //
4621                 bool CanProceedWithResize (ColumnHeader col, int width)
4622                 {
4623                         return true;
4624                 }
4625
4626                 void RaiseColumnWidthChanged (int resize_column)
4627                 {
4628                 }
4629 #endif
4630         }
4631 }