102a688818eff404fa5a020e2ef99b2abef7eaf7
[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                 [MonoTODO ("Paint sort arrows")]
157                 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) {
158                         // Prepaint
159                         DataGridViewPaintParts pre = DataGridViewPaintParts.Background | DataGridViewPaintParts.SelectionBackground;
160                         pre = pre & paintParts;
161
162                         base.Paint (graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, pre);
163
164                         // Paint content
165                         if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground) {
166                                 Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
167
168                                 TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl;
169
170                                 Rectangle contentbounds = cellBounds;
171                                 contentbounds.Height -= 2;
172                                 contentbounds.Width -= 2;
173
174                                 if (formattedValue != null)
175                                         TextRenderer.DrawText (graphics, formattedValue.ToString (), cellStyle.Font, contentbounds, color, flags);
176                         }
177
178                         // Postpaint
179                         DataGridViewPaintParts post = DataGridViewPaintParts.Border;
180                         post = post & paintParts;
181
182                         base.Paint (graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, post);
183                 }
184
185                 protected override bool SetValue (int rowIndex, object value) {
186                         header_text = value;
187                         return true;
188                 }
189
190                 protected class DataGridViewColumnHeaderCellAccessibleObject : DataGridViewCellAccessibleObject {
191
192                         public DataGridViewColumnHeaderCellAccessibleObject (DataGridViewColumnHeaderCell owner) : base (owner) {
193                         }
194
195                         public override Rectangle Bounds {
196                                 get { return base.Bounds; }
197                         }
198
199                         public override string DefaultAction {
200                                 get { return base.DefaultAction; }
201                         }
202
203                         public override string Name {
204                                 get { return base.Name; }
205                         }
206
207                         public override AccessibleObject Parent {
208                                 get { return base.Parent; }
209                         }
210
211                         public override AccessibleRole Role {
212                                 get { return base.Role; }
213                         }
214
215                         public override AccessibleStates State {
216                                 get { return base.State; }
217                         }
218                         
219                         public override string Value {
220                                 get { return base.Value; }
221                         }
222
223                         public override void DoDefaultAction () {
224                                 base.DoDefaultAction();
225                         }
226
227                         public override AccessibleObject Navigate (AccessibleNavigation navigationDirection) {
228                                 return base.Navigate(navigationDirection);
229                         }
230
231                         public override void Select (AccessibleSelection flags)
232                         {
233                                 base.Select (flags);
234                         }
235                 }
236
237         }
238
239 }
240
241 #endif