#
# Feedback
# ----------------------------------------------------------------------
# Implements a Feedback widget, to display feedback on the status of an 
# process to the user. Display is given as a percentage and as a 
# thermometer type bar. Options exist for adding a label and controlling its
# position.
#
# ----------------------------------------------------------------------
#  AUTHOR: Kris Raney                    EMAIL: kraney@spd.dsccc.com
#
#  @(#) $Id: feedback.itk,v 1.5 2001/08/15 18:32:18 smithc Exp $
# ----------------------------------------------------------------------
#            Copyright (c) 1996 DSC Technologies Corporation
# ======================================================================
# Permission to use, copy, modify, distribute and license this software
# and its documentation for any purpose, and without fee or written
# agreement with DSC, is hereby granted, provided that the above copyright
# notice appears in all copies and that both the copyright notice and
# warranty disclaimer below appear in supporting documentation, and that
# the names of DSC Technologies Corporation or DSC Communications
# Corporation not be used in advertising or publicity pertaining to the
# software without specific, written prior permission.
#
# DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON-
# INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE
# AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE,
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL
# DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
# ======================================================================

# Acknowledgements:
#
# Special thanks go to Sam Shen(SLShen@lbl.gov), as this code is based on his 
# feedback.tcl code from tk inspect. The original code is copyright 1995
# Lawrence Berkeley Laboratory.
#
# This software is copyright (C) 1994 by the Lawrence Berkeley Laboratory.
#  
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that: (1) source code distributions
# retain the above copyright notice and this paragraph in its entirety, (2)
# distributions including binary code include the above copyright notice and
# this paragraph in its entirety in the documentation or other materials
# provided with the distribution, and (3) all advertising materials mentioning
# features or use of this software display the following acknowledgement:
# ``This product includes software developed by the University of California,
# Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
# the University nor the names of its contributors may be used to endorse
# or promote products derived from this software without specific prior
# written permission.
#  
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

#
# Default resources.
#
option add *Feedback.borderWidth	2		widgetDefault
option add *Feedback.labelPos		n		widgetDefault
option add *Feedback.barHeight		20		widgetDefault
option add *Feedback.troughColor	White		widgetDefault
option add *Feedback.barColor		Blue	        widgetDefault

#
# Usual options.
#
itk::usual Feedback {
    keep -background -cursor -foreground
}

# ------------------------------------------------------------------
#                          FEEDBACK
# ------------------------------------------------------------------
itcl::class iwidgets::Feedback {
    inherit iwidgets::Labeledwidget

    constructor {args} {}
    destructor {}

    itk_option define -steps steps Steps 10

    public {
	method reset {}
	method step {{inc 1}}
    }

    private {
	method _display

	variable _barwidth 0
	variable _stepval 0
    }
}

#
# Provide a lowercased access method for the Dialogshell class.
# 
proc ::iwidgets::feedback {pathName args} {
    uplevel ::iwidgets::Feedback $pathName $args
}

# ------------------------------------------------------------------
#                        CONSTRUCTOR
# ------------------------------------------------------------------
itcl::body iwidgets::Feedback::constructor {args} {
    itk_component add trough {
	frame $itk_interior.trough -relief sunken
    } {
	usual
	keep -borderwidth
	rename -background -troughcolor troughColor TroughColor
	rename -height -barheight barHeight Height
    }

    itk_component add bar {
	frame $itk_component(trough).bar -relief raised
    } {
	usual
	keep -borderwidth
	rename -background -barcolor barColor BarColor
	rename -height -barheight barHeight Height
    }
    pack $itk_component(bar) -side left -fill y -anchor w

    itk_component add percentage {
	label $itk_interior.percentage -text "0%"
    }
    grid $itk_component(trough) -row 1 -column 0 -sticky sew -padx 2 -pady 2
    grid $itk_component(percentage) -row 2 -column 0 -sticky nsew -padx 2 -pady 2
    grid rowconfigure $itk_interior 0 -weight 1
    grid rowconfigure $itk_interior 1 -weight 1
    grid columnconfigure $itk_interior 0 -weight 1

    bind $itk_component(hull) <Configure> [itcl::code $this _display]

    eval itk_initialize $args
}

# ------------------------------------------------------------------
#                          DESTRUCTOR
# ------------------------------------------------------------------
itcl::body iwidgets::Feedback::destructor {} {
}

# ------------------------------------------------------------------
#                            OPTIONS
# ------------------------------------------------------------------

# ------------------------------------------------------------------
# OPTION: -steps
#
# Set the total number of steps.
# ------------------------------------------------------------------
itcl::configbody iwidgets::Feedback::steps {
    step 0
}

# ------------------------------------------------------------------
#                            METHODS
# ------------------------------------------------------------------

# -----------------------------------------------------------------------------
# PROTECTED METHOD: _display 
#
# Displays the bar in the trough with the width set using the current number
# of steps.
# -----------------------------------------------------------------------------
itcl::body iwidgets::Feedback::_display {} {
    update idletasks
    set troughwidth [winfo width $itk_component(trough)]
    set _barwidth [expr {
      (1.0*$troughwidth-(2.0*[$itk_component(trough) cget -borderwidth])) /
      $itk_option(-steps)}]
    set fraction [expr {int((1.0*$_stepval)/$itk_option(-steps)*100.0)}]

    $itk_component(percentage) config -text "$fraction%"
    $itk_component(bar) config -width [expr {$_barwidth*$_stepval}]

    update
}

# ------------------------------------------------------------------
# METHOD: reset
#
# Resets the status bar to 0
# ------------------------------------------------------------------
itcl::body iwidgets::Feedback::reset {} {
    set _stepval 0
    _display 
}

# ------------------------------------------------------------------
# METHOD: step ?inc?
#
# Increase the value of the status bar by inc. Default to 1
# ------------------------------------------------------------------
itcl::body iwidgets::Feedback::step {{inc 1}} {

    if {$_stepval >= $itk_option(-steps)} {
	return
    }

    incr _stepval $inc
    _display 
}
