# File init.tcl
#
# Diese Prozeduren dienen der Initialisierung etc.


# Eine von aussen sichtbare Prozedur zum Kreiieren eines neuen Displays

proc init_chart_display { name headline } {
 
    global chart edge token map draw

    my_trace "Entering init_chart_display $name $headline"

    # der Titel des Displays
    set chart($name,headline) $headline

    # das Flag fuer Results
    set chart(result) 0

    # Pruefe, ob ein geoeffnetes Window existiert
    set chart(windowopen) 0
    if { [winfo exists .chart$name] == 1 } {
        set chart(windowopen) 1
    }

    # oeffne neues Fenster
    #    if { [info exists chart($name,max_edges)] != 1 || $chart($name,max_edges) != -1 && $chart(windowopen) == 0 } 
    
    if { $chart(windowopen) == 0 } {
        
	my_trace "Creating brandnew display"
	
        # setze Default-Werte
        set_default_values $name
        
	# Parse das Config-File
        parse_config $name
	
        create_toplevel $name
        
    } else {
        # Loesche nur das alte Canvas und ersetze evtl. neuen Namen
        wm title .chart$name "Chart-Display: $headline"
#        wm title . "Chart-Display: $headline"
	my_trace "Erasing the old canvas"
	# Loesche ein paar Werte
	reset_values $name
	# Loesche das Canvas KOMPLETT !!!
	.chart$name.canvas.draw delete all
    }

    my_trace "Leaving init_chart_display"

}

proc create_toplevel { name } {
    
    global chart loadpath
    
    my_trace "Entering proc create_toplevel $name"

    #catch { destroy .chart$name }
    toplevel .chart$name

    # 981014 NEW: use a specified icon

    if {[info exists chart(iconname)] == 1 } {
        if {[info exists chart(imagename)] == 0 } {
            if {[file exists [set icon "$loadpath/$chart(iconname)"]]} {

            wm iconbitmap .chart$name @$icon;

            }    
            

        }
    }
        wm iconname .chart$name "Ch:Babel"


#    frame .chart$name
    wm title .chart$name "Chart-Display: $chart($name,headline)"
#    pack .chart$name -expand 1 -fill both

#    set chart($name,default_minimum_space) [expr [lindex [broadness_of_string "HEAD-FINAL-P-RULE" $name] 0] + 60]
 
    #981127 setze Tokendistance absolut (gleicher Wert wie vorher)
    set chart($name,default_minimum_space) $chart(default_minimum_space)
   
    # Abstand zum linken Rand fuer die Regeln
    set chart($name,leftspace) [expr [lindex [broadness_of_string "NON-FIN-PASS-VERB-RULE" $name] 0] + 80]
    
    # Abstand zum unteren Rand
#    set chart($name,downspace) 150
    # 981130 Abstand zum unteren Rand definierbar
    set chart($name,downspace) $chart(downspace)


    frame .chart$name.canvas
    
    frame .chart$name.window
    frame .chart$name.window.view -relief sunken -bd 1
    frame .chart$name.window.info1 -relief sunken -bd 1
    frame .chart$name.window.info2 -relief sunken -bd 1
    
    # show the current view
    label .chart$name.window.view.label -text "Current view: "
    
    frame .chart$name.window.view.frame
    
    label .chart$name.window.view.frame.label2 -textvariable chart($name,which_edges)
    label .chart$name.window.view.frame.label3 -textvariable chart($name,which_token)
    label .chart$name.window.view.frame.label4 -textvariable chart($name,which_extension)
    
    pack .chart$name.window.view.frame.label2 .chart$name.window.view.frame.label3 .chart$name.window.view.frame.label4
    
    pack .chart$name.window.view.label -side left
    pack .chart$name.window.view.frame -side right
    
    # show some infos
    label .chart$name.window.info1.label1 -text "Edges:"
    label .chart$name.window.info1.label2 -textvariable chart($name,max_edges)
    
    label .chart$name.window.info2.label3 -text "Edges drawn:"
    label .chart$name.window.info2.label4 -textvariable chart($name,drawn_edges)
    
    pack .chart$name.window.info1.label1 -side left
    pack .chart$name.window.info1.label2 -side right -fill x
    pack .chart$name.window.info2.label3 -side left
    pack .chart$name.window.info2.label4 -side right -fill x
    
    
    text .chart$name.window.text -width 20 -height 20 -setgrid true -wrap none -xscrollcommand [list .chart$name.window.xscroll set] -yscrollcommand [list .chart$name.window.yscroll set]
    scrollbar .chart$name.window.xscroll -orient horizontal -command [list .chart$name.window.text xview]
    scrollbar .chart$name.window.yscroll -orient vertical -command [list .chart$name.window.text yview]
    #    pack .chart$name.window.xscroll -side bottom -fill x
    #    pack .chart$name.window.yscroll -side right -fill y
    
    pack .chart$name.window.view -side top -fill x
    pack .chart$name.window.info1 -side top -fill x
    pack .chart$name.window.info2 -side top -fill x
    #    pack .chart$name.window.text -side bottom -fill both -expand true
    
    canvas .chart$name.canvas.draw -width $chart(defaultwidth) -height $chart(defaultheight) -scrollregion "0 0 $chart(defaultscrollwidth) $chart(defaultscrollheight)" -xscrollcommand [list .chart$name.canvas.xscroll set] -yscrollcommand [list .chart$name.canvas.yscroll set] -bd 2 -relief raised
    
    bind .chart$name.canvas.draw <Button-3> "make_defaultpopup $name;tk_popup .chart$name.defaultpopup %X %Y"
    
    scrollbar .chart$name.canvas.xscroll -orient horizontal -command [list .chart$name.canvas.draw xview] -width 10
    scrollbar .chart$name.canvas.yscroll -orient vertical -command [list .chart$name.canvas.draw yview] -width 10
    
    pack .chart$name.canvas.xscroll -side bottom -fill x
    pack .chart$name.canvas.yscroll -side right -fill y
    
    pack .chart$name.canvas.draw -fill both -expand true -side top
    
    #    pack .chart$name.window -side bottom -expand true
    
    # Setze den View nach unten
    
    .chart$name.canvas.draw yview moveto 1
    
    
    my_trace "Default_maximum_space = $chart($name,default_minimum_space)"
    update
    
    frame .chart$name.buttons
#    button .chart$name.buttons.quit -text "Quit !" -command "quit_parser $name;destroy .chart$name;reset_values $name;set chart($name,max_action) -1" -width 10 -underline 0
    button .chart$name.buttons.quit -text "Quit !" -command "quit_parser $name;destroy .chart$name;reset_values $name;set chart($name,max_action) -1" -width 10 -underline 0

    button .chart$name.buttons.config -text "Configure" -command "chart_config $name .top17" -width 10 -underline 0

    button .chart$name.buttons.print -text "Print" -command "print_chart $name" -width 10 -underline 0 -state disabled

    button .chart$name.buttons.about -text "About..." -command "show_about" -width 10
    
    # Bindings fuer Keyboard Shortcuts
    bind .chart$name <q> "quit_parser $name;destroy .chart$name;reset_values $name;set chart($name,max_action) -1"
    bind .chart$name <c> "chart_config $name .top17"
    bind .chart$name <p> "print_chart $name"
    bind .chart$name <v> "toggle_view $name"
    bind .chart$name <a> "redraw_chart $name allrules"
    bind .chart$name <d> "toggle_drawing $name"
    bind .chart$name <Up> ".chart$name.canvas.draw yview scroll -1 units"
    bind .chart$name <Down> ".chart$name.canvas.draw yview scroll 1 units"
    bind .chart$name <Left> ".chart$name.canvas.draw xview scroll -1 units"
    bind .chart$name <Right> ".chart$name.canvas.draw xview scroll 1 units"
    bind .chart$name <Shift-Up> ".chart$name.canvas.draw yview scroll -1 pages"
    bind .chart$name <Shift-Down> ".chart$name.canvas.draw yview scroll 1 pages"
    bind .chart$name <Shift-Left> ".chart$name.canvas.draw xview scroll -1 pages"
    bind .chart$name <Shift-Right> ".chart$name.canvas.draw xview scroll 1 pages"
    
    pack .chart$name.buttons.quit .chart$name.buttons.config .chart$name.buttons.print -side left
    pack .chart$name.buttons.about -side right
    
    # frame fuer Copyright
    frame .chart$name.copyright
    label .chart$name.copyright.label -text "$chart(chart_display_version)"
    
    pack .chart$name.copyright.label
    
    pack .chart$name.copyright -side top -expand false -fill x
    
    pack .chart$name.buttons -side top -expand false -fill x
    pack .chart$name.canvas -side top -expand true -fill both

    my_trace "Leaving create_toplevel"
}

proc set_default_values { name } {
    
    global chart
    
    my_trace "Entering proc set_default_values $name"
    
    # Anzahl der Knoten am Anfang auf -1
    set chart($name,max_token) -1
    
    # Anzahl der Gesamt-Kanten am Anfang auf -1
    set chart($name,max_edges) -1
    
    # Anzahl der tatsaechlich gezeichneten Kanten, -1
    set chart($name,drawn_edges) -1
    
    # Anzahl der Custom-Aktionen
    set chart($name,max_action) -1
    
    # Anzahl der Regeln (Rules)
    set chart($name,max_rules) -1
    
    # Hoehe der Rules
    set chart($name,ruleheight) -1

    # gefaerbte Kinder Flag
    set chart($name,dyed_children) -1
    
    # set some initial values
    
    # Print-Funktion
    set chart(printheight) 29.7
    set chart(printwidth) 21.0
    
    # Font !!!!
    # set chart($name,font) "-*-Courier-Bold-R-Normal--*-120-*"
    #	set chart($name,font) "-*-helvetica-medium-r-*-*-8-*-*-*-*-*-*-*"
    set chart(defaultfont) "-*-clean-medium-r-*-*-8-*-*-*-*-*-*-*"
    # set chart($name,font) fixed
    # set chart($name,font) 5x7
    
    set chart(defaultwidth) 800
    set chart(defaultheight) 600
    
    set chart(defaultscrollwidth) 1024
    set chart(defaultscrollheight) 768
    
    set chart(defaultwidthincrement) 200
    set chart(defaultheightincrement) 1000
    
    # werden die Kanten gekippt ?
    set chart($name,view) "straight"
    
    # das generelle Aussehen der Chart
    set chart($name,drawingstyle) timebased
    
    # Abstand zwischen den Regeln
    set chart(rulespace) 3

    # Linien- und Tokendicke
    set chart(defaultlinewidth) 1
    set chart(defaulttokenwidth) 1
    
    # Durchmesser der Token
    set chart(defaulttokendiameter) 12

    # 981127 Abstand der Token
    #set chart(default_minimum_space) [expr [lindex [broadness_of_string "HEAD-FINAL-P-RULE" $name] 0] + 60]
    #981127 setze Tokendistance absolut (gleicher Wert wie vorher)
    set chart(default_minimum_space) 138

    #981130 Abstand nach unten definierbar
    set chart(downspace) 150
    
    # Faktor, um den die Linien verschoben werden
    set chart(defaultstretchfactor) 5
    
    # Der Stretchwert am Anfang
    set chart($name,stretch) 0
    
    # Defaultwerte fuer Print-Dialog
    set chart(printmedium) "File"
    set chart(printprintername) "hebrew"
    set chart(printfilename) "test1.ps"
    set chart(printdims) "One"
    set chart(printdimensions) "4x4"
    
    # Die im Config-File definierten Farben
    set chart(color,active_color) yellow
    set chart(color,passive_color) white
    set chart(color,blink_color) green
    set chart(color,token_color) blue
    set chart(color,disabled_foreground) white
    
    # 990520 Mark Color fuer Eltern und Kinder
    set chart(color,dye_color) black

    # 980930 Mark-Color
    # 981001 set the mark color directly
    # 990527 the default mark color
    set chart(color,mark_color) blue
    
    set chart(bw,active_color) black
    set chart(bw,passive_color) black
    set chart(bw,blink_color) white
    set chart(bw,token_color) black
    set chart(bw,disabled_foreground) black

    # 990520 Mark Color fuer Eltern und Kinder
    set chart(bw,dye_color) white

    # 980930 Mark-Color
    # 981001 set the mark color directly
    # 990527 set a default mark color
    set chart(bw,mark_color) white
    
    # Die Defaultfarben fuer aktive und passive Kanten
    if { $chart(colormodel) == 1 } {
        set chart($name,color,active_color) "yellow"
        set chart($name,color,passive_color) "white"
        set chart($name,color,blink_color) "red"
        set chart($name,color,token_color) "blue"
        set chart($name,color,disabled_foreground) "white"
        
        # 990520 Mark Color fuer Eltern und Kinder
        set chart($name,color,dye_color) black

        # 980930 Mark-Color
        # 981001 set the mark color directly
        # 990527 set a default mark color
        set chart($name,color,mark_color) "blue"

    } else {
        set chart($name,color,active_color) "black"
        set chart($name,color,passive_color) "black"
        set chart($name,color,token_color) "black"
        set chart($name,color,blink_color) "white"
        set chart($name,color,disabled_foreground) "black"

        # 990520 Mark Color fuer Eltern und Kinder
        set chart($name,color,dye_color) "white"

        # 980930 Mark-Color
        # 981001 set the mark color directly
        # 990527 this is the default mark color
        set chart($name,color,mark_color) "white"
    }

    # Ein Flag, welche Kanten gerade angezeigt werden
    set chart($name,which_edges) "all"
    
    # Ein Flag, welche Tokens gerade beruecksichtigt werden
    set chart($name,which_token) "all"
    
    # Ein Flag, welche Extension gerade angezeigt wird
    set chart($name,which_extension) "both"
    
    my_trace "Leaving set_default_values"
    
}

proc reset_values { name } {

    global chart map edge token draw
    
    my_trace "Entering reset-values $name"
    
    #     # reset chart
    #     set index [array names chart "$name,*"]
    #     for { set i 0 } { $i < [llength $index] } { incr i } {
    #         set this_index [lindex $index $i]
    #         unset chart($this_index)
    #     }
    
    # reset token
    set index [array names token "$name,*,edges_start"]
    for { set i 0 } { $i < [llength $index] } { incr i } {
        set this_index [lindex $index $i]
        unset token($this_index)
    }
    
    # reset edge
    set index [array names edge "$name,*,parents"]
    for { set i 0 } { $i < [llength $index] } { incr i } {
        set this_index [lindex $index $i]
        unset edge($this_index)
    }
    
    # setze Werte wieder zurueck
    
    # Anzahl der Knoten am Anfang auf -1
    set chart($name,max_token) -1
    
    # Anzahl der Gesamt-Kanten am Anfang auf -1
    set chart($name,max_edges) -1
    
    # Anzahl der tatsaechlich gezeichneten Kanten, -1
    set chart($name,drawn_edges) -1
    
    # Anzahl der Custom-Aktionen
    #	set chart($name,max_action) -1
    
    # Anzahl der Regeln (Rules)
    set chart($name,max_rules) -1
    
    # Hoehe der Rules
    set chart($name,ruleheight) -1
    
    # werden die Kanten gekippt ?
    set chart($name,view) "straight"
    
    # das generelle Aussehen der Chart
    set chart($name,drawingstyle) timebased
    
    # Ein Flag, welche Kanten gerade angezeigt werden
    set chart($name,which_edges) "all"
    
    # Ein Flag, welche Tokens gerade beruecksichtigt werden
    set chart($name,which_token) "all"
    
    # Ein Flag, welche Extension gerade angezeigt wird
    set chart($name,which_extension) "both"
    
    
    # reset label of edge
    set index [array names edge "$name,*,label"]
    for { set i 0 } { $i < [llength $index] } { incr i } {
        set this_index [lindex $index $i]
        unset edge($this_index)
    }
    
    #     # reset draw
    #     set index [array names draw "$name,*"]
    #     for { set i 0 } { $i < [llength $index] } { incr i } {
    #         set this_index [lindex $index $i]
    #         unset draw($this_index)
    #     }
    
    #     # reset map
    #     set index [array names map "$name,*"]
    #     for { set i 0 } { $i < [llength $index] } { incr i } {
    #         set this_index [lindex $index $i]
    #         unset map($this_index)
    #     }
    
    my_trace "Leaving reset_values"
    
}

# shows a window with some info about the display
proc show_about { } {

    global chart

    my_trace "Entering proc show_about"

    if {[winfo exists .about] == 1} {
        destroy .about
    }
    toplevel .about
    wm title .about "About the Chartdisplay"

    label .about.title -text "Chartdisplay" -bg white
    label .about.version -text $chart(chart_display_version)
    label .about.copy -text "(c) 1997-1999 Patric Stiffel"
    label .about.dfki -text "part of the masterthesis at the DFKI GmbH"
    label .about.mail -text "Contact me: stiffel@dfki.de"

    button .about.ok -text "Ok" -width 10 -command "destroy .about"

    pack .about.title .about.version .about.copy .about.dfki .about.mail .about.ok -side top


    my_trace "Leaving show_about"
}

# a modal dialog presents a title, a text and a button
proc modal_dialog { title messagetext buttontext } {

    global chart
    
    my_trace "Entering proc modal_dialog $title $messagetext $buttontext"

    if {[winfo exists .modal] == 1} {
        destroy .modal
    }
    toplevel .modal
    wm title .modal $title
    focus .modal
    grab .modal
    
    label .modal.title -text $title

    message .modal.message -text $messagetext -aspect 400 -justify center

    button .modal.button -text $buttontext -command "destroy .modal"

    pack .modal.title .modal.message .modal.button -side top -pady 5



    my_trace "Leaving modal_dialog"
}

# this proc allows an icon to be set for the main window

proc wmIconImage {toplevel image} {
#    variable _imgIcon_count
    if {![info exists _imgIcon_count]} {
        set _imgIcon_count 0
    }
    set iconTop [toplevel ._iconImage[incr _imgIcon_count]]
    pack [label $iconTop.l -image $image]
    wm iconwindow $toplevel $iconTop
}
