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 // Pedro MartÃnez Juliá <pedromj@gmail.com>
27 using System.ComponentModel;
28 using System.Windows.Forms.VisualStyles;
30 namespace System.Windows.Forms {
32 public class DataGridViewButtonCell : DataGridViewCell {
34 private FlatStyle flatStyle;
35 private bool useColumnTextForButtonValue;
36 private PushButtonState button_state;
38 public DataGridViewButtonCell ()
40 useColumnTextForButtonValue = false;
41 button_state = PushButtonState.Normal;
44 public override Type EditType {
48 [DefaultValue (FlatStyle.Standard)]
49 public FlatStyle FlatStyle {
50 get { return flatStyle; }
52 if (!Enum.IsDefined(typeof(FlatStyle), value)) {
53 throw new InvalidEnumArgumentException("Value is not valid FlatStyle.");
55 if (value == FlatStyle.Popup) {
56 throw new Exception("FlatStyle cannot be set to Popup in this control.");
61 public override Type FormattedValueType {
62 get { return typeof (string); }
65 [DefaultValue (false)]
66 public bool UseColumnTextForButtonValue {
67 get { return useColumnTextForButtonValue; }
68 set { useColumnTextForButtonValue = value; }
71 public override Type ValueType {
72 get { return base.ValueType == null ? typeof (object) : base.ValueType; }
75 public override object Clone () {
76 DataGridViewButtonCell result = (DataGridViewButtonCell) base.Clone();
77 result.flatStyle = this.flatStyle;
78 result.useColumnTextForButtonValue = this.useColumnTextForButtonValue;
82 public override string ToString () {
83 return GetType().Name + ": RowIndex: " + RowIndex.ToString() + "; ColumnIndex: " + ColumnIndex.ToString() + ";";
86 protected override AccessibleObject CreateAccessibilityInstance () {
87 return new DataGridViewButtonCellAccessibleObject(this);
90 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
92 if (DataGridView == null)
93 return Rectangle.Empty;
95 Rectangle retval = Rectangle.Empty;
97 retval.Height = OwningRow.Height - 1;
98 retval.Width = OwningColumn.Width - 1;
103 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
105 if (DataGridView == null || string.IsNullOrEmpty (ErrorText))
106 return Rectangle.Empty;
108 Size error_icon = new Size (12, 11);
109 return new Rectangle (new Point (Size.Width - error_icon.Width - 5, (Size.Height - error_icon.Height) / 2), error_icon);
112 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
114 object o = FormattedValue;
117 Size s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
118 s.Height = Math.Max (s.Height, 21);
122 return new Size (21, 21);
125 protected override object GetValue (int rowIndex)
127 if (useColumnTextForButtonValue)
128 return (OwningColumn as DataGridViewButtonColumn).Text;
130 return base.GetValue (rowIndex);
133 protected override bool KeyDownUnsharesRow (KeyEventArgs e, int rowIndex)
135 // true if the user pressed the SPACE key without modifier keys; otherwise, false
136 return e.KeyData == Keys.Space;
139 protected override bool KeyUpUnsharesRow (KeyEventArgs e, int rowIndex)
141 // true if the user released the SPACE key; otherwise false
142 return e.KeyData == Keys.Space;
145 protected override bool MouseDownUnsharesRow (DataGridViewCellMouseEventArgs e) {
146 return (e.Button == MouseButtons.Left);
149 protected override bool MouseEnterUnsharesRow (int rowIndex)
151 // true if the cell was the last cell receiving a mouse click; otherwise, false.
155 protected override bool MouseLeaveUnsharesRow (int rowIndex)
157 // true if the button displayed by the cell is in the pressed state; otherwise, false.
158 return button_state == PushButtonState.Pressed;
161 protected override bool MouseUpUnsharesRow (DataGridViewCellMouseEventArgs e)
163 // true if the mouse up was caused by the release of the left mouse button; otherwise false.
164 return e.Button == MouseButtons.Left;
167 protected override void OnKeyDown (KeyEventArgs e, int rowIndex)
169 // when activated by the SPACE key, this method updates the cell's user interface
170 if ((e.KeyData & Keys.Space) == Keys.Space) {
171 button_state = PushButtonState.Pressed;
172 DataGridView.InvalidateCell (this);
176 protected override void OnKeyUp (KeyEventArgs e, int rowIndex)
178 // when activated by the SPACE key, this method updates the cell's user interface
179 if ((e.KeyData & Keys.Space) == Keys.Space) {
180 button_state = PushButtonState.Normal;
181 DataGridView.InvalidateCell (this);
185 protected override void OnLeave (int rowIndex, bool throughMouseClick)
187 if (button_state != PushButtonState.Normal) {
188 button_state = PushButtonState.Normal;
189 DataGridView.InvalidateCell (this);
193 protected override void OnMouseDown (DataGridViewCellMouseEventArgs e)
195 if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
196 button_state = PushButtonState.Pressed;
197 DataGridView.InvalidateCell (this);
201 protected override void OnMouseLeave (int rowIndex)
203 if (button_state != PushButtonState.Normal) {
204 button_state = PushButtonState.Normal;
205 DataGridView.InvalidateCell (this);
209 protected override void OnMouseMove (DataGridViewCellMouseEventArgs e)
211 if (button_state != PushButtonState.Normal && button_state != PushButtonState.Hot) {
212 button_state = PushButtonState.Hot;
213 DataGridView.InvalidateCell (this);
217 protected override void OnMouseUp (DataGridViewCellMouseEventArgs e)
219 if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
220 button_state = PushButtonState.Normal;
221 DataGridView.InvalidateCell (this);
225 protected override void Paint (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
227 // The internal paint routines are overridden instead of
228 // doing the custom paint logic here
229 base.Paint (graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
232 internal override void PaintPartBackground (Graphics graphics, Rectangle cellBounds, DataGridViewCellStyle style)
234 ButtonRenderer.DrawButton (graphics, cellBounds, button_state);
237 internal override void PaintPartSelectionBackground (Graphics graphics, Rectangle cellBounds, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle)
239 cellBounds.Inflate (-2, -2);
240 base.PaintPartSelectionBackground (graphics, cellBounds, cellState, cellStyle);
243 internal override void PaintPartContent (Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, object formattedValue)
245 Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
247 TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl | TextFormatFlags.HorizontalCenter;
249 cellBounds.Height -= 2;
250 cellBounds.Width -= 2;
252 if (formattedValue != null)
253 TextRenderer.DrawText (graphics, formattedValue.ToString (), cellStyle.Font, cellBounds, color, flags);
256 protected class DataGridViewButtonCellAccessibleObject : DataGridViewCellAccessibleObject {
258 public DataGridViewButtonCellAccessibleObject (DataGridViewCell owner) : base(owner) {
261 public override string DefaultAction {
263 if (Owner.ReadOnly) {
272 public override void DoDefaultAction () {
273 // causes the button in the ButtonCell to be clicked
276 public override int GetChildCount () {