

set apsRadioButtonsID 0
proc APSSDDSRadioButtons {widget args} {
    global apsRadioButtonsID
    incr apsRadioButtonsID
    global apsRadioButtonstkWaitVar$apsRadioButtonsID


    set modal 0
    set page 0
    set variable ""
    set column ""
    set fileName ""
    set parent ""
    set name "Radio Button Dialog"
    set limit 0
    set widthLimit 0
    set callback ""
    set duplicate 0
    APSStrictParseArguments {callback modal page variable column fileName parent name limit duplicate}
    if {$limit==0 && $widthLimit==0} {
        set widthLimit 100
    }

    if {[string length $fileName]==0 || ![file exists $fileName] ||
        [string length $column]==0} { 
        return 0 
    }
    set data [APSGetSDDSColumn -fileName $fileName -column $column -page $page]
    if {[llength $data]==0} {
        return 0
    } elseif {$duplicate == 0} {
        set dataindex 0
        set data [lsort $data]
        set nextdata [lindex $data [expr {$dataindex + 1}]]
        set newdatalist ""

        foreach elem $data {
	    if {($nextdata != $elem)} {
		lappend newdatalist [lindex $data $dataindex]
	    }
	    incr dataindex
	    set nextdata [lindex $data [expr {$dataindex + 1}]]
        }
        unset data
        set data $newdatalist
    }
    global $variable

    APSDialogBox $widget -parent $parent -name $name 
    set w $parent$widget

    set i 0
    set row 0
    set width 0
    set first 1
    foreach elem $data {
        set length [string length $elem]
        set width [expr {$width+$length}]
        if {$first || ($limit!=0 && $i%$limit==0) || ($widthLimit!=0 && $width>$widthLimit)} {
            set first 0
            incr row
            frame $w.userFrame.row$row -bd 2
            pack $w.userFrame.row$row -side top
            set width $length
        }
        radiobutton $w.userFrame.row$row.cb$i -variable $variable -text $elem \
          -value $elem
        pack $w.userFrame.row$row.cb$i -side left
        incr i
    }
    if {[string length $callback]==0} {
        $w.buttonRow.ok.button configure \
          -command "destroy $w; \
                        set apsRadioButtonstkWaitVar$apsRadioButtonsID OK"
        $w.buttonRow.cancel.button configure \
          -command "destroy $w; \
                        set apsRadioButtonstkWaitVar$apsRadioButtonsID CANCEL"  
    } else {
        $w.buttonRow.ok.button configure \
          -command "destroy $w; \
                        $callback $variable OK; \
                        set apsRadioButtonstkWaitVar$apsRadioButtonsID OK"
        $w.buttonRow.cancel.button configure \
          -command "destroy $w; \
                        $callback $variable CANCEL; \
                        set apsRadioButtonstkWaitVar$apsRadioButtonsID CANCEL"  
    }

    APSEnableButton $w.buttonRow.ok.button 

    if {$modal} {
        set oldFocus [focus]
        catch {grab set $w}
        focus $w
        update
        set apsRadioButtonstkWaitVar$apsRadioButtonsID ""
        tkwait variable apsRadioButtonstkWaitVar$apsRadioButtonsID
        focus $oldFocus
        update
        if {[subst \$apsRadioButtonstkWaitVar$apsRadioButtonsID] == "OK"} { return 1 }
        return 0
    }
    return 1
}

