#
# $Log: not supported by cvs2svn $
# Revision 1.16  2006/05/08 19:26:56  shang
# replaced subst by set for getting indirect variable's value, since subst does not
# work appopriately when the variable name has special characters such as ".".
#
# Revision 1.15  2005/12/08 16:38:10  soliday
# Simpified last change.
#
# Revision 1.14  2005/12/08 16:20:33  borland
# On Linux systems, uses single-click instead of double-click.
#
# Revision 1.13  2004/03/01 22:28:31  shang
# added labelFont argument to APSSDDSListBox proc
#
# Revision 1.12  2004/02/23 19:39:26  shang
# added selectMode arguments to APSSDDSListBox proc
#
# Revision 1.11  2003/06/27 19:03:43  shang
# added packOption as an argument to APSSDDSListBox proc
#
# Revision 1.10  2003/06/27 18:48:19  shang
# modified APSSDDSListBox proc to make it expandable
#
# Revision 1.9  2001/02/06 19:44:26  soliday
# Fixed some tcl warning messages found with procheck
#
# Revision 1.8  1999/08/02 18:13:59  borland
# More careful now about passing caller's callback script.
#
# Revision 1.7  1998/02/14 23:35:08  borland
# Added code to deal with goofy descriptions that operators sometimes enter.
# E.g., descriptions containing control characters and large numbers of spaces.
#
# Revision 1.6  1996/10/07 23:29:32  borland
# Added -buttonSize option.
#
# Revision 1.5  1996/08/12 19:58:42  borland
# Added height and doneButton options to APSSDDSListBox
#
# Revision 1.4  1996/03/10  23:50:04  borland
# Fixed use of contextHelp option.
#
# Revision 1.3  1996/03/10  23:47:31  borland
# Fixed destroy command for DONE button.
#
# Revision 1.2  1996/03/10  22:15:40  borland
# First name clash with help routine.
#
# Revision 1.1  1996/03/10  22:13:33  borland
# First version.
#
#


#
# find the length of the longest string in a list
#
proc APSMaxStringLength {stringList} {
    set max 0
    foreach i $stringList {
        set len [string length $i]
        if {$len > $max} { set max $len}
    }
    return $max
}

proc APSMaxStringLengthHelp {} {
    return "Usage: APSMaxStringLength <list>"
}


proc APSSDDSListboxDefaultLabelMaker {args} {
    set dataList {}
    set widthList {}
    APSStrictParseArguments {dataList widthList}
    set count [llength $dataList]
    set result {}
    for {set i 0} {$i<$count} {incr i} {
        set d0 [lindex $dataList  $i]
        set w0 [lindex $widthList $i]
        set sform [format "%%%lds" $w0]
        lappend result [format $sform $d0]
    }
    return [join $result "  "]
}

proc APSSDDSListboxHelp {} {
    return "Usage: APSSDDSListbox <widget> -fileName <fileName> \[-parent <parent>\] \
\[-label <string>\] \[-page <page>\] \[-page <page>\] \[-labelMaker <procName>\] \[-callback <procName>\] \
\[-columnList <list>\] \[-doneButton 0|1\] \[-height <number>\] \[-widthLimit <number>\]"
}

#
# Brings up listbox containing data from an SDDS file
#
proc APSSDDSListbox {widget args} {
    global apsttk
    set fileName ""
    set parent ""
    set page 0
    set labelMaker ""
    set callback ""
    set columnList {}
    set arrayName ""
    set doneButton 1
    set height 10
    set widthLimit 0
    set label ""
    set labelFont ""
    set trim 0
    set buttonSize medium
    set packOption "-side top -expand 1 -fill y"
    set contextHelp "No context help available."
    set selectMode single
    APSStrictParseArguments {fileName parent page labelMaker callback \
                               columnList contextHelp arrayName doneButton height label \
                               buttonSize widthLimit trim packOption selectMode labelFont}

    if {[info exists apsttk] && ($apsttk != 0)} {
        set ttkPrefix ttk::
        if {[lsearch -exact [font names] smallBoldDefaultFont] == -1} {
            eval font create smallBoldDefaultFont [font actual TkDefaultFont]
            set fontsize [font configure TkDefaultFont -size]
            if {$fontsize < 0} {
                incr fontsize 2
            } else {
                incr fontsize -3
            }
            font configure smallBoldDefaultFont -weight bold -size $fontsize
        }
        set textFont smallBoldDefaultFont
    } else {
        set ttkPrefix ""
        set textFont "-adobe-courier-bold-r-normal-*-*-120-75-75-*-*-*-*"
    }
    set LabelMaker $labelMaker
    global $arrayName
    set maxlen {}
    foreach name $columnList {
        if {[string length $name]==0} {
            break;
        }
        set ${arrayName}($name) [APSGetSDDSColumn -fileName $fileName -column $name -page $page]
        set width [APSMaxStringLength [set ${arrayName}($name)]]
        lappend maxlen $width
    }

    APSFrame $widget -parent $parent -label $label -labelFont $labelFont \
      -contextHelp "$contextHelp" -packOption $packOption

    set w $parent$widget.frame
    ${ttkPrefix}frame $w.list 
    ${ttkPrefix}frame $w.ops
    eval pack $w.list $packOption
    #pack $w.list -side top -expand 1 -fill y
    pack $w.ops -side top

    listbox $w.list.lbox -font $textFont \
      -yscrollcommand "$w.list.scroll set" -height $height -selectmode $selectMode
    
    ${ttkPrefix}scrollbar $w.list.scroll -command "$w.list.lbox yview"
    set i 0
    set maxwidth 0
    set itemsInList [llength [set ${arrayName}([lindex $columnList 0])]]
    for {set i 0} {$i<$itemsInList} {incr i} {
        set data {}
        foreach name $columnList {
            lappend data [lindex [set ${arrayName}($name)] $i]
        }
        set label [$LabelMaker -dataList $data -widthList $maxlen]
        set width [string length $label]
        if {$widthLimit && $width>$widthLimit} {
            set width $widthLimit
        }
        if {$width>$maxwidth} { 
            set maxwidth $width
            $w.list.lbox configure -width [expr {$maxwidth+2}]
        }
        if {$trim} {
            # remove leading and trailing spaces
            regsub -all \[\001-\026\] [string trim $label] "" label
            # remove repeated spaces 
            while {[string first "   " $label]!=-1} {
                regsub -all "   " $label " " label
            }
        }
        $w.list.lbox insert end $label
    }
    pack $w.list.lbox $w.list.scroll -side left -expand 0 -fill y
    
    global tcl_platform
    
    if {$tcl_platform(os) != "Linux"} {
        bind $w.list.lbox <Double-ButtonPress-1> \
          "APSSDDSListboxRunCallback $w.list.lbox -callback [list $callback] -y %y"
    } else {
        bind $w.list.lbox <ButtonPress-1> \
          "APSSDDSListboxRunCallback $w.list.lbox -callback [list $callback] -y %y"
    }

    if {$doneButton} {
        APSButton .done -parent $w.ops -text DONE -size $buttonSize \
          -command "destroy $parent$widget" \
          -contextHelp "Remove listbox."
    }

} 

proc APSSDDSListboxRunCallback {widget args} {
    set callback ""
    set y 0
    APSStrictParseArguments {callback y}
    eval $callback [$widget nearest $y]
}
