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