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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Pedro MartÃnez Juliá <pedromj@gmail.com>
28 using System.ComponentModel;
31 namespace System.Windows.Forms {
33 public class DataGridViewTextBoxCell : DataGridViewCell {
35 private int maxInputLength = 32767;
36 private DataGridViewTextBoxEditingControl editingControl;
38 void CreateEditingControl ()
40 editingControl = new DataGridViewTextBoxEditingControl() {
42 BorderStyle = BorderStyle.None
46 public DataGridViewTextBoxCell ()
48 base.ValueType = typeof (object);
51 public override Type FormattedValueType {
52 get { return typeof(string); }
55 [DefaultValue (32767)]
56 public virtual int MaxInputLength {
57 get { return maxInputLength; }
60 throw new ArgumentOutOfRangeException("MaxInputLength coudn't be less than 0.");
62 maxInputLength = value;
66 public override Type ValueType {
67 get { return base.ValueType; }
70 public override object Clone ()
72 DataGridViewTextBoxCell result = (DataGridViewTextBoxCell) base.Clone();
73 result.maxInputLength = maxInputLength;
77 [EditorBrowsable (EditorBrowsableState.Advanced)]
78 public override void DetachEditingControl ()
80 if (DataGridView == null) {
81 throw new InvalidOperationException("There is no associated DataGridView.");
84 DataGridView.EditingControlInternal = null;
87 public override void InitializeEditingControl (int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
89 if (DataGridView == null) {
90 throw new InvalidOperationException("There is no associated DataGridView.");
93 if (editingControl == null)
94 CreateEditingControl ();
96 DataGridView.EditingControlInternal = editingControl;
98 editingControl.EditingControlDataGridView = DataGridView;
99 editingControl.MaxLength = maxInputLength;
101 if (initialFormattedValue == null || initialFormattedValue.ToString () == string.Empty)
102 editingControl.Text = string.Empty;
104 editingControl.Text = initialFormattedValue.ToString ();
106 editingControl.ApplyCellStyleToEditingControl(dataGridViewCellStyle);
107 editingControl.PrepareEditingControlForEdit(true);
110 public override bool KeyEntersEditMode (KeyEventArgs e)
112 if (e.KeyCode == Keys.Space)
114 if ((int)e.KeyCode >= 48 && (int)e.KeyCode <= 90)
116 if ((int)e.KeyCode >= 96 && (int)e.KeyCode <= 111)
118 if (e.KeyCode == Keys.BrowserSearch || e.KeyCode == Keys.SelectMedia)
120 if ((int)e.KeyCode >= 186 && (int)e.KeyCode <= 229)
122 if (e.KeyCode == Keys.Attn || e.KeyCode == Keys.Packet)
124 if ((int)e.KeyCode >= 248 && (int)e.KeyCode <= 254)
130 public override void PositionEditingControl (bool setLocation, bool setSize, Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
132 if (editingControl == null)
133 CreateEditingControl ();
135 cellBounds.Size = new Size (cellBounds.Width - 5, cellBounds.Height + 2);
136 cellBounds.Location = new Point (cellBounds.X + 3, ((cellBounds.Height - editingControl.Height) / 2) + cellBounds.Y - 1);
138 base.PositionEditingControl (setLocation, setSize, cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow);
140 editingControl.Invalidate();
143 public override string ToString ()
145 return string.Format ("DataGridViewTextBoxCell {{ ColumnIndex={0}, RowIndex={1} }}", ColumnIndex, RowIndex);
148 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
150 if (DataGridView == null)
151 return Rectangle.Empty;
153 object o = FormattedValue;
157 s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
161 return new Rectangle (0, (OwningRow.Height - s.Height) / 2, s.Width, s.Height);
164 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
166 if (DataGridView == null || string.IsNullOrEmpty (ErrorText))
167 return Rectangle.Empty;
169 Size error_icon = new Size (12, 11);
170 return new Rectangle (new Point (Size.Width - error_icon.Width - 5, (Size.Height - error_icon.Height) / 2), error_icon);
173 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
175 object o = FormattedValue;
178 Size s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
179 s.Height = Math.Max (s.Height, 20);
183 return new Size (21, 20);
186 protected override void OnEnter (int rowIndex, bool throughMouseClick)
190 protected override void OnLeave (int rowIndex, bool throughMouseClick)
194 protected override void OnMouseClick (DataGridViewCellMouseEventArgs e)
198 protected override void Paint (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
201 DataGridViewPaintParts pre = DataGridViewPaintParts.Background | DataGridViewPaintParts.SelectionBackground;
202 pre = pre & paintParts;
204 base.Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, pre);
207 if (!IsInEditMode && (paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground) {
208 Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
210 TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.TextBoxControl;
211 flags |= AlignmentToFlags (cellStyle.Alignment);
213 Rectangle contentbounds = cellBounds;
215 contentbounds.Height -= 2;
216 contentbounds.Width -= 2;
218 // If we are top aligned, give ourselves some padding from the top
219 if (((int)cellStyle.Alignment & 7) > 0) {
220 contentbounds.Offset (0, 2);
221 contentbounds.Height -= 2;
224 if (formattedValue != null)
225 TextRenderer.DrawText (graphics, formattedValue.ToString (), cellStyle.Font, contentbounds, color, flags);
229 DataGridViewPaintParts post = DataGridViewPaintParts.Border | DataGridViewPaintParts.Focus | DataGridViewPaintParts.ErrorIcon;
230 post = post & paintParts;
232 base.Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, post);