Merge pull request #1275 from ranma42/fix-lib64
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewColumnHeaderCell.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) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Pedro Martínez Juliá <pedromj@gmail.com>
24 //
25
26
27 using System;
28 using System.Drawing;
29 using System.ComponentModel;
30
31 namespace System.Windows.Forms {
32
33         public class DataGridViewColumnHeaderCell : DataGridViewHeaderCell {
34
35                 private SortOrder sortGlyphDirection = SortOrder.None;
36                 private object header_text;
37                 
38                 public DataGridViewColumnHeaderCell () {
39                 }
40
41                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
42                 public SortOrder SortGlyphDirection {
43                         get { return sortGlyphDirection; }
44                         set { sortGlyphDirection = value; }
45                 }
46
47                 public override object Clone () {
48                         return MemberwiseClone();
49                 }
50
51                 public override ContextMenuStrip GetInheritedContextMenuStrip (int rowIndex) {
52                         if (rowIndex != -1) {
53                                 throw new ArgumentOutOfRangeException("RowIndex is not -1");
54                         }
55                         if (base.ContextMenuStrip != null) {
56                                 return base.ContextMenuStrip;
57                         }
58                         return base.GetInheritedContextMenuStrip(rowIndex); //////////////////////////////
59                 }
60
61                 public override DataGridViewCellStyle GetInheritedStyle (DataGridViewCellStyle inheritedCellStyle, int rowIndex, bool includeColors)
62                 {
63                         DataGridViewCellStyle result = new DataGridViewCellStyle (DataGridView.DefaultCellStyle);
64         
65                         result.ApplyStyle (DataGridView.ColumnHeadersDefaultCellStyle);
66         
67                         if (HasStyle)
68                                 result.ApplyStyle (Style);
69
70                         return result;
71                 }
72
73                 public override string ToString () {
74                         return GetType().Name;
75                 }
76
77                 protected override AccessibleObject CreateAccessibilityInstance () {
78                         return new DataGridViewColumnHeaderCellAccessibleObject(this);
79                 }
80
81                 protected override object GetClipboardContent (int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format) {
82                         
83                         string value;
84                         
85                         if (rowIndex != -1)
86                                 throw new ArgumentOutOfRangeException ("rowIndex");
87                         
88                         value = GetValue (rowIndex) as string;
89
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;
92
93                         if (format == DataFormats.UnicodeText || format == DataFormats.Text) {
94                                 if (lastCell && !inLastRow)
95                                         cell_suffix = Environment.NewLine;
96                                 else if (!lastCell)
97                                         cell_suffix = "\t";
98                         } else if (format == DataFormats.CommaSeparatedValue) {
99                                 if (lastCell && !inLastRow)
100                                         cell_suffix = Environment.NewLine;
101                                 else if (!lastCell)
102                                         cell_suffix = ",";
103                         } else if (format == DataFormats.Html) {
104                                 if (firstCell) {
105                                         table_prefix = "<TABLE>";
106                                         row_prefix = "<THEAD>";
107                                 }
108
109                                 cell_prefix = "<TH>";
110                                 cell_suffix = "</TH>";
111
112                                 if (lastCell) {
113                                         row_suffix = "</THEAD>";
114                                         if (inLastRow) {
115                                                 table_suffix = "</TABLE>";
116                                         }
117                                 }
118                                 
119                                 if (value == null) {
120                                         value = "&nbsp;";
121                                 }
122                         } else {
123                                 return value;
124                         }
125
126                         if (value == null)
127                                 value = string.Empty;
128
129                         value = table_prefix + row_prefix + cell_prefix + value + cell_suffix + row_suffix + table_suffix;
130
131                         return value;
132                         
133                 }
134
135                 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
136                 {
137                         if (DataGridView == null)
138                                 return Rectangle.Empty;
139
140                         object o = GetValue (-1);
141                         
142                         if (o == null || o.ToString () == string.Empty)
143                                 return Rectangle.Empty;
144                                 
145                         Size s = Size.Empty;
146
147                         if (o != null)
148                                 s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
149
150                         return new Rectangle (3, (DataGridView.ColumnHeadersHeight - s.Height) / 2, s.Width, s.Height);
151                 }
152
153                 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
154                 {
155                         object o = header_text;
156
157                         if (o != null) {
158                                 Size s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
159                                 s.Height = Math.Max (s.Height, 18);
160                                 s.Width += 25;
161                                 return s;
162                         } else
163                                 return new Size (19, 12);
164                 }
165
166                 protected override object GetValue (int rowIndex) {
167                         if (header_text != null)
168                                 return header_text;
169
170                         if (OwningColumn != null && !OwningColumn.HeaderTextSet)
171                                 return OwningColumn.Name;
172                         
173                         return null;
174                 }
175
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) {
177                         // Prepaint
178                         DataGridViewPaintParts pre = DataGridViewPaintParts.Background | DataGridViewPaintParts.SelectionBackground;
179                         pre = pre & paintParts;
180
181                         base.Paint (graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, pre);
182
183                         // Paint content
184                         if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground) {
185                                 Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
186
187                                 TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl;
188
189                                 Rectangle contentbounds = cellBounds;
190                                 contentbounds.Height -= 2;
191                                 contentbounds.Width -= 2;
192
193                                 if (formattedValue != null)
194                                         TextRenderer.DrawText (graphics, formattedValue.ToString (), cellStyle.Font, contentbounds, color, flags);
195                                         
196                                 Point loc = new Point (cellBounds.Right - 14, cellBounds.Y + ((cellBounds.Height - 4) / 2));
197                                 
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);
205                                         }
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);
213                                         }
214                                 }
215                         }
216
217                         // Postpaint
218                         DataGridViewPaintParts post = DataGridViewPaintParts.Border;
219                         
220                         if (this is DataGridViewTopLeftHeaderCell)
221                                 post |= DataGridViewPaintParts.ErrorIcon;
222                                 
223                         post = post & paintParts;
224
225                         base.Paint (graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, post);
226                 }
227
228                 protected override void PaintBorder (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)
229                 {
230                         if (ThemeEngine.Current.DataGridViewColumnHeaderCellDrawBorder (this, graphics, cellBounds))
231                                 return;
232
233                         Pen p = GetBorderPen ();
234
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);
238
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);                           
241                         } else {
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);
244
245                                 if (ColumnIndex == DataGridView.Columns.Count - 1 || ColumnIndex == -1)
246                                         graphics.DrawLine (p, cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, cellBounds.Bottom - 1);
247                                 else
248                                         graphics.DrawLine (p, cellBounds.Right - 1, cellBounds.Top + 3, cellBounds.Right - 1, cellBounds.Bottom - 3);
249                         }
250                 }
251                 
252                 internal override void PaintPartBackground (Graphics graphics, Rectangle cellBounds, DataGridViewCellStyle style)
253                 {
254                         if (ThemeEngine.Current.DataGridViewColumnHeaderCellDrawBackground (this, graphics, cellBounds))
255                                 return;
256                         base.PaintPartBackground (graphics, cellBounds, style);
257                 }
258
259                 protected override bool SetValue (int rowIndex, object value) {
260                         header_text = value;
261                         return true;
262                 }
263
264                 protected class DataGridViewColumnHeaderCellAccessibleObject : DataGridViewCellAccessibleObject {
265
266                         public DataGridViewColumnHeaderCellAccessibleObject (DataGridViewColumnHeaderCell owner) : base (owner) {
267                         }
268
269                         public override Rectangle Bounds {
270                                 get { return base.Bounds; }
271                         }
272
273                         public override string DefaultAction {
274                                 get { return base.DefaultAction; }
275                         }
276
277                         public override string Name {
278                                 get { return base.Name; }
279                         }
280
281                         public override AccessibleObject Parent {
282                                 get { return base.Parent; }
283                         }
284
285                         public override AccessibleRole Role {
286                                 get { return base.Role; }
287                         }
288
289                         public override AccessibleStates State {
290                                 get { return base.State; }
291                         }
292                         
293                         public override string Value {
294                                 get { return base.Value; }
295                         }
296
297                         public override void DoDefaultAction () {
298                                 base.DoDefaultAction();
299                         }
300
301                         public override AccessibleObject Navigate (AccessibleNavigation navigationDirection) {
302                                 return base.Navigate(navigationDirection);
303                         }
304
305                         public override void Select (AccessibleSelection flags)
306                         {
307                                 base.Select (flags);
308                         }
309                 }
310
311         }
312
313 }
314