Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix default drag adorner with right to left option #78

Merged
merged 4 commits into from
Oct 14, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Examples/DefaultsExample/Window(NET35).xaml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@

<ListBox Grid.Row="1"
SelectionMode="Extended"
dd:DragDrop.UseDefaultDragAdorner="True"
dd:DragDrop.DefaultDragAdornerOpacity="0.5"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True">
<ListBox.ItemsPanel>
Expand All @@ -252,6 +254,7 @@

<ListBox Grid.Row="3"
SelectionMode="Extended"
dd:DragDrop.UseDefaultDragAdorner="True"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True">
<ListBox.ItemsPanel>
Expand Down
3 changes: 3 additions & 0 deletions Examples/DefaultsExample/Window(NET4).xaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@

<ListBox Grid.Row="1"
SelectionMode="Extended"
dd:DragDrop.UseDefaultDragAdorner="True"
dd:DragDrop.DefaultDragAdornerOpacity="0.5"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True">
<ListBox.ItemsPanel>
Expand All @@ -251,6 +253,7 @@

<ListBox Grid.Row="3"
SelectionMode="Extended"
dd:DragDrop.UseDefaultDragAdorner="True"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True">
<ListBox.ItemsPanel>
Expand Down
29 changes: 25 additions & 4 deletions GongSolutions.Wpf.DragDrop/DragDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ public static void SetUseDefaultDragAdorner(UIElement target, bool value)
target.SetValue(UseDefaultDragAdornerProperty, value);
}

public static readonly DependencyProperty DefaultDragAdornerOpacityProperty =
DependencyProperty.RegisterAttached("DefaultDragAdornerOpacity", typeof(double), typeof(DragDrop), new PropertyMetadata(0.8));

public static double GetDefaultDragAdornerOpacity(UIElement target)
{
return (double)target.GetValue(DefaultDragAdornerOpacityProperty);
}

public static void SetDefaultDragAdornerOpacity(UIElement target, double value)
{
target.SetValue(DefaultDragAdornerOpacityProperty, value);
}

public static readonly DependencyProperty UseDefaultEffectDataTemplateProperty =
DependencyProperty.RegisterAttached("UseDefaultEffectDataTemplate", typeof(bool), typeof(DragDrop), new PropertyMetadata(false));

Expand Down Expand Up @@ -346,8 +359,10 @@ private static void CreateDragAdorner()

var factory = new FrameworkElementFactory(typeof(Image));

var bs = CaptureScreen(m_DragInfo.VisualSourceItem);
var bs = CaptureScreen(m_DragInfo.VisualSourceItem, m_DragInfo.VisualSourceFlowDirection);
factory.SetValue(Image.SourceProperty, bs);
factory.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
factory.SetValue(RenderOptions.BitmapScalingModeProperty, BitmapScalingMode.HighQuality);
if (m_DragInfo.VisualSourceItem is FrameworkElement) {
factory.SetValue(FrameworkElement.WidthProperty, ((FrameworkElement)m_DragInfo.VisualSourceItem).ActualWidth);
factory.SetValue(FrameworkElement.HeightProperty, ((FrameworkElement)m_DragInfo.VisualSourceItem).ActualHeight);
Expand Down Expand Up @@ -380,7 +395,7 @@ private static void CreateDragAdorner()

if (adornment != null) {
if (useDefaultDragAdorner) {
adornment.Opacity = 0.5;
adornment.Opacity = GetDefaultDragAdornerOpacity(m_DragInfo.VisualSource);
}

var parentWindow = m_DragInfo.VisualSource.GetVisualAncestor<Window>();
Expand All @@ -403,7 +418,7 @@ private static void CreateDragAdorner()

// Helper to generate the image - I grabbed this off Google
// somewhere. -- Chris Bordeman cbordeman@gmail.com
private static BitmapSource CaptureScreen(Visual target, double dpiX = 96.0, double dpiY = 96.0)
private static BitmapSource CaptureScreen(Visual target, FlowDirection flowDirection, double dpiX = 96.0, double dpiY = 96.0)
{
if (target == null) {
return null;
Expand All @@ -420,11 +435,17 @@ private static BitmapSource CaptureScreen(Visual target, double dpiX = 96.0, dou
var dv = new DrawingVisual();
using (var ctx = dv.RenderOpen()) {
var vb = new VisualBrush(target);
if (flowDirection == FlowDirection.RightToLeft) {
var transformGroup = new TransformGroup();
transformGroup.Children.Add(new ScaleTransform(-1, 1));
transformGroup.Children.Add(new TranslateTransform(bounds.Size.Width - 1, 0));
ctx.PushTransform(transformGroup);
}
ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
}

rtb.Render(dv);

return rtb;
}

Expand Down
7 changes: 7 additions & 0 deletions GongSolutions.Wpf.DragDrop/DragInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public DragInfo(object sender, MouseButtonEventArgs e)
if (sender is ItemsControl) {
var itemsControl = (ItemsControl)sender;

this.VisualSourceFlowDirection = itemsControl.GetItemsPanelFlowDirection();

var sourceItem = e.OriginalSource as UIElement; // If we can't cast object as a UIElement it might be a FrameworkContentElement, if so try and use its parent.
if (sourceItem == null && e.OriginalSource is FrameworkContentElement) {
sourceItem = ((FrameworkContentElement)e.OriginalSource).Parent as UIElement;
Expand Down Expand Up @@ -164,6 +166,11 @@ public DragInfo(object sender, MouseButtonEventArgs e)
/// </remarks>
public UIElement VisualSourceItem { get; private set; }

/// <summary>
/// Gets the FlowDirection of the current drag source.
/// </summary>
public FlowDirection VisualSourceFlowDirection { get; private set; }

/// <summary>
/// Gets the <see cref="IDataObject"/> which is used by the drag and drop operation. Set it to
/// a custom instance if custom drag and drop behavior is needed.
Expand Down
5 changes: 5 additions & 0 deletions GongSolutions.Wpf.DragDrop/IDragInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public interface IDragInfo
/// </remarks>
UIElement VisualSourceItem { get; }

/// <summary>
/// Gets the FlowDirection of the current drag source.
/// </summary>
FlowDirection VisualSourceFlowDirection { get; }

/// <summary>
/// Gets the <see cref="IDataObject"/> which is used by the drag and drop operation. Set it to
/// a custom instance if custom drag and drop behavior is needed.
Expand Down