# File popup.tcl
#
# Prozeduren zum Erzeugen der verschiedenen Popups

proc make_tokenpopup { name id } {

    global chart edge token

    my_trace "Entering make_tokenpopup"
    
    if { [winfo exists .chart$name.tokenpopup] == 1 } {
        destroy .chart$name.tokenpopup
    }
    
    set popup [menu .chart$name.tokenpopup -tearoff 0 -disabledforeground $chart($name,color,disabled_foreground)]

    $popup add command -label "Token No.: $id" -state disabled
    $popup add separator    
    if { $chart(english) == 1 } {
        $popup add command -label "All edges starting here" -command "redraw_chart $name start $id"
    } else {
        $popup add command -label "Alle Kanten, die hier beginnen" -command "redraw_chart $name start $id"
    }
    if { $chart(popup_activepassive) == 1 } {
        if { $chart(english) == 1 } {
            $popup add command -label "Active edges starting here" -command "redraw_chart $name startactive $id"
        } else {
            $popup add command -label "Aktive Kanten, die hier beginnen" -command "redraw_chart $name startactive $id"
        }
        if { $chart(english) == 1 } {
            $popup add command -label "Passive edges starting here" -command "redraw_chart $name startpassive $id"
        } else {
            $popup add command -label "Passive Kanten, die hier beginnen" -command "redraw_chart $name startpassive $id"
        }
    }
    if { $chart(popup_leftright) == 1 } {
        if { $chart(english) == 1 } {
            $popup add command -label "Left extending edges starting here" -command "redraw_chart $name startleft $id"
        } else {
            $popup add command -label "Links expandierende Kanten, die hier beginnen" -command "redraw_chart $name startleft $id"
        }
        if { $chart(english) == 1 } {
            $popup add command -label "Right extending edges starting here" -command "redraw_chart $name startright $id"
        } else {
            $popup add command -label "Rechts expandierende Kanten, die hier beginnen" -command "redraw_chart $name startright $id"
        }
    }
    $popup add separator
    if { $chart(english) == 1 } {
        $popup add command -label "All edges ending here" -command "redraw_chart $name end $id"
    } else {
        $popup add command -label "Alle Kanten, die hier enden" -command "redraw_chart $name end $id"
    }
    if { $chart(popup_activepassive) == 1 } {
        if { $chart(english) == 1 } {
            $popup add command -label "Active edges ending here" -command "redraw_chart $name endactive $id"
        } else {
            $popup add command -label "Aktive Kanten, die hier enden" -command "redraw_chart $name endactive $id"
        }
        if { $chart(english) == 1 } {
            $popup add command -label "Passive edges ending here" -command "redraw_chart $name endpassive $id"
        } else {
            $popup add command -label "Passive Kanten, die hier enden" -command "redraw_chart $name endpassive $id"
        }
    }
    if { $chart(popup_leftright) == 1 } {
        if { $chart(english) == 1 } {
            $popup add command -label "Left extending edges ending here" -command "redraw_chart $name endleft $id"
        } else {
            $popup add command -label "Links expandierende Kanten, die hier enden" -command "redraw_chart $name endleft $id"
        }
        if { $chart(english) == 1 } {
            $popup add command -label "Right extending edges ending here" -command "redraw_chart $name endright $id"
        } else {
            $popup add command -label "Rechts expandiernde Kanten, die hier enden" -command "redraw_chart $name endright $id"
        }
    }

}

