External/Aga.Controls/Tree/NodeControls/NodeIcon.cs
author sl
Thu, 01 Jan 2015 23:35:49 +0100
changeset 405 5715aefd2bcc
permissions -rw-r--r--
SharpDisplay: Migrating to new robust client scheme.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.Drawing;
     5 using System.Windows.Forms;
     6 using Aga.Controls.Properties;
     7 using System.ComponentModel;
     8 
     9 namespace Aga.Controls.Tree.NodeControls
    10 {
    11 	public class NodeIcon : BindableControl
    12 	{
    13 		public NodeIcon()
    14 		{
    15 			LeftMargin = 1;
    16 		}
    17 
    18 		public override Size MeasureSize(TreeNodeAdv node, DrawContext context)
    19 		{
    20 			Image image = GetIcon(node);
    21 			if (image != null)
    22 				return image.Size;
    23 			else
    24 				return Size.Empty;
    25 		}
    26 
    27 
    28 		public override void Draw(TreeNodeAdv node, DrawContext context)
    29 		{
    30 			Image image = GetIcon(node);
    31 			if (image != null)
    32 			{
    33 				Rectangle r = GetBounds(node, context);
    34 				if ( image.Width > 0 && image.Height > 0 )
    35 				{
    36 					switch (_scaleMode)
    37 					{
    38 						case ImageScaleMode.Fit:
    39 							context.Graphics.DrawImage(image, r);
    40 							break;
    41 						case ImageScaleMode.ScaleDown:
    42 							{
    43 								float factor = Math.Min((float)r.Width / (float)image.Width, (float)r.Height / (float)image.Height);
    44 								if (factor < 1)
    45 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width * factor, image.Height * factor);
    46 								else
    47 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width, image.Height);
    48 							} break;
    49 						case ImageScaleMode.ScaleUp:
    50 							{
    51 								float factor = Math.Max((float)r.Width / (float)image.Width, (float)r.Height / (float)image.Height);
    52 								if (factor > 1)
    53 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width * factor, image.Height * factor);
    54 								else
    55 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width, image.Height);
    56 							} break;
    57 						case ImageScaleMode.AlwaysScale:
    58 							{
    59 								float fx = (float)r.Width / (float)image.Width;
    60 								float fy = (float)r.Height / (float)image.Height;
    61 								if (Math.Min(fx, fy) < 1)
    62 								{ //scale down
    63 									float factor = Math.Min(fx, fy);
    64 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width * factor, image.Height * factor);
    65 								}
    66 								else if (Math.Max(fx, fy) > 1)
    67 								{
    68 									float factor = Math.Max(fx, fy);
    69 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width * factor, image.Height * factor);
    70 								}
    71 								else
    72 									context.Graphics.DrawImage(image, r.X, r.Y, image.Width, image.Height);
    73 							} break;
    74 						case ImageScaleMode.Clip:
    75 						default: 
    76 							context.Graphics.DrawImage(image, r.X, r.Y, image.Width, image.Height);
    77 							break;
    78 					}
    79 				}
    80 
    81 			}
    82 		}
    83 
    84 		protected virtual Image GetIcon(TreeNodeAdv node)
    85 		{
    86 			return GetValue(node) as Image;
    87 		}
    88 
    89         private ImageScaleMode _scaleMode = ImageScaleMode.Clip;
    90         [DefaultValue("Clip"), Category("Appearance")]
    91         public ImageScaleMode ScaleMode
    92         {
    93             get { return _scaleMode; }
    94             set { _scaleMode = value; }
    95         }
    96 
    97 
    98 	}
    99 }