Monday, April 16, 2012

gnocl::menuItem: new option -data and new sub-command cget

I'm working on a script that requires popup menus to be built proceedurall and on-the-fly. As a result, there are a number of parameters that I want to send to the popup callback but without the expense of creating and managing global variables or namespaces. This is where the widget -data option comes in. It means that data strings can be conveniently grouped together with a widget, managed throught the Gtk libraries which is totally (hopefully!) secure. The following code-snipetts shows how it can be used.
 
#---------------
## display popup menu
#---------------
#\code
proc doTagPopup {args} {
   
    gnocl::setOpts [list a 1 b 2 c 3]
    gnocl::setOpts $args
   
    puts "a= $a b= $b c= $c"
    puts "w = $w ; t = $t ; n =$n"
   
    foreach {arg val} $args { puts "$arg = [set [string trimleft $arg -]]" }

    #set matches OM|MANI|PADME|HUM|HRIH
    if {$t == "buttonPress" && $b == "3" } {
       
        # these may need runtime re-definition
        proc doItem { w } {
            gnocl::setOpts [$w cget -data]
            puts ">[$widget tag ranges $tag]<"
            foreach {a b} [$widget tag properties $tag] {
                puts "\t$a = $b"
                }
            }
       
        set menu [gnocl::menu -title "menu" -tearoff 0]
       
        foreach word [split $matches |] {
            $menu add [gnocl::menuItem \
                -text $word \
                -data "-widget $w -word $word -tag $n" \
                -onClicked { doItem %w }  ]
            }
        $menu popup $x $y   
    }   
}
#\endcode

No comments: