Click here to Skip to main content
15,886,137 members
Articles / Programming Languages / C#

Check Help Links Tool

Rate me:
Please Sign up or sign in to vote.
4.97/5 (13 votes)
6 Oct 2005CPOL6 min read 85.8K   17.3K   34  
A tool to check links across merged help (CHM) files.
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

using mshtml;

using HtmlHelp;
using HtmlHelp.ChmDecoding;

using Common;

namespace CheckHelpLinks
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ToolBarButton toolBarButtonFileOpen;
		private System.Windows.Forms.MenuItem menuItemFile;
		private System.Windows.Forms.MenuItem menuItemFileOpen;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.MenuItem menuItemFileExit;
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.ToolBar toolBar;
		private System.Windows.Forms.ImageList imageList;
		private System.Windows.Forms.ImageList imageListState;
		private System.Windows.Forms.ToolBarButton toolBarButtonWork;
		private System.Windows.Forms.ToolBarButton toolBarSeparator1;
		private System.Windows.Forms.TreeView _TreeView;
		private System.Windows.Forms.ToolBarButton toolBarButtonViewRefresh;
		private System.Windows.Forms.MenuItem menuItemFileWork;
		private System.Windows.Forms.MenuItem menuItemViewRefresh;
		private System.Windows.Forms.MenuItem menuSeparator1;
		private System.Windows.Forms.MenuItem menuItemView;
		private System.Windows.Forms.MenuItem menuItemViewBrowser;
		private System.Windows.Forms.MenuItem menuItemViewExpand;
		private System.Windows.Forms.MenuItem menuItemViewPage;
		private System.Windows.Forms.ToolBarButton toolBarButtonFast;
		private System.Windows.Forms.MenuItem menuItemFileExport;
		private System.Windows.Forms.MenuItem menuSeparator2;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItemEditFind;
		private System.Windows.Forms.MenuItem menuItemEditFindNext;
		private System.Windows.Forms.MenuItem menuItemEditFindPrevious;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItemModeFast;
		private System.Windows.Forms.MenuItem menuItemModeSlow;
		private System.Windows.Forms.MenuItem menuItemEditCopy;
		private System.Windows.Forms.MenuItem menuItem4;

		private static string[] _Args = null;

		private RecentFileList _RecentFileList = new RecentFileList();
		private string _Filename = null;
		private bool _Refresh = false;

		private HtmlHelpSystem _Reader = null;
		private All _All = null;
		private System.Windows.Forms.MenuItem menuItemViewSource;

		private Find _Find = null;

		public Form1()
		{
			InitializeComponent();

			Settings.DefaultRegistrySettingsPath = Global.RegistrySettingsPath;

			_RecentFileList.RegistryKey = Global.RegistryRecentFilesPath;
			_RecentFileList.FileMenu = menuItemFile;
			_RecentFileList.FileMenuPosition = 6;
			_RecentFileList.OnMenuClick += new RecentFileList.MenuClickEventHandler( OnRecentFileClick );

			Global.FastParse = Global.FastMode.Bool;
			SetFastToolTip();
			SetFastChecks();

			_Find = new Find();
			_Find.LoadFromRegistry();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.menuItemFile = new System.Windows.Forms.MenuItem();
			this.menuItemFileOpen = new System.Windows.Forms.MenuItem();
			this.menuItemViewRefresh = new System.Windows.Forms.MenuItem();
			this.menuItemFileWork = new System.Windows.Forms.MenuItem();
			this.menuSeparator1 = new System.Windows.Forms.MenuItem();
			this.menuItemFileExport = new System.Windows.Forms.MenuItem();
			this.menuSeparator2 = new System.Windows.Forms.MenuItem();
			this.menuItemFileExit = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItemEditCopy = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItemEditFind = new System.Windows.Forms.MenuItem();
			this.menuItemEditFindNext = new System.Windows.Forms.MenuItem();
			this.menuItemEditFindPrevious = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItemModeFast = new System.Windows.Forms.MenuItem();
			this.menuItemModeSlow = new System.Windows.Forms.MenuItem();
			this.menuItemView = new System.Windows.Forms.MenuItem();
			this.menuItemViewExpand = new System.Windows.Forms.MenuItem();
			this.menuItemViewPage = new System.Windows.Forms.MenuItem();
			this.menuItemViewBrowser = new System.Windows.Forms.MenuItem();
			this.menuItemViewSource = new System.Windows.Forms.MenuItem();
			this.toolBar = new System.Windows.Forms.ToolBar();
			this.toolBarButtonFileOpen = new System.Windows.Forms.ToolBarButton();
			this.toolBarButtonViewRefresh = new System.Windows.Forms.ToolBarButton();
			this.toolBarButtonWork = new System.Windows.Forms.ToolBarButton();
			this.toolBarSeparator1 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButtonFast = new System.Windows.Forms.ToolBarButton();
			this.imageList = new System.Windows.Forms.ImageList(this.components);
			this.imageListState = new System.Windows.Forms.ImageList(this.components);
			this._TreeView = new System.Windows.Forms.TreeView();
			this.SuspendLayout();
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuItemFile,
																					 this.menuItem1,
																					 this.menuItem2,
																					 this.menuItemView});
			// 
			// menuItemFile
			// 
			this.menuItemFile.Index = 0;
			this.menuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.menuItemFileOpen,
																						 this.menuItemViewRefresh,
																						 this.menuItemFileWork,
																						 this.menuSeparator1,
																						 this.menuItemFileExport,
																						 this.menuSeparator2,
																						 this.menuItemFileExit});
			this.menuItemFile.Text = "&File";
			this.menuItemFile.Popup += new System.EventHandler(this.menuItemFile_Popup);
			// 
			// menuItemFileOpen
			// 
			this.menuItemFileOpen.Index = 0;
			this.menuItemFileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
			this.menuItemFileOpen.Text = "&Open...";
			this.menuItemFileOpen.Click += new System.EventHandler(this.menuItemFileOpen_Click);
			// 
			// menuItemViewRefresh
			// 
			this.menuItemViewRefresh.Index = 1;
			this.menuItemViewRefresh.Shortcut = System.Windows.Forms.Shortcut.F5;
			this.menuItemViewRefresh.Text = "&Refresh";
			this.menuItemViewRefresh.Click += new System.EventHandler(this.menuItemViewRefresh_Click);
			// 
			// menuItemFileWork
			// 
			this.menuItemFileWork.Index = 2;
			this.menuItemFileWork.Shortcut = System.Windows.Forms.Shortcut.CtrlW;
			this.menuItemFileWork.Text = "&Work";
			this.menuItemFileWork.Click += new System.EventHandler(this.menuItemFileWork_Click);
			// 
			// menuSeparator1
			// 
			this.menuSeparator1.Index = 3;
			this.menuSeparator1.Text = "-";
			// 
			// menuItemFileExport
			// 
			this.menuItemFileExport.Index = 4;
			this.menuItemFileExport.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
			this.menuItemFileExport.Text = "E&xport Results...";
			this.menuItemFileExport.Click += new System.EventHandler(this.menuItemFileExport_Click);
			// 
			// menuSeparator2
			// 
			this.menuSeparator2.Index = 5;
			this.menuSeparator2.Text = "-";
			// 
			// menuItemFileExit
			// 
			this.menuItemFileExit.Index = 6;
			this.menuItemFileExit.Text = "E&xit";
			this.menuItemFileExit.Click += new System.EventHandler(this.menuItemFileExit_Click);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 1;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemEditCopy,
																					  this.menuItem4,
																					  this.menuItemEditFind,
																					  this.menuItemEditFindNext,
																					  this.menuItemEditFindPrevious});
			this.menuItem1.Text = "&Edit";
			// 
			// menuItemEditCopy
			// 
			this.menuItemEditCopy.Index = 0;
			this.menuItemEditCopy.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
			this.menuItemEditCopy.Text = "&Copy link";
			this.menuItemEditCopy.Click += new System.EventHandler(this.menuItemEditCopy_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 1;
			this.menuItem4.Text = "-";
			// 
			// menuItemEditFind
			// 
			this.menuItemEditFind.Index = 2;
			this.menuItemEditFind.Shortcut = System.Windows.Forms.Shortcut.CtrlF;
			this.menuItemEditFind.Text = "&Find...";
			this.menuItemEditFind.Click += new System.EventHandler(this.menuItemEditFind_Click);
			// 
			// menuItemEditFindNext
			// 
			this.menuItemEditFindNext.Index = 3;
			this.menuItemEditFindNext.Shortcut = System.Windows.Forms.Shortcut.F3;
			this.menuItemEditFindNext.Text = "Find &Next";
			this.menuItemEditFindNext.Click += new System.EventHandler(this.menuItemEditFindNext_Click);
			// 
			// menuItemEditFindPrevious
			// 
			this.menuItemEditFindPrevious.Index = 4;
			this.menuItemEditFindPrevious.Shortcut = System.Windows.Forms.Shortcut.ShiftF3;
			this.menuItemEditFindPrevious.Text = "Find &Previous";
			this.menuItemEditFindPrevious.Click += new System.EventHandler(this.menuItemEditFindPrevious_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 2;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemModeFast,
																					  this.menuItemModeSlow});
			this.menuItem2.Text = "&Mode";
			// 
			// menuItemModeFast
			// 
			this.menuItemModeFast.Index = 0;
			this.menuItemModeFast.Shortcut = System.Windows.Forms.Shortcut.CtrlQ;
			this.menuItemModeFast.Text = "&Fast";
			this.menuItemModeFast.Click += new System.EventHandler(this.menuItemModeFast_Click);
			// 
			// menuItemModeSlow
			// 
			this.menuItemModeSlow.Index = 1;
			this.menuItemModeSlow.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
			this.menuItemModeSlow.Text = "&Slow";
			this.menuItemModeSlow.Click += new System.EventHandler(this.menuItemModeSlow_Click);
			// 
			// menuItemView
			// 
			this.menuItemView.Index = 3;
			this.menuItemView.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.menuItemViewExpand,
																						 this.menuItemViewPage,
																						 this.menuItemViewBrowser,
																						 this.menuItemViewSource});
			this.menuItemView.Text = "&View";
			// 
			// menuItemViewExpand
			// 
			this.menuItemViewExpand.Index = 0;
			this.menuItemViewExpand.Shortcut = System.Windows.Forms.Shortcut.CtrlE;
			this.menuItemViewExpand.Text = "&Expand All";
			this.menuItemViewExpand.Click += new System.EventHandler(this.menuItemViewExpand_Click);
			// 
			// menuItemViewPage
			// 
			this.menuItemViewPage.Index = 1;
			this.menuItemViewPage.Shortcut = System.Windows.Forms.Shortcut.CtrlZ;
			this.menuItemViewPage.Text = "&Page";
			this.menuItemViewPage.Click += new System.EventHandler(this.menuItemViewPage_Click);
			// 
			// menuItemViewBrowser
			// 
			this.menuItemViewBrowser.Index = 2;
			this.menuItemViewBrowser.Shortcut = System.Windows.Forms.Shortcut.CtrlB;
			this.menuItemViewBrowser.Text = "&Browser";
			this.menuItemViewBrowser.Click += new System.EventHandler(this.menuItemViewBrowser_Click);
			// 
			// menuItemViewSource
			// 
			this.menuItemViewSource.Index = 3;
			this.menuItemViewSource.Shortcut = System.Windows.Forms.Shortcut.CtrlA;
			this.menuItemViewSource.Text = "&Source";
			this.menuItemViewSource.Click += new System.EventHandler(this.menuItemViewSource_Click);
			// 
			// toolBar
			// 
			this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																					   this.toolBarButtonFileOpen,
																					   this.toolBarButtonViewRefresh,
																					   this.toolBarButtonWork,
																					   this.toolBarSeparator1,
																					   this.toolBarButtonFast});
			this.toolBar.DropDownArrows = true;
			this.toolBar.ImageList = this.imageList;
			this.toolBar.Location = new System.Drawing.Point(0, 0);
			this.toolBar.Name = "toolBar";
			this.toolBar.ShowToolTips = true;
			this.toolBar.Size = new System.Drawing.Size(592, 28);
			this.toolBar.TabIndex = 9;
			this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
			// 
			// toolBarButtonFileOpen
			// 
			this.toolBarButtonFileOpen.ImageIndex = 0;
			this.toolBarButtonFileOpen.ToolTipText = "Open File";
			// 
			// toolBarButtonViewRefresh
			// 
			this.toolBarButtonViewRefresh.ImageIndex = 1;
			this.toolBarButtonViewRefresh.ToolTipText = "Refresh";
			// 
			// toolBarButtonWork
			// 
			this.toolBarButtonWork.ImageIndex = 2;
			this.toolBarButtonWork.ToolTipText = "Work";
			// 
			// toolBarSeparator1
			// 
			this.toolBarSeparator1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// toolBarButtonFast
			// 
			this.toolBarButtonFast.ImageIndex = 3;
			this.toolBarButtonFast.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
			this.toolBarButtonFast.ToolTipText = "Toggle Fast Parsing";
			// 
			// imageList
			// 
			this.imageList.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream")));
			this.imageList.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// imageListState
			// 
			this.imageListState.ImageSize = new System.Drawing.Size(16, 16);
			this.imageListState.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListState.ImageStream")));
			this.imageListState.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// _TreeView
			// 
			this._TreeView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this._TreeView.FullRowSelect = true;
			this._TreeView.HideSelection = false;
			this._TreeView.ImageList = this.imageListState;
			this._TreeView.Location = new System.Drawing.Point(8, 32);
			this._TreeView.Name = "_TreeView";
			this._TreeView.ShowRootLines = false;
			this._TreeView.Size = new System.Drawing.Size(576, 528);
			this._TreeView.TabIndex = 1;
			this._TreeView.DoubleClick += new System.EventHandler(this.TreeView_DoubleClick);
			this._TreeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.TreeView_AfterSelect);
			// 
			// Form1
			// 
			this.AllowDrop = true;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(592, 570);
			this.Controls.Add(this._TreeView);
			this.Controls.Add(this.toolBar);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.mainMenu;
			this.Name = "Form1";
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "CheckHelpLinks";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
			this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main( string[] args ) 
		{
			_Args = args;

			Application.EnableVisualStyles();
			Application.DoEvents();
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			FormPosition.Restore( this );

			ResetTreeView();

			User32.PostMessage( Handle, Global.WM_KickStart, 0, 0 );
		}

		protected override void WndProc( ref Message m )
		{
			if ( m.Msg == Global.WM_KickStart ) KickStart();

			base.WndProc( ref m );
		}

		private void KickStart()
		{
			Arguments args = new Arguments();

#if trueX
			_Args = new string[]
				{
//					"/o:C:\\dev\\CodeProject\\CheckHelpLinks\\Karen\\QVBasic.chm",
					"/o:\"C:\\dev\\CodeProject\\CheckHelpLinks\\Test\\Test.chm\"",
					"-w",
					"-x:'C:\\dev\\CodeProject\\CheckHelpLinks\\Test\\junk'",
					"/close",
				};
#endif

			args.Parse( _Args );

			if ( args.FastSet )
				if ( args.Fast )
					OnModeFast();
				else
					OnModeSlow();

			if ( args.Open != null )
			{
				if ( args.Open.Length > 0 ) LoadFromFile( args.Open );
				else
				{
					StringCollection a = _RecentFileList.RecentFiles;
					if ( a.Count > 0 ) LoadFromFile( a[ 0 ] );
				}
			}

			if ( _All != null )
			{
				if ( args.Work ) _All.Work();

				if ( args.Export != null )
				{
					Export export = new Export( _All );

					if ( args.Export.Length > 0 ) Export.Directory = args.Export;

					export.WriteAll();
				}
			}

			if ( args.Close ) Close();
		}

