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) 2004 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok pbartok@novell.com
27 using System.Collections;
28 using System.ComponentModel;
30 using System.Drawing.Text;
32 namespace System.Windows.Forms {
34 [DefaultEvent ("Popup")]
36 [ProvideProperty ("ToolTip", typeof(System.Windows.Forms.Control))]
37 [ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
42 class ToolTip : System.ComponentModel.Component, System.ComponentModel.IExtenderProvider {
43 #region Local variables
44 internal bool is_active;
45 internal int automatic_delay;
46 internal int autopop_delay;
47 internal int initial_delay;
48 internal int re_show_delay;
49 internal bool show_always;
51 internal ToolTipWindow tooltip_window; // The actual tooltip window
52 internal Hashtable tooltip_strings; // List of strings for each control, indexed by control
53 internal ArrayList controls;
54 internal Control active_control; // Control for which the tooltip is currently displayed
55 internal Control last_control; // last control the mouse was in
56 internal Timer timer; // Used for the various intervals
59 private bool isBalloon;
60 private bool stripAmpersands;
61 private bool useAnimation;
62 private bool useFading;
65 #endregion // Local variables
67 #region ToolTipWindow Class
68 internal class ToolTipWindow : Control {
69 #region ToolTipWindow Class Local Variables
70 internal StringFormat string_format;
71 #endregion // ToolTipWindow Class Local Variables
73 #region ToolTipWindow Class Constructor
74 internal ToolTipWindow() {
76 string_format = new StringFormat();
77 string_format.LineAlignment = StringAlignment.Center;
78 string_format.Alignment = StringAlignment.Center;
79 string_format.FormatFlags = StringFormatFlags.NoWrap;
80 string_format.HotkeyPrefix = HotkeyPrefix.Hide;
83 Size = new Size(100, 20);
84 ForeColor = ThemeEngine.Current.ColorInfoText;
85 BackColor = ThemeEngine.Current.ColorInfo;
87 VisibleChanged += new EventHandler(ToolTipWindow_VisibleChanged);
89 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
90 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
93 #endregion // ToolTipWindow Class Constructor
95 #region ToolTipWindow Class Protected Instance Methods
96 protected override void OnCreateControl() {
97 base.OnCreateControl ();
98 XplatUI.SetTopmost(this.window.Handle, true);
101 protected override CreateParams CreateParams {
105 cp = base.CreateParams;
107 cp.Style = (int)WindowStyles.WS_POPUP;
108 cp.Style |= (int)WindowStyles.WS_CLIPSIBLINGS;
110 cp.ExStyle = (int)(WindowExStyles.WS_EX_TOOLWINDOW | WindowExStyles.WS_EX_TOPMOST);
116 protected override void OnPaint(PaintEventArgs pevent) {
117 // We don't do double-buffering on purpose:
118 // 1) we'd have to meddle with is_visible, it destroys the buffers if !visible
119 // 2) We don't draw much, no need to double buffer
120 ThemeEngine.Current.DrawToolTip(pevent.Graphics, ClientRectangle, this);
122 base.OnPaint(pevent);
125 protected override void Dispose(bool disposing) {
127 this.string_format.Dispose();
129 base.Dispose (disposing);
132 protected override void WndProc(ref Message m) {
133 if (m.Msg == (int)Msg.WM_SETFOCUS) {
134 if (m.WParam != IntPtr.Zero) {
135 XplatUI.SetFocus(m.WParam);
138 base.WndProc (ref m);
142 #endregion // ToolTipWindow Class Protected Instance Methods
144 #region ToolTipWindow Class Private Methods
145 private void ToolTipWindow_VisibleChanged(object sender, EventArgs e) {
146 Control control = (Control)sender;
148 if (control.is_visible) {
149 XplatUI.SetTopmost(control.window.Handle, true);
151 XplatUI.SetTopmost(control.window.Handle, false);
154 #endregion // ToolTipWindow Class Protected Instance Methods
156 #region Internal Properties
157 internal override bool ActivateOnShow { get { return false; } }
160 public void Present (Control control, string text)
166 XplatUI.GetDisplaySize (out display_size);
168 Size size = ThemeEngine.Current.ToolTipSize (this, text);
170 Height = size.Height;
173 int cursor_w, cursor_h, hot_x, hot_y;
174 XplatUI.GetCursorInfo (control.Cursor.Handle, out cursor_w, out cursor_h, out hot_x, out hot_y);
175 Point loc = Control.MousePosition;
176 loc.Y += (cursor_h - hot_y);
178 if ((loc.X + Width) > display_size.Width)
179 loc.X = display_size.Width - Width;
181 if ((loc.Y + Height) > display_size.Height)
182 loc.Y = Control.MousePosition.Y - Height - hot_y;
188 #endregion // ToolTipWindow Class
190 #region Public Constructors & Destructors
195 automatic_delay = 500;
196 autopop_delay = 5000;
202 stripAmpersands = false;
206 tooltip_strings = new Hashtable(5);
207 controls = new ArrayList(5);
209 tooltip_window = new ToolTipWindow();
210 tooltip_window.MouseLeave += new EventHandler(control_MouseLeave);
213 timer.Enabled = false;
214 timer.Tick +=new EventHandler(timer_Tick);
217 public ToolTip(System.ComponentModel.IContainer cont) : this() {
223 #endregion // Public Constructors & Destructors
225 #region Public Instance Properties
226 [DefaultValue (true)]
233 if (is_active != value) {
236 if (tooltip_window.Visible) {
237 tooltip_window.Visible = false;
238 active_control = null;
245 [RefreshProperties (RefreshProperties.All)]
246 public int AutomaticDelay {
248 return automatic_delay;
252 if (automatic_delay != value) {
253 automatic_delay = value;
254 autopop_delay = automatic_delay * 10;
255 initial_delay = automatic_delay;
256 re_show_delay = automatic_delay / 5;
261 [RefreshProperties (RefreshProperties.All)]
262 public int AutoPopDelay {
264 return autopop_delay;
268 if (autopop_delay != value) {
269 autopop_delay = value;
274 [RefreshProperties (RefreshProperties.All)]
275 public int InitialDelay {
277 return initial_delay;
281 if (initial_delay != value) {
282 initial_delay = value;
287 [RefreshProperties (RefreshProperties.All)]
288 public int ReshowDelay {
290 return re_show_delay;
294 if (re_show_delay != value) {
295 re_show_delay = value;
300 [DefaultValue (false)]
301 public bool ShowAlways {
307 if (show_always != value) {
315 [DefaultValue (false)]
316 public bool IsBalloon {
317 get { return isBalloon; }
318 set { isBalloon = value; }
322 [DefaultValue (false)]
323 public bool StripAmpersands {
324 get { return stripAmpersands; }
325 set { stripAmpersands = value; }
329 [DefaultValue (true)]
330 public bool UseAnimation {
331 get { return useAnimation; }
332 set { useAnimation = value; }
336 [DefaultValue (true)]
337 public bool UseFading {
338 get { return useFading; }
339 set { useFading = value; }
343 #endregion // Public Instance Properties
345 #region Public Instance Methods
346 public bool CanExtend(object target) {
351 [Editor ("System.ComponentModel.Design.MultilineStringEditor, " + Consts.AssemblySystem_Design,
352 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
356 public string GetToolTip (Control control)
358 string tooltip = (string)tooltip_strings[control];
364 public void RemoveAll() {
365 tooltip_strings.Clear();
369 public void SetToolTip(Control control, string caption) {
370 tooltip_strings[control] = caption;
372 // no need for duplicates
373 if (!controls.Contains(control)) {
374 control.MouseEnter += new EventHandler(control_MouseEnter);
375 control.MouseMove += new MouseEventHandler(control_MouseMove);
376 control.MouseLeave += new EventHandler(control_MouseLeave);
377 controls.Add(control);
380 // if SetToolTip is called from a control and the mouse is currently over that control,
381 // make sure that tooltip_window.Text gets updated
382 if (caption != null && last_control == control) {
383 Size size = ThemeEngine.Current.ToolTipSize(tooltip_window, caption);
384 tooltip_window.Width = size.Width;
385 tooltip_window.Height = size.Height;
386 tooltip_window.Text = caption;
390 public override string ToString() {
391 return base.ToString() + " InitialDelay: " + initial_delay + ", ShowAlways: " + show_always;
395 public void Show (string text, IWin32Window win)
397 SetToolTip (win as Control, text);
398 ShowTooltip (win as Control);
401 [MonoTODO ("Finish implementing tooltip location")]
402 public void Show (string text, IWin32Window win, Point p)
404 SetToolTip (win as Control, text);
405 ShowTooltip (win as Control);
408 public void Hide (IWin32Window win)
410 Hide (win as Control);
414 #endregion // Public Instance Methods
416 #region Protected Instance Methods
417 protected override void Dispose(bool disposing) {
419 // Mop up the mess; or should we wait for the GC to kick in?
423 // Not sure if we should clean up tooltip_window
424 tooltip_window.Dispose();
426 tooltip_strings.Clear();
431 #endregion // Protected Instance Methods
439 TipState state = TipState.Initial;
441 #region Private Methods
442 private void control_MouseEnter(object sender, EventArgs e)
444 ShowTooltip (sender as Control);
447 private void ShowTooltip (Control control)
449 last_control = control;
451 // Whatever we're displaying right now, we don't want it anymore
452 tooltip_window.Visible = false;
454 state = TipState.Initial;
456 // if we're in the same control as before (how'd that happen?) or if we're not active, leave
457 if (!is_active || (active_control == control)) {
462 IContainerControl cc = last_control.GetContainerControl ();
463 if ((cc == null) || (cc.ActiveControl == null)) {
468 string text = (string)tooltip_strings[control];
469 if (text != null && text.Length > 0) {
471 if (active_control == null) {
472 timer.Interval = initial_delay;
474 timer.Interval = re_show_delay;
477 active_control = control;
482 private void timer_Tick(object sender, EventArgs e) {
486 case TipState.Initial:
487 if (active_control == null)
489 tooltip_window.Present (active_control, (string)tooltip_strings[active_control]);
490 state = TipState.Show;
491 timer.Interval = autopop_delay;
496 tooltip_window.Visible = false;
497 state = TipState.Down;
501 throw new Exception ("Timer shouldn't be running in state: " + state);
506 private bool MouseInControl(Control control) {
511 if (control == null) {
515 m = Control.MousePosition;
516 c = new Point(control.Bounds.X, control.Bounds.Y);
517 if (control.Parent != null) {
518 c = control.Parent.PointToScreen(c);
520 cw = control.ClientSize;
522 if (c.X<=m.X && m.X<(c.X+cw.Width) &&
523 c.Y<=m.Y && m.Y<(c.Y+cw.Height)) {
529 private void control_MouseLeave(object sender, EventArgs e)
531 Hide (sender as Control);
534 private void Hide (Control sender)
538 if (!MouseInControl(tooltip_window) && !MouseInControl(active_control)) {
539 active_control = null;
540 tooltip_window.Visible = false;
543 if (last_control == sender)
547 private void control_MouseMove(object sender, MouseEventArgs e) {
548 if (state != TipState.Down) {
555 internal virtual void OnPopup (PopupEventArgs e)
557 PopupEventHandler eh = (PopupEventHandler) (Events [PopupEvent]);
562 #endregion // Private Methods
566 static object PopupEvent = new object ();
568 public event PopupEventHandler Popup {
569 add { Events.AddHandler (PopupEvent, value); }
570 remove { Events.RemoveHandler (PopupEvent, value); }