Typo error
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / ListViewInsertionMark.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) 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Carlos Alberto Cortez <calberto.cortez@gmail.com>
24 //
25
26
27 using System;
28 using System.Drawing;
29
30 namespace System.Windows.Forms
31 {
32         public sealed class ListViewInsertionMark 
33         {
34                 ListView listview_owner;
35                 bool appears_after_item;
36                 Rectangle bounds;
37                 Color? color;
38                 int index = 0;
39
40                 internal ListViewInsertionMark (ListView listview)
41                 {
42                         listview_owner = listview;
43                 }
44
45                 public bool AppearsAfterItem {
46                         get {
47                                 return appears_after_item;
48                         }
49                         set {
50                                 if (value == appears_after_item)
51                                         return;
52
53                                 appears_after_item = value;
54
55                                 listview_owner.item_control.Invalidate (bounds);
56                                 UpdateBounds ();
57                                 listview_owner.item_control.Invalidate (bounds);
58                         }
59                 }
60
61                 public Rectangle Bounds {
62                         get {
63                                 return bounds;
64                         }
65                 }
66
67                 public Color Color {
68                         get {
69                                 return color == null ? listview_owner.ForeColor : color.Value;
70                         }
71                         set {
72                                 color = value;
73                         }
74                 }
75
76                 public int Index {
77                         get {
78                                 return index;
79                         }
80                         set {
81                                 if (value == index)
82                                         return;
83
84                                 index = value;
85
86                                 listview_owner.item_control.Invalidate (bounds);
87                                 UpdateBounds ();
88                                 listview_owner.item_control.Invalidate (bounds);
89                         }
90                 }
91
92                 void UpdateBounds ()
93                 {
94                         if (index < 0 || index >= listview_owner.Items.Count) {
95                                 bounds = Rectangle.Empty;
96                                 return;
97                         }
98
99                         Rectangle item_bounds = listview_owner.Items [index].Bounds;
100                         int x_origin = (appears_after_item ? item_bounds.Right : item_bounds.Left) - 2;
101                         int height = item_bounds.Height + ThemeEngine.Current.ListViewVerticalSpacing;
102
103                         bounds = new Rectangle (x_origin, item_bounds.Top, 7, height);
104                 }
105
106                 public int NearestIndex (Point pt)
107                 {
108                         double distance = Double.MaxValue;
109                         int nearest = -1;
110
111                         for (int i = 0; i < listview_owner.Items.Count; i++) {
112                                 Point pos = listview_owner.GetItemLocation (i);
113                                 double d = Math.Pow (pos.X - pt.X, 2) + Math.Pow (pos.Y - pt.Y, 2);
114                                 if (d < distance) {
115                                         distance = d;
116                                         nearest = i;
117                                 }
118                         }
119
120                         if (listview_owner.item_control.dragged_item_index == nearest)
121                                 return -1;
122
123                         return nearest;
124                 }
125
126                 internal PointF [] TopTriangle {
127                         get {
128                                 PointF p1 = new PointF (bounds.X, bounds.Y);
129                                 PointF p2 = new PointF (bounds.Right, bounds.Y);
130                                 PointF p3 = new PointF (bounds.X + (bounds.Right - bounds.X) / 2, bounds.Y + 5);
131
132                                 return new PointF [] {p1, p2, p3};
133                         }
134                 }
135
136                 internal PointF [] BottomTriangle {
137                         get {
138                                 PointF p1 = new PointF (bounds.X, bounds.Bottom);
139                                 PointF p2 = new PointF (bounds.Right, bounds.Bottom);
140                                 PointF p3 = new PointF (bounds.X + (bounds.Right - bounds.X) / 2, bounds.Bottom - 5);
141
142                                 return new PointF [] {p1, p2, p3};
143                         }
144                 }
145
146                 internal Rectangle Line {
147                         get {
148                                 return new Rectangle (bounds.X + 2, bounds.Y + 2, 2, bounds.Height - 5);
149                         }
150                 }
151         }
152 }