[MSBuild] Fix minor assembly resolution issue
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / DataGridViewTextBoxCell.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 using System;
28 using System.ComponentModel;
29 using System.Drawing;
30
31 namespace System.Windows.Forms {
32
33         public class DataGridViewTextBoxCell : DataGridViewCell {
34
35                 private int maxInputLength = 32767;
36                 private DataGridViewTextBoxEditingControl editingControl;
37
38                 void CreateEditingControl ()
39                 {
40                         editingControl = new DataGridViewTextBoxEditingControl() {
41                                 Multiline = false,
42                                 BorderStyle = BorderStyle.None
43                         };
44                 }
45
46                 public DataGridViewTextBoxCell ()
47                 {
48                         base.ValueType = typeof (object);
49                 }
50
51                 public override Type FormattedValueType {
52                         get { return typeof(string); }
53                 }
54
55                 [DefaultValue (32767)]
56                 public virtual int MaxInputLength {
57                         get { return maxInputLength; }
58                         set {
59                                 if (value < 0) {
60                                         throw new ArgumentOutOfRangeException("MaxInputLength coudn't be less than 0.");
61                                 }
62                                 maxInputLength = value;
63                         }
64                 }
65
66                 public override Type ValueType {
67                         get { return base.ValueType; }
68                 }
69
70                 public override object Clone ()
71                 {
72                         DataGridViewTextBoxCell result = (DataGridViewTextBoxCell) base.Clone();
73                         result.maxInputLength = maxInputLength;
74                         return result;
75                 }
76
77                 [EditorBrowsable (EditorBrowsableState.Advanced)]
78                 public override void DetachEditingControl ()
79                 {
80                         if (DataGridView == null) {
81                                 throw new InvalidOperationException("There is no associated DataGridView.");
82                         }
83                         
84                         DataGridView.EditingControlInternal = null;
85                 }
86
87                 public override void InitializeEditingControl (int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
88                 {
89                         if (DataGridView == null) {
90                                 throw new InvalidOperationException("There is no associated DataGridView.");
91                         }
92
93                         if (editingControl == null)
94                                 CreateEditingControl ();
95
96                         DataGridView.EditingControlInternal = editingControl;
97
98                         editingControl.EditingControlDataGridView = DataGridView;
99                         editingControl.MaxLength = maxInputLength;
100                         
101                         if (initialFormattedValue == null || initialFormattedValue.ToString () == string.Empty)
102                                 editingControl.Text = string.Empty;
103                         else
104                                 editingControl.Text = initialFormattedValue.ToString ();
105
106                         editingControl.ApplyCellStyleToEditingControl(dataGridViewCellStyle);
107                         editingControl.PrepareEditingControlForEdit(true);
108                 }
109
110                 public override bool KeyEntersEditMode (KeyEventArgs e)
111                 {
112                         if (e.KeyCode == Keys.Space)
113                                 return true;
114                         if ((int)e.KeyCode >= 48 && (int)e.KeyCode <= 90)
115                                 return true;
116                         if ((int)e.KeyCode >= 96 && (int)e.KeyCode <= 111)
117                                 return true;
118                         if (e.KeyCode == Keys.BrowserSearch || e.KeyCode == Keys.SelectMedia)
119                                 return true;
120                         if ((int)e.KeyCode >= 186 && (int)e.KeyCode <= 229)
121                                 return true;
122                         if (e.KeyCode == Keys.Attn || e.KeyCode == Keys.Packet)
123                                 return true;
124                         if ((int)e.KeyCode >= 248 && (int)e.KeyCode <= 254)
125                                 return true;
126                                 
127                         return false;
128                 }
129
130                 public override void PositionEditingControl (bool setLocation, bool setSize, Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
131                 {
132                         if (editingControl == null)
133                                 CreateEditingControl ();
134
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);
137
138                         base.PositionEditingControl (setLocation, setSize, cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow);
139                         
140                         editingControl.Invalidate();
141                 }
142
143                 public override string ToString ()
144                 {
145                         return string.Format ("DataGridViewTextBoxCell {{ ColumnIndex={0}, RowIndex={1} }}", ColumnIndex, RowIndex);
146                 }
147
148                 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
149                 {
150                         if (DataGridView == null)
151                                 return Rectangle.Empty;
152                                 
153                         object o = FormattedValue;
154                         Size s = Size.Empty;
155                         
156                         if (o != null) {
157                                 s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
158                                 s.Height += 2;
159                         }
160                         
161                         return new Rectangle (0, (OwningRow.Height - s.Height) / 2, s.Width, s.Height);
162                 }
163
164                 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
165                 {
166                         if (DataGridView == null || string.IsNullOrEmpty (ErrorText))
167                                 return Rectangle.Empty;
168                                 
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);
171                 }
172
173                 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
174                 {
175                         object o = FormattedValue;
176                         
177                         if (o != null) {
178                                 Size s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
179                                 s.Height = Math.Max (s.Height, 20);
180                                 s.Width += 2;
181                                 return s;
182                         } else
183                                 return new Size (21, 20);
184                 }
185
186                 protected override void OnEnter (int rowIndex, bool throughMouseClick)
187                 {
188                 }
189
190                 protected override void OnLeave (int rowIndex, bool throughMouseClick)
191                 {
192                 }
193
194                 protected override void OnMouseClick (DataGridViewCellMouseEventArgs e)
195                 {
196                 }
197
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)
199                 {
200                         // Prepaint
201                         DataGridViewPaintParts pre = DataGridViewPaintParts.Background | DataGridViewPaintParts.SelectionBackground;
202                         pre = pre & paintParts;
203                         
204                         base.Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, pre);
205
206                         // Paint content
207                         if (!IsInEditMode && (paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground) {
208                                 Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
209
210                                 TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.TextBoxControl;
211                                 flags |= AlignmentToFlags (cellStyle.Alignment);
212
213                                 Rectangle contentbounds = cellBounds;
214                                 
215                                 contentbounds.Height -= 2;
216                                 contentbounds.Width -= 2;
217                                 
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;
222                                 }
223
224                                 if (formattedValue != null)
225                                         TextRenderer.DrawText (graphics, formattedValue.ToString (), cellStyle.Font, contentbounds, color, flags);
226                         }
227
228                         // Postpaint
229                         DataGridViewPaintParts post = DataGridViewPaintParts.Border | DataGridViewPaintParts.Focus | DataGridViewPaintParts.ErrorIcon;
230                         post = post & paintParts;
231
232                         base.Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, post);
233                 }
234
235         }
236
237 }