|
| 1 | +/* |
| 2 | +Copyright 2022 Mark Lagodych |
| 3 | +
|
| 4 | +This file is part of Exquad. |
| 5 | +
|
| 6 | +Exquad is free software: you can redistribute it and/or modify it under the |
| 7 | +terms of the GNU General Public License as published by the Free Software |
| 8 | +Foundation, either version 3 of the License, or (at your option) any later |
| 9 | +version. |
| 10 | +
|
| 11 | +Exquad is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 13 | +A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | +
|
| 15 | +You should have received a copy of the GNU General Public License along with |
| 16 | +Exquad. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | +using System; |
| 20 | +using System.IO; |
| 21 | +using System.Windows.Forms; |
| 22 | + |
| 23 | +namespace Exquad |
| 24 | +{ |
| 25 | + class FileExplorer : Control |
| 26 | + { |
| 27 | + private WebBrowser browser; |
| 28 | + private Button buttonBack, buttonForward, buttonUp, buttonOpen; |
| 29 | + private TextBox textboxPath; |
| 30 | + |
| 31 | + public FileExplorer() |
| 32 | + { |
| 33 | + var table1 = new TableLayoutPanel(); |
| 34 | + this.Controls.Add(table1); |
| 35 | + table1.Dock = DockStyle.Fill; |
| 36 | + table1.RowCount = 2; |
| 37 | + table1.ColumnCount = 1; |
| 38 | + table1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0)); |
| 39 | + table1.RowStyles.Add(new RowStyle(SizeType.Percent, 100)); |
| 40 | + |
| 41 | + var table2 = new TableLayoutPanel(); |
| 42 | + table1.Controls.Add(table2); |
| 43 | + table2.RowCount = 1; |
| 44 | + table2.ColumnCount = 5; |
| 45 | + table2.Dock = DockStyle.Fill; |
| 46 | + table2.Height = 35; |
| 47 | + //table2.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0)); |
| 48 | + table2.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 0)); |
| 49 | + table2.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 0)); |
| 50 | + table2.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 0)); |
| 51 | + table2.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 0)); |
| 52 | + table2.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100)); |
| 53 | + |
| 54 | + buttonBack = new Button(); |
| 55 | + table2.Controls.Add(buttonBack); |
| 56 | + buttonBack.BackgroundImage = Properties.Resources.IconLeft; |
| 57 | + buttonBack.Click += (sender, e) => { GoBack(); }; |
| 58 | + |
| 59 | + buttonForward = new Button(); |
| 60 | + table2.Controls.Add(buttonForward); |
| 61 | + buttonForward.BackgroundImage = Properties.Resources.IconRight; |
| 62 | + buttonForward.Click += (sender, e) => { GoForward(); }; |
| 63 | + |
| 64 | + buttonUp = new Button(); |
| 65 | + table2.Controls.Add(buttonUp); |
| 66 | + buttonUp.BackgroundImage = Properties.Resources.IconUp; |
| 67 | + buttonUp.Click += (sender, e) => { GoUp(); }; |
| 68 | + |
| 69 | + buttonOpen = new Button(); |
| 70 | + table2.Controls.Add(buttonOpen); |
| 71 | + buttonOpen.BackgroundImage = Properties.Resources.IconOpen; |
| 72 | + buttonOpen.Click += (sender, e) => { OpenFolder(); }; |
| 73 | + |
| 74 | + textboxPath = new TextBox(); |
| 75 | + table2.Controls.Add(textboxPath); |
| 76 | + textboxPath.Dock = DockStyle.Fill; |
| 77 | + textboxPath.Font = new System.Drawing.Font("Calibri", 11); |
| 78 | + textboxPath.KeyPress += (sender, e) => { if (e.KeyChar == (char)Keys.Enter) BrowsePath(); }; |
| 79 | + |
| 80 | + |
| 81 | + browser = new WebBrowser(); |
| 82 | + table1.Controls.Add(browser); |
| 83 | + browser.Navigated += (sender, e) => { UpdateControls(); }; |
| 84 | + browser.Dock = DockStyle.Fill; |
| 85 | + browser.Navigate("C:\\"); |
| 86 | + |
| 87 | + foreach (var btn in new Button[] {buttonBack, buttonForward, buttonUp, buttonOpen}) |
| 88 | + { |
| 89 | + btn.BackgroundImageLayout = ImageLayout.Zoom; |
| 90 | + btn.Size = new System.Drawing.Size(24, 24); |
| 91 | + btn.BackColor = System.Drawing.Color.Transparent; |
| 92 | + btn.FlatStyle = FlatStyle.Flat; |
| 93 | + btn.FlatAppearance.BorderSize = 0; |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + private void GoBack() |
| 99 | + { |
| 100 | + if (browser.CanGoBack) |
| 101 | + browser.GoBack(); |
| 102 | + } |
| 103 | + |
| 104 | + private void GoForward() |
| 105 | + { |
| 106 | + if (browser.CanGoForward) |
| 107 | + browser.GoForward(); |
| 108 | + } |
| 109 | + |
| 110 | + private void GoUp() |
| 111 | + { |
| 112 | + var currentDir = browser.Url.LocalPath; |
| 113 | + var parentDir = Directory.GetParent(RemoveProtocolPrefix(currentDir)); |
| 114 | + |
| 115 | + if (parentDir != null) |
| 116 | + browser.Navigate(parentDir.FullName); |
| 117 | + } |
| 118 | + |
| 119 | + private string RemoveProtocolPrefix(string path) |
| 120 | + { |
| 121 | + if (path.StartsWith("file:///")) |
| 122 | + return path.Remove(0, "file:///".Length); |
| 123 | + return path; |
| 124 | + } |
| 125 | + |
| 126 | + private bool CanGoUp() |
| 127 | + { |
| 128 | + return Directory.GetParent(browser.Url.AbsolutePath) != null; |
| 129 | + } |
| 130 | + |
| 131 | + private void UpdateControls() |
| 132 | + { |
| 133 | + buttonBack.Enabled = browser.CanGoBack; |
| 134 | + buttonForward.Enabled = browser.CanGoForward; |
| 135 | + buttonUp.Enabled = CanGoUp(); |
| 136 | + textboxPath.Text = RemoveProtocolPrefix(browser.Url.ToString()); |
| 137 | + } |
| 138 | + |
| 139 | + private void OpenFolder() |
| 140 | + { |
| 141 | + var fd = new FolderBrowserDialog(); |
| 142 | + fd.Description = "Select a location to display"; |
| 143 | + fd.ShowNewFolderButton = true; |
| 144 | + |
| 145 | + if (fd.ShowDialog() == DialogResult.OK) |
| 146 | + browser.Navigate(fd.SelectedPath); |
| 147 | + } |
| 148 | + |
| 149 | + private void BrowsePath() |
| 150 | + { |
| 151 | + browser.Navigate(textboxPath.Text); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments