Tuesday, August 2, 2011

Move User Control arround inside Canvas



namespace Reporting.Wpf.UserControls
{
public class BaseGroupCharts : UserControl
{
protected List RootFunds;
protected int DrillDownGroupId;
protected ApexDataContext _context = ApexDal.Instance.CreateApexDataContext();
protected bool AllowMove;

protected List Unlayedouts = new List();
protected double f = 0.2;

#region buiding Layed-outs/unlayed-outs

public virtual void BuildUnlayedouts(bool UseActual) { }

protected void ShowAtRightUppoerCornerUnlayedouts(bool UseActual, int LeftOffSet, Canvas cv)
{
double W = UseActual ? ActualWidth : Width;
double H = UseActual ? ActualHeight : Height;
int n = Unlayedouts.Count;
int col = (int)Math.Round(Math.Sqrt(n * W / H));
int row = (int)Math.Round(Math.Sqrt(n * H / W));

for (int i = 0; i < Unlayedouts.Count; i++)
{
Resizer r = Unlayedouts[i];
Canvas.SetLeft(r, (1 - f) * W + r.Width * (i % col) + LeftOffSet);
Canvas.SetTop(r, r.Height * (int)(i / col));
cv.Children.Add(r);
}
}

protected void ClearUnlayedouts(Canvas cv)
{
List toRemove = new List();
foreach (var q in cv.Children)
{
Resizer r = q as Resizer;
if (r != null)
{
double left = (double)r.GetValue(Canvas.LeftProperty);
double top = (double)r.GetValue(Canvas.TopProperty);
if (left >= 0.8 * ActualWidth && top <= 0.2 * ActualHeight) toRemove.Add(r);
}
}
foreach (Resizer r in toRemove) cv.Children.Remove(r);
}

#endregion

#region lock/unlock

protected bool Locked = true;
protected void ToggleLockUnlock(Image imgLock, Canvas cv)
{
imgLock.Focus();
if (Locked)
{
imgLock.Source = new BitmapImage(new Uri("pack://application:,,,/GMO.AA.Reporting.Wpf;component/Images/Unlock.png"));
Locked = false;
foreach (UIElement c in cv.Children)
{
Resizer r = c as Resizer;
if (r != null) r.IsGripVisible = true;
}
ClearUnlayedouts(cv);
Unlayedouts.Clear();
BuildUnlayedouts(true);
ShowAtRightUppoerCornerUnlayedouts(true, 0, cv);
}
else
{
imgLock.Source = new BitmapImage(new Uri("pack://application:,,,/GMO.AA.Reporting.Wpf;component/Images/Lock.png"));
Locked = true;

ClearUnlayedouts(cv);
Unlayedouts.Clear();
BuildUnlayedouts(true);
ShowAtRightUppoerCornerUnlayedouts(true, 1000, cv);

foreach (UIElement c in cv.Children)
{
Resizer r = c as Resizer;
if (r != null) r.IsGripVisible = false;
}


}
}

#endregion

#region Drag Move

private Point m_StartPoint;
private double m_OriginalLeft;
private double m_OriginalTop;
private Boolean m_IsDown;
private UIElement m_OriginalElement;
private Boolean m_IsDragging;


protected void UserControl_KeyDown(object sender, KeyEventArgs e)
{

if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl) AllowMove = true;

}

protected void UserControl_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl) AllowMove = false;

}

protected void MyCanvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Canvas cv = sender as Canvas;
if (cv == e.Source || !AllowMove) return;
m_IsDown = true;
m_StartPoint = e.GetPosition(sender as Canvas);
m_OriginalElement = e.Source as UIElement;
cv.CaptureMouse();
e.Handled = true;
}

protected void MyCanvas_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (m_IsDown)
{
if (!m_IsDragging && Math.Abs(e.GetPosition(sender as Canvas).X - m_StartPoint.X) > SystemParameters.MinimumHorizontalDragDistance && Math.Abs(e.GetPosition(sender as Canvas).Y - m_StartPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
{
DragStarted();
}
if (m_IsDragging)
{
DragMoved(sender as Canvas);
}
}
}


protected void MyCanvas_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (m_IsDown)
{
DragFinished(false);
e.Handled = true;
}
}

protected void DragFinished(bool p)
{
System.Windows.Input.Mouse.Capture(null);
m_IsDragging = false;
m_IsDown = false;
}
protected void DragMoved(Canvas cv)
{
Point currentPosition = System.Windows.Input.Mouse.GetPosition(cv);
double elementLeft = (currentPosition.X - m_StartPoint.X) + m_OriginalLeft;
double elementTop = (currentPosition.Y - m_StartPoint.Y) + m_OriginalTop;
Canvas.SetLeft(m_OriginalElement, elementLeft);
Canvas.SetTop(m_OriginalElement, elementTop);
}

