#!/usr/local/bin/wish4.2
#
# Copyright 1992-1996 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.
## begin boiler_header

if {[info exists env(JSTOOLS_LIB)]} {
  set jstools_library $env(JSTOOLS_LIB)
} else {
  set jstools_library /home/js/jstools/lib
}

# add the jstools library to the library search path:

set auto_path [concat [list $jstools_library] $auto_path]

# check for ~/.tk and prepend it to the auto_path if it exists.
# that way the user can override and customise the jstools libraries.

if {[file isdirectory ~/.tk]} then {
  set auto_path [concat [list [glob ~/.tk]] $auto_path]
}

## end boiler_header



wm withdraw .

j:jstools_init jfontsel

######################################################################
# GUI to select a (single) font
######################################################################

proc j:prompt_font { args } {
  j:parse_args {
    {prompt "Font:"}
    {pattern "*"}
  }
  
  global J_PREFS
  global JSTOOLS
  set JSTOOLS(font) {}
  
  toplevel .font
  entry .font.pattern -textvariable JSTOOLS(font_pattern)
  set JSTOOLS(font_pattern) $pattern
  
  frame .font.mid
  listbox .font.mid.lb -width 80 -yscrollcommand ".font.mid.ysb set"
  scrollbar .font.mid.ysb -command ".font.mid.lb yview"
  pack .font.mid.ysb -fill y -side $J_PREFS(scrollbarside)
  pack .font.mid.lb -expand 1 -fill both -side $J_PREFS(scrollbarside)
  
  entry .font.font -textvariable JSTOOLS(font)
  
  j:buttonbar .font.b -buttons {
    {
      ok OK {destroy .font}
    }
    {
      rescan Rescan {j:prompt_font:redisplay}
    }
    {
      preview Preview {j:prompt_font:preview}
    }
  }
  
  pack .font.pattern -side top -fill x
  pack .font.mid -expand y -side top -fill both
  pack .font.font -side top -fill x
  pack .font.b -side top -fill x
  
  bind .font.pattern <Any-Key-Return> {
    j:prompt_font:redisplay
  }
  bind .font.mid.lb <Any-ButtonRelease-1> {
    set JSTOOLS(font) [%W get [%W curselection]]
  }
  bind .font.mid.lb <Double-ButtonRelease-1> {
    destroy .font
    break
  }
  bind .font.font <Any-Key-Return> {
    destroy .font
    break
  }
  
  j:prompt_font:redisplay
  
  tkwait window .font
  return $JSTOOLS(font)
}

######################################################################

proc j:prompt_font:redisplay {} {
  .font.mid.lb delete 0 end
  set fonts [split [exec xlsfonts -fn [.font.pattern get]] \n]
  eval .font.mid.lb insert end $fonts
}

######################################################################

proc j:prompt_font:preview {} {
  set w .font_preview
  catch {destroy $w}
  toplevel $w
  
  text $w.t -width 60 -height 11
  
  set font [.font.font get]
  
  if [catch {$w.t configure -font $font}] {
    destroy $w
    j:alert -text "Error: invalid font"
  } else {
    $w.t insert end {
    !"#$%&'()*+,-./0123456789:;<=>?
   @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
   `abcdefghijklmnopqrstuvwxyz{|}~
    
   
   

   The quick brown fox jumps over the lazy dog.
   THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
}
  }
  
  pack $w.t -in $w
}

######################################################################

#j:prompt_font -pattern *-10-100-75-75-*
#while 1 {
#  puts [j:prompt_font]
#}

puts [j:prompt_font -pattern *-75-75-*]
exit 0





