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;
43 private static readonly int offset_x = 0;
44 private static readonly int offset_y = 0;
45 #endregion // Local Variables
48 public DataGridTextBoxColumn () : this (null, String.Empty, false)
52 public DataGridTextBoxColumn (PropertyDescriptor prop) : this (prop, String.Empty, false)
56 public DataGridTextBoxColumn (PropertyDescriptor prop, bool isDefault) : this (prop, String.Empty, isDefault)
60 public DataGridTextBoxColumn (PropertyDescriptor prop, string format) : this (prop, format, false)
64 public DataGridTextBoxColumn (PropertyDescriptor prop, string format, bool isDefault) : base (prop)
67 is_default = isDefault;
69 textbox = new DataGridTextBox ();
70 textbox.Multiline = true;
71 textbox.WordWrap = false;
72 textbox.BorderStyle = BorderStyle.None;
73 textbox.Visible = false;
78 #region Public Instance Properties
79 [Editor("System.Windows.Forms.Design.DataGridColumnStyleFormatEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
80 public string Format {
81 get { return format; }
83 if (value != format) {
91 [EditorBrowsable(EditorBrowsableState.Advanced)]
92 public IFormatProvider FormatInfo {
93 get { return format_provider; }
95 if (value != format_provider) {
96 format_provider = value;
102 public override PropertyDescriptor PropertyDescriptor {
103 set { base.PropertyDescriptor = value; }
106 public override bool ReadOnly {
107 get { return base.ReadOnly; }
108 set { base.ReadOnly = value; }
112 public virtual TextBox TextBox {
113 get { return textbox; }
115 #endregion // Public Instance Properties
117 #region Public Instance Methods
119 protected internal override void Abort (int rowNum)
124 protected internal override bool Commit (CurrencyManager dataSource, int rowNum)
126 /* Do not write data if not editing. */
127 if (textbox.IsInEditOrNavigateMode)
131 object obj = GetColumnValueAtRow (dataSource, rowNum);
132 string existing_text = GetFormattedString (obj);
134 if (existing_text != textbox.Text) {
135 if (textbox.Text == NullText)
136 SetColumnValueAtRow (dataSource, rowNum, DBNull.Value);
138 SetColumnValueAtRow (dataSource, rowNum, textbox.Text);
141 catch (Exception e) {
142 Console.WriteLine ("exception!?!?!?! {0}", e);
151 protected internal override void ConcedeFocus ()
156 protected internal override void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool _ro, string instantText, bool cellIsVisible)
158 grid.SuspendLayout ();
160 textbox.TextAlign = alignment;
162 if ((ParentReadOnly == true) ||
163 (ParentReadOnly == false && ReadOnly == true) ||
164 (ParentReadOnly == false && _ro == true)) {
165 textbox.ReadOnly = true;
167 textbox.ReadOnly = false;
170 if (instantText != null && instantText != "") {
171 textbox.Text = instantText;
174 object obj = GetColumnValueAtRow (source, rowNum);
178 textbox.Text = GetFormattedString (obj);
181 textbox.Location = new Point (bounds.X + offset_x, bounds.Y + offset_y);
182 textbox.Size = new Size (bounds.Width - offset_x, bounds.Height - offset_y);
184 textbox.IsInEditOrNavigateMode = true;
185 textbox.Visible = cellIsVisible;
187 textbox.SelectAll ();
188 grid.ResumeLayout (false);
191 protected void EndEdit ()
196 protected internal override void EnterNullValue ()
198 textbox.Text = NullText;
201 protected internal override int GetMinimumHeight ()
203 return FontHeight + 3;
207 protected internal override int GetPreferredHeight (Graphics g, object value)
209 throw new NotImplementedException ();
213 protected internal override Size GetPreferredSize (Graphics g, object value)
215 throw new NotImplementedException ();
219 protected void HideEditBox ()
221 if (textbox.Visible) {
222 grid.SuspendLayout ();
223 textbox.Bounds = Rectangle.Empty;
224 textbox.Visible = false;
225 textbox.IsInEditOrNavigateMode = true;
226 grid.ResumeLayout (false);
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);
276 PaintGridLine (g, textBounds);
278 textBounds.Y += offset_y;
279 textBounds.Height -= offset_y;
281 string_format.FormatFlags |= StringFormatFlags.NoWrap;
282 g.DrawString (text, DataGridTableStyle.DataGrid.Font, foreBrush, textBounds, string_format);
286 protected internal override void ReleaseHostedControl ()
288 if (textbox != null) {
289 grid.SuspendLayout ();
290 grid.Controls.Remove (textbox);
291 grid.Invalidate (new Rectangle (textbox.Location, textbox.Size));
294 grid.ResumeLayout (false);
298 protected override void SetDataGridInColumn (DataGrid value)
300 base.SetDataGridInColumn (value);
303 textbox.SetDataGrid (grid);
304 grid.SuspendLayout ();
305 grid.Controls.Add (textbox);
306 grid.ResumeLayout (false);
310 protected internal override void UpdateUI (CurrencyManager source, int rowNum, string instantText)
315 #endregion // Public Instance Methods
318 #region Private Instance Methods
320 private string GetFormattedString (object obj)
325 if (obj == DBNull.Value)
328 if (format != null && obj as IFormattable != null) {
329 return ((IFormattable)obj).ToString (format, format_provider);
332 return obj.ToString ();
335 #endregion Private Instance Methods