#!/usr/bin/wish -f ## ## SCRIPT: make_chest_proto_gradientLinesInCanvases.tk ## ## in $FEDIR/tkGUIs/tkGUIs_DEMOS/feTkDEMOs/NEW_2012jul-aug ## ## where FEDIR = $HOME/apps/fehandytools_2011oct23 ##+########################################################################### ## DESCRIPTION: This Tk script ## ## MAKES TOOLCHEST DRAWERS WITH: 'canvas' widgets ## PROVIDES DRAWER ACTIONS WITH: button1-release bindings on the canvas widgets ## PROVIDES TOOLCHEST/DRAWER DECORATION WITH: 'create line' commands on ## the canvas widgets --- to provide COLOR GRADIENT BACKGROUNDS for ## the 'drawers' ; uses a 'DrawGradient' proc based on ## a Tcl-Tk script by B. Oakley and Damon Courtney and published at ## http://wiki.tcl.tk/6100 - Drawing color gradients. ##+########################################################################### ## REFERENCES: ## See similar toolchest-making test scripts in this series: ## ## 1) 'make_chest_proto_buttonsInFrames.tk' --- ## ## MAKES TOOLCHEST DRAWERS WITH: 'button' widgets ## PROVIDES DRAWER ACTIONS WITH: the '-command' parm of 'button' ## PROVIDES TOOLCHEST/DRAWER DECORATION WITH: 'tk_setPalette' command with ## an optional 'bullet' image on the left of each button/drawer. ## ## 2) 'make_chest_proto_imageCreateInCanvases_txt-can-arrays.tk' --- ## ## MAKES TOOLCHEST DRAWERS WITH: 'canvas' widgets ## PROVIDES DRAWER ACTIONS WITH: button1-release bindings on the canvas widgets ## PROVIDES TOOLCHEST/DRAWER DECORATION WITH: 'image create' on the ## canvas widgets, using a rectangular GIF image file that is used ## to make an image var with 'image create photo'. That image var ## is used in the 'image create' statements. ## ## The 'txt-can-arrays' script puts the text strings for the drawers in an ## array variable. Then, in a loop over the array argument, the canvases are ## defined-and-packed (with image and text put on each canvas). ## In another loop over the array argument, the bindings are defined. ## ## The 'txt-can-arrays' script also defines the canvas widgets as ## .can(1), .can(2), ... rather than .can1, .can2, ... ## ##+################################################################### ## Created: 2012jul25 ## Changed: 2012aug09 - Added DESCRIPTION and REFERENCES sections in ## the comments above. ## - Added comments for code statements and groups ## of code statements below. ## - Put the creation of the drawers in a 'while' loop ## using arrays for the text strings on the drawers ## and arrays for the canvas widget IDs. ## - Put the creation of the button1-release bindings ## for the canvas drawers in a 'while' loop. ## - Put the drawing of the color-gradients on the ## drawers and the placement of the text strings ## on the canvas drawers in a 'while' loop. ##+################################################################### ##+################################################# ## Set window titles, location, and resize behavior. ##+################################################# # wm title . "Toolchest-of-drawers-with-color-gradient-backgrounds - Test" # wm iconname . "ToolchestTest" wm title . "Apps Toolchest" wm iconname . "Apps" wm geometry . +150+30 wm resizable . 0 0 ##+######################################################## ## Set font to use for the text in the 'toolchest drawers'. ## (A variable width font, like that normally used ## on button widgets, would be appropriate/nice.) ##+######################################################## set feFONT_button "-family {comic sans ms} -size -14 -weight bold -slant roman" eval font create fontTEMP_drawer $feFONT_button ##+######################################################## ## Set the color-gradient parameters for the 'drawers', ## which are made with 'canvas' widgets in this script. ## ## The color gradients are drawn in the canvas widgets ## using 'create line' commands. ##+######################################################## set leftCOLOR yellow set rightCOLOR red set colorDIRECTION x ##+############################################################ ## Set a border width for the drawers --- which are like ## 'buttons', made with 'canvas' widgets in this script. ##+############################################################ # set BDwidth_canvas 4 set BDwidth_canvas 0 ##+######################################################################## ## Put the text strings --- that are to be put on the drawers ## (canvas widgets) --- in variables. ## ## In a 'production' script, these vars could be set by reading lines ## of a 'chest definition' file, for each toolchest. ## ## We changed vars ## textSTRING1, textSTRING2, ... ## to array variables ... ## textSTRING(1), textSTRING(2), ... ## elimnating the need to use 'eval' which, in turn, required escaping ## special characters like double-quotes, braces, and brackets. ##+######################################################################## set textSTRING(1) "Seamonkey - web browser" set textSTRING(2) "Thunderbird - email client" set textSTRING(3) "Filezilla - FTP client" set textSTRING(4) "mtpaint - image editor" set textSTRING(5) "SciTE - text editor" set textSTRING(6) "gnome-screenshot - image capture" set textSTRING(7) "gnome-terminal - command-interpreter window" set textSTRING(8) "gcalctool - calculator" set Ndrawers 8 ##+######################################################################## ## Set the width (in pixels) of the drawers --- according to the ## longest text string to be placed on the drawers (canvases). ## ## We use 'font measure' to get the string widths. ## Note that the font used for the text is a factor in determining the widths. ## ## We use the max-string-width and add about 10 pixels to get drawers (canvases) ## a little wider than the longest text string on the drawers. ##+######################################################################## set drawerCNT 1 set strMaxWidthPx 15 while { $drawerCNT <= $Ndrawers } { set strWidthPx [font measure fontTEMP_drawer "$textSTRING($drawerCNT)"] ## FOR TESTING: # puts "strWidthPx = $strWidthPx drawerCNT = $drawerCNT" # puts "$textSTRING($drawerCNT)" # puts "" if { $strWidthPx > $strMaxWidthPx } { set strMaxWidthPx $strWidthPx } set drawerCNT [expr $drawerCNT + 1] } set drawerWidthPx [expr $strMaxWidthPx + 10] ##+######################################################################## ## Set the drawer height (in pixels) --- according to the height of ## the text strings. ## (Depends on the font used for the strings. We use 'font metrics' ## to get the 'linespace' height.) ##+######################################################################## set drawerHeightPx [font metrics fontTEMP_drawer -linespace] ## FOR TESTING: # puts "drawerHeightPx = $drawerHeightPx" ##+######################################################################## ## Set the position in each canvas widget (relative to the top left of each ## canvas widget) at which the left side of each text string will be located. ## ## We add the borderwidth of the canvas onto the x and y text offsets. ## ## We use about 6 pixels of the x offset. 6 might be a bit much. ##+######################################################################## set yLocTextPx [expr $BDwidth_canvas + ($drawerHeightPx / 2)] set xLocTextPx [expr $BDwidth_canvas + 6] ##+############################################################ ## In a loop over the number of drawers, ## define and pack the canvas widgets (toolchest drawers). ## ## We put the canvas IDs into array vars .can(1), .can(2), ... ##+############################################################ set drawerCNT 1 while { $drawerCNT <= $Ndrawers } { canvas .can($drawerCNT) \ -relief raised \ -borderwidth $BDwidth_canvas \ -height $drawerHeightPx \ -width $drawerWidthPx pack .can($drawerCNT) \ -side top \ -anchor nw \ -fill none \ -expand 0 set drawerCNT [expr $drawerCNT + 1] } ## END OF 'while' LOOP to define-pack canvas 'drawers' ##+####################################################### ## Define BINDINGS: ## - one button1-release binding for each canvas drawer ##+####################################################### set drawerCNT 1 while { $drawerCNT <= $Ndrawers } { set drawerCMD "tk_dialog .dialog1 \ {Dear user:} {Toolchest drawer $drawerCNT was clicked} info 0 OK" ## FOR TESTING: # puts "drawerCMD: $drawerCMD" bind .can($drawerCNT) "$drawerCMD" set drawerCNT [expr $drawerCNT + 1] } ## END OF 'while' LOOP for bindings ##+################################################################ ## Define PROCS: ## - 'DrawGradient' to draw color gradient on each canvas drawer ##+################################################################ proc DrawGradient {win axis col1Str col2Str} { # if {[winfo class $win] != "Canvas"} { # return -code error "$win must be a canvas widget" # } # $win delete gradient set width [winfo width $win] set height [winfo height $win] switch -- $axis { "x" { set max $width; set x 1 } "y" { set max $height; set x 0 } default { return -code error "Invalid axis $axis: must be x or y" } } if {[catch {winfo rgb $win $col1Str} color1]} { return -code error "Invalid color $col1Str" } if {[catch {winfo rgb $win $col2Str} color2]} { return -code error "Invalid color $col2Str" } ## FOR TESTING: # puts "color1 = $color1" # puts "color2 = $color2" foreach {r1 g1 b1} $color1 break foreach {r2 g2 b2} $color2 break ## FOR TESTING: # puts "r1 = $r1" # puts "g1 = $g1" # puts "b1 = $b1" # puts "r2 = $r2" # puts "g2 = $g2" # puts "b2 = $b2" set rRange [expr $r2.0 - $r1] set gRange [expr $g2.0 - $g1] set bRange [expr $b2.0 - $b1] set rRatio [expr $rRange / $max] set gRatio [expr $gRange / $max] set bRatio [expr $bRange / $max] for {set i 0} {$i < $max} {incr i} { set nR [expr int( $r1 + ($rRatio * $i) )] set nG [expr int( $g1 + ($gRatio * $i) )] set nB [expr int( $b1 + ($bRatio * $i) )] set col [format {%4.4x} $nR] append col [format {%4.4x} $nG] append col [format {%4.4x} $nB] ## FOR TESTING: # puts "col = $col" if {$x} { $win create line $i 0 $i $height -tags gradient -fill #${col} } else { $win create line 0 $i $width $i -tags gradient -fill #${col} } } # bind $win [list DrawGradient $win $axis $col1Str $col2Str] # return $win } ## END OF proc DrawGradient ##+####################################################### ## Additional GUI INITIALIZATION: ## - draw color-gradients onto (and put text strings on) ## the canvas 'drawers' ##+####################################################### ## Before using the 'DrawGradient' proc to put the color ## gradients in the canvas 'drawers', we need to call ## 'update' to establish the width and height of ## the canvas widgets, because DrawGradient uses ## 'winfo' to get the width and height of each canvas. update ## Now we draw the color gradients and put on the text strings. set drawerCNT 1 while { $drawerCNT <= $Ndrawers } { DrawGradient .can($drawerCNT) $colorDIRECTION $leftCOLOR $rightCOLOR .can($drawerCNT) create text \ $xLocTextPx $yLocTextPx \ -anchor w \ -font fontTEMP_drawer \ -fill black \ -justify left \ -text "$textSTRING($drawerCNT)" set drawerCNT [expr $drawerCNT + 1] } ## END OF 'while' LOOP for drawing the color-gradient and putting text on drawers