
#-------------------------------------------------------------------------------
# Module:     saveas.tk
# Activation: Dynamically loaded on user request
# Scope:      File export dialog
#
# Updated: 2002/12/18 Confirmation before overwriting files
#          2003/01/01 File compression
#-------------------------------------------------------------------------------




source [file join $GoblinTclPath export.tk]


proc ConfirmedExport {} {
    global ExportFilter ThisFile InputFormat goblinExtension FType Ext \
        BruteOverwrite
    
    set exportDir [file dirname $ExportFilter]
    if {![file exists $exportDir] || $ThisFile == ""} {return}

    set exportName [file join $exportDir [file rootname $ThisFile]]
    if {[string equal $FType($InputFormat) "object"] &&
            [string equal $Ext($InputFormat) $goblinExtension]} {
        set target "$exportName.$goblinExtension"
    } else {
        set target "$exportName.$Ext($InputFormat)"
    }
            
    if {![file exists $target] || $BruteOverwrite} {
        ExportNow
        return
    }
    
    destroy .saveChanges
    toplevel .saveChanges
    wm title .saveChanges "GOBLET Prompt"
    wm resizable .saveChanges 0 0
    grab set .saveChanges

    frame .saveChanges.buttons
    button .saveChanges.buttons.cancel -text "Cancel" -command { \
        destroy .saveChanges
    }
    
    button .saveChanges.buttons.save -text "Yes" -command { \
        ExportNow
        destroy .saveChanges
    }
    
    pack .saveChanges.buttons.save .saveChanges.buttons.cancel \
        -side left -fill x -padx 3m
     
    pack .saveChanges.buttons -padx 3m -pady 3m -side bottom
    
    label .saveChanges.warn -image msg_warn
    message .saveChanges.msg -width 10c \
        -text "Overwrite existing file ?"
    pack .saveChanges.warn .saveChanges.msg -padx 2m -pady 3m -side left
}


proc ExportNow {} {
    global FileName FileCounter WorkingDir ExportFilter ThisFile InputFormat \
        goblinExtension FType Ext ThisGraph OrigFileName Mode ThisBackup \
        ZipEnabled ZipCommand
    
    set exportDir [file dirname $ExportFilter]
    if {[file exists $exportDir] && $ThisFile != ""} {
        set exportName [file join $exportDir [file rootname $ThisFile]]
        if {$ZipEnabled} {set exportName [file rootname $exportName]}
        if {[string equal $Mode "navigate"]} {
            set traceFile $FileName.$FileCounter
        } else {
            $ThisGraph write $FileName.backup$ThisBackup.gob
            set traceFile $FileName.backup$ThisBackup
        }
        if {[string equal $FType($InputFormat) "object"]} {
            if {[string equal $Ext($InputFormat) $goblinExtension]} {
                set zipExportName "$exportName.$goblinExtension"
                file copy -force "$traceFile.$goblinExtension" $zipExportName
                set OrigFileName $exportName
                set WorkingDir $exportDir
                OpenFile
            } else {
                set zipExportName $exportName.$Ext($InputFormat)
                if {[$ThisGraph is mip]} {
                    $ThisGraph write $Ext($InputFormat) $zipExportName
                } else {
                    goblinExport $ThisGraph $zipExportName $Ext($InputFormat)
                }
            }

            if {$ZipEnabled} {
                eval exec $ZipCommand $zipExportName
            }
        } 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 $WorkingDir
    }
}


destroy .export
toplevel .export
wm title .export "Export Data Object"
wm resizable .export 0 0

global OrigFileName FileName WorkingDir ExportFilter ThisFile InputFormat
global ThisType Mode goblinExtension ZipEnabled
if {![info exists InputFormat]} {
    if {[$ThisGraph is mip]} {
        set InputFormat "GOBLIN Mixed Integer Problem"
    } else {
        set InputFormat "GOBLIN Graph Object"
    }
}
if {[string equal $Mode "navigate"]} {
    set ThisSelector _$FileCounter
} else {
    set ThisSelector ""
}
set ThisFile [file tail $OrigFileName]$ThisSelector.$goblinExtension
set ExportFilter [file join $WorkingDir *]


frame .export.f1
    label .export.f1.label -text "File:" -anchor w
    pack .export.f1.label -side top -fill x

    entry .export.f1.entry -textvariable ThisFile -bg white -width 45
    pack .export.f1.entry -side left -fill x

    checkbutton .export.f1.compress -text "Compress File" \
        -variable ZipEnabled -anchor e
    pack .export.f1.compress -side right -expand 1 -fill x
    
    bind .export.f1.entry <Return> ConfirmedExport
     
frame .export.f2
    label .export.f2.label -text "Filter:" -anchor w
    entry .export.f2.entry -textvariable ExportFilter -bg white -width 66
    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 ZipExtension ZipEnabled
        if {![file isdirectory [file dirname $ExportFilter]]} {return}
        if {[file isdirectory $ExportFilter]} {
            cd $ExportFilter
            set ExportFilter [file join $ExportFilter *]
        } else {
            cd [file dirname $ExportFilter]
        }
        
        destroy .export.f3.list
        listbox .export.f3.list -yscrollcommand ".export.f3.scroll set" -width 25
        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}
        }
        if {$ZipExtension!=""} {
            foreach i [lsort [glob -nocomplain \
                    [file tail $ExportFilter]$ZipExtension]] {
                if {![file isdirectory $i]} {.export.f3.list insert end $i}
            }
        }
        foreach i [lsort [glob -nocomplain [file tail $ExportFilter]]] {
            if {![file isdirectory $i] && [file extension $i]!=$ZipExtension} {
                .export.f3.list insert end $i
            }
        }

        bind .export.f3.list <Double-Button-1> {
            if {[file isdirectory [selection get]]} {
                cd [selection get]
                set ExportFilter [file join [pwd] [file tail $ExportFilter]]
                set ThisFile ""
                DisplaySelection
            } else {
                set ThisFile [selection get]
                if {[file extension $ThisFile]==$ZipExtension} {
                    set ZipEnabled 1
                } else {
                    set ZipEnabled 0
                }
            }
        }
    }

    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 30
    
    if {[$ThisGraph is mip]} {
        foreach {format extension ftype} { \
            "GOBLIN Mixed Integer Problem"  gob     object \
            "Standard MPS Problem"          mps     object \
            "CPLEX MPS Problem"             cplex   object \
            "CPLEX LP Problem"              lp      object \
            "BAS Basis File"                bas     object \
            } {
            .export.f4.list insert end $format
            set Ext($format) $extension
            set FType($format) $ftype   
        }
    } else {
        foreach {format extension ftype} { \
            "GOBLIN Graph Object"           gob     object \
            "Tcl Library Graph Object"      tcl     object \
            "DIMACS Undirected Graph"       edge    object \
            "DIMACS Max Flow Instance"      max     object \
            "DIMACS Min Cost Flow Problem"  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)
        set ExportFilter [file join [file dirname $ExportFilter] *.$Ext($InputFormat)]
        DisplaySelection
    }
     
frame .export.buttons
    button .export.buttons.save -text "Export" -command ConfirmedExport
    button .export.buttons.cancel -text "Cancel" -command {
        destroy .export
        cd $WorkingDir
    }
    
    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 5m -pady 3m -side top -anchor w -expand 1 -fill x
pack .export.f4 .export.f3 -padx 5m -pady 3m -side left -anchor w

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