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>
30 using System.ComponentModel;
32 namespace System.Windows.Forms {
34 public class DataGridViewImageCell : DataGridViewCell {
36 private object defaultNewRowValue;
37 private string description;
38 private DataGridViewImageCellLayout imageLayout;
39 private bool valueIsIcon;
41 private static Image missing_image;
43 public DataGridViewImageCell (bool valueIsIcon) {
44 this.valueIsIcon = valueIsIcon;
45 this.imageLayout = DataGridViewImageCellLayout.NotSet;
48 public DataGridViewImageCell () : this(false) {
51 static DataGridViewImageCell ()
53 missing_image = ResourceImageLoader.Get ("image-missing.png");
56 public override object DefaultNewRowValue {
57 get { return missing_image; }
61 public string Description {
62 get { return description; }
63 set { description = value; }
66 public override Type EditType {
70 public override Type FormattedValueType {
71 get { return (valueIsIcon)? typeof(Icon) : typeof(Image); }
74 [DefaultValue (DataGridViewImageCellLayout.NotSet)]
75 public DataGridViewImageCellLayout ImageLayout {
76 get { return imageLayout; }
78 if (!Enum.IsDefined(typeof(DataGridViewImageCellLayout), value)) {
79 throw new InvalidEnumArgumentException("Value is invalid image cell layout.");
85 [DefaultValue (false)]
86 public bool ValueIsIcon {
87 get { return valueIsIcon; }
88 set { valueIsIcon = value; }
91 public override Type ValueType {
93 if (base.ValueType != null) {
94 return base.ValueType;
96 if (OwningColumn != null && OwningColumn.ValueType != null) {
97 return OwningColumn.ValueType;
103 return typeof(Image);
106 set { base.ValueType = value; }
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;
117 public override string ToString () {
118 return GetType().Name;
121 protected override AccessibleObject CreateAccessibilityInstance () {
122 return new DataGridViewImageCellAccessibleObject(this);
125 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
127 if (DataGridView == null)
128 return Rectangle.Empty;
130 Rectangle image_bounds = Rectangle.Empty;
131 Image i = (Image)GetFormattedValue (Value, rowIndex, ref cellStyle, null, null, DataGridViewDataErrorContexts.PreferredSize);
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);
141 case DataGridViewImageCellLayout.Stretch:
142 image_bounds = new Rectangle (Point.Empty, Size);
144 case DataGridViewImageCellLayout.Zoom:
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);
150 image_size = new Size ((i.Width * Size.Height) / i.Height, Size.Height);
152 image_bounds = new Rectangle ((Size.Width - image_size.Width) / 2, (Size.Height - image_size.Height) / 2, image_size.Width, image_size.Height);
159 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
161 if (DataGridView == null || string.IsNullOrEmpty (ErrorText))
162 return Rectangle.Empty;
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);
168 protected override object GetFormattedValue (object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
170 return base.GetFormattedValue (value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
173 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
175 Image i = (Image)FormattedValue;
178 return new Size (21, 20);
181 return new Size (i.Width + 1, i.Height + 1);
183 return new Size (21, 20);
186 protected override object GetValue (int rowIndex)
188 return base.GetValue (rowIndex);
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)
193 base.Paint (graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
196 internal override void PaintPartContent (Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, object formattedValue)
200 if (formattedValue == null)
203 i = (Image)formattedValue;
205 Rectangle image_bounds = Rectangle.Empty;
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);
212 case DataGridViewImageCellLayout.Stretch:
213 image_bounds = new Rectangle (Point.Empty, cellBounds.Size);
215 case DataGridViewImageCellLayout.Zoom:
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);
221 image_size = new Size ((i.Width * Size.Height) / i.Height, Size.Height);
223 image_bounds = new Rectangle ((Size.Width - image_size.Width) / 2, (Size.Height - image_size.Height) / 2, image_size.Width, image_size.Height);
229 image_bounds.X += cellBounds.Left;
230 image_bounds.Y += cellBounds.Top;
232 graphics.DrawImage (i, image_bounds);
235 protected class DataGridViewImageCellAccessibleObject : DataGridViewCellAccessibleObject {
237 public DataGridViewImageCellAccessibleObject (DataGridViewCell owner) : base(owner) {
240 public override string DefaultAction {
244 public override string Description {
245 get { return (Owner as DataGridViewImageCell).Description; }
248 public override string Value {
249 get { return base.Value; }
250 set { base.Value = value; }
253 public override void DoDefaultAction () {
254 // The DataGridViewImageCell has no default action.
257 public override int GetChildCount () {