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>
30 using System.ComponentModel;
33 namespace System.Windows.Forms {
35 public class DataGridViewTextBoxCell : DataGridViewCell {
37 private int maxInputLength = 32767;
38 private static DataGridViewTextBoxEditingControl editingControl;
40 static DataGridViewTextBoxCell ()
42 editingControl = new DataGridViewTextBoxEditingControl();
43 editingControl.Multiline = false;
44 editingControl.BorderStyle = BorderStyle.None;
47 public DataGridViewTextBoxCell ()
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 typeof(string); }
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 DataGridView.EditingControlInternal = editingControl;
95 editingControl.EditingControlDataGridView = DataGridView;
96 editingControl.MaxLength = maxInputLength;
97 if (initialFormattedValue == null || (string) initialFormattedValue == "") {
98 editingControl.Text = "";
101 editingControl.Text = (string) initialFormattedValue;
103 editingControl.ApplyCellStyleToEditingControl(dataGridViewCellStyle);
104 editingControl.PrepareEditingControlForEdit(true);
107 public override bool KeyEntersEditMode (KeyEventArgs e)
109 throw new NotImplementedException();
112 public override void PositionEditingControl (bool setLocation, bool setSize, Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
115 editingControl.Size = new Size(cellBounds.Width, cellBounds.Height + 2);
118 editingControl.Location = new Point(cellBounds.X, cellBounds.Y);
120 editingControl.Invalidate();
123 public override string ToString ()
125 return this.GetType().Name;
128 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
130 throw new NotImplementedException();
133 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
135 throw new NotImplementedException();
138 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
140 throw new NotImplementedException();
143 protected override void OnEnter (int rowIndex, bool throughMouseClick)
147 protected override void OnLeave (int rowIndex, bool throughMouseClick)
151 protected override void OnMouseClick (DataGridViewCellMouseEventArgs e)
155 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)
159 Size size = DataGridViewCell.MeasureTextSize(graphics, (string) formattedValue, cellStyle.Font, TextFormatFlags.Default);
160 switch (cellStyle.Alignment) {
161 case DataGridViewContentAlignment.TopLeft:
164 //cell.SetContentBounds(cellBounds);
168 Brush forecolor_brush;
169 Brush backcolor_brush;
170 Rectangle text_rect = cellBounds;
171 Rectangle borders = BorderWidths (advancedBorderStyle);
173 text_rect.X += borders.X;
174 text_rect.Y += borders.Y;
175 text_rect.Height -= (borders.Y + borders.Height);
176 text_rect.Width -= (borders.X + borders.Width);
178 format = cellStyle.SetAlignment ((StringFormat) StringFormat.GenericTypographic.Clone ());
179 if ((cellState & DataGridViewElementStates.Selected) != 0 && !IsInEditMode) {
180 backcolor_brush = ThemeEngine.Current.ResPool.GetSolidBrush (cellStyle.SelectionBackColor);
181 forecolor_brush = ThemeEngine.Current.ResPool.GetSolidBrush (cellStyle.SelectionForeColor);
183 backcolor_brush = ThemeEngine.Current.ResPool.GetSolidBrush (cellStyle.BackColor);
184 forecolor_brush = ThemeEngine.Current.ResPool.GetSolidBrush (cellStyle.ForeColor);
187 graphics.FillRectangle (backcolor_brush, cellBounds);
188 graphics.DrawString ((string) formattedValue, cellStyle.Font, forecolor_brush, text_rect, format);
189 PaintBorder (graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);