//-----------------------------------------------------------------------------
// Handlers

		private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if ( e.Button == toolBarButtonFileOpen             ) { OnFileOpen             (); return; }
			if ( e.Button == toolBarButtonViewRefresh          ) { OnViewRefresh          (); return; }
			if ( e.Button == toolBarButtonWork                 ) { OnWork                 (); return; }
			if ( e.Button == toolBarButtonFast                 ) { OnToggleFast           (); return; }

			Debug.Assert( false );
		}

		private void menuItemFileOpen_Click(object sender, System.EventArgs e)
		{
			OnFileOpen();
		}

		private void menuItemFileWork_Click(object sender, System.EventArgs e)
		{
			OnWork();
		}

		private void menuItemFileExport_Click(object sender, System.EventArgs e)
		{
			OnExport();
		}

		private void menuItemFileExit_Click(object sender, System.EventArgs e)
		{
			OnFileExit();
		}

		private void menuItemEditCopy_Click(object sender, System.EventArgs e)
		{
			OnEditCopy();
		}

		private void menuItemEditFind_Click(object sender, System.EventArgs e)
		{
			OnFind();
		}

		private void menuItemEditFindNext_Click(object sender, System.EventArgs e)
		{
			OnFindNext();
		}

		private void menuItemEditFindPrevious_Click(object sender, System.EventArgs e)
		{
			OnFindPrevious();
		}

		private void menuItemModeFast_Click(object sender, System.EventArgs e)
		{
			OnModeFast();
		}

		private void menuItemModeSlow_Click(object sender, System.EventArgs e)
		{
			OnModeSlow();
		}

		private void menuItemViewRefresh_Click(object sender, System.EventArgs e)
		{
			OnViewRefresh();
		}

		private void menuItemViewExpand_Click(object sender, System.EventArgs e)
		{
			OnViewExpand();
		}

		private void menuItemViewPage_Click(object sender, System.EventArgs e)
		{
			OnViewPage();
		}

		private void menuItemViewBrowser_Click(object sender, System.EventArgs e)
		{
			OnViewBrowser();
		}

		private void menuItemViewSource_Click(object sender, System.EventArgs e)
		{
			OnViewSource();
		}

