From: Miguel de Icaza Date: Fri, 31 Oct 2014 15:20:20 +0000 (-0400) Subject: Merge pull request #960 from ermshiperete/ShowHelp X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=3fd4901a235bda0019f03317f4ee083744195758;hp=100ecb4f0f7e901b593eaa35057a3f8aa2cdcaf7;p=mono.git Merge pull request #960 from ermshiperete/ShowHelp [MWF] Partially implement Help.ShowHelp --- diff --git a/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Help.cs b/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Help.cs index a7d355bc1a5..46065f0d724 100644 --- a/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Help.cs +++ b/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Help.cs @@ -28,9 +28,14 @@ using System; using System.Drawing; +using System.Diagnostics; +using System.IO; namespace System.Windows.Forms { + /// + /// http://msdn.microsoft.com/en-us/library/System.Windows.Forms.Help(v=vs.110).aspx + /// 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 + + /// + /// Show a help file and topic using a help viewer + /// + /// path to a .chm help file + /// path to a topic in helpFile, or null + 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); + } + } + + /// + /// throws exception from Process.Start() if there was a problem starting + /// + 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 (); + } + } } }