# jinit.tcl - common application initialisation procedures
#
###############################################################################
#  Copyright 1992-2002 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 AQSYSINFO env
if {! [info exists J_PREFS(autoposition)]} {set J_PREFS(autoposition) 0}
if {! [info exists J_PREFS(confirm)]} {set J_PREFS(confirm) 1}
if {! [info exists AQSYSINFO(tinyscreen)]} {set AQSYSINFO(tinyscreen) 1}
#
######################################################################


######################################################################
# ::aq::aq_init - common initialisation for aq
######################################################################

proc ::aq::aq_init { {app aq} } {
  if {[catch {package require Tk 8.0}]} {
    tkerror {This application requires at least Tk 8.0.}
    exit 1
  }
  
  package require jldb
  
  global J_PREFS			;# user preferences
  global AQSYSINFO			;# info about current host/display
  global aq_pkg				;# aqtools package directory
  
  global JSTOOLS_VERSION
  set JSTOOLS_VERSION {0.2002.01.26}
  
  global tk_library
  global env
  global NAME HOME
  
  set NAME {}
  set HOME /
  
  if {![info exists env(USER)]} {
    if [info exists env(LOGNAME)] {
      set env(USER) $env(LOGNAME)
    } else {
      set env(USER) [file tail $env(HOME)]
    }
  }
  catch {set NAME $env(USER)}
  catch {set HOME $env(HOME)}
  
  ::aq::wm_client			;# set client hostname
  ::aq::wm_command			;# set client command string
  
  j:pref:create_global_prefs		;# set up standard global prefs
  j:read_global_prefs			;# read standard aq preferences
  ::jldb::init aqtools $J_PREFS(language) [file join $aq_pkg ldb]
  					;# read in language database for library
  j:pref:create_global_prefs		;# again with language set (inefficient)
  
  # check for tiny screen (eg PDA) - if either dimension is less than 400
  # pixels, adjust UI for tiny screen.
  # THIS SHOULD BE CONTROLLED BY A USER PREFERENCE
  
  set AQSYSINFO(tinyscreen) 0
  set AQSYSINFO(main_screenwidth) [winfo screenwidth .]
  set AQSYSINFO(main_screenheight) [winfo screenheight .]
  set AQSYSINFO(min_width) 0
  set AQSYSINFO(min_height) 0
  
  if {$AQSYSINFO(main_screenwidth) <= 400} {
    set AQSYSINFO(tinyscreen) 1
  } elseif {$AQSYSINFO(main_screenheight) <= 400} {
    set AQSYSINFO(tinyscreen) 1
  }
  
  # check for an environment variable to control small-screen behaviour
  # (this is useful for testing)
  
  if {[info exists env(AQTINYSCREEN)]} {
    set AQSYSINFO(tinyscreen) $env(AQTINYSCREEN)
  }
  
  if {$AQSYSINFO(tinyscreen)} {
    set AQSYSINFO(min_width) $AQSYSINFO(main_screenwidth)
    if {$AQSYSINFO(main_screenwidth) > 400} {
      set AQSYSINFO(min_width) 240
    }
    set AQSYSINFO(min_height) $AQSYSINFO(main_screenheight)
    if {$AQSYSINFO(main_screenheight) > 400} {
      set AQSYSINFO(min_height) 320
    }
    
    option add *Font {Helvetica -8} widgetDefault
    option add *Text.Font {Fixed -8} widgetDefault
    option add *Text.Spacing1 0 widgetDefault
    option add *Text.Spacing2 0 widgetDefault
    option add *Text.Spacing3 0 widgetDefault
    option add *Text.background white widgetDefault
    option add *Filler.width 0 startupFile
    option add *Filler.height 0 startupFile
    option add *Entry.Font {Courier -8} widgetDefault
    option add *Scrollbar.width 7 widgetDefault
    option add *BorderWidth 1 widgetDefault
    option add *padX 1 startupFile
    option add *padY 1 startupFile
  }
   
  j:source_config aqtoolsrc.tcl		;# cross-application customization
  catch {				;# read cross-application X defaults:
    option readfile [file join $HOME .tk aqtools.ad] userDefault
  }
  if {"x$app" != "xaqtools"} {		;# application-specific initialisation
    ::jldb::init $app $J_PREFS(language) [file join $aq_pkg ldb]
  					;# read in language database for app
    catch {				;# read application-specific X defaults:      option readfile [file join $HOME .tk $app.ad] userDefault
    }
    ###					;# could read preferences here?
  }
  
  # following use users prefs, bindings files:
  j:tb:init Text			;# uses prefs, ~/.tk/textbindings.tcl
  j:eb:init Entry			;# uses prefs, ~/.tk/entrybindings.tcl
  
  ::aq::command_register \
    ::aq::cmd::prompt_tcl {Issue Tcl Command...}
  ::aq::command_register \
    ::aq::cmd::prompt_unix {Issue Unix Command...}
  ::aq::command_register \
    ::aq::cmd::global_pref_panel {Global Preferences...}
  
  ::aq::command_register j:cmd:text_cut {Cut}
  ::aq::command_register j:cmd:text_copy {Copy}
  ::aq::command_register j:cmd:text_paste {Paste}
  
  # read in site-specific defaults (optional)
  catch {
    ::aq::site_init $app
  }
}



