#!/usr/bin/wish -f ## ## SCRIPT: make_chest_proto_imageCreateInCanvases_txt-can-arrays.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: 'image create' for a background ## image on the canvas widgets. ## A rectangular GIF image file is used to make an image var ## with 'image create photo'. That image var is used in ## 'image create' statements to put the background on the drawers. ## ## This script puts the text strings for the buttons in an array variable. ## Then, in a loop over the array argument (drawer count), 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. ## ## In addition to putting the text strings in an array variable, ## this script defines the canvas widgets as .can(1), .can(2), ... ## rather than .can1, .can2, ... ##+############################################################################ ## 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 for a ## color theme for the buttons ... and ## an (optional) 'bullet' image on the left of each button/drawer. ## ## 2) 'make_chest_proto_gradientLinesInCanvases.tk' --- ## ## MAKES TOOLCHEST DRAWERS WITH: 'canvas' widgets ## PROVIDES DRAWER ACTIONS WITH: button-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 Damon Courtney and published at ## http://wiki.tcl.tk/6100 - Drawing color gradients. ## ##+########################################################################## ## Created: 2012aug08 Made this script based on the script ## 'make_chest_proto_imageCreateInCanvases_txtarray.tk' ## which differs from this script mainly because in that ## script the canvas widgets are defined with 'eval' ## via statements like ## 'eval canvas .can$drawerCNT ...' ## ## In contrast, this script eliminates the use of 'eval' ## by using statements like ## 'canvas .can($drawerCNT) ...'. ## ## This --- using .can($drawerCNT) rather than .can$drawerCNT ## --- eliminates the need to escape special chars like ## double-quotes, braces, and brackets --- but it seems ## to create a small increase in CPU usage (about 5%-10%) ## --- as measured using the 'time' Linux/Unix command ## and looking at the 'user' seconds output. ## ## This CPU-increase observation is based on 'eyeball' ## comparisons of multiple runs of the 'txtarray' script to ## multiple runs of this 'txt-can-array' script. But it might ## take many runs with rigorous averaging of the results ## to several significant digits to make a solid determination ## of the % difference. There is enough variablility ## in the results to indicate there will ## not be enough difference to strongly favor either ## technique, solely on the basis of CPU usage. ## ## Using arrays to eliminate the use of 'eval' (and ## all the complex 'escaping' of special characters) ## might be some people's preference, even if there is ## a minor penalty in slightly increased CPU usage in ## using the 'arrays technique'. ## ## Changed: 2012aug09 Changed the order of some of the groups of code statements. ##+######################################################################### ##+######################################################################## ## Set the window parms - titles, location, size(ability), etc. ##+######################################################################## # wm title . "Toolchest-of-drawers-with-a-common-image-background - Test" # wm iconname . "ToolchestTest" wm title . "Apps Toolchest" wm iconname . "Apps" wm geometry . +150+30 wm resizable . 0 0 ##+########################################################### ## Create a font to use for text on the drawers. ## ## A variable width font, like that typically used on 'button' ## widgets, provide an appropriate/nice appearance. ##+########################################################### set feFONT_button "-family {comic sans ms} -size -14 -weight bold -slant roman" eval font create fontTEMP_drawer $feFONT_button ##+######################################################################### ## Establish an image to use as a background on the drawers (canvas widgets). ## ## This image should be very wide and be trimmed to a little more than ## the width (in pixels) of the longest text string of all the text strings ## to be placed on the drawers. ## ## We will place the image on the canvas widgets and use the '-width' ## parameter on the canvas commands to 'clip' the background image on ## the drawers (canvas widgets). ##+######################################################################## # image create photo IMG4drawerBkgnd -file "${DIR_gif_files}/$gif_img_file" # image create photo IMG4drawerBkgnd -file "./buttonBkgnd_glass_blue_A_1130x24.gif" # image create photo IMG4drawerBkgnd -file "./buttonBkgnd_glossy_orange_1024x24.gif" # image create photo IMG4drawerBkgnd -file "./button_FractalCave_by_EliteSniper_641x26.gif" # image create photo IMG4drawerBkgnd -file "./button_electronMicrograph_verticalLines_purple_913x25.gif" image create photo IMG4drawerBkgnd -file "./button_reliefMap_eastTexas_628x26.gif" ##+############################################################## ## Get the height (in pixels) of the drawer-background image. ## ## This MAY be used for the height of the drawers (canvas widgets). ## Another drawer-height option: the text height for a given font. ##+############################################################## set imgHeightPx [image height IMG4drawerBkgnd] ##+######################################################################## ## Set the border width to be used for each drawer (canvas widget). ##+######################################################################## # set BDwidth_canvas 4 set BDwidth_canvas 0 ##+######################################################################## ## Put the text strings to be put on the drawers (canvas widgets) ## in variables. ## ## We changed variables ## textSTRING1, textSTRING2, ... ## to array variables ... ## textSTRING(1), textSTRING(2), ... ## ## In a 'production' script, these vars could be set by reading lines ## of a 'chest definition' file, for each toolchest. ##+######################################################################## 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 of all the drawers (in pixels) --- according to the ## longest text string to be placed on the drawers (canvas widgets). ## ## We use 'font measure' to determine the text widths. ## Note that the font used for the text is a factor in determining the width. ## ## We get the max-string-width and add about 10 pixels to set a drawer-width ## 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 height (in pixels) of the text strings, using 'font metrics' with ## the '-linespace' parm. This MIGHT be used to set drawer (canvas) height. ## ## Note that the text height depends on the font used for the strings. ##+######################################################################## set maxTextHeightPx [font metrics fontTEMP_drawer -linespace] ## FOR TESTING: # puts "maxTextHeightPx = $maxTextHeightPx" ##+######################################################################## ## Set the height of the canvas drawers (in pixels) from either ## the background image height or the 'linespace' height of the text strings. ##+######################################################################### ## Text height is NOT USED at this time. ## For the height of the drawers (canvas widgets), ## we will use the height of the image chosen for the background of the ## buttons. But we might use text-height in the future --- to 'trim' the ## height of the canvas drawers. ##+######################################################################### set drawerHeightPx $imgHeightPx # set drawerHeightPx $maxTextHeightPx ##+######################################################################## ## 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 adjust the x text offset according the the width of the ## border of the canvas --- so that the text does not lie on the border. ##+######################################################################## set xLocTextPx [expr $BDwidth_canvas + 6] # set yLocTextPx [expr $BDwidth_canvas + ($drawerHeightPx / 2)] set yLocTextPx [expr $drawerHeightPx / 2] # set yLocTextPx [expr $maxTextHeightPx / 2] # set yLocTextPx 6 ##+######################################################################## ## Loop to make the toolchest drawers. Each pass through the loop ## - Defines a canvas widget of appropriate width, height, border-width. ## - Puts a background image on the canvas. ## - Puts a text string on the canvas 'drawer'. ## - Packs the canvas widget in the parent window. ##+######################################################################## set drawerCNT 1 while { $drawerCNT <= $Ndrawers } { canvas .can($drawerCNT) \ -relief raised \ -borderwidth $BDwidth_canvas \ -height $drawerHeightPx \ -width $drawerWidthPx .can($drawerCNT) create image 0 0 \ -image IMG4drawerBkgnd \ -anchor nw .can($drawerCNT) create text \ $xLocTextPx $yLocTextPx \ -anchor w \ -font fontTEMP_drawer \ -text "$textSTRING($drawerCNT)" pack .can($drawerCNT) \ -side top \ -anchor nw \ -fill none \ -expand 0 set drawerCNT [expr $drawerCNT + 1] } ## END OF 'while' LOOP to define-pack canvas widgets ##+######################################################## ## Loop to set BINDINGS for the drawers (canvas widgets). ## ## We use a tk_dialog popup to verify that the ## binding works as desired. ##+######################################################## 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 to set BINDINGS on the canvas widgets ##+################################## ## Define PROCS: none ##+################################## ##+################################## ## Additonal GUI INITIALIZATION: none ##+##################################