Merge pull request #2311 from mlancione/master
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / DataGridViewButtonCell.cs
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.
11 // 
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.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Pedro Martínez Juliá <pedromj@gmail.com>
24 //
25
26 using System.Drawing;
27 using System.ComponentModel;
28 using System.Windows.Forms.VisualStyles;
29
30 namespace System.Windows.Forms {
31
32         public class DataGridViewButtonCell : DataGridViewCell {
33
34                 private FlatStyle flatStyle;
35                 private bool useColumnTextForButtonValue;
36                 private PushButtonState button_state;
37                 
38                 public DataGridViewButtonCell ()
39                 {
40                         useColumnTextForButtonValue = false;
41                         button_state = PushButtonState.Normal;
42                 }
43
44                 public override Type EditType {
45                         get { return null; }
46                 }
47
48                 [DefaultValue (FlatStyle.Standard)]
49                 public FlatStyle FlatStyle {
50                         get { return flatStyle; }
51                         set {
52                                 if (!Enum.IsDefined(typeof(FlatStyle), value)) {
53                                         throw new InvalidEnumArgumentException("Value is not valid FlatStyle.");
54                                 }
55                                 if (value == FlatStyle.Popup) {
56                                         throw new Exception("FlatStyle cannot be set to Popup in this control.");
57                                 }
58                         }
59                 }
60
61                 public override Type FormattedValueType {
62                         get { return typeof (string); }
63                 }
64
65                 [DefaultValue (false)]
66                 public bool UseColumnTextForButtonValue {
67                         get { return useColumnTextForButtonValue; }
68                         set { useColumnTextForButtonValue = value; }
69                 }
70
71                 public override Type ValueType {
72                         get { return base.ValueType == null ? typeof (object) : base.ValueType; }
73                 }
74
75                 public override object Clone () {
76                         DataGridViewButtonCell result = (DataGridViewButtonCell) base.Clone();
77                         result.flatStyle = this.flatStyle;
78                         result.useColumnTextForButtonValue = this.useColumnTextForButtonValue;
79                         return result;
80                 }
81
82                 public override string ToString () {
83                         return GetType().Name + ": RowIndex: " + RowIndex.ToString() + "; ColumnIndex: " + ColumnIndex.ToString() + ";";
84                 }
85
86                 protected override AccessibleObject CreateAccessibilityInstance () {
87                         return new DataGridViewButtonCellAccessibleObject(this);
88                 }
89
90                 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
91                 {
92                         if (DataGridView == null)
93                                 return Rectangle.Empty;
94                                 
95                         Rectangle retval = Rectangle.Empty;
96                         
97                         retval.Height = OwningRow.Height - 1;
98                         retval.Width = OwningColumn.Width - 1;
99                         
100                         return retval;
101                 }
102
103                 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
104                 {
105                         if (DataGridView == null || string.IsNullOrEmpty (ErrorText))
106                                 return Rectangle.Empty;
107
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);
110                 }
111
112                 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
113                 {
114                         object o = FormattedValue;
115
116                         if (o != null) {
117                                 Size s = DataGridViewCell.MeasureTextSize (graphics, o.ToString (), cellStyle.Font, TextFormatFlags.Default);
118                                 s.Height = Math.Max (s.Height, 21);
119                                 s.Width += 10;
120                                 return s;
121                         } else
122                                 return new Size (21, 21);
123                 }
124
125                 protected override object GetValue (int rowIndex)
126                 {
127                         if (useColumnTextForButtonValue)
128                                 return (OwningColumn as DataGridViewButtonColumn).Text;
129                                 
130                         return base.GetValue (rowIndex);
131                 }
132
133                 protected override bool KeyDownUnsharesRow (KeyEventArgs e, int rowIndex)
134                 {
135                         // true if the user pressed the SPACE key without modifier keys; otherwise, false
136                         return e.KeyData == Keys.Space;
137                 }
138
139                 protected override bool KeyUpUnsharesRow (KeyEventArgs e, int rowIndex)
140                 {
141                         // true if the user released the SPACE key; otherwise false
142                         return e.KeyData == Keys.Space;
143                 }
144
145                 protected override bool MouseDownUnsharesRow (DataGridViewCellMouseEventArgs e) {
146                         return (e.Button == MouseButtons.Left);
147                 }
148
149                 protected override bool MouseEnterUnsharesRow (int rowIndex)
150                 {
151                         // true if the cell was the last cell receiving a mouse click; otherwise, false.
152                         return false;
153                 }
154
155                 protected override bool MouseLeaveUnsharesRow (int rowIndex)
156                 {
157                         // true if the button displayed by the cell is in the pressed state; otherwise, false.
158                         return button_state == PushButtonState.Pressed;
159                 }
160
161                 protected override bool MouseUpUnsharesRow (DataGridViewCellMouseEventArgs e)
162                 {
163                         // true if the mouse up was caused by the release of the left mouse button; otherwise false.
164                         return e.Button == MouseButtons.Left;
165                 }
166
167                 protected override void OnKeyDown (KeyEventArgs e, int rowIndex)
168                 {
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);
173                         }
174                 }
175
176                 protected override void OnKeyUp (KeyEventArgs e, int rowIndex)
177                 {
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);
182                         }
183                 }
184
185                 protected override void OnLeave (int rowIndex, bool throughMouseClick)
186                 {
187                         if (button_state != PushButtonState.Normal) {
188                                 button_state = PushButtonState.Normal;
189                                 DataGridView.InvalidateCell (this);
190                         }
191                 }
192
193                 protected override void OnMouseDown (DataGridViewCellMouseEventArgs e)
194                 {
195                         if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
196                                 button_state = PushButtonState.Pressed;
197                                 DataGridView.InvalidateCell (this);
198                         }
199                 }
200
201                 protected override void OnMouseLeave (int rowIndex)
202                 {
203                         if (button_state != PushButtonState.Normal) {
204                                 button_state = PushButtonState.Normal;
205                                 DataGridView.InvalidateCell (this);
206                         }
207                 }
208
209                 protected override void OnMouseMove (DataGridViewCellMouseEventArgs e)
210                 {
211                         if (button_state != PushButtonState.Normal && button_state != PushButtonState.Hot) {
212                                 button_state = PushButtonState.Hot;
213                                 DataGridView.InvalidateCell (this);
214                         }
215                 }
216
217                 protected override void OnMouseUp (DataGridViewCellMouseEventArgs e)
218                 {
219                         if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
220                                 button_state = PushButtonState.Normal;
221                                 DataGridView.InvalidateCell (this);
222                         }
223                 }
224
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)
226                 {
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);
230                 }
231
232                 internal override void PaintPartBackground (Graphics graphics, Rectangle cellBounds, DataGridViewCellStyle style)
233                 {
234                         ButtonRenderer.DrawButton (graphics, cellBounds, button_state);
235                 }
236
237                 internal override void PaintPartSelectionBackground (Graphics graphics, Rectangle cellBounds, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle)
238                 {
239                         cellBounds.Inflate (-2, -2);
240                         base.PaintPartSelectionBackground (graphics, cellBounds, cellState, cellStyle);
241                 }
242                 
243                 internal override void PaintPartContent (Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, object formattedValue)
244                 {
245                         Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
246
247                         TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl | TextFormatFlags.HorizontalCenter;
248
249                         cellBounds.Height -= 2;
250                         cellBounds.Width -= 2;
251
252                         if (formattedValue != null)
253                                 TextRenderer.DrawText (graphics, formattedValue.ToString (), cellStyle.Font, cellBounds, color, flags);
254                 }
255                 
256                 protected class DataGridViewButtonCellAccessibleObject : DataGridViewCellAccessibleObject {
257
258                         public DataGridViewButtonCellAccessibleObject (DataGridViewCell owner) : base(owner) {
259                         }
260
261                         public override string DefaultAction {
262                                 get {
263                                         if (Owner.ReadOnly) {
264                                                 return "Press";
265                                         }
266                                         else {
267                                                 return "";
268                                         }
269                                 }
270                         }
271
272                         public override void DoDefaultAction () {
273                                 // causes the button in the ButtonCell to be clicked
274                         }
275
276                         public override int GetChildCount () {
277                                 return -1;
278                         }
279
280                 }
281
282         }
283
284 }
285