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

#----------------------------------------------------------------------
### CONFIG

set cligd "./Clig"
set env(CLIG_DIR) "./Clig"
set stylesheet [glob "./stylesheets/dt2clig1.xsl"]

### CLIG sourcen (moet boven de her-definitie van 'clig')
source "$cligd/main.tcl"

#----------------------------------------------------------------------
### PROCS

proc start-clig-canvas {} {
        global background_color main_background_color clig_globals
        . configure -bg $main_background_color
        wm minsize . 640 480 ;# top-window is resizable

        # de defaults van Hdrug
        set clig_globals(tree_hspace)  5 ;# horizontal space in trees
        set clig_globals(tree_vspace) 20 ;# vertical space in trees

        scrollbar .ygraph -command ".graphics yview" -width 10
        scrollbar .xgraph -command ".graphics xview" -width 10 \
                -orient horizontal
        
        define-gr-canvas ;# defining the graphics canvas
#         pack .tm -fill x
#        pack .tm.file .tm.view -side left -fill x
#        pack .tm.view -side left -fill x
#        pack .tm.forward .tm.back .tm.help -side right
#        pack .leftframe -side top -anchor sw

	pack .ygraph -side right -fill y
        pack .xgraph -fill x -side bottom
        pack .graphics -padx 3 -pady 1

        zoom-initialize
}

# we definieren 'm opnieuw zodat we de obj-stack van clig niet gebruiken
proc clig {obj} {
    show $obj
}

# xsltproc "-stringparam" "expr" "'//node\[@rel = \"cnj\"]'"
#          "showmatch3.xsl" "/mnt/devel/Alpino/Treebank/cdb/33.xml 

proc apply-expr {expr item} {
    global stylesheet filelist
    set arglist "xsltproc"
    if {[expr [string length $expr] > 0]} {
        lappend arglist "-stringparam" "expr" $expr
    }
    lappend arglist $stylesheet [lindex $filelist $item]

    global argv0
    # dtview <filenaam> (number of total)
    wm title . "[file tail $argv0] [lindex $filelist $item] \
          ([expr $item + 1] of [llength $filelist])"

    eval [eval exec  $arglist]
}

proc next-file {} {
    global current filelist expr prevbut nextbut
    if {[expr $current < ([llength $filelist] - 1) ]} {
        incr current
        # next-file button inactive markeren
	$prevbut configure -state normal
	if {[expr $current == ([llength $filelist] - 1)]} {
	    $nextbut configure -state disabled
	}

        setquery
        apply-expr $expr $current
    }
}

proc previous-file {} {
    global current filelist expr prevbut nextbut
    if {[expr $current > 0 ]} {
        set current [expr $current - 1]
        # next-file button inactive markeren
	if {[expr $current == 0 ]} {
	    $prevbut configure -state disabled
	}
	$nextbut configure -state normal

        setquery
        apply-expr $expr $current
    }
}

proc Pop {varname {nth 0}} {
     upvar $varname argv
     set r [lindex $argv $nth]
     set argv [lreplace $argv $nth $nth]
     return $r
}

proc readfilelistfromstdin {} {
    global filelist
    while {[gets stdin line] != -1} {
        lappend filelist $line
    }
}

# filenaam <TAB> query; Een andere lage ascii waarde i.p.v. TAB is met
# XSLT (helaas) niet mogelijk.  Zie XML standaard v1.0 paragraaf 2.2.
# (http://www.w3.org/TR/2000/REC-xml-20001006#charsets)
proc readfilequerypairsfromstdin {} {
    global filelist querylist descriptionlist
    while {[gets stdin line] != -1} {
        set pair [split $line "\t"]
        lappend filelist [lindex  $pair 0]
        lappend querylist [lindex  $pair 1]

        # We pakken ook gelijk een "description" veld
        lappend descriptionlist [lindex  $pair 2]
    }
}

# update het query-veld en gelijk ook maar het comment-veld
proc setquery {} {
    global expr current querylist opts
    
    if {$opts(-file-query-pairs)} {
        set expr [lindex $querylist $current]
        setdescription
    }
}


proc setdescription {} {
    global descriptionframe topframe descrlabel descrentry \
        description opts descriptionlist current butframe

    # maak en vul frame indien nodig 
    if {[expr ! [info exists descriptionframe]]} {
        set descriptionframe [frame $topframe.descrframe]
        
        set descrlabel [label $descriptionframe.descrlabel \
                            -text "Query description: "]
        
        set descrentry [entry $descriptionframe.descrlentry \
                            -textvariable description]

        pack $descrlabel -side left -padx 2
        pack $descrentry -side left -padx 2 -fill x -expand yes

    }


    # haal description op en maak frame actief indien nodig
    if {$opts(-file-query-pairs)} {
        set description [lindex $descriptionlist $current]
        
        if {[string length $description]} {
            pack $descriptionframe -after $butframe  -fill x -pady 2 -expand yes
        } else {
            pack forget $descriptionframe
        }
    }

}

#----------------------------------------------------------------------
### ENTRY POINT

set current 0
set filelist {}
set querylist {}
set descriptionlist {}

array set opts {-q "" -s 0 -file-query-pairs 0}

while {[string match -* [lindex $argv 0]]} {
    switch -glob -- [lindex $argv 0] {
        -q   { set opts(-q) [Pop argv 1] }
        -s   { set opts(-s) 1;}
        -file-query-pairs   { set opts(-file-query-pairs) 1;}
        --   { Pop argv ; break }
        default {
            set o [join [lsort [array names opts -*]] ", "]
            return -code error "bad option [lindex $argv 0]: \
                     must be one of $o"
        }
    }
    Pop argv
}

set expr $opts(-q)

if {$opts(-s)} {
    readfilelistfromstdin
} elseif {$opts(-file-query-pairs)} {
    readfilequerypairsfromstdin
} else {
    set filelist $argv
}

if [expr ! [llength $filelist] > 0] {
    puts stderr "[file tail $argv0]: Error: No files to process

Usage: dtview \[-q query] <files>
"
    exit 1
}


#----------------------------------------------------------------------
# GUI opbouwen

# Een entry bovenin maken

set topframe [frame .topFrame]
set exprframe [frame $topframe.exprFrame]
set exprlabel [label $exprframe.l1 -text "Highlight: "]
set exprentry [entry $exprframe.expr -width 20 -font [list Courier 10] -textvariable expr]

set butframe [frame $topframe.butFrame]

set applybut [button $butframe.apply -text "Apply" -command {apply-expr $expr $current}]
set prevbut  [button $butframe.prev -text "Previous" -command previous-file -state disabled]
set nextbut  [button $butframe.next -text "Next"     -command next-file]


set smallbut [button $butframe.smaller -text "Smaller" \
        -command {if ($graph_scale>2) {
                        unscale $graph_scale .graphics
                        set graph_scale [expr $graph_scale-2]
                        set scaling [scale-graph $graph_scale .graphics]
                    fit-to-scale $scaling .graphics}} ]

set bigbut [button $butframe.bigger -text "Bigger" \
        -command {if ($graph_scale<32) {
                        unscale $graph_scale .graphics
                        set graph_scale [expr $graph_scale+2]
                        set scaling [scale-graph $graph_scale .graphics]
                    fit-to-scale $scaling .graphics}} ]

set normbut [button $butframe.normal -text "Normal" \
        -command {set graph_scale [expr $clig_globals(fontsize)]
                    show $main_object}]

set thistlebut [button $butframe.thistle -text "Thistle" \
    -command { puts "executing thistle [file rootname [lindex $filelist $current]] "
	exec thistle [file rootname [lindex $filelist $current]] &}]

set quitbut [button $butframe.quit -text "Quit" \
                 -command exit]

# expr frame klaar maken
pack $exprlabel -side left -padx 2
pack $exprentry -side left -padx 2 -fill x -expand yes

# button frame klaar maken
pack $applybut   -side left -padx 2
pack $prevbut    -side left -padx 2
pack $nextbut    -side left -padx 2
pack $smallbut   -side left -padx 2
pack $bigbut     -side left -padx 2
pack $normbut    -side left -padx 2
pack $thistlebut -side left -padx 2
pack $quitbut    -side left -padx 2


pack $exprframe -side top -fill x -pady 2 -expand yes
pack $butframe  -fill x -pady 2
pack $topframe  -side top -fill x -pady 2

# ## TEST ##
# frame .sentenceframe
# text .sentenceframe.sentence -height 2
# .sentenceframe.sentence insert 0.0 "Dit is de zin"
# pack .sentenceframe.sentence -fill x -expand yes
# pack .sentenceframe -fill x


bind $exprentry <Return> {apply-expr $expr $current}

if {[expr [llength $filelist]==1]} {
    $nextbut configure -state disabled
}

# clig canvas starten
start-clig-canvas

#----------------------------------------------------------------------

# puts "number of files: [llength $filelist]"

# starten met de eerste file
setquery
apply-expr $expr $current



# EOF

# Local Variables:
# mode: tcl
# End:
