* TabControl.cs: Show the tooltip depending on the value
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewLinkCell.cs
1 // Permission is hereby granted, free of charge, to any person obtaining\r
2 // a copy of this software and associated documentation files (the\r
3 // "Software"), to deal in the Software without restriction, including\r
4 // without limitation the rights to use, copy, modify, merge, publish,\r
5 // distribute, sublicense, and/or sell copies of the Software, and to\r
6 // permit persons to whom the Software is furnished to do so, subject to\r
7 // the following conditions:\r
8 // \r
9 // The above copyright notice and this permission notice shall be\r
10 // included in all copies or substantial portions of the Software.\r
11 // \r
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
19 //\r
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)\r
21 //\r
22 // Authors:\r
23 //      olivier Dufour  olivier.duff@free.fr\r
24 //\r
25 //\r
26 \r
27 #if NET_2_0\r
28 \r
29 using System;\r
30 using System.Collections.Generic;\r
31 using System.Text;\r
32 using System.Drawing;\r
33 using System.Security.Permissions;\r
34 using System.ComponentModel;\r
35 \r
36 namespace System.Windows.Forms\r
37 {\r
38         public class DataGridViewLinkCell : DataGridViewCell\r
39         {\r
40 \r
41                 public DataGridViewLinkCell ()\r
42                 {\r
43                         activeLinkColor = Color.Red;\r
44                         linkColor = Color.FromArgb (0, 0, 255);\r
45                         trackVisitedState = true;\r
46                         visited_link_color = Color.FromArgb (128, 0, 128);\r
47                 }\r
48 \r
49                 #region Public Methods\r
50 \r
51                 public override object Clone ()\r
52                 {\r
53                         DataGridViewLinkCell clone = (DataGridViewLinkCell)base.Clone ();\r
54                         \r
55                         clone.activeLinkColor = this.activeLinkColor;\r
56                         clone.linkColor = this.linkColor;\r
57                         clone.linkVisited = this.linkVisited;\r
58                         clone.linkBehavior = this.linkBehavior;\r
59                         clone.visited_link_color = this.visited_link_color;\r
60                         clone.trackVisitedState = this.trackVisitedState;\r
61                         \r
62                         return clone;\r
63                 }\r
64 \r
65                 public override string ToString ()\r
66                 {\r
67                         return string.Format ("DataGridViewLinkCell {{ ColumnIndex={0}, RowIndex={1} }}", ColumnIndex, RowIndex);\r
68                 }\r
69 \r
70                 #endregion\r
71 \r
72                 #region Protected Methods\r
73 \r
74                 protected override AccessibleObject CreateAccessibilityInstance ()\r
75                 {\r
76                         return new DataGridViewLinkCellAccessibleObject (this);\r
77                 }\r
78                 \r
79                 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)\r
80                 {\r
81                         if (DataGridView == null)\r
82                                 return Rectangle.Empty;\r
83 \r
84                         object o = FormattedValue;\r
85                         Size s = Size.Empty;\r
86 \r
87                         if (o != null) {\r
88                                 s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);\r
89                                 s.Height += 3;\r
90                         } else {\r
91                                 return new Rectangle (1, 10, 0, 0);\r
92                         }\r
93 \r
94                         return new Rectangle (1, (OwningRow.Height - s.Height) / 2 - 1, s.Width, s.Height);\r
95                 }\r
96                 \r
97                 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)\r
98                 {\r
99                         if (DataGridView == null || string.IsNullOrEmpty (ErrorText))\r
100                                 return Rectangle.Empty;\r
101 \r
102                         Size error_icon = new Size (12, 11);\r
103                         return new Rectangle (new Point (Size.Width - error_icon.Width - 5, (Size.Height - error_icon.Height) / 2), error_icon);\r
104                 }\r
105                 \r
106                 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)\r
107                 {\r
108                         object o = FormattedValue;\r
109 \r
110                         if (o != null) {\r
111                                 Size s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);\r
112                                 s.Height = Math.Max (s.Height, 20);\r
113                                 s.Width += 4;\r
114                                 return s;\r
115                         } else\r
116                                 return new Size (21, 20);\r
117                 }\r
118                 \r
119                 protected override object GetValue (int rowIndex)\r
120                 {\r
121                         if (useColumnTextForLinkValue)\r
122                                 return (OwningColumn as DataGridViewLinkColumn).Text;\r
123                                 \r
124                         return base.GetValue (rowIndex);\r
125                 }\r
126 \r
127                 protected override bool KeyUpUnsharesRow (KeyEventArgs e, int rowIndex)\r
128                 {\r
129                         if (e.KeyCode != Keys.Space\r
130                                 && trackVisitedState == true\r
131                                 && linkVisited == false\r
132                                 && !e.Shift \r
133                                 && !e.Control\r
134                                 && !e.Alt)\r
135                                 return true;\r
136 \r
137                         return false;\r
138                 }\r
139                 \r
140                 protected override bool MouseDownUnsharesRow (DataGridViewCellMouseEventArgs e)\r
141                 {\r
142                         return true;\r
143                 }\r
144                 \r
145                 protected override bool MouseLeaveUnsharesRow (int rowIndex)\r
146                 {\r
147                         return (linkState != LinkState.Normal);\r
148                 }\r
149 \r
150                 protected override bool MouseMoveUnsharesRow (DataGridViewCellMouseEventArgs e)\r
151                 {\r
152                         if (linkState == LinkState.Hover)\r
153                                 return true;\r
154 \r
155                         return false;\r
156                 }\r
157 \r
158                 protected override bool MouseUpUnsharesRow (DataGridViewCellMouseEventArgs e)\r
159                 {\r
160                         return (linkState == LinkState.Hover);\r
161                 }\r
162                 \r
163                 protected override void OnKeyUp (KeyEventArgs e, int rowIndex)\r
164                 {\r
165                         if ((e.KeyData & Keys.Space) == Keys.Space) {\r
166                                 linkState = LinkState.Normal;\r
167                                 DataGridView.InvalidateCell (this);\r
168                         }\r
169                 }\r
170 \r
171                 protected override void OnMouseDown (DataGridViewCellMouseEventArgs e)\r
172                 {\r
173                         base.OnMouseDown (e);\r
174                         \r
175                         linkState = LinkState.Active;\r
176                         DataGridView.InvalidateCell (this);\r
177                 }\r
178                 \r
179                 protected override void OnMouseLeave (int rowIndex)\r
180                 {\r
181                         base.OnMouseLeave (rowIndex);\r
182                         \r
183                         linkState = LinkState.Normal;\r
184                         DataGridView.InvalidateCell (this);\r
185                         DataGridView.Cursor = parent_cursor;\r
186                 }\r
187                 \r
188                 protected override void OnMouseMove (DataGridViewCellMouseEventArgs e)\r
189                 {\r
190                         base.OnMouseMove (e);\r
191                         \r
192                         if (linkState != LinkState.Hover) {\r
193                                 linkState = LinkState.Hover;\r
194                                 DataGridView.InvalidateCell (this);\r
195                                 parent_cursor = DataGridView.Cursor;\r
196                                 DataGridView.Cursor = Cursors.Hand;\r
197                         }\r
198                 }\r
199                 \r
200                 protected override void OnMouseUp (DataGridViewCellMouseEventArgs e)\r
201                 {\r
202                         base.OnMouseUp (e);\r
203                         \r
204                         linkState = LinkState.Hover;\r
205                         LinkVisited = true;\r
206                         DataGridView.InvalidateCell (this);\r
207                 }\r
208 \r
209                 protected override void Paint (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)\r
210                 {\r
211                         base.Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);\r
212                 }\r
213                 \r
214                 internal override void PaintPartContent (Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, object formattedValue)\r
215                 {\r
216                         Font font = cellStyle.Font;\r
217 \r
218                         switch (LinkBehavior) {\r
219                                 case LinkBehavior.AlwaysUnderline:\r
220                                 case LinkBehavior.SystemDefault:\r
221                                         font = new Font (font, FontStyle.Underline);\r
222                                         break;\r
223                                 case LinkBehavior.HoverUnderline:\r
224                                         if (linkState == LinkState.Hover)\r
225                                                 font = new Font (font, FontStyle.Underline);\r
226                                         break;\r
227                         }\r
228 \r
229                         Color color;\r
230                         \r
231                         if (linkState == LinkState.Active)\r
232                                 color = ActiveLinkColor;\r
233                         else if (linkVisited)\r
234                                 color = VisitedLinkColor;\r
235                         else\r
236                                 color = LinkColor;\r
237 \r
238                         TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl;\r
239 \r
240                         cellBounds.Height -= 2;\r
241                         cellBounds.Width -= 2;\r
242 \r
243                         if (formattedValue != null)\r
244                                 TextRenderer.DrawText (graphics, formattedValue.ToString (), font, cellBounds, color, flags);\r
245                 }\r
246                 #endregion\r
247 \r
248                 #region Private fields\r
249 \r
250                 private Color activeLinkColor;\r
251                 private LinkBehavior linkBehavior;\r
252                 private Color linkColor;\r
253                 private bool linkVisited;\r
254                 private Cursor parent_cursor;\r
255                 private bool trackVisitedState;\r
256                 private bool useColumnTextForLinkValue;\r
257                 private Color visited_link_color;\r
258                 private LinkState linkState;\r
259 \r
260                 #endregion\r
261 \r
262                 #region Public properties\r
263 \r
264                 public Color ActiveLinkColor {\r
265                         get { return activeLinkColor; }\r
266                         set { activeLinkColor = value; }\r
267                 }\r
268 \r
269                 [DefaultValue (LinkBehavior.SystemDefault)]\r
270                 public LinkBehavior LinkBehavior {\r
271                         get { return linkBehavior; }\r
272                         set { linkBehavior = value; }\r
273                 }\r
274                 public Color LinkColor {\r
275                         get { return linkColor; }\r
276                         set { linkColor = value; }\r
277                 }\r
278                 public bool LinkVisited {\r
279                         get { return linkVisited; }\r
280                         set { linkVisited = value; }\r
281                 }\r
282                 [DefaultValue (true)]\r
283                 public bool TrackVisitedState {\r
284                         get { return trackVisitedState; }\r
285                         set { trackVisitedState = value; }\r
286                 }\r
287                 [DefaultValue (false)]\r
288                 public bool UseColumnTextForLinkValue {\r
289                         get { return useColumnTextForLinkValue; }\r
290                         set { useColumnTextForLinkValue = value; }\r
291                 }\r
292 \r
293                 public Color VisitedLinkColor {\r
294                         get { return visited_link_color; }\r
295                         set { visited_link_color = value; }\r
296                 }\r
297 \r
298                 public override Type ValueType {\r
299                         get { return base.ValueType == null ? typeof (object) : base.ValueType; }\r
300                 }\r
301 \r
302                 public override Type EditType {\r
303                         get { return null; }\r
304                 }\r
305                 public override Type FormattedValueType {\r
306                         get { return typeof(string); }\r
307                 }\r
308 \r
309                 #endregion\r
310 \r
311                 protected class DataGridViewLinkCellAccessibleObject : DataGridViewCell.DataGridViewCellAccessibleObject\r
312                 {\r
313                         public DataGridViewLinkCellAccessibleObject (DataGridViewCell owner) : base(owner) \r
314                         {\r
315                                 //DO NOTHING\r
316                         }\r
317 \r
318                         [MonoTODO ("Stub, does nothing")]\r
319                         [SecurityPermission (SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]\r
320                         public override void DoDefaultAction ()\r
321                         {\r
322                                 //DataGridViewLinkCell cell = base.Owner as DataGridViewLinkCell;\r
323                                 //if (cell.DataGridView != null && cell.RowIndex == -1)\r
324                                 //        throw new InvalidOperationException ();\r
325                         }\r
326 \r
327                         public override int GetChildCount ()\r
328                         {\r
329                                 return -1;\r
330                         }\r
331 \r
332                         public override string DefaultAction { get { return "Click"; } }\r
333                 }\r
334                 \r
335         }\r
336 }\r
337 \r
338 #endif\r