2007-03-29 Jonathan Pobst <monkey@jpobst.com>
[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                         if (DataGridView.Controls.Contains(editingControl)) {
84                                 DataGridView.Controls.Remove(editingControl);
85                         }
86                         Console.WriteLine("Detached: ({0}, {1});", RowIndex, ColumnIndex);
87                 }
88
89                 public override void InitializeEditingControl (int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
90                 {
91                         if (DataGridView == null) {
92                                 throw new InvalidOperationException("There is no associated DataGridView.");
93                         }
94                         if (!DataGridView.Controls.Contains(editingControl)) {
95                                 DataGridView.Controls.Add(editingControl);
96                         }
97                         editingControl.EditingControlDataGridView = DataGridView;
98                         editingControl.MaxLength = maxInputLength;
99                         if (initialFormattedValue == null || (string) initialFormattedValue == "") {
100                                 editingControl.Text = "";
101                         }
102                         else {
103                                 editingControl.Text = (string) initialFormattedValue;
104                         }
105                         editingControl.ApplyCellStyleToEditingControl(dataGridViewCellStyle);
106                         editingControl.PrepareEditingControlForEdit(true);
107                 }
108
109                 public override bool KeyEntersEditMode (KeyEventArgs e)
110                 {
111                         throw new NotImplementedException();
112                 }
113
114                 public override void PositionEditingControl (bool setLocation, bool setSize, Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
115                 {
116                         if (setSize) {
117                                 editingControl.Size = new Size(cellBounds.Width, cellBounds.Height + 2);
118                         }
119                         if (setLocation) {
120                                 editingControl.Location = new Point(cellBounds.X, cellBounds.Y);
121                         }
122                         editingControl.Invalidate();
123                 }
124
125                 public override string ToString ()
126                 {
127                         return this.GetType().Name;
128                 }
129
130                 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
131                 {
132                         throw new NotImplementedException();
133                 }
134
135                 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
136                 {
137                         throw new NotImplementedException();
138                 }
139
140                 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
141                 {
142                         throw new NotImplementedException();
143                 }
144
145                 protected override void OnEnter (int rowIndex, bool throughMouseClick)
146                 {
147                 }
148
149                 protected override void OnLeave (int rowIndex, bool throughMouseClick)
150                 {
151                 }
152
153                 protected override void OnMouseClick (DataGridViewCellMouseEventArgs e)
154                 {
155                 }
156
157                 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)
158                 {
159                         //////////////////
160                         /*
161                         Size size = DataGridViewCell.MeasureTextSize(graphics, (string) formattedValue, cellStyle.Font, TextFormatFlags.Default);
162                         switch (cellStyle.Alignment) {
163                                 case DataGridViewContentAlignment.TopLeft:
164                                         break;
165                         }
166                         //cell.SetContentBounds(cellBounds);
167                         */
168                         //////////////////
169                         if ((cellState & DataGridViewElementStates.Selected) != 0) {
170                                 graphics.FillRectangle(new SolidBrush(cellStyle.SelectionBackColor), cellBounds);
171                                 graphics.DrawString((string) formattedValue, cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), cellBounds, StringFormat.GenericDefault);
172                         }
173                         else {
174                                 graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);
175                                 graphics.DrawString((string) formattedValue, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), cellBounds, StringFormat.GenericDefault);
176                         }
177                         PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
178                 }
179
180         }
181
182 }
183
184 #endif