uniquename - 2013dec10

On my 'bio' page at uniquename, I have a 'to do' list in the 'CFE' section --- in a bigger section at the bottom of that page, a section of Tk 'scripts implemented or to-do'.

The 'CFE' stands for 'Code for Front Ends'. The 'to-do' scripts in that section are 'start up' front ends for utility programs like 'mplayer', 'ffmpeg', 'find', 'du' (directory usage), and others.

In sketching out the GUI layouts for programs like 'mplayer', 'ffmeg', and 'find', I found that there are so many command-line options for these programs that the GUI's are going to contain a lot of widgets --- and they will probably require multiple 'panels' of 'prompting widgets'.

In doing my first 'CFE' script, I wanted to start out with a GUI that was not quite so complex.

In the past year, I have made some shell scripts for sweeping through sub-directories and selecting image files to display with a simple image display program --- like Image Magick 'display' --- or 'ffplay' --- or 'eog' (Eye of Gnome) --- on Linux. These scripts use the 'find' command to sweep through sub-directories, looking for image files.

I decided to layout a Tk GUI as a 'front end' for such an image-viewing utility. It turned out that the GUI could fit on one screen --- without using 'multiple panels of options'.

My 'text-sketch' for the GUI (after several revisions) was as follows.

  ------------------------------------------------------------------
  'display'/'ffplay' Front End  --- a Tk Image Viewer Utility  [window title]
  ------------------------------------------------------------------

  {Exit} {Help} {LaunchViewerJob}      {CountFilenames} {ShowFilenames}

  Full Filename Mask (for image/s): ___________________  {Browse...}

  Viewer program : O ImageMagick 'display'    O 'ffplay'   O 'eog'

  Search Levels for Mask:  O ONE   O ALL subdirectories of selected dir

  Case Sense for Mask Search:  O case-sensitive  O case-INsensitive

  Files Size (MegaBytes):  _____  O bigger-than  O smaller-than

  Files Age (Days) : ______ O older-than  O younger-than

  --------------------------------------------------------

where

   Braces indicate a Tk 'button' widget.
   Underscores indicate a Tk 'entry' widget.
   A colon indicates that the text before the colon is on a 'label' widget.
   Capital-O indicates a Tk 'radiobutton' widget.
   Capital-X indicates a Tk 'checkbutton' widget.
   Square brackets indicate a comment (not to be placed on the GUI).

---

GUI Components

From the GUI 'sketch' above, it is seen that the GUI consists of about

   -  5 button widgets
   -  6 label widgets
   -  3 entry widgets
   - 11 radiobutton widgets in 5 groups
   -  0 checkbutton widgets
   -  0 scale widgets
   -  0 listbox widgets
   -  0 canvas widgets

This is relatively simple compared to the front end GUI's that I plan to use for 'mplayer', 'ffmpeg', and 'find'.

---

Front-End-for-Two ; the Viewers ; Beauty (or lack thereof)

NOTE that only 'Viewer Program' is a parameter involving the image viewer. The other parameters are for the 'find' command.

So this 'Front End' Tk script is essentially a 'wrapper' for TWO commands --- the command that implements the image viewer AND the 'find' command.

I was looking to use suitable commands that are available on my operating system (Ubuntu 9.10, 2009 October, 'Karmic Koala'). I found that the 'find' command in combination with a simple image viewer program would 'fill the bill'.

The 'display', 'ffplay' and 'eog' programs come close to working OK as an image viewer --- but each one has a few drawbacks. So I still have my eye out for another simple image viewer program to use, as mentioned further below.

I should point out here that I was not especially interested in coming up with a 'beautiful utility'. I just wanted a utility that would be able to sweep through a hierarchy of sub-directories and pick out image files to show based on a rather 'rich' set of selection capabilities --- such as 'file-mask' and/or 'file-size' and/or 'file-age'.

(I may add a 'file-type' capability based on using the 'file' command, in a future enhancement to this utility.)


SOME GUI IMAGES

On the basis of the sketch above, I ended up with the GUI seen in the following image.

tkImageViewer_initial_screenshot_576x242.jpg

Note that there are a couple of radiobuttons that allow you to choose whether application of the file-mask is 'case-sensitive' or 'case-INsensitive'. For example, if you chose 'case-INsensitive' and the mask were set to '*.jpg', then files with the suffix '.JPG' would also be selected.

And there are a couple of radiobuttons that allow you to choose whether to do the 'mask-search' at the current directory level only --- or to sweep through all sub-directories of the current 'base' directory, looking for images that satisfy the criteria chosen via this GUI.

Here is another place where I MAY make a future enhancement: I may change the 'ONE' radiobutton to an 'N-levels' radiobutton --- and if that radiobutton is selected, an entry field will be activated where the user can enter a choice of N (with the entry field being initialized with '1').

---

Here is another image of the GUI, showing a different setting of most of the radiobuttons --- and entries placed in the file-size and file-age entry fields.

tkImageViewer_radbuttChanges_screenshot_575x241.jpg

NOTE that most image-viewers, like 'eog' and more complex image viewers, are oriented toward scanning through ALL image files in a SINGLE directory.

This utility is oriented toward SELECTING image files (based on criteria such as a file-mask, file-size, and/or file-age) from an ENTIRE HIERARCHY OF SUB-DIRECTORIES --- as well as allowing mask-search in a SINGLE directory.

Furthermore, this utility can be used to view a single file. Just select a full filename and do not change the last part of the filename to a mask. Then click the 'Launch' button.

---

This utility can be useful without even using any of the viewer programs. Say you want to know the number or names of the image files satisfying a set of criteria (file-mask, file-size, file-age). Then set the criteria and click on the 'CountFilenames' or 'ShowFilenames' button.


The code

Below, I provide the Tk script code for this 'multi-subdirectory tkImageViewer' utility.

I follow my usual 'canonical' structure for Tk code for this Tk script:

  0) Set general window & widget parms (win-name, win-position,
     win-color-scheme, fonts, widget-geometry-parms, win-size-control,
     text-array-for-labels-etc).

  1a) Define ALL frames (and sub-frames, if any).
  1b) Pack   ALL frames and sub-frames.

  2) Define & pack all widgets in the frames, frame by frame.
              Within each frame, define ALL the widgets.
              Then pack the widgets.

  3) Define keyboard and mouse/touchpad/touch-sensitive-screen action
     BINDINGS, if needed.

  4) Define PROCS, if needed.

  5) Additional GUI initialization (typically with one or more of
     the procs), if needed.

This Tk coding structure is discussed in more detail on the page A Canonical Structure for Tk Code --- and variations.

This structure makes it easy for me to find code sections --- while generating and testing a Tk script, and when looking for code snippets to include in other scripts (code re-use).

I call your attention to step-zero. One thing that I have started doing in 2013 is using a text-array for text in labels, buttons, and other widgets in the GUI. This can make it easier for people to internationalize my scripts. I will be using a text-array like this in most of my scripts in the future.


Experimenting with the GUI

As in all my scripts that use the 'pack' geometry manager (which is all of my 100-plus scripts, so far), I provide the four main pack parameters --- '-side', '-anchor', '-fill', '-expand' --- on all of the 'pack' commands for the frames and widgets.

That helps me when I am initially testing the behavior of a GUI (the various widgets within it) as I resize the main window.

I think that I have used a pretty nice choice of the 'pack' parameters. The label and button and radiobutton widgets stay fixed in size and relative-location if the window is re-sized --- while the entry widgets expand/contract horizontally whenever the window is re-sized horizontally.

You can experiment with the '-side', '-anchor', '-fill', and '-expand' parameters on the 'pack' commands for the various frames and widgets --- to get the widget behavior that you want.

___

Additional experimentation: You might want to change the fonts used for the various GUI widgets. For example, you could change '-weight' from 'bold' to 'normal' --- or '-slant' from 'roman' to 'italic'. Or change font families.

In fact, you may NEED to change the font families, because the families I used may not be available on your computer --- and the default font that the 'wish' interpreter chooses may not be very pleasing.

I use variables to set geometry parameters of widgets --- parameters such as border-widths and padding. And I have included the '-relief' parameter on the definitions of frames and widgets. Feel free to experiment with those 'appearance' parameters as well.

If you find the gray 'palette' of the GUI is not to your liking, you can change the value of the RGB parameter supplied to the 'tk_setPalette' command near the top of the code.


Some features in the code

That said, here's the code --- with plenty of comments to describe what most of the code-sections are doing.

You can look at the top of the PROCS section of the code to see a list of the procs used in this script, along with brief descriptions of how they are called and what they do.

The main procs are

  'get_filemask'         - called by the 'Browse...' button
                           next to the filemask entry field

  'launch_imageViewer'   - called by the 'LaunchViewJob', 'CountFilenames',
                           and 'ShowFilenames' buttons

  'popup_msgVarWithScroll' - called by 'Help' button to show HELPtext var.
                             Also used via the 'CountFilenames' and
                             'ShowFilenames' buttons.

One unique thing about this Tk GUI that is different from the 40-plus scripts that I have contributed (so far) to this wiki:

I used the following statement to allow the GUI to be expanded in the x-direction, but NOT the y-direction.

   wm resizable . 1 0

It is my hope that the copious comments in the code will help Tcl-Tk coding 'newbies' get started in making GUI's like this.

Without the comments, potential young Tcler's might be tempted to return to their iPhones and iPads and iPods --- to watch videos of puppies and kittens.

