📄 filescout.cs
字号:
// FileScout.cs
// Copyright (C) 2001 Mike Krueger
// the ShellTree came orginaly from a ShellTree implementation sample from HyunKeun Cho
// (thanx for this piece of code)
//
// This program 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 2 of the License, or
// (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.IO;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.Reflection;
using System.Resources;
using System.Xml;
using SharpDevelop.Tool.Function;
using SharpDevelop.Tool.Data;
namespace SharpDevelop.Gui.Navigation {
public class DriveSelector : ComboBox
{
public DriveSelector()
{
DropDownStyle = ComboBoxStyle.DropDownList;
ScanDrives();
}
void ScanDrives()
{
string[] drivelist = Directory.GetLogicalDrives();
int selecteddrive = 0;
foreach (string drive in drivelist) {
string itemname = drive;
switch(FSTypeUtility.GetDriveType(drive)) {
case DriveType.Removeable:
itemname += " <removeable>";
break;
case DriveType.Fixed:
itemname += " <fixed>";
if (selecteddrive == 0)
selecteddrive = Items.Count;
break;
case DriveType.Cdrom:
itemname += " <cdrom>";
break;
case DriveType.Remote:
itemname += " <remote>";
break;
default:
itemname += " <unknown>";
break;
}
Items.Add(itemname);
}
SelectedIndex = selecteddrive;
}
public string GetDrive()
{
string item = SelectedItem.ToString();
return item.Substring(0, 3).Trim();
}
}
public class FileList : ListView
{
public FileList()
{
ResourceManager resources = new ResourceManager("ProjectComponentResources", this.GetType().Module.Assembly);
Columns.Add("File", 100, HorizontalAlignment.Left);
SmallImageList = FileUtility.ImageList;
HeaderStyle = ColumnHeaderStyle.None;
View = View.Details;
Alignment = ListViewAlignment.Left;
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (Columns != null && Columns.Count > 0)
Columns[0].Width = Width;
}
public void ShowFilesInPath(string path)
{
string[] files = Directory.GetFiles(path);
BeginUpdate();
Items.Clear();
foreach (string file in files) {
Items.Add(new FileListItem(file));
}
EndUpdate();
}
public class FileListItem : ListViewItem
{
string fullname;
public string FullName {
get {
return fullname;
}
}
public FileListItem(string fullname) : base(Path.GetFileName(fullname))
{
this.fullname = fullname;
ImageIndex = FileUtility.GetImageIndexFor(fullname);
}
}
}
public class FileScout : UserControl
{
MainWindow main;
DriveSelector driveselector = new DriveSelector();
Splitter splitter1 = new Splitter();
FileList filelister = new FileList();
ShellTree filetree = new ShellTree();
public FileScout(MainWindow main)
{
this.main = main;
Dock = DockStyle.Fill;
filetree.Dock = DockStyle.Fill;
filetree.BorderStyle = BorderStyle.Fixed3D;
filetree.Location = new System.Drawing.Point(0, 22);
filetree.Size = new System.Drawing.Size(184, 157);
filetree.TabIndex = 1;
filetree.AfterSelect += new TreeViewEventHandler(DirectorySelected);
ImageList imglist = new ImageList();
imglist.Images.Add(Resource.GetBitmap("Icons.16x16.ClosedFolderBitmap"));
imglist.Images.Add(Resource.GetBitmap("Icons.16x16.OpenFolderBitmap"));
imglist.Images.Add(Resource.GetBitmap("Icons.16x16.FLOPPY"));
imglist.Images.Add(Resource.GetBitmap("Icons.16x16.DRIVE"));
imglist.Images.Add(Resource.GetBitmap("Icons.16x16.CDROM"));
imglist.Images.Add(Resource.GetBitmap("Icons.16x16.NETWORK"));
filetree.ImageList = imglist;
filelister.Dock = DockStyle.Bottom;
filelister.BorderStyle = BorderStyle.Fixed3D;
filelister.Location = new System.Drawing.Point(0, 184);
filelister.Size = new System.Drawing.Size(184, 450);
filelister.TabIndex = 3;
filelister.ItemActivate += new EventHandler(FileSelected);
splitter1.Dock = DockStyle.Bottom;
splitter1.Location = new System.Drawing.Point(0, 179);
splitter1.Size = new System.Drawing.Size(184, 5);
splitter1.TabIndex = 2;
splitter1.TabStop = false;
driveselector.Size = new System.Drawing.Size(184, 21);
driveselector.Dock = DockStyle.Top;
driveselector.TabIndex = 0;
driveselector.SelectedIndexChanged += new EventHandler(DriveSelectionChange);
this.Controls.Add(filetree);
this.Controls.Add(splitter1);
this.Controls.Add(filelister);
this.Controls.Add(driveselector);
DriveSelectionChange(null, null);
}
void DriveSelectionChange(object sender, EventArgs e)
{
filetree.InitializeShellTree(driveselector.GetDrive());
filelister.Items.Clear(); // clear all file entries
}
void DirectorySelected(object sender, TreeViewEventArgs e)
{
filelister.ShowFilesInPath(filetree.Path + "\\");
}
void FileSelected(object sender, EventArgs e)
{
foreach (FileList.FileListItem item in filelister.SelectedItems) {
FileType filetype = FSTypeUtility.GetFileType(item.FullName);
switch (filetype) {
case FileType.SharpDevelopProject:
main.ProjectBrowser.OpenProject(item.FullName);
main.ProjectMode = true;
break;
default:
main.OpenWindow(item.FullName);
break;
}
}
}
}
public class ShellTree : TreeView
{
public string Path {
get {
return SelectedNode.FullPath;
}
set {
PopulateShellTree(value);
}
}
public ShellTree()
{
InitializeComponent();
}
void InitializeComponent ()
{
BeforeSelect += new TreeViewCancelEventHandler(SetClosedIcon);
AfterSelect += new TreeViewEventHandler(SetOpenedIcon);
}
void SetClosedIcon(object sender, TreeViewCancelEventArgs e) // Set icon as closed
{
if (SelectedNode != null && SelectedNode.Parent != null)
SelectedNode.ImageIndex = SelectedNode.SelectedImageIndex = 0;
}
void SetOpenedIcon(object sender, TreeViewEventArgs e) // Set icon as opened
{
if (e.Node.Parent != null)
e.Node.ImageIndex = e.Node.SelectedImageIndex = 1;
}
void PopulateShellTree(string path)
{
string[] pathlist = path.Split(new char[] {'\\'});
TreeNodeCollection curnode = Nodes;
foreach(string dir in pathlist) {
foreach(TreeNode childnode in curnode) {
if (childnode.Text.ToUpper().Equals(dir.ToUpper())) {
SelectedNode = childnode;
PopulateSubDirectory(childnode, 2);
childnode.Expand();
curnode = childnode.Nodes;
break;
}
}
}
}
void PopulateSubDirectory(TreeNode curNode, int depth)
{
if (--depth < 0) {
return;
}
if (curNode.Nodes.Count == 1 && curNode.Nodes[0].Text.Equals("")) {
string[] directories = Directory.GetDirectories(curNode.FullPath + "\\");
curNode.Nodes.Clear();
foreach (string fulldir in directories) {
string dir = System.IO.Path.GetFileName(fulldir);
FileAttributes attr = File.GetAttributes(fulldir);
if ((attr & FileAttributes.Hidden) == 0) {
TreeNode node = curNode.Nodes.Add(dir);
node.ImageIndex = node.SelectedImageIndex = 0;
node.Nodes.Add(""); // Add dummy child node to make node expandable
PopulateSubDirectory(node, depth);
}
}
} else {
foreach (TreeNode node in curNode.Nodes) {
PopulateSubDirectory(node, depth); // Populate sub directory
}
}
}
protected override void OnBeforeExpand(TreeViewCancelEventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
try {
PopulateSubDirectory(e.Node, 2);
Cursor.Current = Cursors.Default;
// SetOpenedIcon(null, null); // optional
} catch (Exception excpt) {
MessageBox.Show(excpt.Message, "Device error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
e.Cancel = true;
}
Cursor.Current = Cursors.Default;
}
protected override void OnBeforeCollapse(TreeViewCancelEventArgs e)
{
if (e.Node.Parent != null) { // Set icon as closed
// e.Node.ImageIndex = e.Node.SelectedImageIndex = 0;
}
}
public void InitializeShellTree(string drive)
{
Nodes.Clear();
TreeNode node = new TreeNode(drive.Substring(0, drive.Length - 1));
Nodes.Add(node);
node.Nodes.Add(""); // Add dummy child (make the node expandable)
node.Expand(); // Expand node
switch(FSTypeUtility.GetDriveType(drive)) {
case DriveType.Removeable:
node.ImageIndex = node.SelectedImageIndex = 2;
break;
case DriveType.Fixed:
node.ImageIndex = node.SelectedImageIndex = 3;
break;
case DriveType.Cdrom:
node.ImageIndex = node.SelectedImageIndex = 4;
break;
case DriveType.Remote:
node.ImageIndex = node.SelectedImageIndex = 5;
break;
default:
node.ImageIndex = node.SelectedImageIndex = 3;
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -