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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok (pbartok@novell.com)
28 // Still missing: Tie-in to HTML help when the user presses F1 on the control
31 using System.Collections;
32 using System.ComponentModel;
35 namespace System.Windows.Forms {
36 [ToolboxItemFilter("System.Windows.Forms")]
37 [ProvideProperty("ShowHelp", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
38 [ProvideProperty("HelpNavigator", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
39 [ProvideProperty("HelpKeyword", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
40 [ProvideProperty("HelpString", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
41 public class HelpProvider : Component, IExtenderProvider {
42 #region HelpProperty Class
43 private class HelpProperty {
44 internal string keyword;
45 internal HelpNavigator navigator;
48 internal Control control;
49 internal HelpProvider hp;
51 public HelpProperty(HelpProvider hp, Control control) {
52 this.control = control;
56 navigator = HelpNavigator.AssociateIndex;
60 control.HelpRequested += hp.HelpRequestHandler;
63 public string Keyword {
64 get { return keyword; }
65 set { keyword = value; }
68 public HelpNavigator Navigator {
69 get { return navigator; }
70 set { navigator = value; }
83 #endregion // HelpProperty Class
85 #region Local Variables
86 private string helpnamespace;
87 private Hashtable controls;
88 private ToolTip.ToolTipWindow tooltip;
89 private EventHandler HideToolTipHandler;
90 private KeyPressEventHandler HideToolTipKeyHandler;
91 private MouseEventHandler HideToolTipMouseHandler;
92 private HelpEventHandler HelpRequestHandler;
96 #endregion // Local Variables
98 #region Public Constructors
99 public HelpProvider() {
100 controls = new Hashtable();
101 tooltip = new ToolTip.ToolTipWindow();
103 HideToolTipHandler = new EventHandler(HideToolTip);
104 HideToolTipKeyHandler = new KeyPressEventHandler(HideToolTipKey);
105 HideToolTipMouseHandler = new MouseEventHandler(HideToolTipMouse);
106 HelpRequestHandler = new HelpEventHandler(HelpRequested);
108 #endregion // Public Constructors
110 #region Public Instance Properties
112 [Editor ("System.Windows.Forms.Design.HelpNamespaceEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
114 public virtual string HelpNamespace {
116 return helpnamespace;
120 helpnamespace = value;
125 [Localizable (false)]
127 [TypeConverter (typeof (StringConverter))]
128 [DefaultValue (null)]
129 [MWFCategory ("Data")]
132 get { return this.tag; }
133 set { this.tag = value; }
136 #endregion // Public Instance Properties
138 #region Public Instance Methods
139 public virtual bool CanExtend(object extendee) {
140 if (!(extendee is Control)) {
144 if ((extendee is Form) || (extendee is ToolBar)) {
153 public virtual string GetHelpKeyword(Control ctl) {
154 return GetHelpProperty(ctl).Keyword;
157 [DefaultValue(HelpNavigator.AssociateIndex)]
159 public virtual HelpNavigator GetHelpNavigator(Control ctl) {
160 return GetHelpProperty(ctl).Navigator;
165 public virtual string GetHelpString(Control ctl) {
166 return GetHelpProperty(ctl).Text;
170 public virtual bool GetShowHelp(Control ctl) {
171 return GetHelpProperty(ctl).Show;
174 public virtual void ResetShowHelp(Control ctl) {
177 hp = GetHelpProperty(ctl);
179 if ((hp.Keyword != null) || (hp.Text != null)) {
186 public virtual void SetHelpKeyword(Control ctl, string keyword) {
187 GetHelpProperty(ctl).Keyword = keyword;
190 public virtual void SetHelpNavigator(Control ctl, HelpNavigator navigator) {
191 GetHelpProperty(ctl).Navigator = navigator;
194 public virtual void SetHelpString(Control ctl, string helpString) {
195 GetHelpProperty(ctl).Text = helpString;
198 public virtual void SetShowHelp(Control ctl, bool value) {
199 GetHelpProperty(ctl).Show = value;
202 public override string ToString() {
203 return base.ToString() + ", HelpNameSpace: " + helpnamespace;
206 #endregion // Public Instance Methods
208 #region Private Methods
209 private HelpProperty GetHelpProperty(Control control) {
212 hp = (HelpProperty)controls[control];
214 hp = new HelpProperty(this, control);
215 controls[control] = hp;
221 private void HideToolTip(object Sender, EventArgs e) {
224 control = (Control)Sender;
225 control.LostFocus -= HideToolTipHandler;
227 this.tooltip.Visible = false;
230 private void HideToolTipKey(object Sender, KeyPressEventArgs e) {
233 control = (Control)Sender;
234 control.KeyPress -= HideToolTipKeyHandler;
236 this.tooltip.Visible = false;
239 private void HideToolTipMouse(object Sender, MouseEventArgs e) {
242 control = (Control)Sender;
243 control.MouseDown -= HideToolTipMouseHandler;
245 this.tooltip.Visible = false;
249 // This is called when the user does a "what's this" style lookup. It uses the 'text' property
250 private void HelpRequested(object sender, HelpEventArgs e) {
255 control = (Control)sender;
257 if (GetHelpProperty(control).Text == null) {
264 tooltip.Text = GetHelpProperty(control).Text;
265 size = ThemeEngine.Current.ToolTipSize(tooltip, tooltip.Text);
266 tooltip.Width = size.Width;
267 tooltip.Height = size.Height;
268 pt.X -= size.Width / 2;
271 pt.X += size.Width / 2;
274 if ((pt.X + size.Width) < SystemInformation.WorkingArea.Width) {
277 tooltip.Left = pt.X - size.Width;
280 if ((pt.Y + size.Height) < (SystemInformation.WorkingArea.Height - 16)) {
283 tooltip.Top = pt.Y - size.Height;
287 tooltip.Visible = true;
288 control.KeyPress += HideToolTipKeyHandler;
289 control.MouseDown += HideToolTipMouseHandler;
290 control.LostFocus += HideToolTipHandler;
293 #endregion // Private Methods