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