fix another ERIC
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / DataGridViewRowHeaderCell.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 using System.Drawing;
27
28 namespace System.Windows.Forms {
29
30         public class DataGridViewRowHeaderCell : DataGridViewHeaderCell {
31
32                 private string headerText;
33                 
34                 public DataGridViewRowHeaderCell ()
35                 {
36                 }
37
38                 public override object Clone ()
39                 {
40                         return MemberwiseClone();
41                 }
42
43                 public override ContextMenuStrip GetInheritedContextMenuStrip (int rowIndex)
44                 {
45                         if (DataGridView == null)
46                                 return null;
47
48                         if (rowIndex < 0 || rowIndex >= DataGridView.Rows.Count)
49                                 throw new ArgumentOutOfRangeException ("rowIndex");
50
51                         if (ContextMenuStrip != null)
52                                 return ContextMenuStrip;
53
54                         return DataGridView.ContextMenuStrip;
55                 }
56
57                 public override DataGridViewCellStyle GetInheritedStyle (DataGridViewCellStyle inheritedCellStyle, int rowIndex, bool includeColors)
58                 {
59                         DataGridViewCellStyle result = new DataGridViewCellStyle (DataGridView.DefaultCellStyle);
60
61                         result.ApplyStyle (DataGridView.RowHeadersDefaultCellStyle);
62                                 
63                         if (HasStyle)
64                                 result.ApplyStyle (Style);
65                                 
66                         return result;
67                 }
68
69                 public override string ToString ()
70                 {
71                         return base.ToString();
72                 }
73
74                 protected override AccessibleObject CreateAccessibilityInstance ()
75                 {
76                         return new DataGridViewRowHeaderCellAccessibleObject(this);
77                 }
78
79                 protected override object GetClipboardContent (int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format)
80                 {
81                         string value;
82
83                         if (DataGridView == null)
84                                 return null;
85
86                         if (rowIndex < 0 || rowIndex >= DataGridView.RowCount)
87                                 throw new ArgumentOutOfRangeException ("rowIndex");
88                                 
89                         value = GetValue (rowIndex) as string;
90
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;
93
94                         if (format == DataFormats.UnicodeText || format == DataFormats.Text) {
95                                 if (lastCell && !inLastRow)
96                                         cell_suffix = Environment.NewLine;
97                                 else if (!lastCell)
98                                         cell_suffix = "\t";
99                         } else if (format == DataFormats.CommaSeparatedValue) {
100                                 if (lastCell && !inLastRow)
101                                         cell_suffix = Environment.NewLine;
102                                 else if (!lastCell)
103                                         cell_suffix = ",";
104                         } else if (format == DataFormats.Html) {
105                                 if (inFirstRow) {
106                                         table_prefix = "<TABLE>";
107                                 }
108                                 row_prefix = "<TR>";
109
110                                 if (lastCell) {
111                                         row_suffix = "</TR>";
112                                         if (inLastRow) {
113                                                 table_suffix = "</TABLE>";
114                                         }
115                                 }
116
117                                 cell_prefix = "<TD ALIGN=\"center\">";
118                                 cell_suffix = "</TD>";
119
120                                 if (value == null) {
121                                         value = "&nbsp;";
122                                 } else {
123                                         value = "<B>" + value + "</B>";
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                 {
140                         if (DataGridView == null)
141                                 return Rectangle.Empty;
142
143                         Size s = new Size (11, 18);
144                         return new Rectangle (24, (OwningRow.Height - s.Height) / 2, s.Width, s.Height);
145                 }
146
147                 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
148                 {
149                         if (DataGridView == null || string.IsNullOrEmpty (DataGridView.GetRowInternal (rowIndex).ErrorText))
150                                 return Rectangle.Empty;
151
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);
154                 }
155
156                 protected internal override string GetErrorText (int rowIndex)
157                 {
158                         if (DataGridView == null)
159                                 return string.Empty;
160                                 
161                         return DataGridView.GetRowInternal (rowIndex).ErrorText;
162                 }
163
164                 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
165                 {
166                         object o = FormattedValue;
167
168                         if (o != null) {
169                                 Size s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
170                                 s.Height = Math.Max (s.Height, 17);
171                                 s.Width += 48;
172                                 return s;
173                         } else
174                                 return new Size (39, 17);
175                 }
176
177                 protected override object GetValue (int rowIndex)
178                 {
179                         if (headerText != null)
180                                 return headerText;
181                                 
182                         return null;
183                 }
184
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)
187                 {
188                         // Prepaint
189                         DataGridViewPaintParts pre = DataGridViewPaintParts.Background | DataGridViewPaintParts.SelectionBackground;
190                         pre = pre & paintParts;
191
192                         base.Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, pre);
193
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;
199
200                                 if (DataGridView.CurrentRow != null && DataGridView.CurrentRow.Index == rowIndex) {
201                                         DrawRightArrowGlyph (graphics, p, x, cellBounds.Top + (cellBounds.Height / 2) - 4);
202                                         x += 7;
203                                 }
204
205                                 if (DataGridView.Rows[rowIndex].IsNewRow)
206                                         DrawNewRowGlyph (graphics, p, x, cellBounds.Top + (cellBounds.Height / 2) - 4);
207                         }
208
209                         // Paint content
210                         if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground) {
211                                 Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
212
213                                 TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl;
214
215                                 Rectangle contentbounds = cellBounds;
216                                 contentbounds.Height -= 2;
217                                 contentbounds.Width -= 2;
218
219                                 if (formattedValue != null)
220                                         TextRenderer.DrawText (graphics, formattedValue.ToString (), cellStyle.Font, contentbounds, color, flags);
221                         }
222
223                         // Postpaint
224                         DataGridViewPaintParts post = DataGridViewPaintParts.Border | DataGridViewPaintParts.ErrorIcon;
225                         post = post & paintParts;
226
227                         base.Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, post);
228                 }
229
230                 protected override void PaintBorder (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)
231                 {
232                         if (ThemeEngine.Current.DataGridViewRowHeaderCellDrawBorder (this, graphics, cellBounds))
233                                 return;
234
235                         Pen p = GetBorderPen ();
236
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);
239
240                         if (RowIndex == DataGridView.Rows.Count - 1 || RowIndex == -1)
241                                 graphics.DrawLine (p, cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right - 1, cellBounds.Bottom - 1);
242                         else
243                                 graphics.DrawLine (p, cellBounds.Left + 3, cellBounds.Bottom - 1, cellBounds.Right - 3, cellBounds.Bottom - 1);
244
245                         if (RowIndex == -1)
246                                 graphics.DrawLine (p, cellBounds.Left, cellBounds.Top, cellBounds.Right - 1, cellBounds.Top);
247                 }
248                 
249                 internal override void PaintPartBackground (Graphics graphics, Rectangle cellBounds, DataGridViewCellStyle style)
250                 {
251                         if (ThemeEngine.Current.DataGridViewRowHeaderCellDrawBackground (this, graphics, cellBounds))
252                                 return;
253                         base.PaintPartBackground (graphics, cellBounds, style);
254                 }
255
256                 internal override void PaintPartSelectionBackground (Graphics graphics, Rectangle cellBounds, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle)
257                 {
258                         if (ThemeEngine.Current.DataGridViewRowHeaderCellDrawSelectionBackground (this))
259                                 return;
260                         base.PaintPartSelectionBackground (graphics, cellBounds, cellState, cellStyle);
261                 }
262
263                 private void DrawRightArrowGlyph (Graphics g, Pen p, int x, int y)
264                 {
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);
270                 }
271
272                 private void DrawNewRowGlyph (Graphics g, Pen p, int x, int y)
273                 {
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);
278                 }
279
280                 internal override Rectangle InternalErrorIconsBounds {
281                         get { return GetErrorIconBounds (null, null, RowIndex); }
282                 }
283                 
284                 protected override bool SetValue (int rowIndex, object value)
285                 {
286                         headerText = (string) value;
287                         return true;
288                 }
289
290                 protected class DataGridViewRowHeaderCellAccessibleObject : DataGridViewCellAccessibleObject {
291
292                         public DataGridViewRowHeaderCellAccessibleObject (DataGridViewRowHeaderCell owner) : base(owner)
293                         {
294                         }
295
296                         public override Rectangle Bounds {
297                                 get { return base.Bounds; }
298                         }
299
300                         public override string DefaultAction {
301                                 get { return base.DefaultAction; }
302                         }
303
304                         public override string Name {
305                                 get { return base.Name; }
306                         }
307
308                         public override AccessibleObject Parent {
309                                 get { return base.Parent; }
310                         }
311
312                         public override AccessibleRole Role {
313                                 get { return base.Role; }
314                         }
315
316                         public override AccessibleStates State {
317                                 get { return base.State; }
318                         }
319                         
320                         public override string Value {
321                                 get { return base.Value; }
322                         }
323
324                         public override void DoDefaultAction ()
325                         {
326                                 base.DoDefaultAction();
327                         }
328
329                         public override AccessibleObject Navigate (AccessibleNavigation navigationDirection)
330                         {
331                                 return base.Navigate(navigationDirection);
332                         }
333
334                         public override void Select (AccessibleSelection flags)
335                         {
336                                 base.Select (flags);
337                         }
338                 }
339
340         }
341
342 }