protected void DragStarted()
{
m_IsDragging = true;
m_OriginalLeft = Canvas.GetLeft(m_OriginalElement);
m_OriginalTop = Canvas.GetTop(m_OriginalElement);

}

#endregion

#region Resize

protected void MyCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (e.PreviousSize.Height == 0 || e.PreviousSize.Width == 0) return;
double rH = e.NewSize.Height / e.PreviousSize.Height;
double rW = e.NewSize.Width / e.PreviousSize.Width;
Canvas cv = sender as Canvas;

foreach (UIElement c in cv.Children)
{
Resizer r = c as Resizer;
if (r != null)
{
r.Width = r.Width * rW;
r.Height = r.Height * rH;
r.SetValue(Canvas.LeftProperty, (double)r.GetValue(Canvas.LeftProperty) * rW);
r.SetValue(Canvas.TopProperty, (double)r.GetValue(Canvas.TopProperty) * rH);
}
}
}

#endregion

}
}




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 GMO.AA.Reporting.Wpf.Common;
using GMO.AA.Reporting.Domain.Objects;
using System.DirectoryServices.AccountManagement;
using System.IO;
using GMO.OpenSource.Windows.Controls;
using GMO.AA.Reporting.UI.Core;


namespace Reporting.Wpf.UserControls
{
///
/// Interaction logic for GroupCharts.xaml
///

public partial class GroupChartsCanvas : BaseGroupCharts
{
public event DrillDownRequestedEventHandler DrillDownRequested;

public GroupChartsCanvas(List list,int drillDownGroupId)
{
InitializeComponent();
RootFunds = list;
DrillDownGroupId = drillDownGroupId;
_context = ApexDal.Instance.CreateApexDataContext();
GetCustomGroupData();
}


#region CG data

IEnumerable cGroups;
void GetCustomGroupData()
{
string userName = UserPrincipal.Current.DisplayName;

cGroups = (from x in _context.CustomGroups
where x.RelatedGroupId == DrillDownGroupId && !x.Deleted &&
(x.Public == true || x.CreatedByUserName == userName)
select x).OrderBy(y => y.CustomGroupName);
}

#endregion

#region override Un-Layed-outs in upper right corner


public override void BuildUnlayedouts(bool UseActual)
{
double W = UseActual ? ActualWidth : Width;
double H = UseActual ? ActualHeight : Height;
int n = cGroups.Count()+1;

double LCw = f * W, LCh = f * H;
int col = (int)Math.Round(Math.Sqrt(n * W / H));
int row = (int)Math.Round(Math.Sqrt(n * H / W));
foreach (CustomGroup cg in cGroups)
{
Resizer r = new Resizer() { Height = LCh / row, Width = LCw / col, ToolTip = cg.CustomGroupName };
CustomGroupExposureChart cgec = new CustomGroupExposureChart(cg, ChartVendors.Infragistic);
cgec.AllowDataPointDrillDown = true;
cgec.RelatedGroupName = EventEngine.Instance.SelectedGroup.GroupName;
cgec.DrillDownRequested += new DrillDownRequestedEventHandler(cgec_DrillDownRequested);
r.Content = cgec;
Unlayedouts.Add(r);
}
Resizer r1 = new Resizer() { Height = LCh / row, Width = LCw / col, ToolTip = "Currency Exposure"};
r1.Content = new CurrencyExposureChart();
Unlayedouts.Add(r1);

Resizer r2 = new Resizer() { Height = LCh / row, Width = LCw / col, ToolTip = "CountryExposure" };
r2.Content = new CountryExposureChart();
Unlayedouts.Add(r2);

Resizer r3 = new Resizer() { Height = LCh / row, Width = LCw / col, ToolTip = "Sector Exposure" };
r3.Content = new SectorExposureChart();
Unlayedouts.Add(r3);
}

void cgec_DrillDownRequested(object sender, DrillDownRequestedEventArgs e)
{
if (DrillDownRequested != null)
DrillDownRequested(sender,new DrillDownRequestedEventArgs() { GroupId = DrillDownGroupId, RelatedGroupId = e.RelatedGroupId, GroupName=e.GroupName, CustomGroup=e.CustomGroup, RelatedGroupName=e.RelatedGroupName });
}

#endregion

#region Control Events

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
BuildUnlayedouts(true);
ShowAtRightUppoerCornerUnlayedouts(true, 1000, MyCanvas);
foreach (UIElement c in MyCanvas.Children)
{
Resizer r = c as Resizer;
if (r != null) r.IsGripVisible = false;
}
Focus();
}

private void imgEdit_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ToggleLockUnlock(imgLock, MyCanvas);

}

#endregion

}
}


Resizer comes from Kent Boogaart

http://kentb.blogspot.com/2007/04/resizer-wpf-control.html

No comments: