Welcome, guest | Sign In | My Account | Store | Cart

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).

Tcl, 79 lines
 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
package require MegaWidget
package provide XYText 1.0

namespace eval XYText {

#-----------------------------------------------------------------------------
#
#   XYText::Create
#
#   Creates an mega-widget with a contained text widget and X and Y
#   scrollbars.
#
#   Returns     :   Name of the main frame containing the XYText widget
#
#   Parameters  :
#       hWnd    :   Name of the main frame of the XYText widget
#       args    :   Options for the text widget (not currently used)
#
#   Side Effects:   Creates a frame containing text and scrollbar widget.
#
#----------------------------------------------------------------------------

    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
    }

    # Create XYText MegaWidget commands to be passed on to the text widget.

    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\]
        "
    }
}

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.

Created by Michael Kirkham on Thu, 25 Apr 2002 (MIT)
Tcl recipes (162)
Michael Kirkham's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks