⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 projectbrowsereventhandler.cs

📁 c#精彩编程百例(源代码)
💻 CS
📖 第 1 页 / 共 2 页
字号:
//  ProjectBrowserEventHandler.cs 
//  Copyright (C) 2000 Mike Krueger
//
//  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.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

using SharpDevelop.Tool.Data;
using SharpDevelop.Tool.Function;
using SharpDevelop.Gui.Dialogs;
using SharpDevelop.Gui.Window;
using SharpDevelop.Gui.Components;
using SharpDevelop.Internal.Project;
using SharpDevelop.Gui.Dialogs.OptionPanels;

namespace SharpDevelop.Gui.Navigation.ProjectBrowser {
	
	public class ProjectBrowserEventHandler
	{
		MainWindow     mainwindow;
		ProjectBrowser browser;
		int  mousex, mousey; // mouse coordinates used for clicking
		bool drag = false;
		
		ContextMenu empty;
		ContextMenu standard;
		ContextMenu file;
		ContextMenu project;
		ContextMenu reference;
		
		public ProjectBrowserEventHandler(MainWindow mainwindow, ProjectBrowser browser)
		{
			this.mainwindow = mainwindow;
			this.browser    = browser;
			
			browser.BeforeExpand   += new TreeViewCancelEventHandler(ShowOpenFolderIcon);
			browser.BeforeCollapse += new TreeViewCancelEventHandler(ShowClosedFolderIcon);
			browser.AfterLabelEdit += new NodeLabelEditEventHandler(RenameEntry);
			browser.DoubleClick    += new EventHandler(OpenFile);
			
			browser.DragDrop  += new DragEventHandler(DragDrop);
			browser.DragEnter += new DragEventHandler(DragEnterEvent);
			browser.DragOver  += new DragEventHandler(DragOver);
			browser.DragLeave += new EventHandler(DragLeaveEvent);
			
			browser.MouseMove += new MouseEventHandler(MouseMove);
			empty    = new ContextMenu();
			
			
			reference = new ContextMenu(new MenuItem[] {
										new IconMenuItem(mainwindow, "AddReference",   new EventHandler(AddReferenceToProject),     ""),
			});
			
			standard = new ContextMenu(new MenuItem[] {
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.AddFiles"),   new EventHandler(AddFilesToProject),     ""),
										new IconMenuItem(mainwindow, "-", null,   ""),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.NewFiles"),   new EventHandler(NewFileEvent),     ""),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.NewFolder"), new EventHandler(NewFolderEvent),   ""),
										new IconMenuItem(mainwindow, "-", null,   ""),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.Remove"),     new EventHandler(RemoveEntryEvent), "", Shortcut.Del),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.Rename"),     new EventHandler(RenameEntryEvent), "", Shortcut.F2)
			});
			
			project = new ContextMenu(new MenuItem[] {
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.AddFiles"),   new EventHandler(AddFilesToProject),     ""),
										new IconMenuItem(mainwindow, "-", null,   ""),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.NewFiles"),   new EventHandler(NewFileEvent),     ""),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.NewFolder"), new EventHandler(NewFolderEvent),   ""),
										new IconMenuItem(mainwindow, "-", null,   ""),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.Remove"),     new EventHandler(RemoveEntryEvent), "", Shortcut.Del),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.Rename"),     new EventHandler(RenameEntryEvent), "", Shortcut.F2),
										new IconMenuItem(mainwindow, "-", null,   ""),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.Options"),     new EventHandler(ProjectOptionsEvent), "")
			});
			
			file     = new ContextMenu(new MenuItem[] {
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.AddFiles"),   new EventHandler(AddFilesToProject),     ""),
										new IconMenuItem(mainwindow, "-", null,   ""),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.Open"),   new EventHandler(OpenFile), ""),
										new IconMenuItem(mainwindow, "-", null,   ""),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.NewFiles"), new EventHandler(NewFileEvent), ""),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.NewFolder"), new EventHandler(NewFolderEvent), ""),
										new IconMenuItem(mainwindow, "-", null,   ""),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.Remove"), new EventHandler(RemoveEntryEvent), "", Shortcut.Del),
										new IconMenuItem(mainwindow, Resource.GetString("ProjectComponent.ContextMenu.Rename"), new EventHandler(RenameEntryEvent), "", Shortcut.F2)
			});
			
			browser.ContextMenu = standard;
			
			browser.MouseDown += new MouseEventHandler(SetContextMenu);
		}
		
		void ProjectOptionsEvent(object sender, EventArgs e)
		{
			ProjectBrowserNode node = (ProjectBrowserNode)browser.SelectedNode;
			if (node == null)
				return;
			// TODO : TabbedOptions config
			TabbedOptions topt = new TabbedOptions(mainwindow);
			topt.AddOptionPanel(new PropertyGridPanel(Resource.GetString("Dialog.Options.ProjectOptionsText"), node.Project));
			topt.AddOptionPanel(new PropertyGridPanel(Resource.GetString("Dialog.Options.CompilerOptionsText"), node.Project.CompilerParameters));
			topt.ShowDialog();
			
			browser.SaveProject();
		}
		
		void SetContextMenu(object sender, MouseEventArgs e)
		{
			ProjectBrowserNode node = (ProjectBrowserNode)browser.GetNodeAt(e.X, e.Y);
			browser.ContextMenu = empty;
			browser.SelectedNode = node;
			if (node != null) {
				switch (node.ProjectNodeType) {
					case ProjectNodeType.ReferenceFolder:
						browser.ContextMenu = reference;
						break;
					case ProjectNodeType.Project:
						browser.ContextMenu = project;
						break;
					case ProjectNodeType.NonEditable:
					case ProjectNodeType.ResourceFolder:
						break;
					case ProjectNodeType.ResourceFile:
					case ProjectNodeType.TextFile:
						browser.ContextMenu = file;
						break;
					default:
						browser.ContextMenu = standard;
						break;
				}
			}
		}
		
		void MouseMove(object sender, MouseEventArgs e)
		{
			mousex = e.X;
			mousey = e.Y;
			if (!drag) {
				ProjectBrowserNode node = (ProjectBrowserNode)browser.GetNodeAt(e.X, e.Y);
				if (node != null && e.Button == MouseButtons.Left) {
					if (node.ProjectNodeType != ProjectNodeType.NonEditable     && 
						node.ProjectNodeType != ProjectNodeType.ReferenceFolder &&
						node.ProjectNodeType != ProjectNodeType.ResourceFolder  &&
						node.ProjectNodeType != ProjectNodeType.Reference) {
							browser.DoDragDrop(new DataObject(DataFormats.FileDrop, new string[] {node.FileName }), DragDropEffects.All);
						}
				}
			} else
				drag = false;
		}
		
		void DragOver(object sender, DragEventArgs e)
		{
			Point clientcoordinate  = browser.PointToClient(new Point(e.X, e.Y));
			ProjectBrowserNode node = (ProjectBrowserNode)browser.GetNodeAt(clientcoordinate);
			if (drag) {
				if (browser.SelectedNode != node) {
					browser.SelectedNode = node;
					browser.Select();
				}
			}
			
			drag = true;
		}
		
		void DragEnterEvent(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.Move;
			drag = true;
		}
		
		void DragLeaveEvent(object sender, EventArgs e)
		{
			drag = false;
		}
		
		void DragDrop(object sender, DragEventArgs e)
		{
			Point clientcoordinate  = browser.PointToClient(new Point(e.X, e.Y));
			ProjectBrowserNode node = (ProjectBrowserNode)browser.GetNodeAt(clientcoordinate);
			if (node == null)
				return;
			
			string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
			foreach (string file in files) {
				MoveCopyFile(file, node, true);
			}
			drag = false;
		}
		
		void AddReferenceToProject(object sender, EventArgs e)
		{
			ProjectBrowserNode node = (ProjectBrowserNode)browser.SelectedNode;
			if (node == null)
				return;
			
			OpenFileDialog fdiag  = new OpenFileDialog();
			fdiag.AddExtension    = true;
			fdiag.Filter          = Option.GetProperty("SharpDevelop.Action.MenuAction.OpenFile.FileFilter", "").ToString();
			fdiag.Multiselect     = true;
			fdiag.CheckFileExists = true;
			
			if (fdiag.ShowDialog() == DialogResult.OK) {
				ISdProject project = node.Project;
				foreach (string file in fdiag.FileNames) {
					node.Nodes.Add(new ProjectBrowserNode(project, ProjectNodeType.Reference, file));
				}
			}
		}
		
		void AddFilesToProject(object sender, EventArgs e)
		{
			ProjectBrowserNode node = (ProjectBrowserNode)browser.SelectedNode;
			if (node == null)
				return;
			
			OpenFileDialog fdiag  = new OpenFileDialog();
			fdiag.AddExtension    = true;
			fdiag.Filter          = Option.GetProperty("SharpDevelop.Action.MenuAction.OpenFile.FileFilter", "").ToString();
			fdiag.Multiselect     = true;
			fdiag.CheckFileExists = true;
			
			
			if (fdiag.ShowDialog() == DialogResult.OK) {
				
				DialogResult dr = MessageBox.Show("Move or copy file ?", "Move or copy", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
				if (dr == DialogResult.Cancel)
					return;
				foreach (string file in fdiag.FileNames) {
					MoveCopyFile(file, node, dr == DialogResult.Yes);
				}
			}
		}
		
		
		void MoveCopyFile(string filename, ProjectBrowserNode node, bool move)
		{
			FileType type      = FSTypeUtility.GetFileType(filename);
			bool     directory = FSTypeUtility.IsDirectory(filename);
			if (type == FileType.Dll || 
				type == FileType.Resource || 
				directory) { // insert reference
				return;
			}
			

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -