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>
27 using System.ComponentModel;
29 namespace System.Windows.Forms {
31 public class DataGridViewImageCell : DataGridViewCell {
33 private object defaultNewRowValue;
34 private string description;
35 private DataGridViewImageCellLayout imageLayout;
36 private bool valueIsIcon;
38 private static Image missing_image;
40 public DataGridViewImageCell (bool valueIsIcon) {
41 this.valueIsIcon = valueIsIcon;
42 this.imageLayout = DataGridViewImageCellLayout.NotSet;
45 public DataGridViewImageCell () : this(false) {
48 static DataGridViewImageCell ()
50 missing_image = ResourceImageLoader.Get ("image-missing.png");
53 public override object DefaultNewRowValue {
54 get { return missing_image; }
58 public string Description {
59 get { return description; }
60 set { description = value; }
63 public override Type EditType {
67 public override Type FormattedValueType {
68 get { return (valueIsIcon)? typeof(Icon) : typeof(Image); }
71 [DefaultValue (DataGridViewImageCellLayout.NotSet)]
72 public DataGridViewImageCellLayout ImageLayout {
73 get { return imageLayout; }
75 if (!Enum.IsDefined(typeof(DataGridViewImageCellLayout), value)) {
76 throw new InvalidEnumArgumentException("Value is invalid image cell layout.");
82 [DefaultValue (false)]
83 public bool ValueIsIcon {
84 get { return valueIsIcon; }
85 set { valueIsIcon = value; }
88 public override Type ValueType {
90 if (base.ValueType != null) {
91 return base.ValueType;
93 if (OwningColumn != null && OwningColumn.ValueType != null) {
94 return OwningColumn.ValueType;
100 return typeof(Image);
103 set { base.ValueType = value; }
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;
114 public override string ToString () {
115 return GetType().Name;
118 protected override AccessibleObject CreateAccessibilityInstance () {
119 return new DataGridViewImageCellAccessibleObject(this);
122 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
124 if (DataGridView == null)
125 return Rectangle.Empty;
127 Rectangle image_bounds = Rectangle.Empty;
128 Image i = (Image)GetFormattedValue (Value, rowIndex, ref cellStyle, null, null, DataGridViewDataErrorContexts.PreferredSize);
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);
138 case DataGridViewImageCellLayout.Stretch:
139 image_bounds = new Rectangle (Point.Empty, Size);
141 case DataGridViewImageCellLayout.Zoom:
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);
147 image_size = new Size ((i.Width * Size.Height) / i.Height, Size.Height);
149 image_bounds = new Rectangle ((Size.Width - image_size.Width) / 2, (Size.Height - image_size.Height) / 2, image_size.Width, image_size.Height);
156 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
158 if (DataGridView == null || string.IsNullOrEmpty (ErrorText))
159 return Rectangle.Empty;
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);
165 protected override object GetFormattedValue (object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
167 return base.GetFormattedValue (value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
170 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
172 Image i = (Image)FormattedValue;
175 return new Size (21, 20);
178 return new Size (i.Width + 1, i.Height + 1);
180 return new Size (21, 20);
183 protected override object GetValue (int rowIndex)
185 return base.GetValue (rowIndex);
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)
190 base.Paint (graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
193 internal override void PaintPartContent (Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, object formattedValue)
197 if (formattedValue == null)
200 i = (Image)formattedValue;
202 Rectangle image_bounds = Rectangle.Empty;
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);
209 case DataGridViewImageCellLayout.Stretch:
210 image_bounds = new Rectangle (Point.Empty, cellBounds.Size);
212 case DataGridViewImageCellLayout.Zoom:
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);
218 image_size = new Size ((i.Width * Size.Height) / i.Height, Size.Height);
220 image_bounds = new Rectangle ((Size.Width - image_size.Width) / 2, (Size.Height - image_size.Height) / 2, image_size.Width, image_size.Height);
226 image_bounds.X += cellBounds.Left;
227 image_bounds.Y += cellBounds.Top;
229 graphics.DrawImage (i, image_bounds);
232 protected class DataGridViewImageCellAccessibleObject : DataGridViewCellAccessibleObject {
234 public DataGridViewImageCellAccessibleObject (DataGridViewCell owner) : base(owner) {
237 public override string DefaultAction {
241 public override string Description {
242 get { return (Owner as DataGridViewImageCell).Description; }
245 public override string Value {
246 get { return base.Value; }
247 set { base.Value = value; }
250 public override void DoDefaultAction () {
251 // The DataGridViewImageCell has no default action.
254 public override int GetChildCount () {