New test + update.
[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 //      Carlos Alberto Cortez <calberto.cortez@gmail.com>
28 //
29
30
31 // NOT COMPLETE
32
33
34 using System.Collections;
35 using System.ComponentModel;
36 using System.ComponentModel.Design;
37 using System.Drawing;
38 using System.Runtime.InteropServices;
39 using System.Globalization;
40 #if NET_2_0
41 using System.Collections.Generic;
42 #endif
43
44 namespace System.Windows.Forms
45 {
46         [DefaultEvent ("SelectedIndexChanged")]
47         [DefaultProperty ("Items")]
48         [Designer ("System.Windows.Forms.Design.ListViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
49 #if NET_2_0
50         [ClassInterface (ClassInterfaceType.AutoDispatch)]
51         [ComVisible (true)]
52         [Docking (DockingBehavior.Ask)]
53 #endif
54         public class ListView : Control
55         {
56                 private ItemActivation activation = ItemActivation.Standard;
57                 private ListViewAlignment alignment = ListViewAlignment.Top;
58                 private bool allow_column_reorder;
59                 private bool auto_arrange = true;
60                 private bool check_boxes;
61                 private readonly CheckedIndexCollection checked_indices;
62                 private readonly CheckedListViewItemCollection checked_items;
63                 private readonly ColumnHeaderCollection columns;
64                 internal int focused_item_index = -1;
65                 private bool full_row_select;
66                 private bool grid_lines;
67                 private ColumnHeaderStyle header_style = ColumnHeaderStyle.Clickable;
68                 private bool hide_selection = true;
69                 private bool hover_selection;
70                 private IComparer item_sorter;
71                 private readonly ListViewItemCollection items;
72 #if NET_2_0
73                 private readonly ListViewGroupCollection groups;
74                 private bool owner_draw;
75                 private bool show_groups = true;
76 #endif
77                 private bool label_edit;
78                 private bool label_wrap = true;
79                 private bool multiselect = true;
80                 private bool scrollable = true;
81                 private bool hover_pending;
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                 internal 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 int[] reordered_items_indices;
101                 private Point [] items_location;
102                 private ItemMatrixLocation [] items_matrix_location;
103                 private Size item_size; // used for caching item size
104                 private int custom_column_width; // used when using Columns with SmallIcon/List views
105                 private int hot_item_index = -1;
106 #if NET_2_0
107                 private bool hot_tracking;
108                 private ListViewInsertionMark insertion_mark;
109                 private bool show_item_tooltips;
110                 private ToolTip item_tooltip;
111                 private Size tile_size;
112                 private bool virtual_mode;
113                 private int virtual_list_size;
114                 private bool right_to_left_layout;
115 #endif
116
117                 // internal variables
118                 internal ImageList large_image_list;
119                 internal ImageList small_image_list;
120                 internal Size text_size = Size.Empty;
121
122                 #region Events
123                 static object AfterLabelEditEvent = new object ();
124                 static object BeforeLabelEditEvent = new object ();
125                 static object ColumnClickEvent = new object ();
126                 static object ItemActivateEvent = new object ();
127                 static object ItemCheckEvent = new object ();
128                 static object ItemDragEvent = new object ();
129                 static object SelectedIndexChangedEvent = new object ();
130 #if NET_2_0
131                 static object DrawColumnHeaderEvent = new object();
132                 static object DrawItemEvent = new object();
133                 static object DrawSubItemEvent = new object();
134                 static object ItemCheckedEvent = new object ();
135                 static object ItemMouseHoverEvent = new object ();
136                 static object ItemSelectionChangedEvent = new object ();
137                 static object CacheVirtualItemsEvent = new object ();
138                 static object RetrieveVirtualItemEvent = new object ();
139                 static object RightToLeftLayoutChangedEvent = new object ();
140                 static object SearchForVirtualItemEvent = new object ();
141                 static object VirtualItemsSelectionRangeChangedEvent = new object ();
142 #endif
143
144                 public event LabelEditEventHandler AfterLabelEdit {
145                         add { Events.AddHandler (AfterLabelEditEvent, value); }
146                         remove { Events.RemoveHandler (AfterLabelEditEvent, value); }
147                 }
148
149 #if !NET_2_0
150                 [Browsable (false)]
151                 [EditorBrowsable (EditorBrowsableState.Never)]
152                 public new event EventHandler BackgroundImageChanged {
153                         add { base.BackgroundImageChanged += value; }
154                         remove { base.BackgroundImageChanged -= value; }
155                 }
156 #endif
157
158 #if NET_2_0
159                 [Browsable (false)]
160                 [EditorBrowsable (EditorBrowsableState.Never)]
161                 public new event EventHandler BackgroundImageLayoutChanged {
162                         add { base.BackgroundImageLayoutChanged += value; }
163                         remove { base.BackgroundImageLayoutChanged -= value; }
164                 }
165 #endif
166
167                 public event LabelEditEventHandler BeforeLabelEdit {
168                         add { Events.AddHandler (BeforeLabelEditEvent, value); }
169                         remove { Events.RemoveHandler (BeforeLabelEditEvent, value); }
170                 }
171
172                 public event ColumnClickEventHandler ColumnClick {
173                         add { Events.AddHandler (ColumnClickEvent, value); }
174                         remove { Events.RemoveHandler (ColumnClickEvent, value); }
175                 }
176
177 #if NET_2_0
178                 public event DrawListViewColumnHeaderEventHandler DrawColumnHeader {
179                         add { Events.AddHandler(DrawColumnHeaderEvent, value); }
180                         remove { Events.RemoveHandler(DrawColumnHeaderEvent, value); }
181                 }
182
183                 public event DrawListViewItemEventHandler DrawItem {
184                         add { Events.AddHandler(DrawItemEvent, value); }
185                         remove { Events.RemoveHandler(DrawItemEvent, value); }
186                 }
187
188                 public event DrawListViewSubItemEventHandler DrawSubItem {
189                         add { Events.AddHandler(DrawSubItemEvent, value); }
190                         remove { Events.RemoveHandler(DrawSubItemEvent, value); }
191                 }
192 #endif
193
194                 public event EventHandler ItemActivate {
195                         add { Events.AddHandler (ItemActivateEvent, value); }
196                         remove { Events.RemoveHandler (ItemActivateEvent, value); }
197                 }
198
199                 public event ItemCheckEventHandler ItemCheck {
200                         add { Events.AddHandler (ItemCheckEvent, value); }
201                         remove { Events.RemoveHandler (ItemCheckEvent, value); }
202                 }
203
204 #if NET_2_0
205                 public event ItemCheckedEventHandler ItemChecked {
206                         add { Events.AddHandler (ItemCheckedEvent, value); }
207                         remove { Events.RemoveHandler (ItemCheckedEvent, value); }
208                 }
209 #endif
210
211                 public event ItemDragEventHandler ItemDrag {
212                         add { Events.AddHandler (ItemDragEvent, value); }
213                         remove { Events.RemoveHandler (ItemDragEvent, value); }
214                 }
215
216 #if NET_2_0
217                 public event ListViewItemMouseHoverEventHandler ItemMouseHover {
218                         add { Events.AddHandler (ItemMouseHoverEvent, value); }
219                         remove { Events.RemoveHandler (ItemMouseHoverEvent, value); }
220                 }
221
222                 public event ListViewItemSelectionChangedEventHandler ItemSelectionChanged {
223                         add { Events.AddHandler (ItemSelectionChangedEvent, value); }
224                         remove { Events.RemoveHandler (ItemSelectionChangedEvent, value); }
225                 }
226
227                 [Browsable (false)]
228                 [EditorBrowsable (EditorBrowsableState.Never)]
229                 public new event EventHandler PaddingChanged {
230                         add { base.PaddingChanged += value; }
231                         remove { base.PaddingChanged -= value; }
232                 }
233 #endif
234
235                 [Browsable (false)]
236                 [EditorBrowsable (EditorBrowsableState.Never)]
237                 public new event PaintEventHandler Paint {
238                         add { base.Paint += value; }
239                         remove { base.Paint -= value; }
240                 }
241
242                 public event EventHandler SelectedIndexChanged {
243                         add { Events.AddHandler (SelectedIndexChangedEvent, value); }
244                         remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
245                 }
246
247                 [Browsable (false)]
248                 [EditorBrowsable (EditorBrowsableState.Never)]
249                 public new event EventHandler TextChanged {
250                         add { base.TextChanged += value; }
251                         remove { base.TextChanged -= value; }
252                 }
253
254 #if NET_2_0
255                 public event CacheVirtualItemsEventHandler CacheVirtualItems {
256                         add { Events.AddHandler (CacheVirtualItemsEvent, value); }
257                         remove { Events.RemoveHandler (CacheVirtualItemsEvent, value); }
258                 }
259
260                 public event RetrieveVirtualItemEventHandler RetrieveVirtualItem {
261                         add { Events.AddHandler (RetrieveVirtualItemEvent, value); }
262                         remove { Events.RemoveHandler (RetrieveVirtualItemEvent, value); }
263                 }
264
265                 public event EventHandler RightToLeftLayoutChanged {
266                         add { Events.AddHandler (RightToLeftLayoutChangedEvent, value); }
267                         remove { Events.RemoveHandler (RightToLeftLayoutChangedEvent, value); }
268                 }
269
270                 public event SearchForVirtualItemEventHandler SearchForVirtualItem {
271                         add { Events.AddHandler (SearchForVirtualItemEvent, value); }
272                         remove { Events.AddHandler (SearchForVirtualItemEvent, value); }
273                 }
274                 
275                 public event ListViewVirtualItemsSelectionRangeChangedEventHandler VirtualItemsSelectionRangeChanged {
276                         add { Events.AddHandler (VirtualItemsSelectionRangeChangedEvent, value); }
277                         remove { Events.RemoveHandler (VirtualItemsSelectionRangeChangedEvent, value); }
278                 }
279 #endif
280
281                 #endregion // Events
282
283                 #region Public Constructors
284                 public ListView ()
285                 {
286                         background_color = ThemeEngine.Current.ColorWindow;
287 #if NET_2_0
288                         groups = new ListViewGroupCollection (this);
289 #endif
290                         items = new ListViewItemCollection (this);
291                         checked_indices = new CheckedIndexCollection (this);
292                         checked_items = new CheckedListViewItemCollection (this);
293                         columns = new ColumnHeaderCollection (this);
294                         foreground_color = SystemColors.WindowText;
295                         selected_indices = new SelectedIndexCollection (this);
296                         selected_items = new SelectedListViewItemCollection (this);
297                         items_location = new Point [16];
298                         items_matrix_location = new ItemMatrixLocation [16];
299                         reordered_items_indices = new int [16];
300 #if NET_2_0
301                         item_tooltip = new ToolTip ();
302                         item_tooltip.Active = false;
303                         insertion_mark = new ListViewInsertionMark (this);
304 #endif
305
306                         InternalBorderStyle = BorderStyle.Fixed3D;
307
308                         header_control = new HeaderControl (this);
309                         header_control.Visible = false;
310                         Controls.AddImplicit (header_control);
311
312                         item_control = new ItemControl (this);
313                         Controls.AddImplicit (item_control);
314
315                         h_scroll = new ImplicitHScrollBar ();
316                         Controls.AddImplicit (this.h_scroll);
317
318                         v_scroll = new ImplicitVScrollBar ();
319                         Controls.AddImplicit (this.v_scroll);
320
321                         h_marker = v_marker = 0;
322                         keysearch_tickcnt = 0;
323
324                         // scroll bars are disabled initially
325                         h_scroll.Visible = false;
326                         h_scroll.ValueChanged += new EventHandler(HorizontalScroller);
327                         v_scroll.Visible = false;
328                         v_scroll.ValueChanged += new EventHandler(VerticalScroller);
329
330                         // event handlers
331                         base.KeyDown += new KeyEventHandler(ListView_KeyDown);
332                         SizeChanged += new EventHandler (ListView_SizeChanged);
333                         GotFocus += new EventHandler (FocusChanged);
334                         LostFocus += new EventHandler (FocusChanged);
335                         MouseWheel += new MouseEventHandler(ListView_MouseWheel);
336                         MouseEnter += new EventHandler (ListView_MouseEnter);
337
338 #if NET_2_0
339                         BackgroundImageTiled = false;
340 #endif
341
342                         this.SetStyle (ControlStyles.UserPaint | ControlStyles.StandardClick
343 #if NET_2_0
344                                 | ControlStyles.UseTextForAccessibility
345 #endif
346                                 , false);
347                 }
348                 #endregion      // Public Constructors
349
350                 #region Private Internal Properties
351                 internal Size CheckBoxSize {
352                         get {
353                                 if (this.check_boxes) {
354                                         if (this.state_image_list != null)
355                                                 return this.state_image_list.ImageSize;
356                                         else
357                                                 return ThemeEngine.Current.ListViewCheckBoxSize;
358                                 }
359                                 return Size.Empty;
360                         }
361                 }
362
363                 internal Size ItemSize {
364                         get {
365                                 if (view != View.Details)
366                                         return item_size;
367
368                                 Size size = new Size ();
369                                 size.Height = item_size.Height;
370                                 for (int i = 0; i < columns.Count; i++)
371                                         size.Width += columns [i].Wd;
372
373                                 return size;
374                         }
375                         set {
376                                 item_size = value;
377                         }
378                 }
379
380                 internal int HotItemIndex {
381                         get {
382                                 return hot_item_index;
383                         }
384                         set {
385                                 hot_item_index = value;
386                         }
387                 }
388
389                 internal override bool ScaleChildrenInternal {
390                         get { return false; }
391                 }
392
393                 internal bool UseCustomColumnWidth {
394                         get {
395                                 return (view == View.List || view == View.SmallIcon) && columns.Count > 0;
396                         }
397                 }
398
399                 #endregion      // Private Internal Properties
400
401                 #region  Protected Properties
402                 protected override CreateParams CreateParams {
403                         get { return base.CreateParams; }
404                 }
405
406                 protected override Size DefaultSize {
407                         get { return ThemeEngine.Current.ListViewDefaultSize; }
408                 }
409 #if NET_2_0
410                 protected override bool DoubleBuffered {
411                         get {
412                                 return base.DoubleBuffered;
413                         }
414                         set {
415                                 base.DoubleBuffered = value;
416                         }
417                 }
418 #endif
419                 #endregion      // Protected Properties
420
421                 #region Public Instance Properties
422                 [DefaultValue (ItemActivation.Standard)]
423                 public ItemActivation Activation {
424                         get { return activation; }
425                         set { 
426                                 if (value != ItemActivation.Standard && value != ItemActivation.OneClick && 
427                                         value != ItemActivation.TwoClick) {
428                                         throw new InvalidEnumArgumentException (string.Format
429                                                 ("Enum argument value '{0}' is not valid for Activation", value));
430                                 }
431 #if NET_2_0
432                                 if (hot_tracking && value != ItemActivation.OneClick)
433                                         throw new ArgumentException ("When HotTracking is on, activation must be ItemActivation.OneClick");
434 #endif
435                                 
436                                 activation = value;
437                         }
438                 }
439
440                 [DefaultValue (ListViewAlignment.Top)]
441                 [Localizable (true)]
442                 public ListViewAlignment Alignment {
443                         get { return alignment; }
444                         set {
445                                 if (value != ListViewAlignment.Default && value != ListViewAlignment.Left && 
446                                         value != ListViewAlignment.SnapToGrid && value != ListViewAlignment.Top) {
447                                         throw new InvalidEnumArgumentException (string.Format 
448                                                 ("Enum argument value '{0}' is not valid for Alignment", value));
449                                 }
450                                 
451                                 if (this.alignment != value) {
452                                         alignment = value;
453                                         // alignment does not matter in Details/List views
454                                         if (this.view == View.LargeIcon || this.View == View.SmallIcon)
455                                                 this.Redraw (true);
456                                 }
457                         }
458                 }
459
460                 [DefaultValue (false)]
461                 public bool AllowColumnReorder {
462                         get { return allow_column_reorder; }
463                         set { allow_column_reorder = value; }
464                 }
465
466                 [DefaultValue (true)]
467                 public bool AutoArrange {
468                         get { return auto_arrange; }
469                         set {
470                                 if (auto_arrange != value) {
471                                         auto_arrange = value;
472                                         // autoarrange does not matter in Details/List views
473                                         if (this.view == View.LargeIcon || this.View == View.SmallIcon)
474                                                 this.Redraw (true);
475                                 }
476                         }
477                 }
478
479                 public override Color BackColor {
480                         get {
481                                 if (background_color.IsEmpty)
482                                         return ThemeEngine.Current.ColorWindow;
483                                 else
484                                         return background_color;
485                         }
486                         set { 
487                                 background_color = value;
488                                 item_control.BackColor = value;
489                         }
490                 }
491
492 #if !NET_2_0
493                 [Browsable (false)]
494                 [EditorBrowsable (EditorBrowsableState.Never)]
495                 public override Image BackgroundImage {
496                         get { return base.BackgroundImage; }
497                         set { base.BackgroundImage = value; }
498                 }
499 #endif
500
501 #if NET_2_0
502                 [Browsable (false)]
503                 [EditorBrowsable (EditorBrowsableState.Never)]
504                 public override ImageLayout BackgroundImageLayout {
505                         get {
506                                 return base.BackgroundImageLayout;
507                         }
508                         set {
509                                 base.BackgroundImageLayout = value;
510                         }
511                 }
512
513                 [DefaultValue (false)]
514                 public bool BackgroundImageTiled {
515                         get {
516                                 return item_control.BackgroundImageLayout == ImageLayout.Tile;
517                         }
518                         set {
519                                 ImageLayout new_image_layout = value ? ImageLayout.Tile : ImageLayout.None;
520                                 if (new_image_layout == item_control.BackgroundImageLayout)
521                                         return;
522
523                                 item_control.BackgroundImageLayout = new_image_layout;
524                         }
525                 }
526 #endif
527
528                 [DefaultValue (BorderStyle.Fixed3D)]
529                 [DispId (-504)]
530                 public BorderStyle BorderStyle {
531                         get { return InternalBorderStyle; }
532                         set { InternalBorderStyle = value; }
533                 }
534
535                 [DefaultValue (false)]
536                 public bool CheckBoxes {
537                         get { return check_boxes; }
538                         set {
539                                 if (check_boxes != value) {
540 #if NET_2_0
541                                         if (value && View == View.Tile)
542                                                 throw new NotSupportedException ("CheckBoxes are not"
543                                                         + " supported in Tile view. Choose a different"
544                                                         + " view or set CheckBoxes to false.");
545 #endif
546
547                                         check_boxes = value;
548                                         this.Redraw (true);
549                                 }
550                         }
551                 }
552
553                 [Browsable (false)]
554                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
555                 public CheckedIndexCollection CheckedIndices {
556                         get { return checked_indices; }
557                 }
558
559                 [Browsable (false)]
560                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
561                 public CheckedListViewItemCollection CheckedItems {
562                         get { return checked_items; }
563                 }
564
565 #if NET_2_0
566                 [Editor ("System.Windows.Forms.Design.ColumnHeaderCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
567 #endif
568                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
569                 [Localizable (true)]
570                 [MergableProperty (false)]
571                 public ColumnHeaderCollection Columns {
572                         get { return columns; }
573                 }
574
575                 [Browsable (false)]
576                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
577                 public ListViewItem FocusedItem {
578                         get {
579                                 if (focused_item_index == -1)
580                                         return null;
581
582                                 return items [focused_item_index];
583                         }
584 #if NET_2_0
585                         set {
586                                 if (value == null || value.ListView != this || 
587                                                 !IsHandleCreated)
588                                         return;
589
590                                 SetFocusedItem (value.Index);
591                         }
592 #endif
593                 }
594
595                 public override Color ForeColor {
596                         get {
597                                 if (foreground_color.IsEmpty)
598                                         return ThemeEngine.Current.ColorWindowText;
599                                 else
600                                         return foreground_color;
601                         }
602                         set { foreground_color = value; }
603                 }
604
605                 [DefaultValue (false)]
606                 public bool FullRowSelect {
607                         get { return full_row_select; }
608                         set { 
609                                 if (full_row_select != value) {
610                                         full_row_select = value;
611                                         InvalidateSelection ();
612                                 }
613                         }
614                 }
615
616                 [DefaultValue (false)]
617                 public bool GridLines {
618                         get { return grid_lines; }
619                         set {
620                                 if (grid_lines != value) {
621                                         grid_lines = value;
622                                         this.Redraw (false);
623                                 }
624                         }
625                 }
626
627                 [DefaultValue (ColumnHeaderStyle.Clickable)]
628                 public ColumnHeaderStyle HeaderStyle {
629                         get { return header_style; }
630                         set {
631                                 if (header_style == value)
632                                         return;
633
634                                 switch (value) {
635                                 case ColumnHeaderStyle.Clickable:
636                                 case ColumnHeaderStyle.Nonclickable:
637                                 case ColumnHeaderStyle.None:
638                                         break;
639                                 default:
640                                         throw new InvalidEnumArgumentException (string.Format 
641                                                 ("Enum argument value '{0}' is not valid for ColumnHeaderStyle", value));
642                                 }
643                                 
644                                 header_style = value;
645                                 if (view == View.Details)
646                                         Redraw (true);
647                         }
648                 }
649
650                 [DefaultValue (true)]
651                 public bool HideSelection {
652                         get { return hide_selection; }
653                         set {
654                                 if (hide_selection != value) {
655                                         hide_selection = value;
656                                         InvalidateSelection ();
657                                 }
658                         }
659                 }
660
661 #if NET_2_0
662                 [DefaultValue (false)]
663                 public bool HotTracking {
664                         get {
665                                 return hot_tracking;
666                         }
667                         set {
668                                 if (hot_tracking == value)
669                                         return;
670                                 
671                                 hot_tracking = value;
672                                 if (hot_tracking) {
673                                         hover_selection = true;
674                                         activation = ItemActivation.OneClick;
675                                 }
676                         }
677                 }
678 #endif
679
680                 [DefaultValue (false)]
681                 public bool HoverSelection {
682                         get { return hover_selection; }
683                         set { 
684 #if NET_2_0
685                                 if (hot_tracking && value == false)
686                                         throw new ArgumentException ("When HotTracking is on, hover selection must be true");
687 #endif
688                                 hover_selection = value; 
689                         }
690                 }
691
692 #if NET_2_0
693                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
694                 [Browsable (false)]
695                 public ListViewInsertionMark InsertionMark {
696                         get {
697                                 return insertion_mark;
698                         }
699                 }
700 #endif
701
702 #if NET_2_0
703                 [Editor ("System.Windows.Forms.Design.ListViewItemCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
704 #endif
705                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
706                 [Localizable (true)]
707                 [MergableProperty (false)]
708                 public ListViewItemCollection Items {
709                         get { return items; }
710                 }
711
712                 [DefaultValue (false)]
713                 public bool LabelEdit {
714                         get { return label_edit; }
715                         set { label_edit = value; }
716                 }
717
718                 [DefaultValue (true)]
719                 [Localizable (true)]
720                 public bool LabelWrap {
721                         get { return label_wrap; }
722                         set {
723                                 if (label_wrap != value) {
724                                         label_wrap = value;
725                                         this.Redraw (true);
726                                 }
727                         }
728                 }
729
730                 [DefaultValue (null)]
731                 public ImageList LargeImageList {
732                         get { return large_image_list; }
733                         set {
734                                 large_image_list = value;
735                                 this.Redraw (true);
736                         }
737                 }
738
739                 [Browsable (false)]
740                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
741                 public IComparer ListViewItemSorter {
742                         get {
743                                 if (View != View.SmallIcon && View != View.LargeIcon && item_sorter is ItemComparer)
744                                         return null;
745                                 return item_sorter;
746                         }
747                         set {
748                                 if (item_sorter != value) {
749                                         item_sorter = value;
750                                         Sort ();
751                                 }
752                         }
753                 }
754
755                 [DefaultValue (true)]
756                 public bool MultiSelect {
757                         get { return multiselect; }
758                         set { multiselect = value; }
759                 }
760
761
762 #if NET_2_0
763                 [DefaultValue(false)]
764                 public bool OwnerDraw {
765                         get { return owner_draw; }
766                         set { 
767                                 owner_draw = value;
768                                 Redraw (true);
769                         }
770                 }
771
772                 [Browsable (false)]
773                 [EditorBrowsable (EditorBrowsableState.Never)]
774                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
775                 public new Padding Padding {
776                         get {
777                                 return base.Padding;
778                         }
779                         set {
780                                 base.Padding = value;
781                         }
782                 }
783                 
784                 [MonoTODO ("RTL not supported")]
785                 [Localizable (true)]
786                 [DefaultValue (false)]
787                 public virtual bool RightToLeftLayout {
788                         get { return right_to_left_layout; }
789                         set { 
790                                 if (right_to_left_layout != value) {
791                                         right_to_left_layout = value;
792                                         OnRightToLeftLayoutChanged (EventArgs.Empty);
793                                 }
794                         }
795                 }
796 #endif
797
798                 [DefaultValue (true)]
799                 public bool Scrollable {
800                         get { return scrollable; }
801                         set {
802                                 if (scrollable != value) {
803                                         scrollable = value;
804                                         this.Redraw (true);
805                                 }
806                         }
807                 }
808
809                 [Browsable (false)]
810                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
811                 public SelectedIndexCollection SelectedIndices {
812                         get { return selected_indices; }
813                 }
814
815                 [Browsable (false)]
816                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
817                 public SelectedListViewItemCollection SelectedItems {
818                         get { return selected_items; }
819                 }
820
821 #if NET_2_0
822                 [DefaultValue(true)]
823                 public bool ShowGroups {
824                         get { return show_groups; }
825                         set {
826                                 if (show_groups != value) {
827                                         show_groups = value;
828                                         Redraw(true);
829                                 }
830                         }
831                 }
832
833                 [LocalizableAttribute (true)]
834                 [MergableProperty (false)]
835                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
836                 [Editor ("System.Windows.Forms.Design.ListViewGroupCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
837                 public ListViewGroupCollection Groups {
838                         get { return groups; }
839                 }
840
841                 [DefaultValue (false)]
842                 public bool ShowItemToolTips {
843                         get {
844                                 return show_item_tooltips;
845                         }
846                         set {
847                                 show_item_tooltips = value;
848                                 item_tooltip.Active = false;
849                         }
850                 }
851 #endif
852
853                 [DefaultValue (null)]
854                 public ImageList SmallImageList {
855                         get { return small_image_list; }
856                         set {
857                                 small_image_list = value;
858                                 this.Redraw (true);
859                         }
860                 }
861
862                 [DefaultValue (SortOrder.None)]
863                 public SortOrder Sorting {
864                         get { return sort_order; }
865                         set { 
866                                 if (!Enum.IsDefined (typeof (SortOrder), value)) {
867                                         throw new InvalidEnumArgumentException ("value", (int) value,
868                                                 typeof (SortOrder));
869                                 }
870                                 
871                                 if (sort_order == value)
872                                         return;
873
874                                 sort_order = value;
875
876 #if NET_2_0
877                                 if (virtual_mode) // Sorting is not allowed in virtual mode
878                                         return;
879 #endif
880
881                                 if (value == SortOrder.None) {
882                                         if (item_sorter != null) {
883                                                 // ListViewItemSorter should never be reset for SmallIcon
884                                                 // and LargeIcon view
885                                                 if (View != View.SmallIcon && View != View.LargeIcon)
886 #if NET_2_0
887                                                         item_sorter = null;
888 #else
889                                                         // in .NET 1.1, only internal IComparer would be
890                                                         // set to null
891                                                         if (item_sorter is ItemComparer)
892                                                                 item_sorter = null;
893 #endif
894                                         }
895                                         this.Redraw (false);
896                                 } else {
897                                         if (item_sorter == null)
898                                                 item_sorter = new ItemComparer (value);
899                                         if (item_sorter is ItemComparer) {
900 #if NET_2_0
901                                                 item_sorter = new ItemComparer (value);
902 #else
903                                                 // in .NET 1.1, the sort order is not updated for
904                                                 // SmallIcon and LargeIcon views if no custom IComparer
905                                                 // is set
906                                                 if (View != View.SmallIcon && View != View.LargeIcon)
907                                                         item_sorter = new ItemComparer (value);
908 #endif
909                                         }
910                                         Sort ();
911                                 }
912                         }
913                 }
914
915                 private void OnImageListChanged (object sender, EventArgs args)
916                 {
917                         item_control.Invalidate ();
918                 }
919
920                 [DefaultValue (null)]
921                 public ImageList StateImageList {
922                         get { return state_image_list; }
923                         set {
924                                 if (state_image_list == value)
925                                         return;
926
927                                 if (state_image_list != null)
928                                         state_image_list.Images.Changed -= new EventHandler (OnImageListChanged);
929
930                                 state_image_list = value;
931
932                                 if (state_image_list != null)
933                                         state_image_list.Images.Changed += new EventHandler (OnImageListChanged);
934
935                                 this.Redraw (true);
936                         }
937                 }
938
939                 [Bindable (false)]
940                 [Browsable (false)]
941                 [EditorBrowsable (EditorBrowsableState.Never)]
942                 public override string Text {
943                         get { return base.Text; } 
944                         set {
945                                 if (value == base.Text)
946                                         return;
947
948                                 base.Text = value;
949                                 this.Redraw (true);
950                         }
951                 }
952
953 #if NET_2_0
954                 [Browsable (true)]
955                 public Size TileSize {
956                         get {
957                                 return tile_size;
958                         }
959                         set {
960                                 if (value.Width <= 0 || value.Height <= 0)
961                                         throw new ArgumentOutOfRangeException ("value");
962
963                                 tile_size = value;
964                                 Redraw (true);
965                         }
966                 }
967 #endif
968
969                 [Browsable (false)]
970                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
971                 public ListViewItem TopItem {
972                         get {
973 #if NET_2_0
974                                 if (view == View.LargeIcon || view == View.SmallIcon || view == View.Tile)
975                                         throw new InvalidOperationException ("Cannot get the top item in LargeIcon, SmallIcon or Tile view.");
976 #endif
977                                 // there is no item
978                                 if (this.items.Count == 0)
979                                         return null;
980                                 // if contents are not scrolled
981                                 // it is the first item
982                                 else if (h_marker == 0 && v_marker == 0)
983                                         return this.items [0];
984                                 // do a hit test for the scrolled position
985                                 else {
986                                         for (int i = 0; i < items.Count; i++) {
987                                                 Point item_loc = GetItemLocation (i);
988                                                 if (item_loc.X >= 0 && item_loc.Y >= 0)
989                                                         return items [i];
990                                         }
991                                         return null;
992                                 }
993                         }
994 #if NET_2_0
995                         set {
996                                 if (view == View.LargeIcon || view == View.SmallIcon || view == View.Tile)
997                                         throw new InvalidOperationException ("Cannot set the top item in LargeIcon, SmallIcon or Tile view.");
998
999                                 // .Net doesn't throw any exception in the cases below
1000                                 if (value == null || value.ListView != this)
1001                                         return;
1002
1003                                 EnsureVisible (value.Index);
1004                         }
1005 #endif
1006                 }
1007
1008 #if NET_2_0
1009                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1010                 [DefaultValue (true)]
1011                 [Browsable (false)]
1012                 [MonoInternalNote ("Stub, not implemented")]
1013                 public bool UseCompatibleStateImageBehavior {
1014                         get {
1015                                 return false;
1016                         }
1017                         set {
1018                         }
1019                 }
1020 #endif
1021
1022                 [DefaultValue (View.LargeIcon)]
1023                 public View View {
1024                         get { return view; }
1025                         set { 
1026                                 if (!Enum.IsDefined (typeof (View), value))
1027                                         throw new InvalidEnumArgumentException ("value", (int) value,
1028                                                 typeof (View));
1029
1030                                 if (view != value) {
1031 #if NET_2_0
1032                                         if (CheckBoxes && value == View.Tile)
1033                                                 throw new NotSupportedException ("CheckBoxes are not"
1034                                                         + " supported in Tile view. Choose a different"
1035                                                         + " view or set CheckBoxes to false.");
1036 #endif
1037
1038                                         h_scroll.Value = v_scroll.Value = 0;
1039                                         view = value; 
1040                                         Redraw (true);
1041                                 }
1042                         }
1043                 }
1044
1045 #if NET_2_0
1046                 [DefaultValue (false)]
1047                 [RefreshProperties (RefreshProperties.Repaint)]
1048                 public bool VirtualMode {
1049                         get {
1050                                 return virtual_mode;
1051                         }
1052                         set {
1053                                 if (virtual_mode == value)
1054                                         return;
1055
1056                                 if (!virtual_mode && items.Count > 0)
1057                                         throw new InvalidOperationException ();
1058
1059                                 virtual_mode = value;
1060                                 Redraw (true);
1061                         }
1062                 }
1063
1064                 [DefaultValue (0)]
1065                 [RefreshProperties (RefreshProperties.Repaint)]
1066                 public int VirtualListSize {
1067                         get {
1068                                 return virtual_list_size;
1069                         }
1070                         set {
1071                                 if (value < 0)
1072                                         throw new ArgumentException ("value");
1073
1074                                 if (virtual_list_size == value)
1075                                         return;
1076
1077                                 virtual_list_size = value;
1078                                 if (virtual_mode)
1079                                         Redraw (true);
1080                         }
1081                 }
1082 #endif
1083                 #endregion      // Public Instance Properties
1084
1085                 #region Internal Methods Properties
1086                 
1087                 internal int FirstVisibleIndex {
1088                         get {
1089                                 // there is no item
1090                                 if (this.items.Count == 0)
1091                                         return 0;
1092                                 
1093                                 if (h_marker == 0 && v_marker == 0)
1094                                         return 0;
1095                                 
1096                                 Size item_size = ItemSize;
1097                                 for (int i = 0; i < items.Count; i++) {
1098                                         Rectangle item_rect = new Rectangle (GetItemLocation (i), item_size);
1099                                         if (item_rect.Right >= 0 && item_rect.Bottom >= 0)
1100                                                 return i;
1101                                 }
1102
1103                                 return 0;
1104                         }
1105                 }
1106
1107                 
1108                 internal int LastVisibleIndex {
1109                         get {
1110                                 for (int i = FirstVisibleIndex; i < Items.Count; i++) {
1111                                         if (View == View.List || Alignment == ListViewAlignment.Left) {
1112                                                 if (GetItemLocation (i).X > item_control.ClientRectangle.Right)
1113                                                         return i - 1;
1114                                         } else {
1115                                                 if (GetItemLocation (i).Y > item_control.ClientRectangle.Bottom)
1116                                                         return i - 1;
1117                                         }
1118                                 }
1119                                 
1120                                 return Items.Count - 1;
1121                         }
1122                 }
1123
1124                 internal void OnSelectedIndexChanged ()
1125                 {
1126                         if (IsHandleCreated)
1127                                 OnSelectedIndexChanged (EventArgs.Empty);
1128                 }
1129
1130                 internal int TotalWidth {
1131                         get { return Math.Max (this.Width, this.layout_wd); }
1132                 }
1133
1134                 internal int TotalHeight {
1135                         get { return Math.Max (this.Height, this.layout_ht); }
1136                 }
1137
1138                 internal void Redraw (bool recalculate)
1139                 {
1140                         // Avoid calculations when control is being updated
1141                         if (updating)
1142                                 return;
1143 #if NET_2_0
1144                         // VirtualMode doesn't do any calculations until handle is created
1145                         if (virtual_mode && !IsHandleCreated)
1146                                 return;
1147 #endif
1148
1149
1150                         if (recalculate)
1151                                 CalculateListView (this.alignment);
1152
1153                         Refresh ();
1154                 }
1155
1156                 void InvalidateSelection ()
1157                 {
1158                         foreach (int selected_index in SelectedIndices)
1159                                 items [selected_index].Invalidate ();
1160                 }
1161
1162                 const int text_padding = 15;
1163
1164                 internal Size GetChildColumnSize (int index)
1165                 {
1166                         Size ret_size = Size.Empty;
1167                         ColumnHeader col = this.columns [index];
1168
1169                         if (col.Width == -2) { // autosize = max(items, columnheader)
1170                                 Size size = Size.Ceiling (TextRenderer.MeasureString
1171                                         (col.Text, this.Font));
1172                                 size.Width += text_padding;
1173                                 ret_size = BiggestItem (index);
1174                                 if (size.Width > ret_size.Width)
1175                                         ret_size = size;
1176                         }
1177                         else { // -1 and all the values < -2 are put under one category
1178                                 ret_size = BiggestItem (index);
1179                                 // fall back to empty columns' width if no subitem is available for a column
1180                                 if (ret_size.IsEmpty) {
1181                                         ret_size.Width = ThemeEngine.Current.ListViewEmptyColumnWidth;
1182                                         if (col.Text.Length > 0)
1183                                                 ret_size.Height = Size.Ceiling (TextRenderer.MeasureString
1184                                                                                 (col.Text, this.Font)).Height;
1185                                         else
1186                                                 ret_size.Height = this.Font.Height;
1187                                 }
1188                         }
1189
1190                         ret_size.Height += text_padding;
1191
1192                         // adjust the size for icon and checkbox for 0th column
1193                         if (index == 0) {
1194                                 ret_size.Width += (this.CheckBoxSize.Width + 4);
1195                                 if (this.small_image_list != null)
1196                                         ret_size.Width += this.small_image_list.ImageSize.Width;
1197                         }
1198                         return ret_size;
1199                 }
1200
1201                 // Returns the size of biggest item text in a column
1202                 // or the sum of the text and indent count if we are on 2.0
1203                 private Size BiggestItem (int col)
1204                 {
1205                         Size temp = Size.Empty;
1206                         Size ret_size = Size.Empty;
1207 #if NET_2_0
1208                         bool use_indent_count = small_image_list != null;
1209
1210                         // VirtualMode uses the first item text size
1211                         if (virtual_mode && items.Count > 0) {
1212                                 ListViewItem item = items [0];
1213                                 ret_size = Size.Ceiling (TextRenderer.MeasureString (item.SubItems[col].Text,
1214                                                         Font));
1215
1216                                 if (use_indent_count)
1217                                         ret_size.Width += item.IndentCount * small_image_list.ImageSize.Width;
1218                         } else {
1219 #endif
1220                                 // 0th column holds the item text, we check the size of
1221                                 // the various subitems falling in that column and get
1222                                 // the biggest one's size.
1223                                 foreach (ListViewItem item in items) {
1224                                         if (col >= item.SubItems.Count)
1225                                                 continue;
1226
1227                                         temp = Size.Ceiling (TextRenderer.MeasureString
1228                                                                 (item.SubItems [col].Text, Font));
1229
1230 #if NET_2_0
1231                                         if (use_indent_count)
1232                                                 temp.Width += item.IndentCount * small_image_list.ImageSize.Width;
1233 #endif
1234     
1235                                         if (temp.Width > ret_size.Width)
1236                                                 ret_size = temp;
1237                                 }
1238 #if NET_2_0
1239                         }
1240 #endif
1241
1242                         // adjustment for space in Details view
1243                         if (!ret_size.IsEmpty && view == View.Details)
1244                                 ret_size.Width += ThemeEngine.Current.ListViewItemPaddingWidth;
1245
1246                         return ret_size;
1247                 }
1248
1249                 const int max_wrap_padding = 30;
1250
1251                 // Sets the size of the biggest item text as per the view
1252                 private void CalcTextSize ()
1253                 {
1254                         // clear the old value
1255                         text_size = Size.Empty;
1256
1257                         if (items.Count == 0)
1258                                 return;
1259
1260                         text_size = BiggestItem (0);
1261
1262                         if (view == View.LargeIcon && this.label_wrap) {
1263                                 Size temp = Size.Empty;
1264                                 if (this.check_boxes)
1265                                         temp.Width += 2 * this.CheckBoxSize.Width;
1266                                 int icon_w = LargeImageList == null ? 12 : LargeImageList.ImageSize.Width;
1267                                 temp.Width += icon_w + max_wrap_padding;
1268                                 // wrapping is done for two lines only
1269                                 if (text_size.Width > temp.Width) {
1270                                         text_size.Width = temp.Width;
1271                                         text_size.Height *= 2;
1272                                 }
1273                         }
1274                         else if (view == View.List) {
1275                                 // in list view max text shown in determined by the
1276                                 // control width, even if scolling is enabled.
1277                                 int max_wd = this.Width - (this.CheckBoxSize.Width - 2);
1278                                 if (this.small_image_list != null)
1279                                         max_wd -= this.small_image_list.ImageSize.Width;
1280
1281                                 if (text_size.Width > max_wd)
1282                                         text_size.Width = max_wd;
1283                         }
1284
1285                         // we do the default settings, if we have got 0's
1286                         if (text_size.Height <= 0)
1287                                 text_size.Height = this.Font.Height;
1288                         if (text_size.Width <= 0)
1289                                 text_size.Width = this.Width;
1290
1291                         // little adjustment
1292                         text_size.Width += 2;
1293                         text_size.Height += 2;
1294                 }
1295
1296                 private void Scroll (ScrollBar scrollbar, int delta)
1297                 {
1298                         if (delta == 0 || !scrollbar.Visible)
1299                                 return;
1300
1301                         int max;
1302                         if (scrollbar == h_scroll)
1303                                 max = h_scroll.Maximum - item_control.Width;
1304                         else
1305                                 max = v_scroll.Maximum - item_control.Height;
1306
1307                         int val = scrollbar.Value + delta;
1308                         if (val > max)
1309                                 val = max;
1310                         else if (val < scrollbar.Minimum)
1311                                 val = scrollbar.Minimum;
1312                         scrollbar.Value = val;
1313                 }
1314
1315                 private void CalculateScrollBars ()
1316                 {
1317                         if (!IsHandleCreated)
1318                                 return;
1319
1320                         Rectangle client_area = ClientRectangle;
1321                         
1322                         if (!scrollable) {
1323                                 h_scroll.Visible = false;
1324                                 v_scroll.Visible = false;
1325                                 item_control.Height = client_area.Height;
1326                                 item_control.Width = client_area.Width;
1327                                 header_control.Width = client_area.Width;
1328                                 return;
1329                         }
1330
1331                         // Don't calculate if the view is not displayable
1332                         if (client_area.Height < 0 || client_area.Width < 0)
1333                                 return;
1334
1335                         // making a scroll bar visible might make
1336                         // other scroll bar visible
1337                         if (layout_wd > client_area.Right) {
1338                                 h_scroll.Visible = true;
1339                                 if ((layout_ht + h_scroll.Height) > client_area.Bottom)
1340                                         v_scroll.Visible = true;
1341                                 else
1342                                         v_scroll.Visible = false;
1343                         } else if (layout_ht > client_area.Bottom) {
1344                                 v_scroll.Visible = true;
1345                                 if ((layout_wd + v_scroll.Width) > client_area.Right)
1346                                         h_scroll.Visible = true;
1347                                 else
1348                                         h_scroll.Visible = false;
1349                         } else {
1350                                 h_scroll.Visible = false;
1351                                 v_scroll.Visible = false;
1352                         }
1353
1354                         item_control.Height = client_area.Height;
1355
1356                         if (h_scroll.is_visible) {
1357                                 h_scroll.Location = new Point (client_area.X, client_area.Bottom - h_scroll.Height);
1358                                 h_scroll.Minimum = 0;
1359
1360                                 // if v_scroll is visible, adjust the maximum of the
1361                                 // h_scroll to account for the width of v_scroll
1362                                 if (v_scroll.Visible) {
1363                                         h_scroll.Maximum = layout_wd + v_scroll.Width;
1364                                         h_scroll.Width = client_area.Width - v_scroll.Width;
1365                                 }
1366                                 else {
1367                                         h_scroll.Maximum = layout_wd;
1368                                         h_scroll.Width = client_area.Width;
1369                                 }
1370
1371                                 h_scroll.LargeChange = client_area.Width;
1372                                 h_scroll.SmallChange = Font.Height;
1373                                 item_control.Height -= h_scroll.Height;
1374                         }
1375
1376                         if (header_control.is_visible)
1377                                 header_control.Width = client_area.Width;
1378                         item_control.Width = client_area.Width;
1379
1380                         if (v_scroll.is_visible) {
1381                                 v_scroll.Location = new Point (client_area.Right - v_scroll.Width, client_area.Y);
1382                                 v_scroll.Minimum = 0;
1383
1384                                 // if h_scroll is visible, adjust the maximum of the
1385                                 // v_scroll to account for the height of h_scroll
1386                                 if (h_scroll.Visible) {
1387                                         v_scroll.Maximum = layout_ht + h_scroll.Height;
1388                                         v_scroll.Height = client_area.Height; // - h_scroll.Height already done 
1389                                 } else {
1390                                         v_scroll.Maximum = layout_ht;
1391                                         v_scroll.Height = client_area.Height;
1392                                 }
1393
1394                                 v_scroll.LargeChange = client_area.Height;
1395                                 v_scroll.SmallChange = Font.Height;
1396                                 if (header_control.Visible)
1397                                         header_control.Width -= v_scroll.Width;
1398                                 item_control.Width -= v_scroll.Width;
1399                         }
1400                 }
1401
1402 #if NET_2_0
1403                 internal int GetReorderedColumnIndex (ColumnHeader column)
1404                 {
1405                         if (reordered_column_indices == null)
1406                                 return column.Index;
1407
1408                         for (int i = 0; i < Columns.Count; i++)
1409                                 if (reordered_column_indices [i] == column.Index)
1410                                         return i;
1411
1412                         return -1;
1413                 }
1414 #endif
1415
1416                 internal ColumnHeader GetReorderedColumn (int index)
1417                 {
1418                         if (reordered_column_indices == null)
1419                                 return Columns [index];
1420                         else
1421                                 return Columns [reordered_column_indices [index]];
1422                 }
1423
1424                 internal void ReorderColumn (ColumnHeader col, int index, bool fireEvent)
1425                 {
1426 #if NET_2_0
1427                         if (fireEvent) {
1428                                 ColumnReorderedEventHandler eh = (ColumnReorderedEventHandler) (Events [ColumnReorderedEvent]);
1429                                 if (eh != null){
1430                                         ColumnReorderedEventArgs args = new ColumnReorderedEventArgs (col.Index, index, col);
1431
1432                                         eh (this, args);
1433                                         if (args.Cancel) {
1434                                                 header_control.Invalidate ();
1435                                                 item_control.Invalidate ();
1436                                                 return;
1437                                         }
1438                                 }
1439                         }
1440 #endif
1441                         int column_count = Columns.Count;
1442
1443                         if (reordered_column_indices == null) {
1444                                 reordered_column_indices = new int [column_count];
1445                                 for (int i = 0; i < column_count; i++)
1446                                         reordered_column_indices [i] = i;
1447                         }
1448
1449                         if (reordered_column_indices [index] == col.Index)
1450                                 return;
1451
1452                         int[] curr = reordered_column_indices;
1453                         int [] result = new int [column_count];
1454                         int curr_idx = 0;
1455                         for (int i = 0; i < column_count; i++) {
1456                                 if (curr_idx < column_count && curr [curr_idx] == col.Index)
1457                                         curr_idx++;
1458
1459                                 if (i == index)
1460                                         result [i] = col.Index;
1461                                 else
1462                                         result [i] = curr [curr_idx++];
1463                         }
1464
1465                         ReorderColumns (result, true);
1466                 }
1467
1468                 internal void ReorderColumns (int [] display_indices, bool redraw)
1469                 {
1470                         reordered_column_indices = display_indices;
1471                         for (int i = 0; i < Columns.Count; i++) {
1472                                 ColumnHeader col = Columns [i];
1473                                 col.InternalDisplayIndex = reordered_column_indices [i];
1474                         }
1475                         if (redraw && view == View.Details && IsHandleCreated) {
1476                                 LayoutDetails ();
1477                                 header_control.Invalidate ();
1478                                 item_control.Invalidate ();
1479                         }
1480                 }
1481
1482                 internal void AddColumn (ColumnHeader newCol, int index, bool redraw)
1483                 {
1484                         int column_count = Columns.Count;
1485                         newCol.SetListView (this);
1486
1487                         int [] display_indices = new int [column_count];
1488                         for (int i = 0; i < column_count; i++) {
1489                                 ColumnHeader col = Columns [i];
1490                                 if (i == index) {
1491                                         display_indices [i] = index;
1492                                 } else {
1493                                         int display_index = col.InternalDisplayIndex;
1494                                         if (display_index < index) {
1495                                                 display_indices [i] = display_index;
1496                                         } else {
1497                                                 display_indices [i] = (display_index + 1);
1498                                         }
1499                                 }
1500                         }
1501
1502                         ReorderColumns (display_indices, redraw);
1503                         Invalidate ();
1504                 }
1505
1506                 Size LargeIconItemSize
1507                 {
1508                         get {
1509                                 int image_w = LargeImageList == null ? 12 : LargeImageList.ImageSize.Width;
1510                                 int image_h = LargeImageList == null ? 2 : LargeImageList.ImageSize.Height;
1511                                 int h = text_size.Height + 2 + Math.Max (CheckBoxSize.Height, image_h);
1512                                 int w = Math.Max (text_size.Width, image_w);
1513
1514                                 if (check_boxes)
1515                                         w += 2 + CheckBoxSize.Width;
1516
1517                                 return new Size (w, h);
1518                         }
1519                 }
1520
1521                 Size SmallIconItemSize {
1522                         get {
1523                                 int image_w = SmallImageList == null ? 0 : SmallImageList.ImageSize.Width;
1524                                 int image_h = SmallImageList == null ? 0 : SmallImageList.ImageSize.Height;
1525                                 int h = Math.Max (text_size.Height, Math.Max (CheckBoxSize.Height, image_h));
1526                                 int w = text_size.Width + image_w;
1527
1528                                 if (check_boxes)
1529                                         w += 2 + CheckBoxSize.Width;
1530
1531                                 return new Size (w, h);
1532                         }
1533                 }
1534
1535 #if NET_2_0
1536                 Size TileItemSize {
1537                         get {
1538                                 // Calculate tile size if needed
1539                                 // It appears that using Font.Size instead of a SizeF value can give us
1540                                 // a slightly better approach to the proportions defined in .Net
1541                                 if (tile_size == Size.Empty) {
1542                                         int image_w = LargeImageList == null ? 0 : LargeImageList.ImageSize.Width;
1543                                         int image_h = LargeImageList == null ? 0 : LargeImageList.ImageSize.Height;
1544                                         int w = (int)Font.Size * ThemeEngine.Current.ListViewTileWidthFactor + image_w + 4;
1545                                         int h = Math.Max ((int)Font.Size * ThemeEngine.Current.ListViewTileHeightFactor, image_h);
1546                                 
1547                                         tile_size = new Size (w, h);
1548                                 }
1549                         
1550                                 return tile_size;
1551                         }
1552                 }
1553 #endif
1554
1555                 int GetDetailsItemHeight ()
1556                 {
1557                         int item_height;
1558                         int checkbox_height = CheckBoxes ? CheckBoxSize.Height : 0;
1559                         int small_image_height = SmallImageList == null ? 0 : SmallImageList.ImageSize.Height;
1560                         item_height = Math.Max (checkbox_height, text_size.Height);
1561                         item_height = Math.Max (item_height, small_image_height);
1562                         return item_height;
1563                 }
1564
1565                 void SetItemLocation (int index, int x, int y, int row, int col)
1566                 {
1567                         Point old_location = items_location [index];
1568                         if (old_location.X == x && old_location.Y == y)
1569                                 return;
1570
1571                         Size item_size = ItemSize;
1572                         Rectangle old_rect = new Rectangle (GetItemLocation (index), item_size);
1573
1574                         items_location [index] = new Point (x, y);
1575                         items_matrix_location [index] = new ItemMatrixLocation (row, col);
1576
1577                         //
1578                         // Initial position matches item's position in ListViewItemCollection
1579                         //
1580                         reordered_items_indices [index] = index;
1581
1582                         // Invalidate both previous and new bounds
1583                         item_control.Invalidate (old_rect);
1584                         item_control.Invalidate (new Rectangle (GetItemLocation (index), item_size));
1585                 }
1586
1587 #if NET_2_0
1588                 void ShiftItemsPositions (int from, int to, bool forward)
1589                 {
1590                         if (forward) {
1591                                 for (int i = to + 1; i > from; i--) {
1592                                         reordered_items_indices [i] = reordered_items_indices [i - 1];
1593
1594                                         ListViewItem item = items [reordered_items_indices [i]];
1595                                         item_control.Invalidate (item.Bounds);
1596                                         item.DisplayIndex = i;
1597                                         item_control.Invalidate (item.Bounds);
1598                                 }
1599                         } else {
1600                                 for (int i = from - 1; i < to; i++) {
1601                                         reordered_items_indices [i] = reordered_items_indices [i + 1];
1602
1603                                         ListViewItem item = items [reordered_items_indices [i]];
1604                                         item_control.Invalidate (item.Bounds);
1605                                         item.DisplayIndex = i;
1606                                         item_control.Invalidate (item.Bounds);
1607                                 }
1608                         }
1609                 }
1610
1611                 internal void ChangeItemLocation (int display_index, Point new_pos)
1612                 {
1613                         int new_display_index = GetDisplayIndexFromLocation (new_pos);
1614                         if (new_display_index == display_index)
1615                                 return;
1616
1617                         int item_index = reordered_items_indices [display_index];
1618                         ListViewItem item = items [item_index];
1619
1620                         bool forward = new_display_index < display_index;
1621                         int index_from, index_to;
1622                         if (forward) {
1623                                 index_from = new_display_index;
1624                                 index_to = display_index - 1;
1625                         } else {
1626                                 index_from = display_index + 1;
1627                                 index_to = new_display_index;
1628                         }
1629
1630                         ShiftItemsPositions (index_from, index_to, forward);
1631
1632                         reordered_items_indices [new_display_index] = item_index;
1633
1634                         item_control.Invalidate (item.Bounds);
1635                         item.DisplayIndex = new_display_index;
1636                         item_control.Invalidate (item.Bounds);
1637                 }
1638
1639                 int GetDisplayIndexFromLocation (Point loc)
1640                 {
1641                         int display_index = -1;
1642                         Rectangle item_area;
1643
1644                         // First item
1645                         if (loc.X < 0 || loc.Y < 0)
1646                                 return 0;
1647
1648                         // Adjustment to put in the next position refered by 'loc'
1649                         loc.X -= item_size.Width / 2;
1650                         if (loc.X < 0)
1651                                 loc.X = 0;
1652
1653                         for (int i = 0; i < items.Count; i++) {
1654                                 item_area = new Rectangle (GetItemLocation (i), item_size);
1655                                 item_area.Inflate (ThemeEngine.Current.ListViewHorizontalSpacing,
1656                                                 ThemeEngine.Current.ListViewVerticalSpacing);
1657
1658                                 if (item_area.Contains (loc)) {
1659                                         display_index = i;
1660                                         break;
1661                                 }
1662                         }
1663
1664                         // Put in in last position
1665                         if (display_index == -1)
1666                                 display_index = items.Count - 1;
1667
1668                         return display_index;
1669                 }
1670
1671                 // When using groups, the items with no group assigned
1672                 // belong to the DefaultGroup
1673                 int GetDefaultGroupItems ()
1674                 {
1675                         int count = 0;
1676                         foreach (ListViewItem item in items)
1677                                 if (item.Group == null)
1678                                         count++;
1679
1680                         return count;
1681                 }
1682 #endif
1683
1684                 int rows;
1685                 int cols;
1686                 int[,] item_index_matrix;
1687
1688                 void CalculateRowsAndCols (Size item_size, bool left_aligned, int x_spacing, int y_spacing)
1689                 {
1690                         Rectangle area = ClientRectangle;
1691
1692                         if (UseCustomColumnWidth)
1693                                 CalculateCustomColumnWidth ();
1694 #if NET_2_0
1695                         if (show_groups && groups.Count > 0 && view != View.List) {
1696                                 // When groups are used the alignment is always top-aligned
1697                                 rows = 0;
1698                                 cols = 0;
1699
1700                                 groups.DefaultGroup.ItemCount = GetDefaultGroupItems ();
1701                                 for (int i = 0; i < groups.InternalCount; i++) {
1702                                         ListViewGroup group = groups.GetInternalGroup (i);
1703                                         int items_in_group = group.ItemCount;
1704
1705                                         if (items_in_group == 0)
1706                                                 continue;
1707
1708                                         int group_cols = (int) Math.Floor ((double)(area.Width - v_scroll.Width + x_spacing) / (double)(item_size.Width + x_spacing));
1709                                         if (group_cols <= 0)
1710                                                 group_cols = 1;
1711                                         int group_rows = (int) Math.Ceiling ((double)items_in_group / (double)group_cols);
1712
1713                                         group.starting_row = rows;
1714                                         group.rows = group_rows;
1715
1716                                         cols = Math.Max (group_cols, cols);
1717                                         rows += group_rows;
1718                                 }
1719                         } else
1720 #endif
1721                         {
1722                                 // Simple matrix if no groups are used
1723                                 if (left_aligned) {
1724                                         rows = (int) Math.Floor ((double)(area.Height - h_scroll.Height + y_spacing) / (double)(item_size.Height + y_spacing));
1725                                         if (rows <= 0)
1726                                                 rows = 1;
1727                                         cols = (int) Math.Ceiling ((double)items.Count / (double)rows);
1728                                 } else {
1729                                         if (UseCustomColumnWidth)
1730                                                 cols = (int) Math.Floor ((double)(area.Width - v_scroll.Width) / (double)(custom_column_width));
1731                                         else
1732                                                 cols = (int) Math.Floor ((double)(area.Width - v_scroll.Width + x_spacing) / (double)(item_size.Width + x_spacing));
1733
1734                                         if (cols < 1)
1735                                                 cols = 1;
1736
1737                                         rows = (int) Math.Ceiling ((double)items.Count / (double)cols);
1738                                 }
1739                         }
1740
1741                         item_index_matrix = new int [rows, cols];
1742                 }
1743
1744                 // When using custom column width, we look for the minimum one
1745                 void CalculateCustomColumnWidth ()
1746                 {
1747                         int min_width = Int32.MaxValue;
1748                         for (int i = 0; i < columns.Count; i++) {
1749                                 int col_width = columns [i].Width;
1750
1751                                 if (col_width < min_width)
1752                                         min_width = col_width;
1753                         }
1754
1755                         custom_column_width = min_width;
1756                 }
1757
1758                 void LayoutIcons (Size item_size, bool left_aligned, int x_spacing, int y_spacing)
1759                 {
1760                         header_control.Visible = false;
1761                         header_control.Size = Size.Empty;
1762                         item_control.Visible = true;
1763                         item_control.Location = Point.Empty;
1764                         ItemSize = item_size; // Cache item size
1765
1766                         if (items.Count == 0)
1767                                 return;
1768
1769                         Size sz = item_size;
1770 #if NET_2_0
1771                         bool using_groups = show_groups && groups.Count > 0 && view != View.List;
1772 #endif
1773
1774                         CalculateRowsAndCols (sz, left_aligned, x_spacing, y_spacing);
1775
1776                         layout_wd = UseCustomColumnWidth ? cols * custom_column_width : cols * (sz.Width + x_spacing) - x_spacing;
1777                         layout_ht = rows * (sz.Height + y_spacing) - y_spacing;
1778 #if NET_2_0
1779                         if (using_groups)
1780                                 CalculateGroupsLayout (sz, y_spacing, 0);
1781 #endif
1782
1783                         int row = 0, col = 0;
1784                         int x = 0, y = 0;
1785
1786                         for (int i = 0; i < items.Count; i++) {
1787                                 ListViewItem item = items [i];
1788 #if NET_2_0
1789                                 if (using_groups) {
1790                                         ListViewGroup group = item.Group;
1791                                         if (group == null)
1792                                                 group = groups.DefaultGroup;
1793
1794                                         Point group_items_loc = group.items_area_location;
1795                                         int current_item = group.current_item++;
1796                                         int starting_row = group.starting_row;
1797
1798                                         row = (current_item / cols);
1799                                         col = current_item % cols;
1800
1801                                         x = UseCustomColumnWidth ? col * custom_column_width : col * (item_size.Width + x_spacing);
1802                                         y = row * (item_size.Height + y_spacing) + group_items_loc.Y;
1803
1804                                         SetItemLocation (i, x, y, row + starting_row, col);
1805                                         item_index_matrix [row + starting_row, col] = i;
1806                                 } else
1807 #endif
1808                                 {
1809                                         x = UseCustomColumnWidth ? col * custom_column_width : col * (item_size.Width + x_spacing);
1810                                         y = row * (item_size.Height + y_spacing);
1811
1812                                         SetItemLocation (i, x, y, row, col);
1813                                         item_index_matrix [row, col] = i;
1814
1815                                         if (left_aligned) {
1816                                                 row++;
1817                                                 if (row == rows) {
1818                                                         row = 0;
1819                                                         col++;
1820                                                 }
1821                                         } else {
1822                                                 if (++col == cols) {
1823                                                         col = 0;
1824                                                         row++;
1825                                                 }
1826                                         }
1827                                 }
1828 #if NET_2_0
1829                                 if (!virtual_mode) 
1830 #endif
1831                                 {
1832                                         item.Layout ();
1833                                         item.DisplayIndex = i;
1834                                         item.SetPosition (new Point (x, y));
1835                                 }
1836
1837
1838                         }
1839
1840                         item_control.Size = new Size (layout_wd, layout_ht);
1841                 }
1842
1843 #if NET_2_0
1844                 void CalculateGroupsLayout (Size item_size, int y_spacing, int y_origin)
1845                 {
1846                         int y = y_origin;
1847                         bool details = view == View.Details;
1848
1849                         for (int i = 0; i < groups.InternalCount; i++) {
1850                                 ListViewGroup group = groups.GetInternalGroup (i);
1851                                 if (group.ItemCount == 0)
1852                                         continue;
1853
1854                                 group.current_item = 0; // Reset layout
1855                                 y += LayoutGroupHeader (group, y, item_size.Height, y_spacing, details ? group.ItemCount : group.rows);
1856                         }
1857
1858                         layout_ht = y; // Update height taking into account Groups' headers heights
1859                 }
1860
1861                 int LayoutGroupHeader (ListViewGroup group, int y_origin, int item_height, int y_spacing, int rows)
1862                 {
1863                         Rectangle client_area = ClientRectangle;
1864                         int header_height = text_size.Height + 10;
1865
1866                         group.HeaderBounds = new Rectangle (0, y_origin, client_area.Width - v_scroll.Width, header_height);
1867                         group.items_area_location = new Point (0, y_origin + header_height);
1868
1869                         int items_area_height = ((item_height + y_spacing) * rows);
1870                         return header_height + items_area_height + 10; // Add a small bottom margin
1871                 }
1872 #endif
1873
1874                 void LayoutHeader ()
1875                 {
1876                         int x = 0;
1877                         for (int i = 0; i < Columns.Count; i++) {
1878                                 ColumnHeader col = GetReorderedColumn (i);
1879                                 col.X = x;
1880                                 col.Y = 0;
1881                                 col.CalcColumnHeader ();
1882                                 x += col.Wd;
1883                         }
1884
1885                         layout_wd = x;
1886
1887                         if (x < ClientRectangle.Width)
1888                                 x = ClientRectangle.Width;
1889
1890                         if (header_style == ColumnHeaderStyle.None) {
1891                                 header_control.Visible = false;
1892                                 header_control.Size = Size.Empty;
1893                                 layout_wd = ClientRectangle.Width;
1894                         } else {
1895                                 header_control.Width = x;
1896                                 header_control.Height = columns.Count > 0 ? columns [0].Ht : Font.Height + 5;
1897                                 header_control.Visible = true;
1898                         }
1899                 }
1900
1901                 void LayoutDetails ()
1902                 {
1903                         LayoutHeader ();
1904
1905                         if (columns.Count == 0) {
1906                                 item_control.Visible = false;
1907                                 layout_wd = ClientRectangle.Width;
1908                                 layout_ht = ClientRectangle.Height;
1909                                 return;
1910                         }
1911
1912                         item_control.Visible = true;
1913                         item_control.Location = Point.Empty;
1914                         item_control.Width = ClientRectangle.Width;
1915
1916                         int item_height = GetDetailsItemHeight ();
1917                         ItemSize = new Size (0, item_height); // We only cache Height for details view
1918                         int y = header_control.Height;
1919 #if NET_2_0
1920                         bool using_groups = show_groups && groups.Count > 0 && view != View.List;
1921                         if (using_groups) 
1922                                 CalculateGroupsLayout (ItemSize, 2, y);
1923 #endif
1924
1925                         for (int i = 0; i < items.Count; i++) {
1926                                 ListViewItem item = items [i];
1927 #if NET_2_0
1928                                 if (!virtual_mode) // Virtual mode sets Layout until draw time
1929 #endif
1930                                 {
1931                                         item.Layout ();
1932                                         item.DisplayIndex = i;
1933                                 }
1934
1935 #if NET_2_0
1936                                 if (using_groups) {
1937                                         ListViewGroup group = item.Group;
1938                                         if (group == null)
1939                                                 group = groups.DefaultGroup;
1940
1941                                         int current_item = group.current_item++;
1942                                         Point group_items_loc = group.items_area_location;
1943
1944                                         y = current_item * (item_height + 2) + group_items_loc.Y;
1945                                         SetItemLocation (i, 0, y, 0, 0);
1946                                         item.SetPosition (new Point (0, y));
1947                                 } else
1948 #endif
1949                                 {
1950                                         SetItemLocation (i, 0, y, 0, 0);
1951                                         item.SetPosition (new Point (0, y));
1952                                         y += item_height;
1953                                 }
1954                         }
1955
1956                         // some space for bottom gridline
1957                         if (items.Count > 0 && grid_lines)
1958                                 y += 2;
1959
1960 #if NET_2_0
1961                         if (!using_groups) // With groups it has been previously computed
1962 #endif
1963                                 layout_ht = y;
1964                 }
1965
1966                 private void AdjustItemsPositionArray (int count)
1967                 {
1968                         if (items_location.Length >= count)
1969                                 return;
1970
1971                         // items_location, items_matrix_location and reordered_items_indices must keep the same length
1972                         count = Math.Max (count, items_location.Length * 2);
1973                         items_location = new Point [count];
1974                         items_matrix_location = new ItemMatrixLocation [count];
1975                         reordered_items_indices = new int [count];
1976                 }
1977
1978                 private void CalculateListView (ListViewAlignment align)
1979                 {
1980                         CalcTextSize ();
1981
1982                         AdjustItemsPositionArray (items.Count);
1983
1984                         switch (view) {
1985                         case View.Details:
1986                                 LayoutDetails ();
1987                                 break;
1988
1989                         case View.SmallIcon:
1990                                 LayoutIcons (SmallIconItemSize, alignment == ListViewAlignment.Left, 
1991                                                 ThemeEngine.Current.ListViewHorizontalSpacing, 2);
1992                                 break;
1993
1994                         case View.LargeIcon:
1995                                 LayoutIcons (LargeIconItemSize, alignment == ListViewAlignment.Left,
1996                                         ThemeEngine.Current.ListViewHorizontalSpacing,
1997                                         ThemeEngine.Current.ListViewVerticalSpacing);
1998                                 break;
1999
2000                         case View.List:
2001                                 LayoutIcons (SmallIconItemSize, true, 
2002                                                 ThemeEngine.Current.ListViewHorizontalSpacing, 2);
2003                                 break;
2004 #if NET_2_0
2005                         case View.Tile:
2006                                 LayoutIcons (TileItemSize, alignment == ListViewAlignment.Left, 
2007                                                 ThemeEngine.Current.ListViewHorizontalSpacing,
2008                                                 ThemeEngine.Current.ListViewVerticalSpacing);
2009                                 break;
2010 #endif
2011                         }
2012
2013                         CalculateScrollBars ();
2014                 }
2015
2016                 internal Point GetItemLocation (int index)
2017                 {
2018                         Point loc = items_location [index];
2019                         loc.X -= h_marker; // Adjust to scroll
2020                         loc.Y -= v_marker;
2021
2022                         return loc;
2023                 }
2024
2025                 internal int GetItemIndex (int display_index)
2026                 {
2027                         return reordered_items_indices [display_index];
2028                 }
2029
2030                 private bool KeySearchString (KeyEventArgs ke)
2031                 {
2032                         int current_tickcnt = Environment.TickCount;
2033                         if (keysearch_tickcnt > 0 && current_tickcnt - keysearch_tickcnt > keysearch_keydelay) {
2034                                 keysearch_text = string.Empty;
2035                         }
2036                         
2037                         if (!Char.IsLetterOrDigit ((char)ke.KeyCode))
2038                                 return false;
2039
2040                         keysearch_text += (char)ke.KeyCode;
2041                         keysearch_tickcnt = current_tickcnt;
2042
2043                         int start = FocusedItem == null ? 0 : FocusedItem.Index;
2044                         int i = start;
2045                         while (true) {
2046                                 if (CultureInfo.CurrentCulture.CompareInfo.IsPrefix (Items[i].Text, keysearch_text,
2047                                         CompareOptions.IgnoreCase)) {
2048                                         SetFocusedItem (i);
2049                                         items [i].Selected = true;
2050                                         EnsureVisible (i);
2051                                         break;
2052                                 }
2053                                 i = (i + 1 < Items.Count) ? i+1 : 0;
2054
2055                                 if (i == start)
2056                                         break;
2057                         }
2058                         return true;
2059                 }
2060
2061                 int GetAdjustedIndex (Keys key)
2062                 {
2063                         int result = -1;
2064
2065                         if (View == View.Details) {
2066                                 switch (key) {
2067                                 case Keys.Up:
2068                                         result = FocusedItem.Index - 1;
2069                                         break;
2070                                 case Keys.Down:
2071                                         result = FocusedItem.Index + 1;
2072                                         if (result == items.Count)
2073                                                 result = -1;
2074                                         break;
2075                                 case Keys.PageDown:
2076                                         int last_index = LastVisibleIndex;
2077                                         Rectangle item_rect = new Rectangle (GetItemLocation (last_index), ItemSize);
2078                                         if (item_rect.Bottom > item_control.ClientRectangle.Bottom)
2079                                                 last_index--;
2080                                         if (FocusedItem.Index == last_index) {
2081                                                 if (FocusedItem.Index < Items.Count - 1) {
2082                                                         int page_size = item_control.Height / ItemSize.Height - 1;
2083                                                         result = FocusedItem.Index + page_size - 1;
2084                                                         if (result >= Items.Count)
2085                                                                 result = Items.Count - 1;
2086                                                 }
2087                                         } else
2088                                                 result = last_index;
2089                                         break;
2090                                 case Keys.PageUp:
2091                                         int first_index = FirstVisibleIndex;
2092                                         if (GetItemLocation (first_index).Y < 0)
2093                                                 first_index++;
2094                                         if (FocusedItem.Index == first_index) {
2095                                                 if (first_index > 0) {
2096                                                         int page_size = item_control.Height / ItemSize.Height - 1;
2097                                                         result = first_index - page_size + 1;
2098                                                         if (result < 0)
2099                                                                 result = 0;
2100                                                 }
2101                                         } else
2102                                                 result = first_index;
2103                                         break;
2104                                 }
2105                                 return result;
2106                         }
2107
2108                         ItemMatrixLocation item_matrix_location = items_matrix_location [FocusedItem.Index];
2109                         int row = item_matrix_location.Row;
2110                         int col = item_matrix_location.Col;
2111
2112                         switch (key) {
2113                         case Keys.Left:
2114                                 if (col == 0)
2115                                         return -1;
2116                                 return item_index_matrix [row, col - 1];
2117
2118                         case Keys.Right:
2119                                 if (col == (cols - 1))
2120                                         return -1;
2121                                 while (item_index_matrix [row, col + 1] == 0) {
2122                                         row--;
2123                                         if (row < 0)
2124                                                 return -1;
2125                                 }
2126                                 return item_index_matrix [row, col + 1];
2127
2128                         case Keys.Up:
2129                                 if (row == 0)
2130                                         return -1;
2131                                 while (item_index_matrix [row - 1, col] == 0 && row != 1) {
2132                                         col--;
2133                                         if (col < 0)
2134                                                 return -1;
2135                                 }
2136                                 return item_index_matrix [row - 1, col];
2137
2138                         case Keys.Down:
2139                                 if (row == (rows - 1) || row == Items.Count - 1)
2140                                         return -1;
2141                                 while (item_index_matrix [row + 1, col] == 0) {
2142                                         col--;
2143                                         if (col < 0)
2144                                                 return -1;
2145                                 }
2146                                 return item_index_matrix [row + 1, col];
2147
2148                         default:
2149                                 return -1;
2150                         }
2151                 }
2152
2153                 ListViewItem selection_start;
2154
2155                 private bool SelectItems (ArrayList sel_items)
2156                 {
2157                         bool changed = false;
2158                         foreach (ListViewItem item in SelectedItems)
2159                                 if (!sel_items.Contains (item)) {
2160                                         item.Selected = false;
2161                                         changed = true;
2162                                 }
2163                         foreach (ListViewItem item in sel_items)
2164                                 if (!item.Selected) {
2165                                         item.Selected = true;
2166                                         changed = true;
2167                                 }
2168                         return changed;
2169                 }
2170
2171                 private void UpdateMultiSelection (int index, bool reselect)
2172                 {
2173                         bool shift_pressed = (XplatUI.State.ModifierKeys & Keys.Shift) != 0;
2174                         bool ctrl_pressed = (XplatUI.State.ModifierKeys & Keys.Control) != 0;
2175                         ListViewItem item = items [index];
2176
2177                         if (shift_pressed && selection_start != null) {
2178                                 ArrayList list = new ArrayList ();
2179                                 int start_index = selection_start.Index;
2180                                 int start = Math.Min (start_index, index);
2181                                 int end = Math.Max (start_index, index);
2182                                 if (View == View.Details) {
2183                                         for (int i = start; i <= end; i++)
2184                                                 list.Add (items [i]);
2185                                 } else {
2186                                         ItemMatrixLocation start_item_matrix_location = items_matrix_location [start];
2187                                         ItemMatrixLocation end_item_matrix_location = items_matrix_location [end];
2188                                         int left = Math.Min (start_item_matrix_location.Col, end_item_matrix_location.Col);
2189                                         int right = Math.Max (start_item_matrix_location.Col, end_item_matrix_location.Col);
2190                                         int top = Math.Min (start_item_matrix_location.Row, end_item_matrix_location.Row);
2191                                         int bottom = Math.Max (start_item_matrix_location.Row, end_item_matrix_location.Row);
2192
2193                                         for (int i = 0; i < items.Count; i++) {
2194                                                 ItemMatrixLocation item_matrix_loc = items_matrix_location [i];
2195
2196                                                 if (item_matrix_loc.Row >= top && item_matrix_loc.Row <= bottom &&
2197                                                                 item_matrix_loc.Col >= left && item_matrix_loc.Col <= right)
2198                                                         list.Add (items [i]);
2199                                         }
2200                                 }
2201                                 SelectItems (list);
2202                         } else if (ctrl_pressed) {
2203                                 item.Selected = !item.Selected;
2204                                 selection_start = item;
2205                         } else {
2206                                 if (!reselect) {
2207                                         // do not unselect, and reselect the item
2208                                         foreach (int itemIndex in SelectedIndices) {
2209                                                 if (index == itemIndex)
2210                                                         continue;
2211                                                 items [itemIndex].Selected = false;
2212                                         }
2213                                 } else {
2214                                         SelectedItems.Clear ();
2215                                         item.Selected = true;
2216                                 }
2217                                 selection_start = item;
2218                         }
2219                 }
2220
2221                 internal override bool InternalPreProcessMessage (ref Message msg)
2222                 {
2223                         if (msg.Msg == (int)Msg.WM_KEYDOWN) {
2224                                 Keys key_data = (Keys)msg.WParam.ToInt32();
2225                                 
2226                                 HandleNavKeys (key_data);
2227                         } 
2228                         
2229                         return base.InternalPreProcessMessage (ref msg);
2230                 }
2231
2232                 bool HandleNavKeys (Keys key_data)
2233                 {
2234                         if (Items.Count == 0 || !item_control.Visible)
2235                                 return false;
2236
2237                         if (FocusedItem == null)
2238                                 SetFocusedItem (0);
2239
2240                         switch (key_data) {
2241                         case Keys.End:
2242                                 SelectIndex (Items.Count - 1);
2243                                 break;
2244
2245                         case Keys.Home:
2246                                 SelectIndex (0);
2247                                 break;
2248
2249                         case Keys.Left:
2250                         case Keys.Right:
2251                         case Keys.Up:
2252                         case Keys.Down:
2253                         case Keys.PageUp:
2254                         case Keys.PageDown:
2255                                 SelectIndex (GetAdjustedIndex (key_data));
2256                                 break;
2257
2258                         case Keys.Space:
2259                                 ToggleItemsCheckState ();
2260                                 break;
2261                         case Keys.Enter:
2262                                 if (selected_indices.Count > 0)
2263                                         OnItemActivate (EventArgs.Empty);
2264                                 break;
2265
2266                         default:
2267                                 return false;
2268                         }
2269
2270                         return true;
2271                 }
2272
2273                 void ToggleItemsCheckState ()
2274                 {
2275                         if (!CheckBoxes)
2276                                 return;
2277
2278                         // Don't modify check state if StateImageList has less than 2 elements
2279                         if (StateImageList != null && StateImageList.Images.Count < 2)
2280                                 return;
2281
2282                         if (SelectedIndices.Count > 0) {
2283                                 for (int i = 0; i < SelectedIndices.Count; i++) {
2284                                         ListViewItem item = Items [SelectedIndices [i]];
2285                                         item.Checked = !item.Checked;
2286                                 }
2287                                 return;
2288                         } 
2289                         
2290                         if (FocusedItem != null) {
2291                                 FocusedItem.Checked = !FocusedItem.Checked;
2292                                 SelectIndex (FocusedItem.Index);
2293                         }
2294                 }
2295
2296                 void SelectIndex (int index)
2297                 {
2298                         if (index == -1)
2299                                 return;
2300
2301                         if (MultiSelect)
2302                                 UpdateMultiSelection (index, true);
2303                         else if (!items [index].Selected)
2304                                 items [index].Selected = true;
2305
2306                         SetFocusedItem (index);
2307                         EnsureVisible (index);
2308                 }
2309
2310                 private void ListView_KeyDown (object sender, KeyEventArgs ke)
2311                 {
2312                         if (ke.Handled || Items.Count == 0 || !item_control.Visible)
2313                                 return;
2314
2315                         if (ke.Alt || ke.Control)
2316                                 return;
2317                                 
2318                         ke.Handled = KeySearchString (ke);
2319                 }
2320
2321                 private MouseEventArgs TranslateMouseEventArgs (MouseEventArgs args)
2322                 {
2323                         Point loc = PointToClient (Control.MousePosition);
2324                         return new MouseEventArgs (args.Button, args.Clicks, loc.X, loc.Y, args.Delta);
2325                 }
2326
2327                 internal class ItemControl : Control {
2328
2329                         ListView owner;
2330                         ListViewItem clicked_item;
2331                         ListViewItem last_clicked_item;
2332                         bool hover_processed = false;
2333                         bool checking = false;
2334                         ListViewItem prev_hovered_item;
2335 #if NET_2_0
2336                         ListViewItem prev_tooltip_item;
2337 #endif
2338                         int clicks;
2339                         Point drag_begin = new Point (-1, -1);
2340                         internal int dragged_item_index = -1;
2341                         
2342                         ListViewLabelEditTextBox edit_text_box;
2343                         internal ListViewItem edit_item;
2344                         LabelEditEventArgs edit_args;
2345
2346                         public ItemControl (ListView owner)
2347                         {
2348                                 this.owner = owner;
2349                                 DoubleClick += new EventHandler(ItemsDoubleClick);
2350                                 MouseDown += new MouseEventHandler(ItemsMouseDown);
2351                                 MouseMove += new MouseEventHandler(ItemsMouseMove);
2352                                 MouseHover += new EventHandler(ItemsMouseHover);
2353                                 MouseUp += new MouseEventHandler(ItemsMouseUp);
2354                         }
2355
2356                         void ItemsDoubleClick (object sender, EventArgs e)
2357                         {
2358                                 if (owner.activation == ItemActivation.Standard)
2359                                         owner.OnItemActivate (EventArgs.Empty);
2360                         }
2361
2362                         enum BoxSelect {
2363                                 None,
2364                                 Normal,
2365                                 Shift,
2366                                 Control
2367                         }
2368
2369                         BoxSelect box_select_mode = BoxSelect.None;
2370                         IList prev_selection;
2371                         Point box_select_start;
2372
2373                         Rectangle box_select_rect;
2374                         internal Rectangle BoxSelectRectangle {
2375                                 get { return box_select_rect; }
2376                                 set {
2377                                         if (box_select_rect == value)
2378                                                 return;
2379
2380                                         InvalidateBoxSelectRect ();
2381                                         box_select_rect = value;
2382                                         InvalidateBoxSelectRect ();
2383                                 }
2384                         }
2385
2386                         void InvalidateBoxSelectRect ()
2387                         {
2388                                 if (BoxSelectRectangle.Size.IsEmpty)
2389                                         return;
2390
2391                                 Rectangle edge = BoxSelectRectangle;
2392                                 edge.X -= 1;
2393                                 edge.Y -= 1;
2394                                 edge.Width += 2;
2395                                 edge.Height = 2;
2396                                 Invalidate (edge);
2397                                 edge.Y = BoxSelectRectangle.Bottom - 1;
2398                                 Invalidate (edge);
2399                                 edge.Y = BoxSelectRectangle.Y - 1;
2400                                 edge.Width = 2;
2401                                 edge.Height = BoxSelectRectangle.Height + 2;
2402                                 Invalidate (edge);
2403                                 edge.X = BoxSelectRectangle.Right - 1;
2404                                 Invalidate (edge);
2405                         }
2406
2407                         private Rectangle CalculateBoxSelectRectangle (Point pt)
2408                         {
2409                                 int left = Math.Min (box_select_start.X, pt.X);
2410                                 int right = Math.Max (box_select_start.X, pt.X);
2411                                 int top = Math.Min (box_select_start.Y, pt.Y);
2412                                 int bottom = Math.Max (box_select_start.Y, pt.Y);
2413                                 return Rectangle.FromLTRB (left, top, right, bottom);
2414                         }
2415
2416                         bool BoxIntersectsItem (int index)
2417                         {
2418                                 Rectangle r = new Rectangle (owner.GetItemLocation (index), owner.ItemSize);
2419                                 if (owner.View != View.Details) {
2420                                         r.X += r.Width / 4;
2421                                         r.Y += r.Height / 4;
2422                                         r.Width /= 2;
2423                                         r.Height /= 2;
2424                                 }
2425                                 return BoxSelectRectangle.IntersectsWith (r);
2426                         }
2427
2428                         bool BoxIntersectsText (int index)
2429                         {
2430                                 Rectangle r = owner.Items [index].TextBounds;
2431                                 return BoxSelectRectangle.IntersectsWith (r);
2432                         }
2433
2434                         ArrayList BoxSelectedItems {
2435                                 get {
2436                                         ArrayList result = new ArrayList ();
2437                                         for (int i = 0; i < owner.Items.Count; i++) {
2438                                                 bool intersects;
2439                                                 if (owner.View == View.Details && !owner.FullRowSelect)
2440                                                         intersects = BoxIntersectsText (i);
2441                                                 else
2442                                                         intersects = BoxIntersectsItem (i);
2443
2444                                                 if (intersects)
2445                                                         result.Add (owner.Items [i]);
2446                                         }
2447                                         return result;
2448                                 }
2449                         }
2450
2451                         private bool PerformBoxSelection (Point pt)
2452                         {
2453                                 if (box_select_mode == BoxSelect.None)
2454                                         return false;
2455
2456                                 BoxSelectRectangle = CalculateBoxSelectRectangle (pt);
2457                                 
2458                                 ArrayList box_items = BoxSelectedItems;
2459
2460                                 ArrayList items;
2461
2462                                 switch (box_select_mode) {
2463
2464                                 case BoxSelect.Normal:
2465                                         items = box_items;
2466                                         break;
2467
2468                                 case BoxSelect.Control:
2469                                         items = new ArrayList ();
2470                                         foreach (int index in prev_selection)
2471                                                 if (!box_items.Contains (owner.Items [index]))
2472                                                         items.Add (owner.Items [index]);
2473                                         foreach (ListViewItem item in box_items)
2474                                                 if (!prev_selection.Contains (item.Index))
2475                                                         items.Add (item);
2476                                         break;
2477
2478                                 case BoxSelect.Shift:
2479                                         items = box_items;
2480                                         foreach (ListViewItem item in box_items)
2481                                                 prev_selection.Remove (item.Index);
2482                                         foreach (int index in prev_selection)
2483                                                 items.Add (owner.Items [index]);
2484                                         break;
2485
2486                                 default:
2487                                         throw new Exception ("Unexpected Selection mode: " + box_select_mode);
2488                                 }
2489
2490                                 SuspendLayout ();
2491                                 owner.SelectItems (items);
2492                                 ResumeLayout ();
2493
2494                                 return true;
2495                         }
2496
2497                         private void ItemsMouseDown (object sender, MouseEventArgs me)
2498                         {
2499                                 owner.OnMouseDown (owner.TranslateMouseEventArgs (me));
2500                                 if (owner.items.Count == 0)
2501                                         return;
2502
2503                                 bool box_selecting = false;
2504                                 Size item_size = owner.ItemSize;
2505                                 Point pt = new Point (me.X, me.Y);
2506                                 for (int i = 0; i < owner.items.Count; i++) {
2507                                         Rectangle item_rect = new Rectangle (owner.GetItemLocation (i), item_size);
2508                                         if (!item_rect.Contains (pt))
2509                                                 continue;
2510
2511                                         // Actual item in 'i' position
2512                                         ListViewItem item = owner.items [owner.reordered_items_indices [i]];
2513
2514                                         if (item.CheckRectReal.Contains (pt)) {
2515                                                 // Don't modify check state if we have only one image
2516                                                 // and if we are in 1.1 profile only take into account
2517                                                 // double clicks
2518                                                 if (owner.StateImageList != null && owner.StateImageList.Images.Count < 2 
2519 #if !NET_2_0
2520                                                                 && me.Clicks == 1
2521 #endif
2522                                                                 )
2523                                                         return;
2524
2525                                                 // Generate an extra ItemCheck event when we got two clicks
2526                                                 // (Match weird .Net behaviour)
2527                                                 if (me.Clicks == 2)
2528                                                         item.Checked = !item.Checked;
2529
2530                                                 item.Checked = !item.Checked;
2531                                                 checking = true;
2532                                                 return;
2533                                         }
2534                                         
2535                                         if (owner.View == View.Details) {
2536                                                 bool over_text = item.TextBounds.Contains (pt);
2537                                                 if (owner.FullRowSelect) {
2538                                                         clicked_item = owner.items [i];
2539                                                         bool over_item_column = (me.X > owner.Columns[0].X && me.X < owner.Columns[0].X + owner.Columns[0].Width);
2540                                                         if (!over_text && over_item_column && owner.MultiSelect)
2541                                                                 box_selecting = true;
2542                                                 } else if (over_text)
2543                                                         clicked_item = item;
2544                                                 else
2545                                                         owner.SetFocusedItem (i);
2546                                         } else
2547                                                 clicked_item = item;
2548
2549                                         break;
2550                                 }
2551
2552
2553                                 if (clicked_item != null) {
2554                                         bool changed = !clicked_item.Selected;
2555                                         if (me.Button == MouseButtons.Left || (XplatUI.State.ModifierKeys == Keys.None && changed))
2556                                                 owner.SetFocusedItem (clicked_item.Index);
2557
2558                                         if (owner.MultiSelect) {
2559                                                 bool reselect = (!owner.LabelEdit || changed);
2560                                                 if (me.Button == MouseButtons.Left || (XplatUI.State.ModifierKeys == Keys.None && changed))
2561                                                         owner.UpdateMultiSelection (clicked_item.Index, reselect);
2562                                         } else {
2563                                                 clicked_item.Selected = true;
2564                                         }
2565
2566 #if NET_2_0
2567                                         if (owner.VirtualMode && changed) {
2568                                                 // Broken event - It's not fired from Item.Selected also
2569                                                 ListViewVirtualItemsSelectionRangeChangedEventArgs args = 
2570                                                         new ListViewVirtualItemsSelectionRangeChangedEventArgs (0, owner.items.Count - 1, false);
2571
2572                                                 owner.OnVirtualItemsSelectionRangeChanged (args);
2573                                         }
2574 #endif
2575                                         // Report clicks only if the item was clicked. On MS the
2576                                         // clicks are only raised if you click an item
2577                                         clicks = me.Clicks;
2578                                         if (me.Clicks > 1) {
2579                                                 if (owner.CheckBoxes)
2580                                                         clicked_item.Checked = !clicked_item.Checked;
2581                                         } else if (me.Clicks == 1) {
2582                                                 if (owner.LabelEdit && !changed)
2583                                                         BeginEdit (clicked_item); // this is probably not the correct place to execute BeginEdit
2584                                         }
2585                                 } else {
2586                                         if (owner.MultiSelect)
2587                                                 box_selecting = true;
2588                                         else if (owner.SelectedItems.Count > 0)
2589                                                 owner.SelectedItems.Clear ();
2590                                 }
2591
2592                                 if (box_selecting) {
2593                                         Keys mods = XplatUI.State.ModifierKeys;
2594                                         if ((mods & Keys.Shift) != 0)
2595                                                 box_select_mode = BoxSelect.Shift;
2596                                         else if ((mods & Keys.Control) != 0)
2597                                                 box_select_mode = BoxSelect.Control;
2598                                         else
2599                                                 box_select_mode = BoxSelect.Normal;
2600                                         box_select_start = pt; 
2601                                         prev_selection = owner.SelectedIndices.List.Clone () as IList;
2602                                 }
2603                         }
2604
2605                         private void ItemsMouseMove (object sender, MouseEventArgs me)
2606                         {
2607                                 bool done = PerformBoxSelection (new Point (me.X, me.Y));
2608
2609                                 owner.OnMouseMove (owner.TranslateMouseEventArgs (me));
2610
2611                                 if (done)
2612                                         return;
2613                                 if ((me.Button != MouseButtons.Left && me.Button != MouseButtons.Right) &&
2614                                         !hover_processed && owner.Activation != ItemActivation.OneClick
2615 #if NET_2_0
2616                                         && !owner.ShowItemToolTips
2617 #endif
2618                                                 )
2619                                         return;
2620
2621                                 Point pt = PointToClient (Control.MousePosition);
2622                                 ListViewItem item = owner.GetItemAt (pt.X, pt.Y);
2623
2624                                 if (hover_processed && item != null && item != prev_hovered_item) {
2625                                         hover_processed = false;
2626                                         XplatUI.ResetMouseHover (Handle);
2627                                 }
2628
2629                                 // Need to invalidate the item in HotTracking to show/hide the underline style
2630                                 if (owner.Activation == ItemActivation.OneClick) {
2631                                         if (item == null && owner.HotItemIndex != -1) {
2632 #if NET_2_0
2633                                                 if (owner.HotTracking)
2634                                                         Invalidate (owner.Items [owner.HotItemIndex].Bounds); // Previous one
2635 #endif
2636
2637                                                 Cursor = Cursors.Default;
2638                                                 owner.HotItemIndex = -1;
2639                                         } else if (item != null && owner.HotItemIndex == -1) {
2640 #if NET_2_0
2641                                                 if (owner.HotTracking)
2642                                                         Invalidate (item.Bounds);
2643 #endif
2644
2645                                                 Cursor = Cursors.Hand;
2646                                                 owner.HotItemIndex = item.Index;
2647                                         }
2648                                 }
2649
2650                                 if (me.Button == MouseButtons.Left || me.Button == MouseButtons.Right) {
2651                                         if (drag_begin.X == -1 && drag_begin.Y == -1) {
2652                                                 if (item != null) {
2653                                                         drag_begin = new Point (me.X, me.Y);
2654                                                         dragged_item_index = item.Index;
2655                                                 }
2656
2657                                         } else {
2658                                                 Rectangle r = new Rectangle (drag_begin, SystemInformation.DragSize);
2659                                                 if (!r.Contains (me.X, me.Y)) {
2660                                                         ListViewItem dragged_item  = owner.items [dragged_item_index];
2661                                                         owner.OnItemDrag (new ItemDragEventArgs (me.Button, dragged_item));
2662
2663                                                         drag_begin = new Point (-1, -1);
2664                                                         dragged_item_index = -1;
2665                                                 }
2666                                         }
2667                                 }
2668
2669 #if NET_2_0
2670                                 if (owner.ShowItemToolTips) {
2671                                         if (item == null) {
2672                                                 owner.item_tooltip.Active = false;
2673                                                 prev_tooltip_item = null;
2674                                         } else if (item != prev_tooltip_item && item.ToolTipText.Length > 0) {
2675                                                 owner.item_tooltip.Active = true;
2676                                                 owner.item_tooltip.SetToolTip (owner, item.ToolTipText);
2677                                                 prev_tooltip_item = item;
2678                                         }
2679                                 }
2680 #endif
2681
2682                         }
2683
2684                         private void ItemsMouseHover (object sender, EventArgs e)
2685                         {
2686                                 if (owner.hover_pending) {
2687                                         owner.OnMouseHover (e);
2688                                         owner.hover_pending = false;
2689                                 }
2690
2691                                 if (Capture)
2692                                         return;
2693
2694                                 hover_processed = true;
2695                                 Point pt = PointToClient (Control.MousePosition);
2696                                 ListViewItem item = owner.GetItemAt (pt.X, pt.Y);
2697                                 if (item == null)
2698                                         return;
2699
2700                                 prev_hovered_item = item;
2701
2702                                 if (owner.HoverSelection) {
2703                                         if (owner.MultiSelect)
2704                                                 owner.UpdateMultiSelection (item.Index, true);
2705                                         else
2706                                                 item.Selected = true;
2707                                         
2708                                         owner.SetFocusedItem (item.Index);
2709                                         Select (); // Make sure we have the focus, since MouseHover doesn't give it to us
2710                                 }
2711
2712 #if NET_2_0
2713                                 owner.OnItemMouseHover (new ListViewItemMouseHoverEventArgs (item));
2714 #endif
2715                         }
2716
2717                         void HandleClicks (MouseEventArgs me)
2718                         {
2719                                 // if the click is not on an item,
2720                                 // clicks remains as 0
2721                                 if (clicks > 1) {
2722 #if !NET_2_0
2723                                         owner.OnDoubleClick (EventArgs.Empty);
2724                                 } else if (clicks == 1) {
2725                                         owner.OnClick (EventArgs.Empty);
2726 #else
2727                                         owner.OnDoubleClick (EventArgs.Empty);
2728                                         owner.OnMouseDoubleClick (me);
2729                                 } else if (clicks == 1) {
2730                                         owner.OnClick (EventArgs.Empty);
2731                                         owner.OnMouseClick (me);
2732 #endif
2733                                 }
2734
2735                                 clicks = 0;
2736                         }
2737
2738                         private void ItemsMouseUp (object sender, MouseEventArgs me)
2739                         {
2740                                 MouseEventArgs owner_me = owner.TranslateMouseEventArgs (me);
2741                                 HandleClicks (owner_me);
2742
2743                                 Capture = false;
2744                                 if (owner.Items.Count == 0) {
2745                                         ResetMouseState ();
2746                                         owner.OnMouseUp (owner_me);
2747                                         return;
2748                                 }
2749
2750                                 Point pt = new Point (me.X, me.Y);
2751
2752                                 Rectangle rect = Rectangle.Empty;
2753                                 if (clicked_item != null) {
2754                                         if (owner.view == View.Details && !owner.full_row_select)
2755                                                 rect = clicked_item.GetBounds (ItemBoundsPortion.Label);
2756                                         else
2757                                                 rect = clicked_item.Bounds;
2758
2759                                         if (rect.Contains (pt)) {
2760                                                 switch (owner.activation) {
2761                                                 case ItemActivation.OneClick:
2762                                                         owner.OnItemActivate (EventArgs.Empty);
2763                                                         break;
2764
2765                                                 case ItemActivation.TwoClick:
2766                                                         if (last_clicked_item == clicked_item) {
2767                                                                 owner.OnItemActivate (EventArgs.Empty);
2768                                                                 last_clicked_item = null;
2769                                                         } else
2770                                                                 last_clicked_item = clicked_item;
2771                                                         break;
2772                                                 default:
2773                                                         // DoubleClick activation is handled in another handler
2774                                                         break;
2775                                                 }
2776                                         }
2777                                 } else if (!checking && owner.SelectedItems.Count > 0 && BoxSelectRectangle.Size.IsEmpty) {
2778                                         // Need this to clean up background clicks
2779                                         owner.SelectedItems.Clear ();
2780                                 }
2781
2782                                 ResetMouseState ();
2783                                 owner.OnMouseUp (owner_me);
2784                         }
2785
2786                         private void ResetMouseState ()
2787                         {                               
2788                                 clicked_item = null;
2789                                 box_select_start = Point.Empty;
2790                                 BoxSelectRectangle = Rectangle.Empty;
2791                                 prev_selection = null;
2792                                 box_select_mode = BoxSelect.None;
2793                                 checking = false;
2794
2795                                 // Clean these bits in case the mouse buttons were
2796                                 // released before firing ItemDrag
2797                                 dragged_item_index = -1;
2798                                 drag_begin = new Point (-1, -1);
2799                         }
2800                         
2801                         private void LabelEditFinished (object sender, EventArgs e)
2802                         {
2803                                 EndEdit (edit_item);
2804                         }
2805
2806                         private void LabelEditCancelled (object sender, EventArgs e)
2807                         {
2808                                 edit_args.SetLabel (null);
2809                                 EndEdit (edit_item);
2810                         }
2811
2812                         private void LabelTextChanged (object sender, EventArgs e)
2813                         {
2814                                 if (edit_args != null)
2815                                         edit_args.SetLabel (edit_text_box.Text);
2816                         }
2817
2818                         internal void BeginEdit (ListViewItem item)
2819                         {
2820                                 if (edit_item != null)
2821                                         EndEdit (edit_item);
2822                                 
2823                                 if (edit_text_box == null) {
2824                                         edit_text_box = new ListViewLabelEditTextBox ();
2825                                         edit_text_box.BorderStyle = BorderStyle.FixedSingle;
2826                                         edit_text_box.EditingCancelled += new EventHandler (LabelEditCancelled);
2827                                         edit_text_box.EditingFinished += new EventHandler (LabelEditFinished);
2828                                         edit_text_box.TextChanged += new EventHandler (LabelTextChanged);
2829                                         edit_text_box.Visible = false;
2830                                         Controls.Add (edit_text_box);
2831                                 }
2832                                 
2833                                 item.EnsureVisible();
2834                                 
2835                                 edit_text_box.Reset ();
2836                                 
2837                                 switch (owner.view) {
2838                                         case View.List:
2839                                         case View.SmallIcon:
2840                                         case View.Details:
2841                                                 edit_text_box.TextAlign = HorizontalAlignment.Left;
2842                                                 edit_text_box.Bounds = item.GetBounds (ItemBoundsPortion.Label);
2843                                                 SizeF sizef = TextRenderer.MeasureString (item.Text, item.Font);
2844                                                 edit_text_box.Width = (int)sizef.Width + 4;
2845                                                 edit_text_box.MaxWidth = owner.ClientRectangle.Width - edit_text_box.Bounds.X;
2846                                                 edit_text_box.WordWrap = false;
2847                                                 edit_text_box.Multiline = false;
2848                                                 break;
2849                                         case View.LargeIcon:
2850                                                 edit_text_box.TextAlign = HorizontalAlignment.Center;
2851                                                 edit_text_box.Bounds = item.GetBounds (ItemBoundsPortion.Label);
2852                                                 sizef = TextRenderer.MeasureString (item.Text, item.Font);
2853                                                 edit_text_box.Width = (int)sizef.Width + 4;
2854                                                 edit_text_box.MaxWidth = item.GetBounds(ItemBoundsPortion.Entire).Width;
2855                                                 edit_text_box.MaxHeight = owner.ClientRectangle.Height - edit_text_box.Bounds.Y;
2856                                                 edit_text_box.WordWrap = true;
2857                                                 edit_text_box.Multiline = true;
2858                                                 break;
2859                                 }
2860
2861                                 edit_item = item;
2862
2863                                 edit_text_box.Text = item.Text;
2864                                 edit_text_box.Font = item.Font;
2865                                 edit_text_box.Visible = true;
2866                                 edit_text_box.Focus ();
2867                                 edit_text_box.SelectAll ();
2868
2869                                 edit_args = new LabelEditEventArgs (owner.Items.IndexOf (edit_item));
2870                                 owner.OnBeforeLabelEdit (edit_args);
2871
2872                                 if (edit_args.CancelEdit)
2873                                         EndEdit (item);
2874                         }
2875
2876                         internal void CancelEdit (ListViewItem item)
2877                         {
2878                                 // do nothing if there's no item being edited, or if the
2879                                 // item being edited is not the one passed in
2880                                 if (edit_item == null || edit_item != item)
2881                                         return;
2882
2883                                 edit_args.SetLabel (null);
2884                                 EndEdit (item);
2885                         }
2886
2887                         internal void EndEdit (ListViewItem item)
2888                         {
2889                                 // do nothing if there's no item being edited, or if the
2890                                 // item being edited is not the one passed in
2891                                 if (edit_item == null || edit_item != item)
2892                                         return;
2893
2894                                 if (edit_text_box != null) {
2895                                         if (edit_text_box.Visible)
2896                                                 edit_text_box.Visible = false;
2897                                         // ensure listview gets focus
2898                                         owner.Focus ();
2899                                 }
2900
2901                                 // Same as TreeView.EndEdit: need to have focus in synch
2902                                 Application.DoEvents ();
2903
2904                                 // 
2905                                 // Create a new instance, since we could get a call to BeginEdit
2906                                 // from the handler and have fields out of synch
2907                                 //
2908                                 LabelEditEventArgs args = new LabelEditEventArgs (item.Index, edit_args.Label);
2909                                 edit_item = null;
2910
2911                                 owner.OnAfterLabelEdit (args);
2912                                 if (!args.CancelEdit && args.Label != null)
2913                                         item.Text = args.Label;
2914                         }
2915
2916                         internal override void OnPaintInternal (PaintEventArgs pe)
2917                         {
2918                                 ThemeEngine.Current.DrawListViewItems (pe.Graphics, pe.ClipRectangle, owner);
2919                         }
2920
2921                         protected override void WndProc (ref Message m)
2922                         {
2923                                 switch ((Msg)m.Msg) {
2924                                 case Msg.WM_KILLFOCUS:
2925                                         owner.Select (false, true);
2926                                         break;
2927                                 case Msg.WM_SETFOCUS:
2928                                         owner.Select (false, true);
2929                                         break;
2930                                 case Msg.WM_LBUTTONDOWN:
2931                                         if (!Focused)
2932                                                 owner.Select (false, true);
2933                                         break;
2934                                 case Msg.WM_RBUTTONDOWN:
2935                                         if (!Focused)
2936                                                 owner.Select (false, true);
2937                                         break;
2938                                 default:
2939                                         break;
2940                                 }
2941                                 base.WndProc (ref m);
2942                         }
2943                 }
2944                 
2945                 internal class ListViewLabelEditTextBox : TextBox
2946                 {
2947                         int max_width = -1;
2948                         int min_width = -1;
2949                         
2950                         int max_height = -1;
2951                         int min_height = -1;
2952                         
2953                         int old_number_lines = 1;
2954                         
2955                         SizeF text_size_one_char;
2956                         
2957                         public ListViewLabelEditTextBox ()
2958                         {
2959                                 min_height = DefaultSize.Height;
2960                                 text_size_one_char = TextRenderer.MeasureString ("B", Font);
2961                         }
2962                         
2963                         public int MaxWidth {
2964                                 set {
2965                                         if (value < min_width)
2966                                                 max_width = min_width;
2967                                         else
2968                                                 max_width = value;
2969                                 }
2970                         }
2971                         
2972                         public int MaxHeight {
2973                                 set {
2974                                         if (value < min_height)
2975                                                 max_height = min_height;
2976                                         else
2977                                                 max_height = value;
2978                                 }
2979                         }
2980                         
2981                         public new int Width {
2982                                 get {
2983                                         return base.Width;
2984                                 }
2985                                 set {
2986                                         min_width = value;
2987                                         base.Width = value;
2988                                 }
2989                         }
2990                         
2991                         public override Font Font {
2992                                 get {
2993                                         return base.Font;
2994                                 }
2995                                 set {
2996                                         base.Font = value;
2997                                         text_size_one_char = TextRenderer.MeasureString ("B", Font);
2998                                 }
2999                         }
3000                         
3001                         protected override void OnTextChanged (EventArgs e)
3002                         {
3003                                 SizeF text_size = TextRenderer.MeasureString (Text, Font);
3004                                 
3005                                 int new_width = (int)text_size.Width + 8;
3006                                 
3007                                 if (!Multiline)
3008                                         ResizeTextBoxWidth (new_width);
3009                                 else {
3010                                         if (Width != max_width)
3011                                                 ResizeTextBoxWidth (new_width);
3012                                         
3013                                         int number_lines = Lines.Length;
3014                                         
3015                                         if (number_lines != old_number_lines) {
3016                                                 int new_height = number_lines * (int)text_size_one_char.Height + 4;
3017                                                 old_number_lines = number_lines;
3018                                                 
3019                                                 ResizeTextBoxHeight (new_height);
3020                                         }
3021                                 }
3022                                 
3023                                 base.OnTextChanged (e);
3024                         }
3025                         
3026                         protected override bool IsInputKey (Keys key_data)
3027                         {
3028                                 if ((key_data & Keys.Alt) == 0) {
3029                                         switch (key_data & Keys.KeyCode) {
3030                                                 case Keys.Enter:
3031                                                         return true;
3032                                                 case Keys.Escape:
3033                                                         return true;
3034                                         }
3035                                 }
3036                                 return base.IsInputKey (key_data);
3037                         }
3038                         
3039                         protected override void OnKeyDown (KeyEventArgs e)
3040                         {
3041                                 if (!Visible)
3042                                         return;
3043
3044                                 switch (e.KeyCode) {
3045                                 case Keys.Return:
3046                                         Visible = false;
3047                                         e.Handled = true;
3048                                         OnEditingFinished (e);
3049                                         break;
3050                                 case Keys.Escape:
3051                                         Visible = false;
3052                                         e.Handled = true;
3053                                         OnEditingCancelled (e);
3054                                         break;
3055                                 }
3056                         }
3057                         
3058                         protected override void OnLostFocus (EventArgs e)
3059                         {
3060                                 if (Visible) {
3061                                         OnEditingFinished (e);
3062                                 }
3063                         }
3064
3065                         protected void OnEditingCancelled (EventArgs e)
3066                         {
3067                                 EventHandler eh = (EventHandler)(Events [EditingCancelledEvent]);
3068                                 if (eh != null)
3069                                         eh (this, e);
3070                         }
3071                         
3072                         protected void OnEditingFinished (EventArgs e)
3073                         {
3074                                 EventHandler eh = (EventHandler)(Events [EditingFinishedEvent]);
3075                                 if (eh != null)
3076                                         eh (this, e);
3077                         }
3078                         
3079                         private void ResizeTextBoxWidth (int new_width)
3080                         {
3081                                 if (new_width > max_width)
3082                                         base.Width = max_width;
3083                                 else 
3084                                 if (new_width >= min_width)
3085                                         base.Width = new_width;
3086                                 else
3087                                         base.Width = min_width;
3088                         }
3089                         
3090                         private void ResizeTextBoxHeight (int new_height)
3091                         {
3092                                 if (new_height > max_height)
3093                                         base.Height = max_height;
3094                                 else 
3095                                 if (new_height >= min_height)
3096                                         base.Height = new_height;
3097                                 else
3098                                         base.Height = min_height;
3099                         }
3100                         
3101                         public void Reset ()
3102                         {
3103                                 max_width = -1;
3104                                 min_width = -1;
3105                                 
3106                                 max_height = -1;
3107                                 
3108                                 old_number_lines = 1;
3109                                 
3110                                 Text = String.Empty;
3111                                 
3112                                 Size = DefaultSize;
3113                         }
3114
3115                         static object EditingCancelledEvent = new object ();
3116                         public event EventHandler EditingCancelled {
3117                                 add { Events.AddHandler (EditingCancelledEvent, value); }
3118                                 remove { Events.RemoveHandler (EditingCancelledEvent, value); }
3119                         }
3120
3121                         static object EditingFinishedEvent = new object ();
3122                         public event EventHandler EditingFinished {
3123                                 add { Events.AddHandler (EditingFinishedEvent, value); }
3124                                 remove { Events.RemoveHandler (EditingFinishedEvent, value); }
3125                         }
3126                 }
3127
3128                 internal override void OnPaintInternal (PaintEventArgs pe)
3129                 {
3130                         if (updating)
3131                                 return;
3132                                 
3133                         CalculateScrollBars ();
3134                 }
3135
3136                 void FocusChanged (object o, EventArgs args)
3137                 {
3138                         if (Items.Count == 0)
3139                                 return;
3140
3141                         if (FocusedItem == null)
3142                                 SetFocusedItem (0);
3143
3144                         ListViewItem focused_item = FocusedItem;
3145
3146                         if (focused_item.ListView != null) {
3147                                 item_control.Invalidate (focused_item.Bounds);
3148                                 focused_item.Layout ();
3149                                 item_control.Invalidate (focused_item.Bounds);
3150                         }
3151                 }
3152
3153                 private void ListView_MouseEnter (object sender, EventArgs args)
3154                 {
3155                         hover_pending = true; // Need a hover event for every Enter/Leave cycle
3156                 }
3157
3158                 private void ListView_MouseWheel (object sender, MouseEventArgs me)
3159                 {
3160                         if (Items.Count == 0)
3161                                 return;
3162
3163                         int lines = me.Delta / 120;
3164
3165                         if (lines == 0)
3166                                 return;
3167
3168                         switch (View) {
3169                         case View.Details:
3170                         case View.SmallIcon:
3171                                 Scroll (v_scroll, -ItemSize.Height * SystemInformation.MouseWheelScrollLines * lines);
3172                                 break;
3173                         case View.LargeIcon:
3174                                 Scroll (v_scroll, -(ItemSize.Height + ThemeEngine.Current.ListViewVerticalSpacing)  * lines);
3175                                 break;
3176                         case View.List:
3177                                 Scroll (h_scroll, -ItemSize.Width * lines);
3178                                 break;
3179 #if NET_2_0
3180                         case View.Tile:
3181                                 Scroll (v_scroll, -(ItemSize.Height + ThemeEngine.Current.ListViewVerticalSpacing) * 2 * lines);
3182                                 break;
3183 #endif
3184                         }
3185                 }
3186
3187                 private void ListView_SizeChanged (object sender, EventArgs e)
3188                 {
3189                         CalculateListView (alignment);
3190                 }
3191                 
3192                 private void SetFocusedItem (int index)
3193                 {
3194                         if (index != -1)
3195                                 items [index].Focused = true;
3196                         else if (focused_item_index != -1 && focused_item_index < items.Count) // Previous focused item
3197                                 items [focused_item_index].Focused = false;
3198
3199                         focused_item_index = index;
3200                 }
3201
3202                 private void HorizontalScroller (object sender, EventArgs e)
3203                 {
3204                         item_control.EndEdit (item_control.edit_item);
3205                         
3206                         // Avoid unnecessary flickering, when button is
3207                         // kept pressed at the end
3208                         if (h_marker != h_scroll.Value) {
3209                                 
3210                                 int pixels = h_marker - h_scroll.Value;
3211                                 
3212                                 h_marker = h_scroll.Value;
3213                                 if (header_control.Visible)
3214                                         XplatUI.ScrollWindow (header_control.Handle, pixels, 0, false);
3215
3216                                 XplatUI.ScrollWindow (item_control.Handle, pixels, 0, false);
3217                         }
3218                 }
3219
3220                 private void VerticalScroller (object sender, EventArgs e)
3221                 {
3222                         item_control.EndEdit (item_control.edit_item);
3223                         
3224                         // Avoid unnecessary flickering, when button is
3225                         // kept pressed at the end
3226                         if (v_marker != v_scroll.Value) {
3227                                 int pixels = v_marker - v_scroll.Value;
3228                                 Rectangle area = item_control.ClientRectangle;
3229                                 if (header_control.Visible) {
3230                                         area.Y += header_control.Height;
3231                                         area.Height -= header_control.Height;
3232                                 }
3233
3234                                 v_marker = v_scroll.Value;
3235                                 XplatUI.ScrollWindow (item_control.Handle, area, 0, pixels, false);
3236                         }
3237                 }
3238
3239                 internal override bool IsInputCharInternal (char charCode)
3240                 {
3241                         return true;
3242                 }
3243                 #endregion      // Internal Methods Properties
3244
3245                 #region Protected Methods
3246                 protected override void CreateHandle ()
3247                 {
3248                         base.CreateHandle ();
3249                         for (int i = 0; i < SelectedItems.Count; i++)
3250                                 OnSelectedIndexChanged (EventArgs.Empty);
3251                 }
3252
3253                 protected override void Dispose (bool disposing)
3254                 {
3255                         if (disposing) {
3256                                 h_scroll.Dispose ();
3257                                 v_scroll.Dispose ();
3258                                 
3259                                 large_image_list = null;
3260                                 small_image_list = null;
3261                                 state_image_list = null;
3262
3263                                 foreach (ColumnHeader col in columns)
3264                                         col.SetListView (null);
3265
3266 #if NET_2_0
3267                                 if (!virtual_mode) // In virtual mode we don't save the items
3268 #endif
3269                                         foreach (ListViewItem item in items)
3270                                                 item.Owner = null;
3271                         }
3272                         
3273                         base.Dispose (disposing);
3274                 }
3275
3276                 protected override bool IsInputKey (Keys keyData)
3277                 {
3278                         switch (keyData) {
3279                         case Keys.Up:
3280                         case Keys.Down:
3281                         case Keys.PageUp:
3282                         case Keys.PageDown:
3283                         case Keys.Right:
3284                         case Keys.Left:
3285                         case Keys.End:
3286                         case Keys.Home:
3287                                 return true;
3288
3289                         default:
3290                                 break;
3291                         }
3292                         
3293                         return base.IsInputKey (keyData);
3294                 }
3295
3296                 protected virtual void OnAfterLabelEdit (LabelEditEventArgs e)
3297                 {
3298                         LabelEditEventHandler eh = (LabelEditEventHandler)(Events [AfterLabelEditEvent]);
3299                         if (eh != null)
3300                                 eh (this, e);
3301                 }
3302
3303 #if NET_2_0
3304                 protected override void OnBackgroundImageChanged (EventArgs e)
3305                 {
3306                         item_control.BackgroundImage = BackgroundImage;
3307                         base.OnBackgroundImageChanged (e);
3308                 }
3309 #endif
3310
3311                 protected virtual void OnBeforeLabelEdit (LabelEditEventArgs e)
3312                 {
3313                         LabelEditEventHandler eh = (LabelEditEventHandler)(Events [BeforeLabelEditEvent]);
3314                         if (eh != null)
3315                                 eh (this, e);
3316                 }
3317
3318                 protected virtual void OnColumnClick (ColumnClickEventArgs e)
3319                 {
3320                         ColumnClickEventHandler eh = (ColumnClickEventHandler)(Events [ColumnClickEvent]);
3321                         if (eh != null)
3322                                 eh (this, e);
3323                 }
3324
3325 #if NET_2_0
3326                 protected internal virtual void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
3327                 {
3328                         DrawListViewColumnHeaderEventHandler eh = (DrawListViewColumnHeaderEventHandler)(Events[DrawColumnHeaderEvent]);
3329                         if (eh != null)
3330                                 eh(this, e);
3331                 }
3332
3333                 protected internal virtual void OnDrawItem(DrawListViewItemEventArgs e)
3334                 {
3335                         DrawListViewItemEventHandler eh = (DrawListViewItemEventHandler)(Events[DrawItemEvent]);
3336                         if (eh != null)
3337                                 eh(this, e);
3338                 }
3339
3340                 protected internal virtual void OnDrawSubItem(DrawListViewSubItemEventArgs e)
3341                 {
3342                         DrawListViewSubItemEventHandler eh = (DrawListViewSubItemEventHandler)(Events[DrawSubItemEvent]);
3343                         if (eh != null)
3344                                 eh(this, e);
3345                 }
3346
3347 #else
3348                 protected override void OnEnabledChanged (EventArgs e)
3349                 {
3350                         base.OnEnabledChanged (e);
3351                 }
3352 #endif
3353
3354                 protected override void OnFontChanged (EventArgs e)
3355                 {
3356                         base.OnFontChanged (e);
3357                         Redraw (true);
3358                 }
3359
3360                 protected override void OnHandleCreated (EventArgs e)
3361                 {
3362                         base.OnHandleCreated (e);
3363                         CalculateListView (alignment);
3364 #if NET_2_0
3365                         if (!virtual_mode) // Sorting is not allowed in virtual mode
3366 #endif
3367                                 Sort ();
3368                 }
3369
3370                 protected override void OnHandleDestroyed (EventArgs e)
3371                 {
3372                         base.OnHandleDestroyed (e);
3373                 }
3374
3375                 protected virtual void OnItemActivate (EventArgs e)
3376                 {
3377                         EventHandler eh = (EventHandler)(Events [ItemActivateEvent]);
3378                         if (eh != null)
3379                                 eh (this, e);
3380                 }
3381
3382                 protected internal virtual void OnItemCheck (ItemCheckEventArgs ice)
3383                 {
3384                         ItemCheckEventHandler eh = (ItemCheckEventHandler)(Events [ItemCheckEvent]);
3385                         if (eh != null)
3386                                 eh (this, ice);
3387                 }
3388
3389 #if NET_2_0
3390                 protected internal virtual void OnItemChecked (ItemCheckedEventArgs e)
3391                 {
3392                         ItemCheckedEventHandler eh = (ItemCheckedEventHandler)(Events [ItemCheckedEvent]);
3393                         if (eh != null)
3394                                 eh (this, e);
3395                 }
3396 #endif
3397
3398                 protected virtual void OnItemDrag (ItemDragEventArgs e)
3399                 {
3400                         ItemDragEventHandler eh = (ItemDragEventHandler)(Events [ItemDragEvent]);
3401                         if (eh != null)
3402                                 eh (this, e);
3403                 }
3404
3405 #if NET_2_0
3406                 protected virtual void OnItemMouseHover (ListViewItemMouseHoverEventArgs e)
3407                 {
3408                         ListViewItemMouseHoverEventHandler eh = (ListViewItemMouseHoverEventHandler)(Events [ItemMouseHoverEvent]);
3409                         if (eh != null)
3410                                 eh (this, e);
3411                 }
3412
3413                 protected internal virtual void OnItemSelectionChanged (ListViewItemSelectionChangedEventArgs e)
3414                 {
3415                         ListViewItemSelectionChangedEventHandler eh = 
3416                                 (ListViewItemSelectionChangedEventHandler) Events [ItemSelectionChangedEvent];
3417                         if (eh != null)
3418                                 eh (this, e);
3419                 }
3420
3421                 protected override void OnMouseHover (EventArgs e)
3422                 {
3423                         base.OnMouseHover (e);
3424                 }
3425
3426                 protected override void OnParentChanged (EventArgs e)
3427                 {
3428                         base.OnParentChanged (e);
3429                 }
3430 #endif
3431
3432                 protected virtual void OnSelectedIndexChanged (EventArgs e)
3433                 {
3434                         EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
3435                         if (eh != null)
3436                                 eh (this, e);
3437                 }
3438
3439                 protected override void OnSystemColorsChanged (EventArgs e)
3440                 {
3441                         base.OnSystemColorsChanged (e);
3442                 }
3443
3444 #if NET_2_0
3445                 protected virtual void OnCacheVirtualItems (CacheVirtualItemsEventArgs e)
3446                 {
3447                         EventHandler eh = (EventHandler)Events [CacheVirtualItemsEvent];
3448                         if (eh != null)
3449                                 eh (this, e);
3450                 }
3451
3452                 protected virtual void OnRetrieveVirtualItem (RetrieveVirtualItemEventArgs e)
3453                 {
3454                         RetrieveVirtualItemEventHandler eh = (RetrieveVirtualItemEventHandler)Events [RetrieveVirtualItemEvent];
3455                         if (eh != null)
3456                                 eh (this, e);
3457                 }
3458
3459                 [EditorBrowsable (EditorBrowsableState.Advanced)]
3460                 protected virtual void OnRightToLeftLayoutChanged (EventArgs e)
3461                 {
3462                         EventHandler eh = (EventHandler)Events[RightToLeftLayoutChangedEvent];
3463                         if (eh != null)
3464                                 eh (this, e);
3465                 }
3466
3467                 protected virtual void OnSearchForVirtualItem (SearchForVirtualItemEventArgs e)
3468                 {
3469                         SearchForVirtualItemEventHandler eh = (SearchForVirtualItemEventHandler) Events [SearchForVirtualItemEvent];
3470                         if (eh != null)
3471                                 eh (this, e);
3472                 }
3473                 
3474                 protected virtual void OnVirtualItemsSelectionRangeChanged (ListViewVirtualItemsSelectionRangeChangedEventArgs e)
3475                 {
3476                         ListViewVirtualItemsSelectionRangeChangedEventHandler eh = 
3477                                 (ListViewVirtualItemsSelectionRangeChangedEventHandler) Events [VirtualItemsSelectionRangeChangedEvent];
3478                         if (eh != null)
3479                                 eh (this, e);
3480                 }
3481 #endif
3482
3483                 protected void RealizeProperties ()
3484                 {
3485                         // FIXME: TODO
3486                 }
3487
3488                 protected void UpdateExtendedStyles ()
3489                 {
3490                         // FIXME: TODO
3491                 }
3492
3493                 bool refocusing = false;
3494
3495                 protected override void WndProc (ref Message m)
3496                 {
3497                         switch ((Msg)m.Msg) {
3498                         case Msg.WM_KILLFOCUS:
3499                                 Control receiver = Control.FromHandle (m.WParam);
3500                                 if (receiver == item_control) {
3501                                         has_focus = false;
3502                                         refocusing = true;
3503                                         return;
3504                                 }
3505                                 break;
3506                         case Msg.WM_SETFOCUS:
3507                                 if (refocusing) {
3508                                         has_focus = true;
3509                                         refocusing = false;
3510                                         return;
3511                                 }
3512                                 break;
3513                         default:
3514                                 break;
3515                         }
3516                         base.WndProc (ref m);
3517                 }
3518                 #endregion // Protected Methods
3519
3520                 #region Public Instance Methods
3521                 public void ArrangeIcons ()
3522                 {
3523                         ArrangeIcons (this.alignment);
3524                 }
3525
3526                 public void ArrangeIcons (ListViewAlignment value)
3527                 {
3528                         // Icons are arranged only if view is set to LargeIcon or SmallIcon
3529                         if (view == View.LargeIcon || view == View.SmallIcon) {
3530                                 this.CalculateListView (value);
3531                                 // we have done the calculations already
3532                                 this.Redraw (false);
3533                         }
3534                 }
3535
3536 #if NET_2_0
3537                 public void AutoResizeColumn (int columnIndex, ColumnHeaderAutoResizeStyle headerAutoResize)
3538                 {
3539                         if (columnIndex < 0 || columnIndex >= columns.Count)
3540                                 throw new ArgumentOutOfRangeException ("columnIndex");
3541
3542                         columns [columnIndex].AutoResize (headerAutoResize);
3543                 }
3544
3545                 public void AutoResizeColumns (ColumnHeaderAutoResizeStyle headerAutoResize)
3546                 {
3547                         BeginUpdate ();
3548                         foreach (ColumnHeader col in columns) 
3549                                 col.AutoResize (headerAutoResize);
3550                         EndUpdate ();
3551                 }
3552 #endif
3553
3554                 public void BeginUpdate ()
3555                 {
3556                         // flag to avoid painting
3557                         updating = true;
3558                 }
3559
3560                 public void Clear ()
3561                 {
3562                         columns.Clear ();
3563                         items.Clear (); // Redraw (true) called here
3564                 }
3565
3566                 public void EndUpdate ()
3567                 {
3568                         // flag to avoid painting
3569                         updating = false;
3570
3571                         // probably, now we need a redraw with recalculations
3572                         this.Redraw (true);
3573                 }
3574
3575                 public void EnsureVisible (int index)
3576                 {
3577                         if (index < 0 || index >= items.Count || scrollable == false)
3578                                 return;
3579
3580                         Rectangle view_rect = item_control.ClientRectangle;
3581                         Rectangle bounds = new Rectangle (GetItemLocation (index), ItemSize);
3582
3583                         if (view_rect.Contains (bounds))
3584                                 return;
3585
3586                         if (View != View.Details) {
3587                                 if (bounds.Left < 0)
3588                                         h_scroll.Value += bounds.Left;
3589                                 else if (bounds.Right > view_rect.Right)
3590                                         h_scroll.Value += (bounds.Right - view_rect.Right);
3591                         }
3592
3593                         if (bounds.Top < 0)
3594                                 v_scroll.Value += bounds.Top;
3595                         else if (bounds.Bottom > view_rect.Bottom)
3596                                 v_scroll.Value += (bounds.Bottom - view_rect.Bottom);
3597                 }
3598
3599 #if NET_2_0
3600                 public ListViewItem FindItemWithText (string text)
3601                 {
3602                         if (items.Count == 0)
3603                                 return null;
3604
3605                         return FindItemWithText (text, true, 0, true);
3606                 }
3607
3608                 public ListViewItem FindItemWithText (string text, bool includeSubItemsInSearch, int startIndex)
3609                 {
3610                         return FindItemWithText (text, includeSubItemsInSearch, startIndex, true);
3611                 }
3612
3613                 public ListViewItem FindItemWithText (string text, bool includeSubItemsInSearch, int startIndex, bool isPrefixSearch)
3614                 {
3615                         if (startIndex < 0 || startIndex >= items.Count)
3616                                 throw new ArgumentOutOfRangeException ("startIndex");
3617
3618                         if (text == null)
3619                                 throw new ArgumentNullException ("text");
3620
3621                         if (virtual_mode) {
3622                                 SearchForVirtualItemEventArgs args = new SearchForVirtualItemEventArgs (true,
3623                                                 isPrefixSearch, includeSubItemsInSearch, text, Point.Empty, 
3624                                                 SearchDirectionHint.Down, startIndex);
3625
3626                                 OnSearchForVirtualItem (args);
3627                                 int idx = args.Index;
3628                                 if (idx >= 0 && idx < virtual_list_size)
3629                                         return items [idx];
3630
3631                                 return null;
3632                         }
3633
3634                         for (int i = startIndex; i < items.Count; i++) {
3635                                 ListViewItem lvi = items [i];
3636
3637                                 if ((isPrefixSearch && lvi.Text.StartsWith (text, true, CultureInfo.CurrentCulture)) // prefix search
3638                                                 || String.Compare (lvi.Text, text, true) == 0) // match
3639                                         return lvi;
3640                         }
3641
3642                         if (includeSubItemsInSearch) {
3643                                 for (int i = startIndex; i < items.Count; i++) {
3644                                         ListViewItem lvi = items [i];
3645                                         foreach (ListViewItem.ListViewSubItem sub_item in lvi.SubItems)
3646                                                 if ((isPrefixSearch && sub_item.Text.StartsWith (text, true, CultureInfo.CurrentCulture))
3647                                                                 || String.Compare (sub_item.Text, text, true) == 0)
3648                                                         return lvi;
3649                                 }
3650                         }
3651
3652                         return null;
3653                 }
3654
3655                 public ListViewItem FindNearestItem (SearchDirectionHint searchDirection, int x, int y)
3656                 {
3657                         return FindNearestItem (searchDirection, new Point (x, y));
3658                 }
3659
3660                 public ListViewItem FindNearestItem (SearchDirectionHint dir, Point point)
3661                 {
3662                         if (dir < SearchDirectionHint.Left || dir > SearchDirectionHint.Down)
3663                                 throw new ArgumentOutOfRangeException ("searchDirection");
3664
3665                         if (view != View.LargeIcon && view != View.SmallIcon)
3666                                 throw new InvalidOperationException ();
3667
3668                         if (virtual_mode) {
3669                                 SearchForVirtualItemEventArgs args = new SearchForVirtualItemEventArgs (false,
3670                                                 false, false, String.Empty, point, 
3671                                                 dir, 0);
3672
3673                                 OnSearchForVirtualItem (args);
3674                                 int idx = args.Index;
3675                                 if (idx >= 0 && idx < virtual_list_size)
3676                                         return items [idx];
3677
3678                                 return null;
3679                         }
3680
3681                         ListViewItem item = null;
3682                         int min_dist = Int32.MaxValue;
3683
3684                         //
3685                         // It looks like .Net does a previous adjustment
3686                         //
3687                         switch (dir) {
3688                                 case SearchDirectionHint.Up:
3689                                         point.Y -= item_size.Height;
3690                                         break;
3691                                 case SearchDirectionHint.Down:
3692                                         point.Y += item_size.Height;
3693                                         break;
3694                                 case SearchDirectionHint.Left:
3695                                         point.X -= item_size.Width;
3696                                         break;
3697                                 case SearchDirectionHint.Right:
3698                                         point.X += item_size.Width;
3699                                         break;
3700                         }
3701
3702                         for (int i = 0; i < items.Count; i++) {
3703                                 Point item_loc = GetItemLocation (i);
3704
3705                                 if (dir == SearchDirectionHint.Up) {
3706                                         if (point.Y < item_loc.Y)
3707                                                 continue;
3708                                 } else if (dir == SearchDirectionHint.Down) {
3709                                         if (point.Y > item_loc.Y)
3710                                                 continue;
3711                                 } else if (dir == SearchDirectionHint.Left) {
3712                                         if (point.X < item_loc.X)
3713                                                 continue;
3714                                 } else if (dir == SearchDirectionHint.Right) {
3715                                         if (point.X > item_loc.X)
3716                                                 continue;
3717                                 }
3718
3719                                 int x_dist = point.X - item_loc.X;
3720                                 int y_dist = point.Y - item_loc.Y;
3721
3722                                 int dist = x_dist * x_dist  + y_dist * y_dist;
3723                                 if (dist < min_dist) {
3724                                         item = items [i];
3725                                         min_dist = dist;
3726                                 }
3727                         }
3728
3729                         return item;
3730                 }
3731 #endif
3732                 
3733                 public ListViewItem GetItemAt (int x, int y)
3734                 {
3735                         Size item_size = ItemSize;
3736                         for (int i = 0; i < items.Count; i++) {
3737                                 Point item_location = GetItemLocation (i);
3738                                 Rectangle item_rect = new Rectangle (item_location, item_size);
3739                                 if (item_rect.Contains (x, y))
3740                                         return items [i];
3741                         }
3742
3743                         return null;
3744                 }
3745
3746                 public Rectangle GetItemRect (int index)
3747                 {
3748                         return GetItemRect (index, ItemBoundsPortion.Entire);
3749                 }
3750
3751                 public Rectangle GetItemRect (int index, ItemBoundsPortion portion)
3752                 {
3753                         if (index < 0 || index >= items.Count)
3754                                 throw new IndexOutOfRangeException ("index");
3755
3756                         return items [index].GetBounds (portion);
3757                 }
3758
3759 #if NET_2_0
3760                 public ListViewHitTestInfo HitTest (Point point)
3761                 {
3762                         return HitTest (point.X, point.Y);
3763                 }
3764
3765                 public ListViewHitTestInfo HitTest (int x, int y)
3766                 {
3767                         if (x < 0)
3768                                 throw new ArgumentOutOfRangeException ("x");
3769                         if (y < 0)
3770                                 throw new ArgumentOutOfRangeException ("y");
3771
3772                         ListViewItem item = GetItemAt (x, y);
3773                         if (item == null)
3774                                 return new ListViewHitTestInfo (null, null, ListViewHitTestLocations.None);
3775
3776                         ListViewHitTestLocations locations = 0;
3777                         if (item.GetBounds (ItemBoundsPortion.Label).Contains (x, y))
3778                                 locations |= ListViewHitTestLocations.Label;
3779                         else if (item.GetBounds (ItemBoundsPortion.Icon).Contains (x, y))
3780                                 locations |= ListViewHitTestLocations.Image;
3781                         else if (item.CheckRectReal.Contains (x, y))
3782                                 locations |= ListViewHitTestLocations.StateImage;
3783
3784                         ListViewItem.ListViewSubItem subitem = null;
3785                         if (view == View.Details)
3786                                 foreach (ListViewItem.ListViewSubItem si in item.SubItems)
3787                                         if (si.Bounds.Contains (x, y)) {
3788                                                 subitem = si;
3789                                                 break;
3790                                         }
3791
3792                         return new ListViewHitTestInfo (item, subitem, locations);
3793                 }
3794
3795                 [EditorBrowsable (EditorBrowsableState.Advanced)]
3796                 public void RedrawItems (int startIndex, int endIndex, bool invalidateOnly)
3797                 {
3798                         if (startIndex < 0 || startIndex >= items.Count)
3799                                 throw new ArgumentOutOfRangeException ("startIndex");
3800                         if (endIndex < 0 || endIndex >= items.Count)
3801                                 throw new ArgumentOutOfRangeException ("endIndex");
3802                         if (startIndex > endIndex)
3803                                 throw new ArgumentException ("startIndex");
3804
3805                         if (updating)
3806                                 return;
3807
3808                         for (int i = startIndex; i <= endIndex; i++)
3809                                 item_control.Invalidate (items [i].Bounds);
3810
3811                         if (!invalidateOnly)
3812                                 Update ();
3813                 }
3814 #endif
3815
3816                 public void Sort ()
3817                 {
3818 #if NET_2_0
3819                         if (virtual_mode)
3820                                 throw new InvalidOperationException ();
3821 #endif
3822
3823                         Sort (true);
3824                 }
3825
3826                 // we need this overload to reuse the logic for sorting, while allowing
3827                 // redrawing to be done by caller or have it done by this method when
3828                 // sorting is really performed
3829                 //
3830                 // ListViewItemCollection's Add and AddRange methods call this overload
3831                 // with redraw set to false, as they take care of redrawing themselves
3832                 // (they even want to redraw the listview if no sort is performed, as 
3833                 // an item was added), while ListView.Sort () only wants to redraw if 
3834                 // sorting was actually performed
3835                 private void Sort (bool redraw)
3836                 {
3837                         if (!IsHandleCreated || item_sorter == null) {
3838                                 return;
3839                         }
3840                         
3841                         items.Sort (item_sorter);
3842                         if (redraw)
3843                                 this.Redraw (true);
3844                 }
3845
3846                 public override string ToString ()
3847                 {
3848                         int count = this.Items.Count;
3849
3850                         if (count == 0)
3851                                 return string.Format ("System.Windows.Forms.ListView, Items.Count: 0");
3852                         else
3853                                 return string.Format ("System.Windows.Forms.ListView, Items.Count: {0}, Items[0]: {1}", count, this.Items [0].ToString ());
3854                 }
3855                 #endregion      // Public Instance Methods
3856
3857
3858                 #region Subclasses
3859
3860                 class HeaderControl : Control {
3861
3862                         ListView owner;
3863                         bool column_resize_active = false;
3864                         ColumnHeader resize_column;
3865                         ColumnHeader clicked_column;
3866                         ColumnHeader drag_column;
3867                         int drag_x;
3868                         int drag_to_index = -1;
3869
3870                         public HeaderControl (ListView owner)
3871                         {
3872                                 this.owner = owner;
3873                                 MouseDown += new MouseEventHandler (HeaderMouseDown);
3874                                 MouseMove += new MouseEventHandler (HeaderMouseMove);
3875                                 MouseUp += new MouseEventHandler (HeaderMouseUp);
3876                         }
3877
3878                         private ColumnHeader ColumnAtX (int x)
3879                         {
3880                                 Point pt = new Point (x, 0);
3881                                 ColumnHeader result = null;
3882                                 foreach (ColumnHeader col in owner.Columns) {
3883                                         if (col.Rect.Contains (pt)) {
3884                                                 result = col;
3885                                                 break;
3886                                         }
3887                                 }
3888                                 return result;
3889                         }
3890
3891                         private int GetReorderedIndex (ColumnHeader col)
3892                         {
3893                                 if (owner.reordered_column_indices == null)
3894                                         return col.Index;
3895                                 else
3896                                         for (int i = 0; i < owner.Columns.Count; i++)
3897                                                 if (owner.reordered_column_indices [i] == col.Index)
3898                                                         return i;
3899                                 throw new Exception ("Column index missing from reordered array");
3900                         }
3901
3902                         private void HeaderMouseDown (object sender, MouseEventArgs me)
3903                         {
3904                                 if (resize_column != null) {
3905                                         column_resize_active = true;
3906                                         Capture = true;
3907                                         return;
3908                                 }
3909
3910                                 clicked_column = ColumnAtX (me.X + owner.h_marker);
3911
3912                                 if (clicked_column != null) {
3913                                         Capture = true;
3914                                         if (owner.AllowColumnReorder) {
3915                                                 drag_x = me.X;
3916                                                 drag_column = (ColumnHeader) (clicked_column as ICloneable).Clone ();
3917                                                 drag_column.Rect = clicked_column.Rect;
3918                                                 drag_to_index = GetReorderedIndex (clicked_column);
3919                                         }
3920                                         clicked_column.Pressed = true;
3921                                         Rectangle bounds = clicked_column.Rect;
3922                                         bounds.X -= owner.h_marker;
3923                                         Invalidate (bounds);
3924                                         return;
3925                                 }
3926                         }
3927
3928                         void StopResize ()
3929                         {
3930                                 column_resize_active = false;
3931                                 resize_column = null;
3932                                 Capture = false;
3933                                 Cursor = Cursors.Default;
3934                         }
3935                         
3936                         private void HeaderMouseMove (object sender, MouseEventArgs me)
3937                         {
3938                                 Point pt = new Point (me.X + owner.h_marker, me.Y);
3939
3940                                 if (column_resize_active) {
3941                                         int width = pt.X - resize_column.X;
3942                                         if (width < 0)
3943                                                 width = 0;
3944
3945                                         if (!owner.CanProceedWithResize (resize_column, width)){
3946                                                 StopResize ();
3947                                                 return;
3948                                         }
3949                                         resize_column.Width = width;
3950                                         return;
3951                                 }
3952
3953                                 resize_column = null;
3954
3955                                 if (clicked_column != null) {
3956                                         if (owner.AllowColumnReorder) {
3957                                                 Rectangle r;
3958
3959                                                 r = drag_column.Rect;
3960                                                 r.X = clicked_column.Rect.X + me.X - drag_x;
3961                                                 drag_column.Rect = r;
3962
3963                                                 int x = me.X + owner.h_marker;
3964                                                 ColumnHeader over = ColumnAtX (x);
3965                                                 if (over == null)
3966                                                         drag_to_index = owner.Columns.Count;
3967                                                 else if (x < over.X + over.Width / 2)
3968                                                         drag_to_index = GetReorderedIndex (over);
3969                                                 else
3970                                                         drag_to_index = GetReorderedIndex (over) + 1;
3971                                                 Invalidate ();
3972                                         } else {
3973                                                 ColumnHeader over = ColumnAtX (me.X + owner.h_marker);
3974                                                 bool pressed = clicked_column.Pressed;
3975                                                 clicked_column.Pressed = over == clicked_column;
3976                                                 if (clicked_column.Pressed ^ pressed) {
3977                                                         Rectangle bounds = clicked_column.Rect;
3978                                                         bounds.X -= owner.h_marker;
3979                                                         Invalidate (bounds);
3980                                                 }
3981                                         }
3982                                         return;
3983                                 }
3984
3985                                 for (int i = 0; i < owner.Columns.Count; i++) {
3986                                         Rectangle zone = owner.Columns [i].Rect;
3987                                         zone.X = zone.Right - 5;
3988                                         zone.Width = 10;
3989                                         if (zone.Contains (pt)) {
3990                                                 if (i < owner.Columns.Count - 1 && owner.Columns [i + 1].Width == 0)
3991                                                         i++;
3992                                                 resize_column = owner.Columns [i];
3993                                                 break;
3994                                         }
3995                                 }
3996
3997                                 if (resize_column == null)
3998                                         Cursor = Cursors.Default;
3999                                 else
4000                                         Cursor = Cursors.VSplit;
4001                         }
4002
4003                         void HeaderMouseUp (object sender, MouseEventArgs me)
4004                         {
4005                                 Capture = false;
4006
4007                                 if (column_resize_active) {
4008                                         int column_idx = resize_column.Index;
4009                                         StopResize ();
4010                                         owner.RaiseColumnWidthChanged (column_idx);
4011                                         return;
4012                                 }
4013
4014                                 if (clicked_column != null && clicked_column.Pressed) {
4015                                         clicked_column.Pressed = false;
4016                                         Rectangle bounds = clicked_column.Rect;
4017                                         bounds.X -= owner.h_marker;
4018                                         Invalidate (bounds);
4019                                         owner.OnColumnClick (new ColumnClickEventArgs (clicked_column.Index));
4020                                 }
4021
4022                                 if (drag_column != null && owner.AllowColumnReorder) {
4023                                         drag_column = null;
4024                                         if (drag_to_index > GetReorderedIndex (clicked_column))
4025                                                 drag_to_index--;
4026                                         if (owner.GetReorderedColumn (drag_to_index) != clicked_column)
4027                                                 owner.ReorderColumn (clicked_column, drag_to_index, true);
4028                                         drag_to_index = -1;
4029                                         Invalidate ();
4030                                 }
4031
4032                                 clicked_column = null;
4033                         }
4034
4035                         internal override void OnPaintInternal (PaintEventArgs pe)
4036                         {
4037                                 if (owner.updating)
4038                                         return;
4039                                 
4040                                 Theme theme = ThemeEngine.Current;
4041                                 theme.DrawListViewHeader (pe.Graphics, pe.ClipRectangle, this.owner);
4042
4043                                 if (drag_column == null)
4044                                         return;
4045
4046                                 int target_x;
4047                                 if (drag_to_index == owner.Columns.Count)
4048                                         target_x = owner.GetReorderedColumn (drag_to_index - 1).Rect.Right - owner.h_marker;
4049                                 else
4050                                         target_x = owner.GetReorderedColumn (drag_to_index).Rect.X - owner.h_marker;
4051                                 theme.DrawListViewHeaderDragDetails (pe.Graphics, owner, drag_column, target_x);
4052                         }
4053
4054                         protected override void WndProc (ref Message m)
4055                         {
4056                                 switch ((Msg)m.Msg) {
4057                                 case Msg.WM_SETFOCUS:
4058                                         owner.Focus ();
4059                                         break;
4060                                 default:
4061                                         base.WndProc (ref m);
4062                                         break;
4063                                 }
4064                         }
4065                 }
4066
4067                 private class ItemComparer : IComparer {
4068                         readonly SortOrder sort_order;
4069
4070                         public ItemComparer (SortOrder sortOrder)
4071                         {
4072                                 sort_order = sortOrder;
4073                         }
4074
4075                         public int Compare (object x, object y)
4076                         {
4077                                 ListViewItem item_x = x as ListViewItem;
4078                                 ListViewItem item_y = y as ListViewItem;
4079                                 if (sort_order == SortOrder.Ascending)
4080                                         return String.Compare (item_x.Text, item_y.Text);
4081                                 else
4082                                         return String.Compare (item_y.Text, item_x.Text);
4083                         }
4084                 }
4085
4086 #if NET_2_0
4087                 [ListBindable (false)]
4088 #endif
4089                 public class CheckedIndexCollection : IList, ICollection, IEnumerable
4090                 {
4091                         private readonly ListView owner;
4092
4093                         #region Public Constructor
4094                         public CheckedIndexCollection (ListView owner)
4095                         {
4096                                 this.owner = owner;
4097                         }
4098                         #endregion      // Public Constructor
4099
4100                         #region Public Properties
4101                         [Browsable (false)]
4102                         public int Count {
4103                                 get { return owner.CheckedItems.Count; }
4104                         }
4105
4106                         public bool IsReadOnly {
4107                                 get { return true; }
4108                         }
4109
4110                         public int this [int index] {
4111                                 get {
4112                                         int [] indices = GetIndices ();
4113                                         if (index < 0 || index >= indices.Length)
4114                                                 throw new ArgumentOutOfRangeException ("index");
4115                                         return indices [index];
4116                                 }
4117                         }
4118
4119                         bool ICollection.IsSynchronized {
4120                                 get { return false; }
4121                         }
4122
4123                         object ICollection.SyncRoot {
4124                                 get { return this; }
4125                         }
4126
4127                         bool IList.IsFixedSize {
4128                                 get { return true; }
4129                         }
4130
4131                         object IList.this [int index] {
4132                                 get { return this [index]; }
4133                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
4134                         }
4135                         #endregion      // Public Properties
4136
4137                         #region Public Methods
4138                         public bool Contains (int checkedIndex)
4139                         {
4140                                 int [] indices = GetIndices ();
4141                                 for (int i = 0; i < indices.Length; i++) {
4142                                         if (indices [i] == checkedIndex)
4143                                                 return true;
4144                                 }
4145                                 return false;
4146                         }
4147
4148                         public IEnumerator GetEnumerator ()
4149                         {
4150                                 int [] indices = GetIndices ();
4151                                 return indices.GetEnumerator ();
4152                         }
4153
4154                         void ICollection.CopyTo (Array dest, int index)
4155                         {
4156                                 int [] indices = GetIndices ();
4157                                 Array.Copy (indices, 0, dest, index, indices.Length);
4158                         }
4159
4160                         int IList.Add (object value)
4161                         {
4162                                 throw new NotSupportedException ("Add operation is not supported.");
4163                         }
4164
4165                         void IList.Clear ()
4166                         {
4167                                 throw new NotSupportedException ("Clear operation is not supported.");
4168                         }
4169
4170                         bool IList.Contains (object checkedIndex)
4171                         {
4172                                 if (!(checkedIndex is int))
4173                                         return false;
4174                                 return Contains ((int) checkedIndex);
4175                         }
4176
4177                         int IList.IndexOf (object checkedIndex)
4178                         {
4179                                 if (!(checkedIndex is int))
4180                                         return -1;
4181                                 return IndexOf ((int) checkedIndex);
4182                         }
4183
4184                         void IList.Insert (int index, object value)
4185                         {
4186                                 throw new NotSupportedException ("Insert operation is not supported.");
4187                         }
4188
4189                         void IList.Remove (object value)
4190                         {
4191                                 throw new NotSupportedException ("Remove operation is not supported.");
4192                         }
4193
4194                         void IList.RemoveAt (int index)
4195                         {
4196                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
4197                         }
4198
4199                         public int IndexOf (int checkedIndex)
4200                         {
4201                                 int [] indices = GetIndices ();
4202                                 for (int i = 0; i < indices.Length; i++) {
4203                                         if (indices [i] == checkedIndex)
4204                                                 return i;
4205                                 }
4206                                 return -1;
4207                         }
4208                         #endregion      // Public Methods
4209
4210                         private int [] GetIndices ()
4211                         {
4212                                 ArrayList checked_items = owner.CheckedItems.List;
4213                                 int [] indices = new int [checked_items.Count];
4214                                 for (int i = 0; i < checked_items.Count; i++) {
4215                                         ListViewItem item = (ListViewItem) checked_items [i];
4216                                         indices [i] = item.Index;
4217                                 }
4218                                 return indices;
4219                         }
4220                 }       // CheckedIndexCollection
4221
4222 #if NET_2_0
4223                 [ListBindable (false)]
4224 #endif
4225                 public class CheckedListViewItemCollection : IList, ICollection, IEnumerable
4226                 {
4227                         private readonly ListView owner;
4228                         private ArrayList list;
4229
4230                         #region Public Constructor
4231                         public CheckedListViewItemCollection (ListView owner)
4232                         {
4233                                 this.owner = owner;
4234                                 this.owner.Items.Changed += new CollectionChangedHandler (
4235                                         ItemsCollection_Changed);
4236                         }
4237                         #endregion      // Public Constructor
4238
4239                         #region Public Properties
4240                         [Browsable (false)]
4241                         public int Count {
4242                                 get {
4243                                         if (!owner.CheckBoxes)
4244                                                 return 0;
4245                                         return List.Count;
4246                                 }
4247                         }
4248
4249                         public bool IsReadOnly {
4250                                 get { return true; }
4251                         }
4252
4253                         public ListViewItem this [int index] {
4254                                 get {
4255 #if NET_2_0
4256                                         if (owner.VirtualMode)
4257                                                 throw new InvalidOperationException ();
4258 #endif
4259                                         ArrayList checked_items = List;
4260                                         if (index < 0 || index >= checked_items.Count)
4261                                                 throw new ArgumentOutOfRangeException ("index");
4262                                         return (ListViewItem) checked_items [index];
4263                                 }
4264                         }
4265
4266 #if NET_2_0
4267                         public virtual ListViewItem this [string key] {
4268                                 get {
4269                                         int idx = IndexOfKey (key);
4270                                         return idx == -1 ? null : (ListViewItem) List [idx];
4271                                 }
4272                         }
4273 #endif
4274
4275                         bool ICollection.IsSynchronized {
4276                                 get { return false; }
4277                         }
4278
4279                         object ICollection.SyncRoot {
4280                                 get { return this; }
4281                         }
4282
4283                         bool IList.IsFixedSize {
4284                                 get { return true; }
4285                         }
4286
4287                         object IList.this [int index] {
4288                                 get { return this [index]; }
4289                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
4290                         }
4291                         #endregion      // Public Properties
4292
4293                         #region Public Methods
4294                         public bool Contains (ListViewItem item)
4295                         {
4296                                 if (!owner.CheckBoxes)
4297                                         return false;
4298                                 return List.Contains (item);
4299                         }
4300
4301 #if NET_2_0
4302                         public virtual bool ContainsKey (string key)
4303                         {
4304                                 return IndexOfKey (key) != -1;
4305                         }
4306 #endif
4307
4308                         public void CopyTo (Array dest, int index)
4309                         {
4310 #if NET_2_0
4311                                 if (owner.VirtualMode)
4312                                         throw new InvalidOperationException ();
4313 #endif
4314                                 if (!owner.CheckBoxes)
4315                                         return;
4316                                 List.CopyTo (dest, index);
4317                         }
4318
4319                         public IEnumerator GetEnumerator ()
4320                         {
4321 #if NET_2_0
4322                                 if (owner.VirtualMode)
4323                                         throw new InvalidOperationException ();
4324 #endif
4325                                 if (!owner.CheckBoxes)
4326                                         return (new ListViewItem [0]).GetEnumerator ();
4327                                 return List.GetEnumerator ();
4328                         }
4329
4330                         int IList.Add (object value)
4331                         {
4332                                 throw new NotSupportedException ("Add operation is not supported.");
4333                         }
4334
4335                         void IList.Clear ()
4336                         {
4337                                 throw new NotSupportedException ("Clear operation is not supported.");
4338                         }
4339
4340                         bool IList.Contains (object item)
4341                         {
4342                                 if (!(item is ListViewItem))
4343                                         return false;
4344                                 return Contains ((ListViewItem) item);
4345                         }
4346
4347                         int IList.IndexOf (object item)
4348                         {
4349                                 if (!(item is ListViewItem))
4350                                         return -1;
4351                                 return IndexOf ((ListViewItem) item);
4352                         }
4353
4354                         void IList.Insert (int index, object value)
4355                         {
4356                                 throw new NotSupportedException ("Insert operation is not supported.");
4357                         }
4358
4359                         void IList.Remove (object value)
4360                         {
4361                                 throw new NotSupportedException ("Remove operation is not supported.");
4362                         }
4363
4364                         void IList.RemoveAt (int index)
4365                         {
4366                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
4367                         }
4368
4369                         public int IndexOf (ListViewItem item)
4370                         {
4371 #if NET_2_0
4372                                 if (owner.VirtualMode)
4373                                         throw new InvalidOperationException ();
4374 #endif
4375                                 if (!owner.CheckBoxes)
4376                                         return -1;
4377                                 return List.IndexOf (item);
4378                         }
4379
4380 #if NET_2_0
4381                         public virtual int IndexOfKey (string key)
4382                         {
4383 #if NET_2_0
4384                                 if (owner.VirtualMode)
4385                                         throw new InvalidOperationException ();
4386 #endif
4387                                 if (key == null || key.Length == 0)
4388                                         return -1;
4389
4390                                 ArrayList checked_items = List;
4391                                 for (int i = 0; i < checked_items.Count; i++) {
4392                                         ListViewItem item = (ListViewItem) checked_items [i];
4393                                         if (String.Compare (key, item.Name, true) == 0)
4394                                                 return i;
4395                                 }
4396
4397                                 return -1;
4398                         }
4399 #endif
4400                         #endregion      // Public Methods
4401
4402                         internal ArrayList List {
4403                                 get {
4404                                         if (list == null) {
4405                                                 list = new ArrayList ();
4406                                                 foreach (ListViewItem item in owner.Items) {
4407                                                         if (item.Checked)
4408                                                                 list.Add (item);
4409                                                 }
4410                                         }
4411                                         return list;
4412                                 }
4413                         }
4414
4415                         internal void Reset ()
4416                         {
4417                                 // force re-population of list
4418                                 list = null;
4419                         }
4420
4421                         private void ItemsCollection_Changed ()
4422                         {
4423                                 Reset ();
4424                         }
4425                 }       // CheckedListViewItemCollection
4426
4427 #if NET_2_0
4428                 [ListBindable (false)]
4429 #endif
4430                 public class ColumnHeaderCollection : IList, ICollection, IEnumerable
4431                 {
4432                         internal ArrayList list;
4433                         private ListView owner;
4434
4435                         #region Public Constructor
4436                         public ColumnHeaderCollection (ListView owner)
4437                         {
4438                                 list = new ArrayList ();
4439                                 this.owner = owner;
4440                         }
4441                         #endregion      // Public Constructor
4442
4443                         #region Public Properties
4444                         [Browsable (false)]
4445                         public int Count {
4446                                 get { return list.Count; }
4447                         }
4448
4449                         public bool IsReadOnly {
4450                                 get { return false; }
4451                         }
4452
4453                         public virtual ColumnHeader this [int index] {
4454                                 get {
4455                                         if (index < 0 || index >= list.Count)
4456                                                 throw new ArgumentOutOfRangeException ("index");
4457                                         return (ColumnHeader) list [index];
4458                                 }
4459                         }
4460
4461 #if NET_2_0
4462                         public virtual ColumnHeader this [string key] {
4463                                 get {
4464                                         int idx = IndexOfKey (key);
4465                                         if (idx == -1)
4466                                                 return null;
4467
4468                                         return (ColumnHeader) list [idx];
4469                                 }
4470                         }
4471 #endif
4472
4473                         bool ICollection.IsSynchronized {
4474                                 get { return true; }
4475                         }
4476
4477                         object ICollection.SyncRoot {
4478                                 get { return this; }
4479                         }
4480
4481                         bool IList.IsFixedSize {
4482                                 get { return list.IsFixedSize; }
4483                         }
4484
4485                         object IList.this [int index] {
4486                                 get { return this [index]; }
4487                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
4488                         }
4489                         #endregion      // Public Properties
4490
4491                         #region Public Methods
4492                         public virtual int Add (ColumnHeader value)
4493                         {
4494                                 int idx = list.Add (value);
4495                                 owner.AddColumn (value, idx, true);
4496                                 return idx;
4497                         }
4498
4499                         public virtual ColumnHeader Add (string text, int width, HorizontalAlignment textAlign)
4500                         {
4501                                 ColumnHeader colHeader = new ColumnHeader (this.owner, text, textAlign, width);
4502                                 this.Add (colHeader);
4503                                 return colHeader;
4504                         }
4505
4506 #if NET_2_0
4507                         public virtual ColumnHeader Add (string text)
4508                         {
4509                                 return Add (String.Empty, text);
4510                         }
4511
4512                         public virtual ColumnHeader Add (string text, int width)
4513                         {
4514                                 return Add (String.Empty, text, width);
4515                         }
4516
4517                         public virtual ColumnHeader Add (string key, string text)
4518                         {
4519                                 ColumnHeader colHeader = new ColumnHeader ();
4520                                 colHeader.Name = key;
4521                                 colHeader.Text = text;
4522                                 Add (colHeader);
4523                                 return colHeader;
4524                         }
4525
4526                         public virtual ColumnHeader Add (string key, string text, int width)
4527                         {
4528                                 return Add (key, text, width, HorizontalAlignment.Left, -1);
4529                         }
4530
4531                         public virtual ColumnHeader Add (string key, string text, int width, HorizontalAlignment textAlign, int imageIndex)
4532                         {
4533                                 ColumnHeader colHeader = new ColumnHeader (key, text, width, textAlign);
4534                                 colHeader.ImageIndex = imageIndex;
4535                                 Add (colHeader);
4536                                 return colHeader;
4537                         }
4538
4539                         public virtual ColumnHeader Add (string key, string text, int width, HorizontalAlignment textAlign, string imageKey)
4540                         {
4541                                 ColumnHeader colHeader = new ColumnHeader (key, text, width, textAlign);
4542                                 colHeader.ImageKey = imageKey;
4543                                 Add (colHeader);
4544                                 return colHeader;
4545                         }
4546 #endif
4547
4548                         public virtual void AddRange (ColumnHeader [] values)
4549                         {
4550                                 foreach (ColumnHeader colHeader in values) {
4551                                         int idx = list.Add (colHeader);
4552                                         owner.AddColumn (colHeader, idx, false);
4553                                 }
4554                                 
4555                                 owner.Redraw (true);
4556                         }
4557
4558                         public virtual void Clear ()
4559                         {
4560                                 foreach (ColumnHeader col in list)
4561                                         col.SetListView (null);
4562                                 list.Clear ();
4563                                 owner.ReorderColumns (new int [0], true);
4564                         }
4565
4566                         public bool Contains (ColumnHeader value)
4567                         {
4568                                 return list.Contains (value);
4569                         }
4570
4571 #if NET_2_0
4572                         public virtual bool ContainsKey (string key)
4573                         {
4574                                 return IndexOfKey (key) != -1;
4575                         }
4576 #endif
4577
4578                         public IEnumerator GetEnumerator ()
4579                         {
4580                                 return list.GetEnumerator ();
4581                         }
4582
4583                         void ICollection.CopyTo (Array dest, int index)
4584                         {
4585                                 list.CopyTo (dest, index);
4586                         }
4587
4588                         int IList.Add (object value)
4589                         {
4590                                 if (! (value is ColumnHeader)) {
4591                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
4592                                 }
4593
4594                                 return this.Add ((ColumnHeader) value);
4595                         }
4596
4597                         bool IList.Contains (object value)
4598                         {
4599                                 if (! (value is ColumnHeader)) {
4600                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
4601                                 }
4602
4603                                 return this.Contains ((ColumnHeader) value);
4604                         }
4605
4606                         int IList.IndexOf (object value)
4607                         {
4608                                 if (! (value is ColumnHeader)) {
4609                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
4610                                 }
4611
4612                                 return this.IndexOf ((ColumnHeader) value);
4613                         }
4614
4615                         void IList.Insert (int index, object value)
4616                         {
4617                                 if (! (value is ColumnHeader)) {
4618                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
4619                                 }
4620
4621                                 this.Insert (index, (ColumnHeader) value);
4622                         }
4623
4624                         void IList.Remove (object value)
4625                         {
4626                                 if (! (value is ColumnHeader)) {
4627                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
4628                                 }
4629
4630                                 this.Remove ((ColumnHeader) value);
4631                         }
4632
4633                         public int IndexOf (ColumnHeader value)
4634                         {
4635                                 return list.IndexOf (value);
4636                         }
4637
4638 #if NET_2_0
4639                         public virtual int IndexOfKey (string key)
4640                         {
4641                                 if (key == null || key.Length == 0)
4642                                         return -1;
4643
4644                                 for (int i = 0; i < list.Count; i++) {
4645                                         ColumnHeader col = (ColumnHeader) list [i];
4646                                         if (String.Compare (key, col.Name, true) == 0)
4647                                                 return i;
4648                                 }
4649
4650                                 return -1;
4651                         }
4652 #endif
4653
4654                         public void Insert (int index, ColumnHeader value)
4655                         {
4656                                 // LAMESPEC: MSDOCS say greater than or equal to the value of the Count property
4657                                 // but it's really only greater.
4658                                 if (index < 0 || index > list.Count)
4659                                         throw new ArgumentOutOfRangeException ("index");
4660
4661                                 list.Insert (index, value);
4662                                 owner.AddColumn (value, index, true);
4663                         }
4664
4665 #if NET_2_0
4666                         public void Insert (int index, string text)
4667                         {
4668                                 Insert (index, String.Empty, text);
4669                         }
4670
4671                         public void Insert (int index, string text, int width)
4672                         {
4673                                 Insert (index, String.Empty, text, width);
4674                         }
4675
4676                         public void Insert (int index, string key, string text)
4677                         {
4678                                 ColumnHeader colHeader = new ColumnHeader ();
4679                                 colHeader.Name = key;
4680                                 colHeader.Text = text;
4681                                 Insert (index, colHeader);
4682                         }
4683
4684                         public void Insert (int index, string key, string text, int width)
4685                         {
4686                                 ColumnHeader colHeader = new ColumnHeader (key, text, width, HorizontalAlignment.Left);
4687                                 Insert (index, colHeader);
4688                         }
4689
4690                         public void Insert (int index, string key, string text, int width, HorizontalAlignment textAlign, int imageIndex)
4691                         {
4692                                 ColumnHeader colHeader = new ColumnHeader (key, text, width, textAlign);
4693                                 colHeader.ImageIndex = imageIndex;
4694                                 Insert (index, colHeader);
4695                         }
4696
4697                         public void Insert (int index, string key, string text, int width, HorizontalAlignment textAlign, string imageKey)
4698                         {
4699                                 ColumnHeader colHeader = new ColumnHeader (key, text, width, textAlign);
4700                                 colHeader.ImageKey = imageKey;
4701                                 Insert (index, colHeader);
4702                         }
4703 #endif
4704
4705                         public void Insert (int index, string text, int width, HorizontalAlignment textAlign)
4706                         {
4707                                 ColumnHeader colHeader = new ColumnHeader (this.owner, text, textAlign, width);
4708                                 this.Insert (index, colHeader);
4709                         }
4710
4711                         public virtual void Remove (ColumnHeader column)
4712                         {
4713                                 if (!Contains (column))
4714                                         return;
4715
4716                                 list.Remove (column);
4717                                 column.SetListView (null);
4718
4719                                 int rem_display_index = column.InternalDisplayIndex;
4720                                 int [] display_indices = new int [list.Count];
4721                                 for (int i = 0; i < display_indices.Length; i++) {
4722                                         ColumnHeader col = (ColumnHeader) list [i];
4723                                         int display_index = col.InternalDisplayIndex;
4724                                         if (display_index < rem_display_index) {
4725                                                 display_indices [i] = display_index;
4726                                         } else {
4727                                                 display_indices [i] = (display_index - 1);
4728                                         }
4729                                 }
4730
4731                                 column.InternalDisplayIndex = -1;
4732                                 owner.ReorderColumns (display_indices, true);
4733                         }
4734
4735 #if NET_2_0
4736                         public virtual void RemoveByKey (string key)
4737                         {
4738                                 int idx = IndexOfKey (key);
4739                                 if (idx != -1)
4740                                         RemoveAt (idx);
4741                         }
4742 #endif
4743
4744                         public virtual void RemoveAt (int index)
4745                         {
4746                                 if (index < 0 || index >= list.Count)
4747                                         throw new ArgumentOutOfRangeException ("index");
4748
4749                                 ColumnHeader col = (ColumnHeader) list [index];
4750                                 Remove (col);
4751                         }
4752                         #endregion      // Public Methods
4753                         
4754
4755                 }       // ColumnHeaderCollection
4756
4757 #if NET_2_0
4758                 [ListBindable (false)]
4759 #endif
4760                 public class ListViewItemCollection : IList, ICollection, IEnumerable
4761                 {
4762                         private readonly ArrayList list;
4763                         private ListView owner;
4764 #if NET_2_0
4765                         private ListViewGroup group;
4766 #endif
4767
4768                         // The collection can belong to a ListView (main) or to a ListViewGroup (sub-collection)
4769                         // In the later case ListViewItem.ListView never gets modified
4770                         private bool is_main_collection = true;
4771
4772                         #region Public Constructor
4773                         public ListViewItemCollection (ListView owner)
4774                         {
4775                                 list = new ArrayList (0);
4776                                 this.owner = owner;
4777                         }
4778                         #endregion      // Public Constructor
4779
4780 #if NET_2_0
4781                         internal ListViewItemCollection (ListView owner, ListViewGroup group) : this (owner)
4782                         {
4783                                 this.group = group;
4784                                 is_main_collection = false;
4785                         }
4786 #endif
4787
4788                         #region Public Properties
4789                         [Browsable (false)]
4790                         public int Count {
4791                                 get {
4792 #if NET_2_0
4793                                         if (owner != null && owner.VirtualMode)
4794                                                 return owner.VirtualListSize;
4795 #endif
4796
4797                                         return list.Count; 
4798                                 }
4799                         }
4800
4801                         public bool IsReadOnly {
4802                                 get { return false; }
4803                         }
4804
4805                         public virtual ListViewItem this [int index] {
4806                                 get {
4807                                         if (index < 0 || index >= Count)
4808                                                 throw new ArgumentOutOfRangeException ("displayIndex");
4809
4810 #if NET_2_0
4811                                         if (owner != null && owner.VirtualMode)
4812                                                 return RetrieveVirtualItemFromOwner (index);
4813 #endif
4814                                         return (ListViewItem) list [index];
4815                                 }
4816
4817                                 set {
4818                                         if (index < 0 || index >= Count)
4819                                                 throw new ArgumentOutOfRangeException ("displayIndex");
4820
4821 #if NET_2_0
4822                                         if (owner != null && owner.VirtualMode)
4823                                                 throw new InvalidOperationException ();
4824 #endif
4825
4826                                         if (list.Contains (value))
4827                                                 throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "value");
4828
4829                                         if (value.ListView != null && value.ListView != owner)
4830                                                 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");
4831
4832                                         if (is_main_collection)
4833                                                 value.Owner = owner;
4834 #if NET_2_0
4835                                         else {
4836                                                 if (value.Group != null)
4837                                                         value.Group.Items.Remove (value);
4838
4839                                                 value.SetGroup (group);
4840                                         }
4841 #endif
4842
4843                                         list [index] = value;
4844                                         CollectionChanged (true);
4845                                 }
4846                         }
4847
4848 #if NET_2_0
4849                         public virtual ListViewItem this [string key] {
4850                                 get {
4851                                         int idx = IndexOfKey (key);
4852                                         if (idx == -1)
4853                                                 return null;
4854
4855                                         return this [idx];
4856                                 }
4857                         }
4858 #endif
4859
4860                         bool ICollection.IsSynchronized {
4861                                 get { return true; }
4862                         }
4863
4864                         object ICollection.SyncRoot {
4865                                 get { return this; }
4866                         }
4867
4868                         bool IList.IsFixedSize {
4869                                 get { return list.IsFixedSize; }
4870                         }
4871
4872                         object IList.this [int index] {
4873                                 get { return this [index]; }
4874                                 set {
4875                                         if (value is ListViewItem)
4876                                                 this [index] = (ListViewItem) value;
4877                                         else
4878                                                 this [index] = new ListViewItem (value.ToString ());
4879                                         OnChange ();
4880                                 }
4881                         }
4882                         #endregion      // Public Properties
4883
4884                         #region Public Methods
4885                         public virtual ListViewItem Add (ListViewItem value)
4886                         {
4887 #if NET_2_0
4888                                 if (owner != null && owner.VirtualMode)
4889                                         throw new InvalidOperationException ();
4890 #endif
4891
4892                                 AddItem (value);
4893                                 CollectionChanged (true);
4894
4895                                 return value;
4896                         }
4897
4898                         public virtual ListViewItem Add (string text)
4899                         {
4900                                 ListViewItem item = new ListViewItem (text);
4901                                 return this.Add (item);
4902                         }
4903
4904                         public virtual ListViewItem Add (string text, int imageIndex)
4905                         {
4906                                 ListViewItem item = new ListViewItem (text, imageIndex);
4907                                 return this.Add (item);
4908                         }
4909
4910 #if NET_2_0
4911                         public virtual ListViewItem Add (string text, string imageKey)
4912                         {
4913                                 ListViewItem item = new ListViewItem (text, imageKey);
4914                                 return this.Add (item);
4915                         }
4916
4917                         public virtual ListViewItem Add (string key, string text, int imageIndex)
4918                         {
4919                                 ListViewItem item = new ListViewItem (text, imageIndex);
4920                                 item.Name = key;
4921                                 return this.Add (item);
4922                         }
4923
4924                         public virtual ListViewItem Add (string key, string text, string imageKey)
4925                         {
4926                                 ListViewItem item = new ListViewItem (text, imageKey);
4927                                 item.Name = key;
4928                                 return this.Add (item);
4929                         }
4930 #endif
4931
4932                         public void AddRange (ListViewItem [] items)
4933                         {
4934                                 if (items == null)
4935                                         throw new ArgumentNullException ("Argument cannot be null!", "values");
4936 #if NET_2_0
4937                                 if (owner != null && owner.VirtualMode)
4938                                         throw new InvalidOperationException ();
4939 #endif
4940
4941                                 foreach (ListViewItem item in items)
4942                                         AddItem (item);
4943
4944                                 CollectionChanged (true);
4945                         }
4946
4947 #if NET_2_0
4948                         public void AddRange (ListViewItemCollection items)
4949                         {
4950                                 if (items == null)
4951                                         throw new ArgumentNullException ("Argument cannot be null!", "items");
4952
4953                                 ListViewItem[] itemArray = new ListViewItem[items.Count];
4954                                 items.CopyTo (itemArray,0);
4955                                 this.AddRange (itemArray);
4956                         }
4957 #endif
4958
4959                         public virtual void Clear ()
4960                         {
4961 #if NET_2_0
4962                                 if (owner != null && owner.VirtualMode)
4963                                         throw new InvalidOperationException ();
4964 #endif
4965                                 if (is_main_collection && owner != null) {
4966                                         owner.SetFocusedItem (-1);
4967                                         owner.h_scroll.Value = owner.v_scroll.Value = 0;
4968                                                 
4969                                         foreach (ListViewItem item in list) {
4970                                                 owner.item_control.CancelEdit (item);
4971                                                 item.Owner = null;
4972                                         }
4973                                         
4974                                 }
4975 #if NET_2_0
4976                                 else
4977                                         foreach (ListViewItem item in list)
4978                                                 item.SetGroup (null);
4979 #endif
4980
4981                                 list.Clear ();
4982                                 CollectionChanged (false);
4983                         }
4984
4985                         public bool Contains (ListViewItem item)
4986                         {
4987                                 return IndexOf (item) != -1;
4988                         }
4989
4990 #if NET_2_0
4991                         public virtual bool ContainsKey (string key)
4992                         {
4993                                 return IndexOfKey (key) != -1;
4994                         }
4995 #endif
4996
4997                         public void CopyTo (Array dest, int index)
4998                         {
4999                                 list.CopyTo (dest, index);
5000                         }
5001
5002 #if NET_2_0
5003                         public ListViewItem [] Find (string key, bool searchAllSubItems)
5004                         {
5005                                 if (key == null)
5006                                         return new ListViewItem [0];
5007
5008                                 List<ListViewItem> temp_list = new List<ListViewItem> ();
5009                                 
5010                                 for (int i = 0; i < list.Count; i++) {
5011                                         ListViewItem lvi = (ListViewItem) list [i];
5012                                         if (String.Compare (key, lvi.Name, true) == 0)
5013                                                 temp_list.Add (lvi);
5014                                 }
5015
5016                                 ListViewItem [] retval = new ListViewItem [temp_list.Count];
5017                                 temp_list.CopyTo (retval);
5018
5019                                 return retval;
5020                         }
5021 #endif
5022
5023                         public IEnumerator GetEnumerator ()
5024                         {
5025 #if NET_2_0
5026                                 if (owner != null && owner.VirtualMode)
5027                                         throw new InvalidOperationException ();
5028 #endif
5029                                 
5030                                 return list.GetEnumerator ();
5031                         }
5032
5033                         int IList.Add (object item)
5034                         {
5035                                 int result;
5036                                 ListViewItem li;
5037
5038 #if NET_2_0
5039                                 if (owner != null && owner.VirtualMode)
5040                                         throw new InvalidOperationException ();
5041 #endif
5042
5043                                 if (item is ListViewItem) {
5044                                         li = (ListViewItem) item;
5045                                         if (list.Contains (li))
5046                                                 throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "item");
5047
5048                                         if (li.ListView != null && li.ListView != owner)
5049                                                 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");
5050                                 }
5051                                 else
5052                                         li = new ListViewItem (item.ToString ());
5053
5054                                 li.Owner = owner;
5055                                 result = list.Add (li);
5056                                 CollectionChanged (true);
5057
5058                                 return result;
5059                         }
5060
5061                         bool IList.Contains (object item)
5062                         {
5063                                 return Contains ((ListViewItem) item);
5064                         }
5065
5066                         int IList.IndexOf (object item)
5067                         {
5068                                 return IndexOf ((ListViewItem) item);
5069                         }
5070
5071                         void IList.Insert (int index, object item)
5072                         {
5073                                 if (item is ListViewItem)
5074                                         this.Insert (index, (ListViewItem) item);
5075                                 else
5076                                         this.Insert (index, item.ToString ());
5077                         }
5078
5079                         void IList.Remove (object item)
5080                         {
5081                                 Remove ((ListViewItem) item);
5082                         }
5083
5084                         public int IndexOf (ListViewItem item)
5085                         {
5086 #if NET_2_0
5087                                 if (owner != null && owner.VirtualMode) {
5088                                         for (int i = 0; i < Count; i++)
5089                                                 if (RetrieveVirtualItemFromOwner (i) == item)
5090                                                         return i;
5091
5092                                         return -1;
5093                                 }
5094 #endif
5095                                 
5096                                 return list.IndexOf (item);
5097                         }
5098
5099 #if NET_2_0
5100                         public virtual int IndexOfKey (string key)
5101                         {
5102                                 if (key == null || key.Length == 0)
5103                                         return -1;
5104
5105                                 for (int i = 0; i < Count; i++) {
5106                                         ListViewItem lvi = this [i];
5107                                         if (String.Compare (key, lvi.Name, true) == 0)
5108                                                 return i;
5109                                 }
5110
5111                                 return -1;
5112                         }
5113 #endif
5114
5115                         public ListViewItem Insert (int index, ListViewItem item)
5116                         {
5117                                 if (index < 0 || index > list.Count)
5118                                         throw new ArgumentOutOfRangeException ("index");
5119
5120 #if NET_2_0
5121                                 if (owner != null && owner.VirtualMode)
5122                                         throw new InvalidOperationException ();
5123 #endif
5124
5125                                 if (list.Contains (item))
5126                                         throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "item");
5127
5128                                 if (item.ListView != null && item.ListView != owner)
5129                                         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");
5130
5131                                 if (is_main_collection)
5132                                         item.Owner = owner;
5133 #if NET_2_0
5134                                 else {
5135                                         if (item.Group != null)
5136                                                 item.Group.Items.Remove (item);
5137
5138                                         item.SetGroup (group);
5139                                 }
5140 #endif
5141
5142                                 list.Insert (index, item);
5143                                 CollectionChanged (true);
5144                                 return item;
5145                         }
5146
5147                         public ListViewItem Insert (int index, string text)
5148                         {
5149                                 return this.Insert (index, new ListViewItem (text));
5150                         }
5151
5152                         public ListViewItem Insert (int index, string text, int imageIndex)
5153                         {
5154                                 return this.Insert (index, new ListViewItem (text, imageIndex));
5155                         }
5156
5157 #if NET_2_0
5158                         public ListViewItem Insert (int index, string text, string imageKey)
5159                         {
5160                                 ListViewItem lvi = new ListViewItem (text, imageKey);
5161                                 return Insert (index, lvi);
5162                         }
5163
5164                         public virtual ListViewItem Insert (int index, string key, string text, int imageIndex)
5165                         {
5166                                 ListViewItem lvi = new ListViewItem (text, imageIndex);
5167                                 lvi.Name = key;
5168                                 return Insert (index, lvi);
5169                         }
5170
5171                         public virtual ListViewItem Insert (int index, string key, string text, string imageKey)
5172                         {
5173                                 ListViewItem lvi = new ListViewItem (text, imageKey);
5174                                 lvi.Name = key;
5175                                 return Insert (index, lvi);
5176                         }
5177 #endif
5178
5179                         public virtual void Remove (ListViewItem item)
5180                         {
5181 #if NET_2_0
5182                                 if (owner != null && owner.VirtualMode)
5183                                         throw new InvalidOperationException ();
5184 #endif
5185
5186                                 int idx = list.IndexOf (item);
5187                                 if (idx != -1)
5188                                         RemoveAt (idx);
5189                         }
5190
5191                         public virtual void RemoveAt (int index)
5192                         {
5193                                 if (index < 0 || index >= Count)
5194                                         throw new ArgumentOutOfRangeException ("index");
5195
5196 #if NET_2_0
5197                                 if (owner != null && owner.VirtualMode)
5198                                         throw new InvalidOperationException ();
5199 #endif
5200
5201                                 ListViewItem item = (ListViewItem) list [index];
5202
5203                                 bool selection_changed = false;
5204                                 if (is_main_collection && owner != null) {
5205
5206                                         if (item.Focused && index + 1 == Count) // Last item
5207                                                 owner.SetFocusedItem (index == 0 ? -1 : index - 1);
5208
5209                                         selection_changed = owner.SelectedIndices.Contains (index);
5210                                         owner.item_control.CancelEdit (item);
5211                                 }
5212
5213                                 list.RemoveAt (index);
5214
5215                                 if (is_main_collection)
5216                                         item.Owner = null;
5217 #if NET_2_0
5218                                 else
5219                                         item.SetGroup (null);
5220 #endif
5221
5222                                 CollectionChanged (false);
5223                                 if (selection_changed && owner != null)
5224                                         owner.OnSelectedIndexChanged (EventArgs.Empty);
5225                         }
5226
5227 #if NET_2_0
5228                         public virtual void RemoveByKey (string key)
5229                         {
5230                                 int idx = IndexOfKey (key);
5231                                 if (idx != -1)
5232                                         RemoveAt (idx);
5233                         }
5234 #endif
5235
5236                         #endregion      // Public Methods
5237
5238                         internal ListView Owner {
5239                                 get {
5240                                         return owner;
5241                                 }
5242                                 set {
5243                                         owner = value;
5244                                 }
5245                         }
5246
5247 #if NET_2_0
5248                         internal ListViewGroup Group {
5249                                 get {
5250                                         return group;
5251                                 }
5252                                 set {
5253                                         group = value;
5254                                 }
5255                         }
5256 #endif
5257
5258                         void AddItem (ListViewItem value)
5259                         {
5260                                 if (list.Contains (value))
5261                                         throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "value");
5262
5263                                 if (value.ListView != null && value.ListView != owner)
5264                                         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");
5265                                 if (is_main_collection)
5266                                         value.Owner = owner;
5267 #if NET_2_0
5268                                 else {
5269                                         if (value.Group != null)
5270                                                 value.Group.Items.Remove (value);
5271
5272                                         value.SetGroup (group);
5273                                 }
5274 #endif
5275
5276                                 list.Add (value);
5277                         }
5278
5279                         void CollectionChanged (bool sort)
5280                         {
5281                                 if (owner != null) {
5282                                         if (sort)
5283                                                 owner.Sort (false);
5284
5285                                         OnChange ();
5286                                         owner.Redraw (true);
5287                                 }
5288                         }
5289
5290 #if NET_2_0
5291                         ListViewItem RetrieveVirtualItemFromOwner (int displayIndex)
5292                         {
5293                                 RetrieveVirtualItemEventArgs args = new RetrieveVirtualItemEventArgs (displayIndex);
5294
5295                                 owner.OnRetrieveVirtualItem (args);
5296                                 ListViewItem retval = args.Item;
5297                                 retval.Owner = owner;
5298                                 retval.DisplayIndex = displayIndex;
5299
5300                                 return retval;
5301                         }
5302 #endif
5303
5304                         internal event CollectionChangedHandler Changed;
5305
5306                         internal void Sort (IComparer comparer)
5307                         {
5308                                 list.Sort (comparer);
5309                                 OnChange ();
5310                         }
5311
5312                         internal void OnChange ()
5313                         {
5314                                 if (Changed != null)
5315                                         Changed ();
5316                         }
5317                 }       // ListViewItemCollection
5318
5319                         
5320                 // In normal mode, the selection information resides in the Items,
5321                 // making SelectedIndexCollection.List read-only
5322                 //
5323                 // In virtual mode, SelectedIndexCollection directly saves the selection
5324                 // information, instead of getting it from Items, making List read-and-write
5325 #if NET_2_0
5326                 [ListBindable (false)]
5327 #endif
5328                 public class SelectedIndexCollection : IList, ICollection, IEnumerable
5329                 {
5330                         private readonly ListView owner;
5331                         private ArrayList list;
5332
5333                         #region Public Constructor
5334                         public SelectedIndexCollection (ListView owner)
5335                         {
5336                                 this.owner = owner;
5337                                 owner.Items.Changed += new CollectionChangedHandler (ItemsCollection_Changed);
5338                         }
5339                         #endregion      // Public Constructor
5340
5341                         #region Public Properties
5342                         [Browsable (false)]
5343                         public int Count {
5344                                 get {
5345                                         if (!owner.IsHandleCreated)
5346                                                 return 0;
5347
5348                                         return List.Count;
5349                                 }
5350                         }
5351
5352                         public bool IsReadOnly {
5353                                 get { 
5354 #if NET_2_0
5355                                         return false;
5356 #else
5357                                         return true; 
5358 #endif
5359                                 }
5360                         }
5361
5362                         public int this [int index] {
5363                                 get {
5364                                         if (!owner.IsHandleCreated || index < 0 || index >= List.Count)
5365                                                 throw new ArgumentOutOfRangeException ("index");
5366
5367                                         return (int) List [index];
5368                                 }
5369                         }
5370
5371                         bool ICollection.IsSynchronized {
5372                                 get { return false; }
5373                         }
5374
5375                         object ICollection.SyncRoot {
5376                                 get { return this; }
5377                         }
5378
5379                         bool IList.IsFixedSize {
5380                                 get { 
5381 #if NET_2_0
5382                                         return false;
5383 #else
5384                                         return true;
5385 #endif
5386                                 }
5387                         }
5388
5389                         object IList.this [int index] {
5390                                 get { return this [index]; }
5391                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
5392                         }
5393                         #endregion      // Public Properties
5394
5395                         #region Public Methods
5396 #if NET_2_0
5397                         public int Add (int itemIndex)
5398                         {
5399                                 if (itemIndex < 0 || itemIndex >= owner.Items.Count)
5400                                         throw new ArgumentOutOfRangeException ("index");
5401
5402                                 if (owner.virtual_mode && !owner.IsHandleCreated)
5403                                         return -1;
5404
5405                                 owner.Items [itemIndex].Selected = true;
5406
5407                                 if (!owner.IsHandleCreated)
5408                                         return 0;
5409
5410                                 return List.Count;
5411                         }
5412 #endif
5413
5414 #if NET_2_0
5415                         public 
5416 #else
5417                         internal
5418 #endif  
5419                         void Clear ()
5420                         {
5421                                 if (!owner.IsHandleCreated)
5422                                         return;
5423
5424                                 int [] indexes = (int []) List.ToArray (typeof (int));
5425                                 foreach (int index in indexes)
5426                                         owner.Items [index].Selected = false;
5427                         }
5428
5429                         public bool Contains (int selectedIndex)
5430                         {
5431                                 return IndexOf (selectedIndex) != -1;
5432                         }
5433
5434                         public void CopyTo (Array dest, int index)
5435                         {
5436                                 List.CopyTo (dest, index);
5437                         }
5438
5439                         public IEnumerator GetEnumerator ()
5440                         {
5441                                 return List.GetEnumerator ();
5442                         }
5443
5444                         int IList.Add (object value)
5445                         {
5446                                 throw new NotSupportedException ("Add operation is not supported.");
5447                         }
5448
5449                         void IList.Clear ()
5450                         {
5451                                 Clear ();
5452                         }
5453
5454                         bool IList.Contains (object selectedIndex)
5455                         {
5456                                 if (!(selectedIndex is int))
5457                                         return false;
5458                                 return Contains ((int) selectedIndex);
5459                         }
5460
5461                         int IList.IndexOf (object selectedIndex)
5462                         {
5463                                 if (!(selectedIndex is int))
5464                                         return -1;
5465                                 return IndexOf ((int) selectedIndex);
5466                         }
5467
5468                         void IList.Insert (int index, object value)
5469                         {
5470                                 throw new NotSupportedException ("Insert operation is not supported.");
5471                         }
5472
5473                         void IList.Remove (object value)
5474                         {
5475                                 throw new NotSupportedException ("Remove operation is not supported.");
5476                         }
5477
5478                         void IList.RemoveAt (int index)
5479                         {
5480                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
5481                         }
5482
5483                         public int IndexOf (int selectedIndex)
5484                         {
5485                                 if (!owner.IsHandleCreated)
5486                                         return -1;
5487
5488                                 return List.IndexOf (selectedIndex);
5489                         }
5490
5491 #if NET_2_0
5492                         public void Remove (int itemIndex)
5493                         {
5494                                 if (itemIndex < 0 || itemIndex >= owner.Items.Count)
5495                                         throw new ArgumentOutOfRangeException ("itemIndex");
5496
5497                                 owner.Items [itemIndex].Selected = false;
5498                         }
5499 #endif
5500                         #endregion      // Public Methods
5501
5502                         internal ArrayList List {
5503                                 get {
5504                                         if (list == null) {
5505                                                 list = new ArrayList ();
5506 #if NET_2_0
5507                                                 if (!owner.VirtualMode)
5508 #endif
5509                                                 for (int i = 0; i < owner.Items.Count; i++) {
5510                                                         if (owner.Items [i].Selected)
5511                                                                 list.Add (i);
5512                                                 }
5513                                         }
5514                                         return list;
5515                                 }
5516                         }
5517
5518                         internal void Reset ()
5519                         {
5520                                 // force re-population of list
5521 #if NET_2_0
5522                                 if (!owner.VirtualMode)
5523 #endif
5524                                 list = null;
5525                         }
5526
5527                         private void ItemsCollection_Changed ()
5528                         {
5529                                 Reset ();
5530                         }
5531
5532 #if NET_2_0
5533                         internal void RemoveIndex (int index)
5534                         {
5535                                 int idx = List.BinarySearch (index);
5536                                 if (idx != -1)
5537                                         List.RemoveAt (idx);
5538                         }
5539
5540                         // actually store index in the collection
5541                         // also, keep the collection sorted, as .Net does
5542                         internal void InsertIndex (int index)
5543                         {
5544                                 int iMin = 0;
5545                                 int iMax = List.Count - 1;
5546                                 while (iMin <= iMax) {
5547                                         int iMid = (iMin + iMax) / 2;
5548                                         int current_index = (int) List [iMid];
5549
5550                                         if (current_index == index)
5551                                                 return; // Already added
5552                                         if (current_index > index)
5553                                                 iMax = iMid - 1;
5554                                         else
5555                                                 iMin = iMid + 1;
5556                                 }
5557
5558                                 List.Insert (iMin, index);
5559                         }
5560 #endif
5561
5562                 }       // SelectedIndexCollection
5563
5564 #if NET_2_0
5565                 [ListBindable (false)]
5566 #endif
5567                 public class SelectedListViewItemCollection : IList, ICollection, IEnumerable
5568                 {
5569                         private readonly ListView owner;
5570
5571                         #region Public Constructor
5572                         public SelectedListViewItemCollection (ListView owner)
5573                         {
5574                                 this.owner = owner;
5575                         }
5576                         #endregion      // Public Constructor
5577
5578                         #region Public Properties
5579                         [Browsable (false)]
5580                         public int Count {
5581                                 get {
5582                                         return owner.SelectedIndices.Count;
5583                                 }
5584                         }
5585
5586                         public bool IsReadOnly {
5587                                 get { return true; }
5588                         }
5589
5590                         public ListViewItem this [int index] {
5591                                 get {
5592                                         if (!owner.IsHandleCreated || index < 0 || index >= Count)
5593                                                 throw new ArgumentOutOfRangeException ("index");
5594
5595                                         int item_index = owner.SelectedIndices [index];
5596                                         return owner.Items [item_index];
5597                                 }
5598                         }
5599
5600 #if NET_2_0
5601                         public virtual ListViewItem this [string key] {
5602                                 get {
5603                                         int idx = IndexOfKey (key);
5604                                         if (idx == -1)
5605                                                 return null;
5606
5607                                         return this [idx];
5608                                 }
5609                         }
5610 #endif
5611
5612                         bool ICollection.IsSynchronized {
5613                                 get { return false; }
5614                         }
5615
5616                         object ICollection.SyncRoot {
5617                                 get { return this; }
5618                         }
5619
5620                         bool IList.IsFixedSize {
5621                                 get { return true; }
5622                         }
5623
5624                         object IList.this [int index] {
5625                                 get { return this [index]; }
5626                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
5627                         }
5628                         #endregion      // Public Properties
5629
5630                         #region Public Methods
5631                         public void Clear ()
5632                         {
5633                                 owner.SelectedIndices.Clear ();
5634                         }
5635
5636                         public bool Contains (ListViewItem item)
5637                         {
5638                                 return IndexOf (item) != -1;
5639                         }
5640
5641 #if NET_2_0
5642                         public virtual bool ContainsKey (string key)
5643                         {
5644                                 return IndexOfKey (key) != -1;
5645                         }
5646 #endif
5647
5648                         public void CopyTo (Array dest, int index)
5649                         {
5650                                 if (!owner.IsHandleCreated)
5651                                         return;
5652                                 if (index > Count) // Throws ArgumentException instead of IOOR exception
5653                                         throw new ArgumentException ("index");
5654
5655                                 for (int i = 0; i < Count; i++)
5656                                         dest.SetValue (this [i], index++);
5657                         }
5658
5659                         public IEnumerator GetEnumerator ()
5660                         {
5661                                 if (!owner.IsHandleCreated)
5662                                         return (new ListViewItem [0]).GetEnumerator ();
5663
5664                                 ListViewItem [] items = new ListViewItem [Count];
5665                                 for (int i = 0; i < Count; i++)
5666                                         items [i] = this [i];
5667
5668                                 return items.GetEnumerator ();
5669                         }
5670
5671                         int IList.Add (object value)
5672                         {
5673                                 throw new NotSupportedException ("Add operation is not supported.");
5674                         }
5675
5676                         bool IList.Contains (object item)
5677                         {
5678                                 if (!(item is ListViewItem))
5679                                         return false;
5680                                 return Contains ((ListViewItem) item);
5681                         }
5682
5683                         int IList.IndexOf (object item)
5684                         {
5685                                 if (!(item is ListViewItem))
5686                                         return -1;
5687                                 return IndexOf ((ListViewItem) item);
5688                         }
5689
5690                         void IList.Insert (int index, object value)
5691                         {
5692                                 throw new NotSupportedException ("Insert operation is not supported.");
5693                         }
5694
5695                         void IList.Remove (object value)
5696                         {
5697                                 throw new NotSupportedException ("Remove operation is not supported.");
5698                         }
5699
5700                         void IList.RemoveAt (int index)
5701                         {
5702                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
5703                         }
5704
5705                         public int IndexOf (ListViewItem item)
5706                         {
5707                                 if (!owner.IsHandleCreated)
5708                                         return -1;
5709
5710                                 for (int i = 0; i < Count; i++)
5711                                         if (this [i] == item)
5712                                                 return i;
5713
5714                                 return -1;
5715                         }
5716
5717 #if NET_2_0
5718                         public virtual int IndexOfKey (string key)
5719                         {
5720                                 if (!owner.IsHandleCreated || key == null || key.Length == 0)
5721                                         return -1;
5722
5723                                 for (int i = 0; i < Count; i++) {
5724                                         ListViewItem item = this [i];
5725                                         if (String.Compare (item.Name, key, true) == 0)
5726                                                 return i;
5727                                 }
5728
5729                                 return -1;
5730                         }
5731 #endif
5732                         #endregion      // Public Methods
5733
5734                 }       // SelectedListViewItemCollection
5735
5736                 internal delegate void CollectionChangedHandler ();
5737
5738                 struct ItemMatrixLocation
5739                 {
5740                         int row;
5741                         int col;
5742
5743                         public ItemMatrixLocation (int row, int col)
5744                         {
5745                                 this.row = row;
5746                                 this.col = col;
5747                 
5748                         }
5749                 
5750                         public int Col {
5751                                 get {
5752                                         return col;
5753                                 }
5754                                 set {
5755                                         col = value;
5756                                 }
5757                         }
5758
5759                         public int Row {
5760                                 get {
5761                                         return row;
5762                                 }
5763                                 set {
5764                                         row = value;
5765                                 }
5766                         }
5767         
5768                 }
5769
5770                 #endregion // Subclasses
5771 #if NET_2_0
5772                 protected override void OnResize (EventArgs e)
5773                 {
5774                         base.OnResize (e);
5775                 }
5776
5777                 protected override void OnMouseLeave (EventArgs e)
5778                 {
5779                         base.OnMouseLeave (e);
5780                 }
5781
5782                 //
5783                 // ColumnReorder event
5784                 //
5785                 static object ColumnReorderedEvent = new object ();
5786                 public event ColumnReorderedEventHandler ColumnReordered {
5787                         add { Events.AddHandler (ColumnReorderedEvent, value); }
5788                         remove { Events.RemoveHandler (ColumnReorderedEvent, value); }
5789                 }
5790
5791                 protected virtual void OnColumnReordered (ColumnReorderedEventArgs e)
5792                 {
5793                         ColumnReorderedEventHandler creh = (ColumnReorderedEventHandler) (Events [ColumnReorderedEvent]);
5794
5795                         if (creh != null)
5796                                 creh (this, e);
5797                 }
5798
5799                 //
5800                 // ColumnWidthChanged
5801                 //
5802                 static object ColumnWidthChangedEvent = new object ();
5803                 public event ColumnWidthChangedEventHandler ColumnWidthChanged {
5804                         add { Events.AddHandler (ColumnWidthChangedEvent, value); }
5805                         remove { Events.RemoveHandler (ColumnWidthChangedEvent, value); }
5806                 }
5807
5808                 protected virtual void OnColumnWidthChanged (ColumnWidthChangedEventArgs e)
5809                 {
5810                         ColumnWidthChangedEventHandler eh = (ColumnWidthChangedEventHandler) (Events[ColumnWidthChangedEvent]);
5811                         if (eh != null)
5812                                 eh (this, e);
5813                 }
5814                 
5815                 void RaiseColumnWidthChanged (int resize_column)
5816                 {
5817                         ColumnWidthChangedEventArgs n = new ColumnWidthChangedEventArgs (resize_column);
5818
5819                         OnColumnWidthChanged (n);
5820                 }
5821                 
5822                 //
5823                 // ColumnWidthChanging
5824                 //
5825                 static object ColumnWidthChangingEvent = new object ();
5826                 public event ColumnWidthChangingEventHandler ColumnWidthChanging {
5827                         add { Events.AddHandler (ColumnWidthChangingEvent, value); }
5828                         remove { Events.RemoveHandler (ColumnWidthChangingEvent, value); }
5829                 }
5830
5831                 protected virtual void OnColumnWidthChanging (ColumnWidthChangingEventArgs e)
5832                 {
5833                         ColumnWidthChangingEventHandler cwceh = (ColumnWidthChangingEventHandler) (Events[ColumnWidthChangingEvent]);
5834                         if (cwceh != null)
5835                                 cwceh (this, e);
5836                 }
5837                 
5838                 //
5839                 // 2.0 profile based implementation
5840                 //
5841                 bool CanProceedWithResize (ColumnHeader col, int width)
5842                 {
5843                         ColumnWidthChangingEventHandler cwceh = (ColumnWidthChangingEventHandler) (Events[ColumnWidthChangingEvent]);
5844                         if (cwceh == null)
5845                                 return true;
5846                         
5847                         ColumnWidthChangingEventArgs changing = new ColumnWidthChangingEventArgs (col.Index, width);
5848                         cwceh (this, changing);
5849                         return !changing.Cancel;
5850                 }
5851 #else
5852                 //
5853                 // 1.0 profile based implementation
5854                 //
5855                 bool CanProceedWithResize (ColumnHeader col, int width)
5856                 {
5857                         return true;
5858                 }
5859
5860                 void RaiseColumnWidthChanged (int resize_column)
5861                 {
5862                 }
5863 #endif
5864         }
5865 }