source [file join $GoblinTclPath export.tk]

destroy .export
toplevel .export
wm title .export "GOBLET Export Graph Object"
wm geometry .export 570x400
# wm resizable .export 0 0

global OrigFileName FileName Filter ExportFilter ThisFile InputFormat
global ThisType Mode goblinExtension
if {![info exists InputFormat]} {set InputFormat "GOBLIN Graph Object"}
if {$Mode=="trace object"} {
    set ThisSelector $FileCounter
} else {
    global ThisGraph
    $ThisGraph write $FileName.export.gob
    set ThisSelector "export"
}
set ThisFile [file tail $OrigFileName]_$ThisSelector.$goblinExtension
set ExportFilter $Filter

proc ExportNow {} {
    global FileName FileCounter Filter ExportFilter ThisFile InputFormat
    global ThisSelector goblinExtension FType Ext ThisGraph OrigFileName
    if {[file exists $ExportFilter] && $ThisFile != ""} {
        set exportName [file join $ExportFilter [file rootname $ThisFile]]
        set traceFile $FileName.$ThisSelector
        if {[string equal $FType($InputFormat) "object"]} {
            if {[string equal $Ext($InputFormat) $goblinExtension]} {
                file copy -force "$traceFile.$goblinExtension" "$exportName.$goblinExtension"
                set OrigFileName $exportName
                OpenFile
            } else {
                goblinExport $ThisGraph $exportName.$Ext($InputFormat) $Ext($InputFormat)
            }
        } else {
            goblin export xfig $traceFile.$goblinExtension $traceFile.fig
            if {[string equal $Ext($InputFormat) "fig"]} {
                file copy -force "$traceFile.fig" "$exportName.$Ext($InputFormat)"
            } else {
                exec fig2dev -L $Ext($InputFormat) "$traceFile.fig" "$exportName.$Ext($InputFormat)"
            }
            file delete $traceFile.fig
        }
            
        destroy .export
        cd $Filter
    }
}

frame .export.f1
    label .export.f1.label -text "File:" -anchor w
    entry .export.f1.entry -textvariable ThisFile -bg white -width 70
    pack .export.f1.label .export.f1.entry -side top -fill x
    
    bind .export.f1.entry <Return> ExportNow
     
frame .export.f2
    label .export.f2.label -text "Filter:" -anchor w
    entry .export.f2.entry -textvariable ExportFilter -bg white -width 70
    pack .export.f2.label .export.f2.entry -side top -fill x
    
    bind .export.f2.entry <Return> DisplaySelection
     
frame .export.f3
    label .export.f3.label -text "Selection:" -anchor w
    scrollbar .export.f3.scroll -command ".export.f3.list yview"
    pack .export.f3.label -side top -fill x
    pack .export.f3.scroll -side left -fill y
    
    proc DisplaySelection {} {
        global ExportFilter ThisFile
        if {![file exists $ExportFilter]} {return}
        cd $ExportFilter
        
        destroy .export.f3.list
        listbox .export.f3.list -yscrollcommand ".export.f3.scroll set" -width 30
        pack .export.f3.list -side left -before .export.f3.scroll
    
        .export.f3.list insert end ".."
        foreach i [lsort [glob -nocomplain *]] {
            if {[file isdirectory $i]} {.export.f3.list insert end $i}
        }
        foreach i [lsort [glob -nocomplain *]] {
            if {![file isdirectory $i]} {.export.f3.list insert end $i}
        }

        bind .export.f3.list <Double-Button-1> {
            if {[file isdirectory [selection get]]} {
                cd [selection get]
                set ExportFilter [pwd]
                set ThisFile ""
                DisplaySelection
            } else {
                set ThisFile [selection get]
            }
        }
    }

    DisplaySelection
    
global Ext FType
frame .export.f4
    label .export.f4.label -text "File Format:" -anchor w
    pack .export.f4.label -side top -fill x

    scrollbar .export.f4.scroll -command ".export.f4.list yview"
    listbox .export.f4.list -yscrollcommand ".export.f4.scroll set" -width 25
    
    foreach {format extension ftype} { \
        "GOBLIN Graph Object"       gob     object \
        "DIMACS Undirected Graph"   edge    object \
        "DIMACS Max Flow Instance"  max     object \
        "DIMACS Min Cost Flow Pr."  min     object \
        "DIMACS Flow Labels"        flow    object \
        "DIMACS Assignment Problem" asn     object \
        "DIMACS Geometric Matching" geom    object \
        "DIMACS Matching Arcs"      match   object \
        "TSPLIB Problem"            tsp     object \
        "TSPLIB Solution"           tour    object \
        "Tk Canvas"                 tk      canvas \
        "xFig Canvas"               fig     canvas \
        "Postscript"                ps      canvas \
        "Encapsulated Postscript"   eps     canvas \
        "Portable Document Format"  pdf     canvas \
        "HTML Image Map"            map     canvas \
        "Autocad Slide"             sld     canvas \
        "GIF"                       gif     bitmap \
        "JPEG"                      jpeg    bitmap \
        "Paintbrush"                pcx     bitmap \
        "Portable Network Graphics" png     bitmap \
        "Portable Pix Map"          ppm     bitmap \
        "TIFF"                      tiff    bitmap \
        "X11 Bitmap"                xbm     bitmap \
        } {
        .export.f4.list insert end $format
        set Ext($format) $extension
        set FType($format) $ftype   
    }
        
    pack .export.f4.list .export.f4.scroll -side left -fill y
    
    bind .export.f4.list <Double-Button-1> {
        set InputFormat [selection get]
        set ThisFile [file tail $FileName]_$ThisSelector.$Ext($InputFormat)
    }
     
frame .export.buttons
    button .export.buttons.save -text "Export" -command ExportNow
    button .export.buttons.cancel -text "Cancel" -command {
        destroy .export
        cd $Filter
    }
    
    pack .export.buttons.save .export.buttons.cancel \
        -side left -fill x -padx 3m
     
pack .export.buttons -padx 3m -pady 3m -side bottom
pack .export.f1 .export.f2 -padx 10m -pady 5m -side top -anchor w
pack .export.f4 .export.f3 -padx 10m -pady 5m -side left -anchor w

set ThisFile [file tail $OrigFileName]_$ThisSelector.$Ext($InputFormat)
