Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / WPF

Equation Calculator with Graphing

Rate me:
Please Sign up or sign in to vote.
4.92/5 (69 votes)
25 Nov 2010CPOL9 min read 133.4K   4.2K   158  
Equation Calculator with Graphing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Collections;
using CommonTools;
using System.Windows.Controls.Primitives;

namespace CommonUtils
{
	/// <summary>
	/// Interaction logic for ColorListBox.xaml
	/// </summary>
	public partial class ColorListBox : UserControl
	{
		public static DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(ColorInfo), typeof(ColorListBox));
		public static DependencyProperty ColorListTypeProperty = DependencyProperty.Register("ColorListType", typeof(KnownColorCollection.eListType), typeof(ColorListBox), 
			new PropertyMetadata(KnownColorCollection.eListType.KnownColors, new PropertyChangedCallback(OnListTypeChanged)));
		static void OnListTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			ColorListBox lb = d as ColorListBox;
			lb.m_collection = new KnownColorCollection(lb.ColorListType);
			lb.Colors = lb.m_collection.Colors;
		}
		public static DependencyProperty ShowDetailProperty = DependencyProperty.Register("ShowDetailProperty", typeof(bool), typeof(ColorListBox));

		public ColorInfo SelectedItem
		{
			get { return (ColorInfo)GetValue(SelectedItemProperty); }
			set { SetValue(SelectedItemProperty, value); }
		}
		public KnownColorCollection.eListType ColorListType
		{
			get { return (KnownColorCollection.eListType)GetValue(ColorListTypeProperty); }
			set { SetValue(ColorListTypeProperty, value); }
		}
		public ObservableCollection<ColorInfo> Colors {get; private set;}
		public bool ShowDetail
		{
			get { return (bool)GetValue(ShowDetailProperty); }
			set { SetValue(ShowDetailProperty, value); }
		}
		KnownColorCollection m_collection;
		public ColorListBox()
		{
			InitializeComponent();
			m_listbox.SelectionChanged += new SelectionChangedEventHandler(m_listbox_SelectionChanged);
			ColorListType = KnownColorCollection.eListType.KnownColors;
			OnListTypeChanged(this, new DependencyPropertyChangedEventArgs());
			DataContext = this;
		}

		void m_listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			SelectedItem = m_listbox.SelectedItem as ColorInfo;
		}

		private void OnLbDoubleClick(object sender, MouseButtonEventArgs e)
		{

		}
	}
	
	[TemplatePart(Name="PART_ColorList", Type=typeof(ColorListBox))]
	public class ColorListPopup : Popup
	{
		public bool PopupResult {get; protected set;}
		public static DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", 
			typeof(ColorInfo), typeof(ColorListPopup), new PropertyMetadata(null, new PropertyChangedCallback(OnSelectedItemChanged)));

		static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			ColorListPopup popup = d as ColorListPopup;
			popup.PopupResult = true;
			popup.IsOpen = false;
		}
		public ColorInfo SelectedItem
		{
			get { return (ColorInfo)GetValue(SelectedItemProperty); }
			private set { SetValue(SelectedItemProperty, value); }
		}

		Color m_colorToSelect;
		public void SetColor(Color color)
		{
			m_colorToSelect = color;
		}

		ColorListBox m_colorList;
		public ColorListPopup()
		{
			Loaded += new RoutedEventHandler(OnLoaded);
		}
		
		void OnLoaded(object sender, RoutedEventArgs e)
		{
			LoadList();
		}
		void LoadList()
		{
			if (m_colorList != null)
				return;
			m_colorList = LogicalTreeUtils.FindLogicalChildByName<ColorListBox>(this, "PART_ColorList");
			if (m_colorList == null)
				return;
			Binding b = new Binding("SelectedItem");
			b.Source = m_colorList;
			SetBinding(SelectedItemProperty, b);
		}
		protected override void OnOpened(EventArgs e)
		{
			LoadList();
			PopupResult = false;
			base.OnOpened(e);

			foreach (ColorInfo info in m_colorList.Colors)
				info.IsSelected = info.Color.Color == m_colorToSelect;
		}
	}
	
	public class ColorInfo : DependencyObject
	{
		public static DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(ColorInfo));

		public bool IsSelected
		{
			get { return (bool )GetValue(IsSelectedProperty);}
			set { SetValue(IsSelectedProperty, value); }
		}
		public HSLColor HSLColor {get; private set;}
		public string Name { get; private set; }
		public SolidColorBrush Color { get; private set; }
		public string ColorValue
		{
			get { return Color.ToString(); }
		}
		public string HSLColorValue { get { return HSLColor.ToString(); }}
		public ColorInfo(PropertyInfo info)
		{
			Name = info.Name;
			Color = (SolidColorBrush)info.GetValue(typeof(SolidColorBrush), null);
			HSLColor = new HSLColor(Color.Color);
		}
	}
	public class KnownColorCollection
	{
		public ObservableCollection<ColorInfo> Colors {get; private set;}
		public enum eListType
		{
			KnownColors,
			SystemColors,
		}
		public KnownColorCollection(eListType type)
		{
			List<ColorInfo> list;
			if (type == eListType.KnownColors)
				list = GetKnownColors();
			else
				list = GetSystemColors();

			//list.Sort((a, b) => { return a.ColorValue.CompareTo(b.ColorValue); });
			list.Sort((a, b) => { return a.HSLColor.Hue.CompareTo(b.HSLColor.Hue); });
			//list.Sort((a, b) => { return a.HSLColor.Avg.CompareTo(b.HSLColor.Avg); });

			Colors = new ObservableCollection<ColorInfo>();
			foreach (ColorInfo info in list)
				Colors.Add(info);
		}
		public static List<ColorInfo> GetKnownColors()
		{
			List<ColorInfo> result = new List<ColorInfo>();
			PropertyInfo[] propinfos = typeof(Brushes).GetProperties(BindingFlags.Public | BindingFlags.Static);
			foreach (PropertyInfo info in propinfos)
				result.Add(new ColorInfo(info));
			return result;
		}
		public static List<ColorInfo> GetSystemColors()
		{
			List<ColorInfo> result = new List<ColorInfo>();
			PropertyInfo[] propinfos = typeof(SystemColors).GetProperties(BindingFlags.Public | BindingFlags.Static);
			foreach (PropertyInfo info in propinfos)
			{
				SolidColorBrush brush = info.GetValue(typeof(SolidColorBrush), null) as SolidColorBrush;
				if (brush == null)
					continue;
				result.Add(new ColorInfo(info));
			}
			return result;
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions