HelpProvider.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok ([email protected])
  24. //
  25. //
  26. // NOT COMPLETE
  27. // Still missing: Tie-in to HTML help when the user presses F1 on the control
  28. using System;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Drawing;
  32. namespace System.Windows.Forms {
  33. [ToolboxItemFilter("System.Windows.Forms")]
  34. [ProvideProperty("ShowHelp", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
  35. [ProvideProperty("HelpNavigator", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
  36. [ProvideProperty("HelpKeyword", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
  37. [ProvideProperty("HelpString", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
  38. public class HelpProvider : Component, IExtenderProvider {
  39. #region HelpProperty Class
  40. private class HelpProperty {
  41. internal string keyword;
  42. internal HelpNavigator navigator;
  43. internal string text;
  44. internal bool show;
  45. internal Control control;
  46. internal HelpProvider hp;
  47. public HelpProperty(HelpProvider hp, Control control) {
  48. this.control = control;
  49. this.hp = hp;
  50. keyword = null;
  51. navigator = HelpNavigator.AssociateIndex;
  52. text = null;
  53. show = false;
  54. control.HelpRequested += hp.HelpRequestHandler;
  55. }
  56. public string Keyword {
  57. get { return keyword; }
  58. set { keyword = value; }
  59. }
  60. public HelpNavigator Navigator {
  61. get { return navigator; }
  62. set { navigator = value; }
  63. }
  64. public string Text {
  65. get { return text; }
  66. set { text = value; }
  67. }
  68. public bool Show {
  69. get { return show; }
  70. set { show = value; }
  71. }
  72. }
  73. #endregion // HelpProperty Class
  74. #region Local Variables
  75. private string helpnamespace;
  76. private Hashtable controls;
  77. private ToolTip.ToolTipWindow tooltip;
  78. private EventHandler HideToolTipHandler;
  79. private KeyPressEventHandler HideToolTipKeyHandler;
  80. private MouseEventHandler HideToolTipMouseHandler;
  81. private HelpEventHandler HelpRequestHandler;
  82. #endregion // Local Variables
  83. #region Public Constructors
  84. public HelpProvider() {
  85. controls = new Hashtable();
  86. tooltip = new ToolTip.ToolTipWindow(null);
  87. HideToolTipHandler = new EventHandler(HideToolTip);
  88. HideToolTipKeyHandler = new KeyPressEventHandler(HideToolTipKey);
  89. HideToolTipMouseHandler = new MouseEventHandler(HideToolTipMouse);
  90. HelpRequestHandler = new HelpEventHandler(HelpRequested);
  91. }
  92. #endregion // Public Constructors
  93. #region Public Instance Properties
  94. [DefaultValue(null)]
  95. [Editor ("System.Windows.Forms.Design.HelpNamespaceEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  96. [Localizable(true)]
  97. public virtual string HelpNamespace {
  98. get {
  99. return helpnamespace;
  100. }
  101. set {
  102. helpnamespace = value;
  103. }
  104. }
  105. #endregion // Public Instance Properties
  106. #region Public Instance Methods
  107. public virtual bool CanExtend(object extendee) {
  108. if (!(extendee is Control)) {
  109. return false;
  110. }
  111. if ((extendee is Form) || (extendee is ToolBar)) {
  112. return false;
  113. }
  114. return true;
  115. }
  116. [DefaultValue(null)]
  117. [Localizable(true)]
  118. public virtual string GetHelpKeyword(Control ctl) {
  119. return GetHelpProperty(ctl).Keyword;
  120. }
  121. [DefaultValue(HelpNavigator.AssociateIndex)]
  122. [Localizable(true)]
  123. public virtual HelpNavigator GetHelpNavigator(Control ctl) {
  124. return GetHelpProperty(ctl).Navigator;
  125. }
  126. [DefaultValue(null)]
  127. [Localizable(true)]
  128. public virtual string GetHelpString(Control ctl) {
  129. return GetHelpProperty(ctl).Text;
  130. }
  131. [Localizable(true)]
  132. public virtual bool GetShowHelp(Control ctl) {
  133. return GetHelpProperty(ctl).Show;
  134. }
  135. public virtual void ResetShowHelp(Control ctl) {
  136. HelpProperty hp;
  137. hp = GetHelpProperty(ctl);
  138. if ((hp.Keyword != null) || (hp.Text != null)) {
  139. hp.Show = true;
  140. } else {
  141. hp.Show = false;
  142. }
  143. }
  144. public virtual void SetHelpKeyword(Control ctl, string keyword) {
  145. GetHelpProperty(ctl).Keyword = keyword;
  146. }
  147. public virtual void SetHelpNavigator(Control ctl, HelpNavigator navigator) {
  148. GetHelpProperty(ctl).Navigator = navigator;
  149. }
  150. public virtual void SetHelpString(Control ctl, string helpString) {
  151. GetHelpProperty(ctl).Text = helpString;
  152. }
  153. public virtual void SetShowHelp(Control ctl, bool value) {
  154. GetHelpProperty(ctl).Show = value;
  155. }
  156. public override string ToString() {
  157. return base.ToString() + ", HelpNameSpace: " + helpnamespace;
  158. }
  159. #endregion // Public Instance Methods
  160. #region Private Methods
  161. private HelpProperty GetHelpProperty(Control control) {
  162. HelpProperty hp;
  163. hp = (HelpProperty)controls[control];
  164. if (hp == null) {
  165. hp = new HelpProperty(this, control);
  166. controls[control] = hp;
  167. }
  168. return hp;
  169. }
  170. private void HideToolTip(object Sender, EventArgs e) {
  171. Control control;
  172. control = (Control)Sender;
  173. control.LostFocus -= HideToolTipHandler;
  174. this.tooltip.Visible = false;
  175. }
  176. private void HideToolTipKey(object Sender, KeyPressEventArgs e) {
  177. Control control;
  178. control = (Control)Sender;
  179. control.KeyPress -= HideToolTipKeyHandler;
  180. this.tooltip.Visible = false;
  181. }
  182. private void HideToolTipMouse(object Sender, MouseEventArgs e) {
  183. Control control;
  184. control = (Control)Sender;
  185. control.MouseDown -= HideToolTipMouseHandler;
  186. this.tooltip.Visible = false;
  187. }
  188. // This is called when the user does a "what's this" style lookup. It uses the 'text' property
  189. private void HelpRequested(object sender, HelpEventArgs e) {
  190. Size size;
  191. Point pt;
  192. Control control;
  193. control = (Control)sender;
  194. if (GetHelpProperty(control).Text == null) {
  195. return;
  196. }
  197. pt = e.MousePos;
  198. // Display Tip
  199. tooltip.Text = GetHelpProperty(control).Text;
  200. size = ThemeEngine.Current.ToolTipSize(tooltip, tooltip.Text);
  201. tooltip.Width = size.Width;
  202. tooltip.Height = size.Height;
  203. pt.X -= size.Width / 2;
  204. if (pt.X < 0) {
  205. pt.X += size.Width / 2;
  206. }
  207. if ((pt.X + size.Width) < SystemInformation.WorkingArea.Width) {
  208. tooltip.Left = pt.X;
  209. } else {
  210. tooltip.Left = pt.X - size.Width;
  211. }
  212. if ((pt.Y + size.Height) < (SystemInformation.WorkingArea.Height - 16)) {
  213. tooltip.Top = pt.Y;
  214. } else {
  215. tooltip.Top = pt.Y - size.Height;
  216. }
  217. tooltip.Visible = true;
  218. control.KeyPress += HideToolTipKeyHandler;
  219. control.MouseDown += HideToolTipMouseHandler;
  220. control.LostFocus += HideToolTipHandler;
  221. e.Handled = true;
  222. }
  223. #endregion // Private Methods
  224. }
  225. }