

proc APSSwapInWidgetHelp {} {
    return "Usage: APSSwapInWidget widget\n\[-parent <string>\]\t\n\[-swapIn <fullWidgetName>]\n"
}

proc APSSwapInWidget {widget args} {
    global apsWidgetSwappedIn
    set parent ""
    set swapIn ""
    APSStrictParseArguments {parent swapIn}
    set w $parent$widget
    set oldWidget [subst \$apsWidgetSwappedIn($w)]
    if {[string compare $oldWidget $swapIn]==0} {
        return
    }
    if {[winfo exists $oldWidget]} {
        pack forget $oldWidget
    }
    pack $swapIn -fill both
    set apsWidgetSwappedIn($w) $swapIn
    pack propagate $swapIn 1
}

proc APSWidgetSwapFrameHelp {} {
    return "Usage: APSSwapInWidget widget \n\[-parent <string>\]\t\n\[-noPack 1\]\t\n\[-packOption <list>\]\t\n\[-label <string>\]\t\n\[-width <num>\]\t\n\[-height <num>\]\t\n\[-orientation <string>\] where <string> is horizontal or vertical (default)\n\[-geometry <string>\]\t\n\[-contextHelp <string>\]\t\n\[-widgetList <list>\]\t\n\[-labelList <list>\]\t\n\[-commandList <list>\]\t\[-contextHelpList <list>\]\t\n\[-buttonPosition {0|1}\]\t\n\[-buttonSize {small|medium}\]\t\n\[-buttonLimitPerRow <number>\]\t\n\nCreates\t\$parent\$widget.\n\tlabel\n\tframe\n"
}

proc APSWidgetSwapFrame {widget args} {
    global apsContextHelp apsWidgetSwappedIn
    set parent ""
    set noPack 0
    set packOption "-side top"
    set label ""
    set name ""
    set width 150
    set height 120
    set orientation vertical
    set geometry ""
    set contextHelp ""
    set widgetList ""
    set labelList ""
    set commandList ""
    set variable ""
    set valueList ""
    set contextHelpList ""
    set buttonPosition 1
    set buttonSize medium
    set buttonLimitPerRow 0
    APSStrictParseArguments {parent noPack packOption label name width height \
			       orientation geometry contextHelp widgetList labelList \
                               commandList variable valueList contextHelpList buttonPosition buttonSize \
                               buttonLimitPerRow}

    if {$contextHelp != ""} {
	set apsContextHelp($parent$widget) $contextHelp    
    }

    if {[llength $widgetList]==0 || [llength $widgetList]!=[llength $labelList]} {
        return -code error "APSWidgetSwapFrame: widgetList must be same length as labelList (APSWidgetSwapFrame)"
    }
    if {[llength $commandList]!=0 && [llength $commandList]!=[llength $labelList]} {
        return -code error "APSWidgetSwapFrame: commandList must be same length as labelList (APSWidgetSwapFrame)"
    }
    if {[llength $valueList]!=0 && [llength $valueList]!=[llength $labelList]} {
        return -code error "APSWidgetSwapFrame: valueList must be same length as labelList (APSWidgetSwapFrame)"
    }
    if {[llength $valueList]!=0 && [string length $variable]==0} {
        return -code error "APSWidgetSwapFrame: must give a variable when a valueList is given (APSWidgetSwapFrame)"
    }
    if {[llength $contextHelpList] && [llength $contextHelpList]!=[llength $labelList]} {
        return -code error "APSWidgetSwapFrame: contextHelpList is not the same length as the labelList"
    }
    if {$parent == ""} {
	toplevel $widget -bd 1
	wm title $widget $name
	if {$geometry != ""} {
	    wm geometry $widget $geometry
	}
    } else {
	frame $parent$widget -bd 1
	if {!$noPack} {
	    eval pack $parent$widget $packOption
	}
    }

    if {$orientation == "vertical"} {
	set packside "top"
    } else {
	set packside "left"
    }
    if {$label != ""} {
	label $parent$widget.label -text $label
	pack $parent$widget.label -side $packside -fill x
    }

    if {$orientation == "vertical"} {
	set packside [expr {$buttonPosition?"top":"bottom"}]
        set opackside left
        set oanchor n
    } else {
	set packside [expr {$buttonPosition?"left":"right"}]
        set opackside top
        set oanchor w
    }
    frame $parent$widget.frame -relief ridge -bd 2 -width $width \
      -height $height
    pack $parent$widget.frame -side $packside -fill both 

    set w $parent$widget.buttonFrame
    frame $w -relief ridge -bd 2 
    pack $w -side $packside -fill both
    set index 0
    set buttonRow 0
    set numberInRow $buttonLimitPerRow
    foreach elem $labelList {
        set cmd [lindex $commandList $index]
        if {[llength $valueList]} {
            set cmd "$cmd ; global $variable ; set $variable [lindex $valueList $index]"
        }
        set cHelp $contextHelp
        if {[llength $contextHelpList]} {
            set cHelp [lindex $contextHelpList $index]
        }
        if {$buttonLimitPerRow && $numberInRow>=$buttonLimitPerRow} {
            set numberInRow 0
            frame $w.row$buttonRow 
            pack $w.row$buttonRow -fill y -fill x -side $packside 
            set parent1 $w.row$buttonRow
            incr buttonRow
        } elseif {!$buttonLimitPerRow} {
            set parent1 $w
        }
        APSButton .b$index -parent $w -noPack 1 -text $elem -size $buttonSize \
          -command "APSSwapInWidget $widget -parent $parent -swapIn [lindex $widgetList $index]; $cmd" \
          -contextHelp $cHelp
        pack $w.b$index -in $parent1 -side $opackside
        incr index
        incr numberInRow
    }
    set apsWidgetSwappedIn($parent$widget) .NONE
}

