#!/bin/sh
# the next line restarts using wish \
exec /usr/bin/wish "$0" "$@"

#!/usr/bin/bltwish

### VITESS simulation GUI start script

# to use BLT uncomment next line
#set NoBLT [catch {package require BLT}]
set NoBLT 1

proc VScroll {w {u 5}} {
  # scroll module windows
  if {[string range $w 0 4] != ".x.m."} return
  switch [string range $w 5 6] {
    "r." { .x.m.r.modf.c yview scroll $u units }
    "l." { .x.m.l.c yview scroll $u units }
  }
}

proc wheelEvent { x y delta } {
  
  # Find out what's the widget we're on
  set w [winfo containing $x $y]
  if {$w == ""} return

  set scom ""
  switch [string range $w 0 6] {
    ".x.m.r." { set scom .x.m.r.modf.c }
    ".x.m.l." { set scom .x.m.l.c }
  }
  if {$scom != ""} {
  	# scroll module windows
	set u [expr $delta < 0 ? 5 : -5]
    catch { $scom yview scroll $u units }
    return
  }

  # Make sure we've got a vertical scrollbar for this widget
  if [catch "$w cget -yscrollcommand" cmd] return
  if {$cmd == ""} return 
  
  # Find out the scrollbar widget we're using
  set scroller [lindex $cmd 0]

  # Now we know we have to process the wheel mouse event
  set xy [$w yview]
  set factor [expr [lindex $xy 1]-[lindex $xy 0]]
  set u [expr int(-$delta/(120*$factor))]
  
  # Make sure we activate the scrollbar's command
  set cmd "[$scroller cget -command] scroll $u units"
  eval $cmd
}

switch $tcl_platform(platform) {
  unix {
    switch $tcl_platform(os) {
      OSF1 - SunOS - Linux {
        # Browser for HTML help display:
        # Select whatever is installed from mozilla, netscape, ...
        # It should be callable as
        # mozilla -remote openURL(file:vitess.html)
        # otherwise change help.tcl accordingly
        set Browser ""
        foreach b {firefox mozilla netscape communicator konqueror} {
          catch {
            catch {exec sh -c "which $b"} bb
            if {$bb != "" && [file exists $bb]} {
              set Browser $bb
            }
          }
          if {$Browser != ""} break
        }
        catch {unset b bb} 
      }
      Darwin {
        set Browser open
      }
      default {
        puts "System not supported.\n"
        exit
      }
    }
    if {$Browser == ""} {
      puts "no browser found!"
    }
    set serif Courier
    set sserif Helvetica
    
    regsub ^./ [glob [info script]] "" s
    set i [string last / $s]
    incr i -1
    if {$i >= 0} {
      set s [string range $s 0 $i]
      if {[file pathtype $s] == "relative"} {
	set SourceDirectory [file join [pwd] $s]
      } else {
	set SourceDirectory $s
      }
    } else {
      set SourceDirectory [pwd]
    }
    set ExeDirectory [file join $SourceDirectory MODULES]
    set ExeSuffix _[set s $tcl_platform(os)]
    set m $tcl_platform(machine)
    if {($s == "Linux" || $s == "Darwin") && $m == "x86_64"} {append ExeSuffix _$m}
    set KillProg kill
    set PsCheck PsCheckUnix
    set WinPos +50+0
    set TextPos +620+0
    set MaxOutstringLength 90
    bind all <Button-4>  {VScroll %W -5}
    bind all <Button-5>  {VScroll %W 5}
    set scrollWidth 8
  }
  windows {
    set SourceDirectory [pwd]
    set ExeDirectory [file join $SourceDirectory MODULES]
    set ExeSuffix ".exe"
    set HelpApp [file join $ExeDirectory helpa.exe]
    set KillProg $HelpApp
    set PsCheck PsCheckWindows
    set WinPos +0+0
    set TextPos +620+0
    set MaxOutstringLength 80
    set Browser [file join $ExeDirectory shelexec.exe]
    set s "Lucida Console"
    if {[lsearch [font families] $s] >= 0} {
      set serif $s
    } else {
      set serif Courier
    }
    set sserif Arial
    set scrollWidth 16
  }
  default {
    puts "System not supported.\n"
    exit
  }
}
catch {unset ld place resname s i m}
set monospaced $serif
set GUIDir [file join $SourceDirectory GUI]
set auto_path [linsert $auto_path 0 $GUIDir]
###    use tk_chooseColor to find better colors
set c #dce2ff
set bgColor $c
set labColor $c
set entryColor #aedace
set radioColor $c
set buttonColor #98b4a0
set menuButtonColor $c
set canvasColor $c
if {$tcl_platform(os) == "Darwin"} {set menuColor black} else {set menuColor $c}

set itemlabwidth 16
set fileentrywidth 48
set VisState 0
# VisGather is either
# - an external program path 
# - or just-concatenate
# to gather 1. step visualisation results
#set VisGather [file join $ExeDirectory sortiap$ExeSuffix]
#if {! [file exists $VisGather]} {set VisGather "just-concatenate"}
set VisGather "just-concatenate"

# VisMerge is an external program to merge 2. step visualisation results
set VisMerge [file join $ExeDirectory sortiap$ExeSuffix]
if [file exists $VisMerge] {
  if {$tcl_platform(platform) == "unix"} {
    set optf [file join $env(HOME) .x3dopt]
  } else {
    set optf [file join $ExeDirectory x3d.opt]
  }
  if [file exists $optf] {
    append VisMerge " -f $optf"
  }
  unset optf
} else {
  set VisMerge ""
}

set MainBitmap [file join $SourceDirectory BITMAPS vit.gif]

### execute all tcl source files
set modl {tools check entries help ldstore menu windows 
  show vitess comexe digest control}
if $NoBLT {
  lappend modl show_plain
} else {
  lappend modl show_blt
}
if [catch {package require md5}] {
  lappend modl md5x
}
foreach f $modl {
  source [file join $GUIDir $f.tcl]
}
unset modl

set ProgressFile [tmpFilename "pgress.txt"]


bind all <MouseWheel> "+wheelEvent %X %Y %D"


### start main widget of Xcontrol, reading experiment environment
controlGUI $SourceDirectory vitess.tcl .x $WinPos

### show message window and update, to have these placed nicely
showText ""
switch $tcl_platform(platform) {
  windows {
    wm iconify .x
    wm deiconify .x
  }
  default update
}
### VITESS parameter directory entry must have a reasonable value
forceParamDir
