///
/// This file is part of ILNumerics Community Edition.
///
/// ILNumerics Community Edition - high performance computing for applications.
/// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
///
/// ILNumerics Community Edition is free software: you can redistribute it and/or modify
/// it under the terms of the GNU General Public License version 3 as published by
/// the Free Software Foundation.
///
/// ILNumerics Community Edition is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with ILNumerics Community Edition. See the file License.txt in the root
/// of your distribution package. If not, see .
///
/// In addition this software uses the following components and/or licenses:
///
/// =================================================================================
/// The Open Toolkit Library License
///
/// Copyright (c) 2006 - 2009 the Open Toolkit library.
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights to
/// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
/// the Software, and to permit persons to whom the Software is furnished to do
/// so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all
/// copies or substantial portions of the Software.
///
/// =================================================================================
///
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using ILNumerics.Drawing;
using ILNumerics.Exceptions;
using ILNumerics.Drawing.Graphs;
using ILNumerics.Drawing.Interfaces;
using ILNumerics.Drawing.Labeling;
using ILNumerics.Drawing.Platform.OpenGL;
namespace ILNumerics.Drawing.Misc {
public abstract class ILLegend {
#region events
public event EventHandler Changed;
protected void OnChanged() {
if (Changed != null) {
Changed(this,null);
}
}
void m_border_Changed(object sender, EventArgs e) {
OnChanged();
}
#endregion
#region attributes
protected ILPanel m_panel;
protected Size m_size;
protected PointF m_location;
protected Color m_bgColor;
protected ILLineProperties m_border;
protected float m_opacity;
protected Padding m_padding;
protected bool m_visible;
#endregion
#region properties
///
/// get / set visibility for legend
///
public bool Visible {
get {
return m_visible;
}
set {
m_visible = value;
OnChanged();
}
}
///
/// Get/ set opacity for filled area, values: 0...1.0
///
public float Opacity {
get {
return m_opacity;
}
set {
m_opacity = value;
OnChanged();
}
}
///
/// Get / set background color
///
public Color BackgroundColor {
get {
return m_bgColor;
}
set {
m_bgColor = value;
OnChanged();
}
}
///
/// get/ set location of legend box (upper/left corner, fraction of ClientSize [(0,0)...(1f,1F)]), empty for auto mode
///
public PointF Location {
get {
return m_location;
}
set {
m_location = value;
OnChanged();
}
}
///
/// get/ set size of legend box, empty for auto mode
///
public Size Size {
get { return m_size; }
set {
m_size = value;
OnChanged();
}
}
///
/// set properties of legend's border
///
public ILLineProperties Border {
get {
return m_border;
}
}
#endregion
#region constructor
///
/// construct new legend object
///
internal ILLegend (ILPanel panel) {
m_panel = panel;
m_size = Size.Empty;
m_border = new ILLineProperties();
m_border.Antialiasing = false;
m_border.Color = Color.LightGray;
m_border.Style = LineStyle.Solid;
m_border.Width = 2;
m_border.Changed += new EventHandler(m_border_Changed);
m_location = Point.Empty;
m_bgColor = Color.FromArgb(250,250,250);
m_opacity = 0.9f;
m_padding = new Padding(5);
m_visible = false;
}
///
/// Create new instance of ILLegend, depending on graphics device type
///
/// panel hosting this legend
/// newly created ILLegend object
public static ILLegend Create(ILPanel panel) {
if (panel is ILOGLPanel) {
return new ILOGLLegend(panel);
} else
throw new NotImplementedException("Currently only legends for OpenGL are supported");
}
#endregion
#region public interface
///
/// Draw the legend onto a predefined bitmap or into GL context
///
/// render properties
/// rectangle area defines region to draw legend contents into.
/// This function does only render to a predefined bitmap, which must be given in p. Rendering to
/// (dvice dependent) graphic contexts is done in derived implementations.
public virtual void Draw(ILRenderProperties p, Rectangle area) {
if (m_panel == null || m_panel.Graphs == null) return;
if (p.Graphics == null) {
throw new ILArgumentException("ILLegend:Draw: unexpected parameter (bitmap) is null. This should be handled in derived classes!");
}
// draw bg + border
PointF p1,p2;
float offsX = Math.Max(m_border.Width / 2.0f,1.0f), offsY;
p1 = new PointF(offsX,offsX);
p2 = new PointF(m_size.Width-offsX,offsX);
Pen pen = new Pen(m_border.Color,m_border.Width);
// todo: implement dash styles
p.Graphics.Clear(m_bgColor);
p.Graphics.DrawLine(pen,p1,p2);
p1.X = p2.X; p1.Y = m_size.Height - offsX;
p.Graphics.DrawLine(pen,p2,p1);
p2.X = offsX; p2.Y = p1.Y;
p.Graphics.DrawLine(pen,p1,p2);
p1.X = offsX; p1.Y = offsX;
p.Graphics.DrawLine(pen,p2,p1);
List providers = new List(10);
foreach (ILGraph graph in m_panel.Graphs) {
if (graph is IILLegendRenderer) {
providers.Add(graph as IILLegendRenderer);
}
}
// compute final example sizes to fit into my size
offsX = m_size.Width * 0.1f; offsY = m_size.Height * 0.1f;
area.Height = (int)(m_size.Height - 2 * offsY) / providers.Count;
area.Width = (int)(m_size.Width - 2 * offsX) / providers.Count;
area.X = (int)offsX;
for (int i = 0; i < providers.Count; i++) {
area.Y = (int)(offsY + i * area.Height);
// todo: not implemented!
providers[i].DrawToLegend(p,area,area);
}
}
#endregion
#region helper functions
protected List getRenderers(ref Size labelSize) {
List ret = new List();
foreach(ILGraph graph in m_panel.Graphs) {
IILLegendRenderer rend = graph as IILLegendRenderer;
if (rend != null) {
ret.Add(rend);
if (rend.LabelSize.Width > labelSize.Width)
labelSize.Width = rend.LabelSize.Width;
labelSize.Height += rend.LabelSize.Height;
}
}
return ret;
}
#endregion
}
}