diff options
| author | Michael <[email protected]> | 2017-01-08 09:57:10 -0500 |
|---|---|---|
| committer | Michael <[email protected]> | 2017-01-08 09:57:10 -0500 |
| commit | f30dcf5ef41d54c588d7b42c48be8d941abba72e (patch) | |
| tree | 7705f99b965673b1c034ac2b1c56e65072c827df /ShiftOS.WinForms/Applications/FileDialog.cs | |
| parent | 69dfad54724d4176dfce238a8d7e73970e6eef24 (diff) | |
| download | shiftos_thereturn-f30dcf5ef41d54c588d7b42c48be8d941abba72e.tar.gz shiftos_thereturn-f30dcf5ef41d54c588d7b42c48be8d941abba72e.tar.bz2 shiftos_thereturn-f30dcf5ef41d54c588d7b42c48be8d941abba72e.zip | |
Initial upload
Diffstat (limited to 'ShiftOS.WinForms/Applications/FileDialog.cs')
| -rw-r--r-- | ShiftOS.WinForms/Applications/FileDialog.cs | 256 |
1 files changed, 256 insertions, 0 deletions
diff --git a/ShiftOS.WinForms/Applications/FileDialog.cs b/ShiftOS.WinForms/Applications/FileDialog.cs new file mode 100644 index 0000000..333a9b7 --- /dev/null +++ b/ShiftOS.WinForms/Applications/FileDialog.cs @@ -0,0 +1,256 @@ +using ShiftOS.Objects.ShiftFS; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using static ShiftOS.Objects.ShiftFS.Utils; +using ShiftOS.Engine; +using ShiftOS.WinForms.Tools; + +namespace ShiftOS.WinForms.Applications +{ + /// <summary> + /// + /// </summary> + public partial class FileDialog : UserControl, IShiftOSWindow + { + public FileDialog(string[] filetypes, FileOpenerStyle style, Action<string> _callback) + { + callback = _callback; + InitializeComponent(); + foreach(var itm in filetypes) + { + cbfiletypes.Items.Add(itm); + } + cbfiletypes.SelectedIndex = 0; + cbfiletypes.SelectedIndexChanged += (o, a) => { ResetList(); }; + this.lvitems.SelectedIndexChanged += (o, a) => + { + try + { + var itm = lvitems.SelectedItems[0]; + if (FileExists(currentdir + "/" + itm.Text)) + { + txtfilename.Text = itm.Text; + } + } + catch { } + + }; + btnok.Click += (o, a) => + { + string fname = ""; + fname = (!string.IsNullOrWhiteSpace(txtfilename.Text)) ? txtfilename.Text : ""; + fname = (!fname.EndsWith(cbfiletypes.SelectedItem.ToString())) ? fname + cbfiletypes.SelectedItem.ToString() : fname; + fname = (fname == cbfiletypes.SelectedItem.ToString()) ? "" : fname; + + switch (style) + { + + case FileOpenerStyle.Open: + + + if(FileExists(currentdir + "/" + fname)) + { + callback?.Invoke(currentdir + "/" + fname); + this.Close(); + } + else + { + Infobox.Show("{FILE_NOT_FOUND}", "{FILE_NOT_FOUND_EXP}"); + } + break; + case FileOpenerStyle.Save: + if (!string.IsNullOrWhiteSpace(fname)) + { + callback?.Invoke(currentdir + "/" + fname); + this.Close(); + } + else + { + Infobox.Show("{ENTER_FILENAME}", "{ENTER_FILENAME_EXP}"); + } + break; + } + }; + btnok.Text = style.ToString(); + this.Text = style.ToString() + " File"; + this.lvitems.DoubleClick += new EventHandler(this.lvitems_DoubleClick); + this.Load += (o, a) => + { + ChangeDirectory(Paths.GetPath("root")); + }; + } + + private void lvitems_DoubleClick(object sender, EventArgs e) + { + if (lvitems.SelectedItems.Count <= 0) + return; + + var item = lvitems.SelectedItems[0]; + var path = item.Tag as string; + if (currentdir == "__system") + { + ChangeDirectory(path); + } + else if (DirectoryExists(currentdir + "/" + path)) + { + ChangeDirectory(currentdir + "/" + path); + } + else if (FileExists(currentdir + "/" + path)) + { + callback?.Invoke(currentdir + "/" + txtfilename.Text); + this.Close(); + } + else if (path == "__..") + { + ChangeToParent(); + } + } + + Action<string> callback; + + string currentdrive = "0:"; + + public void ChangeToParent() + { + if (currentdir == currentdrive) + { + ChangeDirectory("__system"); + } + + ChangeDirectory(GetParent(currentdir)); + } + + public string GetParent(string path) + { + string[] pathlist = path.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries); + if (pathlist.Length > 1) + { + if (path.EndsWith("/")) + { + path = path.Remove(path.Length - 1, 1); + } + path = path.Remove(path.LastIndexOf('/'), path.Length - path.LastIndexOf('/')); + return path; + } + else + { + return "__system"; + } + } + + private string currentdir = ""; + + public void ChangeDirectory(string path) + { + currentdir = path; + lbcurrentfolder.Text = currentdir; + ResetList(); + } + + public void ResetList() + { + if (lvitems.LargeImageList == null) + { + lvitems.LargeImageList = new ImageList(); + lvitems.LargeImageList.TransparentColor = SkinEngine.LoadedSkin.ControlColor; + lvitems.LargeImageList.ImageSize = new Size(42, 42); + FileSkimmer.GetAllTypes(lvitems.LargeImageList); + } + + + lvitems.Items.Clear(); + + if (currentdir == "__system") + { + //List all drives + foreach (var dir in Mounts) + { + var item = FileSkimmer.ConstructItemAsMount(dir); + item.ImageKey = "Mount"; + lvitems.Items.Add(item); + } + } + else if (DirectoryExists(currentdir)) + { + var up = new ListViewItem(); + up.ImageKey = "UpOne"; + up.Text = "Up one"; + up.Tag = "__.."; + lvitems.Items.Add(up); + + + foreach (var dir in GetDirectories(currentdir)) + { + var item = FileSkimmer.ConstructItem(GetDirectoryInfo(dir)); + item.ImageKey = "Directory"; + lvitems.Items.Add(item); + } + + foreach (var dir in GetFiles(currentdir)) + { + if (dir.EndsWith(cbfiletypes.SelectedItem as string)) + { + var item = FileSkimmer.ConstructItem(GetFileInfo(dir)); + item.ImageKey = FileSkimmerBackend.GetFileType(dir).ToString(); + lvitems.Items.Add(item); + } + } + + } + } + + [Obsolete("Use the relevant static method within File Skimmer instead.")] + public static ListViewItem ConstructItemAsMount(Directory dir) + { + var item = new ListViewItem(); + item.Text = dir.Name + "(" + Mounts.IndexOf(dir).ToString() + ":/)"; + item.Tag = Mounts.IndexOf(dir).ToString() + ":"; + return item; + } + + + [Obsolete("Use the relevant static method within File Skimmer instead.")] + public static ListViewItem ConstructItem(Directory dir) + { + var item = new ListViewItem(); + item.Text = dir.Name; + item.Tag = item.Text; + return item; + } + + [Obsolete("Use the relevant static method within File Skimmer instead.")] + public static ListViewItem ConstructItem(File dir) + { + var item = new ListViewItem(); + item.Text = dir.Name; + item.Tag = item.Text; + return item; + } + + public void OnLoad() + { + } + + public void OnSkinLoad() + { + } + + public bool OnUnload() + { + return true; + } + + public void OnUpgrade() + { + } + } + + +} |
