#!/usr/bin/wish -f
# Program: umaxuif
# Tcl version: 7.5 (Tcl/Tk/XF)
# Tk version: 4.1
# XF version: 2.3
#

# Copyright, 1996  Ian McAfee
# Use this for whatever.  Modify it however.  Just leave my name on it.
# However, understand that I guarantee nothing.  Use at your own risk.
# Questions or comments to: ian@wrs.com or lhf@ecst.csuchico.edu (same person)

# module inclusion
global env
global xfLoadPath
global xfLoadInfo
set xfLoadInfo 0
if {[info exists env(XF_LOAD_PATH)]} {
  if {[string first $env(XF_LOAD_PATH) /usr/local/lib/] == -1} {
    set xfLoadPath $env(XF_LOAD_PATH):/usr/local/lib/
  } {
    set xfLoadPath /usr/local/lib/
  }
} {
  set xfLoadPath /usr/local/lib/
}

global argc
global argv
global tkVersion
set tmpArgv ""
for {set counter 0} {$counter < $argc} {incr counter 1} {
  case [string tolower [lindex $argv $counter]] in {
    {-xfloadpath} {
      incr counter 1
      set xfLoadPath "[lindex $argv $counter]:$xfLoadPath"
    }
    {-xfstartup} {
      incr counter 1
      source [lindex $argv $counter]
    }
    {-xfbindfile} {
      incr counter 1
      set env(XF_BIND_FILE) "[lindex $argv $counter]"
    }
    {-xfcolorfile} {
      incr counter 1
      set env(XF_COLOR_FILE) "[lindex $argv $counter]"
    }
    {-xfcursorfile} {
      incr counter 1
      set env(XF_CURSOR_FILE) "[lindex $argv $counter]"
    }
    {-xffontfile} {
      incr counter 1
      set env(XF_FONT_FILE) "[lindex $argv $counter]"
    }
    {-xfmodelmono} {
      if {$tkVersion >= 3.0} {
        tk colormodel . monochrome
      }
    }
    {-xfmodelcolor} {
      if {$tkVersion >= 3.0} {
        tk colormodel . color
      }
    }
    {-xfloading} {
      set xfLoadInfo 1
    }
    {-xfnoloading} {
      set xfLoadInfo 0
    }
    {default} {
      lappend tmpArgv [lindex $argv $counter]
    }
  }
}
set argv $tmpArgv
set argc [llength $tmpArgv]
unset counter
unset tmpArgv


# procedure to show window .
proc ShowWindow. {args} {# xf ignore me 7

  # Window manager configurations
  global tkVersion
  wm positionfrom . user
  wm sizefrom . ""
  wm maxsize . 1024 768
  wm title . {Umax/Envisions Scanner UIF}
  wm geometry . 466x416


  # build widget .frame0
  frame .frame0 \
    -borderwidth {2} \
    -relief {raised}

  # build widget .frame0.label0
  label .frame0.label0 \
    -text {Scan Type}

  # pack widget .frame0
  pack append .frame0 \
    .frame0.label0 {top frame center}

  # build widget .frame1
  frame .frame1 \
    -borderwidth {2} \
    -relief {raised}

  # build widget .frame1.entry12
  entry .frame1.entry12 \
    -relief {sunken} \
    -textvariable {xres}
  # bindings
  bind .frame1.entry12 <Leave> {Recalc}

  # build widget .frame1.entry13
  entry .frame1.entry13 \
    -relief {sunken} \
    -textvariable {yres}
  # bindings
  bind .frame1.entry13 <Leave> {Recalc}

  # build widget .frame1.label1
  label .frame1.label1 \
    -text {XRes}

  # build widget .frame1.label2
  label .frame1.label2 \
    -text {YRes}

  # pack widget .frame1
  pack append .frame1 \
    .frame1.entry13 {top frame center fillx}

  # place
  place .frame1.label2 -x 4 -y 34 -anchor nw

  # place
  place .frame1.label1 -x 4 -y 5 -anchor nw

  # place
  place .frame1.entry12 -x 49 -y 0 -width 66 -height 30 -anchor nw

  # place
  place .frame1.entry13 -x 49 -y 31 -width 65 -height 28 -anchor nw


  # build widget .frame2
  frame .frame2 \
    -borderwidth {2} \
    -relief {raised}

  # build widget .frame2.label2
  label .frame2.label2 \
    -text {Resolution}

  # pack widget .frame2
  pack append .frame2 \
    .frame2.label2 {top frame center}

  # build widget .frame3
  frame .frame3 \
    -borderwidth {2}

  # build widget .frame3.radiobutton10
  radiobutton .frame3.radiobutton10 \
    -command {set ADF 0} \
    -text {Normal          } \
    -value {radiobutton10} \
    -variable {selectedButton2}

  # build widget .frame3.radiobutton11
  radiobutton .frame3.radiobutton11 \
    -command {set ADF 1} \
    -text {ADF              } \
    -value {radiobutton11} \
    -variable {selectedButton2}

  # pack widget .frame3
  pack append .frame3 \
    .frame3.radiobutton10 {top frame center fillx} \
    .frame3.radiobutton11 {top frame center fillx}

  # build widget .frame4
  frame .frame4 \
    -borderwidth {2} \
    -relief {raised}

  # build widget .frame4.label1
  label .frame4.label1 \
    -text {Scan Features}

  # pack widget .frame4
  pack append .frame4 \
    .frame4.label1 {top frame center}

  # build widget .frame5
  frame .frame5 \
    -borderwidth {2}

  # build widget .frame5.radiobutton7
  radiobutton .frame5.radiobutton7 \
    -command {set scan_type "-r"
set pix_size 24
Recalc} \
    -text {Color   (24-bit)} \
    -value {radiobutton7} \
    -variable {selectedButton1}

  # build widget .frame5.radiobutton8
  radiobutton .frame5.radiobutton8 \
    -command {set scan_type "-g"
set pix_size 8
Recalc} \
    -text {Gray    (8-bit)} \
    -value {radiobutton8} \
    -variable {selectedButton1}

  # build widget .frame5.radiobutton9
  radiobutton .frame5.radiobutton9 \
    -command {set scan_type "-l"
set pix_size 1
Recalc} \
    -text {Line Art (1-bit)} \
    -value {radiobutton9} \
    -variable {selectedButton1}

  # pack widget .frame5
  pack append .frame5 \
    .frame5.radiobutton7 {top frame center fillx} \
    .frame5.radiobutton8 {top frame center fillx} \
    .frame5.radiobutton9 {top frame center fillx}

  # build widget .frame6
  frame .frame6 \
    -borderwidth {2} \
    -relief {raised}

  # build widget .frame7
  frame .frame7 \
    -borderwidth {2}

  # build widget .frame7.radiobutton0
  radiobutton .frame7.radiobutton0 \
    -command {set output "> ._tmpfile"
set xv 1
set auto 0} \
    -text {XV               } \
    -value {radiobutton0} \
    -variable {selectedButton3}

  # build widget .frame7.radiobutton1
  radiobutton .frame7.radiobutton1 \
    -command {set xv 0
set auto 0
set output " > $filename"} \
    -text {File              } \
    -value {radiobutton1} \
    -variable {selectedButton3}

  # build widget .frame7.entry2
  entry .frame7.entry2 \
    -relief {sunken} \
    -textvariable {filename}

  # build widget .frame7.radiobutton3
  radiobutton .frame7.radiobutton3 \
    -command {set xv 0
set autofilenum 0
set auto 1
} \
    -text {Auto File      } \
    -value {radiobutton3} \
    -variable {selectedButton3}

  # build widget .frame7.entry4
  entry .frame7.entry4 \
    -relief {sunken} \
    -textvariable {autofilename}

  # build widget .frame7.entry0
  entry .frame7.entry0 \
    -relief {sunken} \
    -textvariable {autofilenum}

  # build widget .frame7.label1
  label .frame7.label1 \
    -text {Name}

  # build widget .frame7.label2
  label .frame7.label2 \
    -text {Name}

  # build widget .frame7.label3
  label .frame7.label3 \
    -text {Num}

  # place
  place .frame7.label3 -x -2 -y 112 -anchor nw

  # place
  place .frame7.label2 -x -2 -y 89 -anchor nw

  # place
  place .frame7.label1 -x -2 -y 43 -anchor nw

  # place
  place .frame7.entry0 -x 39 -y 113 -width 84 -height 23 -anchor nw

  # place
  place .frame7.entry4 -x 39 -y 89 -width 83 -height 23 -anchor nw

  # place
  place .frame7.radiobutton3 -x -3 -y 66 -width 125 -anchor nw

  # place
  place .frame7.entry2 -x 39 -y 44 -width 83 -anchor nw

  # place
  place .frame7.radiobutton1 -x -3 -y 21 -width 125 -anchor nw

  # place
  place .frame7.radiobutton0 -x -3 -y -2 -width 125 -anchor nw


  # build widget .frame8
  frame .frame8 \
    -borderwidth {2} \
    -relief {raised}

  # build widget .frame8.canvas0
  canvas .frame8.canvas0 \
    -height {164} \
    -insertofftime {600} \
    -relief {raised} \
    -width {204}
  # bindings
  bind .frame8.canvas0 <B1-ButtonRelease> {RubberStop %W %x %y}
  bind .frame8.canvas0 <B1-Motion> {RubberMove %W %x %y}
  bind .frame8.canvas0 <B3-Motion> {BoxMove %W %x %y}
  bind .frame8.canvas0 <Button-1> {RubberStart %W %x %y}

  # build widget .frame8.label2
  label .frame8.label2 \
    -text {Size: 0.0 K} \
    -textvariable {SizeLab}

  # build widget .frame8.button0
  button .frame8.button0 \
    -command {BoxAll} \
    -text {Full Page}

  # place
  place .frame8.canvas0 -x 0 -y 0 -width 204 -height 264 -anchor nw

  # place
  place .frame8.label2 -x 2 -y 269 -width 125 -anchor nw

  # place
  place .frame8.button0 -x 120 -y 264 -anchor nw


  # build widget .frame9
  frame .frame9 \
    -borderwidth {2} \
    -relief {raised}

  # build widget .frame9.button4
  button .frame9.button4 \
    -command {if { $auto == 1 } {
  # set output " > $autofilename$autofilenum"
  set output [ format " > $autofilename" $autofilenum ]
  incr autofilenum
}

set command [ concat $scan_type "-x" $xres " -y" $yres " -X" $minX " -Y" $minY " -w" $Hx " -h" $Hy " -t" $Threshold "-b" $Brightness ]

eval exec umax $command $output

if { $xv == 1 } {
    eval exec "xv ._tmpfile &"
}} \
    -text { SCAN}

  # build widget .frame9.button5
  button .frame9.button5 \
    -command {set command [ concat "-g -p -x 24 -y 24 -X 0 -Y 0 -w 204 -h 264 -t " $Threshold "-b" $Brightness ]

eval exec umax $command > ._preview.pgm
exec pgmtopbm < ._preview.pgm > ._preview.pbm
exec pbmtoxbm < ._preview.pbm > ._preview.xbm
Addpic} \
    -text {PREVIEW}

  # build widget .frame9.button6
  button .frame9.button6 \
    -command {destroy .} \
    -text {QUIT}

  # place
  place .frame9.button6 -x 368 -y 14 -anchor nw

  # place
  place .frame9.button4 -x 37 -y 12 -anchor nw

  # place
  place .frame9.button5 -x 194 -y 14 -anchor nw


  # build widget .frame10
  frame .frame10 \
    -borderwidth {2} \
    -relief {raised}

  # build widget .frame10.label3
  label .frame10.label3 \
    -text {Output}

  # pack widget .frame10
  pack append .frame10 \
    .frame10.label3 {top frame center}

  # build widget .frame11
  frame .frame11 \
    -borderwidth {2} \
    -relief {raised}
    # -geometry {30x30} \
	# -width 30 \
	# -height 30 \

  # build widget .frame11.label19
  label .frame11.label19 \
    -text {Threshold}

  # place
  place .frame11.label19 -x 31 -y -1 -height 16 -anchor nw


  # build widget .scale13
  scale .scale13 \
    -borderwidth {0} \
    -command {BrightnessCmd} \
    -orient {horizontal} \
    -relief {raised} \
    -to {255}
  .scale13 set 128

  # build widget .scale14
  scale .scale14 \
    -borderwidth {0} \
    -command {ThresholdCmd} \
    -orient {horizontal} \
    -relief {raised} \
    -to {255}
  .scale14 set 128

  # build widget .frame18
  frame .frame18 \
    -borderwidth {2} \
    -relief {raised}
    # -geometry {30x30} \
	# -width 30 \
	# -height 30 \

  # build widget .frame18.label20
  label .frame18.label20 \
    -text {Brightness}

  # place
  place .frame18.label20 -x 27 -y 2 -height 13 -anchor nw


  # build widget .frame17
  frame .frame17 \
    -borderwidth {2} \
    -relief {raised}
    # -geometry {30x30} \
	# -width 30 \
	# -height 30 \

  # build widget .frame17.entry16
  entry .frame17.entry16
  # bindings
  bind .frame17.entry16 <Leave> {ReBox}

  # build widget .frame17.label21
  label .frame17.label21 \
    -text {Start X,Y}

  # build widget .frame17.label22
  label .frame17.label22 \
    -text {Hght X,Y}

  # build widget .frame17.entry23
  entry .frame17.entry23 \
    -relief {sunken} \
    -textvariable {Sx}
  # bindings
  bind .frame17.entry23 <Leave> {ReBox}

  # build widget .frame17.entry24
  entry .frame17.entry24 \
    -relief {sunken} \
    -textvariable {Sy}

  # build widget .frame17.entry26
  entry .frame17.entry26 \
    -relief {sunken} \
    -textvariable {Hx}

  # build widget .frame17.entry27
  entry .frame17.entry27 \
    -relief {sunken} \
    -textvariable {Hy}

  # place
  place .frame17.entry27 -x 119 -y 24 -width 53 -anchor nw

  # place
  place .frame17.entry26 -x 64 -y 25 -width 50 -anchor nw

  # place
  place .frame17.entry24 -x 119 -y 3 -width 52 -anchor nw

  # place
  place .frame17.entry23 -x 64 -y 3 -width 51 -anchor nw

  # place
  place .frame17.label22 -x 6 -y 25 -anchor nw

  # place
  place .frame17.label21 -x 4 -y 1 -anchor nw

  # place
  place .frame17.entry16 -x 341 -y 333 -anchor nw


  # place
  place .frame10 -x 337 -y 1 -width 128 -height 20 -anchor nw

  # place
  place .frame9 -x 1 -y 361 -width 463 -height 56 -anchor nw

  # place
  place .frame8 -x 129 -y 0 -width 208 -height 330 -anchor nw

  # place
  place .frame7 -x 340 -y 23 -width 127 -height 194 -anchor nw

  # place
  place .frame6 -x 337 -y 221 -width 129 -height 139 -anchor nw

  # place
  place .frame5 -x 1 -y 20 -width 128 -height 76 -anchor nw

  # place
  place .frame4 -x 1 -y 96 -width 128 -height 20 -anchor nw

  # place
  place .frame3 -x 1 -y 116 -width 128 -height 55 -anchor nw

  # place
  place .frame2 -x 1 -y 171 -width 128 -height 20 -anchor nw

  # place
  place .frame1 -x 1 -y 189 -width 128 -height 63 -anchor nw

  # place
  place .frame0 -x 1 -y 1 -width 128 -height 20 -anchor nw

  # place
  place .frame11 -x 1 -y 253 -width 128 -height 20 -anchor nw

  # place
  place .scale13 -x 1 -y 327 -width 128 -height 34 -anchor nw

  # place
  place .scale14 -x 1 -y 273 -width 128 -height 34 -anchor nw

  # place
  place .frame18 -x 1 -y 308 -width 128 -height 20 -anchor nw

  # place
  place .frame17 -x 129 -y 305 -width 208 -height 54 -anchor nw


  # build canvas items .frame8.canvas0
  .frame17.entry16 insert end {}



#  if {"[info procs XFEdit]" != ""} {
#    catch "XFMiscBindWidgetTree ."
#    after 2 "catch {XFEditSetShowWindows}"
#  }
}


