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

📄 htmlhelp2service.cs

📁 c#源代码
💻 CS
字号:
/* ***********************************************************
 *
 * Help 2.0 Environment for SharpDevelop
 * Base Help 2.0 Service
 * Copyright (c) 2005, Mathias Simmack. All rights reserved.
 *
 * ********************************************************* */
namespace HtmlHelp2Service
{
	using System;
	using System.Windows.Forms;
	using System.Xml;
	using ICSharpCode.Core.Services;
	using MSHelpServices;
	using HtmlHelp2;

	public class HtmlHelp2Environment : AbstractService
	{
		static string help2EnvironmentFile = "help2environment.xml";
		static Guid TocGuid                = new Guid("314111B2-A502-11D2-BBCA-00C04F8EC294");
		static Guid IndexGuid              = new Guid("314111CC-A502-11D2-BBCA-00C04F8EC294");
		static Guid QueryGuid              = new Guid("31411193-A502-11D2-BBCA-00C04F8EC294");
		HxSession session                  = null;
		IHxRegFilterList namespaceFilters  = null;
		IHxQuery fulltextSearch            = null;
		IHxQuery dynamicHelp               = null;
		string fidalgoNamespaceName        = "Fidalgo";
		string currentSelectedFilterQuery  = "";
		string currentSelectedFilterName   = "";
		string defaultPage                 = "about:blank";
		string searchPage                  = "http://msdn.microsoft.com";
		bool dynamicHelpIsBusy             = false;

		public override void InitializeService()
		{
			this.LoadHelp2Config();
			this.fidalgoNamespaceName = Help2RegistryWalker.GetFirstNamespace(this.fidalgoNamespaceName);
			InitializeNamespace(this.fidalgoNamespaceName);
		}

		#region Properties
		public bool Help2EnvironmentIsReady
		{
			get
			{
				return this.session != null;
			}
		}

		public string CurrentSelectedNamespace
		{
			get
			{
				return this.fidalgoNamespaceName;
			}
		}

		public string CurrentFilterQuery
		{
			get
			{
				return currentSelectedFilterQuery;
			}
		}

		public string CurrentFilterName
		{
			get
			{
				return currentSelectedFilterName;
			}
		}

		public string DefaultPage
		{
			get
			{
				return defaultPage;
			}
		}

		public string SearchPage
		{
			get
			{
				return searchPage;
			}
		}

		public IHxQuery FTS
		{
			get
			{
				return fulltextSearch;
			}
		}

		public bool DynamicHelpIsBusy
		{
			get
			{
				return dynamicHelpIsBusy;
			}
		}
		#endregion

		#region Namespace Functions
		private void LoadHelp2Config()
		{
			try
			{
				PropertyService propertyService = (PropertyService)ServiceManager.Services.GetService(typeof(PropertyService));

				XmlDocument xmldoc              = new XmlDocument();
				xmldoc.Load(propertyService.ConfigDirectory + help2EnvironmentFile);

				XmlNode node                    = xmldoc.SelectSingleNode("/help2environment/collection");
				if(node != null) this.fidalgoNamespaceName = node.InnerText;
			}
			catch {}
		}

		public void ReloadNamespace()
		{
			this.LoadHelp2Config();
			this.fidalgoNamespaceName = Help2RegistryWalker.GetFirstNamespace(this.fidalgoNamespaceName);
			this.InitializeNamespace(this.fidalgoNamespaceName);
			OnNamespaceReloaded(EventArgs.Empty);
		}

		private void InitializeNamespace(string namespaceName)
		{
			StringParserService sps  = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));

			if(namespaceName == null || namespaceName == "")
				return;

			if(session != null) session = null;

			HtmlHelp2Dialog initDialog = new HtmlHelp2Dialog();
			try
			{
				currentSelectedFilterQuery = "";
				currentSelectedFilterName  = "";

				initDialog.Text            = sps.Parse("${res:AddIns.HtmlHelp2.HelpUpdateCaption}");
				initDialog.ActionLabel     = sps.Parse("${res:AddIns.HtmlHelp2.HelpUpdateInProgress}");
				initDialog.Show();
				Application.DoEvents();

				session                    = new HxSession();
				session.Initialize(String.Format("ms-help://{0}", namespaceName), 0);
				namespaceFilters           = session.GetFilterList();

				this.ReloadDefaultPages();
				this.ReloadFTSSystem();
				this.ReloadDynamicHelpSystem();
			}
			catch
			{
				session = null;
			}
			finally
			{
				initDialog.Dispose();				
			}
		}

		private void ReloadFTSSystem()
		{
			try
			{
				fulltextSearch = (IHxQuery)session.GetNavigationInterface("!DefaultFullTextSearch", currentSelectedFilterQuery, ref QueryGuid);
			}
			catch
			{
				fulltextSearch = null;
			}
		}

		private void ReloadDynamicHelpSystem()
		{
			try
			{
				dynamicHelp = (IHxQuery)session.GetNavigationInterface("!DefaultContextWindowIndex", currentSelectedFilterQuery, ref QueryGuid);
			}
			catch
			{
				dynamicHelp = null;
			}
		}

		private void ReloadDefaultPages()
		{
			try
			{
				defaultPage = this.GetDefaultPage("HomePage", "DefaultPage", "about:blank");
				searchPage  = this.GetDefaultPage("SearchHelpPage", "SearchWebPage", "http://msdn.microsoft.com");
			}
			catch {}
		}

		private string GetDefaultPage(string pageName, string alternatePageName, string defaultValue)
		{
			string resultString = "";

			try
			{
				IHxIndex namedUrlIndex = (IHxIndex)session.GetNavigationInterface("!DefaultNamedUrlIndex", "", ref IndexGuid);
				IHxTopicList topics = null;

				topics = namedUrlIndex.GetTopicsFromString(pageName, 0);

				if(topics.Count == 0 && (alternatePageName != null && alternatePageName != ""))
				{
					topics = namedUrlIndex.GetTopicsFromString(alternatePageName, 0);
				}

				if(topics.Count > 0) resultString = topics.ItemAt(1).URL;
				else resultString = defaultValue;

				return resultString;
			}
			catch
			{
				return "";
			}
		}

		public IHxHierarchy GetTocHierarchy(string filterQuery)
		{
			try
			{
				IHxHierarchy defaultToc = (IHxHierarchy)session.GetNavigationInterface("!DefaultTOC", filterQuery, ref TocGuid);
				return defaultToc;
			}
			catch
			{
				return null;
			}
		}

		public IHxIndex GetIndex(string filterQuery)
		{
			try
			{
				IHxIndex defaultIndex = (IHxIndex)session.GetNavigationInterface("!DefaultKeywordIndex", filterQuery, ref IndexGuid);
				return defaultIndex;
			}
			catch
			{
				return null;
			}
		}

		public void BuildFilterList(ComboBox filterCombobox)
		{
			StringParserService sps  = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));

			filterCombobox.Items.Clear();
			filterCombobox.BeginUpdate();

			try
			{
				foreach(IHxRegFilter filter in namespaceFilters)
				{
					string filterName = (string)filter.GetProperty(HxRegFilterPropId.HxRegFilterName);
					filterCombobox.Items.Add(filterName);
					if(currentSelectedFilterName == "") currentSelectedFilterName = filterName;
				}
//				for(int i = 1; i <= namespaceFilters.Count; i++)
//				{
//					IHxRegFilter filter = namespaceFilters.ItemAt(i);
//					string filterName   = (string)filter.GetProperty(HxRegFilterPropId.HxRegFilterName);
//					filterCombobox.Items.Add(filterName);
//
//					if(currentSelectedFilterName == "" && i == 1) {
//						currentSelectedFilterName = filterName;
//					}
//				}

				if(namespaceFilters.Count == 0)
					filterCombobox.Items.Add(sps.Parse("${res:AddIns.HtmlHelp2.DefaultEmptyFilter}"));

				if(currentSelectedFilterName == "") filterCombobox.SelectedIndex = 0;
				else filterCombobox.SelectedIndex = filterCombobox.Items.IndexOf(currentSelectedFilterName);
			}
			catch {}

			filterCombobox.EndUpdate();
		}

		public string FindFilterQuery(string filterName)
		{
			if(String.Compare(filterName, currentSelectedFilterName) == 0)
			{
				return currentSelectedFilterQuery;
			}

			try
			{
				IHxRegFilter filter        = namespaceFilters.FindFilter(filterName);
				currentSelectedFilterQuery = (string)filter.GetProperty(HxRegFilterPropId.HxRegFilterQuery);
				currentSelectedFilterName  = filterName;
				OnFilterQueryChanged(EventArgs.Empty);

				this.ReloadFTSSystem();
				this.ReloadDynamicHelpSystem();
				this.ReloadDefaultPages();

				return currentSelectedFilterQuery;
			}
			catch
			{
				return "";
			}
		}

		public IHxTopicList GetMatchingTopicsForDynamicHelp(string searchTerm)
		{
			if(dynamicHelpIsBusy) return null;

			try
			{
				dynamicHelpIsBusy = true;
				IHxTopicList topics = ((IHxIndex)dynamicHelp).GetTopicsFromString(searchTerm, 0);
				return topics;
			}
			finally
			{
				dynamicHelpIsBusy = false;
			}
		}
		#endregion

		#region Event Handling
		public event EventHandler FilterQueryChanged;
		public event EventHandler NamespaceReloaded;

		protected virtual void OnFilterQueryChanged(EventArgs e)
		{
			if(FilterQueryChanged != null)
			{
				FilterQueryChanged(this, e);
			}
		}

		protected virtual void OnNamespaceReloaded(EventArgs e)
		{
			if(NamespaceReloaded != null)
			{
				NamespaceReloaded(this, e);
			}
		}
		#endregion
	}
}

⌨️ 快捷键说明

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