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

# widget --
# This script demonstrates the various widgets provided by Tk,
# along with many of the features of the Tk toolkit.  This file
# only contains code to generate the main window for the
# application, which invokes individual demonstrations.  The
# code for the actual demonstrations is contained in separate
# ".tcl" files is this directory, which are sourced by this script
# as needed.
#
# SCCS: @(#) widget 1.35 97/07/19 15:42:22

eval destroy [winfo child .]
wm title . "XMuniPack"

#----------------------------------------------------------------
# The code below create the main window, consisting of a menu bar
# and a text widget that explains how to use the program, plus lists
# all of the demos as hypertext items.
#----------------------------------------------------------------

set font {Helvetica 14}
menu .menuBar -tearoff 0
.menuBar add cascade -menu .menuBar.file -label "Options" -underline 0
menu .menuBar.file -tearoff 0

.menuBar.file add command -label "About..." -command "aboutBox" \
	-underline 0 -accelerator "<F1>"
.menuBar.file add sep

.menuBar.file add command -label "Quit" -command "exit" -underline 0 \
    -accelerator "Alt-Q"
. configure -menu .menuBar
bind . <F1> aboutBox
bind . <Alt-q> exit
bind . <Alt-Q> +exit

frame .statusBar
label .statusBar.lab -text "   " -relief sunken -bd 1 \
    -font -*-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-* -anchor w
label .statusBar.foo -width 8 -relief sunken -bd 1 \
    -font -*-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-* -anchor w
pack .statusBar.lab -side left -padx 2 -expand yes -fill both
pack .statusBar.foo -side left -padx 2
pack .statusBar -side bottom -fill x -pady 2

frame .textFrame
scrollbar .s -orient vertical -command {.t yview} -highlightthickness 0 \
    -takefocus 1
pack .s -in .textFrame -side right -fill y
text .t -yscrollcommand {.s set} -wrap word -width 60 -height 20 -font $font \
    -setgrid 1 -highlightthickness 0 -padx 4 -pady 2 -takefocus 0
pack .t -in .textFrame -expand y -fill both -padx 1
pack  .textFrame -expand yes -fill both

# Create a bunch of tags to use in the text widget, such as those for
# section titles and demo descriptions.  Also define the bindings for
# tags.

.t tag configure title -font {Helvetica 18 bold}

# We put some "space" characters to the left and right of each demo description
# so that the descriptions are highlighted only when the mouse cursor
# is right over them (but not when the cursor is to their left or right)
#
.t tag configure format -lmargin1 1c -lmargin2 1c

if {[winfo depth .] == 1} {
    .t tag configure crewin -lmargin1 1c -lmargin2 1c \
	-underline 1
    .t tag configure visited -lmargin1 1c -lmargin2 1c \
	-underline 1
    .t tag configure hot -background black -foreground white
} else {
    .t tag configure crewin -lmargin1 1c -lmargin2 1c \
	-foreground blue -underline 1
    .t tag configure visited -lmargin1 1c -lmargin2 1c \
	-underline 1
#	-foreground #303080 -underline 1
    .t tag configure hot -foreground red -underline 1
}
.t tag bind crewin <ButtonRelease-1> {
    invoke [.t index {@%x,%y}]
}
set lastLine ""
.t tag bind crewin <Enter> {
    set lastLine [.t index {@%x,%y linestart}]
    .t tag add hot "$lastLine +1 chars" "$lastLine lineend -1 chars"
    .t config -cursor hand2
    showStatus [.t index {@%x,%y}]
}
.t tag bind crewin <Leave> {
    .t tag remove hot 1.0 end
    .t config -cursor xterm
    .statusBar.lab config -text ""
}
.t tag bind crewin <Motion> {
    set newLine [.t index {@%x,%y linestart}]
    if {[string compare $newLine $lastLine] != 0} {
	.t tag remove hot 1.0 end
	set lastLine $newLine

	set tags [.t tag names {@%x,%y}]
	set i [lsearch -glob $tags crewin-*]
	if {$i >= 0} {
	    .t tag add hot "$lastLine +1 chars" "$lastLine lineend -1 chars"
	}
    }
    showStatus [.t index {@%x,%y}]
}

puts "XMunipack Version 0.0 Copyright (C) 1998 F.Hroch, Masaryk University, Brno, CZ\n"

# Create the text for the text widget.

#set date [exec date]
#.t insert end $date

.t insert end " XMuniPack\n " title
.t insert end {
This application provides a front-end to the MUNIPACK photometry package. 

Follow next entries and read instruction to get photometry of your data set:  
 }
.t insert end " \n " {format}

.t insert end " Mean Dark \n" title
.t insert end " MeanDark " {crewin meandark}

.t insert end " \n\n\n " {format}

.t insert end " Scientific exposures. \n" title 
.t insert end " Select scientific exposures \n" {demo filebox} 
.t insert end " Create by converting scientific exposures \n" {demo konve}
.t insert end " Subtract the dark farmes. \n" {demo darkbat} 

.t insert end " \n " {format}

.t insert end " Auto flat fielding \n" title
.t insert end " Select scientific exposures \n" {demo filebox}
.t insert end " Auto flat field." {demo autoflat}
.t insert end " \n " {format}

.t insert end " \n " {format}

.t insert end " Flat fielding \n" title
.t insert end " Select scientific exposures \n" {demo filebox}
.t insert end " Flat fielding." {demo flatbat}
.t insert end " \n " {format}

.t insert end " \n " {format}

.t insert end \n {} "Photometry" title
.t insert end " \n " {format}
.t insert end "Mean dark." {demo meandark}
.t insert end " \n " {format}
.t insert end " \n " {format}


.t configure -state disabled
focus .s

# positionWindow --
# This procedure is invoked by most of the demos to position a
# new demo window.
#
# Arguments:
# w -		The name of the window to position.

proc positionWindow w {
    wm geometry $w +10+10
}

# showVars --
# Displays the values of one or more variables in a window, and
# updates the display whenever any of the variables changes.
#
# Arguments:
# w -		Name of new window to create for display.
# args -	Any number of names of variables.

proc showVars {w args} {
    catch {destroy $w}
    toplevel $w
    wm title $w "Variable values"
    label $w.title -text "Variable values:" -width 20 -anchor center \
	    -font {Helvetica 18}
    pack $w.title -side top -fill x
    set len 1
    foreach i $args {
	if {[string length $i] > $len} {
	    set len [string length $i]
	}
    }
    foreach i $args {
	frame $w.$i
	label $w.$i.name -text "$i: " -width [expr $len + 2] -anchor w
	label $w.$i.value -textvar $i -anchor w
	pack $w.$i.name -side left
	pack $w.$i.value -side left -expand 1 -fill x
	pack $w.$i -side top -anchor w -fill x
    }
    button $w.ok -text OK -command "destroy $w" -default active
    bind $w <Return> "tkButtonInvoke $w.ok"
    pack $w.ok -side bottom -pady 2
}

# invoke --
# This procedure is called when the user clicks on a demo description.
# It is responsible for invoking the demonstration.
#
# Arguments:
# index -	The index of the character that the user clicked on.

proc invoke index {
    global tk_library
    set tags [.t tag names $index]
    set i [lsearch -glob $tags *]
    if {$i < 0} {
	return
    }
     set cursor [.t cget -cursor]
    .t configure -cursor watch
    update
#    set demo [string range [lindex $tags $i] 5 end]
    set crewin [lindex $tags end]
    uplevel [list source [file join $crewin.tcl]]
    update
    .t configure -cursor $cursor

    .t tag add visited "$index linestart +1 chars" "$index lineend -1 chars"
}

# showStatus --
#
#	Show the name of the demo program in the status bar. This procedure
#	is called when the user moves the cursor over a demo description.
#
proc showStatus index {
    global tk_library
    set tags [.t tag names $index] 
    set i [lsearch -glob $tags *]
    set cursor [.t cget -cursor]
    if {$i < 0} {
	.statusBar.lab config -text " "
	set newcursor xterm
    } else {
#	set demo [string range [lindex $tags $i] 5 end]
        set crewin [lindex $tags end]
	.statusBar.lab config -text "Run the \"$crewin\" program utility."
	set newcursor hand2
    }
    if [string compare $cursor $newcursor] {
	.t config -cursor $newcursor
    }
}

# aboutBox --
#
#	Pops up a message box with an "about" message
#
proc aboutBox {} {
    tk_messageBox -icon info -type ok -title "About XMunipack" -message \
"Munipack photometry package\n\n\
Copyright (c) 1998-9 F.Hroch"
}


