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

📄 contentwindow.cs

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

using SharpDevelop.Gui.Components;
using SharpDevelop.Gui.Edit;
using SharpDevelop.Gui.Edit.Text;
using SharpDevelop.Tool.Data;
using SharpDevelop.Tool.Function;
using SharpDevelop.Internal.Project;
using SharpDevelop.Internal.Modules;

namespace SharpDevelop.Gui.Window {
	
	public enum WindowContent { 
		Text,
		CSFile,
		Assembly,
		Browser
	};
	
	/// <summary>
	/// This class is the heart of the contents, which SharpDevelop can display
	/// every MDI child is a ContentWindow, the content is set by the WindowContent property
	/// </summary>
	public class ContentWindow : Form
	{
		ISdEditable        editable        = null;
		
		bool            writeprotected  = false;
		bool            untitled        = true;
		bool            dirty           = true;
		bool            hastextarea     = false;
		
		MainWindow      mainwindow = null;
		TabPage         tabitem    = null;
		
		WindowContent   content;
		string          filename = null;
		string          language = null;
		int          mynum       = -1;
		static int[] untitledNum = new int[256];
		
		string   defaultname;
		
		/// <summary>
		/// Untitled files must have a default name, the icon of the file is set
		/// accordingly to this name.
		/// </summary>
		public string DefaultName {
			get {
				return defaultname;
			}
			set {
				defaultname = value;
				GetIcon(defaultname);
				RenameEvent();
			}
		}
		
		/// <summary>
		/// This is the content of the window.
		/// </summary>
		public WindowContent Content {
			get {
				return content;
			}
		}
		
		/// <summary>
		/// If this property is true, you can get a valid textarea which is displayed in the window
		/// via the TextArea property.
		/// </summary>
		public bool HasTextArea {
			get {
				return hastextarea;
			}
		}
		
		/// <summary>
		/// Here you get the editable component, if any, otherwise null.
		/// </summary>
		public ISdEditable ISdEditable {
			get {
				return editable;
			}
		}
		
		/// <summary>
		/// Here you get a valid textarea which is displayed in this window, check the HasTextArea property
		/// before you access this.
		/// </summary>
		public TextAreaControl TextArea {
			get {
				Debug.Assert(hastextarea, "SharpDevelop.Gui.Window : property TextArea : has no textarea");
				return (TextAreaControl)editable;
			}
		}
		
		/// <summary>
		/// This is the tabpage which this contentwindow is linked to.
		/// </summary>
		public TabPage TabItem {
			get {
				return tabitem;
			}
		}
		
		/// <summary>
		/// true if this window is untitled.
		/// </summary>
		public bool    Untitled {
			get {
				return untitled;
			}
			set {
				untitled = value;
				RenameEvent();
			}
		}
		
		/// <summary>
		/// true if the window content has changed.
		/// </summary>
		public bool    Dirty {
			get {
				return dirty;
			}
			set {
				if (Content == WindowContent.Browser)
					dirty = false;
				else
					dirty = value;
				RenameEvent();
			}
		}
		
		/// <summary>
		/// The filename of the window content.
		/// </summary>
		public string FileName {
			get {
				return filename;
			}
			set {
				filename = value;
				RenameEvent();
			}
		}
		
		/// <summary>
		/// mostly the same code like in rename event, this function is used, when you need
		/// the full name without * or + at the end. 
		/// </summary>
		public string TextName {
			get {
				string newText = "";
				if (Untitled) {
					if (mynum == -1) 
						mynum = ++untitledNum[(int)content];
					
					switch (content) {
						case WindowContent.Text:
							newText = "Text";
							break;
						case WindowContent.CSFile:
							if (defaultname == null) {
								Module info = ModuleManager.GetModulePerLanguageName(language);
								if (info != null) {
									newText = info.Language.Split(new char[] {'|'})[0];
								}  else
									newText = "Source";
							} else {
								newText = defaultname;
							}
							break;
					}
					Console.WriteLine("New text2 : " + newText);
					newText = Path.GetFileNameWithoutExtension(newText) + mynum + Path.GetExtension(newText);
					Console.WriteLine("new New text2 : " + newText);
				} else {
					newText = filename;
				}
				Console.WriteLine(">:>>:>:>:> " + newText);
				return newText;
			}
		}
	
		
		public ContentWindow(MainWindow mainwindow, WindowContent content, string file, string language)
		{
			this.mainwindow = mainwindow;
			this.content = content;
			this.tabitem = new TabPage();
			this.language = language;
			if (mainwindow.MdiChildren.Length == 0)  // first child -> maximize
				WindowState = FormWindowState.Maximized;
			
			SetupLayout();
			
			if (file != null)
				LoadContent(file);
			
			MdiParent = mainwindow;
			
			mainwindow.OpenFileTab.TabPages.Add(tabitem);
			
//			main.OpenFileTab.SelectedTab = tabitem;
			
			RenameEvent();
			GetIcon(file);
		}
		
		protected override void OnGotFocus(EventArgs e)
		{
			base.OnGotFocus(e);
			mainwindow.OpenFileTab.SelectedTab = tabitem;
		}
		