That's nice. BUT ... How is that going to help the world develop the engineers, computer-scientists, and other scientists that are needed in a very challenging future world? (We need people to help deal with that asteroid that is on a collision course with the Earth --- perhaps sooner than most people think.)


 Code for Tk script 'imageViewer_FrontEnd.tk' : (the 'front end')
#!/usr/bin/wish -f
##
## Tk SCRIPT NAME: imageViewer_FrontEnd.tk
##
##+#######################################################################
## PURPOSE:  This Tk script provides a GUI for starting up an image file
##           viewer program --- like the ImageMagick 'display' program or
##           the 'ffplay' program or the 'eog' (Eye of Gnome) program ---
##           according to user-specified parameters that are prompted for
##           via radiobutton widgets and entry widgets on the GUI.
##
##           The GUI is currently coded as a front-end for starting up
##           the 'display' or 'ffplay' commands (along with use of the
##           'find' command).
##
##           The GUI is intended to make the user aware of the main
##           (most useful) options available without the user needing
##           to reference the 'man display'/'man ffplay'/'man eog'
##           and 'man find' commands and other 'display'/'ffplay'/'eog'
##           and 'find' documentation.
##
##           The intent of this utility is to allow the user to
##           easily specify files to be viewed --- via specifying:
##            1  - a directory-and-filename (the latter can be a mask)
##            2  - which of 2 or 3 viewer programs to use
##            3  - whether files in sub-directories should be mask-searched
##            4  - whether the mask-search should be case-INsensitive
##            5  - whether to use a file-size limitation
##            6  - whether to use a file-age limitation.
##
##           NOTE that most image-viewers, like 'eog' and more complex
##           image viewers, are oriented toward scanning through ALL
##           image files in a SINGLE directory.
##
##           This utility is oriented toward SELECTING image files (based
##           on criteria such as a file-mask, file-size, and/or file-age)
##           from an ENTIRE HIERARCHY OF SUB-DIRECTORIES --- as well as
##           allowing mask-search in a SINGLE directory. 
##            
##+######################
## NOTES ON OTHER VIEWERS:
##           As an alternative to 'eog', it would be easy to use
##           the 'Eye of MATE' ('eom') image viewer instead. 'eom' is the
##           image viewer that the MATE desktop project forked from
##           'eog'. Reference: www.mate-desktop.org
##
##           Circa 2010, some Argentinians decided to fork the
##           Gnome 2.x desktop environment --- when the Gnome 3
##           project abandoned many of the features of Gnome 2 and its
##           associated applications, like the 'Nautilus' file
##           manager and the 'Eye of Gnome' image viewer. (The fork
##           of the 'Nautilus' file manager is called 'Caja' --- Spanish
##           for 'box' or 'gift'.)
##
##           Of course, still other 'light-weight' image viewers could be
##           used with very few changes to this system. The 'core' changes would
##           be to the shell script --- 'start_imageViewer.sh' --- that this
##           Tk script calls. That shell script currently uses 'display'
##           or 'ffplay', along with the 'find' command --- and along with
##           parameters supplied from this GUI.
##
##+#################
## THE GUI WIDGETS:
##           The GUI offers a generous set of user specifications.
##           The options available to the user are compactly indicated
##           by the following 'sketch' of the GUI:
##
##  ------------------------------------------------------------------
##  'display'/'ffplay' Front End  --- a Tk Image Viewer Utility  [window title]
##  ------------------------------------------------------------------
##
##  {Exit} {Help} {LaunchViewerJob}      {CountFilenames} {ShowFilenames}
##  
##  Full Filename Mask (for image/s): ___________________  {Browse...}
##  
##  Viewer program : O ImageMagick 'display'    O 'ffplay'   O 'eog'
##  
##  Search Levels for Mask:  O ONE   O ALL subdirectories of selected dir
##  
##  Case Sense for Mask Search:  O case-sensitive  O case-INsensitive
##  
##  Files Size (MegaBytes):  _____  O bigger-than  O smaller-than
##  
##  Files Age (Days) : ______ O older-than  O younger-than
##
## --------------------------------------------------------
##
## Braces indicate a Tk 'button' widget.
## Underscores indicate a Tk 'entry' widget.
## A colon indicates that the text before the colon is on a 'label' widget.
## Capital-O indicates a Tk 'radiobutton' widget.
## Capital-X indicates a Tk 'checkbutton' widget.
## Square brackets indicate a comment (not to be placed on the GUI).
##
## NOTE that only 'Viewer Program' is a parameter involving the image viewer.
## The other parameters are for the 'find' command.
##
##+##############
## GUI components:
##
## From the GUI 'sketch' above, it is seen that the GUI consists of
## about
##   
##   -  5 button widgets
##   -  6 label widgets
##   -  3 entry widgets
##   - 11 radiobutton widgets in 5 groups
##   -  0 checkbutton widgets
##   -  0 scale widgets
##   -  0 listbox widgets
##
##+#####################################################################
## CALLED BY:  This script could be put in a sub-directory of the
##             user's home directory, such as $HOME/apps/tkImgViewer.
##
##             Then the user can use their desktop system (such as
##             Gnome or KDE) to set up the script as an icon on the
##             desktop. Then the user can click on the icon to
##             startup the script.
##+########################################################################
## STRUCTURE OF THIS CODE:
##
##  0) Set general window parms (win-name, win-position, win-color-scheme,
##     fonts, widget-geom-parms, win-size-control, text-array-for-labels-etc).
##
##  1a) Define ALL frames (and sub-frames, if any).
##  1b) Pack the frames.
##
##  2) Define & pack all widgets in the frames, frame by frame.
##     After all the widgets for a frame are defined, pack them in the frame.
##
##  3) Define keyboard and/or mouse/touchpad/touch-sensitive-screen 'event'
##     BINDINGS, if needed.
##  4) Define PROCS, if needed.
##  5) Additional GUI INITIALIZATION (typically with one or more of
##     the procs), if needed.
##
## In more detail:
##
##  1a) Define ALL frames -- and sub-frames:
## 
##   Top-level :
##       'fRbuttons'     for Exit, Help, Launch, ... buttons
##       'fRfileMask'    for a directory-and-filemask entry field
##       'fRdisplayPgm'  for 2 or 3 radiobuttons, with a label
##       'fRlevels'      for 2 radiobuttons, with a label
##       'fRsense'       for 2 radiobuttons, with a label
##       'fRfileSize'    for 1 entry field and 2 radiobuttons, with a label
##       'fRfileAge'     for 1 entry fields and 2 radiobuttons, with a label
##
##  1b) Pack ALL frames, including sub-frames (if any).
##
##  2) Define & pack all widgets in the frames -- basically going through
##     frames & their interiors in  left-to-right, top-to-bottom order:
##
##  3) Define bindings:  See BINDINGS section below.
##
##  4) Define procs:
##
##    'get_filemask'          - called by the 'Browse...' button
##
##    'launch_imageViewer'    - called by the 'Launch' button.                    
##
##   'popup_msgVarWithScroll' - called by 'Help' button to show HELPtext var.
##
##     For other procs, see the PROCS section below.
##
##  5) Additional GUI initialization:  See this section at the bottom
##                                     of this script.
##+#######################################################################
## DEVELOPED WITH: Tcl-Tk 8.5 on Ubuntu 9.10 (2009-october, 'Karmic Koala')
##
##   $ wish
##   % puts "$tcl_version $tk_version"
##
## showed
##     8.5 8.5
## but this script should work in most previous 8.x versions, and probably
## even in some 7.x versions (if font handling is made 'old-style').
##+########################################################################
## MAINTENANCE HISTORY:
## Started by: Blaise Montandon 2013dec08 Started development, on Ubuntu 9.10,
##                                        based on the code of a Tk script
##                                        of mine that contains most of the
##                                        widgets needed.
## Updated by: Blaise Montandon 2013 
##+#######################################################################

##+######################################################
## Set WINDOW TITLE and POSITION.
##+######################################################

wm title    . "'display'/'ffplay' Front End - a Tk Image Viewer Utility"
wm iconname . "tkImgView"

# wm geometry . +15+30
# wm geometry . +250+285
wm geometry . -10-10


##+######################################################
## Set the COLOR SCHEME for the window and its widgets ---
## such as listbox and entry field background color.
##+######################################################

tk_setPalette "#e0e0e0"


  set entryBKGD "#ffffff"
  set textBKGD  "#f0f0f0"
  set radbuttBKGD  "#ffffff"
# set chkbuttBKGD  "#ffffff"
# set scaleBKGD   "#f0f0f0"
# set listboxBKGD "#f0f0f0"


##+########################################################
## DEFINE (temporary) FONT NAMES.
##
## We use a VARIABLE-WIDTH font for text on LABEL and
## BUTTON widgets.
##
## We use a FIXED-WIDTH font for LISTBOX lists,
## for text in ENTRY fields --- and often for text in
## TEXT widgets.
##+########################################################

font create fontTEMP_varwidth \
   -family {comic sans ms} \
   -size -14 \
   -weight bold \
   -slant roman

font create fontTEMP_SMALL_varwidth \
   -family {comic sans ms} \
   -size -12 \
   -weight bold \
   -slant roman

## Some other possible (similar) variable width fonts:
##  Arial
##  Bitstream Vera Sans
##  DejaVu Sans
##  Droid Sans
##  FreeSans
##  Liberation Sans
##  Nimbus Sans L
##  Trebuchet MS
##  Verdana


font create fontTEMP_fixedwidth  \
   -family {liberation mono} \
   -size -14 \
   -weight bold \
   -slant roman

font create fontTEMP_SMALL_fixedwidth  \
   -family {liberation mono} \
   -size -12 \
   -weight bold \
   -slant roman

## Some other possible fixed width fonts (esp. on Linux):
##  Andale Mono
##  Bitstream Vera Sans Mono
##  Courier 10 Pitch
##  DejaVu Sans Mono
##  Droid Sans Mono
##  FreeMono
##  Nimbus Mono L
##  TlwgMono


##+###########################################################
## SET GEOM VARS FOR THE VARIOUS WIDGET DEFINITIONS.
## (e.g. width and height of canvas, and padding for Buttons)
##+###########################################################

## LABEL widget geom settings:

set PADXpx_label 0
set PADYpx_label 0
set BDwidthPx_label 2


## BUTTON widget geom settings:

set PADXpx_button 0
set PADYpx_button 0
set BDwidthPx_button 2


## ENTRY widget geom settings:

set BDwidthPx_entry 2


## RADIOBUTTON widget geom settings:

set PADXpx_radbutton 0
set PADYpx_radbutton 0
set BDwidthPx_radbutt 2


## CHECKBUTTON widget geom settings:

# set PADXpx_chkbutton 0
# set PADYpx_chkbutton 0
# set BDwidthPx_chkbutt 2

## TEXT widget geom settings:

set BDwidthPx_text 2


## SCALE widget geom parameters:

# set BDwidthPx_scale 2
# set scaleThicknessPx 10


##+######################################################################
## Set a MIN-SIZE of the window (roughly).
##
## For WIDTH, allow for the min-width of the '.fRbuttons' frame.
##
## For HEIGHT, allow for the stacked frames:
##      1 char   high for the '.fRbuttons'     frame
##      1 char   high for the '.fRfileMask'    frame
##      1 char   high for the '.fRdisplayPgm'  frame
##      1 char   high for the '.fRlevels'      frame
##      1 char   high for the '.fRsense'       frame   
##      1 char   high for the '.fRfileSize'    frame
##      1 char   high for the '.fRfileAge'     frame
##    --------
##      7 chars  high for the 7 frames
##+#####################################################################

## FOR WIDTH: (allow for widgets in the '.fRbuttons' or '.fRfileMask' frame)

set minWidthPx [font measure fontTEMP_varwidth \
   " Exit  Help  LaunchViewerJob   CountFilenames ShowFilenames "]

## We add some pixels to account for right-left-size of
## window-manager decoration (~8 pixels) and some pixels for
## frame/widget borders (~5 widgets x 4 pixels/widget = 20 pixels).

set minWinWidthPx [expr {28 + $minWidthPx}]


## For HEIGHT --- for
##      1 char   high for the '.fRbuttons'     frame
##      1 char   high for the '.fRfileMask'    frame
##      1 char   high for the '.fRdisplayPgm'  frame
##      1 char   high for the '.fRlevels'      frame
##      1 char   high for the '.fRsense'       frame
##      1 char   high for the '.fRfileSize'    frame
##      1 char   high for the '.fRfileAge'     frame 
##    --------
##      7 chars  high for the 7 frames

set charHeightPx [font metrics fontTEMP_varwidth -linespace]

set minWinHeightPx [expr {7 * $charHeightPx}]


## Add about 20 pixels for top-and-bottom window decoration --
## and some pixels for top-and-bottom of frame/widget borders
## (~7 widgets x 4 pixels/widget = 28 pixels).

set minWinHeightPx [expr {48 + $minWinHeightPx}]


## FOR TESTING:
#   puts "minWinWidthPx = $minWinWidthPx"
#   puts "minWinHeightPx = $minWinHeightPx"

wm minsize . $minWinWidthPx $minWinHeightPx


## We may allow the window to be resizable.  We pack the canvases
## (and the frames that contain them) with '-fill both -expand 1'
## so that the canvases can be enlarged by enlarging the window.

## If you want to make the window un-resizable, 
## you can use the following statement.
#   wm resizable . 0 0

## We fix the y-size of the window, but allow the x-size to vary.
wm resizable . 1 0

##+##############################################################
## Set a TEXT-ARRAY to hold text for buttons & labels on the GUI.
##     NOTE: This can aid INTERNATIONALIZATION. This array can
##           be set according to a nation/region parameter.
##+##############################################################

## if { "$VARlocale" == "en"}

## For '.fRbuttons' frame:

set aRtext(buttonEXIT)    "Exit"
set aRtext(buttonHELP)    "Help"
set aRtext(buttonLAUNCH)  "LaunchViewerJob"
# set aRtext(buttonKILLJOB) "KillViewerJob"

set aRtext(buttonCOUNT)  "CountFilenames"
set aRtext(buttonPRINT)  "ShowFilenames"

## For '.fRfileMask' frame:

set aRtext(labelFILEMASK) "Full Filename Mask (for image/s):"
set aRtext(buttonBROWSE)  "Browse..."


## For '.fRdisplayPgm' frame:

set aRtext(labelDISPLAYPGM)   "Viewer Program:"
set aRtext(radbuttDPGM1) "1 - ImageMagick 'display'"
set aRtext(radbuttDPGM2) "2 - 'ffplay'"
set aRtext(radbuttDPGM3) "3 - 'eog'"

## For '.fRlevels' frame:

set aRtext(labelLEVELS)       "Search LEVELS for mask:"
set aRtext(radbuttLEVELSone)  "ONE"
set aRtext(radbuttLEVELSall)  "ALL subdirectories of selected directory"

## For '.fRsense' frame:

set aRtext(labelCASESENSE) "Case Sense for Mask Search:"
set aRtext(radbuttSENSE)     "case-sensitive"
set aRtext(radbuttNOSENSE)   "case-INsensitive"


## For '.fRfileSize' frame:

set aRtext(labelFILESIZE)   "Files Size (MegaBytes):"
set aRtext(radbuttBIGGER)   "bigger-than"
set aRtext(radbuttSMALLER)  "smaller-than"

## For '.fRfileAge' frame:

set aRtext(labelFILEAGE)  "Files Age (Days):"
set aRtext(radbuttOLDER)  "older-than"
set aRtext(radbuttYOUNGER)  "younger-than"


## END OF  if { "$VARlocale" == "en"}



##+####################################################################
##+####################################################################
## DEFINE *ALL* THE FRAMES:
##
##   Top-level :
##       'fRbuttons'     for Exit, Help, and Launch buttons
##       'fRfileMask'    for a filename/mask entry field
##       'fRdisplayPgm' for 2 radiobuttons, with a label
##       'fRlevels'      for 2 radiobuttons, with a label
##       'fRsense'       for 2 radiobuttons, with a label
##       'fRfileSize'    for 1 entry field and 2 radiobuttons, with a label
##       'fRfileAge'     for 1 entry fields and 2 radiobuttons, with a label
##+####################################################################
##+####################################################################

## FOR TESTING of expansion of frames (esp. during window expansion):

# set feRELIEF_frame raised
# set feBDwidth_frame 2

 set feRELIEF_frame flat
 set feBDwidth_frame 0

frame .fRbuttons     -relief $feRELIEF_frame  -bd $feBDwidth_frame

frame .fRfileMask    -relief raised           -bd 2

frame .fRdisplayPgm -relief raised           -bd 2

frame .fRlevels      -relief $feRELIEF_frame  -bd $feBDwidth_frame
# frame .fRlevels      -relief raised           -bd 2

frame .fRsense       -relief $feRELIEF_frame  -bd $feBDwidth_frame
# frame .fRsense       -relief raised           -bd 2

frame .fRfileSize    -relief raised           -bd 2

frame .fRfileAge     -relief raised           -bd 2


##+########################################################
## PACK *ALL* the FRAMES.
##+########################################################

pack .fRbuttons \
   -side top \
   -anchor nw \
   -fill x \
   -expand 0

pack .fRfileMask \
   -side top \
   -anchor nw \
   -fill x \
   -expand 0

pack .fRdisplayPgm \
   -side top \
   -anchor nw \
   -fill x \
   -expand 0

pack .fRlevels \
   -side top \
   -anchor nw \
   -fill x \
   -expand 0

pack .fRsense \
   -side top \
   -anchor nw \
   -fill x \
   -expand 0

pack .fRfileSize \
   -side top \
   -anchor nw \
   -fill x \
   -expand 0

pack .fRfileAge \
   -side top \
   -anchor nw \
   -fill x \
   -expand 0


##+################################################################
##+################################################################
## START DEFINING & PACKING WIDGETS WITHIN THEIR FRAMES. 
##+################################################################
##+################################################################

##+########################################################
## IN THE '.fRbuttons' frame -- DEFINE several buttons
## --- Exit, Help, Launch.
##+########################################################

button .fRbuttons.buttEXIT \
   -text "$aRtext(buttonEXIT)" \
   -font fontTEMP_varwidth \
   -padx $PADXpx_button \
   -pady $PADYpx_button \
   -relief raised \
   -bd $BDwidthPx_button \
   -command {exit}

button .fRbuttons.buttHELP \
   -text "$aRtext(buttonHELP)" \
   -font fontTEMP_varwidth \
   -padx $PADXpx_button \
   -pady $PADYpx_button \
   -relief raised \
   -bd $BDwidthPx_button \
   -command {popup_msgVarWithScroll .topHelp "$HELPtext"}

## Set a variable to hold the process-ID of the viewer process.
## NOT NEEDED. See 'Kill' button attempt below.
# set ViewerPID ""

button .fRbuttons.buttLAUNCH \
   -text "$aRtext(buttonLAUNCH)" \
   -font fontTEMP_varwidth \
   -padx $PADXpx_button \
   -pady $PADYpx_button \
   -relief raised \
   -bd $BDwidthPx_button \
   -command {launch_imageViewer view}


##+########################################################
## THE FOLLOWING ATTEMPT DID NOT WORK OUT.
## The intent was to use a 'Kill' button on the GUI
## to allow for killing the 'start_imageViewer.sh' script
## --- if it finds lots of files to view and the user wants out.
##
## It seems the process-ID of the script soon disappeared,
## leaving the 'find' process, which had a different 
## process-ID. 
##
## It turned out to be easier to add an 'xterm' window in
## the 'start_imageViewer.sh' script, to run the 'find'
## command that starts the image viewing sequence.
## Closing the 'xterm' window 'kills' the 'find' command.
##+##########################################################


if {0} {

##+############################################################
## We could try using various kill signals:
##          -HUP/-SIGHUP/-1 (to kill all child processes too)
## AND/OR   -KILL/-SIGKILL/-9
## AND/OR   -TERM/-SIGTERM/-15
##+###########################################################
## References: 'man signal' and 'man kill' and 'kill -l'
##             http://wiki.tcl.tk/735 - 'getPid'
##+###########################################################

button .fRbuttons.buttKILLJOB \
   -text "$aRtext(buttonKILLJOB)" \
   -font fontTEMP_varwidth \
   -padx $PADXpx_button \
   -pady $PADYpx_button \
   -relief raised \
   -bd $BDwidthPx_button \
   -command {
      global ViewerPID
         puts "Trying to kill ViewerPID: $ViewerPID"
      if {"$ViewerPID" != ""} {
         # exec kill -TERM $ViewerPID
         exec kill -15 $ViewerPID
         # exec kill -KILL $ViewerPID
         exec kill -9 $ViewerPID
         # set ViewerPID ""
      }
      # exit
    }

}
## END OF 'if {0}'


button .fRbuttons.buttCOUNT \
   -text "$aRtext(buttonCOUNT)" \
   -font fontTEMP_varwidth \
   -padx $PADXpx_button \
   -pady $PADYpx_button \
   -relief raised \
   -bd $BDwidthPx_button \
   -command {launch_imageViewer count}

button .fRbuttons.buttPRINT \
   -text "$aRtext(buttonPRINT)" \
   -font fontTEMP_varwidth \
   -padx $PADXpx_button \
   -pady $PADYpx_button \
   -relief raised \
   -bd $BDwidthPx_button \
   -command {launch_imageViewer print}

##+########################################
## Pack the widgets in the 'fRbuttons' frame
##+########################################

pack .fRbuttons.buttEXIT \
     .fRbuttons.buttHELP \
     .fRbuttons.buttLAUNCH \
   -side left \
   -anchor w \
   -fill none \
   -expand 0

#     .fRbuttons.buttKILLJOB \

pack .fRbuttons.buttPRINT \
     .fRbuttons.buttCOUNT \
   -side right \
   -anchor e \
   -fill none \
   -expand 0


##+########################################################
## IN THE '.fRfileMask' frame -- DEFINE 1 LABEL widget,
## 1 ENTRY widget, and 1 BUTTON.
##+########################################################

label .fRfileMask.labelFILEMASK \
   -text "$aRtext(labelFILEMASK)" \
   -font fontTEMP_varwidth \
   -justify left \
   -anchor w \
   -relief  flat \
   -bd $BDwidthPx_label

entry .fRfileMask.entryFILEMASK \
   -textvariable ENTRYfileMask \
   -bg $entryBKGD \
   -font fontTEMP_fixedwidth \
   -relief sunken \
   -bd $BDwidthPx_entry

## Set an initial value for the entry var.

set curDIR "$env(HOME)"

## FOR TESTING:
   set curDIR [pwd]

set ENTRYfileMask "$curDIR/*.jpg"

## Put the end of the filename in view.

 .fRfileMask.entryFILEMASK xview end


button .fRfileMask.buttBROWSE \
   -text "$aRtext(buttonBROWSE)" \
   -font fontTEMP_varwidth \
   -padx $PADXpx_button \
   -pady $PADYpx_button \
   -relief raised \
   -bd $BDwidthPx_button \
   -command {get_filemask}

## PACK the widgets in the 'fRfileMask' frame.

pack  .fRfileMask.labelFILEMASK \
   -side left \
   -anchor w \
   -fill none \
   -expand 0 

pack .fRfileMask.entryFILEMASK \
   -side left \
   -anchor w \
   -fill x \
   -expand 1 

pack .fRfileMask.buttBROWSE \
   -side left \
   -anchor w \
   -fill none \
   -expand 0 


##+########################################################
## IN THE '.fRdisplayPgm' frame -- DEFINE
##  2 RADIOBUTTONS, preceded by a LABEL widget.
##+########################################################

label .fRdisplayPgm.labelDISPLAYPGM \
   -text "$aRtext(labelDISPLAYPGM)" \
   -font fontTEMP_varwidth \
   -justify left \
   -anchor w \
   -relief  flat \
   -bd $BDwidthPx_label

## DEFINE Radiobuttons for display sizes :

radiobutton  .fRdisplayPgm.radbuttDPGM1 \
   -text "$aRtext(radbuttDPGM1)" \
   -font fontTEMP_varwidth \
   -anchor w \
   -variable RADVARdisplaypgm \
   -value "1" \
   -selectcolor "$radbuttBKGD" \
   -relief flat \
   -bd $BDwidthPx_radbutt

radiobutton  .fRdisplayPgm.radbuttDPGM2 \
   -text "$aRtext(radbuttDPGM2)" \
   -font fontTEMP_varwidth \
   -anchor w \
   -variable RADVARdisplaypgm \
   -value "2" \
   -selectcolor "$radbuttBKGD" \
   -relief flat \
   -bd $BDwidthPx_radbutt

radiobutton  .fRdisplayPgm.radbuttDPGM3 \
   -text "$aRtext(radbuttDPGM3)" \
   -font fontTEMP_varwidth \
   -anchor w \
   -variable RADVARdisplaypgm \
   -value "3" \
   -selectcolor "$radbuttBKGD" \
   -relief flat \
   -bd $BDwidthPx_radbutt

## RADVARdisplaypgm is the var for these 2 or 3 radiobuttons.
## Set an initial value.
  set RADVARdisplaypgm "1"
# set RADVARdisplaypgm "2"

## PACK the widgets in the 'fRdisplayPgm' frame.

pack  .fRdisplayPgm.labelDISPLAYPGM \
      .fRdisplayPgm.radbuttDPGM1 \
      .fRdisplayPgm.radbuttDPGM2 \
      .fRdisplayPgm.radbuttDPGM3 \
   -side left \
   -anchor w \
   -fill none \
   -expand 0 



##+########################################################
## IN THE '.fRlevels' frame -- DEFINE
## 2 RADIOBUTTONS, preceded by a LABEL widget.
##+########################################################

label .fRlevels.labelLEVELS \
   -text "$aRtext(labelLEVELS)" \
   -font fontTEMP_varwidth \
   -justify left \
   -anchor w \
   -relief  flat \
   -bd $BDwidthPx_label

## DEFINE Radiobuttons for Levels :

radiobutton  .fRlevels.radbuttLEVELSone \
   -text "$aRtext(radbuttLEVELSone)" \
   -font fontTEMP_varwidth \
   -anchor w \
   -variable RADVARlevels \
   -value "one" \
   -selectcolor "$radbuttBKGD" \
   -relief flat \
   -bd $BDwidthPx_radbutt

radiobutton  .fRlevels.radbuttLEVELSall \
   -text "$aRtext(radbuttLEVELSall)" \
   -font fontTEMP_varwidth \
   -anchor w \
   -variable RADVARlevels \
   -value "all" \
   -selectcolor "$radbuttBKGD" \
   -relief flat \
   -bd $BDwidthPx_radbutt

## RADVARlevels is the var for these 2 radiobuttons.
## Set an initial value.
  set RADVARlevels "one"
# set RADVARlevels "all"

## PACK the widgets in the 'fRlevels' frame.

pack  .fRlevels.labelLEVELS \
      .fRlevels.radbuttLEVELSone \
      .fRlevels.radbuttLEVELSall \
   -side left \
   -anchor w \
   -fill none \
   -expand 0 


##+########################################################
## IN THE 'fRsense' frame -- DEFINE
## 2 RADIOBUTTONS, preceded by a LABEL widget.
##+########################################################

label .fRsense.labelCASESENSE \
   -text "$aRtext(labelCASESENSE)" \
   -font fontTEMP_varwidth \
   -justify left \
   -anchor w \
   -relief  flat \
   -bd $BDwidthPx_label

## DEFINE Radiobuttons for CaseSensitivity :

radiobutton  .fRsense.radbuttSENSE \
   -text "$aRtext(radbuttSENSE)" \
   -font fontTEMP_varwidth \
   -anchor w \
   -variable RADVARcasesense \
   -value "case-sensitive" \
   -selectcolor "$radbuttBKGD" \
   -relief flat \
   -bd $BDwidthPx_radbutt

radiobutton  .fRsense.radbuttNOSENSE \
   -text "$aRtext(radbuttNOSENSE)" \
   -font fontTEMP_varwidth \
   -anchor w \
   -variable RADVARcasesense \
   -value "case-INsensitive" \
   -selectcolor "$radbuttBKGD" \
   -relief flat \
   -bd $BDwidthPx_radbutt


## RADVARcasesense is the var for these 2 radiobuttons.
## Set an initial value.
  set RADVARcasesense "case-sensitive"
# set RADVARcasesense "case-INsensitive"


## PACK the widgets in the 'fRsense' frame:

pack  .fRsense.labelCASESENSE \
      .fRsense.radbuttSENSE \
      .fRsense.radbuttNOSENSE \
   -side left \
   -anchor w \
   -fill none \
   -expand 0


##+############################################################
## IN THE 'fRfileSize' frame -- DEFINE
## 1 ENTRY field and 2 RADIOBUTTONS, preceded by a LABEL widget.
##+############################################################

label .fRfileSize.labelFILESIZE \
   -text "$aRtext(labelFILESIZE)" \
   -font fontTEMP_varwidth \
   -justify left \
   -anchor w \
   -relief  flat \
   -bd $BDwidthPx_label

entry .fRfileSize.entryFILESIZE \
   -textvariable ENTRYfileSize \
   -bg $entryBKGD \
   -width 10 \
   -font fontTEMP_fixedwidth \
   -relief sunken \
   -bd $BDwidthPx_entry

## DEFINE Radiobuttons for File Size :

radiobutton  .fRfileSize.radbuttBIGGER \
   -text "$aRtext(radbuttBIGGER)" \
   -font fontTEMP_varwidth \
   -anchor w \
   -variable RADVARfilesize \
   -value "bigger" \
   -selectcolor "$radbuttBKGD" \
   -relief flat \
   -bd $BDwidthPx_radbutt

radiobutton  .fRfileSize.radbuttSMALLER \
   -text "$aRtext(radbuttSMALLER)" \
   -font fontTEMP_varwidth \
   -anchor w \
   -variable RADVARfilesize \
   -value "smaller" \
   -selectcolor "$radbuttBKGD" \
   -relief flat \
   -bd $BDwidthPx_radbutt


## RADVARfilesize is the var for these 2 radiobuttons.
## Set an initial value.
  set RADVARfilesize "bigger"
# set RADVARfilesize "smaller"


## PACK the widgets in the 'fRfileSize' frame:
## (in such a way that we can experiment with
##  letting the entry field expand)

pack .fRfileSize.labelFILESIZE \
   -side left \
   -anchor w \
   -fill none \
   -expand 0

pack .fRfileSize.entryFILESIZE \
   -side left \
   -anchor w \
   -fill x \
   -expand 1

pack .fRfileSize.radbuttBIGGER \
     .fRfileSize.radbuttSMALLER \
   -side left \
   -anchor w \
   -fill none \
   -expand 0



##+########################################################
## IN THE 'fRfileAge' frame -- DEFINE 
## 1 ENTRY field and 2 RADIOBUTTONS, preceded by a LABEL widget.
##+########################################################

label .fRfileAge.labelFILEAGE \
   -text "$aRtext(labelFILEAGE)" \
   -font fontTEMP_varwidth \
   -justify left \
   -anchor w \
   -relief  flat \
   -bd $BDwidthPx_label

entry .fRfileAge.entryFILEAGE \
   -textvariable ENTRYfileAge \
   -bg $entryBKGD \
   -width 10 \
   -font fontTEMP_fixedwidth \
   -relief sunken \
   -bd $BDwidthPx_entry


## DEFINE Radiobuttons for File Age :

radiobutton  .fRfileAge.radbuttOLDER \
   -text "$aRtext(radbuttOLDER)" \
   -font fontTEMP_varwidth \
   -anchor w \
   -variable RADVARfileage \
   -value "older" \
   -selectcolor "$radbuttBKGD" \
   -relief flat \
   -bd $BDwidthPx_radbutt

radiobutton  .fRfileAge.radbuttYOUNGER \
   -text "$aRtext(radbuttYOUNGER)" \
   -font fontTEMP_varwidth \
   -anchor w \
   -variable RADVARfileage \
   -value "younger" \
   -selectcolor "$radbuttBKGD" \
   -relief flat \
   -bd $BDwidthPx_radbutt


## RADVARfileage is the var for these 2 radiobuttons.
## Set an initial value.
  set RADVARfileage "older"
# set RADVARfileage "younger"


## PACK the widgets in the 'fRfileAge' frame:
## (in such a way that we can experiment with
##  letting the entry field expand)

pack .fRfileAge.labelFILEAGE \
   -side left \
   -anchor w \
   -fill none \
   -expand 0

pack .fRfileAge.entryFILEAGE \
   -side left \
   -anchor w \
   -fill x \
   -expand 1

pack .fRfileAge.radbuttOLDER \
     .fRfileAge.radbuttYOUNGER \
   -side left \
   -anchor w \
   -fill none \
   -expand 0



##+#####################################################################
## END OF SECTION TO DEFINE AND PACK THE GUI WIDGETS.
##+#####################################################################

##+#####################################################################
##+#####################################################################
## DEFINE BINDINGS:  button1-release bindings on some widgets ???
##                   Return-key bindings on some entry widgets ???
##+#####################################################################

# bind .fR?????.???widget???  <ButtonRelease-1>  {??proc??}
# bind .fR?????.???widget???  <Return>  {??proc??}

##+#####################################################################
##+#####################################################################
## DEFINE PROCEDURES:
##
##  'get_filemask'         - called by the 'Browse...' button
##                           next to the filemask entry field
##
##  'launch_imageViewer'   - called by the 'Launch' button
##
##  'popup_msgVarWithScroll' - called by 'Help' button to show HELPtext var.
##+#####################################################################
##+#####################################################################

##+#####################################################################
## Proc 'get_filemask'
##
## PURPOSE: To get the fully-qualified name of a file and put the
##          name into global var 'ENTRYfileMask'.
##
##          The user may change the filename (after the end of the
##          directory name) to change it into a file-mask. 
##
## CALLED BY: the '-command' option of the 'Browse ...' button.
##+#####################################################################


proc get_filemask {} {

   global ENTRYfileMask env curDIR

   ## Get a file name (possibly to make a mask).

   set fName [tk_getOpenFile -parent .  \
      -title "Select Directory-and-Filename - the latter as basis for a mask" \
      -initialdir "$curDIR" ]

   ## FOR TESTING:
   #   puts "fName : $fName"

   ## Check if fName var is empty.

   if {"$fName" == ""} {return}

   ## Put $fName in ENTRYfileMask. Reset the curDIR var.

   if {[file exists "$fName"]} {
      set ENTRYfileMask "$fName"

      ## Put the end of the filename in view.
      .fRfileMask.entryFILEMASK xview end

      set curDIR [ get_chars_before_last / in "$ENTRYfileMask" ]
   }
   ## END OF if directory-exists

}
## END OF proc 'get_filemask'


##+###################################################################
## Proc 'get_chars_before_last' -
##+###################################################################
## INPUT:  A character and a string.
##         Note: The "in" parameter is there only for clarity.
##
## OUTPUT: Returns all of the characters in the string "strng" that
##         are BEFORE the last occurence of the characater "char".
##
## CALLED BY: proc 'get_img_filename'
##
##+##################################################################

proc get_chars_before_last { char in strng } {

   set endIDX [ expr [string last $char $strng ] - 1 ]
   set output [ string range $strng 0 $endIDX ]

   ## FOR TESTING:
   # puts "From 'get_chars_before_last' proc:"
   # puts "STRING: $strng"
   # puts "CHAR: $char"
   # puts "RANGE up to LAST CHAR - start: 0   endIDX: $endIDX"

   return $output

}
## END OF 'get_chars_before_last' PROCEDURE


##+#############################################################
## proc launch_imageViewer
##
## PURPOSE: The function of this proc depends on the value of
##          the 'option' parm passed to this proc. Its values
##          can be 'view' or 'count' or 'print'.
##
##          For all 3 options,
##          this proc does some checking of the GUI interface
##          parameters, especially from the 'entry' widgets.
##
##          For option='view', this proc runs the
##          'start_imageViewer.sh' shell script with parm 'view'.
##
##          In that case, the shell script builds a 'find' command
##          that calls on the user-specified image viewer program ---
##          to start off the viewing of the found files.
##          The shell script uses an 'xterm' technique to provide
##          the user an easy way to kill the 'find' process if
##          the user wants to interrupt a (long) sequence of displays.
##
##          For option='count', this proc runs the
##          'start_imageViewer.sh' shell script with parm 'count'.
##
##          The shell script returns a count of the to-be-selected
##          files, so that this proc can display the count
##          in a small popup Tk window.
##
##          For option='print', this proc runs the
##          'start_imageViewer.sh' shell script with parm 'print'.
##
##          The shell script returns the names of the to-be-selected
##          files, so that this proc can display the list
##          in a popupTk window.
##
## CALLED BY: - 'Launch' button with option='view'
##            - 'Count'  button with option='count'
##            - 'List'   button with option='print'
##+#############################################################

