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