New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolTip.cs
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) 2004 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25 //
26
27
28 // COMPLETE
29
30 using System.Collections;
31 using System.ComponentModel;
32 using System.Drawing;
33
34 namespace System.Windows.Forms {
35         [ProvideProperty ("ToolTip", typeof(System.Windows.Forms.Control))]
36         [ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
37         public sealed class ToolTip : System.ComponentModel.Component, System.ComponentModel.IExtenderProvider {
38                 #region Local variables
39                 internal bool           is_active;
40                 internal int            automatic_delay;
41                 internal int            autopop_delay;
42                 internal int            initial_delay;
43                 internal int            re_show_delay;
44                 internal bool           show_always;
45
46                 internal ToolTipWindow  tooltip_window;                 // The actual tooltip window
47                 internal Hashtable      tooltip_strings;                // List of strings for each control, indexed by control
48                 internal ArrayList      controls;
49                 internal Control        active_control;                 // Control for which the tooltip is currently displayed
50                 internal Control        last_control;                   // last control the mouse was in
51                 internal Timer          timer;                          // Used for the various intervals
52                 #endregion      // Local variables
53
54                 #region ToolTipWindow Class
55                 internal class ToolTipWindow : Control {
56                         #region ToolTipWindow Class Local Variables
57                         internal StringFormat string_format;
58                         #endregion      // ToolTipWindow Class Local Variables
59
60                         #region ToolTipWindow Class Constructor
61                         internal ToolTipWindow() {
62
63                                 string_format = new StringFormat();
64                                 string_format.LineAlignment = StringAlignment.Center;
65                                 string_format.Alignment = StringAlignment.Center;
66                                 string_format.FormatFlags = StringFormatFlags.NoWrap;
67
68                                 Visible = false;
69                                 Size = new Size(100, 20);
70                                 ForeColor = ThemeEngine.Current.ColorInfoText;
71                                 BackColor = ThemeEngine.Current.ColorInfo;
72
73                                 VisibleChanged += new EventHandler(ToolTipWindow_VisibleChanged);
74
75                                 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
76                                 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
77                         }
78
79                         #endregion      // ToolTipWindow Class Constructor
80
81                         #region ToolTipWindow Class Protected Instance Methods
82                         protected override void OnCreateControl() {
83                                 base.OnCreateControl ();
84                                 XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, true);
85                         }
86
87                         protected override CreateParams CreateParams {
88                                 get {
89                                         CreateParams cp;
90
91                                         cp = base.CreateParams;
92
93                                         cp.Style = (int)WindowStyles.WS_POPUP;
94                                         cp.Style |= (int)WindowStyles.WS_CLIPSIBLINGS;
95
96                                         cp.ExStyle = (int)(WindowExStyles.WS_EX_TOOLWINDOW | WindowExStyles.WS_EX_TOPMOST);
97
98                                         return cp;
99                                 }
100                         }
101
102                         protected override void OnPaint(PaintEventArgs pevent) {
103                                 // We don't do double-buffering on purpose:
104                                 // 1) we'd have to meddle with is_visible, it destroys the buffers if !visible
105                                 // 2) We don't draw much, no need to double buffer
106                                 ThemeEngine.Current.DrawToolTip(pevent.Graphics, ClientRectangle, this);
107
108                                 base.OnPaint(pevent);
109                         }
110
111                         protected override void Dispose(bool disposing) {
112                                 if (disposing) {
113                                         this.string_format.Dispose();
114                                 }
115                                 base.Dispose (disposing);
116                         }
117
118                         protected override void WndProc(ref Message m) {
119                                 if (m.Msg == (int)Msg.WM_SETFOCUS) {
120                                         if (m.WParam != IntPtr.Zero) {
121                                                 XplatUI.SetFocus(m.WParam);
122                                         }
123                                 }
124                                 base.WndProc (ref m);
125                         }
126
127
128                         #endregion      // ToolTipWindow Class Protected Instance Methods
129
130                         #region ToolTipWindow Class Private Methods
131                         private void ToolTipWindow_VisibleChanged(object sender, EventArgs e) {
132                                 Control control = (Control)sender;
133
134                                 if (control.is_visible) {
135                                         XplatUI.SetTopmost(control.window.Handle, IntPtr.Zero, true);
136                                 } else {
137                                         XplatUI.SetTopmost(control.window.Handle, IntPtr.Zero, false);
138                                 }
139                         }
140                         #endregion      // ToolTipWindow Class Protected Instance Methods
141
142                         public void Present (Control control, string text)
143                         {
144                                 if (IsDisposed)
145                                         return;
146
147                                 Size display_size;
148                                 XplatUI.GetDisplaySize (out display_size);
149
150                                 Size size = ThemeEngine.Current.ToolTipSize (this, text);
151                                 Width = size.Width;
152                                 Height = size.Height;
153                                 Text = text;
154
155                                 int cursor_w, cursor_h, hot_x, hot_y;
156                                 XplatUI.GetCursorInfo (control.Cursor.Handle, out cursor_w, out cursor_h, out hot_x, out hot_y);
157                                 Point loc = Control.MousePosition;
158                                 loc.Y += (cursor_h - hot_y);
159
160                                 if ((loc.X + Width) > display_size.Width)
161                                         loc.X = display_size.Width - Width;
162
163                                 if ((loc.Y + Height) > display_size.Height)
164                                         loc.Y = Control.MousePosition.Y - Height - hot_y;
165                                 
166                                 Location = loc;
167                                 Visible = true;
168                         }
169                 }
170                 #endregion      // ToolTipWindow Class
171
172                 #region Public Constructors & Destructors
173                 public ToolTip() {
174
175                         // Defaults from MS
176                         is_active = true;
177                         automatic_delay = 500;
178                         autopop_delay = 5000;
179                         initial_delay = 500;
180                         re_show_delay = 100;
181                         show_always = false;
182
183                         tooltip_strings = new Hashtable(5);
184                         controls = new ArrayList(5);
185
186                         tooltip_window = new ToolTipWindow();
187                         tooltip_window.MouseLeave += new EventHandler(control_MouseLeave);
188
189                         timer = new Timer();
190                         timer.Enabled = false;
191                         timer.Tick +=new EventHandler(timer_Tick);
192                 }
193
194                 public ToolTip(System.ComponentModel.IContainer cont) : this() {
195                         cont.Add (this);
196                 }
197
198                 ~ToolTip() {
199                 }
200                 #endregion      // Public Constructors & Destructors
201
202                 #region Public Instance Properties
203                 [DefaultValue (true)]
204                 public bool Active {
205                         get {
206                                 return is_active;
207                         }
208
209                         set {
210                                 if (is_active != value) {
211                                         is_active = value;
212
213                                         if (tooltip_window.Visible) {
214                                                 tooltip_window.Visible = false;
215                                                 active_control = null;
216                                         }
217                                 }
218                         }
219                 }
220
221                 [DefaultValue (500)]
222                 [RefreshProperties (RefreshProperties.All)]
223                 public int AutomaticDelay {
224                         get {
225                                 return automatic_delay;
226                         }
227
228                         set {
229                                 if (automatic_delay != value) {
230                                         automatic_delay = value;
231                                         autopop_delay = automatic_delay * 10;
232                                         initial_delay = automatic_delay;
233                                         re_show_delay = automatic_delay / 5;
234                                 }
235                         }
236                 }
237
238                 [RefreshProperties (RefreshProperties.All)]
239                 public int AutoPopDelay {
240                         get {
241                                 return autopop_delay;
242                         }
243
244                         set {
245                                 if (autopop_delay != value) {
246                                         autopop_delay = value;
247                                 }
248                         }
249                 }
250
251                 [RefreshProperties (RefreshProperties.All)]
252                 public int InitialDelay {
253                         get {
254                                 return initial_delay;
255                         }
256
257                         set {
258                                 if (initial_delay != value) {
259                                         initial_delay = value;
260                                 }
261                         }
262                 }
263
264                 [RefreshProperties (RefreshProperties.All)]
265                 public int ReshowDelay {
266                         get {
267                                 return re_show_delay;
268                         }
269
270                         set {
271                                 if (re_show_delay != value) {
272                                         re_show_delay = value;
273                                 }
274                         }
275                 }
276
277                 [DefaultValue (false)]
278                 public bool ShowAlways {
279                         get {
280                                 return show_always;
281                         }
282
283                         set {
284                                 if (show_always != value) {
285                                         show_always = value;
286                                 }
287                         }
288                 }
289                 #endregion      // Public Instance Properties
290
291                 #region Public Instance Methods
292                 public bool CanExtend(object target) {
293                         return false;
294                 }
295
296                 [Localizable (true)]
297                 [DefaultValue ("")]
298                 public string GetToolTip(Control control) {
299                         string tooltip = (string)tooltip_strings[control];
300                         if (tooltip == null)
301                                 return "";
302                         return tooltip;
303                 }
304
305                 public void RemoveAll() {
306                         tooltip_strings.Clear();
307                         controls.Clear();
308                 }
309
310                 public void SetToolTip(Control control, string caption) {
311                         tooltip_strings[control] = caption;
312
313                         // no need for duplicates
314                         if (!controls.Contains(control)) {
315                                 control.MouseEnter += new EventHandler(control_MouseEnter);
316                                 control.MouseMove += new MouseEventHandler(control_MouseMove);
317                                 control.MouseLeave += new EventHandler(control_MouseLeave);
318                                 controls.Add(control);
319                         }
320                         
321                         // if SetToolTip is called from a control and the mouse is currently over that control,
322                         // make sure that tooltip_window.Text gets updated
323                         if (caption != null && last_control == control) {
324                                 Size size = ThemeEngine.Current.ToolTipSize(tooltip_window, caption);
325                                 tooltip_window.Width = size.Width;
326                                 tooltip_window.Height = size.Height;
327                                 tooltip_window.Text = caption;
328                         }
329                 }
330
331                 public override string ToString() {
332                         return base.ToString() + " InitialDelay: " + initial_delay + ", ShowAlways: " + show_always;
333                 }
334                 #endregion      // Public Instance Methods
335
336                 #region Protected Instance Methods
337                 protected override void Dispose(bool disposing) {
338                         if (disposing) {
339                                 // Mop up the mess; or should we wait for the GC to kick in?
340                                 timer.Stop();
341                                 timer.Dispose();
342
343                                 // Not sure if we should clean up tooltip_window
344                                 tooltip_window.Dispose();
345
346                                 tooltip_strings.Clear();
347                                 
348                                 controls.Clear();
349                         }
350                 }
351                 #endregion      // Protected Instance Methods
352
353                 enum TipState {
354                         Initial,
355                         Show,
356                         Down
357                 }
358
359                 TipState state = TipState.Initial;
360
361                 #region Private Methods
362                 private void control_MouseEnter(object sender, EventArgs e) 
363                 {
364                         last_control = (Control)sender;
365
366                         // Whatever we're displaying right now, we don't want it anymore
367                         tooltip_window.Visible = false;
368                         timer.Stop();
369                         state = TipState.Initial;
370
371                         // if we're in the same control as before (how'd that happen?) or if we're not active, leave
372                         if (!is_active || (active_control == (Control)sender)) {
373                                 return;
374                         }
375
376                         if (!show_always) {
377                                 IContainerControl cc = last_control.GetContainerControl ();
378                                 if ((cc == null) || (cc.ActiveControl == null)) {
379                                         return;
380                                 }
381                         }
382
383                         string text = (string)tooltip_strings[sender];
384                         if (text != null && text.Length > 0) {
385
386                                 if (active_control == null) {
387                                         timer.Interval = initial_delay;
388                                 } else {
389                                         timer.Interval = re_show_delay;
390                                 }
391
392                                 active_control = (Control)sender;
393                                 timer.Start ();
394                         }
395                 }
396
397                 private void timer_Tick(object sender, EventArgs e) {
398                         timer.Stop();
399
400                         switch (state) {
401                         case TipState.Initial:
402                                 if (active_control == null)
403                                         return;
404                                 tooltip_window.Present (active_control, (string)tooltip_strings[active_control]);
405                                 state = TipState.Show;
406                                 timer.Interval = autopop_delay;
407                                 timer.Start();
408                                 break;
409
410                         case TipState.Show:
411                                 tooltip_window.Visible = false;
412                                 state = TipState.Down;
413                                 break;
414
415                         default:
416                                 throw new Exception ("Timer shouldn't be running in state: " + state);
417                         }
418                 }
419
420
421                 private bool MouseInControl(Control control) {
422                         Point   m;
423                         Point   c;
424                         Size    cw;
425
426                         if (control == null) {
427                                 return false;
428                         }
429
430                         m = Control.MousePosition;
431                         c = new Point(control.Bounds.X, control.Bounds.Y);
432                         if (control.parent != null) {
433                                 c = control.parent.PointToScreen(c);
434                         }
435                         cw = control.ClientSize;
436
437                         if (c.X<=m.X && m.X<(c.X+cw.Width) &&
438                                 c.Y<=m.Y && m.Y<(c.Y+cw.Height)) {
439                                 return true;
440                         }
441                         return false;
442                 }
443
444                 private void control_MouseLeave(object sender, EventArgs e) {
445                         timer.Stop();
446
447                         if (!MouseInControl(tooltip_window) && !MouseInControl(active_control)) {
448                                 active_control = null;
449                                 tooltip_window.Visible = false;
450                         }
451                         
452                         if (last_control == (Control)sender)
453                                 last_control = null;
454                 }
455
456                 private void control_MouseMove(object sender, MouseEventArgs e) {
457                         if (state != TipState.Down) {
458                                 timer.Stop();
459                                 timer.Start();
460                         }
461                 }
462                 #endregion      // Private Methods
463         }
464 }