#
# $Log: not supported by cvs2svn $
# Revision 1.17  2002/10/08 21:59:23  soliday
# Added the modal option to APSDialogBox.
#
# Revision 1.16  2001/02/06 19:44:21  soliday
# Fixed some tcl warning messages found with procheck
#
# Revision 1.15  2000/06/27 16:20:01  soliday
# Added catch statements around all the grab commands.
#
# Revision 1.14  1997/05/01 16:07:01  borland
# APSEMailDialog now prompts for subject for email message.
#
# Revision 1.13  1997/01/30 15:20:11  borland
# Added option for leaving of "Close" button on APSWindow.
#
# Revision 1.12  1997/01/17 20:36:18  borland
# Fixed help message for APSDialogBox.
#
# Revision 1.11  1996/12/13 01:54:55  borland
# Added -cancelCommand and -okCommand options.
#
# Revision 1.10  1996/10/07 23:31:24  borland
# Added -buttonSize and -size options to DialogBox routines.
#
# Revision 1.9  1996/10/04 19:53:00  saunders
# Added "print", "save as..", and "email..." buttons to APSAlertBox.
# In doing so, I removed functionally duplicate procedures, and created
# generic APSPrint and APSSaveDialog. Also addes "save as.." to APSExecLog.
# Removed "Enscript" buttons, since "Print" is adequate.
#
# Revision 1.8  1996/09/18 14:52:55  saunders
# Option "-withButtons 1" added to APSScrolledStatus.
#
# Revision 1.7  1996/09/17 21:51:15  saunders
# Added APSEMailDialog procedure.
#
# Revision 1.6  1996/01/16  21:19:58  saunders
# Added Log for CVS. Should have done this a long time ago.
#
#

proc APSDialogBoxHelp {} {
    return "Usage: APSDialogBox widget \n\[-parent <string>\]\t\n\[-noPack 1\]\t\n\[-packOption <list>\]\t\n\[-name <string>\]\t\n\[-width <string>\]\t\n\[-height <string>\]\t\n\[-contextHelp <string\]\t\n\[-buttonSize <size>\]\n\t\[-modal 1\]\t\n\[-cancelCommand <command>\]\t\n\[-okCommand <command>\t\nCreates\t\$parent\$widget.\n\tuserFrame\n\tbuttonRow.ok.button\n\tbuttonRow.cancel.button"
}

proc APSDialogBox {widget args} {
    global apsContextHelp apsttk
    set parent ""
    set noPack 0
    set packOption "-side top -fill x"
    set name "NoName"
    set width 200
    set height 100
    set contextHelp ""
    set buttonSize medium
    set okCommand ""
    set cancelCommand ""
    set modal 0
    set cancelButton 1
    set okButton 1
    APSStrictParseArguments {parent name noPack packOption width height contextHelp buttonSize okCommand cancelCommand modal okButton cancelButton}

    if {[info exists apsttk] && ($apsttk != 0)} {
        set ttkPrefix ttk::
    } else {
        set ttkPrefix ""
    }
    if {$contextHelp != ""} {
	set apsContextHelp($parent$widget) $contextHelp
    }

    if {$parent == ""} {
	toplevel $widget
	wm title $widget $name
    } else {
	${ttkPrefix}frame $parent$widget
	if {!$noPack} {
	    eval pack $parent$widget $packOption
	}
    }

    ${ttkPrefix}frame $parent$widget.userFrame -relief raised -borderwidth 2 -width $width \
      -height $height
    pack $parent$widget.userFrame -side top -fill both -expand 1

    ${ttkPrefix}frame $parent$widget.buttonRow -relief raised -borderwidth 2
    pack $parent$widget.buttonRow -side bottom -fill x

    if $okButton {
        APSButton .ok -parent $parent$widget.buttonRow -text OK -width "" \
          -command "destroy $parent$widget; $okCommand" -highlight 1 -contextHelp \
          "Done interacting with this dialog box. Remove window." -size $buttonSize
    }
    if $cancelButton {
        APSButton .cancel -parent $parent$widget.buttonRow -text Cancel -width "" \
            -command "destroy $parent$widget; $cancelCommand" -contextHelp \
            "Cancel interaction with dialog box. Remove window." -size $buttonSize
    }
    if {$modal == 1} {
        set grabList [grab current $parent$widget]
        focus $parent$widget
        catch {grab $parent$widget}
        tkwait window $parent$widget
        catch {grab set $grabList}
    }
}

