using ComPDFKit.PDFDocument;
using PDF_Master.CustomControl;
using PDF_Master.EventAggregators;
using PDF_Master.Helper;
using PDF_Master.Model.BOTA;
using PDF_Master.ViewModels.BOTA;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
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 static Dropbox.Api.Files.WriteMode;
using MenuItem = System.Windows.Controls.MenuItem;
namespace PDF_Master.Views.BOTA
{
///
/// OutLineControl.xaml 的交互逻辑
///
public partial class OutLineControl : UserControl
{
///
/// 当前显示了线段的Item
///
private TreeViewItem Treeviewitem = null;
///
/// 需要跳转页面的Item
///
private TreeViewItem GotoPageItem = null;
///
/// 需要重命名的Item的DataContext
///
public OutlineNode ReNameOutlineNode = null;
///
/// 正在拖拽
///
private bool onDrop = false;
public bool IsReName = false;
private string unicode = "";
///
/// 判断是否拖拽到了新窗口
///
///
private bool IsInNowWindow()
{
if (App.DropStartWindow != null)
{
Window frm = Window.GetWindow(OutlineView);
if (frm != App.DropStartWindow)
{
return true;
}
return false;
}
return false;
}
public OutLineControl()
{
InitializeComponent();
unicode = App.mainWindowViewModel.SelectedItem.Unicode;
}
public OutLineControl(IEventAggregator eventAggregator) : this()
{
eventAggregator.GetEvent().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
}
private void CleanSelectAll(CleanSelectAllArgs obj)
{
OutlineNode outlineNode = OutlineView.SelectedItem as OutlineNode;
if (outlineNode != null)
{
outlineNode.IsSelected = false;
}
}
private void BtnMore_Click(object sender, RoutedEventArgs e)
{
MenuMore.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
MenuMore.PlacementTarget = BtnMore;
MenuMore.IsOpen = true;
}
private void OutlineView_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (IsInNowWindow())
{
return;
}
if (e.LeftButton == MouseButtonState.Pressed && !IsReName)
{
var pos = e.GetPosition(OutlineView);
HitTestResult result = VisualTreeHelper.HitTest(OutlineView, pos);
if (result == null)
{
return;
}
var treeViewItem = CommonHelper.FindVisualParent(result.VisualHit);
if (treeViewItem == null)
{
return;
}
DataObject dataObj = new DataObject(treeViewItem);
DragDrop.DoDragDrop(OutlineView, dataObj, DragDropEffects.Move);
return;
}
}
///
/// 拖拽判断区域以及显示什么线
///
///
///
private void Grid_DragOver(object sender, DragEventArgs e)
{
if (IsInNowWindow())
{
e.Effects = DragDropEffects.None;
e.Handled = true;
return;
}
(DataContext as OutLineControlViewModel).IsOnDrop = true;
TreeViewItem sourceitem = e.Data.GetData(typeof(TreeViewItem)) as TreeViewItem;
if (sourceitem == null)
{
return;
}
Point pos = e.GetPosition(OutlineView);
HitTestResult result = VisualTreeHelper.HitTest(OutlineView, pos);
if (result != null)
{
TreeViewItem treeviewitem = CommonHelper.FindVisualParent(result.VisualHit);
if (treeviewitem != null)
{
//控制最顶端的线
if (pos.Y <= 9)
{
if (Treeviewitem == null)
{
Treeviewitem = treeviewitem;
}
(DataContext as OutLineControlViewModel).IsInsertHead = true;
(Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
(Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
}
else
{
(DataContext as OutLineControlViewModel).IsInsertHead = false;
if (Treeviewitem == null)
{
Treeviewitem = treeviewitem;
}
else if (!Treeviewitem.Equals(treeviewitem))
{
(Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
(Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
Treeviewitem = treeviewitem;
}
else
{
(Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
(Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
}
if (Treeviewitem.Equals(sourceitem))
{
return;
}
//鼠标位于上半部分显示实线,下半部分显示虚线
Point p = treeviewitem.TranslatePoint(new Point(0, 0), OutlineView);
if (pos.Y < p.Y + 16)
{
(treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = true;
}
else
{
(treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = true;
}
}
}
}
}
private void Grid_DragLeave(object sender, DragEventArgs e)
{
HiddenaAllLine();
}
private void OutlineView_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
TextBlock text = e.OriginalSource as TextBlock;
if (text == null || text.Name == "PageIndex")
{
return;
}
TreeView tree = sender as TreeView;
if (tree == null)
{
return;
}
OutlineNode viewitem = tree.SelectedItem as OutlineNode;
if (viewitem == null)
{
return;
}
ReNameOutlineNode = viewitem;
ReNameOutlineNode.IsReName = Visibility.Collapsed;
e.Handled = true;
}
private void OutlineView_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
(DataContext as OutLineControlViewModel).IsInsertHead = false;
if (Treeviewitem != null)
{
(Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
(Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
Treeviewitem = null;
}
}
private void OutlineView_Drop(object sender, DragEventArgs e)
{
if (IsInNowWindow())
{
e.Effects = DragDropEffects.None;
e.Handled = true;
return;
}
onDrop = false;
TreeViewItem souredata = e.Data.GetData(typeof(TreeViewItem)) as TreeViewItem;
if (souredata == null)
{
return;
}
OutlineNode soureoutlineNode = souredata.DataContext as OutlineNode;
Point pos = e.GetPosition(OutlineView);
HitTestResult result = VisualTreeHelper.HitTest(OutlineView, pos);
if (result == null)
{
HiddenaAllLine();
return;
}
TreeViewItem targetitem = CommonHelper.FindVisualParent(result.VisualHit);
if (targetitem == null)
{
HiddenaAllLine();
return;
}
OutlineNode targetoutlineNode = targetitem.DataContext as OutlineNode;
if (soureoutlineNode.Equals(targetoutlineNode))
{
HiddenaAllLine();
return;
}
(DataContext as OutLineControlViewModel).MoveOutLine(targetoutlineNode, soureoutlineNode);
HiddenaAllLine();
(DataContext as OutLineControlViewModel).Updata(true);
}
///
/// 清理线条显示,以及相关缓存对象
///
private void HiddenaAllLine()
{
(DataContext as OutLineControlViewModel).IsOnDrop = false;
(DataContext as OutLineControlViewModel).IsInsertHead = false;
if (Treeviewitem != null)
{
(Treeviewitem.DataContext as OutlineNode).IsInsertCurrentLayer = false;
(Treeviewitem.DataContext as OutlineNode).IsInsertNextLayer = false;
Treeviewitem = null;
}
}
private void ReName_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
TextBox ReNameText = sender as TextBox;
if (ReNameText == null)
{
return;
}
if (ReNameText.Visibility == Visibility.Visible)
{
if (ReNameOutlineNode != null)
{
ReNameText.Text = ReNameOutlineNode.Outline.Title;
}
ReNameText.Focus();
ReNameText.SelectAll();
IsReName = true;
}
}
private void RenameGrid_LostFocus(object sender, RoutedEventArgs e)
{
TextBox newdto = e.OriginalSource as TextBox;
if (newdto == null)
{
IsReName = false;
return;
}
string newtitle = string.IsNullOrEmpty(newdto.Text) ? "" : newdto.Text;
Grid senderdto = sender as Grid;
if (senderdto == null)
{
IsReName = false;
return;
}
senderdto.Visibility = Visibility.Collapsed;
if (ReNameOutlineNode != null)
{
CPDFOutline result = ReNameOutlineNode.Outline;
if (result != null)
{
if (newtitle == result.Title)
{
IsReName = false;
return;
}
(DataContext as OutLineControlViewModel).SetTitle(result, newtitle);
}
(DataContext as OutLineControlViewModel).Updata(false);
}
IsReName = false;
}
private void ToolTip_Opened(object sender, RoutedEventArgs e)
{
CustomImageControl imageControl = CommonHelper.FindVisualChild(sender as ToolTip);
OutlineNode outline = imageControl.DataContext as OutlineNode;
if (outline == null || string.IsNullOrEmpty(outline.PageIndex))
{
return;
}
//开始绘制
DrawingContext drawDc = imageControl.OpenDraw();
WriteableBitmap imageData = (DataContext as OutLineControlViewModel).LoadPreview(outline, (int)imageControl.ActualWidth, (int)imageControl.ActualHeight);
if (imageData == null)
{
imageControl.CloseDraw();
return;
}
drawDc.DrawImage(imageData, new Rect(0, 0, imageControl.ActualWidth, imageControl.ActualHeight));
imageControl.CloseDraw();
}
private void ToolTip_Closed(object sender, RoutedEventArgs e)
{
CustomImageControl imageControl = CommonHelper.FindVisualChild(sender as ToolTip);
//清空绘制上下文数据
imageControl.OpenDraw();
imageControl.CloseDraw();
}
private void TreeViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//每次鼠标按下,更新开始拖拽的窗口对象
MainWindow frm = Window.GetWindow(this) as MainWindow;
if (frm != null)
{
App.DropStartWindow = frm;
}
if (e.ClickCount == 1)
{
TreeViewItem content = sender as TreeViewItem;
if (content == null)
return;
Point p = e.GetPosition(OutlineView);
HitTestResult result = VisualTreeHelper.HitTest(OutlineView, p);
if (result != null)
{
//点击部位为箭头时,不跳转
ToggleButton button = CommonHelper.FindVisualParent(result.VisualHit);
if (button != null)
return;
Button btn = CommonHelper.FindVisualParent