-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileExplorer.cs
154 lines (127 loc) · 5.69 KB
/
FileExplorer.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
/*
Copyright 2022 Mark Lagodych
This file is part of Exquad.
Exquad is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
Exquad is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Exquad. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using System.Windows.Forms;
namespace Exquad
{
class FileExplorer : Control
{
private WebBrowser browser;
private Button buttonBack, buttonForward, buttonUp, buttonOpen;
private TextBox textboxPath;
public FileExplorer()
{
var table1 = new TableLayoutPanel();
this.Controls.Add(table1);
table1.Dock = DockStyle.Fill;
table1.RowCount = 2;
table1.ColumnCount = 1;
table1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
table1.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
var table2 = new TableLayoutPanel();
table1.Controls.Add(table2);
table2.RowCount = 1;
table2.ColumnCount = 5;
table2.Dock = DockStyle.Fill;
table2.Height = 35;
//table2.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
table2.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 0));
table2.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 0));
table2.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 0));
table2.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 0));
table2.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
buttonBack = new Button();
table2.Controls.Add(buttonBack);
buttonBack.BackgroundImage = Properties.Resources.IconLeft;
buttonBack.Click += (sender, e) => { GoBack(); };
buttonForward = new Button();
table2.Controls.Add(buttonForward);
buttonForward.BackgroundImage = Properties.Resources.IconRight;
buttonForward.Click += (sender, e) => { GoForward(); };
buttonUp = new Button();
table2.Controls.Add(buttonUp);
buttonUp.BackgroundImage = Properties.Resources.IconUp;
buttonUp.Click += (sender, e) => { GoUp(); };
buttonOpen = new Button();
table2.Controls.Add(buttonOpen);
buttonOpen.BackgroundImage = Properties.Resources.IconOpen;
buttonOpen.Click += (sender, e) => { OpenFolder(); };
textboxPath = new TextBox();
table2.Controls.Add(textboxPath);
textboxPath.Dock = DockStyle.Fill;
textboxPath.Font = new System.Drawing.Font("Calibri", 11);
textboxPath.KeyPress += (sender, e) => { if (e.KeyChar == (char)Keys.Enter) BrowsePath(); };
browser = new WebBrowser();
table1.Controls.Add(browser);
browser.Navigated += (sender, e) => { UpdateControls(); };
browser.Dock = DockStyle.Fill;
browser.Navigate("C:\\");
foreach (var btn in new Button[] {buttonBack, buttonForward, buttonUp, buttonOpen})
{
btn.BackgroundImageLayout = ImageLayout.Zoom;
btn.Size = new System.Drawing.Size(24, 24);
btn.BackColor = System.Drawing.Color.Transparent;
btn.FlatStyle = FlatStyle.Flat;
btn.FlatAppearance.BorderSize = 0;
}
}
private void GoBack()
{
if (browser.CanGoBack)
browser.GoBack();
}
private void GoForward()
{
if (browser.CanGoForward)
browser.GoForward();
}
private void GoUp()
{
var currentDir = browser.Url.LocalPath;
var parentDir = Directory.GetParent(RemoveProtocolPrefix(currentDir));
if (parentDir != null)
browser.Navigate(parentDir.FullName);
}
private string RemoveProtocolPrefix(string path)
{
if (path.StartsWith("file:///"))
return path.Remove(0, "file:///".Length);
return path;
}
private bool CanGoUp()
{
return Directory.GetParent(browser.Url.AbsolutePath) != null;
}
private void UpdateControls()
{
buttonBack.Enabled = browser.CanGoBack;
buttonForward.Enabled = browser.CanGoForward;
buttonUp.Enabled = CanGoUp();
textboxPath.Text = RemoveProtocolPrefix(browser.Url.ToString());
}
private void OpenFolder()
{
var fd = new FolderBrowserDialog();
fd.Description = "Select a location to display";
fd.ShowNewFolderButton = true;
if (fd.ShowDialog() == DialogResult.OK)
browser.Navigate(fd.SelectedPath);
}
private void BrowsePath()
{
browser.Navigate(textboxPath.Text);
}
}
}