//-----------------------------------------------------------------------------
// File

		private void menuItemFile_Popup(object sender, System.EventArgs e)
		{
			_RecentFileList.SetMenuItems();
		}

		private void OnRecentFileClick( object sender, RecentFileList.MenuClickEventArgs e )
		{
			if ( ! LoadFromFile( e.Filename ) )
			{
				_RecentFileList.RemoveFile( e.Filename );
				return;
			}
		}

		private void OnFileOpen()
		{
			string dir = Global.DefaultDirectory.String;

			OpenFileDialog dlg = new OpenFileDialog();
			dlg.InitialDirectory = dir;
			dlg.Filter = "Help Files (*.chm)|*.chm|All Files (*.*)|*.*";

			if ( dlg.ShowDialog( this ) != DialogResult.OK ) return;

			if ( ! LoadFromFile( dlg.FileName ) ) return;

			Global.DefaultDirectory.Set( Path.GetDirectoryName( dlg.FileName ) );
		}

		private bool LoadFromFile( string filename )
		{
			if ( ! File.Exists( filename ) ) return false;

			DateTime start = DateTime.Now;

			using ( new CWaitCursor() )
			{
				_Reader = new HtmlHelpSystem();
				_Reader.OpenFile( filename );

				_All = new All( filename, _Reader );

				_All.FillTreeView( _TreeView );
			}

			_RecentFileList.InsertFile( filename );
			_Filename = filename;
			Text = _Filename + " - CheckHelpLinks";

			if ( ! _Refresh ) Export.Directory = String.Empty;

			TimeSpan t = DateTime.Now - start;

//			MessageBox.Show( t.Seconds + "." + t.Milliseconds );

			return true;
		}

		private void OnFileExit()
		{
			Close();
		}

		private void OnViewRefresh()
		{
			if ( _Filename == null )
			{
				MessageBox.Show( "Please open a .chm file first !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
				return;
			}

			try
			{
				_Refresh = true;

				LoadFromFile( _Filename );
			}
			finally
			{
				_Refresh = false;
			}
		}

//-----------------------------------------------------------------------------
// Edit

		private void OnEditCopy()
		{
			TreeNode n = _TreeView.SelectedNode;
			if ( n == null ) n = _TreeView.Nodes[ 0 ];
			if ( n == null )
			{
				MessageBox.Show( "Please select a node first !", "Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
				return;
			}

			TreeItem item = n.Tag as TreeItem;
			if ( item == null )
			{
				MessageBox.Show( "Failed to copy link to clipboard", "Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
				return;
			}

			string link = item.Link;
			if ( link == null )
			{
				MessageBox.Show( "This node does not have a link", "Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
				return;
			}

			Clipboard.SetDataObject( link, true );

			MessageBox.Show( "Copied link to clipboard :" + Environment.NewLine + Environment.NewLine + link, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information );
		}

		private void OnFind()
		{
			DlgFind dlg = new DlgFind( _Find.Parameters );
			if ( dlg.ShowDialog() != DialogResult.OK ) return;

			_Find.SaveToRegistry();

			switch ( dlg.Direction )
			{
			case FindDirection.Next     : OnFindNext     (); break;
			case FindDirection.Previous : OnFindPrevious (); break;
			default: Debug.Assert( false ); break;
			}
		}

		private void OnFindNext()
		{
			TreeNode n = _TreeView.SelectedNode;
			if ( n == null ) n = _TreeView.Nodes[ 0 ];
			if ( n == null ) return;

			n = _Find.FindNext( n, FindDirection.Next );

			if ( n == null ) User32.MessageBeep( MB.IconAsterisk );

			if ( n != null ) _TreeView.SelectedNode = n;
		}

		private void OnFindPrevious()
		{
			TreeNode n = _TreeView.SelectedNode;
			if ( n == null ) n = _TreeView.Nodes[ 0 ];
			if ( n == null ) return;

			n = _Find.FindNext( n, FindDirection.Previous );

			if ( n == null ) User32.MessageBeep( MB.IconAsterisk );

			if ( n != null ) _TreeView.SelectedNode = n;
		}


//-----------------------------------------------------------------------------
// View

		private void OnViewExpand()
		{
			TreeNode n = _TreeView.SelectedNode;
			if ( n == null ) return;

			n.ExpandAll();
		}

#if false

		private void OnViewNextBroken()
		{
			TreeNode n = _TreeView.SelectedNode;
			if ( n == null ) n = _TreeView.Nodes[ 0 ];
			if ( n == null ) return;

			for (;;)
			{
				Application.DoEvents();
//				n = GetNextNode( n );
				if ( n == null ) return;
				if ( n.ImageIndex == (int) State.Broken ) break;
			}

			if ( n != null ) _TreeView.SelectedNode = n;
		}

		private void OnViewNextNotGood()
		{
			TreeNode n = _TreeView.SelectedNode;
			if ( n == null ) n = _TreeView.Nodes[ 0 ];
			if ( n == null ) return;;

			for (;;)
			{
				Application.DoEvents();
//				n = GetNextNode( n );
				if ( n == null ) return;
				if ( n.ImageIndex != (int) State.Good ) break;
			}

			if ( n != null ) _TreeView.SelectedNode = n;
		}

#endif

		private void OnViewPage()
		{
			TreeNode n = _TreeView.SelectedNode;
			if ( n == null ) return;

			object tag = n.Tag;
			if ( tag == null ) return;

			if ( ! ( tag is TreeItem ) ) return;
//			if ( ! ( tag is HtmlFileItem ) ) return;
			
			TreeItem item = ( TreeItem ) tag;
			string link = item.Link;
			if ( link == null ) return;

			DlgBrowser dlg = new DlgBrowser();
			dlg.Url = link;
			dlg.AutoClose = false;
			dlg.ShowDialog();
		}

		private void OnViewBrowser()
		{
			TreeNode n = _TreeView.SelectedNode;
			if ( n == null ) return;

			object tag = n.Tag;
			if ( tag == null ) return;

			if ( ! ( tag is TreeItem ) ) return;
//			if ( ! ( tag is HtmlFileItem ) ) return;
			
			TreeItem item = ( TreeItem ) tag;
			string link = item.Link;
			if ( link == null ) return;

			Process p = Process.Start( link );
		}

		private void OnViewSource()
		{
			TreeNode n = _TreeView.SelectedNode;
			if ( n == null ) return;

			object tag = n.Tag;
			if ( tag == null ) return;

			if ( ! ( tag is TreeItem ) ) return;
//			if ( ! ( tag is HtmlFileItem ) ) return;
			
			TreeItem item = ( TreeItem ) tag;
			string link = item.Link;
			if ( link == null ) return;

			string source = null;

//			if ( item is HtmlFileItem ) source = ( ( HtmlFileItem ) item ).Html;

			if ( item is LinkItem )
			{
				LinkItem linkItem = ( LinkItem ) item;
				if ( linkItem.LinkType != LinkType.Help ) return;

				ChmFileItem chmFileItem = _All.GetChmFile( linkItem.ChmFile );
				if ( chmFileItem == null ) return;

				HtmlFileItem htmlFileItem = chmFileItem.HtmlFileItem( linkItem.LinkFile );
				if ( htmlFileItem == null ) return;

				source = htmlFileItem.Html;
			}

			if ( source == null ) return;

			DlgSource dlg = new DlgSource();
			dlg.Link = link;
			dlg.Source = source;
			dlg.ShowDialog();
		}

//-----------------------------------------------------------------------------
// TreeView

		private void ResetTreeView()
		{
			_TreeView.Nodes.Clear();

			_TreeView.Nodes.Add( new TreeNode( "Unknown", (int) State.Unknown, (int) State.Unknown ) );
			_TreeView.Nodes.Add( new TreeNode( "Good"   , (int) State.Good   , (int) State.Good    ) );
			_TreeView.Nodes.Add( new TreeNode( "Broken" , (int) State.Broken , (int) State.Broken  ) );
		}


//-----------------------------------------------------------------------------
// TreeView handlers

		private void TreeView_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			object tag = e.Node.Tag;

			if ( tag is TreeItem ) ( ( TreeItem ) tag ).OnAfterSelect();
		}

		private void TreeView_DoubleClick( object sender, System.EventArgs e )
		{
			TreeNode n = _TreeView.SelectedNode;
			if ( n == null ) return;

			object tag = n.Tag;

			if ( tag is TreeItem ) ( ( TreeItem ) tag ).OnDoubleClick();
		}

//-----------------------------------------------------------------------------
// Work

		private void OnWork()
		{
			if ( _All == null )
			{
				MessageBox.Show( "Please open a .chm file first !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
				return;
			}

			TreeNode n = _TreeView.SelectedNode;
			if ( n == null )
			{
				MessageBox.Show( "Please select a node first !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
				return;
			}

			object tag = n.Tag;

			if ( tag is TreeItem )
			{
				TreeItem i = ( TreeItem ) tag;
				
				i.Work();
				i.UpdateParentState();
			}
		}

//-----------------------------------------------------------------------------
// Export

		private void OnExport()
		{
			if ( _All == null )
			{
				MessageBox.Show( "Please open a .chm file first !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
				return;
			}

			Export export = new Export( _All );

			if ( export.ShowOptions() != DialogResult.OK ) return;

			if ( export.WriteAll() )
				MessageBox.Show( "Exported results", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information );
			else
				MessageBox.Show( "Failed to export results", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning );
		}


//-----------------------------------------------------------------------------
// Fast

		private void OnToggleFast()
		{
			Global.FastParse = ! Global.FastParse;

			Debug.Assert( toolBarButtonFast.Pushed == Global.FastParse );

			SetFastToolTip();
			SetFastChecks();
		}

		private void OnModeFast()
		{
			Global.FastParse = true;

			SetFastToolTip();
			SetFastChecks();
		}

		private void OnModeSlow()
		{
			Global.FastParse = false;

			SetFastToolTip();
			SetFastChecks();
		}

		private void SetFastToolTip()
		{
			toolBarButtonFast.Pushed = Global.FastParse;

			toolBarButtonFast.ToolTipText =
				"Toggle Fast Parsing ( currently in " +
				( Global.FastParse ? "Fast" : "Slow" ) +
				" mode )";
		}

		private void SetFastChecks()
		{
			bool b = Global.FastParse;

			menuItemModeFast.Checked =   b;
			menuItemModeSlow.Checked = ! b;

			Global.FastMode.Set( Global.FastParse );
		}

//-----------------------------------------------------------------------------

		private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{
//			string[] sa = e.Data.GetFormats();

			string s = GetFilename( e.Data );

			if ( s != null ) 
				e.Effect = DragDropEffects.Link;
			else
				e.Effect = DragDropEffects.None;
		}

		private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
		{
			string s = GetFilename( e.Data );

			if ( s != null ) LoadFromFile( s );
		}

		private string GetFilename( IDataObject data )
		{
//			if ( e.Data.GetDataPresent( DataFormats.FileDrop ) )

			object o = data.GetData( DataFormats.FileDrop );

			if ( o == null ) return null;

			if ( ! ( o is string[] ) ) return null;

			string[] sa = ( string[] ) o;

			if ( sa.Length != 1 ) return null;

			string s = sa[ 0 ];

			if ( String.Compare( Path.GetExtension( s ), ".chm", true ) != 0 ) return null;

			return s;
		}

//-----------------------------------------------------------------------------
	
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

I can work 'virtually' anywhere!

Comments and Discussions