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

📄 openfiletabeventhandler.cs

📁 c#精彩编程百例(源代码)
💻 CS
字号:
//  OpenFileTabEventHandler.cs
//  Copyright (C) 2001 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.Collections;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Resources;
using System.ComponentModel;
using System.Xml;

using SharpDevelop.Gui.Components;
using SharpDevelop.Gui.Navigation;
using SharpDevelop.Gui.Navigation.ProjectBrowser;
using SharpDevelop.Gui.Window;
using SharpDevelop.Tool.Data;
using SharpDevelop.Tool.Function;
using SharpDevelop.Internal.Plugin;
using SharpDevelop.Gui.Edit.Text;

namespace SharpDevelop.Gui {
	
	/// <summary>
	/// This class handles the events and the context menu for the open file tab control
	/// </summary>
	public class OpenFileTabEventHandler
	{
		MainWindow mainwindow;
		int        clickedtab = -1;
		
		MenuItem  closeitem;
		MenuItem  saveitem;
		
		public OpenFileTabEventHandler(MainWindow mainwindow)
		{
			this.mainwindow = mainwindow;
			
			closeitem = new IconMenuItem(mainwindow, Resource.GetString("OpenFileTabEventHandler.ContextMenu.Close"), new EventHandler(CloseFileEvent), "");
			saveitem  = new IconMenuItem(mainwindow, Resource.GetString("OpenFileTabEventHandler.ContextMenu.Save"), new EventHandler(SaveFileEvent), "");
			
			mainwindow.OpenFileTab.ContextMenu = new ContextMenu(new MenuItem[] {
				closeitem,
				new IconMenuItem(mainwindow, "-", null,   ""),
				saveitem,
				new IconMenuItem(mainwindow, Resource.GetString("OpenFileTabEventHandler.ContextMenu.SaveAs"), new EventHandler(SaveAsFileEvent), ""),
				new IconMenuItem(mainwindow, "-", null,   ""),
				new IconMenuItem(mainwindow, Resource.GetString("OpenFileTabEventHandler.CopyPathName"), new EventHandler(CopyFileNameEvent), ""),
				new IconMenuItem(mainwindow, "-", null,   ""),
				new IconMenuItem(mainwindow, Resource.GetString("OpenFileTabEventHandler.Restore"), new EventHandler(RestoreWindowEvent), ""),
				new IconMenuItem(mainwindow, Resource.GetString("OpenFileTabEventHandler.Minimize"), new EventHandler(MinimizeWindowEvent), ""),
				new IconMenuItem(mainwindow, Resource.GetString("OpenFileTabEventHandler.Maximize"), new EventHandler(MaximizeWindowEvent), ""),
			});
			mainwindow.OpenFileTab.MouseDown += new MouseEventHandler(MouseDown);
		}
		
		void MouseDown(object sender, MouseEventArgs e)
		{
			closeitem.Enabled = false;
			saveitem.Enabled  = false;
			for (int i = 0; i < mainwindow.OpenFileTab.TabCount; ++i) {
				if (mainwindow.OpenFileTab.GetTabRect(i).Contains(e.X, e.Y)) {
					clickedtab = i;
					ContentWindow window = GetClickedWindow();
					if (window != null) {
						closeitem.Enabled = true;
						saveitem.Enabled  = !window.Untitled && window.Dirty;
					}
					return;
				}
			}
			clickedtab = -1;
		}
		
		ContentWindow GetClickedWindow()
		{
			
			if (clickedtab == -1) 
				return null;
			TabPage tabitem   = mainwindow.OpenFileTab.TabPages[clickedtab];
			
			foreach (ContentWindow window in mainwindow.MdiChildren) {
				if (window.TabItem == tabitem) {
					return window;
				}
			}
			return null;
			
		}
		
		void CloseFileEvent(object sender, EventArgs e)
		{
			TabPage selected  = mainwindow.OpenFileTab.SelectedTab;
			ContentWindow window = GetClickedWindow();
			if (window != null) {
				window.Close();
				if (window.TabItem != selected) {
					mainwindow.OpenFileTab.SelectedTab = selected;
				}
			}
		}
		
		void SaveAsFileEvent(object sender, EventArgs e)
		{
			ContentWindow window = GetClickedWindow();
			if (window != null) {
				window.SaveContentAs();
			}
		}
		
		void SaveFileEvent(object sender, EventArgs e)
		{
			ContentWindow window = GetClickedWindow();
			if (window != null) {
				window.SaveContent();
			}
		}
		
		void MaximizeWindowEvent(object sender, EventArgs e)
		{
			ContentWindow window = GetClickedWindow();
			if (window != null) {
				window.WindowState = FormWindowState.Maximized;
			}
		}
		
		void RestoreWindowEvent(object sender, EventArgs e)
		{
			ContentWindow window = GetClickedWindow();
			if (window != null) {
				window.WindowState = FormWindowState.Normal;
			}
		}
		void MinimizeWindowEvent(object sender, EventArgs e)
		{
			ContentWindow window = GetClickedWindow();
			if (window != null) {
				window.WindowState = FormWindowState.Minimized;
			}
		}
		
		void CopyFileNameEvent(object sender, EventArgs e)
		{
			ContentWindow window = GetClickedWindow();
			if (window != null) {
				Clipboard.SetDataObject(new DataObject(DataFormats.Text, window.TextName));
			}
		}
	}
}

⌨️ 快捷键说明

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