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 //	Ivan N. Zlatev  <contact@i-nz.net>
25 //
26 
27 
28 
29 using System;
30 using System.Drawing;
31 using System.ComponentModel;
32 using System.Runtime.InteropServices;
33 
34 namespace System.Windows.Forms {
35 
36 	[TypeConverter (typeof (DataGridViewCellConverter))]
37 	public abstract class DataGridViewCell : DataGridViewElement, ICloneable, IDisposable
38 	{
39 		private DataGridView dataGridViewOwner;
40 
41 		private AccessibleObject accessibilityObject;
42 		private int columnIndex;
43 		private ContextMenuStrip contextMenuStrip;
44 		private bool displayed;
45 		private string errorText;
46 		private bool isInEditMode;
47 		private DataGridViewRow owningRow;
48 		private DataGridViewTriState readOnly;
49 		private bool selected;
50 		private DataGridViewCellStyle style;
51 		private object tag;
52 		private string toolTipText;
53 		private object valuex;
54 		private Type valueType;
55 
DataGridViewCell()56 		protected DataGridViewCell ()
57 		{
58 			columnIndex = -1;
59 			dataGridViewOwner = null;
60 			errorText = string.Empty;
61 		}
62 
~DataGridViewCell()63 		~DataGridViewCell ()
64 		{
65 			Dispose(false);
66 		}
67 
68 		[Browsable (false)]
69 		public AccessibleObject AccessibilityObject {
70 			get {
71 				if (accessibilityObject == null) {
72 					accessibilityObject = CreateAccessibilityInstance();
73 				}
74 				return accessibilityObject;
75 			}
76 		}
77 
78 		public int ColumnIndex {
79 			get {
80 				if (DataGridView == null)
81 					return -1;
82 				return columnIndex;
83 			}
84 		}
85 
86 		[Browsable (false)]
87 		public Rectangle ContentBounds {
88 			get {
89 				return GetContentBounds (RowIndex);
90 			}
91 		}
92 
93 		[DefaultValue (null)]
94 		public virtual ContextMenuStrip ContextMenuStrip {
95 			get { return contextMenuStrip; }
96 			set { contextMenuStrip = value; }
97 		}
98 
99 		[Browsable (false)]
100 		public virtual object DefaultNewRowValue {
101 			get { return null; }
102 		}
103 
104 		[Browsable (false)]
105 		public virtual bool Displayed {
106 			get { return displayed; }
107 		}
108 
109 		[Browsable (false)]
110 		[EditorBrowsable (EditorBrowsableState.Advanced)]
111 		public object EditedFormattedValue {
112 			get {
113 				return GetEditedFormattedValue (RowIndex, DataGridViewDataErrorContexts.Formatting);
114 			}
115 		}
116 
117 		[Browsable (false)]
118 		[EditorBrowsable (EditorBrowsableState.Advanced)]
119 		public virtual Type EditType {
120 			get {
121 				return typeof (DataGridViewTextBoxEditingControl);
122 			}
123 		}
124 
125 		[Browsable (false)]
126 		[EditorBrowsable (EditorBrowsableState.Advanced)]
127 		public Rectangle ErrorIconBounds {
128 			get {
129 				if (this is DataGridViewTopLeftHeaderCell)
130 					return GetErrorIconBounds (null, null, RowIndex);
131 
132 				if (DataGridView == null || columnIndex < 0)
133 					throw new InvalidOperationException ();
134 				if (RowIndex < 0 || RowIndex >= DataGridView.Rows.Count)
135 					throw new ArgumentOutOfRangeException ("rowIndex", "Specified argument was out of the range of valid values.");
136 
137 				return GetErrorIconBounds (null, null, RowIndex);
138 			}
139 		}
140 
141 		[Browsable (false)]
142 		public string ErrorText {
143 			get {
144 				if (this is DataGridViewTopLeftHeaderCell)
145 					return GetErrorText (-1);
146 
147 				if (OwningRow == null)
148 					return string.Empty;
149 
150 				return GetErrorText (OwningRow.Index);
151 			}
152 			set {
153 				if (errorText != value) {
154 					errorText = value;
155 					OnErrorTextChanged(new DataGridViewCellEventArgs(ColumnIndex, RowIndex));
156 				}
157 			}
158 		}
159 
160 		[Browsable (false)]
161 		public object FormattedValue {
162 			get {
163 				if (DataGridView == null)
164 					return null;
165 
166 				DataGridViewCellStyle style = InheritedStyle;
167 				return GetFormattedValue (Value, RowIndex, ref style, null, null, DataGridViewDataErrorContexts.Formatting);
168 			}
169 		}
170 
171 		[Browsable (false)]
172 		public virtual Type FormattedValueType {
173 			get { return null; }
174 		}
175 
176 		[Browsable (false)]
177 		public virtual bool Frozen {
178 			get {
179 				if (DataGridView == null)
180 					return false;
181 
182 				if (RowIndex >= 0)
183 					return OwningRow.Frozen && OwningColumn.Frozen;
184 
185 				return false;
186 			}
187 		}
188 
189 		[Browsable (false)]
190 		public bool HasStyle {
191 			get { return style != null; }
192 		}
193 
194 		[Browsable (false)]
195 		public DataGridViewElementStates InheritedState {
196 			get {
197 				return GetInheritedState (RowIndex);
198 			}
199 		}
200 
201 		[Browsable (false)]
202 		public DataGridViewCellStyle InheritedStyle {
203 			get {
204 				return GetInheritedStyle (null, RowIndex, true);
205 			}
206 		}
207 
208 		[Browsable (false)]
209 		public bool IsInEditMode {
210 			get {
211 				if (DataGridView == null)
212 					return false;
213 
214 				if (RowIndex == -1)
215 					throw new InvalidOperationException ("Operation cannot be performed on a cell of a shared row.");
216 
217 				return isInEditMode;
218 			}
219 		}
220 
221 		[Browsable (false)]
222 		[EditorBrowsable (EditorBrowsableState.Advanced)]
223 		public DataGridViewColumn OwningColumn {
224 			get {
225 				if (DataGridView == null || columnIndex < 0 || columnIndex >= DataGridView.Columns.Count)
226 					return null;
227 
228 				return DataGridView.Columns[columnIndex];
229 			}
230 		}
231 
232 		[Browsable (false)]
233 		[EditorBrowsable (EditorBrowsableState.Advanced)]
234 		public DataGridViewRow OwningRow {
235 			get { return owningRow; }
236 		}
237 
238 		[Browsable (false)]
239 		public Size PreferredSize {
240 			get {
241 				if (DataGridView == null)
242 					return new Size (-1, -1);
243 
244 				return GetPreferredSize (Hwnd.GraphicsContext, InheritedStyle, RowIndex, Size.Empty);
245 			}
246 		}
247 
248 		[Browsable (false)]
249 		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
250 		public virtual bool ReadOnly {
251 			get {
252 				if (DataGridView != null && DataGridView.ReadOnly)
253 					return true;
254 
255 				if (readOnly != DataGridViewTriState.NotSet)
256 					return readOnly == DataGridViewTriState.True;
257 
258 				if (OwningRow != null && !OwningRow.IsShared && OwningRow.ReadOnly)
259 					return true;
260 
261 				if (OwningColumn != null && OwningColumn.ReadOnly)
262 					return true;
263 
264 				return false;
265 			}
266 			set {
267 				readOnly = value ? DataGridViewTriState.True : DataGridViewTriState.False;
268 				if (value) {
269 					SetState (DataGridViewElementStates.ReadOnly | State);
270 				} else {
271 					SetState (~DataGridViewElementStates.ReadOnly & State);
272 				}
273 			}
274 		}
275 
276 		[Browsable (false)]
277 		public virtual bool Resizable {
278 			get {
279 				if (DataGridView == null)
280 					return false;
281 
282 				// Shared cells aren't resizable
283 				if (RowIndex == -1 || columnIndex == -1)
284 					return false;
285 
286 				return OwningRow.Resizable == DataGridViewTriState.True || OwningColumn.Resizable == DataGridViewTriState.True;
287 			}
288 		}
289 
290 		[Browsable (false)]
291 		public int RowIndex {
292 			get {
293 				if (owningRow == null) {
294 					return -1;
295 				}
296 				return owningRow.Index;
297 			}
298 		}
299 
300 		[Browsable (false)]
301 		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
302 		public virtual bool Selected {
303 			get {
304 				if (selected)
305 					return true;
306 
307 				if (DataGridView != null) {
308 					if (RowIndex >= 0 && RowIndex < DataGridView.Rows.Count && DataGridView.Rows [RowIndex].Selected)
309 						return true;
310 
311 					if (ColumnIndex >= 0 && ColumnIndex < DataGridView.Columns.Count && DataGridView.Columns [ColumnIndex].Selected)
312 						return true;
313 				}
314 
315 				return false;
316 			}
317 			set {
318 				bool changed = selected != value;
319 				selected = value;
320 
321 				if (value != ((State & DataGridViewElementStates.Selected) != 0))
322 					SetState(State ^ DataGridViewElementStates.Selected);
323 
324 				// If our row is selected, unselect it and select
325 				// the first cell in it that isn't us
326 				if (!selected && OwningRow != null && OwningRow.Selected) {
327 					OwningRow.Selected = false;
328 
329 					if (columnIndex != 0 && OwningRow.Cells.Count > 0)
330 						OwningRow.Cells[0].Selected = true;
331 					else if (OwningRow.Cells.Count > 1)
332 						OwningRow.Cells[1].Selected = true;
333 				}
334 
335 				if (changed && DataGridView != null && DataGridView.IsHandleCreated)
336 					DataGridView.Invalidate ();
337 			}
338 		}
339 
340 		[Browsable (false)]
341 		public Size Size {
342 			get {
343 				if (DataGridView == null)
344 					return new Size (-1, -1);
345 
346 				return GetSize (RowIndex);
347 			}
348 		}
349 
350 		[Browsable (true)]
351 		public DataGridViewCellStyle Style {
352 			get {
353 				if (style == null) {
354 					style = new DataGridViewCellStyle();
355 					style.StyleChanged += OnStyleChanged;
356 				}
357 				return style;
358 			}
359 			set { style = value; }
360 		}
361 
362 		[Bindable (true, BindingDirection.OneWay)]
363 		[DefaultValue (null)]
364 		[Localizable (false)]
365 		[TypeConverter ("System.ComponentModel.StringConverter, " + Consts.AssemblySystem)]
366 		public object Tag {
367 			get { return tag; }
368 			set { tag = value; }
369 		}
370 
371 		[Browsable (false)]
372 		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
373 		public string ToolTipText {
374 			get { return toolTipText == null ? string.Empty : toolTipText; }
375 			set { toolTipText = value; }
376 		}
377 
378 		[Browsable (false)]
379 		public object Value {
380 			get {
381 				return GetValue (RowIndex);
382 			}
383 			set {
384 				SetValue (RowIndex, value);
385 			}
386 		}
387 
388 		[Browsable (false)]
389 		public virtual Type ValueType {
390 			get {
391 				if (valueType == null) {
392 					if (DataProperty != null)
393 						valueType = DataProperty.PropertyType;
394 					else if (OwningColumn != null)
395 						valueType = OwningColumn.ValueType;
396 				}
397 
398 				return valueType;
399 			}
400 			set { valueType = value; }
401 		}
402 
403 		[Browsable (false)]
404 		public virtual bool Visible {
405 			get {
406 				// This is independent from State...
407 				DataGridViewColumn col = OwningColumn;
408 				DataGridViewRow row = OwningRow;
409 
410 				bool rowVisible = true, colVisible = true;
411 
412 				if (row == null && col == null)
413 					return false;
414 
415 				if (row != null) {
416 					rowVisible = !row.IsShared && row.Visible;
417 				}
418 
419 				if (col != null) {
420 					colVisible = col.Index >= 0 && col.Visible;
421 				}
422 
423 				return rowVisible && colVisible;
424 			}
425 		}
426 
SetState(DataGridViewElementStates state)427 		internal override void SetState (DataGridViewElementStates state) {
428 			base.SetState (state);
429 			if (DataGridView != null)
430 				DataGridView.OnCellStateChangedInternal (new DataGridViewCellStateChangedEventArgs (this, state));
431 		}
432 
433 		[EditorBrowsable (EditorBrowsableState.Advanced)]
AdjustCellBorderStyle(DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStyleInput, DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStylePlaceholder, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)434 		public virtual DataGridViewAdvancedBorderStyle AdjustCellBorderStyle (DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStyleInput,	DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStylePlaceholder, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow) {
435 			if (dataGridViewAdvancedBorderStyleInput.All == DataGridViewAdvancedCellBorderStyle.Single) {
436 
437 				dataGridViewAdvancedBorderStylePlaceholder.Left = (isFirstDisplayedColumn && singleVerticalBorderAdded) ? DataGridViewAdvancedCellBorderStyle.Single : DataGridViewAdvancedCellBorderStyle.None;
438 				dataGridViewAdvancedBorderStylePlaceholder.Right = DataGridViewAdvancedCellBorderStyle.Single;
439 				dataGridViewAdvancedBorderStylePlaceholder.Top = (isFirstDisplayedRow && singleHorizontalBorderAdded)? DataGridViewAdvancedCellBorderStyle.Single : DataGridViewAdvancedCellBorderStyle.None;
440 				dataGridViewAdvancedBorderStylePlaceholder.Bottom = DataGridViewAdvancedCellBorderStyle.Single;
441 				return dataGridViewAdvancedBorderStylePlaceholder;
442 			}
443 
444 			if ((dataGridViewAdvancedBorderStyleInput.All == DataGridViewAdvancedCellBorderStyle.NotSet) && (DataGridView != null) && (DataGridView.AdvancedCellBorderStyle == dataGridViewAdvancedBorderStyleInput)) {
445 				if (DataGridView.CellBorderStyle == DataGridViewCellBorderStyle.SingleVertical) {
446 					dataGridViewAdvancedBorderStylePlaceholder.Left = (isFirstDisplayedColumn && singleVerticalBorderAdded) ? DataGridViewAdvancedCellBorderStyle.Single : DataGridViewAdvancedCellBorderStyle.None;
447 					dataGridViewAdvancedBorderStylePlaceholder.Right = DataGridViewAdvancedCellBorderStyle.Single;
448 					dataGridViewAdvancedBorderStylePlaceholder.Top = DataGridViewAdvancedCellBorderStyle.None;
449 					dataGridViewAdvancedBorderStylePlaceholder.Bottom = DataGridViewAdvancedCellBorderStyle.None;
450 					return dataGridViewAdvancedBorderStylePlaceholder;
451 				}
452 				if (DataGridView.CellBorderStyle == DataGridViewCellBorderStyle.SingleHorizontal) {
453 					dataGridViewAdvancedBorderStylePlaceholder.Left = DataGridViewAdvancedCellBorderStyle.None;
454 					dataGridViewAdvancedBorderStylePlaceholder.Right = DataGridViewAdvancedCellBorderStyle.None;
455 					dataGridViewAdvancedBorderStylePlaceholder.Top = (isFirstDisplayedRow && singleHorizontalBorderAdded)? DataGridViewAdvancedCellBorderStyle.Single : DataGridViewAdvancedCellBorderStyle.None;
456 					dataGridViewAdvancedBorderStylePlaceholder.Bottom = DataGridViewAdvancedCellBorderStyle.Single;
457 					return dataGridViewAdvancedBorderStylePlaceholder;
458 				}
459 			}
460 
461 			return dataGridViewAdvancedBorderStyleInput;
462 		}
463 
Clone()464 		public virtual object Clone ()
465 		{
466 			DataGridViewCell result = (DataGridViewCell) Activator.CreateInstance (GetType ());
467 			result.accessibilityObject = this.accessibilityObject;
468 			result.columnIndex = this.columnIndex;
469 			result.displayed = this.displayed;
470 			result.errorText = this.errorText;
471 			result.isInEditMode = this.isInEditMode;
472 			result.owningRow = this.owningRow;
473 			result.readOnly = this.readOnly;
474 			result.selected = this.selected;
475 			result.style = this.style;
476 			result.tag = this.tag;
477 			result.toolTipText = this.toolTipText;
478 			result.valuex = this.valuex;
479 			result.valueType = this.valueType;
480 			return result;
481 		}
482 
483 		[EditorBrowsable (EditorBrowsableState.Advanced)]
DetachEditingControl()484 		public virtual void DetachEditingControl ()
485 		{
486 		}
487 
Dispose()488 		public void Dispose ()
489 		{
490 		}
491 
GetContentBounds(int rowIndex)492 		public Rectangle GetContentBounds (int rowIndex)
493 		{
494 			if (DataGridView == null)
495 				return Rectangle.Empty;
496 
497 			return GetContentBounds (Hwnd.GraphicsContext, InheritedStyle, rowIndex);
498 		}
499 
GetEditedFormattedValue(int rowIndex, DataGridViewDataErrorContexts context)500 		public object GetEditedFormattedValue (int rowIndex, DataGridViewDataErrorContexts context)
501 		{
502 			if (DataGridView == null)
503 				return null;
504 
505 			if (rowIndex < 0 || rowIndex >= DataGridView.RowCount)
506 				throw new ArgumentOutOfRangeException ("rowIndex", "Specified argument was out of the range of valid values.");
507 
508 			// If we are in edit mode, this returns the value of the editing control
509 			// If we aren't in edit mode, return the cell's value
510 			// Basically, return what the user is currently seeing
511 			if (IsInEditMode) {
512 				if (DataGridView.EditingControl != null)
513 					return (DataGridView.EditingControl as IDataGridViewEditingControl).GetEditingControlFormattedValue (context);
514 				else
515 					return (this as IDataGridViewEditingCell).GetEditingCellFormattedValue (context);
516 			}
517 
518 			DataGridViewCellStyle style = InheritedStyle;
519 
520 			return GetFormattedValue (GetValue (rowIndex), rowIndex, ref style, null, null, context);
521 		}
522 
GetInheritedContextMenuStrip(int rowIndex)523 		public virtual ContextMenuStrip GetInheritedContextMenuStrip (int rowIndex)
524 		{
525 			if (DataGridView == null)
526 				return null;
527 
528 			if (rowIndex < 0 || rowIndex >= DataGridView.Rows.Count)
529 				throw new ArgumentOutOfRangeException ("rowIndex");
530 			if (columnIndex < 0)
531 				throw new InvalidOperationException ("cannot perform this on a column header cell");
532 
533 			if (contextMenuStrip != null)
534 				return contextMenuStrip;
535 			if (OwningRow.ContextMenuStrip != null)
536 				return OwningRow.ContextMenuStrip;
537 			if (OwningColumn.ContextMenuStrip != null)
538 				return OwningColumn.ContextMenuStrip;
539 
540 			return DataGridView.ContextMenuStrip;
541 		}
542 
GetInheritedState(int rowIndex)543 		public virtual DataGridViewElementStates GetInheritedState (int rowIndex)
544 		{
545 
546 			if (DataGridView == null && rowIndex != -1)
547 				throw new ArgumentException ("msg?");
548 
549 			if (DataGridView != null && (rowIndex < 0 || rowIndex >= DataGridView.Rows.Count))
550 				throw new ArgumentOutOfRangeException ("rowIndex", "Specified argument was out of the range of valid values.");
551 
552 			DataGridViewElementStates result;
553 
554 			result = DataGridViewElementStates.ResizableSet | State;
555 
556 			DataGridViewColumn col = OwningColumn;
557 			DataGridViewRow row = OwningRow;
558 
559 			if (DataGridView == null) {
560 				if (row != null) {
561 					if (row.Resizable == DataGridViewTriState.True)
562 						result |= DataGridViewElementStates.Resizable;
563 
564 					if (row.Visible)
565 						result |= DataGridViewElementStates.Visible;
566 
567 					if (row.ReadOnly)
568 						result |= DataGridViewElementStates.ReadOnly;
569 
570 					if (row.Frozen)
571 						result |= DataGridViewElementStates.Frozen;
572 
573 					if (row.Displayed)
574 						result |= DataGridViewElementStates.Displayed;
575 
576 					if (row.Selected)
577 						result |= DataGridViewElementStates.Selected;
578 				}
579 
580 				return result;
581 			}
582 
583 			if (col != null) {
584 				if (col.Resizable == DataGridViewTriState.True && row.Resizable == DataGridViewTriState.True)
585 					result |= DataGridViewElementStates.Resizable;
586 
587 				if (col.Visible && row.Visible)
588 					result |= DataGridViewElementStates.Visible;
589 
590 				if (col.ReadOnly || row.ReadOnly)
591 					result |= DataGridViewElementStates.ReadOnly;
592 
593 				if (col.Frozen || row.Frozen)
594 					result |= DataGridViewElementStates.Frozen;
595 
596 				if (col.Displayed && row.Displayed)
597 					result |= DataGridViewElementStates.Displayed;
598 
599 				if (col.Selected || row.Selected)
600 					result |= DataGridViewElementStates.Selected;
601 			}
602 
603 			return result;
604 		}
605 
GetInheritedStyle(DataGridViewCellStyle inheritedCellStyle, int rowIndex, bool includeColors)606 		public virtual DataGridViewCellStyle GetInheritedStyle (DataGridViewCellStyle inheritedCellStyle, int rowIndex, bool includeColors) {
607 			/*
608 			 * System.InvalidOperationException :: The cell has no associated System.Windows.Forms.DataGridView, or the cell's System.Windows.Forms.DataGridViewCell.ColumnIndex is less than 0.
609 			 * System.ArgumentOutOfRangeException :: rowIndex is less than 0, or greater than or equal to the number of rows in the parent System.Windows.Forms.DataGridView.
610 			 * */
611 
612 			if (DataGridView == null)
613 				throw new InvalidOperationException ("Cell is not in a DataGridView. The cell cannot retrieve the inherited cell style.");
614 
615 			if (rowIndex < 0 || rowIndex >= DataGridView.Rows.Count)
616 				throw new ArgumentOutOfRangeException ("rowIndex");
617 
618 			// Start with DataGridView.DefaultCellStyle
619 			DataGridViewCellStyle result = new DataGridViewCellStyle (DataGridView.DefaultCellStyle);
620 
621 			// If we have a column, add OwningColumn.DefaultCellStyle
622 			if (OwningColumn != null)
623 				result.ApplyStyle (OwningColumn.DefaultCellStyle);
624 
625 			// Add DataGridView.RowsDefaultCellStyle
626 			result.ApplyStyle (DataGridView.RowsDefaultCellStyle);
627 
628 			// If we are an odd row, add DataGridView.AlternatingRowsDefaultCellStyle
629 			if (rowIndex % 2 == 1)
630 				result.ApplyStyle (DataGridView.AlternatingRowsDefaultCellStyle);
631 
632 			// Add Row.DefaultCellStyle
633 			result.ApplyStyle (DataGridView.Rows.SharedRow (rowIndex).DefaultCellStyle);
634 
635 			// Add cell's style
636 			if (HasStyle)
637 				result.ApplyStyle (Style);
638 
639 			return result;
640 		}
641 
642 		[EditorBrowsable (EditorBrowsableState.Advanced)]
InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)643 		public virtual void InitializeEditingControl (int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) {
644 			if (DataGridView == null || DataGridView.EditingControl == null) {
645 				throw new InvalidOperationException("No editing control defined");
646 			}
647 		}
648 
KeyEntersEditMode(KeyEventArgs e)649 		public virtual bool KeyEntersEditMode (KeyEventArgs e)
650 		{
651 			return false;
652 		}
653 
654 		[EditorBrowsable (EditorBrowsableState.Advanced)]
MeasureTextHeight(Graphics graphics, string text, Font font, int maxWidth, TextFormatFlags flags)655 		public static int MeasureTextHeight (Graphics graphics, string text, Font font, int maxWidth, TextFormatFlags flags)
656 		{
657 			if (graphics == null)
658 				throw new ArgumentNullException ("Graphics argument null");
659 			if (font == null)
660 				throw new ArgumentNullException ("Font argument null");
661 			if (maxWidth < 1)
662 				throw new ArgumentOutOfRangeException ("maxWidth is less than 1.");
663 
664 			return TextRenderer.MeasureText (graphics, text, font, new Size (maxWidth, 0), flags).Height;
665 		}
666 
667 		[MonoTODO ("does not use widthTruncated parameter")]
668 		[EditorBrowsable (EditorBrowsableState.Advanced)]
MeasureTextHeight(Graphics graphics, string text, Font font, int maxWidth, TextFormatFlags flags, out bool widthTruncated)669 		public static int MeasureTextHeight (Graphics graphics, string text, Font font, int maxWidth, TextFormatFlags flags, out bool widthTruncated)
670 		{
671 			widthTruncated = false;
672 			return TextRenderer.MeasureText (graphics, text, font, new Size (maxWidth, 0), flags).Height;
673 		}
674 
675 		[EditorBrowsable (EditorBrowsableState.Advanced)]
MeasureTextPreferredSize(Graphics graphics, string text, Font font, float maxRatio, TextFormatFlags flags)676 		public static Size MeasureTextPreferredSize (Graphics graphics, string text, Font font, float maxRatio, TextFormatFlags flags)
677 		{
678 			if (graphics == null)
679 				throw new ArgumentNullException ("Graphics argument null");
680 			if (font == null)
681 				throw new ArgumentNullException ("Font argument null");
682 			if (maxRatio <= 0)
683 				throw new ArgumentOutOfRangeException ("maxRatio is less than or equals to 0.");
684 
685 			// I couldn't find a case where maxRatio
686 			// affected anything on MS
687 			return MeasureTextSize (graphics, text, font, flags);
688 		}
689 
690 		[EditorBrowsable (EditorBrowsableState.Advanced)]
MeasureTextSize(Graphics graphics, string text, Font font, TextFormatFlags flags)691 		public static Size MeasureTextSize (Graphics graphics, string text, Font font, TextFormatFlags flags)
692 		{
693 			return TextRenderer.MeasureText (graphics, text, font, Size.Empty, flags);
694 		}
695 
696 		[EditorBrowsable (EditorBrowsableState.Advanced)]
MeasureTextWidth(Graphics graphics, string text, Font font, int maxHeight, TextFormatFlags flags)697 		public static int MeasureTextWidth (Graphics graphics, string text, Font font, int maxHeight, TextFormatFlags flags)
698 		{
699 			if (graphics == null)
700 				throw new ArgumentNullException ("Graphics argument null");
701 			if (font == null)
702 				throw new ArgumentNullException ("Font argument null");
703 			if (maxHeight < 1)
704 				throw new ArgumentOutOfRangeException ("maxHeight is less than 1.");
705 
706 			return TextRenderer.MeasureText (graphics, text, font, new Size (0, maxHeight), flags).Width;
707 		}
708 
ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)709 		public virtual object ParseFormattedValue (object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
710 		{
711 			if (cellStyle == null)
712 				throw new ArgumentNullException ("cellStyle is null.");
713 			if (FormattedValueType == null)
714 				throw new FormatException ("The System.Windows.Forms.DataGridViewCell.FormattedValueType property value is null.");
715 			if (formattedValue == null)
716 				throw new ArgumentException ("formattedValue is null.");
717 			if (ValueType == null)
718 				throw new FormatException ("valuetype is null");
719 			if (!FormattedValueType.IsAssignableFrom (formattedValue.GetType ()))
720 				throw new ArgumentException ("formattedValue is not of formattedValueType.");
721 
722 			if (formattedValueTypeConverter == null)
723 				formattedValueTypeConverter = FormattedValueTypeConverter;
724 			if (valueTypeConverter == null)
725 				valueTypeConverter = ValueTypeConverter;
726 
727 			if (valueTypeConverter != null && valueTypeConverter.CanConvertFrom (FormattedValueType))
728 				return valueTypeConverter.ConvertFrom (formattedValue);
729 			if (formattedValueTypeConverter != null && formattedValueTypeConverter.CanConvertTo (ValueType))
730 				return formattedValueTypeConverter.ConvertTo (formattedValue, ValueType);
731 			return Convert.ChangeType (formattedValue, ValueType);
732 		}
733 
734 		[EditorBrowsable (EditorBrowsableState.Advanced)]
PositionEditingControl(bool setLocation, bool setSize, Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)735 		public virtual void PositionEditingControl (bool setLocation, bool setSize, Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
736 		{
737 			if (DataGridView.EditingControl != null) {
738 				if (setLocation && setSize)
739 					DataGridView.EditingControl.Bounds = cellBounds;
740 				else if (setLocation)
741 					DataGridView.EditingControl.Location = cellBounds.Location;
742 				else if (setSize)
743 					DataGridView.EditingControl.Size = cellBounds.Size;
744 			}
745 		}
746 
747 		[EditorBrowsable (EditorBrowsableState.Advanced)]
PositionEditingPanel(Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)748 		public virtual Rectangle PositionEditingPanel (Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow) {
749 			throw new NotImplementedException();
750 		}
751 
ToString()752 		public override string ToString () {
753 			return String.Format("{0} {{ ColumnIndex={1}, RowIndex={2} }}", this.GetType().Name, ColumnIndex, RowIndex);
754 		}
755 
BorderWidths(DataGridViewAdvancedBorderStyle advancedBorderStyle)756 		protected virtual Rectangle BorderWidths (DataGridViewAdvancedBorderStyle advancedBorderStyle)
757 		{
758 			Rectangle r = Rectangle.Empty;
759 
760 			r.X = BorderToWidth (advancedBorderStyle.Left);
761 			r.Y = BorderToWidth (advancedBorderStyle.Top);
762 			r.Width = BorderToWidth (advancedBorderStyle.Right);
763 			r.Height = BorderToWidth (advancedBorderStyle.Bottom);
764 
765 			if (OwningColumn != null)
766 				r.Width += OwningColumn.DividerWidth;
767 			if (OwningRow != null)
768 				r.Height += OwningRow.DividerHeight;
769 
770 			return r;
771 		}
772 
BorderToWidth(DataGridViewAdvancedCellBorderStyle style)773 		private int BorderToWidth (DataGridViewAdvancedCellBorderStyle style)
774 		{
775 			switch (style) {
776 				case DataGridViewAdvancedCellBorderStyle.None:
777 					return 0;
778 				case DataGridViewAdvancedCellBorderStyle.NotSet:
779 				case DataGridViewAdvancedCellBorderStyle.Single:
780 				case DataGridViewAdvancedCellBorderStyle.Inset:
781 				case DataGridViewAdvancedCellBorderStyle.Outset:
782 				case DataGridViewAdvancedCellBorderStyle.OutsetPartial:
783 				default:
784 					return 1;
785 				case DataGridViewAdvancedCellBorderStyle.InsetDouble:
786 				case DataGridViewAdvancedCellBorderStyle.OutsetDouble:
787 					return 2;
788 			}
789 		}
790 
ClickUnsharesRow(DataGridViewCellEventArgs e)791 		protected virtual bool ClickUnsharesRow (DataGridViewCellEventArgs e)
792 		{
793 			return false;
794 		}
795 
ContentClickUnsharesRow(DataGridViewCellEventArgs e)796 		protected virtual bool ContentClickUnsharesRow (DataGridViewCellEventArgs e)
797 		{
798 			return false;
799 		}
800 
ContentDoubleClickUnsharesRow(DataGridViewCellEventArgs e)801 		protected virtual bool ContentDoubleClickUnsharesRow (DataGridViewCellEventArgs e)
802 		{
803 			return false;
804 		}
805 
CreateAccessibilityInstance()806 		protected virtual AccessibleObject CreateAccessibilityInstance () {
807 			return new DataGridViewCellAccessibleObject(this);
808 		}
809 
Dispose(bool disposing)810 		protected virtual void Dispose (bool disposing) {
811 		}
812 
DoubleClickUnsharesRow(DataGridViewCellEventArgs e)813 		protected virtual bool DoubleClickUnsharesRow (DataGridViewCellEventArgs e)
814 		{
815 			return false;
816 		}
817 
EnterUnsharesRow(int rowIndex, bool throughMouseClick)818 		protected virtual bool EnterUnsharesRow (int rowIndex, bool throughMouseClick)
819 		{
820 			return false;
821 		}
822 
GetClipboardContent(int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format)823 		protected virtual object GetClipboardContent (int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format) {
824 			if (DataGridView == null)
825 				return null;
826 
827 			if (rowIndex < 0 || rowIndex >= DataGridView.RowCount)
828 				throw new ArgumentOutOfRangeException ("rowIndex", "Specified argument was out of the range of valid values.");
829 
830 			string value = null;
831 
832 			if (Selected) {
833 //				DataGridViewCellStyle style = GetInheritedStyle (null, rowIndex, false);
834 				value = GetEditedFormattedValue (rowIndex, DataGridViewDataErrorContexts.ClipboardContent | DataGridViewDataErrorContexts.Formatting) as string;
835 			}
836 
837 			if (value == null)
838 				value = string.Empty;
839 
840 			string table_prefix = string.Empty, cell_prefix = string.Empty, row_prefix = string.Empty;
841 			string table_suffix = string.Empty, cell_suffix = string.Empty, row_suffix = string.Empty;
842 
843 			if (format == DataFormats.UnicodeText || format == DataFormats.Text) {
844 				if (lastCell && !inLastRow)
845 					cell_suffix = Environment.NewLine;
846 				else if (!lastCell)
847 					cell_suffix = "\t";
848 			} else if (format == DataFormats.CommaSeparatedValue) {
849 				if (lastCell && !inLastRow)
850 					cell_suffix = Environment.NewLine;
851 				else if (!lastCell)
852 					cell_suffix = ",";
853 			} else if (format == DataFormats.Html) {
854 				if (inFirstRow && firstCell)
855 					table_prefix = "<TABLE>";
856 				if (inLastRow && lastCell)
857 					table_suffix = "</TABLE>";
858 				if (firstCell)
859 					row_prefix = "<TR>";
860 				if (lastCell)
861 					row_suffix = "</TR>";
862 				cell_prefix = "<TD>";
863 				cell_suffix = "</TD>";
864 
865 				if (!Selected) {
866 					value = "&nbsp;";
867 				}
868 			} else {
869 				return value;
870 			}
871 
872 			value = table_prefix + row_prefix + cell_prefix + value + cell_suffix + row_suffix + table_suffix;
873 
874 			return value;
875 		}
876 
GetClipboardContentInternal(int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format)877 		internal object GetClipboardContentInternal (int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format) {
878 			return GetClipboardContent (rowIndex, firstCell, lastCell, inFirstRow, inLastRow, format);
879 		}
880 
GetContentBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)881 		protected virtual Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex) {
882 			return Rectangle.Empty;
883 		}
884 
GetErrorIconBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)885 		protected virtual Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
886 		{
887 			return Rectangle.Empty;
888 		}
889 
GetErrorText(int rowIndex)890 		protected internal virtual string GetErrorText (int rowIndex)
891 		{
892 			return errorText;
893 		}
894 
GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)895 		protected virtual object GetFormattedValue (object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
896 		{
897 			if (DataGridView == null)
898 				return null;
899 
900 			if (rowIndex < 0 || rowIndex >= DataGridView.RowCount)
901 				throw new ArgumentOutOfRangeException ("rowIndex");
902 
903 			// Give the user a chance to custom format
904 			if (!(this is DataGridViewRowHeaderCell)) {
905 				DataGridViewCellFormattingEventArgs e = new DataGridViewCellFormattingEventArgs (ColumnIndex, rowIndex, value, FormattedValueType, cellStyle);
906 				DataGridView.OnCellFormattingInternal (e);
907 
908 				if (e.FormattingApplied)
909 					return e.Value;
910 
911 				cellStyle = e.CellStyle;
912 				value = e.Value;
913 			}
914 
915 			if (value == null || (cellStyle != null && value == cellStyle.DataSourceNullValue)) {
916 				if (FormattedValueType == typeof (string))
917 					return String.Empty;
918 			}
919 
920 			if (FormattedValueType == typeof(string) && value is IFormattable && !String.IsNullOrEmpty (cellStyle.Format))
921 				return ((IFormattable) value).ToString (cellStyle.Format, cellStyle.FormatProvider);
922 			if (value != null && FormattedValueType.IsAssignableFrom (value.GetType()))
923 				return value;
924 
925 			if (formattedValueTypeConverter == null)
926 				formattedValueTypeConverter = FormattedValueTypeConverter;
927 			if (valueTypeConverter == null)
928 				valueTypeConverter = ValueTypeConverter;
929 
930 			if (valueTypeConverter != null && valueTypeConverter.CanConvertTo (FormattedValueType))
931 				return valueTypeConverter.ConvertTo (value, FormattedValueType);
932 			if (formattedValueTypeConverter != null && formattedValueTypeConverter.CanConvertFrom (ValueType))
933 				return formattedValueTypeConverter.ConvertFrom (value);
934 
935 			return Convert.ChangeType (value, FormattedValueType);
936 		}
937 
GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)938 		protected virtual Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
939 		{
940 			return new Size (-1, -1);
941 		}
942 
GetSize(int rowIndex)943 		protected virtual Size GetSize (int rowIndex)
944 		{
945 			if (RowIndex == -1)
946 				throw new InvalidOperationException ("Getting the Size property of a cell in a shared row is not a valid operation.");
947 
948 			return new Size (OwningColumn.Width, OwningRow.Height);
949 		}
950 
GetValue(int rowIndex)951 		protected virtual object GetValue (int rowIndex) {
952 			if (DataGridView != null && (RowIndex < 0 || RowIndex >= DataGridView.Rows.Count))
953 				throw new ArgumentOutOfRangeException ("rowIndex", "Specified argument was out of the range of valid values.");
954 
955 			if (OwningRow != null && DataGridView != null && OwningRow.Index == DataGridView.NewRowIndex)
956 				return DefaultNewRowValue;
957 
958 			if (DataProperty != null && OwningRow.DataBoundItem != null)
959 				return DataProperty.GetValue (OwningRow.DataBoundItem);
960 
961 			if (valuex != null)
962 				return valuex;
963 
964 			DataGridViewCellValueEventArgs dgvcvea = new DataGridViewCellValueEventArgs (columnIndex, rowIndex);
965 			if (DataGridView != null)
966 				DataGridView.OnCellValueNeeded (dgvcvea);
967 			return dgvcvea.Value;
968 		}
969 
970 		private PropertyDescriptor DataProperty {
971 			get {
972 				if (OwningColumn != null && OwningColumn.DataColumnIndex != -1 &&
973 				    DataGridView != null && DataGridView.DataManager != null)
974 					return DataGridView.DataManager.GetItemProperties()[OwningColumn.DataColumnIndex];
975 				return null;
976 			}
977 		}
978 
979 		private TypeConverter FormattedValueTypeConverter {
980 			get {
981 				if (FormattedValueType != null)
982 					return TypeDescriptor.GetConverter (FormattedValueType);
983 				return null;
984 			}
985 		}
986 
987 		private TypeConverter ValueTypeConverter {
988 			get {
989 				if (DataProperty != null && DataProperty.Converter != null)
990 					return DataProperty.Converter;
991 				if (Value != null)
992 					return TypeDescriptor.GetConverter (Value);
993 				if (ValueType != null)
994 					return TypeDescriptor.GetConverter (ValueType);
995 				return null;
996 			}
997 		}
998 
KeyDownUnsharesRow(KeyEventArgs e, int rowIndex)999 		protected virtual bool KeyDownUnsharesRow (KeyEventArgs e, int rowIndex)
1000 		{
1001 			return false;
1002 		}
1003 
KeyPressUnsharesRow(KeyPressEventArgs e, int rowIndex)1004 		protected virtual bool KeyPressUnsharesRow (KeyPressEventArgs e, int rowIndex)
1005 		{
1006 			return false;
1007 		}
1008 
KeyUpUnsharesRow(KeyEventArgs e, int rowIndex)1009 		protected virtual bool KeyUpUnsharesRow (KeyEventArgs e, int rowIndex)
1010 		{
1011 			return false;
1012 		}
1013 
LeaveUnsharesRow(int rowIndex, bool throughMouseClick)1014 		protected virtual bool LeaveUnsharesRow (int rowIndex, bool throughMouseClick)
1015 		{
1016 			return false;
1017 		}
1018 
MouseClickUnsharesRow(DataGridViewCellMouseEventArgs e)1019 		protected virtual bool MouseClickUnsharesRow (DataGridViewCellMouseEventArgs e)
1020 		{
1021 			return false;
1022 		}
1023 
MouseDoubleClickUnsharesRow(DataGridViewCellMouseEventArgs e)1024 		protected virtual bool MouseDoubleClickUnsharesRow (DataGridViewCellMouseEventArgs e)
1025 		{
1026 			return false;
1027 		}
1028 
MouseDownUnsharesRow(DataGridViewCellMouseEventArgs e)1029 		protected virtual bool MouseDownUnsharesRow (DataGridViewCellMouseEventArgs e)
1030 		{
1031 			return false;
1032 		}
1033 
MouseEnterUnsharesRow(int rowIndex)1034 		protected virtual bool MouseEnterUnsharesRow (int rowIndex)
1035 		{
1036 			return false;
1037 		}
1038 
MouseLeaveUnsharesRow(int rowIndex)1039 		protected virtual bool MouseLeaveUnsharesRow (int rowIndex)
1040 		{
1041 			return false;
1042 		}
1043 
MouseMoveUnsharesRow(DataGridViewCellMouseEventArgs e)1044 		protected virtual bool MouseMoveUnsharesRow (DataGridViewCellMouseEventArgs e)
1045 		{
1046 			return false;
1047 		}
1048 
MouseUpUnsharesRow(DataGridViewCellMouseEventArgs e)1049 		protected virtual bool MouseUpUnsharesRow (DataGridViewCellMouseEventArgs e)
1050 		{
1051 			return false;
1052 		}
1053 
OnClick(DataGridViewCellEventArgs e)1054 		protected virtual void OnClick (DataGridViewCellEventArgs e) {
1055 		}
1056 
OnClickInternal(DataGridViewCellEventArgs e)1057 		internal void OnClickInternal (DataGridViewCellEventArgs e) {
1058 			OnClick (e);
1059 		}
1060 
OnContentClick(DataGridViewCellEventArgs e)1061 		protected virtual void OnContentClick (DataGridViewCellEventArgs e) {
1062 		}
1063 
OnContentClickInternal(DataGridViewCellEventArgs e)1064 		internal void OnContentClickInternal (DataGridViewCellEventArgs e) {
1065 			OnContentClick (e);
1066 		}
1067 
OnContentDoubleClick(DataGridViewCellEventArgs e)1068 		protected virtual void OnContentDoubleClick (DataGridViewCellEventArgs e) {
1069 		}
1070 
OnContentDoubleClickInternal(DataGridViewCellEventArgs e)1071 		internal void OnContentDoubleClickInternal (DataGridViewCellEventArgs e) {
1072 			OnContentDoubleClick (e);
1073 		}
1074 
OnDataGridViewChanged()1075 		protected override void OnDataGridViewChanged () {
1076 		}
1077 
OnDataGridViewChangedInternal()1078 		internal void OnDataGridViewChangedInternal () {
1079 			OnDataGridViewChanged ();
1080 		}
1081 
OnDoubleClick(DataGridViewCellEventArgs e)1082 		protected virtual void OnDoubleClick (DataGridViewCellEventArgs e) {
1083 		}
1084 
OnDoubleClickInternal(DataGridViewCellEventArgs e)1085 		internal void OnDoubleClickInternal (DataGridViewCellEventArgs e) {
1086 			OnDoubleClick (e);
1087 		}
1088 
OnEnter(int rowIndex, bool throughMouseClick)1089 		protected virtual void OnEnter (int rowIndex, bool throughMouseClick) {
1090 		}
1091 
OnEnterInternal(int rowIndex, bool throughMouseClick)1092 		internal void OnEnterInternal (int rowIndex, bool throughMouseClick) {
1093 			OnEnter (rowIndex, throughMouseClick);
1094 		}
1095 
OnKeyDown(KeyEventArgs e, int rowIndex)1096 		protected virtual void OnKeyDown (KeyEventArgs e, int rowIndex) {
1097 		}
1098 
OnKeyDownInternal(KeyEventArgs e, int rowIndex)1099 		internal void OnKeyDownInternal (KeyEventArgs e, int rowIndex) {
1100 			OnKeyDown (e, rowIndex);
1101 		}
1102 
OnKeyPress(KeyPressEventArgs e, int rowIndex)1103 		protected virtual void OnKeyPress (KeyPressEventArgs e, int rowIndex) {
1104 		}
1105 
OnKeyPressInternal(KeyPressEventArgs e, int rowIndex)1106 		internal void OnKeyPressInternal (KeyPressEventArgs e, int rowIndex) {
1107 			OnKeyPress (e, rowIndex);
1108 		}
1109 
OnKeyUp(KeyEventArgs e, int rowIndex)1110 		protected virtual void OnKeyUp (KeyEventArgs e, int rowIndex) {
1111 		}
1112 
OnKeyUpInternal(KeyEventArgs e, int rowIndex)1113 		internal void OnKeyUpInternal (KeyEventArgs e, int rowIndex) {
1114 			OnKeyUp (e, rowIndex);
1115 		}
1116 
OnLeave(int rowIndex, bool throughMouseClick)1117 		protected virtual void OnLeave (int rowIndex, bool throughMouseClick) {
1118 		}
1119 
OnLeaveInternal(int rowIndex, bool throughMouseClick)1120 		internal void OnLeaveInternal (int rowIndex, bool throughMouseClick) {
1121 			OnLeave (rowIndex, throughMouseClick);
1122 		}
1123 
OnMouseClick(DataGridViewCellMouseEventArgs e)1124 		protected virtual void OnMouseClick (DataGridViewCellMouseEventArgs e) {
1125 		}
1126 
OnMouseClickInternal(DataGridViewCellMouseEventArgs e)1127 		internal void OnMouseClickInternal (DataGridViewCellMouseEventArgs e) {
1128 			OnMouseClick (e);
1129 		}
1130 
OnMouseDoubleClick(DataGridViewCellMouseEventArgs e)1131 		protected virtual void OnMouseDoubleClick (DataGridViewCellMouseEventArgs e) {
1132 		}
1133 
OnMouseDoubleClickInternal(DataGridViewCellMouseEventArgs e)1134 		internal void OnMouseDoubleClickInternal (DataGridViewCellMouseEventArgs e) {
1135 			OnMouseDoubleClick (e);
1136 		}
1137 
OnMouseDown(DataGridViewCellMouseEventArgs e)1138 		protected virtual void OnMouseDown (DataGridViewCellMouseEventArgs e) {
1139 		}
1140 
OnMouseDownInternal(DataGridViewCellMouseEventArgs e)1141 		internal void OnMouseDownInternal (DataGridViewCellMouseEventArgs e) {
1142 			OnMouseDown (e);
1143 		}
1144 
OnMouseEnter(int rowIndex)1145 		protected virtual void OnMouseEnter (int rowIndex) {
1146 		}
1147 
OnMouseEnterInternal(int rowIndex)1148 		internal void OnMouseEnterInternal (int rowIndex) {
1149 			OnMouseEnter (rowIndex) ;
1150 		}
1151 
OnMouseLeave(int rowIndex)1152 		protected virtual void OnMouseLeave (int rowIndex) {
1153 		}
1154 
OnMouseLeaveInternal(int e)1155 		internal void OnMouseLeaveInternal (int e) {
1156 			OnMouseLeave (e);
1157 		}
1158 
OnMouseMove(DataGridViewCellMouseEventArgs e)1159 		protected virtual void OnMouseMove (DataGridViewCellMouseEventArgs e) {
1160 		}
1161 
OnMouseMoveInternal(DataGridViewCellMouseEventArgs e)1162 		internal void OnMouseMoveInternal (DataGridViewCellMouseEventArgs e) {
1163 			OnMouseMove (e);
1164 		}
1165 
OnMouseUp(DataGridViewCellMouseEventArgs e)1166 		protected virtual void OnMouseUp (DataGridViewCellMouseEventArgs e) {
1167 		}
1168 
OnMouseUpInternal(DataGridViewCellMouseEventArgs e)1169 		internal void OnMouseUpInternal (DataGridViewCellMouseEventArgs e) {
1170 			OnMouseUp (e);
1171 		}
1172 
PaintInternal(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)1173 		internal void PaintInternal (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
1174 		{
1175 			Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
1176 		}
1177 
Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)1178 		protected virtual void Paint (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
1179 		{
1180 			if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
1181 				PaintPartBackground (graphics, cellBounds, cellStyle);
1182 			if ((paintParts & DataGridViewPaintParts.SelectionBackground) == DataGridViewPaintParts.SelectionBackground)
1183 				PaintPartSelectionBackground (graphics, cellBounds, cellState, cellStyle);
1184 			if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
1185 				PaintPartContent (graphics, cellBounds, rowIndex, cellState, cellStyle, formattedValue);
1186 			if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
1187 				PaintBorder (graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
1188 			if ((paintParts & DataGridViewPaintParts.Focus) == DataGridViewPaintParts.Focus)
1189 				PaintPartFocus (graphics, cellBounds);
1190 			if ((paintParts & DataGridViewPaintParts.ErrorIcon) == DataGridViewPaintParts.ErrorIcon)
1191 				if (!string.IsNullOrEmpty (ErrorText))
1192 					PaintErrorIcon (graphics, clipBounds, cellBounds, ErrorText);
1193 		}
1194 
PaintDividers(Graphics graphics, ref Rectangle bounds, DataGridViewAdvancedBorderStyle advancedBorderStyle)1195 		private void PaintDividers (Graphics graphics, ref Rectangle bounds, DataGridViewAdvancedBorderStyle advancedBorderStyle)
1196 		{
1197 			// Paint the vertical divider
1198 			int dividerWidth = OwningColumn != null ? OwningColumn.DividerWidth : 0;
1199 			if (dividerWidth > 0) {
1200 				if (dividerWidth > bounds.Width)
1201 					dividerWidth = bounds.Width;
1202 				Color color;
1203 				switch (advancedBorderStyle.Right) {
1204 					case DataGridViewAdvancedCellBorderStyle.Single:
1205 						color = DataGridView.GridColor;
1206 						break;
1207 					case DataGridViewAdvancedCellBorderStyle.Inset:
1208 						color = SystemColors.ControlLightLight;
1209 						break;
1210 					default:
1211 						color = SystemColors.ControlDark;
1212 						break;
1213 				}
1214 
1215 				graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (color), bounds.Right - dividerWidth, bounds.Y, dividerWidth, bounds.Height);
1216 				bounds.Width -= dividerWidth;
1217 				if (bounds.Width <= 0)
1218 					return;
1219 			}
1220 
1221 			dividerWidth = OwningRow != null ? OwningRow.DividerHeight : 0;
1222 			if (dividerWidth > 0) {
1223 				if (dividerWidth > bounds.Height)
1224 					dividerWidth = bounds.Height;
1225 				Color color;
1226 				switch (advancedBorderStyle.Bottom) {
1227 					case DataGridViewAdvancedCellBorderStyle.Single:
1228 						color = DataGridView.GridColor;
1229 						break;
1230 					case DataGridViewAdvancedCellBorderStyle.Inset:
1231 						color = SystemColors.ControlLightLight;
1232 						break;
1233 					default:
1234 						color = SystemColors.ControlDark;
1235 						break;
1236 				}
1237 
1238 				graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (color), bounds.X, bounds.Bottom - dividerWidth, bounds.Width, dividerWidth);
1239 				bounds.Height -= dividerWidth;
1240 			}
1241 		}
1242 
PaintBorder(Graphics graphics, Rectangle clipBounds, Rectangle bounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)1243 		protected virtual void PaintBorder (Graphics graphics, Rectangle clipBounds, Rectangle bounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)
1244 		{
1245 			PaintDividers(graphics, ref bounds, advancedBorderStyle);
1246 			if (bounds.Height <= 0 || bounds.Width <= 0)
1247 				return;
1248 
1249 			if (advancedBorderStyle.All == DataGridViewAdvancedCellBorderStyle.None)
1250 				return;
1251 
1252 			Pen penGrid = ThemeEngine.Current.ResPool.GetPen (DataGridView.GridColor);
1253 			CPColor cpColor = ThemeEngine.Current.ResPool.GetCPColor (cellStyle.BackColor);
1254 			Pen penDark = ThemeEngine.Current.ResPool.GetPen (cpColor.Dark);
1255 			Pen penLight = ThemeEngine.Current.ResPool.GetPen (cpColor.LightLight);
1256 
1257 			int left = bounds.X;
1258 			int right = bounds.Right - 1;
1259 			int top = bounds.Y;
1260 			int bottom = bounds.Bottom - 1;
1261 
1262 			// Paint the left border, if any
1263 			switch (advancedBorderStyle.Left) {
1264 				case DataGridViewAdvancedCellBorderStyle.Single:
1265 					graphics.DrawLine (penGrid, left, top, left, bottom);
1266 					break;
1267 
1268 				case DataGridViewAdvancedCellBorderStyle.Outset:
1269 					graphics.DrawLine (penLight, left, top, left, bottom);
1270 					break;
1271 
1272 				case DataGridViewAdvancedCellBorderStyle.Inset:
1273 					graphics.DrawLine (penDark, left, top, left, bottom);
1274 					break;
1275 
1276 				case DataGridViewAdvancedCellBorderStyle.InsetDouble:
1277 					graphics.DrawLine(penLight, left, top, left, bottom);
1278 					graphics.DrawLine(penDark, left + 1, (advancedBorderStyle.Top != DataGridViewAdvancedCellBorderStyle.None)? top + 1 : top, left + 1, bottom);
1279 					break;
1280 
1281 				case DataGridViewAdvancedCellBorderStyle.OutsetDouble:
1282 					graphics.DrawLine(penDark, left, top, left, bottom);
1283 					graphics.DrawLine(penLight, left + 1, (advancedBorderStyle.Top != DataGridViewAdvancedCellBorderStyle.None)? top + 1 : top, left + 1, bottom);
1284 					break;
1285 			}
1286 
1287 			// Paint the right border, if any
1288 			switch (advancedBorderStyle.Right) {
1289 				case DataGridViewAdvancedCellBorderStyle.Single:
1290 					graphics.DrawLine(penGrid, right, top, right, bottom);
1291 					break;
1292 
1293 				case DataGridViewAdvancedCellBorderStyle.Outset:
1294 					graphics.DrawLine (penDark, right, top, right, bottom);
1295 					break;
1296 
1297 				case DataGridViewAdvancedCellBorderStyle.Inset:
1298 					graphics.DrawLine (penLight, right, top, right, bottom);
1299 					break;
1300 
1301 				case DataGridViewAdvancedCellBorderStyle.InsetDouble:
1302 					graphics.DrawLine (penLight, right - 1, top, right - 1, bottom);
1303 					graphics.DrawLine (penDark, right, (advancedBorderStyle.Top != DataGridViewAdvancedCellBorderStyle.None)? top + 1 : top, right, bottom);
1304 					break;
1305 
1306 				case DataGridViewAdvancedCellBorderStyle.OutsetDouble:
1307 					graphics.DrawLine (penDark, right - 1, top, right - 1, bottom);
1308 					graphics.DrawLine (penLight, right, (advancedBorderStyle.Top != DataGridViewAdvancedCellBorderStyle.None)? top + 1 : top, right, bottom);
1309 					break;
1310 			}
1311 
1312 			// Paint the top border, if any
1313 			switch (advancedBorderStyle.Top) {
1314 				case DataGridViewAdvancedCellBorderStyle.Single:
1315 					graphics.DrawLine(penGrid, left, top, right, top);
1316 					break;
1317 
1318 				case DataGridViewAdvancedCellBorderStyle.Outset: {
1319 						int _left = (advancedBorderStyle.Left == DataGridViewAdvancedCellBorderStyle.InsetDouble) || (advancedBorderStyle.Left == DataGridViewAdvancedCellBorderStyle.OutsetDouble) ? left + 1 : left;
1320 						int _right = (advancedBorderStyle.Right == DataGridViewAdvancedCellBorderStyle.Inset) || (advancedBorderStyle.Right == DataGridViewAdvancedCellBorderStyle.Outset) ? right - 1 : right;
1321 						graphics.DrawLine (penLight, _left, top, _right, top);
1322 					} break;
1323 
1324 				case DataGridViewAdvancedCellBorderStyle.Inset: {
1325 						int _left = (advancedBorderStyle.Left == DataGridViewAdvancedCellBorderStyle.InsetDouble) || (advancedBorderStyle.Left == DataGridViewAdvancedCellBorderStyle.OutsetDouble) ? left + 1 : left;
1326 						int _right = (advancedBorderStyle.Right == DataGridViewAdvancedCellBorderStyle.Inset) || (advancedBorderStyle.Right == DataGridViewAdvancedCellBorderStyle.Outset) ? right - 1 : right;
1327 						graphics.DrawLine (penDark, _left, top, _right, top);
1328 					} break;
1329 
1330 				case DataGridViewAdvancedCellBorderStyle.InsetDouble:
1331 					graphics.DrawLine(penLight, left, top, right, top);
1332 					graphics.DrawLine(penDark, (advancedBorderStyle.Left != DataGridViewAdvancedCellBorderStyle.None)? left + 1 : left, top + 1,
1333 						(advancedBorderStyle.Right != DataGridViewAdvancedCellBorderStyle.None)? right -1 : right, top + 1);
1334 					break;
1335 
1336 				case DataGridViewAdvancedCellBorderStyle.OutsetDouble:
1337 					graphics.DrawLine(penDark, left, top, right, top);
1338 					graphics.DrawLine(penLight, (advancedBorderStyle.Left != DataGridViewAdvancedCellBorderStyle.None)? left + 1 : left, top + 1,
1339 						(advancedBorderStyle.Right != DataGridViewAdvancedCellBorderStyle.None)? right - 1 : right, top + 1);
1340 					break;
1341 			}
1342 
1343 			// Paint the bottom border, if any
1344 			switch (advancedBorderStyle.Bottom) {
1345 				case DataGridViewAdvancedCellBorderStyle.Single:
1346 					graphics.DrawLine(penGrid, left, bottom, right, bottom);
1347 					break;
1348 
1349 				case DataGridViewAdvancedCellBorderStyle.Outset: {
1350 						int _right = (advancedBorderStyle.Right == DataGridViewAdvancedCellBorderStyle.InsetDouble) || (advancedBorderStyle.Right == DataGridViewAdvancedCellBorderStyle.OutsetDouble) ? right - 1 : right;
1351 						graphics.DrawLine (penDark, left, bottom, _right, bottom);
1352 					} break;
1353 
1354 				case DataGridViewAdvancedCellBorderStyle.Inset:
1355 					graphics.DrawLine(penLight, left, bottom, (advancedBorderStyle.Right == DataGridViewAdvancedCellBorderStyle.InsetDouble)? right - 1: right, bottom);
1356 					break;
1357 			}
1358 		}
1359 
PaintErrorIcon(Graphics graphics, Rectangle clipBounds, Rectangle cellValueBounds, string errorText)1360 		protected virtual void PaintErrorIcon (Graphics graphics, Rectangle clipBounds, Rectangle cellValueBounds, string errorText)
1361 		{
1362 			Rectangle error_bounds = GetErrorIconBounds (graphics, null, RowIndex);
1363 
1364 			if (error_bounds.IsEmpty)
1365 				return;
1366 
1367 			Point loc = error_bounds.Location;
1368 			loc.X += cellValueBounds.Left;
1369 			loc.Y += cellValueBounds.Top;
1370 
1371 			graphics.FillRectangle (Brushes.Red, new Rectangle (loc.X + 1, loc.Y + 2, 10, 7));
1372 			graphics.FillRectangle (Brushes.Red, new Rectangle (loc.X + 2, loc.Y + 1, 8, 9));
1373 			graphics.FillRectangle (Brushes.Red, new Rectangle (loc.X + 4, loc.Y, 4, 11));
1374 			graphics.FillRectangle (Brushes.Red, new Rectangle (loc.X, loc.Y + 4, 12, 3));
1375 
1376 			graphics.FillRectangle (Brushes.White, new Rectangle (loc.X + 5, loc.Y + 2, 2, 4));
1377 			graphics.FillRectangle (Brushes.White, new Rectangle (loc.X + 5, loc.Y + 7, 2, 2));
1378 		}
1379 
PaintPartBackground(Graphics graphics, Rectangle cellBounds, DataGridViewCellStyle style)1380 		internal virtual void PaintPartBackground (Graphics graphics, Rectangle cellBounds, DataGridViewCellStyle style)
1381 		{
1382 			Color color = style.BackColor;
1383 			graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (color), cellBounds);
1384 		}
1385 
GetBorderPen()1386 		internal Pen GetBorderPen ()
1387 		{
1388 			return ThemeEngine.Current.ResPool.GetPen (DataGridView.GridColor);
1389 		}
1390 
PaintPartContent(Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, object formattedValue)1391 		internal virtual void PaintPartContent (Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, object formattedValue)
1392 		{
1393 			if (IsInEditMode)
1394 				return;
1395 
1396 			Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
1397 
1398 			TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl;
1399 			flags |= AlignmentToFlags (style.Alignment);
1400 
1401 			cellBounds.Height -= 2;
1402 			cellBounds.Width -= 2;
1403 
1404 			if (formattedValue != null)
1405 				TextRenderer.DrawText (graphics, formattedValue.ToString (), cellStyle.Font, cellBounds, color, flags);
1406 		}
1407 
PaintPartFocus(Graphics graphics, Rectangle cellBounds)1408 		private void PaintPartFocus (Graphics graphics, Rectangle cellBounds)
1409 		{
1410 			cellBounds.Width--;
1411 			cellBounds.Height--;
1412 
1413 			if (DataGridView.ShowFocusCues && DataGridView.CurrentCell == this && DataGridView.Focused)
1414 				ControlPaint.DrawFocusRectangle (graphics, cellBounds);
1415 		}
1416 
PaintPartSelectionBackground(Graphics graphics, Rectangle cellBounds, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle)1417 		internal virtual void PaintPartSelectionBackground (Graphics graphics, Rectangle cellBounds, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle)
1418 		{
1419 			if ((cellState & DataGridViewElementStates.Selected) != DataGridViewElementStates.Selected)
1420 				return;
1421 
1422 			if (RowIndex >= 0 && IsInEditMode && EditType != null)
1423 				return;
1424 
1425 			Color color = cellStyle.SelectionBackColor;
1426 			graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (color), cellBounds);
1427 		}
1428 
PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)1429 		internal void PaintWork (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
1430 		{
1431 			object value;
1432 			object formattedvalue;
1433 
1434 			if (RowIndex == -1 && !(this is DataGridViewColumnHeaderCell)) {
1435 				value = null;
1436 				formattedvalue = null;
1437 			} else if (RowIndex == -1) {
1438 				value = Value;
1439 				formattedvalue = Value;
1440 			} else {
1441 				value = Value;
1442 				formattedvalue = GetFormattedValue (Value, rowIndex, ref cellStyle, null, null, DataGridViewDataErrorContexts.Formatting);
1443 			}
1444 
1445 			DataGridViewCellPaintingEventArgs pea = new DataGridViewCellPaintingEventArgs (DataGridView, graphics, clipBounds, cellBounds, rowIndex, columnIndex, cellState, value, formattedvalue, ErrorText, cellStyle, advancedBorderStyle, paintParts);
1446 			DataGridView.OnCellPaintingInternal (pea);
1447 
1448 			if (pea.Handled)
1449 				return;
1450 
1451 			pea.Paint (pea.ClipBounds, pea.PaintParts);
1452 		}
1453 
SetValue(int rowIndex, object value)1454 		protected virtual bool SetValue (int rowIndex, object value)
1455 		{
1456 			object oldValue = this.Value;
1457 
1458 			if (DataProperty != null && !DataProperty.IsReadOnly) {
1459 				DataProperty.SetValue (OwningRow.DataBoundItem, value);
1460 			} else if (DataGridView != null && DataGridView.VirtualMode) {
1461 				DataGridViewCellValueEventArgs ea = new DataGridViewCellValueEventArgs(ColumnIndex, RowIndex);
1462 				ea.Value = value;
1463 				DataGridView.OnCellValuePushed(ea);
1464 			} else {
1465 				valuex = value;
1466 			}
1467 
1468 			if (!Object.ReferenceEquals (oldValue, value) || !Object.Equals (oldValue, value)) {
1469 				RaiseCellValueChanged (new DataGridViewCellEventArgs (ColumnIndex, RowIndex));
1470 
1471 				// Set this dirty flag back to false
1472 				if (this is IDataGridViewEditingCell)
1473 					(this as IDataGridViewEditingCell).EditingCellValueChanged = false;
1474 
1475 				if (DataGridView != null)
1476 					DataGridView.InvalidateCell (this);
1477 
1478 				return true;
1479 			}
1480 			return false;
1481 		}
1482 
OnStyleChanged(object sender, EventArgs args)1483 		private void OnStyleChanged (object sender, EventArgs args) {
1484 			if (DataGridView != null) {
1485 				DataGridView.RaiseCellStyleChanged(new DataGridViewCellEventArgs(ColumnIndex, RowIndex));
1486 			}
1487 		}
1488 
1489 		internal virtual Rectangle InternalErrorIconsBounds {
1490 			get { return GetErrorIconBounds (null, null, -1); }
1491 		}
1492 
InternalPaint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)1493 		internal void InternalPaint (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) {
1494 			Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
1495 		}
1496 
SetOwningRow(DataGridViewRow row)1497 		internal void SetOwningRow (DataGridViewRow row) {
1498 			owningRow = row;
1499 		}
1500 
SetOwningColumn(DataGridViewColumn col)1501 		internal void SetOwningColumn (DataGridViewColumn col) {
1502 			columnIndex = col.Index;
1503 		}
1504 
SetColumnIndex(int index)1505 		internal void SetColumnIndex (int index) {
1506 			columnIndex = index;
1507 		}
1508 
SetIsInEditMode(bool isInEditMode)1509 		internal void SetIsInEditMode (bool isInEditMode) {
1510 			this.isInEditMode = isInEditMode;
1511 		}
1512 
OnErrorTextChanged(DataGridViewCellEventArgs args)1513 		internal void OnErrorTextChanged (DataGridViewCellEventArgs args) {
1514 			if (DataGridView != null) {
1515 				DataGridView.OnCellErrorTextChanged(args);
1516 			}
1517 		}
1518 
AlignmentToFlags(DataGridViewContentAlignment align)1519 		internal TextFormatFlags AlignmentToFlags (DataGridViewContentAlignment align)
1520 		{
1521 			TextFormatFlags flags = TextFormatFlags.Default;
1522 
1523 			switch (align) {
1524 				case DataGridViewContentAlignment.BottomCenter:
1525 					flags |= TextFormatFlags.Bottom;
1526 					flags |= TextFormatFlags.HorizontalCenter;
1527 					break;
1528 				case DataGridViewContentAlignment.BottomLeft:
1529 					flags |= TextFormatFlags.Bottom;
1530 					break;
1531 				case DataGridViewContentAlignment.BottomRight:
1532 					flags |= TextFormatFlags.Bottom;
1533 					flags |= TextFormatFlags.Right;
1534 					break;
1535 				case DataGridViewContentAlignment.MiddleCenter:
1536 					flags |= TextFormatFlags.VerticalCenter;
1537 					flags |= TextFormatFlags.HorizontalCenter;
1538 					break;
1539 				case DataGridViewContentAlignment.MiddleLeft:
1540 					flags |= TextFormatFlags.VerticalCenter;
1541 					break;
1542 				case DataGridViewContentAlignment.MiddleRight:
1543 					flags |= TextFormatFlags.VerticalCenter;
1544 					flags |= TextFormatFlags.Right;
1545 					break;
1546 				case DataGridViewContentAlignment.TopLeft:
1547 					flags |= TextFormatFlags.Top;
1548 					break;
1549 				case DataGridViewContentAlignment.TopCenter:
1550 					flags |= TextFormatFlags.HorizontalCenter;
1551 					flags |= TextFormatFlags.Top;
1552 					break;
1553 				case DataGridViewContentAlignment.TopRight:
1554 					flags |= TextFormatFlags.Right;
1555 					flags |= TextFormatFlags.Top;
1556 					break;
1557 			}
1558 
1559 			return flags;
1560 		}
1561 
AlignInRectangle(Rectangle outer, Size inner, DataGridViewContentAlignment align)1562 		internal Rectangle AlignInRectangle (Rectangle outer, Size inner, DataGridViewContentAlignment align)
1563 		{
1564 			int x = 0;
1565 			int y = 0;
1566 
1567 			if (align == DataGridViewContentAlignment.BottomLeft || align == DataGridViewContentAlignment.MiddleLeft || align == DataGridViewContentAlignment.TopLeft)
1568 				x = outer.X;
1569 			else if (align == DataGridViewContentAlignment.BottomCenter || align == DataGridViewContentAlignment.MiddleCenter || align == DataGridViewContentAlignment.TopCenter)
1570 				x = Math.Max (outer.X + ((outer.Width - inner.Width) / 2), outer.Left);
1571 			else if (align == DataGridViewContentAlignment.BottomRight || align == DataGridViewContentAlignment.MiddleRight || align == DataGridViewContentAlignment.TopRight)
1572 				x = Math.Max (outer.Right - inner.Width, outer.X);
1573 			if (align == DataGridViewContentAlignment.TopCenter || align == DataGridViewContentAlignment.TopLeft || align == DataGridViewContentAlignment.TopRight)
1574 				y = outer.Y;
1575 			else if (align == DataGridViewContentAlignment.MiddleCenter || align == DataGridViewContentAlignment.MiddleLeft || align == DataGridViewContentAlignment.MiddleRight)
1576 				y = Math.Max (outer.Y + (outer.Height - inner.Height) / 2, outer.Y);
1577 			else if (align == DataGridViewContentAlignment.BottomCenter || align == DataGridViewContentAlignment.BottomRight || align == DataGridViewContentAlignment.BottomLeft)
1578 				y = Math.Max (outer.Bottom - inner.Height, outer.Y);
1579 
1580 			return new Rectangle (x, y, Math.Min (inner.Width, outer.Width), Math.Min (inner.Height, outer.Height));
1581 		}
1582 
1583 		[ComVisibleAttribute(true)]
1584 		protected class DataGridViewCellAccessibleObject : AccessibleObject {
1585 
1586 			private DataGridViewCell dataGridViewCell;
1587 
DataGridViewCellAccessibleObject()1588 			public DataGridViewCellAccessibleObject () {
1589 			}
1590 
DataGridViewCellAccessibleObject(DataGridViewCell owner)1591 			public DataGridViewCellAccessibleObject (DataGridViewCell owner) {
1592 				this.dataGridViewCell = owner;
1593 			}
1594 
1595 			public override Rectangle Bounds {
1596 				get { throw new NotImplementedException(); }
1597 			}
1598 
1599 			public override string DefaultAction {
1600 				get { return "Edit"; }
1601 			}
1602 
1603 			public override string Help {
1604 				get { return base.Help; }
1605 			}
1606 
1607 			public override string Name {
1608 				get { return dataGridViewCell.OwningColumn.HeaderText + ": " + dataGridViewCell.RowIndex.ToString(); }
1609 			}
1610 
1611 			public DataGridViewCell Owner {
1612 				get { return dataGridViewCell; }
1613 				set { dataGridViewCell = value; }
1614 			}
1615 
1616 			public override AccessibleObject Parent {
1617 				get { return dataGridViewCell.OwningRow.AccessibilityObject; }
1618 			}
1619 
1620 			public override AccessibleRole Role {
1621 				get { return AccessibleRole.Cell; }
1622 			}
1623 
1624 			public override AccessibleStates State {
1625 				get {
1626 					if (dataGridViewCell.Selected) {
1627 						return AccessibleStates.Selected;
1628 					}
1629 					else {
1630 						return AccessibleStates.Focused;
1631 					}
1632 				}
1633 			}
1634 
1635 			public override string Value {
1636 				get {
1637 					if (dataGridViewCell.FormattedValue == null) {
1638 						return "(null)";
1639 					}
1640 					return dataGridViewCell.FormattedValue.ToString();
1641 				}
1642 				set {
1643 					if (owner == null)
1644 						throw new InvalidOperationException ("owner is null");
1645 
1646 					throw new NotImplementedException ();
1647 				}
1648 			}
1649 
DoDefaultAction()1650 			public override void DoDefaultAction () {
1651 				if (dataGridViewCell.DataGridView.EditMode != DataGridViewEditMode.EditProgrammatically) {
1652 					if (dataGridViewCell.IsInEditMode) {
1653 						// commit edit
1654 					}
1655 					else {
1656 						// begin edit
1657 					}
1658 				}
1659 			}
1660 
GetChild(int index)1661 			public override AccessibleObject GetChild (int index) {
1662 				throw new NotImplementedException();
1663 			}
1664 
GetChildCount()1665 			public override int GetChildCount () {
1666 				if (dataGridViewCell.IsInEditMode) {
1667 					return 1;
1668 				}
1669 				return -1;
1670 			}
1671 
GetFocused()1672 			public override AccessibleObject GetFocused () {
1673 				return null;
1674 			}
1675 
GetSelected()1676 			public override AccessibleObject GetSelected () {
1677 				return null;
1678 			}
1679 
Navigate(AccessibleNavigation navigationDirection)1680 			public override AccessibleObject Navigate (AccessibleNavigation navigationDirection) {
1681 				switch (navigationDirection) {
1682 					case AccessibleNavigation.Right:
1683 						break;
1684 					case AccessibleNavigation.Left:
1685 						break;
1686 					case AccessibleNavigation.Next:
1687 						break;
1688 					case AccessibleNavigation.Previous:
1689 						break;
1690 					case AccessibleNavigation.Up:
1691 						break;
1692 					case AccessibleNavigation.Down:
1693 						break;
1694 					default:
1695 						return null;
1696 				}
1697 				return null;
1698 			}
1699 
Select(AccessibleSelection flags)1700 			public override void Select (AccessibleSelection flags) {
1701 				switch (flags) {
1702 					case AccessibleSelection.TakeFocus:
1703 						dataGridViewCell.dataGridViewOwner.Focus();
1704 						break;
1705 					case AccessibleSelection.TakeSelection:
1706 						//dataGridViewCell.Focus();
1707 						break;
1708 					case AccessibleSelection.AddSelection:
1709 						dataGridViewCell.dataGridViewOwner.SelectedCells.InternalAdd(dataGridViewCell);
1710 						break;
1711 					case AccessibleSelection.RemoveSelection:
1712 						dataGridViewCell.dataGridViewOwner.SelectedCells.InternalRemove(dataGridViewCell);
1713 						break;
1714 				}
1715 			}
1716 
1717 		}
1718 	}
1719 
1720 	internal class DataGridViewCellConverter : TypeConverter
1721 	{
1722 	}
1723 
1724 }
1725