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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Pedro MartÃnez Juliá <pedromj@gmail.com>
28 namespace System.Windows.Forms {
30 public class DataGridViewRowHeaderCell : DataGridViewHeaderCell {
32 private string headerText;
34 public DataGridViewRowHeaderCell ()
38 public override object Clone ()
40 return MemberwiseClone();
43 public override ContextMenuStrip GetInheritedContextMenuStrip (int rowIndex)
45 if (DataGridView == null)
48 if (rowIndex < 0 || rowIndex >= DataGridView.Rows.Count)
49 throw new ArgumentOutOfRangeException ("rowIndex");
51 if (ContextMenuStrip != null)
52 return ContextMenuStrip;
54 return DataGridView.ContextMenuStrip;
57 public override DataGridViewCellStyle GetInheritedStyle (DataGridViewCellStyle inheritedCellStyle, int rowIndex, bool includeColors)
59 DataGridViewCellStyle result = new DataGridViewCellStyle (DataGridView.DefaultCellStyle);
61 result.ApplyStyle (DataGridView.RowHeadersDefaultCellStyle);
64 result.ApplyStyle (Style);
69 public override string ToString ()
71 return base.ToString();
74 protected override AccessibleObject CreateAccessibilityInstance ()
76 return new DataGridViewRowHeaderCellAccessibleObject(this);
79 protected override object GetClipboardContent (int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format)
83 if (DataGridView == null)
86 if (rowIndex < 0 || rowIndex >= DataGridView.RowCount)
87 throw new ArgumentOutOfRangeException ("rowIndex");
89 value = GetValue (rowIndex) as string;
91 string table_prefix = string.Empty, cell_prefix = string.Empty, row_prefix = string.Empty;
92 string table_suffix = string.Empty, cell_suffix = string.Empty, row_suffix = string.Empty;
94 if (format == DataFormats.UnicodeText || format == DataFormats.Text) {
95 if (lastCell && !inLastRow)
96 cell_suffix = Environment.NewLine;
99 } else if (format == DataFormats.CommaSeparatedValue) {
100 if (lastCell && !inLastRow)
101 cell_suffix = Environment.NewLine;
104 } else if (format == DataFormats.Html) {
106 table_prefix = "<TABLE>";
111 row_suffix = "</TR>";
113 table_suffix = "</TABLE>";
117 cell_prefix = "<TD ALIGN=\"center\">";
118 cell_suffix = "</TD>";
123 value = "<B>" + value + "</B>";
130 value = string.Empty;
132 value = table_prefix + row_prefix + cell_prefix + value + cell_suffix + row_suffix + table_suffix;
138 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
140 if (DataGridView == null)
141 return Rectangle.Empty;
143 Size s = new Size (11, 18);
144 return new Rectangle (24, (OwningRow.Height - s.Height) / 2, s.Width, s.Height);
147 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
149 if (DataGridView == null || string.IsNullOrEmpty (DataGridView.GetRowInternal (rowIndex).ErrorText))
150 return Rectangle.Empty;
152 Size error_icon = new Size (12, 11);
153 return new Rectangle (new Point (Size.Width - error_icon.Width - 5, (Size.Height - error_icon.Height) / 2), error_icon);
156 protected internal override string GetErrorText (int rowIndex)
158 if (DataGridView == null)
161 return DataGridView.GetRowInternal (rowIndex).ErrorText;
164 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
166 object o = FormattedValue;
169 Size s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
170 s.Height = Math.Max (s.Height, 17);
174 return new Size (39, 17);
177 protected override object GetValue (int rowIndex)
179 if (headerText != null)
185 [MonoInternalNote ("Needs row header cell selected/edit pencil glyphs")]
186 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)
189 DataGridViewPaintParts pre = DataGridViewPaintParts.Background | DataGridViewPaintParts.SelectionBackground;
190 pre = pre & paintParts;
192 base.Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, pre);
194 // Paint content background
195 if ((paintParts & DataGridViewPaintParts.ContentBackground) == DataGridViewPaintParts.ContentBackground) {
196 Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
197 Pen p = ThemeEngine.Current.ResPool.GetPen (color);
198 int x = cellBounds.Left + 6;
200 if (DataGridView.CurrentRow != null && DataGridView.CurrentRow.Index == rowIndex) {
201 DrawRightArrowGlyph (graphics, p, x, cellBounds.Top + (cellBounds.Height / 2) - 4);
205 if (DataGridView.Rows[rowIndex].IsNewRow)
206 DrawNewRowGlyph (graphics, p, x, cellBounds.Top + (cellBounds.Height / 2) - 4);
210 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground) {
211 Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
213 TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl;
215 Rectangle contentbounds = cellBounds;
216 contentbounds.Height -= 2;
217 contentbounds.Width -= 2;
219 if (formattedValue != null)
220 TextRenderer.DrawText (graphics, formattedValue.ToString (), cellStyle.Font, contentbounds, color, flags);
224 DataGridViewPaintParts post = DataGridViewPaintParts.Border | DataGridViewPaintParts.ErrorIcon;
225 post = post & paintParts;
227 base.Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, post);
230 protected override void PaintBorder (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)
232 if (ThemeEngine.Current.DataGridViewRowHeaderCellDrawBorder (this, graphics, cellBounds))
235 Pen p = GetBorderPen ();
237 graphics.DrawLine (p, cellBounds.Left, cellBounds.Top, cellBounds.Left, cellBounds.Bottom - 1);
238 graphics.DrawLine (p, cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, cellBounds.Bottom - 1);
240 if (RowIndex == DataGridView.Rows.Count - 1 || RowIndex == -1)
241 graphics.DrawLine (p, cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right - 1, cellBounds.Bottom - 1);
243 graphics.DrawLine (p, cellBounds.Left + 3, cellBounds.Bottom - 1, cellBounds.Right - 3, cellBounds.Bottom - 1);
246 graphics.DrawLine (p, cellBounds.Left, cellBounds.Top, cellBounds.Right - 1, cellBounds.Top);
249 internal override void PaintPartBackground (Graphics graphics, Rectangle cellBounds, DataGridViewCellStyle style)
251 if (ThemeEngine.Current.DataGridViewRowHeaderCellDrawBackground (this, graphics, cellBounds))
253 base.PaintPartBackground (graphics, cellBounds, style);
256 internal override void PaintPartSelectionBackground (Graphics graphics, Rectangle cellBounds, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle)
258 if (ThemeEngine.Current.DataGridViewRowHeaderCellDrawSelectionBackground (this))
260 base.PaintPartSelectionBackground (graphics, cellBounds, cellState, cellStyle);
263 private void DrawRightArrowGlyph (Graphics g, Pen p, int x, int y)
265 g.DrawLine (p, x, y, x, y + 8);
266 g.DrawLine (p, x + 1, y + 1, x + 1, y + 7);
267 g.DrawLine (p, x + 2, y + 2, x + 2, y + 6);
268 g.DrawLine (p, x + 3, y + 3, x + 3, y + 5);
269 g.DrawLine (p, x + 3, y + 4, x + 4, y + 4);
272 private void DrawNewRowGlyph (Graphics g, Pen p, int x, int y)
274 g.DrawLine (p, x, y + 4, x + 8, y + 4);
275 g.DrawLine (p, x + 4, y, x + 4, y + 8);
276 g.DrawLine (p, x + 1, y + 1, x + 7, y + 7);
277 g.DrawLine (p, x + 7, y + 1, x + 1, y + 7);
280 internal override Rectangle InternalErrorIconsBounds {
281 get { return GetErrorIconBounds (null, null, RowIndex); }
284 protected override bool SetValue (int rowIndex, object value)
286 headerText = (string) value;
290 protected class DataGridViewRowHeaderCellAccessibleObject : DataGridViewCellAccessibleObject {
292 public DataGridViewRowHeaderCellAccessibleObject (DataGridViewRowHeaderCell owner) : base(owner)
296 public override Rectangle Bounds {
297 get { return base.Bounds; }
300 public override string DefaultAction {
301 get { return base.DefaultAction; }
304 public override string Name {
305 get { return base.Name; }
308 public override AccessibleObject Parent {
309 get { return base.Parent; }
312 public override AccessibleRole Role {
313 get { return base.Role; }
316 public override AccessibleStates State {
317 get { return base.State; }
320 public override string Value {
321 get { return base.Value; }
324 public override void DoDefaultAction ()
326 base.DoDefaultAction();
329 public override AccessibleObject Navigate (AccessibleNavigation navigationDirection)
331 return base.Navigate(navigationDirection);
334 public override void Select (AccessibleSelection flags)