|
Description:
This is a simple example of a mega-widget based on the "MegaWidget" procedure in "Simplified mega-widiget creation without class libraries" (http://aspn.activestate.com/ASPN/Cookbook/Tcl/Recipe/122547).
Source: Text Source
package require MegaWidget
package provide XYText 1.0
namespace eval XYText {
proc XYText { hWnd args } {
frame $hWnd -bd 1 -relief sunken
set hWndTxt \
[text $hWnd.txt \
-bd 0 \
-relief flat \
-xscroll "$hWnd.scrX set" \
-yscroll "$hWnd.scrY set" \
-wrap none \
]
set hWndXScr \
[scrollbar $hWnd.scrX \
-orient horizontal \
-command "$hWndTxt xview" \
]
set hWndYScr \
[scrollbar $hWnd.scrY \
-orient vertical \
-command "$hWndTxt yview" \
]
set hWndBox \
[frame $hWnd.frBox \
-bd 1 \
-relief raised \
]
grid rowconfig $hWnd 0 -weight 1 -minsize 0
grid rowconfig $hWnd 1 -weight 0 -minsize 0
grid columnconfig $hWnd 0 -weight 1 -minsize 0
grid columnconfig $hWnd 1 -weight 0 -minsize 0
grid $hWndTxt -row 0 -column 0 -sticky news
grid $hWndYScr -row 0 -column 1 -sticky ns
grid $hWndXScr -row 1 -column 0 -sticky ew
grid $hWndBox -row 1 -column 1 -sticky news
MegaWidget $hWnd
return $hWnd
}
foreach textCmd [list bbox cget compare configure debug delete \
dlineinfo dump get image index insert mark scan search see \
tag window xview yview] {
proc $textCmd { hWnd args } "
return \[eval \$hWnd.txt $textCmd \$args\]
"
}
}
The license for this recipe is available here.
Discussion:
This script is a simple working example of use of the MegaWidget procedure also submitted. It functions API-wise essentially like a normal Tk "text" widget (though it's lacking support for configuration at creation time), but adds properly-aligned X and Y scrollbars. It could be further extended with additional commands, in a pseudo derived-class manner, by calling MegaWidget from another namespace (e.g. as ExtendedXYText), passing the name of the main frame widget as an argument.
|