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 // Jordi Mas i Hernandez <jordi@ximian.com>
29 using System.ComponentModel;
31 using System.Runtime.InteropServices;
32 using System.Diagnostics;
34 namespace System.Windows.Forms
36 public class DataGridTextBoxColumn : DataGridColumnStyle
38 #region Local Variables
39 private string format;
40 private IFormatProvider format_provider = null;
41 private StringFormat string_format = new StringFormat ();
42 private DataGridTextBox textbox = null;
43 private static readonly int offset_x = 2;
44 private static readonly int offset_y = 2;
45 #endregion // Local Variables
48 public DataGridTextBoxColumn ()
50 format = string.Empty;
53 public DataGridTextBoxColumn (PropertyDescriptor prop) : base (prop)
55 format = string.Empty;
58 public DataGridTextBoxColumn (PropertyDescriptor prop, bool isDefault) : base (prop)
60 format = string.Empty;
61 is_default = isDefault;
64 public DataGridTextBoxColumn (PropertyDescriptor prop, string format) : base (prop)
69 public DataGridTextBoxColumn (PropertyDescriptor prop, string format, bool isDefault) : base (prop)
72 is_default = isDefault;
77 #region Public Instance Properties
78 [Editor("System.Windows.Forms.Design.DataGridColumnStyleFormatEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
79 public string Format {
84 if (value != format) {
91 [EditorBrowsable(EditorBrowsableState.Advanced)]
92 public IFormatProvider FormatInfo {
94 return format_provider;
97 if (value != format_provider) {
98 format_provider = value;
104 public PropertyDescriptor PropertyDescriptor {
106 base.PropertyDescriptor = value;
110 public override bool ReadOnly {
112 return base.ReadOnly;
115 base.ReadOnly = value;
120 public virtual TextBox TextBox {
125 #endregion // Public Instance Properties
127 #region Public Instance Methods
130 protected internal override void Abort (int rowNum)
135 protected internal override bool Commit (CurrencyManager dataSource, int rowNum)
139 if (textbox.Text == NullText) {
146 SetColumnValueAtRow (dataSource, rowNum, text);
149 catch (Exception e) {
150 string message = "The data entered in column ["+ MappingName +"] has an invalid format.";
151 MessageBox.Show( message);
160 protected internal override void ConcedeFocus ()
165 protected internal override void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool _ro, string instantText, bool cellIsVisible)
168 bool newctrl = false;
170 if (textbox == null) {
171 textbox = new DataGridTextBox ();
172 textbox.SetDataGrid (DataGridTableStyle.DataGrid);
174 textbox.Multiline = true;
175 textbox.BorderStyle = BorderStyle.None;
178 textbox.TextAlign = alignment;
179 textbox.Visible = cellIsVisible;
181 if ((ParentReadOnly == true) ||
182 (ParentReadOnly == false && ReadOnly == true) ||
183 (ParentReadOnly == false && _ro == true)) {
184 textbox.ReadOnly = true;
186 textbox.ReadOnly = false;
189 textbox.Location = new Point (bounds.X + offset_x, bounds.Y + offset_y);
190 textbox.Size = new Size (bounds.Width - offset_x, bounds.Height - offset_y);
192 obj = GetColumnValueAtRow (source, rowNum);
193 textbox.Text = GetFormattedString (obj);
196 DataGridTableStyle.DataGrid.Controls.Add (textbox);
199 textbox.SelectAll ();
202 protected void EndEdit ()
204 ReleaseHostedControl ();
205 grid.Invalidate (grid.GetCurrentCellBounds ());
208 protected internal override void EnterNullValue ()
210 if (textbox != null) {
211 textbox.Text = NullText;
215 protected internal override int GetMinimumHeight ()
217 return FontHeight + 3;
221 protected internal override int GetPreferredHeight (Graphics g, object value)
223 throw new NotImplementedException ();
227 protected internal override Size GetPreferredSize (Graphics g, object value)
229 throw new NotImplementedException ();
233 protected void HideEditBox ()
238 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)
240 Paint (g, bounds, source, rowNum, false);
243 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
245 Paint (g, bounds, source, rowNum, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.BackColor),
246 ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.ForeColor), alignToRight);
249 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
252 obj = GetColumnValueAtRow (source, rowNum);
254 PaintText (g, bounds, GetFormattedString (obj), backBrush, foreBrush, alignToRight);
257 protected void PaintText (Graphics g, Rectangle bounds, string text, bool alignToRight)
259 PaintText (g, bounds, text, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.BackColor),
260 ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.ForeColor), alignToRight);
263 protected void PaintText (Graphics g, Rectangle textBounds, string text, Brush backBrush, Brush foreBrush, bool alignToRight)
265 if (alignToRight == true) {
266 string_format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
268 string_format.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft;
272 case HorizontalAlignment.Center:
273 string_format.Alignment = StringAlignment.Center;
275 case HorizontalAlignment.Right:
276 string_format.Alignment = StringAlignment.Far;
279 string_format.Alignment = StringAlignment.Near;
283 g.FillRectangle (backBrush, textBounds);
284 PaintGridLine (g, textBounds);
286 textBounds.Y += offset_y;
287 textBounds.Height -= offset_y;
289 string_format.FormatFlags |= StringFormatFlags.NoWrap;
290 g.DrawString (text, DataGridTableStyle.DataGrid.Font, foreBrush, textBounds, string_format);
294 protected internal override void ReleaseHostedControl ()
296 if (textbox != null) {
297 DataGridTableStyle.DataGrid.Controls.Remove (textbox);
303 protected override void SetDataGridInColumn (DataGrid value)
305 base.SetDataGridInColumn (value);
308 protected internal override void UpdateUI (CurrencyManager source, int rowNum, string instantText)
313 #endregion // Public Instance Methods
316 #region Private Instance Methods
318 // We use DataGridTextBox to render everything that DataGridBoolColumn does not
319 internal static bool CanRenderType (Type type)
321 return (type != typeof (Boolean));
324 private string GetFormattedString (object obj)
326 if (obj == DBNull.Value) {
330 if (format != null && obj as IFormattable != null) {
331 return ((IFormattable)obj).ToString (format, format_provider);
334 return obj.ToString ();
337 #endregion Private Instance Methods