proc launch_imageViewer {option} {

   global ViewerPID DIRscripts ENTRYfileMask RADVARdisplaypgm \
      RADVARlevels RADVARcasesense \
      RADVARfilesize ENTRYfileSize \
      RADVARfileage  ENTRYfileAge

   #######################################################
   ## Remove trailing and leading blanks (if any) from the
   ## user entries in the 'entry' widgets.
   #######################################################

   set ENTRYfileMask [string trim $ENTRYfileMask]
   set ENTRYfileSize [string trim $ENTRYfileSize]
   set ENTRYfileAge  [string trim $ENTRYfileAge]

   #################################################################
   ## The variables RADVARdisplaypgm, RADVARlevels, RADVARcasesense,
   ## RADVARfilesize, and RADVARfileage are set as single
   ## words in this script and should not need any editing
   ## or checking (if bug-free).
   #################################################################

   ##########################################################
   ## Check that ENTRYfileSize and ENTRYfileAge are integers,
   ## if they are not blank (null).
   ##########################################################

   if {"$ENTRYfileSize" != ""} {
      if {![string is integer -strict "$ENTRYfileSize"]} {
         popup_msgVarWithScroll .topErr \
            "File Size: $ENTRYfileSize  is NOT NUMERIC."
         return
      }
   }
   

   if {"$ENTRYfileAge" != ""} {
      if {![string is integer -strict "$ENTRYfileAge"]} {
         popup_msgVarWithScroll .topErr \
            "File Age: $ENTRYfileAge  is NOT NUMERIC."
         return
      }
   }


   ## FOR TESTING:  (to dummy out the rest of this proc)

   # return

   ############################################################
   ## If option = 'count',
   ## run the 'start_imageViewer.sh' shell script with the
   ## 'count' option. The shell script returns a count of the
   ## 'to-be-selected' filenames, which this proc displays
   ## in a small popup Tk window.
   ############################################################

   if {"$option" == "count"} {
      set FILEScount [exec $DIRscripts/start_imageViewer.sh \
         "$RADVARdisplaypgm" \
         "$RADVARlevels" "$RADVARcasesense" \
         "$RADVARfilesize" "$ENTRYfileSize" \
         "$RADVARfileage"  "$ENTRYfileAge" \
         count "$ENTRYfileMask"]

      popup_msgVarWithScroll .topCount \
        "$FILEScount files 'would-be-selected' with the
current file mask and other GUI settings."

      return
   }
   ## END OF  if {"$option" == "count"}


   ############################################################
   ## If option = 'print',
   ## run the 'start_imageViewer.sh' shell script with the
   ## 'print' option.   The shell script returns a list of
   ## the 'to-be-selected' filenames, which this proc displays
   ## in a popup Tk window.
   ############################################################
   ## NOTE: We could change this function slightly to
   ##       let the user put the filenames in a text file,
   ##       say in a '/tmp' directory. This would be better
   ##       in cases when the list is huge.
   ############################################################

   if {"$option" == "print"} {
      set FILESlist [exec $DIRscripts/start_imageViewer.sh \
         "$RADVARdisplaypgm" \
         "$RADVARlevels" "$RADVARcasesense" \
         "$RADVARfilesize" "$ENTRYfileSize" \
         "$RADVARfileage"  "$ENTRYfileAge" \
         print "$ENTRYfileMask"]

      popup_msgVarWithScroll .topList \
         "Filenames that 'would-be-selected' with the
current file mask and other GUI settings:

$FILESlist"

      return
   }
   ## END OF  if {"$option" == "print"}


   ############################################################
   ## If option = 'view',
   ## run the 'start_imageViewer.sh' shell script with the
   ## 'view' option, to select the image files and to run the
   ## specified image viewer program on the selected files.
   ###########################################################
   ## Attempts were made to use the 'ViewerPID' process ID
   ## which is set below, to allow the user to cancel the
   ## viewing sequence by means of a 'Kill' button on this GUI.
   ## THOSE ATTEMPTS DID NOT WORK.
   ####################################################################
   ## SOME SYNTAX NOTES on storing the process-ID from a Tcl 'exec':
   ##
   ## A couple of examples of using a PID (process ID) with Tcl:
   ##
   ## catch {eval exec $feREADER_text \"$FULFILname\"  &} ViewerPID
   ##
   ## set RETcode [ catch {eval exec ${feDIR}/tkGUIs/shofil.tk \
   ##    "$FULFILname" &} ViewerPID ]
   ##
   ## An alternative from of trying 'exec':
   ##    exec  /bin/sh -c "$...."
   #######################################################################
   ## From page 107 of 4th edition of 'Practical Programming in Tcl & Tk',
   ## on the Tcl 'exec' command:
   ## "A trailing & causes the program to run in the background.
   ##  In this case, the process identifier is returned by the 'exec'
   ##  command. Otherwise, the 'exec' command blocks during execution
   ##  of the program, and the standard output of the program is the
   ##  return code of 'exec'."
   ## Page 83 of the same book says:
   ## "'catch' returns zero if there was no error caught,
   ##   or a nonzero error code if it did catch an error."
   ###############################################################

   if {"$option" == "view"} {

      set RETcode [catch {exec $DIRscripts/start_imageViewer.sh \
         "$RADVARdisplaypgm" \
         "$RADVARlevels" "$RADVARcasesense" \
         "$RADVARfilesize" "$ENTRYfileSize" \
         "$RADVARfileage"  "$ENTRYfileAge" \
         view "$ENTRYfileMask" &}  ViewerPID]

      ## FOR TESTING:
      #   puts "ViewerPID: $ViewerPID"

      if { $RETcode != 0 } then {
         popup_msgVarWithScroll .topErr \
            "ERROR from attempt to run the 'start_imageViewer.sh' script.

RETcode: $RETcode"
         return
      }
      ## END OF   if { $RETcode != 0 }
   }
   ## END OF  if {"$option" == "view"}

}
## END OF proc 'launch_imageViewer'


##+########################################################################
## PROC 'popup_msgVarWithScroll'
##+########################################################################
## PURPOSE: Report help or error conditions to the user.
##
##       We do not use focus,grab,tkwait in this proc,
##       because we use it to show help when the GUI is idle,
##       and we may want the user to be able to keep the Help
##       window open while doing some other things with the GUI
##       such as putting a filename in the filename entry field
##       or clicking on a radiobutton.
##
##       For a similar proc with focus-grab-tkwait added,
##       see the proc 'popup_msgVarWithScroll_wait' in a
##       3DterrainGeneratorExaminer Tk script.
##
## REFERENCE: page 602 of 'Practical Programming in Tcl and Tk',
##            4th edition, by Welch, Jones, Hobbs.
##
## ARGUMENTS: A toplevel frame name (such as .fRhelp or .fRerrmsg)
##            and a variable holding text (many lines, if needed).
##
## CALLED BY: 'help' button
##+########################################################################
## To have more control over the formatting of the message (esp.
## words per line), we use this 'toplevel-text' method, 
## rather than the 'tk_dialog' method -- like on page 574 of the book 
## by Hattie Schroeder & Mike Doyel,'Interactive Web Applications
## with Tcl/Tk', Appendix A "ED, the Tcl Code Editor".
##+########################################################################

