dialog
TCL/TK UseCase3
Abstract : Contains the dialog boxes used through out the program.
Comments : Pay close attention to the way in which the save and
           open works.  Most of the code is reused for both 
           functions and for the different type of saves.
# This file contains code for all of the dialogue boxes used by the application. 
# Each dialogue box is a seperate window. Ex. File open/save windows, print 
# window, etc. 
# Creates a 'confirm' dialogue box which asks the user if they wish to proceed
# with their current action. When this proceedure is called, a text message can be 
# passed which will be printed in the yesno window. There are 2 buttons 
# automatically created: "No!" and "Sure do." A global flag is set according to 
# whether the user clicked on "No!" or "Sure do."
# This proceedure is called when the user attempts do delete an edge or a vertex,
# and when the user attempts to quit without saving. 
proc yesno { words x y } {
    global flagno
    toplevel .asking
    wm transient .asking .
    set temp [winfo x .]
    set temp2 [winfo y .]
    set x2 [expr $x + $temp]
    set y2 [expr $y + $temp2]
    wm geometry .asking 200x125+$x2+$y2
# Creates the message text:
    message .asking.t -relief raised -background gray \
	    -width 150 -text $words
# Creates "No!" button: When clicked, sets global var. flagno to 0.
    button .asking.b1 -text "No!" -background red -command \
	    {set flagno 0 ; destroy .asking}
# Creates "Sure do." button: When clicked, sets global var. flagno to 1.
    button .asking.b2 -text "Sure do." -background red -command \
	    {set flagno 1 ; destroy .asking}
    pack .asking.t
    pack .asking.b2 .asking.b1 -fill y -padx 1m -pady 1m
    update idletask
    grab .asking
    tkwait window .asking
}
# Not used
proc nextcord {words} {
global passx
global passy
toplevel .asking
wm transient .asking .
message .asking.t -relief raised -background gray \
        -width 150 -text $words
pack .asking.t -fill x
bind .area  {set passx %x ; set passy %y ; destroy .asking}
update idletask
grab set .
tkwait window .asking
bind .area  {whattodo %x %y}
}
# Creates a window which displays the text message passed to the procedure, and 
# contains an "Okay" button.  
proc ask {words} {
    toplevel .asking
    wm transient .asking .
    wm group .asking .
#    wm geometry .asking 100x100+$x+$y
# Creates the text message
    message .asking.t -relief raised -background gray \
	    -width 150 -text $words
# Creates the "Okay" button. When this button is clicked, the window gets 
# destroyed.
    button .asking.b -text "Okay" -background red\
	    -command "destroy .asking"
    
    pack .asking.t .asking.b -fill x
    
    update idletask
    grab .asking
    tkwait window .asking
}
# Creates dialogue box used to let user enter in the name of a vertex or edge label.
# This procedure is used when a verted or construction edge is created, and and when 
# the user renames the label of a vertex or construction edge.
proc asklabel {words x y} {
    global askingName
    set askingName ""
    toplevel .asking
    wm transient .asking .
    set temp [winfo x .]
    set temp2 [winfo y .]
    set x2 [expr $x + $temp]
    set y2 [expr $y + $temp2]
    wm geometry .asking 150x75+$x2+$y2
    message .asking.t -relief raised -background gray \
	    -width 150 -text $words
# askingName is the text variable which stores the text entered by the user. This
# gets returned to the routine which called this procedure.
    entry .asking.name -background green -textvariable askingName
# Creates "Enter" and "Cancel" buttons:
    button .asking.b -text "Enter" -background red \
	    -command "destroy .asking"
    button .asking.b2 -text "Cancel" -background red \
	    -command {set askingName "" ; destroy .asking}
    
    pack .asking.t .asking.name -fill x
    pack .asking.b .asking.b2 -side right -padx 1m -pady 1m
    
    bind .asking.name  {destroy .asking}
    
    update idletask
    grab .asking
    tkwait window .asking
    return $askingName
}
#Chi-Chen's and Mike's opening & saving
# The openentry procedure gets called when a filename or directory name is selected -
# either by the user double-clicking on an entry in the file  selection window, or by
# the user entering in a filename into the text entry field of the file selection window
# and hitting return.
proc openentry {entry} {
 global boxflag iGraph changes can filename
# boxflag is a global variable which tells this procedure whether: 
    #    1)a file is to be opened or saved.
    #    2)a CD is to be generated and saved to a file.
    #    3)a postscript file is to be saved.
# Checks to see if the filename is valid.
 if [file exists $entry] {
# If entry is a directory name, we change to that directory and create a new file
# list box containing all files in that directory.
   if [file isdirectory $entry] {
    cd $entry    
    destroy .fs.files
    listbox .fs.files -relief raised -borderwidth 4 \
        -yscrollcommand ".fs.scroll set"
    pack .fs.files -side left
    bind .fs.files  {openentry [selection get]}
    list-out
    global entered
    set $entered {}
   }  elseif [file exists $entry] {
# Do following if file exists:
   if [file isfile $entry] {
#this is where the open & save stuff happens
	if {$boxflag == "open"} {
	set filename $entry
# A file is being opened, so we call the c++ function to open a file.
# We call function cppOpenCD in the iGraph object, passing it the file 
# name entered.
        set retVal [$iGraph cppOpenCD: $entry]
	destroy .fs
        set changes no
	}
   } } } elseif {[file exists $entry] != 1} {
# Execute the following if the filename does not exist:
	if {$boxflag == "savefile"} {
	set changes no
	set filename $entry
# A file is being saved, so we call the c++ function to save a file.
# We call function cppSaveCD in the iGraph object, passing it the file 
# name entered. 
	set retVal [$iGraph cppSaveCD: $entry]
	destroy .fs
	} elseif {$boxflag == "savecd"} {
	puts "Sending: GenCD $entry"
# A Class Dictionary is being generated, so we call the c++ 
# function to generate the CD and save to a file.
# We call function cppGenCD in the iGraph object, passing it the file 
# name entered.
	set retVal [$iGraph cppGenCD: $entry]
	destroy .fs
        # Save as a postscript:
	} elseif {$boxflag == "saveps"} {
# A PostScript file is being generated, so we call the
# PostScript procedure which saves the file with $entry file name.
            Postscript $can $entry
            destroy .fs
	} else {
#	puts "Sorry, that file doesn't exist."
} 
} }
# Creates the text entry field in the file entry window which allows the user to enter
# a filename. If the user hits return, the procedure openentry is called.
proc file-entry {type} {
global entered filename
frame .fs.file
label .fs.file.label -text $type
entry .fs.file.entry -width 20 -relief sunken -bd 2 -textvariable entered
pack .fs.file.label .fs.file.entry -side left -padx 1m -pady 2m
# When user enters a filename in the text box, openentry is called and passed the name of 
# the file which the user entered.
bind .fs.file.entry  {openentry $entered ; set entered $filename}
pack .fs.file -side top
}
# Generates the list of files in the file select window.
proc list-out {} {
.fs.files insert end .
.fs.files insert end ..
foreach i [lsort [glob *]] {
        .fs.files insert end $i
}
}
# File select dialogue box window: Creates a window which contains a scrollable list of
# files in the current directory. The user selects an entry in the window by 
# double-clicking on the file or directory name.  
proc list-box {} {
listbox .fs.files -relief raised -borderwidth 4 \
        -yscrollcommand ".fs.scroll set"
pack .fs.files -side left
scrollbar .fs.scroll -command ".fs.files yview"
pack .fs.scroll -side right -fill y
list-out
# Calls the procedure which generates the list of files in the current directory. 
list-out
# If the user double-clicks on a filename, openentry is called and passed the filename
# or directory name which the user double-clicked on.
bind .fs.files  {openentry [selection get]}
}
# The following functions create the file selection box based on whether the user wishes
# to open a file, save a file, generate a CD and save to a file, or save a postscript file.
# Open a file:
proc openfile {} {
global boxflag
# Tells procedure openentry that we want to open a file.
set boxflag open
toplevel .fs
wm title .fs "Select File:"
file-entry "Open File:"
list-box
}
# Save a file:
proc savefile {} {
global boxflag filename iGraph changes
#Saves if file has already been opened
if {$filename != ""} {
        set changes no
        set retVal [$iGraph cppSaveCD: $filename]
	return
	}
# Tells procedure openentry that we want to save a file.
set boxflag savefile
toplevel .fs
wm title .fs "Save File:"
file-entry "Save File:"
list-box
}
# Save a file as:
proc savefileas {} {
global boxflag filename iGraph changes
# Tells procedure openentry that we want to save a file.
set boxflag savefile
toplevel .fs
wm title .fs "Save File as:"
file-entry "Save File as:"
list-box
}
# Generate a CD:
proc savecd {} {
global boxflag
# Tells procedure openentry that we want to save a CD.
set boxflag savecd
toplevel .fs
wm title .fs "Save CD:"
file-entry "Save CD:"
list-box
}
# Save a postscript file
proc savepost {} {
global boxflag
set boxflag saveps
toplevel .fs
wm title .fs "Save Postscript:"
file-entry "Save Postscript:"
list-box
}
# Print dialogue box
proc printwindow {} {
    toplevel .pw
    wm title .pw "Print:"
    frame .pw.frame1
    frame .pw.frame2
    # Pack the 2 frames:
    pack  .pw.frame1 -side top -fill x
    pack  .pw.frame2 -side bottom -fill x
    label .pw.frame1.label -text {Enter Printer:}
    # Text entry widget
    entry .pw.frame1.entry -width 20 -relief sunken -textvariable printer
    frame .pw.frame2.p -borderwidth 2 -relief sunken
    # Print button runs print procedure
    button .pw.frame2.p.print -text "Print" -padx 5 -pady 5 \
        -command {print $printer}
    # Cancel button destroys window
    button .pw.frame2.cancel -text "Cancel" -padx 5 -pady 5 -command\
        {destroy .pw}
    # When you push , it prints
    bind .pw.frame1.entry  {print $printer}
    # Pack labels and entries and buttons (oh, my!) in each frame:
    pack .pw.frame1.label -side left
    pack .pw.frame1.entry -side right
    pack .pw.frame2.p .pw.frame2.p.print -side left
    pack .pw.frame2.cancel -side right
}
# Prints the postscript file and destroys the printing dialogue box.
proc print {printer} {
    printcanvas $printer
    # Destroy the printer window:
    destroy .pw
}
 
 
 
 
 
 
 
 
