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;
41 private StringFormat string_format = new StringFormat ();
42 private DataGridTextBox textbox = null;
43 #endregion // Local Variables
46 public DataGridTextBoxColumn ()
48 format = string.Empty;
51 public DataGridTextBoxColumn (PropertyDescriptor prop) : base (prop)
53 format = string.Empty;
56 public DataGridTextBoxColumn (PropertyDescriptor prop, bool isDefault) : base (prop)
58 format = string.Empty;
59 is_default = isDefault;
62 public DataGridTextBoxColumn (PropertyDescriptor prop, string format) : base (prop)
67 public DataGridTextBoxColumn (PropertyDescriptor prop, string format, bool isDefault) : base (prop)
70 is_default = isDefault;
75 #region Public Instance Properties
76 [Editor("System.Windows.Forms.Design.DataGridColumnStyleFormatEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
77 public string Format {
82 if (value != format) {
89 [EditorBrowsable(EditorBrowsableState.Advanced)]
90 public IFormatProvider FormatInfo {
92 return format_provider;
95 if (value != format_provider) {
96 format_provider = value;
102 public PropertyDescriptor PropertyDescriptor {
104 base.PropertyDescriptor = value;
108 public override bool ReadOnly {
110 return base.ReadOnly;
113 base.ReadOnly = value;
118 public virtual TextBox TextBox {
123 #endregion // Public Instance Properties
125 #region Public Instance Methods
128 protected internal override void Abort (int rowNum)
133 protected internal override bool Commit (CurrencyManager dataSource, int rowNum)
137 if (textbox.Text == NullText) {
144 SetColumnValueAtRow (dataSource, rowNum, text);
147 catch (Exception e) {
148 string message = "The data entered in column ["+ MappingName +"] has an invalid format.";
149 MessageBox.Show( message);
158 protected internal override void ConcedeFocus ()
163 protected internal override void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool _ro, string instantText, bool cellIsVisible)
167 if (textbox == null) {
168 textbox = new DataGridTextBox ();
169 textbox.SetDataGrid (DataGridTableStyle.DataGrid);
170 DataGridTableStyle.DataGrid.Controls.Add (textbox);
171 textbox.Multiline = true;
172 textbox.BorderStyle = BorderStyle.None;
175 textbox.TextAlign = alignment;
176 textbox.Visible = cellIsVisible;
178 if ((ParentReadOnly == false && ReadOnly == true) ||
179 (ParentReadOnly == false && _ro == true)) {
180 textbox.ReadOnly = true;
182 textbox.ReadOnly = false;
185 textbox.Location = new Point (bounds.X, bounds.Y);
186 textbox.Size = new Size (bounds.Width, bounds.Height);
188 obj = GetColumnValueAtRow (source, rowNum);
189 textbox.Text = GetFormattedString (obj);
191 textbox.SelectAll ();
194 protected void EndEdit ()
196 ReleaseHostedControl ();
197 grid.Invalidate (grid.GetCurrentCellBounds ());
200 protected internal override void EnterNullValue ()
202 if (textbox != null) {
203 textbox.Text = NullText;
207 protected internal override int GetMinimumHeight ()
209 return FontHeight + 3;
213 protected internal override int GetPreferredHeight (Graphics g, object value)
215 throw new NotImplementedException ();
219 protected internal override Size GetPreferredSize (Graphics g, object value)
221 throw new NotImplementedException ();
225 protected void HideEditBox ()
230 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)
232 Paint (g, bounds, source, rowNum, false);
235 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
237 Paint (g, bounds, source, rowNum, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.BackColor),
238 ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.ForeColor), alignToRight);
241 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
244 obj = GetColumnValueAtRow (source, rowNum);
246 PaintText (g, bounds, GetFormattedString (obj), backBrush, foreBrush, alignToRight);
249 protected void PaintText (Graphics g, Rectangle bounds, string text, bool alignToRight)
251 PaintText (g, bounds, text, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.BackColor),
252 ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.ForeColor), alignToRight);
255 protected void PaintText (Graphics g, Rectangle textBounds, string text, Brush backBrush, Brush foreBrush, bool alignToRight)
257 if (alignToRight == true) {
258 string_format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
260 string_format.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft;
264 case HorizontalAlignment.Center:
265 string_format.Alignment = StringAlignment.Center;
267 case HorizontalAlignment.Right:
268 string_format.Alignment = StringAlignment.Far;
271 string_format.Alignment = StringAlignment.Near;
275 g.FillRectangle (backBrush, textBounds);
277 if (table_style.CurrentGridLineStyle == DataGridLineStyle.Solid) {
278 g.DrawRectangle (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor), textBounds);
281 string_format.FormatFlags |= StringFormatFlags.NoWrap;
282 g.DrawString (text, DataGridTableStyle.DataGrid.Font, foreBrush, textBounds, string_format);
285 protected internal override void ReleaseHostedControl ()
287 if (textbox != null) {
288 DataGridTableStyle.DataGrid.Controls.Remove (textbox);
294 protected override void SetDataGridInColumn (DataGrid value)
296 base.SetDataGridInColumn (value);
299 protected internal override void UpdateUI (CurrencyManager source, int rowNum, string instantText)
304 #endregion // Public Instance Methods
307 #region Private Instance Methods
309 // We use DataGridTextBox to render everything that DataGridBoolColumn does not
310 internal static bool CanRenderType (Type type)
312 return (type != typeof (Boolean));
315 private string GetFormattedString (object obj)
317 if (obj == DBNull.Value) {
321 if (format != null && obj as IFormattable != null) {
322 return ((IFormattable)obj).ToString (format, format_provider);
325 return obj.ToString ();
328 #endregion Private Instance Methods