2008-12-08 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewImageCell.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
27 #if NET_2_0
28
29 using System.Drawing;
30 using System.ComponentModel;
31
32 namespace System.Windows.Forms {
33
34         public class DataGridViewImageCell : DataGridViewCell {
35
36                 private object defaultNewRowValue;
37                 private string description;
38                 private DataGridViewImageCellLayout imageLayout;
39                 private bool valueIsIcon;
40
41                 private static Image missing_image;
42                 
43                 public DataGridViewImageCell (bool valueIsIcon) {
44                         this.valueIsIcon = valueIsIcon;
45                         this.imageLayout = DataGridViewImageCellLayout.NotSet;
46                 }
47
48                 public DataGridViewImageCell () : this(false) {
49                 }
50
51                 static DataGridViewImageCell ()
52                 {
53                         missing_image = ResourceImageLoader.Get ("image-missing.png");
54                 }
55                 
56                 public override object DefaultNewRowValue {
57                         get { return missing_image; }
58                 }
59
60                 [DefaultValue ("")]
61                 public string Description {
62                         get { return description; }
63                         set { description = value; }
64                 }
65
66                 public override Type EditType {
67                         get { return null; }
68                 }
69
70                 public override Type FormattedValueType {
71                         get { return (valueIsIcon)? typeof(Icon) : typeof(Image); }
72                 }
73
74                 [DefaultValue (DataGridViewImageCellLayout.NotSet)]
75                 public DataGridViewImageCellLayout ImageLayout {
76                         get { return imageLayout; }
77                         set {
78                                 if (!Enum.IsDefined(typeof(DataGridViewImageCellLayout), value)) {
79                                         throw new InvalidEnumArgumentException("Value is invalid image cell layout.");
80                                 }
81                                 imageLayout = value;
82                         }
83                 }
84
85                 [DefaultValue (false)]
86                 public bool ValueIsIcon {
87                         get { return valueIsIcon; }
88                         set { valueIsIcon = value; }
89                 }
90
91                 public override Type ValueType {
92                         get {
93                                 if (base.ValueType != null) {
94                                         return base.ValueType;
95                                 }
96                                 if (OwningColumn != null && OwningColumn.ValueType != null) {
97                                         return OwningColumn.ValueType;
98                                 }
99                                 if (valueIsIcon) {
100                                         return typeof(Icon);
101                                 }
102                                 else {
103                                         return typeof(Image);
104                                 }
105                         }
106                         set { base.ValueType = value; }
107                 }
108
109                 public override object Clone () {
110                         DataGridViewImageCell cell = (DataGridViewImageCell) base.Clone();
111                         cell.defaultNewRowValue = this.defaultNewRowValue;
112                         cell.description = this.description;
113                         cell.valueIsIcon = this.valueIsIcon;
114                         return cell;
115                 }
116
117                 public override string ToString () {
118                         return GetType().Name;
119                 }
120
121                 protected override AccessibleObject CreateAccessibilityInstance () {
122                         return new DataGridViewImageCellAccessibleObject(this);
123                 }
124
125                 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
126                 {
127                         if (DataGridView == null)
128                                 return Rectangle.Empty;
129                                 
130                         Rectangle image_bounds = Rectangle.Empty;
131                         Image i = (Image)GetFormattedValue (Value, rowIndex, ref cellStyle, null, null, DataGridViewDataErrorContexts.PreferredSize);
132                         
133                         if (i == null)
134                                 i = missing_image;
135
136                         switch (imageLayout) {
137                                 case DataGridViewImageCellLayout.NotSet:
138                                 case DataGridViewImageCellLayout.Normal:
139                                         image_bounds = new Rectangle ((Size.Width - i.Width) / 2, (Size.Height - i.Height) / 2, i.Width, i.Height);
140                                         break;
141                                 case DataGridViewImageCellLayout.Stretch:
142                                         image_bounds = new Rectangle (Point.Empty, Size);
143                                         break;
144                                 case DataGridViewImageCellLayout.Zoom:
145                                         Size image_size;
146
147                                         if (((float)i.Width / (float)i.Height) >= ((float)Size.Width / (float)Size.Height))
148                                                 image_size = new Size (Size.Width, (i.Height * Size.Width) / i.Width);
149                                         else
150                                                 image_size = new Size ((i.Width * Size.Height) / i.Height, Size.Height);
151
152                                         image_bounds = new Rectangle ((Size.Width - image_size.Width) / 2, (Size.Height - image_size.Height) / 2, image_size.Width, image_size.Height);
153                                         break;
154                         }
155                         
156                         return image_bounds;
157                 }
158
159                 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
160                 {
161                         if (DataGridView == null || string.IsNullOrEmpty (ErrorText))
162                                 return Rectangle.Empty;
163
164                         Size error_icon = new Size (12, 11);
165                         return new Rectangle (new Point (Size.Width - error_icon.Width - 5, (Size.Height - error_icon.Height) / 2), error_icon);
166                 }
167
168                 protected override object GetFormattedValue (object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
169                 {
170                         return base.GetFormattedValue (value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
171                 }
172
173                 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
174                 {
175                         Image i = (Image)FormattedValue;
176                         
177                         if (i == null)
178                                 return new Size (21, 20);
179                                 
180                         if (i != null)
181                                 return new Size (i.Width + 1, i.Height + 1);
182
183                         return new Size (21, 20);
184                 }
185
186                 protected override object GetValue (int rowIndex)
187                 {
188                         return base.GetValue (rowIndex);
189                 }
190
191                 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)
192                 {
193                         base.Paint (graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
194                 }
195                 
196                 internal override void PaintPartContent (Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, object formattedValue)
197                 {
198                         Image i;
199                         
200                         if (formattedValue == null)
201                                 i = missing_image;
202                         else
203                                 i = (Image)formattedValue;
204
205                         Rectangle image_bounds = Rectangle.Empty;
206
207                         switch (imageLayout) {
208                                 case DataGridViewImageCellLayout.NotSet:
209                                 case DataGridViewImageCellLayout.Normal:
210                                         image_bounds = AlignInRectangle (new Rectangle (2, 2, cellBounds.Width - 4, cellBounds.Height - 4), i.Size, cellStyle.Alignment);
211                                         break;
212                                 case DataGridViewImageCellLayout.Stretch:
213                                         image_bounds = new Rectangle (Point.Empty, cellBounds.Size);
214                                         break;
215                                 case DataGridViewImageCellLayout.Zoom:
216                                         Size image_size;
217
218                                         if (((float)i.Width / (float)i.Height) >= ((float)Size.Width / (float)Size.Height))
219                                                 image_size = new Size (Size.Width, (i.Height * Size.Width) / i.Width);
220                                         else
221                                                 image_size = new Size ((i.Width * Size.Height) / i.Height, Size.Height);
222
223                                         image_bounds = new Rectangle ((Size.Width - image_size.Width) / 2, (Size.Height - image_size.Height) / 2, image_size.Width, image_size.Height);
224                                         break;
225                                 default:
226                                         break;
227                         }
228
229                         image_bounds.X += cellBounds.Left;
230                         image_bounds.Y += cellBounds.Top;
231                         
232                         graphics.DrawImage (i, image_bounds);
233                 }
234                 
235                 protected class DataGridViewImageCellAccessibleObject : DataGridViewCellAccessibleObject {
236
237                         public DataGridViewImageCellAccessibleObject (DataGridViewCell owner) : base(owner) {
238                         }
239
240                         public override string DefaultAction {
241                                 get { return ""; }
242                         }
243
244                         public override string Description {
245                                 get { return (Owner as DataGridViewImageCell).Description; }
246                         }
247
248                         public override string Value {
249                                 get { return base.Value; }
250                                 set { base.Value = value; }
251                         }
252                         
253                         public override void DoDefaultAction () {
254                                 // The DataGridViewImageCell has no default action.
255                         }
256
257                         public override int GetChildCount () {
258                                 return -1;
259                         }
260
261                 }
262
263         }
264
265 }
266
267 #endif