1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
//
// Authors:
// Peter Bartok (pbartok@novell.com)
//
//
// NOT COMPLETE
// Still missing: Tie-in to HTML help when the user presses F1 on the control
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
namespace System.Windows.Forms {
[ToolboxItemFilter("System.Windows.Forms")]
[ProvideProperty("ShowHelp", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
[ProvideProperty("HelpNavigator", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
[ProvideProperty("HelpKeyword", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
[ProvideProperty("HelpString", "System.Windows.Forms.Control, " + Consts.AssemblySystem_Windows_Forms)]
public class HelpProvider : Component, IExtenderProvider {
#region HelpProperty Class
private class HelpProperty {
internal string keyword;
internal HelpNavigator navigator;
internal string text;
internal bool show;
internal Control control;
internal HelpProvider hp;
public HelpProperty(HelpProvider hp, Control control) {
this.control = control;
this.hp = hp;
keyword = null;
navigator = HelpNavigator.AssociateIndex;
text = null;
show = false;
control.HelpRequested += hp.HelpRequestHandler;
}
public string Keyword {
get { return keyword; }
set { keyword = value; }
}
public HelpNavigator Navigator {
get { return navigator; }
set { navigator = value; }
}
public string Text {
get { return text; }
set { text = value; }
}
public bool Show {
get { return show; }
set { show = value; }
}
}
#endregion // HelpProperty Class
#region Local Variables
private string helpnamespace;
private Hashtable controls;
private ToolTip.ToolTipWindow tooltip;
private EventHandler HideToolTipHandler;
private KeyPressEventHandler HideToolTipKeyHandler;
private MouseEventHandler HideToolTipMouseHandler;
private HelpEventHandler HelpRequestHandler;
private object tag;
#endregion // Local Variables
#region Public Constructors
public HelpProvider() {
controls = new Hashtable();
tooltip = new ToolTip.ToolTipWindow();
//UIA Framework: Event used to indicate that ToolTip is shown
tooltip.VisibleChanged += delegate (object sender, EventArgs args) {
if (tooltip.Visible == true)
OnUIAHelpRequested (this, new ControlEventArgs (UIAControl));
else
OnUIAHelpUnRequested (this, new ControlEventArgs (UIAControl));
};
HideToolTipHandler = new EventHandler(HideToolTip);
HideToolTipKeyHandler = new KeyPressEventHandler(HideToolTipKey);
HideToolTipMouseHandler = new MouseEventHandler(HideToolTipMouse);
HelpRequestHandler = new HelpEventHandler(HelpRequested);
}
#endregion // Public Constructors
#region Public Instance Properties
[DefaultValue(null)]
[Editor ("System.Windows.Forms.Design.HelpNamespaceEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
[Localizable(true)]
public virtual string HelpNamespace {
get {
return helpnamespace;
}
set {
helpnamespace = value;
}
}
[Localizable (false)]
[Bindable (true)]
[TypeConverter (typeof (StringConverter))]
[DefaultValue (null)]
[MWFCategory ("Data")]
public object Tag
{
get { return this.tag; }
set { this.tag = value; }
}
#endregion // Public Instance Properties
#region Public Instance Methods
public virtual bool CanExtend(object target) {
if (!(target is Control)) {
return false;
}
if ((target is Form) || (target is ToolBar)) {
return false;
}
return true;
}
[DefaultValue(null)]
[Localizable(true)]
public virtual string GetHelpKeyword(Control ctl) {
return GetHelpProperty(ctl).Keyword;
}
[DefaultValue(HelpNavigator.AssociateIndex)]
[Localizable(true)]
public virtual HelpNavigator GetHelpNavigator(Control ctl) {
return GetHelpProperty(ctl).Navigator;
}
[DefaultValue(null)]
[Localizable(true)]
public virtual string GetHelpString(Control ctl) {
return GetHelpProperty(ctl).Text;
}
[Localizable(true)]
public virtual bool GetShowHelp(Control ctl) {
return GetHelpProperty(ctl).Show;
}
public virtual void ResetShowHelp(Control ctl) {
HelpProperty hp;
hp = GetHelpProperty(ctl);
if ((hp.Keyword != null) || (hp.Text != null)) {
hp.Show = true;
} else {
hp.Show = false;
}
}
public virtual void SetHelpKeyword(Control ctl, string keyword) {
GetHelpProperty(ctl).Keyword = keyword;
}
public virtual void SetHelpNavigator(Control ctl, HelpNavigator navigator) {
GetHelpProperty(ctl).Navigator = navigator;
}
public virtual void SetHelpString(Control ctl, string helpString) {
GetHelpProperty(ctl).Text = helpString;
}
public virtual void SetShowHelp(Control ctl, bool value) {
GetHelpProperty(ctl).Show = value;
}
public override string ToString() {
return base.ToString() + ", HelpNameSpace: " + helpnamespace;
}
#endregion // Public Instance Methods
#region Private Methods
private HelpProperty GetHelpProperty(Control control) {
HelpProperty hp;
hp = (HelpProperty)controls[control];
if (hp == null) {
hp = new HelpProperty(this, control);
controls[control] = hp;
}
return hp;
}
private void HideToolTip(object Sender, EventArgs e) {
Control control;
control = (Control)Sender;
control.LostFocus -= HideToolTipHandler;
this.tooltip.Visible = false;
}
private void HideToolTipKey(object Sender, KeyPressEventArgs e) {
Control control;
control = (Control)Sender;
control.KeyPress -= HideToolTipKeyHandler;
this.tooltip.Visible = false;
}
private void HideToolTipMouse(object Sender, MouseEventArgs e) {
Control control;
control = (Control)Sender;
control.MouseDown -= HideToolTipMouseHandler;
this.tooltip.Visible = false;
}
// This is called when the user does a "what's this" style lookup. It uses the 'text' property
private void HelpRequested(object sender, HelpEventArgs e) {
Size size;
Point pt;
Control control;
control = (Control)sender;
//UIA Framework: Associates requested control with internal variable to generate event
UIAControl = control;
if (GetHelpProperty(control).Text == null) {
return;
}
pt = e.MousePos;
// Display Tip
tooltip.Text = GetHelpProperty(control).Text;
size = ThemeEngine.Current.ToolTipSize(tooltip, tooltip.Text);
tooltip.Width = size.Width;
tooltip.Height = size.Height;
pt.X -= size.Width / 2;
if (pt.X < 0) {
pt.X += size.Width / 2;
}
if ((pt.X + size.Width) < SystemInformation.WorkingArea.Width) {
tooltip.Left = pt.X;
} else {
tooltip.Left = pt.X - size.Width;
}
if ((pt.Y + size.Height) < (SystemInformation.WorkingArea.Height - 16)) {
tooltip.Top = pt.Y;
} else {
tooltip.Top = pt.Y - size.Height;
}
tooltip.Visible = true;
control.KeyPress += HideToolTipKeyHandler;
control.MouseDown += HideToolTipMouseHandler;
control.LostFocus += HideToolTipHandler;
e.Handled = true;
}
#endregion // Private Methods
#region UIA Framework: Events, Delegates and Methods
private Control uia_control;
private Control UIAControl {
get { return uia_control; }
set { uia_control = value; }
}
internal static event ControlEventHandler UIAHelpRequested;
internal static event ControlEventHandler UIAHelpUnRequested;
internal Rectangle UIAToolTipRectangle {
get { return tooltip.Bounds; }
}
internal static void OnUIAHelpRequested (HelpProvider provider, ControlEventArgs args)
{
if (UIAHelpRequested != null)
UIAHelpRequested (provider, args);
}
internal static void OnUIAHelpUnRequested (HelpProvider provider, ControlEventArgs args)
{
if (UIAHelpUnRequested != null)
UIAHelpUnRequested (provider, args);
}
#endregion
}
}
|