* TextBoxBase.cs: Take HideSelection into account when
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridTextBoxColumn.cs
bloba5abc3e2a638ce5d18c8e5f97248c02ff578b68b
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.
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)
22 // Author:
23 // Jordi Mas i Hernandez <jordi@ximian.com>
27 // NOT COMPLETE
29 using System.ComponentModel;
30 using System.Drawing;
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
47 #region Constructors
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)
66 Format = format;
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;
76 #endregion
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; }
82 set {
83 if (value != format) {
84 format = value;
85 Invalidate ();
90 [Browsable(false)]
91 [EditorBrowsable(EditorBrowsableState.Advanced)]
92 public IFormatProvider FormatInfo {
93 get { return format_provider; }
94 set {
95 if (value != format_provider) {
96 format_provider = value;
101 [DefaultValue(null)]
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; }
111 [Browsable(false)]
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)
121 EndEdit ();
124 protected internal override bool Commit (CurrencyManager dataSource, int rowNum)
126 /* Do not write data if not editing. */
127 if (textbox.IsInEditOrNavigateMode)
128 return true;
130 try {
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);
137 else
138 SetColumnValueAtRow (dataSource, rowNum, textbox.Text);
141 catch {
142 return false;
145 EndEdit ();
146 return true;
149 [MonoTODO]
150 protected internal override void ConcedeFocus ()
152 HideEditBox ();
155 protected internal override void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool _ro, string instantText, bool cellIsVisible)
157 grid.SuspendLayout ();
159 textbox.TextAlign = alignment;
161 if ((ParentReadOnly == true) ||
162 (ParentReadOnly == false && ReadOnly == true) ||
163 (ParentReadOnly == false && _ro == true)) {
164 textbox.ReadOnly = true;
165 } else {
166 textbox.ReadOnly = false;
169 if (instantText != null && instantText != "") {
170 textbox.Text = instantText;
172 else {
173 object obj = GetColumnValueAtRow (source, rowNum);
174 if (obj == null)
175 textbox.Text = "";
176 else
177 textbox.Text = GetFormattedString (obj);
180 textbox.Location = new Point (bounds.X + offset_x, bounds.Y + offset_y);
181 textbox.Size = new Size (bounds.Width - offset_x, bounds.Height - offset_y);
183 textbox.IsInEditOrNavigateMode = true;
184 textbox.Visible = cellIsVisible;
185 textbox.Focus ();
186 textbox.SelectAll ();
187 grid.ResumeLayout (false);
190 protected void EndEdit ()
192 HideEditBox ();
195 protected internal override void EnterNullValue ()
197 textbox.Text = NullText;
200 protected internal override int GetMinimumHeight ()
202 return FontHeight + 3;
205 [MonoTODO]
206 protected internal override int GetPreferredHeight (Graphics g, object value)
208 throw new NotImplementedException ();
211 [MonoTODO]
212 protected internal override Size GetPreferredSize (Graphics g, object value)
214 throw new NotImplementedException ();
217 [MonoTODO]
218 protected void HideEditBox ()
220 if (textbox.Visible) {
221 grid.SuspendLayout ();
222 textbox.Bounds = Rectangle.Empty;
223 textbox.Visible = false;
224 textbox.IsInEditOrNavigateMode = true;
225 grid.ResumeLayout (false);
229 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)
231 Paint (g, bounds, source, rowNum, false);
234 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
236 Paint (g, bounds, source, rowNum, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.BackColor),
237 ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.ForeColor), alignToRight);
240 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
242 object obj;
243 obj = GetColumnValueAtRow (source, rowNum);
245 PaintText (g, bounds, GetFormattedString (obj), backBrush, foreBrush, alignToRight);
248 protected void PaintText (Graphics g, Rectangle bounds, string text, bool alignToRight)
250 PaintText (g, bounds, text, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.BackColor),
251 ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.ForeColor), alignToRight);
254 protected void PaintText (Graphics g, Rectangle textBounds, string text, Brush backBrush, Brush foreBrush, bool alignToRight)
256 if (alignToRight == true) {
257 string_format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
258 } else {
259 string_format.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft;
262 switch (alignment) {
263 case HorizontalAlignment.Center:
264 string_format.Alignment = StringAlignment.Center;
265 break;
266 case HorizontalAlignment.Right:
267 string_format.Alignment = StringAlignment.Far;
268 break;
269 default:
270 string_format.Alignment = StringAlignment.Near;
271 break;
274 g.FillRectangle (backBrush, textBounds);
275 PaintGridLine (g, textBounds);
277 textBounds.Y += offset_y;
278 textBounds.Height -= offset_y;
280 string_format.FormatFlags |= StringFormatFlags.NoWrap;
281 g.DrawString (text, DataGridTableStyle.DataGrid.Font, foreBrush, textBounds, string_format);
285 protected internal override void ReleaseHostedControl ()
287 if (textbox != null) {
288 grid.SuspendLayout ();
289 grid.Controls.Remove (textbox);
290 grid.Invalidate (new Rectangle (textbox.Location, textbox.Size));
291 textbox.Dispose ();
292 textbox = null;
293 grid.ResumeLayout (false);
297 protected override void SetDataGridInColumn (DataGrid value)
299 base.SetDataGridInColumn (value);
301 if (value != null) {
302 textbox.SetDataGrid (grid);
303 grid.SuspendLayout ();
304 grid.Controls.Add (textbox);
305 grid.ResumeLayout (false);
309 protected internal override void UpdateUI (CurrencyManager source, int rowNum, string instantText)
314 #endregion // Public Instance Methods
317 #region Private Instance Methods
319 private string GetFormattedString (object obj)
321 if (obj == null)
322 return "";
324 if (obj == DBNull.Value)
325 return NullText;
327 if (format != null && obj as IFormattable != null) {
328 return ((IFormattable)obj).ToString (format, format_provider);
331 return obj.ToString ();
334 #endregion Private Instance Methods