2008-04-11 Jonathan Pobst <monkey@jpobst.com>
[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 #if NET_2_0
28
29 using System;
30 using System.Drawing;
31 using System.ComponentModel;
32
33 namespace System.Windows.Forms {
34
35         public class DataGridViewColumnHeaderCell : DataGridViewHeaderCell {
36
37                 private SortOrder sortGlyphDirection = SortOrder.None;
38                 private object header_text;
39                 
40                 public DataGridViewColumnHeaderCell () {
41                 }
42
43                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
44                 public SortOrder SortGlyphDirection {
45                         get { return sortGlyphDirection; }
46                         set { sortGlyphDirection = value; }
47                 }
48
49                 public override object Clone () {
50                         return MemberwiseClone();
51                 }
52
53                 public override ContextMenuStrip GetInheritedContextMenuStrip (int rowIndex) {
54                         if (rowIndex != -1) {
55                                 throw new ArgumentOutOfRangeException("RowIndex is not -1");
56                         }
57                         if (base.ContextMenuStrip != null) {
58                                 return base.ContextMenuStrip;
59                         }
60                         return base.GetInheritedContextMenuStrip(rowIndex); //////////////////////////////
61                 }
62
63                 public override DataGridViewCellStyle GetInheritedStyle (DataGridViewCellStyle inheritedCellStyle, int rowIndex, bool includeColors) {
64                         
65                         DataGridViewCellStyle result;
66                         
67                         if (HasStyle) {
68                                 result = Style;
69                         } else {
70                                 result = DataGridView.ColumnHeadersDefaultCellStyle;
71                         }
72                         
73                         return result;                  
74                 }
75
76                 public override string ToString () {
77                         return GetType().Name;
78                 }
79
80                 protected override AccessibleObject CreateAccessibilityInstance () {
81                         return new DataGridViewColumnHeaderCellAccessibleObject(this);
82                 }
83
84                 protected override object GetClipboardContent (int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format) {
85                         
86                         string value;
87                         
88                         if (rowIndex != -1)
89                                 throw new ArgumentOutOfRangeException ("rowIndex");
90                         
91                         value = GetValue (rowIndex) as string;
92
93                         string table_prefix = string.Empty, cell_prefix = string.Empty, row_prefix = string.Empty;
94                         string table_suffix = string.Empty, cell_suffix = string.Empty, row_suffix = string.Empty;
95
96                         if (format == DataFormats.UnicodeText || format == DataFormats.Text) {
97                                 if (lastCell && !inLastRow)
98                                         cell_suffix = Environment.NewLine;
99                                 else if (!lastCell)
100                                         cell_suffix = "\t";
101                         } else if (format == DataFormats.CommaSeparatedValue) {
102                                 if (lastCell && !inLastRow)
103                                         cell_suffix = Environment.NewLine;
104                                 else if (!lastCell)
105                                         cell_suffix = ",";
106                         } else if (format == DataFormats.Html) {
107                                 if (firstCell) {
108                                         table_prefix = "<TABLE>";
109                                         row_prefix = "<THEAD>";
110                                 }
111
112                                 cell_prefix = "<TH>";
113                                 cell_suffix = "</TH>";
114
115                                 if (lastCell) {
116                                         row_suffix = "</THEAD>";
117                                         if (inLastRow) {
118                                                 table_suffix = "</TABLE>";
119                                         }
120                                 }
121                                 
122                                 if (value == null) {
123                                         value = "&nbsp;";
124                                 }
125                         } else {
126                                 return value;
127                         }
128
129                         if (value == null)
130                                 value = string.Empty;
131
132                         value = table_prefix + row_prefix + cell_prefix + value + cell_suffix + row_suffix + table_suffix;
133
134                         return value;
135                         
136                 }
137
138                 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex) {
139                         return new Rectangle();
140                 }
141
142                 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize) {
143                         throw new NotImplementedException();
144                 }
145
146                 protected override object GetValue (int rowIndex) {
147                         if (header_text != null)
148                                 return header_text;
149
150                         if (OwningColumn != null && !OwningColumn.HeaderTextSet)
151                                 return OwningColumn.Name;
152                         
153                         return null;
154                 }
155
156                 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) {
157                         // Prepaint
158                         DataGridViewPaintParts pre = DataGridViewPaintParts.Background | DataGridViewPaintParts.SelectionBackground;
159                         pre = pre & paintParts;
160
161                         base.Paint (graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, pre);
162
163                         // Paint content
164                         if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground) {
165                                 Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
166
167                                 TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl;
168
169                                 Rectangle contentbounds = cellBounds;
170                                 contentbounds.Height -= 2;
171                                 contentbounds.Width -= 2;
172
173                                 if (formattedValue != null)
174                                         TextRenderer.DrawText (graphics, formattedValue.ToString (), cellStyle.Font, contentbounds, color, flags);
175                                         
176                                 Point loc = new Point (cellBounds.Right - 14, cellBounds.Y + ((cellBounds.Height - 4) / 2));
177                                 
178                                 if (sortGlyphDirection == SortOrder.Ascending) {
179                                         using (Pen p = new Pen (color)) {
180                                                 graphics.DrawLine (p, loc.X + 4, loc.Y + 1, loc.X + 4, loc.Y + 2);
181                                                 graphics.DrawLine (p, loc.X + 3, loc.Y + 2, loc.X + 5, loc.Y + 2);
182                                                 graphics.DrawLine (p, loc.X + 2, loc.Y + 3, loc.X + 6, loc.Y + 3);
183                                                 graphics.DrawLine (p, loc.X + 1, loc.Y + 4, loc.X + 7, loc.Y + 4);
184                                                 graphics.DrawLine (p, loc.X + 0, loc.Y + 5, loc.X + 8, loc.Y + 5);
185                                         }
186                                 } else if (sortGlyphDirection == SortOrder.Descending) {
187                                         using (Pen p = new Pen (color)) {
188                                                 graphics.DrawLine (p, loc.X + 4, loc.Y + 5, loc.X + 4, loc.Y + 4);
189                                                 graphics.DrawLine (p, loc.X + 3, loc.Y + 4, loc.X + 5, loc.Y + 4);
190                                                 graphics.DrawLine (p, loc.X + 2, loc.Y + 3, loc.X + 6, loc.Y + 3);
191                                                 graphics.DrawLine (p, loc.X + 1, loc.Y + 2, loc.X + 7, loc.Y + 2);
192                                                 graphics.DrawLine (p, loc.X + 0, loc.Y + 1, loc.X + 8, loc.Y + 1);
193                                         }
194                                 }
195                         }
196
197                         // Postpaint
198                         DataGridViewPaintParts post = DataGridViewPaintParts.Border;
199                         post = post & paintParts;
200
201                         base.Paint (graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, post);
202                 }
203
204                 protected override bool SetValue (int rowIndex, object value) {
205                         header_text = value;
206                         return true;
207                 }
208
209                 protected class DataGridViewColumnHeaderCellAccessibleObject : DataGridViewCellAccessibleObject {
210
211                         public DataGridViewColumnHeaderCellAccessibleObject (DataGridViewColumnHeaderCell owner) : base (owner) {
212                         }
213
214                         public override Rectangle Bounds {
215                                 get { return base.Bounds; }
216                         }
217
218                         public override string DefaultAction {
219                                 get { return base.DefaultAction; }
220                         }
221
222                         public override string Name {
223                                 get { return base.Name; }
224                         }
225
226                         public override AccessibleObject Parent {
227                                 get { return base.Parent; }
228                         }
229
230                         public override AccessibleRole Role {
231                                 get { return base.Role; }
232                         }
233
234                         public override AccessibleStates State {
235                                 get { return base.State; }
236                         }
237                         
238                         public override string Value {
239                                 get { return base.Value; }
240                         }
241
242                         public override void DoDefaultAction () {
243                                 base.DoDefaultAction();
244                         }
245
246                         public override AccessibleObject Navigate (AccessibleNavigation navigationDirection) {
247                                 return base.Navigate(navigationDirection);
248                         }
249
250                         public override void Select (AccessibleSelection flags)
251                         {
252                                 base.Select (flags);
253                         }
254                 }
255
256         }
257
258 }
259
260 #endif