#!/bin/sh
# the next line restarts using wish\
exec wish "$0" "$@" 
global tcl_platform
if {![info exists imScilab(sourcing)]} {
    switch $tcl_platform(platform) {
	windows {
	}
	default {
	    option add *Scrollbar.width 10
	}
    }
    
}
#############################################################################
# Visual Tcl v1.51 Project
#

#################################
# imScilab LIBRARY PROCEDURES
#

if {![info exists imScilab(sourcing)]} {
proc Window {args} {
    global imScilab
    set cmd     [lindex $args 0]
    set name    [lindex $args 1]
    set newname [lindex $args 2]
    set rest    [lrange $args 3 end]
    if {$name == "" || $cmd == ""} { return }
    if {$newname == ""} { set newname $name }
    if {$name == "."} { wm withdraw $name; return }
    set exists [winfo exists $newname]
    switch $cmd {
        show {
            if {$exists} {
                wm deiconify $newname
            } elseif {[info procs imScilabWindow$name] != ""} {
                eval "imScilabWindow$name $newname $rest"
            }
            if {[wm state $newname] == "normal"} {
                imScilab:FireEvent $newname <<Show>>
            }
        }
        hide    {
            if {$exists} {
                wm withdraw $newname
                imScilab:FireEvent $newname <<Hide>>
                return}
        }
        iconify { if $exists {wm iconify $newname; return} }
        destroy { if $exists {destroy $newname; return} }
    }
}

proc imScilab:WindowsCleanup {} {
    global imScilab
    if {[info exists imScilab(sourcing)]} { return }
    foreach w [winfo children .] {
    	wm protocol $w WM_DELETE_WINDOW { exit }
    }
}
}