proc APSDialogBoxAddButtonHelp {} {
    return "Usage: APSDialogBoxAddButton widget \n\[-parent <string>\]\t\n\[-text <string>\]\t\n\[-command <string>\]\t\n\[-contextHelp <string>\]\t\n\[-size <size>\]\t\n\nCreates\t\$parent.buttonRow.\$widget"
}
proc APSDialogBoxAddButton {widget args} {
    set parent ""
    set text "NoName"
    set command ""
    set contextHelp ""
    set size medium
    set width ""
    set packOption "-side left -padx 1 -pady 1"
    APSStrictParseArguments {parent text command contextHelp size width packOption}
    APSButton $widget -parent $parent.buttonRow -text $text \
      -command $command -contextHelp $contextHelp -size $size \
      -width $width -packOption $packOption
}
    
proc APSWindowHelp {} {
    return "Usage: APSWindow widget \n\[-parent <string>\]\t\n\[-noPack 1\]\t\n\[-packOption <list>\]\t\n\[-name <string>\]\t\n\[-contextHelp <string\]\n\t\[-closeButton 0\]\n\t\nCreates\t\$parent\$widget.\n\tuserFrame\n\tbuttonRow.close.button\n"
}

proc APSWindow {widget args} {
    global apsContextHelp apsttk
    set parent ""
    set noPack 0
    set packOption "-side top -fill x"
    set name "NoName"
    set contextHelp ""
    set closeButton 1
    APSStrictParseArguments {parent name noPack packOption contextHelp closeButton}

    if {[info exists apsttk] && ($apsttk != 0)} {
        set ttkPrefix ttk::
    } else {
        set ttkPrefix ""
    }
    if {$contextHelp != ""} {
	set apsContextHelp($parent$widget) $contextHelp
    }

    set windowExists [winfo exists $parent$widget]
    if {$parent == ""} {
	if {$windowExists} {
	    raise $widget
	} else {
	    toplevel $widget
	    wm title $widget $name
	}
    } else {
	${ttkPrefix}frame $parent$widget
	if {!$noPack} {
	    eval pack $parent$widget $packOption
	}
    }

    if {!$windowExists} {
	${ttkPrefix}frame $parent$widget.userFrame -relief raised -borderwidth 2 \
	  -width 200 -height 100
	pack $parent$widget.userFrame -side top -fill both -expand 1
	
	${ttkPrefix}frame $parent$widget.buttonRow -relief raised -borderwidth 2
	pack $parent$widget.buttonRow -side bottom -fill x
	if {$closeButton} {
            APSButton .close -parent $parent$widget.buttonRow -text Close \
              -command "destroy $parent$widget" -highlight 1 -contextHelp \
              "Close Window." -width ""
        }
    }
}

proc APSEMailDialogHelp {} {
    return "Usage: APSEMailDialog widget \n\[-message <string>\]\t\n\[-address <string>\]\t\n\[-modal 1\]\t\n\[-width <number>\]\t\n"
}

