[MWF] Partially implement Help.ShowHelp
authorMarkS <marksvc@gmail.com>
Thu, 24 Oct 2013 22:22:39 +0000 (16:22 -0600)
committerEberhard Beilharz <eb1@sil.org>
Mon, 17 Mar 2014 11:30:16 +0000 (12:30 +0100)
mcs/class/Managed.Windows.Forms/System.Windows.Forms/Help.cs

index a7d355bc1a52888bc1fd22a68006f209062d3e0b..46065f0d7240e46728da412b4e51034bb2d4ba4f 100644 (file)
 
 using System;
 using System.Drawing;
+using System.Diagnostics;
+using System.IO;
 
 namespace System.Windows.Forms
 {
+       /// <summary>
+       /// http://msdn.microsoft.com/en-us/library/System.Windows.Forms.Help(v=vs.110).aspx
+       /// </summary>
        public class Help
        {
                #region Constructor
@@ -42,7 +47,7 @@ namespace System.Windows.Forms
                #region Public Static Methods
                public static void ShowHelp (Control parent, string url)
                {
-                       ShowHelp(parent, url, HelpNavigator.TableOfContents, null);
+                       ShowHelp (parent, url, null);
                }
 
                public static void ShowHelp (Control parent, string url, HelpNavigator navigator)
@@ -57,9 +62,7 @@ namespace System.Windows.Forms
 
                public static void ShowHelp (Control parent, string url, string keyword)
                {
-                       if (keyword == null || keyword == String.Empty)
-                               ShowHelp (parent, url, HelpNavigator.TableOfContents, null);
-                       ShowHelp (parent, url, HelpNavigator.Topic, keyword);
+                       ShowHelpTopic (url, keyword);
                }
 
                public static void ShowHelpIndex (Control parent, string url)
@@ -72,5 +75,55 @@ namespace System.Windows.Forms
                {
                }
                #endregion      // Public Static Methods
+
+               /// <summary>
+               /// Show a help file and topic using a help viewer
+               /// </summary>
+               /// <param name="helpFile">path to a .chm help file</param>
+               /// <param name="helpTopic">path to a topic in helpFile, or null</param>
+               private static void ShowHelpTopic (string helpFile, string helpTopic)
+               {
+                       if (helpFile == null)
+                               throw new ArgumentNullException ();
+                       if (helpFile == String.Empty)
+                               throw new ArgumentException ();
+
+                       // Use forward slashes in helpFile path if needed
+                       helpFile = helpFile.Replace (@"\", Path.DirectorySeparatorChar.ToString ());
+
+                       string helpViewer = Environment.GetEnvironmentVariable ("MONO_HELP_VIEWER") ?? "chmsee";
+                       string arguments = String.Format ("\"{0}\"", helpFile);
+                       if (!String.IsNullOrEmpty (helpTopic)) {
+                               if (!helpTopic.StartsWith ("/"))
+                                       helpTopic = "/" + helpTopic;
+                               helpTopic = helpTopic.TrimEnd (' ');
+                               arguments = String.Format ("\"{0}::{1}\"", helpFile, helpTopic);
+                       }
+
+                       try {
+                               RunNonblockingProcess (helpViewer, arguments);
+                       } catch (Exception e) {
+                               // Don't crash if the help viewer couldn't be launched. There
+                               // won't be an exception thrown if the help viewer can't find
+                               // the help file; it's up to the help viewer to display such an error.
+                               string message = String.Format ("The help viewer could not load. Maybe you don't have {0} installed or haven't set MONO_HELP_VIEWER. The specific error message was: {1}", helpViewer, e.Message);
+                               Console.Error.WriteLine (message);
+                               MessageBox.Show (message);
+                       }
+               }
+
+               /// <remarks>
+               /// throws exception from Process.Start() if there was a problem starting
+               /// </remarks>
+               private static void RunNonblockingProcess (string command, string arguments)
+               {
+                       using (Process process = new Process ()) {
+                               process.StartInfo.FileName = command;
+                               process.StartInfo.Arguments = arguments;
+                               process.StartInfo.UseShellExecute = false;
+
+                               process.Start ();
+                       }
+               }
        }
 }