# User defined procedures


# Procedure: Addpic
proc Addpic {} {
    .frame8.canvas0 create bitmap 102 132 -bitmap @._preview.xbm
}


# Procedure: BoxAll
proc BoxAll {} {
    global box boxInit Rx Ry
    if {$boxInit == 1} {
        catch {.frame8.canvas0 delete $box(last)}
    }
    if {$boxInit == 0} {
        set boxInit 1
    }
	set Rx 0
	set Ry 0
    set box(anchor) [list 0 0]
    set box(last) [eval {.frame8.canvas0 create rect} $box(anchor) {204 264 -tag box}]
    RubberStop .frame8.canvas0 204 264
}


# Procedure: BoxMove
proc BoxMove { W x y} {
    global box Rx Ry Ey Ex minX minY

    $W move $box(last) [expr $x-$Rx] [expr $y-$Ry]
    set Ey [expr $Ey + ($y-$Ry)]
    set Ex [expr $Ex + ($x-$Rx)]
	set Rx $x
	set Ry $y
    set minX $Ex
    if {$Rx<$Ex} {
      set minX $Rx
    }
    set minY $Ey
    if {$Ry<$Ey} {
      set minY $Ry
    }

    Recalc
}


# Procedure: BrightnessCmd
proc BrightnessCmd { l} {
    global Brightness

    set Brightness $l
}


