Fix for the issue of getting occasional -5875 error on the server when
[mono.git] / mcs / class / Managed.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 #if NET_2_0
28
29 using System;
30 using System.ComponentModel;
31 using System.Drawing;
32
33 namespace System.Windows.Forms {
34
35         public class DataGridViewTextBoxCell : DataGridViewCell {
36
37                 private int maxInputLength = 32767;
38                 private static DataGridViewTextBoxEditingControl editingControl;
39
40                 static DataGridViewTextBoxCell ()
41                 {
42                         editingControl = new DataGridViewTextBoxEditingControl();
43                         editingControl.Multiline = false;
44                         editingControl.BorderStyle = BorderStyle.None;
45                 }
46
47                 public DataGridViewTextBoxCell ()
48                 {
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 typeof(string); }
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                         DataGridView.EditingControlInternal = editingControl;
94                         
95                         editingControl.EditingControlDataGridView = DataGridView;
96                         editingControl.MaxLength = maxInputLength;
97                         if (initialFormattedValue == null || (string) initialFormattedValue == "") {
98                                 editingControl.Text = "";
99                         }
100                         else {
101                                 editingControl.Text = (string) initialFormattedValue;
102                         }
103                         editingControl.ApplyCellStyleToEditingControl(dataGridViewCellStyle);
104                         editingControl.PrepareEditingControlForEdit(true);
105                 }
106
107                 public override bool KeyEntersEditMode (KeyEventArgs e)
108                 {
109                         throw new NotImplementedException();
110                 }
111
112                 public override void PositionEditingControl (bool setLocation, bool setSize, Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
113                 {
114                         if (setSize) {
115                                 editingControl.Size = new Size(cellBounds.Width, cellBounds.Height + 2);
116                         }
117                         if (setLocation) {
118                                 editingControl.Location = new Point(cellBounds.X, cellBounds.Y);
119                         }
120                         editingControl.Invalidate();
121                 }
122
123                 public override string ToString ()
124                 {
125                         return this.GetType().Name;
126                 }
127
128                 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
129                 {
130                         throw new NotImplementedException();
131                 }
132
133                 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
134                 {
135                         throw new NotImplementedException();
136                 }
137
138                 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
139                 {
140                         throw new NotImplementedException();
141                 }
142
143                 protected override void OnEnter (int rowIndex, bool throughMouseClick)
144                 {
145                 }
146
147                 protected override void OnLeave (int rowIndex, bool throughMouseClick)
148                 {
149                 }
150
151                 protected override void OnMouseClick (DataGridViewCellMouseEventArgs e)
152                 {
153                 }
154
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)
156                 {
157                         //////////////////
158                         /*
159                         Size size = DataGridViewCell.MeasureTextSize(graphics, (string) formattedValue, cellStyle.Font, TextFormatFlags.Default);
160                         switch (cellStyle.Alignment) {
161                                 case DataGridViewContentAlignment.TopLeft:
162                                         break;
163                         }
164                         //cell.SetContentBounds(cellBounds);
165                         */
166                         //////////////////
167                         StringFormat format;
168                         Brush forecolor_brush;
169                         Brush backcolor_brush;
170                         Rectangle text_rect = cellBounds;
171                         Rectangle borders = BorderWidths (advancedBorderStyle);
172                         
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);
177                         
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);
182                         } else {
183                                 backcolor_brush =  ThemeEngine.Current.ResPool.GetSolidBrush (cellStyle.BackColor);
184                                 forecolor_brush = ThemeEngine.Current.ResPool.GetSolidBrush (cellStyle.ForeColor);
185                         }
186
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);
190                 }
191
192         }
193
194 }
195
196 #endif