proc popup_msgVarWithScroll { toplevName VARtext } {

   ## global fontTEMP_varwidth #; Not needed. 'wish' makes this global.
   ## global env

   # bell
   # bell
  
   #################################################
   ## Set VARwidth & VARheight from $VARtext.
   #################################################
   ## To get VARheight,
   ##    split at '\n' (newlines) and count 'lines'.
   #################################################
 
   set VARlist [ split $VARtext "\n" ]

   ## For testing:
   #  puts "VARlist: $VARlist"

   set VARheight [ llength $VARlist ]

   ## For testing:
   #  puts "VARheight: $VARheight"


   #################################################
   ## To get VARwidth,
   ##    loop through the 'lines' getting length
   ##     of each; save max.
   #################################################

   set VARwidth 0

   #############################################
   ## LOOK AT EACH LINE IN THE LIST.
   #############################################
   foreach line $VARlist {

      #############################################
      ## Get the length of the line.
      #############################################
      set LINEwidth [ string length $line ]

      if { $LINEwidth > $VARwidth } {
         set VARwidth $LINEwidth 
      }

   }
   ## END OF foreach line $VARlist

   ## For testing:
   #    puts "VARwidth: $VARwidth"


   ###############################################################
   ## NOTE: VARwidth works for a fixed-width font used for the
   ##       text widget ... BUT the programmer may need to be
   ##       careful that the contents of VARtext are all
   ##       countable characters by the 'string length' command.
   ###############################################################


   #####################################
   ## SETUP 'TOP LEVEL' HELP WINDOW.
   #####################################

   catch {destroy $toplevName}
   toplevel  $toplevName

   # wm geometry $toplevName 600x400+100+50

   wm geometry $toplevName +100+50

   wm title     $toplevName "Note"
   # wm title   $toplevName "Note to $env(USER)"

   wm iconname  $toplevName "Note"


   #####################################
   ## In the frame '$toplevName' -
   ## DEFINE THE TEXT WIDGET and
   ## its two scrollbars --- and
   ## DEFINE an OK BUTTON widget.
   #####################################

   if {$VARheight > 10 || $VARwidth > 100} {
      text $toplevName.text \
         -wrap none \
         -font fontTEMP_fixedwidth \
         -width  $VARwidth \
         -height $VARheight \
         -bg "#f0f0f0" \
         -relief raised \
         -bd 2 \
         -yscrollcommand "$toplevName.scrolly set" \
         -xscrollcommand "$toplevName.scrollx set"

      scrollbar $toplevName.scrolly \
         -orient vertical \
         -command "$toplevName.text yview"

      scrollbar $toplevName.scrollx \
         -orient horizontal \
         -command "$toplevName.text xview"
   } else {
      text $toplevName.text \
         -wrap none \
         -font fontTEMP_fixedwidth \
         -width  $VARwidth \
         -height $VARheight \
         -bg "#f0f0f0" \
         -relief raised \
         -bd 2 
   }

   button $toplevName.butt \
      -text "OK" \
      -font fontTEMP_varwidth \
      -command  "destroy $toplevName"

   ###############################################
   ## PACK *ALL* the widgets in frame '$toplevName'.
   ###############################################

   ## Pack the bottom button BEFORE the
   ## bottom x-scrollbar widget,

   pack  $toplevName.butt \
      -side bottom \
      -anchor center \
      -fill none \
      -expand 0


   if {$VARheight > 10 || $VARwidth > 100} {
      ## Pack the scrollbars BEFORE the text widget,
      ## so that the text does not monopolize the space.

      pack $toplevName.scrolly \
         -side right \
         -anchor center \
         -fill y \
         -expand 0

      ## DO NOT USE '-expand 1' HERE on the Y-scrollbar.
      ## THAT ALLOWS Y-SCROLLBAR TO EXPAND AND PUTS
      ## BLANK SPACE BETWEEN Y-SCROLLBAR & THE TEXT AREA.
                
      pack $toplevName.scrollx \
         -side bottom \
         -anchor center \
         -fill x  \
         -expand 0

      ## DO NOT USE '-expand 1' HERE on the X-scrollbar.
      ## THAT KEEPS THE TEXT AREA FROM EXPANDING.

      pack $toplevName.text \
         -side top \
         -anchor center \
         -fill both \
         -expand 1
   } else {
      pack $toplevName.text \
         -side top \
         -anchor center \
         -fill both \
         -expand 1
   }


   #####################################
   ## LOAD MSG INTO TEXT WIDGET.
   #####################################

   ##  $toplevName.text delete 1.0 end
 
   $toplevName.text insert end $VARtext
   
   $toplevName.text configure -state disabled
  
}
## END OF PROC 'popup_msgVarWithScroll'


##+########################
## END of PROC definitions.
##+########################
## Set HELPtext var.
##+########################


set HELPtext "\
\ \ ** HELP for this 'Tk Image Viewer' Utility **

** essentially a 'wrapper' for an image viewer and 'find' commands **

This utility provides a GUI for starting up an image viewer
--- like ImageMagick 'display' or 'ffplay' or eog' (Eye of Gnome)
--- according to user-specified parameters that are prompted for
via radiobutton widgets and entry widgets on the GUI.

The GUI is currently coded as a front-end for starting up the
'display' or 'ffplay' or 'eog' program (along with use of the
'find' command). The Tk GUI script runs a shell script to handle
formulating the 'find' command and running it.

A different image viewer program could be added by some simple
label-text changes in the Tk script --- 'imageViewer_FrontEnd.tk'
--- and, additionally, a few coding changes in the shell script
--- 'start_imageViewer.sh'.

The Tk GUI is intended to make the user aware of the main
(most useful) options available --- without the user needing
to reference 'man display'/'man ffplay'/'man eog' and 'man find'
commands and other image viewer and 'find' documentation,
such as web searches.

The intent of this utility is to allow the user to
easily specify image files to be viewed --- via specifying:
 1  - a directory-and-filename (the latter can be a mask)
 2  - which image viewer program to use
 3  - whether files in sub-directories should be mask-searched
 4  - whether the mask-search should be case-INsensitive
 5  - whether to use a file-size limitation
 6  - whether to use a file-age limitation.

NOTE that most image-viewers are oriented toward scanning
through ALL image files in a SINGLE directory.

This utility is oriented toward SELECTING image files (based
criteria such as a file-mask, file-size, and/or file-age)
from an ENTIRE HIERARCHY OF SUB-DIRECTORIES --- as well as
allowing mask-search in a SINGLE directory.

And a single file can be viewed by selecting a single filename
and NOT changing the name to a mask --- like *.jpg  OR
*family*.gif  OR  a*.PNG. 
           
**********************
NOTES ON OTHER VIEWERS:
**********************

As an alternative to 'eog', it would be easy to use
the 'Eye of MATE' (say 'eom') image viewer instead.
'eom' is the image viewer that the MATE desktop project
forked from 'eog'. Reference: www.mate-desktop.org

Circa 2010, some Argentinians decided to fork the
Gnome 2.x desktop environment --- when the Gnome 3
project abandoned many of the features of Gnome 2 and its
associated applications --- applications like the 'Nautilus'
file manager and the 'Eye of Gnome' image viewer.

  (The fork of the 'Nautilus' file manager is called 'Caja'
   --- Spanish for 'box' or 'gift'.)

Of course, still other 'light-weight' image viewers
could be used with very few changes. The 'core' changes would
be to the shell script 'start_imageViewer.sh' that this
Tk script calls. That shell script was initially programmed
to use 'display' or 'ffplay' or 'eog' for the image viewer ---
along with the 'find' command --- and along with parameters
supplied from this GUI.

*******************
THE GUI WIDGETS:

The GUI offers a generous set of hints, help, and options
for the user.  In fact, the GUI consists of about

   -  5 button widgets
   -  6 label widgets
   -  3 entry widgets
   - 11 radiobutton widgets in 5 groups
   -  0 checkbutton widgets
   -  0 scale widgets
   -  0 listbox widgets

**************
TYPICAL USAGE:

You can use the 'Browse...' button to retrieve a full filename
to the file/mask name entry field. This allows one to establish
a 'base' directory for the 'find' command. And the user can change
the filename after the directory name, to a mask. Examples:
*.jpg   OR   *family*.gif   OR   a*.PNG

Before clicking on the 'Launch' button,
you can also change radiobutton settings as needed --- and
enter an integer in the FileSize and/or FileAge entry fields,
if you want to use size or age to reduce the filenames found
via the specified mask.


**********
CALLED BY:

This script could be put in a sub-directory of the
user's home directory, such as \$HOME/apps/tkImgViewer.

Then the user can use their desktop system (such as
Gnome or KDE) to set up the script as an icon on the
desktop. Then the user can click on the icon to
startup the script.
"


##+######################################################
## ADDITIONAL GUI INITIALIZATION section.
##+######################################################

##+#############################################################
## Get the directory that this Tk script is in. That will be the
## directory that the 'external' utility shell script should be
## in. This directory is used to call the shell script that
## is used for the 'view', 'count', and 'print' options.
##+#############################################################

## FOR TESTING:
#  puts "argv0: $argv0"

# set DIRscripts "."
# set DIRscripts "[pwd]"
# set DIRscripts "$env(HOME)/apps/tkUtils"
  set DIRscripts "[file dirname $argv0]"


##+##########################################
## Disable/enable widgets according to intial
## GUI settings. (NOT IMPLMENTED, yet)
##+#########################################

# disable_enable_widgets


The shell script (the 'container' for the 'find' processing) :

And here is the code for the shell script called by this Tk script. This is a wrapper script for the 'find' command, along with a choice of the 'viewer' program.

You can put this script in the same directory with the Tk script. The Tk script includes some code (involving the 'argv0' variable) to determine the location of the shell script by extracting the name of the directory in which the Tk script lies.

 Code for the shell script 'start_imageViewer.sh' :
