moel@345: using System;
moel@345: using System.Collections.Generic;
moel@345: using System.Text;
moel@345: using System.Drawing;
moel@345: using System.Windows.Forms;
moel@345: using Aga.Controls.Properties;
moel@345: using System.ComponentModel;
moel@345: 
moel@345: namespace Aga.Controls.Tree.NodeControls
moel@345: {
moel@345: 	public class NodeIcon : BindableControl
moel@345: 	{
moel@345: 		public NodeIcon()
moel@345: 		{
moel@345: 			LeftMargin = 1;
moel@345: 		}
moel@345: 
moel@345: 		public override Size MeasureSize(TreeNodeAdv node, DrawContext context)
moel@345: 		{
moel@345: 			Image image = GetIcon(node);
moel@345: 			if (image != null)
moel@345: 				return image.Size;
moel@345: 			else
moel@345: 				return Size.Empty;
moel@345: 		}
moel@345: 
moel@345: 
moel@345: 		public override void Draw(TreeNodeAdv node, DrawContext context)
moel@345: 		{
moel@345: 			Image image = GetIcon(node);
moel@345: 			if (image != null)
moel@345: 			{
moel@345: 				Rectangle r = GetBounds(node, context);
moel@345: 				if ( image.Width > 0 && image.Height > 0 )
moel@345: 				{
moel@345: 					switch (_scaleMode)
moel@345: 					{
moel@345: 						case ImageScaleMode.Fit:
moel@345: 							context.Graphics.DrawImage(image, r);
moel@345: 							break;
moel@345: 						case ImageScaleMode.ScaleDown:
moel@345: 							{
moel@345: 								float factor = Math.Min((float)r.Width / (float)image.Width, (float)r.Height / (float)image.Height);
moel@345: 								if (factor < 1)
moel@345: 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width * factor, image.Height * factor);
moel@345: 								else
moel@345: 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width, image.Height);
moel@345: 							} break;
moel@345: 						case ImageScaleMode.ScaleUp:
moel@345: 							{
moel@345: 								float factor = Math.Max((float)r.Width / (float)image.Width, (float)r.Height / (float)image.Height);
moel@345: 								if (factor > 1)
moel@345: 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width * factor, image.Height * factor);
moel@345: 								else
moel@345: 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width, image.Height);
moel@345: 							} break;
moel@345: 						case ImageScaleMode.AlwaysScale:
moel@345: 							{
moel@345: 								float fx = (float)r.Width / (float)image.Width;
moel@345: 								float fy = (float)r.Height / (float)image.Height;
moel@345: 								if (Math.Min(fx, fy) < 1)
moel@345: 								{ //scale down
moel@345: 									float factor = Math.Min(fx, fy);
moel@345: 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width * factor, image.Height * factor);
moel@345: 								}
moel@345: 								else if (Math.Max(fx, fy) > 1)
moel@345: 								{
moel@345: 									float factor = Math.Max(fx, fy);
moel@345: 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width * factor, image.Height * factor);
moel@345: 								}
moel@345: 								else
moel@345: 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width, image.Height);
moel@345: 							} break;
moel@345: 						case ImageScaleMode.Clip:
moel@345: 						default: 
moel@345: 							context.Graphics.DrawImage(image, r.X, r.Y, image.Width, image.Height);
moel@345: 							break;
moel@345: 					}
moel@345: 				}
moel@345: 
moel@345: 			}
moel@345: 		}
moel@345: 
moel@345: 		protected virtual Image GetIcon(TreeNodeAdv node)
moel@345: 		{
moel@345: 			return GetValue(node) as Image;
moel@345: 		}
moel@345: 
moel@345:         private ImageScaleMode _scaleMode = ImageScaleMode.Clip;
moel@345:         [DefaultValue("Clip"), Category("Appearance")]
moel@345:         public ImageScaleMode ScaleMode
moel@345:         {
moel@345:             get { return _scaleMode; }
moel@345:             set { _scaleMode = value; }
moel@345:         }
moel@345: 
moel@345: 
moel@345: 	}
moel@345: }