#f dialog_NEW.tcl : quelques procédures courantes de gestion des dialogues

package provide prompt 0.2


#f   Ces procédures sont extraites de la librairie tk.tcl et adaptée 
#f   à stead.
#f   BUT créer (à terme) une procédure simple et souple pour gérer les 
#f   dialogue (avec ou sans focus...)


# 
# fournit Prompt_Box 
#
# COMMENTAIRES A FAIRE !
# A FAIRE : (REFAIRE...) les Bindings(?) + Multifenetre ?
# 
########################################################################
# REMARQUE de diam le 23/08/00
# 
# modifier Prompt_Box et les autres boites en general en utilisant 
# ces nouvelles procedures  (extraite et adaptées de tk.tcl):
# 
# Extrait d'un boite de dialogue les utilisant :
# 
#     # 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.
# 
#     xtk_placeWindow $w widget $data(-parent)
#     wm title $w $data(-title)
# 
#     # Set a grab and claim the focus too.
# 
#     xtk_setFocusGrab $w $data(ent)
#     $data(ent) delete 0 end
#     $data(ent) insert 0 $data(selectPath)
#     $data(ent) selection range 0 end
#     $data(ent) icursor end
# 
#     # Wait for the user to respond, then restore the focus and
#     # return the index of the selected button.  Restore the focus
#     # before deleting the window, since otherwise the window manager
#     # may take the focus away so we can't redirect it.  Finally,
#     # restore any grab that was in effect.
# 
#     tkwait variable tkPriv(selectFilePath)
# 
#     xtk_restoreFocusGrab $w $data(ent) withdraw
# 
#     # Cleanup traces on selectPath variable
#     #


# xtk_placeWindow --
#   place a toplevel at a particular position
# Arguments:
#   toplevel	name of toplevel window
#   ?placement?	pointer ?center? ; places $w centered on the pointer
#		widget widgetPath ; centers $w over widget_name
#		defaults to placing toplevel in the middle of the screen
#   ?anchor?	center or widgetPath
# Results:
#   Returns nothing
#
proc xtk_placeWindow {w {place ""} {anchor ""}} {
    wm withdraw $w
    update idletasks
    set checkBounds 1
    if {[string equal -len [string length $place] $place "pointer"]} {
	## place at POINTER (centered if $anchor == center)
	if {[string equal -len [string length $anchor] $anchor "center"]} {
	    set x [expr {[winfo pointerx $w]-[winfo reqwidth $w]/2}]
	    set y [expr {[winfo pointery $w]-[winfo reqheight $w]/2}]
	} else {
	    set x [winfo pointerx $w]
	    set y [winfo pointery $w]
	}
    } elseif {[string equal -len [string length $place] $place "widget"] && \
	    [winfo exists $anchor] && [winfo ismapped $anchor]} {
	## center about WIDGET $anchor, widget must be mapped
	set x [expr {[winfo rootx $anchor] + \
		([winfo width $anchor]-[winfo reqwidth $w])/2}]
	set y [expr {[winfo rooty $anchor] + \
		([winfo height $anchor]-[winfo reqheight $w])/2}]
    } else {
	set x [expr {([winfo screenwidth $w]-[winfo reqwidth $w])/2}]
	set y [expr {([winfo screenheight $w]-[winfo reqheight $w])/2}]
	set checkBounds 0
    }
    if {$checkBounds} {
	if {$x < 0} {
	    set x 0
	} elseif {$x > ([winfo screenwidth $w]-[winfo reqwidth $w])} {
	    set x [expr {[winfo screenwidth $w]-[winfo reqwidth $w]}]
	}
	if {$y < 0} {
	    set y 0
	} elseif {$y > ([winfo screenheight $w]-[winfo reqheight $w])} {
	    set y [expr {[winfo screenheight $w]-[winfo reqheight $w]}]
	}
    }
    wm geometry $w +$x+$y
    wm deiconify $w
}

## xtk_setFocusGrab --
#   swap out current focus and grab temporarily (for dialogs)
# Arguments:
#   grab	new window to grab
#   focus	window to give focus to
# Results:
#   Returns nothing
# 
# modif 
#   25/09/01 (diam) suppression du grab (EN FAIRE UNE OPTION)
#   25/09/01 (diam) correction d'un bug tk8.4a2 !
#          rajout upvar xtk(FocusGrab,$index) data dans xtk_restoreFocusGrab
# 
# 
proc xtk_setFocusGrab {grab {focus {}}} {
    set index "$grab,$focus"
    upvar xtk(FocusGrab,$index) data

    lappend data [focus]
    set oldGrab [grab current $grab]
    lappend data $oldGrab
    if {[winfo exists $oldGrab]} {
	lappend data [grab status $oldGrab]
    }

    # 25/09/01 (diam) suppression du grab (EN FAIRE UNE OPTION)
    # 
    # The "grab" command will fail if another application
    # already holds the grab.  So catch it.
    # 
    # # # # # catch {grab $grab}
    
    if {[winfo exists $focus]} {
	focus $focus
    }
}

## xtk_restoreFocusGrab --
#   restore old focus and grab (for dialogs)
# Arguments:
#   grab	window that had taken grab
#   focus	window that had taken focus
#   destroy	destroy|withdraw - how to handle the old grabbed window
# Results:
#   Returns nothing
#
proc xtk_restoreFocusGrab {grab focus {destroy destroy}} {
    set index "$grab,$focus"

    # 25/09/01 (diam) correction d'un bug tk8.4a2 !
    # # foreach {oldFocus oldGrab oldStatus} [set xtk(FocusGrab,$index)] |
    # #         { break }
    # # unset xtk(FocusGrab,$index)
    # 
    upvar xtk(FocusGrab,$index) data
    foreach {oldFocus oldGrab oldStatus} $data { break }
    unset data

    catch {focus $oldFocus}
    
    # 25/09/01 (diam) suppression du grab (EN FAIRE UNE OPTION)
    # # # # # grab release $grab
    
    if {[string equal $destroy "withdraw"]} {
	wm withdraw $grab
    } else {
	destroy $grab
    }
    if {[winfo exists $oldGrab] && [winfo ismapped $oldGrab]} {
	if {[string equal $oldStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
}
# ./