#!/bin/sh
##
## SCRIPT NAME: start_imageViewer.sh
##
##+#######
## PURPOSE:
##  This script
##  1) finds a set of user-specified files --- according to
##     a fully-qualified file mask (directory and mask) and other
##     'find' parameters passed to this script
##  AND
##  2) runs a user-selected image viewer program
##     (with appropriate  parameters) to show each image file.
##
##  When the user closes the image-viewer window, the next image is shown.
##
##  This shell script is meant to be issued from the Tk GUI 'wrapper' script
##  'imageViewer_FrontEnd.tk'. That Tk script is to get parameters
##    - for the 'find' command
##  and
##    - to determine which image-viewer program to use.
##
##+######
## INPUTS:
##
##  The parameters passed to this script, from the 'imageViewer_FrontEnd.tk'
##  Tk GUI script, are:
##
##   Var1: An 'image viewer program' indicator from a Tk radiobuttons var
##         (possible values: '1' or '2' --- and maybe '3')  
##
##          So far, the simplest, least-confusing image viewer programs
##          that I have found for this utility are the ImageMagick 'display'
##          program and the 'ffplay' program.
##
##          The toolbar menus of the 'eog' (Eye of Gnome) program 
##          make it rather confusing to use for this utility.
##          It would be nice to find a way to hide the toolbars of 'eog'.
##
##          That said, the popup menu of the ImageMagick 'display'
##          program can also cause some confusion.
##
##  The following are used to set parameters of the 'find' command:
##
##   Var2: A 'levels' indicator from a Tk radiobuttons var
##         (possible values: 'one' or 'all').
##
##   Var3: A 'case-sensitivity' indicator from a Tk radiobuttons var
##         (possible values: 'case-sensitive' or 'case-INsensitive')
##
##   Var4: A 'bigger/smaller' indicator from a Tk radiobuttons var
##.        (possible values: 'bigger' or 'smaller')
##
##   Var5: An integer file-size-cutoff value (in MegaBytes)
##         (could be null, indicating no size limitation)
##
##   Var6: An 'older/younger' indicator from a Tk radiobuttons var
##.        (possible values: 'older' or 'younger')
##
##   Var7: An integer file-age-cutoff value (in days)
##         (could be null, indicating no age limitation)
##
##   Var8: A string with value 'print' or 'count' or 'view',
##         to indicate the type of 'find' command to run:
##            - to return a selected-files list to stdout
##                 OR
##            - to return a count of the selected files to stdout
##                 OR
##            - to start running the image viewer program against
##              each of the selected files.
## 
##   Var9: 'Base'-directory name and file mask. 
##         Example: /home/fred/IMAGE_CAPTURES/*river*.jpg
##
## In this script, we will put those parameters in shell script variables
## VARdisplaypgm, VARlevels, VARsense,
## VARbigsmall,  VARfilesize,
## VARoldyoung,  VARfileage,
## VARperform and  VARfilemask --- respectively.
##
##+#########################################################################
## MAINTENANCE HISTORY:
## Updated by: Blaise Montandon 2013dec08 Started this script on Linux,
##                                        using Ubuntu 9.10 (2009 October,
##                                        'Karmic Koala').
## Updated by: Blaise Montandon 2013dec09 Add an 'xterm' to call the 'find'
##                                        command that runs $IMGVIEWER ---
##                                        so that closing the 'xterm'
##                                        window can be used to kill the
##                                        sequence of image displays.
##+#########################################################################

## FOR TESTING: (to show statements as they execute)
#  set -x

VARdisplaypgm="$1"
VARlevels="$2" 
VARsense="$3"
VARbigsmall="$4"
VARfilesize="$5"
VARoldyoung="$6"
VARfileage="$7"
VARperform="$8"
VARfilemask="$9"

## FOR TESTING of this script without the Tk wrapper:
## (For stand-alone testing, change 'if test 1 = 0' to 'if test 1 = 1'.)

if test 1 = 0
then
   VARdisplaypgm="1"
   # VARdisplaypgm="2"
   # VARdisplaypgm="3"
   VARlevels="one"
   # VARlevels="all"
   VARsense="case-sensitive"
   VARbigsmall="bigger"
   VARfilesize=""
   VARoldyoung="older"
   VARfileage=""
   # VARperform="print"
   # VARperform="count"
   VARperform="view"
   VARfilemask="$HOME/apps/tkGooies_linux_PREP/tkGUIs/.PREP_tkGooies/00_CFE_eog_FrontEnd_2013dec_PREP/*.jpg"
fi

## FOR TESTING:
#   echo "VARfilemask: $VARfilemask"


## Simply exit if there is no filemask passed to this script.

if test "$VARfilemask" = ""
then
   exit
fi

DIRNAME=`dirname "$VARfilemask"`
FILEMASK=`basename "$VARfilemask"`


if test "$VARdisplaypgm" = "1"
then 
   IMGVIEWER="/usr/bin/display -geometry +15+30"
elif test "$VARdisplaypgm" = "2"
then
   ## Unfortunately, 'ffplay' does not support '-geometry'.
   IMGVIEWER="/usr/bin/ffplay"
elif test "$VARdisplaypgm" = "3"
then
   ## 'eog' automatically centers the display on the screen.
   IMGVIEWER="/usr/bin/eog -c -n"
fi


if test "$VARlevels" = "one"
then 
   DEPTHPARM="-maxdepth 1"
else
   DEPTHPARM=""
fi


if test "$VARsense" = "case-sensitive"
then 
   NAMEPARM="-name"
else
   NAMEPARM="-iname"
fi


## The FILESIZE_PARM can be a parameter like
##      -size +${SIZE_MINinBYTES}c
## for the 'find' command.

if test "$VARfilesize" = ""
then
   FILESIZE_PARM=""
else
   if test "$VARbigsmall" = "bigger"
   then
      FILESIZE_PARM="-size +${VARfilesize}M"
   else
      FILESIZE_PARM="-size -${VARfilesize}M"
   fi
fi


## The FILEAGE_PARM can be a parameter like
##     -mtime +$NDAYS
## for the 'find' command.

if test "$VARfileage" = ""
then
   FILEAGE_PARM=""
else
   if test "$VARoldyoung" = "older"
   then
      FILEAGE_PARM="-mtime +$VARfileage"
   else
      FILEAGE_PARM="-mtime -$VARfileage"
   fi
fi


##+##################################################
## Call on the 'find' command, which 
##  1) prints the selected filenames
## or
##  2) shows a count of the selected filenames
## or
##  3) executes the user-specified image viewer program
##     on each selected filename,
##
## depending on whether $VARperform is
##  1) 'print'
## or
##  2) 'count'
## or
##  3) some other string, such as 'view'.
##
##+#################################################

## FOR TESTING: (to show the 'find' commands as they execute)
## (When called from within the Tk wrapper script,
##  this output may interfere with proper processing.)
#  set -x

if test "$VARperform" = "print"
then
   ## FOR TESTING: (to show the 'find' command)
   #   set -x

   ## NOTE: Do not escape the quotes around $FILEMASK.
   ##       If you do, no filenames are returned.

   find "$DIRNAME"  $DEPTHPARM  -type f \
   $NAMEPARM "$FILEMASK" \
   $FILESIZE_PARM $FILEAGE_PARM -print


elif test  "$VARperform" = "count"
then
   ## FOR TESTING: (to show the 'find' command)
   #   set -x

   ## NOTE: Do not escape the quotes around $FILEMASK.

   find "$DIRNAME"  $DEPTHPARM  -type f \
   $NAMEPARM "$FILEMASK" \
   $FILESIZE_PARM $FILEAGE_PARM -print | wc -l

else
   ## FOR TESTING: (to show the 'find' command)
   #   set -x

   ## NOTE: Do not escape the quotes around $FILEMASK.

   xterm -fg white -bg black -hold \
         -title "To 'kill' the display of image files, close this window." \
         -geometry 90x5-30-30 -e \
   find "$DIRNAME"  $DEPTHPARM  -type f \
   $NAMEPARM "$FILEMASK" \
   $FILESIZE_PARM $FILEAGE_PARM \
   -exec $IMGVIEWER {} 2> /dev/null \;
fi


INSTALLING THESE SCRIPTS:

This pair of scripts could be put in a sub-directory of the user's home directory, such as $HOME/apps/tkImgViewer.

Then the user can use their desktop system (such as Gnome or KDE) to set up the Tk script as an icon on the desktop. Then the user can click on the icon to startup the 'front end'.


SOME ENHANCEMENTS :

This pair of scripts provides a start to my 'to-do' list ---at the bottom of my 'bio' page at uniquename --- in the 'CFE' (Code for Front Ends) group. I plan to work on front ends for 'mplayer', 'ffmpeg', and 'find'. But I may return to this pair of scripts to provide some enhancements.

File-type

As I indicated near the top of this page, I may add another 'entry' widget to the GUI --- to support a 'file-type' search capability, based on using the 'file' command.

In that 'entry' widget, the user could enter keywords such as 'JPEG' or 'GIF' or 'PNG' to retrieve files for which the 'file' command returns text strings like the following:

   - JPEG image data, JFIF standard 1.01
   - GIF image data, version 89a, 256 x 352
   - PNG image, 1024 x 768, 8-bit/color RGB, non-interlaced

The file-mask could then be set to '*' --- to look for files of a specified type (JPEG, GIF, PNG, ...) , in one or more sub-directory levels.

maxdepth N

I also indicated near the top of this page that I may change the 'ONE' radiobutton to an 'N-levels' radiobutton. If that radiobutton is selected, an entry field will be activated where the user can enter a choice of N (with the entry field being initialized with '1'). This capability would be implemented via the '-maxdepth' parameter of the 'find' command.

There is also a '-mindepth' parameter of the 'find' command. But I have never encountered a situation where I felt I needed to use that parameter.

---

There are a lot of parameters available with the 'find' command, but I think I have probably implemented the ones that are most useful to use for this 'multiple-subdirectory image-select-and-display' utility. (On second thought, a '-prune' capability might be handy.)


IN CONCLUSION

As I have said on several other code-donation pages on this wiki ...

There's a lot to like about a utility that is 'free freedom' --- that is, no-cost and open-source so that you can modify/enhance/fix it without having to wait for someone else to do it for you (which may be never).

A BIG THANK YOU to Ousterhout for starting Tcl-Tk, and a BIG THANK YOU to the Tcl-Tk developers and maintainers who have kept the simply MAH-velous 'wish' interpreter going.


RFox - 2013-12-10 19:09:04

"That's nice. BUT ... How is that going to help the world develop the engineers, computer-scientists, and other scientists that are needed in a very challenging future world? (We need people to help deal with that asteroid that is on a collision course with the Earth --- perhaps sooner than most people think.)"

It might not help but at least we'll be looking at some kick-*ss images when the world ends.


uniquename 2013dec11

That observation led me to a mind-image of a twerker-generation teenager saying "You can have my iPad when you pry it from my cold, dead hand."