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

📄 infoview.cs

📁 全功能c#编译器
💻 CS
📖 第 1 页 / 共 2 页
字号:
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Georg Brandl" email="g.brandl@gmx.net"/>
//     <version value="$version"/>
// </file>

using System;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;

using ICSharpCode.SharpDevelop.Gui.HtmlControl;

using ICSharpCode.SharpDevelop.Services;
using SharpDevelop.Internal.Parser;
using ICSharpCode.Core.Services;
using SA = ICSharpCode.SharpAssembly.Assembly;

namespace ICSharpCode.SharpDevelop.AddIns.AssemblyScout
{
	public class InfoView : UserControl
	{
		GradientLabel cap = new GradientLabel();
		Label typ = new Label();
		LinkLabel back = new LinkLabel();
		
		HtmlControl ht = new HtmlControl();
		Panel pan = new Panel();
		
		AssemblyTree tree;
		IParserService parserService;
		IAmbience ambience;
		PropertyService propertyService;
		
		ArrayList references = new ArrayList();
		string cssPath;
		string imgPath;
		string resPath;
		
		public InfoView(AssemblyTree tree)
		{
			ResourceService resourceService = (ResourceService)ServiceManager.Services.GetService(typeof(ResourceService));
			AmbienceService ambienceService = (AmbienceService)ServiceManager.Services.GetService(typeof(AmbienceService));
			propertyService = (PropertyService)ServiceManager.Services.GetService(typeof(PropertyService));			
			
			ambience = ambienceService.CurrentAmbience;
			
			this.tree = tree;
			
			imgPath = Path.Combine(propertyService.ConfigDirectory, "tempPicture.png");
			cssPath = Path.Combine(propertyService.ConfigDirectory, "tempStylesheet.css");
			resPath = Path.Combine(propertyService.ConfigDirectory, "tempResource");
			
			Color col = SystemColors.Control;
			string color = "#" + col.R.ToString("X") + col.G.ToString("X") + col.B.ToString("X");
			
			StreamWriter sw = new StreamWriter(cssPath, false);
			sw.Write(@"body { margin: 0px; border: 0px; overflow: hidden; padding: 0px;
							  background-color: " + color + @"; 
							  background-image: url(" + imgPath + @"); 
							  background-position: bottom right; 
							  background-repeat: no-repeat }
							  
					p   { font: 8pt Tahoma }
					div { margin: 0px; width: 100% }
					p.bottomline { font: 8pt Tahoma; border-bottom: 1px solid black; margin-bottom: 3px }
					p.docmargin  { font: 8pt Tahoma; margin-top: 0px; margin-bottom: 0px; padding-left: 8px; padding-right: 8px; padding-bottom: 3px; border-bottom: 1px solid black }

					a   { font: 8pt Tahoma; text-decoration: none; color: blue}
					a:visited { color: blue }
					a:active  { color: blue }
					a:hover   { color: red; text-decoration: underline }");
			sw.Close();
			
			cap.Location  = new Point(0, 0);
			cap.Size      = new Size(Width, 32);
			cap.Text      = tree.ress.GetString("ObjectBrowser.Welcome");
			cap.Font      = new Font("Tahoma", 14);
			cap.BackColor = SystemColors.ControlLight;
			cap.TextAlign = ContentAlignment.MiddleLeft;
			cap.Anchor    = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
						
			string backt  = tree.ress.GetString("ObjectBrowser.Back");
			back.Size     = new Size(40, 16);
			back.Location = new Point(Width - back.Width, 44);
			back.Text     = backt;
			back.TextAlign = ContentAlignment.TopRight;
			back.Anchor   = AnchorStyles.Top | AnchorStyles.Right;
			back.Links.Add(0, backt.Length);
			back.LinkClicked += new LinkLabelLinkClickedEventHandler(back_click);
			
			typ.Location  = new Point(0, 44);
			typ.Size      = new Size(Width - back.Width, 16);
			typ.Font      = new Font(Font, FontStyle.Bold);
			typ.Text      = tree.ress.GetString("ObjectBrowser.WelcomeText");
			typ.TextAlign = ContentAlignment.TopLeft;
			typ.Anchor    = cap.Anchor;
			
			ht = new HtmlControl();
			//ht.Size = new Size(20, 20);
			//ht.Location = new Point(20, 20);
			ht.BeforeNavigate += new BrowserNavigateEventHandler(HtmlControlBeforeNavigate);
			CreateImage(resourceService.GetIcon("Icons.16x16.Class").ToBitmap());
			ht.CascadingStyleSheet = cssPath;
			string html = RenderHead() + tree.ress.GetString("ObjectBrowser.Info.SelectNode") + RenderFoot();
			ht.Html = html;
			
			pan.Location   = new Point(0, 72);
			pan.DockPadding.Left = 10;
			pan.DockPadding.Bottom = 75;
			pan.Size       = new Size(Width, Height - ht.Top);
			pan.Anchor     = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
			pan.Controls.Add(ht);
			
			ht.Dock = DockStyle.Fill;
			
			Controls.AddRange(new Control[] {
				cap, typ, back, pan
			});
			
			Dock = DockStyle.Fill;
			tree.AfterSelect += new TreeViewEventHandler(SelectNode);
			
			parserService = (IParserService)ServiceManager.Services.GetService(typeof(IParserService));
		}
		
