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>
29 using System.ComponentModel;
31 namespace System.Windows.Forms {
33 public class DataGridViewColumnHeaderCell : DataGridViewHeaderCell {
35 private SortOrder sortGlyphDirection = SortOrder.None;
36 private object header_text;
38 public DataGridViewColumnHeaderCell () {
41 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
42 public SortOrder SortGlyphDirection {
43 get { return sortGlyphDirection; }
44 set { sortGlyphDirection = value; }
47 public override object Clone () {
48 return MemberwiseClone();
51 public override ContextMenuStrip GetInheritedContextMenuStrip (int rowIndex) {
53 throw new ArgumentOutOfRangeException("RowIndex is not -1");
55 if (base.ContextMenuStrip != null) {
56 return base.ContextMenuStrip;
58 return base.GetInheritedContextMenuStrip(rowIndex); //////////////////////////////
61 public override DataGridViewCellStyle GetInheritedStyle (DataGridViewCellStyle inheritedCellStyle, int rowIndex, bool includeColors)
63 DataGridViewCellStyle result = new DataGridViewCellStyle (DataGridView.DefaultCellStyle);
65 result.ApplyStyle (DataGridView.ColumnHeadersDefaultCellStyle);
68 result.ApplyStyle (Style);
73 public override string ToString () {
74 return GetType().Name;
77 protected override AccessibleObject CreateAccessibilityInstance () {
78 return new DataGridViewColumnHeaderCellAccessibleObject(this);
81 protected override object GetClipboardContent (int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format) {
86 throw new ArgumentOutOfRangeException ("rowIndex");
88 value = GetValue (rowIndex) as string;
90 string table_prefix = string.Empty, cell_prefix = string.Empty, row_prefix = string.Empty;
91 string table_suffix = string.Empty, cell_suffix = string.Empty, row_suffix = string.Empty;
93 if (format == DataFormats.UnicodeText || format == DataFormats.Text) {
94 if (lastCell && !inLastRow)
95 cell_suffix = Environment.NewLine;
98 } else if (format == DataFormats.CommaSeparatedValue) {
99 if (lastCell && !inLastRow)
100 cell_suffix = Environment.NewLine;
103 } else if (format == DataFormats.Html) {
105 table_prefix = "<TABLE>";
106 row_prefix = "<THEAD>";
109 cell_prefix = "<TH>";
110 cell_suffix = "</TH>";
113 row_suffix = "</THEAD>";
115 table_suffix = "</TABLE>";
127 value = string.Empty;
129 value = table_prefix + row_prefix + cell_prefix + value + cell_suffix + row_suffix + table_suffix;
135 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
137 if (DataGridView == null)
138 return Rectangle.Empty;
140 object o = GetValue (-1);
142 if (o == null || o.ToString () == string.Empty)
143 return Rectangle.Empty;
148 s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
150 return new Rectangle (3, (DataGridView.ColumnHeadersHeight - s.Height) / 2, s.Width, s.Height);
153 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
155 object o = header_text;
158 Size s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
159 s.Height = Math.Max (s.Height, 18);
163 return new Size (19, 12);
166 protected override object GetValue (int rowIndex) {
167 if (header_text != null)
170 if (OwningColumn != null && !OwningColumn.HeaderTextSet)
171 return OwningColumn.Name;
176 protected override void Paint (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) {
178 DataGridViewPaintParts pre = DataGridViewPaintParts.Background | DataGridViewPaintParts.SelectionBackground;
179 pre = pre & paintParts;
181 base.Paint (graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, pre);
184 if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground) {
185 Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
187 TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl;
189 Rectangle contentbounds = cellBounds;
190 contentbounds.Height -= 2;
191 contentbounds.Width -= 2;
193 if (formattedValue != null)
194 TextRenderer.DrawText (graphics, formattedValue.ToString (), cellStyle.Font, contentbounds, color, flags);
196 Point loc = new Point (cellBounds.Right - 14, cellBounds.Y + ((cellBounds.Height - 4) / 2));
198 if (sortGlyphDirection == SortOrder.Ascending) {
199 using (Pen p = new Pen (color)) {
200 graphics.DrawLine (p, loc.X + 4, loc.Y + 1, loc.X + 4, loc.Y + 2);
201 graphics.DrawLine (p, loc.X + 3, loc.Y + 2, loc.X + 5, loc.Y + 2);
202 graphics.DrawLine (p, loc.X + 2, loc.Y + 3, loc.X + 6, loc.Y + 3);
203 graphics.DrawLine (p, loc.X + 1, loc.Y + 4, loc.X + 7, loc.Y + 4);
204 graphics.DrawLine (p, loc.X + 0, loc.Y + 5, loc.X + 8, loc.Y + 5);
206 } else if (sortGlyphDirection == SortOrder.Descending) {
207 using (Pen p = new Pen (color)) {
208 graphics.DrawLine (p, loc.X + 4, loc.Y + 5, loc.X + 4, loc.Y + 4);
209 graphics.DrawLine (p, loc.X + 3, loc.Y + 4, loc.X + 5, loc.Y + 4);
210 graphics.DrawLine (p, loc.X + 2, loc.Y + 3, loc.X + 6, loc.Y + 3);
211 graphics.DrawLine (p, loc.X + 1, loc.Y + 2, loc.X + 7, loc.Y + 2);
212 graphics.DrawLine (p, loc.X + 0, loc.Y + 1, loc.X + 8, loc.Y + 1);
218 DataGridViewPaintParts post = DataGridViewPaintParts.Border;
220 if (this is DataGridViewTopLeftHeaderCell)
221 post |= DataGridViewPaintParts.ErrorIcon;
223 post = post & paintParts;
225 base.Paint (graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, post);
228 protected override void PaintBorder (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)
230 if (ThemeEngine.Current.DataGridViewColumnHeaderCellDrawBorder (this, graphics, cellBounds))
233 Pen p = GetBorderPen ();
235 if (ColumnIndex == -1) {
236 graphics.DrawLine (p, cellBounds.Left, cellBounds.Top, cellBounds.Left, cellBounds.Bottom - 1);
237 graphics.DrawLine (p, cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, cellBounds.Bottom - 1);
239 graphics.DrawLine (p, cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right - 1, cellBounds.Bottom - 1);
240 graphics.DrawLine (p, cellBounds.Left, cellBounds.Top, cellBounds.Right - 1, cellBounds.Top);
242 graphics.DrawLine (p, cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right - 1, cellBounds.Bottom - 1);
243 graphics.DrawLine (p, cellBounds.Left, cellBounds.Top, cellBounds.Right - 1, cellBounds.Top);
245 if (ColumnIndex == DataGridView.Columns.Count - 1 || ColumnIndex == -1)
246 graphics.DrawLine (p, cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, cellBounds.Bottom - 1);
248 graphics.DrawLine (p, cellBounds.Right - 1, cellBounds.Top + 3, cellBounds.Right - 1, cellBounds.Bottom - 3);
252 internal override void PaintPartBackground (Graphics graphics, Rectangle cellBounds, DataGridViewCellStyle style)
254 if (ThemeEngine.Current.DataGridViewColumnHeaderCellDrawBackground (this, graphics, cellBounds))
256 base.PaintPartBackground (graphics, cellBounds, style);
259 protected override bool SetValue (int rowIndex, object value) {
264 protected class DataGridViewColumnHeaderCellAccessibleObject : DataGridViewCellAccessibleObject {
266 public DataGridViewColumnHeaderCellAccessibleObject (DataGridViewColumnHeaderCell owner) : base (owner) {
269 public override Rectangle Bounds {
270 get { return base.Bounds; }
273 public override string DefaultAction {
274 get { return base.DefaultAction; }
277 public override string Name {
278 get { return base.Name; }
281 public override AccessibleObject Parent {
282 get { return base.Parent; }
285 public override AccessibleRole Role {
286 get { return base.Role; }
289 public override AccessibleStates State {
290 get { return base.State; }
293 public override string Value {
294 get { return base.Value; }
297 public override void DoDefaultAction () {
298 base.DoDefaultAction();
301 public override AccessibleObject Navigate (AccessibleNavigation navigationDirection) {
302 return base.Navigate(navigationDirection);
305 public override void Select (AccessibleSelection flags)