# jconfirm.tcl - (optional) confirmation panel
#
######################################################################
# Copyright 1992-1999 by Jay Sekora.  This file may be freely        #
# distributed, modified or unmodified, for any purpose, provided     #
# that this copyright notice is retained verbatim in all copies and  #
# no attempt is made to obscure the authorship of this file.  If you #
# distribute any modified versions, I ask, but do not require, that  #
# you clearly mark any changes you make as such and that you provide #
# your users with instructions for getting the original sources.     #
######################################################################

package provide aq 0.1

# following has side-effect of making sure ::aq namespace is defined
namespace eval ::aq {
  namespace export {[a-z]*}
  catch {
    package require jldb
    # following likely to fail, because done by other libraries too
    namespace import ::jldb::shortcuts::*
  }
}

######################################################################
# global variables:
#
global J_PREFS env
::aq::default J_PREFS(autoposition) 0
::aq::default J_PREFS(confirm) 1
#
######################################################################


######################################################################
# ::aq::confirm ?options? - Cancel/OK dialogue box
# options include
#   -title (default "Confirm")
#   -text (default "Are you sure?")
#   -priority (default 0)
#   -yesbutton (default "OK")
#   -nobutton (default "Cancel")
# returns true (1) on OK; false (0) on Cancel
# if (priority == 0 && $J_PREFS(confirm) == 0), the dialogue box is
#   not displayed; it always returns 1
######################################################################

proc ::aq::confirm { args } {
  ::aq::parse_args {
    {title Confirm}
    {priority 0}
    {text "Are you sure?"}
    {yesbutton OK}
    {nobutton Cancel}
  }
  
  set title [= $title]
  set text [= $text]
  set yesbutton [::jldb::short_text $yesbutton]
  set nobutton [::jldb::short_text $nobutton]
  
  global confirm_result
  global J_PREFS			;# for J_PREFS(confirm)

  if { (! $J_PREFS(confirm)) && (! $priority) } {
    return 1
  }
  
  set old_focus [focus]			;# so we can restore original focus

  toplevel .confirm
  wm title .confirm $title
  
  message .confirm.msg -width 300 -anchor w -text $text
  ::aq::buttonbar .confirm.b -default ok -buttons [format {
    {ok {%s} {set confirm_result 1; destroy .confirm}}
    {cancel {%s} {set confirm_result 0; destroy .confirm}}
  } $yesbutton $nobutton]
  
  pack .confirm.msg -side top -fill both -expand yes -padx 10 -pady 10
  pack [::aq::rule .confirm -width 200] -side top -fill x
  pack .confirm.b -side bottom -fill x
  
  ::aq::dialogue .confirm		;# position in centre of screen

  focus .confirm
  ::aq::default_button .confirm.b.ok .confirm
  ::aq::cancel_button .confirm.b.cancel .confirm
  update
  grab .confirm
  tkwait window .confirm
  focus -force $old_focus		;# can't figure out a better way...
  return $confirm_result
}
