# jprint.tcl - print panels
#
###############################################################################
#  Copyright 1992-2001 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::*
  }
}

::jldb::set_defaults {
  {Print Print}
  {confirm:print_file... {Save output in file `$J_PREFS(printfile)'?}}
  {confirm:print... {Print to $J_PREFS(printer) using command `${print_cmd}'?}}
  {{Print PostScript} {Print PostScript}}
  {confirm:print_ps_file... {Save PostScript output in file `$J_PREFS(printfile)'?}}
  {confirm:print_ps... {Print PostScript to $J_PREFS(printer) using command `${print_cmd}'?}}
}
######################################################################
# ::aq::print ?options? - print panel
# options include
#   -title (default "Print")
#   -data (default {} - not really optional)
######################################################################

proc ::aq::print { args } {
  ::aq::parse_args {
    {title "Print"}
    {data ""}
  }
  
  global J_PREFS
  
  switch -exact $J_PREFS(printtype) {
    file {
      if {[::aq::confirm -priority 24 \
        -text [subst [= confirm:print_file...]]]} {
        ::aq::write_file $J_PREFS(printfile) $data
      }
      return
    }
    lpr {set print_cmd "lpr -P$J_PREFS(printer)"}
    lp {set print_cmd "lp -d $J_PREFS(printer)"}
    custom {set print_cmd [subst $J_PREFS(printcmd)]}
  }
  
  if {[::aq::confirm -priority 24 \
    -text [subst [= confirm:print...]]]} {
    eval exec $print_cmd << [list $data]
  }
}


######################################################################
# ::aq::print_ps ?options? - PostScript print panel
# options include
#   -title (default "Print")
#   -data (default {} - not really optional)
######################################################################

proc ::aq::print_ps { args } {
  ::aq::parse_args {
    {title "Print"}
    {data ""}
  }
  
  if {! [string match "%!*" $data]} {
    error {Data passed to ::aq::print_ps does not start with "%!"}
  }
  
  global J_PREFS
  
  switch -exact $J_PREFS(printtype) {
    file {
      if {[::aq::confirm -priority 24 \
        -text [subst [= confirm:print_ps_file...]]]} {
        ::aq::write_file $J_PREFS(printfile) $data
      }
      return
    }
    lpr {set print_cmd "lpr -P$J_PREFS(printer)"}
    lp {set print_cmd "lp -d $J_PREFS(printer)"}
    custom {set print_cmd [subst $J_PREFS(printcmd)]}
  }
  
  if {[::aq::confirm -priority 24 \
    -text [subst [= confirm:print_ps...]]]} {
    eval exec $print_cmd << [list $data]
  }
}

