aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/Storage/ExplorerControlDialog.xaml.cs
blob: db12a70f1e18a8933759b0ea9010309c2be695df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
{
    /// <summary>
    /// Interaction logic for ExplorerControlWindow.xaml
    /// </summary>
    public partial class ExplorerControlDialog : Window
    {
        private static string _lastDirectory;
        private static Point? _lastLocation;

        private string _currentLocation;

        public List<String> SelectedItems { get; set; }
        public String InitialDirectory { get; set; }

        public ObservableCollection<ShellObject> ShellHistory
        {
            get { return (ObservableCollection<ShellObject>)GetValue(ShellHistoryProperty); }
            set { SetValue(ShellHistoryProperty, value); }
        }
        public static readonly DependencyProperty ShellHistoryProperty =
            DependencyProperty.Register("ShellHistory", typeof(ObservableCollection<ShellObject>), typeof(ExplorerControlDialog), new PropertyMetadata(null));

        public ExplorerControlDialog()
        {
            SelectedItems = new List<string>();
            ShellHistory = new ObservableCollection<ShellObject>();

            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<ShellObject>().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<ShellObject>())
            {
                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));
        }
    }
}