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