##
## Copyright 2001 Digi International, jeffa@digi.com
##
package require Widget 2.0
package provide Spinbox 1.0
##
## This widget needs more work before it's implemented.
## Fix the following:
## 	-ListVar
##	-spin direction
##------------------------------------------------------------------------
## PROCEDURE
##	spinbox
##
## DESCRIPTION
##	Implements a spinbox megawidget
##
## ARGUMENTS
##	spinbox <window pathname> <options>
##
## OPTIONS
##	(Any listbox widget option may be used in addition to these)
##
## -list list				DEFAULT: {}
##	List for the listbox
##
## RETURNS: the window pathname
##
## METHODS
##	These are the methods that the Spinbox recognizes.  Aside from
##	those listed here, it accepts what is valid for listbox widgets.
##
## configure ?option? ?value option value ...?
##
## cget option
##	Standard tk widget routines.
##
## getindex
##	returns the index (starting at zero) of the item visible in the
##	spinbox.
##
## NAMESPACE & STATE
##	The megawidget creates a global array with the classname, and a
## global array which is the name of each megawidget is created.  The latter
## array is deleted when the megawidget is destroyed.
##	Public procs of $CLASSNAME and [string tolower $CLASSNAME] are used.
## Other procs that begin with $CLASSNAME are private.  For each widget,
## commands named .$widgetname and $CLASSNAME$widgetname are created.
##
## EXAMPLE USAGE:
##
## pack [spinbox .spin -list {one two three four}]
## set index [.spin getindex]
##
##------------------------------------------------------------------------

# Create this to make sure there are registered in auto_mkindex
# these must come before the [widget create ...]
proc Spinbox args {}
proc spinbox args {}
widget create Spinbox -type frame -base listbox -components {
    {base listbox listbox { -height 1 \
	    -yscrollcommand [list $data(yscrollbar) set]}}
    {scrollbar yscrollbar sy {-orient vertical \
	    -command [list $w yview]}}
} -options {
    {-list		list		List		{}}
}

namespace eval ::Widget::Spinbox {;

namespace import -force ::Utility::best_match

;proc construct {w} {
    upvar \#0 [namespace current]::$w data

    grid $data(listbox) $data(yscrollbar) 
    #grid columnconfig $w 0 -weight 1
    #grid rowconfig $w 0 -weight 1
    
}

;proc configure {w args} {
    upvar \#0 [namespace current]::$w data
    set truth {^(1|yes|true|on)$}
    foreach {key val} $args {
	switch -- $key {
	    -list	{
		$data(listbox) delete 0 end
		eval $data(listbox) insert end $val
	    }
	}
    }
}

# yview returns the offset and span of visible items and are real numbers between 0 and 1
# Assuming spinbox visible is always one item, the index can be calculated
;proc _getindex {w args} {
    upvar \#0 [namespace current]::$w data
    catch {uplevel $data(basecmd) yview $args} lstYpos
    set num1 [lindex $lstYpos 0]
    set num2 [lindex $lstYpos 1]
    set res [expr round($num2 / ( $num2 - $num1))]
    # index will start at zero
    return [expr $res - 1]
}

}; # end namespace ::Widget::Spinbox