		~InfoView() {
			System.IO.File.Delete(imgPath);
			System.IO.File.Delete(cssPath);
		}
		
		void HtmlControlBeforeNavigate(object sender, BrowserNavigateEventArgs e)
		{
			e.Cancel = true;
			
			try {
				string url = e.Url;
				int refnr  = Int32.Parse(url.Substring(5, url.Length - 6));
				object obj = references[refnr];
				
				if (obj is IClass) {
					// Go To Type
					tree.GoToType((IClass)obj);
					// try {
					// 	tree.SelectedNode.Expand();
					// } catch {}
				} else if (obj is AssemblyTree.RefNodeAttribute) {
					// Open Assembly Reference
					tree.OpenAssemblyByName((AssemblyTree.RefNodeAttribute)obj);
				} else if (obj is SaveResLink) {
					SaveResLink link = (SaveResLink)obj;
					tree.SaveResource(link.Asm, link.Name);
				} else if (obj is NamespaceLink) {
					NamespaceLink ns = (NamespaceLink)obj;
					tree.GoToNamespace(ns.Asm, ns.Name);
				}
				
			} catch { 
				IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
				messageService.ShowError("Something failed following this link.");
			}
		}
		
		string RenderHead()
		{
			return "<div><p>";
		}
		
		string RenderFoot()
		{
			return "</div>";
		}
				