# Procedure: ThresholdCmd
proc ThresholdCmd { l} {
    global Threshold

    set Threshold $l
}


# Procedure: ReBox
proc ReBox {} {
# someday actually let people edit these.  For now just redraw the numbers
# after they exit
    Recalc
}


# Procedure: Recalc
proc Recalc {} {
    global Rx Ry Ex Ey xres yres Sx Sy Hx Hy SizeLab pix_size minX minY

    set Sx [expr round($minX * $xres * 8.5 / 204) ]
    set Sy [expr $minY * $yres * 11 / 264 ]
	set Hx [expr round(abs( $Ex - $Rx ) * 8.5 * $xres / 204) ]
	set Hy [expr ( abs($Ey - $Ry) ) * 11 * $yres / 264 ]
	#puts stdout "Rx $Rx, Ry $Ry, xres $xres, yres $yres, Sx $Sx, Sy $Sy, Hx $Hx, Hy $Hy"
    set SizeLab [ concat "Size: " [expr abs(($Ex-$Rx)*($Ey-$Ry)*8.5/204*11*$xres/264*$yres/1000*$pix_size/8)] "K" ]
}


# Procedure: RubberMove
proc RubberMove { W x y} {
    global box
    catch {$W delete $box(last)}
    set box(last) [eval {$W create rect} $box(anchor) {$x $y -tag box}]
}


