* roottypes.cs: Rename from tree.cs.
[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.Drawing;
31
32 namespace System.Windows.Forms {
33
34         public class DataGridViewTextBoxCell : DataGridViewCell {
35
36                 private int maxInputLength = 32767;
37                 private static DataGridViewTextBoxEditingControl editingControl;
38
39                 static DataGridViewTextBoxCell () {
40                         editingControl = new DataGridViewTextBoxEditingControl();
41                         editingControl.Multiline = false;
42                         editingControl.BorderStyle = BorderStyle.None;
43                 }
44
45                 public DataGridViewTextBoxCell () {
46                 }
47
48                 public override Type FormattedValueType {
49                         get { return typeof(string); }
50                 }
51
52                 public virtual int MaxInputLength {
53                         get { return maxInputLength; }
54                         set {
55                                 if (value < 0) {
56                                         throw new ArgumentOutOfRangeException("MaxInputLength coudn't be less than 0.");
57                                 }
58                                 maxInputLength = value;
59                         }
60                 }
61
62                 public override Type ValueType {
63                         get { return typeof(string); }
64                 }
65
66                 public override object Clone () {
67                         DataGridViewTextBoxCell result = (DataGridViewTextBoxCell) base.Clone();
68                         result.maxInputLength = maxInputLength;
69                         return result;
70                 }
71
72                 public override void DetachEditingControl () {
73                         if (DataGridView == null) {
74                                 throw new InvalidOperationException("There is no associated DataGridView.");
75                         }
76                         if (DataGridView.Controls.Contains(editingControl)) {
77                                 DataGridView.Controls.Remove(editingControl);
78                         }
79                         Console.WriteLine("Detached: ({0}, {1});", RowIndex, ColumnIndex);
80                 }
81
82                 public override void InitializeEditingControl (int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) {
83                         if (DataGridView == null) {
84                                 throw new InvalidOperationException("There is no associated DataGridView.");
85                         }
86                         if (!DataGridView.Controls.Contains(editingControl)) {
87                                 DataGridView.Controls.Add(editingControl);
88                         }
89                         editingControl.EditingControlDataGridView = DataGridView;
90                         editingControl.MaxLength = maxInputLength;
91                         if (initialFormattedValue == null || (string) initialFormattedValue == "") {
92                                 editingControl.Text = "";
93                         }
94                         else {
95                                 editingControl.Text = (string) initialFormattedValue;
96                         }
97                         editingControl.ApplyCellStyleToEditingControl(dataGridViewCellStyle);
98                         editingControl.PrepareEditingControlForEdit(true);
99                 }
100
101                 public override bool KeyEntersEditMode (KeyEventArgs e) {
102                         throw new NotImplementedException();
103                 }
104
105                 public override void PositionEditingControl (bool setLocation, bool setSize, Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow) {
106                         if (setSize) {
107                                 editingControl.Size = new Size(cellBounds.Width, cellBounds.Height + 2);
108                         }
109                         if (setLocation) {
110                                 editingControl.Location = new Point(cellBounds.X, cellBounds.Y);
111                         }
112                         editingControl.Invalidate();
113                 }
114
115                 public override string ToString () {
116                         return this.GetType().Name;
117                 }
118
119                 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex) {
120                         throw new NotImplementedException();
121                 }
122
123                 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex) {
124                         throw new NotImplementedException();
125                 }
126
127                 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize) {
128                         throw new NotImplementedException();
129                 }
130
131                 protected override void OnEnter (int rowIndex, bool throughMouseClick) {
132                 }
133
134                 protected override void OnLeave (int rowIndex, bool throughMouseClick) {
135                 }
136
137                 protected override void OnMouseClick (DataGridViewCellMouseEventArgs e) {
138                 }
139
140                 protected override void OnMouseEnter (int rowIndex) {
141                 }
142
143                 protected override void OnMouseLeave (int rowIndex) {
144                 }
145
146                 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) {
147                         //////////////////
148                         /*
149                         Size size = DataGridViewCell.MeasureTextSize(graphics, (string) formattedValue, cellStyle.Font, TextFormatFlags.Default);
150                         switch (cellStyle.Alignment) {
151                                 case DataGridViewContentAlignment.TopLeft:
152                                         break;
153                         }
154                         //cell.SetContentBounds(cellBounds);
155                         */
156                         //////////////////
157                         if ((cellState & DataGridViewElementStates.Selected) != 0) {
158                                 graphics.FillRectangle(new SolidBrush(cellStyle.SelectionBackColor), cellBounds);
159                                 graphics.DrawString((string) formattedValue, cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), cellBounds, StringFormat.GenericDefault);
160                         }
161                         else {
162                                 graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);
163                                 graphics.DrawString((string) formattedValue, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), cellBounds, StringFormat.GenericDefault);
164                         }
165                         PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
166                 }
167
168                 protected override void OnDataGridViewChanged () {
169                         editingControl.EditingControlDataGridView = DataGridView;
170                 }
171
172         }
173
174 }
175
176 #endif