proc make_edgepopup { name id what } {

    global chart edge token action

    my_trace "Entering make_edgepopup $name $id $what"
    
    if { [winfo exists .chart$name.edgepopup] == 1 } {
        destroy .chart$name.edgepopup
    }
    
    set popup [menu .chart$name.edgepopup -tearoff 0 -disabledforeground $chart($name,color,disabled_foreground)]

    if { $what == "edge" } {
        bind $popup <Unmap> "rebuild_item $name $id"
    } else {
        bind $popup <Unmap> ""
    }
    
    $popup add command -label $edge($name,$id,label) -state disabled
    $popup add separator
    if { $what == "edge" } {
        if { $chart(english) == 1 } {
            $popup add command -label "Mark edge" -command "mark_edge $name $id 0"
        } else {
            $popup add command -label "Markiere Kante" -command "mark_edge $name $id 0"
        }
        if { $chart(english) == 1 } {
            $popup add command -label "Unmark edge" -command "unmark_edge $name $id"
        } else {
            $popup add command -label "Entferne Markierung" -command "unmark_edge $name $id"
        }
        $popup add separator
        if { $chart(english) == 1 } {
            $popup add command -label "Show children" -command "redraw_children $name $id"
        } else {
            $popup add command -label "Zeige Kinder" -command "redraw_children $name $id"
        }
        if { $chart(english) == 1 } {
            $popup add command -label "Show parents" -command "redraw_parents $name $id"
        } else {
            $popup add command -label "Zeige Eltern" -command "redraw_parents $name $id"
        }
# 990520
        if { $chart(english) == 1 } {
            $popup add command -label "Dye children" -command "dye_children $name $id"
        } else {
            $popup add command -label "Markiere Kinder" -command "dye_children $name $id"
        }
        if { $chart(english) == 1 } {
            $popup add command -label "Undye children" -command "undye_children $name $id"
        } else {
            $popup add command -label "Entferne Kinder-Markierung" -command "undye_children $name $id"
        }

        $popup add separator
    }

    for { set i 0 } { $i <= $chart($name,max_action) } { incr i } {

        if { [string compare edge $action($name,$i,menu)] == 0 } {
            # 981002 new: enabled and disabled entries
            if { $action($name,$i,status) == 1 } {
                $popup add command -label $action($name,$i,label) -command "send_edge_action $name $action($name,$i,id) $id"
            } else {
                $popup add command -label $action($name,$i,label) -command "send_edge_action $name $action($name,$i,id) $id" -state disabled
            }
        }
    }

    my_trace "Leaving make_edgepopup"

}

proc make_defaultpopup { name } {

    global chart edge token action

    my_trace "Entering make_defaultpopup"

    if { [winfo exists .chart$name.defaultpopup] == 1 } {
	destroy .chart$name.defaultpopup
    }

    set popup [menu .chart$name.defaultpopup -tearoff 0 -disabledforeground $chart($name,color,disabled_foreground)]

    $popup add command -label "Chart $name" -state disabled
    $popup add separator
    if { $chart(english) == 1 } {
        $popup add command -label "Show all edges" -command "redraw_chart $name allrules" -underline 5
        } else {
            $popup add command -label "Zeige alle Kanten" -command "redraw_chart $name allrules" -underline 6
        }

    if { $chart(popup_activepassive) == 1 } {
        $popup add separator
        if { $chart(english) == 1 } {
            $popup add command -label "Show only active edges" -command "redraw_chart $name allactive"
        } else {
            $popup add command -label "Zeige nur aktive Kanten" -command "redraw_chart $name allactive"
        }
        if { $chart(english) == 1 } {
            $popup add command -label "Show only passive edges" -command "redraw_chart $name allpassive"
        } else {
            $popup add command -label "Zeige nur passive Kanten" -command "redraw_chart $name allpassive"
        }
    }

    if { $chart(popup_leftright) == 1 } {
        $popup add separator
        if { $chart(english) == 1 } {
            $popup add command -label "Show only right extending edges" -command "redraw_chart $name allright"
        } else {
            $popup add command -label "Zeige nur rechts expandierende Kanten" -command "redraw_chart $name allright"
        }
        if { $chart(english) == 1 } {
            $popup add command -label "Show only left extending edges" -command "redraw_chart $name allleft"
        } else {
            $popup add command -label "Zeige nur links expandierende Kanten" -command "redraw_chart $name allleft"
        }
    }
    if { $chart(popup_view) == 1 || $chart(popup_style) == 1 } {
        $popup add separator
    }
    if { $chart(popup_view) == 1 } {
        if { [string compare $chart($name,view) "straight"] == 0 } {
            if { $chart(english) == 1 } {
                $popup add command -label "Toggle view: stretched" -command "set chart($name,view) stretched ; redraw_chart $name all" -underline 7
            } else {
                $popup add command -label "Ändere Ansicht: gebogen" -command "set chart($name,view) stretched ; redraw_chart $name all"
        }
        } else {
            if { $chart(english) == 1 } {
                $popup add command -label "Toggle view: straight" -command "set chart($name,view) straight ; redraw_chart $name all"
            } else {
                $popup add command -label "Ändere Ansicht: gerade" -command "set chart($name,view) straight ; redraw_chart $name all"
            }
        }
    }
    if { $chart(popup_style) == 1 } {
        if { [string compare $chart($name,drawingstyle) "timebased"] == 0 } {
            if { $chart(english) == 1 } {
                $popup add command -label "Toggle drawing style: compact" -command "set chart($name,drawingstyle) compact;redraw_chart $name all" -underline 7
            } else {
                $popup add command -label "Ändere Zeichenstil: kompakt" -command "set chart($name,drawingstyle) compact;redraw_chart $name all"
            }
        } else {
            if { $chart(english) == 1 } {
                $popup add command -label "Toggle drawing style: timebased" -command "set chart($name,drawingstyle) timebased;redraw_chart $name all"
            } else {
                $popup add command -label "Ändere Zeichenstil: zeitbasiert" -command "set chart($name,drawingstyle) timebased;redraw_chart $name all"
            }
        }
    }
    if { $chart(popup_playback) == 1 } {
        $popup add separator
        if { $chart(english) == 1 } {
            $popup add command -label "Playback" -command "recorder $name"
        } else {
            $popup add command -label "Wiederholung" -command "recorder $name"
        }
    }
    $popup add separator

    for { set i 0 } { $i <= $chart($name,max_action) } { incr i } {

        if { [string compare main $action($name,$i,menu)] == 0 } {

            # 981002 new: en- and disabled entries
            if { $action($name,$i,status) == 1 } {
                $popup add command -label $action($name,$i,label) -command "send_main_action $name $action($name,$i,id)"
            } else {
                $popup add command -label $action($name,$i,label) -command "send_main_action $name $action($name,$i,id)" -state disabled
            }
        }
    }

    
}