# Procedure: RubberStart
proc RubberStart { W x y} {
    global box boxInit Rx Ry
    catch {$W delete $box(last)}
	set Rx $x
	set Ry $y
    if {$boxInit == 1} {
        catch {$W delete $box(last)}
    }
    if {$boxInit == 0} {
        set boxInit 1
    }
    set box(anchor) [list $x $y]
    catch {unset box(last)}
}


# Procedure: RubberStop
proc RubberStop { W x y} {
    global Rx Ry Ex Ey minX minY actX actY actEX actEY

    set minX $x
    if {$Rx<$x} {
      set minX $Rx
    }
    set minY $y
    if {$Ry<$y} {
      set minY $Ry
    }
    set Ex $x
	set Ey $y
    set actX $Rx
    set actY $Ry
    set actEX $Ex
    set actEY $Ey
    Recalc
}

# initialize global variables
proc InitGlobals {} {
  global {0}
  set {0} {0+1}
  global {ADF}
  set {ADF} {0}
  global {Brightness}
  set {Brightness} {128}
  global {Threshold}
  set {Threshold} {128}
  global {Ex}
  set {Ex} {0}
  global {Ey}
  set {Ey} {0}
  global {Hx}
  set {Hx} {0}
  global {Hy}
  set {Hy} {0}
  global {Rect}
  set {Rect} {0}
  global {Rx}
  set {Rx} {0}
  global {Ry}
  set {Ry} {0}
  global {SizeLab}
  set {SizeLab} {Size: 0.0 K}
  global {Sx}
  set {Sx} {0}
  global {Sy}
  set {Sy} {0}
  global {actEX}
  set {actEX} {0}
  global {actEY}
  set {actEY} {0}
  global {actX}
  set {actX} {0}
  global {actY}
  set {actY} {0}
  global {auto}
  set {auto} {1}
  global {autofilename}
  set {autofilename} {outfile%d}
  global {autofilenum}
  set {autofilenum} {0}
  global {box}
  set {box(anchor)} {16 23}
  set {box(last)} {45}
  global {boxInit}
  set {boxInit} {1}
  global {command}
  set {command} {-r -x 100 -y 100 -X 23 -Y 244 -w 0 -h 0 > outfile51}
  global {filename}
  set {filename} {outfile}
  global {minX}
  set {minX} {23}
  global {minY}
  set {minY} {244}
  global {output}
  set {output} { > outfile51}
  global {pix_size}
  set {pix_size} {24}
  global {scan_type}
  set {scan_type} {-r}
  global {selectedButton1}
  set {selectedButton1} {radiobutton7}
  global {selectedButton2}
  set {selectedButton2} {radiobutton10}
  global {selectedButton3}
  set {selectedButton3} {radiobutton3}
  global {xres}
  set {xres} {100}
  global {xv}
  set {xv} {0}
  global {yres}
  set {yres} {100}

  # please don't modify the following
  # variables. They are needed by xf.
  global {autoLoadList}
  set {autoLoadList(main.tcl)} {0}
  set {autoLoadList(test5)} {0}
  global {internalAliasList}
  set {internalAliasList} {}
  global {moduleList}
  set {moduleList(test5)} {}
  global {preloadList}
  set {preloadList(xfInternal)} {}
  global {symbolicName}
  set {symbolicName(root)} {.}
  global {xfWmSetPosition}
  set {xfWmSetPosition} {}
  global {xfWmSetSize}
  set {xfWmSetSize} {}
  global {xfAppDefToplevels}
  set {xfAppDefToplevels} {}
}

# initialize global variables
InitGlobals

# display/remove toplevel windows.
ShowWindow.

# load default bindings.
if {[info exists env(XF_BIND_FILE)] &&
    "[info procs XFShowHelp]" == ""} {
  source $env(XF_BIND_FILE)
}
