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>
29 using System.ComponentModel;
30 using System.Windows.Forms.VisualStyles;
32 namespace System.Windows.Forms {
34 public class DataGridViewButtonCell : DataGridViewCell {
36 private FlatStyle flatStyle;
37 private bool useColumnTextForButtonValue;
38 private PushButtonState button_state;
40 public DataGridViewButtonCell ()
42 useColumnTextForButtonValue = false;
43 button_state = PushButtonState.Normal;
46 public override Type EditType {
50 [DefaultValue (FlatStyle.Standard)]
51 public FlatStyle FlatStyle {
52 get { return flatStyle; }
54 if (!Enum.IsDefined(typeof(FlatStyle), value)) {
55 throw new InvalidEnumArgumentException("Value is not valid FlatStyle.");
57 if (value == FlatStyle.Popup) {
58 throw new Exception("FlatStyle cannot be set to Popup in this control.");
63 public override Type FormattedValueType {
64 get { return typeof (string); }
67 [DefaultValue (false)]
68 public bool UseColumnTextForButtonValue {
69 get { return useColumnTextForButtonValue; }
70 set { useColumnTextForButtonValue = value; }
73 public override Type ValueType {
74 get { return base.ValueType == null ? typeof (object) : base.ValueType; }
77 public override object Clone () {
78 DataGridViewButtonCell result = (DataGridViewButtonCell) base.Clone();
79 result.flatStyle = this.flatStyle;
80 result.useColumnTextForButtonValue = this.useColumnTextForButtonValue;
84 public override string ToString () {
85 return GetType().Name + ": RowIndex: " + RowIndex.ToString() + "; ColumnIndex: " + ColumnIndex.ToString() + ";";
88 protected override AccessibleObject CreateAccessibilityInstance () {
89 return new DataGridViewButtonCellAccessibleObject(this);
92 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
94 if (DataGridView == null)
95 return Rectangle.Empty;
97 Rectangle retval = Rectangle.Empty;
99 retval.Height = OwningRow.Height - 1;
100 retval.Width = OwningColumn.Width - 1;
105 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
107 if (DataGridView == null || string.IsNullOrEmpty (ErrorText))
108 return Rectangle.Empty;
110 Size error_icon = new Size (12, 11);
111 return new Rectangle (new Point (Size.Width - error_icon.Width - 5, (Size.Height - error_icon.Height) / 2), error_icon);
114 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
116 object o = FormattedValue;
119 Size s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
120 s.Height = Math.Max (s.Height, 21);
124 return new Size (21, 21);
127 protected override object GetValue (int rowIndex)
129 if (useColumnTextForButtonValue)
130 return (OwningColumn as DataGridViewButtonColumn).Text;
132 return base.GetValue (rowIndex);
135 protected override bool KeyDownUnsharesRow (KeyEventArgs e, int rowIndex)
137 // true if the user pressed the SPACE key without modifier keys; otherwise, false
138 return e.KeyData == Keys.Space;
141 protected override bool KeyUpUnsharesRow (KeyEventArgs e, int rowIndex)
143 // true if the user released the SPACE key; otherwise false
144 return e.KeyData == Keys.Space;
147 protected override bool MouseDownUnsharesRow (DataGridViewCellMouseEventArgs e) {
148 return (e.Button == MouseButtons.Left);
151 protected override bool MouseEnterUnsharesRow (int rowIndex)
153 // true if the cell was the last cell receiving a mouse click; otherwise, false.
157 protected override bool MouseLeaveUnsharesRow (int rowIndex)
159 // true if the button displayed by the cell is in the pressed state; otherwise, false.
160 return button_state == PushButtonState.Pressed;
163 protected override bool MouseUpUnsharesRow (DataGridViewCellMouseEventArgs e)
165 // true if the mouse up was caused by the release of the left mouse button; otherwise false.
166 return e.Button == MouseButtons.Left;
169 protected override void OnKeyDown (KeyEventArgs e, int rowIndex)
171 // when activated by the SPACE key, this method updates the cell's user interface
172 if ((e.KeyData & Keys.Space) == Keys.Space) {
173 button_state = PushButtonState.Pressed;
174 DataGridView.InvalidateCell (this);
178 protected override void OnKeyUp (KeyEventArgs e, int rowIndex)
180 // when activated by the SPACE key, this method updates the cell's user interface
181 if ((e.KeyData & Keys.Space) == Keys.Space) {
182 button_state = PushButtonState.Normal;
183 DataGridView.InvalidateCell (this);
187 protected override void OnLeave (int rowIndex, bool throughMouseClick)
189 if (button_state != PushButtonState.Normal) {
190 button_state = PushButtonState.Normal;
191 DataGridView.InvalidateCell (this);
195 protected override void OnMouseDown (DataGridViewCellMouseEventArgs e)
197 if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
198 button_state = PushButtonState.Pressed;
199 DataGridView.InvalidateCell (this);
203 protected override void OnMouseLeave (int rowIndex)
205 if (button_state != PushButtonState.Normal) {
206 button_state = PushButtonState.Normal;
207 DataGridView.InvalidateCell (this);
211 protected override void OnMouseMove (DataGridViewCellMouseEventArgs e)
213 if (button_state != PushButtonState.Normal && button_state != PushButtonState.Hot) {
214 button_state = PushButtonState.Hot;
215 DataGridView.InvalidateCell (this);
219 protected override void OnMouseUp (DataGridViewCellMouseEventArgs e)
221 if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
222 button_state = PushButtonState.Normal;
223 DataGridView.InvalidateCell (this);
227 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)
229 // The internal paint routines are overridden instead of
230 // doing the custom paint logic here
231 base.Paint (graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
234 internal override void PaintPartBackground (Graphics graphics, Rectangle cellBounds, DataGridViewCellStyle style)
236 ButtonRenderer.DrawButton (graphics, cellBounds, button_state);
239 internal override void PaintPartSelectionBackground (Graphics graphics, Rectangle cellBounds, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle)
241 cellBounds.Inflate (-2, -2);
242 base.PaintPartSelectionBackground (graphics, cellBounds, cellState, cellStyle);
245 internal override void PaintPartContent (Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, object formattedValue)
247 Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
249 TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl | TextFormatFlags.HorizontalCenter;
251 cellBounds.Height -= 2;
252 cellBounds.Width -= 2;
254 if (formattedValue != null)
255 TextRenderer.DrawText (graphics, formattedValue.ToString (), cellStyle.Font, cellBounds, color, flags);
258 protected class DataGridViewButtonCellAccessibleObject : DataGridViewCellAccessibleObject {
260 public DataGridViewButtonCellAccessibleObject (DataGridViewCell owner) : base(owner) {
263 public override string DefaultAction {
265 if (Owner.ReadOnly) {
274 public override void DoDefaultAction () {
275 // causes the button in the ButtonCell to be clicked
278 public override int GetChildCount () {