proc APSEMailDialog {widget args} {
    global apsContextHelp apsEntryBackground apsEMailDialogAddress apsEMailDialogSubject
    set message "no message"
    if {[info exists apsEMailDialogAddress]} {
	set address $apsEMailDialogAddress
    } else {
	set address ""
    }
    set modal 0
    set width 80
    APSStrictParseArguments {message address modal width}
    if {[llength $address]} {
        set apsEMailDialogAddress $address
    }
    set message [regsub -all {[^[:print:]\r\n]+} $message ?]
    
    set dialogExists [winfo exists $widget]
    if {$dialogExists} {
	raise $widget
	$widget.userFrame.message.frame.text delete 1.0 end
	$widget.userFrame.message.frame.text insert end $message
    } else {
	APSDialogBox $widget -name "E-Mail Dialog"
	APSLabeledEntry .address -parent $widget.userFrame -label \
	  "Send To:" -textVariable apsEMailDialogAddress \
	  -width 30
	APSLabeledEntry .subject -parent $widget.userFrame -label \
	  "Subject:" -textVariable apsEMailDialogSubject \
	  -width 30

	APSFrame .commentTitle -parent $widget.userFrame -label \
	  "Enter Any Comments Here:" -packOption "-side top -expand 1 -fill both"
	APSScrolledText .commentMessage -width $width -height 5 -parent \
	  $widget.userFrame.commentTitle.frame
	set top $widget.userFrame.commentTitle.frame.commentMessage

	APSFrame .messageTitle  -parent $widget.userFrame -label \
	  "Message Body (editable):" -packOption "-side top -expand 1 -fill both"
	APSScrolledText .message -width $width -height 10 -parent \
	  $widget.userFrame.messageTitle.frame
	set bottom $widget.userFrame.messageTitle.frame.message

	$bottom.text insert end $message

	$widget.buttonRow.ok.button configure -command \
	  "APSEMailDialogOK $widget $top.text $bottom.text"
	APSEnableButton $widget.buttonRow.ok.button
    }
    if {$modal == 1} {
	set grabList [grab current $widget]
	focus $widget
	catch {grab $widget}
	tkwait window $widget
	catch {grab set $grabList}
    }
}

proc APSEMailDialogOK {dialogWidget commentsWidget messageWidget} {
    global apsEMailDialogAddress apsEMailDialogSubject

    set comments [$commentsWidget get 1.0 end]
    
    set message [$messageWidget get 1.0 end]

    set body "$comments \n------------------------------------------------------------\n$message\n"
    set body [regsub -all "{" $body ""]
    set body [regsub -all "}" $body ""]
    
    APSSendEMail -address $apsEMailDialogAddress -message "$body" -subject \
      $apsEMailDialogSubject
    destroy $dialogWidget
}


proc APSSaveDialogHelp {} {
    return "Usage: APSSaveDialog widget \n\[-text <string>\]\t\n\[-textWidget <string>\]\t\n\[-modal 1\]\t\n"
}

proc APSSaveDialog {widget args} {
    global apsContextHelp
    set text ""
    set textWidget ""
    set modal 0
    APSStrictParseArguments {text textWidget modal}

    set dialogExists [winfo exists $widget]
    if {$dialogExists} {
	raise $widget
    } else {
        if {$modal == 0} {
            global tk_version
            if {$tk_version == "8.5"} {
                if {[string length $textWidget]} {
                    set selection [tk_getSaveFile -parent $textWidget]
                } else {
                    set selection [tk_getSaveFile]
                }
            } else {
                if {[string length $textWidget]} {
                    set selection [tk_getSaveFile -confirmoverwrite 0 -parent $textWidget]
                } else {
                    set selection [tk_getSaveFile -confirmoverwrite 0]
                }
            }
        } else {
            set selection [APSFileSelectDialog $widget -checkValidity 0 -title \
                             "Save Text To File:" -contextHelp \
                             "Enter a file in which to save text" -modal $modal]
        }
	if {[string length $textWidget]} {
	    set text "${text}[$textWidget get 1.0 end]"
	}
	if {[string length $selection]} {
            if {[file exists $selection]} {
                set reply [tk_dialog .foo "File Exists" "Save file exists" \
                             questhead 0 Overwrite Append Cancel]
                if {$reply == 0} {
                    set mode w
                } elseif {$reply == 1} {
                    set mode a
                } else {
                    return
                }
            } else {
                set mode w
            }
	    if {[catch {open $selection $mode} fd]} {
		APSAlertBox .fa -errorMessage "$fd"
		return
	    } else {
		puts -nonewline $fd $text
		close $fd
		return
	    }
	}
    }    
}

