2007-06-04 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewRow.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.ComponentModel;
30 using System.Drawing;
31 using System.Runtime.InteropServices;
32
33 namespace System.Windows.Forms {
34
35         // XXX [TypeConverter (typeof (DataGridRowConverter))]
36         public class DataGridViewRow : DataGridViewBand {
37
38                 private AccessibleObject accessibilityObject;
39                 private DataGridViewCellCollection cells;
40                 private ContextMenuStrip contextMenuStrip;
41                 private object dataBoundItem;
42                 private int dividerHeight;
43                 private string errorText;
44                 private DataGridViewRowHeaderCell headerCell;
45                 private int height;
46                 private int minimumHeight;
47
48                 public DataGridViewRow ()
49                 {
50                         cells = new DataGridViewCellCollection(this);
51                         minimumHeight = 3;
52                         height = -1;
53                         headerCell = new DataGridViewRowHeaderCell();
54                 }
55
56                 [Browsable (false)]
57                 public AccessibleObject AccessibilityObject {
58                         get { return accessibilityObject; }
59                 }
60
61                 [Browsable (false)]
62                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
63                 public DataGridViewCellCollection Cells {
64                         get { return cells; }
65                 }
66
67                 [DefaultValue (null)]
68                 public override ContextMenuStrip ContextMenuStrip {
69                         get { return contextMenuStrip; }
70                         set {
71                                 if (contextMenuStrip != value) {
72                                         contextMenuStrip = value;
73                                         if (DataGridView != null) {
74                                                 DataGridView.OnRowContextMenuStripChanged(new DataGridViewRowEventArgs(this));
75                                         }
76                                 }
77                         }
78                 }
79
80                 [Browsable (false)]
81                 [EditorBrowsable (EditorBrowsableState.Advanced)]
82                 public object DataBoundItem {
83                         get { return dataBoundItem; }
84                 }
85
86                 [Browsable (true)]
87                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
88                 [NotifyParentProperty (true)]
89                 public override DataGridViewCellStyle DefaultCellStyle {
90                         get { return base.DefaultCellStyle; }
91                         set {
92                                 if (DefaultCellStyle != value) {
93                                         base.DefaultCellStyle = value;
94                                         if (DataGridView != null) {
95                                                 DataGridView.OnRowDefaultCellStyleChanged(new DataGridViewRowEventArgs(this));
96                                         }
97                                 }
98                         }
99                 }
100
101                 [Browsable (false)]
102                 public override bool Displayed {
103                         get { return base.Displayed; }
104                 }
105
106                 [DefaultValue (0)]
107                 [NotifyParentProperty (true)]
108                 public int DividerHeight {
109                         get { return dividerHeight; }
110                         set { dividerHeight = value; }
111                 }
112
113                 [DefaultValue ("")]
114                 [NotifyParentProperty (true)]
115                 public string ErrorText {
116                         get { return errorText; }
117                         set {
118                                 if (errorText != value) {
119                                         errorText = value;
120                                         if (DataGridView != null) {
121                                                 DataGridView.OnRowErrorTextChanged(new DataGridViewRowEventArgs(this));
122                                         }
123                                 }
124                         }
125                 }
126
127                 [Browsable (false)]
128                 public override bool Frozen {
129                         get { return base.Frozen; }
130                         set { base.Frozen = value; }
131                 }
132
133                 [Browsable (false)]
134                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
135                 public DataGridViewRowHeaderCell HeaderCell {
136                         get { return headerCell; }
137                         set {
138                                 if (headerCell != value) {
139                                         headerCell = value;
140                                         if (DataGridView != null) {
141                                                 DataGridView.OnRowHeaderCellChanged(new DataGridViewRowEventArgs(this));
142                                         }
143                                 }
144                         }
145                 }
146
147                 [DefaultValue (22)]
148                 [NotifyParentProperty (true)]
149                 public int Height {
150                         get {
151                                 if (height < 0) {
152                                         if (DefaultCellStyle != null && DefaultCellStyle.Font != null) {
153                                                 return DefaultCellStyle.Font.Height + 9;
154                                         }
155                                         if (InheritedStyle != null && InheritedStyle.Font != null) {
156                                                 return InheritedStyle.Font.Height + 9;
157                                         }
158                                         return System.Windows.Forms.Control.DefaultFont.Height + 9;
159                                 }
160                                 return height;
161                         }
162                         set {
163                                 if (height != value) {
164                                         if (value < minimumHeight) {
165                                                 throw new ArgumentOutOfRangeException("Height can't be less than MinimumHeight.");
166                                         }
167                                         height = value;
168                                         if (DataGridView != null) {
169                                                 DataGridView.OnRowHeightChanged(new DataGridViewRowEventArgs(this));
170                                         }
171                                 }
172                         }
173                 }
174
175                 public override DataGridViewCellStyle InheritedStyle {
176                         get {
177                                 if (DataGridView == null) {
178                                         return DefaultCellStyle;
179                                 }
180                                 else {
181                                         if (DefaultCellStyle == null) {
182                                                 return DataGridView.DefaultCellStyle;
183                                         }
184                                         else {
185                                                 DataGridViewCellStyle style = (DataGridViewCellStyle) DefaultCellStyle.Clone();
186                                                 /////// Combination with dataGridView.DefaultCellStyle
187                                                 return style;
188                                         }
189                                 }
190                         }
191                 }
192
193                 [Browsable (false)]
194                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
195                 public bool IsNewRow {
196                         get {
197                                 if (DataGridView != null && DataGridView.Rows[DataGridView.Rows.Count - 1] == this) {
198                                         return true;
199                                 }
200                                 return false;
201                         }
202                 }
203
204                 [Browsable (false)]
205                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
206                 public int MinimumHeight {
207                         get { return minimumHeight; }
208                         set {
209                                 if (minimumHeight != value) {
210                                         if (value < 2 || value > Int32.MaxValue) {
211                                                 throw new ArgumentOutOfRangeException("MinimumHeight should be between 2 and Int32.MaxValue.");
212                                         }
213                                         minimumHeight = value;
214                                         if (DataGridView != null) {
215                                                 DataGridView.OnRowMinimumHeightChanged(new DataGridViewRowEventArgs(this));
216                                         }
217                                 }
218                         }
219                 }
220
221                 [Browsable (true)]
222                 [DefaultValue (false)]
223                 [NotifyParentProperty (true)]
224                 public override bool ReadOnly {
225                         get { return base.ReadOnly; }
226                         set { base.ReadOnly = value; }
227                 }
228
229                 [NotifyParentProperty (true)]
230                 public override DataGridViewTriState Resizable {
231                         get { return base.Resizable; }
232                         set { base.Resizable = value; }
233                 }
234
235                 public override bool Selected {
236                         get {
237                                 if (Index == -1) {
238                                         throw new InvalidOperationException("The row is a shared row.");
239                                 }
240                                 if (DataGridView == null) {
241                                         throw new InvalidOperationException("The row has not been added to a DataGridView control.");
242                                 }
243                                 return base.Selected;
244                         }
245                         set {
246                                 if (Index == -1) {
247                                         throw new InvalidOperationException("The row is a shared row.");
248                                 }
249                                 if (DataGridView == null) {
250                                         throw new InvalidOperationException("The row has not been added to a DataGridView control.");
251                                 }
252                                 base.Selected = value;
253                                 foreach (DataGridViewCell cell in cells) {
254                                         cell.Selected = value;
255                                 }
256                         }
257                 }
258
259                 public override DataGridViewElementStates State {
260                         get { return base.State; }
261                 }
262
263                 [Browsable (false)]
264                 public override bool Visible {
265                         get { return base.Visible; }
266                         set {
267                                 if (IsNewRow && value == false) {
268                                         throw new InvalidOperationException("Cant make invisible a new row.");
269                                 }
270                                 base.Visible = value;
271                         }
272                 }
273
274                 [EditorBrowsable (EditorBrowsableState.Advanced)]
275                 public virtual DataGridViewAdvancedBorderStyle AdjustRowHeaderBorderStyle (DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStyleInput, DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStylePlaceholder, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedRow, bool isLastVisibleRow)
276                 {
277                         throw new NotImplementedException();
278                 }
279
280                 public override object Clone ()
281                 {
282                         DataGridViewRow row = (DataGridViewRow) MemberwiseClone();
283                         row.cells = new DataGridViewCellCollection(row);
284                         foreach (DataGridViewCell cell in cells) {
285                                 row.cells.Add(cell.Clone() as DataGridViewCell);
286                         }
287                         return row;
288                 }
289
290                 public void CreateCells (DataGridView dataGridView)
291                 {
292                         if (dataGridView == null) {
293                                 throw new ArgumentNullException("DataGridView is null.");
294                         }
295                         if (dataGridView.Rows.Contains(this)) {
296                                 throw new InvalidOperationException("The row already exists in the DataGridView.");
297                         }
298                         DataGridViewCellCollection newCellCollection = new DataGridViewCellCollection(this);
299                         foreach (DataGridViewColumn column in dataGridView.Columns) {
300                                 if (column.CellTemplate == null) {
301                                         throw new InvalidOperationException("Cell template not set in column: " + column.Index.ToString() + ".");
302                                 }
303                                 newCellCollection.Add((DataGridViewCell) column.CellTemplate.Clone());
304                         }
305                         cells = newCellCollection;
306                 }
307
308                 public void CreateCells (DataGridView dataGridView, params object[] values)
309                 {
310                         if (values == null) {
311                                 throw new ArgumentNullException("values is null");
312                         }
313                         CreateCells(dataGridView);
314                         for (int i = 0; i < values.Length; i++) {
315                                 cells[i].Value = values[i];
316                         }
317                 }
318
319                 public ContextMenuStrip GetContextMenuStrip (int rowIndex)
320                 {
321                         if (rowIndex == -1) {
322                                 throw new InvalidOperationException("rowIndex is -1");
323                         }
324                         if (rowIndex < 0 || rowIndex >= DataGridView.Rows.Count) {
325                                 throw new ArgumentOutOfRangeException("rowIndex is out of range");
326                         }
327
328                         return null; // XXX
329                 }
330
331                 public string GetErrorText (int rowIndex)
332                 {
333                         return "";
334                 }
335
336                 public virtual int GetPreferredHeight (int rowIndex, DataGridViewAutoSizeRowMode autoSizeRowMode, bool fixedWidth)
337                 {
338                         throw new NotImplementedException();
339                 }
340
341                 [EditorBrowsable (EditorBrowsableState.Advanced)]
342                 public virtual DataGridViewElementStates GetState (int rowIndex)
343                 {
344                         throw new NotImplementedException();
345                 }
346
347                 public bool SetValues (params object[] values)
348                 {
349                         if (values == null) {
350                                 throw new ArgumentNullException("vues is null");
351                         }
352                         if (DataGridView != null && DataGridView.VirtualMode) {
353                                 throw new InvalidOperationException("DataGridView is operating in virtual mode");
354                         }
355                         /////// COLUMNAS //////////
356                         for (int i = 0; i < values.Length; i++) {
357                                 DataGridViewCell cell = new DataGridViewTextBoxCell();
358                                 cell.Value = values[i];
359                                 cells.Add(cell);
360                         }
361                         
362                         // XXX
363                         return true;
364                 }
365
366                 public override string ToString ()
367                 {
368                         return this.GetType().Name + ", Band Index: " + base.Index.ToString();
369                 }
370
371                 protected virtual AccessibleObject CreateAccessibilityInstance ()
372                 {
373                         return new DataGridViewRowAccessibleObject(this);
374                 }
375
376                 [EditorBrowsable (EditorBrowsableState.Advanced)]
377                 protected virtual DataGridViewCellCollection CreateCellsInstance ()
378                 {
379                         cells = new DataGridViewCellCollection(this);
380                         return cells;
381                 }
382
383                 [EditorBrowsable (EditorBrowsableState.Advanced)]
384                 protected internal virtual void DrawFocus (Graphics graphics, Rectangle clipBounds, Rectangle bounds, int rowIndex, DataGridViewElementStates rowState, DataGridViewCellStyle cellStyle, bool cellsPaintSelectionBackground)
385                 {
386                 }
387
388                 protected internal virtual void Paint (Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirstDisplayedRow, bool isLastVisibleRow)
389                 {
390                 }
391
392                 [EditorBrowsable (EditorBrowsableState.Advanced)]
393                 protected internal virtual void PaintCells (Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirstDisplayedRow, bool isLastVisibleRow, DataGridViewPaintParts paintParts)
394                 {
395                 }
396
397                 [EditorBrowsable (EditorBrowsableState.Advanced)]
398                 protected internal virtual void PaintHeader (Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirstDisplayedRow, bool isLastVisibleRow, DataGridViewPaintParts paintParts)
399                 {
400                 }
401
402                 internal override void SetDataGridView (DataGridView dataGridView)
403                 {
404                         base.SetDataGridView(dataGridView);
405                         headerCell.SetDataGridView(dataGridView);
406                 }
407
408                 internal override void SetState (DataGridViewElementStates state)
409                 {
410                         if (State != state) {
411                                 base.SetState(state);
412                                 if (DataGridView != null) {
413                                         DataGridView.OnRowStateChanged(this.Index, new DataGridViewRowStateChangedEventArgs(this, state));
414                                 }
415                         }
416                 }
417
418                 [ComVisibleAttribute(true)]
419                 protected class DataGridViewRowAccessibleObject : AccessibleObject {
420
421                         private DataGridViewRow dataGridViewRow;
422
423                         public DataGridViewRowAccessibleObject ()
424                         {
425                         }
426
427                         public DataGridViewRowAccessibleObject (DataGridViewRow row)
428                         {
429                                 this.dataGridViewRow = row;
430                         }
431
432                         public override Rectangle Bounds {
433                                 get { throw new NotImplementedException(); }
434                         }
435
436                         public override string DefaultAction {
437                                 get { return "Edit"; }
438                         }
439
440                         public override string Name {
441                                 get { return "Index: " + dataGridViewRow.Index.ToString(); }
442                         }
443
444                         public DataGridViewRow Owner {
445                                 get { return dataGridViewRow; }
446                                 set { dataGridViewRow = value; }
447                         }
448
449                         public override AccessibleObject Parent {
450                                 get { return dataGridViewRow.AccessibilityObject; }
451                         }
452
453                         public override AccessibleRole Role {
454                                 get { return AccessibleRole.Row; }
455                         }
456
457                         public override AccessibleStates State {
458                                 get {
459                                         if (dataGridViewRow.Selected) {
460                                                 return AccessibleStates.Selected;
461                                         }
462                                         else {
463                                                 return AccessibleStates.Focused;
464                                         }
465                                 }
466                         }
467
468                         public override string Value {
469                                 get {
470                                         if (dataGridViewRow.Cells.Count == 0) {
471                                                 return "(Create New)";
472                                         }
473                                         string result = "";
474                                         foreach (DataGridViewCell cell in dataGridViewRow.Cells) {
475                                                 result += cell.AccessibilityObject.Value;
476                                         }
477                                         return result;
478                                 }
479                         }
480
481                         public override AccessibleObject GetChild (int index) {
482                                 throw new NotImplementedException();
483                         }
484
485                         public override int GetChildCount () {
486                                 throw new NotImplementedException();
487                         }
488
489                         public override AccessibleObject GetFocused () {
490                                 return null;
491                         }
492
493                         public override AccessibleObject GetSelected () {
494                                 return null;
495                         }
496
497                         public override AccessibleObject Navigate (AccessibleNavigation navigationDirection) {
498                                 switch (navigationDirection) {
499                                         case AccessibleNavigation.Right:
500                                                 break;
501                                         case AccessibleNavigation.Left:
502                                                 break;
503                                         case AccessibleNavigation.Next:
504                                                 break;
505                                         case AccessibleNavigation.Previous:
506                                                 break;
507                                         case AccessibleNavigation.Up:
508                                                 break;
509                                         case AccessibleNavigation.Down:
510                                                 break;
511                                         default:
512                                                 return null;
513                                 }
514                                 return null;
515                         }
516
517                         public override void Select (AccessibleSelection flags) {
518                                 switch (flags) {
519                                         case AccessibleSelection.TakeFocus:
520                                                 dataGridViewRow.DataGridView.Focus();
521                                                 break;
522                                         case AccessibleSelection.TakeSelection:
523                                                 //dataGridViewRow.Focus();
524                                                 break;
525                                         case AccessibleSelection.AddSelection:
526                                                 dataGridViewRow.DataGridView.SelectedRows.InternalAdd(dataGridViewRow);
527                                                 break;
528                                         case AccessibleSelection.RemoveSelection:
529                                                 dataGridViewRow.DataGridView.SelectedRows.InternalRemove(dataGridViewRow);
530                                                 break;
531                                 }
532                         }
533
534                 }
535
536
537
538         }
539
540 }
541
542 #endif