# dialog.tcl --
#
# This file defines the procedure tk_dialog, which creates a dialog
# box containing a bitmap, a message, and one or more buttons.
#
# Modified version by Wayne Christopher.
#
# $Header$ SPRITE (Berkeley)
#
# Copyright (c) 1992-1993 The Regents of the University of California.
# All rights reserved.
#
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the
# above copyright notice and the following two paragraphs appear in
# all copies of this software.
#
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
# CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
# ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#

#
# tk_dialog:
#
# This procedure displays a dialog box, waits for a button in the dialog
# to be invoked, then returns the index of the selected button.
#
# Arguments:
# w -		Window to use for dialog top-level.
# title -	Title to display in dialog's decorative frame.
# text -	Message to display in dialog.
# bitmap -	Bitmap to display in dialog (empty string means none).
# default -	Index of button that is to display the default ring
#		(-1 means none).
# args -	One or more strings to display in buttons across the
#		bottom of the dialog box.

if {[info commands option] != ""} {
    
    proc tk_dialog {w title text bitmap default args} {
	global tk_priv ic_dialog
	global form_label_text toplevel tk_dialog_no_transient
	
	set oldgrab [grab current]
	if {$oldgrab != ""} { grab release $oldgrab }
	
	if [info exists form_label_text($title)] { set title $form_label_text($title) }
	if [info exists form_label_text($text)] { set text $form_label_text($text) }
	set nargs ""
	foreach a $args {
	    if [info exists form_label_text($a)] { set a $form_label_text($a) }
	    lappend nargs $a
	}
	set args $nargs
	# 1. Create the top-level window and divide it into top
	# and bottom parts.
	
	if [info exists $w] { error "dialog created twice" }
	catch {top_destroy $w}
	# see 48049: must return -1 and change tk_priv(button) on "window destroy"
	set tk_priv(button) ""
	set del_proc "
		global tk_priv;
		if ![llength $tk_priv(button)] {set tk_priv(button) -1};
                list 0"
	top_window $w 0 $title $del_proc "" 2
	#
	# It should not do this all the time - there should be some 
	# option to turn this on since most code will not handle it.
	#top_window $w 0 $title "global tk_priv; set tk_priv(button) -1"
	frame $w.top -relief raised -bd 1
	pack $w.top -side top -fill both
	frame $w.bot -relief raised -bd 1
	pack $w.bot -side bottom -fill both
	
	if {[info exists ic_dialog(aspect)] && [string is integer $ic_dialog(aspect)] && $ic_dialog(aspect) > 400} {
	    set aspect $ic_dialog(aspect)
	} else {
	    set aspect 400
	}
	
	# 2. Fill the top part with bitmap and message.
        if {$text != ""} {
	   message $w.msg -text $text -aspect $aspect
	   pack $w.msg -in $w.top -side right -expand 1 -fill both -padx 5m -pady 5m
        }
	if {($bitmap != "") && ([string index $bitmap 0] != "+") &&
			([string index $bitmap 0] != "-")} {
	    label $w.bitmap -bitmap $bitmap
	    pack $w.bitmap -in $w.top -side left -padx 5m -pady 5m
	}
	
	# 3. Create a row of buttons at the bottom of the dialog.
	
	set i 0
	foreach but $args {
	    button $w.button$i -text $but -command "set tk_priv(button) $i" \
		-background gray80 -activebackground gray70
            if {$i == $default} {
                $w.button$i configure -default active
                bind $w <Return> "$w.button$i flash; set tk_priv(button) $i"
            } else {
                $w.button$i configure -default normal
            }

	    pack $w.button$i -in $w.bot -side left -expand 1 \
		-padx 3m -pady 3m -ipadx 2m -ipady 1m
	    
	    incr i
	}
	
	# 4. Withdraw the window, then update all the geometry information
	# so we know how big it wants to be, then center the window in the
	# display and de-iconify it.
	update idletasks
	if {([string index $bitmap 0] == "+") || \
		([string index $bitmap 0] == "-")} {
	    top_geometry $w $bitmap
	    top_deiconify $w
	} else {
	    set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
		       - [winfo vrootx [winfo parent $w]]]
	    set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
		       - [winfo vrooty [winfo parent $w]]]  
        set wx [expr $x + [winfo rootx .]]
        
	    top_geometry $w +$wx+$y
	    top_deiconify $w
	}
	if {![info exists toplevel(single)] || !$toplevel(single) && ![info exists tk_dialog_no_transient]} {
	    if {[winfo exists .v] && ([winfo class .v] == "Toplevel")} {
		set p .v
	    } else {
		set p [winfo parent $w]
	    }
	    wm transient $w $p
	    if {[info exists ic_dialog(topmost)] && [string is integer $ic_dialog(topmost)]} {
	        catch { wm attributes $w -topmost $ic_dialog(topmost) }
	    }
	} else {
	    form_auto_raise $w
	}
	# 6. Wait for the user to respond, then restore the focus and
	# return the index of the selected button.
	set bb [catch {busy reset} ow]
    catch { wm deiconify $w }
	tkwait variable tk_priv(button)
	if !$bb { if $ow { busy $ow } }
    if {$tk_priv(button) >= 0} {
        set saved $tk_priv(button)
	    top_destroy $w
        set tk_priv(button) $saved
    }
	if {$oldgrab != ""} { grab $oldgrab }
	return $tk_priv(button)
    }
    
} else {
    
    proc tk_dialog {w title text bitmap default args} {
	if {$default < 0} { error "dialog default -1" }
	
	puts "$text\n>> [lindex $args $default]"
	
	return $default
    }
}

proc dialog2 {w title args} {
    global dialog2_value
    
    set geo ""
    set g [lindex $args 0] 
    if {[string match +* $g] || [string match -* $g]} {
	set geo $g
	set args [lrange $args 1 end]
    }
    if ![winfo exists $w] {
	top_window $w 0 $title 
	if {$geo == ""} {
	    update idletasks
	    set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
		       - [winfo vrootx [winfo parent $w]]]
	    set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
		       - [winfo vrooty [winfo parent $w]]]
	    set geo +$x+$y
	}
	top_geometry $w $geo
    }
    
    foreach b [winfo children $w] { destroy $b }
    
    set i 0
    foreach but $args {
	button $w.button$i -text $but -command "set dialog2_value $i" \
	    -background gray80 -activebackground gray70
	pack $w.button$i -side left -expand 1 \
	    -padx 3m -pady 3m -ipadx 2m -ipady 1m
	
	incr i
    }
    
    top_deiconify $w
    
    form_auto_raise $w
    
    tkwait variable dialog2_value
    top_destroy $w
    return $dialog2_value
}
