# print.tcl
# enthaelt Files, die das Drucken von Charts abwickeln

###############################################################################
#
# print_chart { name }
#
# Prozedur, die einen Dialog oeffnet, mit dem man die Printoptionen einstellt
#
# Parameter: - name : ID der entsprechenden Chart
#
###############################################################################

proc print_chart { name } {

    global chart

    my_trace "Entering proc print_chart"

    if { [winfo exists .chart$name.print] == 1 } {
        focus .chart$name.print
    } else {
        toplevel .chart$name.print
        wm title .chart$name.print "Chart Printing: $name"
    }

    focus .chart$name.print
    grab .chart$name.print

    set m [ frame .chart$name.print.medium ]

    label $m.label -text "Print to..." \
        -background #000000 -foreground #fefefe -padx 9 -pady 5 \
        -relief raised
    
    set m1 [frame $m.m1]
    set m2 [frame $m.m2]

    radiobutton $m1.file -text "File" -variable chart(printmedium) -value "File" -width 10 -anchor w -command "focus $m1.filename"
    entry $m1.filename -textvariable chart(printfilename)


    radiobutton $m2.printer -text "Printer" -variable chart(printmedium) -value "Printer" -width 10 -anchor w -command "focus $m2.printername"
    entry $m2.printername -textvariable chart(printprintername)

    pack $m.label -side top -fill x

    pack $m1.file $m1.filename -side left
    pack $m2.printer $m2.printername -side left

    pack $m1 $m2 -side top

    pack $m

    set p [frame .chart$name.print.paper]

    label $p.label -text "Paperdimensions" \
        -background #000000 -foreground #fefefe -padx 9 -pady 5 \
        -relief raised


    label $p.height -text "Height:"
    entry $p.eheight -textvariable chart(printheight) -width 5
    label $p.mheight -text "cm  "

    label $p.width -text "Width:"
    entry $p.ewidth -textvariable chart(printwidth) -width 5
    label $p.mwidth -text "cm"
    
    pack $p.label -fill x -side top

    pack $p.height $p.eheight $p.mheight -side left
    pack $p.width $p.ewidth $p.mwidth -side left

    pack $p

    set c [frame .chart$name.print.canvas]


    # berechne aktuelle Groesse des Canvas

    set werte [.chart$name.canvas.draw bbox all]
    set height [expr [lindex $werte 3] - [lindex $werte 1]]
    set width [expr [lindex $werte 2] - [lindex $werte 0]]

    label $c.size -text "Chartsize: [lindex $werte 0]:[lindex $werte 1] - [lindex $werte 2]:[lindex $werte 3]"

    pack $c.size

    pack $c

    set pa [frame .chart$name.print.pages]

    label $pa.label -text "Number of pages" \
        -background #000000 -foreground #fefefe -padx 9 -pady 5 \
        -relief raised

    # Variable fuer Anzahl der Seiten
    set chart(printdims) "custom"

    radiobutton $pa.bu1 -text "One" -value "one" -variable chart(printdims) -command "$pa.en configure -state disabled"
    radiobutton $pa.bu2 -text "Custom" -value "custom" -variable chart(printdims) -command "$pa.en configure -state normal;focus $pa.en"
    entry $pa.en -textvariable chart(printdimensions) -width 3
    label $pa.label1 -text "horizontal x vertical"

    pack $pa.label -side top -fill x

    pack $pa.bu1 $pa.bu2 $pa.en $pa.label1 -side left
    
    pack $pa -side top

    set b [ frame .chart$name.print.buttons ]

    button $b.print -text "Print !" -command "physical_print $name"
    button $b.cancel -text "Cancel" -command "destroy .chart$name.print"

    pack $b.print $b.cancel -side left

    pack $b

}
# end of print_chart


###############################################################################
#
# physical_print { name }
#
# Prozedur, die den physikalischen Ausdruck vornimmt(in ein File oder direkt
# zum Drucker
#
# Parameter: - name : ID der entsprechenden Chart
#
###############################################################################


        
proc physical_print { name } {

    global chart

    my_trace "Entering proc physical_print"

    set FONT($chart(defaultfont)) [list fixed 8]

    # ziehe 1cm von der tatsaechlichen Papiergroesse ab
    set HEIGHT "[expr $chart(printheight) - 1.5]c"
    set WIDTH "[expr $chart(printwidth) - 1.5]c"

    # berechne Chartgroesse
    set werte [.chart$name.canvas.draw bbox all]
    # addiere noch hinzu, da sonst abgeschnitten
    set chartheight [expr [lindex $werte 3] - [lindex $werte 1] + 40 ]
    set chartwidth [expr [lindex $werte 2] - [lindex $werte 0] + 40 ]
    
    my_trace "HeightxWidth: $chartheight x $chartwidth"

    if { $chartheight > $chartwidth } {
        # use portrait
        set rotate 0
        # scale according to height
        set scale "height"
    } else {
        # use landscape
        set rotate 1
        # scale according to width
        set scale "width"
    }

    if { [string compare $chart(printdims) "one"] == 0 } {
        if { $rotate == 1 } {
            
            .chart$name.canvas.draw postscript -fontmap FONT -rotate $rotate -x 0 -y 0 -width $chartwidth -height $chartheight -pageheight $WIDTH -file $chart(printfilename)
            my_trace "Printing: .chart$name.canvas.draw postscript -fontmap FONT -rotate $rotate -x 0 -width $chartwidth -pagewidth $HEIGHT -file $chart(printfilename)"
        } else {
            .chart$name.canvas.draw postscript -fontmap FONT -rotate $rotate -x 0 -y 0 -height $chartheight -width $chartwidth -pagewidth $HEIGHT -file $chart(printfilename)
            my_trace "Printing: .chart$name.canvas.draw postscript -fontmap FONT -rotate $rotate -x 0 -width $chartwidth -pagewidth $HEIGHT -file $chart(printfilename)"
        }


    } else {

        puts "Custom"
        # extrahiere dimensionsinfos
        set paar [ split $chart(printdimensions) x ]
        set hor [ lindex $paar 0 ]
        set ver [ lindex $paar 1 ]

        set herewidth [ expr $chartwidth / $hor + $hor ]
        set hereheight [ expr $chartheight / $ver + $ver ]

        puts "$chartheight,$chartwidth:$hereheight,$herewidth"

        if { $rotate == 1 } {
            
            set count 0

            for { set i 0 } { $i < $hor } { incr i } {
                for { set u 0 } { $u < $ver } {incr u } {
                    
                    .chart$name.canvas.draw postscript -fontmap FONT -rotate $rotate -x [expr $u * $herewidth] -y [expr $i * $hereheight] -width [expr (1+$u) * $chartwidth] -height [expr (1 + $i) * $chartheight] -pageheight $WIDTH -file "$chart(printfilename)$i$u"
                }
            }
        }
    }
}
