| 1 | package require Metawidget |
|---|
| 2 | |
|---|
| 3 | # name: Tooltip |
|---|
| 4 | # args: args: option-value pairs |
|---|
| 5 | # creates the label for the bubblehelp widget. the new class bindings |
|---|
| 6 | # are later attached to any widget where a bubblehelp shall appear. |
|---|
| 7 | # the nowait flag is true if the bubblehelp was just active within the |
|---|
| 8 | # last second. in this case no new delay would occur. |
|---|
| 9 | metawidget create Tooltip { |
|---|
| 10 | pack [label $this.text -bg #FFC -border 1 -relief solid -padx 2] -fill x |
|---|
| 11 | |
|---|
| 12 | # hide the window and take it away from the window manager |
|---|
| 13 | wm withdraw $this |
|---|
| 14 | wm overrideredirect $this 1 |
|---|
| 15 | |
|---|
| 16 | # define some new class bindings for that instance |
|---|
| 17 | bind Tooltip-$this <Enter> "$this _start %W" |
|---|
| 18 | bind Tooltip-$this <Leave> "$this _hide" |
|---|
| 19 | bind Tooltip-$this <Button> "$this _hide" |
|---|
| 20 | |
|---|
| 21 | my bNowait 0 ;# if 1: no new timer when above new widget |
|---|
| 22 | my -delay 1000 ;# default delay time 1 second |
|---|
| 23 | } { |
|---|
| 24 | } -default text -type toplevel |
|---|
| 25 | |
|---|
| 26 | # name: add |
|---|
| 27 | # args: sWindow: widget to assign a bubblehelp |
|---|
| 28 | # sText: text to appear in the bubblehelp |
|---|
| 29 | # registers a widget with this bubblehelp instance. |
|---|
| 30 | metawidget proc Tooltip add { sWindow sText } { |
|---|
| 31 | # add the class bindings to the widget |
|---|
| 32 | bindtags $sWindow [mkw.lextend [bindtags $sWindow] Tooltip-$this] |
|---|
| 33 | my $sWindow $sText |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | # name: forget |
|---|
| 37 | # args: sWindow: widget to remove bubblehelp from |
|---|
| 38 | # removes a bubblehelp from a widget |
|---|
| 39 | metawidget proc Tooltip forget { sWindow } { |
|---|
| 40 | # remove the class bindings from the widget |
|---|
| 41 | bindtags $sWindow [mkw.lshrink [bindtags $sWindow] Tooltip-$this] |
|---|
| 42 | unmy $sWindow |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | # name: _start |
|---|
| 46 | # args: sWindow: widget to display help for |
|---|
| 47 | # triggered on the <Enter> event. starts a timer or shows the help now. |
|---|
| 48 | metawidget proc Tooltip _start { sWindow } { |
|---|
| 49 | if { [my bNowait] } { ;# if bubblehelp was active... |
|---|
| 50 | _show $sWindow ;# ... then no new waiting time |
|---|
| 51 | } else { ;# otherwise start timer |
|---|
| 52 | our hJobID [after [my -delay] $this _show $sWindow] |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | # name: _show |
|---|
| 57 | # args: sWindow: widget to display help for |
|---|
| 58 | # actually displays the help. nowait is set to 1. if the mouse |
|---|
| 59 | # enters another widget then the timer won't start again. |
|---|
| 60 | metawidget proc Tooltip _show { sWindow } { |
|---|
| 61 | catch { after cancel [our hWaitID] } |
|---|
| 62 | my bNowait 1 ;# set 'bubblehelp active' flag |
|---|
| 63 | |
|---|
| 64 | # set the text for this widget, show and position it |
|---|
| 65 | $this.text config -text [my $sWindow] |
|---|
| 66 | wm deiconify $this |
|---|
| 67 | raise $this |
|---|
| 68 | wm geometry $this +[expr 2+[winfo pointerx .]]+[expr 16+[winfo pointery .]] |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | # name: _hide |
|---|
| 72 | # args: - |
|---|
| 73 | # triggered on the <Leave> event and on any button click. hides the help |
|---|
| 74 | # and resets the nowait flag after 1 second. |
|---|
| 75 | metawidget proc Tooltip _hide {} { |
|---|
| 76 | # start timer which will reset 'bubblehelp active' flag |
|---|
| 77 | our hWaitID [after 500 set bNowait 0] |
|---|
| 78 | |
|---|
| 79 | # kill any pending timer and hide the widget |
|---|
| 80 | catch { after cancel [our hJobID] } |
|---|
| 81 | wm withdraw $this |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | metawidget command Tooltip _start _start |
|---|
| 85 | metawidget command Tooltip _show _show |
|---|
| 86 | metawidget command Tooltip _hide _hide |
|---|
| 87 | |
|---|
| 88 | metawidget command Tooltip add add |
|---|
| 89 | metawidget command Tooltip forget forget |
|---|
| 90 | |
|---|
| 91 | metawidget option Tooltip -delay |
|---|
| 92 | |
|---|
| 93 | proc test {} { |
|---|
| 94 | pack [label .l -text "Move the mouse over the buttons"] |
|---|
| 95 | |
|---|
| 96 | pack [frame .f1 -border 1 -relief raised] |
|---|
| 97 | pack [button .f1.b1 -text B1] -side left -padx 2 -pady 2 |
|---|
| 98 | pack [button .f1.b2 -text B2] -side left -padx 2 -pady 2 |
|---|
| 99 | pack [button .f1.b3 -text B3] -side left -padx 2 -pady 2 |
|---|
| 100 | |
|---|
| 101 | tooltip .ttip1 -delay 500 |
|---|
| 102 | .ttip1 add .l "Yes, it works also on labels and other widget types" |
|---|
| 103 | .ttip1 add .f1.b1 "This is button 1 with .bhelp1" |
|---|
| 104 | .ttip1 add .f1.b2 "Button 2 with .bhelp1" |
|---|
| 105 | .ttip1 add .f1.b3 "Button three with .bhelp1" |
|---|
| 106 | |
|---|
| 107 | pack [frame .f2 -border 1 -relief raised] -side bottom |
|---|
| 108 | pack [button .f2.b1 -text B1] -side left -padx 2 -pady 2 |
|---|
| 109 | pack [button .f2.b2 -text B2] -side left -padx 2 -pady 2 |
|---|
| 110 | pack [button .f2.b3 -text B3] -side left -padx 2 -pady 2 |
|---|
| 111 | |
|---|
| 112 | tooltip .ttip2 -delay 500 |
|---|
| 113 | .ttip2 add .f2.b1 "This is button 1 with .bhelp2" |
|---|
| 114 | .ttip2 add .f2.b2 "Button 2 with .bhelp2" |
|---|
| 115 | .ttip2 add .f2.b3 "Button three with .bhelp2" |
|---|
| 116 | |
|---|
| 117 | .ttip2 configure -bg blue -fg white |
|---|
| 118 | |
|---|
| 119 | raise . |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | #test |
|---|
| 123 | |
|---|