		void GetIcon(string filename)
		{
			Console.WriteLine("file : " + filename);
			if (filename != null) {
				int index = FileUtility.GetImageIndexFor(filename);
				if (index >= 0) {
					Bitmap b = (Bitmap)FileUtility.ImageList.Images[index];
					if (b != null)
						Icon = Icon.FromHandle(b.GetHicon());
				}
			}
		}
		
		void SetupLayout()
		{
			TextAreaControl textcontrol = null;
			TabControl tctrl = new TabControl();
			Controls.Clear();
			
			tctrl.Dock       = DockStyle.Fill;
			tctrl.Alignment  = TabAlignment.Bottom;
			
			switch (content) {
				case WindowContent.Browser:
					BrowserPane htmlview = new BrowserPane(this);
					this.Controls.Add(htmlview);
					hastextarea = false;
					editable    = htmlview;
					return;
				case WindowContent.CSFile: 
					ISdDisplayModule info = ModuleManager.GetModulePerLanguageName(language).DisplayModule;
					if (info != null) {
						WindowContentPane pane = info.GetContentPane(mainwindow, this);
						if (pane == null)
							goto default;
						this.Controls.Add(pane.Pane);
						editable     = pane.ISdEditable;
						hastextarea  = pane.ISdEditable is TextAreaControl;
					} else 
						goto default;
					break;
				default:
					textcontrol = new TextAreaControl(mainwindow);
					if (language != null) {
						textcontrol.Buffer.SetHighlightingTo(language);
					}
					textcontrol.Dock   = DockStyle.Fill;
					this.Controls.Add(textcontrol);
					hastextarea = true;
					editable = textcontrol;
					break;
			}
			
			if (editable != null)
				editable.Changed += new EventHandler(ChangeEvent);
//			setborder = false;
		}
		
		void ChangeEvent(object sender, EventArgs e)
		{
			Dirty = true;
			RenameEvent();
		}
		
		void RenameEvent()
		{
			string newText = "";
			if (Untitled) {
				if (mynum == -1) 
					mynum = ++untitledNum[(int)content];
				
				switch (content) {
					case WindowContent.Text:
						newText = "Text";
						break; 
					case WindowContent.CSFile:
						if (defaultname == null) {
							Module info = ModuleManager.GetModulePerLanguageName(language);
							if (info != null) {
								newText = info.Language;
							}  else
								newText = "Source";
						} else
							newText = Path.GetFileNameWithoutExtension(defaultname);
						break;
				}
				Console.WriteLine("New text : " + newText);
				newText = Path.GetFileNameWithoutExtension(newText) + mynum + Path.GetExtension(newText);
				Console.WriteLine("new New text : " + newText);
			} else {
				newText = (WindowState == FormWindowState.Minimized) ? Path.GetFileName(filename) : filename;
			}
			// file changed ?
			if (Dirty)
				newText += "*";
			
			if (writeprotected)
				newText += "+";
			
			
			// update text, if neccesarry
			if (Text != newText) {
				Text = newText;
				tabitem.Text = Path.GetFileName(newText);
			}
		}
		
		protected override void OnClosed(EventArgs e)
		{
			base.OnClosed(e);
			mainwindow.OpenFileTab.TabPages.Remove(tabitem);
			Dispose();
		}
		
		public void LoadContent(string filename)
		{
			Debug.Assert(editable != null, "SharpDevelop.Gui.Window.ContentWindow.LoadContent(string filename) : no editable defined (editable == null)");

			this.filename = filename;
				
			if (Content == WindowContent.Browser) {
				editable.LoadFile(filename);
			} else {
				
				FileAttributes attribute = File.GetAttributes(filename);
				writeprotected = (attribute & FileAttributes.ReadOnly) != 0;
				if (hastextarea)
					TextArea.Buffer.SetHighlightingFor(filename);
				
				editable.LoadFile(filename);
				editable.WriteProtected = writeprotected;
			}
			Dirty    = false;
			Untitled = false;
			RenameEvent();
				
			GetIcon(filename); // correct ?
		}
		
		public void SaveContent(string filename)
		{
			if (filename == null)
				throw new ArgumentNullException("ContentWindow.SaveContent(string filename) : filename can't be null");
			
			this.filename = filename;
			writeprotected = false;
			
			Debug.Assert(editable != null, "SharpDevelop.Gui.Window.ContentWindow.SaveContent(string filename) : no editable defined (editable == null)");
			
			editable.SaveFile(filename);
			
			if (HasTextArea)
				TextArea.TextAreaPainter.Buffer.ReadOnly = writeprotected;
			
			untitled = false;
			Dirty    = false;
			RenameEvent();
		}
		
		protected override void OnResize(EventArgs e)
		{
			base.OnResize(e);
			RenameEvent();
		}
		
		public void SaveContent()
		{
			if (Untitled)
				SaveContentAs();
			else
				SaveContent(filename);
		}
		
		public void SaveContentAs()
		{
			SaveFileDialog fdiag = new SaveFileDialog();
			fdiag.OverwritePrompt = true;
			fdiag.AddExtension    = true;
			fdiag.Filter          = Option.GetProperty("SharpDevelop.Action.MenuAction.OpenFile.FileFilter", "").ToString();
			
			if (fdiag.ShowDialog() == DialogResult.OK) {
				SaveContent(fdiag.FileName);
				MessageBox.Show(fdiag.FileName, "File saved", MessageBoxButtons.OK);
			}
		}
	}
}

⌨️ 快捷键说明

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