PageRenderTime 35ms CodeModel.GetById 18ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Charts/ViewportUIContainer.cs

#
C# | 101 lines | 79 code | 16 blank | 6 comment | 12 complexity | c568951710ebb95ed07fb5c2fba58eed MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.Windows;
  6using System.Windows.Markup;
  7using Microsoft.Research.DynamicDataDisplay.Common;
  8using System.Windows.Controls;
  9
 10namespace Microsoft.Research.DynamicDataDisplay.Charts
 11{
 12	[ContentProperty("Content")]
 13	public class ViewportUIContainer : DependencyObject, IPlotterElement
 14	{
 15		public ViewportUIContainer() { }
 16
 17		public ViewportUIContainer(FrameworkElement content)
 18		{
 19			this.Content = content;
 20		}
 21
 22		private FrameworkElement content;
 23		[NotNull]
 24		public FrameworkElement Content
 25		{
 26			get { return content; }
 27			set { content = value; }
 28		}
 29
 30		#region IPlotterElement Members
 31
 32		void IPlotterElement.OnPlotterAttached(Plotter plotter)
 33		{
 34			this.plotter = plotter;
 35
 36			if (Content == null) return;
 37
 38			var plotterPanel = GetPlotterPanel(Content);
 39			//Plotter.SetPlotter(Content, plotter);
 40
 41			if (plotterPanel == PlotterPanel.MainCanvas)
 42			{
 43				// if all four Canvas.{Left|Right|Top|Bottom} properties are not set,
 44				// and as we are adding by default content to MainCanvas, 
 45				// and since I like more when buttons are by default in right down corner - 
 46				// set bottom and right to 10.
 47				var left = Canvas.GetLeft(content);
 48				var top = Canvas.GetTop(content);
 49				var bottom = Canvas.GetBottom(content);
 50				var right = Canvas.GetRight(content);
 51
 52				if (left.IsNaN() && right.IsNaN() && bottom.IsNaN() && top.IsNaN())
 53				{
 54					Canvas.SetBottom(content, 10.0);
 55					Canvas.SetRight(content, 10.0);
 56				}
 57				plotter.MainCanvas.Children.Add(Content);
 58			}
 59		}
 60
 61		void IPlotterElement.OnPlotterDetaching(Plotter plotter)
 62		{
 63			if (Content != null)
 64			{
 65				var plotterPanel = GetPlotterPanel(Content);
 66				//Plotter.SetPlotter(Content, null);
 67				if (plotterPanel == PlotterPanel.MainCanvas)
 68				{
 69					plotter.MainCanvas.Children.Remove(Content);
 70				}
 71			}
 72
 73			this.plotter = null;
 74		}
 75
 76		private Plotter plotter;
 77		Plotter IPlotterElement.Plotter
 78		{
 79			get { return plotter; }
 80		}
 81
 82		#endregion
 83
 84		[AttachedPropertyBrowsableForChildren]
 85		public static PlotterPanel GetPlotterPanel(DependencyObject obj)
 86		{
 87			return (PlotterPanel)obj.GetValue(PlotterPanelProperty);
 88		}
 89
 90		public static void SetPlotterPanel(DependencyObject obj, PlotterPanel value)
 91		{
 92			obj.SetValue(PlotterPanelProperty, value);
 93		}
 94
 95		public static readonly DependencyProperty PlotterPanelProperty = DependencyProperty.RegisterAttached(
 96		  "PlotterPanel",
 97		  typeof(PlotterPanel),
 98		  typeof(ViewportUIContainer),
 99		  new FrameworkPropertyMetadata(PlotterPanel.MainCanvas));
100	}
101}