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
9 // The above copyright notice and this permission notice shall be
\r
10 // included in all copies or substantial portions of the Software.
\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
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
\r
23 // olivier Dufour olivier.duff@free.fr
\r
30 using System.Collections.Generic;
\r
32 using System.Drawing;
\r
33 using System.Security.Permissions;
\r
34 using System.ComponentModel;
\r
36 namespace System.Windows.Forms
\r
38 public class DataGridViewLinkCell : DataGridViewCell
\r
41 public DataGridViewLinkCell ()
\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
49 #region Public Methods
\r
51 public override object Clone ()
\r
53 DataGridViewLinkCell clone = (DataGridViewLinkCell)base.Clone ();
\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
65 public override string ToString ()
\r
67 return string.Format ("DataGridViewLinkCell {{ ColumnIndex={0}, RowIndex={1} }}", ColumnIndex, RowIndex);
\r
72 #region Protected Methods
\r
74 protected override AccessibleObject CreateAccessibilityInstance ()
\r
76 return new DataGridViewLinkCellAccessibleObject (this);
\r
79 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
\r
81 if (DataGridView == null)
\r
82 return Rectangle.Empty;
\r
84 object o = FormattedValue;
\r
85 Size s = Size.Empty;
\r
88 s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
\r
91 return new Rectangle (1, 10, 0, 0);
\r
94 return new Rectangle (1, (OwningRow.Height - s.Height) / 2 - 1, s.Width, s.Height);
\r
97 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
\r
99 if (DataGridView == null || string.IsNullOrEmpty (ErrorText))
\r
100 return Rectangle.Empty;
\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
106 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
\r
108 object o = FormattedValue;
\r
111 Size s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
\r
112 s.Height = Math.Max (s.Height, 20);
\r
116 return new Size (21, 20);
\r
119 protected override object GetValue (int rowIndex)
\r
121 if (useColumnTextForLinkValue)
\r
122 return (OwningColumn as DataGridViewLinkColumn).Text;
\r
124 return base.GetValue (rowIndex);
\r
127 protected override bool KeyUpUnsharesRow (KeyEventArgs e, int rowIndex)
\r
129 if (e.KeyCode != Keys.Space
\r
130 && trackVisitedState == true
\r
131 && linkVisited == false
\r
140 protected override bool MouseDownUnsharesRow (DataGridViewCellMouseEventArgs e)
\r
145 protected override bool MouseLeaveUnsharesRow (int rowIndex)
\r
147 return (linkState != LinkState.Normal);
\r
150 protected override bool MouseMoveUnsharesRow (DataGridViewCellMouseEventArgs e)
\r
152 if (linkState == LinkState.Hover)
\r
158 protected override bool MouseUpUnsharesRow (DataGridViewCellMouseEventArgs e)
\r
160 return (linkState == LinkState.Hover);
\r
163 protected override void OnKeyUp (KeyEventArgs e, int rowIndex)
\r
165 if ((e.KeyData & Keys.Space) == Keys.Space) {
\r
166 linkState = LinkState.Normal;
\r
167 DataGridView.InvalidateCell (this);
\r
171 protected override void OnMouseDown (DataGridViewCellMouseEventArgs e)
\r
173 base.OnMouseDown (e);
\r
175 linkState = LinkState.Active;
\r
176 DataGridView.InvalidateCell (this);
\r
179 protected override void OnMouseLeave (int rowIndex)
\r
181 base.OnMouseLeave (rowIndex);
\r
183 linkState = LinkState.Normal;
\r
184 DataGridView.InvalidateCell (this);
\r
185 DataGridView.Cursor = parent_cursor;
\r
188 protected override void OnMouseMove (DataGridViewCellMouseEventArgs e)
\r
190 base.OnMouseMove (e);
\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
200 protected override void OnMouseUp (DataGridViewCellMouseEventArgs e)
\r
202 base.OnMouseUp (e);
\r
204 linkState = LinkState.Hover;
\r
205 LinkVisited = true;
\r
206 DataGridView.InvalidateCell (this);
\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
211 base.Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
\r
214 internal override void PaintPartContent (Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, object formattedValue)
\r
216 Font font = cellStyle.Font;
\r
218 switch (LinkBehavior) {
\r
219 case LinkBehavior.AlwaysUnderline:
\r
220 case LinkBehavior.SystemDefault:
\r
221 font = new Font (font, FontStyle.Underline);
\r
223 case LinkBehavior.HoverUnderline:
\r
224 if (linkState == LinkState.Hover)
\r
225 font = new Font (font, FontStyle.Underline);
\r
231 if (linkState == LinkState.Active)
\r
232 color = ActiveLinkColor;
\r
233 else if (linkVisited)
\r
234 color = VisitedLinkColor;
\r
238 TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl;
\r
240 cellBounds.Height -= 2;
\r
241 cellBounds.Width -= 2;
\r
243 if (formattedValue != null)
\r
244 TextRenderer.DrawText (graphics, formattedValue.ToString (), font, cellBounds, color, flags);
\r
248 #region Private fields
\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
262 #region Public properties
\r
264 public Color ActiveLinkColor {
\r
265 get { return activeLinkColor; }
\r
266 set { activeLinkColor = value; }
\r
269 [DefaultValue (LinkBehavior.SystemDefault)]
\r
270 public LinkBehavior LinkBehavior {
\r
271 get { return linkBehavior; }
\r
272 set { linkBehavior = value; }
\r
274 public Color LinkColor {
\r
275 get { return linkColor; }
\r
276 set { linkColor = value; }
\r
278 public bool LinkVisited {
\r
279 get { return linkVisited; }
\r
280 set { linkVisited = value; }
\r
282 [DefaultValue (true)]
\r
283 public bool TrackVisitedState {
\r
284 get { return trackVisitedState; }
\r
285 set { trackVisitedState = value; }
\r
287 [DefaultValue (false)]
\r
288 public bool UseColumnTextForLinkValue {
\r
289 get { return useColumnTextForLinkValue; }
\r
290 set { useColumnTextForLinkValue = value; }
\r
293 public Color VisitedLinkColor {
\r
294 get { return visited_link_color; }
\r
295 set { visited_link_color = value; }
\r
298 public override Type ValueType {
\r
299 get { return base.ValueType == null ? typeof (object) : base.ValueType; }
\r
302 public override Type EditType {
\r
303 get { return null; }
\r
305 public override Type FormattedValueType {
\r
306 get { return typeof(string); }
\r
311 protected class DataGridViewLinkCellAccessibleObject : DataGridViewCell.DataGridViewCellAccessibleObject
\r
313 public DataGridViewLinkCellAccessibleObject (DataGridViewCell owner) : base(owner)
\r
318 [MonoTODO ("Stub, does nothing")]
\r
319 [SecurityPermission (SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
\r
320 public override void DoDefaultAction ()
\r
322 //DataGridViewLinkCell cell = base.Owner as DataGridViewLinkCell;
\r
323 //if (cell.DataGridView != null && cell.RowIndex == -1)
\r
324 // throw new InvalidOperationException ();
\r
327 public override int GetChildCount ()
\r
332 public override string DefaultAction { get { return "Click"; } }
\r