toolbar

TCL/TK UseCase4



Abstract : Sets up the floating tool bar window. Comments : New tools can be added to the tool bar without much effect at all. All you'll need to do is update the tools list variable with the tool type, button color, bitmap name, and description. (Bitmaps are stored in the /bitmaps folder of the code directory and you 'll have to create one there ....note: optional edge bitmaps are already there but aren't in use at this time.) Everything is then placed into the window and event bindings are automagically created and changed when the tool is selected and deselected. The program is runable....but the tool won't function. To add functionality to the new tool, make action procedures in the bindings.tcl file. Pay close attention to procedure names and how they are are callled. Action event procedures are called by testing to see if a procedure exists with a name relating to the event that took place (like mouse click) and which tool was selected at the time (mouse click with vertex tool.) If the procedure doesn't exist nothing happens. This is pretty slick and will allow you to add functionally to your new tool easily and quickly.
######################## # toolbar.tcl # : The file contains the code for the tool bar window. # : Adding new tools is very simple; add the tool name, # : color, bitmap name, and description name to the # : tools list variable in the same format. Then just # : add the binding event actions you wish to occur in the # : binding.tcl. YOU MUST choose procedure names that # : contain the name of the new tool. There is a special # : format to follow. Example of the format # : can be seen below in "proc toolChanged" # : and in edge.tcl & vertex.tcl in "proc createEdge" & # : "proc createVertex". Also look closely at how the # : binding procedures in "bindings.tcl" work. # # : Event procedures are based on the tool # : name. Special checks are done to see if # : there is a procedure for the action that # : happened and the tool that made the action. # : If the procedure exists, it does it. If it doesn't, # : it doesn't. ######################## # Tools list. (Add new tools here.) set tools { \ { Select black select.xbm "Selection"} \ { Constr green constr.xbm "Construction\nVertex"} \ { Alter green alter.xbm "Alternation\nVertex"} \ { Req_Constr_Edge green edgereq.xbm "Construction\nEdge"} \ { Req_Alter_Edge green edgealtreq.xbm "Alternation\nEdge"} \ { Rename red rename.xbm "Rename"} \ { Delete red trash.xbm "Destroy"} \ } # Select initial startup tool set tool_type Select # Select button style set style flat # Setting up a button proc createButton {name color file} { global style global INCLUDE global tool_type radiobutton .toolwin.toolframe.b_$name \ -relief $style \ -variable tool_type \ -value $name \ -foreground $color \ -bitmap "@$INCLUDE/$file" \ -command "set tool_type $name" \ -background lightblue } # Change binding to match current tool. When a tool is changed # a check occurs to see if there is an action (in the binding.tcl usually) for # the particular event for the particular tool. # If the action for that event doesn't exist # the binding is set to null. If the action does exist, the event is # then bound to that event. This allows for easy tool creatation and # event procedures to be created for a new button because the action # is called based on the name of the tool selected. # NOTE: You will NOT need to change this procedure if you add a # new tool. Just add the properly named procedure to the bindings.tcl # and the tool will work. proc toolChanged {name} { puts "ToolChanged called '$name'" global tool_type set tool_type $name # See the bindings.tcl for the procedures named here. if {[info procs onBtn1Down_Node_$name] != ""} { .area bind node "onBtn1Down_Node_$name %x %y" } else { .area bind node {} } # if {[info procs onBtn1Up_Node_$name] != ""} { # .area bind node "onBtn1Up_Node_$name %x %y" # } else { # .area bind node {} # } # if {[info procs onBtn1Move_Node_$name] != ""} { # .area bind node "onBtn1Move_Node_$name %x %y" # } else { # .area bind node {} # } if {[info procs onBtn1Down_Canvas_$name] != ""} { bind .area "onBtn1Down_Canvas_$name %x %y" } else { bind .area {} } if {[info procs onBtn1Up_Canvas_$name] != ""} { bind .area "onBtn1Up_Canvas_$name %x %y" } else { bind .area {} } if {[info procs onBtn1Move_Canvas_$name] != ""} { bind .area "onBtn1Move_Canvas_$name %x %y" } else { bind .area {} } if {[info procs onMoving_Canvas_$name] != ""} { bind .area "onMoving_Canvas_$name %x %y" } else { bind .area {} } } # Binding procedure for displaying tool name when mouse is over tool. proc bindButton {buttonname showname} { bind .toolwin.toolframe.b_$buttonname \ "shoutit {$showname}" bind .toolwin.toolframe.b_$buttonname \ "toolChanged $buttonname" } # Make window, buttons, and bindings proc init_tools {} { global tool_type global tools global style # Make the window. toplevel .toolwin wm transient .toolwin . wm title .toolwin "DemTools" frame .toolwin.toolframe # Make the buttons foreach i $tools { createButton [lindex $i 0] [lindex $i 1] [lindex $i 2] } # Set the binding for displaying tool names foreach i $tools { bindButton [lindex $i 0] [lindex $i 3] } # Create text box that displays tool name. text .toolwin.toolframe.text -height 3 -width 15 -background purple # Pack the buttons in the window pack .toolwin.toolframe foreach i $tools { set name [lindex $i 0] pack .toolwin.toolframe.b_$name -fill x } pack .toolwin.toolframe.text -fill x } # Cool "zipping lights" function at start up. proc flashy { speed } { global tool_type global tools foreach i $tools { set tool_type [lindex $i 0] after $speed # Make sure screen is updated before moving on. update } for {set i [expr [llength $tools] - 2 ]} \ {$i >= 0} {incr i -1} { set j [lindex $tools $i] set tool_type [lindex $j 0] after $speed # Make sure screen is updated before moving on. update } } # Display tool names in the window proc shoutit { passing } { .toolwin.toolframe.text delete 1.0 end .toolwin.toolframe.text insert end "Description:\n$passing" } # Main procedure to launch the toolbar. proc setup_tools {} { init_tools # Make sure buttons are showing before doing cool flashy procedure. update # Cool initial zipping lights on buttons. flashy 50 # Display tool in use when mouse leaves window. shoutit "Selection" bind .toolwin { global tool_type foreach i $tools { if {[lindex $i 0] == $tool_type} { shoutit [lindex $i 3] } } } }