proc toggle_view { name } {

    global chart

    my_trace "Entering toggle_view"

    if { [string compare $chart($name,view) "straight"] == 0 } {
	set chart($name,view) "stretched"
	set chart(defaultstretchfactor) 5
	redraw_chart $name all
    } else {
	set chart($name,view) "straight"
	set chart(defaultstretchfactor) 0
	redraw_chart $name all
    }
}

proc toggle_drawing { name } {

    global chart

    my_trace "Entering toggle_drawing"

    if { [string compare $chart($name,drawingstyle) "timebased"] == 0 } {
	set chart($name,drawingstyle) "compact"
	redraw_chart $name all
    } else {
	set chart($name,drawingstyle) "timebased"
	redraw_chart $name all
    }
}

proc rebuild_item { chart_id edge_id } {

    global chart edge

    my_trace "Entering proc rebuild_item $chart_id $edge_id"

#    .chart$chart_id.canvas.draw itemconfigure text$id -fill black

    if { $edge($chart_id,$edge_id,dye) == 1 } { # Kante ist markiertes Kind
        .chart$chart_id.canvas.draw itemconfigure "edge$edge_id" -fill $chart($chart_id,color,dye_color) -width 2.0
    
    } else {
        if { $edge($chart_id,$edge_id,mark) == 1 } { # Kante ist sonstwie markiert
            .chart$chart_id.canvas.draw itemconfigure edge$edge_id -fill $edge($chart_id,$edge_id,mark_color)
        } else {
            if { $edge($chart_id,$edge_id,status) == 1} { # Kante ist aktiv
                .chart$chart_id.canvas.draw itemconfigure edge$edge_id -fill $chart($chart_id,color,active_color)
            } else { # Kante ist passiv
                .chart$chart_id.canvas.draw itemconfigure edge$edge_id -fill $chart($chart_id,color,passive_color)
            }
        }
    }

    # Ist das das Elternteil?
    if { [lsearch -exact [.chart$chart_id.canvas.draw gettags "text$edge_id"] "parent"] != -1 } { # es ist ein Elternteil
        if { [string compare $chart($chart_id,color,dye_color) "white"] != 0 } {
            .chart$chart_id.canvas.draw itemconfigure "text$edge_id" -fill white
        }
    } else {
        .chart$chart_id.canvas.draw itemconfigure text$edge_id -fill black
    }
        my_trace "Leaving rebuild_item"
}

# this proc creates a rule popup
proc make_rulepopup { name id } {

    global chart edge token action

    my_trace "Entering make_rulepopup $name $id"
    
    if { [winfo exists .chart$name.rulepopup] == 1 } {
        destroy .chart$name.rulepopup
    }
    
    set popup [menu .chart$name.rulepopup -tearoff 0 -disabledforeground $chart($name,color,disabled_foreground)]

    $popup add command -label $edge($name,$id,label) -state disabled
    $popup add separator

    for { set i 0 } { $i <= $chart($name,max_action) } { incr i } {

        if { [string compare rule $action($name,$i,menu)] == 0 } {

            # 981002 new: en- and disabled entries
            if { $action($name,$i,status) == 1 } {
                $popup add command -label $action($name,$i,label) -command "send_edge_action $name $action($name,$i,id) $id"
            } else {
                $popup add command -label $action($name,$i,label) -command "send_edge_action $name $action($name,$i,id) $id" -state disabled
            }
        }
    }
}
