using PDF_Master.CustomControl;
using PDF_Master.Helper;
using PDF_Master.Model.Dialog.ToolsDialogs;
using PDF_Master.ViewModels.Dialog.ToolsDialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
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;
namespace PDF_Master.Views.Dialog.ToolsDialogs
{
///
/// MergeDialog.xaml 的交互逻辑
///
public partial class MergeDialog : UserControl
{
///
/// 当前显示了线段的Item
///
private ListViewItem listViewItem = null;
private bool IsFile = false;
public MergeDialog()
{
InitializeComponent();
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
(DataContext as MergeDialogViewModel).DeleteItem((sender as Button).DataContext as MergeObject);
}
private void MergeView_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var pos = e.GetPosition(MergeView);
HitTestResult result = VisualTreeHelper.HitTest(MergeView, pos);
if (result == null)
{
return;
}
//点击删除按钮
Button Button = CommonHelper.FindVisualParent(result.VisualHit);
if (Button != null)
{
return;
}
//点击下拉框
WritableComboBox writableComboBox = CommonHelper.FindVisualParent(result.VisualHit);
if (writableComboBox != null)
{
return;
}
var treeViewItem = CommonHelper.FindVisualParent(result.VisualHit);
if (treeViewItem == null)
{
return;
}
DataObject dataObj = new DataObject(treeViewItem);
DragDrop.DoDragDrop(MergeView, dataObj, DragDropEffects.Move);
return;
}
}
private void MergeView_Drop(object sender, DragEventArgs e)
{
if (IsFile)
{
DragDropHelper.Drop(this, e);
string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
(DataContext as MergeDialogViewModel).AddFiles(file);
}
ListViewItem souredata = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
if (souredata == null)
{
return;
}
MergeObject soureNode = souredata.DataContext as MergeObject;
Point pos = e.GetPosition(MergeView);
HitTestResult result = VisualTreeHelper.HitTest(MergeView, pos);
if (result == null)
{
HiddenaAllLine();
return;
}
ListViewItem targetitem = CommonHelper.FindVisualParent(result.VisualHit);
if (targetitem == null)
{
HiddenaAllLine();
return;
}
MergeObject targetNode = targetitem.DataContext as MergeObject;
if (soureNode.Equals(targetNode))
{
HiddenaAllLine();
return;
}
(DataContext as MergeDialogViewModel).MoveMerge(targetNode, soureNode);
HiddenaAllLine();
}
private void MergeView_DragOver(object sender, DragEventArgs e)
{
if (IsFile)
{
DragDropHelper.DragOver(this, e);
}
ListViewItem sourceitem = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
if (sourceitem == null)
{
return;
}
Point pos = e.GetPosition(MergeView);
HitTestResult result = VisualTreeHelper.HitTest(MergeView, pos);
if (result != null)
{
ListViewItem treeviewitem = CommonHelper.FindVisualParent(result.VisualHit);
if (treeviewitem != null)
{
if (listViewItem == null)
{
listViewItem = treeviewitem;
}
else if (!listViewItem.Equals(treeviewitem))
{
(listViewItem.DataContext as MergeObject).IsBackwards = false;
(listViewItem.DataContext as MergeObject).IsForward = false;
listViewItem = treeviewitem;
}
else
{
(listViewItem.DataContext as MergeObject).IsBackwards = false;
(listViewItem.DataContext as MergeObject).IsForward = false;
}
if (listViewItem.Equals(sourceitem))
{
return;
}
//鼠标位于上半部分显示实线,下半部分显示虚线
Point p = treeviewitem.TranslatePoint(new Point(0, 0), MergeView);
if (pos.Y < p.Y + 16)
{
(treeviewitem.DataContext as MergeObject).IsBackwards = true;
}
else
{
(treeviewitem.DataContext as MergeObject).IsForward = true;
}
}
}
}
private void MergeView_DragLeave(object sender, DragEventArgs e)
{
if (IsFile)
{
DragDropHelper.DragLeave();
}
HiddenaAllLine();
}
///
/// 清理线条显示,以及相关缓存对象
///
private void HiddenaAllLine()
{
if (listViewItem != null)
{
(listViewItem.DataContext as MergeObject).IsBackwards = false;
(listViewItem.DataContext as MergeObject).IsForward = false;
listViewItem = null;
}
}
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9.]+");
e.Handled = regex.IsMatch(e.Text);
}
private void TextBoxEx_LostFocus(object sender, RoutedEventArgs e)
{
try
{
double Tag = Double.Parse((sender as TextBox).Text);
if (Tag <= 0)
{
(sender as TextBox).Text = "";
}
}
catch (Exception)
{
(sender as TextBox).Text = "";
}
}
private void TextBoxEx_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
BtnMerge.Focus();
e.Handled = true;
}
}
private void MergeView_DragEnter(object sender, DragEventArgs e)
{
IsFile = false;
var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
if (file == null)//为null 表示内部拖动 触发的
{
return;
}
foreach (string f in file)
{
System.IO.FileInfo info = new System.IO.FileInfo(f);
//只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
if (System.IO.Path.GetExtension(f).ToLower() == ".pdf" && info.Length > 0)
{
DragDropHelper.DragEnter(this, e);
IsFile = true;
}
//图片格式
else if (Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(f).ToLower()) && info.Length > 0)
{
DragDropHelper.DragEnter(this, e);
IsFile = true;
}
}
}
private void Grid_Drop(object sender, DragEventArgs e)
{
if (IsFile)
{
DragDropHelper.Drop(this, e);
string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
(DataContext as MergeDialogViewModel).AddFiles(file);
}
}
private void Grid_DragEnter(object sender, DragEventArgs e)
{
IsFile = false;
var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
if (file == null)//为null 表示内部拖动 触发的
{
return;
}
foreach (string f in file)
{
System.IO.FileInfo info = new System.IO.FileInfo(f);
//只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
if (System.IO.Path.GetExtension(f).ToLower() == ".pdf" && info.Length > 0)
{
DragDropHelper.DragEnter(this, e);
IsFile = true;
}
//图片格式
else if (Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(f).ToLower()) && info.Length > 0)
{
DragDropHelper.DragEnter(this, e);
IsFile = true;
}
}
}
private void Grid_DragOver(object sender, DragEventArgs e)
{
if (IsFile)
{
DragDropHelper.DragOver(this, e);
}
}
private void Grid_DragLeave(object sender, DragEventArgs e)
{
if (IsFile)
{
DragDropHelper.DragLeave();
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
NoFileText.Text = App.MainPageLoader.GetString("Merge_Hint");
TitleFileName.Text = App.MainPageLoader.GetString("Merge_TitleFileName");
TitlePageRange.Text = App.MainPageLoader.GetString("Merge_TitlePageRange");
TitleSize.Text = App.MainPageLoader.GetString("Merge_TitleSize");
AddComboBoxItemText.Text = App.MainPageLoader.GetString("Merge_AddFiles");
AddFolderComboBoxItemText.Text = App.MainPageLoader.GetString("Merge_AddFolder");
AddOpenFileComboBoxItem.Text = App.MainPageLoader.GetString("Merge_AddOpenFile");
BtnMerge.Content = App.MainPageLoader.GetString("Merge_Yes");
BtnCancel.Content = App.MainPageLoader.GetString("Merge_No");
}
}
}