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