* Control.cs: Double buffering is handled differently now. As per
[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 //
26 // TODO:
27 //   - Keys to be handled ENTER/PAGE UP/PAGE DOWN/HOME/END/ARROWS/CTRL/SHIFT
28 //   - Item text editing
29 //   - Column resizing/reodering
30 //   - Feedback for item activation, change in cursor types as mouse moves.
31 //   - HideSelection
32 //   - Focused item (broken and not drawn)
33 //   - LabelEdit
34 //   - Manual column resizing
35 //   - Drag and drop
36 //   - Clipping 
37
38
39 // NOT COMPLETE
40
41
42 using System.Collections;
43 using System.ComponentModel;
44 using System.ComponentModel.Design;
45 using System.Drawing;
46 using System.Runtime.InteropServices;
47
48 namespace System.Windows.Forms
49 {
50         [DefaultEvent ("SelectedIndexChanged")]
51         [DefaultProperty ("Items")]
52         [Designer ("System.Windows.Forms.Design.ListViewDesigner, " + Consts.AssemblySystem_Design, (string)null)]
53         public class ListView : Control
54         {
55                 private ItemActivation activation = ItemActivation.Standard;
56                 private ListViewAlignment alignment = ListViewAlignment.Top;
57                 private bool allow_column_reorder = false;
58                 private bool auto_arrange = true;
59                 private bool check_boxes = false;
60                 private CheckedIndexCollection checked_indices;
61                 private CheckedListViewItemCollection checked_items;
62                 private ColumnHeader clicked_column;
63                 private ListViewItem clicked_item;
64                 private ListViewItem last_clicked_item;
65                 private ColumnHeaderCollection columns;
66                 private bool ctrl_pressed;
67                 private bool shift_pressed;
68                 private bool draw_headers = true; // Used for painting. Do we need to draw column headers ?
69                 private ListViewItem focused_item;
70                 private bool full_row_select = false;
71                 private bool grid_lines = false;
72                 private ColumnHeaderStyle header_style = ColumnHeaderStyle.Clickable;
73                 private bool hide_selection = true;
74                 private bool hover_selection = false;
75                 private IComparer item_sorter;
76                 private ListViewItemCollection items;
77                 private bool label_edit = false;
78                 private bool label_wrap = true;
79                 private bool multiselect = true;
80                 private bool redraw = true;
81                 private bool scrollable = true;
82                 private SelectedIndexCollection selected_indices;
83                 private SelectedListViewItemCollection selected_items;
84                 private SortOrder sort_order = SortOrder.None;
85                 private ImageList state_image_list;
86                 private bool updating = false;
87                 private View view = View.LargeIcon;
88                 private int layout_wd;    // We might draw more than our client area
89                 private int layout_ht;    // therefore we need to have these two.
90                 //private TextBox editor;   // Used for editing an item text
91                 private ScrollBar h_scroll; // used for scrolling horizontally
92                 private ScrollBar v_scroll; // used for scrolling vertically
93                 private int h_marker;           // Position markers for scrolling
94                 private int v_marker;
95
96                 // internal variables
97                 internal ImageList large_image_list;
98                 internal ImageList small_image_list;
99                 internal Size text_size = Size.Empty;
100
101                 #region Events
102                 public event LabelEditEventHandler AfterLabelEdit;
103
104                 [Browsable (false)]
105                 [EditorBrowsable (EditorBrowsableState.Never)]
106                 public new event EventHandler BackgroundImageChanged;
107
108                 public event LabelEditEventHandler BeforeLabelEdit;
109                 public event ColumnClickEventHandler ColumnClick;
110                 public event EventHandler ItemActivate;
111                 public event ItemCheckEventHandler ItemCheck;
112                 public event ItemDragEventHandler ItemDrag;
113
114                 [Browsable (false)]
115                 [EditorBrowsable (EditorBrowsableState.Never)]
116                 public new event PaintEventHandler Paint;
117
118                 public event EventHandler SelectedIndexChanged;
119
120                 [Browsable (false)]
121                 [EditorBrowsable (EditorBrowsableState.Never)]
122                 public new event EventHandler TextChanged;
123                 #endregion // Events
124
125                 #region Public Constructors
126                 public ListView ()
127                 {
128                         background_color = ThemeEngine.Current.ColorWindow;
129                         checked_indices = new CheckedIndexCollection (this);
130                         checked_items = new CheckedListViewItemCollection (this);
131                         columns = new ColumnHeaderCollection (this);
132                         foreground_color = SystemColors.WindowText;
133                         items = new ListViewItemCollection (this);
134                         selected_indices = new SelectedIndexCollection (this);
135                         selected_items = new SelectedListViewItemCollection (this);
136
137                         border_style = BorderStyle.Fixed3D;
138
139                         // we are mostly scrollable
140                         h_scroll = new HScrollBar ();
141                         v_scroll = new VScrollBar ();
142                         h_marker = v_marker = 0;
143
144                         // scroll bars are disabled initially
145                         h_scroll.Visible = false;
146                         h_scroll.ValueChanged += new EventHandler(HorizontalScroller);
147                         v_scroll.Visible = false;
148                         v_scroll.ValueChanged += new EventHandler(VerticalScroller);
149
150                         // event handlers
151                         base.DoubleClick += new EventHandler(ListView_DoubleClick);
152                         base.KeyDown += new KeyEventHandler(ListView_KeyDown);
153                         base.KeyUp += new KeyEventHandler(ListView_KeyUp);
154                         base.MouseDown += new MouseEventHandler(ListView_MouseDown);
155                         base.MouseHover += new EventHandler(ListView_MouseHover);
156                         base.MouseUp += new MouseEventHandler(ListView_MouseUp);
157                         base.MouseMove += new MouseEventHandler(ListView_MouseMove);
158                         base.Paint += new PaintEventHandler (ListView_Paint);
159                         
160                         this.SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
161                 }
162                 #endregion      // Public Constructors
163
164                 #region Private Internal Properties
165                 internal Size CheckBoxSize {
166                         get {
167                                 if (this.check_boxes) {
168                                         if (this.state_image_list != null)
169                                                 return this.state_image_list.ImageSize;
170                                         else
171                                                 return ThemeEngine.Current.ListViewCheckBoxSize;
172                                 }
173                                 return Size.Empty;
174                         }
175                 }
176
177                 internal bool CanMultiselect {
178                         get {
179                                 if (this.multiselect &&
180                                         (this.ctrl_pressed || this.shift_pressed))
181                                         return true;
182                                 else
183                                         return false;
184                         }
185                 }
186                 #endregion      // Private Internal Properties
187
188                 #region  Protected Properties
189                 protected override CreateParams CreateParams {
190                         get { return base.CreateParams; }
191                 }
192
193                 protected override Size DefaultSize {
194                         get { return ThemeEngine.Current.ListViewDefaultSize; }
195                 }
196                 #endregion      // Protected Properties
197
198                 #region Public Instance Properties
199                 [DefaultValue (ItemActivation.Standard)]
200                 public ItemActivation Activation {
201                         get { return activation; }
202                         set { 
203                                 if (value != ItemActivation.Standard && value != ItemActivation.OneClick && 
204                                         value != ItemActivation.TwoClick) {
205                                         throw new InvalidEnumArgumentException (string.Format
206                                                 ("Enum argument value '{0}' is not valid for Activation", value));
207                                 }
208                                   
209                                 activation = value;
210                         }
211                 }
212
213                 [DefaultValue (ListViewAlignment.Top)]
214                 [Localizable (true)]
215                 public ListViewAlignment Alignment {
216                         get { return alignment; }
217                         set {
218                                 if (value != ListViewAlignment.Default && value != ListViewAlignment.Left && 
219                                         value != ListViewAlignment.SnapToGrid && value != ListViewAlignment.Top) {
220                                         throw new InvalidEnumArgumentException (string.Format 
221                                                 ("Enum argument value '{0}' is not valid for Alignment", value));
222                                 }
223                                 
224                                 if (this.alignment != value) {
225                                         alignment = value;
226                                         // alignment does not matter in Details/List views
227                                         if (this.view == View.LargeIcon ||
228                                             this.View == View.SmallIcon)
229                                                 this.Redraw (true);
230                                 }
231                         }
232                 }
233
234                 [DefaultValue (false)]
235                 public bool AllowColumnReorder {
236                         get { return allow_column_reorder; }
237                         set {
238                                 if (this.allow_column_reorder != value) {
239                                         allow_column_reorder = value;
240                                         // column reorder does not matter in Details view
241                                         if (this.view != View.Details)
242                                                 this.Redraw (true);
243                                 }
244                         }
245                 }
246
247                 [DefaultValue (true)]
248                 public bool AutoArrange {
249                         get { return auto_arrange; }
250                         set {
251                                 if (auto_arrange != value) {
252                                         auto_arrange = value;
253                                         // autoarrange does not matter in Details/List views
254                                         if (this.view == View.LargeIcon || this.View == View.SmallIcon)
255                                                 this.Redraw (true);
256                                 }
257                         }
258                 }
259
260                 public override Color BackColor {
261                         get {
262                                 if (background_color.IsEmpty)
263                                         return ThemeEngine.Current.ColorWindow;
264                                 else
265                                         return background_color;
266                         }
267                         set { background_color = value; }
268                 }
269
270                 [Browsable (false)]
271                 [EditorBrowsable (EditorBrowsableState.Never)]
272                 public override Image BackgroundImage {
273                         get { return background_image; }
274                         set {
275                                 if (value == background_image)
276                                         return;
277
278                                 background_image = value;
279                                 if (BackgroundImageChanged != null)
280                                         BackgroundImageChanged (this, new EventArgs ());
281                         }
282                 }
283
284                 [DefaultValue (BorderStyle.Fixed3D)]
285                 [DispId (-504)]
286                 public BorderStyle BorderStyle {
287                         get { return border_style; }
288                         set {
289                                 if (value != BorderStyle.Fixed3D && value != BorderStyle.FixedSingle  && 
290                                         value != BorderStyle.None) {
291                                         throw new InvalidEnumArgumentException (string.Format 
292                                                 ("Enum argument value '{0}' is not valid for BorderStyle", value));
293                                 }
294                                 
295                                 if (border_style != value) {
296                                         border_style = value;
297                                         this.Redraw (true);
298                                 }
299                         }
300                 }
301
302                 [DefaultValue (false)]
303                 public bool CheckBoxes {
304                         get { return check_boxes; }
305                         set {
306                                 if (check_boxes != value) {
307                                         check_boxes = value;
308                                         this.Redraw (true);
309                                 }
310                         }
311                 }
312
313                 [Browsable (false)]
314                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
315                 public CheckedIndexCollection CheckedIndices {
316                         get { return checked_indices; }
317                 }
318
319                 [Browsable (false)]
320                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
321                 public CheckedListViewItemCollection CheckedItems {
322                         get { return checked_items; }
323                 }
324
325                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
326                 [Localizable (true)]
327                 [MergableProperty (false)]
328                 public ColumnHeaderCollection Columns {
329                         get { return columns; }
330                 }
331
332                 [Browsable (false)]
333                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
334                 public ListViewItem FocusedItem {
335                         get { return focused_item; }
336                 }
337
338                 public override Color ForeColor {
339                         get {
340                                 if (foreground_color.IsEmpty)
341                                         return ThemeEngine.Current.ColorWindowText;
342                                 else
343                                         return foreground_color;
344                         }
345                         set { foreground_color = value; }
346                 }
347
348                 [DefaultValue (false)]
349                 public bool FullRowSelect {
350                         get { return full_row_select; }
351                         set { full_row_select = value; }
352                 }
353
354                 [DefaultValue (false)]
355                 public bool GridLines {
356                         get { return grid_lines; }
357                         set {
358                                 if (grid_lines != value) {
359                                         grid_lines = value;
360                                         this.Redraw (false);
361                                 }
362                         }
363                 }
364
365                 [DefaultValue (ColumnHeaderStyle.Clickable)]
366                 public ColumnHeaderStyle HeaderStyle {
367                         get { return header_style; }
368                         set {
369                                 if (value != ColumnHeaderStyle.Clickable && value != ColumnHeaderStyle.Nonclickable  && 
370                                         value != ColumnHeaderStyle.None) {
371                                         throw new InvalidEnumArgumentException (string.Format 
372                                                 ("Enum argument value '{0}' is not valid for ColumnHeaderStyle", value));
373                                 }
374                                 
375                                 if (header_style != value) {
376                                         header_style = value;
377                                         // header style matters only in Details view
378                                         if (this.view == View.Details)
379                                                 this.Redraw (false);
380                                 }
381                         }
382                 }
383
384                 [DefaultValue (true)]
385                 public bool HideSelection {
386                         get { return hide_selection; }
387                         set {
388                                 if (hide_selection != value) {
389                                         hide_selection = value;
390                                         this.Redraw (false);
391                                 }
392                         }
393                 }
394
395                 [DefaultValue (false)]
396                 public bool HoverSelection {
397                         get { return hover_selection; }
398                         set { hover_selection = value; }
399                 }
400
401                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
402                 [Localizable (true)]
403                 [MergableProperty (false)]              
404                 public ListViewItemCollection Items {
405                         get { return items; }
406                 }
407
408                 [DefaultValue (false)]
409                 public bool LabelEdit {
410                         get { return label_edit; }
411                         set { label_edit = value; }
412                 }
413
414                 [DefaultValue (true)]
415                 [Localizable (true)]
416                 public bool LabelWrap {
417                         get { return label_wrap; }
418                         set {
419                                 if (label_wrap != value) {
420                                         label_wrap = value;
421                                         this.Redraw (true);
422                                 }
423                         }
424                 }
425
426                 [DefaultValue (null)]
427                 public ImageList LargeImageList {
428                         get { return large_image_list; }
429                         set {
430                                 large_image_list = value;
431                                 this.Redraw (true);
432                         }
433                 }
434
435                 [Browsable (false)]
436                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
437                 public IComparer ListViewItemSorter {
438                         get { return item_sorter; }
439                         set { item_sorter = value; }
440                 }
441
442                 [DefaultValue (true)]
443                 public bool MultiSelect {
444                         get { return multiselect; }
445                         set { multiselect = value; }
446                 }
447
448                 [DefaultValue (true)]
449                 public bool Scrollable {
450                         get { return scrollable; }
451                         set {
452                                 if (scrollable != value) {
453                                         scrollable = value;
454                                         this.Redraw (true);
455                                 }
456                         }
457                 }
458
459                 [Browsable (false)]
460                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
461                 public SelectedIndexCollection SelectedIndices {
462                         get { return selected_indices; }
463                 }
464
465                 [Browsable (false)]
466                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
467                 public SelectedListViewItemCollection SelectedItems {
468                         get { return selected_items; }
469                 }
470
471                 [DefaultValue (null)]
472                 public ImageList SmallImageList {
473                         get { return small_image_list; }
474                         set {
475                                 small_image_list = value;
476                                 this.Redraw (true);
477                         }
478                 }
479
480                 [DefaultValue (SortOrder.None)]
481                 public SortOrder Sorting {
482                         get { return sort_order; }
483                         set { 
484                                 if (value != SortOrder.Ascending && value != SortOrder.Descending  && 
485                                         value != SortOrder.None) {
486                                         throw new InvalidEnumArgumentException (string.Format
487                                                 ("Enum argument value '{0}' is not valid for Sorting", value));
488                                 }
489                                 
490                                 if (sort_order != value)  {                     
491                                         sort_order = value; 
492                                         this.Redraw (false);
493                                 }
494                         }
495                 }
496
497                 [DefaultValue (null)]
498                 public ImageList StateImageList {
499                         get { return state_image_list; }
500                         set {
501                                 state_image_list = value;
502                                 this.Redraw (true);
503                         }
504                 }
505
506                 [Bindable (false)]
507                 [Browsable (false)]
508                 [EditorBrowsable (EditorBrowsableState.Never)]
509                 public override string Text {
510                         get { return text; } 
511                         set {
512                                 if (value == text)
513                                         return;
514
515                                 text = value;
516                                 this.Redraw (true);
517
518                                 if (TextChanged != null)
519                                         TextChanged (this, new EventArgs ());
520                         }
521                 }
522
523                 [Browsable (false)]
524                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
525                 public ListViewItem TopItem {
526                         get {
527                                 // there is no item
528                                 if (this.items.Count == 0)
529                                         return null;
530                                 // if contents are not scrolled
531                                 // it is the first item
532                                 else if (h_marker == 0 && v_marker == 0)
533                                         return this.items [0];
534                                 // do a hit test for the scrolled position
535                                 else {
536                                         foreach (ListViewItem item in this.items) {
537                                                 if (item.EntireRect.Contains (h_marker, v_marker))
538                                                         return item;
539                                         }
540                                         return null;
541                                 }
542                         }
543                 }
544
545                 [DefaultValue (View.LargeIcon)]
546                 public View View {
547                         get { return view; }
548                         set { 
549                                 if (value != View.Details && value != View.LargeIcon  && 
550                                         value != View.List  && value != View.SmallIcon  ) {
551                                         throw new InvalidEnumArgumentException (string.Format
552                                                 ("Enum argument value '{0}' is not valid for View", value));
553                                 }
554                                 
555                                 if (view != value) {
556                                         view = value; 
557                                         Redraw (true);
558                                 }
559                         }
560                 }
561                 #endregion      // Public Instance Properties
562
563                 #region Internal Methods Properties
564                 internal int TotalWidth {
565                         get { return Math.Max (this.Width, this.layout_wd); }
566                 }
567
568                 internal int TotalHeight {
569                         get { return Math.Max (this.Height, this.layout_ht); }
570                 }
571
572                 internal void Redraw (bool recalculate)
573                 {
574                         // Avoid calculations when control is being updated
575                         if (this.updating)
576                                 return;
577
578                         if (recalculate)
579                                 CalculateListView (this.alignment);
580
581                         redraw = true;
582                         Refresh ();
583                 }
584
585                 internal Size GetChildColumnSize (int index)
586                 {
587                         Size ret_size = Size.Empty;
588                         ColumnHeader col = this.columns [index];
589
590                         if (col.Width == -2) { // autosize = max(items, columnheader)
591                                 Size size = Size.Ceiling (this.DeviceContext.MeasureString
592                                                           (col.Text, this.Font));
593                                 ret_size = BiggestItem (index);
594                                 if (size.Width > ret_size.Width)
595                                         ret_size = size;
596                         }
597                         else { // -1 and all the values < -2 are put under one category
598                                 ret_size = BiggestItem (index);
599                                 // fall back to empty columns' width if no subitem is available for a column
600                                 if (ret_size.IsEmpty) {
601                                         ret_size.Width = ThemeEngine.Current.ListViewEmptyColumnWidth;
602                                         if (col.Text.Length > 0)
603                                                 ret_size.Height = Size.Ceiling (this.DeviceContext.MeasureString
604                                                                                 (col.Text, this.Font)).Height;
605                                         else
606                                                 ret_size.Height = this.Font.Height;
607                                 }
608                         }
609
610                         // adjust the size for icon and checkbox for 0th column
611                         if (index == 0) {
612                                 ret_size.Width += (this.CheckBoxSize.Width + 4);
613                                 if (this.small_image_list != null)
614                                         ret_size.Width += this.small_image_list.ImageSize.Width;
615                         }
616                         return ret_size;
617                 }
618
619                 // Returns the size of biggest item text in a column.
620                 private Size BiggestItem (int col)
621                 {
622                         Size temp = Size.Empty;
623                         Size ret_size = Size.Empty;
624
625                         // 0th column holds the item text, we check the size of
626                         // the various subitems falling in that column and get
627                         // the biggest one's size.
628                         foreach (ListViewItem item in items) {
629                                 if (col >= item.SubItems.Count)
630                                         continue;
631
632                                 temp = Size.Ceiling (this.DeviceContext.MeasureString
633                                                      (item.SubItems [col].Text, this.Font));
634                                 if (temp.Width > ret_size.Width)
635                                         ret_size = temp;
636                         }
637
638                         // adjustment for space
639                         if (!ret_size.IsEmpty)
640                                 ret_size.Width += 4;
641
642                         return ret_size;
643                 }
644
645                 // Sets the size of the biggest item text as per the view
646                 private void CalcTextSize ()
647                 {                       
648                         // clear the old value
649                         text_size = Size.Empty;
650
651                         if (items.Count == 0)
652                                 return;
653
654                         text_size = BiggestItem (0);
655
656                         if (view == View.LargeIcon && this.label_wrap) {
657                                 Size temp = Size.Empty;
658                                 if (this.check_boxes)
659                                         temp.Width += 2 * this.CheckBoxSize.Width;
660                                 if (large_image_list != null)
661                                         temp.Width += large_image_list.ImageSize.Width;
662                                 if (temp.Width == 0)
663                                         temp.Width = 43;
664                                 // wrapping is done for two lines only
665                                 if (text_size.Width > temp.Width) {
666                                         text_size.Width = temp.Width;
667                                         text_size.Height *= 2;
668                                 }
669                         }
670                         else if (view == View.List) {
671                                 // in list view max text shown in determined by the
672                                 // control width, even if scolling is enabled.
673                                 int max_wd = this.Width - (this.CheckBoxSize.Width - 2);
674                                 if (this.small_image_list != null)
675                                         max_wd -= this.small_image_list.ImageSize.Width;
676
677                                 if (text_size.Width > max_wd)
678                                         text_size.Width = max_wd;
679                         }
680
681                         // we do the default settings, if we have got 0's
682                         if (text_size.Height <= 0)
683                                 text_size.Height = this.Font.Height;
684                         if (text_size.Width <= 0)
685                                 text_size.Width = this.Width;
686
687                         // little adjustment
688                         text_size.Width += 4;
689                         text_size.Height += 2;
690                 }
691
692                 // Sets the location of every item on
693                 // the ListView as per the view
694                 private void CalculateListView (ListViewAlignment align)
695                 {
696                         int current_pos_x = 0; // our x-position marker
697                         int current_pos_y = 0; // our y-position marker
698                         int item_ht;
699                         int item_wd;
700                         int max;         // max x_pos or y_pos depending on the alignment
701                         int current = 0; // current row or column
702                         int vertical_spacing = ThemeEngine.Current.ListViewVerticalSpacing;
703                         int horizontal_spacing = ThemeEngine.Current.ListViewHorizontalSpacing;
704
705                         CalcTextSize ();
706
707                         switch (view) {
708
709                         case View.Details:
710                                 // ColumnHeaders are not drawn if headerstyle is none
711                                 int ht = (this.header_style == ColumnHeaderStyle.None) ? 
712                                         2 : this.Font.Height + 5;
713                                 if (columns.Count > 0) {
714                                         foreach (ColumnHeader col in columns) {
715                                                 col.X = current_pos_x;
716                                                 col.Y = 0;
717                                                 col.CalcColumnHeader ();
718                                                 current_pos_x += col.Wd;
719                                         }
720                                         this.layout_wd = current_pos_x;
721                                 }
722                                 // set the position marker for placing items
723                                 // vertically down
724                                 current_pos_y = ht;
725
726                                 if (items.Count > 0) {
727                                         foreach (ListViewItem item in items) {
728                                                 item.location.X = 0;
729                                                 item.location.Y = current_pos_y;
730                                                 item.CalcListViewItem ();
731                                                 current_pos_y += item.EntireRect.Height;
732                                         }
733                                         this.layout_ht = current_pos_y;
734
735                                         // some space for bottom gridline
736                                         if (this.grid_lines)
737                                                 this.layout_ht += 2;
738                                 }
739                                 break;
740
741                         case View.SmallIcon:
742                                 vertical_spacing = 0;
743                                 horizontal_spacing = 0;
744                                 goto case View.LargeIcon;
745
746                         case View.LargeIcon:
747                                 if (items.Count > 0) {
748                                         items [0].CalcListViewItem ();
749                                         item_ht = items [0].EntireRect.Height;
750                                         item_wd = items [0].EntireRect.Width;
751
752                                         // top (default) and snaptogrid alignments are handled same way
753                                         if (align == ListViewAlignment.Left) {
754                                                 max = this.Height;
755                                                 foreach (ListViewItem item in items) {
756                                                         item.location.X = current_pos_x +
757                                                                 horizontal_spacing;
758                                                         item.location.Y = current_pos_y;
759                                                         item.CalcListViewItem ();
760                                                         current_pos_y += item_ht;
761
762                                                         current ++; // just to know about the last element
763                                                         // we just did the last item
764                                                         if (current == items.Count) {
765                                                                 if (max < current_pos_y)
766                                                                         max = current_pos_y;
767                                                                 current_pos_x = item.EntireRect.Right;
768                                                                 break;
769                                                         }
770                                                         else {
771                                                                 // is there enough space for another row ?
772                                                                 if ((current_pos_y + vertical_spacing
773                                                                      + item_ht) <= this.Height)
774                                                                         current_pos_y += vertical_spacing;
775                                                                 else {
776                                                                         // start another column
777                                                                         // make current_pos_y as the
778                                                                         // max value and reset
779                                                                         // current_pos_y value.
780                                                                         max = current_pos_y;
781                                                                         current_pos_x += item_wd;
782                                                                         current_pos_y = 0;
783                                                                 }
784                                                         }
785                                                 }
786                                                 // adjust the layout dimensions
787                                                 this.layout_ht = max;
788                                                 this.layout_wd = current_pos_x;
789                                         }
790                                         else { // other default/top alignment
791                                                 max = this.Width;
792                                                 foreach (ListViewItem item in items) {
793                                                         item.location.X = current_pos_x +
794                                                                 horizontal_spacing;
795
796                                                         item.location.Y = current_pos_y;
797                                                         item.CalcListViewItem ();
798                                                         current_pos_x += item_wd;
799
800                                                         current ++; // just to know about the last element
801                                                         // we just did the last item
802                                                         if (current == items.Count) {
803                                                                 if (max < current_pos_x)
804                                                                         max = current_pos_x;
805                                                                 current_pos_y = item.EntireRect.Bottom;
806                                                                 break;
807                                                         }
808                                                         else {
809                                                                 // is there enough space for another column?
810                                                                 if ((current_pos_x + horizontal_spacing
811                                                                      + item_wd) <= this.Width)
812                                                                         continue;
813                                                                 else {
814                                                                         // start another row
815                                                                         // make current_pos_x as the
816                                                                         // max value and reset
817                                                                         // current_pos_x value.
818                                                                         max = current_pos_x;
819                                                                         current_pos_y += (item_ht +
820                                                                                           vertical_spacing);
821                                                                         current_pos_x = 0;
822                                                                 }
823                                                         }
824                                                 }
825                                                 // adjust the layout dimensions
826                                                 this.layout_wd = max;
827                                                 this.layout_ht = current_pos_y;
828                                         }
829                                 }
830                                 break;
831
832                         case View.List:
833                                 if (items.Count > 0) {
834                                         items [0].CalcListViewItem ();
835                                         item_ht = items [0].EntireRect.Height;
836                                         item_wd = items [0].EntireRect.Width;
837
838                                         max = this.Height / item_ht;
839                                         if (max == 0)
840                                                 max = 1; // we draw at least one row
841
842                                         foreach (ListViewItem item in items) {
843                                                 item.location.X = current_pos_x;
844                                                 item.location.Y = current_pos_y;
845                                                 item.CalcListViewItem ();
846                                                 current ++;
847                                                 if (current == max) {
848                                                         current_pos_x += item_wd;
849                                                         current_pos_y = 0;
850                                                         current = 0;
851                                                 }
852                                                 else
853                                                         current_pos_y += item_ht;
854                                         }
855
856                                         // adjust the layout dimensions
857                                         this.layout_ht = max * item_ht;
858                                         if (current == 0) // we have fully filled layout
859                                                 this.layout_wd = current_pos_x;
860                                         else
861                                                 this.layout_wd = current_pos_x + item_wd;
862                                 }
863                                 break;
864                         }
865
866                         if (this.scrollable && this.items.Count > 0) {
867                                 // making a scroll bar visible might make
868                                 // other scroll bar visible
869                                 if (this.layout_wd > this.Width) {
870                                         this.h_scroll.Visible = true;
871                                         if ((this.layout_ht + this.h_scroll.Height) > this.Height)
872                                                 this.v_scroll.Visible = true;
873                                 }
874                                 else if (this.layout_ht > this.Height) {
875                                         this.v_scroll.Visible = true;
876                                         if ((this.layout_wd + this.v_scroll.Width) > this.Width)
877                                                 this.h_scroll.Visible = true;
878                                 }
879
880                                 // create big enough buffers
881                                 if (this.layout_wd > this.Width ||
882                                     this.layout_ht > this.Height)
883                                         this.CreateBuffers (this.TotalWidth, this.TotalHeight);
884
885                                 if (this.h_scroll.Visible) {
886                                         this.h_scroll.Location = new Point (0, this.Height 
887                                                                             - this.h_scroll.Height);
888                                         
889                                         this.h_scroll.Minimum = 0;
890
891                                         // if v_scroll is visible, adjust the maximum of the
892                                         // h_scroll to account for the width of v_scroll
893                                         if (this.v_scroll.Visible) {
894                                                 this.h_scroll.Maximum = this.layout_wd + this.v_scroll.Width;
895                                                 this.h_scroll.Width = this.Width - this.v_scroll.Width;
896                                         }
897                                         else {
898                                                 this.h_scroll.Maximum = this.layout_wd;
899                                                 this.h_scroll.Width = this.Width;
900                                         }
901    
902                                         this.h_scroll.LargeChange = this.Width;
903                                         this.h_scroll.SmallChange = this.Font.Height;
904                                 }
905
906                                 // vertical scrollbar
907                                 if (this.v_scroll.Visible) {
908                                         this.v_scroll.Location = new Point (this.Width 
909                                                                             - this.v_scroll.Width, 0);
910
911                                         this.v_scroll.Minimum = 0;
912
913                                         // if h_scroll is visible, adjust the maximum of the
914                                         // v_scroll to account for the height of h_scroll
915                                         if (this.h_scroll.Visible) {
916                                                 this.v_scroll.Maximum = this.layout_ht + this.h_scroll.Height;
917                                                 this.v_scroll.Height = this.Height - this.h_scroll.Height;
918                                         }
919                                         else {
920                                                 this.v_scroll.Maximum = this.layout_ht;
921                                                 this.v_scroll.Height = this.Height;
922                                         }
923
924                                         this.v_scroll.LargeChange = this.Height;
925                                         this.v_scroll.SmallChange = this.Font.Height;
926                                 }
927                         }
928                         else {
929                                 this.h_scroll.Visible = false;
930                                 this.v_scroll.Visible = false;
931                         }
932                 }               
933                 
934
935                 // Event Handlers
936                 private void ListView_DoubleClick (object sender, EventArgs e)
937                 {
938                         if (this.activation == ItemActivation.Standard
939                             && this.ItemActivate != null)
940                                 this.ItemActivate (this, e);
941                 }
942
943                 private void ListView_KeyDown (object sender, KeyEventArgs ke)
944                 {
945                         int index = -1;
946                         if (ke.Handled)
947                                 return;
948
949                         ke.Handled = true;
950
951                         switch (ke.KeyCode) {
952
953                         case Keys.ControlKey:
954                                 this.ctrl_pressed = true;
955                                 break;
956
957                         case Keys.Down:                         
958                                 // FIXME:TODO
959                                 break;
960
961                         case Keys.End:
962                                 this.v_scroll.Value = this.v_scroll.Maximum;
963                                 break;
964
965                         case Keys.Home:
966                                 this.v_scroll.Value = this.v_scroll.Minimum;
967                                 break;
968
969                         case Keys.Left:
970                                 index = -1;
971                                 if (this.last_clicked_item != null)
972                                         index = this.last_clicked_item.Index;
973                                 else
974                                         break;
975
976                                 if (index > 0)
977                                         index -= 1;
978
979                                 this.last_clicked_item = this.items [index];
980                                 this.last_clicked_item.Selected = true;
981                                 this.EnsureVisible (index);
982                                 break;
983
984                         case Keys.Right:
985                                 if (this.last_clicked_item != null)
986                                         index = this.last_clicked_item.Index + 1;
987                                 else
988                                         index = 1;
989
990                                 if (index == this.items.Count)
991                                         break;
992
993                                 this.last_clicked_item = this.items [index];
994                                 this.last_clicked_item.Selected = true;
995                                 this.EnsureVisible (index);
996                                 break;
997
998                         case Keys.ShiftKey:
999                                 this.shift_pressed = true;
1000                                 break;
1001
1002                         case Keys.Up:                           
1003                                 // FIXME:TODO
1004                                 break;
1005
1006                         default:
1007                                 ke.Handled = false;
1008                                 break;
1009                         }
1010                 }
1011
1012                 private void ListView_KeyUp (object sender, KeyEventArgs ke)
1013                 {
1014                         if (!ke.Handled) {
1015                                 if (ke.KeyCode == Keys.ControlKey)
1016                                         this.ctrl_pressed = false;
1017
1018                                 if (ke.KeyCode == Keys.ShiftKey)
1019                                         this.shift_pressed = false;
1020                                 ke.Handled = true;
1021                         }
1022                 }
1023
1024                 private void ListView_MouseDown (object sender, MouseEventArgs me)
1025                 {
1026                         if (items.Count == 0)
1027                                 return;
1028
1029                         Point hit = Point.Empty;
1030                         if (this.HeaderStyle != ColumnHeaderStyle.None) {
1031                                 // take horizontal scrolling into account
1032                                 hit = new Point (me.X + h_marker, me.Y);
1033
1034                                 // hit test on columns
1035                                 if (this.view == View.Details && this.columns.Count > 0) {
1036                                         foreach (ColumnHeader col in this.columns) {
1037                                                 if (col.Rect.Contains (hit)) {
1038                                                         this.clicked_column = col;
1039                                                         this.Capture = true;
1040                                                         break;
1041                                                 }
1042                                         }
1043
1044                                         if (this.clicked_column != null) {
1045                                                 this.clicked_column.pressed = true;
1046                                                 this.draw_headers = true;
1047                                                 this.Redraw (false);
1048                                                 return;
1049                                         }
1050                                 }
1051                         }
1052
1053                         // hit test on items
1054                         // we need to take scrolling into account
1055                         hit = new Point (me.X + h_marker, me.Y + v_marker);
1056                         foreach (ListViewItem item in this.items) {
1057                                 if (item.CheckRect.Contains (hit)) {
1058                                         CheckState curr_state = item.Checked ?
1059                                                 CheckState.Checked : CheckState.Unchecked;
1060                                         if (item.Checked)
1061                                                 item.Checked = false;
1062                                         else
1063                                                 item.Checked = true;
1064
1065                                         CheckState new_state = item.Checked ?
1066                                                 CheckState.Checked : CheckState.Unchecked;
1067                                         this.Redraw (false);
1068
1069                                         // Raise the ItemCheck event
1070                                         ItemCheckEventArgs ice = new ItemCheckEventArgs (item.Index,
1071                                                                                          curr_state,
1072                                                                                          new_state);
1073                                         this.OnItemCheck (ice);
1074                                         break;
1075                                 }
1076
1077                                 if (this.view == View.Details &&
1078                                     this.FullRowSelect == false) {
1079                                         if (item.LabelRect.Contains (hit)) {
1080                                                 this.clicked_item = item;
1081                                                 break;
1082                                         }
1083                                 }
1084                                 else {
1085                                         if (item.EntireRect.Contains (hit)) {
1086                                                 this.clicked_item = item;
1087                                                 break;
1088                                         }
1089                                 }
1090                         }
1091                         
1092                         // set the FocusedItem to be the current clicked_item
1093                         this.focused_item = this.clicked_item;
1094
1095                         if (this.clicked_item != null) {
1096                                 this.clicked_item.Selected = true;
1097                                 // Raise the event
1098                                 this.OnSelectedIndexChanged (new EventArgs ());
1099
1100                                 this.Redraw (false);
1101                         }               
1102                 }
1103
1104                 private void ListView_MouseHover (object sender, EventArgs e)
1105                 {
1106                         // handle the hover events only when the mouse
1107                         // is not captured.
1108                         if (this.hover_selection == false || this.Capture)
1109                                 return;
1110
1111                         // hit test for the items
1112                         Point hit = this.PointToClient (Control.MousePosition);
1113                         ListViewItem item = this.GetItemAt (hit.X, hit.Y);
1114
1115                         if (item != null) {
1116                                 item.Selected = true;
1117                                 // Raise the event
1118                                 this.OnSelectedIndexChanged (new EventArgs ());
1119
1120                                 this.Redraw (false);
1121                         }
1122                 }
1123
1124                 private void ListView_MouseMove (object sender, MouseEventArgs me)
1125                 {
1126                         // Column header is always at the top. It can
1127                         // scroll only horizontally. So, we have to take
1128                         // only horizontal scrolling into account
1129                         Point hit = new Point (me.X + h_marker, me.Y);
1130
1131                         // non-null clicked_col means mouse down has happened
1132                         // on a column
1133                         if (this.clicked_column != null) {
1134                                 if (this.clicked_column.pressed == false &&
1135                                     this.clicked_column.Rect.Contains (hit)) {
1136                                         this.clicked_column.pressed = true;
1137                                         this.draw_headers = true;
1138                                         this.Redraw (false);
1139                                 }
1140                                 else if (this.clicked_column.pressed && 
1141                                          ! this.clicked_column.Rect.Contains (hit)) {
1142                                         this.clicked_column.pressed = false;
1143                                         this.draw_headers = true;
1144                                         this.Redraw (false);
1145                                 }
1146                         }
1147                 }
1148
1149                 private void ListView_MouseUp (object sender, MouseEventArgs me)
1150                 {
1151                         this.Capture = false;
1152                         if (items.Count == 0)
1153                                 return;
1154
1155                         Point hit = new Point (me.X, me.Y);
1156
1157                         if (this.clicked_column != null) {
1158                                 if (this.clicked_column.pressed) {
1159                                         this.clicked_column.pressed = false;
1160                                         this.draw_headers = true;
1161                                         this.Redraw (false);
1162
1163                                         // Raise the ColumnClick event
1164                                         this.OnColumnClick (new ColumnClickEventArgs
1165                                                             (this.clicked_column.Index));
1166                                 }
1167                         }
1168
1169                         // Raise the ItemActivate event
1170                         Rectangle rect = Rectangle.Empty;
1171                         if (this.clicked_item != null) {
1172                                 if (this.view == View.Details && !this.full_row_select)
1173                                         rect = this.clicked_item.LabelRect;
1174                                 else
1175                                         rect = this.clicked_item.EntireRect;
1176
1177                                 // We handle double click in a separate handler
1178                                 if (this.activation != ItemActivation.Standard &&
1179                                     rect.Contains (hit)) {
1180                                         if (this.activation == ItemActivation.OneClick)
1181                                                 this.ItemActivate (this, EventArgs.Empty);
1182
1183                                         // ItemActivate is raised on the second click on the same item
1184                                         else if (this.activation == ItemActivation.TwoClick) {
1185                                                 if (this.last_clicked_item == this.clicked_item) {
1186                                                         this.ItemActivate (this, EventArgs.Empty);
1187                                                         this.last_clicked_item = null;
1188                                                 }
1189                                                 else
1190                                                         this.last_clicked_item = this.clicked_item;
1191                                         }
1192                                 }
1193                         }
1194
1195                         this.clicked_column = null;
1196                         this.clicked_item = null;
1197                 }
1198
1199                 private void ListView_Paint (object sender, PaintEventArgs pe)
1200                 {
1201                         if (this.Width <= 0 || this.Height <=  0 ||
1202                             this.Visible == false || this.updating == true)
1203                                 return;
1204
1205                         if (redraw) {
1206                                 ThemeEngine.Current.DrawListView (pe.Graphics,
1207                                                                   pe.ClipRectangle, this);
1208                                 redraw = false;
1209                         }
1210
1211                         // We paint on the screen as per the location set
1212                         // by the two scrollbars. In case of details view
1213                         // since column headers can scroll only horizontally
1214                         // and items can scroll in both directions, paiting is
1215                         // done separtely for the column header and the items.
1216
1217                         Rectangle srcRect = this.ClientRectangle;
1218                         Rectangle dstRect = this.ClientRectangle;
1219
1220                         // set the visible starting point
1221                         if (scrollable) {
1222                                 srcRect.X += h_marker;
1223                                 srcRect.Y += v_marker;
1224
1225                                 if (h_scroll.Visible) {
1226                                         srcRect.Height -= h_scroll.Height;
1227                                         dstRect.Height -= h_scroll.Height;
1228                                 }
1229
1230                                 if (v_scroll.Visible) {
1231                                         srcRect.Width -= v_scroll.Width;
1232                                         dstRect.Width -= v_scroll.Width;
1233                                 }
1234                         }
1235
1236                         // We paint the column headers always at the top, in case
1237                         // of vertical scrolling. Therefore, we advance the painting
1238                         // by the amount equal to the column height.
1239                         /*
1240                         if (this.view == View.Details &&
1241                             this.Columns.Count > 0 &&
1242                             this.header_style != ColumnHeaderStyle.None &&
1243                             v_marker > 0 ) {
1244
1245                                 int col_ht = this.Columns [0].Ht;
1246
1247                                 if (this.draw_headers) {
1248                                         this.draw_headers = false;
1249                                         // Move the source rect by the amount of horizontal
1250                                         // scrolling done so far.
1251                                         Rectangle headerSrc = new Rectangle (h_marker, 0,
1252                                                                              srcRect.Width, col_ht);
1253                                         // dest rect is always stable at 0,0
1254                                         Rectangle headerDst = new Rectangle (0, 0, srcRect.Width, col_ht);
1255                                         pe.Graphics.DrawImage (this.ImageBuffer, headerDst,
1256                                                                headerSrc, GraphicsUnit.Pixel);
1257                                 }
1258
1259                                 dstRect.Y += col_ht;
1260                                 srcRect.Y += col_ht;
1261                         }
1262                         */
1263                         
1264                         // Draw the border of the list view
1265                         // The border is painted here separately, because
1266                         // our imagebuffer might be scrollable
1267                         ThemeEngine.Current.CPDrawBorderStyle (pe.Graphics,
1268                                                                this.ClientRectangle,
1269                                                                this.BorderStyle);
1270
1271                         // Raise the Paint event
1272                         if (Paint != null)
1273                                 Paint (this, pe);
1274                 }
1275
1276                 private void HorizontalScroller (object sender, EventArgs e)
1277                 {
1278                         // Avoid unnecessary flickering, when button is
1279                         // kept pressed at the end
1280                         if (h_marker != h_scroll.Value) {
1281                                 h_marker = h_scroll.Value;
1282                                 // draw the headers again
1283                                 this.draw_headers = true;
1284                                 this.Refresh ();
1285                         }
1286                 }
1287
1288                 private void VerticalScroller (object sender, EventArgs e)
1289                 {
1290                         // Avoid unnecessary flickering, when button is
1291                         // kept pressed at the end
1292                         if (v_marker != v_scroll.Value) {
1293                                 v_marker = v_scroll.Value;
1294                                 this.Refresh ();
1295                         }
1296                 }
1297                 #endregion      // Internal Methods Properties
1298
1299                 #region Protected Methods
1300                 protected override void CreateHandle ()
1301                 {
1302                         base.CreateHandle ();
1303                 }
1304
1305                 protected override void Dispose (bool disposing)
1306                 {
1307                         base.Dispose (disposing);
1308                 }
1309
1310                 protected override bool IsInputKey (Keys keyData)
1311                 {
1312                         return base.IsInputKey (keyData);
1313                 }
1314
1315                 protected virtual void OnAfterLabelEdit (LabelEditEventArgs e)
1316                 {
1317                         if (AfterLabelEdit != null)
1318                                 AfterLabelEdit (this, e);
1319                 }
1320
1321                 protected virtual void OnBeforeLabelEdit (LabelEditEventArgs e)
1322                 {
1323                         if (BeforeLabelEdit != null)
1324                                 BeforeLabelEdit (this, e);
1325                 }
1326
1327                 protected virtual void OnColumnClick (ColumnClickEventArgs e)
1328                 {
1329                         if (ColumnClick != null)
1330                                 ColumnClick (this, e);
1331                 }
1332
1333                 protected override void OnEnabledChanged (EventArgs e)
1334                 {
1335                         base.OnEnabledChanged (e);
1336                 }
1337
1338                 protected override void OnFontChanged (EventArgs e)
1339                 {
1340                         base.OnFontChanged (e);
1341                         Redraw (true);
1342                 }
1343
1344                 protected override void OnHandleCreated (EventArgs e)
1345                 {
1346                         base.OnHandleCreated (e);
1347                         this.Controls.Add (this.v_scroll);
1348                         this.Controls.Add (this.h_scroll);                      
1349                 }
1350
1351                 protected override void OnHandleDestroyed (EventArgs e)
1352                 {
1353                         base.OnHandleDestroyed (e);
1354                 }
1355
1356                 protected virtual void OnItemActivate (EventArgs e)
1357                 {
1358                         if (ItemActivate != null)
1359                                 ItemActivate (this, e);
1360                 }
1361
1362                 protected virtual void OnItemCheck (ItemCheckEventArgs ice)
1363                 {
1364                         if (ItemCheck != null)
1365                                 ItemCheck (this, ice);
1366                 }
1367
1368                 protected virtual void OnItemDrag (ItemDragEventArgs e)
1369                 {
1370                         if (ItemDrag != null)
1371                                 ItemDrag (this, e);
1372                 }
1373
1374                 protected virtual void OnSelectedIndexChanged (EventArgs e)
1375                 {
1376                         if (SelectedIndexChanged != null)
1377                                 SelectedIndexChanged (this, e);
1378                 }
1379
1380                 protected override void OnSystemColorsChanged (EventArgs e)
1381                 {
1382                         base.OnSystemColorsChanged (e);
1383                 }
1384
1385                 protected void RealizeProperties ()
1386                 {
1387                         // FIXME: TODO
1388                 }
1389
1390                 protected void UpdateExtendedStyles ()
1391                 {
1392                         // FIXME: TODO
1393                 }
1394
1395                 protected override void WndProc (ref Message m)
1396                 {
1397                         base.WndProc (ref m);
1398                 }
1399                 #endregion // Protected Methods
1400
1401                 #region Public Instance Methods
1402                 public void ArrangeIcons ()
1403                 {
1404                         ArrangeIcons (this.alignment);
1405                 }
1406
1407                 public void ArrangeIcons (ListViewAlignment alignment)
1408                 {
1409                         // Icons are arranged only if view is set to LargeIcon or SmallIcon
1410                         if (view == View.LargeIcon || view == View.SmallIcon) {
1411                                 this.CalculateListView (alignment);
1412                                 // we have done the calculations already
1413                                 this.Redraw (false);
1414                         }
1415                 }
1416
1417                 public void BeginUpdate ()
1418                 {
1419                         // flag to avoid painting
1420                         updating = true;
1421                 }
1422
1423                 public void Clear ()
1424                 {
1425                         columns.Clear ();
1426                         items.Clear ();
1427                         this.Redraw (true);
1428                 }
1429
1430                 public void EndUpdate ()
1431                 {
1432                         // flag to avoid painting
1433                         updating = false;
1434
1435                         // probably, now we need a redraw with recalculations
1436                         this.Redraw (true);
1437                 }
1438
1439                 public void EnsureVisible (int index)
1440                 {
1441                         if (index < 0 || index >= this.items.Count || this.scrollable == false)
1442                                 return;
1443
1444                         // dimensions of visible area
1445                         int view_wd = this.Width - (this.v_scroll.Visible ? this.v_scroll.Width : 0);
1446                         int view_ht = this.Height - (this.h_scroll.Visible ? this.h_scroll.Height : 0);
1447                         // visible area is decided by the h_marker and v_marker
1448                         Rectangle view_rect = new Rectangle (h_marker, v_marker, view_wd, view_ht);
1449
1450                         // an item's bounding rect
1451                         Rectangle rect = this.items [index].EntireRect;
1452
1453                         // we don't need to do anything if item is visible.
1454                         // visible area is represented by (0,0,view_wd,view_ht)
1455                         if (view_rect.Contains (rect))
1456                                 return;
1457
1458                         // Scroll Left or Up
1459                         if ((rect.Left < view_rect.Left) || (rect.Top < view_rect.Top)) {
1460                                 if (rect.Left < view_rect.Left)
1461                                         this.h_scroll.Value -= (view_rect.Left - rect.Left);
1462                                 if (rect.Top < view_rect.Top)
1463                                         this.v_scroll.Value -= (view_rect.Top - rect.Top);
1464                         }
1465                         // Scroll Right or Down
1466                         else {
1467                                 if (rect.Right > view_rect.Right)
1468                                         this.h_scroll.Value += (rect.Right - view_rect.Right);
1469                                 if (rect.Bottom > view_rect.Bottom)
1470                                         this.v_scroll.Value += (rect.Bottom - view_rect.Bottom);
1471                         }
1472                 }
1473                 
1474                 public ListViewItem GetItemAt (int x, int y)
1475                 {
1476                         foreach (ListViewItem item in items) {
1477                                 if (item.Bounds.Contains (x, y))
1478                                         return item;
1479                         }
1480                         return null;
1481                 }
1482
1483                 public Rectangle GetItemRect (int index)
1484                 {
1485                         return GetItemRect (index, ItemBoundsPortion.Entire);
1486                 }
1487
1488                 public Rectangle GetItemRect (int index, ItemBoundsPortion portion)
1489                 {
1490                         if (index < 0 || index >= items.Count)
1491                                 throw new IndexOutOfRangeException ("Invalid Index");
1492
1493                         return items [index].GetBounds (portion);
1494                 }
1495
1496                 public void Sort ()
1497                 {
1498                         if (sort_order != SortOrder.None)
1499                                 items.list.Sort (item_sorter);
1500
1501                         if (sort_order == SortOrder.Descending)
1502                                 items.list.Reverse ();
1503
1504                         this.Redraw (true);
1505                 }
1506
1507                 public override string ToString ()
1508                 {
1509                         int count = this.Items.Count;
1510
1511                         if (count == 0)
1512                                 return string.Format ("System.Windows.Forms.ListView, Items.Count: 0");
1513                         else
1514                                 return string.Format ("System.Windows.Forms.ListView, Items.Count: {0}, Items[0]: {1}", count, this.Items [0].ToString ());
1515                 }
1516                 #endregion      // Public Instance Methods
1517
1518
1519                 #region Subclasses
1520                 public class CheckedIndexCollection : IList, ICollection, IEnumerable
1521                 {
1522                         internal ArrayList list;
1523                         private ListView owner;
1524
1525                         #region Public Constructor
1526                         public CheckedIndexCollection (ListView owner)
1527                         {
1528                                 list = new ArrayList ();
1529                                 this.owner = owner;
1530                         }
1531                         #endregion      // Public Constructor
1532
1533                         #region Public Properties
1534                         [Browsable (false)]
1535                         public virtual int Count {
1536                                 get { return list.Count; }
1537                         }
1538
1539                         public virtual bool IsReadOnly {
1540                                 get { return true; }
1541                         }
1542
1543                         public int this [int index] {
1544                                 get {
1545                                         if (index < 0 || index >= list.Count)
1546                                                 throw new ArgumentOutOfRangeException ("Index out of range.");
1547                                         return (int) list [index];
1548                                 }
1549                         }
1550
1551                         bool ICollection.IsSynchronized {
1552                                 get { return list.IsSynchronized; }
1553                         }
1554
1555                         object ICollection.SyncRoot {
1556                                 get { return list.SyncRoot; }
1557                         }
1558
1559                         bool IList.IsFixedSize {
1560                                 get { return list.IsFixedSize; }
1561                         }
1562
1563                         object IList.this [int index] {
1564                                 get { return this [index]; }
1565                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
1566                         }
1567                         #endregion      // Public Properties
1568
1569                         #region Public Methods
1570                         public bool Contains (int checkedIndex)
1571                         {
1572                                 return list.Contains (checkedIndex);
1573                         }
1574
1575                         public virtual IEnumerator GetEnumerator ()
1576                         {
1577                                 return list.GetEnumerator ();
1578                         }
1579
1580                         void ICollection.CopyTo (Array dest, int index)
1581                         {
1582                                 list.CopyTo (dest, index);
1583                         }
1584
1585                         int IList.Add (object value)
1586                         {
1587                                 throw new NotSupportedException ("Add operation is not supported.");
1588                         }
1589
1590                         void IList.Clear ()
1591                         {
1592                                 throw new NotSupportedException ("Clear operation is not supported.");
1593                         }
1594
1595                         bool IList.Contains (object checkedIndex)
1596                         {
1597                                 return list.Contains (checkedIndex);
1598                         }
1599
1600                         int IList.IndexOf (object checkedIndex)
1601                         {
1602                                 return list.IndexOf (checkedIndex);
1603                         }
1604
1605                         void IList.Insert (int index, object value)
1606                         {
1607                                 throw new NotSupportedException ("Insert operation is not supported.");
1608                         }
1609
1610                         void IList.Remove (object value)
1611                         {
1612                                 throw new NotSupportedException ("Remove operation is not supported.");
1613                         }
1614
1615                         void IList.RemoveAt (int index)
1616                         {
1617                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
1618                         }
1619
1620                         public int IndexOf (int checkedIndex)
1621                         {
1622                                 return list.IndexOf (checkedIndex);
1623                         }
1624                         #endregion      // Public Methods
1625
1626                 }       // CheckedIndexCollection
1627
1628                 public class CheckedListViewItemCollection : IList, ICollection, IEnumerable
1629                 {
1630                         internal ArrayList list;
1631                         private ListView owner;
1632
1633                         #region Public Constructor
1634                         public CheckedListViewItemCollection (ListView owner)
1635                         {
1636                                 list = new ArrayList ();
1637                                 this.owner = owner;
1638                         }
1639                         #endregion      // Public Constructor
1640
1641                         #region Public Properties
1642                         [Browsable (false)]
1643                         public virtual int Count {
1644                                 get { return list.Count; }
1645                         }
1646
1647                         public virtual bool IsReadOnly {
1648                                 get { return true; }
1649                         }
1650
1651                         public ListViewItem this [int index] {
1652                                 get {
1653                                         if (index < 0 || index >= list.Count)
1654                                                 throw new ArgumentOutOfRangeException ("Index out of range.");
1655                                         return (ListViewItem) list [index];
1656                                 }
1657                         }
1658
1659                         bool ICollection.IsSynchronized {
1660                                 get { return list.IsSynchronized; }
1661                         }
1662
1663                         object ICollection.SyncRoot {
1664                                 get { return list.SyncRoot; }
1665                         }
1666
1667                         bool IList.IsFixedSize {
1668                                 get { return list.IsFixedSize; }
1669                         }
1670
1671                         object IList.this [int index] {
1672                                 get { return this [index]; }
1673                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
1674                         }
1675                         #endregion      // Public Properties
1676
1677                         #region Public Methods
1678                         public bool Contains (ListViewItem item)
1679                         {
1680                                 return list.Contains (item);
1681                         }
1682
1683                         public virtual void CopyTo (Array dest, int index)
1684                         {
1685                                 list.CopyTo (dest, index);
1686                         }
1687
1688                         public virtual IEnumerator GetEnumerator ()
1689                         {
1690                                 return list.GetEnumerator ();
1691                         }
1692
1693                         int IList.Add (object value)
1694                         {
1695                                 throw new NotSupportedException ("Add operation is not supported.");
1696                         }
1697
1698                         void IList.Clear ()
1699                         {
1700                                 throw new NotSupportedException ("Clear operation is not supported.");
1701                         }
1702
1703                         bool IList.Contains (object item)
1704                         {
1705                                 return list.Contains (item);
1706                         }
1707
1708                         int IList.IndexOf (object item)
1709                         {
1710                                 return list.IndexOf (item);
1711                         }
1712
1713                         void IList.Insert (int index, object value)
1714                         {
1715                                 throw new NotSupportedException ("Insert operation is not supported.");
1716                         }
1717
1718                         void IList.Remove (object value)
1719                         {
1720                                 throw new NotSupportedException ("Remove operation is not supported.");
1721                         }
1722
1723                         void IList.RemoveAt (int index)
1724                         {
1725                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
1726                         }
1727
1728                         public int IndexOf (ListViewItem item)
1729                         {
1730                                 return list.IndexOf (item);
1731                         }
1732                         #endregion      // Public Methods
1733
1734                 }       // CheckedListViewItemCollection
1735
1736                 public class ColumnHeaderCollection : IList, ICollection, IEnumerable
1737                 {
1738                         internal ArrayList list;
1739                         private ListView owner;
1740
1741                         #region Public Constructor
1742                         public ColumnHeaderCollection (ListView owner)
1743                         {
1744                                 list = new ArrayList ();
1745                                 this.owner = owner;
1746                         }
1747                         #endregion      // Public Constructor
1748
1749                         #region Public Properties
1750                         [Browsable (false)]
1751                         public virtual int Count {
1752                                 get { return list.Count; }
1753                         }
1754
1755                         public virtual bool IsReadOnly {
1756                                 get { return false; }
1757                         }
1758
1759                         public virtual ColumnHeader this [int index] {
1760                                 get {
1761                                         if (index < 0 || index >= list.Count)
1762                                                 throw new ArgumentOutOfRangeException ("Index out of range.");
1763                                         return (ColumnHeader) list [index];
1764                                 }
1765                         }
1766
1767                         bool ICollection.IsSynchronized {
1768                                 get { return list.IsSynchronized; }
1769                         }
1770
1771                         object ICollection.SyncRoot {
1772                                 get { return list.SyncRoot; }
1773                         }
1774
1775                         bool IList.IsFixedSize {
1776                                 get { return list.IsFixedSize; }
1777                         }
1778
1779                         object IList.this [int index] {
1780                                 get { return this [index]; }
1781                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
1782                         }
1783                         #endregion      // Public Properties
1784
1785                         #region Public Methods
1786                         public virtual int Add (ColumnHeader value)
1787                         {
1788                                 int idx;
1789                                 value.owner = this.owner;
1790                                 idx = list.Add (value);
1791                                 owner.Redraw (true); 
1792                                 return idx;
1793                         }
1794
1795                         public virtual ColumnHeader Add (string str, int width, HorizontalAlignment textAlign)
1796                         {
1797                                 ColumnHeader colHeader = new ColumnHeader (this.owner, str, textAlign, width);
1798                                 this.Add (colHeader);                                                                   
1799                                 return colHeader;
1800                         }
1801
1802                         public virtual void AddRange (ColumnHeader [] values)
1803                         {
1804                                 foreach (ColumnHeader colHeader in values) {
1805                                         colHeader.owner = this.owner;
1806                                         Add (colHeader);
1807                                 }
1808                                 
1809                                 owner.Redraw (true); 
1810                         }
1811
1812                         public virtual void Clear ()
1813                         {
1814                                 list.Clear ();
1815                                 owner.Redraw (true);
1816                         }
1817
1818                         public bool Contains (ColumnHeader value)
1819                         {
1820                                 return list.Contains (value);
1821                         }
1822
1823                         public virtual IEnumerator GetEnumerator ()
1824                         {
1825                                 return list.GetEnumerator ();
1826                         }
1827
1828                         void ICollection.CopyTo (Array dest, int index)
1829                         {
1830                                 list.CopyTo (dest, index);
1831                         }
1832
1833                         int IList.Add (object value)
1834                         {
1835                                 if (! (value is ColumnHeader)) {
1836                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
1837                                 }
1838
1839                                 return this.Add ((ColumnHeader) value);
1840                         }
1841
1842                         bool IList.Contains (object value)
1843                         {
1844                                 if (! (value is ColumnHeader)) {
1845                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
1846                                 }
1847
1848                                 return this.Contains ((ColumnHeader) value);
1849                         }
1850
1851                         int IList.IndexOf (object value)
1852                         {
1853                                 if (! (value is ColumnHeader)) {
1854                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
1855                                 }
1856
1857                                 return this.IndexOf ((ColumnHeader) value);
1858                         }
1859
1860                         void IList.Insert (int index, object value)
1861                         {
1862                                 if (! (value is ColumnHeader)) {
1863                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
1864                                 }
1865
1866                                 this.Insert (index, (ColumnHeader) value);
1867                         }
1868
1869                         void IList.Remove (object value)
1870                         {
1871                                 if (! (value is ColumnHeader)) {
1872                                         throw new ArgumentException ("Not of type ColumnHeader", "value");
1873                                 }
1874
1875                                 this.Remove ((ColumnHeader) value);
1876                         }
1877
1878                         public int IndexOf (ColumnHeader value)
1879                         {
1880                                 return list.IndexOf (value);
1881                         }
1882
1883                         public void Insert (int index, ColumnHeader value)
1884                         {
1885                                 if (index < 0 || index >= list.Count)
1886                                         throw new ArgumentOutOfRangeException ("Index out of range.");
1887
1888                                 value.owner = this.owner;
1889                                 list.Insert (index, value);
1890                                 owner.Redraw (true);
1891                         }
1892
1893                         public void Insert (int index, string str, int width, HorizontalAlignment textAlign)
1894                         {
1895                                 ColumnHeader colHeader = new ColumnHeader (this.owner, str, textAlign, width);
1896                                 this.Insert (index, colHeader);
1897                         }
1898
1899                         public virtual void Remove (ColumnHeader column)
1900                         {
1901                                 // TODO: Update Column internal index ?
1902                                 list.Remove (column);
1903                                 owner.Redraw (true);
1904                         }
1905
1906                         public virtual void RemoveAt (int index)
1907                         {
1908                                 if (index < 0 || index >= list.Count)
1909                                         throw new ArgumentOutOfRangeException ("Index out of range.");
1910
1911                                 // TODO: Update Column internal index ?
1912                                 list.RemoveAt (index);
1913                                 owner.Redraw (true);
1914                         }
1915                         #endregion      // Public Methods
1916                         
1917
1918                 }       // ColumnHeaderCollection
1919
1920                 public class ListViewItemCollection : IList, ICollection, IEnumerable
1921                 {
1922                         internal ArrayList list;
1923                         private ListView owner;
1924
1925                         #region Public Constructor
1926                         public ListViewItemCollection (ListView owner)
1927                         {
1928                                 list = new ArrayList ();
1929                                 this.owner = owner;
1930                         }
1931                         #endregion      // Public Constructor
1932
1933                         #region Public Properties
1934                         [Browsable (false)]
1935                         public virtual int Count {
1936                                 get { return list.Count; }
1937                         }
1938
1939                         public virtual bool IsReadOnly {
1940                                 get { return false; }
1941                         }
1942
1943                         public virtual ListViewItem this [int displayIndex] {
1944                                 get {
1945                                         if (displayIndex < 0 || displayIndex >= list.Count)
1946                                                 throw new ArgumentOutOfRangeException ("Index out of range.");
1947                                         return (ListViewItem) list [displayIndex];
1948                                 }
1949
1950                                 set {
1951                                         if (displayIndex < 0 || displayIndex >= list.Count)
1952                                                 throw new ArgumentOutOfRangeException ("Index out of range.");
1953
1954                                         if (list.Contains (value))
1955                                                 throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "value");
1956
1957                                         value.owner = this.owner;
1958                                         list [displayIndex] = value;
1959
1960                                         owner.Redraw (true);
1961                                 }
1962                         }
1963
1964                         bool ICollection.IsSynchronized {
1965                                 get { return list.IsSynchronized; }
1966                         }
1967
1968                         object ICollection.SyncRoot {
1969                                 get { return list.SyncRoot; }
1970                         }
1971
1972                         bool IList.IsFixedSize {
1973                                 get { return list.IsFixedSize; }
1974                         }
1975
1976                         object IList.this [int index] {
1977                                 get { return this [index]; }
1978                                 set {
1979                                         if (value is ListViewItem)
1980                                                 this [index] = (ListViewItem) value;
1981                                         else
1982                                                 this [index] = new ListViewItem (value.ToString ());
1983                                 }
1984                         }
1985                         #endregion      // Public Properties
1986
1987                         #region Public Methods
1988                         public virtual ListViewItem Add (ListViewItem value)
1989                         {
1990                                 if (list.Contains (value))
1991                                         throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "value");
1992
1993                                 value.owner = this.owner;
1994                                 list.Add (value);
1995
1996                                 if (owner.Sorting != SortOrder.None)
1997                                         owner.Sort ();
1998
1999                                 owner.Redraw (true);
2000
2001                                 return value;
2002                         }
2003
2004                         public virtual ListViewItem Add (string text)
2005                         {
2006                                 ListViewItem item = new ListViewItem (text);
2007                                 return this.Add (item);
2008                         }
2009
2010                         public virtual ListViewItem Add (string text, int imageIndex)
2011                         {
2012                                 ListViewItem item = new ListViewItem (text, imageIndex);
2013                                 return this.Add (item);
2014                         }
2015
2016                         public void AddRange (ListViewItem [] values)
2017                         {
2018                                 list.Clear ();
2019
2020                                 foreach (ListViewItem item in values) {
2021                                         item.owner = this.owner;
2022                                         list.Add (item);
2023                                 }
2024
2025                                 if (owner.Sorting != SortOrder.None)
2026                                         owner.Sort ();
2027
2028                                 owner.Redraw (true);
2029                         }
2030
2031                         public virtual void Clear ()
2032                         {
2033                                 list.Clear ();
2034                         }
2035
2036                         public bool Contains (ListViewItem item)
2037                         {
2038                                 return list.Contains (item);
2039                         }
2040
2041                         public virtual void CopyTo (Array dest, int index)
2042                         {
2043                                 list.CopyTo (dest, index);
2044                         }
2045
2046                         public virtual IEnumerator GetEnumerator ()
2047                         {
2048                                 return list.GetEnumerator ();
2049                         }
2050
2051                         int IList.Add (object item)
2052                         {
2053                                 int result;
2054                                 ListViewItem li;
2055
2056                                 if (item is ListViewItem) {
2057                                         li = (ListViewItem) item;
2058                                         if (list.Contains (li))
2059                                                 throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "item");
2060                                 }
2061                                 else
2062                                         li = new ListViewItem (item.ToString ());
2063
2064                                 li.owner = this.owner;
2065                                 result = list.Add (li);
2066                                 owner.Redraw (true);
2067
2068                                 return result;
2069                         }
2070
2071                         bool IList.Contains (object item)
2072                         {
2073                                 return list.Contains (item);
2074                         }
2075
2076                         int IList.IndexOf (object item)
2077                         {
2078                                 return list.IndexOf (item);
2079                         }
2080
2081                         void IList.Insert (int index, object item)
2082                         {
2083                                 if (item is ListViewItem)
2084                                         this.Insert (index, (ListViewItem) item);
2085                                 else
2086                                         this.Insert (index, item.ToString ());
2087                         }
2088
2089                         void IList.Remove (object item)
2090                         {
2091                                 if (list.Contains (item)) {
2092                                         list.Remove (item);
2093                                         owner.Redraw (true);
2094                                 }
2095                         }
2096
2097                         public int IndexOf (ListViewItem item)
2098                         {
2099                                 return list.IndexOf (item);
2100                         }
2101
2102                         public ListViewItem Insert (int index, ListViewItem item)
2103                         {
2104                                 if (index < 0 || index >= list.Count)
2105                                         throw new ArgumentOutOfRangeException ("Index out of range.");
2106
2107                                 if (list.Contains (item))
2108                                         throw new ArgumentException ("An item cannot be added more than once. To add an item again, you need to clone it.", "item");
2109
2110                                 item.owner = this.owner;
2111                                 list.Insert (index, item);
2112                                 owner.Redraw (true);
2113                                 return item;
2114                         }
2115
2116                         public ListViewItem Insert (int index, string text)
2117                         {
2118                                 return this.Insert (index, new ListViewItem (text));
2119                         }
2120
2121                         public ListViewItem Insert (int index, string text, int imageIndex)
2122                         {
2123                                 return this.Insert (index, new ListViewItem (text, imageIndex));
2124                         }
2125
2126                         public virtual void Remove (ListViewItem item)
2127                         {
2128                                 if (list.Contains (item)) {
2129                                         list.Remove (item);
2130                                         owner.Redraw (true);
2131                                 }
2132                         }
2133
2134                         public virtual void RemoveAt (int index)
2135                         {
2136                                 if (index < 0 || index >= list.Count)
2137                                         throw new ArgumentOutOfRangeException ("Index out of range.");
2138
2139                                 list.RemoveAt (index);
2140                                 owner.Redraw (false);
2141                         }
2142                         #endregion      // Public Methods
2143
2144                 }       // ListViewItemCollection
2145
2146                 public class SelectedIndexCollection : IList, ICollection, IEnumerable
2147                 {
2148                         internal ArrayList list;
2149                         private ListView owner;
2150
2151                         #region Public Constructor
2152                         public SelectedIndexCollection (ListView owner)
2153                         {
2154                                 list = new ArrayList ();
2155                                 this.owner = owner;
2156                         }
2157                         #endregion      // Public Constructor
2158
2159                         #region Public Properties
2160                         [Browsable (false)]
2161                         public virtual int Count {
2162                                 get { return list.Count; }
2163                         }
2164
2165                         public virtual bool IsReadOnly {
2166                                 get { return true; }
2167                         }
2168
2169                         public int this [int index] {
2170                                 get {
2171                                         if (index < 0 || index >= list.Count)
2172                                                 throw new ArgumentOutOfRangeException ("Index out of range.");
2173                                         return (int) list [index];
2174                                 }
2175                         }
2176
2177                         bool ICollection.IsSynchronized {
2178                                 get { return list.IsSynchronized; }
2179                         }
2180
2181                         object ICollection.SyncRoot {
2182                                 get { return list.SyncRoot; }
2183                         }
2184
2185                         bool IList.IsFixedSize {
2186                                 get { return list.IsFixedSize; }
2187                         }
2188
2189                         object IList.this [int index] {
2190                                 get { return this [index]; }
2191                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
2192                         }
2193                         #endregion      // Public Properties
2194
2195                         #region Public Methods
2196                         public bool Contains (int selectedIndex)
2197                         {
2198                                 return list.Contains (selectedIndex);
2199                         }
2200
2201                         public virtual void CopyTo (Array dest, int index)
2202                         {
2203                                 list.CopyTo (dest, index);
2204                         }
2205
2206                         public virtual IEnumerator GetEnumerator ()
2207                         {
2208                                 return list.GetEnumerator ();
2209                         }
2210
2211                         int IList.Add (object value)
2212                         {
2213                                 throw new NotSupportedException ("Add operation is not supported.");
2214                         }
2215
2216                         void IList.Clear ()
2217                         {
2218                                 throw new NotSupportedException ("Clear operation is not supported.");
2219                         }
2220
2221                         bool IList.Contains (object selectedIndex)
2222                         {
2223                                 return list.Contains (selectedIndex);
2224                         }
2225
2226                         int IList.IndexOf (object selectedIndex)
2227                         {
2228                                 return list.IndexOf (selectedIndex);
2229                         }
2230
2231                         void IList.Insert (int index, object value)
2232                         {
2233                                 throw new NotSupportedException ("Insert operation is not supported.");
2234                         }
2235
2236                         void IList.Remove (object value)
2237                         {
2238                                 throw new NotSupportedException ("Remove operation is not supported.");
2239                         }
2240
2241                         void IList.RemoveAt (int index)
2242                         {
2243                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
2244                         }
2245
2246                         public int IndexOf (int selectedIndex)
2247                         {
2248                                 return list.IndexOf (selectedIndex);
2249                         }
2250                         #endregion      // Public Methods
2251
2252                 }       // SelectedIndexCollection
2253
2254                 public class SelectedListViewItemCollection : IList, ICollection, IEnumerable
2255                 {
2256                         internal ArrayList list;
2257                         private ListView owner;
2258
2259                         #region Public Constructor
2260                         public SelectedListViewItemCollection (ListView owner)
2261                         {
2262                                 list = new ArrayList ();
2263                                 this.owner = owner;
2264                         }
2265                         #endregion      // Public Constructor
2266
2267                         #region Public Properties
2268                         [Browsable (false)]
2269                         public virtual int Count {
2270                                 get { return list.Count; }
2271                         }
2272
2273                         public virtual bool IsReadOnly {
2274                                 get { return true; }
2275                         }
2276
2277                         public ListViewItem this [int index] {
2278                                 get {
2279                                         if (index < 0 || index >= list.Count)
2280                                                 throw new ArgumentOutOfRangeException ("Index out of range.");
2281                                         return (ListViewItem) list [index];
2282                                 }
2283                         }
2284
2285                         bool ICollection.IsSynchronized {
2286                                 get { return list.IsSynchronized; }
2287                         }
2288
2289                         object ICollection.SyncRoot {
2290                                 get { return list.SyncRoot; }
2291                         }
2292
2293                         bool IList.IsFixedSize {
2294                                 get { return list.IsFixedSize; }
2295                         }
2296
2297                         object IList.this [int index] {
2298                                 get { return this [index]; }
2299                                 set { throw new NotSupportedException ("SetItem operation is not supported."); }
2300                         }
2301                         #endregion      // Public Properties
2302
2303                         #region Public Methods
2304                         public virtual void Clear ()
2305                         {
2306                                 // mark the items as unselected before clearing the list
2307                                 for (int i = 0; i < list.Count; i++)
2308                                         ((ListViewItem) list [i]).selected = false;
2309
2310                                 list.Clear ();
2311                         }
2312
2313                         public bool Contains (ListViewItem item)
2314                         {
2315                                 return list.Contains (item);
2316                         }
2317
2318                         public virtual void CopyTo (Array dest, int index)
2319                         {
2320                                 list.CopyTo (dest, index);
2321                         }
2322
2323                         public virtual IEnumerator GetEnumerator ()
2324                         {
2325                                 return list.GetEnumerator ();
2326                         }
2327
2328                         int IList.Add (object value)
2329                         {
2330                                 throw new NotSupportedException ("Add operation is not supported.");
2331                         }
2332
2333                         bool IList.Contains (object item)
2334                         {
2335                                 return list.Contains (item);
2336                         }
2337
2338                         int IList.IndexOf (object item)
2339                         {
2340                                 return list.IndexOf (item);
2341                         }
2342
2343                         void IList.Insert (int index, object value)
2344                         {
2345                                 throw new NotSupportedException ("Insert operation is not supported.");
2346                         }
2347
2348                         void IList.Remove (object value)
2349                         {
2350                                 throw new NotSupportedException ("Remove operation is not supported.");
2351                         }
2352
2353                         void IList.RemoveAt (int index)
2354                         {
2355                                 throw new NotSupportedException ("RemoveAt operation is not supported.");
2356                         }
2357
2358                         public int IndexOf (ListViewItem item)
2359                         {
2360                                 return list.IndexOf (item);
2361                         }
2362                         #endregion      // Public Methods
2363
2364                 }       // SelectedListViewItemCollection
2365
2366                 #endregion // Subclasses
2367         }
2368 }