-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathShellView.cs
435 lines (365 loc) · 18.2 KB
/
ShellView.cs
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Vanara.PInvoke;
using static Vanara.PInvoke.Shell32;
namespace Vanara.Windows.Shell;
/// <summary>Event argument for FilterItem event</summary>
public class FilterShellItemEventArgs : EventArgs
{
/// <summary>Initializes a new instance of the <see cref="NavigatedEventArgs"/> class.</summary>
/// <param name="item">The shell item.</param>
public FilterShellItemEventArgs(ShellItem item) => Item = item;
/// <summary>Gets or sets a value indicating whether a <see cref="ShellItem"/> is included by the filter.</summary>
/// <value><see langword="true"/> if included; otherwise, <see langword="false"/>.</value>
public bool Include { get; set; } = true;
/// <summary>The new location of the explorer browser</summary>
public ShellItem Item { get; private set; }
}
/// <summary>Event argument for The Navigated event</summary>
public class NavigatedEventArgs : EventArgs
{
/// <summary>Initializes a new instance of the <see cref="NavigatedEventArgs"/> class.</summary>
/// <param name="folder">The folder.</param>
public NavigatedEventArgs(ShellFolder folder) => NewLocation = folder;
/// <summary>The new location of the explorer browser</summary>
public ShellItem NewLocation { get; private set; }
}
/// <summary>Event argument for The Navigating event</summary>
public class NavigatingEventArgs : CancelEventArgs
{
/// <summary>Initializes a new instance of the <see cref="NavigatingEventArgs"/> class.</summary>
/// <param name="pendingLocation">The pending location.</param>
public NavigatingEventArgs(ShellItem pendingLocation) => PendingLocation = pendingLocation;
/// <summary>The location being navigated to.</summary>
public ShellItem PendingLocation { get; private set; }
}
/// <summary>Event argument for the NavigatinoFailed event</summary>
public class NavigationFailedEventArgs : EventArgs
{
/// <summary>The location the browser would have navigated to.</summary>
public ShellItem? FailedLocation { get; set; }
}
/// <summary>Encapsulates IShellView for display as a control.</summary>
/// <seealso cref="IComparable{T}"/>
/// <seealso cref="IDisposable"/>
/// <seealso cref="IEquatable{T}"/>
/// <seealso cref="IEquatable{T}"/>
/// <seealso cref="INotifyPropertyChanged"/>
// TODO: Fix problems with IShellView.CreateViewWindow returning E_FAIL
internal class ShellView : Control, INotifyPropertyChanged
{
internal HWND shellViewWindow;
private ShellFolder? currentFolder;
private IShellBrowser? iBrowser;
private IShellView? iShellView;
/// <summary>Creates a new <see cref="ShellView"/> from a shell folder and assigns it to a window.</summary>
/// <param name="folder">The shell folder.</param>
/// <returns>A new <see cref="ShellView"/> instance for the supplied shell folder.</returns>
public ShellView(ShellFolder folder) : this() => currentFolder = folder;
/// <summary>Initializes a new instance of the <see cref="ShellView"/> class.</summary>
public ShellView() => History = new ShellNavigationHistory();
/// <summary>Initializes a new instance of the <see cref="ShellView"/> class.</summary>
/// <param name="baseInterface">The base interface.</param>
internal ShellView(IShellView baseInterface) : this() => IShellView = baseInterface;
/// <summary>Fires when determining if an item should be shown in the view.</summary>
[Category("Action"), Description("Filter items in the view.")]
public event EventHandler<FilterShellItemEventArgs>? FilterItem;
/// <summary>Fires when the Items collection changes.</summary>
[Category("Action"), Description("Items changed.")]
public event EventHandler? ItemsChanged;
/// <summary>Fires when the ExplorerBrowser view has finished enumerating files.</summary>
[Category("Behavior"), Description("View is done enumerating files.")]
public event EventHandler? ItemsEnumerated;
/// <summary>
/// Fires when a navigation has been 'completed': no Navigating listener has canceled, and the ExplorerBrowser has created a new
/// view. The view will be populated with new items asynchronously, and ItemsChanged will be fired to reflect this some time later.
/// </summary>
[Category("Action"), Description("Navigation complete.")]
public event EventHandler<NavigatedEventArgs>? Navigated;
/// <summary>Fires when a navigation has been initiated, but is not yet complete.</summary>
[Category("Action"), Description("Navigation initiated, but not complete.")]
public event EventHandler<NavigatingEventArgs>? Navigating;
/// <summary>
/// Fires when either a Navigating listener cancels the navigation, or if the operating system determines that navigation is not possible.
/// </summary>
[Category("Action"), Description("Navigation failed.")]
public event EventHandler<NavigationFailedEventArgs>? NavigationFailed;
/// <summary>Occurs when a property value changes.</summary>
[Category("Behavior"), Description("Property changed.")]
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>Fires when the item selected in the view has changed (i.e., a rename ). This is not the same as SelectionChanged.</summary>
[Category("Action"), Description("Selected item has changed.")]
public event EventHandler? SelectedItemModified;
/// <summary>Fires when the SelectedItems collection changes.</summary>
[Category("Behavior"), Description("Selection changed.")]
public event EventHandler? SelectionChanged;
/// <summary>Gets or sets the <see cref="ShellFolder"/> currently being browsed by the <see cref="ShellView"/>.</summary>
[Category("Data"), DefaultValue(typeof(ShellFolder), ""), Description("The folder currently being browsed.")]
public ShellFolder CurrentFolder
{
get => currentFolder ??= IShellView is null ? ShellFolder.Desktop : new ShellFolder(GetFolderForView(IShellView)!);
set => Navigate(value);
}
/// <summary>A set of flags that indicate the options for the folder.</summary>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public FOLDERFLAGS Flags => IShellView?.GetCurrentInfo().fFlags ?? 0;
/// <summary>Contains the navigation history of the ExplorerBrowser</summary>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ShellNavigationHistory History { get; private set; }
/// <summary>Gets the underlying <see cref="IShellView"/> instance.</summary>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IShellView? IShellView
{
get => iShellView;
private set
{
iShellView = value;
//Items = new ShellItemArray(GetItemArray(iShellView, SVGIO.SVGIO_ALLVIEW));
}
}
///// <summary>Gets all the items in the view.</summary>
///// <value>An array with all the items.</value>
//public ShellItemArray Items { get; private set; }
/// <summary>Gets or sets the currently selected items in the view.</summary>
/// <value>An array with the selected items.</value>
/// <exception cref="ArgumentException">All items must belong to the folder hosted by this view. - SelectedItems</exception>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ShellItem[] SelectedItems
{
get => GetItems(IShellView, SVGIO.SVGIO_SELECTION);
set
{
if (IShellView is null) return;
// Deselect all
IShellView.SelectItem(default, SVSIF.SVSI_DESELECTOTHERS);
if (value is null || value.Length == 0) return;
// Get parent folder of view items
PIDL pidl = CurrentFolder.PIDL;
// Ensure all have this parent
if (!value.All(shi => shi.PIDL.Parent.Equals(pidl)))
throw new ArgumentException("All items must belong to the folder hosted by this view.", nameof(SelectedItems));
// Select all provided
foreach (ShellItem item in value)
IShellView.SelectItem((IntPtr)item.PIDL, SVSIF.SVSI_SELECT);
}
}
/// <summary>Folder view mode.</summary>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public FOLDERVIEWMODE ViewMode => IShellView?.GetCurrentInfo().ViewMode ?? 0;
/// <summary>
/// Retrieves a handle to one of the windows participating in in-place activation (frame, document, parent, or in-place object window).
/// </summary>
/// <returns>The window handle.</returns>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public HWND WindowHandle => shellViewWindow;
/// <summary>Gets the default size of the control.</summary>
protected override Size DefaultSize => new(250, 200);
private IShellBrowser Browser => iBrowser ??= new ShellBrowser(/* this */);
/// <summary>Implements the operator !=.</summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(ShellView? left, ShellView? right) => !(left == right);
/// <summary>Implements the operator ==.</summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(ShellView? left, ShellView? right) => EqualityComparer<ShellView?>.Default.Equals(left, right);
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
/// <exception cref="NotImplementedException"></exception>
public bool Equals(IShellView? other)
{
HWND w1 = HWND.NULL, w2 = HWND.NULL;
other?.GetWindow(out w1);
IShellView?.GetWindow(out w2);
return w1 == w2;
}
/// <summary>Determines whether the specified <see cref="object"/>, is equal to this instance.</summary>
/// <param name="obj">The <see cref="object"/> to compare with this instance.</param>
/// <returns><see langword="true"/> if the specified <see cref="object"/> is equal to this instance; otherwise, <see langword="false"/>.</returns>
public override bool Equals(object? obj) => ReferenceEquals(this, obj) || (obj is ShellView sv && Equals(sv.IShellView)) || (obj is IShellView isv && Equals(isv));
/// <summary>Returns a hash code for this instance.</summary>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
public override int GetHashCode() => (int)(IntPtr)WindowHandle;
/// <summary>
/// Clears the view of existing content, fills it with content from the specified container, and adds a new item to the history.
/// </summary>
/// <param name="folder">The shell folder to navigate to.</param>
public void Navigate(ShellFolder folder)
{
if (!OnNavigating(folder)) return;
ShellFolder? previous = currentFolder;
currentFolder = folder;
try
{
RecreateShellView();
History.Add(new ShellItem(folder.PIDL));
OnNavigated();
}
catch (Exception)
{
currentFolder = previous;
RecreateShellView();
throw;
}
OnPropertyChanged(nameof(CurrentFolder));
}
/// <summary>
/// Navigates to the last item in the navigation history list. This does not change the set of locations in the navigation log.
/// </summary>
public bool NavigateBack() { if (History.CanSeekBackward) { Navigate(new ShellFolder(History.SeekBackward()!)); return true; } return false; }
/// <summary>
/// Navigates to the next item in the navigation history list. This does not change the set of locations in the navigation log.
/// </summary>
/// <returns>True if the navigation succeeded, false if it failed for any reason.</returns>
public bool NavigateForward() { if (History.CanSeekForward) { Navigate(new ShellFolder(History.SeekForward()!)); return true; } return false; }
/// <summary>Navigates to the parent of the currently displayed folder.</summary>
public void NavigateParent() { if (CurrentFolder is not null && CurrentFolder != ShellFolder.Desktop) Navigate(CurrentFolder.Parent!); }
/// <summary>Refreshes the view's contents in response to user input.</summary>
public override void Refresh() => IShellView?.Refresh();
/// <summary>Saves the Shell's view settings so the current state can be restored during a subsequent browsing session.</summary>
public void SaveState() => IShellView?.SaveViewState();
/// <summary>Called to determine if an item should be shown in the view. Calls the <see cref="FilterItem"/> event if defined.</summary>
/// <param name="pidl">The PIDL of the child item.</param>
/// <returns><see langword="true"/> if the item should be included (this is the default); otherwise <see langword="false"/>.</returns>
protected internal virtual bool IncludeItem(PIDL pidl)
{
if (FilterItem is null)
return true;
var e = new FilterShellItemEventArgs(ShellItem.Open(CurrentFolder.IShellFolder, pidl));
FilterItem?.Invoke(this, e);
return e.Include;
}
/// <summary>Raises the <see cref="Control.DoubleClick"/> event.</summary>
/// <param name="e">An <see cref="EventArgs"/> that contains the event data.</param>
protected internal new virtual void OnDoubleClick(EventArgs e) => base.OnDoubleClick(e);
/// <summary>Raises the <see cref="ItemsChanged"/> event.</summary>
protected internal virtual void OnItemsChanged() => ItemsChanged?.Invoke(this, EventArgs.Empty);
/// <summary>Raises the <see cref="ItemsEnumerated"/> event.</summary>
protected internal virtual void OnItemsEnumerated() => ItemsEnumerated?.Invoke(this, EventArgs.Empty);
/// <summary>Raises the <see cref="Navigated"/> event.</summary>
protected internal virtual void OnNavigated() => Navigated?.Invoke(this, new NavigatedEventArgs(CurrentFolder));
/// <summary>Raises the <see cref="Navigating"/> event.</summary>
protected internal virtual bool OnNavigating(ShellFolder pendingLocation)
{
var e = new NavigatingEventArgs(pendingLocation);
Navigating?.Invoke(this, e);
return !e.Cancel;
}
/// <summary>Raises the <see cref="NavigationFailed"/> event.</summary>
protected internal virtual void OnNavigationFailed(NavigationFailedEventArgs nfevent)
{
if (nfevent?.FailedLocation is null) return;
NavigationFailed?.Invoke(this, nfevent);
}
/// <summary>Raises the <see cref="SelectedItemModified"/> event.</summary>
protected internal virtual void OnSelectedItemModified() => SelectedItemModified?.Invoke(this, EventArgs.Empty);
/// <summary>Raises the <see cref="SelectionChanged"/> event.</summary>
protected internal virtual void OnSelectionChanged() => SelectionChanged?.Invoke(this, EventArgs.Empty);
/// <summary>
/// Releases the unmanaged resources used by the <see cref="T:System.Windows.Forms.Control"/> and its child controls and optionally
/// releases the managed resources.
/// </summary>
/// <param name="disposing">
/// <see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.
/// </param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (!shellViewWindow.IsNull)
{
User32.DestroyWindow(shellViewWindow);
shellViewWindow = HWND.NULL;
}
iBrowser = null;
History.Clear();
IShellView = null;
}
base.Dispose(disposing);
}
/// <summary>Raises the <see cref="Control.CreateControl"/> method.</summary>
protected override void OnCreateControl()
{
base.OnCreateControl();
CreateShellView();
History.Add(new ShellItem(CurrentFolder.PIDL));
OnNavigated();
}
/// <summary>Raises the <see cref="Control.Resize"/> event.</summary>
/// <param name="eventargs">The <see cref="EventArgs"/> instance containing the event data.</param>
protected override void OnResize(EventArgs eventargs)
{
base.OnResize(eventargs);
if (!shellViewWindow.IsNull)
User32.SetWindowPos(shellViewWindow, HWND.HWND_TOP, 0, 0, ClientRectangle.Width, ClientRectangle.Height, 0);
}
/// <summary>Processes Windows messages.</summary>
/// <param name="m">The Windows <see cref="Message"/> to process.</param>
protected override void WndProc(ref Message m)
{
const int CWM_GETISHELLBROWSER = 0x407;
// Windows 9x sends the CWM_GETISHELLBROWSER message and expects the IShellBrowser for the window to be returned or an Access
// Violation occurs. This is pseudo-documented in knowledge base article Q157247.
if (m.Msg == CWM_GETISHELLBROWSER && iBrowser is not null)
{
m.Result = Marshal.GetComInterfaceForObject(iBrowser, typeof(IShellBrowser));
}
else
{
base.WndProc(ref m);
}
}
private static IShellView CreateViewObject(ShellFolder folder, HWND owner) =>
folder.IShellFolder.CreateViewObject<IShellView>(owner)!;
private static ShellFolder? GetFolderForView(IShellView? iView) => GetItems(iView, SVGIO.SVGIO_ALLVIEW).FirstOrDefault()?.Parent;
private static IShellItemArray GetItemArray(IShellView iView, SVGIO uItem) => ((IFolderView)iView).Items<IShellItemArray>(uItem)!;
private static ShellItem[] GetItems(IShellView? iView, SVGIO uItem)
{
if (iView is null) return new ShellItem[0];
using ComReleaser<IDataObject> ido = ComReleaserFactory.Create(iView.GetItemObject<IDataObject>(uItem)!);
var shdo = new ShellDataObject(ido.Item);
return shdo.GetShellIdList()?.ToArray() ?? new ShellItem[0];
}
private void CreateShellView()
{
IShellView? prev = IShellView;
IShellView = CreateViewObject(CurrentFolder, Handle);
try
{
var fsettings = prev?.GetCurrentInfo() ?? new FOLDERSETTINGS(ViewMode, Flags);
shellViewWindow = IShellView.CreateViewWindow(prev, fsettings, Browser, ClientRectangle);
}
catch (COMException ex)
{
// If the operation was cancelled by the user (for example because an empty removable media drive was selected, then
// "Cancel" pressed in the resulting dialog) convert the exception into something more meaningfil.
if (ex.ErrorCode == unchecked((int)0x800704C7U))
{
throw new OperationCanceledException("User cancelled.", ex);
}
}
if (prev != null)
{
prev.UIActivate(SVUIA.SVUIA_DEACTIVATE);
prev.DestroyViewWindow();
prev = null;
}
IShellView.UIActivate(SVUIA.SVUIA_ACTIVATE_NOFOCUS);
if (DesignMode) User32.EnableWindow(shellViewWindow, false);
}
private void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
private void RecreateShellView()
{
if (IShellView != null)
{
CreateShellView();
OnNavigated();
}
}
}