if {![info exists imScilab(sourcing)]} {
namespace eval ::progressbar {

proc {::progressbar::Build} {w args} {
  variable widgetOptions
  variable widgetGlobals

  if {$widgetGlobals(debug)} {
    puts stderr "pb_Build '$w' '$args'"
  }

  # create the namespace for this instance, and define a few
  # variables
  namespace eval ::progressbar::$w {
    variable options
    variable widgets
    variable info
  }

  # this gives us access to the namespace variables within
  # this proc
  upvar ::progressbar::${w}::widgets widgets
  upvar ::progressbar::${w}::options options
  upvar ::progressbar::${w}::info info

  set info(rgb) ""
  set info(rgbHasChanged) 0

  # this is our widget -- a frame of class Progressbar. Naturally,
  # it will contain other widgets. We create it here because
  # we need it to be able to set our default options.
  set widgets(this) [frame $w -class Progressbar1]

  # this defines all of the default options. We get the
  # values from the option database. Note that if an array
  # value is a list of length one it is an alias to another
  # option, so we just ignore it
  foreach name [array names widgetOptions] {
    if {[llength $widgetOptions($name)] == 1} continue
    set optName  [lindex $widgetOptions($name) 0]
    set optClass [lindex $widgetOptions($name) 1]
    set options($name) [option get $w $optName $optClass]
    if {$widgetGlobals(debug) > 1} {
      puts stderr "pb_Build:Opt '$w' '$optName' '$optClass' '$options($name)'"
    }
  }

  # now apply any of the options supplied on the command
  # line. This may overwrite our defaults, which is OK
  if {[llength $args] > 0} {
    array set options $args
  }
  
  # this will only set the name of canvas's widget, we will
  # later create the canvas in our drawing procedure.
  set widgets(canvas) $w.pb

  # we will later rename the frame's widget proc to be our
  # own custom widget proc. We need to keep track of this
  # new name, so we'll define and store it here...
  set widgets(frame) ::progressbar::${w}::$w

  # this moves the original frame widget proc into our
  # namespace and gives it a handy name
  rename ::$w $widgets(frame)

  # Alias the window to our WidgetProc and pass the window name.
  interp alias {} ::$w {} ::progressbar::WidgetProc $w

  # ok, the thing exists... let's do a bit more configuration. 
  if {[catch "Configure $widgets(this) [array get options]" error]} {
    return -code error $error
    catch {destroy $w}
  }

  return $w
}

}

namespace eval ::progressbar {

proc {::progressbar::Canonize} {w object opt} {
  variable widgetOptions
  variable widgetCommands
  variable widgetGlobals
  variable widgetShapes

  if {$widgetGlobals(debug)} {
    puts stderr "pb_Canonize '$w' '$object' '$opt'"
  }

  switch $object {
    command {
      if {[lsearch -exact $widgetCommands $opt] >= 0} {
	return $opt
      }

      # command names aren't stored in an array, and there
      # isn't a way to get all the matches in a list, so
      # we'll stuff the columns in a temporary array so
      # we can use [array names]
      set list $widgetCommands
      foreach element $list {
	set tmp($element) ""
      }
      set matches [array names tmp ${opt}*]
    }

    option {
      if {[info exists widgetOptions($opt)]  && [llength $widgetOptions($opt)] == 3} {
	return $opt
      }
      set list [array names widgetOptions]
      set matches [array names widgetOptions ${opt}*]
    }

    shape {
      if {[lsearch -exact $widgetShapes $opt] >= 0} {
	return $opt
      }

      # same procedure as command
      set list $widgetShapes
      foreach element $list {
	set tmp($element) ""
      }
      set matches [array names tmp ${opt}*]
    }
  }
  if {[llength $matches] == 0} {
    set choices [HumanizeList $list]
    return -code error "unknown $object \"$opt\"; must be one of $choices"
  } elseif {[llength $matches] == 1} {
    # deal with option aliases
    set opt [lindex $matches 0]
    switch $object {
      option {
	if {[llength $widgetOptions($opt)] == 1} {
	  set opt $widgetOptions($opt)
	}
      }
    }
    return $opt
  } else {
      set choices [HumanizeList $list]
      return -code error "ambiguous $object \"$opt\"; must be one of $choices"
  }
}

}

namespace eval ::progressbar {

proc {::progressbar::Configure} {w args} {
  variable widgetOptions
  variable widgetGlobals

  if {$widgetGlobals(debug)} {
    puts stderr "pb_Configure '$w' '$args'"
  }

  upvar ${w}::widgets widgets
  upvar ${w}::options options
  upvar ${w}::info info
  
  if {[llength $args] == 0} {
    # hmmm. User must be wanting all configuration information
    # note that if the value of an array element is of length
    # one it is an alias, which needs to be handled slightly
    # differently
    set results {}
    foreach opt [lsort [array names widgetOptions]] {
      if {[llength $widgetOptions($opt)] == 1} {
	set alias $widgetOptions($opt)
	set optName $widgetOptions($alias)
	lappend results [list $opt $optName]
      } else {
	set optName  [lindex $widgetOptions($opt) 0]
	set optClass [lindex $widgetOptions($opt) 1]
	set default [option get $w $optName $optClass]
	lappend results [list $opt $optName $optClass $default $options($opt)]
      }
    }
    return $results
  }
  
  # one argument means we are looking for configuration
  # information on a single option
  if {[llength $args] == 1} {
    set opt [Canonize $w option [lindex $args 0]]
    set optName  [lindex $widgetOptions($opt) 0]
    set optClass [lindex $widgetOptions($opt) 1]
    set default [option get $w $optName $optClass]
    set results [list $opt $optName $optClass $default $options($opt)]
    return $results
  }

  # if we have an odd number of values, bail. 
  if {[expr {[llength $args]%2}] == 1} {
    # hmmm. An odd number of elements in args
    return -code error "value for \"[lindex $args end]\" missing"
  }
  
  # Great. An even number of options. Let's make sure they 
  # are all valid before we do anything. Note that Canonize
  # will generate an error if it finds a bogus option; otherwise
  # it returns the canonical option name
  foreach {name value} $args {
    set name [Canonize $w option $name]
    set opts($name) $value
  }

  # process all of the configuration options
  foreach option [array names opts] {
    set newValue $opts($option)
    if {[info exists options($option)]} {
      set oldValue $options($option)
    }

    if {$widgetGlobals(debug) > 2} {
      puts stderr "pb_Configure:Opt '$option' n='$newValue' o='$oldValue'"
    }
    switch -- $option {
      -background  -
      -borderwidth -
      -relief      {
	if {[winfo exists $widgets(this)]} {
	  $widgets(frame) configure $option $newValue
	  set options($option) [$widgets(frame) cget $option]
	}
      }
      -color {
        switch -- $newValue {
	  @blue0    -
	  @blue1    -
	  @blue2    -
	  @blue3    -
	  @blue4    -
	  @green0   -
	  @green1   -
	  @green2   -
	  @green3   -
	  @yellow0  -
	  @yellow1  -
	  @red0     -
	  @red1     -
	  @magenta0 -
	  @brown0   -
	  @brown1   -
	  @gray0    {
	    set info(rgb) $widgetGlobals($newValue)
	  }
	  @* {
	    set info(rgb) $widgetGlobals(@saphir)
	  }
	  default {
	    set info(rgb) [RGBs $newValue]
	  }
	}
	set info(rgbHasChanged) 1
      }
      -percent {
	set options($option) $newValue
      }
      -shape {
	set options($option) [Canonize $w shape $newValue]
	set info(rgbHasChanged) 1
      }
      -variable {
	# hmmm .. are there any traces left? Yes! Destroy!
	if {[info procs Trace($w)] != ""} {
	  uplevel #0 trace vdelete $oldValue wu ::progressbar::Trace($w)
	  unset widgetGlobals($w)
	  rename Trace($w) {}
	}
	if {$newValue != ""} {
	  # there is a new variable to trace. build a new proc to trace it.
	  proc ::progressbar::Trace($w) {name1 name2 op} "
	    variable widgetGlobals

	    if {\$widgetGlobals(debug)} {
	      puts stderr \"pb_Trace($w) '\$name1' '\$name2' '\$op'\"
	    }
	    switch -- \$op {
	      w {
		if {\$name2 != \"\"} {
		  upvar 1 \${name1}(\$name2) var
		  catch {$w configure -percent \$var}
		} else {
		  upvar 1 \$name1 var
		  catch {$w configure -percent \$var}
		}
	      }
	      u {
		if {\[info procs Trace($w)\] != \"\"} {  unset widgetGlobals($w);  rename Trace($w) {};  }
	      }
	    }
	  "
	  # install trace proc for variable
	  uplevel #0 trace variable $newValue wu ::progressbar::Trace($w)
	}
	set options($option) $newValue
	set widgetGlobals($w) $newValue
      }
      -width {
	if {$newValue < 20} {
	  return -code error "a -width of less than 20 is not supported."
	}
	if {[winfo exists $widgets(canvas)]} {
	  $widgets(canvas) configure $option $newValue
	  set options($option) [$widgets(canvas) cget $option]
	} else {
          set options($option) $newValue
	}
      }
      -textvalue {
        set options($option) $newValue
	if {![winfo exists $widgets(canvas)]} { continue }
	$widgets(canvas) itemconfigure ttxt -text $newValue
      }
      -textcolor {
        set options($option) $newValue
	if {![winfo exists $widgets(canvas)]} { continue }
	$widgets(canvas) itemconfigure ttxt -fill $newValue
      }
    }
  }

  Draw $w
}

}

namespace eval ::progressbar {

proc {::progressbar::DestroyHandler} {w} {
  variable widgetGlobals

  if {$widgetGlobals(debug)} {
    puts stderr "pb_DestroyHandler '$w'"
  }

  # hmmm .. are there any traces left? Yes! Destroy!
  if {[info procs Trace($w)] != ""} {
    uplevel 1 trace vdelete $widgetGlobals($w) wu ::progressbar::Trace($w)
    unset widgetGlobals($w)
    rename Trace($w) {}
  }

  # if the widget actually being destroyed is of class Progressbar,
  # crush the namespace and kill the proc. Get it? Crush. Kill. 
  # Destroy. Heh. Danger Will Robinson! Oh, man! I'm so funny it
  # brings tears to my eyes.
  if {[string compare [winfo class $w] "Progressbar1"] == 0} {
    namespace delete ::progressbar::$w
    rename $w {}
  }
}

}

namespace eval ::progressbar {

proc {::progressbar::Draw} {w} {
  variable widgetGlobals

  if {$widgetGlobals(debug) > 2} {
    puts stderr "pb_Draw '$w'"
  }

  upvar ${w}::widgets widgets
  upvar ${w}::options options
  upvar ${w}::info info

  set width   $options(-width)
  set percent $options(-percent)
  set text    $options(-textvalue)

  if {$options(-shape) == "flat"} {
    set minDisplay 0
    if {[llength $info(rgb)] == 7} {
      set rgb(0) [lindex $info(rgb) 6]
    } else {
      set rgb(0) [lindex $info(rgb) 2]
    }
    set rgb(1) $rgb(0)
    set rgb(2) $rgb(0)
    set rgb(3) $rgb(0)
    set rgb(4) $rgb(0)
    set rgb(5) $rgb(0)
  } else {
    set minDisplay 7
    set rgb(0) [lindex $info(rgb) 0]
    set rgb(1) [lindex $info(rgb) 1]
    set rgb(2) [lindex $info(rgb) 2]
    set rgb(3) [lindex $info(rgb) 3]
    set rgb(4) [lindex $info(rgb) 4]
    set rgb(5) [lindex $info(rgb) 5]
  }

  if {$percent < 0} {
    set percent 0
  } elseif {$percent > 100} {
    set percent 100
  }
  if {$percent == 0} {
    set mark $minDisplay
  } else {
    set mark [expr (($width - $minDisplay) / 100.0 * $percent) + $minDisplay]
  }

  if {![winfo exists $widgets(canvas)]} {
    canvas $widgets(canvas) -width $width -height 14 -bd 0 -highlightthickness 0
    pack $widgets(canvas) -side left -anchor nw -fill both

    foreach {type color tag coords opts} $widgetGlobals(toDraw) {
      eval $widgets(canvas) create $type $coords -fill $color -tag t$tag $opts
    }

    set info(rgbHasChanged) 0
    # nothing more to do
    return
  }

  foreach {type color tag coords opts} $widgetGlobals(toDraw) {
    eval $widgets(canvas) coords t$tag $coords
    if {$info(rgbHasChanged)} {
      eval $widgets(canvas) itemconfigure t$tag -fill $color
    }
  }
  set info(rgbHasChanged) 0
}

}

namespace eval ::progressbar {

proc {::progressbar::HumanizeList} {list} {
  variable widgetGlobals

  if {$widgetGlobals(debug)} {
    puts stderr "pb_HumanizeList $list"
  }

  if {[llength $list] == 1} {
    return [lindex $list 0]
  } else {
    set list [lsort $list]
    set secondToLast [expr {[llength $list] -2}]
    set most [lrange $list 0 $secondToLast]
    set last [lindex $list end]

    return "[join $most {, }] or $last"
  }
}

}

namespace eval ::progressbar {

proc {::progressbar::Init} {} {
  variable widgetOptions
  variable widgetCommands
  variable widgetGlobals
  variable widgetShapes

  if {$widgetGlobals(debug)} {
    puts stderr "pb_Init"
  }

  # here we match up command line options with option database names
  # and classes. As it turns out, this is a handy reference of all of the
  # available options. Note that if an item has a value with only one
  # item (like -bd, for example) it is a synonym and the value is the
  # actual item.

  array set widgetOptions {
    -background		{background	Background	}
    -borderwidth	{borderWidth	BorderWidth	}
    -color		{color		Color		}
    -cursor		{cursor		Cursor		}
    -percent		{percent	Percent		}
    -relief		{relief		Relief		}
    -shape		{shape		Shape		}
    -variable		{variable	Variable	}
    -width		{width		Width		}
    -textvalue		{textValue	TextValue	}
    -textcolor		{textColor	TextColor	}

    -bg			-background
    -bd			-borderwidth
    -pc			-percent
  } 

  # this defines the valid widget commands. It's important to
  # list them here; we use this list to validate commands and
  # expand abbreviations.

  set widgetCommands {
      cget
      configure
      incr
      step
  }

  # this defines the valid shape options. It's important to
  # list them here; we use this list to validate options and
  # expand abbreviations.

  set widgetShapes {
      3D
      3d
      flat
  }
      
  set widgetGlobals(toDraw) {
    rect #bdbdbd es0 {[expr $mark +3] 2 [expr $width -2] 11} {-outline ""}
    line #525252 es1 {[expr $mark +1] 2 [expr $mark +1] 11} {}
    line #8c8c8c es2 {[expr $mark +2] 11 [expr $mark +2] 2  [expr $width -4] 2} {}
    line #8c8c8c es3 {[expr $mark +3] 11 [expr $width -3] 11  [expr $width -3] 3} {}
    line $rgb(0) pb0 {4 11 [expr $mark -1] 11 [expr $mark -1] 3} {}
    line $rgb(1) pb1 {3 11 3 10 [expr $mark -2] 10 [expr $mark -2] 2  [expr $mark -1] 2 4 2} {}
    line $rgb(2) pb2 {3 2 2 2 2 11 2 10 3 10 3 9 [expr $mark -3] 9  [expr $mark -3] 3 [expr $mark -2] 3 4 3} {}
    line $rgb(3) pb3 {3 3 3 9 3 8 [expr $mark -3] 8 [expr $mark -3] 4 4 4} {}
    line $rgb(4) pb4 {3 4 3 8 3 7 [expr $mark -3] 7 [expr $mark -3] 5 4 5} {}
    line $rgb(5) pb5 {3 5 3 7 3 6 [expr $mark -3] 6} {}
    line #000000 mrk {$mark 1 $mark 12} {}
    line #adadad fr0 {0 12 0 0 [expr $width -1] 0} {}
    line #ffffff fr1 {1 13 [expr $width -1] 13 [expr $width -1] 1} {}
    line #000000 fr2 {1 1 [expr $width -2] 1 [expr $width -2] 12 1 12 1 1} {}
    text #000000 txt {[expr $width / 2] 8} {-text $text}
  }

  set widgetGlobals(@blue0) {#000052 #0031ce #3163ff #639cff #9cceff #efefef}
  set widgetGlobals(@blue1) {#000021 #00639c #009cce #00ceff #63ffff #ceffff}
  set widgetGlobals(@blue2) {#000052 #31319c #6363ce #9c9cff #ceceff #efefef}
  set widgetGlobals(@blue3)	 {#21214a #52527b #63639c #8484bd #b5b5ef #ceceff}
  set widgetGlobals(@blue4) {#29396b #4a6b9c #6384b5 #739cd6 #94b5ef #adceff}
  set widgetGlobals(@green0)	 {#003131 #08736b #318c94 #5abdad #63dece #ceffef}
  set widgetGlobals(@green1) {#001000 #003100 #316331 #639c63 #9cce9c #ceffce}
  set widgetGlobals(@green2) {#002100 #006331 #319c63 #31ce63 #63ff9c #ceffce}
  set widgetGlobals(@green3) {#003131 #316363 #427b7b #639c9c #9ccece #bdefef}
  set widgetGlobals(@yellow0) {#101010 #636300 #9c9c00 #cece00 #ffff00 #ffff9c}
  set widgetGlobals(@yellow1) {#8c7321 #cead39 #e7c642 #f7de63 #f7de63 #ffffe7}
  set widgetGlobals(@red0) {#420000 #9c0000 #ce3131 #ff6363 #ff9c9c #ffcece}
  set widgetGlobals(@red1) {#210000 #9c3100 #ce6331 #ff9c63 #ffce9c #ffffce}
  set widgetGlobals(@magenta0) {#210000 #630063 #9c319c #ce63ce #ff9cff #ffceff}
  set widgetGlobals(@brown0) {#210000 #633100 #9c6331 #ce9c63 #efb573 #ffdeb5}
  set widgetGlobals(@brown1) {#310000 #7b4242 #9c6363 #ce9c9c #efcece #ffdede}
  set widgetGlobals(@gray0) {#212121 #525252 #737373 #adadad #cecece #efefef}

  # this initializes the option database. Kinda gross, but it works
  # (I think).
  set tmpWidget ".__tmp__"

  # steal some options from frame widgets; we only want a subset
  # so we'll use a slightly different method. No harm in *not*
  # adding in the one or two that we don't use... :-)
  label $tmpWidget
  foreach option [list Background Relief] {
    set values [$tmpWidget configure -[string tolower $option]]
    option add *Progressbar1.$option [lindex $values 3]
  }
  destroy $tmpWidget

  # these are unique to us...
  option add *Progressbar1.borderWidth	5		widgetDefault
  option add *Progressbar1.color	@blue0		widgetDefault
  option add *Progressbar1.percent	0		widgetDefault
  option add *Progressbar1.shape	3D		widgetDefault
  option add *Progressbar1.variable	{}		widgetDefault
  option add *Progressbar1.width	180		widgetDefault
  option add *Progressbar1.textColor	black		widgetDefault

  # define the class bindings
  # this allows us to clean up some things when we go away
  bind Progressbar1 <Destroy> [list ::progressbar::DestroyHandler %W]
}

}

namespace eval ::progressbar {

proc {::progressbar::RGBs} {color} {
  variable widgetGlobals

  if {$widgetGlobals(debug)} {
    puts stderr "pb_RGB '$color'"
  }

  # get rgb values of given color
  set color [winfo rgb . $color]

  set R [expr int([lindex $color 0] / 256)]
  set G [expr int([lindex $color 1] / 256)]
  set B [expr int([lindex $color 2] / 256)]

  set rgb {}
  foreach factor {0.13 0.32 0.45 0.68 0.8 0.93} {
    set r [expr int($R * $factor)]
    set g [expr int($G * $factor)]
    set b [expr int($B * $factor)]
    lappend rgb [format "#%02x%02x%02x" $r $g $b]
  }
  lappend rgb [format "#%02x%02x%02x" $R $G $B]

  return $rgb
}

}

namespace eval ::progressbar {

proc {::progressbar::WidgetProc} {w args} {
  variable widgetOptions
  variable widgetGlobals

  if {[llength $args] == 0} {
      return -code error [imScilab:WrongNumArgs "$w option ?arg arg ...?"]
  }

  set command [lindex $args 0]
  set args [lrange $args 1 end]

  if {$widgetGlobals(debug)} {
    puts stderr "pb_WidgetProc '$w' '$command' '$args'"
  }

  upvar ::progressbar::${w}::widgets   widgets
  upvar ::progressbar::${w}::options   options
  upvar ::progressbar::${w}::info info

  set command [Canonize $w command $command]

  set result ""

  switch $command {
    cget {
      if {[llength $args] != 1} {
	return -code error "wrong # args: should be $w cget option"
      }
      set opt [Canonize $w option [lindex $args 0]]
      set result $options($opt)
    }

    configure {
      set result [eval Configure {$w} $args]
    }

    incr -
    step {
      set val 1
      if {[llength $args] > 1} {
      	return -code error "wrong # args: should be $w $command <incrValue>"
      } elseif {[llength $args] == 1} {
      	set val $args
      }
      set percent [$w cget -percent]
      set result [eval Configure $w -percent [expr $percent + $val]]
    }

    default {
	return -code error "bad option \"$command\": must be cget or configure"
    }
  }
  return $result
}

}

namespace eval ::progressbar {

proc {::progressbar::progressbar} {args} {
  variable widgetOptions
  variable widgetGlobals

  # perform a one time initialization
  if {![info exists widgetOptions]} {
    __progressbar_Setup
    Init
  }

  if {$widgetGlobals(debug)} {
    puts stderr "pb_progressbar '$args'"
  }

  # make sure we at least have a widget name
  if {[llength $args] == 0} {
    return -code error  "wrong # args: should be \"progressbar pathName ?options?\""
  }

  # ... and make sure a widget doesn't already exist by that name
  if {[winfo exists [lindex $args 0]]} {
    return -code error "window name \"[lindex $args 0]\" already exists"
  }

  # and check that all of the args are valid
  foreach {name value} [lrange $args 1 end] {
    Canonize [lindex $args 0] option $name
  }

  # build it...
  set w [eval Build $args]

  # and we are done!
  return $w
}

}

proc {__progressbar_Setup} {} {
  namespace eval ::progressbar {
    # this is the public interface
    namespace export progressbar

    # these contain references to available options
    variable widgetOptions

    # these contain references to available commands
    variable widgetCommands

    # these contain references to available options for shape option
    variable widgetShapes

    # these contain references to global variables
    variable widgetGlobals

    set widgetGlobals(debug) 0
  }
}

proc {imScilab:DefineAlias} {target alias widgetProc top_or_alias cmdalias} {
    global widget

    set widget($alias) $target
    set widget(rev,$target) $alias

    if {$cmdalias} {
        interp alias {} $alias {} $widgetProc $target
    }

    if {$top_or_alias != ""} {
        set widget($top_or_alias,$alias) $target

        if {$cmdalias} {
            interp alias {} $top_or_alias.$alias {} $widgetProc $target
        }
    }
}

proc {imScilab:DoCmdOption} {target cmd} {
    ## menus are considered toplevel windows
    set parent $target
    while {[winfo class $parent] == "Menu"} {
        set parent [winfo parent $parent]
    }

    regsub -all {\%widget} $cmd $target cmd
    regsub -all {\%top} $cmd [winfo toplevel $parent] cmd

    uplevel #0 [list eval $cmd]
}

proc {imScilab:FireEvent} {target event} {
    foreach bindtag [bindtags $target] {
        set tag_events [bind $bindtag]
        set stop_processing 0
        foreach tag_event $tag_events {
            if {$tag_event == $event} {
                set bind_code [bind $bindtag $tag_event]
                regsub -all %W $bind_code $target bind_code
                set result [catch {uplevel #0 $bind_code} errortext]
                if {$result == 3} {
                    # break exception, stop processing
                    set stop_processing 1
                } elseif {$result != 0} {
                    bgerror $errortext
                }
                break
            }
        }
        if {$stop_processing} {break}
    }
}

proc {imScilab:Toplevel:WidgetProc} {w args} {
    if {[llength $args] == 0} {
        return -code error "wrong # args: should be \"$w option ?arg arg ...?\""
    }

    ## The first argument is a switch, they must be doing a configure.
    if {[string index $args 0] == "-"} {
        set command configure

        ## There's only one argument, must be a cget.
        if {[llength $args] == 1} {
            set command cget
        }
    } else {
        set command [lindex $args 0]
        set args [lrange $args 1 end]
    }

    switch -- $command {
        "hide" -
        "Hide" {
            Window hide $w
        }

        "show" -
        "Show" {
            Window show $w
        }

        "ShowModal" {
            Window show $w
            raise $w
            grab $w
            tkwait window $w
            grab release $w
        }

        default {
            eval $w $command $args
        }
    }
}

proc {imScilab:WidgetProc} {w args} {
    if {[llength $args] == 0} {
        return -code error "wrong # args: should be \"$w option ?arg arg ...?\""
    }

    ## The first argument is a switch, they must be doing a configure.
    if {[string index $args 0] == "-"} {
        set command configure

        ## There's only one argument, must be a cget.
        if {[llength $args] == 1} {
            set command cget
        }
    } else {
        set command [lindex $args 0]
        set args [lrange $args 1 end]
    }

    eval $w $command $args
}

proc {imScilab:toplevel} {args} {
    uplevel #0 eval toplevel $args
    set target [lindex $args 0]
    namespace eval ::$target {}
}
}

if {[info exists imScilab(sourcing)]} {
proc imScilab:project:info {} {
    namespace eval ::widgets::.avisub {
        array set save {}
    }
    namespace eval ::widgets::.avisub.fra24 {
        array set save {-borderwidth 1 -height 1 -relief 1 -width 1}
    }
    namespace eval ::widgets::.avisub.fra24.mes27 {
        array set save {-text 1 -width 1}
    }
    namespace eval ::widgets::.avisub.fra24.pro28 {
        array set save {-percent 1}
    }
    namespace eval ::widgets_bindings {
        set tagslist {}
    }
}
}
#################################
# USER DEFINED PROCEDURES
#
###########################################################
## Procedure:  init
###########################################################
## Procedure:  main

proc {main} {} {
}

proc init {} {
}

init

#################################
# imScilab GENERATED GUI PROCEDURES
#

proc imScilabWindow. {base {container 0}} {
    if {$base == ""} {
        set base .
    }
    global imScilab
    ###################
    # CREATING WIDGETS
    ###################
    if {!$container} {
    wm focusmodel $base passive
    wm geometry $base 200x200+88+116; update
    wm maxsize $base 1028 746
    wm minsize $base 115 1
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm withdraw $base
    wm title $base "imScilab"
    bindtags $base "$base imScilab all"
    imScilab:FireEvent $base <<Create>>
    }
    ###################
    # SETTING GEOMETRY
    ###################

    imScilab:FireEvent $base <<Ready>>
}

proc imScilabWindow.avisub {base {container 0}} {
    if {$base == ""} {
        set base .avisub
    }
    if {[winfo exists $base] && (!$container)} {
        wm deiconify $base; return
    }
    global imScilab
    global widget
    imScilab:DefineAlias "$base" "Toplevel1" imScilab:Toplevel:WidgetProc "" 1
    imScilab:DefineAlias "$base.fra24" "Frame1" imScilab:WidgetProc "Toplevel1" 1
    imScilab:DefineAlias "$base.fra24.mes27" "Message1" imScilab:WidgetProc "Toplevel1" 1
    imScilab:DefineAlias "$base.fra24.pro28" "Progressbar1" imScilab:WidgetProc "Toplevel1" 1

    ###################
    # CREATING WIDGETS
    ###################
    if {!$container} {
    imScilab:toplevel $base -class Toplevel
    wm transient $base .avi
    wm focusmodel $base passive
    wm geometry $base 321x85+260+301; update
    wm maxsize $base 1028 746
    wm minsize $base 115 1
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm deiconify $base
    wm title $base "Avi Save Progress"
    imScilab:FireEvent $base <<Create>>
    }
    frame $base.fra24 \
        -borderwidth 2 -relief groove -height 75 -width 310 
    message $base.fra24.mes27 \
        -text {AviFile Saving,Please Wait...} -width 176 
    ::progressbar::progressbar $base.fra24.pro28 \
        -percent 0 -textvalue 0%
    ###################
    # SETTING GEOMETRY
    ###################
    place $base.fra24 \
        -in $base -x 5 -y 5 -width 310 -height 75 -anchor nw \
        -bordermode ignore 
    place $base.fra24.mes27 \
        -in $base.fra24 -x 65 -y 15 -width 176 -height 21 -anchor nw \
        -bordermode ignore 
    place $base.fra24.pro28 \
        -in $base.fra24 -x 60 -y 40 -anchor nw -bordermode ignore 

    imScilab:FireEvent $base <<Ready>>
}

Window show .
Window show .avisub

main
