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;
93 #endregion // Local Variables
95 #region Public Constructors
96 public HelpProvider() {
97 controls = new Hashtable();
98 tooltip = new ToolTip.ToolTipWindow(null);
100 HideToolTipHandler = new EventHandler(HideToolTip);
101 HideToolTipKeyHandler = new KeyPressEventHandler(HideToolTipKey);
102 HideToolTipMouseHandler = new MouseEventHandler(HideToolTipMouse);
103 HelpRequestHandler = new HelpEventHandler(HelpRequested);
105 #endregion // Public Constructors
107 #region Public Instance Properties
109 [Editor ("System.Windows.Forms.Design.HelpNamespaceEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
111 public string HelpNamespace {
113 return helpnamespace;
117 helpnamespace = value;
120 #endregion // Public Instance Properties
122 #region Public Instance Methods
123 public bool CanExtend(object extendee) {
124 if (!(extendee is Control)) {
128 if ((extendee is Form) || (extendee is ToolBar)) {
137 public virtual string GetHelpKeyword(Control ctl) {
138 return GetHelpProperty(ctl).Keyword;
141 [DefaultValue(HelpNavigator.AssociateIndex)]
143 public virtual HelpNavigator GetHelpNavigator(Control ctl) {
144 return GetHelpProperty(ctl).Navigator;
149 public virtual string GetHelpString(Control ctl) {
150 return GetHelpProperty(ctl).Text;
154 public virtual bool GetShowHelp(Control ctl) {
155 return GetHelpProperty(ctl).Show;
158 public virtual void ResetShowHelp(Control ctl) {
161 hp = GetHelpProperty(ctl);
163 if ((hp.Keyword != null) || (hp.Text != null)) {
170 public virtual void SetHelpKeyword(Control ctl, string keyword) {
171 GetHelpProperty(ctl).Keyword = keyword;
174 public virtual void SetHelpNavigator(Control ctl, HelpNavigator navigator) {
175 GetHelpProperty(ctl).Navigator = navigator;
178 public virtual void SetHelpString(Control ctl, string helpString) {
179 GetHelpProperty(ctl).Text = helpString;
182 public virtual void SetShowHelp(Control ctl, bool value) {
183 GetHelpProperty(ctl).Show = value;
186 public override string ToString() {
187 return base.ToString() + ", HelpNameSpace: " + helpnamespace;
190 #endregion // Public Instance Methods
192 #region Private Methods
193 private HelpProperty GetHelpProperty(Control control) {
196 hp = (HelpProperty)controls[control];
198 hp = new HelpProperty(this, control);
199 controls[control] = hp;
205 private void HideToolTip(object Sender, EventArgs e) {
208 control = (Control)Sender;
209 control.LostFocus -= HideToolTipHandler;
211 this.tooltip.Visible = false;
214 private void HideToolTipKey(object Sender, KeyPressEventArgs e) {
217 control = (Control)Sender;
218 control.KeyPress -= HideToolTipKeyHandler;
220 this.tooltip.Visible = false;
223 private void HideToolTipMouse(object Sender, MouseEventArgs e) {
226 control = (Control)Sender;
227 control.MouseDown -= HideToolTipMouseHandler;
229 this.tooltip.Visible = false;
233 // This is called when the user does a "what's this" style lookup. It uses the 'text' property
234 private void HelpRequested(object sender, HelpEventArgs e) {
239 control = (Control)sender;
241 if (GetHelpProperty(control).Text == null) {
248 tooltip.Text = GetHelpProperty(control).Text;
249 size = ThemeEngine.Current.ToolTipSize(tooltip, tooltip.Text);
250 tooltip.Width = size.Width;
251 tooltip.Height = size.Height;
252 pt.X -= size.Width / 2;
255 pt.X += size.Width / 2;
258 if ((pt.X + size.Width) < SystemInformation.WorkingArea.Width) {
261 tooltip.Left = pt.X - size.Width;
264 if ((pt.Y + size.Height) < (SystemInformation.WorkingArea.Height - 16)) {
267 tooltip.Top = pt.Y - size.Height;
271 tooltip.Visible = true;
272 control.KeyPress += HideToolTipKeyHandler;
273 control.MouseDown += HideToolTipMouseHandler;
274 control.LostFocus += HideToolTipHandler;
277 #endregion // Private Methods