		void SelectNode(object sender, TreeViewEventArgs e)
		{
			AssemblyTreeNode node = (AssemblyTreeNode)e.Node;
			cap.Text = node.Text;
			typ.Text = node.Type.ToString();
			
			references.Clear();
			
			ambience.LinkArrayList = references;
			ambience.ConversionFlags = ConversionFlags.AssemblyScoutDefaults;
						
			CreateImage(tree.ImageList.Images[node.ImageIndex]);
			ht.Cursor = Cursors.Default;
			StringBuilder htmlSB = new StringBuilder(RenderHead());
			
			try {
			
				switch(node.Type) {
					case NodeType.Assembly:
						htmlSB.Append(GetAssemblyInfo((SA.SharpAssembly)node.Attribute));
						break;
					case NodeType.Library:
						htmlSB.Append(GetLibInfo((SA.SharpAssembly)node.Attribute));
						break;
					case NodeType.Reference:
						htmlSB.Append(GetRefInfo((AssemblyTree.RefNodeAttribute)node.Attribute));
						break;
					case NodeType.Resource:
						htmlSB.Append(GetResInfo((SA.SharpAssembly)node.Attribute, node.Text));
						break;
					case NodeType.SingleResource:
						htmlSB.Append(GetSingleResInfo(node.Attribute, node.Text));
						break;
					case NodeType.Folder:
						htmlSB.Append(GetFolderInfo(node.Text));
						break;
					case NodeType.Namespace:
						htmlSB.Append(GetNSInfo((SA.SharpAssembly)node.Attribute));
						break;
					case NodeType.SubTypes:
						htmlSB.Append(GetSubInfo());
						break;
					case NodeType.SuperTypes:
						htmlSB.Append(GetSuperInfo());
						break;
					case NodeType.Link:
						htmlSB.Append(GetLinkInfo((IClass)node.Attribute));
						break;
					case NodeType.Type:
						htmlSB.Append(GetTypeInfo((IClass)node.Attribute));
						break;
					case NodeType.Event:
						htmlSB.Append(GetEventInfo((IEvent)node.Attribute));
						break;
					case NodeType.Field:
						htmlSB.Append(GetFieldInfo((IField)node.Attribute));
						break;
					case NodeType.Constructor:
					case NodeType.Method:
						htmlSB.Append(GetMethodInfo((IMethod)node.Attribute));
						break;
					case NodeType.Property:
						htmlSB.Append(GetPropInfo((IProperty)node.Attribute));
						break;
					default:
						break;
				}
			} catch(Exception ex) {
				htmlSB.Append("<p class='bottomline'>" + tree.ress.GetString("ObjectBrowser.Info.CollectError") + "<p>" + ex.ToString().Replace("\n", "<br>"));
			}
			
			htmlSB.Append(RenderFoot());
			
			ht.Html = htmlSB.ToString();
		}
		
		string GetLinkInfo(IClass type)
		{
			StringBuilder text = new StringBuilder(ln(references.Add(type), RT("GotoType")));
			text.Append("<p>");
			text.Append(RT("LinkedType"));
			text.Append(" ");
			text.Append(GetInAsm((SA.SharpAssembly)type.DeclaredIn));
			return text.ToString();			
		}
		
		string GetSubInfo()
		{
			return RT("SubInfo");
		}
		
		string GetSuperInfo()
		{
			return RT("SuperInfo");
		}
		
		string GetNSInfo(SA.SharpAssembly asm)
		{
			return GetInAsm(asm);
		}
		
		string GetFolderInfo(string folder)
		{
			if (folder == tree.ress.GetString("ObjectBrowser.Nodes.Resources")) 
				return RT("ResFInfo");
			else if (folder == tree.ress.GetString("ObjectBrowser.Nodes.References"))
				return RT("RefFInfo");
			else if (folder == tree.ress.GetString("ObjectBrowser.Nodes.Modules"))
				return RT("ModFInfo");
			else
				return RT("NoInfo");
		}
		
		string GetLibInfo(SA.SharpAssembly asm)
		{
			return String.Concat(RT("LibInfo"), "<p>", GetInAsm(asm));
		}
		
		string GetRefInfo(AssemblyTree.RefNodeAttribute asn)
		{
			string text = String.Format(RT("RefInfo"), asn.RefName.Name, asn.RefName.FullName, asn.RefName.Version.ToString(), "");
			return String.Concat(text, ln(references.Add(asn), RT("OpenRef")));
		}
		
