HelpProvider.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #if NET_2_0
  83. private object tag;
  84. #endif
  85. #endregion // Local Variables
  86. #region Public Constructors
  87. public HelpProvider() {
  88. controls = new Hashtable();
  89. tooltip = new ToolTip.ToolTipWindow();
  90. HideToolTipHandler = new EventHandler(HideToolTip);
  91. HideToolTipKeyHandler = new KeyPressEventHandler(HideToolTipKey);
  92. HideToolTipMouseHandler = new MouseEventHandler(HideToolTipMouse);
  93. HelpRequestHandler = new HelpEventHandler(HelpRequested);
  94. }
  95. #endregion // Public Constructors
  96. #region Public Instance Properties
  97. [DefaultValue(null)]
  98. [Editor ("System.Windows.Forms.Design.HelpNamespaceEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  99. [Localizable(true)]
  100. public virtual string HelpNamespace {
  101. get {
  102. return helpnamespace;
  103. }
  104. set {
  105. helpnamespace = value;
  106. }
  107. }
  108. #if NET_2_0
  109. [Localizable (false)]
  110. [Bindable (true)]
  111. [TypeConverter (typeof (StringConverter))]
  112. [DefaultValue (null)]
  113. [MWFCategory ("Data")]
  114. public object Tag
  115. {
  116. get { return this.tag; }
  117. set { this.tag = value; }
  118. }
  119. #endif
  120. #endregion // Public Instance Properties
  121. #region Public Instance Methods
  122. public virtual bool CanExtend(object extendee) {
  123. if (!(extendee is Control)) {
  124. return false;
  125. }
  126. if ((extendee is Form) || (extendee is ToolBar)) {
  127. return false;
  128. }
  129. return true;
  130. }
  131. [DefaultValue(null)]
  132. [Localizable(true)]
  133. public virtual string GetHelpKeyword(Control ctl) {
  134. return GetHelpProperty(ctl).Keyword;
  135. }
  136. [DefaultValue(HelpNavigator.AssociateIndex)]
  137. [Localizable(true)]
  138. public virtual HelpNavigator GetHelpNavigator(Control ctl) {
  139. return GetHelpProperty(ctl).Navigator;
  140. }
  141. [DefaultValue(null)]
  142. [Localizable(true)]
  143. public virtual string GetHelpString(Control ctl) {
  144. return GetHelpProperty(ctl).Text;
  145. }
  146. [Localizable(true)]
  147. public virtual bool GetShowHelp(Control ctl) {
  148. return GetHelpProperty(ctl).Show;
  149. }
  150. public virtual void ResetShowHelp(Control ctl) {
  151. HelpProperty hp;
  152. hp = GetHelpProperty(ctl);
  153. if ((hp.Keyword != null) || (hp.Text != null)) {
  154. hp.Show = true;
  155. } else {
  156. hp.Show = false;
  157. }
  158. }
  159. public virtual void SetHelpKeyword(Control ctl, string keyword) {
  160. GetHelpProperty(ctl).Keyword = keyword;
  161. }
  162. public virtual void SetHelpNavigator(Control ctl, HelpNavigator navigator) {
  163. GetHelpProperty(ctl).Navigator = navigator;
  164. }
  165. public virtual void SetHelpString(Control ctl, string helpString) {
  166. GetHelpProperty(ctl).Text = helpString;
  167. }
  168. public virtual void SetShowHelp(Control ctl, bool value) {
  169. GetHelpProperty(ctl).Show = value;
  170. }
  171. public override string ToString() {
  172. return base.ToString() + ", HelpNameSpace: " + helpnamespace;
  173. }
  174. #endregion // Public Instance Methods
  175. #region Private Methods
  176. private HelpProperty GetHelpProperty(Control control) {
  177. HelpProperty hp;
  178. hp = (HelpProperty)controls[control];
  179. if (hp == null) {
  180. hp = new HelpProperty(this, control);
  181. controls[control] = hp;
  182. }
  183. return hp;
  184. }
  185. private void HideToolTip(object Sender, EventArgs e) {
  186. Control control;
  187. control = (Control)Sender;
  188. control.LostFocus -= HideToolTipHandler;
  189. this.tooltip.Visible = false;
  190. }
  191. private void HideToolTipKey(object Sender, KeyPressEventArgs e) {
  192. Control control;
  193. control = (Control)Sender;
  194. control.KeyPress -= HideToolTipKeyHandler;
  195. this.tooltip.Visible = false;
  196. }
  197. private void HideToolTipMouse(object Sender, MouseEventArgs e) {
  198. Control control;
  199. control = (Control)Sender;
  200. control.MouseDown -= HideToolTipMouseHandler;
  201. this.tooltip.Visible = false;
  202. }
  203. // This is called when the user does a "what's this" style lookup. It uses the 'text' property
  204. private void HelpRequested(object sender, HelpEventArgs e) {
  205. Size size;
  206. Point pt;
  207. Control control;
  208. control = (Control)sender;
  209. if (GetHelpProperty(control).Text == null) {
  210. return;
  211. }
  212. pt = e.MousePos;
  213. // Display Tip
  214. tooltip.Text = GetHelpProperty(control).Text;
  215. size = ThemeEngine.Current.ToolTipSize(tooltip, tooltip.Text);
  216. tooltip.Width = size.Width;
  217. tooltip.Height = size.Height;
  218. pt.X -= size.Width / 2;
  219. if (pt.X < 0) {
  220. pt.X += size.Width / 2;
  221. }
  222. if ((pt.X + size.Width) < SystemInformation.WorkingArea.Width) {
  223. tooltip.Left = pt.X;
  224. } else {
  225. tooltip.Left = pt.X - size.Width;
  226. }
  227. if ((pt.Y + size.Height) < (SystemInformation.WorkingArea.Height - 16)) {
  228. tooltip.Top = pt.Y;
  229. } else {
  230. tooltip.Top = pt.Y - size.Height;
  231. }
  232. tooltip.Visible = true;
  233. control.KeyPress += HideToolTipKeyHandler;
  234. control.MouseDown += HideToolTipMouseHandler;
  235. control.LostFocus += HideToolTipHandler;
  236. e.Handled = true;
  237. }
  238. #endregion // Private Methods
  239. }
  240. }