# jtclutils.tcl - general utilities that don't require Tk
#
######################################################################
# 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]*}
}

######################################################################
# ::aq::default var value - set var to value only if it's not
#   set already, returning $var
######################################################################

proc ::aq::default { var value } {
  if {![uplevel 1 [list info exists $var]]} {
    uplevel 1 [list set $var $value]
  }
  return [uplevel 1 [list set $var]]
}

######################################################################
# ::aq::debug - record debugging information, if desired
######################################################################

proc ::aq::debug args {}

proc ::aq::debug { {string {::aq::debug called}} } {
  global env J_PREFS
  global AQSYSINFO
  if {![info exists env(JSTOOLS_DEBUGGING)]} {return}
  
  if {![info exists J_PREFS(scrollbarside)]} {set J_PREFS(scrollbarside) right}
  set w .debug_log
  
  if { ! [winfo exists $w] } {
    toplevel $w
    wm minsize . 10 10
    text $w.t -yscrollcommand "$w.sb set"
    if {!$AQSYSINFO(tinyscreen)} {
      $w.sb configure -setgrid true
    }
    scrollbar $w.sb -command "$w.t yview"
    pack $w.sb $w.t -side $J_PREFS(scrollbarside) -fill y
  }
  
  set level [expr [info level] - 1]
  $w.t insert end "$string\n"
  switch -exact -- $level {
    0 {
      $w.t insert end "in top level.\n"
    }
    1 {
      $w.t insert end "in `[info level -1]'.\n"
    }
    default {
      $w.t insert end "in `[info level -1]'\n"
      $w.t insert end "  called from `[info level -2]'.\n"
    }
  }
  $w.t yview -pickplace end
}