		string GetResInfo(SA.SharpAssembly asm, string name)
		{
			long size = 0;
			try {
				size = asm.GetManifestResourceSize(name);
			} catch {}
			
			StringBuilder text = new StringBuilder(String.Format(RT("ResInfo"), name, size));
			text.Append(GetInAsm(asm));
			
			text.Append("<p>");
			text.Append(ln(references.Add(new SaveResLink(asm, name)), RT("SaveRes")));
			
			if (propertyService.GetProperty("AddIns.AssemblyScout.ShowResPreview", true) == false) {
				return text.ToString();
			}
			
			try {
				if (name.ToLower().EndsWith(".bmp") || name.ToLower().EndsWith(".gif") 
					|| name.ToLower().EndsWith(".png") || name.ToLower().EndsWith(".jpg")) {
					byte[] res = asm.GetManifestResource(name);
					FileStream fstr = new FileStream(resPath, FileMode.Create);
					BinaryWriter wr = new BinaryWriter(fstr);
					wr.Write(res);
					fstr.Close();
					
					text.Append("<p>Preview:<p>");
					text.Append("<img src=\"");
					text.Append(resPath);
					text.Append("\">");
				}
				if (name.ToLower().EndsWith(".tif")) {
					byte[] res = asm.GetManifestResource(name);
					Image tifImg = Image.FromStream(new MemoryStream(res));
					tifImg.Save(resPath, ImageFormat.Bmp);
					
					text.Append("<p>Preview:<p>");
					text.Append("<img src=\"");
					text.Append(resPath);
					text.Append("\">");
				}
				if (name.ToLower().EndsWith(".ico")) {
					byte[] res = asm.GetManifestResource(name);
					Icon icon = new Icon(new MemoryStream(res));
					using (Bitmap b = new Bitmap(icon.Width, icon.Height)) {
						Graphics g = Graphics.FromImage(b);
						g.FillRectangle(SystemBrushes.Control, 0, 0, b.Width, b.Height);
						g.DrawIcon(icon, 0, 0);
						            
						b.Save(resPath, System.Drawing.Imaging.ImageFormat.Png);
					}
					text.Append("<p>Preview:<p>");
					text.Append("<img src=\"");
					text.Append(resPath);
					text.Append("\">");
				}
				if (name.ToLower().EndsWith(".cur")) {
					byte[] res = asm.GetManifestResource(name);
					Cursor cursor = new Cursor(new MemoryStream(res));
					
					using (Bitmap b = new Bitmap(cursor.Size.Width, cursor.Size.Height)) {
						Graphics g = Graphics.FromImage(b);
						g.FillRectangle(SystemBrushes.Control, 0, 0, b.Width, b.Height);
						cursor.Draw(g, new Rectangle(0, 0, 32, 32));
						            
						b.Save(resPath, System.Drawing.Imaging.ImageFormat.Png);
					}
					
					text.Append("<p>Preview:<p>");
					text.Append("<img src=\"");
					text.Append(resPath);
					text.Append("\">");
				}
				if (name.ToLower().EndsWith(".txt") || name.ToLower().EndsWith(".xml") ||
				    name.ToLower().EndsWith(".xsd") || name.ToLower().EndsWith(".htm") ||
				    name.ToLower().EndsWith(".html") || name.ToLower().EndsWith(".xshd") ||
				    name.ToLower().EndsWith(".xsl") || name.ToLower().EndsWith(".txt")) {
					byte[] res = asm.GetManifestResource(name);
					string utf = System.Text.UTF8Encoding.UTF8.GetString(res);
					
					text.Append("<p>Preview:<br>");
					text.Append("<textarea style='border: 1px solid black; width: 300px; height: 400px; font: 8pt Tahoma'>");
					text.Append(utf);
					text.Append("</textarea>");
				}
			} catch {}
			
			return text.ToString();
		}
		
		string GetSingleResInfo(object singleRes, string name)
		{
			int len = name.Length;
			if (name.LastIndexOf(":") != -1) len = name.LastIndexOf(":");
			StringBuilder ret = new StringBuilder("Name: ");
			ret.Append(name.Substring(0, len));
			ret.Append("<p>");
			
			if (singleRes != null) {
				ret.Append("Type: ");
				ret.Append(singleRes.GetType().Name);
				ret.Append("<p>");
				ret.Append("Value:<br>");

⌨️ 快捷键说明

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