using Microsoft.WindowsAPICodePack.Controls;
using Microsoft.WindowsAPICodePack.Shell;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
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.Shapes;
namespace Tango.FSE.UI.Storage
{
///
/// Interaction logic for ExplorerControlWindow.xaml
///
public partial class ExplorerControlDialog : Window
{
private static string _lastDirectory;
private static Point? _lastLocation;
private string _currentLocation;
public List SelectedItems { get; set; }
public String InitialDirectory { get; set; }
public ObservableCollection ShellHistory
{
get { return (ObservableCollection)GetValue(ShellHistoryProperty); }
set { SetValue(ShellHistoryProperty, value); }
}
public static readonly DependencyProperty ShellHistoryProperty =
DependencyProperty.Register("ShellHistory", typeof(ObservableCollection), typeof(ExplorerControlDialog), new PropertyMetadata(null));
public ExplorerControlDialog()
{
SelectedItems = new List();
ShellHistory = new ObservableCollection();
InitializeComponent();
Loaded += ExplorerControlWindow_Loaded;
SourceInitialized += ExplorerControlWindow_SourceInitialized;
explorer.browser.SelectionChanged += Browser_SelectionChanged;
explorer.browser.NavigationComplete += Browser_NavigationComplete;
btnSelect.IsEnabled = false;
btnBack.IsEnabled = false;
btnForward.IsEnabled = false;
if (_lastLocation != null)
{
WindowStartupLocation = WindowStartupLocation.Manual;
Left = _lastLocation.Value.X;
Top = _lastLocation.Value.Y;
}
else
{
WindowStartupLocation = WindowStartupLocation.CenterOwner;
}
}
private void FillShellHistory(ShellObject current)
{
ShellHistory.Clear();
ShellObject parent = current;
while (parent != null)
{
ShellHistory.Insert(0, parent);
if (parent.ToString() == "This PC") break;
parent = parent.Parent;
}
}
private void Browser_NavigationComplete(object sender, Microsoft.WindowsAPICodePack.Controls.NavigationCompleteEventArgs e)
{
_currentLocation = e.NewLocation.ParsingName;
ShellObject shell = ShellObject.FromParsingName(_currentLocation);
if (shell.ToString() != "This PC")
{
btnUp.IsEnabled = true;
txtLocation.Text = shell.GetDisplayName(DisplayNameType.FileSystemPath);
}
else
{
btnUp.IsEnabled = false;
txtLocation.Text = "";
}
FillShellHistory(shell);
btnBack.IsEnabled = explorer.browser.NavigationLog.CanNavigateBackward;
btnForward.IsEnabled = explorer.browser.NavigationLog.CanNavigateForward;
}
private void Browser_SelectionChanged(object sender, EventArgs e)
{
if (explorer.browser.SelectedItems.OfType().ToList().Count > 0)
{
btnSelect.IsEnabled = true;
}
else
{
btnSelect.IsEnabled = false;
}
}
private void ExplorerControlWindow_SourceInitialized(object sender, EventArgs e)
{
this.HideMinimizeAndMaximizeButtons();
}
private void ExplorerControlWindow_Loaded(object sender, RoutedEventArgs e)
{
ShellObject initialShellObject = (ShellObject)KnownFolders.Desktop;
if (InitialDirectory != null && Directory.Exists(InitialDirectory))
{
initialShellObject = ShellObject.FromParsingName(InitialDirectory);
}
else if (_lastDirectory != null && Directory.Exists(_lastDirectory))
{
initialShellObject = ShellObject.FromParsingName(_lastDirectory);
}
explorer.browser.Navigate(initialShellObject);
}
private void BtnSelect_Click(object sender, RoutedEventArgs e)
{
SelectedItems.Clear();
foreach (var item in explorer.browser.SelectedItems.OfType())
{
SelectedItems.Add(item.ParsingName);
}
_lastDirectory = _currentLocation;
_lastLocation = new Point(Left, Top);
DialogResult = true;
Close();
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
_lastLocation = new Point(Left, Top);
Close();
}
private void BtnBack_Click(object sender, RoutedEventArgs e)
{
explorer.browser.NavigateLogLocation(NavigationLogDirection.Backward);
}
private void BtnForward_Click(object sender, RoutedEventArgs e)
{
explorer.browser.NavigateLogLocation(NavigationLogDirection.Forward);
}
private void BtnUp_Click(object sender, RoutedEventArgs e)
{
ShellObject currentLocation = ShellObject.FromParsingName(_currentLocation);
explorer.browser.Navigate(currentLocation.Parent);
}
private void TxtLocation_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
listHistory.Visibility = Visibility.Collapsed;
}
private void TxtLocation_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
listHistory.Visibility = Visibility.Visible;
}
private void OnHistoryItemClicked(object sender, RoutedEventArgs e)
{
ShellObject shell = (sender as Button).DataContext as ShellObject;
explorer.browser.Navigate(shell);
}
}
internal static class WindowExtensions
{
// from winuser.h
private const int GWL_STYLE = -16,
WS_MAXIMIZEBOX = 0x10000,
WS_MINIMIZEBOX = 0x20000;
[DllImport("user32.dll")]
extern private static int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
extern private static int SetWindowLong(IntPtr hwnd, int index, int value);
internal static void HideMinimizeAndMaximizeButtons(this Window window)
{
IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;
var currentStyle = GetWindowLong(hwnd, GWL_STYLE);
SetWindowLong(hwnd, GWL_STYLE, (currentStyle & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX));
}
}
}