This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathShellComboBox.cs
503 lines (445 loc) · 16 KB
/
ShellComboBox.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using GongSolutions.Shell.Interop;
namespace GongSolutions.Shell
{
/// <summary>
/// Provides a drop-down list displaying the Windows Shell namespace.
/// </summary>
///
/// <remarks>
/// The <see cref="ShellComboBox"/> class displays a view of the Windows
/// Shell namespace in a drop-down list similar to that displayed in
/// a file open/save dialog.
/// </remarks>
public class ShellComboBox : Control
{
/// <summary>
/// Initializes a new instance of the <see cref="ShellComboBox"/> class.
/// </summary>
public ShellComboBox()
{
m_Combo.Dock = DockStyle.Fill;
m_Combo.DrawMode = DrawMode.OwnerDrawFixed;
m_Combo.DropDownStyle = ComboBoxStyle.DropDownList;
m_Combo.DropDownHeight = 300;
m_Combo.ItemHeight = SystemInformation.SmallIconSize.Height + 1;
m_Combo.Parent = this;
m_Combo.Click += new EventHandler(m_Combo_Click);
m_Combo.DrawItem += new DrawItemEventHandler(m_Combo_DrawItem);
m_Combo.SelectedIndexChanged += new EventHandler(m_Combo_SelectedIndexChanged);
m_Edit.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;
m_Edit.BorderStyle = BorderStyle.None;
m_Edit.Left = 8 + SystemInformation.SmallIconSize.Width;
m_Edit.Top = 4;
m_Edit.Width = Width - m_Edit.Left - 3 - SystemInformation.VerticalScrollBarWidth;
m_Edit.Parent = this;
m_Edit.Visible = false;
m_Edit.GotFocus += new EventHandler(m_Edit_GotFocus);
m_Edit.LostFocus += new EventHandler(m_Edit_LostFocus);
m_Edit.KeyDown += new KeyEventHandler(m_Edit_KeyDown);
m_Edit.MouseDown += new MouseEventHandler(m_Edit_MouseDown);
m_Edit.BringToFront();
m_ShellListener.DriveAdded += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
m_ShellListener.DriveRemoved += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
m_ShellListener.FolderCreated += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
m_ShellListener.FolderDeleted += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
m_ShellListener.FolderRenamed += new ShellItemChangeEventHandler(m_ShellListener_ItemRenamed);
m_ShellListener.FolderUpdated += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
m_ShellListener.ItemCreated += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
m_ShellListener.ItemDeleted += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
m_ShellListener.ItemRenamed += new ShellItemChangeEventHandler(m_ShellListener_ItemRenamed);
m_ShellListener.ItemUpdated += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
m_ShellListener.SharingChanged += new ShellItemEventHandler(m_ShellListener_ItemUpdated);
m_SelectedFolder = ShellItem.Desktop;
m_Edit.Text = GetEditString();
if (m_Computer == null)
{
m_Computer = new ShellItem(Environment.SpecialFolder.MyComputer);
}
CreateItems();
}
/// <summary>
/// Gets/sets a value indicating whether the combo box is editable.
/// </summary>
[DefaultValue(false)]
public bool Editable
{
get { return m_Editable; }
set
{
m_Edit.Visible = m_Editable = value;
}
}
/// <summary>
/// Gets/sets a value indicating whether the full file system path
/// should be displayed in the main portion of the control.
/// </summary>
[DefaultValue(false)]
public bool ShowFileSystemPath
{
get { return m_ShowFileSystemPath; }
set
{
m_ShowFileSystemPath = value;
m_Combo.Invalidate();
}
}
/// <summary>
/// Gets/sets the folder that the <see cref="ShellComboBox"/> should
/// display as the root folder.
/// </summary>
[Editor(typeof(ShellItemEditor), typeof(UITypeEditor))]
public ShellItem RootFolder
{
get { return m_RootFolder; }
set
{
m_RootFolder = value;
if (!m_RootFolder.IsParentOf(m_SelectedFolder))
{
m_SelectedFolder = m_RootFolder;
}
CreateItems();
}
}
/// <summary>
/// Gets/sets the folder currently selected in the
/// <see cref="ShellComboBox"/>.
/// </summary>
[Editor(typeof(ShellItemEditor), typeof(UITypeEditor))]
public ShellItem SelectedFolder
{
get { return m_SelectedFolder; }
set
{
if (m_SelectedFolder != value)
{
m_SelectedFolder = value;
CreateItems();
m_Edit.Text = GetEditString();
NavigateShellView();
OnChanged();
}
}
}
/// <summary>
/// Gets/sets a <see cref="ShellView"/> whose navigation should be
/// controlled by the combo box.
/// </summary>
[DefaultValue(null), Category("Behaviour")]
public ShellView ShellView
{
get { return m_ShellView; }
set
{
if (m_ShellView != null)
{
m_ShellView.Navigated -= new EventHandler(m_ShellView_Navigated);
}
m_ShellView = value;
if (m_ShellView != null)
{
m_ShellView.Navigated += new EventHandler(m_ShellView_Navigated);
m_ShellView_Navigated(m_ShellView, EventArgs.Empty);
}
}
}
/// <summary>
/// Occurs when the <see cref="ShellComboBox"/>'s
/// <see cref="SelectedFolder"/> property changes.
/// </summary>
public event EventHandler Changed;
/// <summary>
/// Occurs when the <see cref="ShellComboBox"/> control wants to know
/// if it should include a folder in its view.
/// </summary>
///
/// <remarks>
/// This event allows the folders displayed in the
/// <see cref="ShellComboBox"/> control to be filtered.
/// </remarks>
public event FilterItemEventHandler FilterItem;
internal bool ShouldSerializeRootFolder()
{
return m_RootFolder != ShellItem.Desktop;
}
internal bool ShouldSerializeSelectedFolder()
{
return m_SelectedFolder != ShellItem.Desktop;
}
void CreateItems()
{
if (!m_CreatingItems)
{
try
{
m_CreatingItems = true;
m_Combo.Items.Clear();
CreateItem(m_RootFolder, 0);
}
finally
{
m_CreatingItems = false;
}
}
}
void CreateItems(ShellItem folder, int indent)
{
IEnumerator<ShellItem> e = folder.GetEnumerator(
SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN);
while (e.MoveNext())
{
if (ShouldCreateItem(e.Current))
{
CreateItem(e.Current, indent);
}
}
}
void CreateItem(ShellItem folder, int indent)
{
int index = m_Combo.Items.Add(new ComboItem(folder, indent));
if (folder == m_SelectedFolder)
{
m_Combo.SelectedIndex = index;
}
if (ShouldCreateChildren(folder))
{
CreateItems(folder, indent + 1);
}
}
bool ShouldCreateItem(ShellItem folder)
{
FilterItemEventArgs e = new FilterItemEventArgs(folder);
ShellItem myComputer = new ShellItem(Environment.SpecialFolder.MyComputer);
e.Include = false;
if (ShellItem.Desktop.IsImmediateParentOf(folder) ||
m_Computer.IsImmediateParentOf(folder))
{
e.Include = folder.IsFileSystemAncestor;
}
else if ((folder == m_SelectedFolder) ||
folder.IsParentOf(m_SelectedFolder))
{
e.Include = true;
}
if (FilterItem != null)
{
FilterItem(this, e);
}
return e.Include;
}
bool ShouldCreateChildren(ShellItem folder)
{
return (folder == m_Computer) ||
(folder == ShellItem.Desktop) ||
folder.IsParentOf(m_SelectedFolder);
}
string GetEditString()
{
if (m_ShowFileSystemPath && m_SelectedFolder.IsFileSystem)
{
return m_SelectedFolder.FileSystemPath;
}
else
{
return m_SelectedFolder.DisplayName;
}
}
void NavigateShellView()
{
if ((m_ShellView != null) && !m_ChangingLocation)
{
try
{
m_ChangingLocation = true;
m_ShellView.Navigate(m_SelectedFolder);
}
catch (Exception)
{
SelectedFolder = m_ShellView.CurrentFolder;
}
finally
{
m_ChangingLocation = false;
}
}
}
void OnChanged()
{
if (Changed != null)
{
Changed(this, EventArgs.Empty);
}
}
void m_Combo_Click(object sender, EventArgs e)
{
OnClick(e);
}
void m_Combo_DrawItem(object sender, DrawItemEventArgs e)
{
int iconWidth = SystemInformation.SmallIconSize.Width;
int indent = ((e.State & DrawItemState.ComboBoxEdit) == 0) ?
(iconWidth / 2) : 0;
if (e.Index != -1)
{
string display;
ComboItem item = (ComboItem)m_Combo.Items[e.Index];
Color textColor = SystemColors.WindowText;
Rectangle textRect;
int textOffset;
SizeF size;
if ((e.State & DrawItemState.ComboBoxEdit) != 0)
{
// Don't draw the folder location in the edit box when
// the control is Editable as the edit control will
// take care of that.
display = m_Editable ? string.Empty : GetEditString();
}
else
{
display = item.Folder.DisplayName;
}
size = TextRenderer.MeasureText(display, m_Combo.Font);
textRect = new Rectangle(
e.Bounds.Left + iconWidth + (item.Indent * indent) + 3,
e.Bounds.Y, (int)size.Width, e.Bounds.Height);
textOffset = (int)((e.Bounds.Height - size.Height) / 2);
// If the text is being drawin in the main combo box edit area,
// draw the text 1 pixel higher - this is how it looks in Windows.
if ((e.State & DrawItemState.ComboBoxEdit) != 0)
{
textOffset -= 1;
}
if ((e.State & DrawItemState.Selected) != 0)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight, textRect);
textColor = SystemColors.HighlightText;
}
else
{
e.DrawBackground();
}
if ((e.State & DrawItemState.Focus) != 0)
{
ControlPaint.DrawFocusRectangle(e.Graphics, textRect);
}
SystemImageList.DrawSmallImage(e.Graphics,
new Point(e.Bounds.Left + (item.Indent * indent),
e.Bounds.Top),
item.Folder.GetSystemImageListIndex(ShellIconType.SmallIcon,
ShellIconFlags.OverlayIndex),
(e.State & DrawItemState.Selected) != 0);
TextRenderer.DrawText(e.Graphics, display, m_Combo.Font,
new Point(textRect.Left, textRect.Top + textOffset),
textColor);
}
}
void m_Combo_SelectedIndexChanged(object sender, EventArgs e)
{
if (!m_CreatingItems)
{
SelectedFolder = ((ComboItem)m_Combo.SelectedItem).Folder;
}
}
void m_Edit_GotFocus(object sender, EventArgs e)
{
m_Edit.SelectAll();
m_SelectAll = true;
}
void m_Edit_LostFocus(object sender, EventArgs e)
{
m_SelectAll = false;
}
void m_Edit_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
string path = m_Edit.Text;
if ((path == string.Empty) ||
(string.Compare(path, "Desktop", true) == 0))
{
SelectedFolder = ShellItem.Desktop;
return;
}
if (Directory.Exists(path))
{
SelectedFolder = new ShellItem(path);
return;
}
path = Path.Combine(m_SelectedFolder.FileSystemPath, path);
if (Directory.Exists(path))
{
SelectedFolder = new ShellItem(path);
return;
}
}
}
void m_Edit_MouseDown(object sender, MouseEventArgs e)
{
if (m_SelectAll)
{
m_Edit.SelectAll();
m_SelectAll = false;
}
else
{
m_Edit.SelectionStart = m_Edit.Text.Length;
}
}
void m_ShellView_Navigated(object sender, EventArgs e)
{
if (!m_ChangingLocation)
{
try
{
m_ChangingLocation = true;
SelectedFolder = m_ShellView.CurrentFolder;
OnChanged();
}
finally
{
m_ChangingLocation = false;
}
}
}
void m_ShellListener_ItemRenamed(object sender, ShellItemChangeEventArgs e)
{
CreateItems();
}
void m_ShellListener_ItemUpdated(object sender, ShellItemEventArgs e)
{
CreateItems();
}
class ComboItem
{
public ComboItem(ShellItem folder, int indent)
{
Folder = folder;
Indent = indent;
}
public ShellItem Folder;
public int Indent;
}
ComboBox m_Combo = new ComboBox();
TextBox m_Edit = new TextBox();
ShellView m_ShellView;
bool m_Editable;
ShellItem m_RootFolder = ShellItem.Desktop;
ShellItem m_SelectedFolder;
bool m_ChangingLocation;
bool m_ShowFileSystemPath;
bool m_CreatingItems;
bool m_SelectAll;
ShellNotificationListener m_ShellListener = new ShellNotificationListener();
static ShellItem m_Computer;
}
}